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_PK =
<<-end_sql
  SELECT pg_attribute.attname
  FROM pg_class, pg_attribute, pg_index
  WHERE pg_class.oid = pg_attribute.attrelid AND
    pg_class.oid = pg_index.indrelid AND
    pg_index.indkey[0] = pg_attribute.attnum AND
    pg_index.indisprimary = 't' AND
    pg_class.relname = '%s'
end_sql
SELECT_PK_AND_CUSTOM_SEQUENCE =
<<-end_sql
  SELECT attr.attname,  
    CASE  
      WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN  
        substr(split_part(def.adsrc, '''', 2),  
               strpos(split_part(def.adsrc, '''', 2), '.')+1) 
      ELSE split_part(def.adsrc, '''', 2)  
    END
  FROM pg_class t
  JOIN pg_namespace  name ON (t.relnamespace = name.oid)
  JOIN pg_attribute  attr ON (t.oid = attrelid)
  JOIN pg_attrdef    def  ON (adrelid = attrelid AND adnum = attnum)
  JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
  WHERE t.oid = '%s'::regclass
    AND cons.contype = 'p'
    AND def.adsrc ~* 'nextval'
end_sql
SELECT_PK_AND_SERIAL_SEQUENCE =
<<-end_sql
  SELECT attr.attname, name.nspname, seq.relname
  FROM pg_class seq, pg_attribute attr, pg_depend dep,
    pg_namespace name, pg_constraint cons
  WHERE seq.oid = dep.objid
    AND seq.relnamespace  = name.oid
    AND seq.relkind = 'S'
    AND attr.attrelid = dep.refobjid
    AND attr.attnum = dep.refobjsubid
    AND attr.attrelid = cons.conrelid
    AND attr.attnum = cons.conkey[1]
    AND cons.contype = 'p'
    AND dep.refobjid = '%s'::regclass
end_sql

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transaction_depthObject

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



53
54
55
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 53

def transaction_depth
  @transaction_depth
end

Instance Method Details

#last_insert_id(table) ⇒ Object

Get the last inserted value for the given table.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 56

def last_insert_id(table)
  @table_sequences ||= {}
  if !@table_sequences.include?(table)
    pkey_and_seq = pkey_and_sequence(table)
    if pkey_and_seq
      @table_sequences[table] = pkey_and_seq[1]
    end
  end
  if seq = @table_sequences[table]
    execute(SELECT_CURRVAL % seq) do |r|
      val = result_set_values(r, 0)
      val.to_i if val
    end
  end
end

#pkey_and_sequence(table) ⇒ Object

Get the primary key and sequence for the given table.



73
74
75
76
77
78
79
80
81
82
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 73

def pkey_and_sequence(table)
  execute(SELECT_PK_AND_SERIAL_SEQUENCE % table) do |r|
    vals = result_set_values(r, 2, 2)
    return vals if vals
  end
    
  execute(SELECT_PK_AND_CUSTOM_SEQUENCE % table) do |r|
    result_set_values(r, 0, 1)
  end
end

#primary_key(table) ⇒ Object

Get the primary key for the given table.



85
86
87
88
89
# File 'lib/sequel_core/adapters/shared/postgres.rb', line 85

def primary_key(table)
  execute(SELECT_PK % table) do |r|
    result_set_values(r, 0)
  end
end