sqlstmt 0.1.24 → 0.1.25

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3186df18978412482910248da3c0132f6a5e0ac
4
- data.tar.gz: 43eda0436e5ffa469e9bd2abe2afe04e71d83951
3
+ metadata.gz: f5d47a52dd9ccf4606743734009c9f8ecce7d074
4
+ data.tar.gz: 672c9d20b75f2890796e487856dda30a82096415
5
5
  SHA512:
6
- metadata.gz: a8ece4b732299a6c16621aa34455353ae174d74b25ddd74c4fb7d39da7a5c4f4123a8fae4287d057942c5d3a506b47e89419f664e2b176136ebf57fbaa00d8b6
7
- data.tar.gz: ad04c575e78642c57687cbd77c11dfd77fc0dacbbb0775d29d5c7ef3bf9655cea1dbbc2136ca65038dc5749e66ec1e4cb34a03b3284de11a2f54f55e922fb990
6
+ metadata.gz: c45f5c3c9f4adbf18a9dddea300ff897028fb982697d7e89883cdfd90e1812b335638c55602ec30d85bcd2bcc7d04d7b26c6c1c310cba6deffe3dd716d3863a5
7
+ data.tar.gz: acb4c4d744a1d01da1ae6db10880e3b42a0d9d3f9226e60c904d80c448c7e006aada0018d6d174eef7025cdc3a0d7184c57fced07a6ce58638a8af0c9c25424a
@@ -7,7 +7,6 @@ class FromQuery < Query
7
7
  super
8
8
  @group_by = nil
9
9
  @order_by = nil
10
- @limit = nil
11
10
  @having = []
12
11
  end
13
12
 
@@ -21,11 +20,6 @@ class FromQuery < Query
21
20
  self
22
21
  end
23
22
 
24
- def limit(clause)
25
- @limit = clause
26
- self
27
- end
28
-
29
23
  def having(*sql)
30
24
  @having.concat(sql)
31
25
  self
@@ -38,10 +32,6 @@ private
38
32
  raise SqlStmt::Error, "unable to build sql - must call :table if using :join (or one if it's variants)" if @tables.empty? && !@joins.empty?
39
33
  end
40
34
 
41
- def simple_clause(keywords, value)
42
- if value then " #{keywords} #{value}" else '' end
43
- end
44
-
45
35
  def having_clause
46
36
  if @having.empty?
47
37
  ''
data/lib/sqlstmt/query.rb CHANGED
@@ -13,6 +13,7 @@ class Query
13
13
  @joins = []
14
14
  @wheres = []
15
15
  @where_behavior = :require
16
+ @limit = nil
16
17
  end
17
18
 
18
19
  def table(table)
@@ -31,21 +32,11 @@ class Query
31
32
  end
32
33
  end
33
34
 
34
- def join_using(table, *fields)
35
- @joins << ['JOIN', table, "USING (#{fields.join(',')})"]
36
- self
37
- end
38
-
39
35
  def left_join(table, *exprs)
40
36
  @joins << ['LEFT JOIN', table, "ON #{exprs.join(' AND ')}"]
41
37
  self
42
38
  end
43
39
 
44
- def left_join_using(table, *fields)
45
- @joins << ['LEFT JOIN', table, "USING (#{fields.join(',')})"]
46
- self
47
- end
48
-
49
40
  def where(*sql)
50
41
  @wheres.concat(sql)
51
42
  self
@@ -61,6 +52,11 @@ class Query
61
52
  self
62
53
  end
63
54
 
55
+ def limit(clause)
56
+ @limit = clause
57
+ self
58
+ end
59
+
64
60
  def to_s
65
61
  verify_minimum_requirements
66
62
  build_stmt
@@ -94,6 +90,10 @@ private
94
90
  @tables.join(',')
95
91
  end
96
92
 
93
+ def simple_clause(keywords, value)
94
+ if value then " #{keywords} #{value}" else '' end
95
+ end
96
+
97
97
  def build_join_clause
98
98
  if @joins.empty?
99
99
  ''
@@ -28,7 +28,8 @@ private
28
28
  end
29
29
 
30
30
  def build_stmt
31
- "UPDATE #{build_table_list}#{build_join_clause} SET #{build_set_clause}#{build_where_clause}"
31
+ limit_clause = simple_clause('LIMIT', @limit)
32
+ "UPDATE #{build_table_list}#{build_join_clause} SET #{build_set_clause}#{build_where_clause}#{limit_clause}"
32
33
  end
33
34
  end
34
35
 
data/test/select.dt.rb CHANGED
@@ -23,24 +23,22 @@ class TestSelect < DohTest::TestGroup
23
23
  assert_equal('SELECT blah FROM source WHERE source_id = 1', Select.new.table('source').field('blah').where('source_id = 1').to_s)
24
24
  assert_equal('SELECT blah FROM source s', Select.new.table('source s').field('blah').no_where.to_s)
25
25
  assert_equal('SELECT blah FROM source s JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
26
- assert_equal('SELECT blah FROM source s JOIN other o USING (blah_id)', Select.new.table('source s').join_using('other o', 'blah_id').field('blah').no_where.to_s)
27
26
  assert_equal('SELECT blah FROM source s LEFT JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').left_join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
28
- assert_equal('SELECT blah FROM source s LEFT JOIN other o USING (blah_id)', Select.new.table('source s').left_join_using('other o', 'blah_id').field('blah').no_where.to_s)
29
27
  assert_equal('SELECT blah,blee FROM source', Select.new.table('source').field('blah','blee').no_where.to_s)
30
28
  assert_equal('SELECT blah FROM source HAVING blee > 0', Select.new.table('source').field('blah').no_where.having('blee > 0').to_s)
31
29
  end
32
30
 
33
31
  def test_duplicate_joins
34
- base_sqlb = Select.new.table('source s').field('frog').no_where
35
-
36
- sqlb = base_sqlb.dup
32
+ sqlb = Select.new.table('source s').field('frog').no_where
37
33
  4.times { sqlb.join('other o', 's.blah_id = o.blah_id') }
38
- sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
39
34
  assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)
35
+ end
40
36
 
41
- sqlb = base_sqlb.dup
37
+ def test_optional_join
38
+ sqlb = Select.new.table('source s').field('frog').no_where
39
+ sqlb.join('other o', 's.blah_id = o.blah_id')
42
40
  sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
43
- assert_equal('SELECT frog FROM source s JOIN other o ON z.blee_id = o.blee_id', sqlb.to_s)
41
+ assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)
44
42
  end
45
43
 
46
44
  def test_join_with_multiple_conditions
data/test/update.dt.rb CHANGED
@@ -10,6 +10,7 @@ class TestUpdate < DohTest::TestGroup
10
10
 
11
11
  def test_simple
12
12
  assert_equal('UPDATE target SET blah = blee', Update.new.table('target').field('blah', 'blee').no_where.to_s)
13
+ assert_equal('UPDATE target SET blah = blee LIMIT 3', Update.new.table('target').field('blah', 'blee').no_where.limit(3).to_s)
13
14
  assert_equal('UPDATE target SET blah = blee WHERE target_id = 1', Update.new.table('target').field('blah', 'blee').where('target_id = 1').to_s)
14
15
  end
15
16
 
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.24
4
+ version: 0.1.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Makani Mason
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-25 00:00:00.000000000 Z
12
+ date: 2014-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dohutil
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  version: '0'
86
86
  requirements: []
87
87
  rubyforge_project:
88
- rubygems_version: 2.0.6
88
+ rubygems_version: 2.0.14
89
89
  signing_key:
90
90
  specification_version: 4
91
91
  summary: build SQL statements in a modular fashion, one piece at a time