1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Consistently quote primary key column names. Closes #7763.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6364 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-03-09 03:23:37 +00:00
parent 49d1f6a813
commit 98165fd31e
24 changed files with 108 additions and 8 deletions

View file

@ -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]

View file

@ -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 <tt>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

View file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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
);

View file

@ -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 file

@ -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 file

@ -31,3 +31,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
DROP TABLE mixed_case_monkeys;

View file

@ -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 file

@ -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 file

@ -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 file

@ -0,0 +1,3 @@
class MixedCaseMonkey < ActiveRecord::Base
set_primary_key 'monkeyID'
end

View file

@ -0,0 +1,6 @@
first:
monkeyID: 1
fleaCount: 42
second:
monkeyID: 2
fleaCount: 43

View file

@ -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