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 = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); 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 } 000098 if( xCallback(pArg, nCol, azVals, azCols) ){ 000099 /* EVIDENCE-OF: R-38229-40159 If the callback function to 000100 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will 000101 ** return SQLITE_ABORT. */ 000102 rc = SQLITE_ABORT; 000103 sqlite3VdbeFinalize((Vdbe *)pStmt); 000104 pStmt = 0; 000105 sqlite3Error(db, SQLITE_ABORT); 000106 goto exec_out; 000107 } 000108 } 000109 000110 if( rc!=SQLITE_ROW ){ 000111 rc = sqlite3VdbeFinalize((Vdbe *)pStmt); 000112 pStmt = 0; 000113 zSql = zLeftover; 000114 while( sqlite3Isspace(zSql[0]) ) zSql++; 000115 break; 000116 } 000117 } 000118 000119 sqlite3DbFree(db, azCols); 000120 azCols = 0; 000121 } 000122 000123 exec_out: 000124 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt); 000125 sqlite3DbFree(db, azCols); 000126 000127 rc = sqlite3ApiExit(db, rc); 000128 if( rc!=SQLITE_OK && pzErrMsg ){ 000129 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db)); 000130 *pzErrMsg = sqlite3Malloc(nErrMsg); 000131 if( *pzErrMsg ){ 000132 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); 000133 }else{ 000134 rc = SQLITE_NOMEM_BKPT; 000135 sqlite3Error(db, SQLITE_NOMEM); 000136 } 000137 }else if( pzErrMsg ){ 000138 *pzErrMsg = 0; 000139 } 000140 000141 assert( (rc&db->errMask)==rc ); 000142 sqlite3_mutex_leave(db->mutex); 000143 return rc; 000144 }