Class: Simple::SQL::Connection::Reflection
- Inherits:
-
Object
- Object
- Simple::SQL::Connection::Reflection
- 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
- #column_info(table_name) ⇒ Object
-
#column_types(table_name) ⇒ Object
returns a Hash of column_name => column_type.
- #columns(table_name) ⇒ Object
-
#initialize(connection) ⇒ Reflection
constructor
A new instance of Reflection.
- #looked_up_pg_classes ⇒ Object
- #lookup_pg_class(oid) ⇒ Object
- #primary_key_columns(table_name) ⇒ Object
- #table_info(schema: "public") ⇒ Object
- #tables(schema: "public") ⇒ Object
-
#timestamp_columns(table_name) ⇒ Object
timestamp_columns are columns that will be set automatically after inserting or updating a record.
Constructor Details
#initialize(connection) ⇒ Reflection
Returns a new instance of Reflection.
11 12 13 |
# File 'lib/simple/sql/connection/reflection.rb', line 11 def initialize(connection) @connection = connection end |
Instance Method Details
#column_info(table_name) ⇒ Object
45 46 47 48 |
# File 'lib/simple/sql/connection/reflection.rb', line 45 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.
52 53 54 55 |
# File 'lib/simple/sql/connection/reflection.rb', line 52 def column_types(table_name) @column_types ||= {} @column_types[table_name] ||= _column_types(table_name) end |
#columns(table_name) ⇒ Object
41 42 43 |
# File 'lib/simple/sql/connection/reflection.rb', line 41 def columns(table_name) column_info(table_name).keys end |
#looked_up_pg_classes ⇒ Object
106 107 108 |
# File 'lib/simple/sql/connection/reflection.rb', line 106 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
110 111 112 |
# File 'lib/simple/sql/connection/reflection.rb', line 110 def lookup_pg_class(oid) looked_up_pg_classes[oid] end |
#primary_key_columns(table_name) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/simple/sql/connection/reflection.rb', line 19 def primary_key_columns(table_name) @connection.all <<~SQL, table_name SELECT pg_attribute.attname FROM pg_index JOIN pg_attribute ON pg_attribute.attrelid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey) WHERE pg_index.indrelid = $1::regclass AND pg_index.indisprimary; SQL end |
#table_info(schema: "public") ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/simple/sql/connection/reflection.rb', line 57 def table_info(schema: "public") recs = @connection.all <<~SQL, schema, into: Hash SELECT table_schema || '.' || table_name AS name, * FROM information_schema.tables WHERE table_schema=$1 SQL records_by_attr(recs, :name) end |
#tables(schema: "public") ⇒ Object
15 16 17 |
# File 'lib/simple/sql/connection/reflection.rb', line 15 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)
37 38 39 |
# File 'lib/simple/sql/connection/reflection.rb', line 37 def (table_name) columns(table_name) & TIMESTAMP_COLUMN_NAMES end |