OLD | NEW |
(Empty) | |
| 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 |
| 18 #include "sqliteInt.h" |
| 19 |
| 20 /* |
| 21 ** Execute SQL code. Return one of the SQLITE_ success/failure |
| 22 ** codes. Also write an error message into memory obtained from |
| 23 ** malloc() and make *pzErrMsg point to that message. |
| 24 ** |
| 25 ** If the SQL is a query, then for each row in the query result |
| 26 ** the xCallback() function is called. pArg becomes the first |
| 27 ** argument to xCallback(). If xCallback=NULL then no callback |
| 28 ** is invoked, even for queries. |
| 29 */ |
| 30 int sqlite3_exec( |
| 31 sqlite3 *db, /* The database on which the SQL executes */ |
| 32 const char *zSql, /* The SQL to be executed */ |
| 33 sqlite3_callback xCallback, /* Invoke this callback routine */ |
| 34 void *pArg, /* First argument to xCallback() */ |
| 35 char **pzErrMsg /* Write error messages here */ |
| 36 ){ |
| 37 int rc = SQLITE_OK; /* Return code */ |
| 38 const char *zLeftover; /* Tail of unprocessed SQL */ |
| 39 sqlite3_stmt *pStmt = 0; /* The current SQL statement */ |
| 40 char **azCols = 0; /* Names of result columns */ |
| 41 int callbackIsInit; /* True if callback data is initialized */ |
| 42 |
| 43 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 44 if( zSql==0 ) zSql = ""; |
| 45 |
| 46 sqlite3_mutex_enter(db->mutex); |
| 47 sqlite3Error(db, SQLITE_OK); |
| 48 while( rc==SQLITE_OK && zSql[0] ){ |
| 49 int nCol; |
| 50 char **azVals = 0; |
| 51 |
| 52 pStmt = 0; |
| 53 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 54 assert( rc==SQLITE_OK || pStmt==0 ); |
| 55 if( rc!=SQLITE_OK ){ |
| 56 continue; |
| 57 } |
| 58 if( !pStmt ){ |
| 59 /* this happens for a comment or white-space */ |
| 60 zSql = zLeftover; |
| 61 continue; |
| 62 } |
| 63 |
| 64 callbackIsInit = 0; |
| 65 nCol = sqlite3_column_count(pStmt); |
| 66 |
| 67 while( 1 ){ |
| 68 int i; |
| 69 rc = sqlite3_step(pStmt); |
| 70 |
| 71 /* Invoke the callback function if required */ |
| 72 if( xCallback && (SQLITE_ROW==rc || |
| 73 (SQLITE_DONE==rc && !callbackIsInit |
| 74 && db->flags&SQLITE_NullCallback)) ){ |
| 75 if( !callbackIsInit ){ |
| 76 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); |
| 77 if( azCols==0 ){ |
| 78 goto exec_out; |
| 79 } |
| 80 for(i=0; i<nCol; i++){ |
| 81 azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 82 /* sqlite3VdbeSetColName() installs column names as UTF8 |
| 83 ** strings so there is no way for sqlite3_column_name() to fail. */ |
| 84 assert( azCols[i]!=0 ); |
| 85 } |
| 86 callbackIsInit = 1; |
| 87 } |
| 88 if( rc==SQLITE_ROW ){ |
| 89 azVals = &azCols[nCol]; |
| 90 for(i=0; i<nCol; i++){ |
| 91 azVals[i] = (char *)sqlite3_column_text(pStmt, i); |
| 92 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){ |
| 93 db->mallocFailed = 1; |
| 94 goto exec_out; |
| 95 } |
| 96 } |
| 97 } |
| 98 if( xCallback(pArg, nCol, azVals, azCols) ){ |
| 99 /* EVIDENCE-OF: R-38229-40159 If the callback function to |
| 100 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will |
| 101 ** return SQLITE_ABORT. */ |
| 102 rc = SQLITE_ABORT; |
| 103 sqlite3VdbeFinalize((Vdbe *)pStmt); |
| 104 pStmt = 0; |
| 105 sqlite3Error(db, SQLITE_ABORT); |
| 106 goto exec_out; |
| 107 } |
| 108 } |
| 109 |
| 110 if( rc!=SQLITE_ROW ){ |
| 111 rc = sqlite3VdbeFinalize((Vdbe *)pStmt); |
| 112 pStmt = 0; |
| 113 zSql = zLeftover; |
| 114 while( sqlite3Isspace(zSql[0]) ) zSql++; |
| 115 break; |
| 116 } |
| 117 } |
| 118 |
| 119 sqlite3DbFree(db, azCols); |
| 120 azCols = 0; |
| 121 } |
| 122 |
| 123 exec_out: |
| 124 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt); |
| 125 sqlite3DbFree(db, azCols); |
| 126 |
| 127 rc = sqlite3ApiExit(db, rc); |
| 128 if( rc!=SQLITE_OK && pzErrMsg ){ |
| 129 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db)); |
| 130 *pzErrMsg = sqlite3Malloc(nErrMsg); |
| 131 if( *pzErrMsg ){ |
| 132 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 133 }else{ |
| 134 rc = SQLITE_NOMEM; |
| 135 sqlite3Error(db, SQLITE_NOMEM); |
| 136 } |
| 137 }else if( pzErrMsg ){ |
| 138 *pzErrMsg = 0; |
| 139 } |
| 140 |
| 141 assert( (rc&db->errMask)==rc ); |
| 142 sqlite3_mutex_leave(db->mutex); |
| 143 return rc; |
| 144 } |
OLD | NEW |