From bdca9ed42ffea10aa6989ea3ecebedb424fa01ed Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:20:29 -0400 Subject: moved sql related code to its own engine area Conflicts: lib/arel/engine.rb lib/arel/extensions/object.rb lib/arel/predicates.rb lib/arel/primitives/attribute.rb lib/arel/primitives/expression.rb lib/arel/primitives/value.rb lib/arel/relations/operations/join.rb lib/arel/relations/relation.rb lib/arel/relations/utilities/externalization.rb lib/arel/relations/utilities/nil.rb lib/arel/relations/writes/delete.rb lib/arel/relations/writes/insert.rb lib/arel/relations/writes/update.rb spec/arel/unit/relations/skip_spec.rb spec/arel/unit/relations/take_spec.rb spec/spec_helper.rb --- doc/TODO | 2 +- lib/arel.rb | 3 +- lib/arel/engine.rb | 42 ------- lib/arel/engines.rb | 2 + lib/arel/engines/array/array.rb | 1 + lib/arel/engines/array/relations/array.rb | 19 ++++ lib/arel/engines/sql/christener.rb | 13 +++ lib/arel/engines/sql/engine.rb | 41 +++++++ lib/arel/engines/sql/extensions.rb | 4 + lib/arel/engines/sql/extensions/array.rb | 9 ++ lib/arel/engines/sql/extensions/nil_class.rb | 5 + lib/arel/engines/sql/extensions/object.rb | 9 ++ lib/arel/engines/sql/extensions/range.rb | 9 ++ lib/arel/engines/sql/formatters.rb | 121 +++++++++++++++++++++ lib/arel/engines/sql/predicates.rb | 37 +++++++ lib/arel/engines/sql/primitives.rb | 3 + lib/arel/engines/sql/primitives/attribute.rb | 17 +++ lib/arel/engines/sql/primitives/expression.rb | 7 ++ lib/arel/engines/sql/primitives/value.rb | 11 ++ lib/arel/engines/sql/relations.rb | 5 + lib/arel/engines/sql/relations/operations.rb | 2 + lib/arel/engines/sql/relations/operations/alias.rb | 5 + lib/arel/engines/sql/relations/operations/join.rb | 19 ++++ lib/arel/engines/sql/relations/relation.rb | 28 +++++ lib/arel/engines/sql/relations/table.rb | 36 ++++++ lib/arel/engines/sql/relations/utilities.rb | 3 + .../sql/relations/utilities/externalization.rb | 14 +++ lib/arel/engines/sql/relations/utilities/nil.rb | 6 + .../engines/sql/relations/utilities/recursion.rb | 13 +++ lib/arel/engines/sql/relations/writes.rb | 3 + lib/arel/engines/sql/relations/writes/delete.rb | 12 ++ lib/arel/engines/sql/relations/writes/insert.rb | 12 ++ lib/arel/engines/sql/relations/writes/update.rb | 14 +++ lib/arel/engines/sql/sql.rb | 7 ++ lib/arel/extensions.rb | 2 - lib/arel/extensions/array.rb | 8 -- lib/arel/extensions/nil_class.rb | 5 - lib/arel/extensions/object.rb | 8 -- lib/arel/extensions/range.rb | 9 -- lib/arel/predicates.rb | 15 --- lib/arel/primitives/attribute.rb | 12 -- lib/arel/primitives/expression.rb | 4 - lib/arel/primitives/value.rb | 13 --- lib/arel/relations.rb | 2 - lib/arel/relations/array.rb | 19 ---- lib/arel/relations/operations/alias.rb | 1 - lib/arel/relations/operations/join.rb | 16 --- lib/arel/relations/relation.rb | 32 +----- lib/arel/relations/table.rb | 36 ------ lib/arel/relations/utilities.rb | 2 - lib/arel/relations/utilities/externalization.rb | 10 -- lib/arel/relations/utilities/nil.rb | 3 - lib/arel/relations/utilities/recursion.rb | 13 --- lib/arel/relations/writes/delete.rb | 9 -- lib/arel/relations/writes/insert.rb | 9 -- lib/arel/relations/writes/update.rb | 11 -- lib/arel/sql.rb | 2 - lib/arel/sql/christener.rb | 13 --- lib/arel/sql/formatters.rb | 121 --------------------- spec/arel/unit/relations/skip_spec.rb | 2 +- spec/arel/unit/relations/table_spec.rb | 4 +- spec/arel/unit/relations/take_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 63 files changed, 495 insertions(+), 424 deletions(-) delete mode 100644 lib/arel/engine.rb create mode 100644 lib/arel/engines.rb create mode 100644 lib/arel/engines/array/array.rb create mode 100644 lib/arel/engines/array/relations/array.rb create mode 100644 lib/arel/engines/sql/christener.rb create mode 100644 lib/arel/engines/sql/engine.rb create mode 100644 lib/arel/engines/sql/extensions.rb create mode 100644 lib/arel/engines/sql/extensions/array.rb create mode 100644 lib/arel/engines/sql/extensions/nil_class.rb create mode 100644 lib/arel/engines/sql/extensions/object.rb create mode 100644 lib/arel/engines/sql/extensions/range.rb create mode 100644 lib/arel/engines/sql/formatters.rb create mode 100644 lib/arel/engines/sql/predicates.rb create mode 100644 lib/arel/engines/sql/primitives.rb create mode 100644 lib/arel/engines/sql/primitives/attribute.rb create mode 100644 lib/arel/engines/sql/primitives/expression.rb create mode 100644 lib/arel/engines/sql/primitives/value.rb create mode 100644 lib/arel/engines/sql/relations.rb create mode 100644 lib/arel/engines/sql/relations/operations.rb create mode 100644 lib/arel/engines/sql/relations/operations/alias.rb create mode 100644 lib/arel/engines/sql/relations/operations/join.rb create mode 100644 lib/arel/engines/sql/relations/relation.rb create mode 100644 lib/arel/engines/sql/relations/table.rb create mode 100644 lib/arel/engines/sql/relations/utilities.rb create mode 100644 lib/arel/engines/sql/relations/utilities/externalization.rb create mode 100644 lib/arel/engines/sql/relations/utilities/nil.rb create mode 100644 lib/arel/engines/sql/relations/utilities/recursion.rb create mode 100644 lib/arel/engines/sql/relations/writes.rb create mode 100644 lib/arel/engines/sql/relations/writes/delete.rb create mode 100644 lib/arel/engines/sql/relations/writes/insert.rb create mode 100644 lib/arel/engines/sql/relations/writes/update.rb create mode 100644 lib/arel/engines/sql/sql.rb delete mode 100644 lib/arel/extensions/nil_class.rb delete mode 100644 lib/arel/extensions/range.rb delete mode 100644 lib/arel/relations/array.rb delete mode 100644 lib/arel/relations/table.rb delete mode 100644 lib/arel/relations/utilities/recursion.rb delete mode 100644 lib/arel/sql.rb delete mode 100644 lib/arel/sql/christener.rb delete mode 100644 lib/arel/sql/formatters.rb diff --git a/doc/TODO b/doc/TODO index ad12640881..8a8dcf5380 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,5 +1,5 @@ todo: - +- expressions should be class-based, and joins too, anything _sql should be renamed - refactor adapter pattern - implement in memory adapter - implement mnesia adapter diff --git a/lib/arel.rb b/lib/arel.rb index 00bfa1e292..f22287fcc5 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -7,9 +7,8 @@ require 'active_record/connection_adapters/abstract/quoting' require 'arel/arel' require 'arel/extensions' -require 'arel/sql' require 'arel/predicates' require 'arel/relations' -require 'arel/engine' +require 'arel/engines' require 'arel/session' require 'arel/primitives' diff --git a/lib/arel/engine.rb b/lib/arel/engine.rb deleted file mode 100644 index 2bb4bbbd90..0000000000 --- a/lib/arel/engine.rb +++ /dev/null @@ -1,42 +0,0 @@ -module Arel - # this file is currently just a hack to adapt between activerecord::base which holds the connection specification - # and active relation. ultimately, this file should be in effect what the connection specification is in active record; - # that is: a spec of the database (url, password, etc.), a quoting adapter layer, and a connection pool. - class Engine - def initialize(ar = nil) - @ar = ar - end - - def connection - @ar.connection - end - - def method_missing(method, *args, &block) - @ar.connection.send(method, *args, &block) - end - - module CRUD - def create(relation) - connection.insert(relation.to_sql) - end - - def read(relation) - results = connection.execute(relation.to_sql) - rows = [] - results.each do |row| - rows << attributes.zip(row).to_hash - end - rows - end - - def update(relation) - connection.update(relation.to_sql) - end - - def delete(relation) - connection.delete(relation.to_sql) - end - end - include CRUD - end -end diff --git a/lib/arel/engines.rb b/lib/arel/engines.rb new file mode 100644 index 0000000000..63a929528f --- /dev/null +++ b/lib/arel/engines.rb @@ -0,0 +1,2 @@ +require 'arel/engines/sql/sql' +require 'arel/engines/array/array' \ No newline at end of file diff --git a/lib/arel/engines/array/array.rb b/lib/arel/engines/array/array.rb new file mode 100644 index 0000000000..3a3a47308a --- /dev/null +++ b/lib/arel/engines/array/array.rb @@ -0,0 +1 @@ +require 'arel/engines/array/relations/array' \ No newline at end of file diff --git a/lib/arel/engines/array/relations/array.rb b/lib/arel/engines/array/relations/array.rb new file mode 100644 index 0000000000..dac65abbc2 --- /dev/null +++ b/lib/arel/engines/array/relations/array.rb @@ -0,0 +1,19 @@ +module Arel + class Array < Relation + include Recursion::BaseCase + + def initialize(array, attribute_names) + @array, @attribute_names = array, attribute_names + end + + def attributes + @attributes ||= @attribute_names.collect do |name| + Attribute.new(self, name.to_sym) + end + end + + def call(connection = nil) + @array.collect { |row| attributes.zip(row).to_hash } + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/christener.rb b/lib/arel/engines/sql/christener.rb new file mode 100644 index 0000000000..5883a75f41 --- /dev/null +++ b/lib/arel/engines/sql/christener.rb @@ -0,0 +1,13 @@ +module Arel + module Sql + class Christener + def name_for(relation) + @used_names ||= Hash.new(0) + (@relation_names ||= Hash.new do |hash, relation| + @used_names[name = relation.name] += 1 + hash[relation] = name + (@used_names[name] > 1 ? "_#{@used_names[name]}" : '') + end)[relation.table] + end + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/engine.rb b/lib/arel/engines/sql/engine.rb new file mode 100644 index 0000000000..e5d1a8b0ca --- /dev/null +++ b/lib/arel/engines/sql/engine.rb @@ -0,0 +1,41 @@ +module Arel + module Sql + class Engine + def initialize(ar = nil) + @ar = ar + end + + def connection + @ar.connection + end + + def method_missing(method, *args, &block) + @ar.connection.send(method, *args, &block) + end + + module CRUD + def create(relation) + connection.insert(relation.to_sql) + end + + def read(relation) + results = connection.execute(relation.to_sql) + rows = [] + results.each do |row| + rows << attributes.zip(row).to_hash + end + rows + end + + def update(relation) + connection.update(relation.to_sql) + end + + def delete(relation) + connection.delete(relation.to_sql) + end + end + include CRUD + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/extensions.rb b/lib/arel/engines/sql/extensions.rb new file mode 100644 index 0000000000..6f4ad32148 --- /dev/null +++ b/lib/arel/engines/sql/extensions.rb @@ -0,0 +1,4 @@ +require 'arel/engines/sql/extensions/object' +require 'arel/engines/sql/extensions/array' +require 'arel/engines/sql/extensions/range' +require 'arel/engines/sql/extensions/nil_class' \ No newline at end of file diff --git a/lib/arel/engines/sql/extensions/array.rb b/lib/arel/engines/sql/extensions/array.rb new file mode 100644 index 0000000000..1daa5abca7 --- /dev/null +++ b/lib/arel/engines/sql/extensions/array.rb @@ -0,0 +1,9 @@ +class Array + def to_sql(formatter = nil) + "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")" + end + + def inclusion_predicate_sql + "IN" + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/extensions/nil_class.rb b/lib/arel/engines/sql/extensions/nil_class.rb new file mode 100644 index 0000000000..729c4cada7 --- /dev/null +++ b/lib/arel/engines/sql/extensions/nil_class.rb @@ -0,0 +1,5 @@ +class NilClass + def equality_predicate_sql + 'IS' + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/extensions/object.rb b/lib/arel/engines/sql/extensions/object.rb new file mode 100644 index 0000000000..ef990eee2f --- /dev/null +++ b/lib/arel/engines/sql/extensions/object.rb @@ -0,0 +1,9 @@ +class Object + def to_sql(formatter) + formatter.scalar self + end + + def equality_predicate_sql + '=' + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/extensions/range.rb b/lib/arel/engines/sql/extensions/range.rb new file mode 100644 index 0000000000..d7329efe34 --- /dev/null +++ b/lib/arel/engines/sql/extensions/range.rb @@ -0,0 +1,9 @@ +class Range + def to_sql(formatter = nil) + formatter.range self.begin, self.end + end + + def inclusion_predicate_sql + "BETWEEN" + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/formatters.rb b/lib/arel/engines/sql/formatters.rb new file mode 100644 index 0000000000..f82ddf631f --- /dev/null +++ b/lib/arel/engines/sql/formatters.rb @@ -0,0 +1,121 @@ +module Arel + module Sql + class Formatter + attr_reader :environment + delegate :christener, :engine, :to => :environment + delegate :name_for, :to => :christener + delegate :quote_table_name, :quote_column_name, :quote, :to => :engine + + def initialize(environment) + @environment = environment + end + end + + class SelectClause < Formatter + def attribute(attribute) + # FIXME this should check that the column exists + if attribute.name.to_s =~ /^\w*$/ + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") + else + attribute.name.to_s + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") + end + end + + def expression(expression) + if expression.function_sql == "DISTINCT" + "#{expression.function_sql} #{expression.attribute.to_sql(self)}" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') + else + "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : " AS #{expression.function_sql.to_s.downcase}_id") + end + end + + def select(select_sql, table) + "(#{select_sql}) AS #{quote_table_name(name_for(table))}" + end + + def value(value) + value + end + end + + class PassThrough < Formatter + def value(value) + value + end + end + + class WhereClause < PassThrough + end + + class OrderClause < PassThrough + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + end + end + + class GroupClause < PassThrough + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + end + end + + class WhereCondition < Formatter + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + end + + def expression(expression) + "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + end + + def value(value) + value.to_sql(self) + end + + def scalar(value, column = nil) + quote(value, column) + end + + def select(select_sql, table) + "(#{select_sql})" + end + end + + class SelectStatement < Formatter + def select(select_sql, table) + select_sql + end + end + + class TableReference < Formatter + def select(select_sql, table) + "(#{select_sql}) AS #{quote_table_name(name_for(table))}" + end + + def table(table) + if table.name =~ /^(\w|-)*$/ + quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') + else + table.name + (table.name != name_for(table) ? " AS " + (name_for(table)) : '') + end + end + end + + class Attribute < WhereCondition + def scalar(scalar) + quote(scalar, environment.column) + end + + def array(array) + "(" + array.collect { |e| e.to_sql(self) }.join(', ') + ")" + end + + def range(left, right) + "#{left} AND #{right}" + end + end + + class Value < WhereCondition + end + end +end diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb new file mode 100644 index 0000000000..dfeddb2de1 --- /dev/null +++ b/lib/arel/engines/sql/predicates.rb @@ -0,0 +1,37 @@ +module Arel + class Binary < Predicate + def to_sql(formatter = nil) + "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" + end + end + + class Equality < Binary + def predicate_sql + operand2.equality_predicate_sql + end + end + + class GreaterThanOrEqualTo < Binary + def predicate_sql; '>=' end + end + + class GreaterThan < Binary + def predicate_sql; '>' end + end + + class LessThanOrEqualTo < Binary + def predicate_sql; '<=' end + end + + class LessThan < Binary + def predicate_sql; '<' end + end + + class Match < Binary + def predicate_sql; 'LIKE' end + end + + class In < Binary + def predicate_sql; operand2.inclusion_predicate_sql end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb new file mode 100644 index 0000000000..405b1ca803 --- /dev/null +++ b/lib/arel/engines/sql/primitives.rb @@ -0,0 +1,3 @@ +require 'arel/engines/sql/primitives/attribute' +require 'arel/engines/sql/primitives/value' +require 'arel/engines/sql/primitives/expression' \ No newline at end of file diff --git a/lib/arel/engines/sql/primitives/attribute.rb b/lib/arel/engines/sql/primitives/attribute.rb new file mode 100644 index 0000000000..48de690b6f --- /dev/null +++ b/lib/arel/engines/sql/primitives/attribute.rb @@ -0,0 +1,17 @@ +require 'set' + +module Arel + class Attribute + def column + original_relation.column_for(self) + end + + def format(object) + object.to_sql(Sql::Attribute.new(self)) + end + + def to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.attribute self + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/primitives/expression.rb b/lib/arel/engines/sql/primitives/expression.rb new file mode 100644 index 0000000000..24f6879848 --- /dev/null +++ b/lib/arel/engines/sql/primitives/expression.rb @@ -0,0 +1,7 @@ +module Arel + class Expression < Attribute + def to_sql(formatter = Sql::SelectClause.new(relation)) + formatter.expression self + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/primitives/value.rb b/lib/arel/engines/sql/primitives/value.rb new file mode 100644 index 0000000000..a44acc26e0 --- /dev/null +++ b/lib/arel/engines/sql/primitives/value.rb @@ -0,0 +1,11 @@ +module Arel + class Value + def to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.value value + end + + def format(object) + object.to_sql(Sql::Value.new(relation)) + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations.rb b/lib/arel/engines/sql/relations.rb new file mode 100644 index 0000000000..4de01b2691 --- /dev/null +++ b/lib/arel/engines/sql/relations.rb @@ -0,0 +1,5 @@ +require 'arel/engines/sql/relations/utilities' +require 'arel/engines/sql/relations/relation' +require 'arel/engines/sql/relations/operations' +require 'arel/engines/sql/relations/writes' +require 'arel/engines/sql/relations/table' \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/operations.rb b/lib/arel/engines/sql/relations/operations.rb new file mode 100644 index 0000000000..ff33fc6bc3 --- /dev/null +++ b/lib/arel/engines/sql/relations/operations.rb @@ -0,0 +1,2 @@ +require 'arel/engines/sql/relations/operations/alias' +require 'arel/engines/sql/relations/operations/join' diff --git a/lib/arel/engines/sql/relations/operations/alias.rb b/lib/arel/engines/sql/relations/operations/alias.rb new file mode 100644 index 0000000000..32c9911a69 --- /dev/null +++ b/lib/arel/engines/sql/relations/operations/alias.rb @@ -0,0 +1,5 @@ +module Arel + class Alias < Compound + include Recursion::BaseCase + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/operations/join.rb b/lib/arel/engines/sql/relations/operations/join.rb new file mode 100644 index 0000000000..be21119bc9 --- /dev/null +++ b/lib/arel/engines/sql/relations/operations/join.rb @@ -0,0 +1,19 @@ +module Arel + class Join < Relation + def table_sql(formatter = Sql::TableReference.new(self)) + relation1.externalize.table_sql(formatter) + end + + def joins(environment, formatter = Sql::TableReference.new(environment)) + @joins ||= begin + this_join = [ + join_sql, + relation2.externalize.table_sql(formatter), + ("ON" unless predicates.blank?), + (ons + relation2.externalize.wheres).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') + ].compact.join(" ") + [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") + end + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb new file mode 100644 index 0000000000..5fd4121176 --- /dev/null +++ b/lib/arel/engines/sql/relations/relation.rb @@ -0,0 +1,28 @@ +module Arel + class Relation + def to_sql(formatter = Sql::SelectStatement.new(self)) + formatter.select select_sql, self + end + + def select_sql + [ + "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", + "FROM #{table_sql(Sql::TableReference.new(self))}", + (joins(self) unless joins(self).blank? ), + ("WHERE #{wheres .collect { |w| w.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless wheres.blank? ), + ("GROUP BY #{groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }.join(', ')}" unless groupings.blank? ), + ("ORDER BY #{orders .collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), + ("LIMIT #{taken}" unless taken.blank? ), + ("OFFSET #{skipped}" unless skipped.blank? ) + ].compact.join("\n") + end + + def inclusion_predicate_sql + "IN" + end + + def christener + @christener ||= Sql::Christener.new + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb new file mode 100644 index 0000000000..0433abbbb2 --- /dev/null +++ b/lib/arel/engines/sql/relations/table.rb @@ -0,0 +1,36 @@ +module Arel + class Table < Relation + include Recursion::BaseCase + + cattr_accessor :engine + attr_reader :name, :engine + hash_on :name + + def initialize(name, engine = Table.engine) + @name, @engine = name.to_s, engine + end + + def attributes + @attributes ||= columns.collect do |column| + Attribute.new(self, column.name.to_sym) + end + end + + def column_for(attribute) + has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s } + end + + def columns + @columns ||= engine.columns(name, "#{name} Columns") + end + + def reset + @attributes = @columns = nil + end + + def ==(other) + Table === other and + name == other.name + end + end +end diff --git a/lib/arel/engines/sql/relations/utilities.rb b/lib/arel/engines/sql/relations/utilities.rb new file mode 100644 index 0000000000..a1451ed448 --- /dev/null +++ b/lib/arel/engines/sql/relations/utilities.rb @@ -0,0 +1,3 @@ +require 'arel/engines/sql/relations/utilities/recursion' +require 'arel/engines/sql/relations/utilities/externalization' +require 'arel/engines/sql/relations/utilities/nil' diff --git a/lib/arel/engines/sql/relations/utilities/externalization.rb b/lib/arel/engines/sql/relations/utilities/externalization.rb new file mode 100644 index 0000000000..1ac6f2de8e --- /dev/null +++ b/lib/arel/engines/sql/relations/utilities/externalization.rb @@ -0,0 +1,14 @@ +module Arel + class Externalization < Compound + include Recursion::BaseCase + + def table_sql(formatter = Sql::TableReference.new(relation)) + formatter.select relation.select_sql, self + end + + # REMOVEME + def name + relation.name + '_external' + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/utilities/nil.rb b/lib/arel/engines/sql/relations/utilities/nil.rb new file mode 100644 index 0000000000..77534b25ad --- /dev/null +++ b/lib/arel/engines/sql/relations/utilities/nil.rb @@ -0,0 +1,6 @@ +module Arel + class Nil < Relation + def table_sql(formatter = nil); '' end + def name; '' end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/utilities/recursion.rb b/lib/arel/engines/sql/relations/utilities/recursion.rb new file mode 100644 index 0000000000..848b059507 --- /dev/null +++ b/lib/arel/engines/sql/relations/utilities/recursion.rb @@ -0,0 +1,13 @@ +module Arel + module Recursion + module BaseCase + def table + self + end + + def table_sql(formatter = Sql::TableReference.new(self)) + formatter.table self + end + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb new file mode 100644 index 0000000000..dcadc326d9 --- /dev/null +++ b/lib/arel/engines/sql/relations/writes.rb @@ -0,0 +1,3 @@ +require 'arel/engines/sql/relations/writes/delete' +require 'arel/engines/sql/relations/writes/insert' +require 'arel/engines/sql/relations/writes/update' diff --git a/lib/arel/engines/sql/relations/writes/delete.rb b/lib/arel/engines/sql/relations/writes/delete.rb new file mode 100644 index 0000000000..b22ee51e24 --- /dev/null +++ b/lib/arel/engines/sql/relations/writes/delete.rb @@ -0,0 +1,12 @@ +module Arel + class Deletion < Compound + def to_sql(formatter = nil) + [ + "DELETE", + "FROM #{table_sql}", + ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("LIMIT #{taken}" unless taken.blank? ), + ].compact.join("\n") + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/writes/insert.rb b/lib/arel/engines/sql/relations/writes/insert.rb new file mode 100644 index 0000000000..aac9c87a5b --- /dev/null +++ b/lib/arel/engines/sql/relations/writes/insert.rb @@ -0,0 +1,12 @@ +module Arel + class Insert < Compound + def to_sql(formatter = nil) + [ + "INSERT", + "INTO #{table_sql}", + "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", + "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + ].join("\n") + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/relations/writes/update.rb b/lib/arel/engines/sql/relations/writes/update.rb new file mode 100644 index 0000000000..3e35a0d5cc --- /dev/null +++ b/lib/arel/engines/sql/relations/writes/update.rb @@ -0,0 +1,14 @@ +module Arel + class Update < Compound + def to_sql(formatter = nil) + [ + "UPDATE #{table_sql} SET", + assignments.collect do |attribute, value| + "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" + end.join(",\n"), + ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("LIMIT #{taken}" unless taken.blank? ) + ].join("\n") + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/sql/sql.rb b/lib/arel/engines/sql/sql.rb new file mode 100644 index 0000000000..aed1fd861e --- /dev/null +++ b/lib/arel/engines/sql/sql.rb @@ -0,0 +1,7 @@ +require 'arel/engines/sql/engine' +require 'arel/engines/sql/relations' +require 'arel/engines/sql/primitives' +require 'arel/engines/sql/predicates' +require 'arel/engines/sql/formatters' +require 'arel/engines/sql/extensions' +require 'arel/engines/sql/christener' \ No newline at end of file diff --git a/lib/arel/extensions.rb b/lib/arel/extensions.rb index 160cf36e5b..299ba0631c 100644 --- a/lib/arel/extensions.rb +++ b/lib/arel/extensions.rb @@ -2,5 +2,3 @@ require 'arel/extensions/object' require 'arel/extensions/class' require 'arel/extensions/array' require 'arel/extensions/hash' -require 'arel/extensions/range' -require 'arel/extensions/nil_class' \ No newline at end of file diff --git a/lib/arel/extensions/array.rb b/lib/arel/extensions/array.rb index 793c06aad8..5b6d6d6abd 100644 --- a/lib/arel/extensions/array.rb +++ b/lib/arel/extensions/array.rb @@ -2,12 +2,4 @@ class Array def to_hash Hash[*flatten] end - - def to_sql(formatter = nil) - "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")" - end - - def inclusion_predicate_sql - "IN" - end end \ No newline at end of file diff --git a/lib/arel/extensions/nil_class.rb b/lib/arel/extensions/nil_class.rb deleted file mode 100644 index 729c4cada7..0000000000 --- a/lib/arel/extensions/nil_class.rb +++ /dev/null @@ -1,5 +0,0 @@ -class NilClass - def equality_predicate_sql - 'IS' - end -end \ No newline at end of file diff --git a/lib/arel/extensions/object.rb b/lib/arel/extensions/object.rb index 14e2f82ce5..d626407dcb 100644 --- a/lib/arel/extensions/object.rb +++ b/lib/arel/extensions/object.rb @@ -7,14 +7,6 @@ class Object bind(relation) end - def to_sql(formatter) - formatter.scalar self - end - - def equality_predicate_sql - '=' - end - def metaclass class << self self diff --git a/lib/arel/extensions/range.rb b/lib/arel/extensions/range.rb deleted file mode 100644 index d7329efe34..0000000000 --- a/lib/arel/extensions/range.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Range - def to_sql(formatter = nil) - formatter.range self.begin, self.end - end - - def inclusion_predicate_sql - "BETWEEN" - end -end \ No newline at end of file diff --git a/lib/arel/predicates.rb b/lib/arel/predicates.rb index b639022b4e..aa206d4e96 100644 --- a/lib/arel/predicates.rb +++ b/lib/arel/predicates.rb @@ -22,11 +22,6 @@ module Arel def bind(relation) self.class.new(operand1.find_correlate_in(relation), operand2.find_correlate_in(relation)) end - - def to_sql(formatter = nil) - "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" - end - alias_method :to_s, :to_sql end class CompoundPredicate < Binary @@ -49,33 +44,23 @@ module Arel ((operand1 == other.operand1 and operand2 == other.operand2) or (operand1 == other.operand2 and operand2 == other.operand1)) end - - def predicate_sql - operand2.equality_predicate_sql - end end class GreaterThanOrEqualTo < Binary - def predicate_sql; '>=' end end class GreaterThan < Binary - def predicate_sql; '>' end end class LessThanOrEqualTo < Binary - def predicate_sql; '<=' end end class LessThan < Binary - def predicate_sql; '<' end end class Match < Binary - def predicate_sql; 'LIKE' end end class In < Binary - def predicate_sql; operand2.inclusion_predicate_sql end end end diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 6cb558d8ce..5e216770e4 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -18,18 +18,6 @@ module Arel false end - def column - original_relation.column_for(self) - end - - def format(object) - object.to_sql(Sql::Attribute.new(self)) - end - - def to_sql(formatter = Sql::WhereCondition.new(relation)) - formatter.attribute self - end - module Transformations def self.included(klass) klass.send :alias_method, :eql?, :== diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb index 836f014745..b67a5e1f8e 100644 --- a/lib/arel/primitives/expression.rb +++ b/lib/arel/primitives/expression.rb @@ -9,10 +9,6 @@ module Arel @attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor end - def to_sql(formatter = Sql::SelectClause.new(relation)) - formatter.expression self - end - def aggregation? true end diff --git a/lib/arel/primitives/value.rb b/lib/arel/primitives/value.rb index 9c6e518a95..91c4045507 100644 --- a/lib/arel/primitives/value.rb +++ b/lib/arel/primitives/value.rb @@ -4,19 +4,6 @@ module Arel deriving :initialize, :== delegate :inclusion_predicate_sql, :equality_predicate_sql, :to => :value - - def to_sql(formatter = Sql::WhereCondition.new(relation)) - if value =~ /^\(.*\)$/ - value - else - formatter.value value - end - end - - def format(object) - object.to_sql(Sql::Value.new(relation)) - end - def bind(relation) Value.new(value, relation) end diff --git a/lib/arel/relations.rb b/lib/arel/relations.rb index fd758ed15d..f97c35e56e 100644 --- a/lib/arel/relations.rb +++ b/lib/arel/relations.rb @@ -1,6 +1,4 @@ require 'arel/relations/relation' require 'arel/relations/utilities' -require 'arel/relations/table' -require 'arel/relations/array' require 'arel/relations/writes' require 'arel/relations/operations' \ No newline at end of file diff --git a/lib/arel/relations/array.rb b/lib/arel/relations/array.rb deleted file mode 100644 index dac65abbc2..0000000000 --- a/lib/arel/relations/array.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Arel - class Array < Relation - include Recursion::BaseCase - - def initialize(array, attribute_names) - @array, @attribute_names = array, attribute_names - end - - def attributes - @attributes ||= @attribute_names.collect do |name| - Attribute.new(self, name.to_sym) - end - end - - def call(connection = nil) - @array.collect { |row| attributes.zip(row).to_hash } - end - end -end \ No newline at end of file diff --git a/lib/arel/relations/operations/alias.rb b/lib/arel/relations/operations/alias.rb index 8ed33fc597..67837f6a75 100644 --- a/lib/arel/relations/operations/alias.rb +++ b/lib/arel/relations/operations/alias.rb @@ -1,6 +1,5 @@ module Arel class Alias < Compound - include Recursion::BaseCase attributes :relation deriving :initialize alias_method :==, :equal? diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb index 8fe89358d8..8e19254378 100644 --- a/lib/arel/relations/operations/join.rb +++ b/lib/arel/relations/operations/join.rb @@ -9,22 +9,6 @@ module Arel @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates end - def table_sql(formatter = Sql::TableReference.new(self)) - relation1.externalize.table_sql(formatter) - end - - def joins(environment, formatter = Sql::TableReference.new(environment)) - @joins ||= begin - this_join = [ - join_sql, - relation2.externalize.table_sql(formatter), - ("ON" unless predicates.blank?), - (ons + relation2.externalize.wheres).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') - ].compact.join(" ") - [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") - end - end - def attributes @attributes ||= (relation1.externalize.attributes + relation2.externalize.attributes).collect { |a| a.bind(self) } diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index ef295dfdd7..20badaf165 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -6,32 +6,6 @@ module Arel Session.new end - def count - @count = "COUNT(*) AS count_all" - end - - def to_sql(formatter = Sql::SelectStatement.new(self)) - formatter.select select_sql, self - end - alias_method :to_s, :to_sql - - def select_sql - [ - "SELECT #{@count} #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ') unless @count}", - "FROM #{table_sql(Sql::TableReference.new(self))}", - (joins(self) unless joins(self).blank? ), - ("WHERE #{wheres .collect { |w| w.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless wheres.blank? ), - ("GROUP BY #{groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }.join(', ')}" unless groupings.blank? ), - ("ORDER BY #{orders .collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ("OFFSET #{skipped}" unless skipped.blank? ) - ].compact.join("\n") - end - - def inclusion_predicate_sql - "IN" - end - def call engine.read(self) end @@ -44,10 +18,6 @@ module Arel self end - def christener - @christener ||= Sql::Christener.new - end - module Enumerable include ::Enumerable @@ -155,7 +125,7 @@ module Arel def orders; [] end def inserts; [] end def groupings; [] end - def joins(formatter = nil); nil end + def joins(formatter = nil); nil end # FIXME def taken; nil end def skipped; nil end end diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb deleted file mode 100644 index 0433abbbb2..0000000000 --- a/lib/arel/relations/table.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Arel - class Table < Relation - include Recursion::BaseCase - - cattr_accessor :engine - attr_reader :name, :engine - hash_on :name - - def initialize(name, engine = Table.engine) - @name, @engine = name.to_s, engine - end - - def attributes - @attributes ||= columns.collect do |column| - Attribute.new(self, column.name.to_sym) - end - end - - def column_for(attribute) - has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s } - end - - def columns - @columns ||= engine.columns(name, "#{name} Columns") - end - - def reset - @attributes = @columns = nil - end - - def ==(other) - Table === other and - name == other.name - end - end -end diff --git a/lib/arel/relations/utilities.rb b/lib/arel/relations/utilities.rb index 454d455359..fbd949ef0c 100644 --- a/lib/arel/relations/utilities.rb +++ b/lib/arel/relations/utilities.rb @@ -1,5 +1,3 @@ require 'arel/relations/utilities/compound' -require 'arel/relations/utilities/recursion' require 'arel/relations/utilities/nil' require 'arel/relations/utilities/externalization' -require 'arel/relations/utilities/recursion' \ No newline at end of file diff --git a/lib/arel/relations/utilities/externalization.rb b/lib/arel/relations/utilities/externalization.rb index 3b9b2296dc..bd067f2304 100644 --- a/lib/arel/relations/utilities/externalization.rb +++ b/lib/arel/relations/utilities/externalization.rb @@ -2,24 +2,14 @@ module Arel class Externalization < Compound attributes :relation deriving :initialize, :== - include Recursion::BaseCase def wheres [] end - def table_sql(formatter = Sql::TableReference.new(relation)) - formatter.select relation.select_sql, self - end - def attributes @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) } end - - # REMOVEME - def name - relation.name + '_external' - end end class Relation diff --git a/lib/arel/relations/utilities/nil.rb b/lib/arel/relations/utilities/nil.rb index 56cf395d2c..6a9d678c45 100644 --- a/lib/arel/relations/utilities/nil.rb +++ b/lib/arel/relations/utilities/nil.rb @@ -3,8 +3,5 @@ require 'singleton' module Arel class Nil < Relation include Singleton - - def table_sql(formatter = nil); '' end - def name; '' end end end diff --git a/lib/arel/relations/utilities/recursion.rb b/lib/arel/relations/utilities/recursion.rb deleted file mode 100644 index 848b059507..0000000000 --- a/lib/arel/relations/utilities/recursion.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Arel - module Recursion - module BaseCase - def table - self - end - - def table_sql(formatter = Sql::TableReference.new(self)) - formatter.table self - end - end - end -end \ No newline at end of file diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index 0a04454e7f..a94067c7fa 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -3,15 +3,6 @@ module Arel attributes :relation deriving :initialize, :== - def to_sql(formatter = nil) - [ - "DELETE", - "FROM #{table_sql}", - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ].compact.join("\n") - end - def call engine.delete(self) end diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index d05bd9cdc8..6d85e9769a 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -7,15 +7,6 @@ module Arel @relation, @record = relation, record.bind(relation) end - def to_sql(formatter = nil) - [ - "INSERT", - "INTO #{table_sql}", - "(#{record.keys.map { |key| engine.quote_column_name(key.name) }.join(', ')})", - "VALUES (#{record.map { |key, value| key.format(value) }.join(', ')})" - ].join("\n") - end - def call engine.create(self) end diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index fd0aadb479..e647218a80 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -7,17 +7,6 @@ module Arel @relation, @assignments = relation, assignments end - def to_sql(formatter = nil) - [ - "UPDATE #{table_sql} SET", - assignments.collect do |attribute, value| - "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(",\n"), - ("WHERE #{wheres.map(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ) - ].join("\n") - end - def call engine.update(self) end diff --git a/lib/arel/sql.rb b/lib/arel/sql.rb deleted file mode 100644 index 7e7dd0a80f..0000000000 --- a/lib/arel/sql.rb +++ /dev/null @@ -1,2 +0,0 @@ -require 'arel/sql/formatters' -require 'arel/sql/christener' \ No newline at end of file diff --git a/lib/arel/sql/christener.rb b/lib/arel/sql/christener.rb deleted file mode 100644 index 5883a75f41..0000000000 --- a/lib/arel/sql/christener.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Arel - module Sql - class Christener - def name_for(relation) - @used_names ||= Hash.new(0) - (@relation_names ||= Hash.new do |hash, relation| - @used_names[name = relation.name] += 1 - hash[relation] = name + (@used_names[name] > 1 ? "_#{@used_names[name]}" : '') - end)[relation.table] - end - end - end -end \ No newline at end of file diff --git a/lib/arel/sql/formatters.rb b/lib/arel/sql/formatters.rb deleted file mode 100644 index f82ddf631f..0000000000 --- a/lib/arel/sql/formatters.rb +++ /dev/null @@ -1,121 +0,0 @@ -module Arel - module Sql - class Formatter - attr_reader :environment - delegate :christener, :engine, :to => :environment - delegate :name_for, :to => :christener - delegate :quote_table_name, :quote_column_name, :quote, :to => :engine - - def initialize(environment) - @environment = environment - end - end - - class SelectClause < Formatter - def attribute(attribute) - # FIXME this should check that the column exists - if attribute.name.to_s =~ /^\w*$/ - "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") - else - attribute.name.to_s + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") - end - end - - def expression(expression) - if expression.function_sql == "DISTINCT" - "#{expression.function_sql} #{expression.attribute.to_sql(self)}" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') - else - "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : " AS #{expression.function_sql.to_s.downcase}_id") - end - end - - def select(select_sql, table) - "(#{select_sql}) AS #{quote_table_name(name_for(table))}" - end - - def value(value) - value - end - end - - class PassThrough < Formatter - def value(value) - value - end - end - - class WhereClause < PassThrough - end - - class OrderClause < PassThrough - def attribute(attribute) - "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" - end - end - - class GroupClause < PassThrough - def attribute(attribute) - "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" - end - end - - class WhereCondition < Formatter - def attribute(attribute) - "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" - end - - def expression(expression) - "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" - end - - def value(value) - value.to_sql(self) - end - - def scalar(value, column = nil) - quote(value, column) - end - - def select(select_sql, table) - "(#{select_sql})" - end - end - - class SelectStatement < Formatter - def select(select_sql, table) - select_sql - end - end - - class TableReference < Formatter - def select(select_sql, table) - "(#{select_sql}) AS #{quote_table_name(name_for(table))}" - end - - def table(table) - if table.name =~ /^(\w|-)*$/ - quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') - else - table.name + (table.name != name_for(table) ? " AS " + (name_for(table)) : '') - end - end - end - - class Attribute < WhereCondition - def scalar(scalar) - quote(scalar, environment.column) - end - - def array(array) - "(" + array.collect { |e| e.to_sql(self) }.join(', ') + ")" - end - - def range(left, right) - "#{left} AND #{right}" - end - end - - class Value < WhereCondition - end - end -end diff --git a/spec/arel/unit/relations/skip_spec.rb b/spec/arel/unit/relations/skip_spec.rb index 0653d795b1..2c8f6ccadb 100644 --- a/spec/arel/unit/relations/skip_spec.rb +++ b/spec/arel/unit/relations/skip_spec.rb @@ -9,7 +9,7 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - sql = Skip.new(@relation, @skipped).to_s + sql = Skip.new(@relation, @skipped).to_sql adapter_is :mysql do sql.should be_like(%Q{ diff --git a/spec/arel/unit/relations/table_spec.rb b/spec/arel/unit/relations/table_spec.rb index 08486c7b6c..211e6921f8 100644 --- a/spec/arel/unit/relations/table_spec.rb +++ b/spec/arel/unit/relations/table_spec.rb @@ -87,12 +87,12 @@ module Arel describe '#engine' do it "defaults to global engine" do - Table.engine = engine = Engine.new + Table.engine = engine = Sql::Engine.new Table.new(:users).engine.should == engine end it "can be specified" do - Table.new(:users, engine = Engine.new).engine.should == engine + Table.new(:users, engine = Sql::Engine.new).engine.should == engine end end end diff --git a/spec/arel/unit/relations/take_spec.rb b/spec/arel/unit/relations/take_spec.rb index 911b84e01e..d6442fc9d1 100644 --- a/spec/arel/unit/relations/take_spec.rb +++ b/spec/arel/unit/relations/take_spec.rb @@ -9,7 +9,7 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - sql = Take.new(@relation, @taken).to_s + sql = Take.new(@relation, @taken).to_sql adapter_is :mysql do sql.should be_like(%Q{ diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8d515d66f1..6a9a2ef23c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,6 +30,6 @@ Spec::Runner.configure do |config| config.include AdapterGuards config.mock_with :rr config.before do - Arel::Table.engine = Arel::Engine.new(ActiveRecord::Base) + Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base) end end -- cgit v1.2.3