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