000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** Main file for the SQLite library.  The routines in this file
000013  ** implement the programmer interface to the library.  Routines in
000014  ** other files are for internal use by SQLite and should not be
000015  ** accessed by users of the library.
000016  */
000017  
000018  #include "sqliteInt.h"
000019  
000020  /*
000021  ** Execute SQL code.  Return one of the SQLITE_ success/failure
000022  ** codes.  Also write an error message into memory obtained from
000023  ** malloc() and make *pzErrMsg point to that message.
000024  **
000025  ** If the SQL is a query, then for each row in the query result
000026  ** the xCallback() function is called.  pArg becomes the first
000027  ** argument to xCallback().  If xCallback=NULL then no callback
000028  ** is invoked, even for queries.
000029  */
000030  int sqlite3_exec(
000031    sqlite3 *db,                /* The database on which the SQL executes */
000032    const char *zSql,           /* The SQL to be executed */
000033    sqlite3_callback xCallback, /* Invoke this callback routine */
000034    void *pArg,                 /* First argument to xCallback() */
000035    char **pzErrMsg             /* Write error messages here */
000036  ){
000037    int rc = SQLITE_OK;         /* Return code */
000038    const char *zLeftover;      /* Tail of unprocessed SQL */
000039    sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
000040    char **azCols = 0;          /* Names of result columns */
000041    int callbackIsInit;         /* True if callback data is initialized */
000042  
000043    if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
000044    if( zSql==0 ) zSql = "";
000045  
000046    sqlite3_mutex_enter(db->mutex);
000047    sqlite3Error(db, SQLITE_OK);
000048    while( rc==SQLITE_OK && zSql[0] ){
000049      int nCol;
000050      char **azVals = 0;
000051  
000052      pStmt = 0;
000053      rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
000054      assert( rc==SQLITE_OK || pStmt==0 );
000055      if( rc!=SQLITE_OK ){
000056        continue;
000057      }
000058      if( !pStmt ){
000059        /* this happens for a comment or white-space */
000060        zSql = zLeftover;
000061        continue;
000062      }
000063  
000064      callbackIsInit = 0;
000065      nCol = sqlite3_column_count(pStmt);
000066  
000067      while( 1 ){
000068        int i;
000069        rc = sqlite3_step(pStmt);
000070  
000071        /* Invoke the callback function if required */
000072        if( xCallback && (SQLITE_ROW==rc || 
000073            (SQLITE_DONE==rc && !callbackIsInit
000074                             && db->flags&SQLITE_NullCallback)) ){
000075          if( !callbackIsInit ){
000076            azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
000077            if( azCols==0 ){
000078              goto exec_out;
000079            }
000080            for(i=0; i<nCol; i++){
000081              azCols[i] = (char *)sqlite3_column_name(pStmt, i);
000082              /* sqlite3VdbeSetColName() installs column names as UTF8
000083              ** strings so there is no way for sqlite3_column_name() to fail. */
000084              assert( azCols[i]!=0 );
000085            }
000086            callbackIsInit = 1;
000087          }
000088          if( rc==SQLITE_ROW ){
000089            azVals = &azCols[nCol];
000090            for(i=0; i<nCol; i++){
000091              azVals[i] = (char *)sqlite3_column_text(pStmt, i);
000092              if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
000093                sqlite3OomFault(db);
000094                goto exec_out;
000095              }
000096            }
000097            azVals[i] = 0;
000098          }
000099          if( xCallback(pArg, nCol, azVals, azCols) ){
000100            /* EVIDENCE-OF: R-38229-40159 If the callback function to
000101            ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
000102            ** return SQLITE_ABORT. */
000103            rc = SQLITE_ABORT;
000104            sqlite3VdbeFinalize((Vdbe *)pStmt);
000105            pStmt = 0;
000106            sqlite3Error(db, SQLITE_ABORT);
000107            goto exec_out;
000108          }
000109        }
000110  
000111        if( rc!=SQLITE_ROW ){
000112          rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
000113          pStmt = 0;
000114          zSql = zLeftover;
000115          while( sqlite3Isspace(zSql[0]) ) zSql++;
000116          break;
000117        }
000118      }
000119  
000120      sqlite3DbFree(db, azCols);
000121      azCols = 0;
000122    }
000123  
000124  exec_out:
000125    if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
000126    sqlite3DbFree(db, azCols);
000127  
000128    rc = sqlite3ApiExit(db, rc);
000129    if( rc!=SQLITE_OK && pzErrMsg ){
000130      *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
000131      if( *pzErrMsg==0 ){
000132        rc = SQLITE_NOMEM_BKPT;
000133        sqlite3Error(db, SQLITE_NOMEM);
000134      }
000135    }else if( pzErrMsg ){
000136      *pzErrMsg = 0;
000137    }
000138  
000139    assert( (rc&db->errMask)==rc );
000140    sqlite3_mutex_leave(db->mutex);
000141    return rc;
000142  }