blob: 867587e5a2fb1a78ad7f84c557649553475557d0 [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 */
drh76aeb342009-05-05 20:02:47 +000041 int callbackIsInit; /* True if callback data is initialized */
danielk197798d30672004-05-26 00:01:35 +000042
drh413c3d32010-02-23 20:11:56 +000043 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
drh35c61902008-05-20 15:44:30 +000044 if( zSql==0 ) zSql = "";
danielk1977a1644fd2007-08-29 12:31:25 +000045
46 sqlite3_mutex_enter(db->mutex);
drh13f40da2014-08-22 18:00:11 +000047 sqlite3Error(db, SQLITE_OK);
drh60625312013-04-06 18:06:51 +000048 while( rc==SQLITE_OK && zSql[0] ){
drhf2305412019-02-04 19:12:54 +000049 int nCol = 0;
danielk197798d30672004-05-26 00:01:35 +000050 char **azVals = 0;
51
52 pStmt = 0;
drh60625312013-04-06 18:06:51 +000053 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
drh43617e92006-03-06 20:55:46 +000054 assert( rc==SQLITE_OK || pStmt==0 );
danielk197798d30672004-05-26 00:01:35 +000055 if( rc!=SQLITE_OK ){
danielk197798d30672004-05-26 00:01:35 +000056 continue;
57 }
58 if( !pStmt ){
59 /* this happens for a comment or white-space */
60 zSql = zLeftover;
61 continue;
62 }
drh76aeb342009-05-05 20:02:47 +000063 callbackIsInit = 0;
danielk197798d30672004-05-26 00:01:35 +000064
65 while( 1 ){
66 int i;
67 rc = sqlite3_step(pStmt);
68
69 /* Invoke the callback function if required */
70 if( xCallback && (SQLITE_ROW==rc ||
drh76aeb342009-05-05 20:02:47 +000071 (SQLITE_DONE==rc && !callbackIsInit
72 && db->flags&SQLITE_NullCallback)) ){
73 if( !callbackIsInit ){
drhf2305412019-02-04 19:12:54 +000074 nCol = sqlite3_column_count(pStmt);
drh5c258dc2017-02-16 17:18:07 +000075 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
drh3b6f7342008-03-21 18:01:14 +000076 if( azCols==0 ){
drh76aeb342009-05-05 20:02:47 +000077 goto exec_out;
drh3b6f7342008-03-21 18:01:14 +000078 }
danielk197798d30672004-05-26 00:01:35 +000079 for(i=0; i<nCol; i++){
80 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
drh701bb3b2008-08-02 03:50:39 +000081 /* sqlite3VdbeSetColName() installs column names as UTF8
82 ** strings so there is no way for sqlite3_column_name() to fail. */
83 assert( azCols[i]!=0 );
danielk197798d30672004-05-26 00:01:35 +000084 }
drh76aeb342009-05-05 20:02:47 +000085 callbackIsInit = 1;
danielk197798d30672004-05-26 00:01:35 +000086 }
87 if( rc==SQLITE_ROW ){
88 azVals = &azCols[nCol];
89 for(i=0; i<nCol; i++){
drh4f26d6c2004-05-26 23:25:30 +000090 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
danielk1977a7a8e142008-02-13 18:25:27 +000091 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
drh4a642b62016-02-05 01:55:27 +000092 sqlite3OomFault(db);
danielk1977a7a8e142008-02-13 18:25:27 +000093 goto exec_out;
94 }
danielk197798d30672004-05-26 00:01:35 +000095 }
drh5c258dc2017-02-16 17:18:07 +000096 azVals[i] = 0;
danielk197798d30672004-05-26 00:01:35 +000097 }
98 if( xCallback(pArg, nCol, azVals, azCols) ){
drhddb17ca2014-08-11 15:54:11 +000099 /* EVIDENCE-OF: R-38229-40159 If the callback function to
100 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
101 ** return SQLITE_ABORT. */
danielk197798d30672004-05-26 00:01:35 +0000102 rc = SQLITE_ABORT;
danielk1977238746a2009-03-19 18:51:06 +0000103 sqlite3VdbeFinalize((Vdbe *)pStmt);
drhf50bebf2008-05-19 23:51:55 +0000104 pStmt = 0;
drh13f40da2014-08-22 18:00:11 +0000105 sqlite3Error(db, SQLITE_ABORT);
danielk197798d30672004-05-26 00:01:35 +0000106 goto exec_out;
107 }
108 }
109
110 if( rc!=SQLITE_ROW ){
danielk1977238746a2009-03-19 18:51:06 +0000111 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
danielk197798d30672004-05-26 00:01:35 +0000112 pStmt = 0;
drh60625312013-04-06 18:06:51 +0000113 zSql = zLeftover;
114 while( sqlite3Isspace(zSql[0]) ) zSql++;
danielk197798d30672004-05-26 00:01:35 +0000115 break;
116 }
117 }
118
drh633e6d52008-07-28 19:34:53 +0000119 sqlite3DbFree(db, azCols);
danielk197798d30672004-05-26 00:01:35 +0000120 azCols = 0;
121 }
122
123exec_out:
danielk1977238746a2009-03-19 18:51:06 +0000124 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
drh633e6d52008-07-28 19:34:53 +0000125 sqlite3DbFree(db, azCols);
danielk197798d30672004-05-26 00:01:35 +0000126
drhf3a65f72007-08-22 20:18:21 +0000127 rc = sqlite3ApiExit(db, rc);
drh09e60542014-09-10 22:46:46 +0000128 if( rc!=SQLITE_OK && pzErrMsg ){
drh6e11b162017-06-28 01:21:16 +0000129 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
130 if( *pzErrMsg==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000131 rc = SQLITE_NOMEM_BKPT;
drh13f40da2014-08-22 18:00:11 +0000132 sqlite3Error(db, SQLITE_NOMEM);
danielk197798d30672004-05-26 00:01:35 +0000133 }
134 }else if( pzErrMsg ){
135 *pzErrMsg = 0;
136 }
137
drh4ac285a2006-09-15 07:28:50 +0000138 assert( (rc&db->errMask)==rc );
danielk1977a1644fd2007-08-29 12:31:25 +0000139 sqlite3_mutex_leave(db->mutex);
danielk197798d30672004-05-26 00:01:35 +0000140 return rc;
141}