sl@0
|
1 |
/*
|
sl@0
|
2 |
** 2001 September 15
|
sl@0
|
3 |
**
|
sl@0
|
4 |
** The author disclaims copyright to this source code. In place of
|
sl@0
|
5 |
** a legal notice, here is a blessing:
|
sl@0
|
6 |
**
|
sl@0
|
7 |
** May you do good and not evil.
|
sl@0
|
8 |
** May you find forgiveness for yourself and forgive others.
|
sl@0
|
9 |
** May you share freely, never taking more than you give.
|
sl@0
|
10 |
**
|
sl@0
|
11 |
*************************************************************************
|
sl@0
|
12 |
** Main file for the SQLite library. The routines in this file
|
sl@0
|
13 |
** implement the programmer interface to the library. Routines in
|
sl@0
|
14 |
** other files are for internal use by SQLite and should not be
|
sl@0
|
15 |
** accessed by users of the library.
|
sl@0
|
16 |
**
|
sl@0
|
17 |
** $Id: legacy.c,v 1.29 2008/08/02 03:50:39 drh Exp $
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "sqliteInt.h"
|
sl@0
|
21 |
#include <ctype.h>
|
sl@0
|
22 |
|
sl@0
|
23 |
/*
|
sl@0
|
24 |
** Execute SQL code. Return one of the SQLITE_ success/failure
|
sl@0
|
25 |
** codes. Also write an error message into memory obtained from
|
sl@0
|
26 |
** malloc() and make *pzErrMsg point to that message.
|
sl@0
|
27 |
**
|
sl@0
|
28 |
** If the SQL is a query, then for each row in the query result
|
sl@0
|
29 |
** the xCallback() function is called. pArg becomes the first
|
sl@0
|
30 |
** argument to xCallback(). If xCallback=NULL then no callback
|
sl@0
|
31 |
** is invoked, even for queries.
|
sl@0
|
32 |
*/
|
sl@0
|
33 |
int sqlite3_exec(
|
sl@0
|
34 |
sqlite3 *db, /* The database on which the SQL executes */
|
sl@0
|
35 |
const char *zSql, /* The SQL to be executed */
|
sl@0
|
36 |
sqlite3_callback xCallback, /* Invoke this callback routine */
|
sl@0
|
37 |
void *pArg, /* First argument to xCallback() */
|
sl@0
|
38 |
char **pzErrMsg /* Write error messages here */
|
sl@0
|
39 |
){
|
sl@0
|
40 |
int rc = SQLITE_OK;
|
sl@0
|
41 |
const char *zLeftover;
|
sl@0
|
42 |
sqlite3_stmt *pStmt = 0;
|
sl@0
|
43 |
char **azCols = 0;
|
sl@0
|
44 |
|
sl@0
|
45 |
int nRetry = 0;
|
sl@0
|
46 |
int nCallback;
|
sl@0
|
47 |
|
sl@0
|
48 |
if( zSql==0 ) zSql = "";
|
sl@0
|
49 |
|
sl@0
|
50 |
sqlite3_mutex_enter(db->mutex);
|
sl@0
|
51 |
sqlite3Error(db, SQLITE_OK, 0);
|
sl@0
|
52 |
while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
|
sl@0
|
53 |
int nCol;
|
sl@0
|
54 |
char **azVals = 0;
|
sl@0
|
55 |
|
sl@0
|
56 |
pStmt = 0;
|
sl@0
|
57 |
rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
|
sl@0
|
58 |
assert( rc==SQLITE_OK || pStmt==0 );
|
sl@0
|
59 |
if( rc!=SQLITE_OK ){
|
sl@0
|
60 |
continue;
|
sl@0
|
61 |
}
|
sl@0
|
62 |
if( !pStmt ){
|
sl@0
|
63 |
/* this happens for a comment or white-space */
|
sl@0
|
64 |
zSql = zLeftover;
|
sl@0
|
65 |
continue;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
nCallback = 0;
|
sl@0
|
69 |
nCol = sqlite3_column_count(pStmt);
|
sl@0
|
70 |
|
sl@0
|
71 |
while( 1 ){
|
sl@0
|
72 |
int i;
|
sl@0
|
73 |
rc = sqlite3_step(pStmt);
|
sl@0
|
74 |
|
sl@0
|
75 |
/* Invoke the callback function if required */
|
sl@0
|
76 |
if( xCallback && (SQLITE_ROW==rc ||
|
sl@0
|
77 |
(SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
|
sl@0
|
78 |
if( 0==nCallback ){
|
sl@0
|
79 |
if( azCols==0 ){
|
sl@0
|
80 |
azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
|
sl@0
|
81 |
if( azCols==0 ){
|
sl@0
|
82 |
goto exec_out;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
}
|
sl@0
|
85 |
for(i=0; i<nCol; i++){
|
sl@0
|
86 |
azCols[i] = (char *)sqlite3_column_name(pStmt, i);
|
sl@0
|
87 |
/* sqlite3VdbeSetColName() installs column names as UTF8
|
sl@0
|
88 |
** strings so there is no way for sqlite3_column_name() to fail. */
|
sl@0
|
89 |
assert( azCols[i]!=0 );
|
sl@0
|
90 |
}
|
sl@0
|
91 |
nCallback++;
|
sl@0
|
92 |
}
|
sl@0
|
93 |
if( rc==SQLITE_ROW ){
|
sl@0
|
94 |
azVals = &azCols[nCol];
|
sl@0
|
95 |
for(i=0; i<nCol; i++){
|
sl@0
|
96 |
azVals[i] = (char *)sqlite3_column_text(pStmt, i);
|
sl@0
|
97 |
if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
|
sl@0
|
98 |
db->mallocFailed = 1;
|
sl@0
|
99 |
goto exec_out;
|
sl@0
|
100 |
}
|
sl@0
|
101 |
}
|
sl@0
|
102 |
}
|
sl@0
|
103 |
if( xCallback(pArg, nCol, azVals, azCols) ){
|
sl@0
|
104 |
rc = SQLITE_ABORT;
|
sl@0
|
105 |
sqlite3_finalize(pStmt);
|
sl@0
|
106 |
pStmt = 0;
|
sl@0
|
107 |
sqlite3Error(db, SQLITE_ABORT, 0);
|
sl@0
|
108 |
goto exec_out;
|
sl@0
|
109 |
}
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
if( rc!=SQLITE_ROW ){
|
sl@0
|
113 |
rc = sqlite3_finalize(pStmt);
|
sl@0
|
114 |
pStmt = 0;
|
sl@0
|
115 |
if( rc!=SQLITE_SCHEMA ){
|
sl@0
|
116 |
nRetry = 0;
|
sl@0
|
117 |
zSql = zLeftover;
|
sl@0
|
118 |
while( isspace((unsigned char)zSql[0]) ) zSql++;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
break;
|
sl@0
|
121 |
}
|
sl@0
|
122 |
}
|
sl@0
|
123 |
|
sl@0
|
124 |
sqlite3DbFree(db, azCols);
|
sl@0
|
125 |
azCols = 0;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
|
sl@0
|
128 |
exec_out:
|
sl@0
|
129 |
if( pStmt ) sqlite3_finalize(pStmt);
|
sl@0
|
130 |
sqlite3DbFree(db, azCols);
|
sl@0
|
131 |
|
sl@0
|
132 |
rc = sqlite3ApiExit(db, rc);
|
sl@0
|
133 |
if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
|
sl@0
|
134 |
int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
|
sl@0
|
135 |
*pzErrMsg = sqlite3Malloc(nErrMsg);
|
sl@0
|
136 |
if( *pzErrMsg ){
|
sl@0
|
137 |
memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
|
sl@0
|
138 |
}
|
sl@0
|
139 |
}else if( pzErrMsg ){
|
sl@0
|
140 |
*pzErrMsg = 0;
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
assert( (rc&db->errMask)==rc );
|
sl@0
|
144 |
sqlite3_mutex_leave(db->mutex);
|
sl@0
|
145 |
return rc;
|
sl@0
|
146 |
}
|