SQLite

Check-in [6ea5cebf05]
Login

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

Overview
Comment:rework the VDBE engine. NULL is now distinct from "" (CVS 49)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6ea5cebf05562de00d2cf0b9e2aac5f3857638ee
User & Date: drh 2000-06-04 12:58:37.000
Context
2000-06-05
02:07
:-) (CVS 50) (check-in: 1cf2873d55 user: drh tags: trunk)
2000-06-04
12:58
rework the VDBE engine. NULL is now distinct from "" (CVS 49) (check-in: 6ea5cebf05 user: drh tags: trunk)
2000-06-03
19:28
:-) (CVS 48) (check-in: f766564165 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/expr.c.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines used for processing expressions
**
** $Id: expr.c,v 1.4 2000/06/03 19:19:41 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine walks an expression tree and resolves references to
** table fields.  Nodes of the form ID.ID or ID resolve into an
** index to the table in the table list and a field offset.  The opcode







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines used for processing expressions
**
** $Id: expr.c,v 1.5 2000/06/04 12:58:37 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine walks an expression tree and resolves references to
** table fields.  Nodes of the form ID.ID or ID resolve into an
** index to the table in the table list and a field offset.  The opcode
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    case TK_STRING: {
      int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
      sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
      sqliteVdbeDequoteP3(v, addr);
      break;
    }
    case TK_NULL: {
      sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
      break;
    }
    case TK_AND:
    case TK_OR:
    case TK_PLUS:
    case TK_STAR:
    case TK_MINUS:







|







320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    case TK_STRING: {
      int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
      sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
      sqliteVdbeDequoteP3(v, addr);
      break;
    }
    case TK_NULL: {
      sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
      break;
    }
    case TK_AND:
    case TK_OR:
    case TK_PLUS:
    case TK_STAR:
    case TK_MINUS:
Changes to src/insert.c.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle INSERT statements.
**
** $Id: insert.c,v 1.4 2000/06/03 18:06:53 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine is call to handle SQL of the following form:
**
**    insert into TABLE (IDLIST) values(EXPRLIST)







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle INSERT statements.
**
** $Id: insert.c,v 1.5 2000/06/04 12:58:38 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine is call to handle SQL of the following form:
**
**    insert into TABLE (IDLIST) values(EXPRLIST)
118
119
120
121
122
123
124
125


126

127
128
129
130
131
132
133
      }else{
        for(j=0; j<pField->nId; j++){
          if( pField->a[j].idx==i ) break;
        }
      }
      if( pField && j>=pField->nId ){
        char *zDflt = pTab->aCol[i].zDflt;
        if( zDflt==0 ) zDflt = "";


        sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);

      }else{
        sqliteExprCode(pParse, pList->a[j].pExpr);
      }
    }
    sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
    sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
    sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);







|
>
>
|
>







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      }else{
        for(j=0; j<pField->nId; j++){
          if( pField->a[j].idx==i ) break;
        }
      }
      if( pField && j>=pField->nId ){
        char *zDflt = pTab->aCol[i].zDflt;
        if( zDflt==0 ){
          sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
        }else{
          sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
        }
      }else{
        sqliteExprCode(pParse, pList->a[j].pExpr);
      }
    }
    sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
    sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
    sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
142
143
144
145
146
147
148
149


150

151
152
153
154
155
156
157
        }else{
          for(j=0; j<pField->nId; j++){
            if( pField->a[j].idx==idx ) break;
          }
        }
        if( pField && j>=pField->nId ){
          char *zDflt = pTab->aCol[idx].zDflt;
          if( zDflt==0 ) zDflt = "";


          sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);

        }else{
          sqliteExprCode(pParse, pList->a[j].pExpr);
        }
      }
      sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
      sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0);
      sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0);







|
>
>
|
>







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        }else{
          for(j=0; j<pField->nId; j++){
            if( pField->a[j].idx==idx ) break;
          }
        }
        if( pField && j>=pField->nId ){
          char *zDflt = pTab->aCol[idx].zDflt;
          if( zDflt==0 ){
            sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
          }else{
            sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
          }
        }else{
          sqliteExprCode(pParse, pList->a[j].pExpr);
        }
      }
      sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
      sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0);
      sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0);
Changes to src/select.c.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle SELECT statements.
**
** $Id: select.c,v 1.5 2000/06/03 18:06:53 drh Exp $
*/
#include "sqliteInt.h"


/*
** Process a SELECT statement.
*/







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle SELECT statements.
**
** $Id: select.c,v 1.6 2000/06/04 12:58:38 drh Exp $
*/
#include "sqliteInt.h"


/*
** Process a SELECT statement.
*/
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  */
  if( isAgg ){
    for(i=0; i<pEList->nExpr; i++){
      Expr *p = pEList->a[i].pExpr;
      switch( sqliteFuncId(&p->token) ){
        case FN_Min:
        case FN_Max: {
          sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
          break;
        }
        default: {
          sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
          break;
        }
      }







|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  */
  if( isAgg ){
    for(i=0; i<pEList->nExpr; i++){
      Expr *p = pEList->a[i].pExpr;
      switch( sqliteFuncId(&p->token) ){
        case FN_Min:
        case FN_Max: {
          sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
          break;
        }
        default: {
          sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
          break;
        }
      }
247
248
249
250
251
252
253

254
255
256
257
258
259
260
      int id = sqliteFuncId(&p->token);
      int op, p1;
      if( n>1 ){
        sqliteVdbeAddOp(v, OP_Pull, n-1, 0, 0, 0);
      }
      if( id!=FN_Count && p->pList && p->pList->nExpr>=1 ){
        sqliteExprCode(pParse, p->pList->a[0].pExpr);

      }
      switch( sqliteFuncId(&p->token) ){
        case FN_Count: op = OP_AddImm; p1 = 1; break;
        case FN_Sum:   op = OP_Add;    p1 = 0; break;
        case FN_Min:   op = OP_Min;    p1 = 1; break;
        case FN_Max:   op = OP_Max;    p1 = 0; break;
      }







>







247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
      int id = sqliteFuncId(&p->token);
      int op, p1;
      if( n>1 ){
        sqliteVdbeAddOp(v, OP_Pull, n-1, 0, 0, 0);
      }
      if( id!=FN_Count && p->pList && p->pList->nExpr>=1 ){
        sqliteExprCode(pParse, p->pList->a[0].pExpr);
        sqliteVdbeAddOp(v, OP_Concat, 1, 0, 0, 0);
      }
      switch( sqliteFuncId(&p->token) ){
        case FN_Count: op = OP_AddImm; p1 = 1; break;
        case FN_Sum:   op = OP_Add;    p1 = 0; break;
        case FN_Min:   op = OP_Min;    p1 = 1; break;
        case FN_Max:   op = OP_Max;    p1 = 0; break;
      }
Changes to src/shell.c.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
**
** $Id: shell.c,v 1.6 2000/06/02 13:27:59 drh Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sqlite.h"
#include <unistd.h>
#include <ctype.h>







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
**
** $Id: shell.c,v 1.7 2000/06/04 12:58:38 drh Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sqlite.h"
#include <unistd.h>
#include <ctype.h>
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
static int callback(void *pArg, int nArg, char **azArg, char **azCol){
  int i;
  struct callback_data *p = (struct callback_data*)pArg;
  switch( p->mode ){
    case MODE_Line: {
      if( p->cnt++>0 ) fprintf(p->out,"\n");
      for(i=0; i<nArg; i++){
        fprintf(p->out,"%s = %s\n", azCol[i], azArg[i]);
      }
      break;
    }
    case MODE_Column: {
      if( p->cnt++==0 && p->showHeader ){
        for(i=0; i<nArg; i++){
          int w;







|







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
static int callback(void *pArg, int nArg, char **azArg, char **azCol){
  int i;
  struct callback_data *p = (struct callback_data*)pArg;
  switch( p->mode ){
    case MODE_Line: {
      if( p->cnt++>0 ) fprintf(p->out,"\n");
      for(i=0; i<nArg; i++){
        fprintf(p->out,"%s = %s\n", azCol[i], azArg[i] ? azArg[i] : 0);
      }
      break;
    }
    case MODE_Column: {
      if( p->cnt++==0 && p->showHeader ){
        for(i=0; i<nArg; i++){
          int w;
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
      for(i=0; i<nArg; i++){
        int w;
        if( i<ArraySize(p->colWidth) && p->colWidth[i]>0 ){
           w = p->colWidth[i];
        }else{
           w = 10;
        }
        fprintf(p->out,"%-*.*s%s",w,w,azArg[i], i==nArg-1 ? "\n": "  ");

      }
      break;
    }
    case MODE_List: {
      if( p->cnt++==0 && p->showHeader ){
        for(i=0; i<nArg; i++){
          fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
        }
      }
      for(i=0; i<nArg; i++){
        fprintf(p->out,"%s%s",azArg[i], i==nArg-1 ? "\n" : p->separator);

      }
      break;
    }
    case MODE_Html: {
      if( p->cnt++==0 && p->showHeader ){
        fprintf(p->out,"<TR>");
        for(i=0; i<nArg; i++){
          fprintf(p->out,"<TH>%s</TH>",azCol[i]);
        }
        fprintf(p->out,"</TR>\n");
      }
      for(i=0; i<nArg; i++){
        fprintf(p->out,"<TR>");
        for(i=0; i<nArg; i++){
          fprintf(p->out,"<TD>%s</TD>",azArg[i]);
        }
        fprintf(p->out,"</TD>\n");
      }
      break;
    }
  }      
  return 0;







|
>










|
>














|







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
      for(i=0; i<nArg; i++){
        int w;
        if( i<ArraySize(p->colWidth) && p->colWidth[i]>0 ){
           w = p->colWidth[i];
        }else{
           w = 10;
        }
        fprintf(p->out,"%-*.*s%s",w,w,
            azArg[i] ? azArg[i] : "", i==nArg-1 ? "\n": "  ");
      }
      break;
    }
    case MODE_List: {
      if( p->cnt++==0 && p->showHeader ){
        for(i=0; i<nArg; i++){
          fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
        }
      }
      for(i=0; i<nArg; i++){
        fprintf(p->out,"%s%s",azArg[i] ? azArg[i] : "",
             i==nArg-1 ? "\n" : p->separator);
      }
      break;
    }
    case MODE_Html: {
      if( p->cnt++==0 && p->showHeader ){
        fprintf(p->out,"<TR>");
        for(i=0; i<nArg; i++){
          fprintf(p->out,"<TH>%s</TH>",azCol[i]);
        }
        fprintf(p->out,"</TR>\n");
      }
      for(i=0; i<nArg; i++){
        fprintf(p->out,"<TR>");
        for(i=0; i<nArg; i++){
          fprintf(p->out,"<TD>%s</TD>",azArg[i] ? azArg[i] : "");
        }
        fprintf(p->out,"</TD>\n");
      }
      break;
    }
  }      
  return 0;
Changes to src/sqliteInt.h.
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
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.11 2000/06/03 18:06:53 drh Exp $
*/
#include "sqlite.h"
#include "dbbe.h"
#include "vdbe.h"
#include "parse.h"
#include <gdbm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* #define MEMORY_DEBUG 2 */
#ifdef MEMORY_DEBUG
# define sqliteMalloc(X)    sqliteMalloc_(X,__FILE__,__LINE__)
# define sqliteFree(X)      sqliteFree_(X,__FILE__,__LINE__)
# define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
  void sqliteStrRealloc(char**);
#else
# define sqliteStrRealloc(X)







|











|







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
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.12 2000/06/04 12:58:38 drh Exp $
*/
#include "sqlite.h"
#include "dbbe.h"
#include "vdbe.h"
#include "parse.h"
#include <gdbm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MEMORY_DEBUG 1
#ifdef MEMORY_DEBUG
# define sqliteMalloc(X)    sqliteMalloc_(X,__FILE__,__LINE__)
# define sqliteFree(X)      sqliteFree_(X,__FILE__,__LINE__)
# define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
  void sqliteStrRealloc(char**);
#else
# define sqliteStrRealloc(X)
Changes to src/tclsqlite.c.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** A TCL Interface to SQLite
**
** $Id: tclsqlite.c,v 1.4 2000/06/02 01:17:38 drh Exp $
*/
#include "sqlite.h"
#include <tcl.h>
#include <stdlib.h>
#include <string.h>

/*







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** A TCL Interface to SQLite
**
** $Id: tclsqlite.c,v 1.5 2000/06/04 12:58:38 drh Exp $
*/
#include "sqlite.h"
#include <tcl.h>
#include <stdlib.h>
#include <string.h>

/*
57
58
59
60
61
62
63


64
65
66
67


68
69
70
71
72
73
74
75
    if( cbData->once ){
      for(i=0; i<nCol; i++){
        Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
           TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
      }
    }
    for(i=0; i<nCol; i++){


      Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], azCol[i], 0);
    }
  }else{
    for(i=0; i<nCol; i++){


      Tcl_SetVar(cbData->interp, azN[i], azCol[i], 0);
    }
  }
  cbData->once = 0;
  rc = Tcl_Eval(cbData->interp, cbData->zCode);
  return rc;
}








>
>
|



>
>
|







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    if( cbData->once ){
      for(i=0; i<nCol; i++){
        Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
           TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
      }
    }
    for(i=0; i<nCol; i++){
      char *z = azCol[i];
      if( z==0 ) z = "";
      Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
    }
  }else{
    for(i=0; i<nCol; i++){
      char *z = azCol[i];
      if( z==0 ) z = "";
      Tcl_SetVar(cbData->interp, azN[i], z, 0);
    }
  }
  cbData->once = 0;
  rc = Tcl_Eval(cbData->interp, cbData->zCode);
  return rc;
}

279
280
281
282
283
284
285
286
287

288
289
290
291
292
293
294
295
296
    Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
    Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
    for(i=2; i<argc; i++){
      Tcl_SetVar(interp, "argv", argv[i],
          TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
    }
    if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
      fprintf(stderr,"%s: %s\n", *argv, 
         Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY)

      );
      return 1;
    }
  }else{
    Tcl_GlobalEval(interp, zMainloop);
  }
  return 0;
}
#endif /* TCLSH */







<
|
>
|








283
284
285
286
287
288
289

290
291
292
293
294
295
296
297
298
299
300
    Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
    Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
    for(i=2; i<argc; i++){
      Tcl_SetVar(interp, "argv", argv[i],
          TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
    }
    if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){

      char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
      if( zInfo==0 ) zInfo = interp->result;
      fprintf(stderr,"%s: %s\n", *argv, zInfo);
      return 1;
    }
  }else{
    Tcl_GlobalEval(interp, zMainloop);
  }
  return 0;
}
#endif /* TCLSH */
Changes to src/util.c.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.9 2000/06/02 13:28:00 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

#ifdef MEMORY_DEBUG








|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.10 2000/06/04 12:58:38 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

#ifdef MEMORY_DEBUG

628
629
630
631
632
633
634

635
636
637
638
639
640
641
          while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
            zString++;
          }
          return *zString!=0;
        }else{
          while( (c2 = *zString)!=0 ){
            while( c2 != 0 && c2 != c ){ c2 = *++zString; }

            if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
            zString++;
          }
          return 0;
        }
      case '?':
        if( *zString==0 ) return 0;







>







628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
          while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
            zString++;
          }
          return *zString!=0;
        }else{
          while( (c2 = *zString)!=0 ){
            while( c2 != 0 && c2 != c ){ c2 = *++zString; }
            if( c2==0 ) return 0;
            if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
            zString++;
          }
          return 0;
        }
      case '?':
        if( *zString==0 ) return 0;
695
696
697
698
699
700
701

702
703
704
705
706
707
708
          while( *zString && sqliteLikeCompare(&zPattern[1],zString)==0 ){
            zString++;
          }
          return *zString!=0;
        }else{
          while( (c2 = UpperToLower[*zString])!=0 ){
            while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }

            if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
            zString++;
          }
          return 0;
        }
      case '_':
        if( *zString==0 ) return 0;







>







696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
          while( *zString && sqliteLikeCompare(&zPattern[1],zString)==0 ){
            zString++;
          }
          return *zString!=0;
        }else{
          while( (c2 = UpperToLower[*zString])!=0 ){
            while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
            if( c2==0 ) return 0;
            if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
            zString++;
          }
          return 0;
        }
      case '_':
        if( *zString==0 ) return 0;
Changes to src/vdbe.c.
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
** inplicit conversion from one type to the other occurs as necessary.
** 
** Most of the code in this file is taken up by the sqliteVdbeExec()
** function which does the work of interpreting a VDBE program.
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.12 2000/06/02 23:22:40 drh Exp $
*/
#include "sqliteInt.h"

/*
** SQL is translated into a sequence of instructions to be
** executed by a virtual machine.  Each instruction is an instance
** of the following structure.







|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
** inplicit conversion from one type to the other occurs as necessary.
** 
** Most of the code in this file is taken up by the sqliteVdbeExec()
** function which does the work of interpreting a VDBE program.
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.13 2000/06/04 12:58:38 drh Exp $
*/
#include "sqliteInt.h"

/*
** SQL is translated into a sequence of instructions to be
** executed by a virtual machine.  Each instruction is an instance
** of the following structure.
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
};

/* 
** Number of buckets used for merge-sort.  
*/
#define NSORT 30

























/*
** An instance of the virtual machine
*/
struct Vdbe {
  Dbbe *pBe;          /* Opaque context structure used by DB backend */
  FILE *trace;        /* Write an execution trace here, if not NULL */
  int nOp;            /* Number of instructions in the program */
  int nOpAlloc;       /* Number of slots allocated for aOp[] */
  Op *aOp;            /* Space to hold the virtual machine's program */
  int nLabel;         /* Number of labels used */
  int nLabelAlloc;    /* Number of slots allocated in aLabel[] */
  int *aLabel;        /* Space to hold the labels */
  int tos;            /* Index of top of stack */
  int nStackAlloc;    /* Size of the stack */
  int *iStack;        /* Integer values of the stack */
  char **zStack;      /* Text or binary values of the stack */
  char **azColName;   /* Becomes the 4th parameter to callbacks */
  int nTable;         /* Number of slots in aTab[] */
  VdbeTable *aTab;    /* On element of this array for each open table */
  int nList;          /* Number of slots in apList[] */
  FILE **apList;      /* An open file for each list */
  int nSort;          /* Number of slots in apSort[] */







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>














|







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
};

/* 
** Number of buckets used for merge-sort.  
*/
#define NSORT 30

/*
** A single level of the stack is an instance of the following
** structure.  Except, string values are stored on a separate
** list of of pointers to character.  The reason for storing
** strings separately is so that they can be easily passed
** to the callback function.
*/
struct Stack {
  int i;         /* Integer value */
  int n;         /* Number of characters in string value, including '\0' */
  int flags;     /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */
  double r;      /* Real value */      
};
typedef struct Stack Stack;

/*
** Allowed values for Stack.flags
*/
#define STK_Null      0x0001   /* Value is NULL */
#define STK_Str       0x0002   /* Value is a string */
#define STK_Int       0x0004   /* Value is an integer */
#define STK_Real      0x0008   /* Value is a real number */
#define STK_Dyn       0x0010   /* Need to call sqliteFree() on zStack[*] */

/*
** An instance of the virtual machine
*/
struct Vdbe {
  Dbbe *pBe;          /* Opaque context structure used by DB backend */
  FILE *trace;        /* Write an execution trace here, if not NULL */
  int nOp;            /* Number of instructions in the program */
  int nOpAlloc;       /* Number of slots allocated for aOp[] */
  Op *aOp;            /* Space to hold the virtual machine's program */
  int nLabel;         /* Number of labels used */
  int nLabelAlloc;    /* Number of slots allocated in aLabel[] */
  int *aLabel;        /* Space to hold the labels */
  int tos;            /* Index of top of stack */
  int nStackAlloc;    /* Size of the stack */
  Stack *aStack;      /* The operand stack, except string values */
  char **zStack;      /* Text or binary values of the stack */
  char **azColName;   /* Becomes the 4th parameter to callbacks */
  int nTable;         /* Number of slots in aTab[] */
  VdbeTable *aTab;    /* On element of this array for each open table */
  int nList;          /* Number of slots in apList[] */
  FILE **apList;      /* An open file for each list */
  int nSort;          /* Number of slots in apSort[] */
285
286
287
288
289
290
291
292
293
















































































294
295
296
297
298
299
300

301


302
303
304

































305
306
307
308
309
310
311
    p->nLabel = 0;
    p->nLabelAlloc = 0;
    return 0;
  }
  p->aLabel[i] = -1;
  return -1-i;
}

/*
















































































** Pop the stack N times.  Free any memory associated with the
** popped stack elements.
*/
static void PopStack(Vdbe *p, int N){
  if( p->zStack==0 ) return;
  while( p->tos>=0 && N-->0 ){
    int i = p->tos--;

    sqliteFree(p->zStack[i]);


    p->zStack[i] = 0;
  }    
}


































/*
** Clean up the VM after execution.
**
** This routine will automatically close any tables, list, and/or
** sorters that were left open.
*/









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
|
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    p->nLabel = 0;
    p->nLabelAlloc = 0;
    return 0;
  }
  p->aLabel[i] = -1;
  return -1-i;
}

/*
** Convert the given stack entity into a string if it isn't one
** already.  Return non-zero if we run out of memory.
**
** NULLs are converted into an empty string.
*/
#define Stringify(P,I) \
   ((P->aStack[I].flags & STK_Str)==0 ? hardStringify(P,I) : 0)
static int hardStringify(Vdbe *p, int i){
  char zBuf[30];
  int fg = p->aStack[i].flags;
  if( fg & STK_Real ){
    sprintf(zBuf,"%g",p->aStack[i].r);
  }else if( fg & STK_Int ){
    sprintf(zBuf,"%d",p->aStack[i].i);
  }else{
    p->zStack[i] = "";
    p->aStack[i].n = 1;
    p->aStack[i].flags |= STK_Str;
    return 0;
  }
  p->zStack[i] = 0;
  sqliteSetString(&p->zStack[i], zBuf, 0);
  if( p->zStack[i]==0 ) return 1;
  p->aStack[i].n = strlen(p->zStack[i])+1;
  p->aStack[i].flags |= STK_Str|STK_Dyn;
  return 0;
}

/*
** Release the memory associated with the given stack level
*/
#define Release(P,I)  if((P)->aStack[I].flags&STK_Dyn){ hardRelease(P,I); }
static void hardRelease(Vdbe *p, int i){
  sqliteFree(p->zStack[i]);
  p->zStack[i] = 0;
  p->aStack[i].flags &= ~(STK_Str|STK_Dyn);
}

/*
** Convert the given stack entity into a integer if it isn't one
** already.
**
** Any prior string or real representation is invalidated.  
** NULLs are converted into 0.
*/
#define Integerify(P,I) \
    if(((P)->aStack[(I)].flags&STK_Int)==0){ hardIntegerify(P,I); }
static void hardIntegerify(Vdbe *p, int i){
  if( p->aStack[i].flags & STK_Real ){
    p->aStack[i].i = p->aStack[i].r;
    Release(p, i);
  }else if( p->aStack[i].flags & STK_Str ){
    p->aStack[i].i = atoi(p->zStack[i]);
    Release(p, i);
  }else{
    p->aStack[i].i = 0;
  }
  p->aStack[i].flags = STK_Int;
}

/*
** Get a valid Real representation for the given stack element.
**
** Any prior string or integer representation is retained.
** NULLs are converted into 0.0.
*/
#define Realify(P,I) \
    if(((P)->aStack[(I)].flags&STK_Real)==0){ hardRealify(P,I); }
static void hardRealify(Vdbe *p, int i){
  if( p->aStack[i].flags & STK_Str ){
    p->aStack[i].r = atof(p->zStack[i]);
  }else if( p->aStack[i].flags & STK_Int ){
    p->aStack[i].r = p->aStack[i].i;
  }else{
    p->aStack[i].r = 0.0;
  }
  p->aStack[i].flags |= STK_Real;
}

/*
** Pop the stack N times.  Free any memory associated with the
** popped stack elements.
*/
static void PopStack(Vdbe *p, int N){
  if( p->zStack==0 ) return;
  while( p->tos>=0 && N-->0 ){
    int i = p->tos--;
    if( p->aStack[i].flags & STK_Dyn ){
      sqliteFree(p->zStack[i]);
    }
    p->aStack[i].flags = 0;
    p->zStack[i] = 0;
  }    
}

/*
** Make sure space has been allocated to hold at least N
** stack elements.  Allocate additional stack space if
** necessary.
**
** Return 0 on success and non-zero if there are memory
** allocation errors.
*/
#define NeedStack(P,N) (((P)->nStackAlloc<=(N)) ? hardNeedStack(P,N) : 0)
static int hardNeedStack(Vdbe *p, int N){
  int oldAlloc;
  int i;
  if( N>=p->nStackAlloc ){
    oldAlloc = p->nStackAlloc;
    p->nStackAlloc = N + 20;
    p->aStack = sqliteRealloc(p->aStack, p->nStackAlloc*sizeof(p->aStack[0]));
    p->zStack = sqliteRealloc(p->zStack, p->nStackAlloc*sizeof(char*));
    if( p->aStack==0 || p->zStack==0 ){
      sqliteFree(p->aStack);
      sqliteFree(p->zStack);
      p->aStack = 0;
      p->zStack = 0;
      p->nStackAlloc = 0;
      return 1;
    }
    for(i=oldAlloc; i<p->nStackAlloc; i++){
      p->zStack[i] = 0;
      p->aStack[i].flags = 0;
    }
  }
  return 0;
}

/*
** Clean up the VM after execution.
**
** This routine will automatically close any tables, list, and/or
** sorters that were left open.
*/
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    p->nOp = 0;
  }
  for(i=0; i<p->nOp; i++){
    sqliteFree(p->aOp[i].p3);
  }
  sqliteFree(p->aOp);
  sqliteFree(p->aLabel);
  sqliteFree(p->iStack);
  sqliteFree(p->zStack);
  sqliteFree(p);
}

/*
** A translation from opcode numbers to opcode names.  Used for testing
** and debugging only.







|







512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    p->nOp = 0;
  }
  for(i=0; i<p->nOp; i++){
    sqliteFree(p->aOp[i].p3);
  }
  sqliteFree(p->aOp);
  sqliteFree(p->aLabel);
  sqliteFree(p->aStack);
  sqliteFree(p->zStack);
  sqliteFree(p);
}

/*
** A translation from opcode numbers to opcode names.  Used for testing
** and debugging only.
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  "DeleteIdx",      "ListOpen",       "ListWrite",      "ListRewind",
  "ListRead",       "ListClose",      "SortOpen",       "SortPut",
  "SortMakeRec",    "SortMakeKey",    "Sort",           "SortNext",
  "SortKey",        "SortCallback",   "SortClose",      "FileOpen",
  "FileRead",       "FileField",      "FileClose",      "MakeRecord",
  "MakeKey",        "Goto",           "If",             "Halt",
  "ColumnCount",    "ColumnName",     "Callback",       "Integer",
  "String",         "Pop",            "Dup",            "Pull",
  "Add",            "AddImm",         "Subtract",       "Multiply",
  "Divide",         "Min",            "Max",            "Like",
  "Glob",           "Eq",             "Ne",             "Lt",
  "Le",             "Gt",             "Ge",             "IsNull",
  "NotNull",        "Negative",       "And",            "Or",
  "Not",            "Concat",         "Noop",         
};

/*
** Given the name of an opcode, return its number.  Return 0 if
** there is no match.
**
** This routine is used for testing and debugging.







|
|
|
|
|
|
|







538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  "DeleteIdx",      "ListOpen",       "ListWrite",      "ListRewind",
  "ListRead",       "ListClose",      "SortOpen",       "SortPut",
  "SortMakeRec",    "SortMakeKey",    "Sort",           "SortNext",
  "SortKey",        "SortCallback",   "SortClose",      "FileOpen",
  "FileRead",       "FileField",      "FileClose",      "MakeRecord",
  "MakeKey",        "Goto",           "If",             "Halt",
  "ColumnCount",    "ColumnName",     "Callback",       "Integer",
  "String",         "Null",           "Pop",            "Dup",
  "Pull",           "Add",            "AddImm",         "Subtract",
  "Multiply",       "Divide",         "Min",            "Max",
  "Like",           "Glob",           "Eq",             "Ne",
  "Lt",             "Le",             "Gt",             "Ge",
  "IsNull",         "NotNull",        "Negative",       "And",
  "Or",             "Not",            "Concat",         "Noop",
};

/*
** Given the name of an opcode, return its number.  Return 0 if
** there is no match.
**
** This routine is used for testing and debugging.
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  rc = SQLITE_OK;
  if( pzErrMsg ){ *pzErrMsg = 0; }
  for(i=0; rc==SQLITE_OK && i<p->nOp; i++){
    sprintf(zAddr,"%d",i);
    sprintf(zP1,"%d", p->aOp[i].p1);
    sprintf(zP2,"%d", p->aOp[i].p2);
    azField[4] = p->aOp[i].p3;
    if( azField[4]==0 ) azField[4] = "";
    azField[1] = zOpName[p->aOp[i].opcode];
    if( xCallback(pArg, 5, azField, azColumnNames) ){
      rc = SQLITE_ABORT;
    }
  }
  return rc;
}

/*
** Make sure space has been allocated to hold at least N
** stack elements.  Allocate additional stack space if
** necessary.
**
** Return 0 on success and non-zero if there are memory
** allocation errors.
*/
static int NeedStack(Vdbe *p, int N){
  int oldAlloc;
  int i;
  if( N>=p->nStackAlloc ){
    oldAlloc = p->nStackAlloc;
    p->nStackAlloc = N + 20;
    p->iStack = sqliteRealloc(p->iStack, p->nStackAlloc*sizeof(int));
    p->zStack = sqliteRealloc(p->zStack, p->nStackAlloc*sizeof(char*));
    if( p->iStack==0 || p->zStack==0 ){
      sqliteFree(p->iStack);
      sqliteFree(p->zStack);
      p->iStack = 0;
      p->zStack = 0;
      p->nStackAlloc = 0;
      return 1;
    }
    for(i=oldAlloc; i<p->nStackAlloc; i++){
      p->zStack[i] = 0;
    }
  }
  return 0;
}

/*
** Convert the given stack entity into a string if it isn't one
** already.  Return non-zero if we run out of memory.
*/
static int Stringify(Vdbe *p, int i){
  if( p->zStack[i]==0 ){
    char zBuf[30];
    sprintf(zBuf,"%d",p->iStack[i]);
    sqliteSetString(&p->zStack[i], zBuf, 0);
    if( p->zStack[i]==0 ) return 1;
    p->iStack[i] = strlen(p->zStack[i])+1;
  }
  return 0;
}

/*
** Convert the given stack entity into a integer if it isn't one
** already.
*/
static int Integerify(Vdbe *p, int i){
  if( p->zStack[i]!=0 ){
    p->iStack[i] = atoi(p->zStack[i]);
    sqliteFree(p->zStack[i]);
    p->zStack[i] = 0;
  }
  return p->iStack[i];
}

/*
** The parameters are pointers to the head of two sorted lists
** of Sorter structures.  Merge these two lists together and return
** a single sorted list.  This routine forms the core of the merge-sort
** algorithm.
**
** In the case of a tie, left sorts in front of right.







<








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







595
596
597
598
599
600
601

602
603
604
605
606
607
608
609



























































610
611
612
613
614
615
616
  rc = SQLITE_OK;
  if( pzErrMsg ){ *pzErrMsg = 0; }
  for(i=0; rc==SQLITE_OK && i<p->nOp; i++){
    sprintf(zAddr,"%d",i);
    sprintf(zP1,"%d", p->aOp[i].p1);
    sprintf(zP2,"%d", p->aOp[i].p2);
    azField[4] = p->aOp[i].p3;

    azField[1] = zOpName[p->aOp[i].opcode];
    if( xCallback(pArg, 5, azField, azColumnNames) ){
      rc = SQLITE_ABORT;
    }
  }
  return rc;
}




























































/*
** The parameters are pointers to the head of two sorted lists
** of Sorter structures.  Merge these two lists together and return
** a single sorted list.  This routine forms the core of the merge-sort
** algorithm.
**
** In the case of a tie, left sorts in front of right.
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
      /* Opcode: Integer P1 * *
      **
      ** The integer value P1 is pushed onto the stack.
      */
      case OP_Integer: {
        int i = ++p->tos;
        if( NeedStack(p, p->tos) ) goto no_mem;
        p->iStack[i] = pOp->p1;
        p->zStack[i] = 0;
        break;
      }

      /* Opcode: String * * P3
      **
      ** The string value P3 is pushed onto the stack.
      */
      case OP_String: {
        int i = ++p->tos;
        char *z;
        if( NeedStack(p, p->tos) ) goto no_mem;
        z = pOp->p3;
        if( z==0 ) z = "";

        p->iStack[i] = strlen(z) + 1;












        sqliteSetString(&p->zStack[i], z, 0);

        break;
      }

      /* Opcode: Pop P1 * *
      **
      ** P1 elements are popped off of the top of stack and discarded.
      */







|
|













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







705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
      /* Opcode: Integer P1 * *
      **
      ** The integer value P1 is pushed onto the stack.
      */
      case OP_Integer: {
        int i = ++p->tos;
        if( NeedStack(p, p->tos) ) goto no_mem;
        p->aStack[i].i = pOp->p1;
        p->aStack[i].flags = STK_Int;
        break;
      }

      /* Opcode: String * * P3
      **
      ** The string value P3 is pushed onto the stack.
      */
      case OP_String: {
        int i = ++p->tos;
        char *z;
        if( NeedStack(p, p->tos) ) goto no_mem;
        z = pOp->p3;
        if( z==0 ) z = "";
        p->zStack[i] = z;
        p->aStack[i].n = strlen(z) + 1;
        p->aStack[i].flags = STK_Str;
        break;
      }

      /* Opcode: NULL * * *
      **
      ** Push a NULL value onto the stack.
      */
      case OP_Null: {
        int i = ++p->tos;
        char *z;
        if( NeedStack(p, p->tos) ) goto no_mem;
        p->zStack[i] = 0;
        p->aStack[i].flags = STK_Null;
        break;
      }

      /* Opcode: Pop P1 * *
      **
      ** P1 elements are popped off of the top of stack and discarded.
      */
667
668
669
670
671
672
673
674
675
676

677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
      ** top of the stack.
      */
      case OP_Dup: {
        int i = p->tos - pOp->p1;
        int j = ++p->tos;
        if( i<0 ) goto not_enough_stack;
        if( NeedStack(p, p->tos) ) goto no_mem;
        p->iStack[j] = p->iStack[i];
        if( p->zStack[i] ){
          p->zStack[j] = sqliteMalloc( p->iStack[j] );

          if( p->zStack[j] ) memcpy(p->zStack[j], p->zStack[i], p->iStack[j]);
        }else{
          p->zStack[j] = 0;
        }
        break;
      }

      /* Opcode: Pull P1 * *
      **
      ** The P1-th element is removed its current location on 
      ** the stack and pushed back on top of the stack.  The
      ** top of the stack is element 0, so "Pull 0 0 0" is
      ** a no-op.
      */
      case OP_Pull: {
        int from = p->tos - pOp->p1;
        int to = p->tos;
        int i;
        int ti;
        char *tz;
        if( from<0 ) goto not_enough_stack;
        ti = p->iStack[from];
        tz = p->zStack[from];
        for(i=from; i<to; i++){
          p->iStack[i] = p->iStack[i+1];
          p->zStack[i] = p->zStack[i+1];
        }
        p->iStack[to] = ti;
        p->zStack[to] = tz;
        break;
      }

      /* Opcode: ColumnCount P1 * *
      **
      ** Specify the number of column values that will appear in the







|
|
|
>
|

|






|








|


|


|


|







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
      ** top of the stack.
      */
      case OP_Dup: {
        int i = p->tos - pOp->p1;
        int j = ++p->tos;
        if( i<0 ) goto not_enough_stack;
        if( NeedStack(p, p->tos) ) goto no_mem;
        p->aStack[j] = p->aStack[i];
        if( p->aStack[i].flags & STK_Dyn ){
          p->zStack[j] = sqliteMalloc( p->aStack[j].n );
          if( p->zStack[j]==0 ) goto no_mem;
          memcpy(p->zStack[j], p->zStack[i], p->aStack[j].n);
        }else{
          p->zStack[j] = p->zStack[i];
        }
        break;
      }

      /* Opcode: Pull P1 * *
      **
      ** The P1-th element is removed from its current location on 
      ** the stack and pushed back on top of the stack.  The
      ** top of the stack is element 0, so "Pull 0 0 0" is
      ** a no-op.
      */
      case OP_Pull: {
        int from = p->tos - pOp->p1;
        int to = p->tos;
        int i;
        Stack ts;
        char *tz;
        if( from<0 ) goto not_enough_stack;
        ts = p->aStack[from];
        tz = p->zStack[from];
        for(i=from; i<to; i++){
          p->aStack[i] = p->aStack[i+1];
          p->zStack[i] = p->zStack[i+1];
        }
        p->aStack[to] = ts;
        p->zStack[to] = tz;
        break;
      }

      /* Opcode: ColumnCount P1 * *
      **
      ** Specify the number of column values that will appear in the
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
      */
      case OP_Callback: {
        int i = p->tos - pOp->p1 + 1;
        int j;
        if( i<0 ) goto not_enough_stack;
        if( NeedStack(p, p->tos+2) ) goto no_mem;
        for(j=i; j<=p->tos; j++){

          if( Stringify(p, j) ) goto no_mem;

        }
        p->zStack[p->tos+1] = 0;
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, &p->zStack[i], p->azColName)!=0 ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, pOp->p1);
        break;
      }

      /* Opcode: Concat * * *
      **
      ** Pop two elements from the stack.  Append the first (what used

      ** to be the top of stack) to the second (the next on stack) to 
      ** form a new string.  Push the new string back onto the stack.




      */
      case OP_Concat: {

        int tos = p->tos;
        int nos = tos - 1;

        char *z;






        if( nos<0 ) goto not_enough_stack;

        Stringify(p, tos);



        Stringify(p, nos);





        z = 0;


        sqliteSetString(&z, p->zStack[nos], p->zStack[tos], 0);









        PopStack(p, 1);

        sqliteFree(p->zStack[nos]);

        p->zStack[nos] = z;
        p->iStack[nos] = strlen(p->zStack[nos])+1;
        break;
      }

      /* Opcode: Add * * *
      **
      ** Pop the top two elements from the stack, add them together,
      ** and push the result back onto the stack.  If either element







>
|
>











|

|
>
|
|
>
>
>
>


>
|
|
>
|
>
>
>
>
>
>
|
>
|
>
>
>
|
>
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
|
>
|
>
|
<







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
      */
      case OP_Callback: {
        int i = p->tos - pOp->p1 + 1;
        int j;
        if( i<0 ) goto not_enough_stack;
        if( NeedStack(p, p->tos+2) ) goto no_mem;
        for(j=i; j<=p->tos; j++){
          if( (p->aStack[j].flags & STK_Null)==0 ){
            if( Stringify(p, j) ) goto no_mem;
          }
        }
        p->zStack[p->tos+1] = 0;
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, &p->zStack[i], p->azColName)!=0 ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, pOp->p1);
        break;
      }

      /* Opcode: Concat P1 P2 P3
      **
      ** Look at the first P1 elements of the stack.  Append them all 
      ** together with the lowest element first.  Use P3 as a separator.  
      ** Put the result on the top of the stack.  The original P1 elements
      ** are popped from the stack if P2==0 and retained if P2==1.
      **
      ** If P3 is NULL, then use no separator.  When P1==1, this routine
      ** makes a copy of the top stack element into memory obtained
      ** from sqliteMalloc().
      */
      case OP_Concat: {
        char *zNew;
        int nByte;
        int nField;
        int i, j;
        char *zSep;
        int nSep;

        nField = pOp->p1;
        zSep = pOp->p3;
        if( zSep==0 ) zSep = "";
        nSep = strlen(zSep);
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 1 - nSep;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            nByte += nSep;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n - 1 + nSep;
          }
        }
        zNew = sqliteMalloc( nByte );
        if( zNew==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNew[j], p->zStack[i], p->aStack[i].n-1);
            j += p->aStack[i].n-1;
          }
          if( nSep>0 && i<p->tos ){
            memcpy(&zNew[j], zSep, nSep);
            j += nSep;
          }
        }
        zNew[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNew;

        break;
      }

      /* Opcode: Add * * *
      **
      ** Pop the top two elements from the stack, add them together,
      ** and push the result back onto the stack.  If either element
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
      case OP_Add:
      case OP_Subtract:
      case OP_Multiply:
      case OP_Divide: {
        int tos = p->tos;
        int nos = tos - 1;
        if( nos<0 ) goto not_enough_stack;
        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
          int a, b;
          a = p->iStack[tos];
          b = p->iStack[nos];
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0 ){ 
                sqliteSetString(pzErrMsg, "division by zero", 0);
                rc = SQLITE_ERROR;
                goto cleanup;
              }
              b /= a;
              break;
            }
          }
          PopStack(p, 1);

          p->iStack[nos] = b;

        }else{
          double a, b;
          Stringify(p, tos);
          Stringify(p, nos);
          a = atof(p->zStack[tos]);
          b = atof(p->zStack[nos]);
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0.0 ){ 
                sqliteSetString(pzErrMsg, "division by zero", 0);
                rc = SQLITE_ERROR;
                goto cleanup;
              }
              b /= a;
              break;
            }
          }
          sprintf(zBuf,"%g",b);
          PopStack(p, 1);
          sqliteSetString(&p->zStack[nos], zBuf, 0);
          if( p->zStack[nos]==0 ) goto no_mem;
          p->iStack[nos] = strlen(p->zStack[nos]) + 1;
        }
        break;
      }

      /* Opcode: Max * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** largest of the two.
      */
      case OP_Max: {
        int tos = p->tos;
        int nos = tos - 1;


        if( nos<0 ) goto not_enough_stack;
        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
          if( p->iStack[nos]<p->iStack[tos] ){



            p->iStack[nos] = p->iStack[tos];
          }




        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          if( sqliteCompare(p->zStack[nos], p->zStack[tos])<0 ){



            sqliteFree(p->zStack[nos]);
            p->zStack[nos] = p->zStack[tos];
            p->iStack[nos] = p->iStack[tos];
            p->zStack[tos] = 0;

          }

        }
        p->tos--;
        break;
      }

      /* Opcode: Min * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** smaller of the two. 
      **
      ** If P1==1, always choose TOS for the min and decrement P1.
      ** This is self-altering code...
      */
      case OP_Min: {
        int tos = p->tos;
        int nos = tos - 1;


        if( nos<0 ) goto not_enough_stack;
        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
          if( p->iStack[nos]>p->iStack[tos] ){






            p->iStack[nos] = p->iStack[tos];
          }




        }else{
          Stringify(p, tos);
          Stringify(p, nos);


          if( pOp->p1==1 ){

            sqliteFree(p->zStack[nos]);
            p->zStack[nos] = p->zStack[tos];
            p->iStack[nos] = p->iStack[tos];
            p->zStack[tos] = 0;
            pOp->p1 = 0;
          }else if( sqliteCompare(p->zStack[nos], p->zStack[tos])>0 ){
            sqliteFree(p->zStack[nos]);
            p->zStack[nos] = p->zStack[tos];
            p->iStack[nos] = p->iStack[tos];
            p->zStack[tos] = 0;
          }

        }
        p->tos--;
        break;
      }

      /* Opcode: AddImm  P1 * *
      ** 
      ** Add the value P1 to whatever is on top of the stack.
      */
      case OP_AddImm: {
        int tos = p->tos;
        if( tos<0 ) goto not_enough_stack;
        Integerify(p, tos);
        p->iStack[tos] += pOp->p1;
        break;
      }

      /* Opcode: Eq * P2 *
      **
      ** Pop the top two elements from the stack.  If they are equal, then
      ** jump to instruction P2.  Otherwise, continue to the next instruction.







|

|
|














|
>
|
>


|
|
|
|














<

|
|
|












>
>

|
|
>
>
>
|
<
>
>
>
>



|
>
>
>
|
|
<
|
>
|
>

|














>
>

|
|
>
>
>
>
>
>
|
<
>
>
>
>



>
>
|
>
|
|
<
|
<
<
<
<
<
|
|
>

|











|







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
      case OP_Add:
      case OP_Subtract:
      case OP_Multiply:
      case OP_Divide: {
        int tos = p->tos;
        int nos = tos - 1;
        if( nos<0 ) goto not_enough_stack;
        if( (p->aStack[tos].flags & p->aStack[nos].flags & STK_Int)==STK_Int ){
          int a, b;
          a = p->aStack[tos].i;
          b = p->aStack[nos].i;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0 ){ 
                sqliteSetString(pzErrMsg, "division by zero", 0);
                rc = SQLITE_ERROR;
                goto cleanup;
              }
              b /= a;
              break;
            }
          }
          PopStack(p, 2);
          p->tos = nos;
          p->aStack[nos].i = b;
          p->aStack[nos].flags = STK_Int;
        }else{
          double a, b;
          Realify(p, tos);
          Realify(p, nos);
          a = p->aStack[tos].r;
          b = p->aStack[nos].r;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0.0 ){ 
                sqliteSetString(pzErrMsg, "division by zero", 0);
                rc = SQLITE_ERROR;
                goto cleanup;
              }
              b /= a;
              break;
            }
          }

          PopStack(p, 1);
          Release(p, nos);
          p->aStack[nos].r = b;
          p->aStack[nos].flags = STK_Real;
        }
        break;
      }

      /* Opcode: Max * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** largest of the two.
      */
      case OP_Max: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        if( nos<0 ) goto not_enough_stack;
        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;
        if( fn & STK_Null ){
          copy = 1;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = p->aStack[nos].i<p->aStack[tos].i;

        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = p->aStack[tos].r>p->aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])>0;
        }
        if( copy ){
          Release(p, nos);
          p->aStack[nos] = p->aStack[tos];
          p->zStack[nos] = p->zStack[tos];

          p->zStack[tos] = 0;
          p->aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

      /* Opcode: Min * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** smaller of the two. 
      **
      ** If P1==1, always choose TOS for the min and decrement P1.
      ** This is self-altering code...
      */
      case OP_Min: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        if( nos<0 ) goto not_enough_stack;
        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;
        if( pOp->p1 ){
          copy = 1;
          pOp->p1 = 0;
        }else if( fn & STK_Null ){
          copy = 1;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = p->aStack[nos].i>p->aStack[tos].i;

        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = p->aStack[tos].r<p->aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])<0;
        }
        if( copy ){
          Release(p, nos);
          p->aStack[nos] = p->aStack[tos];
          p->zStack[nos] = p->zStack[tos];

          p->zStack[tos] = 0;





          p->aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

      /* Opcode: AddImm  P1 * *
      ** 
      ** Add the value P1 to whatever is on top of the stack.
      */
      case OP_AddImm: {
        int tos = p->tos;
        if( tos<0 ) goto not_enough_stack;
        Integerify(p, tos);
        p->aStack[tos].i += pOp->p1;
        break;
      }

      /* Opcode: Eq * P2 *
      **
      ** Pop the top two elements from the stack.  If they are equal, then
      ** jump to instruction P2.  Otherwise, continue to the next instruction.
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
      case OP_Lt:
      case OP_Le:
      case OP_Gt:
      case OP_Ge: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;

        if( nos<0 ) goto not_enough_stack;
        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
          int a, b;
          a = p->iStack[tos];
          b = p->iStack[nos];
          switch( pOp->opcode ){

            case OP_Eq:    c = b==a;     break;
            case OP_Ne:    c = b!=a;     break;
            case OP_Lt:    c = b<a;      break;
            case OP_Le:    c = b<=a;     break;
            case OP_Gt:    c = b>a;      break;
            default:       c = b>=a;     break;
          }
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          c = sqliteCompare(p->zStack[nos], p->zStack[tos]);

          switch( pOp->opcode ){
            case OP_Eq:    c = c==0;     break;
            case OP_Ne:    c = c!=0;     break;
            case OP_Lt:    c = c<0;      break;
            case OP_Le:    c = c<=0;     break;
            case OP_Gt:    c = c>0;      break;
            default:       c = c>=0;     break;
          }
        }
        PopStack(p, 2);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: Like P1 P2 *







>

<
<
|
|
<
>
|
<
<
<
<
<
<




>
|
|
|
|
|
|
|
<







1135
1136
1137
1138
1139
1140
1141
1142
1143


1144
1145

1146
1147






1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159

1160
1161
1162
1163
1164
1165
1166
      case OP_Lt:
      case OP_Le:
      case OP_Gt:
      case OP_Ge: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        int ft, fn;
        if( nos<0 ) goto not_enough_stack;


        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;

        if( (ft & fn)==STK_Int ){
          c = p->aStack[nos].i - p->aStack[tos].i;






        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          c = sqliteCompare(p->zStack[nos], p->zStack[tos]);
        }
        switch( pOp->opcode ){
          case OP_Eq:    c = c==0;     break;
          case OP_Ne:    c = c!=0;     break;
          case OP_Lt:    c = c<0;      break;
          case OP_Le:    c = c<=0;     break;
          case OP_Gt:    c = c>0;      break;
          default:       c = c>=0;     break;

        }
        PopStack(p, 2);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: Like P1 P2 *
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
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147

1148
1149
1150
1151
1152
1153
1154
1155
        break;
      }

      /* Opcode: And * * *
      **
      ** Pop two values off the stack.  Take the logical AND of the
      ** two values and push the resulting boolean value back onto the
      ** stack.  Integers are considered false if zero and true otherwise.
      ** Strings are considered false if their length is zero and true
      ** otherwise.
      */
      /* Opcode: Or * * *
      **
      ** Pop two values off the stack.  Take the logical OR of the
      ** two values and push the resulting boolean value back onto the
      ** stack.  Integers are considered false if zero and true otherwise.
      ** Strings are considered false if their length is zero and true
      ** otherwise.
      */
      case OP_And:
      case OP_Or: {
        int tos = p->tos;
        int nos = tos - 1;
        int x, y, c;
        if( nos<0 ) goto not_enough_stack;
        x = p->zStack[nos] ? p->zStack[nos][0] : p->iStack[nos];
        y = p->zStack[tos] ? p->zStack[tos][0] : p->iStack[tos];
        if( pOp->opcode==OP_And ){
          c = x && y;
        }else{
          c = x || y;
        }
        PopStack(p, 2);
        p->tos++;
        p->iStack[nos] = c;

        break;
      }

      /* Opcode: Negative * * *
      **
      ** Treat the top of the stack as a numeric quantity.  Replace it
      ** with its additive inverse.  If the top of stack is a string,
      ** then it is converted into a number using atof().
      */
      case OP_Negative: {
        int tos;
        if( (tos = p->tos)<0 ) goto not_enough_stack;
        if( p->zStack[tos] ){


          double r = atof(p->zStack[tos]);

          sprintf(zBuf, "%g", -r);
          sqliteSetString(&p->zStack[tos], zBuf, 0);
          p->iStack[tos] = strlen(zBuf) + 1;
        }else{



          p->iStack[tos] = -p->iStack[tos];
        }
        break;
      }

      /* Opcode: Not * * *
      **
      ** Treat the top of the stack as a boolean value.  Replace it
      ** with its complement.  Integers are false if zero and true
      ** otherwise.  Strings are false if zero-length and true otherwise.
      */
      case OP_Not: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        c = p->zStack[p->tos] ? p->zStack[p->tos][0] : p->iStack[p->tos];
        PopStack(p, 1);
        p->tos++;

        p->iStack[p->tos] = !c;
        break;
      }

      /* Opcode: Noop * * *
      **
      ** Do nothing.  This instruction is often useful as a jump
      ** destination.







|
<
<





|
<
<





|

|
|

|

|



|
>






|
<




|
>
>
|
>
|
|
|

>
>
>
|







|
<


|

<
|
|
>
|







1224
1225
1226
1227
1228
1229
1230
1231


1232
1233
1234
1235
1236
1237


1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287

1288
1289
1290
1291

1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
        break;
      }

      /* Opcode: And * * *
      **
      ** Pop two values off the stack.  Take the logical AND of the
      ** two values and push the resulting boolean value back onto the
      ** stack. 


      */
      /* Opcode: Or * * *
      **
      ** Pop two values off the stack.  Take the logical OR of the
      ** two values and push the resulting boolean value back onto the
      ** stack. 


      */
      case OP_And:
      case OP_Or: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        if( nos<0 ) goto not_enough_stack;
        Integerify(p, tos);
        Integerify(p, nos);
        if( pOp->opcode==OP_And ){
          c = p->aStack[tos].i && p->aStack[nos].i;
        }else{
          c = p->aStack[tos].i || p->aStack[nos].i;
        }
        PopStack(p, 2);
        p->tos++;
        p->aStack[nos].i = c;
        p->aStack[nos].flags = STK_Int;
        break;
      }

      /* Opcode: Negative * * *
      **
      ** Treat the top of the stack as a numeric quantity.  Replace it
      ** with its additive inverse.

      */
      case OP_Negative: {
        int tos;
        if( (tos = p->tos)<0 ) goto not_enough_stack;
        if( p->aStack[tos].flags & STK_Real ){
          Release(p, tos);
          p->aStack[tos].r = -p->aStack[tos].r;
          p->aStack[tos].flags = STK_Real;
        }else if( p->aStack[tos].flags & STK_Int ){
          Release(p, tos);
          p->aStack[tos].i = -p->aStack[tos].i;
          p->aStack[tos].flags = STK_Int;
        }else{
          Realify(p, tos);
          Release(p, tos);
          p->aStack[tos].r = -p->aStack[tos].r;
          p->aStack[tos].flags = STK_Real;
        }
        break;
      }

      /* Opcode: Not * * *
      **
      ** Treat the top of the stack as a boolean value.  Replace it
      ** with its complement.

      */
      case OP_Not: {
        int tos = p->tos;
        if( p->tos<0 ) goto not_enough_stack;

        Integerify(p, tos);
        Release(p, tos);
        p->aStack[tos].i = !p->aStack[tos].i;
        p->aStack[tos].flags = STK_Int;
        break;
      }

      /* Opcode: Noop * * *
      **
      ** Do nothing.  This instruction is often useful as a jump
      ** destination.
1164
1165
1166
1167
1168
1169
1170

1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218

1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230

1231
1232

1233
1234
1235
1236
1237
1238
1239




1240
1241
1242
1243
1244
1245

1246

1247
1248

1249
1250
1251
1252
1253

1254
1255
1256
1257
1258
1259
1260
      ** true, then jump to p2.  Otherwise continue to the next instruction.
      ** An integer is false if zero and true otherwise.  A string is
      ** false if it has zero length and true otherwise.
      */
      case OP_If: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;

        c = p->zStack[p->tos] ? p->zStack[p->tos][0] : p->iStack[p->tos];
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: IsNull * P2 *
      **
      ** Pop a single value from the stack.  If the value popped is the
      ** empty string, then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_IsNull: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        c = p->zStack[p->tos]!=0 && p->zStack[p->tos][0]==0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: NotNull * P2 *
      **
      ** Pop a single value from the stack.  If the value popped is not an
      ** empty string, then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_NotNull: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        c = p->zStack[p->tos]==0 || p->zStack[p->tos][0]!=0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: MakeRecord P1 * *
      **
      ** Convert the top P1 entries of the stack into a single entry
      ** suitable for use as a data record in the database.  To do this
      ** each entry is converted to a string and all the strings are
      ** concatenated.  The null-terminators are preserved by the concatation
      ** and serve as a boundry marker between fields.  The lowest entry
      ** on the stack is the first in the concatenation and the top of
      ** the stack is the last.  After all fields are concatenated, an
      ** index header is added.  The index header consists of P1 integers
      ** which hold the offset of the beginning of each field from the
      ** beginning of the completed record including the header.

      */
      case OP_MakeRecord: {
        char *zNewRecord;
        int nByte;
        int nField;
        int i, j;
        int addr;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){

          if( Stringify(p, i) ) goto no_mem;
          nByte += p->iStack[i];

        }
        nByte += sizeof(int)*nField;
        zNewRecord = sqliteMalloc( nByte );
        if( zNewRecord==0 ) goto no_mem;
        j = 0;
        addr = sizeof(int)*nField;
        for(i=p->tos-nField+1; i<p->tos; i++){




          memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
          addr += p->iStack[i];
          j += sizeof(int);
        }
        memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
        j += sizeof(int);

        for(i=p->tos-nField+1; i<=p->tos; i++){

          memcpy(&zNewRecord[j], p->zStack[i], p->iStack[i]);
          j += p->iStack[i];

        }
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->iStack[p->tos] = nByte;

        p->zStack[p->tos] = zNewRecord;
        break;
      }

      /* Opcode: MakeKey P1 P2 *
      **
      ** Convert the top P1 entries of the stack into a single entry suitable







>
|







|
|





|














|









|






|
>












>
|
|
>






|
>
>
>
>
|
|
<
|
<
|
>

>
|
|
>




|
>







1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396

1397

1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
      ** true, then jump to p2.  Otherwise continue to the next instruction.
      ** An integer is false if zero and true otherwise.  A string is
      ** false if it has zero length and true otherwise.
      */
      case OP_If: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        Integerify(p, p->tos);
        c = p->aStack[p->tos].i;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: IsNull * P2 *
      **
      ** Pop a single value from the stack.  If the value popped is NULL
      ** then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_IsNull: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        c = (p->aStack[p->tos].flags & STK_Null)!=0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: NotNull * P2 *
      **
      ** Pop a single value from the stack.  If the value popped is not an
      ** empty string, then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_NotNull: {
        int c;
        if( p->tos<0 ) goto not_enough_stack;
        c = (p->aStack[p->tos].flags & STK_Null)==0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: MakeRecord P1 * *
      **
      ** Convert the top P1 entries of the stack into a single entry
      ** suitable for use as a data record in the database.  To do this
      ** all entries (except NULLs) are converted to strings and 
      ** concatenated.  The null-terminators are preserved by the concatation
      ** and serve as a boundry marker between fields.  The lowest entry
      ** on the stack is the first in the concatenation and the top of
      ** the stack is the last.  After all fields are concatenated, an
      ** index header is added.  The index header consists of P1 integers
      ** which hold the offset of the beginning of each field from the
      ** beginning of the completed record including the header.  The
      ** index for NULL entries is 0.
      */
      case OP_MakeRecord: {
        char *zNewRecord;
        int nByte;
        int nField;
        int i, j;
        int addr;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        nByte += sizeof(int)*nField;
        zNewRecord = sqliteMalloc( nByte );
        if( zNewRecord==0 ) goto no_mem;
        j = 0;
        addr = sizeof(int)*nField;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            int zero = 0;
            memcpy(&zNewRecord[j], (char*)&zero, sizeof(int));
          }else{
            memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
            addr += p->aStack[i].n;

          }

          j += sizeof(int);
        }
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewRecord[j], p->zStack[i], p->aStack[i].n);
            j += p->aStack[i].n;
          }
        }
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str | STK_Dyn;
        p->zStack[p->tos] = zNewRecord;
        break;
      }

      /* Opcode: MakeKey P1 P2 *
      **
      ** Convert the top P1 entries of the stack into a single entry suitable
1277
1278
1279
1280
1281
1282
1283



1284
1285

1286
1287
1288
1289
1290

1291
1292

1293
1294
1295
1296
1297
1298
1299

1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313


1314
1315
1316
1317
1318
1319
1320
        int nField;
        int i, j;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){



          if( Stringify(p, i) ) goto no_mem;
          nByte += p->iStack[i]+1;

        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){

          memcpy(&zNewKey[j], p->zStack[i], p->iStack[i]-1);
          j += p->iStack[i]-1;

          if( i<p->tos ) zNewKey[j++] = '\t';
        }
        zNewKey[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->iStack[p->tos] = nByte;

        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Open P1 P2 P3
      **
      ** Open a new database table named P3.  Give it an identifier P1.
      ** Open readonly if P2==0 and for reading and writing if P2!=0.
      ** The table is created if it does not already exist and P2!=0.
      ** If there is already another table opened on P1, then the old
      ** table is closed first.  All tables are automatically closed when
      ** the VDBE finishes execution.  The P1 values need not be
      ** contiguous but all P1 values should be small integers.  It is
      ** an error for P1 to be negative.


      */
      case OP_Open: {
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( i>=p->nTable ){
          int j;
          p->aTab = sqliteRealloc( p->aTab, (i+1)*sizeof(VdbeTable) );







>
>
>
|
|
>





>
|
|
>






|
>














>
>







1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
        int nField;
        int i, j;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            nByte++;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
            j += p->aStack[i].n-1;
          }
          if( i<p->tos ) zNewKey[j++] = '\t';
        }
        zNewKey[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Open P1 P2 P3
      **
      ** Open a new database table named P3.  Give it an identifier P1.
      ** Open readonly if P2==0 and for reading and writing if P2!=0.
      ** The table is created if it does not already exist and P2!=0.
      ** If there is already another table opened on P1, then the old
      ** table is closed first.  All tables are automatically closed when
      ** the VDBE finishes execution.  The P1 values need not be
      ** contiguous but all P1 values should be small integers.  It is
      ** an error for P1 to be negative.
      **
      ** If P3 is null or an empty string, a temporary table is opened.
      */
      case OP_Open: {
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( i>=p->nTable ){
          int j;
          p->aTab = sqliteRealloc( p->aTab, (i+1)*sizeof(VdbeTable) );
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380

1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405

1406
1407
1408
1409
1410
1411
1412
1413
      ** stack or anything like that.
      */
      case OP_Fetch: {
        int i = pOp->p1;
        int tos = p->tos;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
          if( p->zStack[tos]==0 ){
            sqliteDbbeFetch(p->aTab[i].pTable, sizeof(int), 
                           (char*)&p->iStack[tos]);
          }else{

            sqliteDbbeFetch(p->aTab[i].pTable, p->iStack[tos], 
                           p->zStack[tos]);
          }
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Distinct P1 P2 *
      **
      ** Use the top of the stack as a key.  If a record with that key
      ** does not exist in table P1, then jump to P2.  If the record
      ** does already exist, then fall thru.  The record is not retrieved.
      ** The key is not popped from the stack.
      */
      case OP_Distinct: {
        int i = pOp->p1;
        int tos = p->tos;
        int alreadyExists = 0;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
          if( p->zStack[tos]==0 ){
            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, sizeof(int), 
                                          (char*)&p->iStack[tos]);
          }else{

            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, p->iStack[tos], 
                                           p->zStack[tos]);
          }
        }
        if( !alreadyExists ){
          pc = pOp->p2 - 1;
        }
        break;







|

|

>
|




















|

|

>
|







1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
      ** stack or anything like that.
      */
      case OP_Fetch: {
        int i = pOp->p1;
        int tos = p->tos;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
          if( p->aStack[tos].flags & STK_Int ){
            sqliteDbbeFetch(p->aTab[i].pTable, sizeof(int), 
                           (char*)&p->aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            sqliteDbbeFetch(p->aTab[i].pTable, p->aStack[tos].n, 
                           p->zStack[tos]);
          }
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Distinct P1 P2 *
      **
      ** Use the top of the stack as a key.  If a record with that key
      ** does not exist in table P1, then jump to P2.  If the record
      ** does already exist, then fall thru.  The record is not retrieved.
      ** The key is not popped from the stack.
      */
      case OP_Distinct: {
        int i = pOp->p1;
        int tos = p->tos;
        int alreadyExists = 0;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
          if( p->aStack[tos].flags & STK_Int ){
            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, sizeof(int), 
                                          (char*)&p->aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, p->aStack[tos].n, 
                                           p->zStack[tos]);
          }
        }
        if( !alreadyExists ){
          pc = pOp->p2 - 1;
        }
        break;
1424
1425
1426
1427
1428
1429
1430
1431

1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478

1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494





1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512




1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
        if( i<0 || i>=p->nTable || p->aTab[i].pTable==0 ){
          v = 0;
        }else{
          v = sqliteDbbeNew(p->aTab[i].pTable);
        }
        NeedStack(p, p->tos+1);
        p->tos++;
        p->iStack[p->tos] = v;

        break;
      }

      /* Opcode: Put P1 * *
      **
      ** Write an entry into the database table P1.  A new entry is
      ** created if it doesn't already exist, or the data for an existing
      ** entry is overwritten.  The data is the value on the top of the
      ** stack.  The key is the next value down on the stack.  The stack
      ** is popped twice by this instruction.
      */
      case OP_Put: {
        int tos = p->tos;
        int nos = p->tos-1;
        int i = pOp->p1;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
          char *zKey;
          int nKey;

          Stringify(p, tos);
          if( p->zStack[nos]!=0 ){
            nKey = p->iStack[nos];
            zKey = p->zStack[nos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&p->iStack[nos];
          }
          sqliteDbbePut(p->aTab[i].pTable, nKey, zKey,
                        p->iStack[tos], p->zStack[tos]);
        }
        PopStack(p, 2);
        break;
      }

      /* Opcode: Delete P1 * *
      **
      ** The top of the stack is a key.  Remove this key and its data
      ** from database table P1.  Then pop the stack to discard the key.
      */
      case OP_Delete: {
        int tos = p->tos;
        int i = pOp->p1;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
          char *zKey;
          int nKey;
          if( p->zStack[tos]!=0 ){

            nKey = p->iStack[tos];
            zKey = p->zStack[tos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&p->iStack[tos];
          }
          sqliteDbbeDelete(p->aTab[i].pTable, nKey, zKey);
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Field P1 P2 *
      **
      ** Push onto the stack the value of the P2-th field from the
      ** most recent Fetch from table P1.





      */
      case OP_Field: {
        int *pAddr;
        int amt;
        int i = pOp->p1;
        int p2 = pOp->p2;
        int tos = ++p->tos;
        DbbeTable *pTab;
        char *z;

        if( NeedStack(p, p->tos) ) goto no_mem;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          amt = sqliteDbbeDataLength(pTab);
          if( amt<=sizeof(int)*(p2+1) ){
            sqliteSetString(&p->zStack[tos], "", 0);
            break;
          }
          pAddr = (int*)sqliteDbbeReadData(pTab, sizeof(int)*p2);




          z = sqliteDbbeReadData(pTab, *pAddr);
          sqliteSetString(&p->zStack[tos], z, 0);
          p->iStack[tos] = strlen(z)+1;
        }
        break;
      }

      /* Opcode: Key P1 * *
      **
      ** Push onto the stack an integer which is the first 4 bytes of the
      ** the key to the current entry in a sequential scan of the table P1.
      ** A sequential scan is started using the Next opcode.
      */
      case OP_Key: {
        int i = pOp->p1;
        int tos = ++p->tos;
        DbbeTable *pTab;

        if( NeedStack(p, p->tos) ) goto no_mem;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          char *z = sqliteDbbeReadKey(pTab, 0);
          memcpy(&p->iStack[tos], z, sizeof(int));
          p->zStack[tos] = 0;
        }
        break;
      }

      /* Opcode: Rewind P1 * *
      **
      ** The next use of the Key or Field or Next instruction for P1 







|
>



















>
|
<
|



|


|

















|
>
|



|











>
>
>
>
>










|



|



>
>
>
>
|
|
|


















|
|







1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621

1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
        if( i<0 || i>=p->nTable || p->aTab[i].pTable==0 ){
          v = 0;
        }else{
          v = sqliteDbbeNew(p->aTab[i].pTable);
        }
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].i = v;
        p->aStack[p->tos].flags = STK_Int;
        break;
      }

      /* Opcode: Put P1 * *
      **
      ** Write an entry into the database table P1.  A new entry is
      ** created if it doesn't already exist, or the data for an existing
      ** entry is overwritten.  The data is the value on the top of the
      ** stack.  The key is the next value down on the stack.  The stack
      ** is popped twice by this instruction.
      */
      case OP_Put: {
        int tos = p->tos;
        int nos = p->tos-1;
        int i = pOp->p1;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
          char *zKey;
          int nKey;
          if( (p->aStack[nos].flags & STK_Int)==0 ){
            if( Stringify(p, nos) ) goto no_mem;

            nKey = p->aStack[nos].n;
            zKey = p->zStack[nos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&p->aStack[nos].i;
          }
          sqliteDbbePut(p->aTab[i].pTable, nKey, zKey,
                        p->aStack[tos].n, p->zStack[tos]);
        }
        PopStack(p, 2);
        break;
      }

      /* Opcode: Delete P1 * *
      **
      ** The top of the stack is a key.  Remove this key and its data
      ** from database table P1.  Then pop the stack to discard the key.
      */
      case OP_Delete: {
        int tos = p->tos;
        int i = pOp->p1;
        if( tos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
          char *zKey;
          int nKey;
          if( (p->aStack[tos].flags & STK_Int)==0 ){
            if( Stringify(p, tos) ) goto no_mem;
            nKey = p->aStack[tos].n;
            zKey = p->zStack[tos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&p->aStack[tos].n;
          }
          sqliteDbbeDelete(p->aTab[i].pTable, nKey, zKey);
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Field P1 P2 *
      **
      ** Push onto the stack the value of the P2-th field from the
      ** most recent Fetch from table P1.
      ** 
      ** The value pushed is just a pointer to the data in the cursor.
      ** The value will go away the next time a record is fetched from P1,
      ** or when P1 is closed.  Make a copy of the string if it needs
      ** to persist longer than that.
      */
      case OP_Field: {
        int *pAddr;
        int amt;
        int i = pOp->p1;
        int p2 = pOp->p2;
        int tos = ++p->tos;
        DbbeTable *pTab;
        char *z;

        if( NeedStack(p, tos) ) goto no_mem;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          amt = sqliteDbbeDataLength(pTab);
          if( amt<=sizeof(int)*(p2+1) ){
            p->aStack[tos].flags = STK_Null;
            break;
          }
          pAddr = (int*)sqliteDbbeReadData(pTab, sizeof(int)*p2);
          if( *pAddr==0 ){
            p->aStack[tos].flags = STK_Null;
            break;
          }
          p->zStack[tos] = z = sqliteDbbeReadData(pTab, *pAddr);
          p->aStack[tos].n = strlen(z) + 1;
          p->aStack[tos].flags = STK_Str;
        }
        break;
      }

      /* Opcode: Key P1 * *
      **
      ** Push onto the stack an integer which is the first 4 bytes of the
      ** the key to the current entry in a sequential scan of the table P1.
      ** A sequential scan is started using the Next opcode.
      */
      case OP_Key: {
        int i = pOp->p1;
        int tos = ++p->tos;
        DbbeTable *pTab;

        if( NeedStack(p, p->tos) ) goto no_mem;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          char *z = sqliteDbbeReadKey(pTab, 0);
          memcpy(&p->aStack[tos].i, z, sizeof(int));
          p->aStack[tos].flags = STK_Int;
        }
        break;
      }

      /* Opcode: Rewind P1 * *
      **
      ** The next use of the Key or Field or Next instruction for P1 
1595
1596
1597
1598
1599
1600
1601
1602

1603
1604
1605
1606
1607
1608
1609
          int *aIdx;
          int nIdx;
          int j;
          nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
          aIdx = (int*)sqliteDbbeReadData(pTab, 0);
          for(j=p->aTab[i].index; j<nIdx; j++){
            if( aIdx[j]!=0 ){
              p->iStack[tos] = aIdx[j];

              break;
            }
          }
          if( j>=nIdx ){
            j = -1;
            pc = pOp->p2 - 1;
            PopStack(p, 1);







|
>







1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
          int *aIdx;
          int nIdx;
          int j;
          nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
          aIdx = (int*)sqliteDbbeReadData(pTab, 0);
          for(j=p->aTab[i].index; j<nIdx; j++){
            if( aIdx[j]!=0 ){
              p->aStack[tos].i = aIdx[j];
              p->aStack[tos].flags = STK_Int;
              break;
            }
          }
          if( j>=nIdx ){
            j = -1;
            pc = pOp->p2 - 1;
            PopStack(p, 1);
1626
1627
1628
1629
1630
1631
1632

1633

1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
        int i = pOp->p1;
        int tos = p->tos;
        int nos = tos - 1;
        DbbeTable *pTab;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          int r;

          int newVal = Integerify(p, nos);

          Stringify(p, tos);
          r = sqliteDbbeFetch(pTab, p->iStack[tos], p->zStack[tos]);
          if( r==0 ){
            /* Create a new record for this index */
            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos],
                          sizeof(int), (char*)&newVal);
          }else{
            /* Extend the existing record */
            int nIdx;
            int *aIdx;
            nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
            aIdx = sqliteMalloc( sizeof(int)*(nIdx+1) );
            if( aIdx==0 ) goto no_mem;
            sqliteDbbeCopyData(pTab, 0, nIdx*sizeof(int), (char*)aIdx);
            aIdx[nIdx] = newVal;
            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos],
                          sizeof(int)*(nIdx+1), (char*)aIdx);
            sqliteFree(aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }







>
|
>
|
|


|










|







1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
        int i = pOp->p1;
        int tos = p->tos;
        int nos = tos - 1;
        DbbeTable *pTab;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          int r;
          int newVal;
          Integerify(p, nos);
          newVal = p->aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = sqliteDbbeFetch(pTab, p->aStack[tos].n, p->zStack[tos]);
          if( r==0 ){
            /* Create a new record for this index */
            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos],
                          sizeof(int), (char*)&newVal);
          }else{
            /* Extend the existing record */
            int nIdx;
            int *aIdx;
            nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
            aIdx = sqliteMalloc( sizeof(int)*(nIdx+1) );
            if( aIdx==0 ) goto no_mem;
            sqliteDbbeCopyData(pTab, 0, nIdx*sizeof(int), (char*)aIdx);
            aIdx[nIdx] = newVal;
            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos],
                          sizeof(int)*(nIdx+1), (char*)aIdx);
            sqliteFree(aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }
1671
1672
1673
1674
1675
1676
1677

1678

1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
        DbbeTable *pTab;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          int *aIdx;
          int nIdx;
          int j;
          int r;

          int oldVal = Integerify(p, nos);

          Stringify(p, tos);
          r = sqliteDbbeFetch(pTab, p->iStack[tos], p->zStack[tos]);
          if( r==0 ) break;
          nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
          aIdx = (int*)sqliteDbbeReadData(pTab, 0);
          for(j=0; j<nIdx && aIdx[j]!=oldVal; j++){}
          if( j>=nIdx ) break;
          aIdx[j] = aIdx[nIdx-1];
          if( nIdx==1 ){
            sqliteDbbeDelete(pTab, p->iStack[tos], p->zStack[tos]);
          }else{
            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos], 
                          sizeof(int)*(nIdx-1), (char*)aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }








>
|
>
|
|







|

|







1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
        DbbeTable *pTab;
        if( nos<0 ) goto not_enough_stack;
        if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
          int *aIdx;
          int nIdx;
          int j;
          int r;
          int oldVal;
          Integerify(p, nos);
          oldVal = p->aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = sqliteDbbeFetch(pTab, p->aStack[tos].n, p->zStack[tos]);
          if( r==0 ) break;
          nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
          aIdx = (int*)sqliteDbbeReadData(pTab, 0);
          for(j=0; j<nIdx && aIdx[j]!=oldVal; j++){}
          if( j>=nIdx ) break;
          aIdx[j] = aIdx[nIdx-1];
          if( nIdx==1 ){
            sqliteDbbeDelete(pTab, p->aStack[tos].n, p->zStack[tos]);
          }else{
            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos], 
                          sizeof(int)*(nIdx-1), (char*)aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }

1747
1748
1749
1750
1751
1752
1753

1754

1755
1756
1757
1758
1759
1760
1761
      ** into the temporary storage file P1.
      */
      case OP_ListWrite: {
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( p->tos<0 ) goto not_enough_stack;
        if( i<p->nList && p->apList[i]!=0 ){

          int val = Integerify(p, p->tos);

          PopStack(p, 1);
          fwrite(&val, sizeof(int), 1, p->apList[i]);
        }
        break;
      }

      /* Opcode: ListRewind P1 * *







>
|
>







1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
      ** into the temporary storage file P1.
      */
      case OP_ListWrite: {
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( p->tos<0 ) goto not_enough_stack;
        if( i<p->nList && p->apList[i]!=0 ){
          int val;
          Integerify(p, p->tos);
          val = p->aStack[p->tos].i;
          PopStack(p, 1);
          fwrite(&val, sizeof(int), 1, p->apList[i]);
        }
        break;
      }

      /* Opcode: ListRewind P1 * *
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
        int i = pOp->p1;
        int val, amt;
        if( i<0 || i>=p->nList || p->apList[i]==0 ) goto bad_instruction;
        amt = fread(&val, sizeof(int), 1, p->apList[i]);
        if( amt==1 ){
          p->tos++;
          if( NeedStack(p, p->tos) ) goto no_mem;
          p->iStack[p->tos] = val;
          p->zStack[p->tos] = 0;
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }

      /* Opcode: ListClose P1 * *







|
|







1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
        int i = pOp->p1;
        int val, amt;
        if( i<0 || i>=p->nList || p->apList[i]==0 ) goto bad_instruction;
        amt = fread(&val, sizeof(int), 1, p->apList[i]);
        if( amt==1 ){
          p->tos++;
          if( NeedStack(p, p->tos) ) goto no_mem;
          p->aStack[p->tos].n = val;
          p->aStack[p->tos].flags = STK_Int;
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }

      /* Opcode: ListClose P1 * *
1827
1828
1829
1830
1831
1832
1833


1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846


1847
1848

1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868

1869
1870

1871
1872
1873
1874
1875
1876



1877
1878
1879

1880
1881
1882
1883
1884
1885

1886
1887
1888
1889
1890
1891
1892
      /* Opcode: SortPut P1 * *
      **
      ** The TOS is the key and the NOS is the data.  Pop both from the stack
      ** and put them on the sorter.
      */
      case OP_SortPut: {
        int i = pOp->p1;


        Sorter *pSorter;
        if( i<0 || i>=p->nSort ) goto bad_instruction;
        if( p->tos<1 ) goto not_enough_stack;
        Stringify(p, p->tos);
        Stringify(p, p->tos-1);
        pSorter = sqliteMalloc( sizeof(Sorter) );
        if( pSorter==0 ) goto no_mem;
        pSorter->pNext = p->apSort[i];
        p->apSort[i] = pSorter;
        pSorter->nKey = p->iStack[p->tos];
        pSorter->zKey = p->zStack[p->tos];
        pSorter->nData = p->iStack[p->tos-1];
        pSorter->pData = p->zStack[p->tos-1];


        p->zStack[p->tos] = p->zStack[p->tos-1] = 0;
        PopStack(p, 2);

        break;
      }

      /* Opcode: SortMakeRec P1 * *
      **
      ** The top P1 elements are the arguments to a callback.  Form these
      ** elements into a single data entry that can be stored on a sorter
      ** using SortPut and later fed to a callback using SortCallback.
      */
      case OP_SortMakeRec: {
        char *z;
        char **azArg;
        int nByte;
        int nField;
        int i, j;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){

          if( Stringify(p, i) ) goto no_mem;
          nByte += p->iStack[i];

        }
        nByte += sizeof(char*)*(nField+1);
        azArg = sqliteMalloc( nByte );
        if( azArg==0 ) goto no_mem;
        z = (char*)&azArg[nField+1];
        for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){



          azArg[j] = z;
          strcpy(z, p->zStack[i]);
          z += p->iStack[i];

        }
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->iStack[p->tos] = nByte;
        p->zStack[p->tos] = (char*)azArg;

        break;
      }

      /* Opcode: SortMakeKey P1 * P3
      **
      ** Convert the top few entries of the stack into a sort key.  The
      ** number of stack entries consumed is the number of characters in 







>
>


|
|
<




|
|
|
|
>
>
|
|
>




















>
|
|
>






>
>
>
|
|
|
>




|

>







2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025

2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
      /* Opcode: SortPut P1 * *
      **
      ** The TOS is the key and the NOS is the data.  Pop both from the stack
      ** and put them on the sorter.
      */
      case OP_SortPut: {
        int i = pOp->p1;
        int tos = p->tos;
        int nos = tos - 1;
        Sorter *pSorter;
        if( i<0 || i>=p->nSort ) goto bad_instruction;
        if( tos<1 ) goto not_enough_stack;
        if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem;

        pSorter = sqliteMalloc( sizeof(Sorter) );
        if( pSorter==0 ) goto no_mem;
        pSorter->pNext = p->apSort[i];
        p->apSort[i] = pSorter;
        pSorter->nKey = p->aStack[tos].n;
        pSorter->zKey = p->zStack[tos];
        pSorter->nData = p->aStack[nos].n;
        pSorter->pData = p->zStack[nos];
        p->aStack[tos].flags = 0;
        p->aStack[nos].flags = 0;
        p->zStack[tos] = 0;
        p->zStack[nos] = 0;
        p->tos -= 2;
        break;
      }

      /* Opcode: SortMakeRec P1 * *
      **
      ** The top P1 elements are the arguments to a callback.  Form these
      ** elements into a single data entry that can be stored on a sorter
      ** using SortPut and later fed to a callback using SortCallback.
      */
      case OP_SortMakeRec: {
        char *z;
        char **azArg;
        int nByte;
        int nField;
        int i, j;

        nField = pOp->p1;
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        nByte += sizeof(char*)*(nField+1);
        azArg = sqliteMalloc( nByte );
        if( azArg==0 ) goto no_mem;
        z = (char*)&azArg[nField+1];
        for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
          if( p->aStack[i].flags & STK_Null ){
            azArg[j] = 0;
          }else{
            azArg[j] = z;
            strcpy(z, p->zStack[i]);
            z += p->aStack[i].n;
          }
        }
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->zStack[p->tos] = (char*)azArg;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        break;
      }

      /* Opcode: SortMakeKey P1 * P3
      **
      ** Convert the top few entries of the stack into a sort key.  The
      ** number of stack entries consumed is the number of characters in 
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929

1930
1931
1932
1933
1934
1935
1936
        int i, j, k;

        nField = strlen(pOp->p3);
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( Stringify(p, i) ) goto no_mem;
          nByte += p->iStack[i]+2;
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        k = nField-1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          zNewKey[j++] = pOp->p3[k--];
          memcpy(&zNewKey[j], p->zStack[i], p->iStack[i]-1);
          j += p->iStack[i]-1;
          zNewKey[j++] = 0;
        }
        zNewKey[j] = 0;
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->iStack[p->tos] = nByte;

        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Sort P1 * *
      **
      ** Sort all elements on the given sorter.  The algorithm is a







|







|
|






|
>







2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
        int i, j, k;

        nField = strlen(pOp->p3);
        if( p->tos+1<nField ) goto not_enough_stack;
        nByte = 1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( Stringify(p, i) ) goto no_mem;
          nByte += p->aStack[i].n+2;
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        k = nField-1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          zNewKey[j++] = pOp->p3[k--];
          memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
          j += p->aStack[i].n-1;
          zNewKey[j++] = 0;
        }
        zNewKey[j] = 0;
        PopStack(p, nField);
        NeedStack(p, p->tos+1);
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Sort P1 * *
      **
      ** Sort all elements on the given sorter.  The algorithm is a
1983
1984
1985
1986
1987
1988
1989
1990

1991
1992
1993
1994
1995
1996
1997
        if( i<0 ) goto bad_instruction;
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->apSort[i] = pSorter->pNext;
          p->tos++;
          NeedStack(p, p->tos);
          p->zStack[p->tos] = pSorter->pData;
          p->iStack[p->tos] = pSorter->nData;

          sqliteFree(pSorter->zKey);
          sqliteFree(pSorter);
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }







|
>







2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
        if( i<0 ) goto bad_instruction;
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->apSort[i] = pSorter->pNext;
          p->tos++;
          NeedStack(p, p->tos);
          p->zStack[p->tos] = pSorter->pData;
          p->aStack[p->tos].n = pSorter->nData;
          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
          sqliteFree(pSorter->zKey);
          sqliteFree(pSorter);
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }
2005
2006
2007
2008
2009
2010
2011
2012

2013
2014
2015
2016
2017
2018
2019
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->tos++;
          NeedStack(p, p->tos);
          sqliteSetString(&p->zStack[p->tos], pSorter->zKey, 0);
          p->iStack[p->tos] = pSorter->nKey;

        }
        break;
      }

      /* Opcode: SortCallback P1 P2 *
      **
      ** The top of the stack contains a callback record built using







|
>







2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
        int i = pOp->p1;
        if( i<0 ) goto bad_instruction;
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->tos++;
          NeedStack(p, p->tos);
          sqliteSetString(&p->zStack[p->tos], pSorter->zKey, 0);
          p->aStack[p->tos].n = pSorter->nKey;
          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        }
        break;
      }

      /* Opcode: SortCallback P1 P2 *
      **
      ** The top of the stack contains a callback record built using
2186
2187
2188
2189
2190
2191
2192
2193
2194

2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216







2217



2218
2219
2220
2221
2222
2223
2224
2225
        if( i>=0 && i<p->nField && p->azField ){
          z = p->azField[i];
        }else{
          z = 0;
        }
        if( z==0 ) z = "";
        p->tos++;
        p->iStack[p->tos] = strlen(z) + 1;
        sqliteSetString(&p->zStack[p->tos], z, 0);

        break;
      }

      /* An other opcode is illegal...
      */
      default: {
        sprintf(zBuf,"%d",pOp->opcode);
        sqliteSetString(pzErrMsg, "unknown opcode ", zBuf, 0);
        rc = SQLITE_INTERNAL;
        break;
      }
    }
    if( pc<-1 || pc>=p->nOp ){
      sqliteSetString(pzErrMsg, "jump destination out of range", 0);
      rc = SQLITE_INTERNAL;
    }
    if( p->trace && p->tos>=0 ){
      int i;
      fprintf(p->trace, "Stack:");
      for(i=p->tos; i>=0 && i>p->tos-5; i--){
        if( p->zStack[i] ){
          fprintf(p->trace, " [%.11s]", p->zStack[i]);







        }else{



          fprintf(p->trace, " [%d]", p->iStack[i]);
        }
      }
      fprintf(p->trace,"\n");
    }
  }

cleanup:







|
|
>




















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







2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
        if( i>=0 && i<p->nField && p->azField ){
          z = p->azField[i];
        }else{
          z = 0;
        }
        if( z==0 ) z = "";
        p->tos++;
        p->aStack[p->tos].n = strlen(z) + 1;
        p->zStack[p->tos] = z;
        p->aStack[p->tos].flags = STK_Str;
        break;
      }

      /* An other opcode is illegal...
      */
      default: {
        sprintf(zBuf,"%d",pOp->opcode);
        sqliteSetString(pzErrMsg, "unknown opcode ", zBuf, 0);
        rc = SQLITE_INTERNAL;
        break;
      }
    }
    if( pc<-1 || pc>=p->nOp ){
      sqliteSetString(pzErrMsg, "jump destination out of range", 0);
      rc = SQLITE_INTERNAL;
    }
    if( p->trace && p->tos>=0 ){
      int i;
      fprintf(p->trace, "Stack:");
      for(i=p->tos; i>=0 && i>p->tos-5; i--){
        if( p->aStack[i].flags & STK_Null ){
          fprintf(p->trace, " NULL");
        }else if( p->aStack[i].flags & STK_Int ){
          fprintf(p->trace, " i:%d", p->aStack[i].i);
        }else if( p->aStack[i].flags & STK_Real ){
          fprintf(p->trace, " r:%g", p->aStack[i].r);
        }else if( p->aStack[i].flags & STK_Str ){
          if( p->aStack[i].flags & STK_Dyn ){
            fprintf(p->trace, " z:[%.11s]", p->zStack[i]);
          }else{
            fprintf(p->trace, " s:[%.11s]", p->zStack[i]);
          }
        }else{
          fprintf(p->trace, " ???");
        }
      }
      fprintf(p->trace,"\n");
    }
  }

cleanup:
Changes to src/vdbe.h.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
*************************************************************************
** 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.4 2000/05/31 20:00:53 drh Exp $
*/
#ifndef _SQLITE_VDBE_H_
#define _SQLITE_VDBE_H_
#include <stdio.h>

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







|







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
*************************************************************************
** 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.5 2000/06/04 12:58:39 drh Exp $
*/
#ifndef _SQLITE_VDBE_H_
#define _SQLITE_VDBE_H_
#include <stdio.h>

/*
** A single VDBE is an opaque structure named "Vdbe".  Only routines
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

#define OP_ColumnCount        41
#define OP_ColumnName         42
#define OP_Callback           43

#define OP_Integer            44
#define OP_String             45

#define OP_Pop                46
#define OP_Dup                47
#define OP_Pull               48

#define OP_Add                49
#define OP_AddImm             50
#define OP_Subtract           51
#define OP_Multiply           52
#define OP_Divide             53
#define OP_Min                54
#define OP_Max                55
#define OP_Like               56
#define OP_Glob               57
#define OP_Eq                 58
#define OP_Ne                 59
#define OP_Lt                 60
#define OP_Le                 61
#define OP_Gt                 62
#define OP_Ge                 63
#define OP_IsNull             64
#define OP_NotNull            65
#define OP_Negative           66
#define OP_And                67
#define OP_Or                 68
#define OP_Not                69
#define OP_Concat             70
#define OP_Noop               71

#define OP_MAX                71

/*
** Prototypes for the VDBE interface.  See comments on the implementation
** for a description of what each of these routines does.
*/
Vdbe *sqliteVdbeCreate(Dbbe*);
int sqliteVdbeAddOp(Vdbe*,int,int,int,const char*,int);







>
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|







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

#define OP_ColumnCount        41
#define OP_ColumnName         42
#define OP_Callback           43

#define OP_Integer            44
#define OP_String             45
#define OP_Null               46
#define OP_Pop                47
#define OP_Dup                48
#define OP_Pull               49

#define OP_Add                50
#define OP_AddImm             51
#define OP_Subtract           52
#define OP_Multiply           53
#define OP_Divide             54
#define OP_Min                55
#define OP_Max                56
#define OP_Like               57
#define OP_Glob               58
#define OP_Eq                 59
#define OP_Ne                 60
#define OP_Lt                 61
#define OP_Le                 62
#define OP_Gt                 63
#define OP_Ge                 64
#define OP_IsNull             65
#define OP_NotNull            66
#define OP_Negative           67
#define OP_And                68
#define OP_Or                 69
#define OP_Not                70
#define OP_Concat             71
#define OP_Noop               72

#define OP_MAX                72

/*
** Prototypes for the VDBE interface.  See comments on the implementation
** for a description of what each of these routines does.
*/
Vdbe *sqliteVdbeCreate(Dbbe*);
int sqliteVdbeAddOp(Vdbe*,int,int,int,const char*,int);