Class: Sequel::Postgres::Adapter

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

Constant Summary collapse

TRUE =

the pure-ruby postgres adapter does not have a quote method.

'true'.freeze
FALSE =
'false'.freeze
NULL =
'NULL'.freeze
SELECT_CURRVAL =
"SELECT currval('%s')".freeze
SELECT_PK_AND_SERIAL_SEQUENCE =

Shamelessly appropriated from ActiveRecord’s Postgresql adapter.

<<-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
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 =
<<-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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transaction_in_progressObject

Returns the value of attribute transaction_in_progress.



66
67
68
# File 'lib/sequel_core/adapters/postgres.rb', line 66

def transaction_in_progress
  @transaction_in_progress
end

Class Method Details

.quote(obj) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sequel_core/adapters/postgres.rb', line 30

def self.quote(obj)
  case obj
  when TrueClass
    TRUE
  when FalseClass
    FALSE
  when NilClass
    NULL
  else
    "'#{escape_string(obj.to_s)}'"
  end
end

.string_to_bool(s) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/sequel_core/adapters/postgres.rb', line 150

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)


43
44
45
# File 'lib/sequel_core/adapters/postgres.rb', line 43

def connected?
  status == Adapter::CONNECTION_OK
end

#execute(sql, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sequel_core/adapters/postgres.rb', line 47

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sequel_core/adapters/postgres.rb', line 70

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



130
131
132
133
134
135
136
137
138
# File 'lib/sequel_core/adapters/postgres.rb', line 130

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



140
141
142
143
144
145
146
147
148
# File 'lib/sequel_core/adapters/postgres.rb', line 140

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