Class: Sequel::Postgres::Adapter

Inherits:
PGconn
  • Object
show all
Defined in:
lib/sequel_core/adapters/postgres.rb

Constant Summary collapse

SELECT_CURRVAL =
"SELECT currval('%s')".freeze
SELECT_PK_AND_SERIAL_SEQUENCE =

Shamelessly appropriated from ActiveRecord’s Postgresql adapter.

"  SELECT attr.attname, 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 dep.refobjid = '%s'::regclass\n"
SELECT_PK_AND_CUSTOM_SEQUENCE =
"  SELECT attr.attname,  \n    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 t.oid = '%s'::regclass\n    AND cons.contype = 'p'\n    AND def.adsrc ~* 'nextval'\n"
SELECT_PK =
"  SELECT pg_attribute.attname\n  FROM pg_class, pg_attribute, pg_index\n  WHERE pg_class.oid = pg_attribute.attrelid AND\n    pg_class.oid = pg_index.indrelid AND\n    pg_index.indkey[0] = pg_attribute.attnum AND\n    pg_index.indisprimary = 't' AND\n    pg_class.relname = '%s'\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transaction_depthObject

Returns the value of attribute transaction_depth.



82
83
84
# File 'lib/sequel_core/adapters/postgres.rb', line 82

def transaction_depth
  @transaction_depth
end

Class Method Details

.string_to_bool(s) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/sequel_core/adapters/postgres.rb', line 166

def self.string_to_bool(s)
  if(s.blank?)
    nil
  elsif(s.downcase == 't' || s.downcase == 'true')
    true
  else
    false
  end
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/sequel_core/adapters/postgres.rb', line 59

def connected?
  status == Adapter::CONNECTION_OK
end

#execute(sql, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sequel_core/adapters/postgres.rb', line 63

def execute(sql, &block)
  q = nil
  begin
    q = exec(sql)
  rescue PGError => e
    unless connected?
      reset
      q = exec(sql)
    else
      raise e
    end
  end
  begin
    block ? block[q] : q.cmd_tuples
  ensure
    q.clear
  end
end

#last_insert_id(table) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sequel_core/adapters/postgres.rb', line 86

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|
      return r.getvalue(0,0).to_i unless r.nil? || (r.ntuples == 0)
    end
  end
  nil # primary key sequence not found
end

#pkey_and_sequence(table) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/sequel_core/adapters/postgres.rb', line 146

def pkey_and_sequence(table)
  execute(SELECT_PK_AND_SERIAL_SEQUENCE % table) do |r|
    return [r.getvalue(0,2), r.getvalue(0,2)] unless r.nil? || (r.ntuples == 0)
  end
    
  execute(SELECT_PK_AND_CUSTOM_SEQUENCE % table) do |r|
    return [r.getvalue(0,0), r.getvalue(0,1)] unless r.nil? || (r.ntuples == 0)
  end
end

#primary_key(table) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/sequel_core/adapters/postgres.rb', line 156

def primary_key(table)
  execute(SELECT_PK % table) do |r|
    if (r.nil? || (r.ntuples == 0)) then
      return nil
    else
      r.getvalue(0,0)
    end
  end
end