Class: Og::PostgresqlAdapter

Inherits:
SqlStore show all
Extended by:
PostgresqlUtils
Includes:
PostgresqlUtils
Defined in:
lib/og/adapter/postgresql.rb

Overview

A Store that persists objects into a PostgreSQL database. To read documentation about the methods, consult the documentation for SqlStore and Store.

This is the reference Og store.

Instance Attribute Summary

Attributes inherited from SqlStore

#conn, #typemap

Attributes inherited from Store

#ogmanager

Instance Method Summary collapse

Methods inherited from SqlStore

#aggregate, #count, #create_field_map, #create_table, #delete_all, #drop_table, #eval_og_allocate, #eval_og_create_schema, #eval_og_delete, #eval_og_insert, #eval_og_read, #eval_og_update, #exec, #field_for_attribute, #field_sql_for_attribute, #fields_for_class, #find, #find_one, #force_primary_key, #handle_sql_exception, #join, #load, #pk_field, #prepare_statement, #query, #read_all, #read_field, #read_join_relations, #read_one, #reload, #resolve_limit_options, #resolve_options, #select, #select_one, #serializable_attributes_for_class, #sql_indices_for_class, #sql_type_for_class, #table_exists?, #type_cast, #unjoin, #update, #update_by_sql, #update_condition, #write_attr

Methods inherited from Store

#count, #delete, #delete_all, #find, #force_save!, #insert, #load, #reload, #save, #transaction, #update, #update_attributes

Constructor Details

#initialize(options) ⇒ PostgresqlAdapter

Returns a new instance of PostgresqlAdapter.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/og/adapter/postgresql.rb', line 23

def initialize(options)
  super

  @typemap.update(Og::Blob => 'bytea')

  @conn = PGconn.connect(
    options[:address] || options[:host], 
    options[:port], nil, nil, 
    options[:name], 
    options[:user].to_s, 
    options[:password].to_s
  )
  schema_order = options[:schema_order]
  encoding = options[:encoding]
  min_messages = options[:min_messages]
  
  @conn.exec("SET search_path TO #{schema_order}") if schema_order
  @conn.exec("SET client_encoding TO '#{encoding}'") if encoding
  @conn.exec("SET client_min_messages TO '#{min_messages}'") if min_messages
rescue PGError => ex
  if database_does_not_exist_exception? ex
    Logger.info "Database '#{options[:name]}' not found!"
    create_db(options)
    retry
  end
  raise
end

Instance Method Details

#closeObject


51
52
53
54
# File 'lib/og/adapter/postgresql.rb', line 51

def close
  @conn.close
  super
end

#commitObject

Commit a transaction.


147
148
149
150
151
152
153
154
# File 'lib/og/adapter/postgresql.rb', line 147

def commit
  @transaction_nesting -= 1
  exec('COMMIT') if @transaction_nesting < 1
  
  if @transaction_nesting >= 1 && @conn.server_version > 80000
    exec("RELEASE SAVEPOINT SP#{@transaction_nesting}")
  end
end

#create_db(options) ⇒ Object


56
57
58
59
60
# File 'lib/og/adapter/postgresql.rb', line 56

def create_db(options)
  # gmosx: system is used to avoid shell expansion.
  system 'createdb', options[:name], '-U', options[:user]
  super
end

#destroy_db(options) ⇒ Object


62
63
64
65
# File 'lib/og/adapter/postgresql.rb', line 62

def destroy_db(options)
  system 'dropdb', options[:name], '-U', options[:user]
  super
end

#enchant(klass, manager) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/og/adapter/postgresql.rb', line 73

def enchant(klass, manager)
  pk = klass.primary_key
  
  seq = if klass.schema_inheritance_child?
    "#{table(klass.schema_inheritance_root_class)}_#{pk}_seq"
  else
    "#{table(klass)}_#{pk}_seq"
  end
  
  pkann = klass.ann[pk]
  
  pkann[:sequence] = (pkann[:sql] =~ /SERIAL/i) ? seq : false
  
  super
end

#exec_statement(sql) ⇒ Object


93
94
95
# File 'lib/og/adapter/postgresql.rb', line 93

def exec_statement(sql)
  @conn.exec(sql).clear
end

#insert_sql(sql, klass) ⇒ Object

The insert sql statements.


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/og/adapter/postgresql.rb', line 118

def insert_sql(sql, klass)
  str = ''
  
  if klass.ann[klass.primary_key][:sequence]
    str << "@#{klass.primary_key} = store.last_insert_id(#{klass})\n"
  end
  
  str << "store.exec \"#{sql}\""
  
  return str
end

#last_insert_id(klass) ⇒ Object

Return the last inserted row id.


107
108
109
110
111
112
113
114
# File 'lib/og/adapter/postgresql.rb', line 107

def last_insert_id(klass)
  seq = klass.ann[klass.primary_key][:sequence]
  
  res = query("SELECT nextval('#{seq}')")
  lid = Integer(res.first_value)
  
  return lid
end

#primary_key_typeObject

The type used for default primary keys.


69
70
71
# File 'lib/og/adapter/postgresql.rb', line 69

def primary_key_type
  'serial PRIMARY KEY'
end

#query_statement(sql) ⇒ Object


89
90
91
# File 'lib/og/adapter/postgresql.rb', line 89

def query_statement(sql)
  return @conn.exec(sql)
end

#read_attr(s, a, col) ⇒ Object


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/og/adapter/postgresql.rb', line 167

def read_attr(s, a, col)
  store = self.class
  {
    String    => nil,
    Integer   => :parse_int,
    Float     => :parse_float,
    Time      => :parse_timestamp,
    Date      => :parse_date,
    TrueClass => :parse_boolean,
    Og::Blob  => :parse_blob
  }.each do |klass, meth|
    if a.class.ancestor? klass
      return meth ? 
        "#{store}.#{meth}(res[#{col} + offset])" : "res[#{col} + offset]"
    end
  end

  # else try to load it via YAML
  "YAML::load(res[#{col} + offset])"
end

#read_row(obj, res, res_row, row) ⇒ Object


188
189
190
191
192
# File 'lib/og/adapter/postgresql.rb', line 188

def read_row(obj, res, res_row, row)
  res.fields.each_with_index do |field, idx|
    obj.instance_variable_set "@#{field}", res.getvalue(row, idx)
  end
end

#rollbackObject

Rollback a transaction.


158
159
160
161
162
163
164
165
# File 'lib/og/adapter/postgresql.rb', line 158

def rollback
  @transaction_nesting -= 1
  exec('ROLLBACK') if @transaction_nesting < 1
  
  if @transaction_nesting >= 1 && @conn.server_version > 80000
    exec("ROLLBACK TO SAVEPOINT SP#{@transaction_nesting}")
  end
end

#sql_update(sql) ⇒ Object


97
98
99
100
101
102
103
# File 'lib/og/adapter/postgresql.rb', line 97

def sql_update(sql)
  Logger.debug sql if $DBG  
  res = @conn.exec(sql)
  changed = res.cmdtuples
  res.clear
  return changed
end

#startObject

Start a new transaction.


134
135
136
137
138
139
140
141
142
143
# File 'lib/og/adapter/postgresql.rb', line 134

def start  
  # neumann: works with earlier PSQL databases too.
  exec('BEGIN TRANSACTION') if @transaction_nesting < 1
  
  if @transaction_nesting >= 1 && @conn.server_version > 80000
    exec("SAVEPOINT SP#{@transaction_nesting}")
  end
  
  @transaction_nesting += 1
end

#table_info(table) ⇒ Object

Returns the PostgreSQL information of a table within the database or nil if it doesn't exist. Mostly for internal usage.


197
198
199
200
# File 'lib/og/adapter/postgresql.rb', line 197

def table_info(table)
  r = query_statement("SELECT c.* FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind = 'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid) AND c.relname = '#{self.class.escape(table.to_s)}'")
  return r && r.blank? ? nil : r.next
end