aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-07 22:32:16 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-07 22:32:16 -0800
commit5f03d1616cc6f66013fbaae9c92553df74a7aee4 (patch)
tree6f35b2683c858fb6bb95f7dc895aecf1309f48df
parent2fa1ff4e63f27d49a529116bb5e9933a0f385c3e (diff)
downloadrails-5f03d1616cc6f66013fbaae9c92553df74a7aee4.tar.gz
rails-5f03d1616cc6f66013fbaae9c92553df74a7aee4.tar.bz2
rails-5f03d1616cc6f66013fbaae9c92553df74a7aee4.zip
removed sql builder
-rw-r--r--lib/active_relation.rb20
-rw-r--r--lib/active_relation/extensions/hash.rb6
-rw-r--r--lib/active_relation/extensions/object.rb10
-rw-r--r--lib/active_relation/predicates.rb4
-rw-r--r--lib/active_relation/predicates/binary_predicate.rb13
-rw-r--r--lib/active_relation/predicates/equality_predicate.rb4
-rw-r--r--lib/active_relation/relations/attribute.rb26
-rw-r--r--lib/active_relation/relations/compound_relation.rb4
-rw-r--r--lib/active_relation/relations/deletion_relation.rb23
-rw-r--r--lib/active_relation/relations/inner_join_relation.rb4
-rw-r--r--lib/active_relation/relations/insertion_relation.rb34
-rw-r--r--lib/active_relation/relations/join.rb15
-rw-r--r--lib/active_relation/relations/join_relation.rb7
-rw-r--r--lib/active_relation/relations/left_outer_join_relation.rb4
-rw-r--r--lib/active_relation/relations/order_relation.rb20
-rw-r--r--lib/active_relation/relations/projection_relation.rb10
-rw-r--r--lib/active_relation/relations/range_relation.rb15
-rw-r--r--lib/active_relation/relations/relation.rb45
-rw-r--r--lib/active_relation/sql_builder.rb7
-rw-r--r--lib/active_relation/sql_builder/columns_builder.rb16
-rw-r--r--lib/active_relation/sql_builder/conditions_builder.rb20
-rw-r--r--lib/active_relation/sql_builder/delete_builder.rb32
-rw-r--r--lib/active_relation/sql_builder/equals_condition_builder.rb18
-rw-r--r--lib/active_relation/sql_builder/inner_join_builder.rb5
-rw-r--r--lib/active_relation/sql_builder/insert_builder.rb41
-rw-r--r--lib/active_relation/sql_builder/join_builder.rb13
-rw-r--r--lib/active_relation/sql_builder/joins_builder.rb18
-rw-r--r--lib/active_relation/sql_builder/left_outer_join_builder.rb5
-rw-r--r--lib/active_relation/sql_builder/order_builder.rb16
-rw-r--r--lib/active_relation/sql_builder/select_builder.rb61
-rw-r--r--lib/active_relation/sql_builder/selects_builder.rb9
-rw-r--r--lib/active_relation/sql_builder/sql_builder.rb35
-rw-r--r--lib/active_relation/sql_builder/values_builder.rb16
-rw-r--r--spec/active_relation/integration/scratch_spec.rb16
-rw-r--r--spec/active_relation/predicates/binary_predicate_spec.rb19
-rw-r--r--spec/active_relation/relations/attribute_spec.rb15
-rw-r--r--spec/active_relation/relations/deletion_relation_spec.rb24
-rw-r--r--spec/active_relation/relations/insertion_relation_spec.rb31
-rw-r--r--spec/active_relation/relations/join_relation_spec.rb32
-rw-r--r--spec/active_relation/relations/order_relation_spec.rb25
-rw-r--r--spec/active_relation/relations/projection_relation_spec.rb10
-rw-r--r--spec/active_relation/relations/range_relation_spec.rb25
-rw-r--r--spec/active_relation/relations/relation_spec.rb24
-rw-r--r--spec/active_relation/relations/rename_relation_spec.rb11
-rw-r--r--spec/active_relation/relations/selection_relation_spec.rb37
-rw-r--r--spec/active_relation/relations/table_relation_spec.rb11
-rw-r--r--spec/active_relation/sql_builder/conditions_spec.rb18
-rw-r--r--spec/active_relation/sql_builder/delete_builder_spec.rb22
-rw-r--r--spec/active_relation/sql_builder/insert_builder_spec.rb24
-rw-r--r--spec/active_relation/sql_builder/select_builder_spec.rb148
50 files changed, 206 insertions, 862 deletions
diff --git a/lib/active_relation.rb b/lib/active_relation.rb
index 7cdbd5c02d..2120d09bc8 100644
--- a/lib/active_relation.rb
+++ b/lib/active_relation.rb
@@ -4,6 +4,8 @@ require 'rubygems'
require 'activesupport'
require 'activerecord'
+require 'active_relation/sql_builder'
+
require 'active_relation/relations/relation'
require 'active_relation/relations/compound_relation'
require 'active_relation/relations/table_relation'
@@ -19,7 +21,6 @@ require 'active_relation/relations/selection_relation'
require 'active_relation/relations/order_relation'
require 'active_relation/relations/range_relation'
require 'active_relation/relations/rename_relation'
-require 'active_relation/relations/join'
require 'active_relation/relations/deletion_relation'
require 'active_relation/relations/insertion_relation'
@@ -36,19 +37,4 @@ require 'active_relation/predicates/match_predicate'
require 'active_relation/extensions/object'
require 'active_relation/extensions/array'
require 'active_relation/extensions/base'
-require 'active_relation/extensions/hash'
-
-require 'active_relation/sql_builder/sql_builder'
-require 'active_relation/sql_builder/select_builder'
-require 'active_relation/sql_builder/delete_builder'
-require 'active_relation/sql_builder/insert_builder'
-require 'active_relation/sql_builder/joins_builder'
-require 'active_relation/sql_builder/join_builder'
-require 'active_relation/sql_builder/inner_join_builder'
-require 'active_relation/sql_builder/left_outer_join_builder'
-require 'active_relation/sql_builder/equals_condition_builder'
-require 'active_relation/sql_builder/conditions_builder'
-require 'active_relation/sql_builder/order_builder'
-require 'active_relation/sql_builder/columns_builder'
-require 'active_relation/sql_builder/selects_builder'
-require 'active_relation/sql_builder/values_builder' \ No newline at end of file
+require 'active_relation/extensions/hash' \ No newline at end of file
diff --git a/lib/active_relation/extensions/hash.rb b/lib/active_relation/extensions/hash.rb
index f643ac17ab..d9496df2e9 100644
--- a/lib/active_relation/extensions/hash.rb
+++ b/lib/active_relation/extensions/hash.rb
@@ -5,9 +5,7 @@ class Hash
end
end
- def to_sql(builder = ValuesBuilder.new)
- builder.call do
- row *values
- end
+ def to_sql(options = {})
+ "(#{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 c241581f86..79d7613d9a 100644
--- a/lib/active_relation/extensions/object.rb
+++ b/lib/active_relation/extensions/object.rb
@@ -1,12 +1,12 @@
class Object
+ include SqlBuilder
+
def qualify
self
end
- def to_sql(builder = EqualsConditionBuilder.new)
- me = self
- builder.call do
- value me.to_s
- end
+ def to_sql(options = {})
+ options.reverse_merge!(:quote => true)
+ options[:quote] ? quote(self) : self
end
end \ No newline at end of file
diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb
new file mode 100644
index 0000000000..0179a15035
--- /dev/null
+++ b/lib/active_relation/predicates.rb
@@ -0,0 +1,4 @@
+module ActiveRelation
+ module Predicates
+ end
+end \ No newline at end of file
diff --git a/lib/active_relation/predicates/binary_predicate.rb b/lib/active_relation/predicates/binary_predicate.rb
index c467d63310..f3ce430d00 100644
--- a/lib/active_relation/predicates/binary_predicate.rb
+++ b/lib/active_relation/predicates/binary_predicate.rb
@@ -5,16 +5,15 @@ class BinaryPredicate < Predicate
@attribute1, @attribute2 = attribute1, attribute2
end
+ def ==(other)
+ super and @attribute1.eql?(other.attribute1) and @attribute2.eql?(other.attribute2)
+ end
+
def qualify
self.class.new(attribute1.qualify, attribute2.qualify)
end
- def to_sql(builder = ConditionsBuilder.new)
- builder.call do
- send(predicate_name) do
- attribute1.to_sql(self)
- attribute2.to_sql(self)
- end
- end
+ def to_sql(options = {})
+ "#{attribute1.to_sql} #{predicate_sql} #{attribute2.to_sql}"
end
end \ No newline at end of file
diff --git a/lib/active_relation/predicates/equality_predicate.rb b/lib/active_relation/predicates/equality_predicate.rb
index 7040c45f67..4f4113c740 100644
--- a/lib/active_relation/predicates/equality_predicate.rb
+++ b/lib/active_relation/predicates/equality_predicate.rb
@@ -6,7 +6,7 @@ class EqualityPredicate < BinaryPredicate
end
protected
- def predicate_name
- :equals
+ def predicate_sql
+ '='
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/attribute.rb b/lib/active_relation/relations/attribute.rb
index 9ccbb495ea..8193132de6 100644
--- a/lib/active_relation/relations/attribute.rb
+++ b/lib/active_relation/relations/attribute.rb
@@ -1,12 +1,14 @@
class Attribute
- attr_reader :relation, :name, :aliaz
+ include SqlBuilder
+
+ attr_reader :relation, :name, :alias
def initialize(relation, name, aliaz = nil)
- @relation, @name, @aliaz = relation, name, aliaz
+ @relation, @name, @alias = relation, name, aliaz
end
- def alias(aliaz)
- Attribute.new(relation, name, aliaz)
+ def alias(aliaz = nil)
+ aliaz ? Attribute.new(relation, name, aliaz) : @alias
end
def qualified_name
@@ -16,12 +18,12 @@ class Attribute
def qualify
self.alias(qualified_name)
end
-
- module Predications
- def eql?(other)
- relation == other.relation and name == other.name and aliaz == other.aliaz
- end
+ def eql?(other)
+ relation == other.relation and name == other.name and self.alias == other.alias
+ end
+
+ module Predications
def ==(other)
EqualityPredicate.new(self, other)
end
@@ -48,9 +50,7 @@ class Attribute
end
include Predications
- def to_sql(builder = SelectsBuilder.new)
- builder.call do
- column relation.table, name, aliaz
- end
+ def to_sql(options = {})
+ "#{quote_table_name(relation.table)}.#{quote_column_name(name)}" + (options[:use_alias] && self.alias ? " AS #{self.alias.to_s.to_sql}" : "")
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/compound_relation.rb b/lib/active_relation/relations/compound_relation.rb
index fe92905d92..b18921e06d 100644
--- a/lib/active_relation/relations/compound_relation.rb
+++ b/lib/active_relation/relations/compound_relation.rb
@@ -1,3 +1,5 @@
class CompoundRelation < Relation
- delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :to => :relation
+ attr_reader :relation
+
+ delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :limit, :offset, :to => :relation
end \ No newline at end of file
diff --git a/lib/active_relation/relations/deletion_relation.rb b/lib/active_relation/relations/deletion_relation.rb
index e060efd5f9..8418319055 100644
--- a/lib/active_relation/relations/deletion_relation.rb
+++ b/lib/active_relation/relations/deletion_relation.rb
@@ -1,22 +1,13 @@
class DeletionRelation < CompoundRelation
- attr_reader :relation
-
- def ==(other)
- relation == other.relation
- end
-
def initialize(relation)
@relation = relation
end
- def to_sql(builder = DeleteBuilder.new)
- builder.call do
- delete
- from table
- where do
- selects.each { |s| s.to_sql(self) }
- end
- end
- end
-
+ def to_sql(options = {})
+ [
+ "DELETE",
+ "FROM #{quote_table_name(table)}",
+ ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank?)
+ ].compact.join("\n")
+ end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/inner_join_relation.rb b/lib/active_relation/relations/inner_join_relation.rb
index 5e58f241f8..74160c559f 100644
--- a/lib/active_relation/relations/inner_join_relation.rb
+++ b/lib/active_relation/relations/inner_join_relation.rb
@@ -1,6 +1,6 @@
class InnerJoinRelation < JoinRelation
protected
- def join_type
- :inner_join
+ def join_sql
+ "INNER JOIN"
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/insertion_relation.rb b/lib/active_relation/relations/insertion_relation.rb
index 84752d13f9..002ebbf062 100644
--- a/lib/active_relation/relations/insertion_relation.rb
+++ b/lib/active_relation/relations/insertion_relation.rb
@@ -1,29 +1,21 @@
class InsertionRelation < CompoundRelation
- attr_reader :relation, :tuple
+ attr_reader :record
- def initialize(relation, tuple)
- @relation, @tuple = relation, tuple
- end
-
- def to_sql(builder = InsertBuilder.new)
- builder.call do
- insert
- into table
- columns do
- tuple.keys.each { |attribute| attribute.to_sql(self) }
- end
- values do
- inserts.each { |insert| insert.to_sql(self) }
- end
- end
+ def initialize(relation, record)
+ @relation, @record = relation, record
end
- def ==(other)
- relation == other.relation and tuple == other.tuple
- end
-
+ def to_sql(options = {})
+ [
+ "INSERT",
+ "INTO #{quote_table_name(table)}",
+ "(#{record.keys.collect(&:to_sql).join(', ')})",
+ "VALUES #{inserts.collect(&:to_sql).join(', ')}"
+ ].join("\n")
+ end
+
protected
def inserts
- relation.inserts + [tuple]
+ relation.inserts + [record]
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb
deleted file mode 100644
index 9a6196deac..0000000000
--- a/lib/active_relation/relations/join.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-class Join
- attr_reader :relation1, :relation2, :predicates, :join_type
-
- def initialize(relation1, relation2, predicates, join_type)
- @relation1, @relation2, @predicates, @join_type = relation1, relation2, predicates, join_type
- end
-
- def to_sql(builder = JoinsBuilder.new)
- builder.call do
- send(join_type, relation2.table) do
- predicates.each { |p| p.to_sql(self) }
- end
- end
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/relations/join_relation.rb b/lib/active_relation/relations/join_relation.rb
index 79c8a915b8..845dfd732f 100644
--- a/lib/active_relation/relations/join_relation.rb
+++ b/lib/active_relation/relations/join_relation.rb
@@ -17,7 +17,7 @@ class JoinRelation < Relation
protected
def joins
- relation1.joins + relation2.joins + [Join.new(relation1, relation2, predicates, join_type)]
+ [relation1.joins, relation2.joins, join].compact.join(" ")
end
def selects
@@ -33,4 +33,9 @@ class JoinRelation < Relation
end
delegate :table, :to => :relation1
+
+ private
+ def join
+ "#{join_sql} #{quote_table_name(relation2.table)} ON #{predicates.collect { |p| p.to_sql(:quote => false) }.join(' AND ')}"
+ end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/left_outer_join_relation.rb b/lib/active_relation/relations/left_outer_join_relation.rb
index 6d13d8da07..57eda4e1fc 100644
--- a/lib/active_relation/relations/left_outer_join_relation.rb
+++ b/lib/active_relation/relations/left_outer_join_relation.rb
@@ -1,6 +1,6 @@
class LeftOuterJoinRelation < JoinRelation
protected
- def join_type
- :left_outer_join
+ def join_sql
+ "LEFT OUTER JOIN"
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/order_relation.rb b/lib/active_relation/relations/order_relation.rb
index b39dc45c3f..dfb0c0bf25 100644
--- a/lib/active_relation/relations/order_relation.rb
+++ b/lib/active_relation/relations/order_relation.rb
@@ -1,25 +1,15 @@
class OrderRelation < CompoundRelation
- attr_reader :relation, :attributes
+ attr_reader :relation, :orders
- def initialize(relation, *attributes)
- @relation, @attributes = relation, attributes
+ def initialize(relation, *orders)
+ @relation, @orders = relation, orders
end
def ==(other)
- relation == other.relation and attributes.eql?(other.attributes)
+ relation == other.relation and orders.eql?(other.orders)
end
def qualify
- OrderRelation.new(relation.qualify, *attributes.collect { |a| a.qualify })
- end
-
- def to_sql(builder = SelectBuilder.new)
- relation.to_sql(builder).call do
- attributes.each do |attribute|
- order_by do
- attribute.to_sql(self)
- end
- end
- end
+ OrderRelation.new(relation.qualify, *orders.collect { |o| o.qualify })
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/projection_relation.rb b/lib/active_relation/relations/projection_relation.rb
index 1a0e8dbfe4..53b0ad1e91 100644
--- a/lib/active_relation/relations/projection_relation.rb
+++ b/lib/active_relation/relations/projection_relation.rb
@@ -1,4 +1,4 @@
-class ProjectionRelation < Relation
+class ProjectionRelation < CompoundRelation
attr_reader :relation, :attributes
def initialize(relation, *attributes)
@@ -12,12 +12,4 @@ class ProjectionRelation < Relation
def qualify
ProjectionRelation.new(relation.qualify, *attributes.collect(&:qualify))
end
-
- def to_sql(builder = SelectBuilder.new)
- relation.to_sql(builder).call do
- select do
- attributes.collect { |a| a.to_sql(self) }
- end
- end
- end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/range_relation.rb b/lib/active_relation/relations/range_relation.rb
index 9225d5615b..6a2b0b3470 100644
--- a/lib/active_relation/relations/range_relation.rb
+++ b/lib/active_relation/relations/range_relation.rb
@@ -1,5 +1,5 @@
-class RangeRelation < Relation
- attr_reader :relation, :range
+class RangeRelation < CompoundRelation
+ attr_reader :range
def initialize(relation, range)
@relation, @range = relation, range
@@ -9,10 +9,11 @@ class RangeRelation < Relation
relation == other.relation and range == other.range
end
- def to_sql(builder = SelectBuilder.new)
- relation.to_sql(builder).call do
- limit range.last - range.first + 1
- offset range.first
- end
+ def limit
+ range.end - range.begin + 1
+ end
+
+ def offset
+ range.begin
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb
index be6ee760a5..0a8455b39c 100644
--- a/lib/active_relation/relations/relation.rb
+++ b/lib/active_relation/relations/relation.rb
@@ -1,4 +1,6 @@
class Relation
+ include SqlBuilder
+
module Iteration
include Enumerable
@@ -50,8 +52,8 @@ class Relation
RenameRelation.new(self, attribute => aliaz)
end
- def insert(tuple)
- InsertionRelation.new(self, tuple)
+ def insert(record)
+ InsertionRelation.new(self, record)
end
def delete
@@ -64,28 +66,25 @@ class Relation
ActiveRecord::Base.connection
end
- def to_sql(builder = SelectBuilder.new)
- builder.call do
- select do
- attributes.each { |a| a.to_sql(self) }
- end
- from table do
- joins.each { |j| j.to_sql(self) }
- end
- where do
- selects.each { |s| s.to_sql(self) }
- end
- order_by do
- orders.each { |o| o.to_sql(self) }
- end
- end
+ def to_sql(options = {})
+ [
+ "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true) }.join(', ')}",
+ "FROM #{quote_table_name(table)}",
+ (joins.to_sql(:quote => false) unless joins.blank?),
+ ("WHERE #{selects.collect{|s| s.to_sql(:quote => false)}.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?)
+ ].compact.join("\n")
end
- delegate :to_s, :to => :to_sql
+ alias_method :to_s, :to_sql
protected
- def attributes; [] end
- def joins; [] end
- def selects; [] end
- def orders; [] end
- def inserts; [] end
+ def attributes; [] end
+ def selects; [] end
+ def orders; [] end
+ def inserts; [] end
+ def joins; nil end
+ def limit; nil end
+ def offset; nil end
end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder.rb b/lib/active_relation/sql_builder.rb
new file mode 100644
index 0000000000..0d2187173e
--- /dev/null
+++ b/lib/active_relation/sql_builder.rb
@@ -0,0 +1,7 @@
+module SqlBuilder
+ def connection
+ ActiveRecord::Base.connection
+ end
+
+ delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
+end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/columns_builder.rb b/lib/active_relation/sql_builder/columns_builder.rb
deleted file mode 100644
index a8a5d0e4ca..0000000000
--- a/lib/active_relation/sql_builder/columns_builder.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class ColumnsBuilder < SqlBuilder
- def initialize(&block)
- @columns = []
- super(&block)
- end
-
- def to_s
- @columns.join(', ')
- end
-
- def column(table, column, aliaz = nil)
- @columns << "#{quote_table_name(table)}.#{quote_column_name(column)}"
- end
-
- delegate :blank?, :to => :@columns
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/conditions_builder.rb b/lib/active_relation/sql_builder/conditions_builder.rb
deleted file mode 100644
index 60430f65d8..0000000000
--- a/lib/active_relation/sql_builder/conditions_builder.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-class ConditionsBuilder < SqlBuilder
- def initialize(&block)
- @conditions = []
- super(&block)
- end
-
- def equals(&block)
- @conditions << EqualsConditionBuilder.new(&block)
- end
-
- def value(value)
- @conditions << value
- end
-
- def to_s
- @conditions.join(' AND ')
- end
-
- delegate :blank?, :to => :@conditions
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/delete_builder.rb b/lib/active_relation/sql_builder/delete_builder.rb
deleted file mode 100644
index 2e8be94dfe..0000000000
--- a/lib/active_relation/sql_builder/delete_builder.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-class DeleteBuilder < SqlBuilder
- def delete
- end
-
- def from(table)
- @table = table
- end
-
- def where(&block)
- @conditions = ConditionsBuilder.new(&block)
- end
-
- def to_s
- [delete_clause,
- from_clause,
- where_clause
- ].compact.join("\n")
- end
-
- private
- def delete_clause
- "DELETE"
- end
-
- def from_clause
- "FROM #{quote_table_name(@table)}"
- end
-
- def where_clause
- "WHERE #{@conditions}" unless @conditions.blank?
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/equals_condition_builder.rb b/lib/active_relation/sql_builder/equals_condition_builder.rb
deleted file mode 100644
index cfa919c34c..0000000000
--- a/lib/active_relation/sql_builder/equals_condition_builder.rb
+++ /dev/null
@@ -1,18 +0,0 @@
-class EqualsConditionBuilder < SqlBuilder
- def initialize(&block)
- @operands = []
- super(&block)
- end
-
- def column(table, column, aliaz = nil)
- @operands << "#{quote_table_name(table)}.#{quote_column_name(column)}"
- end
-
- def value(value)
- @operands << value
- end
-
- def to_s
- "#{@operands[0]} = #{@operands[1]}"
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/inner_join_builder.rb b/lib/active_relation/sql_builder/inner_join_builder.rb
deleted file mode 100644
index 6aec703325..0000000000
--- a/lib/active_relation/sql_builder/inner_join_builder.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class InnerJoinBuilder < JoinBuilder
- def join_type
- "INNER JOIN"
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/insert_builder.rb b/lib/active_relation/sql_builder/insert_builder.rb
deleted file mode 100644
index 09deefad10..0000000000
--- a/lib/active_relation/sql_builder/insert_builder.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-class InsertBuilder < SqlBuilder
- def insert
- end
-
- def into(table)
- @table = table
- end
-
- def columns(&block)
- @columns = ColumnsBuilder.new(&block)
- end
-
- def values(&block)
- @values = ValuesBuilder.new(&block)
- end
-
- def to_s
- [insert_clause,
- into_clause,
- columns_clause,
- values_clause
- ].compact.join("\n")
- end
-
- private
- def insert_clause
- "INSERT"
- end
-
- def into_clause
- "INTO #{quote_table_name(@table)}"
- end
-
- def values_clause
- "VALUES #{@values}" unless @values.blank?
- end
-
- def columns_clause
- "(#{@columns})" unless @columns.blank?
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/join_builder.rb b/lib/active_relation/sql_builder/join_builder.rb
deleted file mode 100644
index ef63d1fcb1..0000000000
--- a/lib/active_relation/sql_builder/join_builder.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-class JoinBuilder < SqlBuilder
- def initialize(table, &block)
- @table = table
- @conditions = ConditionsBuilder.new
- super(&block)
- end
-
- delegate :call, :to => :@conditions
-
- def to_s
- "#{join_type} #{quote_table_name(@table)} ON #{@conditions}"
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/joins_builder.rb b/lib/active_relation/sql_builder/joins_builder.rb
deleted file mode 100644
index 36a92e9922..0000000000
--- a/lib/active_relation/sql_builder/joins_builder.rb
+++ /dev/null
@@ -1,18 +0,0 @@
-class JoinsBuilder < SqlBuilder
- def initialize(&block)
- @joins = []
- super(&block)
- end
-
- def inner_join(table, &block)
- @joins << InnerJoinBuilder.new(table, &block)
- end
-
- def left_outer_join(table, &block)
- @joins << LeftOuterJoinBuilder.new(table, &block)
- end
-
- def to_s
- @joins.join(' ')
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/left_outer_join_builder.rb b/lib/active_relation/sql_builder/left_outer_join_builder.rb
deleted file mode 100644
index dad3f85810..0000000000
--- a/lib/active_relation/sql_builder/left_outer_join_builder.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class LeftOuterJoinBuilder < JoinBuilder
- def join_type
- "LEFT OUTER JOIN"
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/order_builder.rb b/lib/active_relation/sql_builder/order_builder.rb
deleted file mode 100644
index 66a8cfdba9..0000000000
--- a/lib/active_relation/sql_builder/order_builder.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class OrderBuilder < SqlBuilder
- def initialize(&block)
- @orders = []
- super(&block)
- end
-
- def column(table, column, aliaz = nil)
- @orders << "#{quote_table_name(table)}.#{quote_column_name(column)}"
- end
-
- def to_s
- @orders.join(', ')
- end
-
- delegate :blank?, :to => :@orders
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/select_builder.rb b/lib/active_relation/sql_builder/select_builder.rb
deleted file mode 100644
index 57116cb64b..0000000000
--- a/lib/active_relation/sql_builder/select_builder.rb
+++ /dev/null
@@ -1,61 +0,0 @@
-class SelectBuilder < SqlBuilder
- def select(&block)
- @selects = SelectsBuilder.new(&block)
- end
-
- def from(table, &block)
- @table = table
- @joins = JoinsBuilder.new(&block)
- end
-
- def where(&block)
- @conditions = ConditionsBuilder.new(&block)
- end
-
- def order_by(&block)
- @orders = OrderBuilder.new(&block)
- end
-
- def limit(i, offset = nil)
- @limit = i
- offset(offset) if offset
- end
-
- def offset(i)
- @offset = i
- end
-
- def to_s
- [select_clause,
- from_clause,
- where_clause,
- order_by_clause,
- limit_clause,
- offset_clause].compact.join("\n")
- end
-
- private
- def select_clause
- "SELECT #{@selects}" unless @selects.blank?
- end
-
- def from_clause
- "FROM #{quote_table_name(@table)} #{@joins}" unless @table.blank?
- end
-
- def where_clause
- "WHERE #{@conditions}" unless @conditions.blank?
- end
-
- def order_by_clause
- "ORDER BY #{@orders}" unless @orders.blank?
- end
-
- def limit_clause
- "LIMIT #{@limit}" unless @limit.blank?
- end
-
- def offset_clause
- "OFFSET #{@offset}" unless @offset.blank?
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/selects_builder.rb b/lib/active_relation/sql_builder/selects_builder.rb
deleted file mode 100644
index 6ad06d0ae4..0000000000
--- a/lib/active_relation/sql_builder/selects_builder.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-class SelectsBuilder < ColumnsBuilder
- def all
- @columns << :*
- end
-
- def column(table, column, aliaz = nil)
- @columns << "#{quote_table_name(table)}.#{quote_column_name(column)}" + (aliaz ? " AS #{quote(aliaz)}" : '')
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/sql_builder.rb b/lib/active_relation/sql_builder/sql_builder.rb
deleted file mode 100644
index c984444e41..0000000000
--- a/lib/active_relation/sql_builder/sql_builder.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-class SqlBuilder
- def initialize(&block)
- @callers = []
- call(&block) if block
- end
-
- def method_missing(method, *args)
- @callers.last.send(method, *args)
- end
-
- def ==(other)
- to_s == other.to_s
- end
-
- def to_s
- end
-
- def call(&block)
- returning self do |builder|
- @callers << eval("self", block.binding)
- begin
- instance_eval &block
- ensure
- @callers.pop
- end
- end
- end
-
- private
- delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
-
- def connection
- ActiveRecord::Base.connection
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/sql_builder/values_builder.rb b/lib/active_relation/sql_builder/values_builder.rb
deleted file mode 100644
index f22b1e507e..0000000000
--- a/lib/active_relation/sql_builder/values_builder.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class ValuesBuilder < SqlBuilder
- def initialize(&block)
- @values = []
- super(&block)
- end
-
- def row(*values)
- @values << "(#{values.collect { |v| quote(v) }.join(', ')})"
- end
-
- def to_s
- @values.join(', ')
- end
-
- delegate :blank?, :to => :@values
-end \ No newline at end of file
diff --git a/spec/active_relation/integration/scratch_spec.rb b/spec/active_relation/integration/scratch_spec.rb
index fba587e4ba..93d332eea7 100644
--- a/spec/active_relation/integration/scratch_spec.rb
+++ b/spec/active_relation/integration/scratch_spec.rb
@@ -71,7 +71,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
# the 'project' operator limits the columns that come back from the query.
# Note how all the operators are compositional: 'project' is applied to a query
# that previously had been joined and selected.
- user_photos.project(*@photos.attributes).to_s.should be_like("""
+ user_photos.project(*@photos.attributes).to_sql.should be_like("""
SELECT `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
FROM `users`
LEFT OUTER JOIN `photos`
@@ -86,7 +86,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
it 'generates the query for User.has_many :cameras :through => :photos' do
# note, again, the compositionality of the operators:
user_cameras = photo_belongs_to_camera(user_has_many_photos(@user))
- user_cameras.project(*@cameras.attributes).to_s.should be_like("""
+ user_cameras.project(*@cameras.attributes).to_sql.should be_like("""
SELECT `cameras`.`id`
FROM `users`
LEFT OUTER JOIN `photos`
@@ -101,7 +101,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
it 'generates the query for an eager join for a collection using the same logic as
for an association on an individual row' do
users_cameras = photo_belongs_to_camera(user_has_many_photos(@users))
- users_cameras.to_s.should be_like("""
+ users_cameras.to_sql.should be_like("""
SELECT `users`.`name`, `users`.`id`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`, `cameras`.`id`
FROM `users`
LEFT OUTER JOIN `photos`
@@ -113,7 +113,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
it 'is trivial to disambiguate columns' do
users_cameras = photo_belongs_to_camera(user_has_many_photos(@users)).qualify
- users_cameras.to_s.should be_like("""
+ users_cameras.to_sql.should be_like("""
SELECT `users`.`name` AS 'users.name', `users`.`id` AS 'users.id', `photos`.`id` AS 'photos.id', `photos`.`user_id` AS 'photos.user_id', `photos`.`camera_id` AS 'photos.camera_id', `cameras`.`id` AS 'cameras.id'
FROM `users`
LEFT OUTER JOIN `photos`
@@ -124,13 +124,13 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
end
it 'allows arbitrary sql to be passed through' do
- (@users << @photos).on("asdf").to_s.should be_like("""
+ (@users << @photos).on("asdf").to_sql.should be_like("""
SELECT `users`.`name`, `users`.`id`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
FROM `users`
LEFT OUTER JOIN `photos`
ON asdf
""")
- @users.select("asdf").to_s.should be_like("""
+ @users.select("asdf").to_sql.should be_like("""
SELECT `users`.`name`, `users`.`id`
FROM `users`
WHERE asdf
@@ -139,7 +139,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
describe 'write operations' do
it 'generates the query for user.destroy' do
- @user.delete.to_s.should be_like("""
+ @user.delete.to_sql.should be_like("""
DELETE
FROM `users`
WHERE `users`.`id` = 1
@@ -147,7 +147,7 @@ describe 'ActiveRelation', 'A proposed refactoring to ActiveRecord, introducing
end
it 'generates an efficient query for two User.creates -- UnitOfWork is within reach!' do
- @users.insert(@users[:name] => "humpty").insert(@users[:name] => "dumpty").to_s.should be_like("""
+ @users.insert(@users[:name] => "humpty").insert(@users[:name] => "dumpty").to_sql.should be_like("""
INSERT
INTO `users`
(`users`.`name`) VALUES ('humpty'), ('dumpty')
diff --git a/spec/active_relation/predicates/binary_predicate_spec.rb b/spec/active_relation/predicates/binary_predicate_spec.rb
index 5de559df41..0bddae8491 100644
--- a/spec/active_relation/predicates/binary_predicate_spec.rb
+++ b/spec/active_relation/predicates/binary_predicate_spec.rb
@@ -7,18 +7,12 @@ describe BinaryPredicate do
@attribute1 = Attribute.new(@relation1, :name1)
@attribute2 = Attribute.new(@relation2, :name2)
class ConcreteBinaryPredicate < BinaryPredicate
- def predicate_name
- :equals
+ def predicate_sql
+ "<=>"
end
end
end
- describe '#initialize' do
- it "requires that both columns come from the same relation" do
- pending
- end
- end
-
describe '==' do
it "obtains if attribute1 and attribute2 are identical" do
BinaryPredicate.new(@attribute1, @attribute2).should == BinaryPredicate.new(@attribute1, @attribute2)
@@ -40,12 +34,9 @@ describe BinaryPredicate do
describe '#to_sql' do
it 'manufactures correct sql' do
- ConcreteBinaryPredicate.new(@attribute1, @attribute2).to_sql.should == ConditionsBuilder.new do
- equals do
- column :foo, :name1
- column :bar, :name2
- end
- end
+ ConcreteBinaryPredicate.new(@attribute1, @attribute2).to_sql.should be_like("""
+ `foo`.`name1` <=> `bar`.`name2`
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/attribute_spec.rb b/spec/active_relation/relations/attribute_spec.rb
index 68ff147ab5..4154a91b85 100644
--- a/spec/active_relation/relations/attribute_spec.rb
+++ b/spec/active_relation/relations/attribute_spec.rb
@@ -8,12 +8,7 @@ describe Attribute do
describe '#alias' do
it "manufactures an aliased attributed" do
- pending
- end
-
- it "should be renamed to #alias!" do
- pending
- @relation1.alias
+ @relation1[:id].alias(:alias).should == Attribute.new(@relation1, :id, :alias)
end
end
@@ -79,12 +74,4 @@ describe Attribute do
end
end
end
-
- describe '#to_sql' do
- it "manufactures a column" do
- Attribute.new(@relation1, :name, :alias).to_sql.should == SelectsBuilder.new do
- column :foo, :name, :alias
- end
- end
- end
end
diff --git a/spec/active_relation/relations/deletion_relation_spec.rb b/spec/active_relation/relations/deletion_relation_spec.rb
index 4f75a261f4..5e8c5137b2 100644
--- a/spec/active_relation/relations/deletion_relation_spec.rb
+++ b/spec/active_relation/relations/deletion_relation_spec.rb
@@ -6,17 +6,19 @@ describe DeletionRelation do
end
describe '#to_sql' do
- it 'manufactures sql deleting the relation' do
- DeletionRelation.new(@relation.select(@relation[:id] == 1)).to_sql.to_s.should == DeleteBuilder.new do
- delete
- from :users
- where do
- equals do
- column :users, :id
- value 1
- end
- end
- end.to_s
+ it 'manufactures sql deleting a table relation' do
+ DeletionRelation.new(@relation).to_sql.should be_like("""
+ DELETE
+ FROM `users`
+ """)
+ end
+
+ it 'manufactures sql deleting a selection relation' do
+ DeletionRelation.new(@relation.select(@relation[:id] == 1)).to_sql.should be_like("""
+ DELETE
+ FROM `users`
+ WHERE `users`.`id` = 1
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/insertion_relation_spec.rb b/spec/active_relation/relations/insertion_relation_spec.rb
index 6bafabb473..177918a94a 100644
--- a/spec/active_relation/relations/insertion_relation_spec.rb
+++ b/spec/active_relation/relations/insertion_relation_spec.rb
@@ -7,31 +7,20 @@ describe InsertionRelation do
describe '#to_sql' do
it 'manufactures sql inserting the data for one item' do
- InsertionRelation.new(@relation, @relation[:name] => "nick").to_sql.should == InsertBuilder.new do
- insert
- into :users
- columns do
- column :users, :name
- end
- values do
- row "nick"
- end
- end
+ InsertionRelation.new(@relation, @relation[:name] => "nick").to_sql.should be_like("""
+ INSERT
+ INTO `users`
+ (`users`.`name`) VALUES ('nick')
+ """)
end
it 'manufactures sql inserting the data for multiple items' do
nested_insertion = InsertionRelation.new(@relation, @relation[:name] => "cobra")
- InsertionRelation.new(nested_insertion, nested_insertion[:name] => "commander").to_sql.to_s.should == InsertBuilder.new do
- insert
- into :users
- columns do
- column :users, :name
- end
- values do
- row "cobra"
- row "commander"
- end
- end.to_s
+ InsertionRelation.new(nested_insertion, nested_insertion[:name] => "commander").to_sql.should be_like("""
+ INSERT
+ INTO `users`
+ (`users`.`name`) VALUES ('cobra'), ('commander')
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/join_relation_spec.rb b/spec/active_relation/relations/join_relation_spec.rb
index d0be270837..3e60cc4c34 100644
--- a/spec/active_relation/relations/join_relation_spec.rb
+++ b/spec/active_relation/relations/join_relation_spec.rb
@@ -27,33 +27,17 @@ describe JoinRelation do
describe '#to_sql' do
before do
- @relation1 = @relation1.select(@relation1[:id] == @relation2[:foo_id])
+ @relation1 = @relation1.select(@relation1[:id] == 1)
end
it 'manufactures sql joining the two tables on the predicate, merging the selects' do
- InnerJoinRelation.new(@relation1, @relation2, @predicate).to_s.should == SelectBuilder.new do
- select do
- column :foo, :name
- column :foo, :id
- column :bar, :name
- column :bar, :foo_id
- column :bar, :id
- end
- from :foo do
- inner_join :bar do
- equals do
- column :foo, :id
- column :bar, :id
- end
- end
- end
- where do
- equals do
- column :foo, :id
- column :bar, :foo_id
- end
- end
- end.to_s
+ InnerJoinRelation.new(@relation1, @relation2, @predicate).to_sql.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id`, `bar`.`name`, `bar`.`foo_id`, `bar`.`id`
+ FROM `foo`
+ INNER JOIN `bar` ON `foo`.`id` = `bar`.`id`
+ WHERE
+ `foo`.`id` = 1
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/order_relation_spec.rb b/spec/active_relation/relations/order_relation_spec.rb
index 17f730b564..8655780c37 100644
--- a/spec/active_relation/relations/order_relation_spec.rb
+++ b/spec/active_relation/relations/order_relation_spec.rb
@@ -7,15 +7,7 @@ describe OrderRelation do
@attribute1 = @relation1[:id]
@attribute2 = @relation2[:id]
end
-
- describe '==' do
- it "obtains if the relation and attributes are identical" do
- OrderRelation.new(@relation1, @attribute1, @attribute2).should == OrderRelation.new(@relation1, @attribute1, @attribute2)
- OrderRelation.new(@relation1, @attribute1).should_not == OrderRelation.new(@relation2, @attribute1)
- OrderRelation.new(@relation1, @attribute1, @attribute2).should_not == OrderRelation.new(@relation1, @attribute2, @attribute1)
- end
- end
-
+
describe '#qualify' do
it "distributes over the relation and attributes" do
OrderRelation.new(@relation1, @attribute1).qualify. \
@@ -25,16 +17,11 @@ describe OrderRelation do
describe '#to_sql' do
it "manufactures sql with an order clause" do
- OrderRelation.new(@relation1, @attribute1).to_s.should == SelectBuilder.new do
- select do
- column :foo, :name
- column :foo, :id
- end
- from :foo
- order_by do
- column :foo, :id
- end
- end.to_s
+ OrderRelation.new(@relation1, @attribute1).to_sql.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id`
+ FROM `foo`
+ ORDER BY `foo`.`id`
+ """)
end
end
diff --git a/spec/active_relation/relations/projection_relation_spec.rb b/spec/active_relation/relations/projection_relation_spec.rb
index 164c485761..77722a17c5 100644
--- a/spec/active_relation/relations/projection_relation_spec.rb
+++ b/spec/active_relation/relations/projection_relation_spec.rb
@@ -25,12 +25,10 @@ describe ProjectionRelation do
describe '#to_sql' do
it "manufactures sql with a limited select clause" do
- ProjectionRelation.new(@relation1, @attribute1).to_s.should == SelectBuilder.new do
- select do
- column :foo, :id
- end
- from :foo
- end.to_s
+ ProjectionRelation.new(@relation1, @attribute1).to_sql.should be_like("""
+ SELECT `foo`.`id`
+ FROM `foo`
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/range_relation_spec.rb b/spec/active_relation/relations/range_relation_spec.rb
index ac9f887d9b..67e2b0d164 100644
--- a/spec/active_relation/relations/range_relation_spec.rb
+++ b/spec/active_relation/relations/range_relation_spec.rb
@@ -7,15 +7,7 @@ describe RangeRelation do
@range1 = 1..2
@range2 = 4..9
end
-
- describe '==' do
- it "obtains if the relation and range are identical" do
- RangeRelation.new(@relation1, @range1).should == RangeRelation.new(@relation1, @range1)
- RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation2, @range1)
- RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation1, @range2)
- end
- end
-
+
describe '#qualify' do
it "distributes over the relation and attributes" do
pending
@@ -26,15 +18,12 @@ describe RangeRelation do
it "manufactures sql with limit and offset" do
range_size = @range2.last - @range2.first + 1
range_start = @range2.first
- RangeRelation.new(@relation1, @range2).to_s.should == SelectBuilder.new do
- select do
- column :foo, :name
- column :foo, :id
- end
- from :foo
- limit range_size
- offset range_start
- end.to_s
+ RangeRelation.new(@relation1, @range2).to_s.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id`
+ FROM `foo`
+ LIMIT #{range_size}
+ OFFSET #{range_start}
+ """)
end
end
diff --git a/spec/active_relation/relations/relation_spec.rb b/spec/active_relation/relations/relation_spec.rb
index 9ed42a98cb..5d7c40a530 100644
--- a/spec/active_relation/relations/relation_spec.rb
+++ b/spec/active_relation/relations/relation_spec.rb
@@ -10,17 +10,17 @@ describe Relation do
describe '[]' do
it "manufactures an attribute when given a symbol" do
- @relation1[:id].should be_eql(Attribute.new(@relation1, :id))
+ @relation1[:id].should be_kind_of(Attribute)
end
it "manufactures a range relation when given a range" do
- @relation1[1..2].should == RangeRelation.new(@relation1, 1..2)
+ @relation1[1..2].should be_kind_of(RangeRelation)
end
end
describe '#include?' do
it "manufactures an inclusion predicate" do
- @relation1.include?(@attribute1).should == RelationInclusionPredicate.new(@attribute1, @relation1)
+ @relation1.include?(@attribute1).should be_kind_of(RelationInclusionPredicate)
end
end
@@ -28,13 +28,13 @@ describe Relation do
describe 'joins' do
describe '<=>' do
it "manufactures an inner join operation between those two relations" do
- (@relation1 <=> @relation2).should == InnerJoinOperation.new(@relation1, @relation2)
+ (@relation1 <=> @relation2).should be_kind_of(InnerJoinOperation)
end
end
describe '<<' do
it "manufactures a left outer join operation between those two relations" do
- (@relation1 << @relation2).should == LeftOuterJoinOperation.new(@relation1, @relation2)
+ (@relation1 << @relation2).should be_kind_of(LeftOuterJoinOperation)
end
end
end
@@ -45,13 +45,13 @@ describe Relation do
end
it "manufactures a projection relation" do
- @relation1.project(@attribute1, @attribute2).should == ProjectionRelation.new(@relation1, @attribute1, @attribute2)
+ @relation1.project(@attribute1, @attribute2).should be_kind_of(ProjectionRelation)
end
end
describe '#rename' do
it "manufactures a rename relation" do
- @relation1.rename(@attribute1, :foo).should == RenameRelation.new(@relation1, @attribute1 => :foo)
+ @relation1.rename(@attribute1, :foo).should be_kind_of(RenameRelation)
end
end
@@ -61,17 +61,17 @@ describe Relation do
end
it "manufactures a selection relation" do
- @relation1.select(@predicate).should == SelectionRelation.new(@relation1, @predicate)
+ @relation1.select(@predicate).should be_kind_of(SelectionRelation)
end
it "accepts arbitrary strings" do
- @relation1.select("arbitrary").should == SelectionRelation.new(@relation1, "arbitrary")
+ @relation1.select("arbitrary").should be_kind_of(SelectionRelation)
end
end
describe '#order' do
it "manufactures an order relation" do
- @relation1.order(@attribute1, @attribute2).should == OrderRelation.new(@relation1, @attribute1, @attribute2)
+ @relation1.order(@attribute1, @attribute2).should be_kind_of(OrderRelation)
end
end
end
@@ -79,13 +79,13 @@ describe Relation do
describe 'write operations' do
describe '#delete' do
it 'manufactures a deletion relation' do
- @relation1.delete.should == DeletionRelation.new(@relation1)
+ @relation1.delete.should be_kind_of(DeletionRelation)
end
end
describe '#insert' do
it 'manufactures an insertion relation' do
- @relation1.insert(tuple = {:id => 1}).should == InsertionRelation.new(@relation1, tuple)
+ @relation1.insert(record = {:id => 1}).should be_kind_of(InsertionRelation)
end
end
end
diff --git a/spec/active_relation/relations/rename_relation_spec.rb b/spec/active_relation/relations/rename_relation_spec.rb
index 00a93d10ac..664e9c6145 100644
--- a/spec/active_relation/relations/rename_relation_spec.rb
+++ b/spec/active_relation/relations/rename_relation_spec.rb
@@ -52,13 +52,10 @@ describe RenameRelation do
describe '#to_sql' do
it 'manufactures sql aliasing the attribute' do
- @renamed_relation.to_s.should == SelectBuilder.new do
- select do
- column :foo, :name
- column :foo, :id, :schmid
- end
- from :foo
- end.to_s
+ @renamed_relation.to_sql.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id` AS 'schmid'
+ FROM `foo`
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/selection_relation_spec.rb b/spec/active_relation/relations/selection_relation_spec.rb
index c4aadc807b..ac1f227c67 100644
--- a/spec/active_relation/relations/selection_relation_spec.rb
+++ b/spec/active_relation/relations/selection_relation_spec.rb
@@ -8,17 +8,6 @@ describe SelectionRelation do
@predicate2 = LessThanPredicate.new(@relation1[:age], 2)
end
- describe '==' do
- it "obtains if both the predicate and the relation are identical" do
- SelectionRelation.new(@relation1, @predicate1). \
- should == SelectionRelation.new(@relation1, @predicate1)
- SelectionRelation.new(@relation1, @predicate1). \
- should_not == SelectionRelation.new(@relation2, @predicate1)
- SelectionRelation.new(@relation1, @predicate1). \
- should_not == SelectionRelation.new(@relation1, @predicate2)
- end
- end
-
describe '#initialize' do
it "manufactures nested selection relations if multiple predicates are provided" do
SelectionRelation.new(@relation1, @predicate1, @predicate2). \
@@ -35,19 +24,19 @@ describe SelectionRelation do
describe '#to_sql' do
it "manufactures sql with where clause conditions" do
- SelectionRelation.new(@relation1, @predicate1).to_s.should == SelectBuilder.new do
- select do
- column :foo, :name
- column :foo, :id
- end
- from :foo
- where do
- equals do
- column :foo, :id
- column :bar, :foo_id
- end
- end
- end.to_s
+ SelectionRelation.new(@relation1, @predicate1).to_sql.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id`
+ FROM `foo`
+ WHERE `foo`.`id` = `bar`.`foo_id`
+ """)
+ end
+
+ it "allows arbitrary sql" do
+ SelectionRelation.new(@relation1, "asdf").to_sql.should be_like("""
+ SELECT `foo`.`name`, `foo`.`id`
+ FROM `foo`
+ WHERE asdf
+ """)
end
end
end \ No newline at end of file
diff --git a/spec/active_relation/relations/table_relation_spec.rb b/spec/active_relation/relations/table_relation_spec.rb
index c943fe6c92..79e9610e1b 100644
--- a/spec/active_relation/relations/table_relation_spec.rb
+++ b/spec/active_relation/relations/table_relation_spec.rb
@@ -7,13 +7,10 @@ describe TableRelation do
describe '#to_sql' do
it "returns a simple SELECT query" do
- @relation.to_sql.should == SelectBuilder.new do |s|
- select do
- column :users, :name
- column :users, :id
- end
- from :users
- end
+ @relation.to_sql.should be_like("""
+ SELECT `users`.`name`, `users`.`id`
+ FROM `users`
+ """)
end
end
diff --git a/spec/active_relation/sql_builder/conditions_spec.rb b/spec/active_relation/sql_builder/conditions_spec.rb
deleted file mode 100644
index dc2d10a2f6..0000000000
--- a/spec/active_relation/sql_builder/conditions_spec.rb
+++ /dev/null
@@ -1,18 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-
-describe ConditionsBuilder do
- describe '#to_s' do
- describe 'with aliased columns' do
- it 'manufactures correct sql' do
- ConditionsBuilder.new do
- equals do
- column(:a, :b)
- column(:c, :d, 'e')
- end
- end.to_s.should be_like("""
- `a`.`b` = `c`.`d`
- """)
- end
- end
- end
-end \ No newline at end of file
diff --git a/spec/active_relation/sql_builder/delete_builder_spec.rb b/spec/active_relation/sql_builder/delete_builder_spec.rb
deleted file mode 100644
index fd62fde155..0000000000
--- a/spec/active_relation/sql_builder/delete_builder_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-
-describe DeleteBuilder do
- describe '#to_s' do
- it 'manufactures correct sql' do
- DeleteBuilder.new do
- delete
- from :users
- where do
- equals do
- column :users, :id
- value 1
- end
- end
- end.to_s.should be_like("""
- DELETE
- FROM `users`
- WHERE `users`.`id` = 1
- """)
- end
- end
-end \ No newline at end of file
diff --git a/spec/active_relation/sql_builder/insert_builder_spec.rb b/spec/active_relation/sql_builder/insert_builder_spec.rb
deleted file mode 100644
index dddc971986..0000000000
--- a/spec/active_relation/sql_builder/insert_builder_spec.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-
-describe InsertBuilder do
- describe '#to_s' do
- it 'manufactures correct sql' do
- InsertBuilder.new do
- insert
- into :users
- columns do
- column :users, :id
- column :users, :name
- end
- values do
- row 1, 'bob'
- row 2, 'moe'
- end
- end.to_s.should be_like("""
- INSERT
- INTO `users`
- (`users`.`id`, `users`.`name`) VALUES (1, 'bob'), (2, 'moe')
- """)
- end
- end
-end \ No newline at end of file
diff --git a/spec/active_relation/sql_builder/select_builder_spec.rb b/spec/active_relation/sql_builder/select_builder_spec.rb
deleted file mode 100644
index 6539afe0c4..0000000000
--- a/spec/active_relation/sql_builder/select_builder_spec.rb
+++ /dev/null
@@ -1,148 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-
-describe SelectBuilder do
- describe '#to_s' do
- describe 'with select and from clauses' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- all
- end
- from :users
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- """)
- end
- end
-
- describe 'with specified columns and column aliases' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- 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`
- """)
- end
- end
-
- describe 'with where clause' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- all
- end
- from :users
- where do
- equals do
- value 1
- column :b, :c
- end
- end
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- WHERE 1 = `b`.`c`
- """)
- end
-
- it 'accepts arbitrary strings' do
- SelectBuilder.new do
- select do
- all
- end
- from :users
- where do
- value "'a' = 'a'"
- end
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- WHERE 'a' = 'a'
- """)
- end
- end
-
- describe 'with inner join' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- all
- end
- from :users do
- inner_join(:friendships) do
- equals do
- column :users, :id
- column :friendships, :user_id
- end
- end
- end
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- INNER JOIN `friendships`
- ON `users`.`id` = `friendships`.`user_id`
- """)
- end
-
- it 'accepts arbitrary on strings' do
- SelectBuilder.new do
- select do
- all
- end
- from :users do
- inner_join :friendships do
- value "arbitrary"
- end
- end
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- INNER JOIN `friendships` ON arbitrary
- """)
- end
- end
-
- describe 'with order' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- all
- end
- from :users
- order_by do
- column :users, :id
- column :users, :created_at, 'alias'
- end
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- ORDER BY `users`.`id`, `users`.`created_at`
- """)
- end
- end
-
- describe 'with limit and/or offset' do
- it 'manufactures correct sql' do
- SelectBuilder.new do
- select do
- all
- end
- from :users
- limit 10
- offset 10
- end.to_s.should be_like("""
- SELECT *
- FROM `users`
- LIMIT 10
- OFFSET 10
- """)
- end
- end
- end
-end \ No newline at end of file