Permalink
Browse files

reorganized file structures

Conflicts:

	lib/arel.rb
	lib/arel/arel.rb
	lib/arel/engines/memory/predicates.rb
	lib/arel/engines/memory/relations/array.rb
	lib/arel/engines/sql/relations/table.rb
  • Loading branch information...
1 parent bdca9ed commit 7032a50297fce4d7724d1735e81e5df5fd919e71 @brynary brynary committed May 17, 2009
Showing with 160 additions and 77 deletions.
  1. +3 −3 README.markdown
  2. +1 −5 lib/arel.rb
  3. +4 −0 lib/arel/algebra.rb
  4. +4 −0 lib/arel/algebra/extensions.rb
  5. 0 lib/arel/{ → algebra}/extensions/array.rb
  6. 0 lib/arel/{ → algebra}/extensions/class.rb
  7. 0 lib/arel/{ → algebra}/extensions/hash.rb
  8. 0 lib/arel/{ → algebra}/extensions/object.rb
  9. +5 −0 lib/arel/algebra/extensions/pathname.rb
  10. +45 −0 lib/arel/algebra/predicates.rb
  11. +4 −0 lib/arel/algebra/primitives.rb
  12. 0 lib/arel/{ → algebra}/primitives/attribute.rb
  13. 0 lib/arel/{ → algebra}/primitives/expression.rb
  14. 0 lib/arel/{ → algebra}/primitives/value.rb
  15. +15 −0 lib/arel/algebra/relations.rb
  16. 0 lib/arel/{ → algebra}/relations/operations/alias.rb
  17. 0 lib/arel/{ → algebra}/relations/operations/group.rb
  18. 0 lib/arel/{ → algebra}/relations/operations/join.rb
  19. 0 lib/arel/{ → algebra}/relations/operations/order.rb
  20. 0 lib/arel/{ → algebra}/relations/operations/project.rb
  21. 0 lib/arel/{ → algebra}/relations/operations/skip.rb
  22. 0 lib/arel/{ → algebra}/relations/operations/take.rb
  23. 0 lib/arel/{ → algebra}/relations/operations/where.rb
  24. 0 lib/arel/{ → algebra}/relations/relation.rb
  25. +1 −1 lib/arel/{ → algebra}/relations/utilities/compound.rb
  26. 0 lib/arel/{ → algebra}/relations/utilities/externalization.rb
  27. 0 lib/arel/{ → algebra}/relations/utilities/nil.rb
  28. 0 lib/arel/{ → algebra}/relations/writes/delete.rb
  29. 0 lib/arel/{ → algebra}/relations/writes/insert.rb
  30. 0 lib/arel/{ → algebra}/relations/writes/update.rb
  31. +0 −3 lib/arel/arel.rb
  32. +2 −2 lib/arel/engines.rb
  33. +0 −1 lib/arel/engines/array/array.rb
  34. +4 −0 lib/arel/engines/memory.rb
  35. +12 −0 lib/arel/engines/memory/engine.rb
  36. +3 −11 lib/arel/{ → engines/memory}/predicates.rb
  37. +2 −0 lib/arel/engines/memory/primitives.rb
  38. +7 −0 lib/arel/engines/memory/primitives/attribute.rb
  39. +7 −0 lib/arel/engines/memory/primitives/value.rb
  40. +2 −0 lib/arel/engines/memory/relations.rb
  41. +6 −4 lib/arel/engines/{array → memory}/relations/array.rb
  42. +7 −0 lib/arel/engines/memory/relations/operations/where.rb
  43. 0 lib/arel/engines/{sql → }/sql.rb
  44. +0 −2 lib/arel/engines/sql/primitives/attribute.rb
  45. +9 −4 lib/arel/engines/sql/relations.rb
  46. +0 −2 lib/arel/engines/sql/relations/operations.rb
  47. +5 −0 lib/arel/engines/sql/relations/table.rb
  48. +0 −3 lib/arel/engines/sql/relations/utilities.rb
  49. +0 −3 lib/arel/engines/sql/relations/writes.rb
  50. +0 −4 lib/arel/extensions.rb
  51. +0 −4 lib/arel/primitives.rb
  52. +0 −4 lib/arel/relations.rb
  53. +0 −8 lib/arel/relations/operations.rb
  54. +0 −3 lib/arel/relations/utilities.rb
  55. +0 −3 lib/arel/relations/writes.rb
  56. +0 −2 lib/arel/session.rb
  57. +1 −1 spec/arel/integration/joins/with_adjacency_spec.rb
  58. +2 −2 spec/arel/integration/joins/with_aggregations_spec.rb
  59. +2 −2 spec/arel/integration/joins/with_compounds_spec.rb
  60. +7 −0 spec/arel/unit/relations/array_spec.rb
View
@@ -18,7 +18,7 @@ Generating a query with ARel is simple. For example, in order to produce
you construct a table relation and convert it to sql:
- users = Arel(:users)
+ users = Table(:users)
users.to_sql
In fact, you will probably never call `#to_sql`. Rather, you'll work with data from the table directly. You can iterate through all rows in the `users` table like this:
@@ -81,7 +81,7 @@ The `AND` operator will behave similarly.
Finally, most operations take a block form. For example:
- Arel(:users) \
+ Table(:users) \
.where { |u| u[:id].eq(1) } \
.project { |u| u[:id] }
@@ -95,7 +95,7 @@ The examples above are fairly simple and other libraries match or come close to
Where Arel really shines in its ability to handle complex joins and aggregations. As a first example, let's consider an "adjacency list", a tree represented in a table. Suppose we have a table `comments`, representing a threaded discussion:
- comments = Arel(:comments)
+ comments = Table(:comments)
And this table has the following attributes:
View
@@ -5,10 +5,6 @@
require 'activerecord'
require 'active_record/connection_adapters/abstract/quoting'
-require 'arel/arel'
-require 'arel/extensions'
-require 'arel/predicates'
-require 'arel/relations'
+require 'arel/algebra'
require 'arel/engines'
require 'arel/session'
-require 'arel/primitives'
View
@@ -0,0 +1,4 @@
+require 'arel/algebra/extensions'
+require 'arel/algebra/predicates'
+require 'arel/algebra/relations'
+require 'arel/algebra/primitives'
@@ -0,0 +1,4 @@
+require 'arel/algebra/extensions/object'
+require 'arel/algebra/extensions/class'
+require 'arel/algebra/extensions/array'
+require 'arel/algebra/extensions/hash'
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,5 @@
+class Pathname
+ def /(path)
+ (self + path).expand_path
+ end
+end
@@ -0,0 +1,45 @@
+module Arel
+ class Predicate
+ end
+
+ class Binary < Predicate
+ attributes :operand1, :operand2
+ deriving :initialize
+
+ def ==(other)
+ self.class === other and
+ @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
+ 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
+ end
+
+ class GreaterThan < Binary
+ end
+
+ class LessThanOrEqualTo < Binary
+ end
+
+ class LessThan < Binary
+ end
+
+ class Match < Binary
+ end
+
+ class In < Binary
+ end
+end
@@ -0,0 +1,4 @@
+require 'arel/algebra/primitives/attribute'
+require 'arel/algebra/primitives/value'
+require 'arel/algebra/primitives/expression'
+
File renamed without changes.
@@ -0,0 +1,15 @@
+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/operations/alias'
+require 'arel/algebra/relations/operations/group'
+require 'arel/algebra/relations/operations/join'
+require 'arel/algebra/relations/operations/order'
+require 'arel/algebra/relations/operations/project'
+require 'arel/algebra/relations/operations/where'
+require 'arel/algebra/relations/operations/skip'
+require 'arel/algebra/relations/operations/take'
@@ -3,7 +3,7 @@ class Compound < Relation
attr_reader :relation
hash_on :relation
delegate :joins, :join?, :inserts, :taken, :skipped, :name, :externalizable?,
- :column_for, :engine, :table, :table_sql,
+ :column_for, :engine, :table, :table_sql, :array,
:to => :relation
[:attributes, :wheres, :groupings, :orders].each do |operation_name|
View
@@ -1,3 +0,0 @@
-def Arel(name, engine = (Arel::Table.engine || ActiveRecord::Base.connection))
- Arel::Table.new(name, engine)
-end
View
@@ -1,2 +1,2 @@
-require 'arel/engines/sql/sql'
-require 'arel/engines/array/array'
+require 'arel/engines/sql'
+require 'arel/engines/memory'
@@ -1 +0,0 @@
-require 'arel/engines/array/relations/array'
@@ -0,0 +1,4 @@
+require 'arel/engines/memory/relations'
+require 'arel/engines/memory/primitives'
+require 'arel/engines/memory/engine'
+require 'arel/engines/memory/predicates'
@@ -0,0 +1,12 @@
+module Arel
+ module Memory
+ class Engine
+ module CRUD
+ def read(relation)
+ relation.eval
+ end
+ end
+ include CRUD
+ end
+ end
+end
@@ -10,17 +10,8 @@ def and(other_predicate)
end
class Binary < Predicate
- attributes :operand1, :operand2
- deriving :initialize
-
- def ==(other)
- self.class === other and
- @operand1 == other.operand1 and
- @operand2 == other.operand2
- end
-
- def bind(relation)
- self.class.new(operand1.find_correlate_in(relation), operand2.find_correlate_in(relation))
+ def eval(row)
+ operand1.eval(row).send(operator, operand2.eval(row))
end
end
@@ -56,6 +47,7 @@ class LessThanOrEqualTo < Binary
end
class LessThan < Binary
+ def operator; :< end
end
class Match < Binary
@@ -0,0 +1,2 @@
+require 'arel/engines/memory/primitives/attribute'
+require 'arel/engines/memory/primitives/value'
@@ -0,0 +1,7 @@
+module Arel
+ class Attribute
+ def eval(row)
+ row[self]
+ end
+ end
+end
@@ -0,0 +1,7 @@
+module Arel
+ class Value
+ def eval(row)
+ value
+ end
+ end
+end
@@ -0,0 +1,2 @@
+require 'arel/engines/memory/relations/array'
+require 'arel/engines/memory/relations/operations/where'
@@ -1,9 +1,11 @@
module Arel
class Array < Relation
+ attributes :array, :attribute_names
+ deriving :initialize
include Recursion::BaseCase
-
- def initialize(array, attribute_names)
- @array, @attribute_names = array, attribute_names
+
+ def engine
+ @engine ||= Memory::Engine.new
end
def attributes
@@ -12,7 +14,7 @@ def attributes
end
end
- def call(connection = nil)
+ def eval
@array.collect { |row| attributes.zip(row).to_hash }
end
end
@@ -0,0 +1,7 @@
+module Arel
+ class Where < Compound
+ def eval
+ relation.eval.select { |row| predicate.eval(row) }
+ end
+ end
+end
File renamed without changes.
@@ -1,5 +1,3 @@
-require 'set'
-
module Arel
class Attribute
def column
@@ -1,5 +1,10 @@
-require 'arel/engines/sql/relations/utilities'
+require 'arel/engines/sql/relations/utilities/recursion'
+require 'arel/engines/sql/relations/utilities/externalization'
+require 'arel/engines/sql/relations/utilities/nil'
require 'arel/engines/sql/relations/relation'
-require 'arel/engines/sql/relations/operations'
-require 'arel/engines/sql/relations/writes'
-require 'arel/engines/sql/relations/table'
+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'
@@ -1,2 +0,0 @@
-require 'arel/engines/sql/relations/operations/alias'
-require 'arel/engines/sql/relations/operations/join'
@@ -34,3 +34,8 @@ def ==(other)
end
end
end
+
+def Table(name, engine = Arel::Table.engine)
+ Arel::Table.new(name, engine)
+end
+
@@ -1,3 +0,0 @@
-require 'arel/engines/sql/relations/utilities/recursion'
-require 'arel/engines/sql/relations/utilities/externalization'
-require 'arel/engines/sql/relations/utilities/nil'
@@ -1,3 +0,0 @@
-require 'arel/engines/sql/relations/writes/delete'
-require 'arel/engines/sql/relations/writes/insert'
-require 'arel/engines/sql/relations/writes/update'
View
@@ -1,4 +0,0 @@
-require 'arel/extensions/object'
-require 'arel/extensions/class'
-require 'arel/extensions/array'
-require 'arel/extensions/hash'
View
@@ -1,4 +0,0 @@
-require 'arel/primitives/attribute'
-require 'arel/primitives/value'
-require 'arel/primitives/expression'
-
View
@@ -1,4 +0,0 @@
-require 'arel/relations/relation'
-require 'arel/relations/utilities'
-require 'arel/relations/writes'
-require 'arel/relations/operations'
@@ -1,8 +0,0 @@
-require 'arel/relations/operations/alias'
-require 'arel/relations/operations/group'
-require 'arel/relations/operations/join'
-require 'arel/relations/operations/order'
-require 'arel/relations/operations/project'
-require 'arel/relations/operations/where'
-require 'arel/relations/operations/skip'
-require 'arel/relations/operations/take'
@@ -1,3 +0,0 @@
-require 'arel/relations/utilities/compound'
-require 'arel/relations/utilities/nil'
-require 'arel/relations/utilities/externalization'
@@ -1,3 +0,0 @@
-require 'arel/relations/writes/delete'
-require 'arel/relations/writes/update'
-require 'arel/relations/writes/insert'
View
@@ -1,5 +1,3 @@
-require 'singleton'
-
module Arel
class Session
class << self
@@ -3,7 +3,7 @@
module Arel
describe Join do
before do
- @relation1 = Arel(:users)
+ @relation1 = Table(:users)
@relation2 = @relation1.alias
@predicate = @relation1[:id].eq(@relation2[:id])
end
@@ -3,8 +3,8 @@
module Arel
describe Join do
before do
- @relation1 = Arel(:users)
- @relation2 = Arel(:photos)
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
@predicate = @relation1[:id].eq(@relation2[:user_id])
end
@@ -3,8 +3,8 @@
module Arel
describe Join do
before do
- @relation1 = Arel(:users)
- @relation2 = Arel(:photos)
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
@predicate = @relation1[:id].eq(@relation2[:user_id])
end
@@ -22,6 +22,13 @@ module Arel
{ @relation[:id] => 3 }
]
end
+
+ it '' do
+ @relation.where(@relation[:id].lt(3)).call.should == [
+ { @relation[:id] => 1 },
+ { @relation[:id] => 2 }
+ ]
+ end
end
end
end

0 comments on commit 7032a50

Please sign in to comment.