engine/sqlite/src/vdbe.cpp
author Sebastian Brannstrom <sebastianb@symbian.org>
Sat, 16 Oct 2010 15:36:19 +0100
branch3rded
changeset 275 e2c3225833c3
parent 2 29cda98b007e
permissions -rw-r--r--
Fix for bug 2818
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     1
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     2
** 2001 September 15
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     3
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     4
** The author disclaims copyright to this source code.  In place of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     5
** a legal notice, here is a blessing:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     6
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     7
**    May you do good and not evil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     8
**    May you find forgiveness for yourself and forgive others.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     9
**    May you share freely, never taking more than you give.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    10
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    11
*************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    12
** The code in this file implements execution method of the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    13
** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    14
** handles housekeeping details such as creating and deleting
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    15
** VDBE instances.  This file is solely interested in executing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    16
** the VDBE program.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    17
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    18
** In the external interface, an "sqlite3_stmt*" is an opaque pointer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    19
** to a VDBE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
** The SQL parser generates a program which is then executed by
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
** the VDBE to do the work of the SQL statement.  VDBE programs are 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
** similar in form to assembly language.  The program consists of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
** a linear sequence of operations.  Each operation has an opcode 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    25
** and 3 operands.  Operands P1 and P2 are integers.  Operand P3 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    26
** is a null-terminated string.   The P2 operand must be non-negative.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
** Opcodes will typically ignore one or more operands.  Many opcodes
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
** ignore all three operands.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
** Computation results are stored on a stack.  Each entry on the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
** stack is either an integer, a null-terminated string, a floating point
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    32
** number, or the SQL "NULL" value.  An inplicit conversion from one
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    33
** type to the other occurs as necessary.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
** Most of the code in this file is taken up by the sqlite3VdbeExec()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
** function which does the work of interpreting a VDBE program.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
** But other routines are also provided to help in building up
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
** a program instruction by instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
** Various scripts scan this source file in order to generate HTML
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
** documentation, headers files, or other derived files.  The formatting
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
** of the code in this file is, therefore, important.  See other comments
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
** in this file for details.  If in doubt, do not deviate from existing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
** commenting and indentation practices when changing or adding code.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
** $Id: vdbe.cpp 1282 2008-11-13 09:31:33Z LarsPson $
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    48
#include "sqliteInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    49
#include <ctype.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    50
#include "vdbeInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    51
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
** The following global variable is incremented every time a cursor
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes.  The test
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
** procedures use this information to make sure that indices are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
** working correctly.  This variable has no function other than to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
** help verify the correct operation of the library.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
int sqlite3_search_count = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
** When this global variable is positive, it gets decremented once before
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
** field of the sqlite3 structure is set in order to simulate and interrupt.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
** This facility is used for testing purposes only.  It does not function
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
** in an ordinary build.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    71
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    72
int sqlite3_interrupt_count = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
** The next global variable is incremented each type the OP_Sort opcode
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
** is executed.  The test procedures use this information to make sure that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
** sorting is occurring or not occuring at appropriate times.   This variable
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    79
** has no function other than to help verify the correct operation of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    80
** library.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    81
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
int sqlite3_sort_count = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    86
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    87
** The next global variable records the size of the largest MEM_Blob
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
** or MEM_Str that has appeared on the VDBE stack.  The test procedures
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
** use this information to make sure that the zero-blob functionality
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
** is working correctly.   This variable has no function other than to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    91
** help verify the correct operation of the library.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    92
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    93
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
int sqlite3_max_blobsize = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    97
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    98
** Release the memory associated with the given stack level.  This
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    99
** leaves the Mem.flags field in an inconsistent state.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   100
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
#define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
** Convert the given stack entity into a string if it isn't one
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   105
** already. Return non-zero if a malloc() fails.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   106
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
#define Stringify(P, enc) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
     { goto no_mem; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   110
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   111
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
** The header of a record consists of a sequence variable-length integers.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   113
** These integers are almost always small and are encoded as a single byte.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   114
** The following macro takes advantage this fact to provide a fast decode
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   115
** of the integers in a record header.  It is faster for the common case
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   116
** where the integer is a single byte.  It is a little slower when the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   117
** integer is two or more bytes.  But overall it is faster.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   118
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   119
** The following expressions are equivalent:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   120
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   121
**     x = sqlite3GetVarint32( A, &B );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   122
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   123
**     x = GetVarint( A, B );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   124
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   125
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   126
#define GetVarint(A,B)  ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   127
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   128
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   129
** An ephemeral string value (signified by the MEM_Ephem flag) contains
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   130
** a pointer to a dynamically allocated string where some other entity
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   131
** is responsible for deallocating that string.  Because the stack entry
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   132
** does not control the string, it might be deleted without the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   133
** entry knowing it.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   134
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   135
** This routine converts an ephemeral string into a dynamically allocated
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   136
** string that the stack entry itself controls.  In other words, it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   137
** converts an MEM_Ephem string into an MEM_Dyn string.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   138
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   139
#define Deephemeralize(P) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   140
   if( ((P)->flags&MEM_Ephem)!=0 \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   141
       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   142
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   143
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   144
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   145
** P if required.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   146
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   147
#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   148
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   149
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   150
** Argument pMem points at a memory cell that will be passed to a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   151
** user-defined function or returned to the user as the result of a query.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   152
** The second argument, 'db_enc' is the text encoding used by the vdbe for
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   153
** stack variables.  This routine sets the pMem->enc and pMem->type
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   154
** variables used by the sqlite3_value_*() routines.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   155
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   156
#define storeTypeInfo(A,B) _storeTypeInfo(A)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   157
static void _storeTypeInfo(Mem *pMem){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   158
  int flags = pMem->flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   159
  if( flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   160
    pMem->type = SQLITE_NULL;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   161
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   162
  else if( flags & MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   163
    pMem->type = SQLITE_INTEGER;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   164
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   165
  else if( flags & MEM_Real ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   166
    pMem->type = SQLITE_FLOAT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   167
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   168
  else if( flags & MEM_Str ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   169
    pMem->type = SQLITE_TEXT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   170
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   171
    pMem->type = SQLITE_BLOB;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   172
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   173
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   174
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   175
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   176
** Pop the stack N times.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   177
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   178
static void popStack(Mem **ppTos, int N){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   179
  Mem *pTos = *ppTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   180
  while( N>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   181
    N--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   182
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   183
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   184
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   185
  *ppTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   186
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   187
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   188
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   189
** Allocate cursor number iCur.  Return a pointer to it.  Return NULL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   190
** if we run out of memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   191
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   192
static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   193
  Cursor *pCx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   194
  assert( iCur<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   195
  if( p->apCsr[iCur] ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   196
    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   197
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   198
  p->apCsr[iCur] = pCx = (Cursor*)sqlite3MallocZero( sizeof(Cursor) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   199
  if( pCx ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   200
    pCx->iDb = iDb;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   201
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   202
  return pCx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   203
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   204
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   205
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   206
** Try to convert a value into a numeric representation if we can
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   207
** do so without loss of information.  In other words, if the string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   208
** looks like a number, convert it into a number.  If it does not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   209
** look like a number, leave it alone.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   210
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   211
static void applyNumericAffinity(Mem *pRec){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   212
  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   213
    int realnum;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   214
    sqlite3VdbeMemNulTerminate(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   215
    if( (pRec->flags&MEM_Str)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   216
         && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   217
      i64 value;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   218
      sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   219
      if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   220
        sqlite3VdbeMemRelease(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   221
        pRec->u.i = value;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   222
        pRec->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   223
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   224
        sqlite3VdbeMemRealify(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   225
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   226
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   227
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   228
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   229
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   230
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   231
** Processing is determine by the affinity parameter:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   232
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   233
** SQLITE_AFF_INTEGER:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   234
** SQLITE_AFF_REAL:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   235
** SQLITE_AFF_NUMERIC:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   236
**    Try to convert pRec to an integer representation or a 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   237
**    floating-point representation if an integer representation
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   238
**    is not possible.  Note that the integer representation is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   239
**    always preferred, even if the affinity is REAL, because
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   240
**    an integer representation is more space efficient on disk.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   241
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   242
** SQLITE_AFF_TEXT:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   243
**    Convert pRec to a text representation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   244
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   245
** SQLITE_AFF_NONE:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   246
**    No-op.  pRec is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   247
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   248
static void applyAffinity(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   249
  Mem *pRec,          /* The value to apply affinity to */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   250
  char affinity,      /* The affinity to be applied */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   251
  u8 enc              /* Use this text encoding */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   252
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   253
  if( affinity==SQLITE_AFF_TEXT ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   254
    /* Only attempt the conversion to TEXT if there is an integer or real
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   255
    ** representation (blob and NULL do not get converted) but no string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   256
    ** representation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   257
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   258
    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   259
      sqlite3VdbeMemStringify(pRec, enc);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   260
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   261
    pRec->flags &= ~(MEM_Real|MEM_Int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   262
  }else if( affinity!=SQLITE_AFF_NONE ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   263
    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   264
             || affinity==SQLITE_AFF_NUMERIC );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   265
    applyNumericAffinity(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   266
    if( pRec->flags & MEM_Real ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   267
      sqlite3VdbeIntegerAffinity(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   268
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   269
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   270
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   271
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   272
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   273
** Try to convert the type of a function argument or a result column
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   274
** into a numeric representation.  Use either INTEGER or REAL whichever
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   275
** is appropriate.  But only do the conversion if it is possible without
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   276
** loss of information and return the revised type of the argument.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   277
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   278
** This is an EXPERIMENTAL api and is subject to change or removal.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   279
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   280
EXPORT_C int sqlite3_value_numeric_type(sqlite3_value *pVal){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   281
  Mem *pMem = (Mem*)pVal;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   282
  applyNumericAffinity(pMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   283
  storeTypeInfo(pMem, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   284
  return pMem->type;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   285
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   286
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   287
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   288
** Exported version of applyAffinity(). This one works on sqlite3_value*, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   289
** not the internal Mem* type.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   290
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   291
void sqlite3ValueApplyAffinity(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   292
  sqlite3_value *pVal, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   293
  u8 affinity, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   294
  u8 enc
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   295
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   296
  applyAffinity((Mem *)pVal, affinity, enc);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   297
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   298
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   299
#ifdef SQLITE_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   300
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   301
** Write a nice string representation of the contents of cell pMem
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   302
** into buffer zBuf, length nBuf.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   303
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   304
void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   305
  char *zCsr = zBuf;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   306
  int f = pMem->flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   307
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   308
  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   309
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   310
  if( f&MEM_Blob ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   311
    int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   312
    char c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   313
    if( f & MEM_Dyn ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   314
      c = 'z';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   315
      assert( (f & (MEM_Static|MEM_Ephem))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   316
    }else if( f & MEM_Static ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   317
      c = 't';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   318
      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   319
    }else if( f & MEM_Ephem ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   320
      c = 'e';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   321
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   322
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   323
      c = 's';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   324
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   325
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   326
    sqlite3_snprintf(100, zCsr, "%c", c);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   327
    zCsr += strlen(zCsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   328
    sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   329
    zCsr += strlen(zCsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   330
    for(i=0; i<16 && i<pMem->n; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   331
      sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   332
      zCsr += strlen(zCsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   333
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   334
    for(i=0; i<16 && i<pMem->n; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   335
      char z = pMem->z[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   336
      if( z<32 || z>126 ) *zCsr++ = '.';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   337
      else *zCsr++ = z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   338
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   339
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   340
    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   341
    zCsr += strlen(zCsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   342
    if( f & MEM_Zero ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   343
      sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   344
      zCsr += strlen(zCsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   345
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   346
    *zCsr = '\0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   347
  }else if( f & MEM_Str ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   348
    int j, k;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   349
    zBuf[0] = ' ';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   350
    if( f & MEM_Dyn ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   351
      zBuf[1] = 'z';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   352
      assert( (f & (MEM_Static|MEM_Ephem))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   353
    }else if( f & MEM_Static ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   354
      zBuf[1] = 't';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   355
      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   356
    }else if( f & MEM_Ephem ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   357
      zBuf[1] = 'e';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   358
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   359
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   360
      zBuf[1] = 's';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   361
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   362
    k = 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   363
    sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   364
    k += strlen(&zBuf[k]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   365
    zBuf[k++] = '[';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   366
    for(j=0; j<15 && j<pMem->n; j++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   367
      u8 c = pMem->z[j];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   368
      if( c>=0x20 && c<0x7f ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   369
        zBuf[k++] = c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   370
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   371
        zBuf[k++] = '.';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   372
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   373
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   374
    zBuf[k++] = ']';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   375
    sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   376
    k += strlen(&zBuf[k]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   377
    zBuf[k++] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   378
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   379
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   380
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   381
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   382
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   383
#ifdef VDBE_PROFILE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   384
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   385
** The following routine only works on pentium-class processors.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   386
** It uses the RDTSC opcode to read the cycle count value out of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   387
** processor and returns that value.  This can be used for high-res
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   388
** profiling.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   389
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   390
__inline__ unsigned long long int hwtime(void){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   391
  unsigned long long int x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   392
  __asm__("rdtsc\n\t"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   393
          "mov %%edx, %%ecx\n\t"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   394
          :"=A" (x));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   395
  return x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   396
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   397
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   398
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   399
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   400
** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   401
** sqlite3_interrupt() routine has been called.  If it has been, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   402
** processing of the VDBE program is interrupted.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   403
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   404
** This macro added to every instruction that does a jump in order to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   405
** implement a loop.  This test used to be on every single instruction,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   406
** but that meant we more testing that we needed.  By only testing the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   407
** flag on jump instructions, we get a (small) speed improvement.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   408
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   409
#define CHECK_FOR_INTERRUPT \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   410
   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   411
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   412
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   413
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   414
** Execute as much of a VDBE program as we can then return.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   415
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   416
** sqlite3VdbeMakeReady() must be called before this routine in order to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   417
** close the program with a final OP_Halt and to set up the callbacks
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   418
** and the error message pointer.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   419
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   420
** Whenever a row or result data is available, this routine will either
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   421
** invoke the result callback (if there is one) or return with
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   422
** SQLITE_ROW.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   423
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   424
** If an attempt is made to open a locked database, then this routine
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   425
** will either invoke the busy callback (if there is one) or it will
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   426
** return SQLITE_BUSY.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   427
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   428
** If an error occurs, an error message is written to memory obtained
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   429
** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   430
** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   431
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   432
** If the callback ever returns non-zero, then the program exits
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   433
** immediately.  There will be no error message but the p->rc field is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   434
** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   435
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   436
** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   437
** routine to return SQLITE_ERROR.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   438
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   439
** Other fatal errors return SQLITE_ERROR.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   440
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   441
** After this routine has finished, sqlite3VdbeFinalize() should be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   442
** used to clean up the mess that was left behind.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   443
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   444
int sqlite3VdbeExec(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   445
  Vdbe *p                    /* The VDBE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   446
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   447
  int pc;                    /* The program counter */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   448
  Op *pOp;                   /* Current operation */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   449
  int rc = SQLITE_OK;        /* Value to return */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   450
  sqlite3 *db = p->db;       /* The database */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   451
  u8 encoding = ENC(db);     /* The database encoding */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   452
  Mem *pTos;                 /* Top entry in the operand stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   453
#ifdef VDBE_PROFILE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   454
  unsigned long long start;  /* CPU clock count at start of opcode */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   455
  int origPc;                /* Program counter at start of opcode */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   456
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   457
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   458
  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   459
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   460
#ifndef NDEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   461
  Mem *pStackLimit;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   462
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   463
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   464
  if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   465
  assert( db->magic==SQLITE_MAGIC_BUSY );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   466
  pTos = p->pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   467
  sqlite3BtreeMutexArrayEnter(&p->aMutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   468
  if( p->rc==SQLITE_NOMEM ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   469
    /* This happens if a malloc() inside a call to sqlite3_column_text() or
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   470
    ** sqlite3_column_text16() failed.  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   471
    goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   472
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   473
  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   474
  p->rc = SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   475
  assert( p->explain==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   476
  if( p->popStack ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   477
    popStack(&pTos, p->popStack);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   478
    p->popStack = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   479
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   480
  p->resOnStack = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   481
  db->busyHandler.nBusy = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   482
  CHECK_FOR_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   483
  sqlite3VdbeIOTraceSql(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   484
#ifdef SQLITE_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   485
  if( (p->db->flags & SQLITE_VdbeListing)!=0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   486
    || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   487
  ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   488
    int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   489
    printf("VDBE Program Listing:\n");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   490
    sqlite3VdbePrintSql(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   491
    for(i=0; i<p->nOp; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   492
      sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   493
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   494
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   495
  if( sqlite3OsAccess(db->pVfs, "vdbe_trace", SQLITE_ACCESS_EXISTS) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   496
    p->trace = stdout;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   497
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   498
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   499
  for(pc=p->pc; rc==SQLITE_OK; pc++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   500
    assert( pc>=0 && pc<p->nOp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   501
    assert( pTos<=&p->aStack[pc] );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   502
    if( db->mallocFailed ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   503
#ifdef VDBE_PROFILE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   504
    origPc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   505
    start = hwtime();
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   506
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   507
    pOp = &p->aOp[pc];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   508
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   509
    /* Only allow tracing if SQLITE_DEBUG is defined.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   510
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   511
#ifdef SQLITE_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   512
    if( p->trace ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   513
      if( pc==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   514
        printf("VDBE Execution Trace:\n");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   515
        sqlite3VdbePrintSql(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   516
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   517
      sqlite3VdbePrintOp(p->trace, pc, pOp);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   518
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   519
    if( p->trace==0 && pc==0 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   520
     && sqlite3OsAccess(db->pVfs, "vdbe_sqltrace", SQLITE_ACCESS_EXISTS) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   521
      sqlite3VdbePrintSql(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   522
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   523
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   524
      
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   525
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   526
    /* Check to see if we need to simulate an interrupt.  This only happens
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   527
    ** if we have a special test build.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   528
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   529
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   530
    if( sqlite3_interrupt_count>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   531
      sqlite3_interrupt_count--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   532
      if( sqlite3_interrupt_count==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   533
        sqlite3_interrupt(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   534
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   535
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   536
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   537
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   538
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   539
    /* Call the progress callback if it is configured and the required number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   540
    ** of VDBE ops have been executed (either since this invocation of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   541
    ** sqlite3VdbeExec() or since last time the progress callback was called).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   542
    ** If the progress callback returns non-zero, exit the virtual machine with
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   543
    ** a return code SQLITE_ABORT.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   544
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   545
    if( db->xProgress ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   546
      if( db->nProgressOps==nProgressOps ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   547
        int prc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   548
        if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   549
        prc =db->xProgress(db->pProgressArg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   550
        if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   551
        if( prc!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   552
          rc = SQLITE_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   553
          goto vdbe_halt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   554
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   555
        nProgressOps = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   556
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   557
      nProgressOps++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   558
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   559
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   560
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   561
#ifndef NDEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   562
    /* This is to check that the return value of static function
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   563
    ** opcodeNoPush() (see vdbeaux.c) returns values that match the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   564
    ** implementation of the virtual machine in this file. If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   565
    ** opcodeNoPush() returns non-zero, then the stack is guarenteed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   566
    ** not to grow when the opcode is executed. If it returns zero, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   567
    ** the stack may grow by at most 1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   568
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   569
    ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   570
    ** available if NDEBUG is defined at build time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   571
    */ 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   572
    pStackLimit = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   573
    if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   574
      pStackLimit++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   575
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   576
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   577
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   578
    switch( pOp->opcode ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   579
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   580
/*****************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   581
** What follows is a massive switch statement where each case implements a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   582
** separate instruction in the virtual machine.  If we follow the usual
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   583
** indentation conventions, each case should be indented by 6 spaces.  But
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   584
** that is a lot of wasted space on the left margin.  So the code within
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   585
** the switch statement will break with convention and be flush-left. Another
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   586
** big comment (similar to this one) will mark the point in the code where
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   587
** we transition back to normal indentation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   588
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   589
** The formatting of each case is important.  The makefile for SQLite
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   590
** generates two C files "opcodes.h" and "opcodes.c" by scanning this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   591
** file looking for lines that begin with "case OP_".  The opcodes.h files
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   592
** will be filled with #defines that give unique integer values to each
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   593
** opcode and the opcodes.c file is filled with an array of strings where
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   594
** each string is the symbolic name for the corresponding opcode.  If the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   595
** case statement is followed by a comment of the form "/# same as ... #/"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   596
** that comment is used to determine the particular value of the opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   597
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   598
** If a comment on the same line as the "case OP_" construction contains
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   599
** the word "no-push", then the opcode is guarenteed not to grow the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   600
** vdbe stack when it is executed. See function opcode() in
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   601
** vdbeaux.c for details.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   602
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   603
** Documentation about VDBE opcodes is generated by scanning this file
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   604
** for lines of that contain "Opcode:".  That line and all subsequent
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   605
** comment lines are used in the generation of the opcode.html documentation
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   606
** file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   607
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   608
** SUMMARY:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   609
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   610
**     Formatting is important to scripts that scan this file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   611
**     Do not deviate from the formatting style currently in use.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   612
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   613
*****************************************************************************/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   614
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   615
/* Opcode:  Goto * P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   616
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   617
** An unconditional jump to address P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   618
** The next instruction executed will be 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   619
** the one at index P2 from the beginning of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   620
** the program.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   621
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   622
case OP_Goto: {             /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   623
  CHECK_FOR_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   624
  pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   625
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   626
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   627
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   628
/* Opcode:  Gosub * P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   629
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   630
** Push the current address plus 1 onto the return address stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   631
** and then jump to address P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   632
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   633
** The return address stack is of limited depth.  If too many
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   634
** OP_Gosub operations occur without intervening OP_Returns, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   635
** the return address stack will fill up and processing will abort
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   636
** with a fatal error.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   637
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   638
case OP_Gosub: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   639
  assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   640
  p->returnStack[p->returnDepth++] = pc+1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   641
  pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   642
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   643
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   644
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   645
/* Opcode:  Return * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   646
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   647
** Jump immediately to the next instruction after the last unreturned
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   648
** OP_Gosub.  If an OP_Return has occurred for all OP_Gosubs, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   649
** processing aborts with a fatal error.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   650
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   651
case OP_Return: {           /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   652
  assert( p->returnDepth>0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   653
  p->returnDepth--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   654
  pc = p->returnStack[p->returnDepth] - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   655
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   656
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   657
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   658
/* Opcode:  Halt P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   659
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   660
** Exit immediately.  All open cursors, Fifos, etc are closed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   661
** automatically.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   662
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   663
** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   664
** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   665
** For errors, it can be some other value.  If P1!=0 then P2 will determine
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   666
** whether or not to rollback the current transaction.  Do not rollback
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   667
** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   668
** then back out all changes that have occurred during this execution of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   669
** VDBE, but do not rollback the transaction. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   670
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   671
** If P3 is not null then it is an error message string.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   672
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   673
** There is an implied "Halt 0 0 0" instruction inserted at the very end of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   674
** every program.  So a jump past the last instruction of the program
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   675
** is the same as executing Halt.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   676
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   677
case OP_Halt: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   678
  p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   679
  p->rc = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   680
  p->pc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   681
  p->errorAction = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   682
  if( pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   683
    sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   684
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   685
  rc = sqlite3VdbeHalt(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   686
  assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   687
  if( rc==SQLITE_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   688
    p->rc = rc = SQLITE_BUSY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   689
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   690
    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   691
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   692
  goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   693
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   694
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   695
/* Opcode:  StackDepth P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   696
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   697
** If P1 is less than zero, then store the current stack depth
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   698
** in P1.  If P1 is zero or greater, verify that the current stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   699
** depth is equal to P1 and throw an exception if it is not.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   700
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   701
** This opcode is used for internal consistency checking.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   702
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   703
case OP_StackDepth: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   704
  int n = pTos - p->aStack + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   705
  if( pOp->p1<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   706
    pOp->p1 = n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   707
  }else if( pOp->p1!=n ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   708
    p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   709
    p->rc = rc = SQLITE_INTERNAL;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   710
    p->pc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   711
    p->errorAction = OE_Rollback;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   712
    sqlite3SetString(&p->zErrMsg, "internal error: VDBE stack leak", (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   713
    goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   714
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   715
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   716
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   717
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   718
/* Opcode: Integer P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   719
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   720
** The 32-bit integer value P1 is pushed onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   721
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   722
case OP_Integer: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   723
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   724
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   725
  pTos->u.i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   726
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   727
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   728
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   729
/* Opcode: Int64 * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   730
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   731
** P3 is a pointer to a 64-bit integer value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   732
** Push  that value onto  the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   733
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   734
case OP_Int64: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   735
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   736
  assert( pOp->p3!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   737
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   738
  memcpy(&pTos->u.i, pOp->p3, 8);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   739
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   740
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   741
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   742
/* Opcode: Real * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   743
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   744
** P3 is a pointer to a 64-bit floating point value.  Push that value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   745
** onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   746
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   747
case OP_Real: {            /* same as TK_FLOAT, */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   748
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   749
  pTos->flags = MEM_Real;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   750
  memcpy(&pTos->r, pOp->p3, 8);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   751
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   752
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   753
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   754
/* Opcode: String8 * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   755
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   756
** P3 points to a nul terminated UTF-8 string. This opcode is transformed 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   757
** into an OP_String before it is executed for the first time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   758
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   759
case OP_String8: {         /* same as TK_STRING */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   760
  assert( pOp->p3!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   761
  pOp->opcode = OP_String;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   762
  pOp->p1 = strlen(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   763
  assert( SQLITE_MAX_SQL_LENGTH <= SQLITE_MAX_LENGTH );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   764
  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   765
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   766
#ifndef SQLITE_OMIT_UTF16
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   767
  if( encoding!=SQLITE_UTF8 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   768
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   769
    sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   770
    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   771
    if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   772
    pTos->flags &= ~(MEM_Dyn);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   773
    pTos->flags |= MEM_Static;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   774
    if( pOp->p3type==P3_DYNAMIC ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   775
      sqlite3_free(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   776
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   777
    pOp->p3type = P3_DYNAMIC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   778
    pOp->p3 = pTos->z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   779
    pOp->p1 = pTos->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   780
    assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   781
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   782
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   783
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   784
  /* Otherwise fall through to the next case, OP_String */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   785
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   786
  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   787
/* Opcode: String P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   788
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   789
** The string value P3 of length P1 (bytes) is pushed onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   790
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   791
case OP_String: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   792
  assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   793
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   794
  assert( pOp->p3!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   795
  pTos->flags = MEM_Str|MEM_Static|MEM_Term;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   796
  pTos->z = pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   797
  pTos->n = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   798
  pTos->enc = encoding;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   799
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   800
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   801
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   802
/* Opcode: Null * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   803
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   804
** Push a NULL onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   805
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   806
case OP_Null: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   807
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   808
  pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   809
  pTos->n = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   810
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   811
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   812
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   813
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   814
#ifndef SQLITE_OMIT_BLOB_LITERAL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   815
/* Opcode: HexBlob * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   816
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   817
** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   818
** vdbe stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   819
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   820
** The first time this instruction executes, in transforms itself into a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   821
** 'Blob' opcode with a binary blob as P3.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   822
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   823
case OP_HexBlob: {            /* same as TK_BLOB */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   824
  pOp->opcode = OP_Blob;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   825
  pOp->p1 = strlen(pOp->p3)/2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   826
  assert( SQLITE_MAX_SQL_LENGTH <= SQLITE_MAX_LENGTH );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   827
  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   828
  if( pOp->p1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   829
    char *zBlob = (char*)sqlite3HexToBlob(db, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   830
    if( !zBlob ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   831
    if( pOp->p3type==P3_DYNAMIC ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   832
      sqlite3_free(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   833
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   834
    pOp->p3 = zBlob;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   835
    pOp->p3type = P3_DYNAMIC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   836
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   837
    if( pOp->p3type==P3_DYNAMIC ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   838
      sqlite3_free(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   839
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   840
    pOp->p3type = P3_STATIC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   841
    pOp->p3 = "";
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   842
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   843
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   844
  /* Fall through to the next case, OP_Blob. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   845
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   846
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   847
/* Opcode: Blob P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   848
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   849
** P3 points to a blob of data P1 bytes long. Push this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   850
** value onto the stack. This instruction is not coded directly
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   851
** by the compiler. Instead, the compiler layer specifies
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   852
** an OP_HexBlob opcode, with the hex string representation of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   853
** the blob as P3. This opcode is transformed to an OP_Blob
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   854
** the first time it is executed.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   855
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   856
case OP_Blob: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   857
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   858
  assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   859
  sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   860
  pTos->enc = encoding;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   861
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   862
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   863
#endif /* SQLITE_OMIT_BLOB_LITERAL */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   864
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   865
/* Opcode: Variable P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   866
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   867
** Push the value of variable P1 onto the stack.  A variable is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   868
** an unknown in the original SQL string as handed to sqlite3_compile().
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   869
** Any occurance of the '?' character in the original SQL is considered
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   870
** a variable.  Variables in the SQL string are number from left to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   871
** right beginning with 1.  The values of variables are set using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   872
** sqlite3_bind() API.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   873
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   874
case OP_Variable: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   875
  int j = pOp->p1 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   876
  Mem *pVar;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   877
  assert( j>=0 && j<p->nVar );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   878
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   879
  pVar = &p->aVar[j];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   880
  if( sqlite3VdbeMemTooBig(pVar) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   881
    goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   882
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   883
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   884
  sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   885
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   886
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   887
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   888
/* Opcode: Pop P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   889
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   890
** P1 elements are popped off of the top of stack and discarded.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   891
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   892
case OP_Pop: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   893
  assert( pOp->p1>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   894
  popStack(&pTos, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   895
  assert( pTos>=&p->aStack[-1] );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   896
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   897
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   898
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   899
/* Opcode: Dup P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   900
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   901
** A copy of the P1-th element of the stack 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   902
** is made and pushed onto the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   903
** The top of the stack is element 0.  So the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   904
** instruction "Dup 0 0 0" will make a copy of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   905
** top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   906
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   907
** If the content of the P1-th element is a dynamically
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   908
** allocated string, then a new copy of that string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   909
** is made if P2==0.  If P2!=0, then just a pointer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   910
** to the string is copied.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   911
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   912
** Also see the Pull instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   913
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   914
case OP_Dup: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   915
  Mem *pFrom = &pTos[-pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   916
  assert( pFrom<=pTos && pFrom>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   917
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   918
  sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   919
  if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   920
    Deephemeralize(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   921
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   922
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   923
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   924
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   925
/* Opcode: Pull P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   926
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   927
** The P1-th element is removed from its current location on 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   928
** the stack and pushed back on top of the stack.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   929
** top of the stack is element 0, so "Pull 0 0 0" is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   930
** a no-op.  "Pull 1 0 0" swaps the top two elements of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   931
** the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   932
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   933
** See also the Dup instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   934
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   935
case OP_Pull: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   936
  Mem *pFrom = &pTos[-pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   937
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   938
  Mem ts;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   939
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   940
  ts = *pFrom;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   941
  Deephemeralize(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   942
  for(i=0; i<pOp->p1; i++, pFrom++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   943
    Deephemeralize(&pFrom[1]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   944
    assert( (pFrom[1].flags & MEM_Ephem)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   945
    *pFrom = pFrom[1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   946
    if( pFrom->flags & MEM_Short ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   947
      assert( pFrom->flags & (MEM_Str|MEM_Blob) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   948
      assert( pFrom->z==pFrom[1].zShort );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   949
      pFrom->z = pFrom->zShort;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   950
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   951
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   952
  *pTos = ts;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   953
  if( pTos->flags & MEM_Short ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   954
    assert( pTos->flags & (MEM_Str|MEM_Blob) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   955
    assert( pTos->z==pTos[-pOp->p1].zShort );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   956
    pTos->z = pTos->zShort;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   957
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   958
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   959
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   960
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   961
/* Opcode: Push P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   962
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   963
** Overwrite the value of the P1-th element down on the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   964
** stack (P1==0 is the top of the stack) with the value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   965
** of the top of the stack.  Then pop the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   966
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   967
case OP_Push: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   968
  Mem *pTo = &pTos[-pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   969
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   970
  assert( pTo>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   971
  sqlite3VdbeMemMove(pTo, pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   972
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   973
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   974
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   975
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   976
/* Opcode: Callback P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   977
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   978
** The top P1 values on the stack represent a single result row from
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   979
** a query.  This opcode causes the sqlite3_step() call to terminate
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   980
** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   981
** structure to provide access to the top P1 values as the result
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   982
** row.  When the sqlite3_step() function is run again, the top P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   983
** values will be automatically popped from the stack before the next
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   984
** instruction executes.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   985
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   986
case OP_Callback: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   987
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   988
  Mem *pFirstColumn;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   989
  assert( p->nResColumn==pOp->p1 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   990
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   991
  /* Data in the pager might be moved or changed out from under us
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   992
  ** in between the return from this sqlite3_step() call and the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   993
  ** next call to sqlite3_step().  So deephermeralize everything on 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   994
  ** the stack.  Note that ephemeral data is never stored in memory 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   995
  ** cells so we do not have to worry about them.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   996
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   997
  pFirstColumn = &pTos[0-pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   998
  for(pMem = p->aStack; pMem<pFirstColumn; pMem++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   999
    Deephemeralize(pMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1000
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1001
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1002
  /* Invalidate all ephemeral cursor row caches */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1003
  p->cacheCtr = (p->cacheCtr + 2)|1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1004
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1005
  /* Make sure the results of the current row are \000 terminated
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1006
  ** and have an assigned type.  The results are deephemeralized as
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1007
  ** as side effect.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1008
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1009
  for(; pMem<=pTos; pMem++ ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1010
    sqlite3VdbeMemNulTerminate(pMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1011
    storeTypeInfo(pMem, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1012
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1013
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1014
  /* Set up the statement structure so that it will pop the current
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1015
  ** results from the stack when the statement returns.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1016
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1017
  p->resOnStack = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1018
  p->nCallback++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1019
  p->popStack = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1020
  p->pc = pc + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1021
  p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1022
  rc = SQLITE_ROW;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1023
  goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1024
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1025
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1026
/* Opcode: Concat P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1027
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1028
** Look at the first P1+2 elements of the stack.  Append them all 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1029
** together with the lowest element first.  The original P1+2 elements
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1030
** are popped from the stack if P2==0 and retained if P2==1.  If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1031
** any element of the stack is NULL, then the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1032
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1033
** When P1==1, this routine makes a copy of the top stack element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1034
** into memory obtained from sqlite3_malloc().
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1035
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1036
case OP_Concat: {           /* same as TK_CONCAT */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1037
  char *zNew;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1038
  i64 nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1039
  int nField;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1040
  int i, j;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1041
  Mem *pTerm;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1042
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1043
  /* Loop through the stack elements to see how long the result will be. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1044
  nField = pOp->p1 + 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1045
  pTerm = &pTos[1-nField];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1046
  nByte = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1047
  for(i=0; i<nField; i++, pTerm++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1048
    assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1049
    if( pTerm->flags&MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1050
      nByte = -1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1051
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1052
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1053
    ExpandBlob(pTerm);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1054
    Stringify(pTerm, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1055
    nByte += pTerm->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1056
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1057
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1058
  if( nByte<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1059
    /* If nByte is less than zero, then there is a NULL value on the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1060
    ** In this case just pop the values off the stack (if required) and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1061
    ** push on a NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1062
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1063
    if( pOp->p2==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1064
      popStack(&pTos, nField);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1065
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1066
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1067
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1068
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1069
    /* Otherwise malloc() space for the result and concatenate all the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1070
    ** stack values.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1071
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1072
    if( nByte+2>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1073
      goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1074
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1075
    zNew = (char*)sqlite3DbMallocRaw(db, nByte+2 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1076
    if( zNew==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1077
    j = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1078
    pTerm = &pTos[1-nField];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1079
    for(i=j=0; i<nField; i++, pTerm++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1080
      int n = pTerm->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1081
      assert( pTerm->flags & (MEM_Str|MEM_Blob) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1082
      memcpy(&zNew[j], pTerm->z, n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1083
      j += n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1084
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1085
    zNew[j] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1086
    zNew[j+1] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1087
    assert( j==nByte );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1088
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1089
    if( pOp->p2==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1090
      popStack(&pTos, nField);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1091
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1092
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1093
    pTos->n = j;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1094
    pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1095
    pTos->xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1096
    pTos->enc = encoding;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1097
    pTos->z = zNew;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1098
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1099
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1100
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1101
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1102
/* Opcode: Add * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1103
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1104
** Pop the top two elements from the stack, add them together,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1105
** and push the result back onto the stack.  If either element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1106
** is a string then it is converted to a double using the atof()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1107
** function before the addition.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1108
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1109
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1110
/* Opcode: Multiply * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1111
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1112
** Pop the top two elements from the stack, multiply them together,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1113
** and push the result back onto the stack.  If either element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1114
** is a string then it is converted to a double using the atof()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1115
** function before the multiplication.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1116
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1117
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1118
/* Opcode: Subtract * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1119
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1120
** Pop the top two elements from the stack, subtract the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1121
** first (what was on top of the stack) from the second (the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1122
** next on stack)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1123
** and push the result back onto the stack.  If either element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1124
** is a string then it is converted to a double using the atof()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1125
** function before the subtraction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1126
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1127
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1128
/* Opcode: Divide * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1129
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1130
** Pop the top two elements from the stack, divide the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1131
** first (what was on top of the stack) from the second (the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1132
** next on stack)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1133
** and push the result back onto the stack.  If either element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1134
** is a string then it is converted to a double using the atof()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1135
** function before the division.  Division by zero returns NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1136
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1137
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1138
/* Opcode: Remainder * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1139
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1140
** Pop the top two elements from the stack, divide the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1141
** first (what was on top of the stack) from the second (the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1142
** next on stack)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1143
** and push the remainder after division onto the stack.  If either element
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1144
** is a string then it is converted to a double using the atof()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1145
** function before the division.  Division by zero returns NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1146
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1147
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1148
case OP_Add:                   /* same as TK_PLUS, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1149
case OP_Subtract:              /* same as TK_MINUS, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1150
case OP_Multiply:              /* same as TK_STAR, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1151
case OP_Divide:                /* same as TK_SLASH, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1152
case OP_Remainder: {           /* same as TK_REM, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1153
  Mem *pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1154
  int flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1155
  assert( pNos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1156
  flags = pTos->flags | pNos->flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1157
  if( (flags & MEM_Null)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1158
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1159
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1160
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1161
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1162
  }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1163
    i64 a, b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1164
    a = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1165
    b = pNos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1166
    switch( pOp->opcode ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1167
      case OP_Add:         b += a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1168
      case OP_Subtract:    b -= a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1169
      case OP_Multiply:    b *= a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1170
      case OP_Divide: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1171
        if( a==0 ) goto divide_by_zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1172
        /* Dividing the largest possible negative 64-bit integer (1<<63) by 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1173
        ** -1 returns an integer to large to store in a 64-bit data-type. On
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1174
        ** some architectures, the value overflows to (1<<63). On others,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1175
        ** a SIGFPE is issued. The following statement normalizes this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1176
        ** behaviour so that all architectures behave as if integer 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1177
        ** overflow occured.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1178
        */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1179
        if( a==-1 && b==(((i64)1)<<63) ) a = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1180
        b /= a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1181
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1182
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1183
      default: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1184
        if( a==0 ) goto divide_by_zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1185
        if( a==-1 ) a = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1186
        b %= a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1187
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1188
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1189
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1190
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1191
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1192
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1193
    pTos->u.i = b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1194
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1195
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1196
    double a, b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1197
    a = sqlite3VdbeRealValue(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1198
    b = sqlite3VdbeRealValue(pNos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1199
    switch( pOp->opcode ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1200
      case OP_Add:         b += a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1201
      case OP_Subtract:    b -= a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1202
      case OP_Multiply:    b *= a;       break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1203
      case OP_Divide: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1204
        if( a==0.0 ) goto divide_by_zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1205
        b /= a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1206
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1207
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1208
      default: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1209
        i64 ia = (i64)a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1210
        i64 ib = (i64)b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1211
        if( ia==0 ) goto divide_by_zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1212
        if( ia==-1 ) ia = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1213
        b = ib % ia;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1214
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1215
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1216
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1217
    if( sqlite3_isnan(b) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1218
      goto divide_by_zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1219
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1220
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1221
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1222
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1223
    pTos->r = b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1224
    pTos->flags = MEM_Real;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1225
    if( (flags & MEM_Real)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1226
      sqlite3VdbeIntegerAffinity(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1227
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1228
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1229
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1230
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1231
divide_by_zero:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1232
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1233
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1234
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1235
  pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1236
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1237
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1238
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1239
/* Opcode: CollSeq * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1240
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1241
** P3 is a pointer to a CollSeq struct. If the next call to a user function
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1242
** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1243
** be returned. This is used by the built-in min(), max() and nullif()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1244
** functions.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1245
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1246
** The interface used by the implementation of the aforementioned functions
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1247
** to retrieve the collation sequence set by this opcode is not available
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1248
** publicly, only to user functions defined in func.c.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1249
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1250
case OP_CollSeq: {             /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1251
  assert( pOp->p3type==P3_COLLSEQ );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1252
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1253
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1254
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1255
/* Opcode: Function P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1256
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1257
** Invoke a user function (P3 is a pointer to a Function structure that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1258
** defines the function) with P2 arguments taken from the stack.  Pop all
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1259
** arguments from the stack and push back the result.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1260
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1261
** P1 is a 32-bit bitmask indicating whether or not each argument to the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1262
** function was determined to be constant at compile time. If the first
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1263
** argument was constant then bit 0 of P1 is set. This is used to determine
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1264
** whether meta data associated with a user function argument using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1265
** sqlite3_set_auxdata() API may be safely retained until the next
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1266
** invocation of this opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1267
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1268
** See also: AggStep and AggFinal
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1269
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1270
case OP_Function: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1271
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1272
  Mem *pArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1273
  sqlite3_context ctx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1274
  sqlite3_value **apVal;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1275
  int n = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1276
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1277
  apVal = p->apArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1278
  assert( apVal || n==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1279
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1280
  pArg = &pTos[1-n];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1281
  for(i=0; i<n; i++, pArg++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1282
    apVal[i] = pArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1283
    storeTypeInfo(pArg, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1284
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1285
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1286
  assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1287
  if( pOp->p3type==P3_FUNCDEF ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1288
    ctx.pFunc = (FuncDef*)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1289
    ctx.pVdbeFunc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1290
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1291
    ctx.pVdbeFunc = (VdbeFunc*)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1292
    ctx.pFunc = ctx.pVdbeFunc->pFunc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1293
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1294
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1295
  ctx.s.flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1296
  ctx.s.z = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1297
  ctx.s.xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1298
  ctx.s.db = db;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1299
  ctx.isError = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1300
  if( ctx.pFunc->needCollSeq ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1301
    assert( pOp>p->aOp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1302
    assert( pOp[-1].p3type==P3_COLLSEQ );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1303
    assert( pOp[-1].opcode==OP_CollSeq );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1304
    ctx.pColl = (CollSeq *)pOp[-1].p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1305
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1306
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1307
  (*ctx.pFunc->xFunc)(&ctx, n, apVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1308
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1309
  if( db->mallocFailed ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1310
    /* Even though a malloc() has failed, the implementation of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1311
    ** user function may have called an sqlite3_result_XXX() function
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1312
    ** to return a value. The following call releases any resources
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1313
    ** associated with such a value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1314
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1315
    ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1316
    ** fails also (the if(...) statement above). But if people are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1317
    ** misusing sqlite, they have bigger problems than a leaked value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1318
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1319
    sqlite3VdbeMemRelease(&ctx.s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1320
    goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1321
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1322
  popStack(&pTos, n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1323
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1324
  /* If any auxilary data functions have been called by this user function,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1325
  ** immediately call the destructor for any non-static values.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1326
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1327
  if( ctx.pVdbeFunc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1328
    sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1329
    pOp->p3 = (char *)ctx.pVdbeFunc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1330
    pOp->p3type = P3_VDBEFUNC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1331
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1332
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1333
  /* If the function returned an error, throw an exception */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1334
  if( ctx.isError ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1335
    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1336
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1337
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1338
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1339
  /* Copy the result of the function to the top of the stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1340
  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1341
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1342
  pTos->flags = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1343
  sqlite3VdbeMemMove(pTos, &ctx.s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1344
  if( sqlite3VdbeMemTooBig(pTos) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1345
    goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1346
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1347
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1348
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1349
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1350
/* Opcode: BitAnd * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1351
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1352
** Pop the top two elements from the stack.  Convert both elements
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1353
** to integers.  Push back onto the stack the bit-wise AND of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1354
** two elements.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1355
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1356
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1357
/* Opcode: BitOr * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1358
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1359
** Pop the top two elements from the stack.  Convert both elements
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1360
** to integers.  Push back onto the stack the bit-wise OR of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1361
** two elements.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1362
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1363
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1364
/* Opcode: ShiftLeft * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1365
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1366
** Pop the top two elements from the stack.  Convert both elements
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1367
** to integers.  Push back onto the stack the second element shifted
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1368
** left by N bits where N is the top element on the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1369
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1370
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1371
/* Opcode: ShiftRight * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1372
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1373
** Pop the top two elements from the stack.  Convert both elements
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1374
** to integers.  Push back onto the stack the second element shifted
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1375
** right by N bits where N is the top element on the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1376
** If either operand is NULL, the result is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1377
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1378
case OP_BitAnd:                 /* same as TK_BITAND, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1379
case OP_BitOr:                  /* same as TK_BITOR, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1380
case OP_ShiftLeft:              /* same as TK_LSHIFT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1381
case OP_ShiftRight: {           /* same as TK_RSHIFT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1382
  Mem *pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1383
  i64 a, b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1384
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1385
  assert( pNos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1386
  if( (pTos->flags | pNos->flags) & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1387
    popStack(&pTos, 2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1388
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1389
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1390
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1391
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1392
  a = sqlite3VdbeIntValue(pNos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1393
  b = sqlite3VdbeIntValue(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1394
  switch( pOp->opcode ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1395
    case OP_BitAnd:      a &= b;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1396
    case OP_BitOr:       a |= b;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1397
    case OP_ShiftLeft:   a <<= b;    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1398
    case OP_ShiftRight:  a >>= b;    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1399
    default:   /* CANT HAPPEN */     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1400
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1401
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1402
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1403
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1404
  pTos->u.i = a;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1405
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1406
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1407
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1408
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1409
/* Opcode: AddImm  P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1410
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1411
** Add the value P1 to whatever is on top of the stack.  The result
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1412
** is always an integer.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1413
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1414
** To force the top of the stack to be an integer, just add 0.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1415
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1416
case OP_AddImm: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1417
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1418
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1419
  pTos->u.i += pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1420
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1421
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1422
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1423
/* Opcode: ForceInt P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1424
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1425
** Convert the top of the stack into an integer.  If the current top of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1426
** the stack is not numeric (meaning that is is a NULL or a string that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1427
** does not look like an integer or floating point number) then pop the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1428
** stack and jump to P2.  If the top of the stack is numeric then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1429
** convert it into the least integer that is greater than or equal to its
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1430
** current value if P1==0, or to the least integer that is strictly
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1431
** greater than its current value if P1==1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1432
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1433
case OP_ForceInt: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1434
  i64 v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1435
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1436
  applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1437
  if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1438
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1439
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1440
    pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1441
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1442
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1443
  if( pTos->flags & MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1444
    v = pTos->u.i + (pOp->p1!=0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1445
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1446
    /* FIX ME:  should this not be assert( pTos->flags & MEM_Real ) ??? */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1447
    sqlite3VdbeMemRealify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1448
    v = (int)pTos->r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1449
    if( pTos->r>(double)v ) v++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1450
    if( pOp->p1 && pTos->r==(double)v ) v++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1451
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1452
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1453
  pTos->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1454
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1455
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1456
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1457
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1458
/* Opcode: MustBeInt P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1459
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1460
** Force the top of the stack to be an integer.  If the top of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1461
** stack is not an integer and cannot be converted into an integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1462
** without data loss, then jump immediately to P2, or if P2==0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1463
** raise an SQLITE_MISMATCH exception.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1464
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1465
** If the top of the stack is not an integer and P2 is not zero and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1466
** P1 is 1, then the stack is popped.  In all other cases, the depth
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1467
** of the stack is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1468
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1469
case OP_MustBeInt: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1470
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1471
  applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1472
  if( (pTos->flags & MEM_Int)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1473
    if( pOp->p2==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1474
      rc = SQLITE_MISMATCH;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1475
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1476
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1477
      if( pOp->p1 ) popStack(&pTos, 1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1478
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1479
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1480
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1481
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1482
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1483
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1484
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1485
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1486
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1487
/* Opcode: RealAffinity * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1488
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1489
** If the top of the stack is an integer, convert it to a real value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1490
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1491
** This opcode is used when extracting information from a column that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1492
** has REAL affinity.  Such column values may still be stored as
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1493
** integers, for space efficiency, but after extraction we want them
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1494
** to have only a real value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1495
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1496
case OP_RealAffinity: {                  /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1497
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1498
  if( pTos->flags & MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1499
    sqlite3VdbeMemRealify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1500
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1501
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1502
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1503
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1504
#ifndef SQLITE_OMIT_CAST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1505
/* Opcode: ToText * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1506
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1507
** Force the value on the top of the stack to be text.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1508
** If the value is numeric, convert it to a string using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1509
** equivalent of printf().  Blob values are unchanged and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1510
** are afterwards simply interpreted as text.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1511
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1512
** A NULL value is not changed by this routine.  It remains NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1513
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1514
case OP_ToText: {                  /* same as TK_TO_TEXT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1515
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1516
  if( pTos->flags & MEM_Null ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1517
  assert( MEM_Str==(MEM_Blob>>3) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1518
  pTos->flags |= (pTos->flags&MEM_Blob)>>3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1519
  applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1520
  rc = ExpandBlob(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1521
  assert( pTos->flags & MEM_Str );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1522
  pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1523
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1524
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1525
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1526
/* Opcode: ToBlob * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1527
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1528
** Force the value on the top of the stack to be a BLOB.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1529
** If the value is numeric, convert it to a string first.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1530
** Strings are simply reinterpreted as blobs with no change
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1531
** to the underlying data.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1532
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1533
** A NULL value is not changed by this routine.  It remains NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1534
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1535
case OP_ToBlob: {                  /* same as TK_TO_BLOB, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1536
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1537
  if( pTos->flags & MEM_Null ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1538
  if( (pTos->flags & MEM_Blob)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1539
    applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1540
    assert( pTos->flags & MEM_Str );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1541
    pTos->flags |= MEM_Blob;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1542
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1543
  pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1544
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1545
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1546
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1547
/* Opcode: ToNumeric * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1548
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1549
** Force the value on the top of the stack to be numeric (either an
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1550
** integer or a floating-point number.)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1551
** If the value is text or blob, try to convert it to an using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1552
** equivalent of atoi() or atof() and store 0 if no such conversion 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1553
** is possible.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1554
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1555
** A NULL value is not changed by this routine.  It remains NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1556
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1557
case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1558
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1559
  if( (pTos->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1560
    sqlite3VdbeMemNumerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1561
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1562
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1563
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1564
#endif /* SQLITE_OMIT_CAST */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1565
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1566
/* Opcode: ToInt * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1567
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1568
** Force the value on the top of the stack to be an integer.  If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1569
** The value is currently a real number, drop its fractional part.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1570
** If the value is text or blob, try to convert it to an integer using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1571
** equivalent of atoi() and store 0 if no such conversion is possible.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1572
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1573
** A NULL value is not changed by this routine.  It remains NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1574
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1575
case OP_ToInt: {                  /* same as TK_TO_INT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1576
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1577
  if( (pTos->flags & MEM_Null)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1578
    sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1579
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1580
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1581
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1582
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1583
#ifndef SQLITE_OMIT_CAST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1584
/* Opcode: ToReal * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1585
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1586
** Force the value on the top of the stack to be a floating point number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1587
** If The value is currently an integer, convert it.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1588
** If the value is text or blob, try to convert it to an integer using the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1589
** equivalent of atoi() and store 0 if no such conversion is possible.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1590
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1591
** A NULL value is not changed by this routine.  It remains NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1592
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1593
case OP_ToReal: {                  /* same as TK_TO_REAL, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1594
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1595
  if( (pTos->flags & MEM_Null)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1596
    sqlite3VdbeMemRealify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1597
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1598
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1599
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1600
#endif /* SQLITE_OMIT_CAST */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1601
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1602
/* Opcode: Eq P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1603
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1604
** Pop the top two elements from the stack.  If they are equal, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1605
** jump to instruction P2.  Otherwise, continue to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1606
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1607
** If the 0x100 bit of P1 is true and either operand is NULL then take the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1608
** jump.  If the 0x100 bit of P1 is clear then fall thru if either operand
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1609
** is NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1610
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1611
** If the 0x200 bit of P1 is set and either operand is NULL then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1612
** both operands are converted to integers prior to comparison.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1613
** NULL operands are converted to zero and non-NULL operands are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1614
** converted to 1.  Thus, for example, with 0x200 set,  NULL==NULL is true
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1615
** whereas it would normally be NULL.  Similarly,  NULL==123 is false when
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1616
** 0x200 is set but is NULL when the 0x200 bit of P1 is clear.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1617
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1618
** The least significant byte of P1 (mask 0xff) must be an affinity character -
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1619
** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1620
** to coerce both values
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1621
** according to the affinity before the comparison is made. If the byte is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1622
** 0x00, then numeric affinity is used.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1623
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1624
** Once any conversions have taken place, and neither value is NULL, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1625
** the values are compared. If both values are blobs, or both are text,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1626
** then memcmp() is used to determine the results of the comparison. If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1627
** both values are numeric, then a numeric comparison is used. If the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1628
** two values are of different types, then they are inequal.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1629
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1630
** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1631
** stack if the jump would have been taken, or a 0 if not.  Push a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1632
** NULL if either operand was NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1633
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1634
** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1635
** structure) that defines how to compare text.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1636
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1637
/* Opcode: Ne P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1638
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1639
** This works just like the Eq opcode except that the jump is taken if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1640
** the operands from the stack are not equal.  See the Eq opcode for
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1641
** additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1642
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1643
/* Opcode: Lt P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1644
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1645
** This works just like the Eq opcode except that the jump is taken if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1646
** the 2nd element down on the stack is less than the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1647
** See the Eq opcode for additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1648
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1649
/* Opcode: Le P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1650
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1651
** This works just like the Eq opcode except that the jump is taken if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1652
** the 2nd element down on the stack is less than or equal to the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1653
** top of the stack.  See the Eq opcode for additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1654
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1655
/* Opcode: Gt P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1656
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1657
** This works just like the Eq opcode except that the jump is taken if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1658
** the 2nd element down on the stack is greater than the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1659
** See the Eq opcode for additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1660
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1661
/* Opcode: Ge P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1662
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1663
** This works just like the Eq opcode except that the jump is taken if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1664
** the 2nd element down on the stack is greater than or equal to the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1665
** top of the stack.  See the Eq opcode for additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1666
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1667
case OP_Eq:               /* same as TK_EQ, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1668
case OP_Ne:               /* same as TK_NE, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1669
case OP_Lt:               /* same as TK_LT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1670
case OP_Le:               /* same as TK_LE, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1671
case OP_Gt:               /* same as TK_GT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1672
case OP_Ge: {             /* same as TK_GE, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1673
  Mem *pNos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1674
  int flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1675
  int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1676
  char affinity;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1677
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1678
  pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1679
  flags = pTos->flags|pNos->flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1680
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1681
  /* If either value is a NULL P2 is not zero, take the jump if the least
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1682
  ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1683
  ** the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1684
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1685
  if( flags&MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1686
    if( (pOp->p1 & 0x200)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1687
      /* The 0x200 bit of P1 means, roughly "do not treat NULL as the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1688
      ** magic SQL value it normally is - treat it as if it were another
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1689
      ** integer".
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1690
      **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1691
      ** With 0x200 set, if either operand is NULL then both operands
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1692
      ** are converted to integers prior to being passed down into the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1693
      ** normal comparison logic below.  NULL operands are converted to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1694
      ** zero and non-NULL operands are converted to 1.  Thus, for example,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1695
      ** with 0x200 set,  NULL==NULL is true whereas it would normally
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1696
      ** be NULL.  Similarly,  NULL!=123 is true.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1697
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1698
      sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1699
      sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1700
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1701
      /* If the 0x200 bit of P1 is clear and either operand is NULL then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1702
      ** the result is always NULL.  The jump is taken if the 0x100 bit
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1703
      ** of P1 is set.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1704
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1705
      popStack(&pTos, 2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1706
      if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1707
        if( pOp->p1 & 0x100 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1708
          pc = pOp->p2-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1709
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1710
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1711
        pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1712
        pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1713
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1714
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1715
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1716
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1717
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1718
  affinity = pOp->p1 & 0xFF;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1719
  if( affinity ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1720
    applyAffinity(pNos, affinity, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1721
    applyAffinity(pTos, affinity, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1722
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1723
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1724
  assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1725
  ExpandBlob(pNos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1726
  ExpandBlob(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1727
  res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1728
  switch( pOp->opcode ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1729
    case OP_Eq:    res = res==0;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1730
    case OP_Ne:    res = res!=0;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1731
    case OP_Lt:    res = res<0;      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1732
    case OP_Le:    res = res<=0;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1733
    case OP_Gt:    res = res>0;      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1734
    default:       res = res>=0;     break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1735
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1736
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1737
  popStack(&pTos, 2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1738
  if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1739
    if( res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1740
      pc = pOp->p2-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1741
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1742
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1743
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1744
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1745
    pTos->u.i = res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1746
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1747
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1748
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1749
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1750
/* Opcode: And * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1751
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1752
** Pop two values off the stack.  Take the logical AND of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1753
** two values and push the resulting boolean value back onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1754
** stack. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1755
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1756
/* Opcode: Or * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1757
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1758
** Pop two values off the stack.  Take the logical OR of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1759
** two values and push the resulting boolean value back onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1760
** stack. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1761
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1762
case OP_And:              /* same as TK_AND, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1763
case OP_Or: {             /* same as TK_OR, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1764
  Mem *pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1765
  int v1, v2;    /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1766
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1767
  assert( pNos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1768
  if( pTos->flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1769
    v1 = 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1770
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1771
    sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1772
    v1 = pTos->u.i==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1773
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1774
  if( pNos->flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1775
    v2 = 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1776
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1777
    sqlite3VdbeMemIntegerify(pNos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1778
    v2 = pNos->u.i==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1779
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1780
  if( pOp->opcode==OP_And ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1781
    static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1782
    v1 = and_logic[v1*3+v2];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1783
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1784
    static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1785
    v1 = or_logic[v1*3+v2];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1786
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1787
  popStack(&pTos, 2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1788
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1789
  if( v1==2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1790
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1791
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1792
    pTos->u.i = v1==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1793
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1794
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1795
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1796
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1797
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1798
/* Opcode: Negative * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1799
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1800
** Treat the top of the stack as a numeric quantity.  Replace it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1801
** with its additive inverse.  If the top of the stack is NULL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1802
** its value is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1803
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1804
/* Opcode: AbsValue * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1805
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1806
** Treat the top of the stack as a numeric quantity.  Replace it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1807
** with its absolute value. If the top of the stack is NULL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1808
** its value is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1809
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1810
case OP_Negative:              /* same as TK_UMINUS, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1811
case OP_AbsValue: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1812
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1813
  if( (pTos->flags & (MEM_Real|MEM_Int|MEM_Null))==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1814
    sqlite3VdbeMemNumerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1815
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1816
  if( pTos->flags & MEM_Real ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1817
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1818
    if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1819
      pTos->r = -pTos->r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1820
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1821
    pTos->flags = MEM_Real;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1822
  }else if( pTos->flags & MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1823
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1824
    if( pOp->opcode==OP_Negative || pTos->u.i<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1825
      pTos->u.i = -pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1826
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1827
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1828
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1829
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1830
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1831
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1832
/* Opcode: Not * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1833
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1834
** Interpret the top of the stack as a boolean value.  Replace it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1835
** with its complement.  If the top of the stack is NULL its value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1836
** is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1837
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1838
case OP_Not: {                /* same as TK_NOT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1839
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1840
  if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1841
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1842
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1843
  pTos->u.i = !pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1844
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1845
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1846
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1847
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1848
/* Opcode: BitNot * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1849
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1850
** Interpret the top of the stack as an value.  Replace it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1851
** with its ones-complement.  If the top of the stack is NULL its
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1852
** value is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1853
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1854
case OP_BitNot: {             /* same as TK_BITNOT, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1855
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1856
  if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1857
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1858
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1859
  pTos->u.i = ~pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1860
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1861
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1862
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1863
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1864
/* Opcode: Noop * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1865
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1866
** Do nothing.  This instruction is often useful as a jump
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1867
** destination.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1868
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1869
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1870
** The magic Explain opcode are only inserted when explain==2 (which
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1871
** is to say when the EXPLAIN QUERY PLAN syntax is used.)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1872
** This opcode records information from the optimizer.  It is the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1873
** the same as a no-op.  This opcodesnever appears in a real VM program.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1874
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1875
case OP_Explain:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1876
case OP_Noop: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1877
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1878
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1879
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1880
/* Opcode: If P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1881
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1882
** Pop a single boolean from the stack.  If the boolean popped is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1883
** true, then jump to p2.  Otherwise continue to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1884
** An integer is false if zero and true otherwise.  A string is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1885
** false if it has zero length and true otherwise.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1886
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1887
** If the value popped of the stack is NULL, then take the jump if P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1888
** is true and fall through if P1 is false.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1889
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1890
/* Opcode: IfNot P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1891
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1892
** Pop a single boolean from the stack.  If the boolean popped is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1893
** false, then jump to p2.  Otherwise continue to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1894
** An integer is false if zero and true otherwise.  A string is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1895
** false if it has zero length and true otherwise.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1896
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1897
** If the value popped of the stack is NULL, then take the jump if P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1898
** is true and fall through if P1 is false.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1899
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1900
case OP_If:                 /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1901
case OP_IfNot: {            /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1902
  int c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1903
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1904
  if( pTos->flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1905
    c = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1906
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1907
#ifdef SQLITE_OMIT_FLOATING_POINT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1908
    c = sqlite3VdbeIntValue(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1909
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1910
    c = sqlite3VdbeRealValue(pTos)!=0.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1911
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1912
    if( pOp->opcode==OP_IfNot ) c = !c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1913
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1914
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1915
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1916
  if( c ) pc = pOp->p2-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1917
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1918
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1919
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1920
/* Opcode: IsNull P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1921
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1922
** Check the top of the stack and jump to P2 if the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1923
** is NULL.  If P1 is positive, then pop P1 elements from the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1924
** regardless of whether or not the jump is taken.  If P1 is negative,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1925
** pop -P1 elements from the stack only if the jump is taken and leave
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1926
** the stack unchanged if the jump is not taken.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1927
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1928
case OP_IsNull: {            /* same as TK_ISNULL, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1929
  if( pTos->flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1930
    pc = pOp->p2-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1931
    if( pOp->p1<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1932
      popStack(&pTos, -pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1933
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1934
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1935
  if( pOp->p1>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1936
    popStack(&pTos, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1937
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1938
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1939
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1940
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1941
/* Opcode: NotNull P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1942
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1943
** Jump to P2 if the top abs(P1) values on the stack are all not NULL.  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1944
** Regardless of whether or not the jump is taken, pop the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1945
** P1 times if P1 is greater than zero.  But if P1 is negative,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1946
** leave the stack unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1947
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1948
case OP_NotNull: {            /* same as TK_NOTNULL, no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1949
  int i, cnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1950
  cnt = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1951
  if( cnt<0 ) cnt = -cnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1952
  assert( &pTos[1-cnt] >= p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1953
  for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1954
  if( i>=cnt ) pc = pOp->p2-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1955
  if( pOp->p1>0 ) popStack(&pTos, cnt);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1956
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1957
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1958
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1959
/* Opcode: SetNumColumns P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1960
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1961
** Before the OP_Column opcode can be executed on a cursor, this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1962
** opcode must be called to set the number of fields in the table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1963
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1964
** This opcode sets the number of columns for cursor P1 to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1965
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1966
** If OP_KeyAsData is to be applied to cursor P1, it must be executed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1967
** before this op-code.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1968
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1969
case OP_SetNumColumns: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1970
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1971
  assert( (pOp->p1)<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1972
  assert( p->apCsr[pOp->p1]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1973
  pC = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1974
  pC->nField = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1975
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1976
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1977
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1978
/* Opcode: Column P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1979
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1980
** Interpret the data that cursor P1 points to as a structure built using
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1981
** the MakeRecord instruction.  (See the MakeRecord opcode for additional
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1982
** information about the format of the data.) Push onto the stack the value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1983
** of the P2-th column contained in the data. If there are less that (P2+1) 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1984
** values in the record, push a NULL onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1985
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1986
** If the KeyAsData opcode has previously executed on this cursor, then the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1987
** field might be extracted from the key rather than the data.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1988
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1989
** If the column contains fewer than P2 fields, then push a NULL.  Or
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1990
** if P3 is of type P3_MEM, then push the P3 value.  The P3 value will
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1991
** be default value for a column that has been added using the ALTER TABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1992
** ADD COLUMN command.  If P3 is an ordinary string, just push a NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1993
** When P3 is a string it is really just a comment describing the value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1994
** to be pushed, not a default value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1995
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1996
case OP_Column: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1997
  u32 payloadSize;   /* Number of bytes in the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1998
  int p1 = pOp->p1;  /* P1 value of the opcode */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1999
  int p2 = pOp->p2;  /* column number to retrieve */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2000
  Cursor *pC = 0;    /* The VDBE cursor */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2001
  char *zRec;        /* Pointer to complete record-data */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2002
  BtCursor *pCrsr;   /* The BTree cursor */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2003
  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2004
  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2005
  u32 nField;        /* number of fields in the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2006
  int len;           /* The length of the serialized data for the column */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2007
  int i;             /* Loop counter */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2008
  char *zData;       /* Part of the record being decoded */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2009
  Mem sMem;          /* For storing the record being decoded */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2010
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2011
  sMem.flags = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2012
  assert( p1<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2013
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2014
  pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2015
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2016
  /* This block sets the variable payloadSize to be the total number of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2017
  ** bytes in the record.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2018
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2019
  ** zRec is set to be the complete text of the record if it is available.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2020
  ** The complete record text is always available for pseudo-tables
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2021
  ** If the record is stored in a cursor, the complete record text
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2022
  ** might be available in the  pC->aRow cache.  Or it might not be.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2023
  ** If the data is unavailable,  zRec is set to NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2024
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2025
  ** We also compute the number of columns in the record.  For cursors,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2026
  ** the number of columns is stored in the Cursor.nField element.  For
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2027
  ** records on the stack, the next entry down on the stack is an integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2028
  ** which is the number of records.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2029
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2030
  pC = p->apCsr[p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2031
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2032
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2033
  assert( pC->pVtabCursor==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2034
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2035
  if( pC->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2036
    /* The record is stored in a B-Tree */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2037
    rc = sqlite3VdbeCursorMoveto(pC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2038
    if( rc ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2039
    zRec = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2040
    pCrsr = pC->pCursor;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2041
    if( pC->nullRow ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2042
      payloadSize = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2043
    }else if( pC->cacheStatus==p->cacheCtr ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2044
      payloadSize = pC->payloadSize;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2045
      zRec = (char*)pC->aRow;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2046
    }else if( pC->isIndex ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2047
      i64 payloadSize64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2048
      sqlite3BtreeKeySize(pCrsr, &payloadSize64);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2049
      payloadSize = payloadSize64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2050
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2051
      sqlite3BtreeDataSize(pCrsr, &payloadSize);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2052
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2053
    nField = pC->nField;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2054
  }else if( pC->pseudoTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2055
    /* The record is the sole entry of a pseudo-table */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2056
    payloadSize = pC->nData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2057
    zRec = pC->pData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2058
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2059
    assert( payloadSize==0 || zRec!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2060
    nField = pC->nField;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2061
    pCrsr = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2062
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2063
    zRec = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2064
    payloadSize = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2065
    pCrsr = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2066
    nField = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2067
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2068
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2069
  /* If payloadSize is 0, then just push a NULL onto the stack. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2070
  if( payloadSize==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2071
    assert( pTos->flags==MEM_Null );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2072
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2073
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2074
  if( payloadSize>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2075
    goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2076
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2077
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2078
  assert( p2<nField );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2079
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2080
  /* Read and parse the table header.  Store the results of the parse
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2081
  ** into the record header cache fields of the cursor.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2082
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2083
  if( pC && pC->cacheStatus==p->cacheCtr ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2084
    aType = pC->aType;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2085
    aOffset = pC->aOffset;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2086
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2087
    u8 *zIdx;        /* Index into header */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2088
    u8 *zEndHdr;     /* Pointer to first byte after the header */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2089
    u32 offset;      /* Offset into the data */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2090
    int szHdrSz;     /* Size of the header size field at start of record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2091
    int avail;       /* Number of bytes of available data */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2092
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2093
    aType = pC->aType;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2094
    if( aType==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2095
      pC->aType = aType = (u32*)sqlite3DbMallocRaw(db, 2*nField*sizeof(aType) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2096
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2097
    if( aType==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2098
      goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2099
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2100
    pC->aOffset = aOffset = &aType[nField];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2101
    pC->payloadSize = payloadSize;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2102
    pC->cacheStatus = p->cacheCtr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2103
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2104
    /* Figure out how many bytes are in the header */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2105
    if( zRec ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2106
      zData = zRec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2107
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2108
      if( pC->isIndex ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2109
        zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2110
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2111
        zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2112
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2113
      /* If KeyFetch()/DataFetch() managed to get the entire payload,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2114
      ** save the payload in the pC->aRow cache.  That will save us from
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2115
      ** having to make additional calls to fetch the content portion of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2116
      ** the record.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2117
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2118
      if( avail>=payloadSize ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2119
        zRec = zData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2120
        pC->aRow = (u8*)zData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2121
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2122
        pC->aRow = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2123
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2124
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2125
    /* The following assert is true in all cases accept when
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2126
    ** the database file has been corrupted externally.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2127
    **    assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2128
    szHdrSz = GetVarint((u8*)zData, offset);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2129
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2130
    /* The KeyFetch() or DataFetch() above are fast and will get the entire
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2131
    ** record header in most cases.  But they will fail to get the complete
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2132
    ** record header if the record header does not fit on a single page
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2133
    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2134
    ** acquire the complete header text.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2135
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2136
    if( !zRec && avail<offset ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2137
      rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2138
      if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2139
        goto op_column_out;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2140
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2141
      zData = sMem.z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2142
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2143
    zEndHdr = (u8 *)&zData[offset];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2144
    zIdx = (u8 *)&zData[szHdrSz];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2145
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2146
    /* Scan the header and use it to fill in the aType[] and aOffset[]
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2147
    ** arrays.  aType[i] will contain the type integer for the i-th
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2148
    ** column and aOffset[i] will contain the offset from the beginning
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2149
    ** of the record to the start of the data for the i-th column
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2150
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2151
    for(i=0; i<nField; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2152
      if( zIdx<zEndHdr ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2153
        aOffset[i] = offset;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2154
        zIdx += GetVarint(zIdx, aType[i]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2155
        offset += sqlite3VdbeSerialTypeLen(aType[i]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2156
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2157
        /* If i is less that nField, then there are less fields in this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2158
        ** record than SetNumColumns indicated there are columns in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2159
        ** table. Set the offset for any extra columns not present in
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2160
        ** the record to 0. This tells code below to push a NULL onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2161
        ** stack instead of deserializing a value from the record.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2162
        */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2163
        aOffset[i] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2164
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2165
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2166
    Release(&sMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2167
    sMem.flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2168
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2169
    /* If we have read more header data than was contained in the header,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2170
    ** or if the end of the last field appears to be past the end of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2171
    ** record, then we must be dealing with a corrupt database.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2172
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2173
    if( zIdx>zEndHdr || offset>payloadSize ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2174
      rc = SQLITE_CORRUPT_BKPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2175
      goto op_column_out;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2176
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2177
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2178
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2179
  /* Get the column information. If aOffset[p2] is non-zero, then 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2180
  ** deserialize the value from the record. If aOffset[p2] is zero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2181
  ** then there are not enough fields in the record to satisfy the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2182
  ** request.  In this case, set the value NULL or to P3 if P3 is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2183
  ** a pointer to a Mem object.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2184
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2185
  if( aOffset[p2] ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2186
    assert( rc==SQLITE_OK );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2187
    if( zRec ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2188
      zData = &zRec[aOffset[p2]];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2189
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2190
      len = sqlite3VdbeSerialTypeLen(aType[p2]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2191
      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2192
      if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2193
        goto op_column_out;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2194
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2195
      zData = sMem.z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2196
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2197
    sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2198
    pTos->enc = encoding;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2199
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2200
    if( pOp->p3type==P3_MEM ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2201
      sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2202
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2203
      pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2204
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2205
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2206
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2207
  /* If we dynamically allocated space to hold the data (in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2208
  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2209
  ** dynamically allocated space over to the pTos structure.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2210
  ** This prevents a memory copy.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2211
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2212
  if( (sMem.flags & MEM_Dyn)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2213
    assert( pTos->flags & MEM_Ephem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2214
    assert( pTos->flags & (MEM_Str|MEM_Blob) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2215
    assert( pTos->z==sMem.z );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2216
    assert( sMem.flags & MEM_Term );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2217
    pTos->flags &= ~MEM_Ephem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2218
    pTos->flags |= MEM_Dyn|MEM_Term;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2219
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2220
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2221
  /* pTos->z might be pointing to sMem.zShort[].  Fix that so that we
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2222
  ** can abandon sMem */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2223
  rc = sqlite3VdbeMemMakeWriteable(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2224
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2225
op_column_out:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2226
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2227
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2228
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2229
/* Opcode: MakeRecord P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2230
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2231
** Convert the top abs(P1) entries of the stack into a single entry
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2232
** suitable for use as a data record in a database table or as a key
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2233
** in an index.  The details of the format are irrelavant as long as
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2234
** the OP_Column opcode can decode the record later and as long as the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2235
** sqlite3VdbeRecordCompare function will correctly compare two encoded
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2236
** records.  Refer to source code comments for the details of the record
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2237
** format.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2238
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2239
** The original stack entries are popped from the stack if P1>0 but
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2240
** remain on the stack if P1<0.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2241
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2242
** If P2 is not zero and one or more of the entries are NULL, then jump
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2243
** to the address given by P2.  This feature can be used to skip a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2244
** uniqueness test on indices.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2245
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2246
** P3 may be a string that is P1 characters long.  The nth character of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2247
** string indicates the column affinity that should be used for the nth
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2248
** field of the index key (i.e. the first character of P3 corresponds to the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2249
** lowest element on the stack).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2250
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2251
** The mapping from character to affinity is given by the SQLITE_AFF_
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2252
** macros defined in sqliteInt.h.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2253
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2254
** If P3 is NULL then all index fields have the affinity NONE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2255
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2256
** See also OP_MakeIdxRec
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2257
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2258
/* Opcode: MakeIdxRec P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2259
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2260
** This opcode works just OP_MakeRecord except that it reads an extra
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2261
** integer from the stack (thus reading a total of abs(P1+1) entries)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2262
** and appends that extra integer to the end of the record as a varint.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2263
** This results in an index key.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2264
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2265
case OP_MakeIdxRec:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2266
case OP_MakeRecord: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2267
  /* Assuming the record contains N fields, the record format looks
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2268
  ** like this:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2269
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2270
  ** ------------------------------------------------------------------------
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2271
  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2272
  ** ------------------------------------------------------------------------
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2273
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2274
  ** Data(0) is taken from the lowest element of the stack and data(N-1) is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2275
  ** the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2276
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2277
  ** Each type field is a varint representing the serial type of the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2278
  ** corresponding data element (see sqlite3VdbeSerialType()). The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2279
  ** hdr-size field is also a varint which is the offset from the beginning
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2280
  ** of the record to data0.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2281
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2282
  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2283
  Mem *pRec;             /* The new record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2284
  Mem *pRowid = 0;       /* Rowid appended to the new record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2285
  u64 nData = 0;         /* Number of bytes of data space */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2286
  int nHdr = 0;          /* Number of bytes of header space */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2287
  u64 nByte = 0;         /* Data space required for this record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2288
  int nZero = 0;         /* Number of zero bytes at the end of the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2289
  int nVarint;           /* Number of bytes in a varint */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2290
  u32 serial_type;       /* Type field */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2291
  int containsNull = 0;  /* True if any of the data fields are NULL */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2292
  Mem *pData0;           /* Bottom of the stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2293
  int leaveOnStack;      /* If true, leave the entries on the stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2294
  int nField;            /* Number of fields in the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2295
  int jumpIfNull;        /* Jump here if non-zero and any entries are NULL. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2296
  int addRowid;          /* True to append a rowid column at the end */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2297
  char *zAffinity;       /* The affinity string for the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2298
  int file_format;       /* File format to use for encoding */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2299
  int i;                 /* Space used in zNewRecord[] */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2300
  char zTemp[NBFS];      /* Space to hold small records */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2301
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2302
  leaveOnStack = ((pOp->p1<0)?1:0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2303
  nField = pOp->p1 * (leaveOnStack?-1:1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2304
  jumpIfNull = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2305
  addRowid = pOp->opcode==OP_MakeIdxRec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2306
  zAffinity = pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2307
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2308
  pData0 = &pTos[1-nField];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2309
  assert( pData0>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2310
  containsNull = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2311
  file_format = p->minWriteFileFormat;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2312
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2313
  /* Loop through the elements that will make up the record to figure
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2314
  ** out how much space is required for the new record.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2315
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2316
  for(pRec=pData0; pRec<=pTos; pRec++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2317
    int len;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2318
    if( zAffinity ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2319
      applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2320
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2321
    if( pRec->flags&MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2322
      containsNull = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2323
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2324
    if( pRec->flags&MEM_Zero && pRec->n>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2325
      ExpandBlob(pRec);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2326
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2327
    serial_type = sqlite3VdbeSerialType(pRec, file_format);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2328
    len = sqlite3VdbeSerialTypeLen(serial_type);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2329
    nData += len;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2330
    nHdr += sqlite3VarintLen(serial_type);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2331
    if( pRec->flags & MEM_Zero ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2332
      /* Only pure zero-filled BLOBs can be input to this Opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2333
      ** We do not allow blobs with a prefix and a zero-filled tail. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2334
      nZero += pRec->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2335
    }else if( len ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2336
      nZero = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2337
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2338
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2339
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2340
  /* If we have to append a varint rowid to this record, set pRowid
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2341
  ** to the value of the rowid and increase nByte by the amount of space
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2342
  ** required to store it.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2343
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2344
  if( addRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2345
    pRowid = &pTos[0-nField];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2346
    assert( pRowid>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2347
    sqlite3VdbeMemIntegerify(pRowid);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2348
    serial_type = sqlite3VdbeSerialType(pRowid, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2349
    nData += sqlite3VdbeSerialTypeLen(serial_type);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2350
    nHdr += sqlite3VarintLen(serial_type);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2351
    nZero = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2352
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2353
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2354
  /* Add the initial header varint and total the size */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2355
  nHdr += nVarint = sqlite3VarintLen(nHdr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2356
  if( nVarint<sqlite3VarintLen(nHdr) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2357
    nHdr++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2358
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2359
  nByte = nHdr+nData-nZero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2360
  if( nByte>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2361
    goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2362
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2363
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2364
  /* Allocate space for the new record. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2365
  if( nByte>sizeof(zTemp) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2366
    zNewRecord = (u8*)sqlite3DbMallocRaw(db, nByte);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2367
    if( !zNewRecord ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2368
      goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2369
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2370
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2371
    zNewRecord = (u8*)zTemp;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2372
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2373
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2374
  /* Write the record */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2375
  i = sqlite3PutVarint(zNewRecord, nHdr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2376
  for(pRec=pData0; pRec<=pTos; pRec++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2377
    serial_type = sqlite3VdbeSerialType(pRec, file_format);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2378
    i += sqlite3PutVarint(&zNewRecord[i], serial_type);      /* serial type */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2379
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2380
  if( addRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2381
    i += sqlite3PutVarint(&zNewRecord[i], sqlite3VdbeSerialType(pRowid, 0));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2382
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2383
  for(pRec=pData0; pRec<=pTos; pRec++){  /* serial data */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2384
    i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2385
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2386
  if( addRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2387
    i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRowid, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2388
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2389
  assert( i==nByte );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2390
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2391
  /* Pop entries off the stack if required. Push the new record on. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2392
  if( !leaveOnStack ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2393
    popStack(&pTos, nField+addRowid);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2394
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2395
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2396
  pTos->n = nByte;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2397
  if( nByte<=sizeof(zTemp) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2398
    assert( zNewRecord==(unsigned char *)zTemp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2399
    pTos->z = pTos->zShort;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2400
    memcpy(pTos->zShort, zTemp, nByte);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2401
    pTos->flags = MEM_Blob | MEM_Short;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2402
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2403
    assert( zNewRecord!=(unsigned char *)zTemp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2404
    pTos->z = (char*)zNewRecord;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2405
    pTos->flags = MEM_Blob | MEM_Dyn;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2406
    pTos->xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2407
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2408
  if( nZero ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2409
    pTos->u.i = nZero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2410
    pTos->flags |= MEM_Zero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2411
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2412
  pTos->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2413
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2414
  /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2415
  if( jumpIfNull && containsNull ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2416
    pc = jumpIfNull - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2417
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2418
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2419
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2420
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2421
/* Opcode: Statement P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2422
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2423
** Begin an individual statement transaction which is part of a larger
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2424
** BEGIN..COMMIT transaction.  This is needed so that the statement
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2425
** can be rolled back after an error without having to roll back the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2426
** entire transaction.  The statement transaction will automatically
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2427
** commit when the VDBE halts.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2428
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2429
** The statement is begun on the database file with index P1.  The main
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2430
** database file has an index of 0 and the file used for temporary tables
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2431
** has an index of 1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2432
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2433
case OP_Statement: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2434
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2435
  Btree *pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2436
  if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2437
        && (db->autoCommit==0 || db->activeVdbeCnt>1) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2438
    assert( sqlite3BtreeIsInTrans(pBt) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2439
    assert( (p->btreeMask & (1<<i))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2440
    if( !sqlite3BtreeIsInStmt(pBt) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2441
      rc = sqlite3BtreeBeginStmt(pBt);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2442
      p->openedStatement = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2443
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2444
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2445
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2446
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2447
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2448
/* Opcode: AutoCommit P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2449
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2450
** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2451
** back any currently active btree transactions. If there are any active
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2452
** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2453
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2454
** This instruction causes the VM to halt.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2455
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2456
case OP_AutoCommit: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2457
  u8 i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2458
  u8 rollback = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2459
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2460
  assert( i==1 || i==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2461
  assert( i==1 || rollback==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2462
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2463
  assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2464
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2465
  if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2466
    /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2467
    ** still running, and a transaction is active, return an error indicating
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2468
    ** that the other VMs must complete first. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2469
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2470
    sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2471
        " transaction - SQL statements in progress", (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2472
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2473
  }else if( i!=db->autoCommit ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2474
    if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2475
      assert( i==1 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2476
      sqlite3RollbackAll(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2477
      db->autoCommit = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2478
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2479
      db->autoCommit = i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2480
      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2481
        p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2482
        p->pc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2483
        db->autoCommit = 1-i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2484
        p->rc = rc = SQLITE_BUSY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2485
        goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2486
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2487
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2488
    if( p->rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2489
      rc = SQLITE_DONE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2490
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2491
      rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2492
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2493
    goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2494
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2495
    sqlite3SetString(&p->zErrMsg,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2496
        (!i)?"cannot start a transaction within a transaction":(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2497
        (rollback)?"cannot rollback - no transaction is active":
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2498
                   "cannot commit - no transaction is active"), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2499
         
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2500
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2501
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2502
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2503
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2504
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2505
/* Opcode: Transaction P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2506
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2507
** Begin a transaction.  The transaction ends when a Commit or Rollback
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2508
** opcode is encountered.  Depending on the ON CONFLICT setting, the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2509
** transaction might also be rolled back if an error is encountered.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2510
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2511
** P1 is the index of the database file on which the transaction is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2512
** started.  Index 0 is the main database file and index 1 is the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2513
** file used for temporary tables.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2514
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2515
** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2516
** obtained on the database file when a write-transaction is started.  No
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2517
** other process can start another write transaction while this transaction is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2518
** underway.  Starting a write transaction also creates a rollback journal. A
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2519
** write transaction must be started before any changes can be made to the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2520
** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2521
** on the file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2522
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2523
** If P2 is zero, then a read-lock is obtained on the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2524
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2525
case OP_Transaction: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2526
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2527
  Btree *pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2528
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2529
  assert( i>=0 && i<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2530
  assert( (p->btreeMask & (1<<i))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2531
  pBt = db->aDb[i].pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2532
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2533
  if( pBt ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2534
    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2535
    if( rc==SQLITE_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2536
      p->pc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2537
      p->rc = rc = SQLITE_BUSY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2538
      p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2539
      goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2540
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2541
    if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2542
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2543
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2544
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2545
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2546
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2547
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2548
/* Opcode: ReadCookie P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2549
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2550
** Read cookie number P2 from database P1 and push it onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2551
** P2==0 is the schema version.  P2==1 is the database format.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2552
** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2553
** the main database file and P1==1 is the database file used to store
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2554
** temporary tables.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2555
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2556
** If P1 is negative, then this is a request to read the size of a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2557
** databases free-list. P2 must be set to 1 in this case. The actual
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2558
** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2559
** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2560
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2561
** There must be a read-lock on the database (either a transaction
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2562
** must be started or there must be an open cursor) before
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2563
** executing this instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2564
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2565
case OP_ReadCookie: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2566
  int iMeta;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2567
  int iDb = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2568
  int iCookie = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2569
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2570
  assert( pOp->p2<SQLITE_N_BTREE_META );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2571
  if( iDb<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2572
    iDb = (-1*(iDb+1));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2573
    iCookie *= -1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2574
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2575
  assert( iDb>=0 && iDb<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2576
  assert( db->aDb[iDb].pBt!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2577
  assert( (p->btreeMask & (1<<iDb))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2578
  /* The indexing of meta values at the schema layer is off by one from
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2579
  ** the indexing in the btree layer.  The btree considers meta[0] to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2580
  ** be the number of free pages in the database (a read-only value)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2581
  ** and meta[1] to be the schema cookie.  The schema layer considers
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2582
  ** meta[1] to be the schema cookie.  So we have to shift the index
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2583
  ** by one in the following statement.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2584
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2585
  rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2586
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2587
  pTos->u.i = iMeta;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2588
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2589
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2590
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2591
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2592
/* Opcode: SetCookie P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2593
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2594
** Write the top of the stack into cookie number P2 of database P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2595
** P2==0 is the schema version.  P2==1 is the database format.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2596
** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2597
** the main database file and P1==1 is the database file used to store
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2598
** temporary tables.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2599
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2600
** A transaction must be started before executing this opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2601
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2602
case OP_SetCookie: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2603
  Db *pDb;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2604
  assert( pOp->p2<SQLITE_N_BTREE_META );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2605
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2606
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2607
  pDb = &db->aDb[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2608
  assert( pDb->pBt!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2609
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2610
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2611
  /* See note about index shifting on OP_ReadCookie */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2612
  rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2613
  if( pOp->p2==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2614
    /* When the schema cookie changes, record the new cookie internally */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2615
    pDb->pSchema->schema_cookie = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2616
    db->flags |= SQLITE_InternChanges;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2617
  }else if( pOp->p2==1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2618
    /* Record changes in the file format */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2619
    pDb->pSchema->file_format = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2620
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2621
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2622
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2623
  if( pOp->p1==1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2624
    /* Invalidate all prepared statements whenever the TEMP database
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2625
    ** schema is changed.  Ticket #1644 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2626
    sqlite3ExpirePreparedStatements(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2627
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2628
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2629
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2630
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2631
/* Opcode: VerifyCookie P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2632
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2633
** Check the value of global database parameter number 0 (the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2634
** schema version) and make sure it is equal to P2.  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2635
** P1 is the database number which is 0 for the main database file
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2636
** and 1 for the file holding temporary tables and some higher number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2637
** for auxiliary databases.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2638
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2639
** The cookie changes its value whenever the database schema changes.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2640
** This operation is used to detect when that the cookie has changed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2641
** and that the current process needs to reread the schema.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2642
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2643
** Either a transaction needs to have been started or an OP_Open needs
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2644
** to be executed (to establish a read lock) before this opcode is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2645
** invoked.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2646
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2647
case OP_VerifyCookie: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2648
  int iMeta;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2649
  Btree *pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2650
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2651
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2652
  pBt = db->aDb[pOp->p1].pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2653
  if( pBt ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2654
    rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2655
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2656
    rc = SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2657
    iMeta = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2658
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2659
  if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2660
    sqlite3_free(p->zErrMsg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2661
    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2662
    /* If the schema-cookie from the database file matches the cookie 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2663
    ** stored with the in-memory representation of the schema, do
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2664
    ** not reload the schema from the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2665
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2666
    ** If virtual-tables are in use, this is not just an optimisation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2667
    ** Often, v-tables store their data in other SQLite tables, which
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2668
    ** are queried from within xNext() and other v-table methods using
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2669
    ** prepared queries. If such a query is out-of-date, we do not want to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2670
    ** discard the database schema, as the user code implementing the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2671
    ** v-table would have to be ready for the sqlite3_vtab structure itself
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2672
    ** to be invalidated whenever sqlite3_step() is called from within 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2673
    ** a v-table method.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2674
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2675
    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2676
      sqlite3ResetInternalSchema(db, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2677
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2678
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2679
    sqlite3ExpirePreparedStatements(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2680
    rc = SQLITE_SCHEMA;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2681
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2682
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2683
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2684
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2685
/* Opcode: OpenRead P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2686
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2687
** Open a read-only cursor for the database table whose root page is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2688
** P2 in a database file.  The database file is determined by an 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2689
** integer from the top of the stack.  0 means the main database and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2690
** 1 means the database used for temporary tables.  Give the new 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2691
** cursor an identifier of P1.  The P1 values need not be contiguous
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2692
** but all P1 values should be small integers.  It is an error for
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2693
** P1 to be negative.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2694
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2695
** If P2==0 then take the root page number from the next of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2696
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2697
** There will be a read lock on the database whenever there is an
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2698
** open cursor.  If the database was unlocked prior to this instruction
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2699
** then a read lock is acquired as part of this instruction.  A read
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2700
** lock allows other processes to read the database but prohibits
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2701
** any other process from modifying the database.  The read lock is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2702
** released when all cursors are closed.  If this instruction attempts
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2703
** to get a read lock but fails, the script terminates with an
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2704
** SQLITE_BUSY error code.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2705
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2706
** The P3 value is a pointer to a KeyInfo structure that defines the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2707
** content and collating sequence of indices.  P3 is NULL for cursors
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2708
** that are not pointing to indices.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2709
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2710
** See also OpenWrite.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2711
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2712
/* Opcode: OpenWrite P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2713
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2714
** Open a read/write cursor named P1 on the table or index whose root
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2715
** page is P2.  If P2==0 then take the root page number from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2716
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2717
** The P3 value is a pointer to a KeyInfo structure that defines the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2718
** content and collating sequence of indices.  P3 is NULL for cursors
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2719
** that are not pointing to indices.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2720
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2721
** This instruction works just like OpenRead except that it opens the cursor
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2722
** in read/write mode.  For a given table, there can be one or more read-only
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2723
** cursors or a single read/write cursor but not both.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2724
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2725
** See also OpenRead.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2726
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2727
case OP_OpenRead:          /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2728
case OP_OpenWrite: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2729
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2730
  int p2 = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2731
  int wrFlag;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2732
  Btree *pX;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2733
  int iDb;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2734
  Cursor *pCur;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2735
  Db *pDb;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2736
  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2737
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2738
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2739
  iDb = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2740
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2741
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2742
  assert( iDb>=0 && iDb<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2743
  assert( (p->btreeMask & (1<<iDb))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2744
  pDb = &db->aDb[iDb];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2745
  pX = pDb->pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2746
  assert( pX!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2747
  if( pOp->opcode==OP_OpenWrite ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2748
    wrFlag = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2749
    if( pDb->pSchema->file_format < p->minWriteFileFormat ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2750
      p->minWriteFileFormat = pDb->pSchema->file_format;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2751
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2752
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2753
    wrFlag = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2754
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2755
  if( p2<=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2756
    assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2757
    sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2758
    p2 = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2759
    assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2760
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2761
    assert( p2>=2 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2762
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2763
  assert( i>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2764
  pCur = allocateCursor(p, i, iDb);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2765
  if( pCur==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2766
  pCur->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2767
  if( pX==0 ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2768
  /* We always provide a key comparison function.  If the table being
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2769
  ** opened is of type INTKEY, the comparision function will be ignored. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2770
  rc = sqlite3BtreeCursor(pX, p2, wrFlag,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2771
           sqlite3VdbeRecordCompare, pOp->p3,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2772
           &pCur->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2773
  if( pOp->p3type==P3_KEYINFO ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2774
    pCur->pKeyInfo = (KeyInfo*)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2775
    pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2776
    pCur->pKeyInfo->enc = ENC(p->db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2777
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2778
    pCur->pKeyInfo = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2779
    pCur->pIncrKey = &pCur->bogusIncrKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2780
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2781
  switch( rc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2782
    case SQLITE_BUSY: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2783
      p->pc = pc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2784
      p->rc = rc = SQLITE_BUSY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2785
      p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2786
      goto vdbe_return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2787
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2788
    case SQLITE_OK: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2789
      int flags = sqlite3BtreeFlags(pCur->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2790
      /* Sanity checking.  Only the lower four bits of the flags byte should
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2791
      ** be used.  Bit 3 (mask 0x08) is unpreditable.  The lower 3 bits
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2792
      ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2793
      ** 2 (zerodata for indices).  If these conditions are not met it can
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2794
      ** only mean that we are dealing with a corrupt database file
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2795
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2796
      if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2797
        rc = SQLITE_CORRUPT_BKPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2798
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2799
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2800
      pCur->isTable = (flags & BTREE_INTKEY)!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2801
      pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2802
      /* If P3==0 it means we are expected to open a table.  If P3!=0 then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2803
      ** we expect to be opening an index.  If this is not what happened,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2804
      ** then the database is corrupt
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2805
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2806
      if( (pCur->isTable && pOp->p3type==P3_KEYINFO)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2807
       || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2808
        rc = SQLITE_CORRUPT_BKPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2809
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2810
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2811
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2812
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2813
    case SQLITE_EMPTY: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2814
      pCur->isTable = pOp->p3type!=P3_KEYINFO;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2815
      pCur->isIndex = !pCur->isTable;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2816
      rc = SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2817
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2818
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2819
    default: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2820
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2821
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2822
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2823
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2824
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2825
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2826
/* Opcode: OpenEphemeral P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2827
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2828
** Open a new cursor P1 to a transient table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2829
** The cursor is always opened read/write even if 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2830
** the main database is read-only.  The transient or virtual
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2831
** table is deleted automatically when the cursor is closed.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2832
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2833
** P2 is the number of columns in the virtual table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2834
** The cursor points to a BTree table if P3==0 and to a BTree index
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2835
** if P3 is not 0.  If P3 is not NULL, it points to a KeyInfo structure
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2836
** that defines the format of keys in the index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2837
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2838
** This opcode was once called OpenTemp.  But that created
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2839
** confusion because the term "temp table", might refer either
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2840
** to a TEMP table at the SQL level, or to a table opened by
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2841
** this opcode.  Then this opcode was call OpenVirtual.  But
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2842
** that created confusion with the whole virtual-table idea.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2843
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2844
case OP_OpenEphemeral: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2845
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2846
  Cursor *pCx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2847
  static const int openFlags = 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2848
      SQLITE_OPEN_READWRITE |
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2849
      SQLITE_OPEN_CREATE |
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2850
      SQLITE_OPEN_EXCLUSIVE |
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2851
      SQLITE_OPEN_DELETEONCLOSE |
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2852
      SQLITE_OPEN_TRANSIENT_DB;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2853
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2854
  assert( i>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2855
  pCx = allocateCursor(p, i, -1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2856
  if( pCx==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2857
  pCx->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2858
  rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2859
                           &pCx->pBt);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2860
  if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2861
    rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2862
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2863
  if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2864
    /* If a transient index is required, create it by calling
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2865
    ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2866
    ** opening it. If a transient table is required, just use the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2867
    ** automatically created table with root-page 1 (an INTKEY table).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2868
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2869
    if( pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2870
      int pgno;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2871
      assert( pOp->p3type==P3_KEYINFO );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2872
      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2873
      if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2874
        assert( pgno==MASTER_ROOT+1 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2875
        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2876
            pOp->p3, &pCx->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2877
        pCx->pKeyInfo = (KeyInfo*)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2878
        pCx->pKeyInfo->enc = ENC(p->db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2879
        pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2880
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2881
      pCx->isTable = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2882
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2883
      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2884
      pCx->isTable = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2885
      pCx->pIncrKey = &pCx->bogusIncrKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2886
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2887
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2888
  pCx->nField = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2889
  pCx->isIndex = !pCx->isTable;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2890
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2891
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2892
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2893
/* Opcode: OpenPseudo P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2894
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2895
** Open a new cursor that points to a fake table that contains a single
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2896
** row of data.  Any attempt to write a second row of data causes the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2897
** first row to be deleted.  All data is deleted when the cursor is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2898
** closed.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2899
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2900
** A pseudo-table created by this opcode is useful for holding the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2901
** NEW or OLD tables in a trigger.  Also used to hold the a single
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2902
** row output from the sorter so that the row can be decomposed into
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2903
** individual columns using the OP_Column opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2904
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2905
case OP_OpenPseudo: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2906
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2907
  Cursor *pCx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2908
  assert( i>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2909
  pCx = allocateCursor(p, i, -1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2910
  if( pCx==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2911
  pCx->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2912
  pCx->pseudoTable = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2913
  pCx->pIncrKey = &pCx->bogusIncrKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2914
  pCx->isTable = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2915
  pCx->isIndex = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2916
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2917
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2918
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2919
/* Opcode: Close P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2920
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2921
** Close a cursor previously opened as P1.  If P1 is not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2922
** currently open, this instruction is a no-op.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2923
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2924
case OP_Close: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2925
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2926
  if( i>=0 && i<p->nCursor ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2927
    sqlite3VdbeFreeCursor(p, p->apCsr[i]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2928
    p->apCsr[i] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2929
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2930
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2931
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2932
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2933
/* Opcode: MoveGe P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2934
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2935
** Pop the top of the stack and use its value as a key.  Reposition
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2936
** cursor P1 so that it points to the smallest entry that is greater
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2937
** than or equal to the key that was popped ffrom the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2938
** If there are no records greater than or equal to the key and P2 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2939
** is not zero, then jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2940
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2941
** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2942
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2943
/* Opcode: MoveGt P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2944
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2945
** Pop the top of the stack and use its value as a key.  Reposition
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2946
** cursor P1 so that it points to the smallest entry that is greater
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2947
** than the key from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2948
** If there are no records greater than the key and P2 is not zero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2949
** then jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2950
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2951
** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2952
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2953
/* Opcode: MoveLt P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2954
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2955
** Pop the top of the stack and use its value as a key.  Reposition
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2956
** cursor P1 so that it points to the largest entry that is less
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2957
** than the key from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2958
** If there are no records less than the key and P2 is not zero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2959
** then jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2960
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2961
** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2962
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2963
/* Opcode: MoveLe P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2964
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2965
** Pop the top of the stack and use its value as a key.  Reposition
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2966
** cursor P1 so that it points to the largest entry that is less than
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2967
** or equal to the key that was popped from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2968
** If there are no records less than or eqal to the key and P2 is not zero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2969
** then jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2970
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2971
** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2972
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2973
case OP_MoveLt:         /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2974
case OP_MoveLe:         /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2975
case OP_MoveGe:         /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2976
case OP_MoveGt: {       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2977
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2978
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2979
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2980
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2981
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2982
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2983
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2984
  if( pC->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2985
    int res, oc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2986
    oc = pOp->opcode;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2987
    pC->nullRow = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2988
    *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2989
    if( pC->isTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2990
      i64 iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2991
      sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2992
      iKey = intToKey(pTos->u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2993
      if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2994
        pC->movetoTarget = iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2995
        pC->deferredMoveto = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2996
        assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2997
        pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2998
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  2999
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3000
      rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3001
      if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3002
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3003
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3004
      pC->lastRowid = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3005
      pC->rowidIsValid = res==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3006
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3007
      assert( pTos->flags & MEM_Blob );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3008
      ExpandBlob(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3009
      rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3010
      if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3011
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3012
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3013
      pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3014
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3015
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3016
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3017
    *pC->pIncrKey = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3018
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3019
    sqlite3_search_count++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3020
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3021
    if( oc==OP_MoveGe || oc==OP_MoveGt ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3022
      if( res<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3023
        rc = sqlite3BtreeNext(pC->pCursor, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3024
        if( rc!=SQLITE_OK ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3025
        pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3026
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3027
        res = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3028
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3029
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3030
      assert( oc==OP_MoveLt || oc==OP_MoveLe );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3031
      if( res>=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3032
        rc = sqlite3BtreePrevious(pC->pCursor, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3033
        if( rc!=SQLITE_OK ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3034
        pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3035
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3036
        /* res might be negative because the table is empty.  Check to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3037
        ** see if this is the case.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3038
        */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3039
        res = sqlite3BtreeEof(pC->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3040
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3041
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3042
    if( res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3043
      if( pOp->p2>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3044
        pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3045
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3046
        pC->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3047
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3048
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3049
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3050
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3051
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3052
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3053
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3054
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3055
/* Opcode: Distinct P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3056
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3057
** Use the top of the stack as a record created using MakeRecord.  P1 is a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3058
** cursor on a table that declared as an index.  If that table contains an
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3059
** entry that matches the top of the stack fall thru.  If the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3060
** matches no entry in P1 then jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3061
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3062
** The cursor is left pointing at the matching entry if it exists.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3063
** record on the top of the stack is not popped.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3064
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3065
** This instruction is similar to NotFound except that this operation
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3066
** does not pop the key from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3067
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3068
** The instruction is used to implement the DISTINCT operator on SELECT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3069
** statements.  The P1 table is not a true index but rather a record of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3070
** all results that have produced so far.  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3071
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3072
** See also: Found, NotFound, MoveTo, IsUnique, NotExists
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3073
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3074
/* Opcode: Found P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3075
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3076
** Top of the stack holds a blob constructed by MakeRecord.  P1 is an index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3077
** If an entry that matches the top of the stack exists in P1 then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3078
** jump to P2.  If the top of the stack does not match any entry in P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3079
** then fall thru.  The P1 cursor is left pointing at the matching entry
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3080
** if it exists.  The blob is popped off the top of the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3081
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3082
** This instruction is used to implement the IN operator where the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3083
** left-hand side is a SELECT statement.  P1 may be a true index, or it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3084
** may be a temporary index that holds the results of the SELECT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3085
** statement. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3086
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3087
** This instruction checks if index P1 contains a record for which 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3088
** the first N serialised values exactly match the N serialised values
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3089
** in the record on the stack, where N is the total number of values in
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3090
** the stack record (stack record is a prefix of the P1 record). 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3091
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3092
** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3093
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3094
/* Opcode: NotFound P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3095
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3096
** The top of the stack holds a blob constructed by MakeRecord.  P1 is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3097
** an index.  If no entry exists in P1 that matches the blob then jump
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3098
** to P2.  If an entry does existing, fall through.  The cursor is left
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3099
** pointing to the entry that matches.  The blob is popped from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3100
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3101
** The difference between this operation and Distinct is that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3102
** Distinct does not pop the key from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3103
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3104
** See also: Distinct, Found, MoveTo, NotExists, IsUnique
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3105
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3106
case OP_Distinct:       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3107
case OP_NotFound:       /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3108
case OP_Found: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3109
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3110
  int alreadyExists = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3111
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3112
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3113
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3114
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3115
  if( (pC = p->apCsr[i])->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3116
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3117
    assert( pC->isTable==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3118
    assert( pTos->flags & MEM_Blob );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3119
    Stringify(pTos, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3120
    if( pOp->opcode==OP_Found ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3121
      pC->pKeyInfo->prefixIsEqual = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3122
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3123
    rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3124
    pC->pKeyInfo->prefixIsEqual = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3125
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3126
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3127
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3128
    alreadyExists = (res==0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3129
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3130
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3131
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3132
  if( pOp->opcode==OP_Found ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3133
    if( alreadyExists ) pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3134
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3135
    if( !alreadyExists ) pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3136
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3137
  if( pOp->opcode!=OP_Distinct ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3138
    Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3139
    pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3140
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3141
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3142
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3143
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3144
/* Opcode: IsUnique P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3145
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3146
** The top of the stack is an integer record number.  Call this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3147
** record number R.  The next on the stack is an index key created
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3148
** using MakeIdxRec.  Call it K.  This instruction pops R from the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3149
** stack but it leaves K unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3150
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3151
** P1 is an index.  So it has no data and its key consists of a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3152
** record generated by OP_MakeRecord where the last field is the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3153
** rowid of the entry that the index refers to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3154
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3155
** This instruction asks if there is an entry in P1 where the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3156
** fields matches K but the rowid is different from R.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3157
** If there is no such entry, then there is an immediate
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3158
** jump to P2.  If any entry does exist where the index string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3159
** matches K but the record number is not R, then the record
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3160
** number for that entry is pushed onto the stack and control
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3161
** falls through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3162
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3163
** See also: Distinct, NotFound, NotExists, Found
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3164
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3165
case OP_IsUnique: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3166
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3167
  Mem *pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3168
  Cursor *pCx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3169
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3170
  i64 R;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3171
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3172
  /* Pop the value R off the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3173
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3174
  assert( pNos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3175
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3176
  R = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3177
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3178
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3179
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3180
  pCx = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3181
  assert( pCx!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3182
  pCrsr = pCx->pCursor;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3183
  if( pCrsr!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3184
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3185
    i64 v;         /* The record number on the P1 entry that matches K */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3186
    char *zKey;    /* The value of K */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3187
    int nKey;      /* Number of bytes in K */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3188
    int len;       /* Number of bytes in K without the rowid at the end */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3189
    int szRowid;   /* Size of the rowid column at the end of zKey */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3190
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3191
    /* Make sure K is a string and make zKey point to K
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3192
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3193
    assert( pNos->flags & MEM_Blob );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3194
    Stringify(pNos, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3195
    zKey = pNos->z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3196
    nKey = pNos->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3197
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3198
    szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3199
    len = nKey-szRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3200
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3201
    /* Search for an entry in P1 where all but the last four bytes match K.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3202
    ** If there is no such entry, jump immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3203
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3204
    assert( pCx->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3205
    pCx->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3206
    rc = sqlite3BtreeMoveto(pCrsr, zKey, len, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3207
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3208
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3209
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3210
    if( res<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3211
      rc = sqlite3BtreeNext(pCrsr, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3212
      if( res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3213
        pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3214
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3215
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3216
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3217
    rc = sqlite3VdbeIdxKeyCompare(pCx, len, (u8*)zKey, &res); 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3218
    if( rc!=SQLITE_OK ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3219
    if( res>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3220
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3221
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3222
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3223
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3224
    /* At this point, pCrsr is pointing to an entry in P1 where all but
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3225
    ** the final entry (the rowid) matches K.  Check to see if the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3226
    ** final rowid column is different from R.  If it equals R then jump
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3227
    ** immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3228
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3229
    rc = sqlite3VdbeIdxRowid(pCrsr, &v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3230
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3231
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3232
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3233
    if( v==R ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3234
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3235
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3236
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3237
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3238
    /* The final varint of the key is different from R.  Push it onto
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3239
    ** the stack.  (The record number of an entry that violates a UNIQUE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3240
    ** constraint.)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3241
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3242
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3243
    pTos->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3244
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3245
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3246
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3247
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3248
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3249
/* Opcode: NotExists P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3250
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3251
** Use the top of the stack as a integer key.  If a record with that key
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3252
** does not exist in table of P1, then jump to P2.  If the record
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3253
** does exist, then fall thru.  The cursor is left pointing to the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3254
** record if it exists.  The integer key is popped from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3255
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3256
** The difference between this operation and NotFound is that this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3257
** operation assumes the key is an integer and that P1 is a table whereas
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3258
** NotFound assumes key is a blob constructed from MakeRecord and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3259
** P1 is an index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3260
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3261
** See also: Distinct, Found, MoveTo, NotFound, IsUnique
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3262
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3263
case OP_NotExists: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3264
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3265
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3266
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3267
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3268
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3269
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3270
  if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3271
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3272
    u64 iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3273
    assert( pTos->flags & MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3274
    assert( p->apCsr[i]->isTable );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3275
    iKey = intToKey(pTos->u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3276
    rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, 0,&res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3277
    pC->lastRowid = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3278
    pC->rowidIsValid = res==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3279
    pC->nullRow = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3280
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3281
    /* res might be uninitialized if rc!=SQLITE_OK.  But if rc!=SQLITE_OK
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3282
    ** processing is about to abort so we really do not care whether or not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3283
    ** the following jump is taken.  (In other words, do not stress over
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3284
    ** the error that valgrind sometimes shows on the next statement when
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3285
    ** running ioerr.test and similar failure-recovery test scripts.) */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3286
    if( res!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3287
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3288
      pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3289
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3290
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3291
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3292
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3293
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3294
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3295
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3296
/* Opcode: Sequence P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3297
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3298
** Push an integer onto the stack which is the next available
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3299
** sequence number for cursor P1.  The sequence number on the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3300
** cursor is incremented after the push.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3301
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3302
case OP_Sequence: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3303
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3304
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3305
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3306
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3307
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3308
  pTos->u.i = p->apCsr[i]->seqCount++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3309
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3310
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3311
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3312
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3313
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3314
/* Opcode: NewRowid P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3315
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3316
** Get a new integer record number (a.k.a "rowid") used as the key to a table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3317
** The record number is not previously used as a key in the database
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3318
** table that cursor P1 points to.  The new record number is pushed 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3319
** onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3320
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3321
** If P2>0 then P2 is a memory cell that holds the largest previously
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3322
** generated record number.  No new record numbers are allowed to be less
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3323
** than this value.  When this value reaches its maximum, a SQLITE_FULL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3324
** error is generated.  The P2 memory cell is updated with the generated
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3325
** record number.  This P2 mechanism is used to help implement the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3326
** AUTOINCREMENT feature.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3327
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3328
case OP_NewRowid: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3329
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3330
  i64 v = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3331
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3332
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3333
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3334
  if( (pC = p->apCsr[i])->pCursor==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3335
    /* The zero initialization above is all that is needed */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3336
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3337
    /* The next rowid or record number (different terms for the same
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3338
    ** thing) is obtained in a two-step algorithm.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3339
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3340
    ** First we attempt to find the largest existing rowid and add one
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3341
    ** to that.  But if the largest existing rowid is already the maximum
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3342
    ** positive integer, we have to fall through to the second
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3343
    ** probabilistic algorithm
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3344
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3345
    ** The second algorithm is to select a rowid at random and see if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3346
    ** it already exists in the table.  If it does not exist, we have
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3347
    ** succeeded.  If the random rowid does exist, we select a new one
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3348
    ** and try again, up to 1000 times.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3349
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3350
    ** For a table with less than 2 billion entries, the probability
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3351
    ** of not finding a unused rowid is about 1.0e-300.  This is a 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3352
    ** non-zero probability, but it is still vanishingly small and should
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3353
    ** never cause a problem.  You are much, much more likely to have a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3354
    ** hardware failure than for this algorithm to fail.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3355
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3356
    ** The analysis in the previous paragraph assumes that you have a good
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3357
    ** source of random numbers.  Is a library function like lrand48()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3358
    ** good enough?  Maybe. Maybe not. It's hard to know whether there
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3359
    ** might be subtle bugs is some implementations of lrand48() that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3360
    ** could cause problems. To avoid uncertainty, SQLite uses its own 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3361
    ** random number generator based on the RC4 algorithm.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3362
    **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3363
    ** To promote locality of reference for repetitive inserts, the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3364
    ** first few attempts at chosing a random rowid pick values just a little
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3365
    ** larger than the previous rowid.  This has been shown experimentally
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3366
    ** to double the speed of the COPY operation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3367
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3368
    int res, rx=SQLITE_OK, cnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3369
    i64 x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3370
    cnt = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3371
    if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3372
          BTREE_INTKEY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3373
      rc = SQLITE_CORRUPT_BKPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3374
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3375
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3376
    assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3377
    assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3378
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3379
#ifdef SQLITE_32BIT_ROWID
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3380
#   define MAX_ROWID 0x7fffffff
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3381
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3382
    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3383
    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3384
    ** to provide the constant while making all compilers happy.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3385
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3386
#   define MAX_ROWID  ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3387
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3388
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3389
    if( !pC->useRandomRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3390
      if( pC->nextRowidValid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3391
        v = pC->nextRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3392
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3393
        rc = sqlite3BtreeLast(pC->pCursor, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3394
        if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3395
          goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3396
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3397
        if( res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3398
          v = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3399
        }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3400
          sqlite3BtreeKeySize(pC->pCursor, &v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3401
          v = keyToInt(v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3402
          if( v==MAX_ROWID ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3403
            pC->useRandomRowid = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3404
          }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3405
            v++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3406
          }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3407
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3408
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3409
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3410
#ifndef SQLITE_OMIT_AUTOINCREMENT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3411
      if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3412
        Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3413
        assert( pOp->p2>0 && pOp->p2<p->nMem );  /* P2 is a valid memory cell */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3414
        pMem = &p->aMem[pOp->p2];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3415
        sqlite3VdbeMemIntegerify(pMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3416
        assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P2) holds an integer */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3417
        if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3418
          rc = SQLITE_FULL;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3419
          goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3420
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3421
        if( v<pMem->u.i+1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3422
          v = pMem->u.i + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3423
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3424
        pMem->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3425
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3426
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3427
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3428
      if( v<MAX_ROWID ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3429
        pC->nextRowidValid = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3430
        pC->nextRowid = v+1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3431
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3432
        pC->nextRowidValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3433
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3434
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3435
    if( pC->useRandomRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3436
      assert( pOp->p2==0 );  /* SQLITE_FULL must have occurred prior to this */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3437
      v = db->priorNewRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3438
      cnt = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3439
      do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3440
        if( v==0 || cnt>2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3441
          sqlite3Randomness(sizeof(v), &v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3442
          if( cnt<5 ) v &= 0xffffff;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3443
        }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3444
          unsigned char r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3445
          sqlite3Randomness(1, &r);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3446
          v += r + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3447
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3448
        if( v==0 ) continue;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3449
        x = intToKey(v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3450
        rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3451
        cnt++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3452
      }while( cnt<1000 && rx==SQLITE_OK && res==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3453
      db->priorNewRowid = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3454
      if( rx==SQLITE_OK && res==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3455
        rc = SQLITE_FULL;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3456
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3457
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3458
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3459
    pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3460
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3461
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3462
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3463
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3464
  pTos->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3465
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3466
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3467
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3468
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3469
/* Opcode: Insert P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3470
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3471
** Write an entry into the table of cursor P1.  A new entry is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3472
** created if it doesn't already exist or the data for an existing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3473
** entry is overwritten.  The data is the value on the top of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3474
** stack.  The key is the next value down on the stack.  The key must
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3475
** be an integer.  The stack is popped twice by this instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3476
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3477
** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3478
** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P2 is set,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3479
** then rowid is stored for subsequent return by the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3480
** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3481
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3482
** Parameter P3 may point to a string containing the table-name, or
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3483
** may be NULL. If it is not NULL, then the update-hook 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3484
** (sqlite3.xUpdateCallback) is invoked following a successful insert.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3485
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3486
** This instruction only works on tables.  The equivalent instruction
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3487
** for indices is OP_IdxInsert.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3488
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3489
case OP_Insert: {         /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3490
  Mem *pNos = &pTos[-1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3491
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3492
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3493
  assert( pNos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3494
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3495
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3496
  if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3497
    i64 iKey;   /* The integer ROWID or key for the record to be inserted */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3498
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3499
    assert( pNos->flags & MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3500
    assert( pC->isTable );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3501
    iKey = intToKey(pNos->u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3502
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3503
    if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3504
    if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3505
    if( pC->nextRowidValid && pNos->u.i>=pC->nextRowid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3506
      pC->nextRowidValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3507
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3508
    if( pTos->flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3509
      pTos->z = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3510
      pTos->n = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3511
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3512
      assert( pTos->flags & (MEM_Blob|MEM_Str) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3513
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3514
    if( pC->pseudoTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3515
      sqlite3_free(pC->pData);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3516
      pC->iKey = iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3517
      pC->nData = pTos->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3518
      if( pTos->flags & MEM_Dyn ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3519
        pC->pData = pTos->z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3520
        pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3521
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3522
        pC->pData = (char*)sqlite3_malloc( pC->nData+2 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3523
        if( !pC->pData ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3524
        memcpy(pC->pData, pTos->z, pC->nData);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3525
        pC->pData[pC->nData] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3526
        pC->pData[pC->nData+1] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3527
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3528
      pC->nullRow = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3529
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3530
      int nZero;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3531
      if( pTos->flags & MEM_Zero ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3532
        nZero = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3533
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3534
        nZero = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3535
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3536
      rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3537
                              pTos->z, pTos->n, nZero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3538
                              pOp->p2 & OPFLAG_APPEND);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3539
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3540
    
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3541
    pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3542
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3543
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3544
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3545
    /* Invoke the update-hook if required. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3546
    if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3547
      const char *zDb = db->aDb[pC->iDb].zName;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3548
      const char *zTbl = pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3549
      int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3550
      assert( pC->isTable );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3551
      db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3552
      assert( pC->iDb>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3553
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3554
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3555
  popStack(&pTos, 2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3556
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3557
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3558
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3559
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3560
/* Opcode: Delete P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3561
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3562
** Delete the record at which the P1 cursor is currently pointing.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3563
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3564
** The cursor will be left pointing at either the next or the previous
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3565
** record in the table. If it is left pointing at the next record, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3566
** the next Next instruction will be a no-op.  Hence it is OK to delete
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3567
** a record from within an Next loop.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3568
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3569
** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3570
** incremented (otherwise not).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3571
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3572
** If P1 is a pseudo-table, then this instruction is a no-op.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3573
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3574
case OP_Delete: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3575
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3576
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3577
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3578
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3579
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3580
  if( pC->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3581
    i64 iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3582
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3583
    /* If the update-hook will be invoked, set iKey to the rowid of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3584
    ** row being deleted.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3585
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3586
    if( db->xUpdateCallback && pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3587
      assert( pC->isTable );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3588
      if( pC->rowidIsValid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3589
        iKey = pC->lastRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3590
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3591
        rc = sqlite3BtreeKeySize(pC->pCursor, &iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3592
        if( rc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3593
          goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3594
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3595
        iKey = keyToInt(iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3596
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3597
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3598
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3599
    rc = sqlite3VdbeCursorMoveto(pC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3600
    if( rc ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3601
    rc = sqlite3BtreeDelete(pC->pCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3602
    pC->nextRowidValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3603
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3604
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3605
    /* Invoke the update-hook if required. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3606
    if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3607
      const char *zDb = db->aDb[pC->iDb].zName;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3608
      const char *zTbl = pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3609
      db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3610
      assert( pC->iDb>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3611
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3612
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3613
  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3614
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3615
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3616
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3617
/* Opcode: ResetCount P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3618
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3619
** This opcode resets the VMs internal change counter to 0. If P1 is true,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3620
** then the value of the change counter is copied to the database handle
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3621
** change counter (returned by subsequent calls to sqlite3_changes())
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3622
** before it is reset. This is used by trigger programs.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3623
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3624
case OP_ResetCount: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3625
  if( pOp->p1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3626
    sqlite3VdbeSetChanges(db, p->nChange);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3627
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3628
  p->nChange = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3629
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3630
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3631
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3632
/* Opcode: RowData P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3633
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3634
** Push onto the stack the complete row data for cursor P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3635
** There is no interpretation of the data.  It is just copied
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3636
** onto the stack exactly as it is found in the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3637
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3638
** If the cursor is not pointing to a valid row, a NULL is pushed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3639
** onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3640
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3641
/* Opcode: RowKey P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3642
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3643
** Push onto the stack the complete row key for cursor P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3644
** There is no interpretation of the key.  It is just copied
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3645
** onto the stack exactly as it is found in the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3646
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3647
** If the cursor is not pointing to a valid row, a NULL is pushed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3648
** onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3649
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3650
case OP_RowKey:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3651
case OP_RowData: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3652
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3653
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3654
  u32 n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3655
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3656
  /* Note that RowKey and RowData are really exactly the same instruction */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3657
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3658
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3659
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3660
  assert( pC->isTable || pOp->opcode==OP_RowKey );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3661
  assert( pC->isIndex || pOp->opcode==OP_RowData );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3662
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3663
  if( pC->nullRow ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3664
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3665
  }else if( pC->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3666
    BtCursor *pCrsr = pC->pCursor;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3667
    rc = sqlite3VdbeCursorMoveto(pC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3668
    if( rc ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3669
    if( pC->nullRow ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3670
      pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3671
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3672
    }else if( pC->isIndex ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3673
      i64 n64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3674
      assert( !pC->isTable );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3675
      sqlite3BtreeKeySize(pCrsr, &n64);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3676
      if( n64>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3677
        goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3678
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3679
      n = n64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3680
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3681
      sqlite3BtreeDataSize(pCrsr, &n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3682
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3683
    if( n>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3684
      goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3685
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3686
    pTos->n = n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3687
    if( n<=NBFS ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3688
      pTos->flags = MEM_Blob | MEM_Short;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3689
      pTos->z = pTos->zShort;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3690
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3691
      char *z = (char*)sqlite3_malloc( n );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3692
      if( z==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3693
      pTos->flags = MEM_Blob | MEM_Dyn;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3694
      pTos->xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3695
      pTos->z = z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3696
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3697
    if( pC->isIndex ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3698
      rc = sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3699
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3700
      rc = sqlite3BtreeData(pCrsr, 0, n, pTos->z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3701
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3702
  }else if( pC->pseudoTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3703
    pTos->n = pC->nData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3704
    assert( pC->nData<=SQLITE_MAX_LENGTH );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3705
    pTos->z = pC->pData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3706
    pTos->flags = MEM_Blob|MEM_Ephem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3707
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3708
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3709
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3710
  pTos->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3711
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3712
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3713
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3714
/* Opcode: Rowid P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3715
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3716
** Push onto the stack an integer which is the key of the table entry that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3717
** P1 is currently point to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3718
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3719
case OP_Rowid: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3720
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3721
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3722
  i64 v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3723
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3724
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3725
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3726
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3727
  rc = sqlite3VdbeCursorMoveto(pC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3728
  if( rc ) goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3729
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3730
  if( pC->rowidIsValid ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3731
    v = pC->lastRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3732
  }else if( pC->pseudoTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3733
    v = keyToInt(pC->iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3734
  }else if( pC->nullRow || pC->pCursor==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3735
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3736
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3737
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3738
    assert( pC->pCursor!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3739
    sqlite3BtreeKeySize(pC->pCursor, &v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3740
    v = keyToInt(v);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3741
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3742
  pTos->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3743
  pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3744
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3745
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3746
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3747
/* Opcode: NullRow P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3748
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3749
** Move the cursor P1 to a null row.  Any OP_Column operations
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3750
** that occur while the cursor is on the null row will always push 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3751
** a NULL onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3752
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3753
case OP_NullRow: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3754
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3755
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3756
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3757
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3758
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3759
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3760
  pC->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3761
  pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3762
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3763
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3764
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3765
/* Opcode: Last P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3766
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3767
** The next use of the Rowid or Column or Next instruction for P1 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3768
** will refer to the last entry in the database table or index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3769
** If the table or index is empty and P2>0, then jump immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3770
** If P2 is 0 or if the table or index is not empty, fall through
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3771
** to the following instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3772
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3773
case OP_Last: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3774
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3775
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3776
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3777
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3778
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3779
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3780
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3781
  if( (pCrsr = pC->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3782
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3783
    rc = sqlite3BtreeLast(pCrsr, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3784
    pC->nullRow = res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3785
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3786
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3787
    if( res && pOp->p2>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3788
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3789
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3790
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3791
    pC->nullRow = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3792
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3793
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3794
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3795
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3796
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3797
/* Opcode: Sort P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3798
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3799
** This opcode does exactly the same thing as OP_Rewind except that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3800
** it increments an undocumented global variable used for testing.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3801
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3802
** Sorting is accomplished by writing records into a sorting index,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3803
** then rewinding that index and playing it back from beginning to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3804
** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3805
** rewinding so that the global variable will be incremented and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3806
** regression tests can determine whether or not the optimizer is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3807
** correctly optimizing out sorts.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3808
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3809
case OP_Sort: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3810
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3811
  sqlite3_sort_count++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3812
  sqlite3_search_count--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3813
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3814
  /* Fall through into OP_Rewind */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3815
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3816
/* Opcode: Rewind P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3817
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3818
** The next use of the Rowid or Column or Next instruction for P1 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3819
** will refer to the first entry in the database table or index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3820
** If the table or index is empty and P2>0, then jump immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3821
** If P2 is 0 or if the table or index is not empty, fall through
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3822
** to the following instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3823
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3824
case OP_Rewind: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3825
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3826
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3827
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3828
  int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3829
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3830
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3831
  pC = p->apCsr[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3832
  assert( pC!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3833
  if( (pCrsr = pC->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3834
    rc = sqlite3BtreeFirst(pCrsr, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3835
    pC->atFirst = res==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3836
    pC->deferredMoveto = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3837
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3838
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3839
    res = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3840
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3841
  pC->nullRow = res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3842
  if( res && pOp->p2>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3843
    pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3844
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3845
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3846
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3847
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3848
/* Opcode: Next P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3849
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3850
** Advance cursor P1 so that it points to the next key/data pair in its
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3851
** table or index.  If there are no more key/value pairs then fall through
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3852
** to the following instruction.  But if the cursor advance was successful,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3853
** jump immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3854
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3855
** See also: Prev
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3856
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3857
/* Opcode: Prev P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3858
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3859
** Back up cursor P1 so that it points to the previous key/data pair in its
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3860
** table or index.  If there is no previous key/value pairs then fall through
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3861
** to the following instruction.  But if the cursor backup was successful,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3862
** jump immediately to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3863
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3864
case OP_Prev:          /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3865
case OP_Next: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3866
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3867
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3868
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3869
  CHECK_FOR_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3870
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3871
  pC = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3872
  if( pC==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3873
    break;  /* See ticket #2273 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3874
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3875
  if( (pCrsr = pC->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3876
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3877
    if( pC->nullRow ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3878
      res = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3879
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3880
      assert( pC->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3881
      rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3882
                                  sqlite3BtreePrevious(pCrsr, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3883
      pC->nullRow = res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3884
      pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3885
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3886
    if( res==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3887
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3888
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3889
      sqlite3_search_count++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3890
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3891
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3892
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3893
    pC->nullRow = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3894
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3895
  pC->rowidIsValid = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3896
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3897
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3898
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3899
/* Opcode: IdxInsert P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3900
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3901
** The top of the stack holds a SQL index key made using either the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3902
** MakeIdxRec or MakeRecord instructions.  This opcode writes that key
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3903
** into the index P1.  Data for the entry is nil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3904
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3905
** P2 is a flag that provides a hint to the b-tree layer that this
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3906
** insert is likely to be an append.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3907
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3908
** This instruction only works for indices.  The equivalent instruction
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3909
** for tables is OP_Insert.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3910
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3911
case OP_IdxInsert: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3912
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3913
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3914
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3915
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3916
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3917
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3918
  assert( pTos->flags & MEM_Blob );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3919
  if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3920
    assert( pC->isTable==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3921
    rc = ExpandBlob(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3922
    if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3923
      int nKey = pTos->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3924
      const char *zKey = pTos->z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3925
      rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3926
      assert( pC->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3927
      pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3928
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3929
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3930
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3931
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3932
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3933
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3934
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3935
/* Opcode: IdxDelete P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3936
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3937
** The top of the stack is an index key built using the either the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3938
** MakeIdxRec or MakeRecord opcodes.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3939
** This opcode removes that entry from the index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3940
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3941
case OP_IdxDelete: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3942
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3943
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3944
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3945
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3946
  assert( pTos->flags & MEM_Blob );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3947
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3948
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3949
  if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3950
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3951
    rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, 0, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3952
    if( rc==SQLITE_OK && res==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3953
      rc = sqlite3BtreeDelete(pCrsr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3954
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3955
    assert( pC->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3956
    pC->cacheStatus = CACHE_STALE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3957
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3958
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3959
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3960
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3961
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3962
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3963
/* Opcode: IdxRowid P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3964
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3965
** Push onto the stack an integer which is the last entry in the record at
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3966
** the end of the index key pointed to by cursor P1.  This integer should be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3967
** the rowid of the table entry to which this index entry points.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3968
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3969
** See also: Rowid, MakeIdxRec.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3970
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3971
case OP_IdxRowid: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3972
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3973
  BtCursor *pCrsr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3974
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3975
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3976
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3977
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3978
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3979
  pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3980
  if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3981
    i64 rowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3982
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3983
    assert( pC->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3984
    assert( pC->isTable==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3985
    if( pC->nullRow ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3986
      pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3987
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3988
      rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3989
      if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3990
        goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3991
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3992
      pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3993
      pTos->u.i = rowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3994
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3995
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3996
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3997
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3998
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  3999
/* Opcode: IdxGT P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4000
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4001
** The top of the stack is an index entry that omits the ROWID.  Compare
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4002
** the top of stack against the index that P1 is currently pointing to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4003
** Ignore the ROWID on the P1 index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4004
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4005
** The top of the stack might have fewer columns that P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4006
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4007
** If the P1 index entry is greater than the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4008
** then jump to P2.  Otherwise fall through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4009
** In either case, the stack is popped once.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4010
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4011
/* Opcode: IdxGE P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4012
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4013
** The top of the stack is an index entry that omits the ROWID.  Compare
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4014
** the top of stack against the index that P1 is currently pointing to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4015
** Ignore the ROWID on the P1 index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4016
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4017
** If the P1 index entry is greater than or equal to the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4018
** then jump to P2.  Otherwise fall through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4019
** In either case, the stack is popped once.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4020
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4021
** If P3 is the "+" string (or any other non-NULL string) then the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4022
** index taken from the top of the stack is temporarily increased by
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4023
** an epsilon prior to the comparison.  This make the opcode work
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4024
** like IdxGT except that if the key from the stack is a prefix of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4025
** the key in the cursor, the result is false whereas it would be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4026
** true with IdxGT.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4027
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4028
/* Opcode: IdxLT P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4029
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4030
** The top of the stack is an index entry that omits the ROWID.  Compare
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4031
** the top of stack against the index that P1 is currently pointing to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4032
** Ignore the ROWID on the P1 index.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4033
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4034
** If the P1 index entry is less than  the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4035
** then jump to P2.  Otherwise fall through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4036
** In either case, the stack is popped once.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4037
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4038
** If P3 is the "+" string (or any other non-NULL string) then the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4039
** index taken from the top of the stack is temporarily increased by
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4040
** an epsilon prior to the comparison.  This makes the opcode work
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4041
** like IdxLE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4042
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4043
case OP_IdxLT:          /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4044
case OP_IdxGT:          /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4045
case OP_IdxGE: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4046
  int i= pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4047
  Cursor *pC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4048
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4049
  assert( i>=0 && i<p->nCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4050
  assert( p->apCsr[i]!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4051
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4052
  if( (pC = p->apCsr[i])->pCursor!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4053
    int res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4054
 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4055
    assert( pTos->flags & MEM_Blob );  /* Created using OP_MakeRecord */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4056
    assert( pC->deferredMoveto==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4057
    ExpandBlob(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4058
    *pC->pIncrKey = pOp->p3!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4059
    assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4060
    rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4061
    *pC->pIncrKey = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4062
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4063
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4064
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4065
    if( pOp->opcode==OP_IdxLT ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4066
      res = -res;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4067
    }else if( pOp->opcode==OP_IdxGE ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4068
      res++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4069
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4070
    if( res>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4071
      pc = pOp->p2 - 1 ;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4072
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4073
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4074
  Release(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4075
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4076
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4077
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4078
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4079
/* Opcode: Destroy P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4080
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4081
** Delete an entire database table or index whose root page in the database
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4082
** file is given by P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4083
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4084
** The table being destroyed is in the main database file if P2==0.  If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4085
** P2==1 then the table to be clear is in the auxiliary database file
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4086
** that is used to store tables create using CREATE TEMPORARY TABLE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4087
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4088
** If AUTOVACUUM is enabled then it is possible that another root page
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4089
** might be moved into the newly deleted root page in order to keep all
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4090
** root pages contiguous at the beginning of the database.  The former
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4091
** value of the root page that moved - its value before the move occurred -
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4092
** is pushed onto the stack.  If no page movement was required (because
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4093
** the table being dropped was already the last one in the database) then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4094
** a zero is pushed onto the stack.  If AUTOVACUUM is disabled
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4095
** then a zero is pushed onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4096
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4097
** See also: Clear
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4098
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4099
case OP_Destroy: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4100
  int iMoved;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4101
  int iCnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4102
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4103
  Vdbe *pVdbe;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4104
  iCnt = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4105
  for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4106
    if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4107
      iCnt++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4108
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4109
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4110
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4111
  iCnt = db->activeVdbeCnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4112
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4113
  if( iCnt>1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4114
    rc = SQLITE_LOCKED;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4115
    p->errorAction = OE_Abort;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4116
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4117
    assert( iCnt==1 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4118
    assert( (p->btreeMask & (1<<pOp->p2))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4119
    rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4120
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4121
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4122
    pTos->u.i = iMoved;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4123
#ifndef SQLITE_OMIT_AUTOVACUUM
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4124
    if( rc==SQLITE_OK && iMoved!=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4125
      sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4126
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4127
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4128
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4129
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4130
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4131
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4132
/* Opcode: Clear P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4133
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4134
** Delete all contents of the database table or index whose root page
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4135
** in the database file is given by P1.  But, unlike Destroy, do not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4136
** remove the table or index from the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4137
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4138
** The table being clear is in the main database file if P2==0.  If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4139
** P2==1 then the table to be clear is in the auxiliary database file
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4140
** that is used to store tables create using CREATE TEMPORARY TABLE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4141
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4142
** See also: Destroy
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4143
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4144
case OP_Clear: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4145
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4146
  /* For consistency with the way other features of SQLite operate
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4147
  ** with a truncate, we will also skip the update callback.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4148
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4149
#if 0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4150
  Btree *pBt = db->aDb[pOp->p2].pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4151
  if( db->xUpdateCallback && pOp->p3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4152
    const char *zDb = db->aDb[pOp->p2].zName;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4153
    const char *zTbl = pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4154
    BtCursor *pCur = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4155
    int fin = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4156
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4157
    rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4158
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4159
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4160
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4161
    for(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4162
      rc=sqlite3BtreeFirst(pCur, &fin); 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4163
      rc==SQLITE_OK && !fin; 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4164
      rc=sqlite3BtreeNext(pCur, &fin)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4165
    ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4166
      i64 iKey;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4167
      rc = sqlite3BtreeKeySize(pCur, &iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4168
      if( rc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4169
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4170
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4171
      iKey = keyToInt(iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4172
      db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4173
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4174
    sqlite3BtreeCloseCursor(pCur);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4175
    if( rc!=SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4176
      goto abort_due_to_error;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4177
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4178
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4179
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4180
  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4181
  rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4182
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4183
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4184
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4185
/* Opcode: CreateTable P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4186
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4187
** Allocate a new table in the main database file if P2==0 or in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4188
** auxiliary database file if P2==1.  Push the page number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4189
** for the root page of the new table onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4190
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4191
** The difference between a table and an index is this:  A table must
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4192
** have a 4-byte integer key and can have arbitrary data.  An index
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4193
** has an arbitrary key but no data.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4194
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4195
** See also: CreateIndex
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4196
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4197
/* Opcode: CreateIndex P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4198
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4199
** Allocate a new index in the main database file if P2==0 or in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4200
** auxiliary database file if P2==1.  Push the page number of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4201
** root page of the new index onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4202
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4203
** See documentation on OP_CreateTable for additional information.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4204
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4205
case OP_CreateIndex:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4206
case OP_CreateTable: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4207
  int pgno;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4208
  int flags;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4209
  Db *pDb;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4210
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4211
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4212
  pDb = &db->aDb[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4213
  assert( pDb->pBt!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4214
  if( pOp->opcode==OP_CreateTable ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4215
    /* flags = BTREE_INTKEY; */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4216
    flags = BTREE_LEAFDATA|BTREE_INTKEY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4217
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4218
    flags = BTREE_ZERODATA;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4219
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4220
  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4221
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4222
  if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4223
    pTos->u.i = pgno;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4224
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4225
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4226
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4227
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4228
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4229
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4230
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4231
/* Opcode: ParseSchema P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4232
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4233
** Read and parse all entries from the SQLITE_MASTER table of database P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4234
** that match the WHERE clause P3.  P2 is the "force" flag.   Always do
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4235
** the parsing if P2 is true.  If P2 is false, then this routine is a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4236
** no-op if the schema is not currently loaded.  In other words, if P2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4237
** is false, the SQLITE_MASTER table is only parsed if the rest of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4238
** schema is already loaded into the symbol table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4239
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4240
** This opcode invokes the parser to create a new virtual machine,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4241
** then runs the new virtual machine.  It is thus a reentrant opcode.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4242
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4243
case OP_ParseSchema: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4244
  char *zSql;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4245
  int iDb = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4246
  const char *zMaster;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4247
  InitData initData;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4248
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4249
  assert( iDb>=0 && iDb<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4250
  if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4251
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4252
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4253
  zMaster = SCHEMA_TABLE(iDb);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4254
  initData.db = db;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4255
  initData.iDb = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4256
  initData.pzErrMsg = &p->zErrMsg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4257
  zSql = sqlite3MPrintf(db,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4258
     "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4259
     db->aDb[iDb].zName, zMaster, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4260
  if( zSql==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4261
  sqlite3SafetyOff(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4262
  assert( db->init.busy==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4263
  db->init.busy = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4264
  assert( !db->mallocFailed );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4265
  rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4266
  if( rc==SQLITE_ABORT ) rc = initData.rc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4267
  sqlite3_free(zSql);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4268
  db->init.busy = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4269
  sqlite3SafetyOn(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4270
  if( rc==SQLITE_NOMEM ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4271
    goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4272
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4273
  break;  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4274
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4275
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4276
#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4277
/* Opcode: LoadAnalysis P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4278
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4279
** Read the sqlite_stat1 table for database P1 and load the content
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4280
** of that table into the internal index hash table.  This will cause
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4281
** the analysis to be used when preparing all subsequent queries.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4282
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4283
case OP_LoadAnalysis: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4284
  int iDb = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4285
  assert( iDb>=0 && iDb<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4286
  rc = sqlite3AnalysisLoad(db, iDb);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4287
  break;  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4288
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4289
#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4290
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4291
/* Opcode: DropTable P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4292
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4293
** Remove the internal (in-memory) data structures that describe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4294
** the table named P3 in database P1.  This is called after a table
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4295
** is dropped in order to keep the internal representation of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4296
** schema consistent with what is on disk.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4297
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4298
case OP_DropTable: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4299
  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4300
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4301
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4302
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4303
/* Opcode: DropIndex P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4304
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4305
** Remove the internal (in-memory) data structures that describe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4306
** the index named P3 in database P1.  This is called after an index
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4307
** is dropped in order to keep the internal representation of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4308
** schema consistent with what is on disk.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4309
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4310
case OP_DropIndex: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4311
  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4312
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4313
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4314
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4315
/* Opcode: DropTrigger P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4316
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4317
** Remove the internal (in-memory) data structures that describe
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4318
** the trigger named P3 in database P1.  This is called after a trigger
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4319
** is dropped in order to keep the internal representation of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4320
** schema consistent with what is on disk.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4321
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4322
case OP_DropTrigger: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4323
  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4324
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4325
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4326
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4327
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4328
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4329
/* Opcode: IntegrityCk P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4330
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4331
** Do an analysis of the currently open database.  Push onto the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4332
** stack the text of an error message describing any problems.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4333
** If no problems are found, push a NULL onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4334
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4335
** P1 is the address of a memory cell that contains the maximum
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4336
** number of allowed errors.  At most mem[P1] errors will be reported.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4337
** In other words, the analysis stops as soon as mem[P1] errors are 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4338
** seen.  Mem[P1] is updated with the number of errors remaining.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4339
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4340
** The root page numbers of all tables in the database are integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4341
** values on the stack.  This opcode pulls as many integers as it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4342
** can off of the stack and uses those numbers as the root pages.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4343
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4344
** If P2 is not zero, the check is done on the auxiliary database
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4345
** file, not the main database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4346
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4347
** This opcode is used to implement the integrity_check pragma.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4348
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4349
case OP_IntegrityCk: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4350
  int nRoot;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4351
  int *aRoot;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4352
  int j;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4353
  int nErr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4354
  char *z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4355
  Mem *pnErr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4356
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4357
  for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4358
    if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4359
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4360
  assert( nRoot>0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4361
  aRoot = (int*)sqlite3_malloc( sizeof(int)*(nRoot+1) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4362
  if( aRoot==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4363
  j = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4364
  assert( j>=0 && j<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4365
  pnErr = &p->aMem[j];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4366
  assert( (pnErr->flags & MEM_Int)!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4367
  for(j=0; j<nRoot; j++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4368
    aRoot[j] = (pTos-j)->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4369
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4370
  aRoot[j] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4371
  popStack(&pTos, nRoot);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4372
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4373
  assert( pOp->p2>=0 && pOp->p2<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4374
  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4375
  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4376
                                 pnErr->u.i, &nErr);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4377
  pnErr->u.i -= nErr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4378
  if( nErr==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4379
    assert( z==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4380
    pTos->flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4381
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4382
    pTos->z = z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4383
    pTos->n = strlen(z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4384
    pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4385
    pTos->xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4386
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4387
  pTos->enc = SQLITE_UTF8;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4388
  sqlite3VdbeChangeEncoding(pTos, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4389
  sqlite3_free(aRoot);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4390
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4391
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4392
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4393
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4394
/* Opcode: FifoWrite * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4395
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4396
** Write the integer on the top of the stack
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4397
** into the Fifo.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4398
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4399
case OP_FifoWrite: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4400
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4401
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4402
  if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4403
    goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4404
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4405
  assert( (pTos->flags & MEM_Dyn)==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4406
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4407
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4408
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4409
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4410
/* Opcode: FifoRead * P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4411
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4412
** Attempt to read a single integer from the Fifo
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4413
** and push it onto the stack.  If the Fifo is empty
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4414
** push nothing but instead jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4415
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4416
case OP_FifoRead: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4417
  i64 v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4418
  CHECK_FOR_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4419
  if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4420
    pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4421
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4422
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4423
    pTos->u.i = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4424
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4425
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4426
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4427
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4428
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4429
#ifndef SQLITE_OMIT_TRIGGER
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4430
/* Opcode: ContextPush * * * 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4431
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4432
** Save the current Vdbe context such that it can be restored by a ContextPop
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4433
** opcode. The context stores the last insert row id, the last statement change
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4434
** count, and the current statement change count.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4435
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4436
case OP_ContextPush: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4437
  int i = p->contextStackTop++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4438
  Context *pContext;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4439
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4440
  assert( i>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4441
  /* FIX ME: This should be allocated as part of the vdbe at compile-time */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4442
  if( i>=p->contextStackDepth ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4443
    p->contextStackDepth = i+1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4444
    p->contextStack = (Context*)sqlite3DbReallocOrFree(db, p->contextStack,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4445
                                          sizeof(Context)*(i+1));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4446
    if( p->contextStack==0 ) goto no_mem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4447
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4448
  pContext = &p->contextStack[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4449
  pContext->lastRowid = db->lastRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4450
  pContext->nChange = p->nChange;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4451
  pContext->sFifo = p->sFifo;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4452
  sqlite3VdbeFifoInit(&p->sFifo);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4453
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4454
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4455
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4456
/* Opcode: ContextPop * * * 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4457
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4458
** Restore the Vdbe context to the state it was in when contextPush was last
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4459
** executed. The context stores the last insert row id, the last statement
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4460
** change count, and the current statement change count.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4461
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4462
case OP_ContextPop: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4463
  Context *pContext = &p->contextStack[--p->contextStackTop];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4464
  assert( p->contextStackTop>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4465
  db->lastRowid = pContext->lastRowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4466
  p->nChange = pContext->nChange;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4467
  sqlite3VdbeFifoClear(&p->sFifo);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4468
  p->sFifo = pContext->sFifo;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4469
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4470
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4471
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4472
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4473
/* Opcode: MemStore P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4474
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4475
** Write the top of the stack into memory location P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4476
** P1 should be a small integer since space is allocated
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4477
** for all memory locations between 0 and P1 inclusive.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4478
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4479
** After the data is stored in the memory location, the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4480
** stack is popped once if P2 is 1.  If P2 is zero, then
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4481
** the original data remains on the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4482
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4483
case OP_MemStore: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4484
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4485
  assert( pOp->p1>=0 && pOp->p1<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4486
  rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4487
  pTos--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4488
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4489
  /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4490
  ** restore the top of the stack to its original value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4491
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4492
  if( pOp->p2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4493
    break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4494
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4495
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4496
/* Opcode: MemLoad P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4497
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4498
** Push a copy of the value in memory location P1 onto the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4499
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4500
** If the value is a string, then the value pushed is a pointer to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4501
** the string that is stored in the memory location.  If the memory
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4502
** location is subsequently changed (using OP_MemStore) then the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4503
** value pushed onto the stack will change too.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4504
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4505
case OP_MemLoad: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4506
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4507
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4508
  pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4509
  sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4510
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4511
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4512
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4513
#ifndef SQLITE_OMIT_AUTOINCREMENT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4514
/* Opcode: MemMax P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4515
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4516
** Set the value of memory cell P1 to the maximum of its current value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4517
** and the value on the top of the stack.  The stack is unchanged.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4518
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4519
** This instruction throws an error if the memory cell is not initially
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4520
** an integer.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4521
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4522
case OP_MemMax: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4523
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4524
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4525
  assert( pTos>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4526
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4527
  pMem = &p->aMem[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4528
  sqlite3VdbeMemIntegerify(pMem);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4529
  sqlite3VdbeMemIntegerify(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4530
  if( pMem->u.i<pTos->u.i){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4531
    pMem->u.i = pTos->u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4532
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4533
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4534
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4535
#endif /* SQLITE_OMIT_AUTOINCREMENT */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4536
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4537
/* Opcode: MemIncr P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4538
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4539
** Increment the integer valued memory cell P2 by the value in P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4540
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4541
** It is illegal to use this instruction on a memory cell that does
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4542
** not contain an integer.  An assertion fault will result if you try.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4543
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4544
case OP_MemIncr: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4545
  int i = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4546
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4547
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4548
  pMem = &p->aMem[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4549
  assert( pMem->flags==MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4550
  pMem->u.i += pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4551
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4552
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4553
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4554
/* Opcode: IfMemPos P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4555
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4556
** If the value of memory cell P1 is 1 or greater, jump to P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4557
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4558
** It is illegal to use this instruction on a memory cell that does
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4559
** not contain an integer.  An assertion fault will result if you try.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4560
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4561
case OP_IfMemPos: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4562
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4563
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4564
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4565
  pMem = &p->aMem[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4566
  assert( pMem->flags==MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4567
  if( pMem->u.i>0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4568
     pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4569
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4570
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4571
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4572
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4573
/* Opcode: IfMemNeg P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4574
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4575
** If the value of memory cell P1 is less than zero, jump to P2. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4576
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4577
** It is illegal to use this instruction on a memory cell that does
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4578
** not contain an integer.  An assertion fault will result if you try.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4579
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4580
case OP_IfMemNeg: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4581
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4582
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4583
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4584
  pMem = &p->aMem[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4585
  assert( pMem->flags==MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4586
  if( pMem->u.i<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4587
     pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4588
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4589
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4590
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4591
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4592
/* Opcode: IfMemZero P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4593
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4594
** If the value of memory cell P1 is exactly 0, jump to P2. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4595
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4596
** It is illegal to use this instruction on a memory cell that does
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4597
** not contain an integer.  An assertion fault will result if you try.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4598
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4599
case OP_IfMemZero: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4600
  int i = pOp->p1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4601
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4602
  assert( i>=0 && i<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4603
  pMem = &p->aMem[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4604
  assert( pMem->flags==MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4605
  if( pMem->u.i==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4606
     pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4607
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4608
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4609
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4610
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4611
/* Opcode: MemNull P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4612
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4613
** Store a NULL in memory cell P1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4614
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4615
case OP_MemNull: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4616
  assert( pOp->p1>=0 && pOp->p1<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4617
  sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4618
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4619
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4620
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4621
/* Opcode: MemInt P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4622
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4623
** Store the integer value P1 in memory cell P2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4624
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4625
case OP_MemInt: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4626
  assert( pOp->p2>=0 && pOp->p2<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4627
  sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4628
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4629
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4630
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4631
/* Opcode: MemMove P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4632
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4633
** Move the content of memory cell P2 over to memory cell P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4634
** Any prior content of P1 is erased.  Memory cell P2 is left
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4635
** containing a NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4636
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4637
case OP_MemMove: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4638
  assert( pOp->p1>=0 && pOp->p1<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4639
  assert( pOp->p2>=0 && pOp->p2<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4640
  rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4641
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4642
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4643
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4644
/* Opcode: AggStep P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4645
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4646
** Execute the step function for an aggregate.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4647
** function has P2 arguments.  P3 is a pointer to the FuncDef
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4648
** structure that specifies the function.  Use memory location
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4649
** P1 as the accumulator.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4650
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4651
** The P2 arguments are popped from the stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4652
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4653
case OP_AggStep: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4654
  int n = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4655
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4656
  Mem *pMem, *pRec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4657
  sqlite3_context ctx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4658
  sqlite3_value **apVal;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4659
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4660
  assert( n>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4661
  pRec = &pTos[1-n];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4662
  assert( pRec>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4663
  apVal = p->apArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4664
  assert( apVal || n==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4665
  for(i=0; i<n; i++, pRec++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4666
    apVal[i] = pRec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4667
    storeTypeInfo(pRec, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4668
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4669
  ctx.pFunc = (FuncDef*)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4670
  assert( pOp->p1>=0 && pOp->p1<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4671
  ctx.pMem = pMem = &p->aMem[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4672
  pMem->n++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4673
  ctx.s.flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4674
  ctx.s.z = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4675
  ctx.s.xDel = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4676
  ctx.s.db = db;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4677
  ctx.isError = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4678
  ctx.pColl = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4679
  if( ctx.pFunc->needCollSeq ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4680
    assert( pOp>p->aOp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4681
    assert( pOp[-1].p3type==P3_COLLSEQ );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4682
    assert( pOp[-1].opcode==OP_CollSeq );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4683
    ctx.pColl = (CollSeq *)pOp[-1].p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4684
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4685
  (ctx.pFunc->xStep)(&ctx, n, apVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4686
  popStack(&pTos, n);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4687
  if( ctx.isError ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4688
    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4689
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4690
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4691
  sqlite3VdbeMemRelease(&ctx.s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4692
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4693
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4694
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4695
/* Opcode: AggFinal P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4696
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4697
** Execute the finalizer function for an aggregate.  P1 is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4698
** the memory location that is the accumulator for the aggregate.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4699
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4700
** P2 is the number of arguments that the step function takes and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4701
** P3 is a pointer to the FuncDef for this function.  The P2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4702
** argument is not used by this opcode.  It is only there to disambiguate
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4703
** functions that can take varying numbers of arguments.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4704
** P3 argument is only needed for the degenerate case where
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4705
** the step function was not previously called.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4706
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4707
case OP_AggFinal: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4708
  Mem *pMem;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4709
  assert( pOp->p1>=0 && pOp->p1<p->nMem );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4710
  pMem = &p->aMem[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4711
  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4712
  rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4713
  if( rc==SQLITE_ERROR ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4714
    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4715
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4716
  if( sqlite3VdbeMemTooBig(pMem) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4717
    goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4718
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4719
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4720
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4721
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4722
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4723
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4724
/* Opcode: Vacuum * * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4725
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4726
** Vacuum the entire database.  This opcode will cause other virtual
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4727
** machines to be created and run.  It may not be called from within
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4728
** a transaction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4729
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4730
case OP_Vacuum: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4731
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4732
  rc = sqlite3RunVacuum(&p->zErrMsg, db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4733
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4734
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4735
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4736
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4737
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4738
#if !defined(SQLITE_OMIT_AUTOVACUUM)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4739
/* Opcode: IncrVacuum P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4740
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4741
** Perform a single step of the incremental vacuum procedure on
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4742
** the P1 database. If the vacuum has finished, jump to instruction
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4743
** P2. Otherwise, fall through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4744
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4745
case OP_IncrVacuum: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4746
  Btree *pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4747
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4748
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4749
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4750
  pBt = db->aDb[pOp->p1].pBt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4751
  rc = sqlite3BtreeIncrVacuum(pBt);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4752
  if( rc==SQLITE_DONE ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4753
    pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4754
    rc = SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4755
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4756
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4757
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4758
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4759
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4760
/* Opcode: Expire P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4761
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4762
** Cause precompiled statements to become expired. An expired statement
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4763
** fails with an error code of SQLITE_SCHEMA if it is ever executed 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4764
** (via sqlite3_step()).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4765
** 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4766
** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4767
** then only the currently executing statement is affected. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4768
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4769
case OP_Expire: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4770
  if( !pOp->p1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4771
    sqlite3ExpirePreparedStatements(db);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4772
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4773
    p->expired = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4774
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4775
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4776
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4777
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4778
#ifndef SQLITE_OMIT_SHARED_CACHE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4779
/* Opcode: TableLock P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4780
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4781
** Obtain a lock on a particular table. This instruction is only used when
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4782
** the shared-cache feature is enabled. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4783
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4784
** If P1 is not negative, then it is the index of the database
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4785
** in sqlite3.aDb[] and a read-lock is required. If P1 is negative, a 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4786
** write-lock is required. In this case the index of the database is the 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4787
** absolute value of P1 minus one (iDb = abs(P1) - 1;) and a write-lock is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4788
** required. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4789
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4790
** P2 contains the root-page of the table to lock.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4791
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4792
** P3 contains a pointer to the name of the table being locked. This is only
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4793
** used to generate an error message if the lock cannot be obtained.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4794
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4795
case OP_TableLock: {        /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4796
  int p1 = pOp->p1; 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4797
  u8 isWriteLock = (p1<0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4798
  if( isWriteLock ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4799
    p1 = (-1*p1)-1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4800
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4801
  assert( p1>=0 && p1<db->nDb );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4802
  assert( (p->btreeMask & (1<<p1))!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4803
  rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4804
  if( rc==SQLITE_LOCKED ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4805
    const char *z = (const char *)pOp->p3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4806
    sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4807
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4808
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4809
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4810
#endif /* SQLITE_OMIT_SHARED_CACHE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4811
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4812
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4813
/* Opcode: VBegin * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4814
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4815
** P3 a pointer to an sqlite3_vtab structure. Call the xBegin method 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4816
** for that table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4817
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4818
case OP_VBegin: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4819
  rc = sqlite3VtabBegin(db, (sqlite3_vtab *)pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4820
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4821
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4822
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4823
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4824
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4825
/* Opcode: VCreate P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4826
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4827
** P3 is the name of a virtual table in database P1. Call the xCreate method
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4828
** for that table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4829
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4830
case OP_VCreate: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4831
  rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4832
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4833
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4834
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4835
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4836
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4837
/* Opcode: VDestroy P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4838
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4839
** P3 is the name of a virtual table in database P1.  Call the xDestroy method
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4840
** of that table.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4841
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4842
case OP_VDestroy: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4843
  p->inVtabMethod = 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4844
  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4845
  p->inVtabMethod = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4846
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4847
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4848
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4849
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4850
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4851
/* Opcode: VOpen P1 * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4852
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4853
** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4854
** P1 is a cursor number.  This opcode opens a cursor to the virtual
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4855
** table and stores that cursor in P1.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4856
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4857
case OP_VOpen: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4858
  Cursor *pCur = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4859
  sqlite3_vtab_cursor *pVtabCursor = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4860
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4861
  sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4862
  sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4863
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4864
  assert(pVtab && pModule);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4865
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4866
  rc = pModule->xOpen(pVtab, &pVtabCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4867
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4868
  if( SQLITE_OK==rc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4869
    /* Initialise sqlite3_vtab_cursor base class */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4870
    pVtabCursor->pVtab = pVtab;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4871
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4872
    /* Initialise vdbe cursor object */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4873
    pCur = allocateCursor(p, pOp->p1, -1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4874
    if( pCur ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4875
      pCur->pVtabCursor = pVtabCursor;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4876
      pCur->pModule = pVtabCursor->pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4877
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4878
      db->mallocFailed = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4879
      pModule->xClose(pVtabCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4880
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4881
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4882
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4883
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4884
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4885
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4886
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4887
/* Opcode: VFilter P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4888
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4889
** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4890
** the filtered result set is empty.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4891
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4892
** P3 is either NULL or a string that was generated by the xBestIndex
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4893
** method of the module.  The interpretation of the P3 string is left
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4894
** to the module implementation.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4895
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4896
** This opcode invokes the xFilter method on the virtual table specified
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4897
** by P1.  The integer query plan parameter to xFilter is the top of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4898
** stack.  Next down on the stack is the argc parameter.  Beneath the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4899
** next of stack are argc additional parameters which are passed to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4900
** xFilter as argv. The topmost parameter (i.e. 3rd element popped from
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4901
** the stack) becomes argv[argc-1] when passed to xFilter.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4902
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4903
** The integer query plan parameter, argc, and all argv stack values 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4904
** are popped from the stack before this instruction completes.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4905
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4906
** A jump is made to P2 if the result set after filtering would be 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4907
** empty.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4908
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4909
case OP_VFilter: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4910
  int nArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4911
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4912
  const sqlite3_module *pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4913
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4914
  Cursor *pCur = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4915
  assert( pCur->pVtabCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4916
  pModule = pCur->pVtabCursor->pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4917
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4918
  /* Grab the index number and argc parameters off the top of the stack. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4919
  assert( (&pTos[-1])>=p->aStack );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4920
  assert( (pTos[0].flags&MEM_Int)!=0 && pTos[-1].flags==MEM_Int );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4921
  nArg = pTos[-1].u.i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4922
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4923
  /* Invoke the xFilter method */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4924
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4925
    int res = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4926
    int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4927
    Mem **apArg = p->apArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4928
    for(i = 0; i<nArg; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4929
      apArg[i] = &pTos[i+1-2-nArg];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4930
      storeTypeInfo(apArg[i], 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4931
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4932
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4933
    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4934
    p->inVtabMethod = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4935
    rc = pModule->xFilter(pCur->pVtabCursor, pTos->u.i, pOp->p3, nArg, apArg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4936
    p->inVtabMethod = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4937
    if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4938
      res = pModule->xEof(pCur->pVtabCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4939
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4940
    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4941
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4942
    if( res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4943
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4944
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4945
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4946
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4947
  /* Pop the index number, argc value and parameters off the stack */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4948
  popStack(&pTos, 2+nArg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4949
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4950
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4951
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4952
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4953
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4954
/* Opcode: VRowid P1 * *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4955
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4956
** Push an integer onto the stack which is the rowid of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4957
** the virtual-table that the P1 cursor is pointing to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4958
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4959
case OP_VRowid: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4960
  const sqlite3_module *pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4961
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4962
  Cursor *pCur = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4963
  assert( pCur->pVtabCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4964
  pModule = pCur->pVtabCursor->pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4965
  if( pModule->xRowid==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4966
    sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4967
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4968
  } else {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4969
    sqlite_int64 iRow;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4970
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4971
    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4972
    rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4973
    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4974
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4975
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4976
    pTos->flags = MEM_Int;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4977
    pTos->u.i = iRow;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4978
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4979
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4980
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4981
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4982
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4983
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4984
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4985
/* Opcode: VColumn P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4986
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4987
** Push onto the stack the value of the P2-th column of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4988
** the row of the virtual-table that the P1 cursor is pointing to.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4989
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4990
case OP_VColumn: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4991
  const sqlite3_module *pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4992
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4993
  Cursor *pCur = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4994
  assert( pCur->pVtabCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4995
  pModule = pCur->pVtabCursor->pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4996
  if( pModule->xColumn==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4997
    sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4998
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  4999
  } else {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5000
    sqlite3_context sContext;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5001
    memset(&sContext, 0, sizeof(sContext));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5002
    sContext.s.flags = MEM_Null;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5003
    sContext.s.db = db;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5004
    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5005
    rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5006
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5007
    /* Copy the result of the function to the top of the stack. We
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5008
    ** do this regardless of whether or not an error occured to ensure any
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5009
    ** dynamic allocation in sContext.s (a Mem struct) is  released.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5010
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5011
    sqlite3VdbeChangeEncoding(&sContext.s, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5012
    pTos++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5013
    pTos->flags = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5014
    sqlite3VdbeMemMove(pTos, &sContext.s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5015
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5016
    if( sqlite3SafetyOn(db) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5017
      goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5018
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5019
    if( sqlite3VdbeMemTooBig(pTos) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5020
      goto too_big;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5021
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5022
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5023
  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5024
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5025
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5026
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5027
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5028
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5029
/* Opcode: VNext P1 P2 *
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5030
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5031
** Advance virtual table P1 to the next row in its result set and
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5032
** jump to instruction P2.  Or, if the virtual table has reached
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5033
** the end of its result set, then fall through to the next instruction.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5034
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5035
case OP_VNext: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5036
  const sqlite3_module *pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5037
  int res = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5038
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5039
  Cursor *pCur = p->apCsr[pOp->p1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5040
  assert( pCur->pVtabCursor );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5041
  pModule = pCur->pVtabCursor->pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5042
  if( pModule->xNext==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5043
    sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5044
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5045
  } else {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5046
    /* Invoke the xNext() method of the module. There is no way for the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5047
    ** underlying implementation to return an error if one occurs during
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5048
    ** xNext(). Instead, if an error occurs, true is returned (indicating that 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5049
    ** data is available) and the error code returned when xColumn or
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5050
    ** some other method is next invoked on the save virtual table cursor.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5051
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5052
    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5053
    p->inVtabMethod = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5054
    rc = pModule->xNext(pCur->pVtabCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5055
    p->inVtabMethod = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5056
    if( rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5057
      res = pModule->xEof(pCur->pVtabCursor);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5058
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5059
    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5060
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5061
    if( !res ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5062
      /* If there is data, jump to P2 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5063
      pc = pOp->p2 - 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5064
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5065
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5066
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5067
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5068
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5069
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5070
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5071
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5072
/* Opcode: VRename * * P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5073
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5074
** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5075
** This opcode invokes the corresponding xRename method. The value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5076
** on the top of the stack is popped and passed as the zName argument
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5077
** to the xRename method.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5078
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5079
case OP_VRename: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5080
  sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5081
  assert( pVtab->pModule->xRename );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5082
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5083
  Stringify(pTos, encoding);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5084
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5085
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5086
  sqlite3VtabLock(pVtab);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5087
  rc = pVtab->pModule->xRename(pVtab, pTos->z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5088
  sqlite3VtabUnlock(db, pVtab);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5089
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5090
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5091
  popStack(&pTos, 1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5092
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5093
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5094
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5095
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5096
#ifndef SQLITE_OMIT_VIRTUALTABLE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5097
/* Opcode: VUpdate P1 P2 P3
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5098
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5099
** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5100
** This opcode invokes the corresponding xUpdate method. P2 values
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5101
** are taken from the stack to pass to the xUpdate invocation. The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5102
** value on the top of the stack corresponds to the p2th element 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5103
** of the argv array passed to xUpdate.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5104
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5105
** The xUpdate method will do a DELETE or an INSERT or both.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5106
** The argv[0] element (which corresponds to the P2-th element down
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5107
** on the stack) is the rowid of a row to delete.  If argv[0] is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5108
** NULL then no deletion occurs.  The argv[1] element is the rowid
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5109
** of the new row.  This can be NULL to have the virtual table
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5110
** select the new rowid for itself.  The higher elements in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5111
** stack are the values of columns in the new row.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5112
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5113
** If P2==1 then no insert is performed.  argv[0] is the rowid of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5114
** a row to delete.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5115
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5116
** P1 is a boolean flag. If it is set to true and the xUpdate call
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5117
** is successful, then the value returned by sqlite3_last_insert_rowid() 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5118
** is set to the value of the rowid for the row just inserted.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5119
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5120
case OP_VUpdate: {   /* no-push */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5121
  sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5122
  sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5123
  int nArg = pOp->p2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5124
  assert( pOp->p3type==P3_VTAB );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5125
  if( pModule->xUpdate==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5126
    sqlite3SetString(&p->zErrMsg, "read-only table", 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5127
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5128
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5129
    int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5130
    sqlite_int64 rowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5131
    Mem **apArg = p->apArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5132
    Mem *pX = &pTos[1-nArg];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5133
    for(i = 0; i<nArg; i++, pX++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5134
      storeTypeInfo(pX, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5135
      apArg[i] = pX;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5136
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5137
    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5138
    sqlite3VtabLock(pVtab);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5139
    rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5140
    sqlite3VtabUnlock(db, pVtab);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5141
    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5142
    if( pOp->p1 && rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5143
      assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5144
      db->lastRowid = rowid;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5145
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5146
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5147
  popStack(&pTos, nArg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5148
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5149
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5150
#endif /* SQLITE_OMIT_VIRTUALTABLE */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5151
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5152
/* An other opcode is illegal...
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5153
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5154
default: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5155
  assert( 0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5156
  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5157
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5158
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5159
/*****************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5160
** The cases of the switch statement above this line should all be indented
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5161
** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5162
** readability.  From this point on down, the normal indentation rules are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5163
** restored.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5164
*****************************************************************************/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5165
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5166
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5167
    /* Make sure the stack limit was not exceeded */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5168
    assert( pTos<=pStackLimit );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5169
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5170
#ifdef VDBE_PROFILE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5171
    {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5172
      long long elapse = hwtime() - start;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5173
      pOp->cycles += elapse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5174
      pOp->cnt++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5175
#if 0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5176
        fprintf(stdout, "%10lld ", elapse);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5177
        sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5178
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5179
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5180
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5181
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5182
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5183
    /* Keep track of the size of the largest BLOB or STR that has appeared
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5184
    ** on the top of the VDBE stack.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5185
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5186
    if( pTos>=p->aStack && (pTos->flags & (MEM_Blob|MEM_Str))!=0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5187
         && pTos->n>sqlite3_max_blobsize ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5188
      sqlite3_max_blobsize = pTos->n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5189
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5190
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5191
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5192
    /* The following code adds nothing to the actual functionality
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5193
    ** of the program.  It is only here for testing and debugging.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5194
    ** On the other hand, it does burn CPU cycles every time through
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5195
    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5196
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5197
#ifndef NDEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5198
    /* Sanity checking on the top element of the stack. If the previous
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5199
    ** instruction was VNoChange, then the flags field of the top
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5200
    ** of the stack is set to 0. This is technically invalid for a memory
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5201
    ** cell, so avoid calling MemSanity() in this case.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5202
    */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5203
    if( pTos>=p->aStack && pTos->flags ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5204
      assert( pTos->db==db );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5205
      sqlite3VdbeMemSanity(pTos);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5206
      assert( !sqlite3VdbeMemTooBig(pTos) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5207
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5208
    assert( pc>=-1 && pc<p->nOp );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5209
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5210
#ifdef SQLITE_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5211
    /* Code for tracing the vdbe stack. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5212
    if( p->trace && pTos>=p->aStack ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5213
      int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5214
      fprintf(p->trace, "Stack:");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5215
      for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5216
        if( pTos[i].flags & MEM_Null ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5217
          fprintf(p->trace, " NULL");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5218
        }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5219
          fprintf(p->trace, " si:%lld", pTos[i].u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5220
        }else if( pTos[i].flags & MEM_Int ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5221
          fprintf(p->trace, " i:%lld", pTos[i].u.i);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5222
        }else if( pTos[i].flags & MEM_Real ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5223
          fprintf(p->trace, " r:%g", pTos[i].r);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5224
        }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5225
          char zBuf[200];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5226
          sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5227
          fprintf(p->trace, " ");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5228
          fprintf(p->trace, "%s", zBuf);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5229
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5230
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5231
      if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5232
      fprintf(p->trace,"\n");
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5233
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5234
#endif  /* SQLITE_DEBUG */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5235
#endif  /* NDEBUG */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5236
  }  /* The end of the for(;;) loop the loops through opcodes */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5237
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5238
  /* If we reach this point, it means that execution is finished.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5239
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5240
vdbe_halt:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5241
  if( rc ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5242
    p->rc = rc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5243
    rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5244
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5245
    rc = SQLITE_DONE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5246
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5247
  sqlite3VdbeHalt(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5248
  p->pTos = pTos;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5249
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5250
  /* This is the only way out of this procedure.  We have to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5251
  ** release the mutexes on btrees that were acquired at the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5252
  ** top. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5253
vdbe_return:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5254
  sqlite3BtreeMutexArrayLeave(&p->aMutex);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5255
  return rc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5256
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5257
  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5258
  ** is encountered.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5259
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5260
too_big:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5261
  sqlite3SetString(&p->zErrMsg, "string or blob too big", (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5262
  rc = SQLITE_TOOBIG;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5263
  goto vdbe_halt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5264
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5265
  /* Jump to here if a malloc() fails.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5266
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5267
no_mem:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5268
  db->mallocFailed = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5269
  sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5270
  rc = SQLITE_NOMEM;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5271
  goto vdbe_halt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5272
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5273
  /* Jump to here for an SQLITE_MISUSE error.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5274
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5275
abort_due_to_misuse:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5276
  rc = SQLITE_MISUSE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5277
  /* Fall thru into abort_due_to_error */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5278
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5279
  /* Jump to here for any other kind of fatal error.  The "rc" variable
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5280
  ** should hold the error number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5281
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5282
abort_due_to_error:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5283
  if( p->zErrMsg==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5284
    if( db->mallocFailed ) rc = SQLITE_NOMEM;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5285
    sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5286
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5287
  goto vdbe_halt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5288
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5289
  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5290
  ** flag.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5291
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5292
abort_due_to_interrupt:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5293
  assert( db->u1.isInterrupted );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5294
  if( db->magic!=SQLITE_MAGIC_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5295
    rc = SQLITE_MISUSE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5296
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5297
    rc = SQLITE_INTERRUPT;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5298
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5299
  p->rc = rc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5300
  sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5301
  goto vdbe_halt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  5302
}