aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/active_relation.rb2
-rw-r--r--lib/active_relation/extensions/hash.rb2
-rw-r--r--lib/active_relation/extensions/object.rb7
-rw-r--r--lib/active_relation/predicates.rb39
-rw-r--r--lib/active_relation/primitives/aggregation.rb6
-rw-r--r--lib/active_relation/primitives/attribute.rb6
-rw-r--r--lib/active_relation/relations/base.rb10
-rw-r--r--lib/active_relation/relations/deletion.rb2
-rw-r--r--lib/active_relation/relations/insertion.rb2
-rw-r--r--lib/active_relation/relations/join.rb2
-rw-r--r--lib/active_relation/sql.rb51
-rw-r--r--lib/active_relation/sql_builder.rb9
-rw-r--r--spec/active_relation/predicates/equality_spec.rb3
13 files changed, 86 insertions, 55 deletions
diff --git a/lib/active_relation.rb b/lib/active_relation.rb
index 0b55ed74d0..23a80a9c38 100644
--- a/lib/active_relation.rb
+++ b/lib/active_relation.rb
@@ -4,7 +4,7 @@ require 'rubygems'
require 'activesupport'
require 'activerecord'
-require 'active_relation/sql_builder'
+require 'active_relation/sql'
require 'active_relation/extensions'
require 'active_relation/predicates'
require 'active_relation/relations'
diff --git a/lib/active_relation/extensions/hash.rb b/lib/active_relation/extensions/hash.rb
index d9496df2e9..50864912bf 100644
--- a/lib/active_relation/extensions/hash.rb
+++ b/lib/active_relation/extensions/hash.rb
@@ -5,7 +5,7 @@ class Hash
end
end
- def to_sql(options = {})
+ def to_sql(strategy = nil)
"(#{values.collect(&:to_sql).join(', ')})"
end
end \ No newline at end of file
diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb
index ea582d1ca1..2d43120f70 100644
--- a/lib/active_relation/extensions/object.rb
+++ b/lib/active_relation/extensions/object.rb
@@ -1,12 +1,9 @@
class Object
- include ActiveRelation::SqlBuilder
-
def qualify
self
end
- def to_sql(options = {})
- options.reverse_merge!(:quote => true)
- options[:quote] ? quote(self) : self
+ def to_sql(strategy = ActiveRelation::Sql::Scalar.new)
+ strategy.scalar self
end
end \ No newline at end of file
diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb
index fba1cc90b6..84db60e3b9 100644
--- a/lib/active_relation/predicates.rb
+++ b/lib/active_relation/predicates.rb
@@ -7,30 +7,30 @@ module ActiveRelation
end
class Binary < Base
- attr_reader :attribute1, :attribute2
+ attr_reader :attribute, :operand
- def initialize(attribute1, attribute2)
- @attribute1, @attribute2 = attribute1, attribute2
+ def initialize(attribute, operand)
+ @attribute, @operand = attribute, operand
end
def ==(other)
- super and @attribute1 == other.attribute1 and @attribute2 == other.attribute2
+ super and @attribute == other.attribute and @operand == other.operand
end
def qualify
- self.class.new(attribute1.qualify, attribute2.qualify)
+ self.class.new(attribute.qualify, operand.qualify)
end
- def to_sql(options = {})
- "#{attribute1.to_sql} #{predicate_sql} #{attribute2.to_sql}"
+ def to_sql(strategy = Sql::Predicate.new)
+ "#{attribute.to_sql(strategy)} #{predicate_sql} #{operand.to_sql(strategy)}"
end
end
class Equality < Binary
def ==(other)
self.class == other.class and
- ((attribute1 == other.attribute1 and attribute2 == other.attribute2) or
- (attribute1 == other.attribute2 and attribute2 == other.attribute1))
+ ((attribute == other.attribute and operand == other.operand) or
+ (attribute == other.operand and operand == other.attribute))
end
protected
@@ -67,27 +67,20 @@ module ActiveRelation
end
end
- class Match < Base
- attr_reader :attribute, :regexp
+ class Match < Binary
+ alias_method :regexp, :operand
def initialize(attribute, regexp)
@attribute, @regexp = attribute, regexp
end
end
- class RelationInclusion < Base
- attr_reader :attribute, :relation
-
- def initialize(attribute, relation)
- @attribute, @relation = attribute, relation
- end
-
- def ==(other)
- super and attribute == other.attribute and relation == other.relation
- end
+ class RelationInclusion < Binary
+ alias_method :relation, :operand
- def to_sql(options = {})
- "#{attribute.to_sql} IN (#{relation.to_sql})"
+ protected
+ def predicate_sql
+ 'IN'
end
end
end
diff --git a/lib/active_relation/primitives/aggregation.rb b/lib/active_relation/primitives/aggregation.rb
index 403c03fab5..189eb33ac9 100644
--- a/lib/active_relation/primitives/aggregation.rb
+++ b/lib/active_relation/primitives/aggregation.rb
@@ -1,15 +1,13 @@
module ActiveRelation
module Primitives
- class Aggregation
- include SqlBuilder
-
+ class Aggregation
attr_reader :attribute, :function_sql
def initialize(attribute, function_sql)
@attribute, @function_sql = attribute, function_sql
end
- def to_sql(options = {})
+ def to_sql(strategy = nil)
"#{function_sql}(#{attribute.to_sql})"
end
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 5ce3dfd531..6ebaf1b292 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -1,8 +1,6 @@
module ActiveRelation
module Primitives
class Attribute
- include SqlBuilder
-
attr_reader :relation, :name, :alias
def initialize(relation, name, aliaz = nil)
@@ -77,8 +75,8 @@ module ActiveRelation
end
include Aggregations
- def to_sql(options = {})
- "#{quote_table_name(relation.name)}.#{quote_column_name(name)}" + (options[:use_alias] && self.alias ? " AS #{self.alias.to_s.to_sql}" : "")
+ def to_sql(strategy = Sql::Predicate.new)
+ strategy.attribute relation.name, name, self.alias
end
end
end
diff --git a/lib/active_relation/relations/base.rb b/lib/active_relation/relations/base.rb
index 71d15f31f1..c464b6bc31 100644
--- a/lib/active_relation/relations/base.rb
+++ b/lib/active_relation/relations/base.rb
@@ -1,7 +1,7 @@
module ActiveRelation
module Relations
class Base
- include SqlBuilder
+ include Sql::Quoting
module Iteration
include Enumerable
@@ -78,12 +78,12 @@ module ActiveRelation
ActiveRecord::Base.connection
end
- def to_sql(options = {})
- sql = [
- "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true) }.join(', ')}",
+ def to_sql(strategy = Sql::Select.new)
+ strategy.select [
+ "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}",
"FROM #{table_sql}",
(joins unless joins.blank?),
- ("WHERE #{selects.collect{|s| s.to_sql(:quote => false)}.join("\n\tAND ")}" unless selects.blank?),
+ ("WHERE #{selects.collect{|s| s.to_sql(Sql::Predicate.new)}.join("\n\tAND ")}" unless selects.blank?),
("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank?),
("LIMIT #{limit.to_sql}" unless limit.blank?),
("OFFSET #{offset.to_sql}" unless offset.blank?)
diff --git a/lib/active_relation/relations/deletion.rb b/lib/active_relation/relations/deletion.rb
index 0276dc1bc4..add6440c2a 100644
--- a/lib/active_relation/relations/deletion.rb
+++ b/lib/active_relation/relations/deletion.rb
@@ -5,7 +5,7 @@ module ActiveRelation
@relation = relation
end
- def to_sql(options = {})
+ def to_sql(strategy = nil)
[
"DELETE",
"FROM #{table_sql}",
diff --git a/lib/active_relation/relations/insertion.rb b/lib/active_relation/relations/insertion.rb
index 86581c23d7..c82513ce0f 100644
--- a/lib/active_relation/relations/insertion.rb
+++ b/lib/active_relation/relations/insertion.rb
@@ -7,7 +7,7 @@ module ActiveRelation
@relation, @record = relation, record
end
- def to_sql(options = {})
+ def to_sql(strategy = nil)
[
"INSERT",
"INTO #{table_sql}",
diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb
index 3d50365c40..4e6c85979e 100644
--- a/lib/active_relation/relations/join.rb
+++ b/lib/active_relation/relations/join.rb
@@ -38,7 +38,7 @@ module ActiveRelation
private
def join
- "#{join_sql} #{relation2.send(:table_sql)} ON #{predicates.collect { |p| p.to_sql(:quote => false) }.join(' AND ')}"
+ "#{join_sql} #{relation2.send(:table_sql)} ON #{predicates.collect { |p| p.to_sql(Sql::Predicate.new) }.join(' AND ')}"
end
end
end
diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb
new file mode 100644
index 0000000000..89f6193cd8
--- /dev/null
+++ b/lib/active_relation/sql.rb
@@ -0,0 +1,51 @@
+module ActiveRelation
+ module Sql
+ module Quoting
+ def connection
+ ActiveRecord::Base.connection
+ end
+
+ delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
+ end
+
+ class Strategy
+ include Quoting
+ end
+
+ class Projection < Strategy
+ def attribute(relation_name, attribute_name, aliaz)
+ "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "")
+ end
+
+ def select(select_sql)
+ "(#{select_sql})"
+ end
+ end
+
+ class Predicate < Strategy
+ def attribute(relation_name, attribute_name, aliaz)
+ "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}"
+ end
+
+ def scalar(scalar)
+ scalar
+ end
+
+ def select(select_sql)
+ "(#{select_sql})"
+ end
+ end
+
+ class Select < Strategy
+ def select(select_sql)
+ select_sql
+ end
+ end
+
+ class Scalar < Strategy
+ def scalar(scalar)
+ quote(scalar)
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder.rb b/lib/active_relation/sql_builder.rb
deleted file mode 100644
index 07a4ebabb7..0000000000
--- a/lib/active_relation/sql_builder.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-module ActiveRelation
- module SqlBuilder
- def connection
- ActiveRecord::Base.connection
- end
-
- delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
- end
-end \ No newline at end of file
diff --git a/spec/active_relation/predicates/equality_spec.rb b/spec/active_relation/predicates/equality_spec.rb
index b3c7b597a0..9394e63835 100644
--- a/spec/active_relation/predicates/equality_spec.rb
+++ b/spec/active_relation/predicates/equality_spec.rb
@@ -22,4 +22,7 @@ describe ActiveRelation::Predicates::Equality do
ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Equality.new(@attribute2, @attribute1)
end
end
+
+ describe '#to_sql' do
+ end
end \ No newline at end of file