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 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 17 | ** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 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 | #include <ctype.h> |
| 22 | |
| 23 | /* |
| 24 | ** Execute SQL code. Return one of the SQLITE_ success/failure |
| 25 | ** codes. Also write an error message into memory obtained from |
| 26 | ** malloc() and make *pzErrMsg point to that message. |
| 27 | ** |
| 28 | ** If the SQL is a query, then for each row in the query result |
| 29 | ** the xCallback() function is called. pArg becomes the first |
| 30 | ** argument to xCallback(). If xCallback=NULL then no callback |
| 31 | ** is invoked, even for queries. |
| 32 | */ |
| 33 | int sqlite3_exec( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 34 | sqlite3 *db, /* The database on which the SQL executes */ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 35 | const char *zSql, /* The SQL to be executed */ |
drh | 12057d5 | 2004-09-06 17:34:12 +0000 | [diff] [blame] | 36 | sqlite3_callback xCallback, /* Invoke this callback routine */ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 37 | void *pArg, /* First argument to xCallback() */ |
| 38 | char **pzErrMsg /* Write error messages here */ |
| 39 | ){ |
| 40 | int rc = SQLITE_OK; |
| 41 | const char *zLeftover; |
| 42 | sqlite3_stmt *pStmt = 0; |
| 43 | char **azCols = 0; |
| 44 | |
| 45 | int nRetry = 0; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 46 | int nCallback; |
| 47 | |
| 48 | if( zSql==0 ) return SQLITE_OK; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 49 | |
| 50 | sqlite3_mutex_enter(db->mutex); |
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; |
| 68 | |
| 69 | nCol = sqlite3_column_count(pStmt); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 70 | azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1); |
drh | 158ff67 | 2006-01-23 13:14:55 +0000 | [diff] [blame] | 71 | if( azCols==0 ){ |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 72 | goto exec_out; |
| 73 | } |
| 74 | |
| 75 | while( 1 ){ |
| 76 | int i; |
| 77 | rc = sqlite3_step(pStmt); |
| 78 | |
| 79 | /* Invoke the callback function if required */ |
| 80 | if( xCallback && (SQLITE_ROW==rc || |
| 81 | (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){ |
| 82 | if( 0==nCallback ){ |
| 83 | for(i=0; i<nCol; i++){ |
| 84 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 85 | } |
| 86 | nCallback++; |
| 87 | } |
| 88 | if( rc==SQLITE_ROW ){ |
| 89 | azVals = &azCols[nCol]; |
| 90 | for(i=0; i<nCol; i++){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 91 | azVals[i] = (char *)sqlite3_column_text(pStmt, i); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | if( xCallback(pArg, nCol, azVals, azCols) ){ |
| 95 | rc = SQLITE_ABORT; |
| 96 | goto exec_out; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if( rc!=SQLITE_ROW ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 101 | rc = sqlite3_finalize(pStmt); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 102 | pStmt = 0; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 103 | if( rc!=SQLITE_SCHEMA ){ |
| 104 | nRetry = 0; |
| 105 | zSql = zLeftover; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 106 | while( isspace((unsigned char)zSql[0]) ) zSql++; |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 107 | } |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 112 | sqlite3_free(azCols); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 113 | azCols = 0; |
| 114 | } |
| 115 | |
| 116 | exec_out: |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 117 | if( pStmt ) sqlite3_finalize(pStmt); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 118 | if( azCols ) sqlite3_free(azCols); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 119 | |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 120 | rc = sqlite3ApiExit(db, rc); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 121 | if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 122 | int nErrMsg = 1 + strlen(sqlite3_errmsg(db)); |
| 123 | *pzErrMsg = sqlite3_malloc(nErrMsg); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 124 | if( *pzErrMsg ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 125 | memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 126 | } |
| 127 | }else if( pzErrMsg ){ |
| 128 | *pzErrMsg = 0; |
| 129 | } |
| 130 | |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 131 | assert( (rc&db->errMask)==rc ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 132 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 98d3067 | 2004-05-26 00:01:35 +0000 | [diff] [blame] | 133 | return rc; |
| 134 | } |