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