blob: 72637b268e4d39f1b273bd6bc1819df646bf3c5c [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.
16**
drhf3a65f72007-08-22 20:18:21 +000017** $Id: legacy.c,v 1.21 2007/08/22 20:18:22 drh Exp $
danielk197798d30672004-05-26 00:01:35 +000018*/
19
20#include "sqliteInt.h"
danielk197798d30672004-05-26 00:01:35 +000021#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*/
33int sqlite3_exec(
drh9bb575f2004-09-06 17:24:11 +000034 sqlite3 *db, /* The database on which the SQL executes */
danielk197798d30672004-05-26 00:01:35 +000035 const char *zSql, /* The SQL to be executed */
drh12057d52004-09-06 17:34:12 +000036 sqlite3_callback xCallback, /* Invoke this callback routine */
danielk197798d30672004-05-26 00:01:35 +000037 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;
danielk197798d30672004-05-26 00:01:35 +000046 int nCallback;
47
48 if( zSql==0 ) return SQLITE_OK;
49 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
danielk197798d30672004-05-26 00:01:35 +000065 nCallback = 0;
66
67 nCol = sqlite3_column_count(pStmt);
drh17435752007-08-16 04:30:38 +000068 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1);
drh158ff672006-01-23 13:14:55 +000069 if( azCols==0 ){
danielk197798d30672004-05-26 00:01:35 +000070 goto exec_out;
71 }
72
73 while( 1 ){
74 int i;
75 rc = sqlite3_step(pStmt);
76
77 /* Invoke the callback function if required */
78 if( xCallback && (SQLITE_ROW==rc ||
79 (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
80 if( 0==nCallback ){
81 for(i=0; i<nCol; i++){
82 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
83 }
84 nCallback++;
85 }
86 if( rc==SQLITE_ROW ){
87 azVals = &azCols[nCol];
88 for(i=0; i<nCol; i++){
drh4f26d6c2004-05-26 23:25:30 +000089 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
danielk197798d30672004-05-26 00:01:35 +000090 }
91 }
92 if( xCallback(pArg, nCol, azVals, azCols) ){
93 rc = SQLITE_ABORT;
94 goto exec_out;
95 }
96 }
97
98 if( rc!=SQLITE_ROW ){
danielk1977fc57d7b2004-05-26 02:04:57 +000099 rc = sqlite3_finalize(pStmt);
danielk197798d30672004-05-26 00:01:35 +0000100 pStmt = 0;
danielk197798d30672004-05-26 00:01:35 +0000101 if( rc!=SQLITE_SCHEMA ){
102 nRetry = 0;
103 zSql = zLeftover;
drh4c755c02004-08-08 20:22:17 +0000104 while( isspace((unsigned char)zSql[0]) ) zSql++;
danielk197798d30672004-05-26 00:01:35 +0000105 }
106 break;
107 }
108 }
109
drh17435752007-08-16 04:30:38 +0000110 sqlite3_free(azCols);
danielk197798d30672004-05-26 00:01:35 +0000111 azCols = 0;
112 }
113
114exec_out:
danielk1977fc57d7b2004-05-26 02:04:57 +0000115 if( pStmt ) sqlite3_finalize(pStmt);
drh17435752007-08-16 04:30:38 +0000116 if( azCols ) sqlite3_free(azCols);
danielk197798d30672004-05-26 00:01:35 +0000117
drhf3a65f72007-08-22 20:18:21 +0000118 rc = sqlite3ApiExit(db, rc);
danielk197798d30672004-05-26 00:01:35 +0000119 if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
drh5bb3eb92007-05-04 13:15:55 +0000120 int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
121 *pzErrMsg = sqlite3_malloc(nErrMsg);
danielk197798d30672004-05-26 00:01:35 +0000122 if( *pzErrMsg ){
drh5bb3eb92007-05-04 13:15:55 +0000123 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
danielk197798d30672004-05-26 00:01:35 +0000124 }
125 }else if( pzErrMsg ){
126 *pzErrMsg = 0;
127 }
128
drh4ac285a2006-09-15 07:28:50 +0000129 assert( (rc&db->errMask)==rc );
danielk197798d30672004-05-26 00:01:35 +0000130 return rc;
131}