Module: Sequel::Postgres::AdapterMethods
- Included in:
- Adapter
- Defined in:
- lib/sequel_core/adapters/shared/postgres.rb
Overview
Methods shared by adapter/connection instances.
Constant Summary collapse
- SELECT_CURRVAL =
"SELECT currval('%s')".freeze
- SELECT_CUSTOM_SEQUENCE =
" SELECT '\"' || name.nspname || '\".\"' || CASE \n WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN \n substr(split_part(def.adsrc, '''', 2), \n strpos(split_part(def.adsrc, '''', 2), '.')+1) \n ELSE split_part(def.adsrc, '''', 2) \n END || '\"'\n FROM pg_class t\n JOIN pg_namespace name ON (t.relnamespace = name.oid)\n JOIN pg_attribute attr ON (t.oid = attrelid)\n JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)\n JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])\n WHERE cons.contype = 'p'\n AND def.adsrc ~* 'nextval'\n AND name.nspname = '%s'\n AND t.relname = '%s'\n"- SELECT_PK =
" SELECT pg_attribute.attname\n FROM pg_class, pg_attribute, pg_index, pg_namespace\n WHERE pg_class.oid = pg_attribute.attrelid\n AND pg_class.relnamespace = pg_namespace.oid\n AND pg_class.oid = pg_index.indrelid\n AND pg_index.indkey[0] = pg_attribute.attnum\n AND pg_index.indisprimary = 't'\n AND pg_namespace.nspname = '%s'\n AND pg_class.relname = '%s'\n"- SELECT_SERIAL_SEQUENCE =
" SELECT '\"' || name.nspname || '\".\"' || seq.relname || '\"'\n FROM pg_class seq, pg_attribute attr, pg_depend dep,\n pg_namespace name, pg_constraint cons\n WHERE seq.oid = dep.objid\n AND seq.relnamespace = name.oid\n AND seq.relkind = 'S'\n AND attr.attrelid = dep.refobjid\n AND attr.attnum = dep.refobjsubid\n AND attr.attrelid = cons.conrelid\n AND attr.attnum = cons.conkey[1]\n AND cons.contype = 'p'\n AND name.nspname = '%s'\n AND seq.relname = '%s'\n"
Instance Attribute Summary collapse
-
#db ⇒ Object
writeonly
Sets the attribute db.
-
#transaction_depth ⇒ Object
Depth of the current transaction on this connection, used to implement multi-level transactions with savepoints.
Instance Method Summary collapse
-
#apply_connection_settings ⇒ Object
Apply connection settings for this connection.
-
#last_insert_id(sequence) ⇒ Object
Get the last inserted value for the given sequence.
-
#primary_key(schema, table) ⇒ Object
Get the primary key for the given table.
-
#sequence(schema, table) ⇒ Object
Get the primary key and sequence for the given table.
Instance Attribute Details
#db=(value) ⇒ Object (writeonly)
Sets the attribute db
46 47 48 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 46 def db=(value) @db = value end |
#transaction_depth ⇒ Object
Depth of the current transaction on this connection, used to implement multi-level transactions with savepoints.
95 96 97 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 95 def transaction_depth @transaction_depth end |
Instance Method Details
#apply_connection_settings ⇒ Object
Apply connection settings for this connection. Currently, turns standard_conforming_strings ON if Postgres.force_standard_strings is true.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 100 def apply_connection_settings if Postgres.force_standard_strings sql = "SET standard_conforming_strings = ON" @db.log_info(sql) # This setting will only work on PostgreSQL 8.2 or greater # and we don't know the server version at this point, so # try it unconditionally and rescue any errors. execute(sql) rescue nil end end |
#last_insert_id(sequence) ⇒ Object
Get the last inserted value for the given sequence.
112 113 114 115 116 117 118 119 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 112 def last_insert_id(sequence) sql = SELECT_CURRVAL % sequence @db.log_info(sql) execute(sql) do |r| val = single_value(r) return val.to_i if val end end |
#primary_key(schema, table) ⇒ Object
Get the primary key for the given table.
122 123 124 125 126 127 128 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 122 def primary_key(schema, table) sql = SELECT_PK % [schema, table] @db.log_info(sql) execute(sql) do |r| return single_value(r) end end |
#sequence(schema, table) ⇒ Object
Get the primary key and sequence for the given table.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 131 def sequence(schema, table) sql = SELECT_SERIAL_SEQUENCE % [schema, table] @db.log_info(sql) execute(sql) do |r| seq = single_value(r) return seq if seq end sql = SELECT_CUSTOM_SEQUENCE % [schema, table] @db.log_info(sql) execute(sql) do |r| return single_value(r) end end |