Class: Simple::SQL::Connection::Reflection

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/sql/connection/reflection.rb

Constant Summary collapse

TIMESTAMP_COLUMN_NAMES =
%w(inserted_at created_at updated_at)

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Reflection

Returns a new instance of Reflection.



13
14
15
# File 'lib/simple/sql/connection/reflection.rb', line 13

def initialize(connection)
  @connection = connection
end

Instance Method Details

#column_info(table_name) ⇒ Object



70
71
72
73
# File 'lib/simple/sql/connection/reflection.rb', line 70

def column_info(table_name)
  @column_info ||= {}
  @column_info[table_name] ||= _column_info(table_name)
end

#column_types(table_name) ⇒ Object

returns a Hash of column_name => column_type. Names are available both as Symbol and as String.



77
78
79
80
# File 'lib/simple/sql/connection/reflection.rb', line 77

def column_types(table_name)
  @column_types ||= {}
  @column_types[table_name] ||= _column_types(table_name)
end

#columns(table_name) ⇒ Object



66
67
68
# File 'lib/simple/sql/connection/reflection.rb', line 66

def columns(table_name)
  column_info(table_name).keys
end

#looked_up_pg_classesObject



131
132
133
# File 'lib/simple/sql/connection/reflection.rb', line 131

def looked_up_pg_classes
  @looked_up_pg_classes ||= Hash.new { |hsh, key| hsh[key] = _lookup_pg_class(key) }
end

#lookup_pg_class(oid) ⇒ Object



135
136
137
# File 'lib/simple/sql/connection/reflection.rb', line 135

def lookup_pg_class(oid)
  looked_up_pg_classes[oid]
end

#primary_key_column(table_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple/sql/connection/reflection.rb', line 21

def primary_key_column(table_name)
  @primary_key_column ||= {}
  @primary_key_column[table_name] ||= begin
    pk_column, other = primary_key_columns(table_name)

    raise "#{table_name}: No support for combined primary keys" if other
    raise "#{table_name}: No primary key" if pk_column.nil?

    pk_column
  end
end

#primary_key_columns(table_name) ⇒ Object



33
34
35
36
# File 'lib/simple/sql/connection/reflection.rb', line 33

def primary_key_columns(table_name)
  @primary_key_columns ||= {}
  @primary_key_columns[table_name] ||= _primary_key_columns(table_name)
end

#table_info(schema: "public") ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/simple/sql/connection/reflection.rb', line 82

def table_info(schema: "public")
  recs = @connection.all "    SELECT table_schema || '.' || table_name AS name, *\n    FROM information_schema.tables\n    WHERE table_schema=$1\n  SQL\n  records_by_attr(recs, :name)\nend\n", schema, into: Hash

#tables(schema: "public") ⇒ Object



17
18
19
# File 'lib/simple/sql/connection/reflection.rb', line 17

def tables(schema: "public")
  table_info(schema: schema).keys
end

#timestamp_columns(table_name) ⇒ Object

timestamp_columns are columns that will be set automatically after inserting or updating a record. This includes:

  • inserted_at (for Ecto)

  • created_at (for ActiveRecord)

  • updated_at (for Ecto and ActiveRecord)



62
63
64
# File 'lib/simple/sql/connection/reflection.rb', line 62

def timestamp_columns(table_name)
  columns(table_name) & TIMESTAMP_COLUMN_NAMES
end