danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 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 | ** |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 17 | ** $Id: legacy.c,v 1.32 2009/03/19 18:51:07 danielk1977 Exp $ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "sqliteInt.h" |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 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( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 33 | sqlite3 *db, /* The database on which the SQL executes */ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 34 | const char *zSql, /* The SQL to be executed */ |
drh | 12057d5 | 2004-09-06 17:34:12 +0000 | [diff] [blame] | 35 | sqlite3_callback xCallback, /* Invoke this callback routine */ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 36 | void *pArg, /* First argument to xCallback() */ |
| 37 | char **pzErrMsg /* Write error messages here */ |
| 38 | ){ |
| 39 | int rc = SQLITE_OK; |
| 40 | const char *zLeftover; |
| 41 | sqlite3_stmt *pStmt = 0; |
| 42 | char **azCols = 0; |
| 43 | |
| 44 | int nRetry = 0; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 45 | int nCallback; |
| 46 | |
drh | 35c6190 | 2008-05-20 15:44:30 +0000 | [diff] [blame] | 47 | if( zSql==0 ) zSql = ""; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 48 | |
| 49 | sqlite3_mutex_enter(db->mutex); |
drh | 35c6190 | 2008-05-20 15:44:30 +0000 | [diff] [blame] | 50 | sqlite3Error(db, SQLITE_OK, 0); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 51 | while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){ |
| 52 | int nCol; |
| 53 | char **azVals = 0; |
| 54 | |
| 55 | pStmt = 0; |
| 56 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 57 | assert( rc==SQLITE_OK || pStmt==0 ); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 58 | if( rc!=SQLITE_OK ){ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 59 | continue; |
| 60 | } |
| 61 | if( !pStmt ){ |
| 62 | /* this happens for a comment or white-space */ |
| 63 | zSql = zLeftover; |
| 64 | continue; |
| 65 | } |
| 66 | |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 67 | nCallback = 0; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 68 | nCol = sqlite3_column_count(pStmt); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 69 | |
| 70 | while( 1 ){ |
| 71 | int i; |
| 72 | rc = sqlite3_step(pStmt); |
| 73 | |
| 74 | /* Invoke the callback function if required */ |
| 75 | if( xCallback && (SQLITE_ROW==rc || |
| 76 | (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){ |
| 77 | if( 0==nCallback ){ |
drh | 3b6f734 | 2008-03-21 18:01:14 +0000 | [diff] [blame] | 78 | if( azCols==0 ){ |
| 79 | azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); |
| 80 | if( azCols==0 ){ |
| 81 | goto exec_out; |
| 82 | } |
| 83 | } |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 84 | for(i=0; i<nCol; i++){ |
| 85 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 86 | /* sqlite3VdbeSetColName() installs column names as UTF8 |
| 87 | ** strings so there is no way for sqlite3_column_name() to fail. */ |
| 88 | assert( azCols[i]!=0 ); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 89 | } |
| 90 | nCallback++; |
| 91 | } |
| 92 | if( rc==SQLITE_ROW ){ |
| 93 | azVals = &azCols[nCol]; |
| 94 | for(i=0; i<nCol; i++){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 95 | azVals[i] = (char *)sqlite3_column_text(pStmt, i); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 96 | if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){ |
| 97 | db->mallocFailed = 1; |
| 98 | goto exec_out; |
| 99 | } |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | if( xCallback(pArg, nCol, azVals, azCols) ){ |
| 103 | rc = SQLITE_ABORT; |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 104 | sqlite3VdbeFinalize((Vdbe *)pStmt); |
drh | f50bebf | 2008-05-19 23:51:55 +0000 | [diff] [blame] | 105 | pStmt = 0; |
| 106 | sqlite3Error(db, SQLITE_ABORT, 0); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 107 | goto exec_out; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if( rc!=SQLITE_ROW ){ |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 112 | rc = sqlite3VdbeFinalize((Vdbe *)pStmt); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 113 | pStmt = 0; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 114 | if( rc!=SQLITE_SCHEMA ){ |
| 115 | nRetry = 0; |
| 116 | zSql = zLeftover; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 117 | while( sqlite3Isspace(zSql[0]) ) zSql++; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 123 | sqlite3DbFree(db, azCols); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 124 | azCols = 0; |
| 125 | } |
| 126 | |
| 127 | exec_out: |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 128 | if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 129 | sqlite3DbFree(db, azCols); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 130 | |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 131 | rc = sqlite3ApiExit(db, rc); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 132 | if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 133 | int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db)); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 134 | *pzErrMsg = sqlite3Malloc(nErrMsg); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 135 | if( *pzErrMsg ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 136 | memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 137 | } |
| 138 | }else if( pzErrMsg ){ |
| 139 | *pzErrMsg = 0; |
| 140 | } |
| 141 | |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 142 | assert( (rc&db->errMask)==rc ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 143 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 144 | return rc; |
| 145 | } |