sqlite3 1.5.0.rc1-x86_64-linux

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