sqlstmt 0.1.12 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/sqlstmt/query.rb +18 -6
  2. data/test/select.dt.rb +8 -1
  3. 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.push(table)
19
+ @tables << table
20
20
  self
21
21
  end
22
22
 
23
23
  def join(table, expr)
24
- @joins.push("JOIN #{table} ON #{expr}")
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.push("JOIN #{table} USING (#{fields.join(',')})")
35
+ @joins << ['JOIN', table, "USING (#{fields.join(',')})"]
30
36
  self
31
37
  end
32
38
 
33
39
  def left_join(table, expr)
34
- @joins.push("LEFT JOIN #{table} ON #{expr}")
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.push("LEFT JOIN #{table} USING (#{fields.join(',')})")
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
- " #{@joins.uniq.join(' ')}"
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
- sqlb = Select.new.table('source s').field('frog').no_where
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlstmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: