wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Diagnostics;
3 using System.Text;
4  
5 namespace Community.CsharpSqlite
6 {
7 using sqlite3_callback = Sqlite3.dxCallback;
8 using sqlite3_stmt = Sqlite3.Vdbe;
9  
10 public partial class Sqlite3
11 {
12 /*
13 ** 2001 September 15
14 **
15 ** The author disclaims copyright to this source code. In place of
16 ** a legal notice, here is a blessing:
17 **
18 ** May you do good and not evil.
19 ** May you find forgiveness for yourself and forgive others.
20 ** May you share freely, never taking more than you give.
21 **
22 *************************************************************************
23 ** Main file for the SQLite library. The routines in this file
24 ** implement the programmer interface to the library. Routines in
25 ** other files are for internal use by SQLite and should not be
26 ** accessed by users of the library.
27 *************************************************************************
28 ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
29 ** C#-SQLite is an independent reimplementation of the SQLite software library
30 **
31 ** SQLITE_SOURCE_ID: 2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3
32 **
33 *************************************************************************
34 */
35  
36 //#include "sqliteInt.h"
37  
38 /*
39 ** Execute SQL code. Return one of the SQLITE_ success/failure
40 ** codes. Also write an error message into memory obtained from
41 ** malloc() and make pzErrMsg point to that message.
42 **
43 ** If the SQL is a query, then for each row in the query result
44 ** the xCallback() function is called. pArg becomes the first
45 ** argument to xCallback(). If xCallback=NULL then no callback
46 ** is invoked, even for queries.
47 */
48 //C# Alias
49 static public int exec( sqlite3 db, /* The database on which the SQL executes */ string zSql, /* The SQL to be executed */ int NoCallback, int NoArgs, int NoErrors )
50 {
51 string Errors = string.Empty;
52 return sqlite3_exec( db, zSql, null, null, ref Errors );
53 }
54  
55 static public int exec( sqlite3 db, /* The database on which the SQL executes */ string zSql, /* The SQL to be executed */ sqlite3_callback xCallback, /* Invoke this callback routine */ object pArg, /* First argument to xCallback() */ int NoErrors )
56 {
57 string Errors = string.Empty;
58 return sqlite3_exec( db, zSql, xCallback, pArg, ref Errors );
59 }
60 static public int exec( sqlite3 db, /* The database on which the SQL executes */ string zSql, /* The SQL to be executed */ sqlite3_callback xCallback, /* Invoke this callback routine */ object pArg, /* First argument to xCallback() */ ref string pzErrMsg /* Write error messages here */)
61 {
62 return sqlite3_exec( db, zSql, xCallback, pArg, ref pzErrMsg );
63 }
64  
65 //OVERLOADS
66 static public int sqlite3_exec(
67 sqlite3 db, /* The database on which the SQL executes */
68 string zSql, /* The SQL to be executed */
69 int NoCallback, int NoArgs, int NoErrors
70 )
71 {
72 string Errors = string.Empty;
73 return sqlite3_exec( db, zSql, null, null, ref Errors );
74 }
75  
76 static public int sqlite3_exec(
77 sqlite3 db, /* The database on which the SQL executes */
78 string zSql, /* The SQL to be executed */
79 sqlite3_callback xCallback, /* Invoke this callback routine */
80 object pArg, /* First argument to xCallback() */
81 int NoErrors
82 )
83 {
84 string Errors = string.Empty;
85 return sqlite3_exec( db, zSql, xCallback, pArg, ref Errors );
86 }
87 static public int sqlite3_exec(
88 sqlite3 db, /* The database on which the SQL executes */
89 string zSql, /* The SQL to be executed */
90 sqlite3_callback xCallback, /* Invoke this callback routine */
91 object pArg, /* First argument to xCallback() */
92 ref string pzErrMsg /* Write error messages here */
93 )
94 {
95  
96 int rc = SQLITE_OK; /* Return code */
97 string zLeftover = string.Empty; /* Tail of unprocessed SQL */
98 sqlite3_stmt pStmt = null; /* The current SQL statement */
99 string[] azCols = null; /* Names of result columns */
100 int nRetry = 0; /* Number of retry attempts */
101 int callbackIsInit; /* True if callback data is initialized */
102  
103 if ( !sqlite3SafetyCheckOk( db ) )
104 return SQLITE_MISUSE_BKPT();
105  
106 zSql = zSql ?? string.Empty;
107  
108 sqlite3_mutex_enter( db.mutex );
109 sqlite3Error( db, SQLITE_OK, 0 );
110 while ( ( rc == SQLITE_OK || ( rc == SQLITE_SCHEMA && ( ++nRetry ) < 2 ) ) && zSql.Length > 0 )
111 {
112 int nCol;
113 string[] azVals = null;
114  
115 pStmt = null;
116 rc = sqlite3_prepare( db, zSql, -1, ref pStmt, ref zLeftover );
117 Debug.Assert( rc == SQLITE_OK || pStmt == null );
118 if ( rc != SQLITE_OK )
119 {
120 continue;
121 }
122 if ( pStmt == null )
123 {
124 /* this happens for a comment or white-space */
125 zSql = zLeftover;
126 continue;
127 }
128  
129 callbackIsInit = 0;
130 nCol = sqlite3_column_count( pStmt );
131  
132 while ( true )
133 {
134 int i;
135 rc = sqlite3_step( pStmt );
136  
137 /* Invoke the callback function if required */
138 if ( xCallback != null && ( SQLITE_ROW == rc ||
139 ( SQLITE_DONE == rc && callbackIsInit == 0
140 && ( db.flags & SQLITE_NullCallback ) != 0 ) ) )
141 {
142 if ( 0 == callbackIsInit )
143 {
144 azCols = new string[nCol];//sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
145 //if ( azCols == null )
146 //{
147 // goto exec_out;
148 //}
149 for ( i = 0; i < nCol; i++ )
150 {
151 azCols[i] = sqlite3_column_name( pStmt, i );
152 /* sqlite3VdbeSetColName() installs column names as UTF8
153 ** strings so there is no way for sqlite3_column_name() to fail. */
154 Debug.Assert( azCols[i] != null );
155 }
156 callbackIsInit = 1;
157 }
158 if ( rc == SQLITE_ROW )
159 {
160 azVals = new string[nCol];// azCols[nCol];
161 for ( i = 0; i < nCol; i++ )
162 {
163 azVals[i] = sqlite3_column_text( pStmt, i );
164 if ( azVals[i] == null && sqlite3_column_type( pStmt, i ) != SQLITE_NULL )
165 {
166 //db.mallocFailed = 1;
167 //goto exec_out;
168 }
169 }
170 }
171 if ( xCallback( pArg, nCol, azVals, azCols ) != 0 )
172 {
173 rc = SQLITE_ABORT;
174 sqlite3VdbeFinalize( ref pStmt );
175 pStmt = null;
176 sqlite3Error( db, SQLITE_ABORT, 0 );
177 goto exec_out;
178 }
179 }
180  
181 if ( rc != SQLITE_ROW )
182 {
183 rc = sqlite3VdbeFinalize( ref pStmt );
184 pStmt = null;
185 if ( rc != SQLITE_SCHEMA )
186 {
187 nRetry = 0;
188 zSql = zLeftover;
189 int zindex = 0;
190 while ( zindex < zSql.Length && sqlite3Isspace( zSql[zindex] ) )
191 zindex++;
192 if ( zindex > 0 )
193 zSql = zindex < zSql.Length ? zSql.Substring( zindex ) : string.Empty;
194 }
195 break;
196 }
197 }
198  
199 sqlite3DbFree( db, ref azCols );
200 azCols = null;
201 }
202  
203 exec_out:
204 if ( pStmt != null )
205 sqlite3VdbeFinalize( ref pStmt );
206 sqlite3DbFree( db, ref azCols );
207  
208 rc = sqlite3ApiExit( db, rc );
209 if ( rc != SQLITE_OK && ALWAYS( rc == sqlite3_errcode( db ) ) && pzErrMsg != null )
210 {
211 //int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
212 //pzErrMsg = sqlite3Malloc(nErrMsg);
213 //if (pzErrMsg)
214 //{
215 // memcpy(pzErrMsg, sqlite3_errmsg(db), nErrMsg);
216 //}else{
217 //rc = SQLITE_NOMEM;
218 //sqlite3Error(db, SQLITE_NOMEM, 0);
219 //}
220 pzErrMsg = sqlite3_errmsg( db );
221 }
222 else if ( pzErrMsg.Length > 0 )
223 {
224 pzErrMsg = string.Empty;
225 }
226  
227 Debug.Assert( ( rc & db.errMask ) == rc );
228 sqlite3_mutex_leave( db.mutex );
229 return rc;
230 }
231 }
232 }