Build Lunapaint from Contrib.
[AROS-Contrib.git] / sqlite3 / legacy.c
blobe523ce419aee86f7d889d36caec5037e4e93bdbb
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.
11 *************************************************************************
13 /**
14 * @file legacy.c
16 * Main file for the SQLite library. The routines in this file implement the
17 * programmer interface to the library. Routines in other files are for
18 * internal use by SQLite and should not be accessed by users of the library.
20 * */
21 /* $Id$ */
23 #include "sqliteInt.h"
24 #include "os.h"
25 #include <ctype.h>
28 * Execute SQL code. Return one of the SQLITE_ success/failure codes. Also
29 * write an error message into memory obtained from malloc() and make *pzErrMsg
30 * point to that message.
32 * If the SQL is a query, then for each row in the query result the xCallback()
33 * function is called. pArg becomes the first argument to xCallback(). If
34 * xCallback=NULL then no callback is invoked, even for queries.
35 * */
36 int sqlite3_exec(
37 sqlite3 *db, /* The database on which the SQL executes */
38 const char *zSql, /* The SQL to be executed */
39 sqlite3_callback xCallback, /* Invoke this callback routine */
40 void *pArg, /* First argument to xCallback() */
41 char **pzErrMsg /* Write error messages here */
44 int rc = SQLITE_OK;
45 const char *zLeftover;
46 sqlite3_stmt *pStmt = 0;
47 char **azCols = 0;
49 int nRetry = 0;
50 int nChange = 0;
51 int nCallback;
53 if( zSql==0 ) return SQLITE_OK;
55 while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] )
57 int nCol;
58 char **azVals = 0;
60 pStmt = 0;
61 rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
62 if( rc!=SQLITE_OK )
64 if( pStmt )
65 sqlite3_finalize(pStmt);
66 continue;
68 if( !pStmt )
70 /* this happens for a comment or white-space */
71 zSql = zLeftover;
72 continue;
75 db->nChange += nChange;
76 nCallback = 0;
78 nCol = sqlite3_column_count(pStmt);
79 azCols = sqliteMalloc(2*nCol*sizeof(const char *));
80 if( nCol && !azCols )
82 rc = SQLITE_NOMEM;
83 goto exec_out;
86 while( 1 )
88 int i;
89 rc = sqlite3_step(pStmt);
91 /* Invoke the callback function if required */
92 if( xCallback && (SQLITE_ROW==rc ||
93 (SQLITE_DONE==rc && !nCallback &&
94 db->flags&SQLITE_NullCallback)) )
96 if( 0==nCallback )
98 for(i=0; i<nCol; i++)
100 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
102 nCallback++;
104 if( rc==SQLITE_ROW )
106 azVals = &azCols[nCol];
107 for(i=0; i<nCol; i++)
109 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
112 if( xCallback(pArg, nCol, azVals, azCols) )
114 rc = SQLITE_ABORT;
115 goto exec_out;
119 if( rc!=SQLITE_ROW )
121 rc = sqlite3_finalize(pStmt);
122 pStmt = 0;
123 if( db->pVdbe==0 )
124 nChange = db->nChange;
126 if( rc!=SQLITE_SCHEMA )
128 nRetry = 0;
129 zSql = zLeftover;
130 while( isspace((unsigned char)zSql[0]) ) zSql++;
132 break;
136 sqliteFree(azCols);
137 azCols = 0;
140 exec_out:
141 if( pStmt ) sqlite3_finalize(pStmt);
142 if( azCols ) sqliteFree(azCols);
143 if( sqlite3_malloc_failed )
144 rc = SQLITE_NOMEM;
145 if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg )
147 *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
148 if( *pzErrMsg )
150 strcpy(*pzErrMsg, sqlite3_errmsg(db));
152 }else if( pzErrMsg )
153 *pzErrMsg = 0;
155 return rc;