File tree 3 files changed +17
-0
lines changed
3 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -3,15 +3,18 @@ module SQLTree::Node
3
3
class Join < Base
4
4
5
5
leaf :join_type
6
+ leaf :is_outer
6
7
child :table_reference
7
8
child :join_expression
8
9
9
10
def initialize ( values = { } )
11
+ self . is_outer = false
10
12
values . each { |key , value | self . send ( :"#{ key } =" , value ) }
11
13
end
12
14
13
15
def to_sql ( options = { } )
14
16
join_sql = join_type ? "#{ join_type . to_s . upcase } " : ""
17
+ join_sql << "OUTER " if is_outer
15
18
join_sql << "JOIN #{ table_reference . to_sql ( options ) } "
16
19
join_sql << "ON #{ join_expression . to_sql ( options ) } "
17
20
join_sql
@@ -35,6 +38,11 @@ def self.parse(tokens)
35
38
join . join_type = tokens . next . literal . downcase . to_sym
36
39
end
37
40
41
+ if [ :right , :left ] . include? ( join . join_type ) && tokens . peek . class == SQLTree ::Token ::OUTER
42
+ join . is_outer = true
43
+ tokens . consume ( SQLTree ::Token ::OUTER )
44
+ end
45
+
38
46
tokens . consume ( SQLTree ::Token ::JOIN )
39
47
join . table_reference = SQLTree ::Node ::TableReference . parse ( tokens )
40
48
tokens . consume ( SQLTree ::Token ::ON )
Original file line number Diff line number Diff line change 99
99
# SQLTree["SELECT count(*) FROM jobs"].to_sql.should ==
100
100
# "SELECT count(*) FROM jobs"
101
101
# end
102
+
103
+ it "should parse and generate a LEFT OUTER JOIN query" do
104
+ SQLTree [ "SELECT * FROM table_a LEFT OUTER JOIN table_b ON foo = bar" ] . to_sql . should ==
105
+ "SELECT * FROM \" table_a\" LEFT OUTER JOIN \" table_b\" ON (\" foo\" = \" bar\" )"
106
+ end
102
107
end
Original file line number Diff line number Diff line change 73
73
it "should parse a table alias without AS" do
74
74
SQLTree ::Node ::Join [ 'LEFT JOIN table t ON other.field = table.field' ] . table_alias . should == 't'
75
75
end
76
+
77
+ it "should parse an outer join table" do
78
+ SQLTree ::Node ::Join [ 'LEFT OUTER JOIN table ON other.field = table.field' ] . table . should == 'table'
79
+ end
76
80
end
77
81
78
82
describe SQLTree ::Node ::Ordering do
You can’t perform that action at this time.
0 commit comments