Class: Og::PsqlStore

Inherits:
SqlStore show all
Extended by:
PsqlUtils
Includes:
PsqlUtils
Defined in:
lib/og/store/psql.rb

Overview

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

Design

The getvalue interface is used instead of each for extra performance.

Instance Attribute Summary

Attributes inherited from SqlStore

#conn

Attributes inherited from Store

#options, #transaction_nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PsqlUtils

escape

Methods included from SqlUtils

#date, #escape, #join_table, #parse_date, #parse_float, #parse_int, #parse_timestamp, #quote, #table, #timestamp

Methods inherited from SqlStore

#commit, #count, #enable_logging, #find, #find_one, #join, #load, #reload, #rollback, #start, #unjoin, #update, #update_properties

Methods inherited from Store

#commit, #count, #delete, #find, for_name, #insert, #load, #reload, #rollback, #save, #start, #transaction, #update, #update_properties

Constructor Details

#initialize(options) ⇒ PsqlStore

Returns a new instance of PsqlStore.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/og/store/psql.rb', line 72

def initialize(options)
	super

	@conn = PGconn.connect(
		options[:address], 
		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 => ex
	# gmosx: any idea how to better test this?
	if ex.to_s =~ /database .* does not exist/i
		Logger.info "Database '#{options[:name]}' not found!"
		self.class.create(options)
		retry
	end
	raise
end

Class Method Details

.create(options) ⇒ Object



61
62
63
64
65
# File 'lib/og/store/psql.rb', line 61

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

.destroy(options) ⇒ Object



67
68
69
70
# File 'lib/og/store/psql.rb', line 67

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

Instance Method Details

#closeObject



100
101
102
103
# File 'lib/og/store/psql.rb', line 100

def close
	@conn.close
	super
end

#enchant(klass, manager) ⇒ Object



105
106
107
108
109
# File 'lib/og/store/psql.rb', line 105

def enchant(klass, manager)
	klass.const_set 'OGSEQ', "#{table(klass)}_oid_seq"
	klass.property :oid, Fixnum, :sql => 'serial PRIMARY KEY'
	super
end

#exec(sql) ⇒ Object



118
119
120
121
122
123
# File 'lib/og/store/psql.rb', line 118

def exec(sql)
	Logger.debug sql if $DBG
	@conn.exec(sql).clear
rescue => ex
	handle_sql_exception(ex, sql)
end

#query(sql) ⇒ Object



111
112
113
114
115
116
# File 'lib/og/store/psql.rb', line 111

def query(sql)
	Logger.debug sql if $DBG
	return @conn.exec(sql)
rescue => ex
	handle_sql_exception(ex, sql)
end