Module: ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements

Defined in:
lib/activerecord_postgresql_procedures/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#primary_key(table) ⇒ Object

Returns just a table’s primary key



7
8
9
10
11
12
13
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 7

def primary_key(table)
  if table_or_view_exists?(table)
     table_primary_key(table)
  else
    procedure_primary_key(table)
  end
end

#procedure_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 72

def procedure_exists?(name)
  name = Utils.extract_schema_qualified_name(name.to_s)
  return false unless name.identifier
  exec_query("    SELECT  COUNT(*)\n    FROM\n      pg_catalog.pg_proc p\n      JOIN    pg_catalog.pg_namespace n ON pronamespace = n.oid\n      JOIN    pg_catalog.pg_type t ON typelem = p.prorettype\n    WHERE\n      typname = '_record'\n      AND p.proname = '\#{name.identifier}'\n      AND n.nspname = \#{name.schema ? \"'\#{name.schema}'\" : 'ANY (current_schemas(false))'}\n  SQL\nend\n", 'SCHEMA').rows.first[0].to_i > 0

#procedure_primary_key(name) ⇒ Object

Return the procedure’s primary key

The procedure doesn’t have ‘native’ primary key, the first column is used.



29
30
31
32
33
34
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 29

def procedure_primary_key(name)
  row = exec_query("        SELECT proargnames[1] FROM pg_proc WHERE proname = '\#{name}'\n      end_sql\n  row && row.first\nend\n", 'SCHEMA').rows.first

#table_exists?(name) ⇒ Boolean

Returns true if table or procedure exists. If the schema is not specified as part of name then it will only find tables within the current schema search path (regardless of permissions to access tables in other schemas)

Returns:

  • (Boolean)


49
50
51
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 49

def table_exists?(name)
  table_or_view_exists?(name) or procedure_exists?(name)
end

#table_or_view_exists?(name) ⇒ Boolean

Returns true if table exists. If the schema is not specified as part of name then it will only find tables within the current schema search path (regardless of permissions to access tables in other schemas)

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 56

def table_or_view_exists?(name)
  name = Utils.extract_schema_qualified_name(name.to_s)
  return false unless name.identifier
  exec_query("    SELECT COUNT(*)\n    FROM\n      pg_class c\n      LEFT JOIN pg_namespace n ON n.oid = c.relnamespace\n    WHERE\n      c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view\n      AND c.relname = '\#{name.identifier}'\n      AND n.nspname = \#{name.schema ? \"'\#{name.schema}'\" : 'ANY (current_schemas(false))'}\n  SQL\nend\n", 'SCHEMA').rows.first[0].to_i > 0

#table_primary_key(table) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/activerecord_postgresql_procedures/schema_statements.rb', line 15

def table_primary_key(table)
  row = exec_query("    SELECT attr.attname\n    FROM pg_attribute attr\n    INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]\n    WHERE cons.contype = 'p'\n    AND cons.conrelid =  (SELECT oid FROM pg_class WHERE relname = '\#{table}')\n    end_sql\n  row && row.first\nend\n", 'SCHEMA').rows.first