square-arel 2.0.9.20110222133018

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/.autotest +26 -0
  2. data/History.txt +105 -0
  3. data/MIT-LICENSE.txt +20 -0
  4. data/Manifest.txt +124 -0
  5. data/README.markdown +94 -0
  6. data/Rakefile +20 -0
  7. data/lib/arel.rb +39 -0
  8. data/lib/arel/attributes.rb +20 -0
  9. data/lib/arel/attributes/attribute.rb +18 -0
  10. data/lib/arel/compatibility/wheres.rb +33 -0
  11. data/lib/arel/crud.rb +37 -0
  12. data/lib/arel/delete_manager.rb +18 -0
  13. data/lib/arel/deprecated.rb +4 -0
  14. data/lib/arel/expression.rb +4 -0
  15. data/lib/arel/expressions.rb +23 -0
  16. data/lib/arel/insert_manager.rb +34 -0
  17. data/lib/arel/nodes.rb +53 -0
  18. data/lib/arel/nodes/and.rb +6 -0
  19. data/lib/arel/nodes/as.rb +6 -0
  20. data/lib/arel/nodes/assignment.rb +6 -0
  21. data/lib/arel/nodes/avg.rb +6 -0
  22. data/lib/arel/nodes/between.rb +6 -0
  23. data/lib/arel/nodes/binary.rb +12 -0
  24. data/lib/arel/nodes/count.rb +13 -0
  25. data/lib/arel/nodes/delete_statement.rb +19 -0
  26. data/lib/arel/nodes/does_not_match.rb +6 -0
  27. data/lib/arel/nodes/equality.rb +9 -0
  28. data/lib/arel/nodes/except.rb +7 -0
  29. data/lib/arel/nodes/exists.rb +7 -0
  30. data/lib/arel/nodes/function.rb +18 -0
  31. data/lib/arel/nodes/greater_than.rb +6 -0
  32. data/lib/arel/nodes/greater_than_or_equal.rb +6 -0
  33. data/lib/arel/nodes/group.rb +6 -0
  34. data/lib/arel/nodes/grouping.rb +6 -0
  35. data/lib/arel/nodes/having.rb +6 -0
  36. data/lib/arel/nodes/in.rb +6 -0
  37. data/lib/arel/nodes/inner_join.rb +6 -0
  38. data/lib/arel/nodes/insert_statement.rb +19 -0
  39. data/lib/arel/nodes/intersect.rb +7 -0
  40. data/lib/arel/nodes/join.rb +13 -0
  41. data/lib/arel/nodes/less_than.rb +6 -0
  42. data/lib/arel/nodes/less_than_or_equal.rb +6 -0
  43. data/lib/arel/nodes/limit.rb +7 -0
  44. data/lib/arel/nodes/lock.rb +6 -0
  45. data/lib/arel/nodes/matches.rb +6 -0
  46. data/lib/arel/nodes/max.rb +6 -0
  47. data/lib/arel/nodes/min.rb +6 -0
  48. data/lib/arel/nodes/node.rb +44 -0
  49. data/lib/arel/nodes/not.rb +6 -0
  50. data/lib/arel/nodes/not_equal.rb +6 -0
  51. data/lib/arel/nodes/not_in.rb +6 -0
  52. data/lib/arel/nodes/offset.rb +7 -0
  53. data/lib/arel/nodes/on.rb +6 -0
  54. data/lib/arel/nodes/or.rb +6 -0
  55. data/lib/arel/nodes/ordering.rb +20 -0
  56. data/lib/arel/nodes/outer_join.rb +6 -0
  57. data/lib/arel/nodes/select_core.rb +26 -0
  58. data/lib/arel/nodes/select_statement.rb +22 -0
  59. data/lib/arel/nodes/sql_literal.rb +8 -0
  60. data/lib/arel/nodes/string_join.rb +11 -0
  61. data/lib/arel/nodes/sum.rb +6 -0
  62. data/lib/arel/nodes/table_alias.rb +13 -0
  63. data/lib/arel/nodes/top.rb +6 -0
  64. data/lib/arel/nodes/unary.rb +11 -0
  65. data/lib/arel/nodes/union.rb +7 -0
  66. data/lib/arel/nodes/union_all.rb +7 -0
  67. data/lib/arel/nodes/unqualified_column.rb +16 -0
  68. data/lib/arel/nodes/update_statement.rb +21 -0
  69. data/lib/arel/nodes/values.rb +14 -0
  70. data/lib/arel/predications.rb +183 -0
  71. data/lib/arel/relation.rb +6 -0
  72. data/lib/arel/select_manager.rb +237 -0
  73. data/lib/arel/sql/engine.rb +10 -0
  74. data/lib/arel/sql_literal.rb +4 -0
  75. data/lib/arel/table.rb +134 -0
  76. data/lib/arel/tree_manager.rb +36 -0
  77. data/lib/arel/update_manager.rb +49 -0
  78. data/lib/arel/visitors.rb +38 -0
  79. data/lib/arel/visitors/depth_first.rb +154 -0
  80. data/lib/arel/visitors/dot.rb +230 -0
  81. data/lib/arel/visitors/join_sql.rb +40 -0
  82. data/lib/arel/visitors/mssql.rb +16 -0
  83. data/lib/arel/visitors/mysql.rb +34 -0
  84. data/lib/arel/visitors/oracle.rb +116 -0
  85. data/lib/arel/visitors/order_clauses.rb +11 -0
  86. data/lib/arel/visitors/postgresql.rb +58 -0
  87. data/lib/arel/visitors/sqlite.rb +11 -0
  88. data/lib/arel/visitors/to_sql.rb +331 -0
  89. data/lib/arel/visitors/visitor.rb +27 -0
  90. data/lib/arel/visitors/where_sql.rb +9 -0
  91. data/square-arel.gemspec +36 -0
  92. data/test/attributes/test_attribute.rb +664 -0
  93. data/test/helper.rb +13 -0
  94. data/test/nodes/test_as.rb +16 -0
  95. data/test/nodes/test_count.rb +18 -0
  96. data/test/nodes/test_delete_statement.rb +14 -0
  97. data/test/nodes/test_equality.rb +74 -0
  98. data/test/nodes/test_insert_statement.rb +18 -0
  99. data/test/nodes/test_node.rb +33 -0
  100. data/test/nodes/test_not.rb +20 -0
  101. data/test/nodes/test_or.rb +22 -0
  102. data/test/nodes/test_select_core.rb +22 -0
  103. data/test/nodes/test_select_statement.rb +13 -0
  104. data/test/nodes/test_sql_literal.rb +52 -0
  105. data/test/nodes/test_sum.rb +12 -0
  106. data/test/nodes/test_update_statement.rb +18 -0
  107. data/test/support/fake_record.rb +91 -0
  108. data/test/test_activerecord_compat.rb +18 -0
  109. data/test/test_attributes.rb +46 -0
  110. data/test/test_crud.rb +69 -0
  111. data/test/test_delete_manager.rb +42 -0
  112. data/test/test_insert_manager.rb +125 -0
  113. data/test/test_select_manager.rb +659 -0
  114. data/test/test_table.rb +193 -0
  115. data/test/test_update_manager.rb +86 -0
  116. data/test/visitors/test_depth_first.rb +212 -0
  117. data/test/visitors/test_dot.rb +29 -0
  118. data/test/visitors/test_join_sql.rb +35 -0
  119. data/test/visitors/test_mssql.rb +18 -0
  120. data/test/visitors/test_mysql.rb +45 -0
  121. data/test/visitors/test_oracle.rb +147 -0
  122. data/test/visitors/test_postgres.rb +36 -0
  123. data/test/visitors/test_sqlite.rb +18 -0
  124. data/test/visitors/test_to_sql.rb +255 -0
  125. metadata +261 -0
@@ -0,0 +1,18 @@
1
+ module Arel
2
+ module Attributes
3
+ class Attribute < Struct.new :relation, :name, :column
4
+ include Arel::Expressions
5
+ include Arel::Predications
6
+ end
7
+
8
+ class String < Attribute; end
9
+ class Time < Attribute; end
10
+ class Boolean < Attribute; end
11
+ class Decimal < Attribute; end
12
+ class Float < Attribute; end
13
+ class Integer < Attribute; end
14
+ class Undefined < Attribute; end
15
+ end
16
+
17
+ Attribute = Attributes::Attribute
18
+ end
@@ -0,0 +1,33 @@
1
+ module Arel
2
+ module Compatibility # :nodoc:
3
+ class Wheres # :nodoc:
4
+ include Enumerable
5
+
6
+ module Value # :nodoc:
7
+ attr_accessor :visitor
8
+ def value
9
+ visitor.accept self
10
+ end
11
+
12
+ def name
13
+ super.to_sym
14
+ end
15
+ end
16
+
17
+ def initialize engine, collection
18
+ @engine = engine
19
+ @collection = collection
20
+ end
21
+
22
+ def each
23
+ to_sql = Visitors::ToSql.new @engine
24
+
25
+ @collection.each { |c|
26
+ c.extend(Value)
27
+ c.visitor = to_sql
28
+ yield c
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ module Arel
2
+ ###
3
+ # FIXME hopefully we can remove this
4
+ module Crud
5
+ # FIXME: this method should go away
6
+ def update values
7
+ um = UpdateManager.new @engine
8
+
9
+ if Nodes::SqlLiteral === values
10
+ relation = @ctx.froms
11
+ else
12
+ relation = values.first.first.relation
13
+ end
14
+ um.table relation
15
+ um.set values
16
+ um.take @ast.limit.expr if @ast.limit
17
+ um.order(*@ast.orders)
18
+ um.wheres = @ctx.wheres
19
+
20
+ @engine.connection.update um.to_sql, 'AREL'
21
+ end
22
+
23
+ # FIXME: this method should go away
24
+ def insert values
25
+ im = InsertManager.new @engine
26
+ im.insert values
27
+ @engine.connection.insert im.to_sql
28
+ end
29
+
30
+ def delete
31
+ dm = DeleteManager.new @engine
32
+ dm.wheres = @ctx.wheres
33
+ dm.from @ctx.froms
34
+ @engine.connection.delete dm.to_sql, 'AREL'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ module Arel
2
+ class DeleteManager < Arel::TreeManager
3
+ def initialize engine
4
+ super
5
+ @ast = Nodes::DeleteStatement.new
6
+ @ctx = @ast
7
+ end
8
+
9
+ def from relation
10
+ @ast.relation = relation
11
+ self
12
+ end
13
+
14
+ def wheres= list
15
+ @ast.wheres = list
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Arel
2
+ InnerJoin = Nodes::InnerJoin
3
+ OuterJoin = Nodes::OuterJoin
4
+ end
@@ -0,0 +1,4 @@
1
+ module Arel
2
+ module Expression
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ module Arel
2
+ module Expressions
3
+ def count distinct = false
4
+ Nodes::Count.new [self], distinct
5
+ end
6
+
7
+ def sum
8
+ Nodes::Sum.new [self], Nodes::SqlLiteral.new('sum_id')
9
+ end
10
+
11
+ def maximum
12
+ Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
13
+ end
14
+
15
+ def minimum
16
+ Nodes::Min.new [self], Nodes::SqlLiteral.new('min_id')
17
+ end
18
+
19
+ def average
20
+ Nodes::Avg.new [self], Nodes::SqlLiteral.new('avg_id')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ module Arel
2
+ class InsertManager < Arel::TreeManager
3
+ def initialize engine
4
+ super
5
+ @ast = Nodes::InsertStatement.new
6
+ end
7
+
8
+ def into table
9
+ @ast.relation = table
10
+ self
11
+ end
12
+
13
+ def columns; @ast.columns end
14
+ def values= val; @ast.values = val; end
15
+
16
+ def insert fields
17
+ return if fields.empty?
18
+
19
+ if String === fields
20
+ @ast.values = SqlLiteral.new(fields)
21
+ else
22
+ @ast.relation ||= fields.first.first.relation
23
+
24
+ values = []
25
+
26
+ fields.each do |column, value|
27
+ @ast.columns << column
28
+ values << value
29
+ end
30
+ @ast.values = Nodes::Values.new values, @ast.columns
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,53 @@
1
+ require 'arel/nodes/node'
2
+ require 'arel/nodes/unary'
3
+ require 'arel/nodes/binary'
4
+ require 'arel/nodes/equality'
5
+ require 'arel/nodes/between'
6
+ require 'arel/nodes/not_equal'
7
+ require 'arel/nodes/assignment'
8
+ require 'arel/nodes/or'
9
+ require 'arel/nodes/and'
10
+ require 'arel/nodes/as'
11
+ require 'arel/nodes/not'
12
+ require 'arel/nodes/greater_than'
13
+ require 'arel/nodes/greater_than_or_equal'
14
+ require 'arel/nodes/less_than'
15
+ require 'arel/nodes/less_than_or_equal'
16
+ require 'arel/nodes/matches'
17
+ require 'arel/nodes/does_not_match'
18
+
19
+ require 'arel/nodes/in'
20
+ require 'arel/nodes/not_in'
21
+ require 'arel/nodes/ordering'
22
+ require 'arel/nodes/lock'
23
+ require 'arel/nodes/function'
24
+ require 'arel/nodes/count'
25
+ require 'arel/nodes/values'
26
+ require 'arel/nodes/offset'
27
+ require 'arel/nodes/limit'
28
+ require 'arel/nodes/top'
29
+ require 'arel/nodes/sum'
30
+ require 'arel/nodes/exists'
31
+ require 'arel/nodes/max'
32
+ require 'arel/nodes/min'
33
+ require 'arel/nodes/avg'
34
+ require 'arel/nodes/having'
35
+ require 'arel/nodes/sql_literal'
36
+ require 'arel/nodes/select_core'
37
+ require 'arel/nodes/select_statement'
38
+ require 'arel/nodes/insert_statement'
39
+ require 'arel/nodes/update_statement'
40
+ require 'arel/nodes/delete_statement'
41
+ require 'arel/nodes/unqualified_column'
42
+ require 'arel/nodes/table_alias'
43
+ require 'arel/nodes/join'
44
+ require 'arel/nodes/group'
45
+ require 'arel/nodes/grouping'
46
+ require 'arel/nodes/inner_join'
47
+ require 'arel/nodes/outer_join'
48
+ require 'arel/nodes/string_join'
49
+ require 'arel/nodes/on'
50
+ require 'arel/nodes/except'
51
+ require 'arel/nodes/intersect'
52
+ require 'arel/nodes/union'
53
+ require 'arel/nodes/union_all'
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class And < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class As < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Assignment < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Avg < Arel::Nodes::Function
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Between < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Arel
2
+ module Nodes
3
+ class Binary < Arel::Nodes::Node
4
+ attr_accessor :left, :right
5
+
6
+ def initialize left, right
7
+ @left = left
8
+ @right = right
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Arel
2
+ module Nodes
3
+ class Count < Arel::Nodes::Function
4
+
5
+ attr_accessor :distinct
6
+
7
+ def initialize expr, distinct = false, aliaz = nil
8
+ super(expr, aliaz)
9
+ @distinct = distinct
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module Arel
2
+ module Nodes
3
+ class DeleteStatement < Arel::Nodes::Binary
4
+ alias :relation :left
5
+ alias :relation= :left=
6
+ alias :wheres :right
7
+ alias :wheres= :right=
8
+
9
+ def initialize relation = nil, wheres = []
10
+ super
11
+ end
12
+
13
+ def initialize_copy other
14
+ super
15
+ @right = @right.clone
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class DoesNotMatch < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module Arel
2
+ module Nodes
3
+ class Equality < Arel::Nodes::Binary
4
+ def operator; :== end
5
+ alias :operand1 :left
6
+ alias :operand2 :right
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module Arel
2
+ module Nodes
3
+ class Except < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ module Arel
2
+ module Nodes
3
+ class Exists < Arel::Nodes::Function
4
+ alias :select_stmt :expressions
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Arel
2
+ module Nodes
3
+ class Function < Arel::Nodes::Node
4
+ include Arel::Expression
5
+ attr_accessor :expressions, :alias
6
+
7
+ def initialize expr, aliaz = nil
8
+ @expressions = expr
9
+ @alias = aliaz
10
+ end
11
+
12
+ def as aliaz
13
+ self.alias = SqlLiteral.new(aliaz)
14
+ self
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class GreaterThan < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class GreaterThanOrEqual < Arel::Nodes::Binary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Group < Arel::Nodes::Unary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Grouping < Arel::Nodes::Unary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class Having < Arel::Nodes::Unary
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class In < Equality
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Arel
2
+ module Nodes
3
+ class InnerJoin < Arel::Nodes::Join
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,19 @@
1
+ module Arel
2
+ module Nodes
3
+ class InsertStatement < Arel::Nodes::Node
4
+ attr_accessor :relation, :columns, :values
5
+
6
+ def initialize
7
+ @relation = nil
8
+ @columns = []
9
+ @values = nil
10
+ end
11
+
12
+ def initialize_copy other
13
+ super
14
+ @columns = @columns.clone
15
+ @values = @values.clone if @values
16
+ end
17
+ end
18
+ end
19
+ end