Skip to content
Browse files

pick better names and add a little documentation

  • Loading branch information...
1 parent 945c53e commit 3a0d08163a76427611df0c109a5159daaf5b51bf @dougcole dougcole committed Apr 1, 2012
View
24 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -406,7 +406,7 @@ def initialize(connection, logger, connection_parameters, config)
initialize_type_map
@local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"]
- self.use_returning = true
+ self.enable_insert_returning!
end
# Clears the prepared statements cache.
@@ -668,7 +668,7 @@ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
pk = primary_key(table_ref) if table_ref
end
- if pk && use_returning?
+ if pk && use_insert_returning?
select_value("#{sql} RETURNING #{quote_column_name(pk)}")
elsif pk
super
@@ -787,7 +787,7 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
pk = primary_key(table_ref) if table_ref
end
- if pk && use_returning?
+ if pk && use_insert_returning?
sql = "#{sql} RETURNING #{quote_column_name(pk)}"
end
@@ -796,7 +796,7 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
val = exec_query(sql, name, binds)
- if !use_returning? && pk
+ if !use_insert_returning? && pk
unless sequence_name
table_ref = extract_table_ref_from_insert_sql(sql)
sequence_name = default_sequence_name(table_ref, pk)
@@ -1258,12 +1258,20 @@ def extract_schema_and_table(name)
end
end
- def use_returning=(val)
- @use_returning = val
+ # Enable insert statements to use INSERT ... RETURNING ... statements. On by default.
+ def enable_insert_returning!
+ @use_insert_returning = true
end
- def use_returning?
- @use_returning
+ # 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?
+ @use_insert_returning
end
protected
View
16 activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -50,38 +50,38 @@ def test_insert_sql_with_no_space_after_table_name
end
def test_insert_sql_with_returning_disabled
- @connection.use_returning = false
+ @connection.disable_insert_returning!
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
assert_equal expect, id
ensure
- @connection.use_returning = true
+ @connection.enable_insert_returning!
end
def test_exec_insert_with_returning_disabled
- @connection.use_returning = false
+ @connection.disable_insert_returning!
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
assert_equal expect, result.rows.first.first
ensure
- @connection.use_returning = true
+ @connection.enable_insert_returning!
end
def test_exec_insert_with_returning_disabled_and_no_sequence_name_given
- @connection.use_returning = false
+ @connection.disable_insert_returning!
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
assert_equal expect, result.rows.first.first
ensure
- @connection.use_returning = true
+ @connection.enable_insert_returning!
end
def test_sql_for_insert_with_returning_disabled
- @connection.use_returning = false
+ @connection.disable_insert_returning!
result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds')
assert_equal ['sql', 'binds'], result
ensure
- @connection.use_returning = true
+ @connection.enable_insert_returning!
end
def test_serial_sequence

0 comments on commit 3a0d081

Please sign in to comment.
Something went wrong with that request. Please try again.