blob: 98f37a922a52d021f699a133d9b9e76800921ec4 [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**
danielk197754f01982006-01-18 15:25:17 +000017** $Id: legacy.c,v 1.12 2006/01/18 15:25:17 danielk1977 Exp $
danielk197798d30672004-05-26 00:01:35 +000018*/
19
20#include "sqliteInt.h"
21#include "os.h"
22#include <ctype.h>
23
24/*
25** Execute SQL code. Return one of the SQLITE_ success/failure
26** codes. Also write an error message into memory obtained from
27** malloc() and make *pzErrMsg point to that message.
28**
29** If the SQL is a query, then for each row in the query result
30** the xCallback() function is called. pArg becomes the first
31** argument to xCallback(). If xCallback=NULL then no callback
32** is invoked, even for queries.
33*/
34int sqlite3_exec(
drh9bb575f2004-09-06 17:24:11 +000035 sqlite3 *db, /* The database on which the SQL executes */
danielk197798d30672004-05-26 00:01:35 +000036 const char *zSql, /* The SQL to be executed */
drh12057d52004-09-06 17:34:12 +000037 sqlite3_callback xCallback, /* Invoke this callback routine */
danielk197798d30672004-05-26 00:01:35 +000038 void *pArg, /* First argument to xCallback() */
39 char **pzErrMsg /* Write error messages here */
40){
41 int rc = SQLITE_OK;
42 const char *zLeftover;
43 sqlite3_stmt *pStmt = 0;
44 char **azCols = 0;
45
46 int nRetry = 0;
47 int nChange = 0;
48 int nCallback;
49
50 if( zSql==0 ) return SQLITE_OK;
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);
57 if( rc!=SQLITE_OK ){
danielk1977fc57d7b2004-05-26 02:04:57 +000058 if( pStmt ) sqlite3_finalize(pStmt);
danielk197798d30672004-05-26 00:01:35 +000059 continue;
60 }
61 if( !pStmt ){
62 /* this happens for a comment or white-space */
63 zSql = zLeftover;
64 continue;
65 }
66
67 db->nChange += nChange;
68 nCallback = 0;
69
70 nCol = sqlite3_column_count(pStmt);
71 azCols = sqliteMalloc(2*nCol*sizeof(const char *));
72 if( nCol && !azCols ){
danielk197798d30672004-05-26 00:01:35 +000073 goto exec_out;
74 }
75
76 while( 1 ){
77 int i;
78 rc = sqlite3_step(pStmt);
79
80 /* Invoke the callback function if required */
81 if( xCallback && (SQLITE_ROW==rc ||
82 (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
83 if( 0==nCallback ){
84 for(i=0; i<nCol; i++){
85 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
86 }
87 nCallback++;
88 }
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);
danielk197798d30672004-05-26 00:01:35 +000093 }
94 }
95 if( xCallback(pArg, nCol, azVals, azCols) ){
96 rc = SQLITE_ABORT;
97 goto exec_out;
98 }
99 }
100
101 if( rc!=SQLITE_ROW ){
danielk1977fc57d7b2004-05-26 02:04:57 +0000102 rc = sqlite3_finalize(pStmt);
danielk197798d30672004-05-26 00:01:35 +0000103 pStmt = 0;
104 if( db->pVdbe==0 ){
105 nChange = db->nChange;
106 }
107 if( rc!=SQLITE_SCHEMA ){
108 nRetry = 0;
109 zSql = zLeftover;
drh4c755c02004-08-08 20:22:17 +0000110 while( isspace((unsigned char)zSql[0]) ) zSql++;
danielk197798d30672004-05-26 00:01:35 +0000111 }
112 break;
113 }
114 }
115
116 sqliteFree(azCols);
117 azCols = 0;
118 }
119
120exec_out:
danielk1977fc57d7b2004-05-26 02:04:57 +0000121 if( pStmt ) sqlite3_finalize(pStmt);
danielk197798d30672004-05-26 00:01:35 +0000122 if( azCols ) sqliteFree(azCols);
123
danielk197754f01982006-01-18 15:25:17 +0000124 rc = sqlite3ApiExit(0, rc);
danielk197798d30672004-05-26 00:01:35 +0000125 if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
126 *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
127 if( *pzErrMsg ){
128 strcpy(*pzErrMsg, sqlite3_errmsg(db));
129 }
130 }else if( pzErrMsg ){
131 *pzErrMsg = 0;
132 }
133
134 return rc;
135}