Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/activerecord_postgresql_procedures/postgresql_adapter.rb
Instance Method Summary collapse
-
#column_definitions(name) ⇒ Object
Returns the list of a table’s or procedure’s column names, data types, and default values.
-
#procedure_column_definitions(name) ⇒ Object
Returns the list of procedure’s column names, data types, and default values.
-
#table_column_definitions(name) ⇒ Object
Returns the list of a table’s column names, data types, and default values.
Instance Method Details
#column_definitions(name) ⇒ Object
Returns the list of a table’s or procedure’s column names, data types, and default values.
6 7 8 9 10 11 12 |
# File 'lib/activerecord_postgresql_procedures/postgresql_adapter.rb', line 6 def column_definitions(name) # :nodoc: if procedure_exists?(name) procedure_column_definitions(name) else table_column_definitions(name) end end |
#procedure_column_definitions(name) ⇒ Object
Returns the list of procedure’s column names, data types, and default values.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/activerecord_postgresql_procedures/postgresql_adapter.rb', line 34 def procedure_column_definitions(name) exec_query(" SELECT\n argname, format_type(argtype,typtypmod), typdefault, CASE row_number() over() WHEN 1 THEN true ELSE typnotnull END, argtype, typtypmod\n FROM\n (\n SELECT\n unnest(proargnames) AS argname,\n unnest(proallargtypes) AS argtype,\n unnest(proargmodes) AS argmode\n FROM pg_proc\n WHERE proname = '\#{name}'\n ) t\n JOIN pg_type ON t.argtype = pg_type.oid and t.argmode = 't'\n SQL\nend\n", 'SCHEMA').rows |
#table_column_definitions(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
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/activerecord_postgresql_procedures/postgresql_adapter.rb', line 19 def table_column_definitions(name) exec_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 FROM\n pg_attribute a\n LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum\n WHERE\n a.attrelid = '\#{quote_table_name(name)}'::regclass\n AND a.attnum > 0 AND NOT a.attisdropped\n ORDER BY a.attnum\n SQL\nend\n", 'SCHEMA').rows |