Skip to content

Commit

Permalink
Consistently quote primary key column names. Closes #7763.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6364 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Mar 9, 2007
1 parent 49d1f6a commit 98165fd
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 8 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
14 changes: 7 additions & 7 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -482,7 +482,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).
Expand Down Expand Up @@ -537,9 +537,9 @@ def count_by_sql(sql)
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",
Expand Down Expand Up @@ -1047,7 +1047,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
Expand All @@ -1062,7 +1062,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)

Expand Down Expand Up @@ -1608,7 +1608,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

Expand Down Expand Up @@ -1847,7 +1847,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
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/db2.drop.sql
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/db2.sql
Expand Up @@ -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
);
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/db_definitions/firebird.drop.sql
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
7 changes: 7 additions & 0 deletions activerecord/test/fixtures/db_definitions/firebird.sql
Expand Up @@ -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;
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions activerecord/test/fixtures/db_definitions/frontbase.sql
Expand Up @@ -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");
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/mysql.drop.sql
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions activerecord/test/fixtures/db_definitions/mysql.sql
Expand Up @@ -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;
8 changes: 8 additions & 0 deletions activerecord/test/fixtures/db_definitions/openbase.sql
Expand Up @@ -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
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/db_definitions/oracle.drop.sql
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
6 changes: 6 additions & 0 deletions activerecord/test/fixtures/db_definitions/oracle.sql
Expand Up @@ -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;
Expand Up @@ -34,3 +34,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE column_data;
DROP TABLE mixed_case_monkeys;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/postgresql.sql
Expand Up @@ -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
);
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/sqlite.drop.sql
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlite.sql
Expand Up @@ -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
);
Expand Up @@ -31,3 +31,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
DROP TABLE mixed_case_monkeys;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlserver.sql
Expand Up @@ -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
);
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/sybase.drop.sql
Expand Up @@ -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
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/sybase.sql
Expand Up @@ -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
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/mixed_case_monkey.rb
@@ -0,0 +1,3 @@
class MixedCaseMonkey < ActiveRecord::Base
set_primary_key 'monkeyID'
end
6 changes: 6 additions & 0 deletions activerecord/test/fixtures/mixed_case_monkeys.yml
@@ -0,0 +1,6 @@
first:
monkeyID: 1
fleaCount: 42
second:
monkeyID: 2
fleaCount: 43
22 changes: 21 additions & 1 deletion activerecord/test/pk_test.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -78,4 +79,23 @@ 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_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

0 comments on commit 98165fd

Please sign in to comment.