arel 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +20 -0
- data/Manifest.txt +105 -0
- data/README.markdown +12 -32
- data/Rakefile +17 -0
- data/arel.gemspec +39 -0
- data/lib/arel.rb +30 -9
- data/lib/arel/attributes.rb +20 -0
- data/lib/arel/attributes/attribute.rb +190 -0
- data/lib/arel/compatibility/wheres.rb +33 -0
- data/lib/arel/crud.rb +37 -0
- data/lib/arel/delete_manager.rb +22 -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 +44 -0
- data/lib/arel/nodes/and.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 +17 -0
- data/lib/arel/nodes/does_not_match.rb +6 -0
- data/lib/arel/nodes/equality.rb +9 -0
- data/lib/arel/nodes/exists.rb +11 -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 +11 -0
- data/lib/arel/nodes/grouping.rb +11 -0
- data/lib/arel/nodes/having.rb +11 -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/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/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 +30 -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 +11 -0
- data/lib/arel/nodes/on.rb +11 -0
- data/lib/arel/nodes/or.rb +6 -0
- data/lib/arel/nodes/ordering.rb +19 -0
- data/lib/arel/nodes/outer_join.rb +6 -0
- data/lib/arel/nodes/select_core.rb +25 -0
- data/lib/arel/nodes/select_statement.rb +22 -0
- data/lib/arel/nodes/sql_literal.rb +7 -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 +21 -0
- data/lib/arel/nodes/unqualified_column.rb +19 -0
- data/lib/arel/nodes/update_statement.rb +21 -0
- data/lib/arel/nodes/values.rb +12 -0
- data/lib/arel/relation.rb +6 -0
- data/lib/arel/select_manager.rb +203 -0
- data/lib/arel/sql/engine.rb +10 -0
- data/lib/arel/sql_literal.rb +1 -10
- data/lib/arel/table.rb +126 -0
- data/lib/arel/tree_manager.rb +26 -0
- data/lib/arel/update_manager.rb +48 -0
- data/lib/arel/visitors.rb +30 -0
- data/lib/arel/visitors/dot.rb +233 -0
- data/lib/arel/visitors/join_sql.rb +38 -0
- data/lib/arel/visitors/mysql.rb +16 -0
- data/lib/arel/visitors/oracle.rb +69 -0
- data/lib/arel/visitors/order_clauses.rb +9 -0
- data/lib/arel/visitors/postgresql.rb +54 -0
- data/lib/arel/visitors/to_sql.rb +301 -0
- data/lib/arel/visitors/where_sql.rb +9 -0
- data/spec/activerecord_compat_spec.rb +18 -0
- data/spec/attributes/attribute_spec.rb +648 -0
- data/spec/attributes_spec.rb +33 -6
- data/spec/crud_spec.rb +69 -0
- data/spec/delete_manager_spec.rb +53 -0
- data/spec/insert_manager_spec.rb +141 -0
- data/spec/nodes/count_spec.rb +18 -0
- data/spec/nodes/delete_statement_spec.rb +15 -0
- data/spec/nodes/equality_spec.rb +72 -0
- data/spec/nodes/insert_statement_spec.rb +18 -0
- data/spec/nodes/or_spec.rb +20 -0
- data/spec/nodes/select_core_spec.rb +21 -0
- data/spec/nodes/select_statement_spec.rb +14 -0
- data/spec/nodes/sql_literal_spec.rb +26 -0
- data/spec/nodes/sum_spec.rb +12 -0
- data/spec/nodes/update_statement_spec.rb +18 -0
- data/spec/select_manager_spec.rb +581 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +6 -21
- data/spec/support/fake_record.rb +89 -0
- data/spec/support/shared/tree_manager_shared.rb +9 -0
- data/spec/table_spec.rb +176 -0
- data/spec/update_manager_spec.rb +89 -0
- data/spec/visitors/join_sql_spec.rb +35 -0
- data/spec/visitors/oracle_spec.rb +111 -0
- data/spec/visitors/to_sql_spec.rb +134 -0
- metadata +160 -260
- data/lib/arel/algebra.rb +0 -10
- data/lib/arel/algebra/attributes.rb +0 -7
- data/lib/arel/algebra/attributes/attribute.rb +0 -304
- data/lib/arel/algebra/attributes/boolean.rb +0 -21
- data/lib/arel/algebra/attributes/decimal.rb +0 -9
- data/lib/arel/algebra/attributes/float.rb +0 -9
- data/lib/arel/algebra/attributes/integer.rb +0 -10
- data/lib/arel/algebra/attributes/string.rb +0 -10
- data/lib/arel/algebra/attributes/time.rb +0 -6
- data/lib/arel/algebra/core_extensions.rb +0 -3
- data/lib/arel/algebra/core_extensions/hash.rb +0 -7
- data/lib/arel/algebra/core_extensions/object.rb +0 -13
- data/lib/arel/algebra/core_extensions/symbol.rb +0 -9
- data/lib/arel/algebra/expression.rb +0 -56
- data/lib/arel/algebra/header.rb +0 -66
- data/lib/arel/algebra/ordering.rb +0 -31
- data/lib/arel/algebra/predicates.rb +0 -306
- data/lib/arel/algebra/relations.rb +0 -16
- data/lib/arel/algebra/relations/operations/from.rb +0 -14
- data/lib/arel/algebra/relations/operations/group.rb +0 -14
- data/lib/arel/algebra/relations/operations/having.rb +0 -14
- data/lib/arel/algebra/relations/operations/join.rb +0 -103
- data/lib/arel/algebra/relations/operations/lock.rb +0 -10
- data/lib/arel/algebra/relations/operations/order.rb +0 -23
- data/lib/arel/algebra/relations/operations/project.rb +0 -20
- data/lib/arel/algebra/relations/operations/skip.rb +0 -14
- data/lib/arel/algebra/relations/operations/take.rb +0 -18
- data/lib/arel/algebra/relations/operations/where.rb +0 -24
- data/lib/arel/algebra/relations/relation.rb +0 -205
- data/lib/arel/algebra/relations/row.rb +0 -29
- data/lib/arel/algebra/relations/utilities/compound.rb +0 -55
- data/lib/arel/algebra/relations/utilities/externalization.rb +0 -26
- data/lib/arel/algebra/relations/utilities/nil.rb +0 -7
- data/lib/arel/algebra/relations/writes.rb +0 -47
- data/lib/arel/algebra/value.rb +0 -53
- data/lib/arel/engines.rb +0 -2
- data/lib/arel/engines/memory.rb +0 -2
- data/lib/arel/engines/memory/engine.rb +0 -10
- data/lib/arel/engines/memory/relations.rb +0 -2
- data/lib/arel/engines/memory/relations/array.rb +0 -37
- data/lib/arel/engines/memory/relations/operations.rb +0 -9
- data/lib/arel/engines/sql.rb +0 -6
- data/lib/arel/engines/sql/attributes.rb +0 -45
- data/lib/arel/engines/sql/christener.rb +0 -20
- data/lib/arel/engines/sql/compilers/ibm_db_compiler.rb +0 -48
- data/lib/arel/engines/sql/compilers/mysql_compiler.rb +0 -11
- data/lib/arel/engines/sql/compilers/oracle_compiler.rb +0 -106
- data/lib/arel/engines/sql/compilers/postgresql_compiler.rb +0 -50
- data/lib/arel/engines/sql/compilers/sqlite_compiler.rb +0 -9
- data/lib/arel/engines/sql/core_extensions.rb +0 -4
- data/lib/arel/engines/sql/core_extensions/array.rb +0 -24
- data/lib/arel/engines/sql/core_extensions/nil_class.rb +0 -15
- data/lib/arel/engines/sql/core_extensions/object.rb +0 -19
- data/lib/arel/engines/sql/core_extensions/range.rb +0 -19
- data/lib/arel/engines/sql/engine.rb +0 -47
- data/lib/arel/engines/sql/formatters.rb +0 -138
- data/lib/arel/engines/sql/relations.rb +0 -3
- data/lib/arel/engines/sql/relations/compiler.rb +0 -153
- data/lib/arel/engines/sql/relations/table.rb +0 -100
- data/lib/arel/engines/sql/relations/utilities/nil.rb +0 -6
- data/lib/arel/recursion/base_case.rb +0 -13
- data/lib/arel/session.rb +0 -35
- data/lib/arel/version.rb +0 -3
- data/spec/algebra/unit/predicates/binary_spec.rb +0 -35
- data/spec/algebra/unit/predicates/equality_spec.rb +0 -29
- data/spec/algebra/unit/predicates/in_spec.rb +0 -12
- data/spec/algebra/unit/predicates/inequality_spec.rb +0 -32
- data/spec/algebra/unit/predicates/predicate_spec.rb +0 -22
- data/spec/algebra/unit/primitives/attribute_spec.rb +0 -175
- data/spec/algebra/unit/primitives/expression_spec.rb +0 -39
- data/spec/algebra/unit/primitives/value_spec.rb +0 -15
- data/spec/algebra/unit/relations/alias_spec.rb +0 -16
- data/spec/algebra/unit/relations/delete_spec.rb +0 -9
- data/spec/algebra/unit/relations/group_spec.rb +0 -10
- data/spec/algebra/unit/relations/insert_spec.rb +0 -9
- data/spec/algebra/unit/relations/join_spec.rb +0 -18
- data/spec/algebra/unit/relations/order_spec.rb +0 -21
- data/spec/algebra/unit/relations/project_spec.rb +0 -34
- data/spec/algebra/unit/relations/relation_spec.rb +0 -241
- data/spec/algebra/unit/relations/skip_spec.rb +0 -10
- data/spec/algebra/unit/relations/table_spec.rb +0 -38
- data/spec/algebra/unit/relations/take_spec.rb +0 -10
- data/spec/algebra/unit/relations/update_spec.rb +0 -9
- data/spec/algebra/unit/relations/where_spec.rb +0 -19
- data/spec/algebra/unit/session/session_spec.rb +0 -84
- data/spec/attributes/boolean_spec.rb +0 -57
- data/spec/attributes/float_spec.rb +0 -119
- data/spec/attributes/header_spec.rb +0 -42
- data/spec/attributes/integer_spec.rb +0 -119
- data/spec/attributes/string_spec.rb +0 -43
- data/spec/attributes/time_spec.rb +0 -24
- data/spec/engines/memory/integration/joins/cross_engine_spec.rb +0 -61
- data/spec/engines/memory/unit/relations/array_spec.rb +0 -33
- data/spec/engines/memory/unit/relations/insert_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/join_spec.rb +0 -32
- data/spec/engines/memory/unit/relations/order_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/project_spec.rb +0 -27
- data/spec/engines/memory/unit/relations/skip_spec.rb +0 -31
- data/spec/engines/memory/unit/relations/take_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/where_spec.rb +0 -43
- data/spec/engines/sql/integration/joins/with_adjacency_spec.rb +0 -258
- data/spec/engines/sql/integration/joins/with_aggregations_spec.rb +0 -221
- data/spec/engines/sql/integration/joins/with_compounds_spec.rb +0 -137
- data/spec/engines/sql/unit/engine_spec.rb +0 -65
- data/spec/engines/sql/unit/predicates/binary_spec.rb +0 -140
- data/spec/engines/sql/unit/predicates/equality_spec.rb +0 -75
- data/spec/engines/sql/unit/predicates/in_spec.rb +0 -179
- data/spec/engines/sql/unit/predicates/noteq_spec.rb +0 -75
- data/spec/engines/sql/unit/predicates/predicates_spec.rb +0 -79
- data/spec/engines/sql/unit/primitives/attribute_spec.rb +0 -36
- data/spec/engines/sql/unit/primitives/expression_spec.rb +0 -28
- data/spec/engines/sql/unit/primitives/literal_spec.rb +0 -43
- data/spec/engines/sql/unit/primitives/value_spec.rb +0 -29
- data/spec/engines/sql/unit/relations/alias_spec.rb +0 -53
- data/spec/engines/sql/unit/relations/delete_spec.rb +0 -83
- data/spec/engines/sql/unit/relations/from_spec.rb +0 -64
- data/spec/engines/sql/unit/relations/group_spec.rb +0 -72
- data/spec/engines/sql/unit/relations/having_spec.rb +0 -78
- data/spec/engines/sql/unit/relations/insert_spec.rb +0 -143
- data/spec/engines/sql/unit/relations/join_spec.rb +0 -180
- data/spec/engines/sql/unit/relations/lock_spec.rb +0 -86
- data/spec/engines/sql/unit/relations/order_spec.rb +0 -161
- data/spec/engines/sql/unit/relations/project_spec.rb +0 -143
- data/spec/engines/sql/unit/relations/skip_spec.rb +0 -41
- data/spec/engines/sql/unit/relations/table_spec.rb +0 -122
- data/spec/engines/sql/unit/relations/take_spec.rb +0 -75
- data/spec/engines/sql/unit/relations/update_spec.rb +0 -203
- data/spec/engines/sql/unit/relations/where_spec.rb +0 -72
- data/spec/relations/join_spec.rb +0 -42
- data/spec/relations/relation_spec.rb +0 -31
- data/spec/shared/relation_spec.rb +0 -255
- data/spec/sql/christener_spec.rb +0 -70
- data/spec/support/connections/mysql_connection.rb +0 -14
- data/spec/support/connections/oracle_connection.rb +0 -17
- data/spec/support/connections/postgresql_connection.rb +0 -13
- data/spec/support/connections/sqlite3_connection.rb +0 -24
- data/spec/support/guards.rb +0 -28
- data/spec/support/matchers/disambiguate_attributes.rb +0 -28
- data/spec/support/matchers/hash_the_same_as.rb +0 -26
- data/spec/support/matchers/have_rows.rb +0 -18
- data/spec/support/model.rb +0 -67
- data/spec/support/schemas/mysql_schema.rb +0 -26
- data/spec/support/schemas/oracle_schema.rb +0 -20
- data/spec/support/schemas/postgresql_schema.rb +0 -26
- data/spec/support/schemas/sqlite3_schema.rb +0 -26
@@ -0,0 +1,30 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
###
|
4
|
+
# Abstract base class for all AST nodes
|
5
|
+
class Node
|
6
|
+
###
|
7
|
+
# Factory method to create a Nodes::Grouping node that has an Nodes::Or
|
8
|
+
# node as a child.
|
9
|
+
def or right
|
10
|
+
Nodes::Grouping.new Nodes::Or.new(self, right)
|
11
|
+
end
|
12
|
+
|
13
|
+
###
|
14
|
+
# Factory method to create an Nodes::And node.
|
15
|
+
def and right
|
16
|
+
Nodes::And.new self, right
|
17
|
+
end
|
18
|
+
|
19
|
+
# FIXME: this method should go away. I don't like people calling
|
20
|
+
# to_sql on non-head nodes. This forces us to walk the AST until we
|
21
|
+
# can find a node that has a "relation" member.
|
22
|
+
#
|
23
|
+
# Maybe we should just use `Table.engine`? :'(
|
24
|
+
def to_sql engine = Table.engine
|
25
|
+
viz = Visitors.for engine
|
26
|
+
viz.accept self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class Ordering < Arel::Nodes::Node
|
4
|
+
attr_accessor :expr, :direction
|
5
|
+
|
6
|
+
def initialize expression, direction = :asc
|
7
|
+
@expr, @direction = expression, direction
|
8
|
+
end
|
9
|
+
|
10
|
+
def ascending?
|
11
|
+
direction == :asc
|
12
|
+
end
|
13
|
+
|
14
|
+
def descending?
|
15
|
+
direction == :desc
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class SelectCore
|
4
|
+
attr_accessor :froms, :projections, :wheres, :groups
|
5
|
+
attr_accessor :having
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@froms = nil
|
9
|
+
@projections = []
|
10
|
+
@wheres = []
|
11
|
+
@groups = []
|
12
|
+
@having = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize_copy other
|
16
|
+
super
|
17
|
+
@froms = @froms.clone if @froms
|
18
|
+
@projections = @projections.clone
|
19
|
+
@wheres = @wheres.clone
|
20
|
+
@group = @groups.clone
|
21
|
+
@having = @having.clone if @having
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class SelectStatement
|
4
|
+
attr_reader :cores
|
5
|
+
attr_accessor :limit, :orders, :lock, :offset
|
6
|
+
|
7
|
+
def initialize cores = [SelectCore.new]
|
8
|
+
@cores = cores
|
9
|
+
@orders = []
|
10
|
+
@limit = nil
|
11
|
+
@lock = nil
|
12
|
+
@offset = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize_copy other
|
16
|
+
super
|
17
|
+
@cores = @cores.map { |x| x.clone }
|
18
|
+
@orders = @orders.map { |x| x.clone }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class TableAlias
|
4
|
+
attr_reader :name, :relation, :columns
|
5
|
+
alias :table_alias :name
|
6
|
+
|
7
|
+
def initialize name, relation
|
8
|
+
@name = name
|
9
|
+
@relation = relation
|
10
|
+
@columns = relation.columns.map { |column|
|
11
|
+
column.dup.tap { |col| col.relation = self }
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def [] name
|
16
|
+
name = name.to_sym
|
17
|
+
columns.find { |column| column.name == name }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class UnqualifiedColumn
|
4
|
+
attr_accessor :attribute
|
5
|
+
|
6
|
+
def initialize attribute
|
7
|
+
@attribute = attribute
|
8
|
+
end
|
9
|
+
|
10
|
+
def column
|
11
|
+
@attribute.column
|
12
|
+
end
|
13
|
+
|
14
|
+
def name
|
15
|
+
@attribute.name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Arel
|
2
|
+
module Nodes
|
3
|
+
class UpdateStatement
|
4
|
+
attr_accessor :relation, :wheres, :values, :orders, :limit
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@relation = nil
|
8
|
+
@wheres = []
|
9
|
+
@values = []
|
10
|
+
@orders = []
|
11
|
+
@limit = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize_copy other
|
15
|
+
super
|
16
|
+
@wheres = @wheres.clone
|
17
|
+
@values = @values.clone
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
module Arel
|
2
|
+
class SelectManager < Arel::TreeManager
|
3
|
+
include Arel::Crud
|
4
|
+
|
5
|
+
def initialize engine, table = nil
|
6
|
+
super(engine)
|
7
|
+
@head = Nodes::SelectStatement.new
|
8
|
+
@ctx = @head.cores.last
|
9
|
+
from table
|
10
|
+
end
|
11
|
+
|
12
|
+
def taken
|
13
|
+
@head.limit
|
14
|
+
end
|
15
|
+
|
16
|
+
def constraints
|
17
|
+
@ctx.wheres
|
18
|
+
end
|
19
|
+
|
20
|
+
def skip amount
|
21
|
+
@head.offset = Nodes::Offset.new(amount)
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def where_clauses
|
26
|
+
#warn "where_clauses is deprecated" if $VERBOSE
|
27
|
+
to_sql = Visitors::ToSql.new @engine
|
28
|
+
@ctx.wheres.map { |c| to_sql.accept c }
|
29
|
+
end
|
30
|
+
|
31
|
+
def lock locking = true
|
32
|
+
# FIXME: do we even need to store this? If locking is +false+ shouldn't
|
33
|
+
# we just remove the node from the AST?
|
34
|
+
@head.lock = Nodes::Lock.new
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def locked
|
39
|
+
@head.lock
|
40
|
+
end
|
41
|
+
|
42
|
+
def on *exprs
|
43
|
+
@ctx.froms.constraint = Nodes::On.new(collapse(exprs))
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def group *columns
|
48
|
+
columns.each do |column|
|
49
|
+
# FIXME: backwards compat
|
50
|
+
column = Nodes::SqlLiteral.new(column) if String === column
|
51
|
+
column = Nodes::SqlLiteral.new(column.to_s) if Symbol === column
|
52
|
+
|
53
|
+
@ctx.groups.push Nodes::Group.new column
|
54
|
+
end
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def from table
|
59
|
+
table = Nodes::SqlLiteral.new(table) if String === table
|
60
|
+
# FIXME: this is a hack to support
|
61
|
+
# test_with_two_tables_in_from_without_getting_double_quoted
|
62
|
+
# from the AR tests.
|
63
|
+
if @ctx.froms
|
64
|
+
source = @ctx.froms
|
65
|
+
|
66
|
+
if Nodes::SqlLiteral === table && Nodes::Join === source
|
67
|
+
source.left = table
|
68
|
+
table = source
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
@ctx.froms = table
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def join relation, klass = Nodes::InnerJoin
|
77
|
+
return self unless relation
|
78
|
+
|
79
|
+
case relation
|
80
|
+
when String, Nodes::SqlLiteral
|
81
|
+
raise if relation.blank?
|
82
|
+
from Nodes::StringJoin.new(@ctx.froms, relation)
|
83
|
+
else
|
84
|
+
from klass.new(@ctx.froms, relation, nil)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def having expr
|
89
|
+
expr = Nodes::SqlLiteral.new(expr) if String === expr
|
90
|
+
|
91
|
+
@ctx.having = Nodes::Having.new(expr)
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
def project *projections
|
96
|
+
# FIXME: converting these to SQLLiterals is probably not good, but
|
97
|
+
# rails tests require it.
|
98
|
+
@ctx.projections.concat projections.map { |x|
|
99
|
+
String == x.class ? SqlLiteral.new(x) : x
|
100
|
+
}
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def where expr
|
105
|
+
@ctx.wheres << expr
|
106
|
+
self
|
107
|
+
end
|
108
|
+
|
109
|
+
def order *expr
|
110
|
+
# FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
|
111
|
+
@head.orders.concat expr.map { |x|
|
112
|
+
String === x || Symbol === x ? Nodes::SqlLiteral.new(x.to_s) : x
|
113
|
+
}
|
114
|
+
self
|
115
|
+
end
|
116
|
+
|
117
|
+
def orders
|
118
|
+
@head.orders
|
119
|
+
end
|
120
|
+
|
121
|
+
def wheres
|
122
|
+
Compatibility::Wheres.new @engine, @ctx.wheres
|
123
|
+
end
|
124
|
+
|
125
|
+
def where_sql
|
126
|
+
return if @ctx.wheres.empty?
|
127
|
+
|
128
|
+
viz = Visitors::WhereSql.new @engine
|
129
|
+
Nodes::SqlLiteral.new viz.accept @ctx
|
130
|
+
end
|
131
|
+
|
132
|
+
def take limit
|
133
|
+
@head.limit = limit
|
134
|
+
self
|
135
|
+
end
|
136
|
+
|
137
|
+
def join_sql
|
138
|
+
return nil unless @ctx.froms
|
139
|
+
|
140
|
+
viz = Visitors::JoinSql.new @engine
|
141
|
+
Nodes::SqlLiteral.new viz.accept @ctx
|
142
|
+
end
|
143
|
+
|
144
|
+
def order_clauses
|
145
|
+
Visitors::OrderClauses.new(@engine).accept(@head).map { |x|
|
146
|
+
Nodes::SqlLiteral.new x
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
def joins manager
|
151
|
+
manager.join_sql
|
152
|
+
end
|
153
|
+
|
154
|
+
class Row < Struct.new(:data) # :nodoc:
|
155
|
+
def id
|
156
|
+
data['id']
|
157
|
+
end
|
158
|
+
|
159
|
+
def method_missing(name, *args)
|
160
|
+
name = name.to_s
|
161
|
+
return data[name] if data.key?(name)
|
162
|
+
super
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_a # :nodoc:
|
167
|
+
warn "to_a is deprecated. Please remove it from #{caller[0]}"
|
168
|
+
# FIXME: I think `select` should be made public...
|
169
|
+
@engine.connection.send(:select, to_sql, 'AREL').map { |x| Row.new(x) }
|
170
|
+
end
|
171
|
+
|
172
|
+
# FIXME: this method should go away
|
173
|
+
def insert values
|
174
|
+
im = InsertManager.new @engine
|
175
|
+
table = @ctx.froms
|
176
|
+
primary_key_name = (primary_key = table.primary_key) && primary_key.name
|
177
|
+
# FIXME: in AR tests values sometimes were Array and not Hash therefore is_a?(Hash) check is added
|
178
|
+
primary_key_value = primary_key && values.is_a?(Hash) && values[primary_key]
|
179
|
+
im.into table
|
180
|
+
im.insert values
|
181
|
+
# Oracle adapter needs primary key name to generate RETURNING ... INTO ... clause
|
182
|
+
# for tables which assign primary key value using trigger.
|
183
|
+
# RETURNING ... INTO ... clause will be added only if primary_key_value is nil
|
184
|
+
# therefore it is necessary to pass primary key value as well
|
185
|
+
@engine.connection.insert im.to_sql, 'AREL', primary_key_name, primary_key_value
|
186
|
+
end
|
187
|
+
|
188
|
+
private
|
189
|
+
def collapse exprs
|
190
|
+
return exprs.first if exprs.length == 1
|
191
|
+
|
192
|
+
right = exprs.pop
|
193
|
+
left = exprs.pop
|
194
|
+
|
195
|
+
right = Nodes::SqlLiteral.new(right) if String === right
|
196
|
+
|
197
|
+
right = Nodes::And.new left, right
|
198
|
+
exprs.reverse.inject(right) { |memo,expr|
|
199
|
+
Nodes::And.new(expr, memo)
|
200
|
+
}
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|