1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

consolidated files

Conflicts:

	lib/arel/algebra/predicates.rb
	lib/arel/algebra/relations/writes/delete.rb
	lib/arel/algebra/relations/writes/insert.rb
	lib/arel/algebra/relations/writes/update.rb
	lib/arel/engines/memory/predicates.rb
	lib/arel/engines/memory/relations.rb
	lib/arel/engines/sql/primitives/attribute.rb
	lib/arel/engines/sql/relations/writes/insert.rb
	lib/arel/engines/sql/relations/writes/update.rb
This commit is contained in:
Bryan Helmkamp 2009-05-17 14:38:09 -04:00
parent 7032a50297
commit b0a45d52fd
22 changed files with 135 additions and 168 deletions

View file

@ -1,5 +1,12 @@
module Arel
class Predicate
def or(other_predicate)
Or.new(self, other_predicate)
end
def and(other_predicate)
And.new(self, other_predicate)
end
end
class Binary < Predicate
@ -25,21 +32,10 @@ module Arel
end
end
class GreaterThanOrEqualTo < Binary
end
class GreaterThan < Binary
end
class LessThanOrEqualTo < Binary
end
class LessThan < Binary
end
class Match < Binary
end
class In < Binary
end
class GreaterThanOrEqualTo < Binary; end
class GreaterThan < Binary; end
class LessThanOrEqualTo < Binary; end
class LessThan < Binary; end
class Match < Binary; end
class In < Binary; end
end

View file

@ -2,9 +2,7 @@ require 'arel/algebra/relations/relation'
require 'arel/algebra/relations/utilities/compound'
require 'arel/algebra/relations/utilities/nil'
require 'arel/algebra/relations/utilities/externalization'
require 'arel/algebra/relations/writes/delete'
require 'arel/algebra/relations/writes/update'
require 'arel/algebra/relations/writes/insert'
require 'arel/algebra/relations/writes'
require 'arel/algebra/relations/operations/alias'
require 'arel/algebra/relations/operations/group'
require 'arel/algebra/relations/operations/join'

View file

@ -0,0 +1,36 @@
module Arel
class Deletion < Compound
attributes :relation
deriving :initialize, :==
def call
engine.delete(self)
end
end
class Insert < Compound
attributes :relation, :record
deriving :==
def initialize(relation, record)
@relation, @record = relation, record.bind(relation)
end
def call
engine.create(self)
end
end
class Update < Compound
attributes :relation, :assignments
deriving :==
def initialize(relation, assignments)
@relation, @assignments = relation, assignments.bind(relation)
end
def call
engine.update(self)
end
end
end

View file

@ -1,10 +0,0 @@
module Arel
class Deletion < Compound
attributes :relation
deriving :initialize, :==
def call
engine.delete(self)
end
end
end

View file

@ -1,14 +0,0 @@
module Arel
class Insert < Compound
attributes :relation, :record
deriving :==
def initialize(relation, record)
@relation, @record = relation, record.bind(relation)
end
def call
engine.create(self)
end
end
end

View file

@ -1,14 +0,0 @@
module Arel
class Update < Compound
attributes :relation, :assignments
deriving :==
def initialize(relation, assignments)
@relation, @assignments = relation, assignments
end
def call
engine.update(self)
end
end
end

View file

@ -1,13 +1,4 @@
module Arel
class Predicate
def or(other_predicate)
Or.new(self, other_predicate)
end
def and(other_predicate)
And.new(self, other_predicate)
end
end
class Binary < Predicate
def eval(row)
@ -30,11 +21,6 @@ module Arel
end
class Equality < Binary
def ==(other)
Equality === other and
((operand1 == other.operand1 and operand2 == other.operand2) or
(operand1 == other.operand2 and operand2 == other.operand1))
end
end
class GreaterThanOrEqualTo < Binary

View file

@ -1,2 +1,13 @@
require 'arel/engines/memory/primitives/attribute'
require 'arel/engines/memory/primitives/value'
module Arel
class Attribute
def eval(row)
row[self]
end
end
class Value
def eval(row)
value
end
end
end

View file

@ -1,7 +0,0 @@
module Arel
class Attribute
def eval(row)
row[self]
end
end
end

View file

@ -1,7 +0,0 @@
module Arel
class Value
def eval(row)
value
end
end
end

View file

@ -1,2 +1,3 @@
require 'arel/engines/memory/relations/array'
require 'arel/engines/memory/relations/operations/where'
require 'arel/engines/memory/relations/operations'

View file

@ -1,3 +1,31 @@
require 'arel/engines/sql/primitives/attribute'
require 'arel/engines/sql/primitives/value'
require 'arel/engines/sql/primitives/expression'
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
class Expression < Attribute
def to_sql(formatter = Sql::SelectClause.new(relation))
formatter.expression self
end
end
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

View file

@ -1,15 +0,0 @@
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

View file

@ -1,7 +0,0 @@
module Arel
class Expression < Attribute
def to_sql(formatter = Sql::SelectClause.new(relation))
formatter.expression self
end
end
end

View file

@ -1,11 +0,0 @@
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

View file

@ -5,6 +5,4 @@ require 'arel/engines/sql/relations/relation'
require 'arel/engines/sql/relations/table'
require 'arel/engines/sql/relations/operations/join'
require 'arel/engines/sql/relations/operations/alias'
require 'arel/engines/sql/relations/writes/delete'
require 'arel/engines/sql/relations/writes/insert'
require 'arel/engines/sql/relations/writes/update'
require 'arel/engines/sql/relations/writes'

View file

@ -0,0 +1,36 @@
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
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
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

View file

@ -1,12 +0,0 @@
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

View file

@ -1,12 +0,0 @@
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

View file

@ -1,14 +0,0 @@
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

View file

@ -73,14 +73,14 @@ module Arel
describe '#reset' do
it "reloads columns from the database" do
lambda { stub(@relation.engine).columns { [] } }.should_not change { @relation.attributes }
lambda { @relation.reset }.should change { @relation.attributes }
lambda { @relation.reset }.should change { @relation.attributes }
end
end
end
describe 'hashing' do
it "implements hash equality" do
Table.new(:users).should hash_the_same_as(Table.new(:users))
Table.new(:users).should hash_the_same_as(Table.new(:users))
Table.new(:users).should_not hash_the_same_as(Table.new(:photos))
end
end