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 = 0; 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 callbackIsInit = 0; 000064 000065 while( 1 ){ 000066 int i; 000067 rc = sqlite3_step(pStmt); 000068 000069 /* Invoke the callback function if required */ 000070 if( xCallback && (SQLITE_ROW==rc || 000071 (SQLITE_DONE==rc && !callbackIsInit 000072 && db->flags&SQLITE_NullCallback)) ){ 000073 if( !callbackIsInit ){ 000074 nCol = sqlite3_column_count(pStmt); 000075 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*)); 000076 if( azCols==0 ){ 000077 goto exec_out; 000078 } 000079 for(i=0; i<nCol; i++){ 000080 azCols[i] = (char *)sqlite3_column_name(pStmt, i); 000081 /* sqlite3VdbeSetColName() installs column names as UTF8 000082 ** strings so there is no way for sqlite3_column_name() to fail. */ 000083 assert( azCols[i]!=0 ); 000084 } 000085 callbackIsInit = 1; 000086 } 000087 if( rc==SQLITE_ROW ){ 000088 azVals = &azCols[nCol]; 000089 for(i=0; i<nCol; i++){ 000090 azVals[i] = (char *)sqlite3_column_text(pStmt, i); 000091 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){ 000092 sqlite3OomFault(db); 000093 goto exec_out; 000094 } 000095 } 000096 azVals[i] = 0; 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 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db)); 000130 if( *pzErrMsg==0 ){ 000131 rc = SQLITE_NOMEM_BKPT; 000132 sqlite3Error(db, SQLITE_NOMEM); 000133 } 000134 }else if( pzErrMsg ){ 000135 *pzErrMsg = 0; 000136 } 000137 000138 assert( (rc&db->errMask)==rc ); 000139 sqlite3_mutex_leave(db->mutex); 000140 return rc; 000141 }