Class: Sequel::Postgres::Database
Constant Summary
collapse
- RELATION_QUERY =
{:from => [:pg_class], :select => [:relname]}.freeze
- RELATION_FILTER =
"(relkind = 'r') AND (relname !~ '^pg|sql')".freeze
- SYSTEM_TABLE_REGEXP =
/^pg|sql/.freeze
- RE_CURRVAL_ERROR =
/currval of sequence "(.*)" is not yet defined in this session/.freeze
- SQL_BEGIN =
'BEGIN'.freeze
- SQL_COMMIT =
'COMMIT'.freeze
- SQL_ROLLBACK =
'ROLLBACK'.freeze
Constants included
from Schema::SQL
Schema::SQL::AUTOINCREMENT, Schema::SQL::CASCADE, Schema::SQL::COMMA_SEPARATOR, Schema::SQL::NOT_NULL, Schema::SQL::NO_ACTION, Schema::SQL::PRIMARY_KEY, Schema::SQL::RESTRICT, Schema::SQL::SET_DEFAULT, Schema::SQL::SET_NULL, Schema::SQL::TYPES, Schema::SQL::UNDERSCORE, Schema::SQL::UNIQUE
Instance Attribute Summary
Attributes inherited from Database
#logger, #opts, #pool
Instance Method Summary
collapse
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #drop_column, #drop_index, #drop_table, #drop_view, #fetch, #from, #initialize, #multi_threaded?, #query, #rename_column, #rename_table, #select, set_adapter_scheme, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #table_exists?, #test_connection, #uri, uri_to_options
#alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #create_table_sql_list, #default_index_name, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #rename_table_sql, #schema_utility_dataset
Instance Method Details
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/sequel/adapters/postgres.rb', line 174
def connect
conn = PGconn.connect(
@opts[:host] || 'localhost',
@opts[:port] || 5432,
'', '',
@opts[:database],
@opts[:user],
@opts[:password]
)
if encoding = @opts[:encoding] || @opts[:charset]
conn.set_client_encoding(encoding)
end
conn
end
|
#dataset(opts = nil) ⇒ Object
193
194
195
|
# File 'lib/sequel/adapters/postgres.rb', line 193
def dataset(opts = nil)
Postgres::Dataset.new(self, opts)
end
|
#disconnect ⇒ Object
189
190
191
|
# File 'lib/sequel/adapters/postgres.rb', line 189
def disconnect
@pool.disconnect {|c| c.close}
end
|
#drop_table_sql(name) ⇒ Object
310
311
312
|
# File 'lib/sequel/adapters/postgres.rb', line 310
def drop_table_sql(name)
"DROP TABLE #{name} CASCADE"
end
|
#execute(sql) ⇒ Object
211
212
213
214
215
216
217
|
# File 'lib/sequel/adapters/postgres.rb', line 211
def execute(sql)
@logger.info(sql) if @logger
@pool.hold {|conn| conn.execute(sql)}
rescue => e
@logger.error(e.message) if @logger
raise e
end
|
#execute_and_forget(sql) ⇒ Object
219
220
221
222
223
224
225
|
# File 'lib/sequel/adapters/postgres.rb', line 219
def execute_and_forget(sql)
@logger.info(sql) if @logger
@pool.hold {|conn| conn.execute(sql).clear}
rescue => e
@logger.error(e.message) if @logger
raise e
end
|
#execute_insert(sql, table, values) ⇒ Object
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/sequel/adapters/postgres.rb', line 258
def execute_insert(sql, table, values)
@logger.info(sql) if @logger
@pool.hold do |conn|
conn.execute(sql).clear
insert_result(conn, table, values)
end
rescue => e
@logger.error(e.message) if @logger
raise e
end
|
#insert_result(conn, table, values) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/sequel/adapters/postgres.rb', line 234
def insert_result(conn, table, values)
begin
result = conn.last_insert_id(table)
return result if result
rescue PGError => e
if e.message =~ RE_CURRVAL_ERROR
raise Error, "Could not return primary key value for the inserted record. Are you specifying a primary key value for a serial primary key?"
else
raise e
end
end
case values
when Hash
values[primary_key_for_table(conn, table)]
when Array
values.first
else
nil
end
end
|
205
206
207
208
209
|
# File 'lib/sequel/adapters/postgres.rb', line 205
def locks
dataset.from("pg_class, pg_locks").
select("pg_class.relname, pg_locks.*").
filter("pg_class.relfilenode=pg_locks.relation")
end
|
#primary_key_for_table(conn, table) ⇒ Object
227
228
229
230
|
# File 'lib/sequel/adapters/postgres.rb', line 227
def primary_key_for_table(conn, table)
@primary_keys ||= {}
@primary_keys[table] ||= conn.primary_key(table)
end
|
#serial_primary_key_options ⇒ Object
306
307
308
|
# File 'lib/sequel/adapters/postgres.rb', line 306
def serial_primary_key_options
{:primary_key => true, :type => :serial}
end
|
#synchronize(&block) ⇒ Object
269
270
271
|
# File 'lib/sequel/adapters/postgres.rb', line 269
def synchronize(&block)
@pool.hold(&block)
end
|
201
202
203
|
# File 'lib/sequel/adapters/postgres.rb', line 201
def tables
dataset(RELATION_QUERY).filter(RELATION_FILTER).map {|r| r[:relname].to_sym}
end
|
#transaction ⇒ Object
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/sequel/adapters/postgres.rb', line 277
def transaction
@pool.hold do |conn|
if conn.transaction_in_progress
yield conn
else
@logger.info(SQL_BEGIN) if @logger
conn.async_exec(SQL_BEGIN)
begin
conn.transaction_in_progress = true
result = yield
begin
@logger.info(SQL_COMMIT) if @logger
conn.async_exec(SQL_COMMIT)
rescue => e
@logger.error(e.message) if @logger
raise e
end
result
rescue => e
@logger.info(SQL_ROLLBACK) if @logger
conn.async_exec(SQL_ROLLBACK) rescue nil
raise e unless Error::Rollback === e
ensure
conn.transaction_in_progress = nil
end
end
end
end
|