sqlstmt 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98430baa411f71f5730952f97c9ccffda3872ee4
4
- data.tar.gz: 3fd532738e1c3f4d337e21c9cabaf1c2bb0286af
3
+ metadata.gz: 00e1c0b2d84d706f5c31a73452924c121058bfa2
4
+ data.tar.gz: cf52152357734f73abc182e07b03110324bbf7cf
5
5
  SHA512:
6
- metadata.gz: fc948dcf7eda79d0eb54f168c47f74bb2a920f03b38e0adddb99aa7b1a9b09e8ee799e6ae2ce4aa679c086aec3d3f5a4064e1fdff60342dc49156e1eaa169d4e
7
- data.tar.gz: cc247caaa97d7df7195533f8fcba869381c972c4a40521de4528c61d25c93c7f5a8500e47347e49c1b7c255ce76ce8497f5127a348dccbf64c62c364e805b97c
6
+ metadata.gz: 6af961b7e0b6415fd0716f0c50f6d63246bd7875dcd5d656ba48e98d1c1a801fda67feba9c29685254bc918bac2916332acf66b34f98b8c503797a17e55c2290
7
+ data.tar.gz: 130e2e8808466ef222ba23e6831db9b9407fda5b78e3b06d0a6e50022a18b50990c5ed0ef0290f4f1b76e3b14c3fca604c2bacf912655b44617c6c241e083478
@@ -3,6 +3,7 @@ require 'sqlstmt/to_sql'
3
3
 
4
4
  class SqlStmt
5
5
  attr_reader :fields, :tables, :joins, :wheres
6
+ Table = Struct.new(:str, :name, :alias, :index)
6
7
 
7
8
  def initialize
8
9
  @stmt_type = nil
@@ -68,8 +69,10 @@ class SqlStmt
68
69
 
69
70
  ###### common operations
70
71
 
71
- def table(table)
72
- @tables << table
72
+ def table(table_str, use_index = nil)
73
+ parts = table_str.split(' ')
74
+ table_obj = Table.new(table_str, parts[0], parts[1], use_index)
75
+ @tables << table_obj
73
76
  return self
74
77
  end
75
78
 
@@ -184,7 +187,7 @@ class SqlStmt
184
187
 
185
188
  ###### methods to analyze what the statement contains
186
189
  def includes_table?(table_to_find)
187
- retval = @tables.find { |table| table_names_match?(table, table_to_find) }
190
+ retval = @tables.find { |table| (table.name == table_to_find) || (table.alias == table_to_find) }
188
191
  retval ||= @joins.find { |_, table, _| table_names_match?(table, table_to_find) }
189
192
  return retval
190
193
  end
@@ -314,8 +317,15 @@ private
314
317
  return set_exprs.join(', ')
315
318
  end
316
319
 
320
+ def table_to_str(table)
321
+ if table.index
322
+ return "#{table.str} USE INDEX (#{table.index})"
323
+ end
324
+ return table.str
325
+ end
326
+
317
327
  def build_table_list
318
- return @tables.join(',')
328
+ return @tables.map {|table| table_to_str(table) }.join(',')
319
329
  end
320
330
 
321
331
  def simple_clause(keywords, value)
data/test/select_test.rb CHANGED
@@ -8,6 +8,12 @@ class TestSelect < Minitest::Test
8
8
  assert(!sqlb.includes_table?('blah'))
9
9
  end
10
10
 
11
+ def test_tables
12
+ assert_equal('SELECT blah FROM target', SqlStmt.new.select.table('target').no_where.get('blah').to_sql)
13
+ assert_equal('SELECT t.blah FROM target t', SqlStmt.new.select.table('target t').no_where.get('t.blah').to_sql)
14
+ assert_equal('SELECT t.blah FROM target t USE INDEX (blee)', SqlStmt.new.select.table('target t', 'blee').no_where.get('t.blah').to_sql)
15
+ end
16
+
11
17
  def test_minimum_requirements
12
18
  assert_raises(SqlStmtError) { SqlStmt.new.select.table('target').to_s }
13
19
  assert_raises(SqlStmtError) { SqlStmt.new.select.table('target').no_where.to_s }
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.2.5
4
+ version: 0.2.6
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: 2016-02-12 00:00:00.000000000 Z
12
+ date: 2016-07-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: build SQL statements in a modular fashion, one piece at a time; only
15
15
  used/tested with MySQL so far