1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/legacy.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,134 @@
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 <ctype.h>
1.25 +
1.26 +/*
1.27 +** Execute SQL code. Return one of the SQLITE_ success/failure
1.28 +** codes. Also write an error message into memory obtained from
1.29 +** malloc() and make *pzErrMsg point to that message.
1.30 +**
1.31 +** If the SQL is a query, then for each row in the query result
1.32 +** the xCallback() function is called. pArg becomes the first
1.33 +** argument to xCallback(). If xCallback=NULL then no callback
1.34 +** is invoked, even for queries.
1.35 +*/
1.36 +int sqlite3_exec(
1.37 + sqlite3 *db, /* The database on which the SQL executes */
1.38 + const char *zSql, /* The SQL to be executed */
1.39 + sqlite3_callback xCallback, /* Invoke this callback routine */
1.40 + void *pArg, /* First argument to xCallback() */
1.41 + char **pzErrMsg /* Write error messages here */
1.42 +){
1.43 + int rc = SQLITE_OK;
1.44 + const char *zLeftover;
1.45 + sqlite3_stmt *pStmt = 0;
1.46 + char **azCols = 0;
1.47 +
1.48 + int nRetry = 0;
1.49 + int nCallback;
1.50 +
1.51 + if( zSql==0 ) return SQLITE_OK;
1.52 +
1.53 + sqlite3_mutex_enter(db->mutex);
1.54 + while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
1.55 + int nCol;
1.56 + char **azVals = 0;
1.57 +
1.58 + pStmt = 0;
1.59 + rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
1.60 + assert( rc==SQLITE_OK || pStmt==0 );
1.61 + if( rc!=SQLITE_OK ){
1.62 + continue;
1.63 + }
1.64 + if( !pStmt ){
1.65 + /* this happens for a comment or white-space */
1.66 + zSql = zLeftover;
1.67 + continue;
1.68 + }
1.69 +
1.70 + nCallback = 0;
1.71 +
1.72 + nCol = sqlite3_column_count(pStmt);
1.73 + azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1);
1.74 + if( azCols==0 ){
1.75 + goto exec_out;
1.76 + }
1.77 +
1.78 + while( 1 ){
1.79 + int i;
1.80 + rc = sqlite3_step(pStmt);
1.81 +
1.82 + /* Invoke the callback function if required */
1.83 + if( xCallback && (SQLITE_ROW==rc ||
1.84 + (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
1.85 + if( 0==nCallback ){
1.86 + for(i=0; i<nCol; i++){
1.87 + azCols[i] = (char *)sqlite3_column_name(pStmt, i);
1.88 + }
1.89 + nCallback++;
1.90 + }
1.91 + if( rc==SQLITE_ROW ){
1.92 + azVals = &azCols[nCol];
1.93 + for(i=0; i<nCol; i++){
1.94 + azVals[i] = (char *)sqlite3_column_text(pStmt, i);
1.95 + }
1.96 + }
1.97 + if( xCallback(pArg, nCol, azVals, azCols) ){
1.98 + rc = SQLITE_ABORT;
1.99 + goto exec_out;
1.100 + }
1.101 + }
1.102 +
1.103 + if( rc!=SQLITE_ROW ){
1.104 + rc = sqlite3_finalize(pStmt);
1.105 + pStmt = 0;
1.106 + if( rc!=SQLITE_SCHEMA ){
1.107 + nRetry = 0;
1.108 + zSql = zLeftover;
1.109 + while( isspace((unsigned char)zSql[0]) ) zSql++;
1.110 + }
1.111 + break;
1.112 + }
1.113 + }
1.114 +
1.115 + sqlite3_free(azCols);
1.116 + azCols = 0;
1.117 + }
1.118 +
1.119 +exec_out:
1.120 + if( pStmt ) sqlite3_finalize(pStmt);
1.121 + if( azCols ) sqlite3_free(azCols);
1.122 +
1.123 + rc = sqlite3ApiExit(db, rc);
1.124 + if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
1.125 + int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
1.126 + *pzErrMsg = sqlite3_malloc(nErrMsg);
1.127 + if( *pzErrMsg ){
1.128 + memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
1.129 + }
1.130 + }else if( pzErrMsg ){
1.131 + *pzErrMsg = 0;
1.132 + }
1.133 +
1.134 + assert( (rc&db->errMask)==rc );
1.135 + sqlite3_mutex_leave(db->mutex);
1.136 + return rc;
1.137 +}