Class: Webhookdb::DBAdapter
- Inherits:
-
Object
- Object
- Webhookdb::DBAdapter
show all
- Defined in:
- lib/webhookdb/db_adapter.rb
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
-
#add_column_sql(table, column, if_not_exists: false) ⇒ String
-
#connection(url) ⇒ Connection
Return a new Connection for the adapter.
-
#create_index_sql(index, concurrently:) ⇒ String
-
#create_schema_sql(schema, if_not_exists: false) ⇒ String
-
#create_table_sql(table, columns, schema: nil, if_not_exists: false) ⇒ String
-
#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.
-
#verify_connection(url, timeout: 2, statement: "SELECT 1") ⇒ Object
Instance Attribute Details
#name ⇒ Symbol
|
# File 'lib/webhookdb/db_adapter.rb', line 76
|
|
# File 'lib/webhookdb/db_adapter.rb', line 76
|
#targets ⇒ Array<Column>
|
# File 'lib/webhookdb/db_adapter.rb', line 76
|
#unique ⇒ Boolean
|
# File 'lib/webhookdb/db_adapter.rb', line 76
|
Class Method Details
.supported_adapters_message ⇒ Object
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
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!
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
171
172
173
|
# File 'lib/webhookdb/db_adapter.rb', line 171
def add_column_sql(table, column, if_not_exists: false)
raise NotImplementedError
end
|
Return a new Connection for the adapter. By default, return a SequelConnection, but adapters not using Sequel will need their own type.
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
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
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
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.
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
|