extralite-bundle 2.3 → 2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -103,7 +103,8 @@ class IteratorTest < MiniTest::Test
103
103
  end
104
104
 
105
105
  def test_return_from_block_issue_26
106
- db = Extralite::Database.new('/tmp/locked.db')
106
+ fn = Tempfile.new('extralite_test_return_from_block_issue_26').path
107
+ db = Extralite::Database.new(fn)
107
108
 
108
109
  λ = ->(sql) {
109
110
  db.prepare(sql).each { |r| r.each { |_, v| return v } }
data/test/test_query.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'helper'
4
+ require 'date'
4
5
 
5
6
  class QueryTest < MiniTest::Test
6
7
  def setup
@@ -267,7 +268,7 @@ class QueryTest < MiniTest::Test
267
268
  end
268
269
 
269
270
  def test_query_each_without_block
270
- query = @db.prepare('select * from t')
271
+ query = @db.prepare('select * from t')
271
272
  iter = query.each
272
273
  assert_kind_of Extralite::Iterator, iter
273
274
 
@@ -294,7 +295,7 @@ class QueryTest < MiniTest::Test
294
295
  end
295
296
 
296
297
  def test_query_each_ary_without_block
297
- query = @db.prepare('select * from t')
298
+ query = @db.prepare('select * from t')
298
299
  iter = query.each_ary
299
300
  assert_kind_of Extralite::Iterator, iter
300
301
 
@@ -322,7 +323,7 @@ class QueryTest < MiniTest::Test
322
323
  end
323
324
 
324
325
  def test_query_each_single_column_without_block
325
- query = @db.prepare('select x from t')
326
+ query = @db.prepare('select x from t')
326
327
  iter = query.each_single_column
327
328
  assert_kind_of Extralite::Iterator, iter
328
329
 
@@ -375,6 +376,9 @@ class QueryTest < MiniTest::Test
375
376
  def test_query_parameter_binding_simple
376
377
  r = @db.prepare('select x, y, z from t where x = ?').bind(1).next
377
378
  assert_equal({ x: 1, y: 2, z: 3 }, r)
379
+
380
+ error = assert_raises(Extralite::ParameterError) { @db.prepare('select ?').bind(Date.today).next }
381
+ assert_equal error.message, 'Cannot bind parameter at position 1 of type Date'
378
382
  end
379
383
 
380
384
  def test_query_parameter_binding_with_index
@@ -404,6 +408,38 @@ class QueryTest < MiniTest::Test
404
408
  assert_equal({ x: 4, y: 5, z: 6 }, r)
405
409
  end
406
410
 
411
+ class Foo; end
412
+
413
+ def test_parameter_binding_from_hash
414
+ assert_equal 42, @db.prepare('select :bar').bind(foo: 41, bar: 42).next_single_column
415
+ assert_equal 42, @db.prepare('select :bar').bind('foo' => 41, 'bar' => 42).next_single_column
416
+ assert_equal 42, @db.prepare('select ?8').bind(7 => 41, 8 => 42).next_single_column
417
+ assert_nil @db.prepare('select :bar').bind(foo: 41).next_single_column
418
+
419
+ error = assert_raises(Extralite::ParameterError) { @db.prepare('select ?').bind(Foo.new => 42).next_single_column }
420
+ assert_equal error.message, 'Cannot bind parameter with a key of type QueryTest::Foo'
421
+
422
+ error = assert_raises(Extralite::ParameterError) { @db.prepare('select ?').bind(%w[a b] => 42).next_single_column }
423
+ assert_equal error.message, 'Cannot bind parameter with a key of type Array'
424
+ end
425
+
426
+ def test_parameter_binding_from_struct
427
+ foo_bar = Struct.new(:":foo", :bar)
428
+ value = foo_bar.new(41, 42)
429
+ assert_equal 41, @db.prepare('select :foo').bind(value).next_single_column
430
+ assert_equal 42, @db.prepare('select :bar').bind(value).next_single_column
431
+ assert_nil @db.prepare('select :baz').bind(value).next_single_column
432
+ end
433
+
434
+ def test_parameter_binding_from_data_class
435
+ skip "Data isn't supported in Ruby < 3.2" if RUBY_VERSION < '3.2'
436
+
437
+ foo_bar = Data.define(:":foo", :bar)
438
+ value = foo_bar.new(":foo": 41, bar: 42)
439
+ assert_equal 42, @db.prepare('select :bar').bind(value).next_single_column
440
+ assert_nil @db.prepare('select :baz').bind(value).next_single_column
441
+ end
442
+
407
443
  def test_query_columns
408
444
  r = @db.prepare("select 'abc' as a, 'def' as b").columns
409
445
  assert_equal [:a, :b], r
@@ -442,7 +478,25 @@ class QueryTest < MiniTest::Test
442
478
  assert_equal [[1, 2, 3], [4, 5, 6], [42, 8, 9]], @db.query_ary('select * from t order by z')
443
479
  end
444
480
 
445
- def test_query_execute_multi
481
+ def test_query_execute_with_mixed_params
482
+ @db.execute 'delete from t'
483
+ q = @db.prepare('insert into t values (?, ?, ?)')
484
+
485
+ q.execute(1, [2], 3)
486
+ q.execute([4, 5], 6)
487
+ q.execute([7], 8, [9])
488
+
489
+ assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]], @db.query_ary('select * from t order by z')
490
+ end
491
+
492
+ def test_query_chverons
493
+ q = @db.prepare('update t set x = ? where z = ?')
494
+ assert_equal q, (q << [42, 9])
495
+ assert_equal [[1, 2, 3], [4, 5, 6], [42, 8, 9]], @db.query_ary('select * from t order by z')
496
+ end
497
+
498
+
499
+ def test_query_batch_execute
446
500
  @db.query('create table foo (a, b, c)')
447
501
  assert_equal [], @db.query('select * from foo')
448
502
 
@@ -452,14 +506,336 @@ class QueryTest < MiniTest::Test
452
506
  ]
453
507
 
454
508
  p = @db.prepare('insert into foo values (?, ?, ?)')
455
- changes = p.execute_multi(records)
509
+ changes = p.batch_execute(records)
456
510
 
457
511
  assert_equal 2, changes
458
512
  assert_equal [
459
513
  { a: 1, b: '2', c: 3 },
460
514
  { a: '4', b: 5, c: 6 }
461
515
  ], @db.query('select * from foo')
462
- end
516
+ end
517
+
518
+ def test_query_batch_execute_with_each_interface
519
+ @db.query('create table foo (a)')
520
+ assert_equal [], @db.query('select * from foo')
521
+
522
+ p = @db.prepare('insert into foo values (?)')
523
+ changes = p.batch_execute(1..3)
524
+
525
+ assert_equal 3, changes
526
+ assert_equal [
527
+ { a: 1 },
528
+ { a: 2 },
529
+ { a: 3 }
530
+ ], @db.query('select * from foo')
531
+ end
532
+
533
+ def test_query_batch_execute_with_proc
534
+ source = [42, 43, 44]
535
+
536
+ @db.query('create table foo (a)')
537
+ assert_equal [], @db.query('select * from foo')
538
+
539
+ p = @db.prepare('insert into foo values (?)')
540
+ pr = proc { source.shift }
541
+ changes = p.batch_execute(pr)
542
+
543
+ assert_equal 3, changes
544
+ assert_equal [
545
+ { a: 42 },
546
+ { a: 43 },
547
+ { a: 44 }
548
+ ], @db.query('select * from foo')
549
+ end
550
+
551
+ def test_query_batch_query_with_array
552
+ @db.query('create table foo (a integer primary key, b)')
553
+ assert_equal [], @db.query('select * from foo')
554
+
555
+ data = [5, 4, 3]
556
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
557
+ assert_equal [
558
+ [[1, 5]],
559
+ [[2, 4]],
560
+ [[3, 3]]
561
+ ], results
562
+
563
+ q = @db.prepare('update foo set b = ? returning *')
564
+
565
+ results = q.batch_query([42, 43])
566
+ assert_equal [
567
+ [{ a: 1, b: 42 }, { a: 2, b: 42 }, { a: 3, b: 42 }],
568
+ [{ a: 1, b: 43 }, { a: 2, b: 43 }, { a: 3, b: 43 }]
569
+ ], results
570
+
571
+ array = []
572
+ changes = q.batch_query([44, 45]) do |rows|
573
+ array << rows
574
+ end
575
+ assert_equal 6, changes
576
+ assert_equal [
577
+ [{ a: 1, b: 44 }, { a: 2, b: 44 }, { a: 3, b: 44 }],
578
+ [{ a: 1, b: 45 }, { a: 2, b: 45 }, { a: 3, b: 45 }]
579
+ ], array
580
+ end
581
+
582
+ def test_query_batch_query_with_enumerable
583
+ @db.query('create table foo (a integer primary key, b)')
584
+ assert_equal [], @db.query('select * from foo')
585
+
586
+ data = [5, 4, 3]
587
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
588
+ assert_equal [
589
+ [[1, 5]],
590
+ [[2, 4]],
591
+ [[3, 3]]
592
+ ], results
593
+
594
+ q = @db.prepare('update foo set b = ? returning *')
595
+
596
+ results = q.batch_query(42..43)
597
+ assert_equal [
598
+ [{ a: 1, b: 42 }, { a: 2, b: 42 }, { a: 3, b: 42 }],
599
+ [{ a: 1, b: 43 }, { a: 2, b: 43 }, { a: 3, b: 43 }]
600
+ ], results
601
+
602
+ array = []
603
+ changes = q.batch_query(44..45) do |rows|
604
+ array << rows
605
+ end
606
+ assert_equal 6, changes
607
+ assert_equal [
608
+ [{ a: 1, b: 44 }, { a: 2, b: 44 }, { a: 3, b: 44 }],
609
+ [{ a: 1, b: 45 }, { a: 2, b: 45 }, { a: 3, b: 45 }]
610
+ ], array
611
+ end
612
+
613
+ def parameter_source_proc(values)
614
+ proc { values.shift }
615
+ end
616
+
617
+ def test_query_batch_query_with_proc
618
+ @db.query('create table foo (a integer primary key, b)')
619
+ assert_equal [], @db.query('select * from foo')
620
+
621
+ data = [5, 4, 3]
622
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
623
+ assert_equal [
624
+ [[1, 5]],
625
+ [[2, 4]],
626
+ [[3, 3]]
627
+ ], results
628
+
629
+ q = @db.prepare('update foo set b = ? returning *')
630
+
631
+ pr = parameter_source_proc([42, 43])
632
+ results = q.batch_query(pr)
633
+ assert_equal [
634
+ [{ a: 1, b: 42 }, { a: 2, b: 42 }, { a: 3, b: 42 }],
635
+ [{ a: 1, b: 43 }, { a: 2, b: 43 }, { a: 3, b: 43 }]
636
+ ], results
637
+
638
+ array = []
639
+ pr = parameter_source_proc([44, 45])
640
+ changes = q.batch_query(pr) do |rows|
641
+ array << rows
642
+ end
643
+ assert_equal 6, changes
644
+ assert_equal [
645
+ [{ a: 1, b: 44 }, { a: 2, b: 44 }, { a: 3, b: 44 }],
646
+ [{ a: 1, b: 45 }, { a: 2, b: 45 }, { a: 3, b: 45 }]
647
+ ], array
648
+ end
649
+
650
+ def test_query_batch_query_ary_with_array
651
+ @db.query('create table foo (a integer primary key, b)')
652
+ assert_equal [], @db.query('select * from foo')
653
+
654
+ data = [5, 4, 3]
655
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
656
+ assert_equal [
657
+ [[1, 5]],
658
+ [[2, 4]],
659
+ [[3, 3]]
660
+ ], results
661
+
662
+ q = @db.prepare('update foo set b = ? returning *')
663
+
664
+ results = q.batch_query_ary([42, 43])
665
+ assert_equal [
666
+ [[1, 42], [2, 42], [3, 42]],
667
+ [[1, 43], [2, 43], [3, 43]]
668
+ ], results
669
+
670
+ array = []
671
+ changes = q.batch_query_ary([44, 45]) do |rows|
672
+ array << rows
673
+ end
674
+ assert_equal 6, changes
675
+ assert_equal [
676
+ [[1, 44], [2, 44], [3, 44]],
677
+ [[1, 45], [2, 45], [3, 45]]
678
+ ], array
679
+ end
680
+
681
+ def test_query_batch_query_ary_with_enumerable
682
+ @db.query('create table foo (a integer primary key, b)')
683
+ assert_equal [], @db.query('select * from foo')
684
+
685
+ data = [5, 4, 3]
686
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
687
+ assert_equal [
688
+ [[1, 5]],
689
+ [[2, 4]],
690
+ [[3, 3]]
691
+ ], results
692
+
693
+ q = @db.prepare('update foo set b = ? returning *')
694
+
695
+ results = q.batch_query_ary(42..43)
696
+ assert_equal [
697
+ [[1, 42], [2, 42], [3, 42]],
698
+ [[1, 43], [2, 43], [3, 43]]
699
+ ], results
700
+
701
+ array = []
702
+ changes = q.batch_query_ary(44..45) do |rows|
703
+ array << rows
704
+ end
705
+ assert_equal 6, changes
706
+ assert_equal [
707
+ [[1, 44], [2, 44], [3, 44]],
708
+ [[1, 45], [2, 45], [3, 45]]
709
+ ], array
710
+ end
711
+
712
+ def test_query_batch_query_ary_with_proc
713
+ @db.query('create table foo (a integer primary key, b)')
714
+ assert_equal [], @db.query('select * from foo')
715
+
716
+ data = [5, 4, 3]
717
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
718
+ assert_equal [
719
+ [[1, 5]],
720
+ [[2, 4]],
721
+ [[3, 3]]
722
+ ], results
723
+
724
+ q = @db.prepare('update foo set b = ? returning *')
725
+
726
+ pr = parameter_source_proc([42, 43])
727
+ results = q.batch_query_ary(pr)
728
+ assert_equal [
729
+ [[1, 42], [2, 42], [3, 42]],
730
+ [[1, 43], [2, 43], [3, 43]]
731
+ ], results
732
+
733
+ array = []
734
+ pr = parameter_source_proc([44, 45])
735
+ changes = q.batch_query_ary(pr) do |rows|
736
+ array << rows
737
+ end
738
+ assert_equal 6, changes
739
+ assert_equal [
740
+ [[1, 44], [2, 44], [3, 44]],
741
+ [[1, 45], [2, 45], [3, 45]]
742
+ ], array
743
+ end
744
+
745
+ def test_query_batch_query_single_column_with_array
746
+ @db.query('create table foo (a integer primary key, b)')
747
+ assert_equal [], @db.query('select * from foo')
748
+
749
+ data = [5, 4, 3]
750
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
751
+ assert_equal [
752
+ [[1, 5]],
753
+ [[2, 4]],
754
+ [[3, 3]]
755
+ ], results
756
+
757
+ q = @db.prepare('update foo set b = ? returning b * 10 + a')
758
+
759
+ results = q.batch_query_single_column([42, 43])
760
+ assert_equal [
761
+ [421, 422, 423],
762
+ [431, 432, 433]
763
+ ], results
764
+
765
+ array = []
766
+ changes = q.batch_query_single_column([44, 45]) do |rows|
767
+ array << rows
768
+ end
769
+ assert_equal 6, changes
770
+ assert_equal [
771
+ [441, 442, 443],
772
+ [451, 452, 453]
773
+ ], array
774
+ end
775
+
776
+ def test_query_batch_query_single_column_with_enumerable
777
+ @db.query('create table foo (a integer primary key, b)')
778
+ assert_equal [], @db.query('select * from foo')
779
+
780
+ data = [5, 4, 3]
781
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
782
+ assert_equal [
783
+ [[1, 5]],
784
+ [[2, 4]],
785
+ [[3, 3]]
786
+ ], results
787
+
788
+ q = @db.prepare('update foo set b = ? returning b * 10 + a')
789
+
790
+ results = q.batch_query_single_column(42..43)
791
+ assert_equal [
792
+ [421, 422, 423],
793
+ [431, 432, 433]
794
+ ], results
795
+
796
+ array = []
797
+ changes = q.batch_query_single_column(44..45) do |rows|
798
+ array << rows
799
+ end
800
+ assert_equal 6, changes
801
+ assert_equal [
802
+ [441, 442, 443],
803
+ [451, 452, 453]
804
+ ], array
805
+ end
806
+
807
+ def test_query_batch_query_single_column_with_proc
808
+ @db.query('create table foo (a integer primary key, b)')
809
+ assert_equal [], @db.query('select * from foo')
810
+
811
+ data = [5, 4, 3]
812
+ results = @db.batch_query_ary('insert into foo (b) values (?) returning *', data)
813
+ assert_equal [
814
+ [[1, 5]],
815
+ [[2, 4]],
816
+ [[3, 3]]
817
+ ], results
818
+
819
+ q = @db.prepare('update foo set b = ? returning b * 10 + a')
820
+
821
+ pr = parameter_source_proc([42, 43])
822
+ results = q.batch_query_single_column(pr)
823
+ assert_equal [
824
+ [421, 422, 423],
825
+ [431, 432, 433]
826
+ ], results
827
+
828
+ array = []
829
+ pr = parameter_source_proc([44, 45])
830
+ changes = q.batch_query_single_column(pr) do |rows|
831
+ array << rows
832
+ end
833
+ assert_equal 6, changes
834
+ assert_equal [
835
+ [441, 442, 443],
836
+ [451, 452, 453]
837
+ ], array
838
+ end
463
839
 
464
840
  def test_query_status
465
841
  assert_equal 0, @query.status(Extralite::SQLITE_STMTSTATUS_RUN)
@@ -516,4 +892,24 @@ class QueryTest < MiniTest::Test
516
892
  q = @db.prepare('select x from t')
517
893
  assert_match /^\#\<Extralite::Query:0x[0-9a-f]+ #{q.sql.inspect}\>$/, q.inspect
518
894
  end
895
+
896
+ def test_query_clone
897
+ q1 = @db.prepare('select x from t')
898
+ q2 = q1.clone
899
+
900
+ assert_kind_of Extralite::Query, q2
901
+ assert_equal @db, q2.database
902
+ assert_equal q1.sql, q2.sql
903
+ refute_equal q1, q2
904
+ end
905
+
906
+ def test_query_dup
907
+ q1 = @db.prepare('select x from t')
908
+ q2 = q1.dup
909
+
910
+ assert_kind_of Extralite::Query, q2
911
+ assert_equal @db, q2.database
912
+ assert_equal q1.sql, q2.sql
913
+ refute_equal q1, q2
914
+ end
519
915
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extralite-bundle
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.3'
4
+ version: '2.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-12 00:00:00.000000000 Z
11
+ date: 2024-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -88,12 +88,15 @@ extensions:
88
88
  extra_rdoc_files:
89
89
  - README.md
90
90
  files:
91
+ - ".editorconfig"
91
92
  - ".github/FUNDING.yml"
93
+ - ".github/workflows/test-bundle.yml"
92
94
  - ".github/workflows/test.yml"
93
95
  - ".gitignore"
94
96
  - ".yardopts"
95
97
  - CHANGELOG.md
96
98
  - Gemfile
99
+ - Gemfile-bundle
97
100
  - Gemfile.lock
98
101
  - LICENSE
99
102
  - README.md
@@ -120,7 +123,11 @@ files:
120
123
  - lib/sequel/adapters/extralite.rb
121
124
  - test/extensions/text.dylib
122
125
  - test/extensions/text.so
126
+ - test/fixtures/image.png
123
127
  - test/helper.rb
128
+ - test/issue-38.rb
129
+ - test/issue-54.rb
130
+ - test/issue-59.rb
124
131
  - test/perf_ary.rb
125
132
  - test/perf_hash.rb
126
133
  - test/perf_prepared.rb
@@ -150,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
157
  requirements:
151
158
  - - ">="
152
159
  - !ruby/object:Gem::Version
153
- version: '2.7'
160
+ version: '3.0'
154
161
  required_rubygems_version: !ruby/object:Gem::Requirement
155
162
  requirements:
156
163
  - - ">="