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

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


43
44
45
46
47
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 43

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:



84
85
86
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 84

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.



17
18
19
20
21
22
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 17

def columns(table_name)
  # Limit, precision, and scale are all handled by the superclass.
  column_definitions(table_name).map do |column_name, options|
    new_column(column_name, options)
  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"])


94
95
96
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 94

def columns_for_distinct(columns, orders) # :nodoc:
  columns
end

#definition(table_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 24

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:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 98

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(cast_type, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 73

def (cast_type, options)
  simple_type = SqlTypeMetadata.new(
    sql_type: options['type'],
    type: cast_type.type,
    limit: cast_type.limit,
    precision: cast_type.precision,
    scale: cast_type.scale
  )
  SunstoneSQLTypeMetadata.new(simple_type, options)
end

#limit_definition(table_name) ⇒ Object

Returns the limit definition of the table (the maximum limit that can be used).



51
52
53
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 51

def limit_definition(table_name)
  definition(table_name)['limit'] || nil
end

#lookup_cast_type(options) ⇒ Object



69
70
71
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 69

def lookup_cast_type(options)
  @type_map.lookup(options['type'], options.symbolize_keys)
end

#new_column(name, options) ⇒ Object



63
64
65
66
67
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 63

def new_column(name, options)
  cast_type = lookup_cast_type(options)
   = (cast_type, options)
  SunstoneColumn.new(name, cast_type, , options)
end

#primary_key(table) ⇒ Object

Returns just a table's primary key



120
121
122
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 120

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)

Returns:

  • (Boolean)


12
13
14
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 12

def table_exists?(name)
  tables.include?(name)
end

#tablesObject



55
56
57
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 55

def tables
  JSON.parse(with_raw_connection { |conn| conn.get('/tables').body })
end

#viewsObject



59
60
61
# File 'lib/active_record/connection_adapters/sunstone/schema_statements.rb', line 59

def views
  []
end