Permalink
Browse files

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...
brynary committed May 17, 2009
1 parent 2bbf8ca commit bdca9ed42ffea10aa6989ea3ecebedb424fa01ed
Showing with 279 additions and 208 deletions.
  1. +1 −1 doc/TODO
  2. +1 −2 lib/arel.rb
  3. +0 −42 lib/arel/engine.rb
  4. +2 −0 lib/arel/engines.rb
  5. +1 −0 lib/arel/engines/array/array.rb
  6. 0 lib/arel/{ → engines/array}/relations/array.rb
  7. 0 lib/arel/{ → engines}/sql/christener.rb
  8. +41 −0 lib/arel/engines/sql/engine.rb
  9. +4 −0 lib/arel/engines/sql/extensions.rb
  10. +9 −0 lib/arel/engines/sql/extensions/array.rb
  11. 0 lib/arel/{ → engines/sql}/extensions/nil_class.rb
  12. +9 −0 lib/arel/engines/sql/extensions/object.rb
  13. 0 lib/arel/{ → engines/sql}/extensions/range.rb
  14. 0 lib/arel/{ → engines}/sql/formatters.rb
  15. +37 −0 lib/arel/engines/sql/predicates.rb
  16. +3 −0 lib/arel/engines/sql/primitives.rb
  17. +17 −0 lib/arel/engines/sql/primitives/attribute.rb
  18. +7 −0 lib/arel/engines/sql/primitives/expression.rb
  19. +11 −0 lib/arel/engines/sql/primitives/value.rb
  20. +5 −0 lib/arel/engines/sql/relations.rb
  21. +2 −0 lib/arel/engines/sql/relations/operations.rb
  22. +5 −0 lib/arel/engines/sql/relations/operations/alias.rb
  23. +19 −0 lib/arel/engines/sql/relations/operations/join.rb
  24. +28 −0 lib/arel/engines/sql/relations/relation.rb
  25. 0 lib/arel/{ → engines/sql}/relations/table.rb
  26. +3 −0 lib/arel/engines/sql/relations/utilities.rb
  27. +14 −0 lib/arel/engines/sql/relations/utilities/externalization.rb
  28. +6 −0 lib/arel/engines/sql/relations/utilities/nil.rb
  29. 0 lib/arel/{ → engines/sql}/relations/utilities/recursion.rb
  30. +3 −0 lib/arel/engines/sql/relations/writes.rb
  31. +12 −0 lib/arel/engines/sql/relations/writes/delete.rb
  32. +12 −0 lib/arel/engines/sql/relations/writes/insert.rb
  33. +14 −0 lib/arel/engines/sql/relations/writes/update.rb
  34. +7 −0 lib/arel/engines/sql/sql.rb
  35. +0 −2 lib/arel/extensions.rb
  36. +0 −8 lib/arel/extensions/array.rb
  37. +0 −8 lib/arel/extensions/object.rb
  38. +0 −15 lib/arel/predicates.rb
  39. +0 −12 lib/arel/primitives/attribute.rb
  40. +0 −4 lib/arel/primitives/expression.rb
  41. +0 −13 lib/arel/primitives/value.rb
  42. +0 −2 lib/arel/relations.rb
  43. +0 −1 lib/arel/relations/operations/alias.rb
  44. +0 −16 lib/arel/relations/operations/join.rb
  45. +1 −31 lib/arel/relations/relation.rb
  46. +0 −2 lib/arel/relations/utilities.rb
  47. +0 −10 lib/arel/relations/utilities/externalization.rb
  48. +0 −3 lib/arel/relations/utilities/nil.rb
  49. +0 −9 lib/arel/relations/writes/delete.rb
  50. +0 −9 lib/arel/relations/writes/insert.rb
  51. +0 −11 lib/arel/relations/writes/update.rb
  52. +0 −2 lib/arel/sql.rb
  53. +1 −1 spec/arel/unit/relations/skip_spec.rb
  54. +2 −2 spec/arel/unit/relations/table_spec.rb
  55. +1 −1 spec/arel/unit/relations/take_spec.rb
  56. +1 −1 spec/spec_helper.rb
View
@@ -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
View
@@ -7,9 +7,8 @@
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'
View
@@ -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
View
@@ -0,0 +1,2 @@
+require 'arel/engines/sql/sql'
+require 'arel/engines/array/array'
@@ -0,0 +1 @@
+require 'arel/engines/array/relations/array'
File renamed without changes.
@@ -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
@@ -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'
@@ -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
@@ -0,0 +1,9 @@
+class Object
+ def to_sql(formatter)
+ formatter.scalar self
+ end
+
+ def equality_predicate_sql
+ '='
+ end
+end
File renamed without changes.
@@ -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
@@ -0,0 +1,3 @@
+require 'arel/engines/sql/primitives/attribute'
+require 'arel/engines/sql/primitives/value'
+require 'arel/engines/sql/primitives/expression'
@@ -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
@@ -0,0 +1,7 @@
+module Arel
+ class Expression < Attribute
+ def to_sql(formatter = Sql::SelectClause.new(relation))
+ formatter.expression self
+ end
+ end
+end
@@ -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
@@ -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'
@@ -0,0 +1,2 @@
+require 'arel/engines/sql/relations/operations/alias'
+require 'arel/engines/sql/relations/operations/join'
@@ -0,0 +1,5 @@
+module Arel
+ class Alias < Compound
+ include Recursion::BaseCase
+ end
+end
@@ -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
@@ -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
@@ -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'
@@ -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
@@ -0,0 +1,6 @@
+module Arel
+ class Nil < Relation
+ def table_sql(formatter = nil); '' end
+ def name; '' end
+ end
+end
@@ -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'
@@ -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
@@ -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
@@ -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
@@ -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'
View
@@ -2,5 +2,3 @@
require 'arel/extensions/class'
require 'arel/extensions/array'
require 'arel/extensions/hash'
-require 'arel/extensions/range'
-require 'arel/extensions/nil_class'
@@ -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
Oops, something went wrong.

0 comments on commit bdca9ed

Please sign in to comment.