persistentstorage/sql/SQLite364/legacy.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 ** 2001 September 15
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 ** Main file for the SQLite library.  The routines in this file
       
    13 ** implement the programmer interface to the library.  Routines in
       
    14 ** other files are for internal use by SQLite and should not be
       
    15 ** accessed by users of the library.
       
    16 **
       
    17 ** $Id: legacy.c,v 1.29 2008/08/02 03:50:39 drh Exp $
       
    18 */
       
    19 
       
    20 #include "sqliteInt.h"
       
    21 #include <ctype.h>
       
    22 
       
    23 /*
       
    24 ** Execute SQL code.  Return one of the SQLITE_ success/failure
       
    25 ** codes.  Also write an error message into memory obtained from
       
    26 ** malloc() and make *pzErrMsg point to that message.
       
    27 **
       
    28 ** If the SQL is a query, then for each row in the query result
       
    29 ** the xCallback() function is called.  pArg becomes the first
       
    30 ** argument to xCallback().  If xCallback=NULL then no callback
       
    31 ** is invoked, even for queries.
       
    32 */
       
    33 int sqlite3_exec(
       
    34   sqlite3 *db,                /* The database on which the SQL executes */
       
    35   const char *zSql,           /* The SQL to be executed */
       
    36   sqlite3_callback xCallback, /* Invoke this callback routine */
       
    37   void *pArg,                 /* First argument to xCallback() */
       
    38   char **pzErrMsg             /* Write error messages here */
       
    39 ){
       
    40   int rc = SQLITE_OK;
       
    41   const char *zLeftover;
       
    42   sqlite3_stmt *pStmt = 0;
       
    43   char **azCols = 0;
       
    44 
       
    45   int nRetry = 0;
       
    46   int nCallback;
       
    47 
       
    48   if( zSql==0 ) zSql = "";
       
    49 
       
    50   sqlite3_mutex_enter(db->mutex);
       
    51   sqlite3Error(db, SQLITE_OK, 0);
       
    52   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
       
    53     int nCol;
       
    54     char **azVals = 0;
       
    55 
       
    56     pStmt = 0;
       
    57     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
       
    58     assert( rc==SQLITE_OK || pStmt==0 );
       
    59     if( rc!=SQLITE_OK ){
       
    60       continue;
       
    61     }
       
    62     if( !pStmt ){
       
    63       /* this happens for a comment or white-space */
       
    64       zSql = zLeftover;
       
    65       continue;
       
    66     }
       
    67 
       
    68     nCallback = 0;
       
    69     nCol = sqlite3_column_count(pStmt);
       
    70 
       
    71     while( 1 ){
       
    72       int i;
       
    73       rc = sqlite3_step(pStmt);
       
    74 
       
    75       /* Invoke the callback function if required */
       
    76       if( xCallback && (SQLITE_ROW==rc || 
       
    77           (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
       
    78         if( 0==nCallback ){
       
    79           if( azCols==0 ){
       
    80             azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
       
    81             if( azCols==0 ){
       
    82               goto exec_out;
       
    83             }
       
    84           }
       
    85           for(i=0; i<nCol; i++){
       
    86             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
       
    87             /* sqlite3VdbeSetColName() installs column names as UTF8
       
    88             ** strings so there is no way for sqlite3_column_name() to fail. */
       
    89             assert( azCols[i]!=0 );
       
    90           }
       
    91           nCallback++;
       
    92         }
       
    93         if( rc==SQLITE_ROW ){
       
    94           azVals = &azCols[nCol];
       
    95           for(i=0; i<nCol; i++){
       
    96             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
       
    97             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
       
    98               db->mallocFailed = 1;
       
    99               goto exec_out;
       
   100             }
       
   101           }
       
   102         }
       
   103         if( xCallback(pArg, nCol, azVals, azCols) ){
       
   104           rc = SQLITE_ABORT;
       
   105           sqlite3_finalize(pStmt);
       
   106           pStmt = 0;
       
   107           sqlite3Error(db, SQLITE_ABORT, 0);
       
   108           goto exec_out;
       
   109         }
       
   110       }
       
   111 
       
   112       if( rc!=SQLITE_ROW ){
       
   113         rc = sqlite3_finalize(pStmt);
       
   114         pStmt = 0;
       
   115         if( rc!=SQLITE_SCHEMA ){
       
   116           nRetry = 0;
       
   117           zSql = zLeftover;
       
   118           while( isspace((unsigned char)zSql[0]) ) zSql++;
       
   119         }
       
   120         break;
       
   121       }
       
   122     }
       
   123 
       
   124     sqlite3DbFree(db, azCols);
       
   125     azCols = 0;
       
   126   }
       
   127 
       
   128 exec_out:
       
   129   if( pStmt ) sqlite3_finalize(pStmt);
       
   130   sqlite3DbFree(db, azCols);
       
   131 
       
   132   rc = sqlite3ApiExit(db, rc);
       
   133   if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
       
   134     int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
       
   135     *pzErrMsg = sqlite3Malloc(nErrMsg);
       
   136     if( *pzErrMsg ){
       
   137       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
       
   138     }
       
   139   }else if( pzErrMsg ){
       
   140     *pzErrMsg = 0;
       
   141   }
       
   142 
       
   143   assert( (rc&db->errMask)==rc );
       
   144   sqlite3_mutex_leave(db->mutex);
       
   145   return rc;
       
   146 }