sqlstmt 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sqlstmt/query.rb +18 -6
- data/test/select.dt.rb +8 -1
- metadata +1 -1
data/lib/sqlstmt/query.rb
CHANGED
@@ -16,27 +16,33 @@ class Query
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def table(table)
|
19
|
-
@tables
|
19
|
+
@tables << table
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
23
|
def join(table, expr)
|
24
|
-
@joins
|
24
|
+
@joins << ['JOIN', table, "ON #{expr}"]
|
25
25
|
self
|
26
26
|
end
|
27
27
|
|
28
|
+
def optional_join(table, expr)
|
29
|
+
unless find_join(table)
|
30
|
+
join(table, expr)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
28
34
|
def join_using(table, *fields)
|
29
|
-
@joins
|
35
|
+
@joins << ['JOIN', table, "USING (#{fields.join(',')})"]
|
30
36
|
self
|
31
37
|
end
|
32
38
|
|
33
39
|
def left_join(table, expr)
|
34
|
-
@joins
|
40
|
+
@joins << ['LEFT JOIN', table, "ON #{expr}"]
|
35
41
|
self
|
36
42
|
end
|
37
43
|
|
38
44
|
def left_join_using(table, *fields)
|
39
|
-
@joins
|
45
|
+
@joins << ['LEFT JOIN', table, "USING (#{fields.join(',')})"]
|
40
46
|
self
|
41
47
|
end
|
42
48
|
|
@@ -61,6 +67,12 @@ class Query
|
|
61
67
|
end
|
62
68
|
|
63
69
|
private
|
70
|
+
def find_join(table_to_find)
|
71
|
+
@joins.find do |_, table, _|
|
72
|
+
table_to_find == table
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
64
76
|
def verify_minimum_requirements
|
65
77
|
if (@where_behavior == :require) && @wheres.empty?
|
66
78
|
raise SqlStmt::Error, "unable to build sql - must call :where, :no_where, or :optional_where"
|
@@ -77,7 +89,7 @@ private
|
|
77
89
|
if @joins.empty?
|
78
90
|
''
|
79
91
|
else
|
80
|
-
|
92
|
+
' ' + @joins.collect{|ary| ary.join(' ')}.uniq.join(' ')
|
81
93
|
end
|
82
94
|
end
|
83
95
|
|
data/test/select.dt.rb
CHANGED
@@ -23,9 +23,16 @@ class TestSelect < DohTest::TestGroup
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_duplicate_joins
|
26
|
-
|
26
|
+
base_sqlb = Select.new.table('source s').field('frog').no_where
|
27
|
+
|
28
|
+
sqlb = base_sqlb.dup
|
27
29
|
4.times { sqlb.join('other o', 's.blah_id = o.blah_id') }
|
30
|
+
sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
|
28
31
|
assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)
|
32
|
+
|
33
|
+
sqlb = base_sqlb.dup
|
34
|
+
sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
|
35
|
+
assert_equal('SELECT frog FROM source s JOIN other o ON z.blee_id = o.blee_id', sqlb.to_s)
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|