os/persistentdata/persistentstorage/sql/SQLite/legacy.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite/legacy.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** Main file for the SQLite library.  The routines in this file
    1.16 +** implement the programmer interface to the library.  Routines in
    1.17 +** other files are for internal use by SQLite and should not be
    1.18 +** accessed by users of the library.
    1.19 +**
    1.20 +** $Id: legacy.c,v 1.29 2008/08/02 03:50:39 drh Exp $
    1.21 +*/
    1.22 +
    1.23 +#include "sqliteInt.h"
    1.24 +#include <ctype.h>
    1.25 +
    1.26 +/*
    1.27 +** Execute SQL code.  Return one of the SQLITE_ success/failure
    1.28 +** codes.  Also write an error message into memory obtained from
    1.29 +** malloc() and make *pzErrMsg point to that message.
    1.30 +**
    1.31 +** If the SQL is a query, then for each row in the query result
    1.32 +** the xCallback() function is called.  pArg becomes the first
    1.33 +** argument to xCallback().  If xCallback=NULL then no callback
    1.34 +** is invoked, even for queries.
    1.35 +*/
    1.36 +int sqlite3_exec(
    1.37 +  sqlite3 *db,                /* The database on which the SQL executes */
    1.38 +  const char *zSql,           /* The SQL to be executed */
    1.39 +  sqlite3_callback xCallback, /* Invoke this callback routine */
    1.40 +  void *pArg,                 /* First argument to xCallback() */
    1.41 +  char **pzErrMsg             /* Write error messages here */
    1.42 +){
    1.43 +  int rc = SQLITE_OK;
    1.44 +  const char *zLeftover;
    1.45 +  sqlite3_stmt *pStmt = 0;
    1.46 +  char **azCols = 0;
    1.47 +
    1.48 +  int nRetry = 0;
    1.49 +  int nCallback;
    1.50 +
    1.51 +  if( zSql==0 ) zSql = "";
    1.52 +
    1.53 +  sqlite3_mutex_enter(db->mutex);
    1.54 +  sqlite3Error(db, SQLITE_OK, 0);
    1.55 +  while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
    1.56 +    int nCol;
    1.57 +    char **azVals = 0;
    1.58 +
    1.59 +    pStmt = 0;
    1.60 +    rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
    1.61 +    assert( rc==SQLITE_OK || pStmt==0 );
    1.62 +    if( rc!=SQLITE_OK ){
    1.63 +      continue;
    1.64 +    }
    1.65 +    if( !pStmt ){
    1.66 +      /* this happens for a comment or white-space */
    1.67 +      zSql = zLeftover;
    1.68 +      continue;
    1.69 +    }
    1.70 +
    1.71 +    nCallback = 0;
    1.72 +    nCol = sqlite3_column_count(pStmt);
    1.73 +
    1.74 +    while( 1 ){
    1.75 +      int i;
    1.76 +      rc = sqlite3_step(pStmt);
    1.77 +
    1.78 +      /* Invoke the callback function if required */
    1.79 +      if( xCallback && (SQLITE_ROW==rc || 
    1.80 +          (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
    1.81 +        if( 0==nCallback ){
    1.82 +          if( azCols==0 ){
    1.83 +            azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
    1.84 +            if( azCols==0 ){
    1.85 +              goto exec_out;
    1.86 +            }
    1.87 +          }
    1.88 +          for(i=0; i<nCol; i++){
    1.89 +            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
    1.90 +            /* sqlite3VdbeSetColName() installs column names as UTF8
    1.91 +            ** strings so there is no way for sqlite3_column_name() to fail. */
    1.92 +            assert( azCols[i]!=0 );
    1.93 +          }
    1.94 +          nCallback++;
    1.95 +        }
    1.96 +        if( rc==SQLITE_ROW ){
    1.97 +          azVals = &azCols[nCol];
    1.98 +          for(i=0; i<nCol; i++){
    1.99 +            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
   1.100 +            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
   1.101 +              db->mallocFailed = 1;
   1.102 +              goto exec_out;
   1.103 +            }
   1.104 +          }
   1.105 +        }
   1.106 +        if( xCallback(pArg, nCol, azVals, azCols) ){
   1.107 +          rc = SQLITE_ABORT;
   1.108 +          sqlite3_finalize(pStmt);
   1.109 +          pStmt = 0;
   1.110 +          sqlite3Error(db, SQLITE_ABORT, 0);
   1.111 +          goto exec_out;
   1.112 +        }
   1.113 +      }
   1.114 +
   1.115 +      if( rc!=SQLITE_ROW ){
   1.116 +        rc = sqlite3_finalize(pStmt);
   1.117 +        pStmt = 0;
   1.118 +        if( rc!=SQLITE_SCHEMA ){
   1.119 +          nRetry = 0;
   1.120 +          zSql = zLeftover;
   1.121 +          while( isspace((unsigned char)zSql[0]) ) zSql++;
   1.122 +        }
   1.123 +        break;
   1.124 +      }
   1.125 +    }
   1.126 +
   1.127 +    sqlite3DbFree(db, azCols);
   1.128 +    azCols = 0;
   1.129 +  }
   1.130 +
   1.131 +exec_out:
   1.132 +  if( pStmt ) sqlite3_finalize(pStmt);
   1.133 +  sqlite3DbFree(db, azCols);
   1.134 +
   1.135 +  rc = sqlite3ApiExit(db, rc);
   1.136 +  if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
   1.137 +    int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
   1.138 +    *pzErrMsg = sqlite3Malloc(nErrMsg);
   1.139 +    if( *pzErrMsg ){
   1.140 +      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
   1.141 +    }
   1.142 +  }else if( pzErrMsg ){
   1.143 +    *pzErrMsg = 0;
   1.144 +  }
   1.145 +
   1.146 +  assert( (rc&db->errMask)==rc );
   1.147 +  sqlite3_mutex_leave(db->mutex);
   1.148 +  return rc;
   1.149 +}