-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.c
1398 lines (1063 loc) · 31.3 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "sqlite3.h"
#include "assert.h"
sqlite3 *db;
#include "db_functions.c"
/*
TO BE TESTED ON STORED PROCEDURES:
- DECLARE
- SET
- IF THEN .. ELSEIF .. ELSE .. END IF
- LOOP .. BREAK .. CONTINUE .. END LOOP
- FOREACH .. BREAK .. CONTINUE .. END LOOP
- CALL
- RETURN
- RAISE EXCEPTION
some tests:
- RETURN without arguments
- RETURN with one argument (fixed)
- RETURN with one argument (variable)
- RETURN with many arguments (fixed and variable)
- RETURN with 1 array argument
- RETURN with 1 array of arrays
- RETURN with expression
- SET with ARRAY literal
- SET with ARRAY variable
- SET with SELECT / expression
- SET with INSERT and RETURNING
- SET with UPDATE and RETURNING
- SET with DELETE and RETURNING
- SET with CALL
- IF blocks that are active:
- on the IF branch
- on the first ELSEIF branch
- on the second ELSEIF branch
- on the ELSE branch
also:
- IF with RETURN
- IF with SET
- IF with CALL
- IF with LOOP
- IF with FOREACH
- IF with nested LOOP
- IF with nested FOREACH
- nested IF blocks
- basic LOOP block
- LOOP block with BREAK
- LOOP block with CONTINUE
- LOOP block with RETURN
- nested LOOP blocks
- FOREACH with ARRAY literal
- FOREACH with ARRAY variable
- FOREACH with SELECT
- FOREACH with INSERT and RETURNING
- FOREACH with UPDATE and RETURNING
- FOREACH with DELETE and RETURNING
- FOREACH with CALL
- FOREACH VALUE
- FOREACH block with BREAK
- FOREACH block with CONTINUE
- FOREACH block with RETURN
- nested FOREACH blocks
- CALL (another procedure) without arguments
- CALL (another procedure) with fixed/literal arguments
- CALL (another procedure) with variables as arguments
- recursive CALL (should fail)
- RAISE EXCEPTION
invalid syntax:
- RETURN with many arrays
*/
int main(){
int rc;
rc = sqlite3_open(":memory:", &db);
assert(rc==SQLITE_OK);
// RETURN without arguments (insert on a table and then returns before another insertion)
db_execute("CREATE TABLE t1 (id INTEGER PRIMARY KEY, a, b, c, d)");
db_execute("CREATE PROCEDURE return_nothing() BEGIN "
"INSERT INTO t1 VALUES (1, 123, 2.5, 'hello world!', x'4142434445');"
"RETURN;"
"INSERT INTO t1 VALUES (2, 456, 3.7, 'how are you?', x'782059207a');"
"END;"
);
db_check_many("CALL return_nothing()",
NULL
);
db_check_many("SELECT * FROM t1",
"1|123|2.5|hello world!|ABCDE",
NULL
);
// RETURN with one argument (fixed)
db_execute("CREATE PROCEDURE return_11() BEGIN RETURN 11; END;");
// CALL without arguments
db_check_int("call return_11()", 11);
// RETURN with one argument (variable)
db_execute("CREATE PROCEDURE echo(@value) BEGIN RETURN @value; END;");
db_check_int("call echo(11)", 11);
db_check_double("call echo(2.5)", 2.5, 0.0001);
db_check_str("call echo('hello!')", "hello!");
db_check_str("call echo('hello''world!')", "hello'world!");
db_check_str("call echo(x'6120622063')", "a b c");
db_check_many("CALL echo('hello''world!')",
"hello'world!",
NULL
);
db_check_many("CALL echo(ARRAY(11,2.5,'hello!',x'6120622063'))",
"11",
"2.5",
"hello!",
"a b c",
NULL
);
db_check_many("CALL echo( ARRAY( 11 , 2.5 , 'hello!' , x'6120622063' ) )",
"11",
"2.5",
"hello!",
"a b c",
NULL
);
db_check_many("CALL echo(ARRAY( ARRAY(1,'first',1.1), ARRAY(2,'second',2.2), ARRAY(3,'third',3.3) ))",
"1|first|1.1",
"2|second|2.2",
"3|third|3.3",
NULL
);
db_check_many("CALL echo( ARRAY( ARRAY(11,2.3,'hello!',x'6120622063') , ARRAY(12,2.5,'world!',x'4120422043') ) )",
"11|2.3|hello!|a b c",
"12|2.5|world!|A B C",
NULL
);
db_check_many("CALL echo( ARRAY( ARRAY(11,2.3,'hello!',x'6120622063') , ARRAY(12,2.5,'world!',x'4120422043') , ARRAY(13,2.7,'bye!',x'782059207a') ) )",
"11|2.3|hello!|a b c",
"12|2.5|world!|A B C",
"13|2.7|bye!|x Y z",
NULL
);
// RETURN with many literal arguments on the same row
db_execute("CREATE PROCEDURE return_many_literal() BEGIN "
"RETURN 123, 2.5, 'hello world', x'4142434445';"
"END;"
);
db_check_many("CALL return_many_literal()",
"123|2.5|hello world|ABCDE",
NULL
);
// RETURN with many literal arguments on different rows (ARRAY literal)
#if 0
db_execute("CREATE PROCEDURE return_array_literal() BEGIN "
"RETURN ARRAY(123, 2.5, 'hello world', x'4142434445');"
"END;"
);
db_check_many("CALL return_array_literal()",
"123",
"2.5",
"hello world",
"ABCDE",
NULL
);
#endif
// RETURN with many variable arguments on the same row
db_execute("CREATE PROCEDURE return_many_variable(@a, @b, @c, @d) BEGIN "
"RETURN @a, @b, @c, @d;"
"END;"
);
db_check_many("CALL return_many_variable(123, 2.5, 'hello world', x'4142434445')",
"123|2.5|hello world|ABCDE",
NULL
);
// RETURN with array of variables
#if 0
db_execute("CREATE PROCEDURE return_array_variables(@a, @b, @c, @d) BEGIN "
"RETURN ARRAY(@a, @b, @c, @d);"
"END;"
);
db_check_many("CALL return_array_variables(123, 2.5, 'hello world', x'4142434445')",
"123",
"2.5",
"hello world",
"ABCDE",
NULL
);
#endif
// RETURN with expression
db_execute("CREATE PROCEDURE sum(@a, @b) BEGIN RETURN @a + @b; END;");
db_check_int("call sum(11, 22)", 33);
db_check_double("call sum(11.1, 22.2)", 33.3, 0.0001);
db_execute("CREATE PROCEDURE div(@a, @b) BEGIN RETURN @b / @a + 1; END;");
db_check_int("call div(11, 33)", 4);
db_check_double("call div(8, 100.0)", 13.5, 0.0001);
db_execute("CREATE PROCEDURE concat(@a, @b) BEGIN RETURN @a || @b; END;");
db_check_str("call concat('hello', 'world')", "helloworld");
db_check_str("call concat(11, 22)", "1122");
////////////////////////////////////////////////////////////////////////////////
// SET
////////////////////////////////////////////////////////////////////////////////
// SET with ARRAY literal
db_execute(
"CREATE PROCEDURE set_array_literal() BEGIN "
"SET @arr = ARRAY(11,2.5,'hello!',x'6120622063');"
"RETURN @arr;"
"END;"
);
db_check_many("CALL set_array_literal()",
"11",
"2.5",
"hello!",
"a b c",
NULL
);
// SET with ARRAY variable
#if 0
db_execute(
"CREATE PROCEDURE set_array_variable(@arr) BEGIN "
"SET @copy = @arr;"
"RETURN @copy;"
"END;"
);
db_check_many("CALL set_array_variable( ARRAY(11,2.5,'hello!',x'6120622063') )",
"11",
"2.5",
"hello!",
"a b c",
NULL
);
#endif
// SET with SELECT (and expression) (pass one value as argument and multiply by 2)
db_execute(
"CREATE PROCEDURE set_select(@a) BEGIN "
"SET @b = SELECT @a * 2;"
"RETURN @b;"
"END;"
);
db_check_int("CALL set_select(11)", 22);
db_check_double("CALL set_select(2.2)", 4.4, 0.0001);
// SET with SELECT (and expression) (pass one value as argument and multiply by 2)
db_execute(
"CREATE PROCEDURE set_select2(@a, @b) BEGIN "
"SET @c = 2;"
"SET @res = SELECT @a * @b + @c;"
"RETURN @res;"
"END;"
);
db_check_int("CALL set_select2(11, 3)", 35);
db_check_double("CALL set_select2(2.2, 3)", 8.6, 0.0001);
// SET with expression (pass one value as argument and multiply by 3)
db_execute(
"CREATE PROCEDURE set_expression(@a) BEGIN "
"SET @b = 2;"
"SET @res = @a * @b + 3;"
"RETURN @res;"
"END;"
);
db_check_int("CALL set_expression(11)", 25);
db_check_double("CALL set_expression(2.2)", 7.4, 0.0001);
// SET with SELECT from table (many rows)
db_execute("CREATE TABLE test (id integer primary key, a integer, b real, c text, d blob)");
db_execute("INSERT INTO test (a,b,c,d) VALUES (11,2.5,'hello!',x'6120622063')");
db_execute("INSERT INTO test (a,b,c,d) VALUES (22,3.5,'world!',x'6220622064')");
db_execute("INSERT INTO test (a,b,c,d) VALUES (33,4.5,'foo!',x'6320622065')");
db_execute("INSERT INTO test (a,b,c,d) VALUES (44,5.5,'bar!',x'6420622066')");
db_execute(
"CREATE PROCEDURE set_select_table(@column) BEGIN "
" IF @column = 'a' THEN"
" SET @values = (SELECT a FROM test);"
" ELSEIF @column = 'b' THEN"
" SET @values = (SELECT b FROM test);"
" ELSEIF @column = 'c' THEN"
" SET @values = (SELECT c FROM test);"
" ELSEIF @column = 'd' THEN"
" SET @values = (SELECT d FROM test);"
" ELSE"
" SET @values = (SELECT * FROM test);"
" END IF;"
"RETURN @values;"
"END;"
);
db_check_many("CALL set_select_table('a')",
"11",
"22",
"33",
"44",
NULL
);
db_check_many("CALL set_select_table('b')",
"2.5",
"3.5",
"4.5",
"5.5",
NULL
);
db_check_many("CALL set_select_table('c')",
"hello!",
"world!",
"foo!",
"bar!",
NULL
);
db_check_many("CALL set_select_table('d')",
"a b c",
"b b d",
"c b e",
"d b f",
NULL
);
db_check_many("CALL set_select_table('x')",
"1|11|2.5|hello!|a b c",
"2|22|3.5|world!|b b d",
"3|33|4.5|foo!|c b e",
"4|44|5.5|bar!|d b f",
NULL
);
// SET with INSERT and RETURNING
#if 0
db_execute("DROP TABLE IF EXISTS test");
db_execute("CREATE TABLE test (id integer primary key, a integer, b real, c text, d blob)");
db_execute(
"CREATE PROCEDURE set_insert_returning() BEGIN "
"SET @id = INSERT INTO test (a,b,c,d) VALUES (11,2.5,'hello!',x'6120622063') RETURNING id;"
"RETURN @id;"
"END;"
);
db_check_int("CALL set_insert_returning()", 1);
db_check_int("CALL set_insert_returning()", 2);
db_check_int("CALL set_insert_returning()", 3);
// SET with UPDATE and RETURNING
db_execute(
"CREATE PROCEDURE set_update_returning() BEGIN "
"SET @id = UPDATE test SET a = 22, b = 3.5, c = 'world!', d = x'4120422043' WHERE id = 1 RETURNING id;"
"RETURN @id;"
"END;"
);
db_check_int("CALL set_update_returning()", 1);
db_execute(
"CREATE PROCEDURE set_update_all_returning() BEGIN "
"SET @ids = (UPDATE test SET a = 33, b = 4.5, c = 'bye!', d = x'582059207A' RETURNING id);"
"RETURN @ids;"
"END;"
);
db_check_many("CALL set_update_all_returning()",
"1",
"2",
"3",
NULL
);
// SET with SELECT and many rows
db_execute(
"CREATE PROCEDURE set_select_many() BEGIN "
"SET @a = (SELECT id FROM test);"
"RETURN @a;"
"END;"
);
db_check_many("CALL set_select_many()",
"1",
"2",
"3",
NULL
);
// SET with DELETE and RETURNING
db_execute(
"CREATE PROCEDURE set_delete_returning() BEGIN "
"SET @ids = (DELETE FROM test WHERE a = 33 RETURNING id);"
"RETURN @ids;"
"END;"
);
db_check_many("CALL set_delete_returning()",
"1",
"2",
"3",
NULL
);
#endif
// SET with CALL - procedure 1 (set_call) calls procedure 2 (sum) passing 2 values to it
db_execute(
"CREATE PROCEDURE set_call(@a, @b) BEGIN "
"SET @c = CALL sum(@a, @b);"
"RETURN @c;"
"END;"
);
db_execute(
"CREATE OR REPLACE PROCEDURE sum(@a, @b) BEGIN "
"SET @c = @a + @b;"
"RETURN @c;"
"END;"
);
db_check_int("CALL set_call(11, 22)", 33);
db_check_double("CALL set_call(2.2, 3.3)", 5.5, 0.0001);
// SET with CALL - parameters in different order
db_execute(
"CREATE PROCEDURE calc(@nsub, @nnum, @ndiv) BEGIN "
"SET @res = @nnum / @ndiv - @nsub;"
"RETURN @res;"
"END;"
);
db_execute(
"CREATE PROCEDURE test_call2(@num, @div, @sub) BEGIN "
"SET @result = CALL calc(@sub, @num, @div);"
"RETURN @result;"
"END;"
);
db_check_int("CALL test_call2(10, 2, 1)", 4);
db_check_double("CALL test_call2(100.0, 8, 2)", 10.5, 0.0001);
// SET with CALL - returning many rows on a single call
db_execute(
"CREATE PROCEDURE set_call_many() BEGIN "
"SET @a = (CALL echo(ARRAY(11,2.5,'hello!',x'6120622063')));"
"RETURN @a;"
"END;"
);
db_check_many("CALL set_call_many()",
"11",
"2.5",
"hello!",
"a b c",
NULL
);
////////////////////////////////////////////////////////////////////////////////
// IF .. THEN .. ELSEIF .. THEN .. ELSE .. END IF
////////////////////////////////////////////////////////////////////////////////
/*
- IF blocks that are active:
- on the IF branch
- on the first ELSEIF branch
- on the second ELSEIF branch
- on the ELSE branch
*/
// IF with SELECT
#if 0
db_execute(
"CREATE PROCEDURE if_select(@a) BEGIN "
"IF SELECT @a > 10 THEN "
" SET @b = 1;"
"ELSEIF SELECT @a > 5 THEN "
" SET @b = 2;"
"ELSEIF SELECT @a > 0 THEN "
" SET @b = 3;"
"ELSE "
" SET @b = 4;"
"END IF;"
"RETURN @b;"
"END;"
);
db_check_int("CALL if_select(11)", 1);
db_check_int("CALL if_select(6)", 2);
db_check_int("CALL if_select(1)", 3);
db_check_int("CALL if_select(-1)", 4);
#endif
// IF with expression
db_execute(
"CREATE PROCEDURE if_expression(@a) BEGIN "
"IF @a > 10 THEN "
" SET @b = 1;"
"ELSEIF @a > 5 THEN "
" SET @b = 2;"
"ELSEIF @a > 0 THEN "
" SET @b = 3;"
"ELSE "
" SET @b = 4;"
"END IF;"
"RETURN @b;"
"END;"
);
db_check_int("CALL if_expression(11)", 1);
db_check_int("CALL if_expression(6)", 2);
db_check_int("CALL if_expression(1)", 3);
db_check_int("CALL if_expression(-1)", 4);
// nested IF blocks
db_execute(
"CREATE PROCEDURE if_nested(@a) BEGIN "
"IF @a > 10 THEN "
" IF @a > 40 THEN "
" SET @b = 11;"
" ELSEIF @a > 30 THEN "
" SET @b = 12;"
" ELSEIF @a > 20 THEN "
" SET @b = 13;"
" ELSE "
" SET @b = 14;"
" END IF;"
"ELSEIF @a > 0 THEN "
" IF @a >= 7 THEN "
" SET @b = 21;"
" ELSEIF @a >= 5 THEN "
" SET @b = 22;"
" ELSEIF @a >= 3 THEN "
" SET @b = 23;"
" ELSE "
" SET @b = 24;"
" END IF;"
"ELSEIF @a < -10 THEN "
" IF @a < -40 THEN "
" SET @b = 31;"
" ELSEIF @a < -30 THEN "
" SET @b = 32;"
" ELSEIF @a < -20 THEN "
" SET @b = 33;"
" ELSE "
" SET @b = 34;"
" END IF;"
"ELSE "
" IF @a < -7 THEN "
" SET @b = 41;"
" ELSEIF @a < -5 THEN "
" SET @b = 42;"
" ELSEIF @a < -3 THEN "
" SET @b = 43;"
" ELSE "
" SET @b = 44;"
" END IF;"
"END IF;"
"RETURN @b;"
"END;"
);
db_check_int("CALL if_nested(41)", 11);
db_check_int("CALL if_nested(31)", 12);
db_check_int("CALL if_nested(21)", 13);
db_check_int("CALL if_nested(11)", 14);
db_check_int("CALL if_nested(7)", 21);
db_check_int("CALL if_nested(5)", 22);
db_check_int("CALL if_nested(3)", 23);
db_check_int("CALL if_nested(1)", 24);
db_check_int("CALL if_nested(-41)", 31);
db_check_int("CALL if_nested(-31)", 32);
db_check_int("CALL if_nested(-21)", 33);
db_check_int("CALL if_nested(-11)", 34);
db_check_int("CALL if_nested(-8)", 41);
db_check_int("CALL if_nested(-6)", 42);
db_check_int("CALL if_nested(-4)", 43);
db_check_int("CALL if_nested(-1)", 44);
////////////////////////////////////////////////////////////////////////////////
// LOOP .. BREAK .. CONTINUE .. END LOOP
////////////////////////////////////////////////////////////////////////////////
// LOOP block with BREAK
db_execute(
"CREATE PROCEDURE loop_basic(@a) BEGIN "
"SET @b = 0;"
"LOOP "
" SET @b = @b + 1;"
" IF @b > @a THEN "
" BREAK;"
" END IF;"
"END LOOP;"
"RETURN @b;"
"END;"
);
db_check_int("CALL loop_basic(10)", 11);
db_check_int("CALL loop_basic(5)", 6);
// LOOP block with CONTINUE
db_execute(
"CREATE PROCEDURE loop_continue(@a) BEGIN "
"SET @b = 0;"
"LOOP "
" SET @b = @b + 10;"
" IF @a - @b > 10 THEN "
" CONTINUE;"
" END IF;"
" IF @b > @a THEN "
" BREAK;"
" END IF;"
"END LOOP;"
"RETURN @b;"
"END;"
);
db_check_int("CALL loop_continue(15)", 20);
db_check_int("CALL loop_continue(25)", 30);
// LOOP block with RETURN
db_execute(
"CREATE PROCEDURE loop_return(@a) BEGIN "
"SET @b = 0;"
"LOOP "
" SET @b = @b + 1;"
" IF @b > @a THEN "
" RETURN @b;"
" END IF;"
"END LOOP;"
"RETURN @b;"
"END;"
);
db_check_int("CALL loop_return(10)", 11);
db_check_int("CALL loop_return(5)", 6);
// nested LOOP blocks with BREAK to the first level
// outer loop: from 1 to @nrows -> concatenate @nrows times the string '|'
// inner loop: from 1 to @ncols -> concatenate @ncols times the string 'x'
db_execute(
"CREATE PROCEDURE loop_nested(@nrows, @ncols) BEGIN "
"SET @s = '';"
"LOOP "
" SET @i = 0;"
" LOOP "
" SET @s = @s || 'x';"
" SET @i = @i + 1;"
" IF @i >= @ncols THEN "
" BREAK;"
" END IF;"
" END LOOP;"
" SET @s = @s || '|';"
" SET @nrows = @nrows - 1;"
" IF @nrows <= 0 THEN "
" BREAK;"
" END IF;"
"END LOOP;"
"RETURN @s;"
"END;"
);
db_check_str("CALL loop_nested(3, 5)", "xxxxx|xxxxx|xxxxx|");
db_check_str("CALL loop_nested(5, 3)", "xxx|xxx|xxx|xxx|xxx|");
// nested LOOP blocks with BREAK to the second level (BREAK 2)
db_execute(
"CREATE PROCEDURE loop_nested_break2(@nrows, @ncols) BEGIN "
"SET @s = '';"
"LOOP "
" SET @i = 0;"
" LOOP "
" SET @s = @s || 'x';"
" SET @i = @i + 1;"
" IF @i >= @ncols THEN "
" BREAK 2;"
" END IF;"
" END LOOP;"
" SET @s = @s || '|';"
" SET @nrows = @nrows - 1;"
" IF @nrows <= 0 THEN "
" BREAK;"
" END IF;"
"END LOOP;"
"RETURN @s;"
"END;"
);
db_check_str("CALL loop_nested_break2(3, 5)", "xxxxx");
db_check_str("CALL loop_nested_break2(5, 3)", "xxx");
// nested LOOP blocks with CONTINUE to the second level
db_execute(
"CREATE PROCEDURE loop_nested_continue2(@nrows, @ncols) BEGIN "
"SET @s = '';"
"LOOP "
" SET @s = @s || '|';"
" SET @nrows = @nrows - 1;"
" IF @nrows < 0 THEN "
" BREAK;"
" END IF;"
" SET @i = 0;"
" LOOP "
" SET @s = @s || 'x';"
" SET @i = @i + 1;"
" IF @i >= @ncols THEN "
" CONTINUE 2;"
" END IF;"
" END LOOP;"
" SET @s = @s || '###';"
"END LOOP;"
"RETURN @s;"
"END;"
);
db_check_str("CALL loop_nested_continue2(3, 5)", "|xxxxx|xxxxx|xxxxx|");
db_check_str("CALL loop_nested_continue2(5, 3)", "|xxx|xxx|xxx|xxx|xxx|");
////////////////////////////////////////////////////////////////////////////////
// FOREACH
////////////////////////////////////////////////////////////////////////////////
// FOREACH with array variable and 1 simple value (integer) per row
db_execute(
"CREATE PROCEDURE sum_array(@arr) BEGIN"
" SET @sum = 0;"
" FOREACH @item IN @arr DO"
" SET @sum = @sum + @item;"
" END LOOP;"
" RETURN @sum;"
"END"
);
db_check_int("CALL sum_array( ARRAY(11,22,33) )", 66);
// SET with SELECT
db_execute("CREATE TABLE sales (id INTEGER PRIMARY KEY, time)");
db_execute("CREATE TABLE sale_items (id INTEGER PRIMARY KEY, sale_id INTEGER, prod TEXT, qty INTEGER, price REAL)");
db_execute(
"CREATE PROCEDURE add_sale(@prod, @qty, @price) BEGIN "
"INSERT INTO sales (time) VALUES (datetime('now'));"
"SET @sale_id = SELECT last_insert_rowid();"
"INSERT INTO sale_items (sale_id, prod, qty, price) VALUES (@sale_id, @prod, @qty, @price);"
"RETURN @sale_id;"
"END;"
);
db_check_int("call add_sale('iphone 12',3,1234.90)", 1);
db_check_many("select * from sale_items",
"1|1|iphone 12|3|1234.9",
NULL
);
db_check_int("call add_sale('iphone 13',1,1250.00)", 2);
db_check_many("select * from sale_items",
"1|1|iphone 12|3|1234.9",
"2|2|iphone 13|1|1250.0",
NULL
);
// FOREACH with array variable and many values per row
db_execute(
"CREATE OR REPLACE PROCEDURE add_sale(@prods) BEGIN "
"INSERT INTO sales (time) VALUES (datetime('now'));"
"SET @sale_id = last_insert_rowid();"
"FOREACH @prod, @qty, @price IN @prods DO "
"INSERT INTO sale_items (sale_id, prod, qty, price) VALUES (@sale_id, @prod, @qty, @price);"
"END LOOP;"
"RETURN @sale_id;"
"END;"
);
db_check_int("CALL add_sale( ARRAY( ARRAY('iphone 14',1,12390.00), ARRAY('ipad',2,5950.00), ARRAY('iwatch',2,490.00) ) )", 3);
db_check_many("select * from sale_items",
"1|1|iphone 12|3|1234.9",
"2|2|iphone 13|1|1250.0",
"3|3|iphone 14|1|12390.0",
"4|3|ipad|2|5950.0",
"5|3|iwatch|2|490.0",
NULL
);
// FOREACH with ARRAY variable - 1 array per row - nested FOREACH
#if 0
db_execute(
"CREATE OR REPLACE PROCEDURE add_sale(@prods) BEGIN "
"INSERT INTO sales (time) VALUES (datetime('now'));"
"SET @sale_id = last_insert_rowid();"
"FOREACH @prod IN @prods DO "
"FOREACH @name, @qty, @price IN @prod DO "
"INSERT INTO sale_items (sale_id, prod, qty, price) VALUES (@sale_id, @name, @qty, @price);"
"END LOOP;"
"END LOOP;"
"RETURN @sale_id;"
"END;"
);
db_check_int("CALL add_sale( ARRAY( ARRAY('iphone 15',1,12390.00), ARRAY('ipad',2,5950.00), ARRAY('iwatch',2,490.00) ) )", 4);
db_check_many("select * from sale_items",
"1|1|iphone 12|3|1234.9",
"2|2|iphone 13|1|1250.0",
"3|3|iphone 14|1|12390.0",
"4|3|ipad|2|5950.0",
"5|3|iwatch|2|490.0",
"6|4|iphone 15|1|12390.0",
"7|4|ipad|2|5950.0",
"8|4|iwatch|2|490.0",
NULL
);
#endif
// FOREACH with ARRAY literal - 1 simple value per row
db_execute(
"CREATE OR REPLACE PROCEDURE sum_array() BEGIN"
" SET @sum = 0;"
" FOREACH @item IN ARRAY(11,22,33) DO"
" SET @sum = @sum + @item;"
" END LOOP;"
" RETURN @sum;"
"END"
);
db_check_int("CALL sum_array( )", 66);
// FOREACH with ARRAY literal - 1 array per row - nested FOREACH
#if 0
db_execute(
"CREATE OR REPLACE PROCEDURE sum_array() BEGIN"
" SET @sum = 0;"
" FOREACH @item IN ARRAY( ARRAY(11,22,33), ARRAY(44,55,66) ) DO"
" FOREACH @i IN @item DO"
" SET @sum = @sum + @i;"
" END LOOP;"
" END LOOP;"
" RETURN @sum;"
"END"
);
db_check_int("CALL sum_array()", 231);
#endif
// FOREACH with SELECT
db_execute(
"CREATE OR REPLACE PROCEDURE sum_sales() BEGIN"
" SET @sum = 0;"
" FOREACH @id, @sale_id, @prod, @qty, @price IN SELECT * FROM sale_items DO"
" SET @sum = @sum + @qty * @price;"
" END LOOP;"
" RETURN @sum;"
"END"
);
//db_check_double("CALL sum_sales()", 3*1234.9 + 1*1250.0 + 1*12390.0 + 2*5950.0 + 2*490.0 + 1*12390.0 + 2*5950.0 + 2*490.0, 0.0001);
db_check_double("CALL sum_sales()", 3*1234.9 + 1*1250.0 + 1*12390.0 + 2*5950.0 + 2*490.0, 0.0001);
// FOREACH VALUE with SELECT
db_execute(
"CREATE OR REPLACE PROCEDURE sum_sales2() BEGIN"
" SET @sum = 0;"
" FOREACH VALUE IN SELECT * FROM sale_items DO"
" SET @sum = @sum + @qty * @price;"
" END LOOP;"
" RETURN @sum;"
"END"
);
//db_check_double("CALL sum_sales2()", 3*1234.9 + 1*1250.0 + 1*12390.0 + 2*5950.0 + 2*490.0 + 1*12390.0 + 2*5950.0 + 2*490.0, 0.0001);
db_check_double("CALL sum_sales2()", 3*1234.9 + 1*1250.0 + 1*12390.0 + 2*5950.0 + 2*490.0, 0.0001);
// FOREACH with INSERT and RETURNING
#if 0
db_execute("drop table test");