legacy.c

Go to the documentation of this file.
00001 /*
00002 ** 2001 September 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** Main file for the SQLite library.  The routines in this file
00013 ** implement the programmer interface to the library.  Routines in
00014 ** other files are for internal use by SQLite and should not be
00015 ** accessed by users of the library.
00016 **
00017 ** $Id: legacy.c,v 1.29 2008/08/02 03:50:39 drh Exp $
00018 */
00019 
00020 #include "sqliteInt.h"
00021 #include <ctype.h>
00022 
00023 /*
00024 ** Execute SQL code.  Return one of the SQLITE_ success/failure
00025 ** codes.  Also write an error message into memory obtained from
00026 ** malloc() and make *pzErrMsg point to that message.
00027 **
00028 ** If the SQL is a query, then for each row in the query result
00029 ** the xCallback() function is called.  pArg becomes the first
00030 ** argument to xCallback().  If xCallback=NULL then no callback
00031 ** is invoked, even for queries.
00032 */
00033 EXPORT_C int sqlite3_exec(
00034   sqlite3 *db,                /* The database on which the SQL executes */
00035   const char *zSql,           /* The SQL to be executed */
00036   sqlite3_callback xCallback, /* Invoke this callback routine */
00037   void *pArg,                 /* First argument to xCallback() */
00038   char **pzErrMsg             /* Write error messages here */
00039 ){
00040   int rc = SQLITE_OK;
00041   const char *zLeftover;
00042   sqlite3_stmt *pStmt = 0;
00043   char **azCols = 0;
00044 
00045   int nRetry = 0;
00046   int nCallback;
00047 
00048   if( zSql==0 ) zSql = "";
00049 
00050   sqlite3_mutex_enter(db->mutex);
00051   sqlite3Error(db, SQLITE_OK, 0);
00052   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
00053     int nCol;
00054     char **azVals = 0;
00055 
00056     pStmt = 0;
00057     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
00058     assert( rc==SQLITE_OK || pStmt==0 );
00059     if( rc!=SQLITE_OK ){
00060       continue;
00061     }
00062     if( !pStmt ){
00063       /* this happens for a comment or white-space */
00064       zSql = zLeftover;
00065       continue;
00066     }
00067 
00068     nCallback = 0;
00069     nCol = sqlite3_column_count(pStmt);
00070 
00071     while( 1 ){
00072       int i;
00073       rc = sqlite3_step(pStmt);
00074 
00075       /* Invoke the callback function if required */
00076       if( xCallback && (SQLITE_ROW==rc || 
00077           (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
00078         if( 0==nCallback ){
00079           if( azCols==0 ){
00080             azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
00081             if( azCols==0 ){
00082               goto exec_out;
00083             }
00084           }
00085           for(i=0; i<nCol; i++){
00086             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
00087             /* sqlite3VdbeSetColName() installs column names as UTF8
00088             ** strings so there is no way for sqlite3_column_name() to fail. */
00089             assert( azCols[i]!=0 );
00090           }
00091           nCallback++;
00092         }
00093         if( rc==SQLITE_ROW ){
00094           azVals = &azCols[nCol];
00095           for(i=0; i<nCol; i++){
00096             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
00097             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
00098               db->mallocFailed = 1;
00099               goto exec_out;
00100             }
00101           }
00102         }
00103         if( xCallback(pArg, nCol, azVals, azCols) ){
00104           rc = SQLITE_ABORT;
00105           sqlite3_finalize(pStmt);
00106           pStmt = 0;
00107           sqlite3Error(db, SQLITE_ABORT, 0);
00108           goto exec_out;
00109         }
00110       }
00111 
00112       if( rc!=SQLITE_ROW ){
00113         rc = sqlite3_finalize(pStmt);
00114         pStmt = 0;
00115         if( rc!=SQLITE_SCHEMA ){
00116           nRetry = 0;
00117           zSql = zLeftover;
00118           while( isspace((unsigned char)zSql[0]) ) zSql++;
00119         }
00120         break;
00121       }
00122     }
00123 
00124     sqlite3DbFree(db, azCols);
00125     azCols = 0;
00126   }
00127 
00128 exec_out:
00129   if( pStmt ) sqlite3_finalize(pStmt);
00130   sqlite3DbFree(db, azCols);
00131 
00132   rc = sqlite3ApiExit(db, rc);
00133   if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
00134     int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
00135     *pzErrMsg = sqlite3Malloc(nErrMsg);
00136     if( *pzErrMsg ){
00137       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
00138     }
00139   }else if( pzErrMsg ){
00140     *pzErrMsg = 0;
00141   }
00142 
00143   assert( (rc&db->errMask)==rc );
00144   sqlite3_mutex_leave(db->mutex);
00145   return rc;
00146 }

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1