SQLite

Check-in [2eb9087055]
Login

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

Overview
Comment:Remove code for SQL cursors. (CVS 2312)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2eb90870556c90b8d85da3d5eb7554f74ec922c2
User & Date: danielk1977 2005-02-04 04:07:17.000
Context
2005-02-04
21:13
Detect and report a subtle case of database file corruption. (CVS 2313) (check-in: 9fc0a5cbf8 user: drh tags: trunk)
04:07
Remove code for SQL cursors. (CVS 2312) (check-in: 2eb9087055 user: danielk1977 tags: trunk)
2005-02-03
01:08
Get AUTOINCREMENT and VACUUM working together. Ticket #1095. (CVS 2311) (check-in: 332a531d06 user: drh tags: trunk)
Changes
Side-by-Side Diff Ignore Whitespace Patch
Changes to Makefile.in.
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
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







-
+















-







###############################################################################

OPTS += -DSQLITE_OMIT_CURSOR          # Cursors do not work at this time
TCC += -DSQLITE_OMIT_CURSOR

# Object files for the SQLite library.
#
LIBOBJ = attach.lo auth.lo btree.lo build.lo cursor.lo date.lo \
LIBOBJ = attach.lo auth.lo btree.lo build.lo date.lo \
         delete.lo expr.lo func.lo hash.lo insert.lo \
         main.lo opcodes.lo os_unix.lo os_win.lo \
         pager.lo parse.lo pragma.lo printf.lo random.lo \
         select.lo table.lo tokenize.lo trigger.lo update.lo \
         util.lo vacuum.lo vdbe.lo vdbeapi.lo vdbeaux.lo vdbemem.lo \
         where.lo utf.lo legacy.lo

# All of the source code files.
#
SRC = \
  $(TOP)/src/attach.c \
  $(TOP)/src/auth.c \
  $(TOP)/src/btree.c \
  $(TOP)/src/btree.h \
  $(TOP)/src/build.c \
  $(TOP)/src/cursor.c \
  $(TOP)/src/date.c \
  $(TOP)/src/delete.c \
  $(TOP)/src/expr.c \
  $(TOP)/src/func.c \
  $(TOP)/src/hash.c \
  $(TOP)/src/hash.h \
  $(TOP)/src/insert.c \
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
283
284
285
286
287
288
289



290
291
292
293
294
295
296







-
-
-







	echo '"#define SQLITE_PTR_SZ %d",sizeof(char*));' >>temp.c
	echo 'exit(0);}' >>temp.c
	$(BCC) -o temp temp.c
	./temp >config.h
	echo >>config.h
	rm -f temp.c temp

cursor.lo:	$(TOP)/src/cursor.c $(HDR)
	$(LTCOMPILE) -c $(TOP)/src/cursor.c

date.lo:	$(TOP)/src/date.c $(HDR)
	$(LTCOMPILE) -c $(TOP)/src/date.c

delete.lo:	$(TOP)/src/delete.c $(HDR)
	$(LTCOMPILE) -c $(TOP)/src/delete.c

expr.lo:	$(TOP)/src/expr.c $(HDR)
Changes to main.mk.
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
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







-
+
















-








# This is how we compile
#
TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I. -I$(TOP)/src

# Object files for the SQLite library.
#
LIBOBJ+= attach.o auth.o btree.o build.o cursor.o date.o delete.o \
LIBOBJ+= attach.o auth.o btree.o build.o date.o delete.o \
         expr.o func.o hash.o insert.o \
         main.o opcodes.o os_unix.o os_win.o \
         pager.o parse.o pragma.o printf.o random.o \
         select.o table.o tclsqlite.o tokenize.o trigger.o \
         update.o util.o vacuum.o \
         vdbe.o vdbeapi.o vdbeaux.o vdbemem.o \
         where.o utf.o legacy.o

# All of the source code files.
#
SRC = \
  $(TOP)/src/attach.c \
  $(TOP)/src/auth.c \
  $(TOP)/src/btree.c \
  $(TOP)/src/btree.h \
  $(TOP)/src/build.c \
  $(TOP)/src/cursor.c \
  $(TOP)/src/date.c \
  $(TOP)/src/delete.c \
  $(TOP)/src/expr.c \
  $(TOP)/src/func.c \
  $(TOP)/src/hash.c \
  $(TOP)/src/hash.h \
  $(TOP)/src/insert.c \
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
219
220
221
222
223
224
225



226
227
228
229
230
231
232







-
-
-







	echo '"#define SQLITE_PTR_SZ %d",sizeof(char*));' >>temp.c
	echo 'exit(0);}' >>temp.c
	$(BCC) -o temp temp.c
	./temp >config.h
	echo >>config.h
	rm -f temp.c temp

cursor.o:	$(TOP)/src/cursor.c $(HDR)
	$(TCCX) -c $(TOP)/src/cursor.c

date.o:	$(TOP)/src/date.c $(HDR)
	$(TCCX) -c $(TOP)/src/date.c

delete.o:	$(TOP)/src/delete.c $(HDR)
	$(TCCX) -c $(TOP)/src/delete.c

expr.o:	$(TOP)/src/expr.c $(HDR)
Changes to src/btree.c.
1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
19











-
+







/*
** 2004 April 6
**
** 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.
**
*************************************************************************
** $Id: btree.c,v 1.244 2005/01/21 11:55:26 danielk1977 Exp $
** $Id: btree.c,v 1.245 2005/02/04 04:07:17 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
**
**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
**     "Sorting And Searching", pages 473-480. Addison-Wesley
**     Publishing Company, Reading, Massachusetts.
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1947
1948
1949
1950
1951
1952
1953

1954
1955
1956
1957
1958
1959
1960
1961
1962
1963

1964
1965
1966
1967
1968
1969
1970







-










-







#endif

#ifdef SQLITE_TEST
/*
** Print debugging information about all cursors to standard output.
*/
void sqlite3BtreeCursorList(Btree *pBt){
#ifndef SQLITE_OMIT_CURSOR
  BtCursor *pCur;
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
    MemPage *pPage = pCur->pPage;
    char *zMode = pCur->wrFlag ? "rw" : "ro";
    sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
       pCur, pCur->pgnoRoot, zMode,
       pPage ? pPage->pgno : 0, pCur->idx,
       pCur->isValid ? "" : " eof"
    );
  }
#endif
}
#endif

/*
** Rollback the transaction in progress.  All cursors will be
** invalided by this operation.  Any attempt to use a cursor
** that was open at the beginning of this operation will result
Deleted src/cursor.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















































































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
** 2004 November 21
**
** 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 contains code used to implement the DECLARE...CURSOR syntax
** of SQL and related processing.
**
** Do not confuse SQL cursors and B-tree cursors.  An SQL cursor (as
** implemented by this file) is a user-visible cursor that is created
** using the DECLARE...CURSOR command and deleted using CLOSE.  A
** B-tree cursor is an abstraction of the b-tree layer.  See the btree.c
** module for additional information.  There is also a VDBE-cursor that
** is used by the VDBE module.  Even though all these objects are called
** cursors, they are really very different things.  It is worth your while
** to fully understand the difference.
**
** @(#) $Id: cursor.c,v 1.3 2005/01/20 02:14:31 drh Exp $
*/
#include "sqliteInt.h"
#ifndef SQLITE_OMIT_CURSOR
#include "vdbeInt.h"

/*
** Delete a cursor object.
*/
void sqlite3CursorDelete(SqlCursor *p){
  if( p ){
    int i;
    sqlite3SelectDelete(p->pSelect);
    for(i=0; i<p->nPtr; i++){
      sqlite3VdbeMemRelease(&p->aPtr[i]);
    }
    sqliteFree(p->aPtr);
    sqliteFree(p);
  }
}

/*
** Look up a cursor by name.  Return NULL if not found.
*/
static SqlCursor *findCursor(sqlite3 *db, Token *pName){
  int i;
  SqlCursor *p;
  for(i=0; i<db->nSqlCursor; i++){
    p = db->apSqlCursor[i];
    if( p && sqlite3StrNICmp(p->zName, pName->z, pName->n)==0 ){
      return p;
    }
  }
  return 0;
}  

/*
** The parser calls this routine in order to create a new cursor.
** The arguments are the name of the new cursor and the SELECT statement
** that the new cursor will access.
*/
void sqlite3CursorCreate(Parse *pParse, Token *pName, Select *pSelect){
  SqlCursor *pNew;
  sqlite3 *db = pParse->db;
  int i;
  

  pNew = findCursor(db, pName);
  if( pNew ){
    sqlite3ErrorMsg(pParse, "another cursor named %T already exists", pName);
    goto end_create_cursor;
  }
  if( pSelect==0 ){
    /* This can only happen due to a prior malloc failure */
    goto end_create_cursor;
  }
  for(i=0; i<db->nSqlCursor; i++){
    if( db->apSqlCursor[i]==0 ) break;
  }
  if( i>=db->nSqlCursor ){
    db->apSqlCursor = sqliteRealloc(db->apSqlCursor, (i+1)*sizeof(pNew));
    db->nSqlCursor = i+1;
  }
  db->apSqlCursor[i] = pNew = sqliteMallocRaw( sizeof(*pNew) + pName->n + 1 );
  if( pNew==0 ) goto end_create_cursor;
  pNew->idx = i;
  pNew->zName = (char*)&pNew[1];
  memcpy(pNew->zName, pName->z, pName->n);
  pNew->zName[pName->n] = 0;
  pNew->pSelect = sqlite3SelectDup(pSelect);
  pNew->nPtr = 2;
  pNew->aPtr = sqliteMalloc( sizeof(Mem)*2 );
  for(i=0; i<2; i++){
    pNew->aPtr[i].flags = MEM_Null;
  }

end_create_cursor:
  sqlite3SelectDelete(pSelect);
}

/*
** The parser calls this routine in response to a CLOSE command.  Delete
** the cursor named in the argument.
*/
void sqlite3CursorClose(Parse *pParse, Token *pName){
  SqlCursor *p;
  sqlite3 *db = pParse->db;

  p = findCursor(db, pName);
  if( p==0 ){
    sqlite3ErrorMsg(pParse, "no such cursor: %T", pName);
    return;
  }
  assert( p->idx>=0 && p->idx<db->nSqlCursor );
  assert( db->apSqlCursor[p->idx]==p );
  db->apSqlCursor[p->idx] = 0;
  sqlite3CursorDelete(p);
}

/*
** Reverse the direction the ORDER BY clause on the SELECT statement.
*/
static void reverseSortOrder(Select *p){
  if( p->pOrderBy==0 ){
    /* If there no ORDER BY clause, add a new one that is "rowid DESC" */
    static const Token rowid = { "ROWID", 0, 5 };
    Expr *pExpr = sqlite3Expr(TK_ID, 0, 0, &rowid);
    ExprList *pList = sqlite3ExprListAppend(0, pExpr, 0);
    if( pList )  pList->a[0].sortOrder = SQLITE_SO_DESC;
    p->pOrderBy = pList;
  }else{
    int i;
    ExprList *pList = p->pOrderBy;
    for(i=0; i<pList->nExpr; i++){
      pList->a[i].sortOrder = !pList->a[i].sortOrder;
    }
  }
}

/*
** The parser calls this routine when it sees a complete FETCH statement.
** This routine generates code to implement the FETCH.
**
** Information about the direction of the FETCH has already been inserted
** into the pParse structure by parser rules.  The arguments specify the
** name of the cursor from which we are fetching and the optional INTO
** clause.
*/
void sqlite3Fetch(Parse *pParse, Token *pName, IdList *pInto){
  SqlCursor *p;
  sqlite3 *db = pParse->db;
  Select *pCopy;
  Fetch sFetch;

  p = findCursor(db, pName);
  if( p==0 ){
    sqlite3ErrorMsg(pParse, "no such cursor: %T", pName);
    return;
  }
  sFetch.pCursor = p;
  pCopy = sqlite3SelectDup(p->pSelect);
  pCopy->pFetch = &sFetch;
  switch( pParse->fetchDir ){
    case TK_FIRST: {
      sFetch.isBackwards = 0;
      sFetch.doRewind = 1;
      pCopy->nLimit = pParse->dirArg1;
      pCopy->nOffset = 0;
      break;
    }
    case TK_LAST: {
      reverseSortOrder(pCopy);
      sFetch.isBackwards = 1;
      sFetch.doRewind = 1;
      pCopy->nLimit = pParse->dirArg1;
      pCopy->nOffset = 0;
      break;
    }
    case TK_NEXT: {
      sFetch.isBackwards = 0;
      sFetch.doRewind = 0;
      pCopy->nLimit = pParse->dirArg1;
      pCopy->nOffset = 0;
      break;
    }
    case TK_PRIOR: {
      reverseSortOrder(pCopy);
      sFetch.isBackwards = 1;
      sFetch.doRewind = 0;
      pCopy->nLimit = pParse->dirArg1;
      pCopy->nOffset = 0;
      break;
    }
    case TK_ABSOLUTE: {
      sFetch.isBackwards = 0;
      sFetch.doRewind = 1;
      pCopy->nLimit = pParse->dirArg1;
      pCopy->nOffset = pParse->dirArg2;
      break;
    }
    default: {
      assert( pParse->fetchDir==TK_RELATIVE );
      if( pParse->dirArg2>=0 ){
        /* The index parameter is positive.  Move forward from the current
        ** location */
        sFetch.isBackwards = 0;
        sFetch.doRewind = 0;
        pCopy->nLimit = pParse->dirArg1;
        pCopy->nOffset = pParse->dirArg2;
      }else{
        /* The index is negative.  We have to code two separate SELECTs.
        ** The first one seeks to the no position and the second one does
        ** the query.
        */
        Select *pSeek = sqlite3SelectDup(pCopy);
        reverseSortOrder(pSeek);
        sFetch.isBackwards = 1;
        sFetch.doRewind = 0;
        pSeek->nLimit = pParse->dirArg2;
        pSeek->pFetch = &sFetch;
        sqlite3Select(pParse, pSeek, SRT_Discard, 0, 0, 0, 0, 0);
        sFetch.isBackwards = 0;
        sFetch.doRewind = 0;
        pCopy->nLimit = pParse->dirArg1;
        pCopy->nOffset = 0;
      }
      break;
    }
  }
  sqlite3Select(pParse, pCopy, SRT_Callback, 0, 0, 0, 0, 0);

end_fetch:
  sqlite3IdListDelete(pInto);
}

#endif /* SQLITE_OMIT_CURSOR */
Changes to src/expr.c.
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22







-
+







**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.190 2005/01/30 09:17:59 danielk1977 Exp $
** $Id: expr.c,v 1.191 2005/02/04 04:07:17 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** Return the 'affinity' of the expression pExpr if any.
**
449
450
451
452
453
454
455
456
457


458
459
460
461
462
463
464
449
450
451
452
453
454
455


456
457
458
459
460
461
462
463
464







-
-
+
+








/*
** If cursors, triggers, views and subqueries are all omitted from
** the build, then none of the following routines, except for 
** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
** called with a NULL argument.
*/
#if !defined(SQLITE_OMIT_CURSOR) || !defined(SQLITE_OMIT_VIEW) \
 || !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_SUBQUERY)
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
 || !defined(SQLITE_OMIT_SUBQUERY)
SrcList *sqlite3SrcListDup(SrcList *p){
  SrcList *pNew;
  int i;
  int nByte;
  if( p==0 ) return 0;
  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
  pNew = sqliteMallocRaw( nByte );
Changes to src/main.c.
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24







-
+







**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.277 2005/01/31 12:42:29 danielk1977 Exp $
** $Id: main.c,v 1.278 2005/02/04 04:07:17 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** The following constant value is used by the SQLITE_BIGENDIAN and
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
496
497
498
499
500
501
502







503
504
505
506
507
508
509







-
-
-
-
-
-
-







  if( db->pValue ){
    sqlite3ValueFree(db->pValue);
  }
  if( db->pErr ){
    sqlite3ValueFree(db->pErr);
  }

#ifndef SQLITE_OMIT_CURSOR
  for(j=0; j<db->nSqlCursor; j++){
    sqlite3CursorDelete(db->apSqlCursor[j]);
  }
  sqliteFree(db->apSqlCursor);
#endif

  db->magic = SQLITE_MAGIC_ERROR;
  sqliteFree(db);
  return SQLITE_OK;
}

/*
** Rollback all database files.
Changes to src/parse.y.
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24







-
+







**
*************************************************************************
** This file contains SQLite's grammar for SQL.  Process this file
** using the lemon parser generator to generate C code that runs
** the parser.  Lemon will also generate a header file containing
** numeric codes for all of the tokens.
**
** @(#) $Id: parse.y,v 1.163 2005/01/29 08:32:45 danielk1977 Exp $
** @(#) $Id: parse.y,v 1.164 2005/02/04 04:07:17 danielk1977 Exp $
*/
%token_prefix TK_
%token_type {Token}
%default_type {Token}
%extra_argument {Parse *pParse}
%syntax_error {
  if( pParse->zErrMsg==0 ){
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
596
597
598
599
600
601
602



603
604
605
606
607
608
609







-
-
-







  Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
  A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
}
term(A) ::= INTEGER(X).      {A = sqlite3Expr(@X, 0, 0, &X);}
term(A) ::= FLOAT(X).        {A = sqlite3Expr(@X, 0, 0, &X);}
term(A) ::= STRING(X).       {A = sqlite3Expr(@X, 0, 0, &X);}
expr(A) ::= BLOB(X).         {A = sqlite3Expr(@X, 0, 0, &X);}
%ifdef SQLITE_ENABLE_CURSOR
expr(A) ::= CURRENT OF id.
%endif 
expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
expr(A) ::= VARIABLE(X).     {
  Token *pToken = &X;
  Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
  sqlite3ExprAssignVarNumber(pParse, pExpr);
}
expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
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
961
962
963
964
965
966
967




































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

//////////////////////// ALTER TABLE table ... ////////////////////////////////
%ifndef SQLITE_OMIT_ALTERTABLE
cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
  sqlite3AlterRenameTable(pParse,X,&Z);
}
%endif

////////////////////////////// CURSORS //////////////////////////////////////
%ifdef SQLITE_ENABLE_CURSOR
cmd ::= DECLARE nm(X) CURSOR FOR select(Y).  {sqlite3CursorCreate(pParse,&X,Y);}
cmd ::= CLOSE nm(X).                         {sqlite3CursorClose(pParse,&X);}

cmd ::= FETCH direction FROM nm(N) into_opt(D).
                                      {sqlite3Fetch(pParse,&N,D);}

%type into_opt {IdList*}
%destructor into_opt {sqlite3IdListDelete($$);}
into_opt(A) ::= .                     {A = 0;}
into_opt(A) ::= INTO inscollist(X).   {A = X;}
direction ::= NEXT(X) count_opt(Y).   {pParse->fetchDir=@X; pParse->dirArg1=Y;}
direction ::= PRIOR(X) count_opt(Y).  {pParse->fetchDir=@X; pParse->dirArg1=Y;}
direction ::= FIRST(X) count_opt(Y).  {pParse->fetchDir=@X; pParse->dirArg1=Y;}
direction ::= LAST(X) count_opt(Y).   {pParse->fetchDir=@X; pParse->dirArg1=Y;}
direction ::= ABSOLUTE(X) signed(Z) comma_count_opt(Y).
                   {pParse->fetchDir=@X; pParse->dirArg1=Y; pParse->dirArg2=Z;}
direction ::= RELATIVE(X) signed(Z) comma_count_opt(Y).
                   {pParse->fetchDir=@X; pParse->dirArg1=Y; pParse->dirArg2=Z;}

%type count_opt {int}
count_opt(A) ::= .          {A = 1;}
count_opt(A) ::= signed(X). {A = X;}
%type comma_count_opt {int}
comma_count_opt(A) ::= .                 {A = 1;}
comma_count_opt(A) ::= COMMA signed(X).  {A = X;}
%endif // SQLITE_ENABLE_CURSOR
Changes to src/pragma.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21













-
+







/*
** 2003 April 6
**
** 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 contains code used to implement the PRAGMA command.
**
** $Id: pragma.c,v 1.86 2005/01/24 10:25:59 danielk1977 Exp $
** $Id: pragma.c,v 1.87 2005/02/04 04:07:17 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/* Ignore this whole file if pragmas are disabled
*/
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
534
535
536
537
538
539
540


















541
542
543
544
545
546
547







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







      sqlite3VdbeAddOp(v, OP_Integer, i, 0);
      sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
      sqlite3VdbeOp3(v, OP_String8, 0, 0,
           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
      sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
    }
  }else

#ifndef SQLITE_OMIT_CURSOR
  if( sqlite3StrICmp(zLeft, "cursor_list")==0 ){
    int i;
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
    sqlite3VdbeSetNumCols(v, 2);
    sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
    sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
    for(i=0; i<db->nSqlCursor; i++){
      SqlCursor *p = db->apSqlCursor[i];
      if( p==0 ) continue;
      assert( p->zName!=0 );
      sqlite3VdbeAddOp(v, OP_Integer, i, 0);
      sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
      sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
    }
  }else
#endif /* SQLITE_OMIT_CURSOR */
#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */

#ifndef SQLITE_OMIT_FOREIGN_KEY
  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
    FKey *pFK;
    Table *pTab;
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
Changes to src/select.c.
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22







-
+







**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
** $Id: select.c,v 1.237 2005/01/31 12:42:29 danielk1977 Exp $
** $Id: select.c,v 1.238 2005/02/04 04:07:17 danielk1977 Exp $
*/
#include "sqliteInt.h"


/*
** Allocate a new Select structure and return a pointer to that
** structure.
543
544
545
546
547
548
549
550

551
552
553
554
555
556
557
543
544
545
546
547
548
549

550
551
552
553
554
555
556
557







-
+







        pushOntoSorter(pParse, v, pOrderBy);
      }else{
        sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
      }
      break;
    }

#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_CURSOR)
#if !defined(SQLITE_OMIT_TRIGGER)
    /* Discard the results.  This is used for SELECT statements inside
    ** the body of a TRIGGER.  The purpose of such selects is to call
    ** user-defined functions that have side effects.  We do not care
    ** about the actual results of the select.
    */
    default: {
      assert( eDest==SRT_Discard );
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2518
2519
2520
2521
2522
2523
2524






2525
2526
2527
2528
2529
2530
2531







-
-
-
-
-
-







  if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1;
  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;

#ifndef SQLITE_OMIT_COMPOUND_SELECT
  /* If there is are a sequence of queries, do the earlier ones first.
  */
  if( p->pPrior ){
#ifndef SQLITE_OMIT_CURSOR
    if( p->pFetch ){
      sqlite3ErrorMsg(pParse, "cursors cannot be used on compound queries");
      goto select_end;
    }
#endif
    return multiSelect(pParse, p, eDest, iParm, aff);
  }
#endif

  saveAggregateInfo(pParse, &sAggInfo);
  pOrderBy = p->pOrderBy;
  if( eDest==SRT_Union || eDest==SRT_Except || eDest==SRT_Discard ){
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2573
2574
2575
2576
2577
2578
2579




















2580
2581
2582
2583
2584
2585
2586







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







    case SRT_Discard:
      pOrderBy = 0;
      break;
    default:
      break;
  }

  /* We cannot use a SQL cursor on a join or on a DISTINCT query
  */
#ifndef SQLITE_OMIT_CURSOR
  if( p->pFetch ){
    if( p->isDistinct ){
      sqlite3ErrorMsg(pParse, "cursors cannot be used on DISTINCT queries");
      goto select_end;
    }
    if( pTabList->nSrc>0 ){
      sqlite3ErrorMsg(pParse, "cursors cannot be used on joins");
      goto select_end;
    }
    if( pTabList->a[0].pSelect ){
      sqlite3ErrorMsg(pParse, "cursor cannot be used with nested queries "
          "or views");
      goto select_end;
    }
  }
#endif

  /* Begin generating code.
  */
  v = sqlite3GetVdbe(pParse);
  if( v==0 ) goto select_end;

  /* Identify column names if we will be using them in a callback.  This
  ** step is skipped if the output is going to some other destination.
Changes to src/sqliteInt.h.
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
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













-
+




-
-
-
-
-
-
-
-







/*
** 2001 September 15
**
** 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.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.365 2005/01/31 12:56:44 danielk1977 Exp $
** @(#) $Id: sqliteInt.h,v 1.366 2005/02/04 04:07:17 danielk1977 Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_

/*
** Cursor support is turned off unless the SQLITE_ENABLE_CURSOR option
** is defined.
*/
#ifndef SQLITE_ENABLE_CURSOR
# define SQLITE_OMIT_CURSOR 1
#endif

/*
** These #defines should enable >2GB file support on Posix if the
** underlying operating system supports it.  If the OS lacks
** large file support, or if the OS is windows, these should be no-ops.
**
** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
** on the compiler command line.  This is necessary if you are compiling
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
316
317
318
319
320
321
322

323
324

325
326
327
328
329
330
331







-


-







typedef struct TriggerStack TriggerStack;
typedef struct FKey FKey;
typedef struct Db Db;
typedef struct AuthContext AuthContext;
typedef struct KeyClass KeyClass;
typedef struct CollSeq CollSeq;
typedef struct KeyInfo KeyInfo;
typedef struct SqlCursor SqlCursor;
typedef struct NameContext NameContext;
typedef struct Fetch Fetch;
typedef struct CursorSubst CursorSubst;

/*
** Each database file to be accessed by the system is an instance
** of the following structure.  There are normally two of these structures
** in the sqlite.aDb[] array.  aDb[0] is the main database file and
** aDb[1] is the database file used to hold temporary tables.  Additional
** databases may be attached.
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
431
432
433
434
435
436
437




438
439
440
441
442
443
444







-
-
-
-







  void *pAuthArg;               /* 1st argument to the access auth function */
#endif
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  int (*xProgress)(void *);     /* The progress callback */
  void *pProgressArg;           /* Argument to the progress callback */
  int nProgressOps;             /* Number of opcodes for progress callback */
#endif
#ifndef SQLITE_OMIT_CURSOR
  int nSqlCursor;               /* Number of slots in apSqlCursor[] */
  SqlCursor **apSqlCursor;      /* Pointers to all active SQL cursors */
#endif
  int errCode;                  /* Most recent error code (SQLITE_*) */
  u8 enc;                       /* Text encoding for this database. */
  u8 autoCommit;                /* The auto-commit flag. */
  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  void *pCollNeededArg;
  sqlite3_value *pValue;        /* Value used for transient conversions */
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
956
957
958
959
960
961
962










963
964
965
966
967
968
969







-
-
-
-
-
-
-
-
-
-







  int iTop;            /* The very beginning of the WHERE loop */
  int iContinue;       /* Jump here to continue with next record */
  int iBreak;          /* Jump here to break out of the loop */
  int nLevel;          /* Number of nested loop */
  WhereLevel a[1];     /* Information about each nest loop in the WHERE */
};

/*
** An instance of the following structure is used to store information
** about a single FETCH sql command.
*/
struct Fetch {
  SqlCursor *pCursor;  /* Cursor used by the fetch */
  int isBackwards;     /* Cursor moves backwards if true, forward if false */
  int doRewind;        /* True to rewind cursor before starting */
};

/*
** A NameContext defines a context in which to resolve table and column
** names.  The context consists of a list of tables (the pSrcList) field and
** a list of named expression (pEList).  The named expression list may
** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
** to the table being operated on by INSERT, UPDATE, or DELETE.  The
** pEList corresponds to the result set of a SELECT and is NULL for
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1095
1096
1097
1098
1099
1100
1101





1102
1103
1104
1105
1106
1107
1108







-
-
-
-
-







  ** each recursion */

  int nVar;            /* Number of '?' variables seen in the SQL so far */
  int nVarExpr;        /* Number of used slots in apVarExpr[] */
  int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
  Expr **apVarExpr;    /* Pointers to :aaa and $aaaa wildcard expressions */
  u8 explain;          /* True if the EXPLAIN flag is found on the query */
#ifndef SQLITE_OMIT_CURSOR
  u8 fetchDir;         /* The direction argument to the FETCH command */
  int dirArg1;         /* First argument to the direction */
  int dirArg2;         /* Second argument to the direction */
#endif
  Token sErrToken;     /* The token at which the error occurred */
  Token sNameToken;    /* Token with unqualified schema object name */
  Token sLastToken;    /* The last token parsed */
  const char *zSql;    /* All SQL text */
  const char *zTail;   /* All SQL text past the last semicolon parsed */
  Table *pNewTable;    /* A table being constructed by CREATE TABLE */
  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1278
1279
1280
1281
1282
1283
1284













1285
1286
1287
1288
1289
1290
1291







-
-
-
-
-
-
-
-
-
-
-
-
-







** A pointer to this structure is used to communicate information
** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
*/
typedef struct {
  sqlite3 *db;        /* The database being initialized */
  char **pzErrMsg;    /* Error message stored here */
} InitData;

/*
** Each SQL cursor (a cursor created by the DECLARE ... CURSOR syntax)
** is represented by an instance of the following structure.
*/
struct SqlCursor {
  char *zName;           /* Name of this cursor */
  int idx;               /* Index of this cursor in db->apSqlCursor[] */
  Select *pSelect;       /* The SELECT statement that defines this cursor */
  int nPtr;              /* Number of slots in aPtr[] */
  sqlite3_value *aPtr;   /* Values that define the current cursor position */
};


/*
 * This global flag is set for performance testing of triggers. When it is set
 * SQLite will perform the overhead of building new and old trigger references 
 * even when no triggers exist
 */
extern int sqlite3_always_code_trigger_setup;
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1532
1533
1534
1535
1536
1537
1538







1539







-
-
-
-
-
-
-

void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
int sqlite3GetToken(const unsigned char *, int *);
void sqlite3NestedParse(Parse*, const char*, ...);
void sqlite3ExpirePreparedStatements(sqlite3*);
void sqlite3CodeSubselect(Parse *, Expr *);
int sqlite3SelectResolve(Parse *, Select *, NameContext *);

#ifndef SQLITE_OMIT_CURSOR
void sqlite3CursorDelete(SqlCursor*);
void sqlite3CursorCreate(Parse*, Token*, Select*);
void sqlite3CursorClose(Parse*, Token*);
void sqlite3Fetch(Parse*, Token*, IdList*);
#endif /* SQLITE_OMIT_CURSOR */

#endif
Changes to src/test1.c.
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
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.
**
*************************************************************************
** Code for testing the printf() interface to SQLite.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
**
** $Id: test1.c,v 1.128 2005/01/25 04:27:55 danielk1977 Exp $
** $Id: test1.c,v 1.129 2005/02/04 04:07:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include "os.h"
#include <stdlib.h>
#include <string.h>

2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2725
2726
2727
2728
2729
2730
2731






2732
2733
2734
2735
2736
2737
2738







-
-
-
-
-
-








#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
  Tcl_SetVar2(interp, "sqlite_options", "conflict", "0", TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp, "sqlite_options", "conflict", "1", TCL_GLOBAL_ONLY);
#endif

#ifdef SQLITE_OMIT_CURSOR
  Tcl_SetVar2(interp, "sqlite_options", "cursor", "0", TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp, "sqlite_options", "cursor", "1", TCL_GLOBAL_ONLY);
#endif

#ifdef SQLITE_OMIT_DATETIME_FUNCS
  Tcl_SetVar2(interp, "sqlite_options", "datetime", "0", TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp, "sqlite_options", "datetime", "1", TCL_GLOBAL_ONLY);
#endif

#ifdef SQLITE_OMIT_EXPLAIN
Changes to src/vdbe.c.
39
40
41
42
43
44
45
46

47
48
49
50
51
52
53
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53







-
+







**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.449 2005/01/29 08:32:45 danielk1977 Exp $
** $Id: vdbe.c,v 1.450 2005/02/04 04:07:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4473
4474
4475
4476
4477
4478
4479
















































4480
4481
4482
4483
4484
4485
4486







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







case OP_Vacuum: {
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; 
  rc = sqlite3RunVacuum(&p->zErrMsg, db);
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  break;
}

#ifndef SQLITE_OMIT_CURSOR
/* Opcode: CursorStore P1 P2 *
**
** The implementation of SQL cursors (not to be confused with VDBE cursors
** or B-tree cursors) stores information in the SQLite database connection
** structure (the sqlite3* pointer) that identifies the row
** in a table that an SQL cursor is pointing to.  This opcode is
** used to store that information.  P1 is an index of an SQL cursor.
** P2 is the index of a memory slot within that SQL cursor.  This opcode
** pops the top of the stack and stores it in the SQL cursor.
*/
case OP_CursorStore: {
  SqlCursor *pCursor;
  assert( pTos>=p->aStack );
  assert( pOp->p1>=0 && pOp->p1<db->nSqlCursor );
  pCursor = db->apSqlCursor[pOp->p1];
  assert( pCursor!=0 );
  assert( pOp->p2>=0 && pOp->p2<2 );  
  rc = sqlite3VdbeMemMove(&pCursor->aPtr[pOp->p1], pTos);
  pTos--;
  break;
}

/* Opcode: CursorLoad P1 P2 *
**
** The implementation of SQL cursors (not to be confused with VDBE cursors
** or B-tree cursors) stores information in the SQLite database connection
** structure (the sqlite3* pointer) that effectively records the current
** location in a table that an SQL cursor is pointing to.  This opcode is
** used to recover that information.  P1 is an index of an SQL cursor.
** P2 is the index of a memory slot within that SQL cursor.  This opcode
** pushes a new value onto the stack which is a copy of the information
** obtained from entry P2 of cursor P1.
*/
case OP_CursorLoad: {
  SqlCursor *pCursor;
  int i = pOp->p1;
  assert( pTos>=p->aStack );
  assert( pOp->p1>=0 && pOp->p1<db->nSqlCursor );
  pCursor = db->apSqlCursor[pOp->p1];
  assert( pCursor!=0 );
  assert( pOp->p2>=0 && pOp->p2<2 );  
  pTos++;
  sqlite3VdbeMemShallowCopy(pTos, &pCursor->aPtr[i], MEM_Ephem);
  break;
}
#endif /* SQLITE_OMIT_CURSOR */

/* Opcode: Expire P1 * *
**
** Cause precompiled statements to become expired. An expired statement
** fails with an error code of SQLITE_SCHEMA if it is ever executed 
** (via sqlite3_step()).
** 
** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
Changes to test/capi2.test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21













-
+







# 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.24 2005/01/20 22:48:48 drh Exp $
# $Id: capi2.test,v 1.25 2005/02/04 04:07:18 danielk1977 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} {
248
249
250
251
252
253
254




















255
256
257
258
259
260
261
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







       [get_row_values $VM] \
       [get_column_names $VM]
} {SQLITE_ERROR 0 {} {}}
do_test capi2-3.19 {
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
} {SQLITE_CONSTRAINT {t2.a may not be NULL}}

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 {
  sqlite3_errcode $DB
} {SQLITE_CONSTRAINT}

# 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 {
Changes to test/corrupt.test.
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23







-
+







#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests to make sure SQLite does not crash or
# segfault if it sees a corrupt database file.
#
# $Id: corrupt.test,v 1.4 2005/01/20 02:17:02 danielk1977 Exp $
# $Id: corrupt.test,v 1.5 2005/02/04 04:07:18 danielk1977 Exp $

catch {file delete -force test.db}
catch {file delete -force test.db-journal}

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

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
77
78
79
80
81
82
83

84
85
86
87
88
89
90







-







  db close
  copy_file test.bu test.db
  set fd [open test.db r+]
  fconfigure $fd -translation binary
  seek $fd $i
  puts -nonewline $fd $junk
  close $fd
  sqlite3 db test.db
  do_test corrupt-2.$tn.1 {
    sqlite3 db test.db
    catchsql {SELECT count(*) FROM sqlite_master}
    set x {}
  } {}
  do_test corrupt-2.$tn.2 {
    catchsql {SELECT count(*) FROM t1}
Deleted test/cursor.test.
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








































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
# 2004 November 22
#
# 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 is DECLARE...CURSOR functionality
#
# $Id: cursor.test,v 1.2 2004/11/23 01:47:31 drh Exp $
#

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

# If SQLITE_OMIT_CURSOR is defined, omit this file.
ifcapable {!cursor} {
  finish_test
  return
}

########
# Test the logic that creates and destroys cursors
########
do_test cursor-1.1 {
  execsql {
    CREATE TABLE t1(a,b,c);
    CREATE INDEX t1i1 ON t1(a);
    CREATE INDEX t1i2 ON t1(b,c);
  }
  execsql {
    DECLARE c1 CURSOR FOR SELECT c FROM t1 ORDER BY a;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.2 {
    execsql {PRAGMA cursor_list}
  } {0 c1}
}
do_test cursor-1.3 {
  execsql {
    DECLARE c2 CURSOR FOR SELECT a FROM t1 ORDER BY b, c;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.4 {
    execsql {PRAGMA cursor_list}
  } {0 c1 1 c2}
}
do_test cursor-1.5 {
  catchsql {
    CLOSE c3;
  }
} {1 {no such cursor: c3}}
ifcapable schema_pragmas {
  do_test cursor-1.6 {
    execsql {PRAGMA cursor_list}
  } {0 c1 1 c2}
}
do_test cursor-1.7 {
  catchsql {
    CLOSE c1;
  }
} {0 {}}
ifcapable schema_pragmas {
  do_test cursor-1.8 {
    execsql {PRAGMA cursor_list}
  } {1 c2}
}
do_test cursor-1.9 {
  catchsql {
    CLOSE c1;
  }
} {1 {no such cursor: c1}}
ifcapable schema_pragmas {
  do_test cursor-1.10 {
    execsql {PRAGMA cursor_list}
  } {1 c2}
}
do_test cursor-1.11 {
  catchsql {
    DECLARE c2 CURSOR FOR SELECT * FROM t1;
  }
} {1 {another cursor named c2 already exists}}
do_test cursor-1.12 {
  catchsql {
    DECLARE c3 CURSOR FOR SELECT * FROM t1;
  }
} {0 {}}
ifcapable schema_pragmas {
  do_test cursor-1.13 {
    execsql {PRAGMA cursor_list}
  } {0 c3 1 c2}
}
do_test cursor-1.14 {
  execsql {
    CLOSE c2;
    CLOSE c3;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.15 {
    execsql {PRAGMA cursor_list}
  } {}
}

set all {}
for {set i 1} {$i<=50} {incr i} {
  lappend all [expr {$i-1}] x$i
  do_test cursor-2.1.$i.1 {
    execsql "DECLARE x$i CURSOR FOR SELECT * FROM t1"
  } {}
  ifcapable schema_pragmas {
    do_test cursor-2.1.$i.2 {
      execsql {PRAGMA cursor_list}
    } $all
  }
}
for {set i 1} {$i<=50} {incr i} {
  set all [lrange $all 2 end]
  do_test cursor-2.2.$i.1 {
    execsql "CLOSE x$i"
  } {}
  ifcapable schema_pragmas {
    do_test cursor-2.2.$i.2 {
      execsql {PRAGMA cursor_list}
    } $all
  }
}  


finish_test
Changes to tool/mkkeywordhash.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9







10
11
12
13
14
15
16









-
-
-
-
-
-
-







/*
** Compile and run this standalone program in order to generate code that
** implements a function that will translate alphabetic identifiers into
** parser token codes.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
** Cursor support is off by default.
*/
#if !defined(SQLITE_ENABLE_CURSOR) && !defined(SQLITE_OMIT_CURSOR)
# define SQLITE_OMIT_CURSOR 1
#endif

/*
** All the keywords of the SQL language are stored as in a hash
** table composed of instances of the following structure.
*/
typedef struct Keyword Keyword;
struct Keyword {
  char *zName;         /* The keyword name */
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
51
52
53
54
55
56
57





58
59
60
61
62
63
64







-
-
-
-
-







#  define COMPOUND   16
#endif
#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
#  define CONFLICT   0
#else
#  define CONFLICT   32
#endif
#ifdef SQLITE_OMIT_CURSOR
#  define CURSOR     0
#else
#  define CURSOR     64
#endif
#ifdef SQLITE_OMIT_EXPLAIN
#  define EXPLAIN    0
#else
#  define EXPLAIN    128
#endif
#ifdef SQLITE_OMIT_FOREIGN_KEY
#  define FKEY       0
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
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







-















-






-
-




-

















-
-
-
+









/*
** These are the keywords
*/
static Keyword aKeywordTable[] = {
  { "ABORT",            "TK_ABORT",        CONFLICT|TRIGGER       },
  { "ABSOLUTE",         "TK_ABSOLUTE",     CURSOR                 },
  { "AFTER",            "TK_AFTER",        TRIGGER                },
  { "ALL",              "TK_ALL",          ALWAYS                 },
  { "ALTER",            "TK_ALTER",        ALTER                  },
  { "AND",              "TK_AND",          ALWAYS                 },
  { "AS",               "TK_AS",           ALWAYS                 },
  { "ASC",              "TK_ASC",          ALWAYS                 },
  { "ATTACH",           "TK_ATTACH",       ATTACH                 },
  { "AUTOINCREMENT",    "TK_AUTOINCR",     AUTOINCR               },
  { "BEFORE",           "TK_BEFORE",       TRIGGER                },
  { "BEGIN",            "TK_BEGIN",        ALWAYS                 },
  { "BETWEEN",          "TK_BETWEEN",      ALWAYS                 },
  { "BY",               "TK_BY",           ALWAYS                 },
  { "CASCADE",          "TK_CASCADE",      FKEY                   },
  { "CASE",             "TK_CASE",         ALWAYS                 },
  { "CHECK",            "TK_CHECK",        ALWAYS                 },
  { "CLOSE",            "TK_CLOSE",        CURSOR                 },
  { "COLLATE",          "TK_COLLATE",      ALWAYS                 },
  { "COMMIT",           "TK_COMMIT",       ALWAYS                 },
  { "CONFLICT",         "TK_CONFLICT",     CONFLICT               },
  { "CONSTRAINT",       "TK_CONSTRAINT",   ALWAYS                 },
  { "CREATE",           "TK_CREATE",       ALWAYS                 },
  { "CROSS",            "TK_JOIN_KW",      ALWAYS                 },
  { "CURSOR",           "TK_CURSOR",       CURSOR                 },
  { "CURRENT",          "TK_CURRENT",      CURSOR                 },
  { "CURRENT_DATE",     "TK_CDATE",        ALWAYS                 },
  { "CURRENT_TIME",     "TK_CTIME",        ALWAYS                 },
  { "CURRENT_TIMESTAMP","TK_CTIMESTAMP",   ALWAYS                 },
  { "DATABASE",         "TK_DATABASE",     ATTACH                 },
  { "DECLARE",          "TK_DECLARE",      CURSOR                 },
  { "DEFAULT",          "TK_DEFAULT",      ALWAYS                 },
  { "DEFERRED",         "TK_DEFERRED",     ALWAYS                 },
  { "DEFERRABLE",       "TK_DEFERRABLE",   FKEY                   },
  { "DELETE",           "TK_DELETE",       ALWAYS                 },
  { "DESC",             "TK_DESC",         ALWAYS                 },
  { "DETACH",           "TK_DETACH",       ATTACH                 },
  { "DISTINCT",         "TK_DISTINCT",     ALWAYS                 },
  { "DROP",             "TK_DROP",         ALWAYS                 },
  { "END",              "TK_END",          ALWAYS                 },
  { "EACH",             "TK_EACH",         TRIGGER                },
  { "ELSE",             "TK_ELSE",         ALWAYS                 },
  { "ESCAPE",           "TK_ESCAPE",       ALWAYS                 },
  { "EXCEPT",           "TK_EXCEPT",       COMPOUND               },
  { "EXCLUSIVE",        "TK_EXCLUSIVE",    ALWAYS                 },
  { "EXISTS",           "TK_EXISTS",       SUBQUERY               },
  { "EXPLAIN",          "TK_EXPLAIN",      EXPLAIN                },
  { "FAIL",             "TK_FAIL",         CONFLICT|TRIGGER       },
  { "FETCH",            "TK_FETCH",        CURSOR                 },
  { "FIRST",            "TK_FIRST",        CURSOR                 },
  { "FOR",              "TK_FOR",          TRIGGER|CURSOR         },
  { "FOR",              "TK_FOR",          TRIGGER                },
  { "FOREIGN",          "TK_FOREIGN",      FKEY                   },
  { "FROM",             "TK_FROM",         ALWAYS                 },
  { "FULL",             "TK_JOIN_KW",      ALWAYS                 },
  { "GLOB",             "TK_GLOB",         ALWAYS                 },
  { "GROUP",            "TK_GROUP",        ALWAYS                 },
  { "HAVING",           "TK_HAVING",       ALWAYS                 },
  { "IGNORE",           "TK_IGNORE",       CONFLICT|TRIGGER       },
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
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







-










-




-







  { "JOIN",             "TK_JOIN",         ALWAYS                 },
  { "KEY",              "TK_KEY",          ALWAYS                 },
  { "LEFT",             "TK_JOIN_KW",      ALWAYS                 },
  { "LIKE",             "TK_LIKE",         ALWAYS                 },
  { "LIMIT",            "TK_LIMIT",        ALWAYS                 },
  { "MATCH",            "TK_MATCH",        ALWAYS                 },
  { "NATURAL",          "TK_JOIN_KW",      ALWAYS                 },
  { "NEXT",             "TK_NEXT",         CURSOR                 },
  { "NOT",              "TK_NOT",          ALWAYS                 },
  { "NOTNULL",          "TK_NOTNULL",      ALWAYS                 },
  { "NULL",             "TK_NULL",         ALWAYS                 },
  { "OF",               "TK_OF",           ALWAYS                 },
  { "OFFSET",           "TK_OFFSET",       ALWAYS                 },
  { "ON",               "TK_ON",           ALWAYS                 },
  { "OR",               "TK_OR",           ALWAYS                 },
  { "ORDER",            "TK_ORDER",        ALWAYS                 },
  { "OUTER",            "TK_JOIN_KW",      ALWAYS                 },
  { "PRAGMA",           "TK_PRAGMA",       PRAGMA                 },
  { "PRIOR",            "TK_PRIOR",        CURSOR                 },
  { "PRIMARY",          "TK_PRIMARY",      ALWAYS                 },
  { "RAISE",            "TK_RAISE",        TRIGGER                },
  { "REFERENCES",       "TK_REFERENCES",   FKEY                   },
  { "REINDEX",          "TK_REINDEX",      REINDEX                },
  { "RELATIVE",         "TK_RELATIVE",     CURSOR                 },
  { "RENAME",           "TK_RENAME",       ALTER                  },
  { "REPLACE",          "TK_REPLACE",      CONFLICT               },
  { "RESTRICT",         "TK_RESTRICT",     FKEY                   },
  { "RIGHT",            "TK_JOIN_KW",      ALWAYS                 },
  { "ROLLBACK",         "TK_ROLLBACK",     ALWAYS                 },
  { "ROW",              "TK_ROW",          TRIGGER                },
  { "SELECT",           "TK_SELECT",       ALWAYS                 },