sql-maker 0.0.1 → 0.0.2

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: 913d95f6a4b16c768820651f43606bad4a1e6d48
4
- data.tar.gz: a9a05ffd8835ac0626bfc69f440df1f666f25c8e
3
+ metadata.gz: 32ac24cfad827c72f15a9d677d0c7ea6a68317a2
4
+ data.tar.gz: 51e81c781768b04148522f9408777dd532b4c62b
5
5
  SHA512:
6
- metadata.gz: aeb4c999c4a6482778cf33c1a4434c6e3bbcfd9562e14c07873a5da7dd700dbe0d380d0b0bd223df094ce5e1463a36ad9db2d9e59ef953bbf67ca6f9cba61d19
7
- data.tar.gz: ce05861fe69d9aa7557b1b0491a5f2191ed711ad2435467a9b2edc1debee6a98e17f4845c68bfb7400eae6f8014c48654675ef1400100b0fe49c9ce120763968
6
+ metadata.gz: 029d20d44324f0e04c82a68556301dfb31b18e09c3f2e633e9e00f3ab450dddccd8b04fb715af57e489d5bdb2aa81b5dc32980484037170cc2b6d9dfc381965b
7
+ data.tar.gz: 30e4544a8d797d4b36dd0bde3bd6d342ce9b1d7ca9e63c510930b5d97d5aef298a5f92752d49ac877879f82eacbf86ae1fe0bc5c840f05ca04401e6139961b85
@@ -1,3 +1,11 @@
1
+ ## 0.0.2 (2014/06/18)
2
+
3
+ Enhancements:
4
+
5
+ * Add default :quote_char, :name_sep for SQL::Maker::Select, and SQL::Maker::Condition
6
+ * Add auto_bind option to SQL::Maker::Condition
7
+ * Inherit SQL::Maker::Condition parameters on creating sub conditions
8
+
1
9
  ## 0.0.1 (2014/06/18)
2
10
 
3
11
  First version
@@ -20,7 +20,7 @@ class SQL::Maker
20
20
  end
21
21
  unless @quote_char = args[:quote_char]
22
22
  @quote_char =
23
- if driver.to_s == 'mysql'
23
+ if @driver == 'mysql'
24
24
  %q{`}
25
25
  else
26
26
  %q{"}
@@ -28,7 +28,7 @@ class SQL::Maker
28
28
  end
29
29
  @select_class = @driver == 'oracle' ? SQL::Maker::Select::Oracle : SQL::Maker::Select
30
30
 
31
- @name_sep = args[:name_sep] || ','
31
+ @name_sep = args[:name_sep] || '.'
32
32
  @new_line = args[:new_line] || "\n"
33
33
  @strict = args[:strict] || false
34
34
  @auto_bind = args[:auto_bind] || false # apply client-side prepared statement binding autocatically
@@ -3,14 +3,25 @@ require 'sql/query_maker'
3
3
 
4
4
  class SQL::Maker::Condition
5
5
  include SQL::Maker::Util
6
- attr_accessor :sql, :bind, :strict, :name_sep, :quote_char
6
+ attr_accessor :quote_char, :name_sep, :strict, :auto_bind
7
+ attr_accessor :sql, :bind
7
8
 
8
9
  def initialize(args = {})
10
+ @quote_char = args[:quote_char] || ''
11
+ @name_sep = args[:name_sep] || '.'
12
+ @strict = args[:strict] || false
13
+ @auto_bind = args[:auto_bind] || false
14
+
9
15
  @sql = args[:sql] || []
10
16
  @bind = args[:bind] || []
11
- @strict = args[:strict].nil? ? false : args[:strict]
12
- @name_sep = args[:name_sep] || ''
13
- @quote_char = args[:quote_char] || ''
17
+ end
18
+
19
+ def new_condition(args = {})
20
+ SQL::Maker::Condition.new({
21
+ :quote_char => self.quote_char,
22
+ :name_sep => self.name_sep,
23
+ :strict => self.strict,
24
+ }.merge(args))
14
25
  end
15
26
 
16
27
  def &(other)
@@ -125,21 +136,21 @@ class SQL::Maker::Condition
125
136
  def compose_and(other)
126
137
  if self.sql.empty?
127
138
  if other.sql.empty?
128
- return SQL::Maker::Condition.new
139
+ return new_condition
129
140
  end
130
- return SQL::Maker::Condition.new(
141
+ return new_condition(
131
142
  :sql => ['(' + other.as_sql() + ')'],
132
143
  :bind => other.bind,
133
144
  )
134
145
  end
135
146
  if other.sql.empty?
136
- return SQL::Maker::Condition.new(
147
+ return new_condition(
137
148
  :sql => ['(' + self.as_sql() + ')'],
138
149
  :bind => self.bind,
139
150
  )
140
151
  end
141
152
 
142
- return SQL::Maker::Condition.new(
153
+ return new_condition(
143
154
  :sql => ['(' + self.as_sql() + ') AND (' + other.as_sql() + ')'],
144
155
  :bind => self.bind + other.bind,
145
156
  )
@@ -148,15 +159,15 @@ class SQL::Maker::Condition
148
159
  def compose_or(other)
149
160
  if self.sql.empty?
150
161
  if other.sql.empty?
151
- return SQL::Maker::Condition.new
162
+ return new_condition
152
163
  end
153
- return SQL::Maker::Condition.new(
164
+ return new_condition(
154
165
  :sql => ['(' + other.as_sql() + ')'],
155
166
  :bind => other.bind,
156
167
  )
157
168
  end
158
169
  if other.sql.empty?
159
- return SQL::Maker::Condition.new(
170
+ return new_condition(
160
171
  :sql => ['(' + self.as_sql() + ')'],
161
172
  :bind => self.bind,
162
173
  )
@@ -164,16 +175,16 @@ class SQL::Maker::Condition
164
175
 
165
176
  # return value is enclosed with '()'.
166
177
  # because 'OR' operator priority less than 'AND'.
167
- return SQL::Maker::Condition.new(
178
+ return new_condition(
168
179
  :sql => ['((' + self.as_sql() + ') OR (' + other.as_sql() + '))'],
169
180
  :bind => self.bind + other.bind,
170
181
  )
171
182
  end
172
183
 
173
184
  def as_sql
174
- self.sql.join(' AND ')
185
+ sql = self.sql.join(' AND ')
186
+ @auto_bind ? bind_param(sql, self.bind) : sql
175
187
  end
176
- alias_method :to_s, :as_sql
177
188
  end
178
189
 
179
190
  __END__
@@ -9,6 +9,12 @@ class SQL::Maker::Select
9
9
  :index_hint, :group_by, :order_by, :where, :having, :for_update, :subqueries
10
10
 
11
11
  def initialize(args = {})
12
+ @quote_char = args[:quote_char] || ''
13
+ @name_sep = args[:name_sep] || '.'
14
+ @new_line = args[:new_line] || "\n"
15
+ @strict = args[:strict] || false
16
+ @auto_bind = args[:auto_bind] || false
17
+
12
18
  @select = args[:select] || []
13
19
  @distinct = args[:distinct] || false
14
20
  @select_map = args[:select_map] || {}
@@ -19,16 +25,11 @@ class SQL::Maker::Select
19
25
  @group_by = args[:group_by] || []
20
26
  @order_by = args[:order_by] || []
21
27
  @prefix = args[:prefix] || 'SELECT '
22
- @new_line = args[:new_line] || "\n"
23
- @strict = args[:strict] || false
24
- @auto_bind = args[:auto_bind] || false
25
28
  @where = args[:where]
26
29
  @having = args[:having]
27
30
  @limit = args[:limit]
28
31
  @offset = args[:offset]
29
32
  @for_update = args[:for_update]
30
- @quote_char = args[:quote_char]
31
- @name_sep = args[:name_sep]
32
33
  @subqueries = []
33
34
  end
34
35
 
@@ -60,3 +60,25 @@ describe 'SQL::Maker' do
60
60
  end
61
61
  end
62
62
  end
63
+
64
+ describe 'SQL::Maker::Select' do
65
+ let(:builder) { SQL::Maker::Select.new(:auto_bind => true, :new_line => ' ') }
66
+
67
+ it 'auto bind' do
68
+ sql = builder
69
+ .add_select('foo')
70
+ .add_from('table')
71
+ .add_where('hoge' => "t' OR 't' = 't")
72
+ .as_sql
73
+ expect(sql).to be == %q{SELECT foo FROM table WHERE (hoge = 't'' OR ''t'' = ''t')}
74
+ end
75
+ end
76
+
77
+ describe 'SQL::Maker::Condition' do
78
+ let(:builder) { SQL::Maker::Condition.new(:auto_bind => true) }
79
+
80
+ it 'auto bind' do
81
+ sql = builder.add('hoge' => "t' OR 't' = 't").as_sql
82
+ expect(sql).to be == %q{(hoge = 't'' OR ''t'' = ''t')}
83
+ end
84
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "sql-maker"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["Naotoshi Seo"]
9
9
  spec.email = ["sonots@gmail.com"]
10
10
  spec.summary = %q{SQL Builder for Ruby}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql-maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo