SQLite

Check-in [3401388dba]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix a critical bug in sqlite3_prepare_v2 that can lead to segfaults when the schema changes and the statement is recompiled automatically. Ticket #2154. (CVS 3576)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3401388dba6c150f788397a4dfbcdb01313247e2
User & Date: drh 2007-01-08 21:07:18.000
Context
2007-01-08
22:40
Additional tests of sqlite3_prepare_v2. (CVS 3577) (check-in: b0650aa6cf user: drh tags: trunk)
21:07
Fix a critical bug in sqlite3_prepare_v2 that can lead to segfaults when the schema changes and the statement is recompiled automatically. Ticket #2154. (CVS 3576) (check-in: 3401388dba user: drh tags: trunk)
16:19
Fix another round-off problem in strftime(). Ticket #2153. (CVS 3574) (check-in: d49ddc5ff0 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/prepare.c.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the implementation of the sqlite3_prepare()
** interface, and routines that contribute to loading the database schema
** from disk.
**
** $Id: prepare.c,v 1.41 2006/11/09 00:24:54 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** Fill the InitData structure with an error message that indicates







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the implementation of the sqlite3_prepare()
** interface, and routines that contribute to loading the database schema
** from disk.
**
** $Id: prepare.c,v 1.42 2007/01/08 21:07:18 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** Fill the InitData structure with an error message that indicates
571
572
573
574
575
576
577
578

579
580
581
582
583
584
585
  rc = sqlite3Prepare(db, zSql, -1, 0, (sqlite3_stmt**)&pNew, 0);
  if( rc ){
    assert( pNew==0 );
    return 0;
  }else{
    assert( pNew!=0 );
  }
  sqlite3VdbeSwapOps(pNew, p);

  sqlite3_finalize((sqlite3_stmt*)pNew);
  return 1;
}


/*
** Two versions of the official API.  Legacy and new use.  In the legacy







|
>







571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  rc = sqlite3Prepare(db, zSql, -1, 0, (sqlite3_stmt**)&pNew, 0);
  if( rc ){
    assert( pNew==0 );
    return 0;
  }else{
    assert( pNew!=0 );
  }
  sqlite3VdbeSwap(pNew, p);
  sqlite3_transfer_bindings((sqlite3_stmt*)pNew, (sqlite3_stmt*)p);
  sqlite3_finalize((sqlite3_stmt*)pNew);
  return 1;
}


/*
** Two versions of the official API.  Legacy and new use.  In the legacy
Changes to src/vdbe.h.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*************************************************************************
** Header file for the Virtual DataBase Engine (VDBE)
**
** This header defines the interface to the virtual database engine
** or VDBE.  The VDBE implements an abstract machine that runs a
** simple program to access and modify the underlying database.
**
** $Id: vdbe.h,v 1.106 2006/11/09 00:24:54 drh Exp $
*/
#ifndef _SQLITE_VDBE_H_
#define _SQLITE_VDBE_H_
#include <stdio.h>

/*
** A single VDBE is an opaque structure named "Vdbe".  Only routines







|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*************************************************************************
** Header file for the Virtual DataBase Engine (VDBE)
**
** This header defines the interface to the virtual database engine
** or VDBE.  The VDBE implements an abstract machine that runs a
** simple program to access and modify the underlying database.
**
** $Id: vdbe.h,v 1.107 2007/01/08 21:07:18 drh Exp $
*/
#ifndef _SQLITE_VDBE_H_
#define _SQLITE_VDBE_H_
#include <stdio.h>

/*
** A single VDBE is an opaque structure named "Vdbe".  Only routines
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
int sqliteVdbeSetVariables(Vdbe*,int,const char**);
void sqlite3VdbeSetNumCols(Vdbe*,int);
int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, int);
void sqlite3VdbeCountChanges(Vdbe*);
sqlite3 *sqlite3VdbeDb(Vdbe*);
void sqlite3VdbeSetSql(Vdbe*, const char *z, int n);
const char *sqlite3VdbeGetSql(Vdbe*);
void sqlite3VdbeSwapOps(Vdbe*,Vdbe*);

#ifndef NDEBUG
  void sqlite3VdbeComment(Vdbe*, const char*, ...);
# define VdbeComment(X)  sqlite3VdbeComment X
#else
# define VdbeComment(X)
#endif

#endif







|









133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
int sqliteVdbeSetVariables(Vdbe*,int,const char**);
void sqlite3VdbeSetNumCols(Vdbe*,int);
int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, int);
void sqlite3VdbeCountChanges(Vdbe*);
sqlite3 *sqlite3VdbeDb(Vdbe*);
void sqlite3VdbeSetSql(Vdbe*, const char *z, int n);
const char *sqlite3VdbeGetSql(Vdbe*);
void sqlite3VdbeSwap(Vdbe*,Vdbe*);

#ifndef NDEBUG
  void sqlite3VdbeComment(Vdbe*, const char*, ...);
# define VdbeComment(X)  sqlite3VdbeComment X
#else
# define VdbeComment(X)
#endif

#endif
Changes to src/vdbeaux.c.
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
** Return the SQL associated with a prepared statement
*/
const char *sqlite3VdbeGetSql(Vdbe *p){
  return p->zSql;
}

/*
** Swap the set of Opcodes between to Vdbe structures.  No
** other parts of either Vdbe structure are changed.
*/
void sqlite3VdbeSwapOps(Vdbe *pA, Vdbe *pB){

  Op *aOp;
  int nOp;
  


  aOp = pA->aOp;


  nOp = pA->nOp;
  pA->aOp = pB->aOp;


  pA->nOp = pB->nOp;
  pB->aOp = aOp;


  pB->nOp = nOp;
}

/*
** Turn tracing on or off
*/
void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
  p->trace = trace;







|
<

|
>
|
|
|
>
>
|
>
>
|
|
>
>
|
|
>
>
|







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
** Return the SQL associated with a prepared statement
*/
const char *sqlite3VdbeGetSql(Vdbe *p){
  return p->zSql;
}

/*
** Swap all content between two VDBE structures.

*/
void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
  Vdbe tmp, *pTmp;
  char *zTmp;
  int nTmp;
  tmp = *pA;
  *pA = *pB;
  *pB = tmp;
  pTmp = pA->pNext;
  pA->pNext = pB->pNext;
  pB->pNext = pTmp;
  pTmp = pA->pPrev;
  pA->pPrev = pB->pPrev;
  pB->pPrev = pTmp;
  zTmp = pA->zSql;
  pA->zSql = pB->zSql;
  pB->zSql = zTmp;
  nTmp = pA->nSql;
  pA->nSql = pB->nSql;
  pB->nSql = nTmp;
}

/*
** Turn tracing on or off
*/
void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
  p->trace = trace;
Changes to test/capi3c.test.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#***********************************************************************
# This file implements regression tests for SQLite library.  
#
# This is a copy of the capi3.test file that has been adapted to
# test the new sqlite3_prepare_v2 interface.
#
# $Id: capi3c.test,v 1.1 2006/11/09 00:24:55 drh Exp $
#

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

# Return the UTF-16 representation of the supplied UTF-8 string $str.
# If $nt is true, append two 0x00 bytes as a nul terminator.







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#***********************************************************************
# This file implements regression tests for SQLite library.  
#
# This is a copy of the capi3.test file that has been adapted to
# test the new sqlite3_prepare_v2 interface.
#
# $Id: capi3c.test,v 1.2 2007/01/08 21:07:18 drh Exp $
#

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

# Return the UTF-16 representation of the supplied UTF-8 string $str.
# If $nt is true, append two 0x00 bytes as a nul terminator.
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
  set r [encoding convertfrom unicode $str]
  return $r
}

# These tests complement those in capi2.test. They are organized
# as follows:
#
# capi3-1.*: Test sqlite3_prepare_v2 
# capi3-2.*: Test sqlite3_prepare16_v2 
# capi3-3.*: Test sqlite3_open
# capi3-4.*: Test sqlite3_open16
# capi3-5.*: Test the various sqlite3_result_* APIs
# capi3-6.*: Test that sqlite3_close fails if there are outstanding VMs.
#

set DB [sqlite3_connection_pointer db]

do_test capi3-1.0 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3-1.1 {
  set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL]
  sqlite3_finalize $STMT
  set TAIL
} {}
do_test capi3-1.2 {
  sqlite3_errcode $DB
} {SQLITE_OK}
do_test capi3-1.3 {
  sqlite3_errmsg $DB
} {not an error}
do_test capi3-1.4 {
  set sql {SELECT name FROM sqlite_master;SELECT 10}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  set TAIL
} {SELECT 10}
do_test capi3-1.5 {
  set sql {SELECT namex FROM sqlite_master}
  catch {
    set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  }
} {1}
do_test capi3-1.6 {
  sqlite3_errcode $DB
} {SQLITE_ERROR}
do_test capi3-1.7 {
  sqlite3_errmsg $DB
} {no such column: namex}

ifcapable {utf16} {
  do_test capi3-2.1 {
    set sql16 [utf16 {SELECT name FROM sqlite_master}]
    set STMT [sqlite3_prepare16_v2  $DB $sql16 -1 ::TAIL]
    sqlite3_finalize $STMT
    utf8 $::TAIL
  } {}
  do_test capi3-2.2 {
    set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
    set STMT [sqlite3_prepare16_v2  $DB $sql -1 TAIL]
    sqlite3_finalize $STMT
    utf8 $TAIL
  } {SELECT 10}
  do_test capi3-2.3 {
    set sql [utf16 {SELECT namex FROM sqlite_master}]
    catch {
      set STMT [sqlite3_prepare16_v2  $DB $sql -1 TAIL]
    }
  } {1}
  do_test capi3-2.4 {
    sqlite3_errcode $DB
  } {SQLITE_ERROR}
  do_test capi3-2.5 {
    sqlite3_errmsg $DB
  } {no such column: namex}

  ifcapable schema_pragmas {
    do_test capi3-2.6 {
      execsql {CREATE TABLE tablename(x)}
      set sql16 [utf16 {PRAGMA table_info("TableName")}]
      set STMT [sqlite3_prepare16_v2  $DB $sql16 -1 TAIL]
      sqlite3_step $STMT
    } SQLITE_ROW
    do_test capi3-2.7 {
      sqlite3_step $STMT
    } SQLITE_DONE
    do_test capi3-2.8 {
      sqlite3_finalize $STMT
    } SQLITE_OK
  }

} ;# endif utf16

# rename sqlite3_open sqlite3_open_old
# proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}

do_test capi3-3.1 {
  set db2 [sqlite3_open test.db {}]
  sqlite3_errcode $db2
} {SQLITE_OK}
# FIX ME: Should test the db handle works.
do_test capi3-3.2 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3-3.3 {
  catch {
    set db2 [sqlite3_open /bogus/path/test.db {}]
  }
  sqlite3_errcode $db2
} {SQLITE_CANTOPEN}
do_test capi3-3.4 {
  sqlite3_errmsg $db2
} {unable to open database file}
do_test capi3-3.5 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3-3.6.1 {
  sqlite3_close $db2
} {SQLITE_MISUSE}
do_test capi3-3.6.2 {
  sqlite3_errmsg $db2
} {library routine called out of sequence}
ifcapable {utf16} {
  do_test capi3-3.6.3 {
    utf8 [sqlite3_errmsg16 $db2]
  } {library routine called out of sequence}
}

# rename sqlite3_open ""
# rename sqlite3_open_old sqlite3_open

ifcapable {utf16} {
do_test capi3-4.1 {
  set db2 [sqlite3_open16 [utf16 test.db] {}]
  sqlite3_errcode $db2
} {SQLITE_OK}
# FIX ME: Should test the db handle works.
do_test capi3-4.2 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3-4.3 {
  catch {
    set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
  }
  sqlite3_errcode $db2
} {SQLITE_CANTOPEN}
do_test capi3-4.4 {
  utf8 [sqlite3_errmsg16 $db2]
} {unable to open database file}
do_test capi3-4.5 {
  sqlite3_close $db2
} {SQLITE_OK}
} ;# utf16

# This proc is used to test the following API calls:
#
# sqlite3_column_count







|
|
|
|
|
|




|


|




|


|


|





|





|


|




|





|





|





|


|




|





|


|









|




|


|





|


|


|


|



|








|




|


|





|


|







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
  set r [encoding convertfrom unicode $str]
  return $r
}

# These tests complement those in capi2.test. They are organized
# as follows:
#
# capi3c-1.*: Test sqlite3_prepare_v2 
# capi3c-2.*: Test sqlite3_prepare16_v2 
# capi3c-3.*: Test sqlite3_open
# capi3c-4.*: Test sqlite3_open16
# capi3c-5.*: Test the various sqlite3_result_* APIs
# capi3c-6.*: Test that sqlite3_close fails if there are outstanding VMs.
#

set DB [sqlite3_connection_pointer db]

do_test capi3c-1.0 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3c-1.1 {
  set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL]
  sqlite3_finalize $STMT
  set TAIL
} {}
do_test capi3c-1.2 {
  sqlite3_errcode $DB
} {SQLITE_OK}
do_test capi3c-1.3 {
  sqlite3_errmsg $DB
} {not an error}
do_test capi3c-1.4 {
  set sql {SELECT name FROM sqlite_master;SELECT 10}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  set TAIL
} {SELECT 10}
do_test capi3c-1.5 {
  set sql {SELECT namex FROM sqlite_master}
  catch {
    set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  }
} {1}
do_test capi3c-1.6 {
  sqlite3_errcode $DB
} {SQLITE_ERROR}
do_test capi3c-1.7 {
  sqlite3_errmsg $DB
} {no such column: namex}

ifcapable {utf16} {
  do_test capi3c-2.1 {
    set sql16 [utf16 {SELECT name FROM sqlite_master}]
    set STMT [sqlite3_prepare16_v2  $DB $sql16 -1 ::TAIL]
    sqlite3_finalize $STMT
    utf8 $::TAIL
  } {}
  do_test capi3c-2.2 {
    set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
    set STMT [sqlite3_prepare16_v2  $DB $sql -1 TAIL]
    sqlite3_finalize $STMT
    utf8 $TAIL
  } {SELECT 10}
  do_test capi3c-2.3 {
    set sql [utf16 {SELECT namex FROM sqlite_master}]
    catch {
      set STMT [sqlite3_prepare16_v2  $DB $sql -1 TAIL]
    }
  } {1}
  do_test capi3c-2.4 {
    sqlite3_errcode $DB
  } {SQLITE_ERROR}
  do_test capi3c-2.5 {
    sqlite3_errmsg $DB
  } {no such column: namex}

  ifcapable schema_pragmas {
    do_test capi3c-2.6 {
      execsql {CREATE TABLE tablename(x)}
      set sql16 [utf16 {PRAGMA table_info("TableName")}]
      set STMT [sqlite3_prepare16_v2  $DB $sql16 -1 TAIL]
      sqlite3_step $STMT
    } SQLITE_ROW
    do_test capi3c-2.7 {
      sqlite3_step $STMT
    } SQLITE_DONE
    do_test capi3c-2.8 {
      sqlite3_finalize $STMT
    } SQLITE_OK
  }

} ;# endif utf16

# rename sqlite3_open sqlite3_open_old
# proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}

do_test capi3c-3.1 {
  set db2 [sqlite3_open test.db {}]
  sqlite3_errcode $db2
} {SQLITE_OK}
# FIX ME: Should test the db handle works.
do_test capi3c-3.2 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3c-3.3 {
  catch {
    set db2 [sqlite3_open /bogus/path/test.db {}]
  }
  sqlite3_errcode $db2
} {SQLITE_CANTOPEN}
do_test capi3c-3.4 {
  sqlite3_errmsg $db2
} {unable to open database file}
do_test capi3c-3.5 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3c-3.6.1-misuse {
  sqlite3_close $db2
} {SQLITE_MISUSE}
do_test capi3c-3.6.2-misuse {
  sqlite3_errmsg $db2
} {library routine called out of sequence}
ifcapable {utf16} {
  do_test capi3c-3.6.3-misuse {
    utf8 [sqlite3_errmsg16 $db2]
  } {library routine called out of sequence}
}

# rename sqlite3_open ""
# rename sqlite3_open_old sqlite3_open

ifcapable {utf16} {
do_test capi3c-4.1 {
  set db2 [sqlite3_open16 [utf16 test.db] {}]
  sqlite3_errcode $db2
} {SQLITE_OK}
# FIX ME: Should test the db handle works.
do_test capi3c-4.2 {
  sqlite3_close $db2
} {SQLITE_OK}
do_test capi3c-4.3 {
  catch {
    set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
  }
  sqlite3_errcode $db2
} {SQLITE_CANTOPEN}
do_test capi3c-4.4 {
  utf8 [sqlite3_errmsg16 $db2]
} {unable to open database file}
do_test capi3c-4.5 {
  sqlite3_close $db2
} {SQLITE_OK}
} ;# utf16

# This proc is used to test the following API calls:
#
# sqlite3_column_count
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
}

ifcapable !floatingpoint {
  finish_test
  return
}

do_test capi3-5.0 {
  execsql {
    CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t1 VALUES('one', 'two', NULL);
    INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
  }
  set sql "SELECT * FROM t1"
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_column_count $STMT
} 3

check_header $STMT capi3-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3-5.1 {main main main} {t1 t1 t1} {a b c}
do_test capi3-5.2 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3-5.3 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}

do_test capi3-5.5 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3-5.6 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}

do_test capi3-5.8 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3-5.9 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}

do_test capi3-5.11 {
  sqlite3_step $STMT
} SQLITE_DONE

do_test capi3-5.12 {
  sqlite3_finalize $STMT
} SQLITE_OK

do_test capi3-5.20 {
  set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_column_count $STMT
} 3

check_header $STMT capi3-5.21 {a sum(b) max(c)} {VARINT {} {}}
check_origin_header $STMT capi3-5.22 {main {} {}} {t1 {} {}} {a {} {}}
do_test capi3-5.23 {
  sqlite3_finalize $STMT
} SQLITE_OK


set ::ENC [execsql {pragma encoding}]
db close

do_test capi3-6.0 {
btree_breakpoint
  sqlite3 db test.db
  set DB [sqlite3_connection_pointer db]
btree_breakpoint
  sqlite3_key $DB xyzzy
  set sql {SELECT a FROM t1 order by rowid}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  expr 0
} {0}
do_test capi3-6.1 {
  db cache flush
  sqlite3_close $DB
} {SQLITE_BUSY}
do_test capi3-6.2 {
  sqlite3_step $STMT
} {SQLITE_ROW}
check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}
do_test capi3-6.3 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3-6.4 {
  db cache flush
  sqlite3_close $DB
} {SQLITE_OK}

db close


if {![sqlite3 -has-codec]} {
  # Test what happens when the library encounters a newer file format.
  # Do this by updating the file format via the btree layer.
  do_test capi3-7.1 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set meta [btree_get_meta $::bt]
    lset meta 2 5
    eval [concat btree_update_meta $::bt [lrange $meta 0 end]]
    btree_commit $::bt
    btree_close $::bt
  } {}
  do_test capi3-7.2 {
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {unsupported file format}}
  db close
}

if {![sqlite3 -has-codec]} {
  # Now test that the library correctly handles bogus entries in the
  # sqlite_master table (schema corruption).
  do_test capi3-8.1 {
    file delete -force test.db
    file delete -force test.db-journal
    sqlite3 db test.db
    execsql {
      CREATE TABLE t1(a);
    }
    db close
  } {}
  do_test capi3-8.2 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set ::bc [btree_cursor $::bt 1 1]

    # Build a 5-field row record consisting of 5 null records. This is
    # officially black magic.
    catch {unset data}
    set data [binary format c6 {6 0 0 0 0 0}]
    btree_insert $::bc 5 $data

    btree_close_cursor $::bc
    btree_commit $::bt
    btree_close $::bt
  } {}
  do_test capi3-8.3 {
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {malformed database schema}}
  do_test capi3-8.4 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set ::bc [btree_cursor $::bt 1 1]
  
    # Build a 5-field row record. The first field is a string 'table', and
    # subsequent fields are all NULL. Replace the other broken record with
    # this one and try to read the schema again. The broken record uses
    # either UTF-8 or native UTF-16 (if this file is being run by
    # utf16.test).
    if { [string match UTF-16* $::ENC] } {
      set data [binary format c6a10 {6 33 0 0 0 0} [utf16 table]]
    } else {
      set data [binary format c6a5 {6 23 0 0 0 0} table]
    }
    btree_insert $::bc 5 $data
  
    btree_close_cursor $::bc
    btree_commit $::bt
    btree_close $::bt
  } {};
  do_test capi3-8.5 {
    db close 
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {malformed database schema}}
  db close







|











|
|
|



|
|
|

|



|
|
|

|



|
|
|

|



|



|





|
|
|







|









|



|


|
|


|



>
|
>




|








|











|








|














|





|




















|







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
}

ifcapable !floatingpoint {
  finish_test
  return
}

do_test capi3c-5.0 {
  execsql {
    CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t1 VALUES('one', 'two', NULL);
    INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
  }
  set sql "SELECT * FROM t1"
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_column_count $STMT
} 3

check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c}
do_test capi3c-5.2 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}

do_test capi3c-5.5 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}

do_test capi3c-5.8 {
  sqlite3_step $STMT
} SQLITE_ROW

check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c}
check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}

do_test capi3c-5.11 {
  sqlite3_step $STMT
} SQLITE_DONE

do_test capi3c-5.12 {
  sqlite3_finalize $STMT
} SQLITE_OK

do_test capi3c-5.20 {
  set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_column_count $STMT
} 3

check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}}
check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}}
do_test capi3c-5.23 {
  sqlite3_finalize $STMT
} SQLITE_OK


set ::ENC [execsql {pragma encoding}]
db close

do_test capi3c-6.0 {
btree_breakpoint
  sqlite3 db test.db
  set DB [sqlite3_connection_pointer db]
btree_breakpoint
  sqlite3_key $DB xyzzy
  set sql {SELECT a FROM t1 order by rowid}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  expr 0
} {0}
do_test capi3c-6.1 {
  db cache flush
  sqlite3_close $DB
} {SQLITE_BUSY}
do_test capi3c-6.2 {
  sqlite3_step $STMT
} {SQLITE_ROW}
check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}
do_test capi3c-6.3 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3c-6.4 {
  db cache flush
  sqlite3_close $DB
} {SQLITE_OK}
do_test capi3c-6.99-misuse {
  db close
} {}

if {![sqlite3 -has-codec]} {
  # Test what happens when the library encounters a newer file format.
  # Do this by updating the file format via the btree layer.
  do_test capi3c-7.1 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set meta [btree_get_meta $::bt]
    lset meta 2 5
    eval [concat btree_update_meta $::bt [lrange $meta 0 end]]
    btree_commit $::bt
    btree_close $::bt
  } {}
  do_test capi3c-7.2 {
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {unsupported file format}}
  db close
}

if {![sqlite3 -has-codec]} {
  # Now test that the library correctly handles bogus entries in the
  # sqlite_master table (schema corruption).
  do_test capi3c-8.1 {
    file delete -force test.db
    file delete -force test.db-journal
    sqlite3 db test.db
    execsql {
      CREATE TABLE t1(a);
    }
    db close
  } {}
  do_test capi3c-8.2 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set ::bc [btree_cursor $::bt 1 1]

    # Build a 5-field row record consisting of 5 null records. This is
    # officially black magic.
    catch {unset data}
    set data [binary format c6 {6 0 0 0 0 0}]
    btree_insert $::bc 5 $data

    btree_close_cursor $::bc
    btree_commit $::bt
    btree_close $::bt
  } {}
  do_test capi3c-8.3 {
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {malformed database schema}}
  do_test capi3c-8.4 {
    set ::bt [btree_open test.db 10 0]
    btree_begin_transaction $::bt
    set ::bc [btree_cursor $::bt 1 1]
  
    # Build a 5-field row record. The first field is a string 'table', and
    # subsequent fields are all NULL. Replace the other broken record with
    # this one and try to read the schema again. The broken record uses
    # either UTF-8 or native UTF-16 (if this file is being run by
    # utf16.test).
    if { [string match UTF-16* $::ENC] } {
      set data [binary format c6a10 {6 33 0 0 0 0} [utf16 table]]
    } else {
      set data [binary format c6a5 {6 23 0 0 0 0} table]
    }
    btree_insert $::bc 5 $data
  
    btree_close_cursor $::bc
    btree_commit $::bt
    btree_close $::bt
  } {};
  do_test capi3c-8.5 {
    db close 
    sqlite3 db test.db
    catchsql {
      SELECT * FROM sqlite_master;
    }
  } {1 {malformed database schema}}
  db close
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049

1050










































1051



































1052
SQLITE_RANGE      {bind or column index out of range} \
SQLITE_NOTADB     {file is encrypted or is not a database} \
unknownerror      {unknown error} \
]

set test_number 1
foreach {code english} $code2english {
  do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english
  incr test_number
}

# Test the error message when a "real" out of memory occurs.
if {[info command sqlite_malloc_stat]!=""} {
set sqlite_malloc_fail 1
do_test capi3-10-1 {
  sqlite3 db test.db
  set DB [sqlite3_connection_pointer db]
  sqlite_malloc_fail 1
  catchsql {
    select * from sqlite_master;
  }
} {1 {out of memory}}
do_test capi3-10-2 {
  sqlite3_errmsg $::DB
} {out of memory}
ifcapable {utf16} {
  do_test capi3-10-3 {
    utf8 [sqlite3_errmsg16 $::DB]
  } {out of memory}
}
db close
sqlite_malloc_fail 0
}

# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK
# statement issued while there are still outstanding VMs that are part of
# the transaction fails.
sqlite3 db test.db
set DB [sqlite3_connection_pointer db]
sqlite_register_test_function $DB func
do_test capi3-11.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 'int');
    INSERT INTO t1 VALUES(2, 'notatype');
  }
} {}
do_test capi3-11.1.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.2 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-11.3 {
  catchsql {
    COMMIT;
  }
} {1 {cannot commit transaction - SQL statements in progress}}
do_test capi3-11.3.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.4 {
  sqlite3_step $STMT
} {SQLITE_ERROR}
do_test capi3-11.5 {
  sqlite3_finalize $STMT
} {SQLITE_ERROR}
do_test capi3-11.6 {
  catchsql {
    SELECT * FROM t1;
  }
} {0 {1 int 2 notatype}}
do_test capi3-11.6.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.7 {
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3-11.7.1 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3-11.8 {
  execsql {
    CREATE TABLE t2(a);
    INSERT INTO t2 VALUES(1);
    INSERT INTO t2 VALUES(2);
    BEGIN;
    INSERT INTO t2 VALUES(3);
  }
} {}
do_test capi3-11.8.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.9 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-11.9.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.9.2 {
  catchsql {
    ROLLBACK;
  }
} {1 {cannot rollback transaction - SQL statements in progress}}
do_test capi3-11.9.3 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.10 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-11.11 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-11.12 {
  sqlite3_step $STMT
} {SQLITE_DONE}
do_test capi3-11.13 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3-11.14 {
  execsql {
    SELECT a FROM t2;
  }
} {1 2 3}
do_test capi3-11.14.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3-11.15 {
  catchsql {
    ROLLBACK;
  }
} {0 {}}
do_test capi3-11.15.1 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3-11.16 {
  execsql {
    SELECT a FROM t2;
  }
} {1 2}

# Sanity check on the definition of 'outstanding VM'. This means any VM
# that has had sqlite3_step() called more recently than sqlite3_finalize() or
# sqlite3_reset(). So a VM that has just been prepared or reset does not
# count as an active VM.
do_test capi3-11.17 {
  execsql {
    BEGIN;
  }
} {}
do_test capi3-11.18 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3-11.19 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-11.20 {
  catchsql {
    BEGIN;
    COMMIT;
  }
} {1 {cannot commit transaction - SQL statements in progress}}
do_test capi3-11.20 {
  sqlite3_reset $STMT
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3-11.21 {
  sqlite3_finalize $STMT
} {SQLITE_OK}

# The following tests - capi3-12.* - check that it's Ok to start a
# transaction while other VMs are active, and that it's Ok to execute
# atomic updates in the same situation 
#
do_test capi3-12.1 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-12.2 {
  catchsql {
    INSERT INTO t1 VALUES(3, NULL);
  }
} {0 {}}
do_test capi3-12.3 {
  catchsql {
    INSERT INTO t2 VALUES(4);
  }
} {0 {}}
do_test capi3-12.4 {
  catchsql {
    BEGIN;
    INSERT INTO t1 VALUES(4, NULL);
  }
} {0 {}}
do_test capi3-12.5 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-12.5.1 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3-12.6 {
  sqlite3_step $STMT
} {SQLITE_DONE}
do_test capi3-12.7 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3-12.8 {
  execsql {
    COMMIT;
    SELECT a FROM t1;
  }
} {1 2 3 4}

# Test cases capi3-13.* test the sqlite3_clear_bindings() and 
# sqlite3_sleep APIs.
#
if {[llength [info commands sqlite3_clear_bindings]]>0} {
  do_test capi3-13.1 {
    execsql {
      DELETE FROM t1;
    }
    set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3-13.2 {
    sqlite3_reset $STMT
    sqlite3_bind_text $STMT 1 hello 5
    sqlite3_bind_text $STMT 2 world 5
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3-13.3 {
    sqlite3_reset $STMT
    sqlite3_clear_bindings $STMT
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3-13-4 {
    sqlite3_finalize $STMT
    execsql {
      SELECT * FROM t1;
    }
  } {{} {} hello world {} {}}
}
if {[llength [info commands sqlite3_sleep]]>0} {
  do_test capi3-13-5 {
    set ms [sqlite3_sleep 80]
    expr {$ms==80 || $ms==1000}
  } {1}
}

# Ticket #1219:  Make sure binding APIs can handle a NULL pointer.
#
do_test capi3-14.1 {
  set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
  lappend rc $msg
} {1 SQLITE_MISUSE}

# Ticket #1650:  Honor the nBytes parameter to sqlite3_prepare.
#
do_test capi3-15.1 {
  set sql {SELECT * FROM t2}
  set nbytes [string length $sql]
  append sql { WHERE a==1}
  set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL]
  sqlite3_step $STMT
  sqlite3_column_int $STMT 0
} {1}
do_test capi3-15.2 {
  sqlite3_step $STMT
  sqlite3_column_int $STMT 0
} {2}
do_test capi3-15.3 {
  sqlite3_finalize $STMT
} {SQLITE_OK}

# Make sure code is always generated even if an IF EXISTS or 
# IF NOT EXISTS clause is present that the table does not or
# does exists.  That way we will always have a prepared statement
# to expire when the schema changes.
#
do_test capi3-16.1 {
  set sql {DROP TABLE IF EXISTS t3}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT!=""}
} {1}
do_test capi3-16.2 {
  set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT!=""}
} {1}

# But still we do not generate code if there is no SQL
#
do_test capi3-16.3 {
  set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT==""}
} {1}
do_test capi3-16.4 {
  set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT==""}
} {1}

















































































finish_test







|






|







|



|







|





|







|


|



|




|


|


|


|




|


|




|


|








|


|



|


|




|


|


|


|


|


|




|


|




|


|









|




|





|


|





|





|



|



|



|




|




|





|


|


|


|


|






|



|






|





|




|







|







|






|







|



|








|





|








|




|





>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
SQLITE_RANGE      {bind or column index out of range} \
SQLITE_NOTADB     {file is encrypted or is not a database} \
unknownerror      {unknown error} \
]

set test_number 1
foreach {code english} $code2english {
  do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english
  incr test_number
}

# Test the error message when a "real" out of memory occurs.
if {[info command sqlite_malloc_stat]!=""} {
set sqlite_malloc_fail 1
do_test capi3c-10-1 {
  sqlite3 db test.db
  set DB [sqlite3_connection_pointer db]
  sqlite_malloc_fail 1
  catchsql {
    select * from sqlite_master;
  }
} {1 {out of memory}}
do_test capi3c-10-2 {
  sqlite3_errmsg $::DB
} {out of memory}
ifcapable {utf16} {
  do_test capi3c-10-3 {
    utf8 [sqlite3_errmsg16 $::DB]
  } {out of memory}
}
db close
sqlite_malloc_fail 0
}

# The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
# statement issued while there are still outstanding VMs that are part of
# the transaction fails.
sqlite3 db test.db
set DB [sqlite3_connection_pointer db]
sqlite_register_test_function $DB func
do_test capi3c-11.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 'int');
    INSERT INTO t1 VALUES(2, 'notatype');
  }
} {}
do_test capi3c-11.1.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.2 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-11.3 {
  catchsql {
    COMMIT;
  }
} {1 {cannot commit transaction - SQL statements in progress}}
do_test capi3c-11.3.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.4 {
  sqlite3_step $STMT
} {SQLITE_ERROR}
do_test capi3c-11.5 {
  sqlite3_finalize $STMT
} {SQLITE_ERROR}
do_test capi3c-11.6 {
  catchsql {
    SELECT * FROM t1;
  }
} {0 {1 int 2 notatype}}
do_test capi3c-11.6.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.7 {
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3c-11.7.1 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3c-11.8 {
  execsql {
    CREATE TABLE t2(a);
    INSERT INTO t2 VALUES(1);
    INSERT INTO t2 VALUES(2);
    BEGIN;
    INSERT INTO t2 VALUES(3);
  }
} {}
do_test capi3c-11.8.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.9 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-11.9.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.9.2 {
  catchsql {
    ROLLBACK;
  }
} {1 {cannot rollback transaction - SQL statements in progress}}
do_test capi3c-11.9.3 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.10 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-11.11 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-11.12 {
  sqlite3_step $STMT
} {SQLITE_DONE}
do_test capi3c-11.13 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3c-11.14 {
  execsql {
    SELECT a FROM t2;
  }
} {1 2 3}
do_test capi3c-11.14.1 {
  sqlite3_get_autocommit $DB
} 0
do_test capi3c-11.15 {
  catchsql {
    ROLLBACK;
  }
} {0 {}}
do_test capi3c-11.15.1 {
  sqlite3_get_autocommit $DB
} 1
do_test capi3c-11.16 {
  execsql {
    SELECT a FROM t2;
  }
} {1 2}

# Sanity check on the definition of 'outstanding VM'. This means any VM
# that has had sqlite3_step() called more recently than sqlite3_finalize() or
# sqlite3_reset(). So a VM that has just been prepared or reset does not
# count as an active VM.
do_test capi3c-11.17 {
  execsql {
    BEGIN;
  }
} {}
do_test capi3c-11.18 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3c-11.19 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-11.20 {
  catchsql {
    BEGIN;
    COMMIT;
  }
} {1 {cannot commit transaction - SQL statements in progress}}
do_test capi3c-11.20 {
  sqlite3_reset $STMT
  catchsql {
    COMMIT;
  }
} {0 {}}
do_test capi3c-11.21 {
  sqlite3_finalize $STMT
} {SQLITE_OK}

# The following tests - capi3c-12.* - check that it's Ok to start a
# transaction while other VMs are active, and that it's Ok to execute
# atomic updates in the same situation 
#
do_test capi3c-12.1 {
  set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-12.2 {
  catchsql {
    INSERT INTO t1 VALUES(3, NULL);
  }
} {0 {}}
do_test capi3c-12.3 {
  catchsql {
    INSERT INTO t2 VALUES(4);
  }
} {0 {}}
do_test capi3c-12.4 {
  catchsql {
    BEGIN;
    INSERT INTO t1 VALUES(4, NULL);
  }
} {0 {}}
do_test capi3c-12.5 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-12.5.1 {
  sqlite3_step $STMT
} {SQLITE_ROW}
do_test capi3c-12.6 {
  sqlite3_step $STMT
} {SQLITE_DONE}
do_test capi3c-12.7 {
  sqlite3_finalize $STMT
} {SQLITE_OK}
do_test capi3c-12.8 {
  execsql {
    COMMIT;
    SELECT a FROM t1;
  }
} {1 2 3 4}

# Test cases capi3c-13.* test the sqlite3_clear_bindings() and 
# sqlite3_sleep APIs.
#
if {[llength [info commands sqlite3_clear_bindings]]>0} {
  do_test capi3c-13.1 {
    execsql {
      DELETE FROM t1;
    }
    set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3c-13.2 {
    sqlite3_reset $STMT
    sqlite3_bind_text $STMT 1 hello 5
    sqlite3_bind_text $STMT 2 world 5
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3c-13.3 {
    sqlite3_reset $STMT
    sqlite3_clear_bindings $STMT
    sqlite3_step $STMT
  } {SQLITE_DONE}
  do_test capi3c-13-4 {
    sqlite3_finalize $STMT
    execsql {
      SELECT * FROM t1;
    }
  } {{} {} hello world {} {}}
}
if {[llength [info commands sqlite3_sleep]]>0} {
  do_test capi3c-13-5 {
    set ms [sqlite3_sleep 80]
    expr {$ms==80 || $ms==1000}
  } {1}
}

# Ticket #1219:  Make sure binding APIs can handle a NULL pointer.
#
do_test capi3c-14.1 {
  set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
  lappend rc $msg
} {1 SQLITE_MISUSE}

# Ticket #1650:  Honor the nBytes parameter to sqlite3_prepare.
#
do_test capi3c-15.1 {
  set sql {SELECT * FROM t2}
  set nbytes [string length $sql]
  append sql { WHERE a==1}
  set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL]
  sqlite3_step $STMT
  sqlite3_column_int $STMT 0
} {1}
do_test capi3c-15.2 {
  sqlite3_step $STMT
  sqlite3_column_int $STMT 0
} {2}
do_test capi3c-15.3 {
  sqlite3_finalize $STMT
} {SQLITE_OK}

# Make sure code is always generated even if an IF EXISTS or 
# IF NOT EXISTS clause is present that the table does not or
# does exists.  That way we will always have a prepared statement
# to expire when the schema changes.
#
do_test capi3c-16.1 {
  set sql {DROP TABLE IF EXISTS t3}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT!=""}
} {1}
do_test capi3c-16.2 {
  set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT!=""}
} {1}

# But still we do not generate code if there is no SQL
#
do_test capi3c-16.3 {
  set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT==""}
} {1}
do_test capi3c-16.4 {
  set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]
  sqlite3_finalize $STMT
  expr {$STMT==""}
} {1}

# Ticket #2154.
#
do_test capi3c-17.1 {
  set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL]
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.2 {
  sqlite3_column_int $STMT 0
} 4
do_test capi3c-17.3 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.4 {
  sqlite3_reset $STMT
  db eval {CREATE INDEX i2 ON t2(a)}
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.5 {
  sqlite3_column_int $STMT 0
} 4
do_test capi3c-17.6 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.7 {
  sqlite3_reset $STMT
  db eval {DROP INDEX i2}
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.8 {
  sqlite3_column_int $STMT 0
} 4
do_test capi3c-17.9 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.10 {
  sqlite3_finalize $STMT
  set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL]
  sqlite3_bind_int $STMT 1 2
  db eval {
    DELETE FROM t1;
    INSERT INTO t1 VALUES(1,'one');
    INSERT INTO t1 VALUES(2,'two');
    INSERT INTO t1 VALUES(3,'three');
    INSERT INTO t1 VALUES(4,'four');
  }
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.11 {
  sqlite3_column_text $STMT 0
} two
do_test capi3c-17.12 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.13 {
  sqlite3_reset $STMT
  db eval {CREATE INDEX i1 ON t1(a)}
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.14 {
  sqlite3_column_text $STMT 0
} two
do_test capi3c-17.15 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.16 {
  sqlite3_reset $STMT
  db eval {DROP INDEX i1}
  sqlite3_step $STMT
} SQLITE_ROW
do_test capi3c-17.17 {
  sqlite3_column_text $STMT 0
} two
do_test capi3c-17.18 {
  sqlite3_step $STMT
} SQLITE_DONE
do_test capi3c-17.99 {
  sqlite3_finalize $STMT
} SQLITE_OK


finish_test