add pragma cipher_default_use_hmac to toggle global HMAC setting
[sqlcipher.git] / src / legacy.c
blobebab2de37d996e1bb4a4d9a84caec76a5eff99ea
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 *************************************************************************
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.
18 #include "sqliteInt.h"
21 ** Execute SQL code. Return one of the SQLITE_ success/failure
22 ** codes. Also write an error message into memory obtained from
23 ** malloc() and make *pzErrMsg point to that message.
25 ** If the SQL is a query, then for each row in the query result
26 ** the xCallback() function is called. pArg becomes the first
27 ** argument to xCallback(). If xCallback=NULL then no callback
28 ** is invoked, even for queries.
30 int sqlite3_exec(
31 sqlite3 *db, /* The database on which the SQL executes */
32 const char *zSql, /* The SQL to be executed */
33 sqlite3_callback xCallback, /* Invoke this callback routine */
34 void *pArg, /* First argument to xCallback() */
35 char **pzErrMsg /* Write error messages here */
37 int rc = SQLITE_OK; /* Return code */
38 const char *zLeftover; /* Tail of unprocessed SQL */
39 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
40 char **azCols = 0; /* Names of result columns */
41 int nRetry = 0; /* Number of retry attempts */
42 int callbackIsInit; /* True if callback data is initialized */
44 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
45 if( zSql==0 ) zSql = "";
47 sqlite3_mutex_enter(db->mutex);
48 sqlite3Error(db, SQLITE_OK, 0);
49 while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
50 int nCol;
51 char **azVals = 0;
53 pStmt = 0;
54 rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
55 assert( rc==SQLITE_OK || pStmt==0 );
56 if( rc!=SQLITE_OK ){
57 continue;
59 if( !pStmt ){
60 /* this happens for a comment or white-space */
61 zSql = zLeftover;
62 continue;
65 callbackIsInit = 0;
66 nCol = sqlite3_column_count(pStmt);
68 while( 1 ){
69 int i;
70 rc = sqlite3_step(pStmt);
72 /* Invoke the callback function if required */
73 if( xCallback && (SQLITE_ROW==rc ||
74 (SQLITE_DONE==rc && !callbackIsInit
75 && db->flags&SQLITE_NullCallback)) ){
76 if( !callbackIsInit ){
77 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
78 if( azCols==0 ){
79 goto exec_out;
81 for(i=0; i<nCol; i++){
82 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
83 /* sqlite3VdbeSetColName() installs column names as UTF8
84 ** strings so there is no way for sqlite3_column_name() to fail. */
85 assert( azCols[i]!=0 );
87 callbackIsInit = 1;
89 if( rc==SQLITE_ROW ){
90 azVals = &azCols[nCol];
91 for(i=0; i<nCol; i++){
92 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
93 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
94 db->mallocFailed = 1;
95 goto exec_out;
99 if( xCallback(pArg, nCol, azVals, azCols) ){
100 rc = SQLITE_ABORT;
101 sqlite3VdbeFinalize((Vdbe *)pStmt);
102 pStmt = 0;
103 sqlite3Error(db, SQLITE_ABORT, 0);
104 goto exec_out;
108 if( rc!=SQLITE_ROW ){
109 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
110 pStmt = 0;
111 if( rc!=SQLITE_SCHEMA ){
112 nRetry = 0;
113 zSql = zLeftover;
114 while( sqlite3Isspace(zSql[0]) ) zSql++;
116 break;
120 sqlite3DbFree(db, azCols);
121 azCols = 0;
124 exec_out:
125 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
126 sqlite3DbFree(db, azCols);
128 rc = sqlite3ApiExit(db, rc);
129 if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
130 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
131 *pzErrMsg = sqlite3Malloc(nErrMsg);
132 if( *pzErrMsg ){
133 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
134 }else{
135 rc = SQLITE_NOMEM;
136 sqlite3Error(db, SQLITE_NOMEM, 0);
138 }else if( pzErrMsg ){
139 *pzErrMsg = 0;
142 assert( (rc&db->errMask)==rc );
143 sqlite3_mutex_leave(db->mutex);
144 return rc;