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(<<-SQL, 'SCHEMA').rows SELECT argname, format_type(argtype,typtypmod), typdefault, CASE row_number() over() WHEN 1 THEN true ELSE typnotnull END, argtype, typtypmod FROM ( SELECT unnest(proargnames) AS argname, unnest(proallargtypes) AS argtype, unnest(proargmodes) AS argmode FROM pg_proc WHERE proname = '#{name}' ) t JOIN pg_type ON t.argtype = pg_type.oid and t.argmode = 't' SQL end |
#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(<<-SQL, 'SCHEMA').rows SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '#{quote_table_name(name)}'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum SQL end |