public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/JackDanger/rails.git
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
jeremy (author)
Thu Mar 08 19:25:37 -0800 2007
commit  4a8d3d51f78b19993d3168d3c86e90e69c4a7ef4
tree    935713ed650af6fa37b0ec4ff4d23d874e2bb8d2
parent  243cdde6a849b3051739fd8824e4c62df6bd761a
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Consistently quote primary key column names. #7763 [toolmantim]
0
+
0
 * Fixtures: fix YAML ordered map support. #2665 [Manuel Holtgrewe, nfbuckley]
0
 
0
 * Fix has_many :through << with custom foreign keys. #6466, #7153 [naffis, Rich Collins]
...
479
480
481
482
 
483
484
485
...
526
527
528
529
 
530
531
532
533
534
 
535
536
537
...
1020
1021
1022
1023
 
1024
1025
1026
...
1035
1036
1037
1038
 
1039
1040
1041
...
1558
1559
1560
1561
 
1562
1563
1564
...
1797
1798
1799
1800
 
1801
1802
1803
...
479
480
481
 
482
483
484
485
...
526
527
528
 
529
530
531
532
533
 
534
535
536
537
...
1020
1021
1022
 
1023
1024
1025
1026
...
1035
1036
1037
 
1038
1039
1040
1041
...
1558
1559
1560
 
1561
1562
1563
1564
...
1797
1798
1799
 
1800
1801
1802
1803
0
@@ -479,7 +479,7 @@ module ActiveRecord #:nodoc:
0
       # Deletes the record with the given +id+ without instantiating an object first. If an array of ids is provided, all of them
0
       # are deleted.
0
       def delete(id)
0
- delete_all([ "#{primary_key} IN (?)", id ])
0
+ delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ])
0
       end
0
 
0
       # Destroys the record with the given +id+ by instantiating the object and calling #destroy (all the callbacks are the triggered).
0
@@ -526,12 +526,12 @@ module ActiveRecord #:nodoc:
0
       # for looping over a collection where each element require a number of aggregate values. Like the DiscussionBoard
0
       # that needs to list both the number of posts and comments.
0
       def increment_counter(counter_name, id)
0
- update_all "#{counter_name} = #{counter_name} + 1", "#{primary_key} = #{quote_value(id)}"
0
+ update_all "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} + 1", "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}"
0
       end
0
 
0
       # Works like increment_counter, but decrements instead.
0
       def decrement_counter(counter_name, id)
0
- update_all "#{counter_name} = #{counter_name} - 1", "#{primary_key} = #{quote_value(id)}"
0
+ update_all "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} - 1", "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}"
0
       end
0
 
0
 
0
@@ -1020,7 +1020,7 @@ module ActiveRecord #:nodoc:
0
       
0
         def find_one(id, options)
0
           conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
0
- options.update :conditions => "#{table_name}.#{primary_key} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
0
+ options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
0
 
0
           # Use find_every(options).first since the primary key condition
0
           # already ensures we have a single record. Using find_initial adds
0
@@ -1035,7 +1035,7 @@ module ActiveRecord #:nodoc:
0
         def find_some(ids, options)
0
           conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
0
           ids_list = ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',')
0
- options.update :conditions => "#{table_name}.#{primary_key} IN (#{ids_list})#{conditions}"
0
+ options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} IN (#{ids_list})#{conditions}"
0
 
0
           result = find_every(options)
0
 
0
@@ -1558,7 +1558,7 @@ module ActiveRecord #:nodoc:
0
         unless new_record?
0
           connection.delete <<-end_sql, "#{self.class.name} Destroy"
0
             DELETE FROM #{self.class.table_name}
0
- WHERE #{self.class.primary_key} = #{quoted_id}
0
+ WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id}
0
           end_sql
0
         end
0
 
0
@@ -1797,7 +1797,7 @@ module ActiveRecord #:nodoc:
0
         connection.update(
0
           "UPDATE #{self.class.table_name} " +
0
           "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
0
- "WHERE #{self.class.primary_key} = #{quote_value(id)}",
0
+ "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}",
0
           "#{self.class.name} Update"
0
         )
0
       end
...
29
30
31
 
...
29
30
31
32
0
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_fk;
0
 DROP TABLE keyboards;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
+DROP TABLE mixed_case_monkeys;
...
224
225
226
 
 
 
 
 
...
224
225
226
227
228
229
230
231
0
@@ -224,3 +224,8 @@ CREATE TABLE numeric_data (
0
   my_house_population DECIMAL(2),
0
   decimal_number_with_default DECIMAL(3,2) DEFAULT 2.78
0
 );
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ monkeyID INT NOT NULL PRIMARY KEY,
0
+ fleaCount INT
0
+);
...
30
31
32
 
33
34
35
...
59
60
61
 
62
...
30
31
32
33
34
35
36
...
60
61
62
63
64
0
@@ -30,6 +30,7 @@ DROP TABLE keyboards;
0
 DROP TABLE defaults;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
+DROP TABLE mixed_case_monkeys;
0
 
0
 DROP DOMAIN D_BOOLEAN;
0
 
0
@@ -59,3 +60,4 @@ DROP GENERATOR keyboards_seq;
0
 DROP GENERATOR defaults_seq;
0
 DROP GENERATOR legacy_things_seq;
0
 DROP GENERATOR numeric_data_seq;
0
+DROP GENERATOR mixed_case_monkeys_seq;
0
\ No newline at end of file
...
295
296
297
 
 
 
 
 
 
 
...
295
296
297
298
299
300
301
302
303
304
0
@@ -295,3 +295,10 @@ CREATE TABLE numeric_data (
0
 );
0
 CREATE GENERATOR numeric_data_seq;
0
 SET GENERATOR numeric_data_seq TO 10000;
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ "monkeyID" BIGINT NOT NULL,
0
+ "fleaCount" INTEGER
0
+);
0
+CREATE GENERATOR mixed_case_monkeys_seq;
0
+SET GENERATOR mixed_case_monkeys_seq TO 10000;
...
29
30
31
 
...
29
30
31
32
0
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk CASCADE;
0
 DROP TABLE keyboards CASCADE;
0
 DROP TABLE legacy_things CASCADE;
0
 DROP TABLE numeric_data CASCADE;
0
+DROP TABLE mixed_case_monkeys CASCADE;
...
260
261
262
 
 
 
 
 
 
...
260
261
262
263
264
265
266
267
268
0
@@ -260,3 +260,9 @@ CREATE TABLE "numeric_data" (
0
   primary key ("id")
0
 );
0
 SET UNIQUE FOR numeric_data(id);
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ "monkeyID" integer DEFAULT unique,
0
+ "fleaCount" integer
0
+);
0
+SET UNIQUE FOR mixed_case_monkeys("monkeyID");
...
29
30
31
 
...
29
30
31
32
0
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk;
0
 DROP TABLE keyboards;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
+DROP TABLE mixed_case_monkeys;
...
226
227
228
 
 
 
 
 
 
...
226
227
228
229
230
231
232
233
234
0
@@ -226,3 +226,9 @@ CREATE TABLE `numeric_data` (
0
   `my_house_population` decimal(2),
0
   `decimal_number_with_default` decimal(3,2) DEFAULT 2.78
0
 ) TYPE=InnoDB;
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ `monkeyID` int(11) NOT NULL auto_increment,
0
+ `fleaCount` int(11),
0
+ PRIMARY KEY (`monkeyID`)
0
+) TYPE=InnoDB;
...
292
293
294
 
 
 
 
 
 
 
 
...
292
293
294
295
296
297
298
299
300
301
302
0
@@ -292,3 +292,11 @@ CREATE TABLE numeric_data (
0
 go
0
 CREATE PRIMARY KEY numeric_data (id)
0
 go
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ monkeyID INTEGER NOT NULL DEFAULT _rowid,
0
+ fleaCount INTEGER
0
+);
0
+go
0
+CREATE PRIMARY KEY mixed_case_monkeys (monkeyID)
0
+go
...
30
31
32
 
33
34
35
...
61
62
63
 
...
30
31
32
33
34
35
36
...
62
63
64
65
0
@@ -30,6 +30,7 @@ drop table fk_test_has_fk;
0
 drop table keyboards;
0
 drop table legacy_things;
0
 drop table numeric_data;
0
+drop table mixed_case_monkeys;
0
 
0
 drop sequence accounts_seq;
0
 drop sequence funny_jokes_seq;
0
@@ -61,3 +62,4 @@ drop sequence fk_test_has_fk_seq;
0
 drop sequence keyboards_seq;
0
 drop sequence legacy_things_seq;
0
 drop sequence numeric_data_seq;
0
+drop sequence mixed_case_monkeys_seq;
...
317
318
319
 
 
 
 
 
 
...
317
318
319
320
321
322
323
324
325
0
@@ -317,3 +317,9 @@ CREATE TABLE numeric_data (
0
   decimal_number_with_default decimal(3,2) DEFAULT 2.78
0
 );
0
 create sequence numeric_data_seq minvalue 10000;
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ "monkeyID" INTEGER NOT NULL PRIMARY KEY,
0
+ "fleaCount" INTEGER
0
+);
0
+create sequence mixed_case_monkeys_seq minvalue 10000;
...
34
35
36
 
...
34
35
36
37
0
@@ -34,3 +34,4 @@ DROP TABLE keyboards;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
 DROP TABLE column_data;
0
+DROP TABLE mixed_case_monkeys;
...
256
257
258
 
 
 
 
 
...
256
257
258
259
260
261
262
263
0
@@ -256,3 +256,8 @@ CREATE TABLE numeric_data (
0
   my_house_population decimal(2),
0
   decimal_number_with_default decimal(3,2) default 2.78
0
 );
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ "monkeyID" INTEGER PRIMARY KEY,
0
+ "fleaCount" INTEGER
0
+);
...
29
30
31
 
...
29
30
31
32
0
@@ -29,3 +29,4 @@ DROP TABLE fk_test_has_pk;
0
 DROP TABLE keyboards;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
+DROP TABLE mixed_case_monkeys;
...
208
209
210
 
 
 
 
 
...
208
209
210
211
212
213
214
215
0
@@ -208,3 +208,8 @@ CREATE TABLE 'numeric_data' (
0
   'my_house_population' DECIMAL(2),
0
   'decimal_number_with_default' DECIMAL(3,2) DEFAULT 2.78
0
 );
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ 'monkeyID' INTEGER NOT NULL PRIMARY KEY,
0
+ 'fleaCount' INTEGER
0
+);
...
31
32
33
 
...
31
32
33
34
0
@@ -31,3 +31,4 @@ DROP TABLE keyboards;
0
 DROP TABLE legacy_things;
0
 DROP TABLE numeric_data;
0
 DROP TABLE [order];
0
+DROP TABLE mixed_case_monkeys;
...
236
237
238
 
 
 
 
 
...
236
237
238
239
240
241
242
243
0
@@ -236,3 +236,8 @@ CREATE TABLE [order] (
0
   texture varchar(255),
0
   flavor varchar(255)
0
 );
0
+
0
+CREATE TABLE mixed_case_monkeys (
0
+ [monkeyID] int NOT NULL IDENTITY(1, 1),
0
+ [fleaCount] int default NULL
0
+);
...
29
30
31
 
32
33
...
29
30
31
32
33
34
0
@@ -29,5 +29,6 @@ DROP TABLE fk_test_has_pk
0
 DROP TABLE keyboards
0
 DROP TABLE legacy_things
0
 DROP TABLE numeric_data
0
+DROP TABLE mixed_case_monkeys
0
 DROP TABLE schema_info
0
 go
...
210
211
212
 
 
 
 
 
213
...
210
211
212
213
214
215
216
217
218
0
@@ -210,4 +210,9 @@ CREATE TABLE numeric_data (
0
   decimal_number_with_default numeric(3,2) DEFAULT 2.78
0
 )
0
 
0
+CREATE TABLE mixed_case_monkeys (
0
+ [monkeyID] numeric(9,0) IDENTITY PRIMARY KEY,
0
+ [fleaCount] numeric(9,0)
0
+);
0
+
0
 go
...
4
5
6
 
7
8
9
 
10
11
12
...
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
...
4
5
6
7
8
9
 
10
11
12
13
...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
0
@@ -4,9 +4,10 @@ require 'fixtures/reply'
0
 require 'fixtures/subscriber'
0
 require 'fixtures/movie'
0
 require 'fixtures/keyboard'
0
+require 'fixtures/mixed_case_monkey'
0
 
0
 class PrimaryKeysTest < Test::Unit::TestCase
0
- fixtures :topics, :subscribers, :movies
0
+ fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
0
 
0
   def test_integer_key
0
     topic = Topic.find(1)
0
@@ -78,4 +79,26 @@ class PrimaryKeysTest < Test::Unit::TestCase
0
     Topic.reset_primary_key
0
     assert_equal "id", Topic.primary_key
0
   end
0
+
0
+ def test_delete_should_quote_pkey
0
+ assert_nothing_raised { MixedCaseMonkey.delete(1) }
0
+ end
0
+ def test_increment_counter_should_quote_pkey_and_quote_counter_columns
0
+ assert_nothing_raised { MixedCaseMonkey.increment_counter(:fleaCount, 1) }
0
+ end
0
+ def test_decrement_counter_should_quote_pkey_and_quote_counter_columns
0
+ assert_nothing_raised { MixedCaseMonkey.decrement_counter(:fleaCount, 1) }
0
+ end
0
+ def test_find_with_one_id_should_quote_pkey
0
+ assert_nothing_raised { MixedCaseMonkey.find(1) }
0
+ end
0
+ def test_find_with_multiple_ids_should_quote_pkey
0
+ assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
0
+ end
0
+ def test_instance_update_should_quote_pkey
0
+ assert_nothing_raised { MixedCaseMonkey.find(1).save }
0
+ end
0
+ def test_instance_destry_should_quote_pkey
0
+ assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
0
+ end
0
 end

Comments

    No one has commented yet.