Module: Sequel::Postgres::AdapterMethods

Included in:
DataObjects::Postgres::AdapterMethods, JDBC::Postgres::AdapterMethods, Adapter
Defined in:
lib/sequel/lib/sequel/adapters/shared/postgres.rb

Overview

Methods shared by adapter/connection instances.

Constant Summary collapse

SELECT_CURRVAL =
"SELECT currval('%s')".freeze
SELECT_CUSTOM_SEQUENCE =
proc do |schema, table| "SELECT '\"' || name.nspname || '\".' || CASE\nWHEN split_part(def.adsrc, '''', 2) ~ '.' THEN\nsubstr(split_part(def.adsrc, '''', 2),\nstrpos(split_part(def.adsrc, '''', 2), '.')+1)\nELSE split_part(def.adsrc, '''', 2)\nEND\nFROM pg_class t\nJOIN pg_namespace  name ON (t.relnamespace = name.oid)\nJOIN pg_attribute  attr ON (t.oid = attrelid)\nJOIN pg_attrdef    def  ON (adrelid = attrelid AND adnum = attnum)\nJOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])\nWHERE cons.contype = 'p'\nAND def.adsrc ~* 'nextval'\n\#{\"AND name.nspname = '\#{schema}'\" if schema}\nAND t.relname = '\#{table}'\n"
end
SELECT_PK =
proc do |schema, table| "SELECT pg_attribute.attname\nFROM pg_class, pg_attribute, pg_index, pg_namespace\nWHERE pg_class.oid = pg_attribute.attrelid\nAND pg_class.relnamespace  = pg_namespace.oid\nAND pg_class.oid = pg_index.indrelid\nAND pg_index.indkey[0] = pg_attribute.attnum\nAND pg_index.indisprimary = 't'\n\#{\"AND pg_namespace.nspname = '\#{schema}'\" if schema}\nAND pg_class.relname = '\#{table}'\n"
end
SELECT_SERIAL_SEQUENCE =
proc do |schema, table| "SELECT  '\"' || name.nspname || '\".' || seq.relname || ''\nFROM pg_class seq, pg_attribute attr, pg_depend dep,\npg_namespace name, pg_constraint cons\nWHERE seq.oid = dep.objid\nAND seq.relnamespace  = name.oid\nAND seq.relkind = 'S'\nAND attr.attrelid = dep.refobjid\nAND attr.attnum = dep.refobjsubid\nAND attr.attrelid = cons.conrelid\nAND attr.attnum = cons.conkey[1]\nAND cons.contype = 'p'\n\#{\"AND name.nspname = '\#{schema}'\" if schema}\nAND seq.relname = '\#{table}'\n"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db=(value) ⇒ Object (writeonly)

Sets the attribute db


55
56
57
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 55

def db=(value)
  @db = value
end

#transaction_depthObject

Depth of the current transaction on this connection, used to implement multi-level transactions with savepoints.


107
108
109
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 107

def transaction_depth
  @transaction_depth
end

Instance Method Details

#apply_connection_settingsObject

Apply connection settings for this connection. Currently, turns standard_conforming_strings ON if Postgres.force_standard_strings is true.


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 112

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
  if cmm = Postgres.client_min_messages
    sql = "SET client_min_messages = '#{cmm.to_s.upcase}'"
    @db.log_info(sql)
    execute(sql)
  end
end

#last_insert_id(sequence) ⇒ Object

Get the last inserted value for the given sequence.


129
130
131
132
133
134
135
136
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 129

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.


139
140
141
142
143
144
145
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 139

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.


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sequel/lib/sequel/adapters/shared/postgres.rb', line 148

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