mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
initial exec() method is working in pg adapter
This commit is contained in:
parent
eb83eb6c98
commit
ffb999125a
2 changed files with 55 additions and 0 deletions
|
@ -220,11 +220,19 @@ module ActiveRecord
|
||||||
@local_tz = nil
|
@local_tz = nil
|
||||||
@table_alias_length = nil
|
@table_alias_length = nil
|
||||||
@postgresql_version = nil
|
@postgresql_version = nil
|
||||||
|
@statements = Hash.new { |h,k| h[k] = "a#{h.length + 1}" }
|
||||||
|
|
||||||
connect
|
connect
|
||||||
@local_tz = execute('SHOW TIME ZONE').first["TimeZone"]
|
@local_tz = execute('SHOW TIME ZONE').first["TimeZone"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clear_cache!
|
||||||
|
@statements.each_value do |value|
|
||||||
|
exec "DEALLOCATE #{value}"
|
||||||
|
end
|
||||||
|
@statements.clear
|
||||||
|
end
|
||||||
|
|
||||||
# Is this connection alive and ready for queries?
|
# Is this connection alive and ready for queries?
|
||||||
def active?
|
def active?
|
||||||
if @connection.respond_to?(:status)
|
if @connection.respond_to?(:status)
|
||||||
|
@ -250,8 +258,14 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset!
|
||||||
|
clear_cache!
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
# Close the connection.
|
# Close the connection.
|
||||||
def disconnect!
|
def disconnect!
|
||||||
|
clear_cache!
|
||||||
@connection.close rescue nil
|
@connection.close rescue nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -501,6 +515,30 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def exec(sql, name = 'SQL', binds = [])
|
||||||
|
return async_exec(sql, name, binds) if @async
|
||||||
|
|
||||||
|
log(sql, name) do
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def async_exec(sql, name, binds)
|
||||||
|
log(sql, name) do
|
||||||
|
unless @statements.key? sql
|
||||||
|
@connection.prepare @statements[sql], sql
|
||||||
|
end
|
||||||
|
|
||||||
|
key = @statements[sql]
|
||||||
|
|
||||||
|
# Clear the queue
|
||||||
|
@connection.get_last_result
|
||||||
|
@connection.send_query_prepared(key, [])
|
||||||
|
@connection.block
|
||||||
|
result = @connection.get_last_result
|
||||||
|
ActiveRecord::Result.new(result.fields, result_as_array(result))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Executes an UPDATE query and returns the number of affected tuples.
|
# Executes an UPDATE query and returns the number of affected tuples.
|
||||||
def update_sql(sql, name = nil)
|
def update_sql(sql, name = nil)
|
||||||
super.cmd_tuples
|
super.cmd_tuples
|
||||||
|
|
|
@ -5,6 +5,8 @@ module ActiveRecord
|
||||||
class PostgreSQLAdapterTest < ActiveRecord::TestCase
|
class PostgreSQLAdapterTest < ActiveRecord::TestCase
|
||||||
def setup
|
def setup
|
||||||
@connection = ActiveRecord::Base.connection
|
@connection = ActiveRecord::Base.connection
|
||||||
|
@connection.exec('drop table if exists ex')
|
||||||
|
@connection.exec('create table ex(id serial primary key, data character varying(255))')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_table_alias_length
|
def test_table_alias_length
|
||||||
|
@ -12,6 +14,21 @@ module ActiveRecord
|
||||||
@connection.table_alias_length
|
@connection.table_alias_length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_exec_no_binds
|
||||||
|
result = @connection.exec('SELECT id, data FROM ex')
|
||||||
|
assert_equal 0, result.rows.length
|
||||||
|
assert_equal 2, result.columns.length
|
||||||
|
assert_equal %w{ id data }, result.columns
|
||||||
|
|
||||||
|
string = @connection.quote('foo')
|
||||||
|
@connection.exec("INSERT INTO ex (id, data) VALUES (1, #{string})")
|
||||||
|
result = @connection.exec('SELECT id, data FROM ex')
|
||||||
|
assert_equal 1, result.rows.length
|
||||||
|
assert_equal 2, result.columns.length
|
||||||
|
|
||||||
|
assert_equal [['1', 'foo']], result.rows
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue