forked from brynary/arel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
56 changed files
with
279 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'arel/engines/sql/sql' | ||
require 'arel/engines/array/array' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'arel/engines/array/relations/array' |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Object | ||
def to_sql(formatter) | ||
formatter.scalar self | ||
end | ||
|
||
def equality_predicate_sql | ||
'=' | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'arel/engines/sql/primitives/attribute' | ||
require 'arel/engines/sql/primitives/value' | ||
require 'arel/engines/sql/primitives/expression' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Arel | ||
class Expression < Attribute | ||
def to_sql(formatter = Sql::SelectClause.new(relation)) | ||
formatter.expression self | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'arel/engines/sql/relations/operations/alias' | ||
require 'arel/engines/sql/relations/operations/join' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Arel | ||
class Alias < Compound | ||
include Recursion::BaseCase | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
14 changes: 14 additions & 0 deletions
14
lib/arel/engines/sql/relations/utilities/externalization.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Arel | ||
class Nil < Relation | ||
def table_sql(formatter = nil); '' end | ||
def name; '' end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.