sqlite3 1.3.13 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,336 @@
1
+ require 'helper'
2
+
3
+ class TC_Integration_Aggregate < SQLite3::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new(":memory:")
6
+ @db.transaction do
7
+ @db.execute "create table foo ( a integer primary key, b text, c integer )"
8
+ @db.execute "insert into foo ( b, c ) values ( 'foo', 10 )"
9
+ @db.execute "insert into foo ( b, c ) values ( 'bar', 11 )"
10
+ @db.execute "insert into foo ( b, c ) values ( 'bar', 12 )"
11
+ end
12
+ end
13
+
14
+ def teardown
15
+ @db.close
16
+ end
17
+
18
+ def test_create_aggregate_without_block
19
+ step = proc do |ctx,a|
20
+ ctx[:sum] ||= 0
21
+ ctx[:sum] += a.to_i
22
+ end
23
+
24
+ final = proc { |ctx| ctx.result = ctx[:sum] }
25
+
26
+ @db.create_aggregate( "accumulate", 1, step, final )
27
+
28
+ value = @db.get_first_value( "select accumulate(a) from foo" )
29
+ assert_equal 6, value
30
+
31
+ # calling #get_first_value twice don't add up to the latest result
32
+ value = @db.get_first_value( "select accumulate(a) from foo" )
33
+ assert_equal 6, value
34
+ end
35
+
36
+ def test_create_aggregate_with_block
37
+ @db.create_aggregate( "accumulate", 1 ) do
38
+ step do |ctx,a|
39
+ ctx[:sum] ||= 0
40
+ ctx[:sum] += a.to_i
41
+ end
42
+
43
+ finalize { |ctx| ctx.result = ctx[:sum] }
44
+ end
45
+
46
+ value = @db.get_first_value( "select accumulate(a) from foo" )
47
+ assert_equal 6, value
48
+ end
49
+
50
+ def test_create_aggregate_with_group_by
51
+ @db.create_aggregate( "accumulate", 1 ) do
52
+ step do |ctx,a|
53
+ ctx[:sum] ||= 0
54
+ ctx[:sum] += a.to_i
55
+ end
56
+
57
+ finalize { |ctx| ctx.result = ctx[:sum] }
58
+ end
59
+
60
+ values = @db.execute( "select b, accumulate(c) from foo group by b order by b" )
61
+ assert_equal "bar", values[0][0]
62
+ assert_equal 23, values[0][1]
63
+ assert_equal "foo", values[1][0]
64
+ assert_equal 10, values[1][1]
65
+ end
66
+
67
+ def test_create_aggregate_with_the_same_function_twice_in_a_query
68
+ @db.create_aggregate( "accumulate", 1 ) do
69
+ step do |ctx,a|
70
+ ctx[:sum] ||= 0
71
+ ctx[:sum] += a.to_i
72
+ end
73
+
74
+ finalize { |ctx| ctx.result = ctx[:sum] }
75
+ end
76
+
77
+ values = @db.get_first_row( "select accumulate(a), accumulate(c) from foo" )
78
+ assert_equal 6, values[0]
79
+ assert_equal 33, values[1]
80
+ end
81
+
82
+ def test_create_aggregate_with_two_different_functions
83
+ @db.create_aggregate( "accumulate", 1 ) do
84
+ step do |ctx,a|
85
+ ctx[:sum] ||= 0
86
+ ctx[:sum] += a.to_i
87
+ end
88
+
89
+ finalize { |ctx| ctx.result = ctx[:sum] }
90
+ end
91
+
92
+ @db.create_aggregate( "multiply", 1 ) do
93
+ step do |ctx,a|
94
+ ctx[:sum] ||= 1
95
+ ctx[:sum] *= a.to_i
96
+ end
97
+
98
+ finalize { |ctx| ctx.result = ctx[:sum] }
99
+ end
100
+
101
+ GC.start
102
+
103
+ values = @db.get_first_row( "select accumulate(a), multiply(c) from foo" )
104
+ assert_equal 6, values[0]
105
+ assert_equal 1320, values[1]
106
+
107
+ value = @db.get_first_value( "select accumulate(c) from foo")
108
+ assert_equal 33, value
109
+
110
+ value = @db.get_first_value( "select multiply(a) from foo")
111
+ assert_equal 6, value
112
+ end
113
+
114
+ def test_create_aggregate_overwrite_function
115
+ @db.create_aggregate( "accumulate", 1 ) do
116
+ step do |ctx,a|
117
+ ctx[:sum] ||= 0
118
+ ctx[:sum] += a.to_i
119
+ end
120
+
121
+ finalize { |ctx| ctx.result = ctx[:sum] }
122
+ end
123
+
124
+ value = @db.get_first_value( "select accumulate(c) from foo")
125
+ assert_equal 33, value
126
+
127
+ GC.start
128
+
129
+ @db.create_aggregate( "accumulate", 1 ) do
130
+ step do |ctx,a|
131
+ ctx[:sum] ||= 1
132
+ ctx[:sum] *= a.to_i
133
+ end
134
+
135
+ finalize { |ctx| ctx.result = ctx[:sum] }
136
+ end
137
+
138
+ value = @db.get_first_value( "select accumulate(c) from foo")
139
+ assert_equal 1320, value
140
+ end
141
+
142
+ def test_create_aggregate_overwrite_function_with_different_arity
143
+ @db.create_aggregate( "accumulate", -1 ) do
144
+ step do |ctx,*args|
145
+ ctx[:sum] ||= 0
146
+ args.each { |a| ctx[:sum] += a.to_i }
147
+ end
148
+
149
+ finalize { |ctx| ctx.result = ctx[:sum] }
150
+ end
151
+
152
+ @db.create_aggregate( "accumulate", 2 ) do
153
+ step do |ctx,a,b|
154
+ ctx[:sum] ||= 1
155
+ ctx[:sum] *= (a.to_i + b.to_i)
156
+ end
157
+
158
+ finalize { |ctx| ctx.result = ctx[:sum] }
159
+ end
160
+
161
+ GC.start
162
+
163
+ values = @db.get_first_row( "select accumulate(c), accumulate(a,c) from foo")
164
+ assert_equal 33, values[0]
165
+ assert_equal 2145, values[1]
166
+ end
167
+
168
+ def test_create_aggregate_with_invalid_arity
169
+ assert_raise ArgumentError do
170
+ @db.create_aggregate( "accumulate", 1000 ) do
171
+ step {|ctx,*args| }
172
+ finalize { |ctx| }
173
+ end
174
+ end
175
+ end
176
+
177
+ class CustomException < Exception
178
+ end
179
+
180
+ def test_create_aggregate_with_exception_in_step
181
+ @db.create_aggregate( "raiseexception", 1 ) do
182
+ step do |ctx,a|
183
+ raise CustomException.new( "bogus aggregate handler" )
184
+ end
185
+
186
+ finalize { |ctx| ctx.result = 42 }
187
+ end
188
+
189
+ assert_raise CustomException do
190
+ @db.get_first_value( "select raiseexception(a) from foo")
191
+ end
192
+ end
193
+
194
+ def test_create_aggregate_with_exception_in_finalize
195
+ @db.create_aggregate( "raiseexception", 1 ) do
196
+ step do |ctx,a|
197
+ raise CustomException.new( "bogus aggregate handler" )
198
+ end
199
+
200
+ finalize do |ctx|
201
+ raise CustomException.new( "bogus aggregate handler" )
202
+ end
203
+ end
204
+
205
+ assert_raise CustomException do
206
+ @db.get_first_value( "select raiseexception(a) from foo")
207
+ end
208
+ end
209
+
210
+ def test_create_aggregate_with_no_data
211
+ @db.create_aggregate( "accumulate", 1 ) do
212
+ step do |ctx,a|
213
+ ctx[:sum] ||= 0
214
+ ctx[:sum] += a.to_i
215
+ end
216
+
217
+ finalize { |ctx| ctx.result = ctx[:sum] || 0 }
218
+ end
219
+
220
+ value = @db.get_first_value(
221
+ "select accumulate(a) from foo where a = 100" )
222
+ assert_equal 0, value
223
+ end
224
+
225
+ class AggregateHandler
226
+ class << self
227
+ def arity; 1; end
228
+ def text_rep; SQLite3::Constants::TextRep::ANY; end
229
+ def name; "multiply"; end
230
+ end
231
+ def step(ctx, a)
232
+ ctx[:buffer] ||= 1
233
+ ctx[:buffer] *= a.to_i
234
+ end
235
+ def finalize(ctx); ctx.result = ctx[:buffer]; end
236
+ end
237
+
238
+ def test_aggregate_initialized_twice
239
+ initialized = 0
240
+ handler = Class.new(AggregateHandler) do
241
+ define_method(:initialize) do
242
+ initialized += 1
243
+ super()
244
+ end
245
+ end
246
+
247
+ @db.create_aggregate_handler handler
248
+ @db.get_first_value( "select multiply(a) from foo" )
249
+ @db.get_first_value( "select multiply(a) from foo" )
250
+ assert_equal 2, initialized
251
+ end
252
+
253
+ def test_create_aggregate_handler_call_with_wrong_arity
254
+ @db.create_aggregate_handler AggregateHandler
255
+
256
+ assert_raise(SQLite3::SQLException) do
257
+ @db.get_first_value( "select multiply(a,c) from foo" )
258
+ end
259
+ end
260
+
261
+ class RaiseExceptionStepAggregateHandler
262
+ class << self
263
+ def arity; 1; end
264
+ def text_rep; SQLite3::Constants::TextRep::ANY; end
265
+ def name; "raiseexception"; end
266
+ end
267
+ def step(ctx, a)
268
+ raise CustomException.new( "bogus aggregate handler" )
269
+ end
270
+ def finalize(ctx); ctx.result = nil; end
271
+ end
272
+
273
+ def test_create_aggregate_handler_with_exception_step
274
+ @db.create_aggregate_handler RaiseExceptionStepAggregateHandler
275
+ assert_raise CustomException do
276
+ @db.get_first_value( "select raiseexception(a) from foo")
277
+ end
278
+ end
279
+
280
+ class RaiseExceptionNewAggregateHandler
281
+ class << self
282
+ def name; "raiseexception"; end
283
+ end
284
+ def initialize
285
+ raise CustomException.new( "bogus aggregate handler" )
286
+ end
287
+ def step(ctx, a); end
288
+ def finalize(ctx); ctx.result = nil; end
289
+ end
290
+
291
+ def test_create_aggregate_handler_with_exception_new
292
+ @db.create_aggregate_handler RaiseExceptionNewAggregateHandler
293
+ assert_raise CustomException do
294
+ @db.get_first_value( "select raiseexception(a) from foo")
295
+ end
296
+ end
297
+
298
+ def test_create_aggregate_handler
299
+ @db.create_aggregate_handler AggregateHandler
300
+ value = @db.get_first_value( "select multiply(a) from foo" )
301
+ assert_equal 6, value
302
+ end
303
+
304
+ class AccumulateAggregator
305
+ def step(*args)
306
+ @sum ||= 0
307
+ args.each { |a| @sum += a.to_i }
308
+ end
309
+
310
+ def finalize
311
+ @sum
312
+ end
313
+ end
314
+
315
+ class AccumulateAggregator2
316
+ def step(a, b)
317
+ @sum ||= 1
318
+ @sum *= (a.to_i + b.to_i)
319
+ end
320
+
321
+ def finalize
322
+ @sum
323
+ end
324
+ end
325
+
326
+ def test_define_aggregator_with_two_different_arities
327
+ @db.define_aggregator( "accumulate", AccumulateAggregator.new )
328
+ @db.define_aggregator( "accumulate", AccumulateAggregator2.new )
329
+
330
+ GC.start
331
+
332
+ values = @db.get_first_row( "select accumulate(c), accumulate(a,c) from foo")
333
+ assert_equal 33, values[0]
334
+ assert_equal 2145, values[1]
335
+ end
336
+ end
@@ -105,23 +105,6 @@ class TC_ResultSet < SQLite3::TestCase
105
105
  assert_equal hash[1], "foo"
106
106
  end
107
107
 
108
- def test_tainted_results_as_hash
109
- @db.results_as_hash = true
110
- @result.reset( 1 )
111
- row = @result.next
112
- row.each do |_, v|
113
- assert(v.tainted?) if String === v
114
- end
115
- end
116
-
117
- def test_tainted_row_values
118
- @result.reset( 1 )
119
- row = @result.next
120
- row.each do |v|
121
- assert(v.tainted?) if String === v
122
- end
123
- end
124
-
125
108
  def test_each
126
109
  called = 0
127
110
  @result.reset( 1, 2 )
@@ -119,7 +119,15 @@ module SQLite3
119
119
  assert_equal [nil], result
120
120
  end
121
121
 
122
- def test_bind_blobs
122
+ def test_bind_blob
123
+ @db.execute('create table foo(text BLOB)')
124
+ stmt = SQLite3::Statement.new(@db, 'insert into foo(text) values (?)')
125
+ stmt.bind_param(1, SQLite3::Blob.new('hello'))
126
+ stmt.execute
127
+ row = @db.execute('select * from foo')
128
+
129
+ assert_equal ['hello'], row.first
130
+ assert_equal row.first.types, ['BLOB']
123
131
  end
124
132
 
125
133
  def test_bind_64
@@ -190,11 +198,6 @@ module SQLite3
190
198
  assert_equal ['foo'], r
191
199
  end
192
200
 
193
- def test_tainted
194
- r = @stmt.step
195
- assert r.first.tainted?
196
- end
197
-
198
201
  def test_step_twice
199
202
  assert_not_nil @stmt.step
200
203
  assert !@stmt.done?
@@ -216,7 +219,7 @@ module SQLite3
216
219
 
217
220
  def test_column_name
218
221
  assert_equal "'foo'", @stmt.column_name(0)
219
- assert_equal nil, @stmt.column_name(10)
222
+ assert_nil @stmt.column_name(10)
220
223
  end
221
224
 
222
225
  def test_bind_parameter_count
@@ -243,7 +246,7 @@ module SQLite3
243
246
  assert stmt.execute('employee-2')
244
247
  end
245
248
 
246
- def test_clear_bindings
249
+ def test_clear_bindings!
247
250
  stmt = @db.prepare('select ?, ?')
248
251
  stmt.bind_param 1, "foo"
249
252
  stmt.bind_param 2, "bar"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.13
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamis Buck
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-01-04 00:00:00.000000000 Z
13
+ date: 2019-12-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -18,72 +18,72 @@ dependencies:
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: '5.9'
21
+ version: '5.13'
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: '5.9'
28
+ version: '5.13'
29
29
  - !ruby/object:Gem::Dependency
30
- name: rdoc
30
+ name: rake-compiler
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - "~>"
34
34
  - !ruby/object:Gem::Version
35
- version: '4.0'
35
+ version: '1.0'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - "~>"
41
41
  - !ruby/object:Gem::Version
42
- version: '4.0'
42
+ version: '1.0'
43
43
  - !ruby/object:Gem::Dependency
44
- name: rake-compiler
44
+ name: rake-compiler-dock
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: 0.9.3
49
+ version: 0.6.0
50
50
  type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
- version: 0.9.3
56
+ version: 0.6.0
57
57
  - !ruby/object:Gem::Dependency
58
- name: rake-compiler-dock
58
+ name: mini_portile
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - "~>"
62
62
  - !ruby/object:Gem::Version
63
- version: 0.5.2
63
+ version: 0.6.2
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - "~>"
69
69
  - !ruby/object:Gem::Version
70
- version: 0.5.2
70
+ version: 0.6.2
71
71
  - !ruby/object:Gem::Dependency
72
- name: mini_portile
72
+ name: hoe-bundler
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - "~>"
76
76
  - !ruby/object:Gem::Version
77
- version: 0.6.2
77
+ version: '1.0'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - "~>"
83
83
  - !ruby/object:Gem::Version
84
- version: 0.6.2
84
+ version: '1.0'
85
85
  - !ruby/object:Gem::Dependency
86
- name: hoe-bundler
86
+ name: hoe-gemspec
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - "~>"
@@ -96,20 +96,40 @@ dependencies:
96
96
  - - "~>"
97
97
  - !ruby/object:Gem::Version
98
98
  version: '1.0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rdoc
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '4.0'
106
+ - - "<"
107
+ - !ruby/object:Gem::Version
108
+ version: '7'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '4.0'
116
+ - - "<"
117
+ - !ruby/object:Gem::Version
118
+ version: '7'
99
119
  - !ruby/object:Gem::Dependency
100
120
  name: hoe
101
121
  requirement: !ruby/object:Gem::Requirement
102
122
  requirements:
103
123
  - - "~>"
104
124
  - !ruby/object:Gem::Version
105
- version: '3.15'
125
+ version: '3.20'
106
126
  type: :development
107
127
  prerelease: false
108
128
  version_requirements: !ruby/object:Gem::Requirement
109
129
  requirements:
110
130
  - - "~>"
111
131
  - !ruby/object:Gem::Version
112
- version: '3.15'
132
+ version: '3.20'
113
133
  description: |-
114
134
  This module allows Ruby programs to interface with the SQLite3
115
135
  database engine (http://www.sqlite.org). You must have the
@@ -128,12 +148,15 @@ extra_rdoc_files:
128
148
  - CHANGELOG.rdoc
129
149
  - Manifest.txt
130
150
  - README.rdoc
151
+ - ext/sqlite3/aggregator.c
131
152
  - ext/sqlite3/backup.c
132
153
  - ext/sqlite3/database.c
133
154
  - ext/sqlite3/exception.c
134
155
  - ext/sqlite3/sqlite3.c
135
156
  - ext/sqlite3/statement.c
136
157
  files:
158
+ - ".gemtest"
159
+ - ".travis.yml"
137
160
  - API_CHANGES.rdoc
138
161
  - CHANGELOG.rdoc
139
162
  - ChangeLog.cvs
@@ -142,6 +165,9 @@ files:
142
165
  - Manifest.txt
143
166
  - README.rdoc
144
167
  - Rakefile
168
+ - appveyor.yml
169
+ - ext/sqlite3/aggregator.c
170
+ - ext/sqlite3/aggregator.h
145
171
  - ext/sqlite3/backup.c
146
172
  - ext/sqlite3/backup.h
147
173
  - ext/sqlite3/database.c
@@ -165,19 +191,22 @@ files:
165
191
  - lib/sqlite3/translator.rb
166
192
  - lib/sqlite3/value.rb
167
193
  - lib/sqlite3/version.rb
194
+ - rakelib/faq.rake
195
+ - rakelib/gem.rake
196
+ - rakelib/native.rake
197
+ - rakelib/vendor_sqlite3.rake
168
198
  - setup.rb
169
- - tasks/faq.rake
170
- - tasks/gem.rake
171
- - tasks/native.rake
172
- - tasks/vendor_sqlite3.rake
173
199
  - test/helper.rb
174
200
  - test/test_backup.rb
175
201
  - test/test_collation.rb
176
202
  - test/test_database.rb
203
+ - test/test_database_flags.rb
177
204
  - test/test_database_readonly.rb
205
+ - test/test_database_readwrite.rb
178
206
  - test/test_deprecated.rb
179
207
  - test/test_encoding.rb
180
208
  - test/test_integration.rb
209
+ - test/test_integration_aggregate.rb
181
210
  - test/test_integration_open_close.rb
182
211
  - test/test_integration_pending.rb
183
212
  - test/test_integration_resultset.rb
@@ -188,8 +217,9 @@ files:
188
217
  - test/test_statement_execute.rb
189
218
  homepage: https://github.com/sparklemotion/sqlite3-ruby
190
219
  licenses:
191
- - BSD-3
192
- metadata: {}
220
+ - BSD-3-Clause
221
+ metadata:
222
+ msys2_mingw_dependencies: sqlite3
193
223
  post_install_message:
194
224
  rdoc_options:
195
225
  - "--main"
@@ -207,8 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
237
  - !ruby/object:Gem::Version
208
238
  version: 1.3.5
209
239
  requirements: []
210
- rubyforge_project:
211
- rubygems_version: 2.6.8
240
+ rubygems_version: 3.0.3
212
241
  signing_key:
213
242
  specification_version: 4
214
243
  summary: This module allows Ruby programs to interface with the SQLite3 database engine