blob: ebab2de37d996e1bb4a4d9a84caec76a5eff99ea [file] [log] [blame]
danielk197798d30672004-05-26 00:01:35 +00001/*
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.
danielk197798d30672004-05-26 00:01:35 +000016*/
17
18#include "sqliteInt.h"
danielk197798d30672004-05-26 00:01:35 +000019
20/*
21** Execute SQL code. Return one of the SQLITE_ success/failure
22** codes. Also write an error message into memory obtained from
23** malloc() and make *pzErrMsg point to that message.
24**
25** If the SQL is a query, then for each row in the query result
26** the xCallback() function is called. pArg becomes the first
27** argument to xCallback(). If xCallback=NULL then no callback
28** is invoked, even for queries.
29*/
30int sqlite3_exec(
drh9bb575f2004-09-06 17:24:11 +000031 sqlite3 *db, /* The database on which the SQL executes */
danielk197798d30672004-05-26 00:01:35 +000032 const char *zSql, /* The SQL to be executed */
drh12057d52004-09-06 17:34:12 +000033 sqlite3_callback xCallback, /* Invoke this callback routine */
danielk197798d30672004-05-26 00:01:35 +000034 void *pArg, /* First argument to xCallback() */
35 char **pzErrMsg /* Write error messages here */
36){
drh76aeb342009-05-05 20:02:47 +000037 int rc = SQLITE_OK; /* Return code */
38 const char *zLeftover; /* Tail of unprocessed SQL */
39 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
40 char **azCols = 0; /* Names of result columns */
41 int nRetry = 0; /* Number of retry attempts */
42 int callbackIsInit; /* True if callback data is initialized */
danielk197798d30672004-05-26 00:01:35 +000043
drh413c3d32010-02-23 20:11:56 +000044 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
drh35c61902008-05-20 15:44:30 +000045 if( zSql==0 ) zSql = "";
danielk1977a1644fd2007-08-29 12:31:25 +000046
47 sqlite3_mutex_enter(db->mutex);
drh35c61902008-05-20 15:44:30 +000048 sqlite3Error(db, SQLITE_OK, 0);
danielk197798d30672004-05-26 00:01:35 +000049 while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
50 int nCol;
51 char **azVals = 0;
52
53 pStmt = 0;
54 rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
drh43617e92006-03-06 20:55:46 +000055 assert( rc==SQLITE_OK || pStmt==0 );
danielk197798d30672004-05-26 00:01:35 +000056 if( rc!=SQLITE_OK ){
danielk197798d30672004-05-26 00:01:35 +000057 continue;
58 }
59 if( !pStmt ){
60 /* this happens for a comment or white-space */
61 zSql = zLeftover;
62 continue;
63 }
64
drh76aeb342009-05-05 20:02:47 +000065 callbackIsInit = 0;
danielk197798d30672004-05-26 00:01:35 +000066 nCol = sqlite3_column_count(pStmt);
danielk197798d30672004-05-26 00:01:35 +000067
68 while( 1 ){
69 int i;
70 rc = sqlite3_step(pStmt);
71
72 /* Invoke the callback function if required */
73 if( xCallback && (SQLITE_ROW==rc ||
drh76aeb342009-05-05 20:02:47 +000074 (SQLITE_DONE==rc && !callbackIsInit
75 && db->flags&SQLITE_NullCallback)) ){
76 if( !callbackIsInit ){
77 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
drh3b6f7342008-03-21 18:01:14 +000078 if( azCols==0 ){
drh76aeb342009-05-05 20:02:47 +000079 goto exec_out;
drh3b6f7342008-03-21 18:01:14 +000080 }
danielk197798d30672004-05-26 00:01:35 +000081 for(i=0; i<nCol; i++){
82 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
drh701bb3b2008-08-02 03:50:39 +000083 /* sqlite3VdbeSetColName() installs column names as UTF8
84 ** strings so there is no way for sqlite3_column_name() to fail. */
85 assert( azCols[i]!=0 );
danielk197798d30672004-05-26 00:01:35 +000086 }
drh76aeb342009-05-05 20:02:47 +000087 callbackIsInit = 1;
danielk197798d30672004-05-26 00:01:35 +000088 }
89 if( rc==SQLITE_ROW ){
90 azVals = &azCols[nCol];
91 for(i=0; i<nCol; i++){
drh4f26d6c2004-05-26 23:25:30 +000092 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
danielk1977a7a8e142008-02-13 18:25:27 +000093 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
94 db->mallocFailed = 1;
95 goto exec_out;
96 }
danielk197798d30672004-05-26 00:01:35 +000097 }
98 }
99 if( xCallback(pArg, nCol, azVals, azCols) ){
100 rc = SQLITE_ABORT;
danielk1977238746a2009-03-19 18:51:06 +0000101 sqlite3VdbeFinalize((Vdbe *)pStmt);
drhf50bebf2008-05-19 23:51:55 +0000102 pStmt = 0;
103 sqlite3Error(db, SQLITE_ABORT, 0);
danielk197798d30672004-05-26 00:01:35 +0000104 goto exec_out;
105 }
106 }
107
108 if( rc!=SQLITE_ROW ){
danielk1977238746a2009-03-19 18:51:06 +0000109 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
danielk197798d30672004-05-26 00:01:35 +0000110 pStmt = 0;
danielk197798d30672004-05-26 00:01:35 +0000111 if( rc!=SQLITE_SCHEMA ){
112 nRetry = 0;
113 zSql = zLeftover;
danielk197778ca0e72009-01-20 16:53:39 +0000114 while( sqlite3Isspace(zSql[0]) ) zSql++;
danielk197798d30672004-05-26 00:01:35 +0000115 }
116 break;
117 }
118 }
119
drh633e6d52008-07-28 19:34:53 +0000120 sqlite3DbFree(db, azCols);
danielk197798d30672004-05-26 00:01:35 +0000121 azCols = 0;
122 }
123
124exec_out:
danielk1977238746a2009-03-19 18:51:06 +0000125 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
drh633e6d52008-07-28 19:34:53 +0000126 sqlite3DbFree(db, azCols);
danielk197798d30672004-05-26 00:01:35 +0000127
drhf3a65f72007-08-22 20:18:21 +0000128 rc = sqlite3ApiExit(db, rc);
drh76aeb342009-05-05 20:02:47 +0000129 if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
drhea678832008-12-10 19:26:22 +0000130 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
drhe5ae5732008-06-15 02:51:47 +0000131 *pzErrMsg = sqlite3Malloc(nErrMsg);
danielk197798d30672004-05-26 00:01:35 +0000132 if( *pzErrMsg ){
drh5bb3eb92007-05-04 13:15:55 +0000133 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
drh6bef6482009-07-03 19:18:43 +0000134 }else{
135 rc = SQLITE_NOMEM;
danielk1977410b6bb2009-08-07 16:55:59 +0000136 sqlite3Error(db, SQLITE_NOMEM, 0);
danielk197798d30672004-05-26 00:01:35 +0000137 }
138 }else if( pzErrMsg ){
139 *pzErrMsg = 0;
140 }
141
drh4ac285a2006-09-15 07:28:50 +0000142 assert( (rc&db->errMask)==rc );
danielk1977a1644fd2007-08-29 12:31:25 +0000143 sqlite3_mutex_leave(db->mutex);
danielk197798d30672004-05-26 00:01:35 +0000144 return rc;
145}