diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-04-23 18:53:03 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-04-23 18:53:03 -0300 |
commit | a454d45403cd0b8a24b05b7ff37021e307905825 (patch) | |
tree | 1d5b989e1bd32f47f00f7e72d3bfe97cf085ac0e /lib | |
parent | 318cf575eb2b7cf42cb133b3f24cd1aa5fa5e155 (diff) | |
download | rails-a454d45403cd0b8a24b05b7ff37021e307905825.tar.gz rails-a454d45403cd0b8a24b05b7ff37021e307905825.tar.bz2 rails-a454d45403cd0b8a24b05b7ff37021e307905825.zip |
Fix insertion to work on SQLite3
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/extensions/object.rb | 12 | ||||
-rw-r--r-- | lib/arel/predicates.rb | 16 | ||||
-rw-r--r-- | lib/arel/primitives/attribute.rb | 42 | ||||
-rw-r--r-- | lib/arel/primitives/expression.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/operations/where.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 40 | ||||
-rw-r--r-- | lib/arel/relations/writes/insert.rb | 10 | ||||
-rw-r--r-- | lib/arel/sql/formatters.rb | 46 |
8 files changed, 93 insertions, 89 deletions
diff --git a/lib/arel/extensions/object.rb b/lib/arel/extensions/object.rb index ea73c336dc..14e2f82ce5 100644 --- a/lib/arel/extensions/object.rb +++ b/lib/arel/extensions/object.rb @@ -1,23 +1,23 @@ -class Object +class Object def bind(relation) Arel::Value.new(self, relation) end - + def find_correlate_in(relation) bind(relation) end - + def to_sql(formatter) formatter.scalar self end - + def equality_predicate_sql '=' end - + def metaclass class << self self end end -end
\ No newline at end of file +end diff --git a/lib/arel/predicates.rb b/lib/arel/predicates.rb index 051f8abdad..b639022b4e 100644 --- a/lib/arel/predicates.rb +++ b/lib/arel/predicates.rb @@ -3,7 +3,7 @@ module Arel def or(other_predicate) Or.new(self, other_predicate) end - + def and(other_predicate) And.new(self, other_predicate) end @@ -18,27 +18,27 @@ module Arel @operand1 == other.operand1 and @operand2 == other.operand2 end - + 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 def to_sql(formatter = nil) "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})" end end - + class Or < CompoundPredicate def predicate_sql; "OR" end end - + class And < CompoundPredicate def predicate_sql; "AND" end end @@ -74,8 +74,8 @@ module Arel 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 +end diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 30885dd129..7021e9f9ed 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -9,11 +9,11 @@ module Arel def initialize(relation, name, options = {}) @relation, @name, @alias, @ancestor = relation, name, options[:alias], options[:ancestor] end - + def named?(hypothetical_name) (@alias || name).to_s == hypothetical_name.to_s end - + def aggregation? false end @@ -21,7 +21,7 @@ module Arel def column original_relation.column_for(self) end - + def format(object) object.to_sql(Sql::Attribute.new(self)) end @@ -30,19 +30,19 @@ module Arel formatter.attribute self end - module Transformations + module Transformations def self.included(klass) klass.send :alias_method, :eql?, :== end - + def hash @hash ||= history.size + name.hash + relation.hash end - + def as(aliaz = nil) Attribute.new(relation, name, :alias => aliaz, :ancestor => self) end - + def bind(new_relation) relation == new_relation ? self : Attribute.new(new_relation, name, :alias => @alias, :ancestor => self) end @@ -52,20 +52,20 @@ module Arel end end include Transformations - + module Congruence def history @history ||= [self] + (ancestor ? ancestor.history : []) end - + def join? relation.join? end - + def root history.last end - + def original_relation @original_relation ||= original_attribute.relation end @@ -77,17 +77,17 @@ module Arel def find_correlate_in(relation) relation[self] || self end - + def descends_from?(other) history.include?(other) end - + def /(other) other ? (history & other.history).size : 0 end end include Congruence - + module Predications def eq(other) Equality.new(self, other) @@ -112,34 +112,34 @@ module Arel def matches(regexp) Match.new(self, regexp) end - + def in(array) In.new(self, array) end end include Predications - + module Expressions def count Expression.new(self, "COUNT") end - + def sum Expression.new(self, "SUM") end - + def maximum Expression.new(self, "MAX") end - + def minimum Expression.new(self, "MIN") end - + def average Expression.new(self, "AVG") end end include Expressions end -end
\ No newline at end of file +end diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb index 26186ccad8..836f014745 100644 --- a/lib/arel/primitives/expression.rb +++ b/lib/arel/primitives/expression.rb @@ -4,32 +4,32 @@ module Arel deriving :== delegate :relation, :to => :attribute alias_method :name, :alias - + def initialize(attribute, function_sql, aliaz = nil, ancestor = nil) @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 - + module Transformations def as(aliaz) Expression.new(attribute, function_sql, aliaz, self) end - + def bind(new_relation) new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) end - + def to_attribute Attribute.new(relation, @alias, :ancestor => self) end end include Transformations end -end
\ No newline at end of file +end diff --git a/lib/arel/relations/operations/where.rb b/lib/arel/relations/operations/where.rb index 8882f36104..608aaeb4b7 100644 --- a/lib/arel/relations/operations/where.rb +++ b/lib/arel/relations/operations/where.rb @@ -13,4 +13,4 @@ module Arel @wheres ||= (relation.wheres + [predicate]).collect { |p| p.bind(self) } end end -end
\ No newline at end of file +end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index a2d8bba6ea..d9ba9a108b 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -3,12 +3,16 @@ module Arel def session Session.new end - + + def select_values + engine.select_values self.to_sql + 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 #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", @@ -21,12 +25,12 @@ module Arel ("OFFSET #{skipped}" unless skipped.blank? ) ].compact.join("\n") end - + def inclusion_predicate_sql "IN" end - - def call(connection = engine.connection) + + def call(connection = engine) results = connection.execute(to_sql) rows = [] results.each do |row| @@ -34,19 +38,19 @@ module Arel end rows end - + def bind(relation) self end - + def root self end - + def christener @christener ||= Sql::Christener.new end - + module Enumerable include ::Enumerable @@ -75,7 +79,7 @@ module Arel def outer_join(other_relation = nil) join(other_relation, "LEFT OUTER JOIN") end - + [:where, :project, :order, :take, :skip, :group].each do |operation_name| operation = <<-OPERATION def #{operation_name}(*arguments, &block) @@ -88,7 +92,7 @@ module Arel def alias Alias.new(self) end - + module Writable def insert(record) session.create Insert.new(self, record); self @@ -103,7 +107,7 @@ module Arel end end include Writable - + JoinOperation = Struct.new(:join_sql, :relation1, :relation2) do def on(*predicates) Join.new(join_sql, relation1, relation2, *predicates) @@ -111,7 +115,7 @@ module Arel end end include Operable - + module AttributeAccessable def [](index) case index @@ -123,17 +127,17 @@ module Arel index.collect { |i| self[i] } end end - + def find_attribute_matching_name(name) attributes.detect { |a| a.named?(name) } end - + def find_attribute_matching_attribute(attribute) matching_attributes(attribute).max do |a1, a2| (a1.original_attribute / attribute) <=> (a2.original_attribute / attribute) end end - + private def matching_attributes(attribute) (@matching_attributes ||= attributes.inject({}) do |hash, a| @@ -141,7 +145,7 @@ module Arel hash end)[attribute.root] || [] end - + def has_attribute?(attribute) !matching_attributes(attribute).empty? end @@ -160,4 +164,4 @@ module Arel end include DefaultOperations end -end
\ No newline at end of file +end diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index 0545d7faa3..d579ad06d0 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -11,13 +11,13 @@ module Arel [ "INSERT", "INTO #{table_sql}", - "(#{record.keys.collect(&:to_sql).join(', ')})", - "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + "(#{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(connection = engine.connection) + + def call(connection = engine) connection.insert(to_sql) end end -end
\ No newline at end of file +end diff --git a/lib/arel/sql/formatters.rb b/lib/arel/sql/formatters.rb index 068fb8d22d..22a116117e 100644 --- a/lib/arel/sql/formatters.rb +++ b/lib/arel/sql/formatters.rb @@ -5,104 +5,104 @@ module Arel 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) "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") end - + def expression(expression) "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') 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 + + 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) quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') 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
\ No newline at end of file +end |