Module: Torque::PostgreSQL::Adapter::DatabaseStatements
- Included in:
- Torque::PostgreSQL::Adapter
- Defined in:
- lib/torque/postgresql/adapter/database_statements.rb
Constant Summary collapse
- EXTENDED_DATABASE_TYPES =
i(enum enum_set interval)
Instance Method Summary collapse
-
#column_definitions(table_name) ⇒ Object
Get the list of columns, and their definition, but only from the actual table, does not include columns that comes from inherited table.
-
#configure_connection ⇒ Object
Configure the interval format.
-
#dump_mode! ⇒ Object
Switch between dump mode or not.
-
#extended_types ⇒ Object
Get the list of extended types.
-
#inherited_tables ⇒ Object
Get the list of inherited tables associated with their parent tables.
-
#initialize_type_map(m = type_map) ⇒ Object
Change some of the types being mapped.
-
#load_additional_types(oids = nil) ⇒ Object
:nodoc:.
-
#torque_load_additional_types(oids = nil) ⇒ Object
Add the composite types to be loaded too.
-
#type_exists?(name) ⇒ Boolean
(also: #data_type_exists?)
Returns true if type exists.
-
#user_defined_types(*categories) ⇒ Object
Gets a list of user defined types.
-
#valid_type?(type) ⇒ Boolean
Check if a given type is valid.
Instance Method Details
#column_definitions(table_name) ⇒ Object
Get the list of columns, and their definition, but only from the actual table, does not include columns that comes from inherited table
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 134 def column_definitions(table_name) # :nodoc: local_condition = 'AND a.attislocal IS TRUE' if @_dump_mode query(" SELECT a.attname, format_type(a.atttypid, a.atttypmod),\n pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,\n (SELECT c.collname FROM pg_collation c, pg_type t\n WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),\n col_description(a.attrelid, a.attnum) AS comment\n FROM pg_attribute a\n LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum\n WHERE a.attrelid = '\#{quote_table_name(table_name)}'::regclass\n AND a.attnum > 0\n AND a.attisdropped IS FALSE\n \#{local_condition}\n ORDER BY a.attnum\n SQL\nend\n", 'SCHEMA') |
#configure_connection ⇒ Object
Configure the interval format
32 33 34 35 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 32 def configure_connection super execute("SET SESSION IntervalStyle TO 'iso_8601'", 'SCHEMA') end |
#dump_mode! ⇒ Object
Switch between dump mode or not
11 12 13 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 11 def dump_mode! @_dump_mode = !!!@_dump_mode end |
#extended_types ⇒ Object
Get the list of extended types
21 22 23 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 21 def extended_types EXTENDED_DATABASE_TYPES end |
#inherited_tables ⇒ Object
Get the list of inherited tables associated with their parent tables
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 116 def inherited_tables tables = query(" SELECT child.relname AS table_name,\n array_agg(parent.relname) AS inheritances\n FROM pg_inherits\n JOIN pg_class parent ON pg_inherits.inhparent = parent.oid\n JOIN pg_class child ON pg_inherits.inhrelid = child.oid\n GROUP BY child.relname, pg_inherits.inhrelid\n ORDER BY pg_inherits.inhrelid\n SQL\n\n tables.map do |(table, refs)|\n [table, PG::TextDecoder::Array.new.decode(refs)]\n end.to_h\nend\n", 'SCHEMA') |
#initialize_type_map(m = type_map) ⇒ Object
Change some of the types being mapped
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 38 def initialize_type_map(m = type_map) super m.register_type 'box', OID::Box.new m.register_type 'circle', OID::Circle.new m.register_type 'interval', OID::Interval.new m.register_type 'line', OID::Line.new m.register_type 'segment', OID::Segment.new m.alias_type 'regclass', 'varchar' end |
#load_additional_types(oids = nil) ⇒ Object
:nodoc:
50 51 52 53 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 50 def load_additional_types(oids = nil) super torque_load_additional_types(oids) end |
#torque_load_additional_types(oids = nil) ⇒ Object
Add the composite types to be loaded too.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 56 def torque_load_additional_types(oids = nil) filter = "AND a.typelem::integer IN (%s)" % oids.join(", ") if oids query = " SELECT a.typelem AS oid, t.typname, t.typelem,\n t.typdelim, t.typbasetype, t.typtype,\n t.typarray\n FROM pg_type t\n INNER JOIN pg_type a ON (a.oid = t.typarray)\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND t.typtype IN ( 'e' )\n \#{filter}\n AND NOT EXISTS(\n SELECT 1 FROM pg_catalog.pg_type el\n WHERE el.oid = t.typelem AND el.typarray = t.oid\n )\n AND (t.typrelid = 0 OR (\n SELECT c.relkind = 'c' FROM pg_catalog.pg_class c\n WHERE c.oid = t.typrelid\n ))\n SQL\n\n execute_and_clear(query, 'SCHEMA', []) do |records|\n records.each { |row| OID::Enum.create(row, type_map) }\n end\nend\n" |
#type_exists?(name) ⇒ Boolean Also known as: data_type_exists?
Returns true if type exists.
26 27 28 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 26 def type_exists?(name) user_defined_types.key? name.to_s end |
#user_defined_types(*categories) ⇒ Object
Gets a list of user defined types. You can even choose the category filter
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 86 def user_defined_types(*categories) category_condition = categories.present? \ ? "AND t.typtype IN ('#{categories.join("', '")}')" \ : "AND t.typtype NOT IN ('b', 'd')" select_all(" SELECT t.typname AS name,\n CASE t.typtype\n WHEN 'e' THEN 'enum'\n END AS type\n FROM pg_type t\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')\n \#{category_condition}\n AND NOT EXISTS(\n SELECT 1\n FROM pg_catalog.pg_type el\n WHERE el.oid = t.typelem\n AND el.typarray = t.oid\n )\n AND (t.typrelid = 0 OR (\n SELECT c.relkind = 'c'\n FROM pg_catalog.pg_class c\n WHERE c.oid = t.typrelid\n ))\n ORDER BY t.typtype DESC\n SQL\nend\n", 'SCHEMA').rows.to_h |
#valid_type?(type) ⇒ Boolean
Check if a given type is valid.
16 17 18 |
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 16 def valid_type?(type) super || extended_types.include?(type) end |