From 98165fd31e7be605ef37e096e8423ce8953ef4ed Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 9 Mar 2007 03:23:37 +0000 Subject: [PATCH] Consistently quote primary key column names. Closes #7763. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6364 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 14 ++++++------ .../test/fixtures/db_definitions/db2.drop.sql | 1 + .../test/fixtures/db_definitions/db2.sql | 5 +++++ .../fixtures/db_definitions/firebird.drop.sql | 2 ++ .../test/fixtures/db_definitions/firebird.sql | 7 ++++++ .../db_definitions/frontbase.drop.sql | 1 + .../fixtures/db_definitions/frontbase.sql | 6 +++++ .../fixtures/db_definitions/mysql.drop.sql | 1 + .../test/fixtures/db_definitions/mysql.sql | 6 +++++ .../test/fixtures/db_definitions/openbase.sql | 8 +++++++ .../fixtures/db_definitions/oracle.drop.sql | 2 ++ .../test/fixtures/db_definitions/oracle.sql | 6 +++++ .../db_definitions/postgresql.drop.sql | 1 + .../fixtures/db_definitions/postgresql.sql | 5 +++++ .../fixtures/db_definitions/sqlite.drop.sql | 1 + .../test/fixtures/db_definitions/sqlite.sql | 5 +++++ .../db_definitions/sqlserver.drop.sql | 1 + .../fixtures/db_definitions/sqlserver.sql | 5 +++++ .../fixtures/db_definitions/sybase.drop.sql | 1 + .../test/fixtures/db_definitions/sybase.sql | 5 +++++ .../test/fixtures/mixed_case_monkey.rb | 3 +++ .../test/fixtures/mixed_case_monkeys.yml | 6 +++++ activerecord/test/pk_test.rb | 22 ++++++++++++++++++- 24 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 activerecord/test/fixtures/mixed_case_monkey.rb create mode 100644 activerecord/test/fixtures/mixed_case_monkeys.yml diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 1b961e0c1f..d044c7a845 100644 --- a/activerecord/CHANGELOG +++ b/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] * DateTimes assume the default timezone. #7764 [Geoff Buesing] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3a9783bac5..f3d4cf856a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -482,7 +482,7 @@ module ActiveRecord #:nodoc: # 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). @@ -537,9 +537,9 @@ module ActiveRecord #:nodoc: def update_counters(id, counters) updates = counters.inject([]) { |list, (counter_name, increment)| sign = increment < 0 ? "-" : "+" - list << "#{counter_name} = #{counter_name} #{sign} #{increment.abs}" + list << "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} #{sign} #{increment.abs}" }.join(", ") - update_all(updates, "#{primary_key} = #{quote_value(id)}") + update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}") end # Increments the specified counter by one. So DiscussionBoard.increment_counter("post_count", @@ -1047,7 +1047,7 @@ module ActiveRecord #:nodoc: 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 @@ -1062,7 +1062,7 @@ module ActiveRecord #:nodoc: 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) @@ -1608,7 +1608,7 @@ module ActiveRecord #:nodoc: 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 @@ -1847,7 +1847,7 @@ module ActiveRecord #:nodoc: 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 diff --git a/activerecord/test/fixtures/db_definitions/db2.drop.sql b/activerecord/test/fixtures/db_definitions/db2.drop.sql index 50cffe3b47..9837def303 100644 --- a/activerecord/test/fixtures/db_definitions/db2.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index ee76fc4d05..750e74a0c3 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/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 +); diff --git a/activerecord/test/fixtures/db_definitions/firebird.drop.sql b/activerecord/test/fixtures/db_definitions/firebird.drop.sql index 2e43243af7..69aa093150 100644 --- a/activerecord/test/fixtures/db_definitions/firebird.drop.sql +++ b/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; \ No newline at end of file diff --git a/activerecord/test/fixtures/db_definitions/firebird.sql b/activerecord/test/fixtures/db_definitions/firebird.sql index 8ca0bf6222..779701c354 100644 --- a/activerecord/test/fixtures/db_definitions/firebird.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/frontbase.drop.sql b/activerecord/test/fixtures/db_definitions/frontbase.drop.sql index 5fc64f769b..1c87fdf7ed 100644 --- a/activerecord/test/fixtures/db_definitions/frontbase.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/frontbase.sql b/activerecord/test/fixtures/db_definitions/frontbase.sql index e102f65aa7..798b0204d2 100644 --- a/activerecord/test/fixtures/db_definitions/frontbase.sql +++ b/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"); diff --git a/activerecord/test/fixtures/db_definitions/mysql.drop.sql b/activerecord/test/fixtures/db_definitions/mysql.drop.sql index cb6a870a7c..f269b3a3b7 100644 --- a/activerecord/test/fixtures/db_definitions/mysql.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 8a56ab2172..1450d20890 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/openbase.sql b/activerecord/test/fixtures/db_definitions/openbase.sql index c3da2efee6..4f5d27e63d 100644 --- a/activerecord/test/fixtures/db_definitions/openbase.sql +++ b/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 diff --git a/activerecord/test/fixtures/db_definitions/oracle.drop.sql b/activerecord/test/fixtures/db_definitions/oracle.drop.sql index 6fa01cdf51..910b118e05 100644 --- a/activerecord/test/fixtures/db_definitions/oracle.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/oracle.sql b/activerecord/test/fixtures/db_definitions/oracle.sql index 869c59fb75..f84a86a655 100644 --- a/activerecord/test/fixtures/db_definitions/oracle.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql index 4910b0ecee..3afe6d39a9 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 18f2ededeb..15a61ea8c4 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -240,3 +240,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 +); diff --git a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql index cb6a870a7c..f269b3a3b7 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index edf1e815da..973639f177 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/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 +); diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql index e8327f0826..c415d223f5 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql +++ b/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; diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 50e1938d0a..3cec4e4a16 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/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 +); diff --git a/activerecord/test/fixtures/db_definitions/sybase.drop.sql b/activerecord/test/fixtures/db_definitions/sybase.drop.sql index ebf2fc9736..9d85221610 100644 --- a/activerecord/test/fixtures/db_definitions/sybase.drop.sql +++ b/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 diff --git a/activerecord/test/fixtures/db_definitions/sybase.sql b/activerecord/test/fixtures/db_definitions/sybase.sql index 8544a4150e..6c42c66031 100644 --- a/activerecord/test/fixtures/db_definitions/sybase.sql +++ b/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 diff --git a/activerecord/test/fixtures/mixed_case_monkey.rb b/activerecord/test/fixtures/mixed_case_monkey.rb new file mode 100644 index 0000000000..853f2682b3 --- /dev/null +++ b/activerecord/test/fixtures/mixed_case_monkey.rb @@ -0,0 +1,3 @@ +class MixedCaseMonkey < ActiveRecord::Base + set_primary_key 'monkeyID' +end diff --git a/activerecord/test/fixtures/mixed_case_monkeys.yml b/activerecord/test/fixtures/mixed_case_monkeys.yml new file mode 100644 index 0000000000..eecd448f4b --- /dev/null +++ b/activerecord/test/fixtures/mixed_case_monkeys.yml @@ -0,0 +1,6 @@ +first: + monkeyID: 1 + fleaCount: 42 +second: + monkeyID: 2 + fleaCount: 43 diff --git a/activerecord/test/pk_test.rb b/activerecord/test/pk_test.rb index 604b263571..4daef43062 100644 --- a/activerecord/test/pk_test.rb +++ b/activerecord/test/pk_test.rb @@ -4,9 +4,10 @@ require 'fixtures/reply' 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,23 @@ class PrimaryKeysTest < Test::Unit::TestCase 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_update_counters_should_quote_pkey_and_quote_counter_columns + assert_nothing_raised { MixedCaseMonkey.update_counters(1, :fleaCount => 99) } + 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