summaryrefslogtreecommitdiff
path: root/test/capi2.test
blob: 20ee3401bf477ef0430aed4fdbc278e48eb08ae0 (plain)
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
# 2003 January 29
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script testing the callback-free C/C++ API.
#
# $Id: capi2.test,v 1.37 2008/12/30 17:55:00 drh Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Return the text values from the current row pointed at by STMT as a list.
proc get_row_values {STMT} {
  set VALUES [list]
  for {set i 0} {$i < [sqlite3_data_count $STMT]} {incr i} {
    lappend VALUES [sqlite3_column_text $STMT $i]
  }
  return $VALUES
}

# Return the column names followed by declaration types for the result set
# of the SQL statement STMT.
#
# i.e. for:
# CREATE TABLE abc(a text, b integer); 
# SELECT * FROM abc;
#
# The result is {a b text integer}
proc get_column_names {STMT} {
  set VALUES [list]
  for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
    lappend VALUES [sqlite3_column_name $STMT $i]
  }
  for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
    lappend VALUES [sqlite3_column_decltype $STMT $i]
  }
  return $VALUES
}

# Check basic functionality
#
do_test capi2-1.1 {
  set DB [sqlite3_connection_pointer db]
  execsql {CREATE TABLE t1(a,b,c)}
  set VM [sqlite3_prepare $DB {SELECT name, rowid FROM sqlite_master} -1 TAIL]
  set TAIL
} {}
do_test capi2-1.2 {
  sqlite3_step $VM
} {SQLITE_ROW}
do_test capi2-1.3 {
  sqlite3_data_count $VM
} {2}
do_test capi2-1.4 {
  get_row_values $VM
} {t1 1}
do_test capi2-1.5 {
  get_column_names $VM
} {name rowid text INTEGER}
do_test capi2-1.6 {
  sqlite3_step $VM 
} {SQLITE_DONE}
do_test capi2-1.7 {
  list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
} {2 {} {name rowid text INTEGER}}

# This used to be SQLITE_MISUSE.  But now we automatically reset prepared
# statements.
ifcapable autoreset {
  do_test capi2-1.8 {
    sqlite3_step $VM
  } {SQLITE_ROW}
} else {
  do_test capi2-1.8 {
    sqlite3_step $VM
  } {SQLITE_MISUSE}
}

# Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot
# be interrogated for more information. However in v3, since the column
# count, names and types are determined at compile time, these are still
# accessible after an SQLITE_MISUSE error.
do_test capi2-1.9 {
  sqlite3_reset $VM
  list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
} {2 {} {name rowid text INTEGER}}
do_test capi2-1.10 {
  sqlite3_data_count $VM
} {0}

do_test capi2-1.11 {
  sqlite3_finalize $VM
} {SQLITE_OK}

# Check to make sure that the "tail" of a multi-statement SQL script
# is returned by sqlite3_prepare.
#
do_test capi2-2.1 {
  set SQL {
    SELECT name, rowid FROM sqlite_master;
    SELECT name, rowid FROM sqlite_master WHERE 0;
    -- A comment at the end
  }
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
  set SQL
} {
    SELECT name, rowid FROM sqlite_master WHERE 0;
    -- A comment at the end
  }
do_test capi2-2.2 {
  set r [sqlite3_step $VM]
  lappend r [sqlite3_column_count $VM] \
            [get_row_values $VM] \
            [get_column_names $VM]
} {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
do_test capi2-2.3 {
  set r [sqlite3_step $VM]
  lappend r [sqlite3_column_count $VM] \
            [get_row_values $VM] \
            [get_column_names $VM]
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
do_test capi2-2.4 {
  sqlite3_finalize $VM
} {SQLITE_OK}
do_test capi2-2.5 {
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
  set SQL
} {
    -- A comment at the end
  }
do_test capi2-2.6 {
  set r [sqlite3_step $VM]
  lappend r [sqlite3_column_count $VM] \
            [get_row_values $VM] \
            [get_column_names $VM]
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
do_test capi2-2.7 {
  sqlite3_finalize $VM
} {SQLITE_OK}
do_test capi2-2.8 {
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
  list $SQL $VM
} {{} {}}

# Check the error handling.
#
do_test capi2-3.1 {
  set rc [catch {
      sqlite3_prepare $DB {select bogus from sqlite_master} -1 TAIL
  } msg]
  lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {}}
do_test capi2-3.2 {
  set rc [catch {
      sqlite3_prepare $DB {select bogus from } -1 TAIL
  } msg]
  lappend rc $msg $TAIL
} {1 {(1) near " ": syntax error} {}}
do_test capi2-3.3 {
  set rc [catch {
      sqlite3_prepare $DB {;;;;select bogus from sqlite_master} -1 TAIL
  } msg]
  lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {}}
do_test capi2-3.4 {
  set rc [catch {
      sqlite3_prepare $DB {select bogus from sqlite_master;x;} -1 TAIL
  } msg]
  lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {x;}}
do_test capi2-3.5 {
  set rc [catch {
      sqlite3_prepare $DB {select bogus from sqlite_master;;;x;} -1 TAIL
  } msg]
  lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {;;x;}}
do_test capi2-3.6 {
  set rc [catch {
      sqlite3_prepare $DB {select 5/0} -1 TAIL
  } VM]
  lappend rc $TAIL
} {0 {}}
do_test capi2-3.7 {
  list [sqlite3_step $VM] \
       [sqlite3_column_count $VM] \
       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_ROW 1 {{}} {5/0 {}}}
do_test capi2-3.8 {
  sqlite3_finalize $VM
} {SQLITE_OK}
do_test capi2-3.9 {
  execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
  set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,2,3)} -1 TAIL]
  set TAIL
} {}
do_test capi2-3.9b {db changes} {0}
do_test capi2-3.10 {
  list [sqlite3_step $VM] \
       [sqlite3_column_count $VM] \
       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_DONE 0 {} {}}

# Update for v3 - the change has not actually happened until the query is
# finalized. Is this going to cause trouble for anyone? Lee Nelson maybe?
# (Later:) The change now happens just before SQLITE_DONE is returned.
do_test capi2-3.10b {db changes} {1}
do_test capi2-3.11 {
  sqlite3_finalize $VM
} {SQLITE_OK}
do_test capi2-3.11b {db changes} {1}
#do_test capi2-3.12-misuse {
#  sqlite3_finalize $VM
#} {SQLITE_MISUSE}
do_test capi2-3.13 {
  set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,3,4)} -1 TAIL]
  list [sqlite3_step $VM] \
       [sqlite3_column_count $VM] \
       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_ERROR 0 {} {}}

# Update for v3: Preparing a statement does not affect the change counter.
# (Test result changes from 0 to 1).  (Later:) change counter updates occur
# when sqlite3_step returns, not at finalize time.
do_test capi2-3.13b {db changes} {0}

do_test capi2-3.14 {
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
       [sqlite3_extended_errcode $DB]
} {SQLITE_CONSTRAINT {column a is not unique} SQLITE_CONSTRAINT_UNIQUE}
do_test capi2-3.15 {
  set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
  set TAIL
} {}
do_test capi2-3.16 {
  list [sqlite3_step $VM] \
       [sqlite3_column_count $VM] \
       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_DONE 0 {} {}}
do_test capi2-3.17 {
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
} {SQLITE_OK {not an error}}
do_test capi2-3.18 {
  set VM [sqlite3_prepare $DB {INSERT INTO t2 VALUES(NULL,2)} -1 TAIL]
  list [sqlite3_step $VM] \
       [sqlite3_column_count $VM] \
       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_ERROR 0 {} {}}
do_test capi2-3.19 {
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
       [sqlite3_extended_errcode $DB]
} {SQLITE_CONSTRAINT {t2.a may not be NULL} SQLITE_CONSTRAINT_NOTNULL}

do_test capi2-3.20 {
  execsql {
    CREATE TABLE a1(message_id, name , UNIQUE(message_id, name) );
    INSERT INTO a1 VALUES(1, 1);
  }
} {}
do_test capi2-3.21 {
  set VM [sqlite3_prepare $DB {INSERT INTO a1 VALUES(1, 1)} -1 TAIL]
  sqlite3_step $VM
} {SQLITE_ERROR}
do_test capi2-3.22 {
  sqlite3_errcode $DB
} {SQLITE_ERROR}
do_test capi2-3.23 {
  sqlite3_finalize $VM
} {SQLITE_CONSTRAINT}
do_test capi2-3.24 {
  list [sqlite3_errcode $DB] [sqlite3_extended_errcode $DB]
} {SQLITE_CONSTRAINT SQLITE_CONSTRAINT_UNIQUE}

# Two or more virtual machines exists at the same time.
#
do_test capi2-4.1 {
  set VM1 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(1,2)} -1 TAIL]
  set TAIL
} {}
do_test capi2-4.2 {
  set VM2 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
  set TAIL
} {}
do_test capi2-4.3 {
  set VM3 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(3,4)} -1 TAIL]
  set TAIL
} {}
do_test capi2-4.4 {
  list [sqlite3_step $VM2] \
       [sqlite3_column_count $VM2] \
       [get_row_values $VM2] \
       [get_column_names $VM2]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.5 {
  execsql {SELECT * FROM t2 ORDER BY a}
} {2 3}
do_test capi2-4.6 {
  sqlite3_finalize $VM2
} {SQLITE_OK}
do_test capi2-4.7 {
  list [sqlite3_step $VM3] \
       [sqlite3_column_count $VM3] \
       [get_row_values $VM3] \
       [get_column_names $VM3]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.8 {
  execsql {SELECT * FROM t2 ORDER BY a}
} {2 3 3 4}
do_test capi2-4.9 {
  sqlite3_finalize $VM3
} {SQLITE_OK}
do_test capi2-4.10 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.11 {
  execsql {SELECT * FROM t2 ORDER BY a}
} {1 2 2 3 3 4}
do_test capi2-4.12 {
  sqlite3_finalize $VM1
} {SQLITE_OK}

# Interleaved SELECTs
#
do_test capi2-5.1 {
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  set VM2 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  set VM3 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.2 {
  list [sqlite3_step $VM2] \
       [sqlite3_column_count $VM2] \
       [get_row_values $VM2] \
       [get_column_names $VM2]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.3 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.4 {
  list [sqlite3_step $VM3] \
       [sqlite3_column_count $VM3] \
       [get_row_values $VM3] \
       [get_column_names $VM3]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.5 {
  list [sqlite3_step $VM3] \
       [sqlite3_column_count $VM3] \
       [get_row_values $VM3] \
       [get_column_names $VM3]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.6 {
  list [sqlite3_step $VM3] \
       [sqlite3_column_count $VM3] \
       [get_row_values $VM3] \
       [get_column_names $VM3]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.7 {
  list [sqlite3_step $VM3] \
       [sqlite3_column_count $VM3] \
       [get_row_values $VM3] \
       [get_column_names $VM3]
} {SQLITE_DONE 2 {} {a b {} {}}}
do_test capi2-5.8 {
  sqlite3_finalize $VM3
} {SQLITE_OK}
do_test capi2-5.9 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.10 {
  sqlite3_finalize $VM1
} {SQLITE_OK}
do_test capi2-5.11 {
  list [sqlite3_step $VM2] \
       [sqlite3_column_count $VM2] \
       [get_row_values $VM2] \
       [get_column_names $VM2]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.12 {
  list [sqlite3_step $VM2] \
       [sqlite3_column_count $VM2] \
       [get_row_values $VM2] \
       [get_column_names $VM2]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.11 {
  sqlite3_finalize $VM2
} {SQLITE_OK}

# Check for proper SQLITE_BUSY returns.
#
do_test capi2-6.1 {
  execsql {
    BEGIN;
    CREATE TABLE t3(x counter);
    INSERT INTO t3 VALUES(1);
    INSERT INTO t3 VALUES(2);
    INSERT INTO t3 SELECT x+2 FROM t3;
    INSERT INTO t3 SELECT x+4 FROM t3;
    INSERT INTO t3 SELECT x+8 FROM t3;
    COMMIT;
  }
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
  sqlite3 db2 test.db
  execsql {BEGIN} db2
} {}
# Update for v3: BEGIN doesn't write-lock the database. It is quite
# difficult to get v3 to write-lock the database, which causes a few
# problems for test scripts.
#
# do_test capi2-6.2 {
#   list [sqlite3_step $VM1] \
#        [sqlite3_column_count $VM1] \
#        [get_row_values $VM1] \
#        [get_column_names $VM1]
# } {SQLITE_BUSY 0 {} {}}
do_test capi2-6.3 {
  execsql {COMMIT} db2
} {}
do_test capi2-6.4 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 1 {x counter}}
do_test capi2-6.5 {
  catchsql {INSERT INTO t3 VALUES(10);} db2
} {1 {database is locked}}
do_test capi2-6.6 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 2 {x counter}}
do_test capi2-6.7 {
  execsql {SELECT * FROM t2} db2
} {2 3 3 4 1 2}
do_test capi2-6.8 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 3 {x counter}}
do_test capi2-6.9 {
  execsql {SELECT * FROM t2} 
} {2 3 3 4 1 2}
do_test capi2-6.10 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 4 {x counter}}
do_test capi2-6.11 {
  execsql {BEGIN}
} {}
do_test capi2-6.12 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 5 {x counter}}

# A read no longer blocks a write in the same connection.
#do_test capi2-6.13 {
#  catchsql {UPDATE t3 SET x=x+1}
#} {1 {database table is locked}}

do_test capi2-6.14 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 6 {x counter}}
do_test capi2-6.15 {
  execsql {SELECT * FROM t1}
} {1 2 3}
do_test capi2-6.16 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 7 {x counter}}
do_test capi2-6.17 {
  catchsql {UPDATE t1 SET b=b+1}
} {0 {}}
do_test capi2-6.18 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 8 {x counter}}
do_test capi2-6.19 {
  execsql {SELECT * FROM t1}
} {1 3 3}
do_test capi2-6.20 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 9 {x counter}}
#do_test capi2-6.21 {
#  execsql {ROLLBACK; SELECT * FROM t1}
#} {1 2 3}
do_test capi2-6.22 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 10 {x counter}}
#do_test capi2-6.23 {
#  execsql {BEGIN TRANSACTION;}
#} {}
do_test capi2-6.24 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 11 {x counter}}
do_test capi2-6.25 {
  execsql {
    INSERT INTO t1 VALUES(2,3,4);
    SELECT * FROM t1;
  }
} {1 3 3 2 3 4}
do_test capi2-6.26 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 12 {x counter}}
do_test capi2-6.27 {
  catchsql {
    INSERT INTO t1 VALUES(2,4,5);
    SELECT * FROM t1;
  }
} {1 {column a is not unique}}
do_test capi2-6.28 {
  list [sqlite3_step $VM1] \
       [sqlite3_column_count $VM1] \
       [get_row_values $VM1] \
       [get_column_names $VM1]
} {SQLITE_ROW 1 13 {x counter}}
do_test capi2-6.99 {
  sqlite3_finalize $VM1
} {SQLITE_OK}
catchsql {ROLLBACK}

do_test capi2-7.1 {
  stepsql $DB {
    SELECT * FROM t1
  }
} {0 1 2 3}
do_test capi2-7.2 {
  stepsql $DB {
    PRAGMA count_changes=on
  }
} {0}
do_test capi2-7.3 {
  stepsql $DB {
    UPDATE t1 SET a=a+10;
  }
} {0 1}
do_test capi2-7.4 {
  stepsql $DB {
    INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
  }
} {0 1}
do_test capi2-7.4b {sqlite3_changes $DB} {1}
do_test capi2-7.5 {
  stepsql $DB {
    UPDATE t1 SET a=a+10;
  }
} {0 2}
do_test capi2-7.5b {sqlite3_changes $DB} {2}
do_test capi2-7.6 {
  stepsql $DB {
    SELECT * FROM t1;
  }
} {0 21 2 3 22 3 4}
do_test capi2-7.7 {
  stepsql $DB {
    INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
  }
} {0 2}
do_test capi2-7.8 {
  sqlite3_changes $DB
} {2}
do_test capi2-7.9 {
  stepsql $DB {
    SELECT * FROM t1;
  }
} {0 21 2 3 22 3 4 23 4 5 24 5 6}
do_test capi2-7.10 {
  stepsql $DB {
    UPDATE t1 SET a=a-20;
    SELECT * FROM t1;
  }
} {0 4 1 2 3 2 3 4 3 4 5 4 5 6}

# Update for version 3: A SELECT statement no longer resets the change
# counter (Test result changes from 0 to 4).
do_test capi2-7.11 {
  sqlite3_changes $DB
} {4}
do_test capi2-7.11a {
  execsql {SELECT count(*) FROM t1}
} {4}

ifcapable {explain} {
  do_test capi2-7.12 {
    set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
    lindex $x 0
  } {0}
}

# Ticket #261 - make sure we can finalize before the end of a query.
#
do_test capi2-8.1 {
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  sqlite3_finalize $VM1
} {SQLITE_OK}
  
# Tickets #384 and #385 - make sure the TAIL argument to sqlite3_prepare
# and all of the return pointers in sqlite_step can be null.
#
do_test capi2-9.1 {
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 DUMMY]
  sqlite3_step $VM1
  sqlite3_finalize $VM1
} {SQLITE_OK}

# Test that passing a NULL pointer to sqlite3_finalize() or sqlite3_reset
# does not cause an error.
do_test capi2-10.1 {
  sqlite3_finalize 0
} {SQLITE_OK}
do_test capi2-10.2 {
  sqlite3_reset 0
} {SQLITE_OK}

#---------------------------------------------------------------------------
# The following tests - capi2-11.* - test the "column origin" APIs.
#
#   sqlite3_column_origin_name()
#   sqlite3_column_database_name()
#   sqlite3_column_table_name()
#

ifcapable columnmetadata {

# This proc uses the database handle $::DB to compile the SQL statement passed
# as a parameter. The return value of this procedure is a list with one
# element for each column returned by the compiled statement. Each element of
# this list is itself a list of length three, consisting of the origin
# database, table and column for the corresponding returned column.
proc check_origins {sql} {
  set ret [list]
  set ::STMT [sqlite3_prepare $::DB $sql -1 dummy]
  for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
    lappend ret [list                           \
      [sqlite3_column_database_name $::STMT $i] \
      [sqlite3_column_table_name $::STMT $i]    \
      [sqlite3_column_origin_name $::STMT $i]   \
    ]
  }
  sqlite3_finalize $::STMT
  return $ret
}
do_test capi2-11.1 {
  execsql {
    CREATE TABLE tab1(col1, col2);
  }
} {}
do_test capi2-11.2 {
  check_origins {SELECT col2, col1 FROM tab1}
} [list {main tab1 col2} {main tab1 col1}]
do_test capi2-11.3 {
  check_origins {SELECT col2 AS hello, col1 AS world FROM tab1}
} [list {main tab1 col2} {main tab1 col1}]

ifcapable subquery {
  do_test capi2-11.4 {
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM tab1)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-11.5 {
    check_origins {SELECT (SELECT col2 FROM tab1), (SELECT col1 FROM tab1)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-11.6 {
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM tab1}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-11.7 {
    check_origins {SELECT * FROM tab1}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-11.8 {
    check_origins {SELECT * FROM (SELECT * FROM tab1)}
  } [list {main tab1 col1} {main tab1 col2}]
}

ifcapable view&&subquery {
  do_test capi2-12.1 {
    execsql {
      CREATE VIEW view1 AS SELECT * FROM  tab1;
    }
  } {}
  do_test capi2-12.2 {
    check_origins {SELECT col2, col1 FROM view1}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-12.3 {
    check_origins {SELECT col2 AS hello, col1 AS world FROM view1}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-12.4 {
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view1)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-12.5 {
    check_origins {SELECT (SELECT col2 FROM view1), (SELECT col1 FROM view1)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-12.6 {
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM view1}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-12.7 {
    check_origins {SELECT * FROM view1}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-12.8 {
    check_origins {select * from (select * from view1)}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-12.9 {
    check_origins {select * from (select * from (select * from view1))}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-12.10 {
    db close
    sqlite3 db test.db
    set ::DB [sqlite3_connection_pointer db]
    check_origins {select * from (select * from (select * from view1))}
  } [list {main tab1 col1} {main tab1 col2}]
  
  # This view will thwart the flattening optimization.
  do_test capi2-13.1 {
    execsql {
      CREATE VIEW view2 AS SELECT * FROM tab1 limit 10 offset 10;
    }
  } {}
  do_test capi2-13.2 {
    check_origins {SELECT col2, col1 FROM view2}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-13.3 {
    check_origins {SELECT col2 AS hello, col1 AS world FROM view2}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-13.4 {
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view2)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-13.5 {
    check_origins {SELECT (SELECT col2 FROM view2), (SELECT col1 FROM view2)}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-13.6 {
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM view2}
  } [list {main tab1 col2} {main tab1 col1}]
  do_test capi2-13.7 {
    check_origins {SELECT * FROM view2}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-13.8 {
    check_origins {select * from (select * from view2)}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-13.9 {
    check_origins {select * from (select * from (select * from view2))}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-13.10 {
    db close
    sqlite3 db test.db
    set ::DB [sqlite3_connection_pointer db]
    check_origins {select * from (select * from (select * from view2))}
  } [list {main tab1 col1} {main tab1 col2}]
  do_test capi2-13.11 {
    check_origins {select * from (select * from tab1 limit 10 offset 10)}
  } [list {main tab1 col1} {main tab1 col2}]
}


} ;# ifcapable columnmetadata

db2 close
finish_test