Module: ActiveRecord::ConnectionAdapters::Sunstone::SchemaStatements
- Included in:
- ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
- Defined in:
- lib/active_record/connection_adapters/sunstone/schema_statements.rb
Instance Method Summary collapse
-
#column_definitions(table_name) ⇒ Object
Returns the list of a table’s column names, data types, and default values.
-
#column_name_for_operation(operation, node) ⇒ Object
:nodoc:.
-
#columns(table_name) ⇒ Object
Returns the list of all column definitions for a table.
-
#columns_for_distinct(columns, orders) ⇒ Object
Given a set of columns and an ORDER BY clause, returns the columns for a SELECT DISTINCT.
- #definition(table_name) ⇒ Object
-
#distinct_relation_for_primary_key(relation) ⇒ Object
:nodoc:.
- #fetch_type_metadata(options) ⇒ Object
-
#limit_definition(table_name) ⇒ Object
Returns the limit definition of the table (the maximum limit that can be used).
- #lookup_cast_type(options) ⇒ Object
- #new_column(name, options) ⇒ Object
-
#primary_key(table) ⇒ Object
Returns just a table’s primary key.
-
#table_exists?(name) ⇒ Boolean
Returns true if table exists.
- #tables ⇒ Object
- #views ⇒ Object
Instance Method Details
#column_definitions(table_name) ⇒ Object
Returns the list of a table’s column names, data types, and default values.
Query implementation notes:
- format_type includes the column size constraint, e.g. varchar(50)
- ::regclass is a function that gives the id for a table name
41 42 43 44 45 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 41 def column_definitions(table_name) # :nodoc: # TODO: settle on schema, I think we've switched to attributes, so # columns can be removed soon? definition(table_name)['attributes'] || definition(table_name)['columns'] end |
#column_name_for_operation(operation, node) ⇒ Object
:nodoc:
82 83 84 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 82 def column_name_for_operation(operation, node) # :nodoc: visitor.accept(node, collector).first[operation.to_sym] end |
#columns(table_name) ⇒ Object
Returns the list of all column definitions for a table.
15 16 17 18 19 20 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 15 def columns(table_name) # Limit, precision, and scale are all handled by the superclass. column_definitions(table_name).map do |column_name, | new_column(column_name, ) end end |
#columns_for_distinct(columns, orders) ⇒ Object
Given a set of columns and an ORDER BY clause, returns the columns for a SELECT DISTINCT. PostgreSQL, MySQL, and Oracle override this for custom DISTINCT syntax - they require the order columns appear in the SELECT.
columns_for_distinct("posts.id", ["posts.created_at desc"])
92 93 94 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 92 def columns_for_distinct(columns, orders) # :nodoc: columns end |
#definition(table_name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 22 def definition(table_name) # TODO move @definitions to using @schema_cache @definitions = {} if !defined?(@definitions) if @definitions[table_name] return @definitions[table_name] end response = with_raw_connection { |conn| conn.get("/#{table_name}/schema") } @definitions[table_name] = JSON.parse(response.body) rescue ::Sunstone::Exception::NotFound raise ActiveRecord::StatementInvalid, "Table \"#{table_name}\" does not exist" end |
#distinct_relation_for_primary_key(relation) ⇒ Object
:nodoc:
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 96 def distinct_relation_for_primary_key(relation) # :nodoc: values = columns_for_distinct( relation.table[relation.primary_key], relation.order_values ) limited = relation.reselect(values).distinct! limited_ids = select_rows(limited.arel, "SQL").map(&:last) if limited_ids.empty? relation.none! else relation.where!(relation.primary_key => limited_ids) end relation.limit_value = relation.offset_value = nil relation end |
#fetch_type_metadata(options) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 70 def () cast_type = lookup_cast_type() simple_type = SqlTypeMetadata.new( sql_type: ['type'], type: cast_type.type, limit: cast_type.limit, precision: cast_type.precision, scale: cast_type.scale ) SunstoneSQLTypeMetadata.new(simple_type, ) end |
#limit_definition(table_name) ⇒ Object
Returns the limit definition of the table (the maximum limit that can be used).
49 50 51 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 49 def limit_definition(table_name) definition(table_name)['limit'] || nil end |
#lookup_cast_type(options) ⇒ Object
66 67 68 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 66 def lookup_cast_type() @type_map.lookup(['type'], .symbolize_keys) end |
#new_column(name, options) ⇒ Object
61 62 63 64 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 61 def new_column(name, ) = () SunstoneColumn.new(name, , ) end |
#primary_key(table) ⇒ Object
Returns just a table’s primary key
118 119 120 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 118 def primary_key(table) columns(table).find{ |c| c.primary_key? }.try(:name) end |
#table_exists?(name) ⇒ Boolean
Returns true if table exists. If the schema is not specified as part of name
then it will only find tables within the current schema search path (regardless of permissions to access tables in other schemas)
10 11 12 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 10 def table_exists?(name) tables.include?(name) end |
#tables ⇒ Object
53 54 55 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 53 def tables JSON.parse(with_raw_connection { |conn| conn.get('/tables').body }) end |
#views ⇒ Object
57 58 59 |
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 57 def views [] end |