sqlite3-ruby 1.3.0.beta.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/API_CHANGES.rdoc +48 -0
  2. data/CHANGELOG.rdoc +108 -0
  3. data/ChangeLog.cvs +88 -0
  4. data/LICENSE +27 -0
  5. data/Manifest.txt +44 -0
  6. data/README.rdoc +54 -0
  7. data/Rakefile +10 -0
  8. data/ext/sqlite3/database.c +693 -0
  9. data/ext/sqlite3/database.h +15 -0
  10. data/ext/sqlite3/exception.c +94 -0
  11. data/ext/sqlite3/exception.h +8 -0
  12. data/ext/sqlite3/extconf.rb +39 -0
  13. data/ext/sqlite3/sqlite3.c +33 -0
  14. data/ext/sqlite3/sqlite3_ruby.h +43 -0
  15. data/ext/sqlite3/statement.c +419 -0
  16. data/ext/sqlite3/statement.h +16 -0
  17. data/faq/faq.rb +145 -0
  18. data/faq/faq.yml +426 -0
  19. data/lib/sqlite3.rb +10 -0
  20. data/lib/sqlite3/1.8/sqlite3_native.so +0 -0
  21. data/lib/sqlite3/1.9/sqlite3_native.so +0 -0
  22. data/lib/sqlite3/constants.rb +49 -0
  23. data/lib/sqlite3/database.rb +568 -0
  24. data/lib/sqlite3/errors.rb +44 -0
  25. data/lib/sqlite3/pragmas.rb +280 -0
  26. data/lib/sqlite3/resultset.rb +126 -0
  27. data/lib/sqlite3/statement.rb +146 -0
  28. data/lib/sqlite3/translator.rb +114 -0
  29. data/lib/sqlite3/value.rb +57 -0
  30. data/lib/sqlite3/version.rb +16 -0
  31. data/setup.rb +1333 -0
  32. data/tasks/faq.rake +9 -0
  33. data/tasks/gem.rake +31 -0
  34. data/tasks/native.rake +31 -0
  35. data/tasks/vendor_sqlite3.rake +107 -0
  36. data/test/helper.rb +3 -0
  37. data/test/test_database.rb +291 -0
  38. data/test/test_deprecated.rb +25 -0
  39. data/test/test_encoding.rb +115 -0
  40. data/test/test_integration.rb +545 -0
  41. data/test/test_integration_open_close.rb +30 -0
  42. data/test/test_integration_pending.rb +113 -0
  43. data/test/test_integration_resultset.rb +183 -0
  44. data/test/test_integration_statement.rb +194 -0
  45. data/test/test_sqlite3.rb +9 -0
  46. data/test/test_statement.rb +207 -0
  47. metadata +181 -0
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ module SQLite3
4
+ class TestDeprecated < Test::Unit::TestCase
5
+ def setup
6
+ @db = SQLite3::Database.new(':memory:')
7
+ end
8
+
9
+ def test_query_with_many_bind_params
10
+ assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a
11
+ end
12
+
13
+ def test_query_with_nil_bind_params
14
+ assert_equal [['foo']], @db.query("select 'foo'", nil).to_a
15
+ end
16
+
17
+ def test_execute_with_many_bind_params
18
+ assert_equal [[nil, 1]], @db.execute("select ?, ?", nil, 1)
19
+ end
20
+
21
+ def test_execute_with_nil_bind_params
22
+ assert_equal [['foo']], @db.execute("select 'foo'", nil)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,115 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'helper'
4
+
5
+ module SQLite3
6
+ class TestEncoding < Test::Unit::TestCase
7
+ def setup
8
+ @db = SQLite3::Database.new(':memory:')
9
+ @create = "create table ex(id int, data string)"
10
+ @insert = "insert into ex(id, data) values (?, ?)"
11
+ @db.execute(@create);
12
+ end
13
+
14
+ def test_default_internal_is_honored
15
+ before_enc = Encoding.default_internal
16
+
17
+ str = "壁に耳あり、障子に目あり"
18
+ stmt = @db.prepare('insert into ex(data) values (?)')
19
+ stmt.bind_param 1, str
20
+ stmt.step
21
+
22
+ Encoding.default_internal = 'EUC-JP'
23
+ string = @db.execute('select data from ex').first.first
24
+
25
+ assert_equal Encoding.default_internal, string.encoding
26
+ assert_equal str.encode('EUC-JP'), string
27
+ assert_equal str, string.encode(str.encoding)
28
+ ensure
29
+ Encoding.default_internal = before_enc
30
+ end
31
+
32
+ def test_blob_is_binary
33
+ str = "猫舌"
34
+ @db.execute('create table foo(data text)')
35
+ stmt = @db.prepare('insert into foo(data) values (?)')
36
+ stmt.bind_param(1, SQLite3::Blob.new(str))
37
+ stmt.step
38
+
39
+ string = @db.execute('select data from foo').first.first
40
+ assert_equal Encoding.find('ASCII-8BIT'), string.encoding
41
+ assert_equal str, string.force_encoding('UTF-8')
42
+ end
43
+
44
+ def test_blob_is_ascii8bit
45
+ str = "猫舌"
46
+ @db.execute('create table foo(data text)')
47
+ stmt = @db.prepare('insert into foo(data) values (?)')
48
+ stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
49
+ stmt.step
50
+
51
+ string = @db.execute('select data from foo').first.first
52
+ assert_equal Encoding.find('ASCII-8BIT'), string.encoding
53
+ assert_equal str, string.force_encoding('UTF-8')
54
+ end
55
+
56
+ def test_blob_with_eucjp
57
+ str = "猫舌".encode("EUC-JP")
58
+ @db.execute('create table foo(data text)')
59
+ stmt = @db.prepare('insert into foo(data) values (?)')
60
+ stmt.bind_param(1, SQLite3::Blob.new(str))
61
+ stmt.step
62
+
63
+ string = @db.execute('select data from foo').first.first
64
+ assert_equal Encoding.find('ASCII-8BIT'), string.encoding
65
+ assert_equal str, string.force_encoding('EUC-JP')
66
+ end
67
+
68
+ def test_db_with_eucjp
69
+ db = SQLite3::Database.new(':memory:'.encode('EUC-JP'))
70
+ assert_equal(Encoding.find('UTF-8'), db.encoding)
71
+ end
72
+
73
+ def test_db_with_utf16
74
+ db = SQLite3::Database.new(':memory:'.encode('UTF-16LE'))
75
+ assert_equal(Encoding.find('UTF-16LE'), db.encoding)
76
+ end
77
+
78
+ def test_statement_eucjp
79
+ str = "猫舌"
80
+ @db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
81
+ row = @db.execute("select data from ex")
82
+ assert_equal @db.encoding, row.first.first.encoding
83
+ assert_equal str, row.first.first
84
+ end
85
+
86
+ def test_statement_utf8
87
+ str = "猫舌"
88
+ @db.execute("insert into ex(data) values ('#{str}')")
89
+ row = @db.execute("select data from ex")
90
+ assert_equal @db.encoding, row.first.first.encoding
91
+ assert_equal str, row.first.first
92
+ end
93
+
94
+ def test_encoding
95
+ assert_equal Encoding.find("UTF-8"), @db.encoding
96
+ end
97
+
98
+ def test_utf_8
99
+ str = "猫舌"
100
+ @db.execute(@insert, 10, str)
101
+ row = @db.execute("select data from ex")
102
+ assert_equal @db.encoding, row.first.first.encoding
103
+ assert_equal str, row.first.first
104
+ end
105
+
106
+ def test_euc_jp
107
+ str = "猫舌".encode('EUC-JP')
108
+ @db.execute(@insert, 10, str)
109
+ row = @db.execute("select data from ex")
110
+ assert_equal @db.encoding, row.first.first.encoding
111
+ assert_equal str.encode('UTF-8'), row.first.first
112
+ end
113
+
114
+ end if RUBY_VERSION >= '1.9.1'
115
+ end
@@ -0,0 +1,545 @@
1
+ require 'helper'
2
+
3
+ class TC_Database_Integration < Test::Unit::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 )"
8
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
9
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
10
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
11
+ end
12
+ end
13
+
14
+ def teardown
15
+ @db.close
16
+ end
17
+
18
+ def test_table_info_with_type_translation_active
19
+ @db.type_translation = true
20
+ assert_nothing_raised { @db.table_info("foo") }
21
+ end
22
+
23
+ def test_table_info_with_defaults_for_version_3_3_8_and_higher
24
+ @db.transaction do
25
+ @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
26
+ data = @db.table_info( "defaults_test" )
27
+ assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => 0, "cid" => 0, "pk" => 0},
28
+ data[0])
29
+ assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => 0, "cid" => 1, "pk" => 0},
30
+ data[1])
31
+ end
32
+ end
33
+
34
+ def test_table_info_without_defaults_for_version_3_3_8_and_higher
35
+ @db.transaction do
36
+ @db.execute "create table no_defaults_test ( a integer default 1, b integer )"
37
+ data = @db.table_info( "no_defaults_test" )
38
+ assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => 0, "cid" => 0, "pk" => 0},
39
+ data[0])
40
+ assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => 0, "cid" => 1, "pk" => 0},
41
+ data[1])
42
+ end
43
+ end
44
+
45
+ def test_complete_fail
46
+ assert !@db.complete?( "select * from foo" )
47
+ end
48
+ def test_complete_success
49
+ assert @db.complete?( "select * from foo;" )
50
+ end
51
+
52
+ # FIXME: do people really need UTF16 sql statements?
53
+ #def test_complete_fail_utf16
54
+ # assert !@db.complete?( "select * from foo".to_utf16(false), true )
55
+ #end
56
+
57
+ # FIXME: do people really need UTF16 sql statements?
58
+ #def test_complete_success_utf16
59
+ # assert @db.complete?( "select * from foo;".to_utf16(true), true )
60
+ #end
61
+
62
+ def test_errmsg
63
+ assert_equal "not an error", @db.errmsg
64
+ end
65
+
66
+ # FIXME: do people really need UTF16 error messages?
67
+ #def test_errmsg_utf16
68
+ # msg = Iconv.conv('UTF-16', 'UTF-8', 'not an error')
69
+ # assert_equal msg, @db.errmsg(true)
70
+ #end
71
+
72
+ def test_errcode
73
+ assert_equal 0, @db.errcode
74
+ end
75
+
76
+ def test_trace
77
+ result = nil
78
+ @db.trace { |sql| result = sql }
79
+ @db.execute "select * from foo"
80
+ assert_equal "select * from foo", result
81
+ end
82
+
83
+ def test_authorizer_okay
84
+ @db.authorizer { |type,a,b,c,d| 0 }
85
+ rows = @db.execute "select * from foo"
86
+ assert_equal 3, rows.length
87
+ end
88
+
89
+ def test_authorizer_error
90
+ @db.authorizer { |type,a,b,c,d| 1 }
91
+ assert_raise( SQLite3::AuthorizationException ) do
92
+ @db.execute "select * from foo"
93
+ end
94
+ end
95
+
96
+ def test_authorizer_silent
97
+ @db.authorizer { |type,a,b,c,d| 2 }
98
+ rows = @db.execute "select * from foo"
99
+ assert rows.empty?
100
+ end
101
+
102
+ def test_prepare_invalid_syntax
103
+ assert_raise( SQLite3::SQLException ) do
104
+ @db.prepare "select from foo"
105
+ end
106
+ end
107
+
108
+ def test_prepare_invalid_column
109
+ assert_raise( SQLite3::SQLException ) do
110
+ @db.prepare "select k from foo"
111
+ end
112
+ end
113
+
114
+ def test_prepare_invalid_table
115
+ assert_raise( SQLite3::SQLException ) do
116
+ @db.prepare "select * from barf"
117
+ end
118
+ end
119
+
120
+ def test_prepare_no_block
121
+ stmt = @db.prepare "select * from foo"
122
+ assert stmt.respond_to?(:execute)
123
+ stmt.close
124
+ end
125
+
126
+ def test_prepare_with_block
127
+ called = false
128
+ @db.prepare "select * from foo" do |stmt|
129
+ called = true
130
+ assert stmt.respond_to?(:execute)
131
+ end
132
+ assert called
133
+ end
134
+
135
+ def test_execute_no_block_no_bind_no_match
136
+ rows = @db.execute( "select * from foo where a > 100" )
137
+ assert rows.empty?
138
+ end
139
+
140
+ def test_execute_with_block_no_bind_no_match
141
+ called = false
142
+ @db.execute( "select * from foo where a > 100" ) do |row|
143
+ called = true
144
+ end
145
+ assert !called
146
+ end
147
+
148
+ def test_execute_no_block_with_bind_no_match
149
+ rows = @db.execute( "select * from foo where a > ?", 100 )
150
+ assert rows.empty?
151
+ end
152
+
153
+ def test_execute_with_block_with_bind_no_match
154
+ called = false
155
+ @db.execute( "select * from foo where a > ?", 100 ) do |row|
156
+ called = true
157
+ end
158
+ assert !called
159
+ end
160
+
161
+ def test_execute_no_block_no_bind_with_match
162
+ rows = @db.execute( "select * from foo where a = 1" )
163
+ assert_equal 1, rows.length
164
+ end
165
+
166
+ def test_execute_with_block_no_bind_with_match
167
+ called = 0
168
+ @db.execute( "select * from foo where a = 1" ) do |row|
169
+ called += 1
170
+ end
171
+ assert_equal 1, called
172
+ end
173
+
174
+ def test_execute_no_block_with_bind_with_match
175
+ rows = @db.execute( "select * from foo where a = ?", 1 )
176
+ assert_equal 1, rows.length
177
+ end
178
+
179
+ def test_execute_with_block_with_bind_with_match
180
+ called = 0
181
+ @db.execute( "select * from foo where a = ?", 1 ) do |row|
182
+ called += 1
183
+ end
184
+ assert_equal 1, called
185
+ end
186
+
187
+ def test_execute2_no_block_no_bind_no_match
188
+ columns, *rows = @db.execute2( "select * from foo where a > 100" )
189
+ assert rows.empty?
190
+ assert_equal [ "a", "b" ], columns
191
+ end
192
+
193
+ def test_execute2_with_block_no_bind_no_match
194
+ called = 0
195
+ @db.execute2( "select * from foo where a > 100" ) do |row|
196
+ assert [ "a", "b" ], row unless called == 0
197
+ called += 1
198
+ end
199
+ assert_equal 1, called
200
+ end
201
+
202
+ def test_execute2_no_block_with_bind_no_match
203
+ columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
204
+ assert rows.empty?
205
+ assert_equal [ "a", "b" ], columns
206
+ end
207
+
208
+ def test_execute2_with_block_with_bind_no_match
209
+ called = 0
210
+ @db.execute2( "select * from foo where a > ?", 100 ) do |row|
211
+ assert_equal [ "a", "b" ], row unless called == 0
212
+ called += 1
213
+ end
214
+ assert_equal 1, called
215
+ end
216
+
217
+ def test_execute2_no_block_no_bind_with_match
218
+ columns, *rows = @db.execute2( "select * from foo where a = 1" )
219
+ assert_equal 1, rows.length
220
+ assert_equal [ "a", "b" ], columns
221
+ end
222
+
223
+ def test_execute2_with_block_no_bind_with_match
224
+ called = 0
225
+ @db.execute2( "select * from foo where a = 1" ) do |row|
226
+ assert_equal [ 1, "foo" ], row unless called == 0
227
+ called += 1
228
+ end
229
+ assert_equal 2, called
230
+ end
231
+
232
+ def test_execute2_no_block_with_bind_with_match
233
+ columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
234
+ assert_equal 1, rows.length
235
+ assert_equal [ "a", "b" ], columns
236
+ end
237
+
238
+ def test_execute2_with_block_with_bind_with_match
239
+ called = 0
240
+ @db.execute2( "select * from foo where a = ?", 1 ) do |row|
241
+ called += 1
242
+ end
243
+ assert_equal 2, called
244
+ end
245
+
246
+ def test_execute_batch_empty
247
+ assert_nothing_raised { @db.execute_batch "" }
248
+ end
249
+
250
+ def test_execute_batch_no_bind
251
+ @db.transaction do
252
+ @db.execute_batch <<-SQL
253
+ create table bar ( a, b, c );
254
+ insert into bar values ( 'one', 2, 'three' );
255
+ insert into bar values ( 'four', 5, 'six' );
256
+ insert into bar values ( 'seven', 8, 'nine' );
257
+ SQL
258
+ end
259
+ rows = @db.execute( "select * from bar" )
260
+ assert_equal 3, rows.length
261
+ end
262
+
263
+ def test_execute_batch_with_bind
264
+ @db.execute_batch( <<-SQL, 1 )
265
+ create table bar ( a, b, c );
266
+ insert into bar values ( 'one', 2, ? );
267
+ insert into bar values ( 'four', 5, ? );
268
+ insert into bar values ( 'seven', 8, ? );
269
+ SQL
270
+ rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
271
+ assert_equal [1, 1, 1], rows
272
+ end
273
+
274
+ def test_query_no_block_no_bind_no_match
275
+ result = @db.query( "select * from foo where a > 100" )
276
+ assert_nil result.next
277
+ result.close
278
+ end
279
+
280
+ def test_query_with_block_no_bind_no_match
281
+ r = nil
282
+ @db.query( "select * from foo where a > 100" ) do |result|
283
+ assert_nil result.next
284
+ r = result
285
+ end
286
+ assert r.closed?
287
+ end
288
+
289
+ def test_query_no_block_with_bind_no_match
290
+ result = @db.query( "select * from foo where a > ?", 100 )
291
+ assert_nil result.next
292
+ result.close
293
+ end
294
+
295
+ def test_query_with_block_with_bind_no_match
296
+ r = nil
297
+ @db.query( "select * from foo where a > ?", 100 ) do |result|
298
+ assert_nil result.next
299
+ r = result
300
+ end
301
+ assert r.closed?
302
+ end
303
+
304
+ def test_query_no_block_no_bind_with_match
305
+ result = @db.query( "select * from foo where a = 1" )
306
+ assert_not_nil result.next
307
+ assert_nil result.next
308
+ result.close
309
+ end
310
+
311
+ def test_query_with_block_no_bind_with_match
312
+ r = nil
313
+ @db.query( "select * from foo where a = 1" ) do |result|
314
+ assert_not_nil result.next
315
+ assert_nil result.next
316
+ r = result
317
+ end
318
+ assert r.closed?
319
+ end
320
+
321
+ def test_query_no_block_with_bind_with_match
322
+ result = @db.query( "select * from foo where a = ?", 1 )
323
+ assert_not_nil result.next
324
+ assert_nil result.next
325
+ result.close
326
+ end
327
+
328
+ def test_query_with_block_with_bind_with_match
329
+ r = nil
330
+ @db.query( "select * from foo where a = ?", 1 ) do |result|
331
+ assert_not_nil result.next
332
+ assert_nil result.next
333
+ r = result
334
+ end
335
+ assert r.closed?
336
+ end
337
+
338
+ def test_get_first_row_no_bind_no_match
339
+ result = @db.get_first_row( "select * from foo where a=100" )
340
+ assert_nil result
341
+ end
342
+
343
+ def test_get_first_row_no_bind_with_match
344
+ result = @db.get_first_row( "select * from foo where a=1" )
345
+ assert_equal [ 1, "foo" ], result
346
+ end
347
+
348
+ def test_get_first_row_with_bind_no_match
349
+ result = @db.get_first_row( "select * from foo where a=?", 100 )
350
+ assert_nil result
351
+ end
352
+
353
+ def test_get_first_row_with_bind_with_match
354
+ result = @db.get_first_row( "select * from foo where a=?", 1 )
355
+ assert_equal [ 1, "foo" ], result
356
+ end
357
+
358
+ def test_get_first_value_no_bind_no_match
359
+ result = @db.get_first_value( "select b, a from foo where a=100" )
360
+ assert_nil result
361
+ end
362
+
363
+ def test_get_first_value_no_bind_with_match
364
+ result = @db.get_first_value( "select b, a from foo where a=1" )
365
+ assert_equal "foo", result
366
+ end
367
+
368
+ def test_get_first_value_with_bind_no_match
369
+ result = @db.get_first_value( "select b, a from foo where a=?", 100 )
370
+ assert_nil result
371
+ end
372
+
373
+ def test_get_first_value_with_bind_with_match
374
+ result = @db.get_first_value( "select b, a from foo where a=?", 1 )
375
+ assert_equal "foo", result
376
+ end
377
+
378
+ def test_last_insert_row_id
379
+ @db.execute "insert into foo ( b ) values ( 'test' )"
380
+ assert_equal 4, @db.last_insert_row_id
381
+ @db.execute "insert into foo ( b ) values ( 'again' )"
382
+ assert_equal 5, @db.last_insert_row_id
383
+ end
384
+
385
+ def test_changes
386
+ @db.execute "insert into foo ( b ) values ( 'test' )"
387
+ assert_equal 1, @db.changes
388
+ @db.execute "delete from foo where 1=1"
389
+ assert_equal 4, @db.changes
390
+ end
391
+
392
+ def test_total_changes
393
+ assert_equal 3, @db.total_changes
394
+ @db.execute "insert into foo ( b ) values ( 'test' )"
395
+ @db.execute "delete from foo where 1=1"
396
+ assert_equal 8, @db.total_changes
397
+ end
398
+
399
+ def test_transaction_nest
400
+ assert_raise( SQLite3::SQLException ) do
401
+ @db.transaction do
402
+ @db.transaction do
403
+ end
404
+ end
405
+ end
406
+ end
407
+
408
+ def test_transaction_rollback
409
+ @db.transaction
410
+ @db.execute_batch <<-SQL
411
+ insert into foo (b) values ( 'test1' );
412
+ insert into foo (b) values ( 'test2' );
413
+ insert into foo (b) values ( 'test3' );
414
+ insert into foo (b) values ( 'test4' );
415
+ SQL
416
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
417
+ @db.rollback
418
+ assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
419
+ end
420
+
421
+ def test_transaction_commit
422
+ @db.transaction
423
+ @db.execute_batch <<-SQL
424
+ insert into foo (b) values ( 'test1' );
425
+ insert into foo (b) values ( 'test2' );
426
+ insert into foo (b) values ( 'test3' );
427
+ insert into foo (b) values ( 'test4' );
428
+ SQL
429
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
430
+ @db.commit
431
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
432
+ end
433
+
434
+ def test_transaction_rollback_in_block
435
+ assert_raise( SQLite3::SQLException ) do
436
+ @db.transaction do
437
+ @db.rollback
438
+ end
439
+ end
440
+ end
441
+
442
+ def test_transaction_commit_in_block
443
+ assert_raise( SQLite3::SQLException ) do
444
+ @db.transaction do
445
+ @db.commit
446
+ end
447
+ end
448
+ end
449
+
450
+ def test_transaction_active
451
+ assert !@db.transaction_active?
452
+ @db.transaction
453
+ assert @db.transaction_active?
454
+ @db.commit
455
+ assert !@db.transaction_active?
456
+ end
457
+
458
+ def test_interrupt
459
+ @db.create_function( "abort", 1 ) do |func,x|
460
+ @db.interrupt
461
+ func.result = x
462
+ end
463
+
464
+ assert_raise( SQLite3::InterruptException ) do
465
+ @db.execute "select abort(a) from foo"
466
+ end
467
+ end
468
+
469
+ def test_create_function
470
+ @db.create_function( "munge", 1 ) do |func,x|
471
+ func.result = ">>>#{x}<<<"
472
+ end
473
+
474
+ value = @db.get_first_value( "select munge(b) from foo where a=1" )
475
+ assert_match( />>>.*<<</, value )
476
+ end
477
+
478
+ def test_create_aggregate_without_block
479
+ step = proc do |ctx,a|
480
+ ctx[:sum] ||= 0
481
+ ctx[:sum] += a.to_i
482
+ end
483
+
484
+ final = proc { |ctx| ctx.result = ctx[:sum] }
485
+
486
+ @db.create_aggregate( "accumulate", 1, step, final )
487
+
488
+ value = @db.get_first_value( "select accumulate(a) from foo" )
489
+ assert_equal 6, value
490
+ end
491
+
492
+ def test_create_aggregate_with_block
493
+ @db.create_aggregate( "accumulate", 1 ) do
494
+ step do |ctx,a|
495
+ ctx[:sum] ||= 0
496
+ ctx[:sum] += a.to_i
497
+ end
498
+
499
+ finalize { |ctx| ctx.result = ctx[:sum] }
500
+ end
501
+
502
+ value = @db.get_first_value( "select accumulate(a) from foo" )
503
+ assert_equal 6, value
504
+ end
505
+
506
+ def test_create_aggregate_with_no_data
507
+ @db.create_aggregate( "accumulate", 1 ) do
508
+ step do |ctx,a|
509
+ ctx[:sum] ||= 0
510
+ ctx[:sum] += a.to_i
511
+ end
512
+
513
+ finalize { |ctx| ctx.result = ctx[:sum] || 0 }
514
+ end
515
+
516
+ value = @db.get_first_value(
517
+ "select accumulate(a) from foo where a = 100" )
518
+ assert_equal 0, value
519
+ end
520
+
521
+ def test_create_aggregate_handler
522
+ handler = Class.new do
523
+ class << self
524
+ def arity; 1; end
525
+ def text_rep; SQLite3::Constants::TextRep::ANY; end
526
+ def name; "multiply"; end
527
+ end
528
+ def step(ctx, a)
529
+ ctx[:buffer] ||= 1
530
+ ctx[:buffer] *= a.to_i
531
+ end
532
+ def finalize(ctx); ctx.result = ctx[:buffer]; end
533
+ end
534
+
535
+ @db.create_aggregate_handler( handler )
536
+ value = @db.get_first_value( "select multiply(a) from foo" )
537
+ assert_equal 6, value
538
+ end
539
+
540
+ def test_bind_array_parameter
541
+ result = @db.get_first_value( "select b from foo where a=? and b=?",
542
+ [ 1, "foo" ] )
543
+ assert_equal "foo", result
544
+ end
545
+ end