Class: Webhookdb::DBAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/webhookdb/db_adapter.rb

Direct Known Subclasses

PG, Snowflake

Defined Under Namespace

Modules: ColumnTypes, DefaultSql Classes: Column, Connection, Index, InvalidIdentifier, PG, Schema, SequelConnection, Snowflake, Table, TableDescriptor, UnsupportedAdapter

Constant Summary collapse

VALID_IDENTIFIER =
/^[a-zA-Z][a-zA-Z\d_ ]*$/
INVALID_IDENTIFIER_PROMPT =
"Identifiers must start with a letter and contain only letters, numbers, spaces, and underscores.\n" \
"See https://docs.webhookdb.com/concepts/valid-identifiers/ for rules\n" \
"about identifiers like schema, table, and column names."
INVALID_IDENTIFIER_MESSAGE =
INVALID_IDENTIFIER_PROMPT.tr("\n", " ")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameSymbol

Returns:

  • (Symbol)


# File 'lib/webhookdb/db_adapter.rb', line 76

#tableTable

Returns:



# File 'lib/webhookdb/db_adapter.rb', line 76

#targetsArray<Column>

Returns:



# File 'lib/webhookdb/db_adapter.rb', line 76

#uniqueBoolean

Returns:

  • (Boolean)


# File 'lib/webhookdb/db_adapter.rb', line 76

Class Method Details

.adapter(url) ⇒ Webhookdb::DBAdapter

Parameters:

  • url (String)

Returns:



195
196
197
198
199
200
201
202
203
204
# File 'lib/webhookdb/db_adapter.rb', line 195

def self.adapter(url)
  case url
    when /^postgres/
      return Webhookdb::DBAdapter::PG.new
    when /^snowflake/
      return Webhookdb::DBAdapter::Snowflake.new
    else
      raise UnsupportedAdapter, "no adapter available for #{url}"
  end
end

.supported_adapters_messageObject



206
207
208
# File 'lib/webhookdb/db_adapter.rb', line 206

def self.supported_adapters_message
  return "Postgres (postgres://), SnowflakeDB (snowflake://)"
end

.valid_identifier?(s) ⇒ Boolean

Returns:

  • (Boolean)


210
# File 'lib/webhookdb/db_adapter.rb', line 210

def self.valid_identifier?(s) = VALID_IDENTIFIER.match?(s)

.validate_identifier!(s, type:) ⇒ Object

Raise if the identifier s is invalid according to VALID_IDENTIFIER. type is used in the error message, like ‘Sorry, this is not a valid table name.’ If the user tries SQL injection, let them know we noticed!

Raises:



215
216
217
218
219
220
# File 'lib/webhookdb/db_adapter.rb', line 215

def self.validate_identifier!(s, type:)
  return if self.valid_identifier?(s)
  msg = "Sorry, this is not a valid #{type} name. #{INVALID_IDENTIFIER_MESSAGE}"
  msg += " And we see you what you did there ;)" if s.include?(";") && s.downcase.include?("drop")
  raise InvalidIdentifier, msg
end

Instance Method Details

#add_column_sql(table, column, if_not_exists: false) ⇒ String

Parameters:

  • table (Table)
  • column (Column)
  • if_not_exists (Boolean) (defaults to: false)

Returns:

  • (String)

Raises:

  • (NotImplementedError)


171
172
173
# File 'lib/webhookdb/db_adapter.rb', line 171

def add_column_sql(table, column, if_not_exists: false)
  raise NotImplementedError
end

#connection(url) ⇒ Connection

Return a new Connection for the adapter. By default, return a SequelConnection, but adapters not using Sequel will need their own type.

Returns:



141
142
143
# File 'lib/webhookdb/db_adapter.rb', line 141

def connection(url)
  return SequelConnection.new(url)
end

#create_index_sql(index, concurrently:) ⇒ String

Parameters:

Returns:

  • (String)

Raises:

  • (NotImplementedError)


163
164
165
# File 'lib/webhookdb/db_adapter.rb', line 163

def create_index_sql(index, concurrently:)
  raise NotImplementedError
end

#create_schema_sql(schema, if_not_exists: false) ⇒ String

Parameters:

  • schema (Schema)
  • if_not_exists (Boolean) (defaults to: false)

Returns:

  • (String)

Raises:

  • (NotImplementedError)


148
149
150
# File 'lib/webhookdb/db_adapter.rb', line 148

def create_schema_sql(schema, if_not_exists: false)
  raise NotImplementedError
end

#create_table_sql(table, columns, schema: nil, if_not_exists: false) ⇒ String

Parameters:

  • table (Table)
  • columns (Array<Column>)
  • schema (Schema) (defaults to: nil)
  • if_not_exists (Boolean) (defaults to: false)

Returns:

  • (String)

Raises:

  • (NotImplementedError)


157
158
159
# File 'lib/webhookdb/db_adapter.rb', line 157

def create_table_sql(table, columns, schema: nil, if_not_exists: false)
  raise NotImplementedError
end

#merge_from_csv(connection, file, table, pk_col, copy_columns) ⇒ Object

Given a table and a (temporary) file with CSV data, import it into the table. Usually this is a COPY INTO command. For PG it would read from stdin, for Snowflake it would have to stage the file.

Parameters:

  • connection (Connection)
  • file (File)
  • table (Table)
  • pk_col (Column)

    Use this to identifier the same row between source and destination.

  • copy_columns (Array<Column>)

    All columns to copy. NOTE: This includes the pk column, since it should be copied, as we depend on it persisting.

Raises:

  • (NotImplementedError)


185
186
187
# File 'lib/webhookdb/db_adapter.rb', line 185

def merge_from_csv(connection, file, table, pk_col, copy_columns)
  raise NotImplementedError
end

#verify_connection(url, timeout: 2, statement: "SELECT 1") ⇒ Object



189
190
191
# File 'lib/webhookdb/db_adapter.rb', line 189

def verify_connection(url, timeout: 2, statement: "SELECT 1")
  return self._verify_connection(url, timeout:, statement:)
end