Skip to content

Commit

Permalink
refactor configuration of insert_returning
Browse files Browse the repository at this point in the history
  • Loading branch information
dougcole committed Apr 8, 2012
1 parent 3a0d081 commit cd6ddc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def postgresql_connection(config) # :nodoc:
# Forward any unused config params to PGconn.connect. # Forward any unused config params to PGconn.connect.
[:statement_limit, :encoding, :min_messages, :schema_search_path, [:statement_limit, :encoding, :min_messages, :schema_search_path,
:schema_order, :adapter, :pool, :wait_timeout, :template, :schema_order, :adapter, :pool, :wait_timeout, :template,
:reaping_frequency].each do |key| :reaping_frequency, :insert_returning].each do |key|
conn_params.delete key conn_params.delete key
end end
conn_params.delete_if { |k,v| v.nil? } conn_params.delete_if { |k,v| v.nil? }
Expand Down Expand Up @@ -259,6 +259,8 @@ def simplified_type(field_type)
# <encoding></tt> call on the connection. # <encoding></tt> call on the connection.
# * <tt>:min_messages</tt> - An optional client min messages that is used in a # * <tt>:min_messages</tt> - An optional client min messages that is used in a
# <tt>SET client_min_messages TO <min_messages></tt> call on the connection. # <tt>SET client_min_messages TO <min_messages></tt> call on the connection.
# * <tt>:insert_returning</tt> - An optional boolean to control the use or <tt>RETURNING</tt> for <tt>INSERT<tt> statements
# defaults to true.
# #
# Any further options are used as connection parameters to libpq. See # Any further options are used as connection parameters to libpq. See
# http://www.postgresql.org/docs/9.1/static/libpq-connect.html for the # http://www.postgresql.org/docs/9.1/static/libpq-connect.html for the
Expand Down Expand Up @@ -406,7 +408,7 @@ def initialize(connection, logger, connection_parameters, config)


initialize_type_map initialize_type_map
@local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"] @local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"]
self.enable_insert_returning! @use_insert_returning = @config.key?(:insert_returning) ? @config[:insert_returning] : true
end end


# Clears the prepared statements cache. # Clears the prepared statements cache.
Expand Down Expand Up @@ -1258,18 +1260,6 @@ def extract_schema_and_table(name)
end end
end end


# Enable insert statements to use INSERT ... RETURNING ... statements. On by default.
def enable_insert_returning!
@use_insert_returning = true
end

# Disable INSERT ... RETURNING ... statements, using currval() instead.
# Useful for trigger based insertions where INSERT RETURNING does the wrong thing.
def disable_insert_returning!
@use_insert_returning = false
end


def use_insert_returning? def use_insert_returning?
@use_insert_returning @use_insert_returning
end end
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,38 +50,30 @@ def test_insert_sql_with_no_space_after_table_name
end end


def test_insert_sql_with_returning_disabled def test_insert_sql_with_returning_disabled
@connection.disable_insert_returning! connection = connection_without_insert_returning
id = @connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)") id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")
expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, id assert_equal expect, id
ensure
@connection.enable_insert_returning!
end end


def test_exec_insert_with_returning_disabled def test_exec_insert_with_returning_disabled
@connection.disable_insert_returning! connection = connection_without_insert_returning
result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq') result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq')
expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, result.rows.first.first assert_equal expect, result.rows.first.first
ensure
@connection.enable_insert_returning!
end end


def test_exec_insert_with_returning_disabled_and_no_sequence_name_given def test_exec_insert_with_returning_disabled_and_no_sequence_name_given
@connection.disable_insert_returning! connection = connection_without_insert_returning
result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id') result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id')
expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, result.rows.first.first assert_equal expect, result.rows.first.first
ensure
@connection.enable_insert_returning!
end end


def test_sql_for_insert_with_returning_disabled def test_sql_for_insert_with_returning_disabled
@connection.disable_insert_returning! connection = connection_without_insert_returning
result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds') result = connection.sql_for_insert('sql', nil, nil, nil, 'binds')
assert_equal ['sql', 'binds'], result assert_equal ['sql', 'binds'], result
ensure
@connection.enable_insert_returning!
end end


def test_serial_sequence def test_serial_sequence
Expand Down Expand Up @@ -239,6 +231,10 @@ def insert(ctx, data)


ctx.exec_insert(sql, 'SQL', binds) ctx.exec_insert(sql, 'SQL', binds)
end end

def connection_without_insert_returning
ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.configurations['arunit'].merge(:insert_returning => false))
end
end end
end end
end end

0 comments on commit cd6ddc8

Please sign in to comment.