mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
quoting
This commit is contained in:
parent
648209d11f
commit
b8a4dabab9
14 changed files with 49 additions and 45 deletions
|
@ -6,7 +6,7 @@ class Object
|
|||
def to_sql(builder = EqualsConditionBuilder.new)
|
||||
me = self
|
||||
builder.call do
|
||||
value me
|
||||
value me.to_s
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,4 +1,5 @@
|
|||
class InnerJoinRelation < JoinRelation
|
||||
protected
|
||||
def join_type
|
||||
:inner_join
|
||||
end
|
||||
|
|
|
@ -12,7 +12,7 @@ class JoinRelation < Relation
|
|||
end
|
||||
|
||||
def qualify
|
||||
JoinRelation.new(relation1.qualify, relation2.qualify, *predicates.collect(&:qualify))
|
||||
self.class.new(relation1.qualify, relation2.qualify, *predicates.collect(&:qualify))
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class LeftOuterJoinRelation < JoinRelation
|
||||
protected
|
||||
def join_type
|
||||
:left_outer_join
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ class EqualsConditionBuilder < SqlBuilder
|
|||
end
|
||||
|
||||
def column(table, column, aliaz = nil)
|
||||
@operands << (aliaz ? aliaz : "#{table}.#{column}")
|
||||
@operands << (aliaz ? quote(aliaz) : "#{quote_table_name(table)}.#{quote_column_name(column)}")
|
||||
end
|
||||
|
||||
def value(value)
|
||||
|
|
|
@ -8,6 +8,6 @@ class JoinBuilder < SqlBuilder
|
|||
delegate :call, :to => :@conditions
|
||||
|
||||
def to_s
|
||||
"#{join_type} #{@table} ON #{@conditions}"
|
||||
"#{join_type} #{quote_table_name(@table)} ON #{@conditions}"
|
||||
end
|
||||
end
|
|
@ -5,7 +5,7 @@ class OrderBuilder < SqlBuilder
|
|||
end
|
||||
|
||||
def column(table, column, aliaz = nil)
|
||||
@orders << (aliaz ? aliaz : "#{table}.#{column}")
|
||||
@orders << (aliaz ? quote(aliaz) : "#{quote_table_name(table)}.#{quote_column_name(column)}")
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
|
|
@ -41,7 +41,7 @@ class SelectBuilder < SqlBuilder
|
|||
end
|
||||
|
||||
def from_clause
|
||||
"FROM #{@table} #{@joins}" unless @table.blank?
|
||||
"FROM #{quote_table_name(@table)} #{@joins}" unless @table.blank?
|
||||
end
|
||||
|
||||
def where_clause
|
||||
|
|
|
@ -13,7 +13,7 @@ class SelectsBuilder < SqlBuilder
|
|||
end
|
||||
|
||||
def column(table, column, aliaz = nil)
|
||||
@selects << "#{table}.#{column}" + (aliaz ? " AS #{aliaz}" : '')
|
||||
@selects << "#{quote_table_name(table)}.#{quote_column_name(column)}" + (aliaz ? " AS #{quote(aliaz)}" : '')
|
||||
end
|
||||
|
||||
delegate :blank?, :to => :@selects
|
||||
|
|
|
@ -25,4 +25,11 @@ class SqlBuilder
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
|
||||
|
||||
def connection
|
||||
ActiveRecord::Base.connection
|
||||
end
|
||||
end
|
|
@ -12,30 +12,30 @@ describe 'Relational Algebra' do
|
|||
|
||||
it 'simulates User.has_many :photos' do
|
||||
@user_photos.project(*@photos.attributes).to_s.should be_like("""
|
||||
SELECT photos.id, photos.user_id, photos.camera_id
|
||||
FROM users
|
||||
LEFT OUTER JOIN photos
|
||||
ON users.id = photos.user_id
|
||||
SELECT `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
|
||||
FROM `users`
|
||||
LEFT OUTER JOIN `photos`
|
||||
ON `users`.`id` = `photos`.`user_id`
|
||||
WHERE
|
||||
users.id = 1
|
||||
`users`.`id` = 1
|
||||
""")
|
||||
end
|
||||
|
||||
it 'simulates a User.has_many :cameras :through => :photos' do
|
||||
@user_cameras.project(*@cameras.attributes).to_s.should be_like("""
|
||||
SELECT cameras.id
|
||||
FROM users
|
||||
LEFT OUTER JOIN photos
|
||||
ON users.id = photos.user_id
|
||||
LEFT OUTER JOIN cameras
|
||||
ON photos.camera_id = cameras.id
|
||||
SELECT `cameras`.`id`
|
||||
FROM `users`
|
||||
LEFT OUTER JOIN `photos`
|
||||
ON `users`.`id` = `photos`.`user_id`
|
||||
LEFT OUTER JOIN `cameras`
|
||||
ON `photos`.`camera_id` = `cameras`.`id`
|
||||
WHERE
|
||||
users.id = 1
|
||||
`users`.`id` = 1
|
||||
""")
|
||||
end
|
||||
|
||||
it '' do
|
||||
# @user_cameras.qualify.to_s
|
||||
# p @user_cameras.qualify.to_s
|
||||
#
|
||||
# @users.rename()
|
||||
end
|
||||
|
|
|
@ -20,23 +20,18 @@ describe JoinRelation do
|
|||
|
||||
describe '#qualify' do
|
||||
it 'distributes over the relations and predicates' do
|
||||
JoinRelation.new(@relation1, @relation2, @predicate).qualify. \
|
||||
should == JoinRelation.new(@relation1.qualify, @relation2.qualify, @predicate.qualify)
|
||||
InnerJoinRelation.new(@relation1, @relation2, @predicate).qualify. \
|
||||
should == InnerJoinRelation.new(@relation1.qualify, @relation2.qualify, @predicate.qualify)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_sql' do
|
||||
before do
|
||||
@relation1 = @relation1.select(@relation1[:id] == @relation2[:foo_id])
|
||||
class ConcreteJoinRelation < JoinRelation
|
||||
def join_type
|
||||
:inner_join
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'manufactures sql joining the two tables on the predicate, merging the selects' do
|
||||
ConcreteJoinRelation.new(@relation1, @relation2, @predicate).to_s.should == SelectBuilder.new do
|
||||
InnerJoinRelation.new(@relation1, @relation2, @predicate).to_s.should == SelectBuilder.new do
|
||||
select do
|
||||
column :foo, :name
|
||||
column :foo, :id
|
||||
|
|
|
@ -7,10 +7,10 @@ describe ConditionsBuilder do
|
|||
ConditionsBuilder.new do
|
||||
equals do
|
||||
column(:a, :b)
|
||||
column(:c, :d, :e)
|
||||
column(:c, :d, 'e')
|
||||
end
|
||||
end.to_s.should be_like("""
|
||||
a.b = e
|
||||
`a`.`b` = 'e'
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ describe SelectBuilder do
|
|||
from :users
|
||||
end.to_s.should be_like("""
|
||||
SELECT *
|
||||
FROM users
|
||||
FROM `users`
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
@ -20,13 +20,13 @@ describe SelectBuilder do
|
|||
it 'manufactures correct sql' do
|
||||
SelectBuilder.new do
|
||||
select do
|
||||
column(:a, :b, :c)
|
||||
column(:e, :f)
|
||||
column :a, :b, 'c'
|
||||
column :e, :f
|
||||
end
|
||||
from :users
|
||||
end.to_s.should be_like("""
|
||||
SELECT a.b AS c, e.f
|
||||
FROM users
|
||||
SELECT `a`.`b` AS 'c', `e`.`f`
|
||||
FROM `users`
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
@ -40,14 +40,14 @@ describe SelectBuilder do
|
|||
from :users
|
||||
where do
|
||||
equals do
|
||||
value :a
|
||||
value 1
|
||||
column :b, :c
|
||||
end
|
||||
end
|
||||
end.to_s.should be_like("""
|
||||
SELECT *
|
||||
FROM users
|
||||
WHERE a = b.c
|
||||
FROM `users`
|
||||
WHERE 1 = `b`.`c`
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
@ -61,14 +61,14 @@ describe SelectBuilder do
|
|||
from :users do
|
||||
inner_join(:friendships) do
|
||||
equals do
|
||||
value :id
|
||||
value :user_id
|
||||
column :users, :id
|
||||
column :friendships, :user_id
|
||||
end
|
||||
end
|
||||
end
|
||||
end.to_s.should be_like("""
|
||||
SELECT *
|
||||
FROM users INNER JOIN friendships ON id = user_id
|
||||
FROM `users` INNER JOIN `friendships` ON `users`.`id` = `friendships`.`user_id`
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
@ -82,12 +82,12 @@ describe SelectBuilder do
|
|||
from :users
|
||||
order_by do
|
||||
column :users, :id
|
||||
column :users, :created_at, :alias
|
||||
column :users, :created_at, 'alias'
|
||||
end
|
||||
end.to_s.should be_like("""
|
||||
SELECT *
|
||||
FROM users
|
||||
ORDER BY users.id, alias
|
||||
FROM `users`
|
||||
ORDER BY `users`.`id`, 'alias'
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
@ -103,7 +103,7 @@ describe SelectBuilder do
|
|||
offset 10
|
||||
end.to_s.should be_like("""
|
||||
SELECT *
|
||||
FROM users
|
||||
FROM `users`
|
||||
LIMIT 10
|
||||
OFFSET 10
|
||||
""")
|
||||
|
|
Loading…
Reference in a new issue