1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/legacy.c Sun Jul 29 01:15:31 2007 +0000
1.3 @@ -0,0 +1,132 @@
1.4 +/*
1.5 +** 2001 September 15
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** Main file for the SQLite library. The routines in this file
1.16 +** implement the programmer interface to the library. Routines in
1.17 +** other files are for internal use by SQLite and should not be
1.18 +** accessed by users of the library.
1.19 +**
1.20 +** $Id$
1.21 +*/
1.22 +
1.23 +#include "sqliteInt.h"
1.24 +#include "os.h"
1.25 +#include <ctype.h>
1.26 +
1.27 +/*
1.28 +** Execute SQL code. Return one of the SQLITE_ success/failure
1.29 +** codes. Also write an error message into memory obtained from
1.30 +** malloc() and make *pzErrMsg point to that message.
1.31 +**
1.32 +** If the SQL is a query, then for each row in the query result
1.33 +** the xCallback() function is called. pArg becomes the first
1.34 +** argument to xCallback(). If xCallback=NULL then no callback
1.35 +** is invoked, even for queries.
1.36 +*/
1.37 +int sqlite3_exec(
1.38 + sqlite3 *db, /* The database on which the SQL executes */
1.39 + const char *zSql, /* The SQL to be executed */
1.40 + sqlite3_callback xCallback, /* Invoke this callback routine */
1.41 + void *pArg, /* First argument to xCallback() */
1.42 + char **pzErrMsg /* Write error messages here */
1.43 +){
1.44 + int rc = SQLITE_OK;
1.45 + const char *zLeftover;
1.46 + sqlite3_stmt *pStmt = 0;
1.47 + char **azCols = 0;
1.48 +
1.49 + int nRetry = 0;
1.50 + int nCallback;
1.51 +
1.52 + if( zSql==0 ) return SQLITE_OK;
1.53 + while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
1.54 + int nCol;
1.55 + char **azVals = 0;
1.56 +
1.57 + pStmt = 0;
1.58 + rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
1.59 + assert( rc==SQLITE_OK || pStmt==0 );
1.60 + if( rc!=SQLITE_OK ){
1.61 + continue;
1.62 + }
1.63 + if( !pStmt ){
1.64 + /* this happens for a comment or white-space */
1.65 + zSql = zLeftover;
1.66 + continue;
1.67 + }
1.68 +
1.69 + nCallback = 0;
1.70 +
1.71 + nCol = sqlite3_column_count(pStmt);
1.72 + azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
1.73 + if( azCols==0 ){
1.74 + goto exec_out;
1.75 + }
1.76 +
1.77 + while( 1 ){
1.78 + int i;
1.79 + rc = sqlite3_step(pStmt);
1.80 +
1.81 + /* Invoke the callback function if required */
1.82 + if( xCallback && (SQLITE_ROW==rc ||
1.83 + (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
1.84 + if( 0==nCallback ){
1.85 + for(i=0; i<nCol; i++){
1.86 + azCols[i] = (char *)sqlite3_column_name(pStmt, i);
1.87 + }
1.88 + nCallback++;
1.89 + }
1.90 + if( rc==SQLITE_ROW ){
1.91 + azVals = &azCols[nCol];
1.92 + for(i=0; i<nCol; i++){
1.93 + azVals[i] = (char *)sqlite3_column_text(pStmt, i);
1.94 + }
1.95 + }
1.96 + if( xCallback(pArg, nCol, azVals, azCols) ){
1.97 + rc = SQLITE_ABORT;
1.98 + goto exec_out;
1.99 + }
1.100 + }
1.101 +
1.102 + if( rc!=SQLITE_ROW ){
1.103 + rc = sqlite3_finalize(pStmt);
1.104 + pStmt = 0;
1.105 + if( rc!=SQLITE_SCHEMA ){
1.106 + nRetry = 0;
1.107 + zSql = zLeftover;
1.108 + while( isspace((unsigned char)zSql[0]) ) zSql++;
1.109 + }
1.110 + break;
1.111 + }
1.112 + }
1.113 +
1.114 + sqliteFree(azCols);
1.115 + azCols = 0;
1.116 + }
1.117 +
1.118 +exec_out:
1.119 + if( pStmt ) sqlite3_finalize(pStmt);
1.120 + if( azCols ) sqliteFree(azCols);
1.121 +
1.122 + rc = sqlite3ApiExit(0, rc);
1.123 + if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
1.124 + int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
1.125 + *pzErrMsg = sqlite3_malloc(nErrMsg);
1.126 + if( *pzErrMsg ){
1.127 + memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
1.128 + }
1.129 + }else if( pzErrMsg ){
1.130 + *pzErrMsg = 0;
1.131 + }
1.132 +
1.133 + assert( (rc&db->errMask)==rc );
1.134 + return rc;
1.135 +}