Back to index

lightning-sunbird  0.9+nobinonly
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.14 2006/03/06 20:55:46 drh Exp $
00018 */
00019 
00020 #include "sqliteInt.h"
00021 #include "os.h"
00022 #include <ctype.h>
00023 
00024 /*
00025 ** Execute SQL code.  Return one of the SQLITE_ success/failure
00026 ** codes.  Also write an error message into memory obtained from
00027 ** malloc() and make *pzErrMsg point to that message.
00028 **
00029 ** If the SQL is a query, then for each row in the query result
00030 ** the xCallback() function is called.  pArg becomes the first
00031 ** argument to xCallback().  If xCallback=NULL then no callback
00032 ** is invoked, even for queries.
00033 */
00034 int sqlite3_exec(
00035   sqlite3 *db,                /* The database on which the SQL executes */
00036   const char *zSql,           /* The SQL to be executed */
00037   sqlite3_callback xCallback, /* Invoke this callback routine */
00038   void *pArg,                 /* First argument to xCallback() */
00039   char **pzErrMsg             /* Write error messages here */
00040 ){
00041   int rc = SQLITE_OK;
00042   const char *zLeftover;
00043   sqlite3_stmt *pStmt = 0;
00044   char **azCols = 0;
00045 
00046   int nRetry = 0;
00047   int nChange = 0;
00048   int nCallback;
00049 
00050   if( zSql==0 ) return SQLITE_OK;
00051   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
00052     int nCol;
00053     char **azVals = 0;
00054 
00055     pStmt = 0;
00056     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
00057     assert( rc==SQLITE_OK || pStmt==0 );
00058     if( rc!=SQLITE_OK ){
00059       continue;
00060     }
00061     if( !pStmt ){
00062       /* this happens for a comment or white-space */
00063       zSql = zLeftover;
00064       continue;
00065     }
00066 
00067     db->nChange += nChange;
00068     nCallback = 0;
00069 
00070     nCol = sqlite3_column_count(pStmt);
00071     azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
00072     if( azCols==0 ){
00073       goto exec_out;
00074     }
00075 
00076     while( 1 ){
00077       int i;
00078       rc = sqlite3_step(pStmt);
00079 
00080       /* Invoke the callback function if required */
00081       if( xCallback && (SQLITE_ROW==rc || 
00082           (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
00083         if( 0==nCallback ){
00084           for(i=0; i<nCol; i++){
00085             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
00086           }
00087           nCallback++;
00088         }
00089         if( rc==SQLITE_ROW ){
00090           azVals = &azCols[nCol];
00091           for(i=0; i<nCol; i++){
00092             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
00093           }
00094         }
00095         if( xCallback(pArg, nCol, azVals, azCols) ){
00096           rc = SQLITE_ABORT;
00097           goto exec_out;
00098         }
00099       }
00100 
00101       if( rc!=SQLITE_ROW ){
00102         rc = sqlite3_finalize(pStmt);
00103         pStmt = 0;
00104         if( db->pVdbe==0 ){
00105           nChange = db->nChange;
00106         }
00107         if( rc!=SQLITE_SCHEMA ){
00108           nRetry = 0;
00109           zSql = zLeftover;
00110           while( isspace((unsigned char)zSql[0]) ) zSql++;
00111         }
00112         break;
00113       }
00114     }
00115 
00116     sqliteFree(azCols);
00117     azCols = 0;
00118   }
00119 
00120 exec_out:
00121   if( pStmt ) sqlite3_finalize(pStmt);
00122   if( azCols ) sqliteFree(azCols);
00123 
00124   rc = sqlite3ApiExit(0, rc);
00125   if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
00126     *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
00127     if( *pzErrMsg ){
00128       strcpy(*pzErrMsg, sqlite3_errmsg(db));
00129     }
00130   }else if( pzErrMsg ){
00131     *pzErrMsg = 0;
00132   }
00133 
00134   return rc;
00135 }