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