1 : /*
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 : **
17 : ** $Id$
18 : */
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 : */
34 : int sqlite3_exec(
35 : sqlite3 *db, /* The database on which the SQL executes */
36 : const char *zSql, /* The SQL to be executed */
37 : sqlite3_callback xCallback, /* Invoke this callback routine */
38 : void *pArg, /* First argument to xCallback() */
39 : char **pzErrMsg /* Write error messages here */
40 796 : ){
41 796 : int rc = SQLITE_OK;
42 : const char *zLeftover;
43 796 : sqlite3_stmt *pStmt = 0;
44 796 : char **azCols = 0;
45 :
46 796 : int nRetry = 0;
47 : int nCallback;
48 :
49 796 : if( zSql==0 ) return SQLITE_OK;
50 2388 : while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
51 : int nCol;
52 796 : char **azVals = 0;
53 :
54 796 : pStmt = 0;
55 796 : rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
56 : assert( rc==SQLITE_OK || pStmt==0 );
57 796 : if( rc!=SQLITE_OK ){
58 309 : continue;
59 : }
60 487 : if( !pStmt ){
61 : /* this happens for a comment or white-space */
62 0 : zSql = zLeftover;
63 0 : continue;
64 : }
65 :
66 487 : nCallback = 0;
67 :
68 487 : nCol = sqlite3_column_count(pStmt);
69 487 : azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
70 487 : if( azCols==0 ){
71 0 : goto exec_out;
72 : }
73 :
74 : while( 1 ){
75 : int i;
76 583 : rc = sqlite3_step(pStmt);
77 :
78 : /* Invoke the callback function if required */
79 583 : if( xCallback && (SQLITE_ROW==rc ||
80 : (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
81 96 : if( 0==nCallback ){
82 228 : for(i=0; i<nCol; i++){
83 171 : azCols[i] = (char *)sqlite3_column_name(pStmt, i);
84 : }
85 57 : nCallback++;
86 : }
87 96 : if( rc==SQLITE_ROW ){
88 96 : azVals = &azCols[nCol];
89 384 : for(i=0; i<nCol; i++){
90 288 : azVals[i] = (char *)sqlite3_column_text(pStmt, i);
91 : }
92 : }
93 96 : if( xCallback(pArg, nCol, azVals, azCols) ){
94 0 : rc = SQLITE_ABORT;
95 0 : goto exec_out;
96 : }
97 : }
98 :
99 583 : if( rc!=SQLITE_ROW ){
100 487 : rc = sqlite3_finalize(pStmt);
101 487 : pStmt = 0;
102 487 : if( rc!=SQLITE_SCHEMA ){
103 487 : nRetry = 0;
104 487 : zSql = zLeftover;
105 487 : while( isspace((unsigned char)zSql[0]) ) zSql++;
106 : }
107 : break;
108 : }
109 96 : }
110 :
111 487 : sqliteFree(azCols);
112 487 : azCols = 0;
113 : }
114 :
115 796 : exec_out:
116 796 : if( pStmt ) sqlite3_finalize(pStmt);
117 796 : if( azCols ) sqliteFree(azCols);
118 :
119 796 : rc = sqlite3ApiExit(0, rc);
120 1105 : if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
121 309 : *pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg(db)));
122 309 : if( *pzErrMsg ){
123 309 : strcpy(*pzErrMsg, sqlite3_errmsg(db));
124 : }
125 487 : }else if( pzErrMsg ){
126 426 : *pzErrMsg = 0;
127 : }
128 :
129 : assert( (rc&db->errMask)==rc );
130 796 : return rc;
131 : }
|