ext/pdo_sqlite/sqlite/src/prepare.c
/*
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
*/
Compile the UTF-8 encoded SQL statement zSql into a statement handle.
Compile the UTF - 8 encoded SQL statement zSql into a statement handle .
int sqlite3Prepare(
  sqlite3 *db,              
  const char *zSql,         
  int nBytes,               
  int saveSqlFlag,          
  sqlite3_stmt **ppStmt,    
  const char **pzTail       
){
  Parse sParse;
  char *zErrMsg = 0;
  int rc = SQLITE_OK;
  int i;
  assert( !sqlite3MallocFailed() );
  assert( ppStmt );
  *ppStmt = 0;
  if( sqlite3SafetyOn(db) ){
    return SQLITE_MISUSE;
  }
  for(i=0; i<db->nDb; i++) {
    Btree *pBt = db->aDb[i].pBt;
    if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
      const char *zDb = db->aDb[i].zName;
      sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
      sqlite3SafetyOff(db);
      return SQLITE_LOCKED;
    }
  }
  memset(&sParse, 0, sizeof(sParse));
  sParse.db = db;
  if( nBytes>=0 && zSql[nBytes]!=0 ){
    char *zSqlCopy = sqlite3StrNDup(zSql, nBytes);
    sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
    sParse.zTail += zSql - zSqlCopy;
    sqliteFree(zSqlCopy);
  }else{
    sqlite3RunParser(&sParse, zSql, &zErrMsg);
  }
  if( sqlite3MallocFailed() ){
    sParse.rc = SQLITE_NOMEM;
  }
  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
  if( sParse.checkSchema && !schemaIsValid(db) ){
    sParse.rc = SQLITE_SCHEMA;
  }
  if( sParse.rc==SQLITE_SCHEMA ){
    sqlite3ResetInternalSchema(db, 0);
  }
  if( sqlite3MallocFailed() ){
    sParse.rc = SQLITE_NOMEM;
  }
  if( pzTail ){
    *pzTail = sParse.zTail;
  }
  rc = sParse.rc;
#ifndef SQLITE_OMIT_EXPLAIN
  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
    if( sParse.explain==2 ){
      sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
    }else{
      sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
      sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
    }
  }
#endif
  if( sqlite3SafetyOff(db) ){
    rc = SQLITE_MISUSE;
  }
  if( saveSqlFlag ){
    sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
  }
  if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
    sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
    assert(!(*ppStmt));
  }else{
    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
  }
  if( zErrMsg ){
    sqlite3Error(db, rc, "%s", zErrMsg);
    sqliteFree(zErrMsg);
  }else{
    sqlite3Error(db, rc, 0);
  }
  rc = sqlite3ApiExit(db, rc);
  sqlite3ReleaseThreadData();
  assert( (rc&db->errMask)==rc );
  return rc;
}
int sqlite3Prepare ( sqlite3 * db , const char * zSql , int nBytes , int saveSqlFlag , sqlite3_stmt * * ppStmt , const char * * pzTail ) { Parse sParse ; char * zErrMsg = 0 ; int rc = SQLITE_OK ; int i ; assert ( ! sqlite3MallocFailed ( ) ) ; assert ( ppStmt ) ; * ppStmt = 0 ; if ( sqlite3SafetyOn ( db ) ) { return SQLITE_MISUSE ; } for ( i = 0 ; i < db -> nDb ; i ++ ) { Btree * pBt = db -> aDb [ i ] . pBt ; if ( pBt && sqlite3BtreeSchemaLocked ( pBt ) ) { const char * zDb = db -> aDb [ i ] . zName ; sqlite3Error ( db , SQLITE_LOCKED , " " , zDb ) ; sqlite3SafetyOff ( db ) ; return SQLITE_LOCKED ; } } memset ( & sParse , 0 , sizeof ( sParse ) ) ; sParse . db = db ; if ( nBytes >= 0 && zSql [ nBytes ] != 0 ) { char * zSqlCopy = sqlite3StrNDup ( zSql , nBytes ) ; sqlite3RunParser ( & sParse , zSqlCopy , & zErrMsg ) ; sParse . zTail += zSql - zSqlCopy ; sqliteFree ( zSqlCopy ) ; } else { sqlite3RunParser ( & sParse , zSql , & zErrMsg ) ; } if ( sqlite3MallocFailed ( ) ) { sParse . rc = SQLITE_NOMEM ; } if ( sParse . rc == SQLITE_DONE ) sParse . rc = SQLITE_OK ; if ( sParse . checkSchema && ! schemaIsValid ( db ) ) { sParse . rc = SQLITE_SCHEMA ; } if ( sParse . rc == SQLITE_SCHEMA ) { sqlite3ResetInternalSchema ( db , 0 ) ; } if ( sqlite3MallocFailed ( ) ) { sParse . rc = SQLITE_NOMEM ; } if ( pzTail ) { * pzTail = sParse . zTail ; } rc = sParse . rc ; #ifndef SQLITE_OMIT_EXPLAIN if ( rc == SQLITE_OK && sParse . pVdbe && sParse . explain ) { if ( sParse . explain == 2 ) { sqlite3VdbeSetNumCols ( sParse . pVdbe , 3 ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 0 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 1 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 2 , COLNAME_NAME , " " , P3_STATIC ) ; } else { sqlite3VdbeSetNumCols ( sParse . pVdbe , 5 ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 0 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 1 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 2 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 3 , COLNAME_NAME , " " , P3_STATIC ) ; sqlite3VdbeSetColName ( sParse . pVdbe , 4 , COLNAME_NAME , " " , P3_STATIC ) ; } } #endif if ( sqlite3SafetyOff ( db ) ) { rc = SQLITE_MISUSE ; } if ( saveSqlFlag ) { sqlite3VdbeSetSql ( sParse . pVdbe , zSql , sParse . zTail - zSql ) ; } if ( rc != SQLITE_OK || sqlite3MallocFailed ( ) ) { sqlite3_finalize ( ( sqlite3_stmt * ) sParse . pVdbe ) ; assert ( ! ( * ppStmt ) ) ; } else { * ppStmt = ( sqlite3_stmt * ) sParse . pVdbe ; } if ( zErrMsg ) { sqlite3Error ( db , rc , " " , zErrMsg ) ; sqliteFree ( zErrMsg ) ; } else { sqlite3Error ( db , rc , 0 ) ; } rc = sqlite3ApiExit ( db , rc ) ; sqlite3ReleaseThreadData ( ) ; assert ( ( rc & db -> errMask ) == rc ) ; return rc ; }
Compile the UTF-8 encoded SQL statement zSql into a statement handle.
Compile the UTF - 8 encoded SQL statement zSql into a statement handle .
/* Database handle. */ /* UTF-8 encoded SQL statement. */ /* Length of zSql in bytes. */ /* True to copy SQL text into the sqlite3_stmt */ /* OUT: A pointer to the prepared statement */ /* OUT: End of parsed string */ /* Assert that malloc() has not failed */ /* If any attached database schemas are locked, do not proceed with
  ** compilation. Instead return SQLITE_LOCKED immediately.
  */