extralite-bundle 2.7.1 → 2.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +1 -1
- data/CHANGELOG.md +17 -4
- data/README.md +79 -42
- data/examples/kv_store.rb +1 -1
- data/examples/pubsub_store_polyphony.rb +7 -7
- data/examples/pubsub_store_threads.rb +3 -3
- data/ext/extralite/changeset.c +1 -1
- data/ext/extralite/common.c +27 -27
- data/ext/extralite/database.c +134 -61
- data/ext/extralite/extralite.h +10 -17
- data/ext/extralite/query.c +17 -17
- data/ext/sqlite3/sqlite3.c +494 -198
- data/ext/sqlite3/sqlite3.h +22 -3
- data/gemspec.rb +1 -1
- data/lib/extralite/version.rb +1 -1
- data/lib/extralite.rb +158 -3
- data/test/{perf_ary.rb → perf_array.rb} +1 -1
- data/test/perf_splat.rb +63 -0
- data/test/{perf_argv_transform.rb → perf_splat_transform.rb} +7 -7
- data/test/test_database.rb +193 -120
- data/test/test_iterator.rb +12 -12
- data/test/test_query.rb +92 -92
- metadata +7 -7
- data/lib/extralite/sqlite3_constants.rb +0 -157
data/test/test_database.rb
CHANGED
@@ -40,30 +40,30 @@ class DatabaseTest < Minitest::Test
|
|
40
40
|
assert_equal [], r
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
44
|
-
r = @db.
|
43
|
+
def test_query_array
|
44
|
+
r = @db.query_array('select * from t')
|
45
45
|
assert_equal [[1, 2, 3], [4, 5, 6]], r
|
46
46
|
|
47
|
-
r = @db.
|
47
|
+
r = @db.query_array('select * from t where x = 2')
|
48
48
|
assert_equal [], r
|
49
49
|
end
|
50
50
|
|
51
|
-
def
|
52
|
-
r = @db.
|
51
|
+
def test_query_splat
|
52
|
+
r = @db.query_splat('select * from t')
|
53
53
|
assert_equal [[1, 2, 3], [4, 5, 6]], r
|
54
54
|
|
55
|
-
r = @db.
|
55
|
+
r = @db.query_splat('select * from t where x = 2')
|
56
56
|
assert_equal [], r
|
57
57
|
|
58
58
|
# with block
|
59
59
|
r = []
|
60
|
-
@db.
|
60
|
+
@db.query_splat('select * from t') { |a, b, c| r << [a, b, c] }
|
61
61
|
assert_equal [[1, 2, 3], [4, 5, 6]], r
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
64
|
+
def test_query_splat_with_too_many_columns
|
65
65
|
assert_raises(Extralite::Error) {
|
66
|
-
@db.
|
66
|
+
@db.query_splat('select 1, 2, 3, 4, 5, 6, 7, 8, 9');
|
67
67
|
}
|
68
68
|
end
|
69
69
|
|
@@ -75,11 +75,11 @@ class DatabaseTest < Minitest::Test
|
|
75
75
|
assert_nil r
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
79
|
-
r = @db.
|
78
|
+
def test_query_single_splat
|
79
|
+
r = @db.query_single_splat('select z from t order by Z desc limit 1')
|
80
80
|
assert_equal 6, r
|
81
81
|
|
82
|
-
r = @db.
|
82
|
+
r = @db.query_single_splat('select z from t where x = 2')
|
83
83
|
assert_nil r
|
84
84
|
end
|
85
85
|
|
@@ -99,7 +99,7 @@ class DatabaseTest < Minitest::Test
|
|
99
99
|
def test_multiple_statements
|
100
100
|
@db.query("insert into t values ('a', 'b', 'c'); insert into t values ('d', 'e', 'f');")
|
101
101
|
|
102
|
-
assert_equal [1, 4, 'a', 'd'], @db.
|
102
|
+
assert_equal [1, 4, 'a', 'd'], @db.query_splat('select x from t order by x')
|
103
103
|
end
|
104
104
|
|
105
105
|
def test_multiple_statements_with_error
|
@@ -123,13 +123,13 @@ class DatabaseTest < Minitest::Test
|
|
123
123
|
|
124
124
|
def test_close
|
125
125
|
assert_equal false, @db.closed?
|
126
|
-
r = @db.
|
126
|
+
r = @db.query_single_splat('select 42')
|
127
127
|
assert_equal 42, r
|
128
128
|
|
129
129
|
assert_equal @db, @db.close
|
130
130
|
assert_equal true, @db.closed?
|
131
131
|
|
132
|
-
assert_raises(Extralite::Error) { @db.
|
132
|
+
assert_raises(Extralite::Error) { @db.query_single_splat('select 42') }
|
133
133
|
end
|
134
134
|
|
135
135
|
def test_parameter_binding_simple
|
@@ -139,7 +139,7 @@ class DatabaseTest < Minitest::Test
|
|
139
139
|
r = @db.query('select x, y, z from t where z = ?', 6)
|
140
140
|
assert_equal [{ x: 4, y: 5, z: 6 }], r
|
141
141
|
|
142
|
-
error = assert_raises(Extralite::ParameterError) { @db.
|
142
|
+
error = assert_raises(Extralite::ParameterError) { @db.query_single_splat('select ?', Date.today) }
|
143
143
|
assert_equal error.message, 'Cannot bind parameter at position 1 of type Date'
|
144
144
|
end
|
145
145
|
|
@@ -173,24 +173,24 @@ class DatabaseTest < Minitest::Test
|
|
173
173
|
class Foo; end
|
174
174
|
|
175
175
|
def test_parameter_binding_from_hash
|
176
|
-
assert_equal 42, @db.
|
177
|
-
assert_equal 42, @db.
|
178
|
-
assert_equal 42, @db.
|
179
|
-
assert_nil @db.
|
176
|
+
assert_equal 42, @db.query_single_splat('select :bar', foo: 41, bar: 42)
|
177
|
+
assert_equal 42, @db.query_single_splat('select :bar', 'foo' => 41, 'bar' => 42)
|
178
|
+
assert_equal 42, @db.query_single_splat('select ?8', 7 => 41, 8 => 42)
|
179
|
+
assert_nil @db.query_single_splat('select :bar', foo: 41)
|
180
180
|
|
181
|
-
error = assert_raises(Extralite::ParameterError) { @db.
|
181
|
+
error = assert_raises(Extralite::ParameterError) { @db.query_single_splat('select ?', Foo.new => 42) }
|
182
182
|
assert_equal error.message, 'Cannot bind parameter with a key of type DatabaseTest::Foo'
|
183
183
|
|
184
|
-
error = assert_raises(Extralite::ParameterError) { @db.
|
184
|
+
error = assert_raises(Extralite::ParameterError) { @db.query_single_splat('select ?', %w[a b] => 42) }
|
185
185
|
assert_equal error.message, 'Cannot bind parameter with a key of type Array'
|
186
186
|
end
|
187
187
|
|
188
188
|
def test_parameter_binding_from_struct
|
189
189
|
foo_bar = Struct.new(:':foo', :bar)
|
190
190
|
value = foo_bar.new(41, 42)
|
191
|
-
assert_equal 41, @db.
|
192
|
-
assert_equal 42, @db.
|
193
|
-
assert_nil @db.
|
191
|
+
assert_equal 41, @db.query_single_splat('select :foo', value)
|
192
|
+
assert_equal 42, @db.query_single_splat('select :bar', value)
|
193
|
+
assert_nil @db.query_single_splat('select :baz', value)
|
194
194
|
end
|
195
195
|
|
196
196
|
def test_parameter_binding_from_data_class
|
@@ -198,8 +198,8 @@ class DatabaseTest < Minitest::Test
|
|
198
198
|
|
199
199
|
foo_bar = Data.define(:':foo', :bar)
|
200
200
|
value = foo_bar.new(':foo': 41, bar: 42)
|
201
|
-
assert_equal 42, @db.
|
202
|
-
assert_nil @db.
|
201
|
+
assert_equal 42, @db.query_single_splat('select :bar', value)
|
202
|
+
assert_nil @db.query_single_splat('select :baz', value)
|
203
203
|
end
|
204
204
|
|
205
205
|
def test_parameter_binding_for_blobs
|
@@ -235,43 +235,43 @@ class DatabaseTest < Minitest::Test
|
|
235
235
|
end
|
236
236
|
|
237
237
|
def test_parameter_binding_for_simple_types
|
238
|
-
assert_nil @db.
|
238
|
+
assert_nil @db.query_single_splat('select ?', nil)
|
239
239
|
|
240
240
|
# 32-bit integers
|
241
|
-
assert_equal(-2** 31, @db.
|
242
|
-
assert_equal(2**31 - 1, @db.
|
241
|
+
assert_equal(-2** 31, @db.query_single_splat('select ?', -2**31))
|
242
|
+
assert_equal(2**31 - 1, @db.query_single_splat('select ?', 2**31 - 1))
|
243
243
|
|
244
244
|
# 64-bit integers
|
245
|
-
assert_equal(-2 ** 63, @db.
|
246
|
-
assert_equal(2**63 - 1, @db.
|
245
|
+
assert_equal(-2 ** 63, @db.query_single_splat('select ?', -2 ** 63))
|
246
|
+
assert_equal(2**63 - 1, @db.query_single_splat('select ?', 2**63 - 1))
|
247
247
|
|
248
248
|
# floats
|
249
|
-
assert_equal Float::MIN, @db.
|
250
|
-
assert_equal Float::MAX, @db.
|
249
|
+
assert_equal Float::MIN, @db.query_single_splat('select ?', Float::MIN)
|
250
|
+
assert_equal Float::MAX, @db.query_single_splat('select ?', Float::MAX)
|
251
251
|
|
252
252
|
# boolean
|
253
|
-
assert_equal 1, @db.
|
254
|
-
assert_equal 0, @db.
|
253
|
+
assert_equal 1, @db.query_single_splat('select ?', true)
|
254
|
+
assert_equal 0, @db.query_single_splat('select ?', false)
|
255
255
|
|
256
256
|
# strings and symbols
|
257
|
-
assert_equal 'foo', @db.
|
258
|
-
assert_equal 'foo', @db.
|
257
|
+
assert_equal 'foo', @db.query_single_splat('select ?', 'foo')
|
258
|
+
assert_equal 'foo', @db.query_single_splat('select ?', :foo)
|
259
259
|
end
|
260
260
|
|
261
261
|
def test_value_casting
|
262
|
-
r = @db.
|
262
|
+
r = @db.query_single_splat("select 'abc'")
|
263
263
|
assert_equal 'abc', r
|
264
264
|
|
265
|
-
r = @db.
|
265
|
+
r = @db.query_single_splat('select 123')
|
266
266
|
assert_equal 123, r
|
267
267
|
|
268
|
-
r = @db.
|
268
|
+
r = @db.query_single_splat('select 12.34')
|
269
269
|
assert_equal 12.34, r
|
270
270
|
|
271
|
-
r = @db.
|
271
|
+
r = @db.query_single_splat('select zeroblob(4)')
|
272
272
|
assert_equal "\x00\x00\x00\x00", r
|
273
273
|
|
274
|
-
r = @db.
|
274
|
+
r = @db.query_single_splat('select null')
|
275
275
|
assert_nil r
|
276
276
|
end
|
277
277
|
|
@@ -283,7 +283,7 @@ class DatabaseTest < Minitest::Test
|
|
283
283
|
@db.load_extension(File.join(__dir__, 'extensions/text.dylib'))
|
284
284
|
end
|
285
285
|
|
286
|
-
r = @db.
|
286
|
+
r = @db.query_single_splat("select reverse('abcd')")
|
287
287
|
assert_equal 'dcba', r
|
288
288
|
end
|
289
289
|
|
@@ -342,13 +342,13 @@ class DatabaseTest < Minitest::Test
|
|
342
342
|
def test_execute_with_params
|
343
343
|
changes = @db.execute('update t set x = ? where z = ?', 42, 6)
|
344
344
|
assert_equal 1, changes
|
345
|
-
assert_equal [[1, 2, 3], [42, 5, 6]], @db.
|
345
|
+
assert_equal [[1, 2, 3], [42, 5, 6]], @db.query_array('select * from t order by x')
|
346
346
|
end
|
347
347
|
|
348
348
|
def test_execute_with_params_array
|
349
349
|
changes = @db.execute('update t set x = ? where z = ?', [42, 6])
|
350
350
|
assert_equal 1, changes
|
351
|
-
assert_equal [[1, 2, 3], [42, 5, 6]], @db.
|
351
|
+
assert_equal [[1, 2, 3], [42, 5, 6]], @db.query_array('select * from t order by x')
|
352
352
|
end
|
353
353
|
|
354
354
|
def test_batch_execute
|
@@ -513,7 +513,7 @@ class DatabaseTest < Minitest::Test
|
|
513
513
|
], array
|
514
514
|
end
|
515
515
|
|
516
|
-
def
|
516
|
+
def test_batch_query_array_with_array
|
517
517
|
@db.query('create table foo (a, b, c)')
|
518
518
|
assert_equal [], @db.query('select * from foo')
|
519
519
|
|
@@ -521,20 +521,20 @@ class DatabaseTest < Minitest::Test
|
|
521
521
|
[1, '2', 3],
|
522
522
|
['4', 5, 6]
|
523
523
|
]
|
524
|
-
results = @db.
|
524
|
+
results = @db.batch_query_array('insert into foo values (?, ?, ?) returning *', data)
|
525
525
|
assert_equal [
|
526
526
|
[[1, '2', 3]],
|
527
527
|
[['4', 5, 6]]
|
528
528
|
], results
|
529
529
|
|
530
|
-
results = @db.
|
530
|
+
results = @db.batch_query_array('update foo set c = ? returning *', [42, 43])
|
531
531
|
assert_equal [
|
532
532
|
[[1, '2', 42], ['4', 5, 42]],
|
533
533
|
[[1, '2', 43], ['4', 5, 43]]
|
534
534
|
], results
|
535
535
|
|
536
536
|
array = []
|
537
|
-
changes = @db.
|
537
|
+
changes = @db.batch_query_array('update foo set c = ? returning *', [44, 45]) do |rows|
|
538
538
|
array << rows
|
539
539
|
end
|
540
540
|
assert_equal 4, changes
|
@@ -544,25 +544,25 @@ class DatabaseTest < Minitest::Test
|
|
544
544
|
], array
|
545
545
|
end
|
546
546
|
|
547
|
-
def
|
547
|
+
def test_batch_query_array_with_enumerable
|
548
548
|
@db.query('create table foo (a integer primary key, b)')
|
549
549
|
assert_equal [], @db.query('select * from foo')
|
550
550
|
|
551
|
-
results = @db.
|
551
|
+
results = @db.batch_query_array('insert into foo (b) values (?) returning *', 11..13)
|
552
552
|
assert_equal [
|
553
553
|
[[1, 11]],
|
554
554
|
[[2, 12]],
|
555
555
|
[[3, 13]]
|
556
556
|
], results
|
557
557
|
|
558
|
-
results = @db.
|
558
|
+
results = @db.batch_query_array('update foo set b = ? returning *', [42, 43])
|
559
559
|
assert_equal [
|
560
560
|
[[1, 42], [2, 42], [3, 42]],
|
561
561
|
[[1, 43], [2, 43], [3, 43]]
|
562
562
|
], results
|
563
563
|
|
564
564
|
array = []
|
565
|
-
changes = @db.
|
565
|
+
changes = @db.batch_query_array('update foo set b = ? returning *', [44, 45]) do |rows|
|
566
566
|
array << rows
|
567
567
|
end
|
568
568
|
assert_equal 6, changes
|
@@ -572,13 +572,13 @@ class DatabaseTest < Minitest::Test
|
|
572
572
|
], array
|
573
573
|
end
|
574
574
|
|
575
|
-
def
|
575
|
+
def test_batch_query_array_with_proc
|
576
576
|
@db.query('create table foo (a integer primary key, b)')
|
577
577
|
assert_equal [], @db.query('select * from foo')
|
578
578
|
|
579
579
|
pr = parameter_source_proc([5, 4, 3])
|
580
580
|
assert_kind_of Proc, pr
|
581
|
-
results = @db.
|
581
|
+
results = @db.batch_query_array('insert into foo (b) values (?) returning *', pr)
|
582
582
|
assert_equal [
|
583
583
|
[[1, 5]],
|
584
584
|
[[2, 4]],
|
@@ -586,7 +586,7 @@ class DatabaseTest < Minitest::Test
|
|
586
586
|
], results
|
587
587
|
|
588
588
|
pr = parameter_source_proc([42, 43])
|
589
|
-
results = @db.
|
589
|
+
results = @db.batch_query_array('update foo set b = ? returning *', pr)
|
590
590
|
assert_equal [
|
591
591
|
[[1, 42], [2, 42], [3, 42]],
|
592
592
|
[[1, 43], [2, 43], [3, 43]]
|
@@ -594,7 +594,7 @@ class DatabaseTest < Minitest::Test
|
|
594
594
|
|
595
595
|
array = []
|
596
596
|
pr = parameter_source_proc([44, 45])
|
597
|
-
changes = @db.
|
597
|
+
changes = @db.batch_query_array('update foo set b = ? returning *', pr) do |rows|
|
598
598
|
array << rows
|
599
599
|
end
|
600
600
|
assert_equal 6, changes
|
@@ -604,7 +604,7 @@ class DatabaseTest < Minitest::Test
|
|
604
604
|
], array
|
605
605
|
end
|
606
606
|
|
607
|
-
def
|
607
|
+
def test_batch_query_splat_with_array
|
608
608
|
@db.query('create table foo (a, b, c)')
|
609
609
|
assert_equal [], @db.query('select * from foo')
|
610
610
|
|
@@ -612,20 +612,20 @@ class DatabaseTest < Minitest::Test
|
|
612
612
|
[1, '2', 3],
|
613
613
|
['4', 5, 6]
|
614
614
|
]
|
615
|
-
results = @db.
|
615
|
+
results = @db.batch_query_splat('insert into foo values (?, ?, ?) returning c', data)
|
616
616
|
assert_equal [
|
617
617
|
[3],
|
618
618
|
[6]
|
619
619
|
], results
|
620
620
|
|
621
|
-
results = @db.
|
621
|
+
results = @db.batch_query_splat('update foo set c = ? returning c * 10 + cast(b as integer)', [42, 43])
|
622
622
|
assert_equal [
|
623
623
|
[422, 425],
|
624
624
|
[432, 435]
|
625
625
|
], results
|
626
626
|
|
627
627
|
array = []
|
628
|
-
changes = @db.
|
628
|
+
changes = @db.batch_query_splat('update foo set c = ? returning c * 10 + cast(b as integer)', [44, 45]) do |rows|
|
629
629
|
array << rows
|
630
630
|
end
|
631
631
|
assert_equal 4, changes
|
@@ -635,25 +635,25 @@ class DatabaseTest < Minitest::Test
|
|
635
635
|
], array
|
636
636
|
end
|
637
637
|
|
638
|
-
def
|
638
|
+
def test_batch_query_splat_with_enumerable
|
639
639
|
@db.query('create table foo (a integer primary key, b)')
|
640
640
|
assert_equal [], @db.query('select * from foo')
|
641
641
|
|
642
|
-
results = @db.
|
642
|
+
results = @db.batch_query_splat('insert into foo (b) values (?) returning b * 10 + a', 11..13)
|
643
643
|
assert_equal [
|
644
644
|
[111],
|
645
645
|
[122],
|
646
646
|
[133]
|
647
647
|
], results
|
648
648
|
|
649
|
-
results = @db.
|
649
|
+
results = @db.batch_query_splat('update foo set b = ? returning b * 10 + a', 42..43)
|
650
650
|
assert_equal [
|
651
651
|
[421, 422, 423],
|
652
652
|
[431, 432, 433]
|
653
653
|
], results
|
654
654
|
|
655
655
|
array = []
|
656
|
-
changes = @db.
|
656
|
+
changes = @db.batch_query_splat('update foo set b = ? returning b * 10 + a', 44..45) do |rows|
|
657
657
|
array << rows
|
658
658
|
end
|
659
659
|
assert_equal 6, changes
|
@@ -663,13 +663,13 @@ class DatabaseTest < Minitest::Test
|
|
663
663
|
], array
|
664
664
|
end
|
665
665
|
|
666
|
-
def
|
666
|
+
def test_batch_query_splat_with_proc
|
667
667
|
@db.query('create table foo (a integer primary key, b)')
|
668
668
|
assert_equal [], @db.query('select * from foo')
|
669
669
|
|
670
670
|
pr = parameter_source_proc([5, 4, 3])
|
671
671
|
assert_kind_of Proc, pr
|
672
|
-
results = @db.
|
672
|
+
results = @db.batch_query_splat('insert into foo (b) values (?) returning b', pr)
|
673
673
|
assert_equal [
|
674
674
|
[5],
|
675
675
|
[4],
|
@@ -677,7 +677,7 @@ class DatabaseTest < Minitest::Test
|
|
677
677
|
], results
|
678
678
|
|
679
679
|
pr = parameter_source_proc([42, 43])
|
680
|
-
results = @db.
|
680
|
+
results = @db.batch_query_splat('update foo set b = ? returning b * 10 + a', pr)
|
681
681
|
assert_equal [
|
682
682
|
[421, 422, 423],
|
683
683
|
[431, 432, 433]
|
@@ -685,7 +685,7 @@ class DatabaseTest < Minitest::Test
|
|
685
685
|
|
686
686
|
array = []
|
687
687
|
pr = parameter_source_proc([44, 45])
|
688
|
-
changes = @db.
|
688
|
+
changes = @db.batch_query_splat('update foo set b = ? returning b * 10 + a', pr) do |rows|
|
689
689
|
array << rows
|
690
690
|
end
|
691
691
|
assert_equal 6, changes
|
@@ -857,7 +857,7 @@ class DatabaseTest < Minitest::Test
|
|
857
857
|
|
858
858
|
def test_string_encoding
|
859
859
|
db = Extralite::Database.new(':memory:')
|
860
|
-
v = db.
|
860
|
+
v = db.query_single_splat("select 'foo'")
|
861
861
|
assert_equal 'foo', v
|
862
862
|
assert_equal 'UTF-8', v.encoding.name
|
863
863
|
end
|
@@ -942,24 +942,24 @@ class DatabaseTest < Minitest::Test
|
|
942
942
|
assert_equal [], db.query('select * from foo')
|
943
943
|
|
944
944
|
db.execute('insert into foo values (42)')
|
945
|
-
assert_equal [42], db.
|
945
|
+
assert_equal [42], db.query_splat('select x from foo')
|
946
946
|
|
947
947
|
db.savepoint(:a)
|
948
948
|
|
949
949
|
db.execute('insert into foo values (43)')
|
950
|
-
assert_equal [42, 43], db.
|
951
|
-
|
950
|
+
assert_equal [42, 43], db.query_splat('select x from foo')
|
951
|
+
|
952
952
|
db.savepoint(:b)
|
953
953
|
|
954
954
|
db.execute('insert into foo values (44)')
|
955
|
-
assert_equal [42, 43, 44], db.
|
955
|
+
assert_equal [42, 43, 44], db.query_splat('select x from foo')
|
956
956
|
|
957
957
|
db.rollback_to(:b)
|
958
|
-
assert_equal [42, 43], db.
|
958
|
+
assert_equal [42, 43], db.query_splat('select x from foo')
|
959
959
|
|
960
960
|
db.release(:a)
|
961
961
|
|
962
|
-
assert_equal [42, 43], db.
|
962
|
+
assert_equal [42, 43], db.query_splat('select x from foo')
|
963
963
|
end
|
964
964
|
end
|
965
965
|
|
@@ -973,8 +973,8 @@ class DatabaseTest < Minitest::Test
|
|
973
973
|
], q.to_a
|
974
974
|
end
|
975
975
|
|
976
|
-
def
|
977
|
-
q = @db.
|
976
|
+
def test_prepare_splat
|
977
|
+
q = @db.prepare_splat('select * from t order by x')
|
978
978
|
assert_kind_of Extralite::Query, q
|
979
979
|
|
980
980
|
buf = []
|
@@ -982,8 +982,8 @@ class DatabaseTest < Minitest::Test
|
|
982
982
|
assert_equal [3, 6], buf
|
983
983
|
end
|
984
984
|
|
985
|
-
def
|
986
|
-
q = @db.
|
985
|
+
def test_prepare_array
|
986
|
+
q = @db.prepare_array('select * from t order by x')
|
987
987
|
assert_kind_of Extralite::Query, q
|
988
988
|
|
989
989
|
assert_equal [
|
@@ -1000,21 +1000,94 @@ class DatabaseTest < Minitest::Test
|
|
1000
1000
|
], q.to_a
|
1001
1001
|
end
|
1002
1002
|
|
1003
|
-
def
|
1004
|
-
q = @db.
|
1003
|
+
def test_prepare_splat_with_transform
|
1004
|
+
q = @db.prepare_splat('select * from t order by x') { |x, y, z| x * 100 + y * 10 + z }
|
1005
1005
|
assert_equal [
|
1006
1006
|
123,
|
1007
1007
|
456
|
1008
1008
|
], q.to_a
|
1009
1009
|
end
|
1010
1010
|
|
1011
|
-
def
|
1012
|
-
q = @db.
|
1011
|
+
def test_prepare_array_with_transform
|
1012
|
+
q = @db.prepare_array('select * from t order by x') { |r| r * 2 }
|
1013
1013
|
assert_equal [
|
1014
1014
|
[1, 2, 3, 1, 2, 3],
|
1015
1015
|
[4, 5, 6, 4, 5, 6]
|
1016
1016
|
], q.to_a
|
1017
1017
|
end
|
1018
|
+
|
1019
|
+
def test_wal_checkpoint
|
1020
|
+
fn = Tempfile.new('extralite_test_wal_checkpoint').path
|
1021
|
+
|
1022
|
+
db = Extralite::Database.new(fn, wal: true)
|
1023
|
+
|
1024
|
+
db.execute 'create table t (x, y, z)'
|
1025
|
+
rows = (1..1000).map { [rand, rand, rand] }
|
1026
|
+
db.batch_execute('insert into t values (?, ?, ?)', rows)
|
1027
|
+
|
1028
|
+
wal_fn = "#{fn}-wal"
|
1029
|
+
|
1030
|
+
assert File.exist?(wal_fn)
|
1031
|
+
assert File.size(wal_fn) > 0
|
1032
|
+
|
1033
|
+
# invalid mode
|
1034
|
+
assert_raises(ArgumentError) { db.wal_checkpoint(:foo) }
|
1035
|
+
|
1036
|
+
# invalid db name
|
1037
|
+
assert_raises(Extralite::Error) { db.wal_checkpoint(:passive, 'foo') }
|
1038
|
+
|
1039
|
+
r = db.wal_checkpoint(:full)
|
1040
|
+
assert File.exist?(wal_fn)
|
1041
|
+
assert File.size(wal_fn) > 0
|
1042
|
+
assert_equal [19, 19], r
|
1043
|
+
|
1044
|
+
r = db.wal_checkpoint(:truncate)
|
1045
|
+
assert File.exist?(wal_fn)
|
1046
|
+
assert File.size(wal_fn) == 0
|
1047
|
+
assert_equal [0, 0], r
|
1048
|
+
end
|
1049
|
+
|
1050
|
+
def test_execute_with_comments
|
1051
|
+
result = @db.execute(<<~SQL)
|
1052
|
+
-- this is a comment
|
1053
|
+
SQL
|
1054
|
+
assert_nil result
|
1055
|
+
|
1056
|
+
result = @db.execute(<<~SQL)
|
1057
|
+
DELETE FROM t;
|
1058
|
+
INSERT INTO t (x, y, z) VALUES (1, 1, 1);
|
1059
|
+
INSERT INTO t (x, y, z) VALUES (2, 2, 2);
|
1060
|
+
SQL
|
1061
|
+
assert_equal 1, result
|
1062
|
+
assert_equal [1, 2], @db.query_splat('SELECT x FROM t ORDER BY x')
|
1063
|
+
|
1064
|
+
result = @db.execute(<<~SQL)
|
1065
|
+
-- this is a comment at the beginning
|
1066
|
+
DELETE FROM t;
|
1067
|
+
INSERT INTO t (x, y, z) VALUES (3, 3, 3);
|
1068
|
+
INSERT INTO t (x, y, z) VALUES (4, 4, 4);
|
1069
|
+
SQL
|
1070
|
+
assert_equal 1, result
|
1071
|
+
assert_equal [3, 4], @db.query_splat('SELECT x FROM t ORDER BY x')
|
1072
|
+
|
1073
|
+
result = @db.execute(<<~SQL)
|
1074
|
+
DELETE FROM t;
|
1075
|
+
INSERT INTO t (x, y, z) VALUES (5, 5, 5);
|
1076
|
+
-- this is a comment in the middle
|
1077
|
+
INSERT INTO t (x, y, z) VALUES (6, 6, 6);
|
1078
|
+
SQL
|
1079
|
+
assert_equal 1, result
|
1080
|
+
assert_equal [5, 6], @db.query_splat('SELECT x FROM t ORDER BY x')
|
1081
|
+
|
1082
|
+
result = @db.execute(<<~SQL)
|
1083
|
+
DELETE FROM t;
|
1084
|
+
INSERT INTO t (x, y, z) VALUES (7, 7, 7);
|
1085
|
+
INSERT INTO t (x, y, z) VALUES (8, 8, 8);
|
1086
|
+
-- this is a comment at the end
|
1087
|
+
SQL
|
1088
|
+
assert_nil result
|
1089
|
+
assert_equal [7, 8], @db.query_splat('SELECT x FROM t ORDER BY x')
|
1090
|
+
end
|
1018
1091
|
end
|
1019
1092
|
|
1020
1093
|
class ScenarioTest < Minitest::Test
|
@@ -1046,7 +1119,7 @@ class ScenarioTest < Minitest::Test
|
|
1046
1119
|
|
1047
1120
|
sleep 0.1
|
1048
1121
|
@db.query 'begin deferred'
|
1049
|
-
result = @db.
|
1122
|
+
result = @db.query_splat('select x from t')
|
1050
1123
|
assert_equal [1, 4], result
|
1051
1124
|
|
1052
1125
|
assert_raises(Extralite::BusyError) do
|
@@ -1080,7 +1153,7 @@ class ScenarioTest < Minitest::Test
|
|
1080
1153
|
@db.query('insert into t values (7, 8, 9)')
|
1081
1154
|
@db.query('commit')
|
1082
1155
|
|
1083
|
-
result = @db.
|
1156
|
+
result = @db.query_splat('select x from t')
|
1084
1157
|
assert_equal [1, 4, 7], result
|
1085
1158
|
ensure
|
1086
1159
|
t&.kill
|
@@ -1109,7 +1182,7 @@ class ScenarioTest < Minitest::Test
|
|
1109
1182
|
t1.join
|
1110
1183
|
t2.join
|
1111
1184
|
|
1112
|
-
assert_equal (1..100).to_a, @db.
|
1185
|
+
assert_equal (1..100).to_a, @db.query_splat('select x from t order by x')
|
1113
1186
|
ensure
|
1114
1187
|
t1&.kill
|
1115
1188
|
t2&.kill
|
@@ -1153,19 +1226,19 @@ class BackupTest < Minitest::Test
|
|
1153
1226
|
|
1154
1227
|
def test_backup
|
1155
1228
|
@src.backup(@dst)
|
1156
|
-
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.
|
1229
|
+
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.query_array('select * from t')
|
1157
1230
|
end
|
1158
1231
|
|
1159
1232
|
def test_backup_with_block
|
1160
1233
|
progress = []
|
1161
1234
|
@src.backup(@dst) { |r, t| progress << [r, t] }
|
1162
|
-
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.
|
1235
|
+
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.query_array('select * from t')
|
1163
1236
|
assert_equal [[2, 2]], progress
|
1164
1237
|
end
|
1165
1238
|
|
1166
1239
|
def test_backup_with_schema_names
|
1167
1240
|
@src.backup(@dst, 'main', 'temp')
|
1168
|
-
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.
|
1241
|
+
assert_equal [[1, 2, 3], [4, 5, 6]], @dst.query_array('select * from temp.t')
|
1169
1242
|
end
|
1170
1243
|
|
1171
1244
|
def test_backup_with_fn
|
@@ -1173,7 +1246,7 @@ class BackupTest < Minitest::Test
|
|
1173
1246
|
@src.backup(tmp_fn)
|
1174
1247
|
|
1175
1248
|
db = Extralite::Database.new(tmp_fn)
|
1176
|
-
assert_equal [[1, 2, 3], [4, 5, 6]], db.
|
1249
|
+
assert_equal [[1, 2, 3], [4, 5, 6]], db.query_array('select * from t')
|
1177
1250
|
end
|
1178
1251
|
end
|
1179
1252
|
|
@@ -1407,7 +1480,7 @@ class ConcurrencyTest < Minitest::Test
|
|
1407
1480
|
|
1408
1481
|
def test_progress_handler_invalid_arg
|
1409
1482
|
db = Extralite::Database.new(':memory:')
|
1410
|
-
|
1483
|
+
|
1411
1484
|
assert_raises(TypeError) { db.on_progress(period: :foo) }
|
1412
1485
|
assert_raises(TypeError) { db.on_progress(tick: :foo) }
|
1413
1486
|
assert_raises(ArgumentError) { db.on_progress(mode: :foo) }
|
@@ -1604,7 +1677,7 @@ end
|
|
1604
1677
|
class RactorTest < Minitest::Test
|
1605
1678
|
def test_ractor_simple
|
1606
1679
|
skip if SKIP_RACTOR_TESTS
|
1607
|
-
|
1680
|
+
|
1608
1681
|
fn = Tempfile.new('extralite_test_database_in_ractor').path
|
1609
1682
|
|
1610
1683
|
r = Ractor.new do
|
@@ -1620,7 +1693,7 @@ class RactorTest < Minitest::Test
|
|
1620
1693
|
r << 42
|
1621
1694
|
r.take # wait for ractor to terminate
|
1622
1695
|
|
1623
|
-
assert_equal 42, db.
|
1696
|
+
assert_equal 42, db.query_single_splat('select x from foo')
|
1624
1697
|
end
|
1625
1698
|
|
1626
1699
|
# Adapted from here: https://github.com/sparklemotion/sqlite3-ruby/pull/365/files
|
@@ -1681,7 +1754,7 @@ class RactorTest < Minitest::Test
|
|
1681
1754
|
|
1682
1755
|
final_check = Ractor.new(fn) do |rfn|
|
1683
1756
|
rdb = Extralite::Database.new(rfn, wal: true)
|
1684
|
-
count = rdb.
|
1757
|
+
count = rdb.query_single_splat('select count(*) from stress_test')
|
1685
1758
|
Ractor.yield count
|
1686
1759
|
end
|
1687
1760
|
count = final_check.take
|
@@ -1739,15 +1812,15 @@ class DatabaseTransformTest < Minitest::Test
|
|
1739
1812
|
], buf
|
1740
1813
|
end
|
1741
1814
|
|
1742
|
-
def
|
1815
|
+
def test_query_array_transform
|
1743
1816
|
transform = ->(h) { MyModel.new(h) }
|
1744
1817
|
|
1745
1818
|
sql = 'select a, b from t where a = ?'
|
1746
|
-
o = @db.
|
1819
|
+
o = @db.query_array(transform, sql, 1).first
|
1747
1820
|
assert_kind_of MyModel, o
|
1748
1821
|
assert_equal([1, 2], o.values)
|
1749
1822
|
|
1750
|
-
o = @db.
|
1823
|
+
o = @db.query_array(transform, sql, 4).first
|
1751
1824
|
assert_kind_of MyModel, o
|
1752
1825
|
assert_equal([4, 5], o.values)
|
1753
1826
|
|
@@ -1755,10 +1828,10 @@ class DatabaseTransformTest < Minitest::Test
|
|
1755
1828
|
assert_equal [
|
1756
1829
|
[1, 2],
|
1757
1830
|
[4, 5]
|
1758
|
-
], @db.
|
1831
|
+
], @db.query_array(transform, sql).map(&:values)
|
1759
1832
|
|
1760
1833
|
buf = []
|
1761
|
-
@db.
|
1834
|
+
@db.query_array(transform, sql) { |r| buf << r.values }
|
1762
1835
|
assert_equal [
|
1763
1836
|
[1, 2],
|
1764
1837
|
[4, 5]
|
@@ -1778,74 +1851,74 @@ class DatabaseTransformTest < Minitest::Test
|
|
1778
1851
|
assert_equal({ a: 4, b: 5 }, o.values)
|
1779
1852
|
end
|
1780
1853
|
|
1781
|
-
def
|
1854
|
+
def test_query_array_single_row_transform
|
1782
1855
|
transform = ->(h) { MyModel.new(h) }
|
1783
1856
|
|
1784
1857
|
sql = 'select a, b from t where a = ?'
|
1785
|
-
o = @db.
|
1858
|
+
o = @db.query_single_array(transform, sql, 1)
|
1786
1859
|
assert_kind_of MyModel, o
|
1787
1860
|
assert_equal([1, 2], o.values)
|
1788
1861
|
|
1789
|
-
o = @db.
|
1862
|
+
o = @db.query_single_array(transform, sql, 4)
|
1790
1863
|
assert_kind_of MyModel, o
|
1791
1864
|
assert_equal([4, 5], o.values)
|
1792
1865
|
end
|
1793
1866
|
|
1794
|
-
def
|
1867
|
+
def test_query_splat_single_column_transform
|
1795
1868
|
transform = ->(c) { JSON.parse(c, symbolize_names: true) }
|
1796
1869
|
sql = 'select c from t where a = ?'
|
1797
1870
|
|
1798
|
-
assert_equal([{ foo: 42, bar: 43 }], @db.
|
1799
|
-
assert_equal([{ foo: 45, bar: 46 }], @db.
|
1871
|
+
assert_equal([{ foo: 42, bar: 43 }], @db.query_splat(transform, sql, 1))
|
1872
|
+
assert_equal([{ foo: 45, bar: 46 }], @db.query_splat(transform, sql, 4))
|
1800
1873
|
|
1801
1874
|
sql = 'select c from t order by a'
|
1802
1875
|
assert_equal [
|
1803
1876
|
{ foo: 42, bar: 43 },
|
1804
1877
|
{ foo: 45, bar: 46 }
|
1805
|
-
], @db.
|
1878
|
+
], @db.query_splat(transform, sql)
|
1806
1879
|
|
1807
1880
|
buf = []
|
1808
|
-
@db.
|
1881
|
+
@db.query_splat(transform, sql) { |r| buf << r }
|
1809
1882
|
assert_equal [
|
1810
1883
|
{ foo: 42, bar: 43 },
|
1811
1884
|
{ foo: 45, bar: 46 }
|
1812
1885
|
], buf
|
1813
1886
|
end
|
1814
1887
|
|
1815
|
-
def
|
1888
|
+
def test_query_splat_single_row_single_column
|
1816
1889
|
transform = ->(c) { JSON.parse(c, symbolize_names: true) }
|
1817
1890
|
sql = 'select c from t where a = ?'
|
1818
1891
|
|
1819
|
-
assert_equal({ foo: 42, bar: 43 }, @db.
|
1820
|
-
assert_equal({ foo: 45, bar: 46 }, @db.
|
1892
|
+
assert_equal({ foo: 42, bar: 43 }, @db.query_single_splat(transform, sql, 1))
|
1893
|
+
assert_equal({ foo: 45, bar: 46 }, @db.query_single_splat(transform, sql, 4))
|
1821
1894
|
end
|
1822
1895
|
|
1823
|
-
def
|
1896
|
+
def test_query_splat_multi_column
|
1824
1897
|
transform = ->(a, b, c) { { a: a, b: b, c: JSON.parse(c, symbolize_names: true) } }
|
1825
1898
|
sql = 'select * from t where a = ?'
|
1826
1899
|
|
1827
|
-
assert_equal([{ a: 1, b: 2, c: { foo: 42, bar: 43 }}], @db.
|
1828
|
-
assert_equal([{ a: 4, b: 5, c: { foo: 45, bar: 46 }}], @db.
|
1900
|
+
assert_equal([{ a: 1, b: 2, c: { foo: 42, bar: 43 }}], @db.query_splat(transform, sql, 1))
|
1901
|
+
assert_equal([{ a: 4, b: 5, c: { foo: 45, bar: 46 }}], @db.query_splat(transform, sql, 4))
|
1829
1902
|
|
1830
1903
|
sql = 'select * from t order by a'
|
1831
1904
|
assert_equal [
|
1832
1905
|
{ a: 1, b: 2, c: { foo: 42, bar: 43 }},
|
1833
1906
|
{ a: 4, b: 5, c: { foo: 45, bar: 46 }}
|
1834
|
-
], @db.
|
1907
|
+
], @db.query_splat(transform, sql)
|
1835
1908
|
|
1836
1909
|
buf = []
|
1837
|
-
@db.
|
1910
|
+
@db.query_splat(transform, sql) { |r| buf << r }
|
1838
1911
|
assert_equal [
|
1839
1912
|
{ a: 1, b: 2, c: { foo: 42, bar: 43 }},
|
1840
1913
|
{ a: 4, b: 5, c: { foo: 45, bar: 46 }}
|
1841
1914
|
], buf
|
1842
1915
|
end
|
1843
1916
|
|
1844
|
-
def
|
1917
|
+
def test_query_splat_single_row_multi_column
|
1845
1918
|
transform = ->(a, b, c) { { a: a, b: b, c: JSON.parse(c, symbolize_names: true) } }
|
1846
1919
|
sql = 'select * from t where a = ?'
|
1847
1920
|
|
1848
|
-
assert_equal({ a: 1, b: 2, c: { foo: 42, bar: 43 }}, @db.
|
1849
|
-
assert_equal({ a: 4, b: 5, c: { foo: 45, bar: 46 }}, @db.
|
1921
|
+
assert_equal({ a: 1, b: 2, c: { foo: 42, bar: 43 }}, @db.query_single_splat(transform, sql, 1))
|
1922
|
+
assert_equal({ a: 4, b: 5, c: { foo: 45, bar: 46 }}, @db.query_single_splat(transform, sql, 4))
|
1850
1923
|
end
|
1851
1924
|
end
|