Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge [6364] from trunk. Consistently quote primary key column names.…

… References #7763.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6365 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4a8d3d51f78b19993d3168d3c86e90e69c4a7ef4 1 parent 243cdde
@jeremy jeremy authored
Showing with 111 additions and 8 deletions.
  1. +2 −0  activerecord/CHANGELOG
  2. +7 −7 activerecord/lib/active_record/base.rb
  3. +1 −0  activerecord/test/fixtures/db_definitions/db2.drop.sql
  4. +5 −0 activerecord/test/fixtures/db_definitions/db2.sql
  5. +2 −0  activerecord/test/fixtures/db_definitions/firebird.drop.sql
  6. +7 −0 activerecord/test/fixtures/db_definitions/firebird.sql
  7. +1 −0  activerecord/test/fixtures/db_definitions/frontbase.drop.sql
  8. +6 −0 activerecord/test/fixtures/db_definitions/frontbase.sql
  9. +1 −0  activerecord/test/fixtures/db_definitions/mysql.drop.sql
  10. +6 −0 activerecord/test/fixtures/db_definitions/mysql.sql
  11. +8 −0 activerecord/test/fixtures/db_definitions/openbase.sql
  12. +2 −0  activerecord/test/fixtures/db_definitions/oracle.drop.sql
  13. +6 −0 activerecord/test/fixtures/db_definitions/oracle.sql
  14. +1 −0  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
  15. +5 −0 activerecord/test/fixtures/db_definitions/postgresql.sql
  16. +1 −0  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
  17. +5 −0 activerecord/test/fixtures/db_definitions/sqlite.sql
  18. +1 −0  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
  19. +5 −0 activerecord/test/fixtures/db_definitions/sqlserver.sql
  20. +1 −0  activerecord/test/fixtures/db_definitions/sybase.drop.sql
  21. +5 −0 activerecord/test/fixtures/db_definitions/sybase.sql
  22. +3 −0  activerecord/test/fixtures/mixed_case_monkey.rb
  23. +6 −0 activerecord/test/fixtures/mixed_case_monkeys.yml
  24. +24 −1 activerecord/test/pk_test.rb
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Consistently quote primary key column names. #7763 [toolmantim]
+
* Fixtures: fix YAML ordered map support. #2665 [Manuel Holtgrewe, nfbuckley]
* Fix has_many :through << with custom foreign keys. #6466, #7153 [naffis, Rich Collins]
View
14 activerecord/lib/active_record/base.rb
@@ -479,7 +479,7 @@ def update(id, attributes)
# Deletes the record with the given +id+ without instantiating an object first. If an array of ids is provided, all of them
# are deleted.
def delete(id)
- delete_all([ "#{primary_key} IN (?)", id ])
+ delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ])
end
# Destroys the record with the given +id+ by instantiating the object and calling #destroy (all the callbacks are the triggered).
@@ -526,12 +526,12 @@ def count_by_sql(sql)
# for looping over a collection where each element require a number of aggregate values. Like the DiscussionBoard
# that needs to list both the number of posts and comments.
def increment_counter(counter_name, id)
- update_all "#{counter_name} = #{counter_name} + 1", "#{primary_key} = #{quote_value(id)}"
+ update_all "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} + 1", "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}"
end
# Works like increment_counter, but decrements instead.
def decrement_counter(counter_name, id)
- update_all "#{counter_name} = #{counter_name} - 1", "#{primary_key} = #{quote_value(id)}"
+ update_all "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} - 1", "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}"
end
@@ -1020,7 +1020,7 @@ def find_from_ids(ids, options)
def find_one(id, options)
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
- options.update :conditions => "#{table_name}.#{primary_key} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
+ options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
# Use find_every(options).first since the primary key condition
# already ensures we have a single record. Using find_initial adds
@@ -1035,7 +1035,7 @@ def find_one(id, options)
def find_some(ids, options)
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
ids_list = ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',')
- options.update :conditions => "#{table_name}.#{primary_key} IN (#{ids_list})#{conditions}"
+ options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} IN (#{ids_list})#{conditions}"
result = find_every(options)
@@ -1558,7 +1558,7 @@ def destroy
unless new_record?
connection.delete <<-end_sql, "#{self.class.name} Destroy"
DELETE FROM #{self.class.table_name}
- WHERE #{self.class.primary_key} = #{quoted_id}
+ WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id}
end_sql
end
@@ -1797,7 +1797,7 @@ def update
connection.update(
"UPDATE #{self.class.table_name} " +
"SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
- "WHERE #{self.class.primary_key} = #{quote_value(id)}",
+ "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}",
"#{self.class.name} Update"
)
end
View
1  activerecord/test/fixtures/db_definitions/db2.drop.sql
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_fk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
+DROP TABLE mixed_case_monkeys;
View
5 activerecord/test/fixtures/db_definitions/db2.sql
@@ -224,3 +224,8 @@ CREATE TABLE numeric_data (
my_house_population DECIMAL(2),
decimal_number_with_default DECIMAL(3,2) DEFAULT 2.78
);
+
+CREATE TABLE mixed_case_monkeys (
+ monkeyID INT NOT NULL PRIMARY KEY,
+ fleaCount INT
+);
View
2  activerecord/test/fixtures/db_definitions/firebird.drop.sql
@@ -30,6 +30,7 @@ DROP TABLE keyboards;
DROP TABLE defaults;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
+DROP TABLE mixed_case_monkeys;
DROP DOMAIN D_BOOLEAN;
@@ -59,3 +60,4 @@ DROP GENERATOR keyboards_seq;
DROP GENERATOR defaults_seq;
DROP GENERATOR legacy_things_seq;
DROP GENERATOR numeric_data_seq;
+DROP GENERATOR mixed_case_monkeys_seq;
View
7 activerecord/test/fixtures/db_definitions/firebird.sql
@@ -295,3 +295,10 @@ CREATE TABLE numeric_data (
);
CREATE GENERATOR numeric_data_seq;
SET GENERATOR numeric_data_seq TO 10000;
+
+CREATE TABLE mixed_case_monkeys (
+ "monkeyID" BIGINT NOT NULL,
+ "fleaCount" INTEGER
+);
+CREATE GENERATOR mixed_case_monkeys_seq;
+SET GENERATOR mixed_case_monkeys_seq TO 10000;
View
1  activerecord/test/fixtures/db_definitions/frontbase.drop.sql
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk CASCADE;
DROP TABLE keyboards CASCADE;
DROP TABLE legacy_things CASCADE;
DROP TABLE numeric_data CASCADE;
+DROP TABLE mixed_case_monkeys CASCADE;
View
6 activerecord/test/fixtures/db_definitions/frontbase.sql
@@ -260,3 +260,9 @@ CREATE TABLE "numeric_data" (
primary key ("id")
);
SET UNIQUE FOR numeric_data(id);
+
+CREATE TABLE mixed_case_monkeys (
+ "monkeyID" integer DEFAULT unique,
+ "fleaCount" integer
+);
+SET UNIQUE FOR mixed_case_monkeys("monkeyID");
View
1  activerecord/test/fixtures/db_definitions/mysql.drop.sql
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
+DROP TABLE mixed_case_monkeys;
View
6 activerecord/test/fixtures/db_definitions/mysql.sql
@@ -226,3 +226,9 @@ CREATE TABLE `numeric_data` (
`my_house_population` decimal(2),
`decimal_number_with_default` decimal(3,2) DEFAULT 2.78
) TYPE=InnoDB;
+
+CREATE TABLE mixed_case_monkeys (
+ `monkeyID` int(11) NOT NULL auto_increment,
+ `fleaCount` int(11),
+ PRIMARY KEY (`monkeyID`)
+) TYPE=InnoDB;
View
8 activerecord/test/fixtures/db_definitions/openbase.sql
@@ -292,3 +292,11 @@ CREATE TABLE numeric_data (
go
CREATE PRIMARY KEY numeric_data (id)
go
+
+CREATE TABLE mixed_case_monkeys (
+ monkeyID INTEGER NOT NULL DEFAULT _rowid,
+ fleaCount INTEGER
+);
+go
+CREATE PRIMARY KEY mixed_case_monkeys (monkeyID)
+go
View
2  activerecord/test/fixtures/db_definitions/oracle.drop.sql
@@ -30,6 +30,7 @@ drop table fk_test_has_fk;
drop table keyboards;
drop table legacy_things;
drop table numeric_data;
+drop table mixed_case_monkeys;
drop sequence accounts_seq;
drop sequence funny_jokes_seq;
@@ -61,3 +62,4 @@ drop sequence fk_test_has_fk_seq;
drop sequence keyboards_seq;
drop sequence legacy_things_seq;
drop sequence numeric_data_seq;
+drop sequence mixed_case_monkeys_seq;
View
6 activerecord/test/fixtures/db_definitions/oracle.sql
@@ -317,3 +317,9 @@ CREATE TABLE numeric_data (
decimal_number_with_default decimal(3,2) DEFAULT 2.78
);
create sequence numeric_data_seq minvalue 10000;
+
+CREATE TABLE mixed_case_monkeys (
+ "monkeyID" INTEGER NOT NULL PRIMARY KEY,
+ "fleaCount" INTEGER
+);
+create sequence mixed_case_monkeys_seq minvalue 10000;
View
1  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
@@ -34,3 +34,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE column_data;
+DROP TABLE mixed_case_monkeys;
View
5 activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -256,3 +256,8 @@ CREATE TABLE numeric_data (
my_house_population decimal(2),
decimal_number_with_default decimal(3,2) default 2.78
);
+
+CREATE TABLE mixed_case_monkeys (
+ "monkeyID" INTEGER PRIMARY KEY,
+ "fleaCount" INTEGER
+);
View
1  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
+DROP TABLE mixed_case_monkeys;
View
5 activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -208,3 +208,8 @@ CREATE TABLE 'numeric_data' (
'my_house_population' DECIMAL(2),
'decimal_number_with_default' DECIMAL(3,2) DEFAULT 2.78
);
+
+CREATE TABLE mixed_case_monkeys (
+ 'monkeyID' INTEGER NOT NULL PRIMARY KEY,
+ 'fleaCount' INTEGER
+);
View
1  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
@@ -31,3 +31,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
+DROP TABLE mixed_case_monkeys;
View
5 activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -236,3 +236,8 @@ CREATE TABLE [order] (
texture varchar(255),
flavor varchar(255)
);
+
+CREATE TABLE mixed_case_monkeys (
+ [monkeyID] int NOT NULL IDENTITY(1, 1),
+ [fleaCount] int default NULL
+);
View
1  activerecord/test/fixtures/db_definitions/sybase.drop.sql
@@ -29,5 +29,6 @@ DROP TABLE fk_test_has_pk
DROP TABLE keyboards
DROP TABLE legacy_things
DROP TABLE numeric_data
+DROP TABLE mixed_case_monkeys
DROP TABLE schema_info
go
View
5 activerecord/test/fixtures/db_definitions/sybase.sql
@@ -210,4 +210,9 @@ CREATE TABLE numeric_data (
decimal_number_with_default numeric(3,2) DEFAULT 2.78
)
+CREATE TABLE mixed_case_monkeys (
+ [monkeyID] numeric(9,0) IDENTITY PRIMARY KEY,
+ [fleaCount] numeric(9,0)
+);
+
go
View
3  activerecord/test/fixtures/mixed_case_monkey.rb
@@ -0,0 +1,3 @@
+class MixedCaseMonkey < ActiveRecord::Base
+ set_primary_key 'monkeyID'
+end
View
6 activerecord/test/fixtures/mixed_case_monkeys.yml
@@ -0,0 +1,6 @@
+first:
+ monkeyID: 1
+ fleaCount: 42
+second:
+ monkeyID: 2
+ fleaCount: 43
View
25 activerecord/test/pk_test.rb
@@ -4,9 +4,10 @@
require 'fixtures/subscriber'
require 'fixtures/movie'
require 'fixtures/keyboard'
+require 'fixtures/mixed_case_monkey'
class PrimaryKeysTest < Test::Unit::TestCase
- fixtures :topics, :subscribers, :movies
+ fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
def test_integer_key
topic = Topic.find(1)
@@ -78,4 +79,26 @@ def test_primary_key_prefix
Topic.reset_primary_key
assert_equal "id", Topic.primary_key
end
+
+ def test_delete_should_quote_pkey
+ assert_nothing_raised { MixedCaseMonkey.delete(1) }
+ end
+ def test_increment_counter_should_quote_pkey_and_quote_counter_columns
+ assert_nothing_raised { MixedCaseMonkey.increment_counter(:fleaCount, 1) }
+ end
+ def test_decrement_counter_should_quote_pkey_and_quote_counter_columns
+ assert_nothing_raised { MixedCaseMonkey.decrement_counter(:fleaCount, 1) }
+ end
+ def test_find_with_one_id_should_quote_pkey
+ assert_nothing_raised { MixedCaseMonkey.find(1) }
+ end
+ def test_find_with_multiple_ids_should_quote_pkey
+ assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
+ end
+ def test_instance_update_should_quote_pkey
+ assert_nothing_raised { MixedCaseMonkey.find(1).save }
+ end
+ def test_instance_destry_should_quote_pkey
+ assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.