Module: ActiveRecord::ConnectionAdapters::SnowflakeOdbc::SchemaStatements
- Defined in:
- lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb
Overview
:nodoc:
Constant Summary collapse
- SQL_NO_NULLS =
ODBC constants missing from Christian Werner’s Ruby ODBC driver
0
- SQL_NULLABLE =
1
- SQL_NULLABLE_UNKNOWN =
2
Instance Method Summary collapse
- #column_definitions(table_name) ⇒ Object
- #data_sources ⇒ Object
-
#native_database_types ⇒ Object
Returns a Hash of mappings from the abstract data types to the native database types.
- #new_column_from_field(table_name, field, _definitions) ⇒ Object
- #primary_key(table_name) ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
Renames a column in a table.
- #rename_table(table_name, new_name, **options) ⇒ Object
-
#table_exists?(table_name) ⇒ Boolean
Checks to see if the table
table_name
exists on the database. -
#tables(_name = nil) ⇒ Object
Returns an array of table names, for database tables visible on the current connection.
-
#views(_name = nil) ⇒ Object
Returns an array of view names, for database views visible on the current connection.
Instance Method Details
#column_definitions(table_name) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 58 def column_definitions(table_name) stmt = @raw_connection.columns(native_case(table_name.to_s)) result = stmt.fetch_all || [] stmt.drop # Column can return some technical columns ::SnowflakeOdbcAdapter::Snowflake.column_filters(result) end |
#data_sources ⇒ Object
12 13 14 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 12 def data_sources tables | views end |
#native_database_types ⇒ Object
Returns a Hash of mappings from the abstract data types to the native database types. See TableDefinition#column for details on the recognized abstract data types.
19 20 21 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 19 def native_database_types @native_database_types ||= ::SnowflakeOdbcAdapter::ColumnMetadata.new(self).native_database_types end |
#new_column_from_field(table_name, field, _definitions) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 66 def new_column_from_field(table_name, field, _definitions) col_name = field[3] SnowflakeOdbc::Column.new( format_case(col_name), # SQLColumns: COLUMN_NAME, field[12], # SQLColumns: COLUMN_DEF, (field), nullability(field[17], field[10]) ) end |
#primary_key(table_name) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 76 def primary_key(table_name) stmt = @raw_connection.primary_keys(native_case(table_name.to_s)) result = stmt.fetch_all || [] stmt&.drop result[0] && format_case(result[0][3]) end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
Renames a column in a table.
92 93 94 95 96 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 92 def rename_column(table_name, column_name, new_column_name) # :nodoc: clear_cache! execute("ALTER TABLE #{quote_table_name(table_name)} #{rename_column_sql(table_name, column_name, new_column_name)}") end |
#rename_table(table_name, new_name, **options) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 83 def rename_table(table_name, new_name, **) validate_table_length!(new_name) unless [:_uses_legacy_table_name] clear_cache! schema_cache.clear_data_source_cache!(table_name.to_s) schema_cache.clear_data_source_cache!(new_name.to_s) execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}" end |
#table_exists?(table_name) ⇒ Boolean
Checks to see if the table table_name
exists on the database.
table_exists?(:developers)
51 52 53 54 55 56 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 51 def table_exists?(table_name) stmt = @raw_connection.tables(native_case(table_name.to_s)) result = stmt.fetch_all || [] stmt.drop result.size.positive? end |
#tables(_name = nil) ⇒ Object
Returns an array of table names, for database tables visible on the current connection.
25 26 27 28 29 30 31 32 33 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 25 def tables(_name = nil) stmt = @raw_connection.tables result = stmt.fetch_all || [] stmt&.drop result = ::SnowflakeOdbcAdapter::Snowflake.table_filter(result, @raw_connection) result.each_with_object([]) do |row, table_names| table_names << format_case(row[2]) end end |
#views(_name = nil) ⇒ Object
Returns an array of view names, for database views visible on the current connection.
37 38 39 40 41 42 43 44 45 |
# File 'lib/active_record/connection_adapters/snowflake_odbc/schema_statements.rb', line 37 def views(_name = nil) stmt = @raw_connection.tables result = stmt.fetch_all || [] stmt&.drop result = ::SnowflakeOdbcAdapter::Snowflake.view_filter(result, @raw_connection) result.each_with_object([]) do |row, table_names| table_names << format_case(row[2]) end end |