⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbe.c

📁 调用sqlite开源数据的小程序
💻 C
📖 第 1 页 / 共 5 页
字号:
** false if it has zero length and true otherwise.**** If the value popped of the stack is NULL, then take the jump if P1** is true and fall through if P1 is false.*//* Opcode: IfNot P1 P2 ***** Pop a single boolean from the stack.  If the boolean popped is** false, then jump to p2.  Otherwise continue to the next instruction.** An integer is false if zero and true otherwise.  A string is** false if it has zero length and true otherwise.**** If the value popped of the stack is NULL, then take the jump if P1** is true and fall through if P1 is false.*/case OP_If:                 /* no-push */case OP_IfNot: {            /* no-push */  int c;  assert( pTos>=p->aStack );  if( pTos->flags & MEM_Null ){    c = pOp->p1;  }else{#ifdef SQLITE_OMIT_FLOATING_POINT    c = sqlite3VdbeIntValue(pTos);#else    c = sqlite3VdbeRealValue(pTos)!=0.0;#endif    if( pOp->opcode==OP_IfNot ) c = !c;  }  Release(pTos);  pTos--;  if( c ) pc = pOp->p2-1;  break;}/* Opcode: IsNull P1 P2 ***** If any of the top abs(P1) values on the stack are NULL, then jump** to P2.  Pop the stack P1 times if P1>0.   If P1<0 leave the stack** unchanged.*/case OP_IsNull: {            /* same as TK_ISNULL, no-push */  int i, cnt;  Mem *pTerm;  cnt = pOp->p1;  if( cnt<0 ) cnt = -cnt;  pTerm = &pTos[1-cnt];  assert( pTerm>=p->aStack );  for(i=0; i<cnt; i++, pTerm++){    if( pTerm->flags & MEM_Null ){      pc = pOp->p2-1;      break;    }  }  if( pOp->p1>0 ) popStack(&pTos, cnt);  break;}/* Opcode: NotNull P1 P2 ***** Jump to P2 if the top P1 values on the stack are all not NULL.  Pop the** stack if P1 times if P1 is greater than zero.  If P1 is less than** zero then leave the stack unchanged.*/case OP_NotNull: {            /* same as TK_NOTNULL, no-push */  int i, cnt;  cnt = pOp->p1;  if( cnt<0 ) cnt = -cnt;  assert( &pTos[1-cnt] >= p->aStack );  for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}  if( i>=cnt ) pc = pOp->p2-1;  if( pOp->p1>0 ) popStack(&pTos, cnt);  break;}/* Opcode: SetNumColumns P1 P2 ***** Before the OP_Column opcode can be executed on a cursor, this** opcode must be called to set the number of fields in the table.**** This opcode sets the number of columns for cursor P1 to P2.**** If OP_KeyAsData is to be applied to cursor P1, it must be executed** before this op-code.*/case OP_SetNumColumns: {       /* no-push */  Cursor *pC;  assert( (pOp->p1)<p->nCursor );  assert( p->apCsr[pOp->p1]!=0 );  pC = p->apCsr[pOp->p1];  pC->nField = pOp->p2;  break;}/* Opcode: Column P1 P2 P3**** Interpret the data that cursor P1 points to as a structure built using** the MakeRecord instruction.  (See the MakeRecord opcode for additional** information about the format of the data.) Push onto the stack the value** of the P2-th column contained in the data. If there are less that (P2+1) ** values in the record, push a NULL onto the stack.**** If the KeyAsData opcode has previously executed on this cursor, then the** field might be extracted from the key rather than the data.**** If P1 is negative, then the record is stored on the stack rather than in** a table.  For P1==-1, the top of the stack is used.  For P1==-2, the** next on the stack is used.  And so forth.  The value pushed is always** just a pointer into the record which is stored further down on the** stack.  The column value is not copied. The number of columns in the** record is stored on the stack just above the record itself.**** If the column contains fewer than P2 fields, then push a NULL.  Or** if P3 is of type P3_MEM, then push the P3 value.  The P3 value will** be default value for a column that has been added using the ALTER TABLE** ADD COLUMN command.  If P3 is an ordinary string, just push a NULL.** When P3 is a string it is really just a comment describing the value** to be pushed, not a default value.*/case OP_Column: {  u32 payloadSize;   /* Number of bytes in the record */  int p1 = pOp->p1;  /* P1 value of the opcode */  int p2 = pOp->p2;  /* column number to retrieve */  Cursor *pC = 0;    /* The VDBE cursor */  char *zRec;        /* Pointer to complete record-data */  BtCursor *pCrsr;   /* The BTree cursor */  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */  u32 nField;        /* number of fields in the record */  u32 szHdr;         /* Number of bytes in the record header */  int len;           /* The length of the serialized data for the column */  int offset = 0;    /* Offset into the data */  int idx;           /* Index into the header */  int i;             /* Loop counter */  char *zData;       /* Part of the record being decoded */  Mem sMem;          /* For storing the record being decoded */  sMem.flags = 0;  assert( p1<p->nCursor );  pTos++;  pTos->flags = MEM_Null;  /* This block sets the variable payloadSize to be the total number of  ** bytes in the record.  **  ** zRec is set to be the complete text of the record if it is available.  ** The complete record text is always available for pseudo-tables and  ** when we are decoded a record from the stack.  If the record is stored  ** in a cursor, the complete record text might be available in the   ** pC->aRow cache.  Or it might not be.  If the data is unavailable,  ** zRec is set to NULL.  **  ** We also compute the number of columns in the record.  For cursors,  ** the number of columns is stored in the Cursor.nField element.  For  ** records on the stack, the next entry down on the stack is an integer  ** which is the number of records.  */  assert( p1<0 || p->apCsr[p1]!=0 );  if( p1<0 ){    /* Take the record off of the stack */    Mem *pRec = &pTos[p1];    Mem *pCnt = &pRec[-1];    assert( pRec>=p->aStack );    assert( pRec->flags & MEM_Blob );    payloadSize = pRec->n;    zRec = pRec->z;    assert( pCnt>=p->aStack );    assert( pCnt->flags & MEM_Int );    nField = pCnt->i;    pCrsr = 0;  }else if( (pC = p->apCsr[p1])->pCursor!=0 ){    /* The record is stored in a B-Tree */    rc = sqlite3VdbeCursorMoveto(pC);    if( rc ) goto abort_due_to_error;    zRec = 0;    pCrsr = pC->pCursor;    if( pC->nullRow ){      payloadSize = 0;    }else if( pC->cacheValid ){      payloadSize = pC->payloadSize;      zRec = pC->aRow;    }else if( pC->isIndex ){      i64 payloadSize64;      sqlite3BtreeKeySize(pCrsr, &payloadSize64);      payloadSize = payloadSize64;    }else{      sqlite3BtreeDataSize(pCrsr, &payloadSize);    }    nField = pC->nField;#ifndef SQLITE_OMIT_TRIGGER  }else if( pC->pseudoTable ){    /* The record is the sole entry of a pseudo-table */    payloadSize = pC->nData;    zRec = pC->pData;    pC->cacheValid = 0;    assert( payloadSize==0 || zRec!=0 );    nField = pC->nField;    pCrsr = 0;#endif  }else{    zRec = 0;    payloadSize = 0;    pCrsr = 0;    nField = 0;  }  /* If payloadSize is 0, then just push a NULL onto the stack. */  if( payloadSize==0 ){    pTos->flags = MEM_Null;    break;  }  assert( p2<nField );  /* Read and parse the table header.  Store the results of the parse  ** into the record header cache fields of the cursor.  */  if( pC && pC->cacheValid ){    aType = pC->aType;    aOffset = pC->aOffset;  }else{    int avail;    /* Number of bytes of available data */    if( pC && pC->aType ){      aType = pC->aType;    }else{      aType = sqliteMallocRaw( 2*nField*sizeof(aType) );    }    aOffset = &aType[nField];    if( aType==0 ){      goto no_mem;    }    /* Figure out how many bytes are in the header */    if( zRec ){      zData = zRec;    }else{      if( pC->isIndex ){        zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);      }else{        zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);      }      /* If KeyFetch()/DataFetch() managed to get the entire payload,      ** save the payload in the pC->aRow cache.  That will save us from      ** having to make additional calls to fetch the content portion of      ** the record.      */      if( avail>=payloadSize ){        zRec = pC->aRow = zData;      }else{        pC->aRow = 0;      }    }    idx = sqlite3GetVarint32(zData, &szHdr);    /* The KeyFetch() or DataFetch() above are fast and will get the entire    ** record header in most cases.  But they will fail to get the complete    ** record header if the record header does not fit on a single page    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to    ** acquire the complete header text.    */    if( !zRec && avail<szHdr ){      rc = sqlite3VdbeMemFromBtree(pCrsr, 0, szHdr, pC->isIndex, &sMem);      if( rc!=SQLITE_OK ){        goto op_column_out;      }      zData = sMem.z;    }    /* Scan the header and use it to fill in the aType[] and aOffset[]    ** arrays.  aType[i] will contain the type integer for the i-th    ** column and aOffset[i] will contain the offset from the beginning    ** of the record to the start of the data for the i-th column    */    offset = szHdr;    assert( offset>0 );    i = 0;    while( idx<szHdr && i<nField && offset<=payloadSize ){      aOffset[i] = offset;      idx += sqlite3GetVarint32(&zData[idx], &aType[i]);      offset += sqlite3VdbeSerialTypeLen(aType[i]);      i++;    }    Release(&sMem);    sMem.flags = MEM_Null;    /* If i is less that nField, then there are less fields in this    ** record than SetNumColumns indicated there are columns in the    ** table. Set the offset for any extra columns not present in    ** the record to 0. This tells code below to push a NULL onto the    ** stack instead of deserializing a value from the record.    */    while( i<nField ){      aOffset[i++] = 0;    }    /* The header should end at the start of data and the data should    ** end at last byte of the record. If this is not the case then    ** we are dealing with a malformed record.    */    if( idx!=szHdr || offset!=payloadSize ){      rc = SQLITE_CORRUPT_BKPT;      goto op_column_out;    }    /* Remember all aType and aColumn information if we have a cursor    ** to remember it in. */    if( pC ){      pC->payloadSize = payloadSize;      pC->aType = aType;      pC->aOffset = aOffset;      pC->cacheValid = 1;    }  }  /* Get the column information. If aOffset[p2] is non-zero, then   ** deserialize the value from the record. If aOffset[p2] is zero,  ** then there are not enough fields in the record to satisfy the  ** request.  In this case, set the value NULL or to P3 if P3 is  ** a pointer to a Mem object.  */  if( aOffset[p2] ){    assert( rc==SQLITE_OK );    if( zRec ){      zData = &zRec[aOffset[p2]];    }else{      len = sqlite3VdbeSerialTypeLen(aType[p2]);      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);      if( rc!=SQLITE_OK ){        goto op_column_out;      }      zData = sMem.z;    }    sqlite3VdbeSerialGet(zData, aType[p2], pTos);    pTos->enc = db->enc;  }else{    if( pOp->p3type==P3_MEM ){      sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);    }else{      pTos->flags = MEM_Null;    }  }  /* If we dynamically allocated space to hold the data (in the  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that  ** dynamically allocated space over to the pTos structure rather.  ** This prevents a memory copy.  */  if( (sMem.flags & MEM_Dyn)!=0 ){    assert( pTos->flags & MEM_Ephem );    assert( pTos->flags & (MEM_Str|MEM_Blob) );    assert( pTos->z==sMem.z );    assert( sMem.flags & MEM_Term );    pTos->flags &= ~MEM_Ephem;    pTos->flags |= MEM_Dyn|MEM_Term;  }  /* pTos->z might be pointing to sMem.zShort[].  Fix that so that we  ** can abandon sMem */  rc = sqlite3VdbeMemMakeWriteable(pTos);op_column_out:  /* Release the aType[] memory if we are not dealing with cursor */  if( !pC || !pC->aType ){    sqliteFree(aType);  }  break;}/* Opcode: MakeRecord P1 P2 P3**** Convert the top abs(P1) entries of the stack into a single entry** suitable for use as a data record in a database table or as a key** in an index.  The details of the format are irrelavant as long as** the OP_Column opcode can decode the record later and as long as the** sqlite3VdbeRecordCompare function will correctly compare two encoded** records.  Refer to source code comments for the details of the record** format.**** The original stack entries are popped from the stack if P1>0 but** remain on the stack if P1<0.**** If P2 is not zero and one or more of the entries are NULL, then jump** to the address given by P2.  This feature can be used to skip a** uniqueness test on indices.**** P3 may be a string that is P1 characters long.  The nth character of the** string indicates the column affinity that should be used for the nth** field of the index key (i.e. the first character of P3 corresponds to the** lowest element on the stack).**** The mapping from character to affinity is as follows:**    'n' = NUMERIC.**    'i' = INTEGER.**    't' = TEXT.**    'o' = NONE.**** If P3 is NULL then all index fields have the affinity NONE.**** See also OP_MakeIdxRec*//* Opcode: MakeRecordI P1 P2 P3**** This opcode works just OP_MakeRecord except that it reads an extra** integer from the stack (thus reading a total of abs(P1+1) entries)** and appends that extra integer to the end of the record as a varint.** This results in an index key.*/case OP_MakeIdxRec:case OP_MakeRecord: {  /* Assuming the record contains N fields, the record format looks  ** like this:  **  ** ------------------------------------------------------------------------  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |   ** ------------------------------------------------------------------------  **  ** Data(0) is taken from the lowest element of the stack and data(N-1) is  ** the top of the stack.  **  ** Each type field is a varint repr

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -