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

using the last seen column for quoting

This commit is contained in:
Aaron Patterson 2010-09-27 11:32:04 -07:00
parent a25b9cbc39
commit ec998ae9a6
4 changed files with 23 additions and 3 deletions

View file

@ -6,11 +6,13 @@ module Arel
def initialize engine def initialize engine
@engine = engine @engine = engine
@connection = nil @connection = nil
@last_column = []
@quoted_tables = {} @quoted_tables = {}
@quoted_columns = {} @quoted_columns = {}
end end
def accept object def accept object
@last_column = []
@engine.connection_pool.with_connection do |conn| @engine.connection_pool.with_connection do |conn|
@connection = conn @connection = conn
visit object visit object
@ -226,6 +228,7 @@ module Arel
end end
def visit_Arel_Attributes_Attribute o def visit_Arel_Attributes_Attribute o
@last_column.push o.column
join_name = o.relation.table_alias || o.relation.name join_name = o.relation.table_alias || o.relation.name
"#{quote_table_name join_name}.#{quote_column_name o.name}" "#{quote_table_name join_name}.#{quote_column_name o.name}"
end end
@ -238,7 +241,7 @@ module Arel
alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum
alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated
def visit_String o; quote(o) end def visit_String o; quote(o, @last_column.pop) end
alias :visit_ActiveSupport_Multibyte_Chars :visit_String alias :visit_ActiveSupport_Multibyte_Chars :visit_String
alias :visit_BigDecimal :visit_String alias :visit_BigDecimal :visit_String

View file

@ -37,7 +37,10 @@ module Arel
manager = Arel::InsertManager.new Table.engine manager = Arel::InsertManager.new Table.engine
time = Time.now time = Time.now
manager.insert [[table[:id], time]] attribute = table[:id]
attribute.column.type = :date
manager.insert [[attribute, time]]
manager.to_sql.should be_like %{ manager.to_sql.should be_like %{
INSERT INTO "users" ("id") VALUES (#{Table.engine.connection.quote time}) INSERT INTO "users" ("id") VALUES (#{Table.engine.connection.quote time})
} }

View file

@ -39,6 +39,11 @@ module FakeRecord
end end
def quote thing, column = nil def quote thing, column = nil
if column && column.type == :integer
return 'NULL' if thing.nil?
return thing.to_i
end
case thing case thing
when true when true
"'t'" "'t'"

View file

@ -13,6 +13,12 @@ module Arel
sql = @visitor.accept Nodes::Equality.new(false, false) sql = @visitor.accept Nodes::Equality.new(false, false)
sql.should be_like %{ 'f' = 'f' } sql.should be_like %{ 'f' = 'f' }
end end
it 'should use the column to quote' do
table = Table.new(:users)
sql = @visitor.accept Nodes::Equality.new(table[:id], '1-fooo')
sql.should be_like %{ "users"."id" = 1 }
end
end end
it "should visit_DateTime" do it "should visit_DateTime" do
@ -55,7 +61,9 @@ module Arel
end end
it "should visit_TrueClass" do it "should visit_TrueClass" do
@visitor.accept(@attr.eq(true)).should be_like %{ "users"."id" = 't' } test = @attr.eq(true)
test.left.column.type = :boolean
@visitor.accept(test).should be_like %{ "users"."id" = 't' }
end end
describe "Nodes::In" do describe "Nodes::In" do
@ -91,6 +99,7 @@ module Arel
describe 'Equality' do describe 'Equality' do
it "should escape strings" do it "should escape strings" do
test = @attr.eq 'Aaron Patterson' test = @attr.eq 'Aaron Patterson'
test.left.column.type = :string
@visitor.accept(test).should be_like %{ @visitor.accept(test).should be_like %{
"users"."id" = 'Aaron Patterson' "users"."id" = 'Aaron Patterson'
} }