square-arel 2.0.9.20110222133018
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/History.txt +105 -0
- data/MIT-LICENSE.txt +20 -0
- data/Manifest.txt +124 -0
- data/README.markdown +94 -0
- data/Rakefile +20 -0
- data/lib/arel.rb +39 -0
- data/lib/arel/attributes.rb +20 -0
- data/lib/arel/attributes/attribute.rb +18 -0
- data/lib/arel/compatibility/wheres.rb +33 -0
- data/lib/arel/crud.rb +37 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/deprecated.rb +4 -0
- data/lib/arel/expression.rb +4 -0
- data/lib/arel/expressions.rb +23 -0
- data/lib/arel/insert_manager.rb +34 -0
- data/lib/arel/nodes.rb +53 -0
- data/lib/arel/nodes/and.rb +6 -0
- data/lib/arel/nodes/as.rb +6 -0
- data/lib/arel/nodes/assignment.rb +6 -0
- data/lib/arel/nodes/avg.rb +6 -0
- data/lib/arel/nodes/between.rb +6 -0
- data/lib/arel/nodes/binary.rb +12 -0
- data/lib/arel/nodes/count.rb +13 -0
- data/lib/arel/nodes/delete_statement.rb +19 -0
- data/lib/arel/nodes/does_not_match.rb +6 -0
- data/lib/arel/nodes/equality.rb +9 -0
- data/lib/arel/nodes/except.rb +7 -0
- data/lib/arel/nodes/exists.rb +7 -0
- data/lib/arel/nodes/function.rb +18 -0
- data/lib/arel/nodes/greater_than.rb +6 -0
- data/lib/arel/nodes/greater_than_or_equal.rb +6 -0
- data/lib/arel/nodes/group.rb +6 -0
- data/lib/arel/nodes/grouping.rb +6 -0
- data/lib/arel/nodes/having.rb +6 -0
- data/lib/arel/nodes/in.rb +6 -0
- data/lib/arel/nodes/inner_join.rb +6 -0
- data/lib/arel/nodes/insert_statement.rb +19 -0
- data/lib/arel/nodes/intersect.rb +7 -0
- data/lib/arel/nodes/join.rb +13 -0
- data/lib/arel/nodes/less_than.rb +6 -0
- data/lib/arel/nodes/less_than_or_equal.rb +6 -0
- data/lib/arel/nodes/limit.rb +7 -0
- data/lib/arel/nodes/lock.rb +6 -0
- data/lib/arel/nodes/matches.rb +6 -0
- data/lib/arel/nodes/max.rb +6 -0
- data/lib/arel/nodes/min.rb +6 -0
- data/lib/arel/nodes/node.rb +44 -0
- data/lib/arel/nodes/not.rb +6 -0
- data/lib/arel/nodes/not_equal.rb +6 -0
- data/lib/arel/nodes/not_in.rb +6 -0
- data/lib/arel/nodes/offset.rb +7 -0
- data/lib/arel/nodes/on.rb +6 -0
- data/lib/arel/nodes/or.rb +6 -0
- data/lib/arel/nodes/ordering.rb +20 -0
- data/lib/arel/nodes/outer_join.rb +6 -0
- data/lib/arel/nodes/select_core.rb +26 -0
- data/lib/arel/nodes/select_statement.rb +22 -0
- data/lib/arel/nodes/sql_literal.rb +8 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/sum.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +13 -0
- data/lib/arel/nodes/top.rb +6 -0
- data/lib/arel/nodes/unary.rb +11 -0
- data/lib/arel/nodes/union.rb +7 -0
- data/lib/arel/nodes/union_all.rb +7 -0
- data/lib/arel/nodes/unqualified_column.rb +16 -0
- data/lib/arel/nodes/update_statement.rb +21 -0
- data/lib/arel/nodes/values.rb +14 -0
- data/lib/arel/predications.rb +183 -0
- data/lib/arel/relation.rb +6 -0
- data/lib/arel/select_manager.rb +237 -0
- data/lib/arel/sql/engine.rb +10 -0
- data/lib/arel/sql_literal.rb +4 -0
- data/lib/arel/table.rb +134 -0
- data/lib/arel/tree_manager.rb +36 -0
- data/lib/arel/update_manager.rb +49 -0
- data/lib/arel/visitors.rb +38 -0
- data/lib/arel/visitors/depth_first.rb +154 -0
- data/lib/arel/visitors/dot.rb +230 -0
- data/lib/arel/visitors/join_sql.rb +40 -0
- data/lib/arel/visitors/mssql.rb +16 -0
- data/lib/arel/visitors/mysql.rb +34 -0
- data/lib/arel/visitors/oracle.rb +116 -0
- data/lib/arel/visitors/order_clauses.rb +11 -0
- data/lib/arel/visitors/postgresql.rb +58 -0
- data/lib/arel/visitors/sqlite.rb +11 -0
- data/lib/arel/visitors/to_sql.rb +331 -0
- data/lib/arel/visitors/visitor.rb +27 -0
- data/lib/arel/visitors/where_sql.rb +9 -0
- data/square-arel.gemspec +36 -0
- data/test/attributes/test_attribute.rb +664 -0
- data/test/helper.rb +13 -0
- data/test/nodes/test_as.rb +16 -0
- data/test/nodes/test_count.rb +18 -0
- data/test/nodes/test_delete_statement.rb +14 -0
- data/test/nodes/test_equality.rb +74 -0
- data/test/nodes/test_insert_statement.rb +18 -0
- data/test/nodes/test_node.rb +33 -0
- data/test/nodes/test_not.rb +20 -0
- data/test/nodes/test_or.rb +22 -0
- data/test/nodes/test_select_core.rb +22 -0
- data/test/nodes/test_select_statement.rb +13 -0
- data/test/nodes/test_sql_literal.rb +52 -0
- data/test/nodes/test_sum.rb +12 -0
- data/test/nodes/test_update_statement.rb +18 -0
- data/test/support/fake_record.rb +91 -0
- data/test/test_activerecord_compat.rb +18 -0
- data/test/test_attributes.rb +46 -0
- data/test/test_crud.rb +69 -0
- data/test/test_delete_manager.rb +42 -0
- data/test/test_insert_manager.rb +125 -0
- data/test/test_select_manager.rb +659 -0
- data/test/test_table.rb +193 -0
- data/test/test_update_manager.rb +86 -0
- data/test/visitors/test_depth_first.rb +212 -0
- data/test/visitors/test_dot.rb +29 -0
- data/test/visitors/test_join_sql.rb +35 -0
- data/test/visitors/test_mssql.rb +18 -0
- data/test/visitors/test_mysql.rb +45 -0
- data/test/visitors/test_oracle.rb +147 -0
- data/test/visitors/test_postgres.rb +36 -0
- data/test/visitors/test_sqlite.rb +18 -0
- data/test/visitors/test_to_sql.rb +255 -0
- 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
|
data/lib/arel/crud.rb
ADDED
@@ -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,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
|
data/lib/arel/nodes.rb
ADDED
@@ -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,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,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,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
|