aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries
diff options
context:
space:
mode:
authordan miller2007-10-20 05:34:26 +0000
committerdan miller2007-10-20 05:34:26 +0000
commit354ea97baf765759911f0c56d3ed511350ebe348 (patch)
tree1adf96a98045d24b8741ba02bf21d195e70993ca /libraries
parentsqlite source (unix build) added to libraries (diff)
downloadopensim-SC_OLD-354ea97baf765759911f0c56d3ed511350ebe348.zip
opensim-SC_OLD-354ea97baf765759911f0c56d3ed511350ebe348.tar.gz
opensim-SC_OLD-354ea97baf765759911f0c56d3ed511350ebe348.tar.bz2
opensim-SC_OLD-354ea97baf765759911f0c56d3ed511350ebe348.tar.xz
sqlite 3.5.1 windows source
Diffstat (limited to 'libraries')
-rwxr-xr-xlibraries/sqlite/win32/alter.c622
-rwxr-xr-xlibraries/sqlite/win32/analyze.c418
-rwxr-xr-xlibraries/sqlite/win32/attach.c521
-rwxr-xr-xlibraries/sqlite/win32/auth.c234
-rwxr-xr-xlibraries/sqlite/win32/btmutex.c315
-rwxr-xr-xlibraries/sqlite/win32/btree.c6890
-rwxr-xr-xlibraries/sqlite/win32/btree.h204
-rwxr-xr-xlibraries/sqlite/win32/btreeInt.h648
-rwxr-xr-xlibraries/sqlite/win32/build.c3409
-rwxr-xr-xlibraries/sqlite/win32/callback.c378
-rwxr-xr-xlibraries/sqlite/win32/complete.c271
-rwxr-xr-xlibraries/sqlite/win32/date.c1045
-rwxr-xr-xlibraries/sqlite/win32/delete.c467
-rwxr-xr-xlibraries/sqlite/win32/expr.c2617
-rwxr-xr-xlibraries/sqlite/win32/fts1.c3344
-rwxr-xr-xlibraries/sqlite/win32/fts1.h11
-rwxr-xr-xlibraries/sqlite/win32/fts1_hash.c369
-rwxr-xr-xlibraries/sqlite/win32/fts1_hash.h112
-rwxr-xr-xlibraries/sqlite/win32/fts1_porter.c643
-rwxr-xr-xlibraries/sqlite/win32/fts1_tokenizer.h90
-rwxr-xr-xlibraries/sqlite/win32/fts1_tokenizer1.c221
-rwxr-xr-xlibraries/sqlite/win32/func.c1509
-rwxr-xr-xlibraries/sqlite/win32/hash.c418
-rwxr-xr-xlibraries/sqlite/win32/hash.h110
-rwxr-xr-xlibraries/sqlite/win32/insert.c1605
-rwxr-xr-xlibraries/sqlite/win32/journal.c238
-rwxr-xr-xlibraries/sqlite/win32/keywordhash.h112
-rwxr-xr-xlibraries/sqlite/win32/legacy.c134
-rwxr-xr-xlibraries/sqlite/win32/loadext.c516
-rwxr-xr-xlibraries/sqlite/win32/main.c1485
-rwxr-xr-xlibraries/sqlite/win32/malloc.c240
-rwxr-xr-xlibraries/sqlite/win32/mem1.c229
-rwxr-xr-xlibraries/sqlite/win32/mem2.c546
-rwxr-xr-xlibraries/sqlite/win32/mutex.c126
-rwxr-xr-xlibraries/sqlite/win32/mutex.h82
-rwxr-xr-xlibraries/sqlite/win32/mutex_os2.c236
-rwxr-xr-xlibraries/sqlite/win32/mutex_unix.c223
-rwxr-xr-xlibraries/sqlite/win32/mutex_w32.c210
-rwxr-xr-xlibraries/sqlite/win32/opcodes.c151
-rwxr-xr-xlibraries/sqlite/win32/opcodes.h160
-rw-r--r--libraries/sqlite/win32/opensim_build_notes.txt18
-rwxr-xr-xlibraries/sqlite/win32/os.c282
-rwxr-xr-xlibraries/sqlite/win32/os.h284
-rwxr-xr-xlibraries/sqlite/win32/os_common.h127
-rwxr-xr-xlibraries/sqlite/win32/os_os2.c1032
-rwxr-xr-xlibraries/sqlite/win32/os_unix.c2749
-rwxr-xr-xlibraries/sqlite/win32/os_win.c1545
-rwxr-xr-xlibraries/sqlite/win32/pager.c5104
-rwxr-xr-xlibraries/sqlite/win32/pager.h125
-rwxr-xr-xlibraries/sqlite/win32/parse.c3505
-rwxr-xr-xlibraries/sqlite/win32/parse.h152
-rwxr-xr-xlibraries/sqlite/win32/pragma.c1186
-rwxr-xr-xlibraries/sqlite/win32/prepare.c742
-rwxr-xr-xlibraries/sqlite/win32/printf.c907
-rwxr-xr-xlibraries/sqlite/win32/random.c103
-rwxr-xr-xlibraries/sqlite/win32/select.c3539
-rwxr-xr-xlibraries/sqlite/win32/shell.c2019
-rwxr-xr-xlibraries/sqlite/win32/sqlite3.h3552
-rwxr-xr-xlibraries/sqlite/win32/sqlite3ext.h350
-rwxr-xr-xlibraries/sqlite/win32/sqliteInt.h1972
-rwxr-xr-xlibraries/sqlite/win32/sqliteLimit.h169
-rwxr-xr-xlibraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005.sln20
-rwxr-xr-xlibraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite3.def131
-rwxr-xr-xlibraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj497
-rwxr-xr-xlibraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj.dan-PC.dan.user65
-rwxr-xr-xlibraries/sqlite/win32/table.c206
-rwxr-xr-xlibraries/sqlite/win32/tclsqlite.c2551
-rwxr-xr-xlibraries/sqlite/win32/tokenize.c508
-rwxr-xr-xlibraries/sqlite/win32/trigger.c839
-rwxr-xr-xlibraries/sqlite/win32/update.c630
-rwxr-xr-xlibraries/sqlite/win32/utf.c545
-rwxr-xr-xlibraries/sqlite/win32/util.c706
-rwxr-xr-xlibraries/sqlite/win32/vacuum.c262
-rwxr-xr-xlibraries/sqlite/win32/vdbe.c5279
-rwxr-xr-xlibraries/sqlite/win32/vdbe.h151
-rwxr-xr-xlibraries/sqlite/win32/vdbeInt.h427
-rwxr-xr-xlibraries/sqlite/win32/vdbeapi.c1066
-rwxr-xr-xlibraries/sqlite/win32/vdbeaux.c2243
-rwxr-xr-xlibraries/sqlite/win32/vdbeblob.c340
-rwxr-xr-xlibraries/sqlite/win32/vdbefifo.c114
-rwxr-xr-xlibraries/sqlite/win32/vdbemem.c1017
-rwxr-xr-xlibraries/sqlite/win32/vtab.c797
-rwxr-xr-xlibraries/sqlite/win32/where.c2773
83 files changed, 81788 insertions, 0 deletions
diff --git a/libraries/sqlite/win32/alter.c b/libraries/sqlite/win32/alter.c
new file mode 100755
index 0000000..9d1ebd2
--- /dev/null
+++ b/libraries/sqlite/win32/alter.c
@@ -0,0 +1,622 @@
1/*
2** 2005 February 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.
10**
11*************************************************************************
12** This file contains C code routines that used to generate VDBE code
13** that implements the ALTER TABLE command.
14**
15** $Id: alter.c,v 1.32 2007/08/29 14:06:23 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18#include <ctype.h>
19
20/*
21** The code in this file only exists if we are not omitting the
22** ALTER TABLE logic from the build.
23*/
24#ifndef SQLITE_OMIT_ALTERTABLE
25
26
27/*
28** This function is used by SQL generated to implement the
29** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
30** CREATE INDEX command. The second is a table name. The table name in
31** the CREATE TABLE or CREATE INDEX statement is replaced with the third
32** argument and the result returned. Examples:
33**
34** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
35** -> 'CREATE TABLE def(a, b, c)'
36**
37** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
38** -> 'CREATE INDEX i ON def(a, b, c)'
39*/
40static void renameTableFunc(
41 sqlite3_context *context,
42 int argc,
43 sqlite3_value **argv
44){
45 unsigned char const *zSql = sqlite3_value_text(argv[0]);
46 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
47
48 int token;
49 Token tname;
50 unsigned char const *zCsr = zSql;
51 int len = 0;
52 char *zRet;
53
54 sqlite3 *db = sqlite3_user_data(context);
55
56 /* The principle used to locate the table name in the CREATE TABLE
57 ** statement is that the table name is the first token that is immediatedly
58 ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
59 */
60 if( zSql ){
61 do {
62 if( !*zCsr ){
63 /* Ran out of input before finding an opening bracket. Return NULL. */
64 return;
65 }
66
67 /* Store the token that zCsr points to in tname. */
68 tname.z = zCsr;
69 tname.n = len;
70
71 /* Advance zCsr to the next token. Store that token type in 'token',
72 ** and it's length in 'len' (to be used next iteration of this loop).
73 */
74 do {
75 zCsr += len;
76 len = sqlite3GetToken(zCsr, &token);
77 } while( token==TK_SPACE );
78 assert( len>0 );
79 } while( token!=TK_LP && token!=TK_USING );
80
81 zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql,
82 zTableName, tname.z+tname.n);
83 sqlite3_result_text(context, zRet, -1, sqlite3_free);
84 }
85}
86
87#ifndef SQLITE_OMIT_TRIGGER
88/* This function is used by SQL generated to implement the
89** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
90** statement. The second is a table name. The table name in the CREATE
91** TRIGGER statement is replaced with the third argument and the result
92** returned. This is analagous to renameTableFunc() above, except for CREATE
93** TRIGGER, not CREATE INDEX and CREATE TABLE.
94*/
95static void renameTriggerFunc(
96 sqlite3_context *context,
97 int argc,
98 sqlite3_value **argv
99){
100 unsigned char const *zSql = sqlite3_value_text(argv[0]);
101 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
102
103 int token;
104 Token tname;
105 int dist = 3;
106 unsigned char const *zCsr = zSql;
107 int len = 0;
108 char *zRet;
109
110 sqlite3 *db = sqlite3_user_data(context);
111
112 /* The principle used to locate the table name in the CREATE TRIGGER
113 ** statement is that the table name is the first token that is immediatedly
114 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
115 ** of TK_WHEN, TK_BEGIN or TK_FOR.
116 */
117 if( zSql ){
118 do {
119
120 if( !*zCsr ){
121 /* Ran out of input before finding the table name. Return NULL. */
122 return;
123 }
124
125 /* Store the token that zCsr points to in tname. */
126 tname.z = zCsr;
127 tname.n = len;
128
129 /* Advance zCsr to the next token. Store that token type in 'token',
130 ** and it's length in 'len' (to be used next iteration of this loop).
131 */
132 do {
133 zCsr += len;
134 len = sqlite3GetToken(zCsr, &token);
135 }while( token==TK_SPACE );
136 assert( len>0 );
137
138 /* Variable 'dist' stores the number of tokens read since the most
139 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
140 ** token is read and 'dist' equals 2, the condition stated above
141 ** to be met.
142 **
143 ** Note that ON cannot be a database, table or column name, so
144 ** there is no need to worry about syntax like
145 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
146 */
147 dist++;
148 if( token==TK_DOT || token==TK_ON ){
149 dist = 0;
150 }
151 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
152
153 /* Variable tname now contains the token that is the old table-name
154 ** in the CREATE TRIGGER statement.
155 */
156 zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql,
157 zTableName, tname.z+tname.n);
158 sqlite3_result_text(context, zRet, -1, sqlite3_free);
159 }
160}
161#endif /* !SQLITE_OMIT_TRIGGER */
162
163/*
164** Register built-in functions used to help implement ALTER TABLE
165*/
166void sqlite3AlterFunctions(sqlite3 *db){
167 static const struct {
168 char *zName;
169 signed char nArg;
170 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
171 } aFuncs[] = {
172 { "sqlite_rename_table", 2, renameTableFunc},
173#ifndef SQLITE_OMIT_TRIGGER
174 { "sqlite_rename_trigger", 2, renameTriggerFunc},
175#endif
176 };
177 int i;
178
179 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
180 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
181 SQLITE_UTF8, (void *)db, aFuncs[i].xFunc, 0, 0);
182 }
183}
184
185/*
186** Generate the text of a WHERE expression which can be used to select all
187** temporary triggers on table pTab from the sqlite_temp_master table. If
188** table pTab has no temporary triggers, or is itself stored in the
189** temporary database, NULL is returned.
190*/
191static char *whereTempTriggers(Parse *pParse, Table *pTab){
192 Trigger *pTrig;
193 char *zWhere = 0;
194 char *tmp = 0;
195 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
196
197 /* If the table is not located in the temp-db (in which case NULL is
198 ** returned, loop through the tables list of triggers. For each trigger
199 ** that is not part of the temp-db schema, add a clause to the WHERE
200 ** expression being built up in zWhere.
201 */
202 if( pTab->pSchema!=pTempSchema ){
203 sqlite3 *db = pParse->db;
204 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
205 if( pTrig->pSchema==pTempSchema ){
206 if( !zWhere ){
207 zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
208 }else{
209 tmp = zWhere;
210 zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
211 sqlite3_free(tmp);
212 }
213 }
214 }
215 }
216 return zWhere;
217}
218
219/*
220** Generate code to drop and reload the internal representation of table
221** pTab from the database, including triggers and temporary triggers.
222** Argument zName is the name of the table in the database schema at
223** the time the generated code is executed. This can be different from
224** pTab->zName if this function is being called to code part of an
225** "ALTER TABLE RENAME TO" statement.
226*/
227static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
228 Vdbe *v;
229 char *zWhere;
230 int iDb; /* Index of database containing pTab */
231#ifndef SQLITE_OMIT_TRIGGER
232 Trigger *pTrig;
233#endif
234
235 v = sqlite3GetVdbe(pParse);
236 if( !v ) return;
237 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
238 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
239 assert( iDb>=0 );
240
241#ifndef SQLITE_OMIT_TRIGGER
242 /* Drop any table triggers from the internal schema. */
243 for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
244 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
245 assert( iTrigDb==iDb || iTrigDb==1 );
246 sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
247 }
248#endif
249
250 /* Drop the table and index from the internal schema */
251 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
252
253 /* Reload the table, index and permanent trigger schemas. */
254 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
255 if( !zWhere ) return;
256 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
257
258#ifndef SQLITE_OMIT_TRIGGER
259 /* Now, if the table is not stored in the temp database, reload any temp
260 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
261 */
262 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
263 sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC);
264 }
265#endif
266}
267
268/*
269** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
270** command.
271*/
272void sqlite3AlterRenameTable(
273 Parse *pParse, /* Parser context. */
274 SrcList *pSrc, /* The table to rename. */
275 Token *pName /* The new table name. */
276){
277 int iDb; /* Database that contains the table */
278 char *zDb; /* Name of database iDb */
279 Table *pTab; /* Table being renamed */
280 char *zName = 0; /* NULL-terminated version of pName */
281 sqlite3 *db = pParse->db; /* Database connection */
282 int nTabName; /* Number of UTF-8 characters in zTabName */
283 const char *zTabName; /* Original name of the table */
284 Vdbe *v;
285#ifndef SQLITE_OMIT_TRIGGER
286 char *zWhere = 0; /* Where clause to locate temp triggers */
287#endif
288 int isVirtualRename = 0; /* True if this is a v-table with an xRename() */
289
290 if( db->mallocFailed ) goto exit_rename_table;
291 assert( pSrc->nSrc==1 );
292 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
293
294 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
295 if( !pTab ) goto exit_rename_table;
296 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
297 zDb = db->aDb[iDb].zName;
298
299 /* Get a NULL terminated version of the new table name. */
300 zName = sqlite3NameFromToken(db, pName);
301 if( !zName ) goto exit_rename_table;
302
303 /* Check that a table or index named 'zName' does not already exist
304 ** in database iDb. If so, this is an error.
305 */
306 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
307 sqlite3ErrorMsg(pParse,
308 "there is already another table or index with this name: %s", zName);
309 goto exit_rename_table;
310 }
311
312 /* Make sure it is not a system table being altered, or a reserved name
313 ** that the table is being renamed to.
314 */
315 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
316 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
317 goto exit_rename_table;
318 }
319 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
320 goto exit_rename_table;
321 }
322
323#ifndef SQLITE_OMIT_AUTHORIZATION
324 /* Invoke the authorization callback. */
325 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
326 goto exit_rename_table;
327 }
328#endif
329
330#ifndef SQLITE_OMIT_VIRTUALTABLE
331 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
332 goto exit_rename_table;
333 }
334 if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
335 isVirtualRename = 1;
336 }
337#endif
338
339 /* Begin a transaction and code the VerifyCookie for database iDb.
340 ** Then modify the schema cookie (since the ALTER TABLE modifies the
341 ** schema). Open a statement transaction if the table is a virtual
342 ** table.
343 */
344 v = sqlite3GetVdbe(pParse);
345 if( v==0 ){
346 goto exit_rename_table;
347 }
348 sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
349 sqlite3ChangeCookie(db, v, iDb);
350
351 /* If this is a virtual table, invoke the xRename() function if
352 ** one is defined. The xRename() callback will modify the names
353 ** of any resources used by the v-table implementation (including other
354 ** SQLite tables) that are identified by the name of the virtual table.
355 */
356#ifndef SQLITE_OMIT_VIRTUALTABLE
357 if( isVirtualRename ){
358 sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0);
359 sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB);
360 }
361#endif
362
363 /* figure out how many UTF-8 characters are in zName */
364 zTabName = pTab->zName;
365 nTabName = sqlite3Utf8CharLen(zTabName, -1);
366
367 /* Modify the sqlite_master table to use the new table name. */
368 sqlite3NestedParse(pParse,
369 "UPDATE %Q.%s SET "
370#ifdef SQLITE_OMIT_TRIGGER
371 "sql = sqlite_rename_table(sql, %Q), "
372#else
373 "sql = CASE "
374 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
375 "ELSE sqlite_rename_table(sql, %Q) END, "
376#endif
377 "tbl_name = %Q, "
378 "name = CASE "
379 "WHEN type='table' THEN %Q "
380 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
381 "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) "
382 "ELSE name END "
383 "WHERE tbl_name=%Q AND "
384 "(type='table' OR type='index' OR type='trigger');",
385 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
386#ifndef SQLITE_OMIT_TRIGGER
387 zName,
388#endif
389 zName, nTabName, zTabName
390 );
391
392#ifndef SQLITE_OMIT_AUTOINCREMENT
393 /* If the sqlite_sequence table exists in this database, then update
394 ** it with the new table name.
395 */
396 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
397 sqlite3NestedParse(pParse,
398 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
399 zDb, zName, pTab->zName);
400 }
401#endif
402
403#ifndef SQLITE_OMIT_TRIGGER
404 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
405 ** table. Don't do this if the table being ALTERed is itself located in
406 ** the temp database.
407 */
408 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
409 sqlite3NestedParse(pParse,
410 "UPDATE sqlite_temp_master SET "
411 "sql = sqlite_rename_trigger(sql, %Q), "
412 "tbl_name = %Q "
413 "WHERE %s;", zName, zName, zWhere);
414 sqlite3_free(zWhere);
415 }
416#endif
417
418 /* Drop and reload the internal table schema. */
419 reloadTableSchema(pParse, pTab, zName);
420
421exit_rename_table:
422 sqlite3SrcListDelete(pSrc);
423 sqlite3_free(zName);
424}
425
426
427/*
428** This function is called after an "ALTER TABLE ... ADD" statement
429** has been parsed. Argument pColDef contains the text of the new
430** column definition.
431**
432** The Table structure pParse->pNewTable was extended to include
433** the new column during parsing.
434*/
435void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
436 Table *pNew; /* Copy of pParse->pNewTable */
437 Table *pTab; /* Table being altered */
438 int iDb; /* Database number */
439 const char *zDb; /* Database name */
440 const char *zTab; /* Table name */
441 char *zCol; /* Null-terminated column definition */
442 Column *pCol; /* The new column */
443 Expr *pDflt; /* Default value for the new column */
444 sqlite3 *db; /* The database connection; */
445
446 if( pParse->nErr ) return;
447 pNew = pParse->pNewTable;
448 assert( pNew );
449
450 db = pParse->db;
451 assert( sqlite3BtreeHoldsAllMutexes(db) );
452 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
453 zDb = db->aDb[iDb].zName;
454 zTab = pNew->zName;
455 pCol = &pNew->aCol[pNew->nCol-1];
456 pDflt = pCol->pDflt;
457 pTab = sqlite3FindTable(db, zTab, zDb);
458 assert( pTab );
459
460#ifndef SQLITE_OMIT_AUTHORIZATION
461 /* Invoke the authorization callback. */
462 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
463 return;
464 }
465#endif
466
467 /* If the default value for the new column was specified with a
468 ** literal NULL, then set pDflt to 0. This simplifies checking
469 ** for an SQL NULL default below.
470 */
471 if( pDflt && pDflt->op==TK_NULL ){
472 pDflt = 0;
473 }
474
475 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
476 ** If there is a NOT NULL constraint, then the default value for the
477 ** column must not be NULL.
478 */
479 if( pCol->isPrimKey ){
480 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
481 return;
482 }
483 if( pNew->pIndex ){
484 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
485 return;
486 }
487 if( pCol->notNull && !pDflt ){
488 sqlite3ErrorMsg(pParse,
489 "Cannot add a NOT NULL column with default value NULL");
490 return;
491 }
492
493 /* Ensure the default expression is something that sqlite3ValueFromExpr()
494 ** can handle (i.e. not CURRENT_TIME etc.)
495 */
496 if( pDflt ){
497 sqlite3_value *pVal;
498 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
499 db->mallocFailed = 1;
500 return;
501 }
502 if( !pVal ){
503 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
504 return;
505 }
506 sqlite3ValueFree(pVal);
507 }
508
509 /* Modify the CREATE TABLE statement. */
510 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
511 if( zCol ){
512 char *zEnd = &zCol[pColDef->n-1];
513 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
514 *zEnd-- = '\0';
515 }
516 sqlite3NestedParse(pParse,
517 "UPDATE %Q.%s SET "
518 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
519 "WHERE type = 'table' AND name = %Q",
520 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
521 zTab
522 );
523 sqlite3_free(zCol);
524 }
525
526 /* If the default value of the new column is NULL, then set the file
527 ** format to 2. If the default value of the new column is not NULL,
528 ** the file format becomes 3.
529 */
530 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
531
532 /* Reload the schema of the modified table. */
533 reloadTableSchema(pParse, pTab, pTab->zName);
534}
535
536/*
537** This function is called by the parser after the table-name in
538** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
539** pSrc is the full-name of the table being altered.
540**
541** This routine makes a (partial) copy of the Table structure
542** for the table being altered and sets Parse.pNewTable to point
543** to it. Routines called by the parser as the column definition
544** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
545** the copy. The copy of the Table structure is deleted by tokenize.c
546** after parsing is finished.
547**
548** Routine sqlite3AlterFinishAddColumn() will be called to complete
549** coding the "ALTER TABLE ... ADD" statement.
550*/
551void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
552 Table *pNew;
553 Table *pTab;
554 Vdbe *v;
555 int iDb;
556 int i;
557 int nAlloc;
558 sqlite3 *db = pParse->db;
559
560 /* Look up the table being altered. */
561 assert( pParse->pNewTable==0 );
562 assert( sqlite3BtreeHoldsAllMutexes(db) );
563 if( db->mallocFailed ) goto exit_begin_add_column;
564 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
565 if( !pTab ) goto exit_begin_add_column;
566
567#ifndef SQLITE_OMIT_VIRTUALTABLE
568 if( IsVirtual(pTab) ){
569 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
570 goto exit_begin_add_column;
571 }
572#endif
573
574 /* Make sure this is not an attempt to ALTER a view. */
575 if( pTab->pSelect ){
576 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
577 goto exit_begin_add_column;
578 }
579
580 assert( pTab->addColOffset>0 );
581 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
582
583 /* Put a copy of the Table struct in Parse.pNewTable for the
584 ** sqlite3AddColumn() function and friends to modify.
585 */
586 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
587 if( !pNew ) goto exit_begin_add_column;
588 pParse->pNewTable = pNew;
589 pNew->nRef = 1;
590 pNew->nCol = pTab->nCol;
591 assert( pNew->nCol>0 );
592 nAlloc = (((pNew->nCol-1)/8)*8)+8;
593 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
594 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
595 pNew->zName = sqlite3DbStrDup(db, pTab->zName);
596 if( !pNew->aCol || !pNew->zName ){
597 db->mallocFailed = 1;
598 goto exit_begin_add_column;
599 }
600 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
601 for(i=0; i<pNew->nCol; i++){
602 Column *pCol = &pNew->aCol[i];
603 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
604 pCol->zColl = 0;
605 pCol->zType = 0;
606 pCol->pDflt = 0;
607 }
608 pNew->pSchema = db->aDb[iDb].pSchema;
609 pNew->addColOffset = pTab->addColOffset;
610 pNew->nRef = 1;
611
612 /* Begin a transaction and increment the schema cookie. */
613 sqlite3BeginWriteOperation(pParse, 0, iDb);
614 v = sqlite3GetVdbe(pParse);
615 if( !v ) goto exit_begin_add_column;
616 sqlite3ChangeCookie(db, v, iDb);
617
618exit_begin_add_column:
619 sqlite3SrcListDelete(pSrc);
620 return;
621}
622#endif /* SQLITE_ALTER_TABLE */
diff --git a/libraries/sqlite/win32/analyze.c b/libraries/sqlite/win32/analyze.c
new file mode 100755
index 0000000..119e7f6
--- /dev/null
+++ b/libraries/sqlite/win32/analyze.c
@@ -0,0 +1,418 @@
1/*
2** 2005 July 8
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.
10**
11*************************************************************************
12** This file contains code associated with the ANALYZE command.
13**
14** @(#) $Id: analyze.c,v 1.23 2007/08/29 17:43:20 drh Exp $
15*/
16#ifndef SQLITE_OMIT_ANALYZE
17#include "sqliteInt.h"
18
19/*
20** This routine generates code that opens the sqlite_stat1 table on cursor
21** iStatCur.
22**
23** If the sqlite_stat1 tables does not previously exist, it is created.
24** If it does previously exist, all entires associated with table zWhere
25** are removed. If zWhere==0 then all entries are removed.
26*/
27static void openStatTable(
28 Parse *pParse, /* Parsing context */
29 int iDb, /* The database we are looking in */
30 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
31 const char *zWhere /* Delete entries associated with this table */
32){
33 sqlite3 *db = pParse->db;
34 Db *pDb;
35 int iRootPage;
36 Table *pStat;
37 Vdbe *v = sqlite3GetVdbe(pParse);
38
39 if( v==0 ) return;
40 assert( sqlite3BtreeHoldsAllMutexes(db) );
41 assert( sqlite3VdbeDb(v)==db );
42 pDb = &db->aDb[iDb];
43 if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
44 /* The sqlite_stat1 tables does not exist. Create it.
45 ** Note that a side-effect of the CREATE TABLE statement is to leave
46 ** the rootpage of the new table on the top of the stack. This is
47 ** important because the OpenWrite opcode below will be needing it. */
48 sqlite3NestedParse(pParse,
49 "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)",
50 pDb->zName
51 );
52 iRootPage = 0; /* Cause rootpage to be taken from top of stack */
53 }else if( zWhere ){
54 /* The sqlite_stat1 table exists. Delete all entries associated with
55 ** the table zWhere. */
56 sqlite3NestedParse(pParse,
57 "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q",
58 pDb->zName, zWhere
59 );
60 iRootPage = pStat->tnum;
61 }else{
62 /* The sqlite_stat1 table already exists. Delete all rows. */
63 iRootPage = pStat->tnum;
64 sqlite3VdbeAddOp(v, OP_Clear, pStat->tnum, iDb);
65 }
66
67 /* Open the sqlite_stat1 table for writing. Unless it was created
68 ** by this vdbe program, lock it for writing at the shared-cache level.
69 ** If this vdbe did create the sqlite_stat1 table, then it must have
70 ** already obtained a schema-lock, making the write-lock redundant.
71 */
72 if( iRootPage>0 ){
73 sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
74 }
75 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
76 sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage);
77 sqlite3VdbeAddOp(v, OP_SetNumColumns, iStatCur, 3);
78}
79
80/*
81** Generate code to do an analysis of all indices associated with
82** a single table.
83*/
84static void analyzeOneTable(
85 Parse *pParse, /* Parser context */
86 Table *pTab, /* Table whose indices are to be analyzed */
87 int iStatCur, /* Cursor that writes to the sqlite_stat1 table */
88 int iMem /* Available memory locations begin here */
89){
90 Index *pIdx; /* An index to being analyzed */
91 int iIdxCur; /* Cursor number for index being analyzed */
92 int nCol; /* Number of columns in the index */
93 Vdbe *v; /* The virtual machine being built up */
94 int i; /* Loop counter */
95 int topOfLoop; /* The top of the loop */
96 int endOfLoop; /* The end of the loop */
97 int addr; /* The address of an instruction */
98 int iDb; /* Index of database containing pTab */
99
100 v = sqlite3GetVdbe(pParse);
101 if( v==0 || pTab==0 || pTab->pIndex==0 ){
102 /* Do no analysis for tables that have no indices */
103 return;
104 }
105 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
106 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
107 assert( iDb>=0 );
108#ifndef SQLITE_OMIT_AUTHORIZATION
109 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
110 pParse->db->aDb[iDb].zName ) ){
111 return;
112 }
113#endif
114
115 /* Establish a read-lock on the table at the shared-cache level. */
116 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
117
118 iIdxCur = pParse->nTab;
119 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
121
122 /* Open a cursor to the index to be analyzed
123 */
124 assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
125 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
126 VdbeComment((v, "# %s", pIdx->zName));
127 sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum,
128 (char *)pKey, P3_KEYINFO_HANDOFF);
129 nCol = pIdx->nColumn;
130 if( iMem+nCol*2>=pParse->nMem ){
131 pParse->nMem = iMem+nCol*2+1;
132 }
133 sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, nCol+1);
134
135 /* Memory cells are used as follows:
136 **
137 ** mem[iMem]: The total number of rows in the table.
138 ** mem[iMem+1]: Number of distinct values in column 1
139 ** ...
140 ** mem[iMem+nCol]: Number of distinct values in column N
141 ** mem[iMem+nCol+1] Last observed value of column 1
142 ** ...
143 ** mem[iMem+nCol+nCol]: Last observed value of column N
144 **
145 ** Cells iMem through iMem+nCol are initialized to 0. The others
146 ** are initialized to NULL.
147 */
148 for(i=0; i<=nCol; i++){
149 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i);
150 }
151 for(i=0; i<nCol; i++){
152 sqlite3VdbeAddOp(v, OP_MemNull, iMem+nCol+i+1, 0);
153 }
154
155 /* Do the analysis.
156 */
157 endOfLoop = sqlite3VdbeMakeLabel(v);
158 sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, endOfLoop);
159 topOfLoop = sqlite3VdbeCurrentAddr(v);
160 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iMem);
161 for(i=0; i<nCol; i++){
162 sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i);
163 sqlite3VdbeAddOp(v, OP_MemLoad, iMem+nCol+i+1, 0);
164 sqlite3VdbeAddOp(v, OP_Ne, 0x100, 0);
165 }
166 sqlite3VdbeAddOp(v, OP_Goto, 0, endOfLoop);
167 for(i=0; i<nCol; i++){
168 addr = sqlite3VdbeAddOp(v, OP_MemIncr, 1, iMem+i+1);
169 sqlite3VdbeChangeP2(v, topOfLoop + 3*i + 3, addr);
170 sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i);
171 sqlite3VdbeAddOp(v, OP_MemStore, iMem+nCol+i+1, 1);
172 }
173 sqlite3VdbeResolveLabel(v, endOfLoop);
174 sqlite3VdbeAddOp(v, OP_Next, iIdxCur, topOfLoop);
175 sqlite3VdbeAddOp(v, OP_Close, iIdxCur, 0);
176
177 /* Store the results.
178 **
179 ** The result is a single row of the sqlite_stat1 table. The first
180 ** two columns are the names of the table and index. The third column
181 ** is a string composed of a list of integer statistics about the
182 ** index. The first integer in the list is the total number of entires
183 ** in the index. There is one additional integer in the list for each
184 ** column of the table. This additional integer is a guess of how many
185 ** rows of the table the index will select. If D is the count of distinct
186 ** values and K is the total number of rows, then the integer is computed
187 ** as:
188 **
189 ** I = (K+D-1)/D
190 **
191 ** If K==0 then no entry is made into the sqlite_stat1 table.
192 ** If K>0 then it is always the case the D>0 so division by zero
193 ** is never possible.
194 */
195 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
196 addr = sqlite3VdbeAddOp(v, OP_IfNot, 0, 0);
197 sqlite3VdbeAddOp(v, OP_NewRowid, iStatCur, 0);
198 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
199 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
200 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
201 sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0);
202 for(i=0; i<nCol; i++){
203 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
204 sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0);
205 sqlite3VdbeAddOp(v, OP_Add, 0, 0);
206 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
207 sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0);
208 sqlite3VdbeAddOp(v, OP_Divide, 0, 0);
209 sqlite3VdbeAddOp(v, OP_ToInt, 0, 0);
210 if( i==nCol-1 ){
211 sqlite3VdbeAddOp(v, OP_Concat, nCol*2-1, 0);
212 }else{
213 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
214 }
215 }
216 sqlite3VdbeOp3(v, OP_MakeRecord, 3, 0, "aaa", 0);
217 sqlite3VdbeAddOp(v, OP_Insert, iStatCur, OPFLAG_APPEND);
218 sqlite3VdbeJumpHere(v, addr);
219 }
220}
221
222/*
223** Generate code that will cause the most recent index analysis to
224** be laoded into internal hash tables where is can be used.
225*/
226static void loadAnalysis(Parse *pParse, int iDb){
227 Vdbe *v = sqlite3GetVdbe(pParse);
228 if( v ){
229 sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0);
230 }
231}
232
233/*
234** Generate code that will do an analysis of an entire database
235*/
236static void analyzeDatabase(Parse *pParse, int iDb){
237 sqlite3 *db = pParse->db;
238 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
239 HashElem *k;
240 int iStatCur;
241 int iMem;
242
243 sqlite3BeginWriteOperation(pParse, 0, iDb);
244 iStatCur = pParse->nTab++;
245 openStatTable(pParse, iDb, iStatCur, 0);
246 iMem = pParse->nMem;
247 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
248 Table *pTab = (Table*)sqliteHashData(k);
249 analyzeOneTable(pParse, pTab, iStatCur, iMem);
250 }
251 loadAnalysis(pParse, iDb);
252}
253
254/*
255** Generate code that will do an analysis of a single table in
256** a database.
257*/
258static void analyzeTable(Parse *pParse, Table *pTab){
259 int iDb;
260 int iStatCur;
261
262 assert( pTab!=0 );
263 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
264 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
265 sqlite3BeginWriteOperation(pParse, 0, iDb);
266 iStatCur = pParse->nTab++;
267 openStatTable(pParse, iDb, iStatCur, pTab->zName);
268 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem);
269 loadAnalysis(pParse, iDb);
270}
271
272/*
273** Generate code for the ANALYZE command. The parser calls this routine
274** when it recognizes an ANALYZE command.
275**
276** ANALYZE -- 1
277** ANALYZE <database> -- 2
278** ANALYZE ?<database>.?<tablename> -- 3
279**
280** Form 1 causes all indices in all attached databases to be analyzed.
281** Form 2 analyzes all indices the single database named.
282** Form 3 analyzes all indices associated with the named table.
283*/
284void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
285 sqlite3 *db = pParse->db;
286 int iDb;
287 int i;
288 char *z, *zDb;
289 Table *pTab;
290 Token *pTableName;
291
292 /* Read the database schema. If an error occurs, leave an error message
293 ** and code in pParse and return NULL. */
294 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
295 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
296 return;
297 }
298
299 if( pName1==0 ){
300 /* Form 1: Analyze everything */
301 for(i=0; i<db->nDb; i++){
302 if( i==1 ) continue; /* Do not analyze the TEMP database */
303 analyzeDatabase(pParse, i);
304 }
305 }else if( pName2==0 || pName2->n==0 ){
306 /* Form 2: Analyze the database or table named */
307 iDb = sqlite3FindDb(db, pName1);
308 if( iDb>=0 ){
309 analyzeDatabase(pParse, iDb);
310 }else{
311 z = sqlite3NameFromToken(db, pName1);
312 pTab = sqlite3LocateTable(pParse, z, 0);
313 sqlite3_free(z);
314 if( pTab ){
315 analyzeTable(pParse, pTab);
316 }
317 }
318 }else{
319 /* Form 3: Analyze the fully qualified table name */
320 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
321 if( iDb>=0 ){
322 zDb = db->aDb[iDb].zName;
323 z = sqlite3NameFromToken(db, pTableName);
324 if( z ){
325 pTab = sqlite3LocateTable(pParse, z, zDb);
326 sqlite3_free(z);
327 if( pTab ){
328 analyzeTable(pParse, pTab);
329 }
330 }
331 }
332 }
333}
334
335/*
336** Used to pass information from the analyzer reader through to the
337** callback routine.
338*/
339typedef struct analysisInfo analysisInfo;
340struct analysisInfo {
341 sqlite3 *db;
342 const char *zDatabase;
343};
344
345/*
346** This callback is invoked once for each index when reading the
347** sqlite_stat1 table.
348**
349** argv[0] = name of the index
350** argv[1] = results of analysis - on integer for each column
351*/
352static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
353 analysisInfo *pInfo = (analysisInfo*)pData;
354 Index *pIndex;
355 int i, c;
356 unsigned int v;
357 const char *z;
358
359 assert( argc==2 );
360 if( argv==0 || argv[0]==0 || argv[1]==0 ){
361 return 0;
362 }
363 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
364 if( pIndex==0 ){
365 return 0;
366 }
367 z = argv[1];
368 for(i=0; *z && i<=pIndex->nColumn; i++){
369 v = 0;
370 while( (c=z[0])>='0' && c<='9' ){
371 v = v*10 + c - '0';
372 z++;
373 }
374 pIndex->aiRowEst[i] = v;
375 if( *z==' ' ) z++;
376 }
377 return 0;
378}
379
380/*
381** Load the content of the sqlite_stat1 table into the index hash tables.
382*/
383int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
384 analysisInfo sInfo;
385 HashElem *i;
386 char *zSql;
387 int rc;
388
389 assert( iDb>=0 && iDb<db->nDb );
390 assert( db->aDb[iDb].pBt!=0 );
391 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
392
393 /* Clear any prior statistics */
394 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
395 Index *pIdx = sqliteHashData(i);
396 sqlite3DefaultRowEst(pIdx);
397 }
398
399 /* Check to make sure the sqlite_stat1 table existss */
400 sInfo.db = db;
401 sInfo.zDatabase = db->aDb[iDb].zName;
402 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
403 return SQLITE_ERROR;
404 }
405
406
407 /* Load new statistics out of the sqlite_stat1 table */
408 zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
409 sInfo.zDatabase);
410 sqlite3SafetyOff(db);
411 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
412 sqlite3SafetyOn(db);
413 sqlite3_free(zSql);
414 return rc;
415}
416
417
418#endif /* SQLITE_OMIT_ANALYZE */
diff --git a/libraries/sqlite/win32/attach.c b/libraries/sqlite/win32/attach.c
new file mode 100755
index 0000000..2fb950f
--- /dev/null
+++ b/libraries/sqlite/win32/attach.c
@@ -0,0 +1,521 @@
1/*
2** 2003 April 6
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.
10**
11*************************************************************************
12** This file contains code used to implement the ATTACH and DETACH commands.
13**
14** $Id: attach.c,v 1.63 2007/10/03 08:46:44 danielk1977 Exp $
15*/
16#include "sqliteInt.h"
17
18#ifndef SQLITE_OMIT_ATTACH
19/*
20** Resolve an expression that was part of an ATTACH or DETACH statement. This
21** is slightly different from resolving a normal SQL expression, because simple
22** identifiers are treated as strings, not possible column names or aliases.
23**
24** i.e. if the parser sees:
25**
26** ATTACH DATABASE abc AS def
27**
28** it treats the two expressions as literal strings 'abc' and 'def' instead of
29** looking for columns of the same name.
30**
31** This only applies to the root node of pExpr, so the statement:
32**
33** ATTACH DATABASE abc||def AS 'db2'
34**
35** will fail because neither abc or def can be resolved.
36*/
37static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
38{
39 int rc = SQLITE_OK;
40 if( pExpr ){
41 if( pExpr->op!=TK_ID ){
42 rc = sqlite3ExprResolveNames(pName, pExpr);
43 if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
44 sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span);
45 return SQLITE_ERROR;
46 }
47 }else{
48 pExpr->op = TK_STRING;
49 }
50 }
51 return rc;
52}
53
54/*
55** An SQL user-function registered to do the work of an ATTACH statement. The
56** three arguments to the function come directly from an attach statement:
57**
58** ATTACH DATABASE x AS y KEY z
59**
60** SELECT sqlite_attach(x, y, z)
61**
62** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
63** third argument.
64*/
65static void attachFunc(
66 sqlite3_context *context,
67 int argc,
68 sqlite3_value **argv
69){
70 int i;
71 int rc = 0;
72 sqlite3 *db = sqlite3_user_data(context);
73 const char *zName;
74 const char *zFile;
75 Db *aNew;
76 char *zErrDyn = 0;
77 char zErr[128];
78
79 zFile = (const char *)sqlite3_value_text(argv[0]);
80 zName = (const char *)sqlite3_value_text(argv[1]);
81 if( zFile==0 ) zFile = "";
82 if( zName==0 ) zName = "";
83
84 /* Check for the following errors:
85 **
86 ** * Too many attached databases,
87 ** * Transaction currently open
88 ** * Specified database name already being used.
89 */
90 if( db->nDb>=SQLITE_MAX_ATTACHED+2 ){
91 sqlite3_snprintf(
92 sizeof(zErr), zErr, "too many attached databases - max %d",
93 SQLITE_MAX_ATTACHED
94 );
95 goto attach_error;
96 }
97 if( !db->autoCommit ){
98 sqlite3_snprintf(sizeof(zErr), zErr,
99 "cannot ATTACH database within transaction");
100 goto attach_error;
101 }
102 for(i=0; i<db->nDb; i++){
103 char *z = db->aDb[i].zName;
104 if( z && zName && sqlite3StrICmp(z, zName)==0 ){
105 sqlite3_snprintf(sizeof(zErr), zErr,
106 "database %s is already in use", zName);
107 goto attach_error;
108 }
109 }
110
111 /* Allocate the new entry in the db->aDb[] array and initialise the schema
112 ** hash tables.
113 */
114 if( db->aDb==db->aDbStatic ){
115 aNew = sqlite3_malloc( sizeof(db->aDb[0])*3 );
116 if( aNew==0 ){
117 db->mallocFailed = 1;
118 return;
119 }
120 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
121 }else{
122 aNew = sqlite3_realloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
123 if( aNew==0 ){
124 db->mallocFailed = 1;
125 return;
126 }
127 }
128 db->aDb = aNew;
129 aNew = &db->aDb[db->nDb++];
130 memset(aNew, 0, sizeof(*aNew));
131
132 /* Open the database file. If the btree is successfully opened, use
133 ** it to obtain the database schema. At this point the schema may
134 ** or may not be initialised.
135 */
136 rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
137 db->openFlags | SQLITE_OPEN_MAIN_DB,
138 &aNew->pBt);
139 if( rc==SQLITE_OK ){
140 aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
141 if( !aNew->pSchema ){
142 rc = SQLITE_NOMEM;
143 }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
144 sqlite3_snprintf(sizeof(zErr), zErr,
145 "attached databases must use the same text encoding as main database");
146 goto attach_error;
147 }
148 sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode);
149 }
150 aNew->zName = sqlite3DbStrDup(db, zName);
151 aNew->safety_level = 3;
152
153#if SQLITE_HAS_CODEC
154 {
155 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
156 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
157 int nKey;
158 char *zKey;
159 int t = sqlite3_value_type(argv[2]);
160 switch( t ){
161 case SQLITE_INTEGER:
162 case SQLITE_FLOAT:
163 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
164 rc = SQLITE_ERROR;
165 break;
166
167 case SQLITE_TEXT:
168 case SQLITE_BLOB:
169 nKey = sqlite3_value_bytes(argv[2]);
170 zKey = (char *)sqlite3_value_blob(argv[2]);
171 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
172 break;
173
174 case SQLITE_NULL:
175 /* No key specified. Use the key from the main database */
176 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
177 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
178 break;
179 }
180 }
181#endif
182
183 /* If the file was opened successfully, read the schema for the new database.
184 ** If this fails, or if opening the file failed, then close the file and
185 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
186 ** we found it.
187 */
188 if( rc==SQLITE_OK ){
189 sqlite3SafetyOn(db);
190 rc = sqlite3Init(db, &zErrDyn);
191 sqlite3SafetyOff(db);
192 }
193 if( rc ){
194 int iDb = db->nDb - 1;
195 assert( iDb>=2 );
196 if( db->aDb[iDb].pBt ){
197 sqlite3BtreeClose(db->aDb[iDb].pBt);
198 db->aDb[iDb].pBt = 0;
199 db->aDb[iDb].pSchema = 0;
200 }
201 sqlite3ResetInternalSchema(db, 0);
202 db->nDb = iDb;
203 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
204 db->mallocFailed = 1;
205 sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
206 }else{
207 sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
208 }
209 goto attach_error;
210 }
211
212 return;
213
214attach_error:
215 /* Return an error if we get here */
216 if( zErrDyn ){
217 sqlite3_result_error(context, zErrDyn, -1);
218 sqlite3_free(zErrDyn);
219 }else{
220 zErr[sizeof(zErr)-1] = 0;
221 sqlite3_result_error(context, zErr, -1);
222 }
223}
224
225/*
226** An SQL user-function registered to do the work of an DETACH statement. The
227** three arguments to the function come directly from a detach statement:
228**
229** DETACH DATABASE x
230**
231** SELECT sqlite_detach(x)
232*/
233static void detachFunc(
234 sqlite3_context *context,
235 int argc,
236 sqlite3_value **argv
237){
238 const char *zName = (const char *)sqlite3_value_text(argv[0]);
239 sqlite3 *db = sqlite3_user_data(context);
240 int i;
241 Db *pDb = 0;
242 char zErr[128];
243
244 if( zName==0 ) zName = "";
245 for(i=0; i<db->nDb; i++){
246 pDb = &db->aDb[i];
247 if( pDb->pBt==0 ) continue;
248 if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
249 }
250
251 if( i>=db->nDb ){
252 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
253 goto detach_error;
254 }
255 if( i<2 ){
256 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
257 goto detach_error;
258 }
259 if( !db->autoCommit ){
260 sqlite3_snprintf(sizeof(zErr), zErr,
261 "cannot DETACH database within transaction");
262 goto detach_error;
263 }
264 if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){
265 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
266 goto detach_error;
267 }
268
269 sqlite3BtreeClose(pDb->pBt);
270 pDb->pBt = 0;
271 pDb->pSchema = 0;
272 sqlite3ResetInternalSchema(db, 0);
273 return;
274
275detach_error:
276 sqlite3_result_error(context, zErr, -1);
277}
278
279/*
280** This procedure generates VDBE code for a single invocation of either the
281** sqlite_detach() or sqlite_attach() SQL user functions.
282*/
283static void codeAttach(
284 Parse *pParse, /* The parser context */
285 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
286 const char *zFunc, /* Either "sqlite_attach" or "sqlite_detach */
287 int nFunc, /* Number of args to pass to zFunc */
288 Expr *pAuthArg, /* Expression to pass to authorization callback */
289 Expr *pFilename, /* Name of database file */
290 Expr *pDbname, /* Name of the database to use internally */
291 Expr *pKey /* Database key for encryption extension */
292){
293 int rc;
294 NameContext sName;
295 Vdbe *v;
296 FuncDef *pFunc;
297 sqlite3* db = pParse->db;
298
299#ifndef SQLITE_OMIT_AUTHORIZATION
300 assert( db->mallocFailed || pAuthArg );
301 if( pAuthArg ){
302 char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
303 if( !zAuthArg ){
304 goto attach_end;
305 }
306 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
307 sqlite3_free(zAuthArg);
308 if(rc!=SQLITE_OK ){
309 goto attach_end;
310 }
311 }
312#endif /* SQLITE_OMIT_AUTHORIZATION */
313
314 memset(&sName, 0, sizeof(NameContext));
315 sName.pParse = pParse;
316
317 if(
318 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
319 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
320 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
321 ){
322 pParse->nErr++;
323 goto attach_end;
324 }
325
326 v = sqlite3GetVdbe(pParse);
327 sqlite3ExprCode(pParse, pFilename);
328 sqlite3ExprCode(pParse, pDbname);
329 sqlite3ExprCode(pParse, pKey);
330
331 assert( v || db->mallocFailed );
332 if( v ){
333 sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
334 pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
335 sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
336
337 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
338 ** statement only). For DETACH, set it to false (expire all existing
339 ** statements).
340 */
341 sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0);
342 }
343
344attach_end:
345 sqlite3ExprDelete(pFilename);
346 sqlite3ExprDelete(pDbname);
347 sqlite3ExprDelete(pKey);
348}
349
350/*
351** Called by the parser to compile a DETACH statement.
352**
353** DETACH pDbname
354*/
355void sqlite3Detach(Parse *pParse, Expr *pDbname){
356 codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname);
357}
358
359/*
360** Called by the parser to compile an ATTACH statement.
361**
362** ATTACH p AS pDbname KEY pKey
363*/
364void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
365 codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey);
366}
367#endif /* SQLITE_OMIT_ATTACH */
368
369/*
370** Register the functions sqlite_attach and sqlite_detach.
371*/
372void sqlite3AttachFunctions(sqlite3 *db){
373#ifndef SQLITE_OMIT_ATTACH
374 static const int enc = SQLITE_UTF8;
375 sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0);
376 sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0);
377#endif
378}
379
380/*
381** Initialize a DbFixer structure. This routine must be called prior
382** to passing the structure to one of the sqliteFixAAAA() routines below.
383**
384** The return value indicates whether or not fixation is required. TRUE
385** means we do need to fix the database references, FALSE means we do not.
386*/
387int sqlite3FixInit(
388 DbFixer *pFix, /* The fixer to be initialized */
389 Parse *pParse, /* Error messages will be written here */
390 int iDb, /* This is the database that must be used */
391 const char *zType, /* "view", "trigger", or "index" */
392 const Token *pName /* Name of the view, trigger, or index */
393){
394 sqlite3 *db;
395
396 if( iDb<0 || iDb==1 ) return 0;
397 db = pParse->db;
398 assert( db->nDb>iDb );
399 pFix->pParse = pParse;
400 pFix->zDb = db->aDb[iDb].zName;
401 pFix->zType = zType;
402 pFix->pName = pName;
403 return 1;
404}
405
406/*
407** The following set of routines walk through the parse tree and assign
408** a specific database to all table references where the database name
409** was left unspecified in the original SQL statement. The pFix structure
410** must have been initialized by a prior call to sqlite3FixInit().
411**
412** These routines are used to make sure that an index, trigger, or
413** view in one database does not refer to objects in a different database.
414** (Exception: indices, triggers, and views in the TEMP database are
415** allowed to refer to anything.) If a reference is explicitly made
416** to an object in a different database, an error message is added to
417** pParse->zErrMsg and these routines return non-zero. If everything
418** checks out, these routines return 0.
419*/
420int sqlite3FixSrcList(
421 DbFixer *pFix, /* Context of the fixation */
422 SrcList *pList /* The Source list to check and modify */
423){
424 int i;
425 const char *zDb;
426 struct SrcList_item *pItem;
427
428 if( pList==0 ) return 0;
429 zDb = pFix->zDb;
430 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
431 if( pItem->zDatabase==0 ){
432 pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
433 }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
434 sqlite3ErrorMsg(pFix->pParse,
435 "%s %T cannot reference objects in database %s",
436 pFix->zType, pFix->pName, pItem->zDatabase);
437 return 1;
438 }
439#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
440 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
441 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
442#endif
443 }
444 return 0;
445}
446#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
447int sqlite3FixSelect(
448 DbFixer *pFix, /* Context of the fixation */
449 Select *pSelect /* The SELECT statement to be fixed to one database */
450){
451 while( pSelect ){
452 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
453 return 1;
454 }
455 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
456 return 1;
457 }
458 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
459 return 1;
460 }
461 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
462 return 1;
463 }
464 pSelect = pSelect->pPrior;
465 }
466 return 0;
467}
468int sqlite3FixExpr(
469 DbFixer *pFix, /* Context of the fixation */
470 Expr *pExpr /* The expression to be fixed to one database */
471){
472 while( pExpr ){
473 if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
474 return 1;
475 }
476 if( sqlite3FixExprList(pFix, pExpr->pList) ){
477 return 1;
478 }
479 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
480 return 1;
481 }
482 pExpr = pExpr->pLeft;
483 }
484 return 0;
485}
486int sqlite3FixExprList(
487 DbFixer *pFix, /* Context of the fixation */
488 ExprList *pList /* The expression to be fixed to one database */
489){
490 int i;
491 struct ExprList_item *pItem;
492 if( pList==0 ) return 0;
493 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
494 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
495 return 1;
496 }
497 }
498 return 0;
499}
500#endif
501
502#ifndef SQLITE_OMIT_TRIGGER
503int sqlite3FixTriggerStep(
504 DbFixer *pFix, /* Context of the fixation */
505 TriggerStep *pStep /* The trigger step be fixed to one database */
506){
507 while( pStep ){
508 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
509 return 1;
510 }
511 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
512 return 1;
513 }
514 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
515 return 1;
516 }
517 pStep = pStep->pNext;
518 }
519 return 0;
520}
521#endif
diff --git a/libraries/sqlite/win32/auth.c b/libraries/sqlite/win32/auth.c
new file mode 100755
index 0000000..5630c23
--- /dev/null
+++ b/libraries/sqlite/win32/auth.c
@@ -0,0 +1,234 @@
1/*
2** 2003 January 11
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.
10**
11*************************************************************************
12** This file contains code used to implement the sqlite3_set_authorizer()
13** API. This facility is an optional feature of the library. Embedded
14** systems that do not need this facility may omit it by recompiling
15** the library with -DSQLITE_OMIT_AUTHORIZATION=1
16**
17** $Id: auth.c,v 1.29 2007/09/18 15:55:07 drh Exp $
18*/
19#include "sqliteInt.h"
20
21/*
22** All of the code in this file may be omitted by defining a single
23** macro.
24*/
25#ifndef SQLITE_OMIT_AUTHORIZATION
26
27/*
28** Set or clear the access authorization function.
29**
30** The access authorization function is be called during the compilation
31** phase to verify that the user has read and/or write access permission on
32** various fields of the database. The first argument to the auth function
33** is a copy of the 3rd argument to this routine. The second argument
34** to the auth function is one of these constants:
35**
36** SQLITE_CREATE_INDEX
37** SQLITE_CREATE_TABLE
38** SQLITE_CREATE_TEMP_INDEX
39** SQLITE_CREATE_TEMP_TABLE
40** SQLITE_CREATE_TEMP_TRIGGER
41** SQLITE_CREATE_TEMP_VIEW
42** SQLITE_CREATE_TRIGGER
43** SQLITE_CREATE_VIEW
44** SQLITE_DELETE
45** SQLITE_DROP_INDEX
46** SQLITE_DROP_TABLE
47** SQLITE_DROP_TEMP_INDEX
48** SQLITE_DROP_TEMP_TABLE
49** SQLITE_DROP_TEMP_TRIGGER
50** SQLITE_DROP_TEMP_VIEW
51** SQLITE_DROP_TRIGGER
52** SQLITE_DROP_VIEW
53** SQLITE_INSERT
54** SQLITE_PRAGMA
55** SQLITE_READ
56** SQLITE_SELECT
57** SQLITE_TRANSACTION
58** SQLITE_UPDATE
59**
60** The third and fourth arguments to the auth function are the name of
61** the table and the column that are being accessed. The auth function
62** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
63** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
64** means that the SQL statement will never-run - the sqlite3_exec() call
65** will return with an error. SQLITE_IGNORE means that the SQL statement
66** should run but attempts to read the specified column will return NULL
67** and attempts to write the column will be ignored.
68**
69** Setting the auth function to NULL disables this hook. The default
70** setting of the auth function is NULL.
71*/
72int sqlite3_set_authorizer(
73 sqlite3 *db,
74 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
75 void *pArg
76){
77 sqlite3_mutex_enter(db->mutex);
78 db->xAuth = xAuth;
79 db->pAuthArg = pArg;
80 sqlite3ExpirePreparedStatements(db);
81 sqlite3_mutex_leave(db->mutex);
82 return SQLITE_OK;
83}
84
85/*
86** Write an error message into pParse->zErrMsg that explains that the
87** user-supplied authorization function returned an illegal value.
88*/
89static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
90 sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
91 "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
92 "or SQLITE_DENY", rc);
93 pParse->rc = SQLITE_ERROR;
94}
95
96/*
97** The pExpr should be a TK_COLUMN expression. The table referred to
98** is in pTabList or else it is the NEW or OLD table of a trigger.
99** Check to see if it is OK to read this particular column.
100**
101** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
102** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
103** then generate an error.
104*/
105void sqlite3AuthRead(
106 Parse *pParse, /* The parser context */
107 Expr *pExpr, /* The expression to check authorization on */
108 Schema *pSchema, /* The schema of the expression */
109 SrcList *pTabList /* All table that pExpr might refer to */
110){
111 sqlite3 *db = pParse->db;
112 int rc;
113 Table *pTab = 0; /* The table being read */
114 const char *zCol; /* Name of the column of the table */
115 int iSrc; /* Index in pTabList->a[] of table being read */
116 const char *zDBase; /* Name of database being accessed */
117 TriggerStack *pStack; /* The stack of current triggers */
118 int iDb; /* The index of the database the expression refers to */
119
120 if( db->xAuth==0 ) return;
121 if( pExpr->op!=TK_COLUMN ) return;
122 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
123 if( iDb<0 ){
124 /* An attempt to read a column out of a subquery or other
125 ** temporary table. */
126 return;
127 }
128 for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
129 if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
130 }
131 if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
132 pTab = pTabList->a[iSrc].pTab;
133 }else if( (pStack = pParse->trigStack)!=0 ){
134 /* This must be an attempt to read the NEW or OLD pseudo-tables
135 ** of a trigger.
136 */
137 assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
138 pTab = pStack->pTab;
139 }
140 if( pTab==0 ) return;
141 if( pExpr->iColumn>=0 ){
142 assert( pExpr->iColumn<pTab->nCol );
143 zCol = pTab->aCol[pExpr->iColumn].zName;
144 }else if( pTab->iPKey>=0 ){
145 assert( pTab->iPKey<pTab->nCol );
146 zCol = pTab->aCol[pTab->iPKey].zName;
147 }else{
148 zCol = "ROWID";
149 }
150 assert( iDb>=0 && iDb<db->nDb );
151 zDBase = db->aDb[iDb].zName;
152 rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase,
153 pParse->zAuthContext);
154 if( rc==SQLITE_IGNORE ){
155 pExpr->op = TK_NULL;
156 }else if( rc==SQLITE_DENY ){
157 if( db->nDb>2 || iDb!=0 ){
158 sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",
159 zDBase, pTab->zName, zCol);
160 }else{
161 sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
162 }
163 pParse->rc = SQLITE_AUTH;
164 }else if( rc!=SQLITE_OK ){
165 sqliteAuthBadReturnCode(pParse, rc);
166 }
167}
168
169/*
170** Do an authorization check using the code and arguments given. Return
171** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
172** is returned, then the error count and error message in pParse are
173** modified appropriately.
174*/
175int sqlite3AuthCheck(
176 Parse *pParse,
177 int code,
178 const char *zArg1,
179 const char *zArg2,
180 const char *zArg3
181){
182 sqlite3 *db = pParse->db;
183 int rc;
184
185 /* Don't do any authorization checks if the database is initialising
186 ** or if the parser is being invoked from within sqlite3_declare_vtab.
187 */
188 if( db->init.busy || IN_DECLARE_VTAB ){
189 return SQLITE_OK;
190 }
191
192 if( db->xAuth==0 ){
193 return SQLITE_OK;
194 }
195 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
196 if( rc==SQLITE_DENY ){
197 sqlite3ErrorMsg(pParse, "not authorized");
198 pParse->rc = SQLITE_AUTH;
199 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
200 rc = SQLITE_DENY;
201 sqliteAuthBadReturnCode(pParse, rc);
202 }
203 return rc;
204}
205
206/*
207** Push an authorization context. After this routine is called, the
208** zArg3 argument to authorization callbacks will be zContext until
209** popped. Or if pParse==0, this routine is a no-op.
210*/
211void sqlite3AuthContextPush(
212 Parse *pParse,
213 AuthContext *pContext,
214 const char *zContext
215){
216 pContext->pParse = pParse;
217 if( pParse ){
218 pContext->zAuthContext = pParse->zAuthContext;
219 pParse->zAuthContext = zContext;
220 }
221}
222
223/*
224** Pop an authorization context that was previously pushed
225** by sqlite3AuthContextPush
226*/
227void sqlite3AuthContextPop(AuthContext *pContext){
228 if( pContext->pParse ){
229 pContext->pParse->zAuthContext = pContext->zAuthContext;
230 pContext->pParse = 0;
231 }
232}
233
234#endif /* SQLITE_OMIT_AUTHORIZATION */
diff --git a/libraries/sqlite/win32/btmutex.c b/libraries/sqlite/win32/btmutex.c
new file mode 100755
index 0000000..1f63434
--- /dev/null
+++ b/libraries/sqlite/win32/btmutex.c
@@ -0,0 +1,315 @@
1/*
2** 2007 August 27
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.
10**
11*************************************************************************
12**
13** $Id: btmutex.c,v 1.7 2007/08/30 01:19:59 drh Exp $
14**
15** This file contains code used to implement mutexes on Btree objects.
16** This code really belongs in btree.c. But btree.c is getting too
17** big and we want to break it down some. This packaged seemed like
18** a good breakout.
19*/
20#include "btreeInt.h"
21#if SQLITE_THREADSAFE && !defined(SQLITE_OMIT_SHARED_CACHE)
22
23
24/*
25** Enter a mutex on the given BTree object.
26**
27** If the object is not sharable, then no mutex is ever required
28** and this routine is a no-op. The underlying mutex is non-recursive.
29** But we keep a reference count in Btree.wantToLock so the behavior
30** of this interface is recursive.
31**
32** To avoid deadlocks, multiple Btrees are locked in the same order
33** by all database connections. The p->pNext is a list of other
34** Btrees belonging to the same database connection as the p Btree
35** which need to be locked after p. If we cannot get a lock on
36** p, then first unlock all of the others on p->pNext, then wait
37** for the lock to become available on p, then relock all of the
38** subsequent Btrees that desire a lock.
39*/
40void sqlite3BtreeEnter(Btree *p){
41 Btree *pLater;
42
43 /* Some basic sanity checking on the Btree. The list of Btrees
44 ** connected by pNext and pPrev should be in sorted order by
45 ** Btree.pBt value. All elements of the list should belong to
46 ** the same connection. Only shared Btrees are on the list. */
47 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
48 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
49 assert( p->pNext==0 || p->pNext->pSqlite==p->pSqlite );
50 assert( p->pPrev==0 || p->pPrev->pSqlite==p->pSqlite );
51 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
52
53 /* Check for locking consistency */
54 assert( !p->locked || p->wantToLock>0 );
55 assert( p->sharable || p->wantToLock==0 );
56
57 /* We should already hold a lock on the database connection */
58 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
59
60 if( !p->sharable ) return;
61 p->wantToLock++;
62 if( p->locked ) return;
63
64 /* In most cases, we should be able to acquire the lock we
65 ** want without having to go throught the ascending lock
66 ** procedure that follows. Just be sure not to block.
67 */
68 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
69 p->locked = 1;
70 return;
71 }
72
73 /* To avoid deadlock, first release all locks with a larger
74 ** BtShared address. Then acquire our lock. Then reacquire
75 ** the other BtShared locks that we used to hold in ascending
76 ** order.
77 */
78 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
79 assert( pLater->sharable );
80 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
81 assert( !pLater->locked || pLater->wantToLock>0 );
82 if( pLater->locked ){
83 sqlite3_mutex_leave(pLater->pBt->mutex);
84 pLater->locked = 0;
85 }
86 }
87 sqlite3_mutex_enter(p->pBt->mutex);
88 p->locked = 1;
89 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
90 if( pLater->wantToLock ){
91 sqlite3_mutex_enter(pLater->pBt->mutex);
92 pLater->locked = 1;
93 }
94 }
95}
96
97/*
98** Exit the recursive mutex on a Btree.
99*/
100void sqlite3BtreeLeave(Btree *p){
101 if( p->sharable ){
102 assert( p->wantToLock>0 );
103 p->wantToLock--;
104 if( p->wantToLock==0 ){
105 assert( p->locked );
106 sqlite3_mutex_leave(p->pBt->mutex);
107 p->locked = 0;
108 }
109 }
110}
111
112#ifndef NDEBUG
113/*
114** Return true if the BtShared mutex is held on the btree.
115**
116** This routine makes no determination one why or another if the
117** database connection mutex is held.
118**
119** This routine is used only from within assert() statements.
120*/
121int sqlite3BtreeHoldsMutex(Btree *p){
122 return (p->sharable==0 ||
123 (p->locked && p->wantToLock && sqlite3_mutex_held(p->pBt->mutex)));
124}
125#endif
126
127
128#ifndef SQLITE_OMIT_INCRBLOB
129/*
130** Enter and leave a mutex on a Btree given a cursor owned by that
131** Btree. These entry points are used by incremental I/O and can be
132** omitted if that module is not used.
133*/
134void sqlite3BtreeEnterCursor(BtCursor *pCur){
135 sqlite3BtreeEnter(pCur->pBtree);
136}
137void sqlite3BtreeLeaveCursor(BtCursor *pCur){
138 sqlite3BtreeLeave(pCur->pBtree);
139}
140#endif /* SQLITE_OMIT_INCRBLOB */
141
142
143/*
144** Enter the mutex on every Btree associated with a database
145** connection. This is needed (for example) prior to parsing
146** a statement since we will be comparing table and column names
147** against all schemas and we do not want those schemas being
148** reset out from under us.
149**
150** There is a corresponding leave-all procedures.
151**
152** Enter the mutexes in accending order by BtShared pointer address
153** to avoid the possibility of deadlock when two threads with
154** two or more btrees in common both try to lock all their btrees
155** at the same instant.
156*/
157void sqlite3BtreeEnterAll(sqlite3 *db){
158 int i;
159 Btree *p, *pLater;
160 assert( sqlite3_mutex_held(db->mutex) );
161 for(i=0; i<db->nDb; i++){
162 p = db->aDb[i].pBt;
163 if( p && p->sharable ){
164 p->wantToLock++;
165 if( !p->locked ){
166 assert( p->wantToLock==1 );
167 while( p->pPrev ) p = p->pPrev;
168 while( p->locked && p->pNext ) p = p->pNext;
169 for(pLater = p->pNext; pLater; pLater=pLater->pNext){
170 if( pLater->locked ){
171 sqlite3_mutex_leave(pLater->pBt->mutex);
172 pLater->locked = 0;
173 }
174 }
175 while( p ){
176 sqlite3_mutex_enter(p->pBt->mutex);
177 p->locked++;
178 p = p->pNext;
179 }
180 }
181 }
182 }
183}
184void sqlite3BtreeLeaveAll(sqlite3 *db){
185 int i;
186 Btree *p;
187 assert( sqlite3_mutex_held(db->mutex) );
188 for(i=0; i<db->nDb; i++){
189 p = db->aDb[i].pBt;
190 if( p && p->sharable ){
191 assert( p->wantToLock>0 );
192 p->wantToLock--;
193 if( p->wantToLock==0 ){
194 assert( p->locked );
195 sqlite3_mutex_leave(p->pBt->mutex);
196 p->locked = 0;
197 }
198 }
199 }
200}
201
202#ifndef NDEBUG
203/*
204** Return true if the current thread holds the database connection
205** mutex and all required BtShared mutexes.
206**
207** This routine is used inside assert() statements only.
208*/
209int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
210 int i;
211 if( !sqlite3_mutex_held(db->mutex) ){
212 return 0;
213 }
214 for(i=0; i<db->nDb; i++){
215 Btree *p;
216 p = db->aDb[i].pBt;
217 if( p && p->sharable &&
218 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
219 return 0;
220 }
221 }
222 return 1;
223}
224#endif /* NDEBUG */
225
226/*
227** Potentially dd a new Btree pointer to a BtreeMutexArray.
228** Really only add the Btree if it can possibly be shared with
229** another database connection.
230**
231** The Btrees are kept in sorted order by pBtree->pBt. That
232** way when we go to enter all the mutexes, we can enter them
233** in order without every having to backup and retry and without
234** worrying about deadlock.
235**
236** The number of shared btrees will always be small (usually 0 or 1)
237** so an insertion sort is an adequate algorithm here.
238*/
239void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
240 int i, j;
241 BtShared *pBt;
242 if( pBtree==0 || pBtree->sharable==0 ) return;
243#ifndef NDEBUG
244 {
245 for(i=0; i<pArray->nMutex; i++){
246 assert( pArray->aBtree[i]!=pBtree );
247 }
248 }
249#endif
250 assert( pArray->nMutex>=0 );
251 assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
252 pBt = pBtree->pBt;
253 for(i=0; i<pArray->nMutex; i++){
254 assert( pArray->aBtree[i]!=pBtree );
255 if( pArray->aBtree[i]->pBt>pBt ){
256 for(j=pArray->nMutex; j>i; j--){
257 pArray->aBtree[j] = pArray->aBtree[j-1];
258 }
259 pArray->aBtree[i] = pBtree;
260 pArray->nMutex++;
261 return;
262 }
263 }
264 pArray->aBtree[pArray->nMutex++] = pBtree;
265}
266
267/*
268** Enter the mutex of every btree in the array. This routine is
269** called at the beginning of sqlite3VdbeExec(). The mutexes are
270** exited at the end of the same function.
271*/
272void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
273 int i;
274 for(i=0; i<pArray->nMutex; i++){
275 Btree *p = pArray->aBtree[i];
276 /* Some basic sanity checking */
277 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
278 assert( !p->locked || p->wantToLock>0 );
279
280 /* We should already hold a lock on the database connection */
281 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
282
283 p->wantToLock++;
284 if( !p->locked && p->sharable ){
285 sqlite3_mutex_enter(p->pBt->mutex);
286 p->locked = 1;
287 }
288 }
289}
290
291/*
292** Leave the mutex of every btree in the group.
293*/
294void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
295 int i;
296 for(i=0; i<pArray->nMutex; i++){
297 Btree *p = pArray->aBtree[i];
298 /* Some basic sanity checking */
299 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
300 assert( p->locked || !p->sharable );
301 assert( p->wantToLock>0 );
302
303 /* We should already hold a lock on the database connection */
304 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
305
306 p->wantToLock--;
307 if( p->wantToLock==0 && p->locked ){
308 sqlite3_mutex_leave(p->pBt->mutex);
309 p->locked = 0;
310 }
311 }
312}
313
314
315#endif /* SQLITE_THREADSAFE && !SQLITE_OMIT_SHARED_CACHE */
diff --git a/libraries/sqlite/win32/btree.c b/libraries/sqlite/win32/btree.c
new file mode 100755
index 0000000..de8821a
--- /dev/null
+++ b/libraries/sqlite/win32/btree.c
@@ -0,0 +1,6890 @@
1/*
2** 2004 April 6
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.
10**
11*************************************************************************
12** $Id: btree.c,v 1.428 2007/10/03 08:46:44 danielk1977 Exp $
13**
14** This file implements a external (disk-based) database using BTrees.
15** See the header comment on "btreeInt.h" for additional information.
16** Including a description of file format and an overview of operation.
17*/
18#include "btreeInt.h"
19
20/*
21** The header string that appears at the beginning of every
22** SQLite database.
23*/
24static const char zMagicHeader[] = SQLITE_FILE_HEADER;
25
26/*
27** Set this global variable to 1 to enable tracing using the TRACE
28** macro.
29*/
30#if SQLITE_TEST
31int sqlite3_btree_trace=0; /* True to enable tracing */
32#endif
33
34
35
36#ifndef SQLITE_OMIT_SHARED_CACHE
37/*
38** A flag to indicate whether or not shared cache is enabled. Also,
39** a list of BtShared objects that are eligible for participation
40** in shared cache. The variables have file scope during normal builds,
41** but the test harness needs to access these variables so we make them
42** global for test builds.
43*/
44#ifdef SQLITE_TEST
45BtShared *sqlite3SharedCacheList = 0;
46int sqlite3SharedCacheEnabled = 0;
47#else
48static BtShared *sqlite3SharedCacheList = 0;
49static int sqlite3SharedCacheEnabled = 0;
50#endif
51#endif /* SQLITE_OMIT_SHARED_CACHE */
52
53#ifndef SQLITE_OMIT_SHARED_CACHE
54/*
55** Enable or disable the shared pager and schema features.
56**
57** This routine has no effect on existing database connections.
58** The shared cache setting effects only future calls to
59** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
60*/
61int sqlite3_enable_shared_cache(int enable){
62 sqlite3SharedCacheEnabled = enable;
63 return SQLITE_OK;
64}
65#endif
66
67
68/*
69** Forward declaration
70*/
71static int checkReadLocks(Btree*,Pgno,BtCursor*);
72
73
74#ifdef SQLITE_OMIT_SHARED_CACHE
75 /*
76 ** The functions queryTableLock(), lockTable() and unlockAllTables()
77 ** manipulate entries in the BtShared.pLock linked list used to store
78 ** shared-cache table level locks. If the library is compiled with the
79 ** shared-cache feature disabled, then there is only ever one user
80 ** of each BtShared structure and so this locking is not necessary.
81 ** So define the lock related functions as no-ops.
82 */
83 #define queryTableLock(a,b,c) SQLITE_OK
84 #define lockTable(a,b,c) SQLITE_OK
85 #define unlockAllTables(a)
86#endif
87
88#ifndef SQLITE_OMIT_SHARED_CACHE
89/*
90** Query to see if btree handle p may obtain a lock of type eLock
91** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
92** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
93** SQLITE_LOCKED if not.
94*/
95static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
96 BtShared *pBt = p->pBt;
97 BtLock *pIter;
98
99 assert( sqlite3BtreeHoldsMutex(p) );
100
101 /* This is a no-op if the shared-cache is not enabled */
102 if( !p->sharable ){
103 return SQLITE_OK;
104 }
105
106 /* This (along with lockTable()) is where the ReadUncommitted flag is
107 ** dealt with. If the caller is querying for a read-lock and the flag is
108 ** set, it is unconditionally granted - even if there are write-locks
109 ** on the table. If a write-lock is requested, the ReadUncommitted flag
110 ** is not considered.
111 **
112 ** In function lockTable(), if a read-lock is demanded and the
113 ** ReadUncommitted flag is set, no entry is added to the locks list
114 ** (BtShared.pLock).
115 **
116 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
117 ** not create or respect table locks. The locking procedure for a
118 ** write-cursor does not change.
119 */
120 if(
121 !p->pSqlite ||
122 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) ||
123 eLock==WRITE_LOCK ||
124 iTab==MASTER_ROOT
125 ){
126 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
127 if( pIter->pBtree!=p && pIter->iTable==iTab &&
128 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
129 return SQLITE_LOCKED;
130 }
131 }
132 }
133 return SQLITE_OK;
134}
135#endif /* !SQLITE_OMIT_SHARED_CACHE */
136
137#ifndef SQLITE_OMIT_SHARED_CACHE
138/*
139** Add a lock on the table with root-page iTable to the shared-btree used
140** by Btree handle p. Parameter eLock must be either READ_LOCK or
141** WRITE_LOCK.
142**
143** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
144** SQLITE_NOMEM may also be returned.
145*/
146static int lockTable(Btree *p, Pgno iTable, u8 eLock){
147 BtShared *pBt = p->pBt;
148 BtLock *pLock = 0;
149 BtLock *pIter;
150
151 assert( sqlite3BtreeHoldsMutex(p) );
152
153 /* This is a no-op if the shared-cache is not enabled */
154 if( !p->sharable ){
155 return SQLITE_OK;
156 }
157
158 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
159
160 /* If the read-uncommitted flag is set and a read-lock is requested,
161 ** return early without adding an entry to the BtShared.pLock list. See
162 ** comment in function queryTableLock() for more info on handling
163 ** the ReadUncommitted flag.
164 */
165 if(
166 (p->pSqlite) &&
167 (p->pSqlite->flags&SQLITE_ReadUncommitted) &&
168 (eLock==READ_LOCK) &&
169 iTable!=MASTER_ROOT
170 ){
171 return SQLITE_OK;
172 }
173
174 /* First search the list for an existing lock on this table. */
175 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
176 if( pIter->iTable==iTable && pIter->pBtree==p ){
177 pLock = pIter;
178 break;
179 }
180 }
181
182 /* If the above search did not find a BtLock struct associating Btree p
183 ** with table iTable, allocate one and link it into the list.
184 */
185 if( !pLock ){
186 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
187 if( !pLock ){
188 return SQLITE_NOMEM;
189 }
190 pLock->iTable = iTable;
191 pLock->pBtree = p;
192 pLock->pNext = pBt->pLock;
193 pBt->pLock = pLock;
194 }
195
196 /* Set the BtLock.eLock variable to the maximum of the current lock
197 ** and the requested lock. This means if a write-lock was already held
198 ** and a read-lock requested, we don't incorrectly downgrade the lock.
199 */
200 assert( WRITE_LOCK>READ_LOCK );
201 if( eLock>pLock->eLock ){
202 pLock->eLock = eLock;
203 }
204
205 return SQLITE_OK;
206}
207#endif /* !SQLITE_OMIT_SHARED_CACHE */
208
209#ifndef SQLITE_OMIT_SHARED_CACHE
210/*
211** Release all the table locks (locks obtained via calls to the lockTable()
212** procedure) held by Btree handle p.
213*/
214static void unlockAllTables(Btree *p){
215 BtLock **ppIter = &p->pBt->pLock;
216
217 assert( sqlite3BtreeHoldsMutex(p) );
218 assert( p->sharable || 0==*ppIter );
219
220 while( *ppIter ){
221 BtLock *pLock = *ppIter;
222 if( pLock->pBtree==p ){
223 *ppIter = pLock->pNext;
224 sqlite3_free(pLock);
225 }else{
226 ppIter = &pLock->pNext;
227 }
228 }
229}
230#endif /* SQLITE_OMIT_SHARED_CACHE */
231
232static void releasePage(MemPage *pPage); /* Forward reference */
233
234/*
235** Verify that the cursor holds a mutex on the BtShared
236*/
237#ifndef NDEBUG
238static int cursorHoldsMutex(BtCursor *p){
239 return sqlite3_mutex_held(p->pBt->mutex);
240}
241#endif
242
243
244#ifndef SQLITE_OMIT_INCRBLOB
245/*
246** Invalidate the overflow page-list cache for cursor pCur, if any.
247*/
248static void invalidateOverflowCache(BtCursor *pCur){
249 assert( cursorHoldsMutex(pCur) );
250 sqlite3_free(pCur->aOverflow);
251 pCur->aOverflow = 0;
252}
253
254/*
255** Invalidate the overflow page-list cache for all cursors opened
256** on the shared btree structure pBt.
257*/
258static void invalidateAllOverflowCache(BtShared *pBt){
259 BtCursor *p;
260 assert( sqlite3_mutex_held(pBt->mutex) );
261 for(p=pBt->pCursor; p; p=p->pNext){
262 invalidateOverflowCache(p);
263 }
264}
265#else
266 #define invalidateOverflowCache(x)
267 #define invalidateAllOverflowCache(x)
268#endif
269
270/*
271** Save the current cursor position in the variables BtCursor.nKey
272** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
273*/
274static int saveCursorPosition(BtCursor *pCur){
275 int rc;
276
277 assert( CURSOR_VALID==pCur->eState );
278 assert( 0==pCur->pKey );
279 assert( cursorHoldsMutex(pCur) );
280
281 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
282
283 /* If this is an intKey table, then the above call to BtreeKeySize()
284 ** stores the integer key in pCur->nKey. In this case this value is
285 ** all that is required. Otherwise, if pCur is not open on an intKey
286 ** table, then malloc space for and store the pCur->nKey bytes of key
287 ** data.
288 */
289 if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
290 void *pKey = sqlite3_malloc(pCur->nKey);
291 if( pKey ){
292 rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
293 if( rc==SQLITE_OK ){
294 pCur->pKey = pKey;
295 }else{
296 sqlite3_free(pKey);
297 }
298 }else{
299 rc = SQLITE_NOMEM;
300 }
301 }
302 assert( !pCur->pPage->intKey || !pCur->pKey );
303
304 if( rc==SQLITE_OK ){
305 releasePage(pCur->pPage);
306 pCur->pPage = 0;
307 pCur->eState = CURSOR_REQUIRESEEK;
308 }
309
310 invalidateOverflowCache(pCur);
311 return rc;
312}
313
314/*
315** Save the positions of all cursors except pExcept open on the table
316** with root-page iRoot. Usually, this is called just before cursor
317** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
318*/
319static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
320 BtCursor *p;
321 assert( sqlite3_mutex_held(pBt->mutex) );
322 assert( pExcept==0 || pExcept->pBt==pBt );
323 for(p=pBt->pCursor; p; p=p->pNext){
324 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
325 p->eState==CURSOR_VALID ){
326 int rc = saveCursorPosition(p);
327 if( SQLITE_OK!=rc ){
328 return rc;
329 }
330 }
331 }
332 return SQLITE_OK;
333}
334
335/*
336** Clear the current cursor position.
337*/
338static void clearCursorPosition(BtCursor *pCur){
339 assert( cursorHoldsMutex(pCur) );
340 sqlite3_free(pCur->pKey);
341 pCur->pKey = 0;
342 pCur->eState = CURSOR_INVALID;
343}
344
345/*
346** Restore the cursor to the position it was in (or as close to as possible)
347** when saveCursorPosition() was called. Note that this call deletes the
348** saved position info stored by saveCursorPosition(), so there can be
349** at most one effective restoreOrClearCursorPosition() call after each
350** saveCursorPosition().
351**
352** If the second argument argument - doSeek - is false, then instead of
353** returning the cursor to it's saved position, any saved position is deleted
354** and the cursor state set to CURSOR_INVALID.
355*/
356int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){
357 int rc;
358 assert( cursorHoldsMutex(pCur) );
359 assert( pCur->eState>=CURSOR_REQUIRESEEK );
360 if( pCur->eState==CURSOR_FAULT ){
361 return pCur->skip;
362 }
363#ifndef SQLITE_OMIT_INCRBLOB
364 if( pCur->isIncrblobHandle ){
365 return SQLITE_ABORT;
366 }
367#endif
368 pCur->eState = CURSOR_INVALID;
369 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
370 if( rc==SQLITE_OK ){
371 sqlite3_free(pCur->pKey);
372 pCur->pKey = 0;
373 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
374 }
375 return rc;
376}
377
378#define restoreOrClearCursorPosition(p) \
379 (p->eState>=CURSOR_REQUIRESEEK ? \
380 sqlite3BtreeRestoreOrClearCursorPosition(p) : \
381 SQLITE_OK)
382
383#ifndef SQLITE_OMIT_AUTOVACUUM
384/*
385** Given a page number of a regular database page, return the page
386** number for the pointer-map page that contains the entry for the
387** input page number.
388*/
389static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
390 int nPagesPerMapPage, iPtrMap, ret;
391 assert( sqlite3_mutex_held(pBt->mutex) );
392 nPagesPerMapPage = (pBt->usableSize/5)+1;
393 iPtrMap = (pgno-2)/nPagesPerMapPage;
394 ret = (iPtrMap*nPagesPerMapPage) + 2;
395 if( ret==PENDING_BYTE_PAGE(pBt) ){
396 ret++;
397 }
398 return ret;
399}
400
401/*
402** Write an entry into the pointer map.
403**
404** This routine updates the pointer map entry for page number 'key'
405** so that it maps to type 'eType' and parent page number 'pgno'.
406** An error code is returned if something goes wrong, otherwise SQLITE_OK.
407*/
408static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
409 DbPage *pDbPage; /* The pointer map page */
410 u8 *pPtrmap; /* The pointer map data */
411 Pgno iPtrmap; /* The pointer map page number */
412 int offset; /* Offset in pointer map page */
413 int rc;
414
415 assert( sqlite3_mutex_held(pBt->mutex) );
416 /* The master-journal page number must never be used as a pointer map page */
417 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
418
419 assert( pBt->autoVacuum );
420 if( key==0 ){
421 return SQLITE_CORRUPT_BKPT;
422 }
423 iPtrmap = PTRMAP_PAGENO(pBt, key);
424 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
425 if( rc!=SQLITE_OK ){
426 return rc;
427 }
428 offset = PTRMAP_PTROFFSET(pBt, key);
429 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
430
431 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
432 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
433 rc = sqlite3PagerWrite(pDbPage);
434 if( rc==SQLITE_OK ){
435 pPtrmap[offset] = eType;
436 put4byte(&pPtrmap[offset+1], parent);
437 }
438 }
439
440 sqlite3PagerUnref(pDbPage);
441 return rc;
442}
443
444/*
445** Read an entry from the pointer map.
446**
447** This routine retrieves the pointer map entry for page 'key', writing
448** the type and parent page number to *pEType and *pPgno respectively.
449** An error code is returned if something goes wrong, otherwise SQLITE_OK.
450*/
451static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
452 DbPage *pDbPage; /* The pointer map page */
453 int iPtrmap; /* Pointer map page index */
454 u8 *pPtrmap; /* Pointer map page data */
455 int offset; /* Offset of entry in pointer map */
456 int rc;
457
458 assert( sqlite3_mutex_held(pBt->mutex) );
459
460 iPtrmap = PTRMAP_PAGENO(pBt, key);
461 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
462 if( rc!=0 ){
463 return rc;
464 }
465 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
466
467 offset = PTRMAP_PTROFFSET(pBt, key);
468 assert( pEType!=0 );
469 *pEType = pPtrmap[offset];
470 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
471
472 sqlite3PagerUnref(pDbPage);
473 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
474 return SQLITE_OK;
475}
476
477#endif /* SQLITE_OMIT_AUTOVACUUM */
478
479/*
480** Given a btree page and a cell index (0 means the first cell on
481** the page, 1 means the second cell, and so forth) return a pointer
482** to the cell content.
483**
484** This routine works only for pages that do not contain overflow cells.
485*/
486#define findCell(pPage, iCell) \
487 ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)]))
488#ifdef SQLITE_TEST
489u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){
490 assert( iCell>=0 );
491 assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) );
492 return findCell(pPage, iCell);
493}
494#endif
495
496/*
497** This a more complex version of sqlite3BtreeFindCell() that works for
498** pages that do contain overflow cells. See insert
499*/
500static u8 *findOverflowCell(MemPage *pPage, int iCell){
501 int i;
502 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
503 for(i=pPage->nOverflow-1; i>=0; i--){
504 int k;
505 struct _OvflCell *pOvfl;
506 pOvfl = &pPage->aOvfl[i];
507 k = pOvfl->idx;
508 if( k<=iCell ){
509 if( k==iCell ){
510 return pOvfl->pCell;
511 }
512 iCell--;
513 }
514 }
515 return findCell(pPage, iCell);
516}
517
518/*
519** Parse a cell content block and fill in the CellInfo structure. There
520** are two versions of this function. sqlite3BtreeParseCell() takes a
521** cell index as the second argument and sqlite3BtreeParseCellPtr()
522** takes a pointer to the body of the cell as its second argument.
523**
524** Within this file, the parseCell() macro can be called instead of
525** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
526*/
527void sqlite3BtreeParseCellPtr(
528 MemPage *pPage, /* Page containing the cell */
529 u8 *pCell, /* Pointer to the cell text. */
530 CellInfo *pInfo /* Fill in this structure */
531){
532 int n; /* Number bytes in cell content header */
533 u32 nPayload; /* Number of bytes of cell payload */
534
535 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
536
537 pInfo->pCell = pCell;
538 assert( pPage->leaf==0 || pPage->leaf==1 );
539 n = pPage->childPtrSize;
540 assert( n==4-4*pPage->leaf );
541 if( pPage->hasData ){
542 n += getVarint32(&pCell[n], &nPayload);
543 }else{
544 nPayload = 0;
545 }
546 pInfo->nData = nPayload;
547 if( pPage->intKey ){
548 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
549 }else{
550 u32 x;
551 n += getVarint32(&pCell[n], &x);
552 pInfo->nKey = x;
553 nPayload += x;
554 }
555 pInfo->nPayload = nPayload;
556 pInfo->nHeader = n;
557 if( nPayload<=pPage->maxLocal ){
558 /* This is the (easy) common case where the entire payload fits
559 ** on the local page. No overflow is required.
560 */
561 int nSize; /* Total size of cell content in bytes */
562 pInfo->nLocal = nPayload;
563 pInfo->iOverflow = 0;
564 nSize = nPayload + n;
565 if( nSize<4 ){
566 nSize = 4; /* Minimum cell size is 4 */
567 }
568 pInfo->nSize = nSize;
569 }else{
570 /* If the payload will not fit completely on the local page, we have
571 ** to decide how much to store locally and how much to spill onto
572 ** overflow pages. The strategy is to minimize the amount of unused
573 ** space on overflow pages while keeping the amount of local storage
574 ** in between minLocal and maxLocal.
575 **
576 ** Warning: changing the way overflow payload is distributed in any
577 ** way will result in an incompatible file format.
578 */
579 int minLocal; /* Minimum amount of payload held locally */
580 int maxLocal; /* Maximum amount of payload held locally */
581 int surplus; /* Overflow payload available for local storage */
582
583 minLocal = pPage->minLocal;
584 maxLocal = pPage->maxLocal;
585 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
586 if( surplus <= maxLocal ){
587 pInfo->nLocal = surplus;
588 }else{
589 pInfo->nLocal = minLocal;
590 }
591 pInfo->iOverflow = pInfo->nLocal + n;
592 pInfo->nSize = pInfo->iOverflow + 4;
593 }
594}
595#define parseCell(pPage, iCell, pInfo) \
596 sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
597void sqlite3BtreeParseCell(
598 MemPage *pPage, /* Page containing the cell */
599 int iCell, /* The cell index. First cell is 0 */
600 CellInfo *pInfo /* Fill in this structure */
601){
602 parseCell(pPage, iCell, pInfo);
603}
604
605/*
606** Compute the total number of bytes that a Cell needs in the cell
607** data area of the btree-page. The return number includes the cell
608** data header and the local payload, but not any overflow page or
609** the space used by the cell pointer.
610*/
611#ifndef NDEBUG
612static int cellSize(MemPage *pPage, int iCell){
613 CellInfo info;
614 sqlite3BtreeParseCell(pPage, iCell, &info);
615 return info.nSize;
616}
617#endif
618static int cellSizePtr(MemPage *pPage, u8 *pCell){
619 CellInfo info;
620 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
621 return info.nSize;
622}
623
624#ifndef SQLITE_OMIT_AUTOVACUUM
625/*
626** If the cell pCell, part of page pPage contains a pointer
627** to an overflow page, insert an entry into the pointer-map
628** for the overflow page.
629*/
630static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
631 if( pCell ){
632 CellInfo info;
633 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
634 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
635 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
636 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
637 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
638 }
639 }
640 return SQLITE_OK;
641}
642/*
643** If the cell with index iCell on page pPage contains a pointer
644** to an overflow page, insert an entry into the pointer-map
645** for the overflow page.
646*/
647static int ptrmapPutOvfl(MemPage *pPage, int iCell){
648 u8 *pCell;
649 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
650 pCell = findOverflowCell(pPage, iCell);
651 return ptrmapPutOvflPtr(pPage, pCell);
652}
653#endif
654
655
656/*
657** Defragment the page given. All Cells are moved to the
658** end of the page and all free space is collected into one
659** big FreeBlk that occurs in between the header and cell
660** pointer array and the cell content area.
661*/
662static int defragmentPage(MemPage *pPage){
663 int i; /* Loop counter */
664 int pc; /* Address of a i-th cell */
665 int addr; /* Offset of first byte after cell pointer array */
666 int hdr; /* Offset to the page header */
667 int size; /* Size of a cell */
668 int usableSize; /* Number of usable bytes on a page */
669 int cellOffset; /* Offset to the cell pointer array */
670 int brk; /* Offset to the cell content area */
671 int nCell; /* Number of cells on the page */
672 unsigned char *data; /* The page data */
673 unsigned char *temp; /* Temp area for cell content */
674
675 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
676 assert( pPage->pBt!=0 );
677 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
678 assert( pPage->nOverflow==0 );
679 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
680 temp = sqlite3_malloc( pPage->pBt->pageSize );
681 if( temp==0 ) return SQLITE_NOMEM;
682 data = pPage->aData;
683 hdr = pPage->hdrOffset;
684 cellOffset = pPage->cellOffset;
685 nCell = pPage->nCell;
686 assert( nCell==get2byte(&data[hdr+3]) );
687 usableSize = pPage->pBt->usableSize;
688 brk = get2byte(&data[hdr+5]);
689 memcpy(&temp[brk], &data[brk], usableSize - brk);
690 brk = usableSize;
691 for(i=0; i<nCell; i++){
692 u8 *pAddr; /* The i-th cell pointer */
693 pAddr = &data[cellOffset + i*2];
694 pc = get2byte(pAddr);
695 assert( pc<pPage->pBt->usableSize );
696 size = cellSizePtr(pPage, &temp[pc]);
697 brk -= size;
698 memcpy(&data[brk], &temp[pc], size);
699 put2byte(pAddr, brk);
700 }
701 assert( brk>=cellOffset+2*nCell );
702 put2byte(&data[hdr+5], brk);
703 data[hdr+1] = 0;
704 data[hdr+2] = 0;
705 data[hdr+7] = 0;
706 addr = cellOffset+2*nCell;
707 memset(&data[addr], 0, brk-addr);
708 sqlite3_free(temp);
709 return SQLITE_OK;
710}
711
712/*
713** Allocate nByte bytes of space on a page.
714**
715** Return the index into pPage->aData[] of the first byte of
716** the new allocation. Or return 0 if there is not enough free
717** space on the page to satisfy the allocation request.
718**
719** If the page contains nBytes of free space but does not contain
720** nBytes of contiguous free space, then this routine automatically
721** calls defragementPage() to consolidate all free space before
722** allocating the new chunk.
723*/
724static int allocateSpace(MemPage *pPage, int nByte){
725 int addr, pc, hdr;
726 int size;
727 int nFrag;
728 int top;
729 int nCell;
730 int cellOffset;
731 unsigned char *data;
732
733 data = pPage->aData;
734 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
735 assert( pPage->pBt );
736 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
737 if( nByte<4 ) nByte = 4;
738 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
739 pPage->nFree -= nByte;
740 hdr = pPage->hdrOffset;
741
742 nFrag = data[hdr+7];
743 if( nFrag<60 ){
744 /* Search the freelist looking for a slot big enough to satisfy the
745 ** space request. */
746 addr = hdr+1;
747 while( (pc = get2byte(&data[addr]))>0 ){
748 size = get2byte(&data[pc+2]);
749 if( size>=nByte ){
750 if( size<nByte+4 ){
751 memcpy(&data[addr], &data[pc], 2);
752 data[hdr+7] = nFrag + size - nByte;
753 return pc;
754 }else{
755 put2byte(&data[pc+2], size-nByte);
756 return pc + size - nByte;
757 }
758 }
759 addr = pc;
760 }
761 }
762
763 /* Allocate memory from the gap in between the cell pointer array
764 ** and the cell content area.
765 */
766 top = get2byte(&data[hdr+5]);
767 nCell = get2byte(&data[hdr+3]);
768 cellOffset = pPage->cellOffset;
769 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
770 if( defragmentPage(pPage) ) return 0;
771 top = get2byte(&data[hdr+5]);
772 }
773 top -= nByte;
774 assert( cellOffset + 2*nCell <= top );
775 put2byte(&data[hdr+5], top);
776 return top;
777}
778
779/*
780** Return a section of the pPage->aData to the freelist.
781** The first byte of the new free block is pPage->aDisk[start]
782** and the size of the block is "size" bytes.
783**
784** Most of the effort here is involved in coalesing adjacent
785** free blocks into a single big free block.
786*/
787static void freeSpace(MemPage *pPage, int start, int size){
788 int addr, pbegin, hdr;
789 unsigned char *data = pPage->aData;
790
791 assert( pPage->pBt!=0 );
792 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
793 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
794 assert( (start + size)<=pPage->pBt->usableSize );
795 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
796 if( size<4 ) size = 4;
797
798#ifdef SQLITE_SECURE_DELETE
799 /* Overwrite deleted information with zeros when the SECURE_DELETE
800 ** option is enabled at compile-time */
801 memset(&data[start], 0, size);
802#endif
803
804 /* Add the space back into the linked list of freeblocks */
805 hdr = pPage->hdrOffset;
806 addr = hdr + 1;
807 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
808 assert( pbegin<=pPage->pBt->usableSize-4 );
809 assert( pbegin>addr );
810 addr = pbegin;
811 }
812 assert( pbegin<=pPage->pBt->usableSize-4 );
813 assert( pbegin>addr || pbegin==0 );
814 put2byte(&data[addr], start);
815 put2byte(&data[start], pbegin);
816 put2byte(&data[start+2], size);
817 pPage->nFree += size;
818
819 /* Coalesce adjacent free blocks */
820 addr = pPage->hdrOffset + 1;
821 while( (pbegin = get2byte(&data[addr]))>0 ){
822 int pnext, psize;
823 assert( pbegin>addr );
824 assert( pbegin<=pPage->pBt->usableSize-4 );
825 pnext = get2byte(&data[pbegin]);
826 psize = get2byte(&data[pbegin+2]);
827 if( pbegin + psize + 3 >= pnext && pnext>0 ){
828 int frag = pnext - (pbegin+psize);
829 assert( frag<=data[pPage->hdrOffset+7] );
830 data[pPage->hdrOffset+7] -= frag;
831 put2byte(&data[pbegin], get2byte(&data[pnext]));
832 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
833 }else{
834 addr = pbegin;
835 }
836 }
837
838 /* If the cell content area begins with a freeblock, remove it. */
839 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
840 int top;
841 pbegin = get2byte(&data[hdr+1]);
842 memcpy(&data[hdr+1], &data[pbegin], 2);
843 top = get2byte(&data[hdr+5]);
844 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
845 }
846}
847
848/*
849** Decode the flags byte (the first byte of the header) for a page
850** and initialize fields of the MemPage structure accordingly.
851*/
852static void decodeFlags(MemPage *pPage, int flagByte){
853 BtShared *pBt; /* A copy of pPage->pBt */
854
855 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
856 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
857 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
858 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
859 pPage->leaf = (flagByte & PTF_LEAF)!=0;
860 pPage->childPtrSize = 4*(pPage->leaf==0);
861 pBt = pPage->pBt;
862 if( flagByte & PTF_LEAFDATA ){
863 pPage->leafData = 1;
864 pPage->maxLocal = pBt->maxLeaf;
865 pPage->minLocal = pBt->minLeaf;
866 }else{
867 pPage->leafData = 0;
868 pPage->maxLocal = pBt->maxLocal;
869 pPage->minLocal = pBt->minLocal;
870 }
871 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
872}
873
874/*
875** Initialize the auxiliary information for a disk block.
876**
877** The pParent parameter must be a pointer to the MemPage which
878** is the parent of the page being initialized. The root of a
879** BTree has no parent and so for that page, pParent==NULL.
880**
881** Return SQLITE_OK on success. If we see that the page does
882** not contain a well-formed database page, then return
883** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
884** guarantee that the page is well-formed. It only shows that
885** we failed to detect any corruption.
886*/
887int sqlite3BtreeInitPage(
888 MemPage *pPage, /* The page to be initialized */
889 MemPage *pParent /* The parent. Might be NULL */
890){
891 int pc; /* Address of a freeblock within pPage->aData[] */
892 int hdr; /* Offset to beginning of page header */
893 u8 *data; /* Equal to pPage->aData */
894 BtShared *pBt; /* The main btree structure */
895 int usableSize; /* Amount of usable space on each page */
896 int cellOffset; /* Offset from start of page to first cell pointer */
897 int nFree; /* Number of unused bytes on the page */
898 int top; /* First byte of the cell content area */
899
900 pBt = pPage->pBt;
901 assert( pBt!=0 );
902 assert( pParent==0 || pParent->pBt==pBt );
903 assert( sqlite3_mutex_held(pBt->mutex) );
904 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
905 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
906 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
907 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
908 /* The parent page should never change unless the file is corrupt */
909 return SQLITE_CORRUPT_BKPT;
910 }
911 if( pPage->isInit ) return SQLITE_OK;
912 if( pPage->pParent==0 && pParent!=0 ){
913 pPage->pParent = pParent;
914 sqlite3PagerRef(pParent->pDbPage);
915 }
916 hdr = pPage->hdrOffset;
917 data = pPage->aData;
918 decodeFlags(pPage, data[hdr]);
919 pPage->nOverflow = 0;
920 pPage->idxShift = 0;
921 usableSize = pBt->usableSize;
922 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
923 top = get2byte(&data[hdr+5]);
924 pPage->nCell = get2byte(&data[hdr+3]);
925 if( pPage->nCell>MX_CELL(pBt) ){
926 /* To many cells for a single page. The page must be corrupt */
927 return SQLITE_CORRUPT_BKPT;
928 }
929 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
930 /* All pages must have at least one cell, except for root pages */
931 return SQLITE_CORRUPT_BKPT;
932 }
933
934 /* Compute the total free space on the page */
935 pc = get2byte(&data[hdr+1]);
936 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
937 while( pc>0 ){
938 int next, size;
939 if( pc>usableSize-4 ){
940 /* Free block is off the page */
941 return SQLITE_CORRUPT_BKPT;
942 }
943 next = get2byte(&data[pc]);
944 size = get2byte(&data[pc+2]);
945 if( next>0 && next<=pc+size+3 ){
946 /* Free blocks must be in accending order */
947 return SQLITE_CORRUPT_BKPT;
948 }
949 nFree += size;
950 pc = next;
951 }
952 pPage->nFree = nFree;
953 if( nFree>=usableSize ){
954 /* Free space cannot exceed total page size */
955 return SQLITE_CORRUPT_BKPT;
956 }
957
958 pPage->isInit = 1;
959 return SQLITE_OK;
960}
961
962/*
963** Set up a raw page so that it looks like a database page holding
964** no entries.
965*/
966static void zeroPage(MemPage *pPage, int flags){
967 unsigned char *data = pPage->aData;
968 BtShared *pBt = pPage->pBt;
969 int hdr = pPage->hdrOffset;
970 int first;
971
972 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
973 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
974 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
975 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
976 assert( sqlite3_mutex_held(pBt->mutex) );
977 memset(&data[hdr], 0, pBt->usableSize - hdr);
978 data[hdr] = flags;
979 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
980 memset(&data[hdr+1], 0, 4);
981 data[hdr+7] = 0;
982 put2byte(&data[hdr+5], pBt->usableSize);
983 pPage->nFree = pBt->usableSize - first;
984 decodeFlags(pPage, flags);
985 pPage->hdrOffset = hdr;
986 pPage->cellOffset = first;
987 pPage->nOverflow = 0;
988 pPage->idxShift = 0;
989 pPage->nCell = 0;
990 pPage->isInit = 1;
991}
992
993/*
994** Get a page from the pager. Initialize the MemPage.pBt and
995** MemPage.aData elements if needed.
996**
997** If the noContent flag is set, it means that we do not care about
998** the content of the page at this time. So do not go to the disk
999** to fetch the content. Just fill in the content with zeros for now.
1000** If in the future we call sqlite3PagerWrite() on this page, that
1001** means we have started to be concerned about content and the disk
1002** read should occur at that point.
1003*/
1004int sqlite3BtreeGetPage(
1005 BtShared *pBt, /* The btree */
1006 Pgno pgno, /* Number of the page to fetch */
1007 MemPage **ppPage, /* Return the page in this parameter */
1008 int noContent /* Do not load page content if true */
1009){
1010 int rc;
1011 MemPage *pPage;
1012 DbPage *pDbPage;
1013
1014 assert( sqlite3_mutex_held(pBt->mutex) );
1015 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
1016 if( rc ) return rc;
1017 pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1018 pPage->aData = sqlite3PagerGetData(pDbPage);
1019 pPage->pDbPage = pDbPage;
1020 pPage->pBt = pBt;
1021 pPage->pgno = pgno;
1022 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
1023 *ppPage = pPage;
1024 return SQLITE_OK;
1025}
1026
1027/*
1028** Get a page from the pager and initialize it. This routine
1029** is just a convenience wrapper around separate calls to
1030** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
1031*/
1032static int getAndInitPage(
1033 BtShared *pBt, /* The database file */
1034 Pgno pgno, /* Number of the page to get */
1035 MemPage **ppPage, /* Write the page pointer here */
1036 MemPage *pParent /* Parent of the page */
1037){
1038 int rc;
1039 assert( sqlite3_mutex_held(pBt->mutex) );
1040 if( pgno==0 ){
1041 return SQLITE_CORRUPT_BKPT;
1042 }
1043 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
1044 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
1045 rc = sqlite3BtreeInitPage(*ppPage, pParent);
1046 }
1047 return rc;
1048}
1049
1050/*
1051** Release a MemPage. This should be called once for each prior
1052** call to sqlite3BtreeGetPage.
1053*/
1054static void releasePage(MemPage *pPage){
1055 if( pPage ){
1056 assert( pPage->aData );
1057 assert( pPage->pBt );
1058 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1059 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
1060 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1061 sqlite3PagerUnref(pPage->pDbPage);
1062 }
1063}
1064
1065/*
1066** This routine is called when the reference count for a page
1067** reaches zero. We need to unref the pParent pointer when that
1068** happens.
1069*/
1070static void pageDestructor(DbPage *pData, int pageSize){
1071 MemPage *pPage;
1072 assert( (pageSize & 7)==0 );
1073 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
1074 assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
1075 if( pPage->pParent ){
1076 MemPage *pParent = pPage->pParent;
1077 assert( pParent->pBt==pPage->pBt );
1078 pPage->pParent = 0;
1079 releasePage(pParent);
1080 }
1081 pPage->isInit = 0;
1082}
1083
1084/*
1085** During a rollback, when the pager reloads information into the cache
1086** so that the cache is restored to its original state at the start of
1087** the transaction, for each page restored this routine is called.
1088**
1089** This routine needs to reset the extra data section at the end of the
1090** page to agree with the restored data.
1091*/
1092static void pageReinit(DbPage *pData, int pageSize){
1093 MemPage *pPage;
1094 assert( (pageSize & 7)==0 );
1095 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
1096 if( pPage->isInit ){
1097 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1098 pPage->isInit = 0;
1099 sqlite3BtreeInitPage(pPage, pPage->pParent);
1100 }
1101}
1102
1103/*
1104** Open a database file.
1105**
1106** zFilename is the name of the database file. If zFilename is NULL
1107** a new database with a random name is created. This randomly named
1108** database file will be deleted when sqlite3BtreeClose() is called.
1109** If zFilename is ":memory:" then an in-memory database is created
1110** that is automatically destroyed when it is closed.
1111*/
1112int sqlite3BtreeOpen(
1113 const char *zFilename, /* Name of the file containing the BTree database */
1114 sqlite3 *pSqlite, /* Associated database handle */
1115 Btree **ppBtree, /* Pointer to new Btree object written here */
1116 int flags, /* Options */
1117 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
1118){
1119 sqlite3_vfs *pVfs; /* The VFS to use for this btree */
1120 BtShared *pBt = 0; /* Shared part of btree structure */
1121 Btree *p; /* Handle to return */
1122 int rc = SQLITE_OK;
1123 int nReserve;
1124 unsigned char zDbHeader[100];
1125
1126 /* Set the variable isMemdb to true for an in-memory database, or
1127 ** false for a file-based database. This symbol is only required if
1128 ** either of the shared-data or autovacuum features are compiled
1129 ** into the library.
1130 */
1131#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1132 #ifdef SQLITE_OMIT_MEMORYDB
1133 const int isMemdb = 0;
1134 #else
1135 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
1136 #endif
1137#endif
1138
1139 assert( pSqlite!=0 );
1140 assert( sqlite3_mutex_held(pSqlite->mutex) );
1141
1142 pVfs = pSqlite->pVfs;
1143 p = sqlite3MallocZero(sizeof(Btree));
1144 if( !p ){
1145 return SQLITE_NOMEM;
1146 }
1147 p->inTrans = TRANS_NONE;
1148 p->pSqlite = pSqlite;
1149
1150#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1151 /*
1152 ** If this Btree is a candidate for shared cache, try to find an
1153 ** existing BtShared object that we can share with
1154 */
1155 if( (flags & BTREE_PRIVATE)==0
1156 && isMemdb==0
1157 && (pSqlite->flags & SQLITE_Vtab)==0
1158 && zFilename && zFilename[0]
1159 ){
1160 if( sqlite3SharedCacheEnabled ){
1161 int nFullPathname = pVfs->mxPathname+1;
1162 char *zFullPathname = (char *)sqlite3_malloc(nFullPathname);
1163 sqlite3_mutex *mutexShared;
1164 p->sharable = 1;
1165 if( pSqlite ){
1166 pSqlite->flags |= SQLITE_SharedCache;
1167 }
1168 if( !zFullPathname ){
1169 sqlite3_free(p);
1170 return SQLITE_NOMEM;
1171 }
1172 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
1173 mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1174 sqlite3_mutex_enter(mutexShared);
1175 for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
1176 assert( pBt->nRef>0 );
1177 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
1178 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
1179 p->pBt = pBt;
1180 pBt->nRef++;
1181 break;
1182 }
1183 }
1184 sqlite3_mutex_leave(mutexShared);
1185 sqlite3_free(zFullPathname);
1186 }
1187#ifdef SQLITE_DEBUG
1188 else{
1189 /* In debug mode, we mark all persistent databases as sharable
1190 ** even when they are not. This exercises the locking code and
1191 ** gives more opportunity for asserts(sqlite3_mutex_held())
1192 ** statements to find locking problems.
1193 */
1194 p->sharable = 1;
1195 }
1196#endif
1197 }
1198#endif
1199 if( pBt==0 ){
1200 /*
1201 ** The following asserts make sure that structures used by the btree are
1202 ** the right size. This is to guard against size changes that result
1203 ** when compiling on a different architecture.
1204 */
1205 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1206 assert( sizeof(u64)==8 || sizeof(u64)==4 );
1207 assert( sizeof(u32)==4 );
1208 assert( sizeof(u16)==2 );
1209 assert( sizeof(Pgno)==4 );
1210
1211 pBt = sqlite3MallocZero( sizeof(*pBt) );
1212 if( pBt==0 ){
1213 rc = SQLITE_NOMEM;
1214 goto btree_open_out;
1215 }
1216 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
1217 EXTRA_SIZE, flags, vfsFlags);
1218 if( rc==SQLITE_OK ){
1219 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
1220 }
1221 if( rc!=SQLITE_OK ){
1222 goto btree_open_out;
1223 }
1224 p->pBt = pBt;
1225
1226 sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
1227 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
1228 pBt->pCursor = 0;
1229 pBt->pPage1 = 0;
1230 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
1231 pBt->pageSize = get2byte(&zDbHeader[16]);
1232 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1233 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
1234 pBt->pageSize = 0;
1235 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1236 pBt->maxEmbedFrac = 64; /* 25% */
1237 pBt->minEmbedFrac = 32; /* 12.5% */
1238 pBt->minLeafFrac = 32; /* 12.5% */
1239#ifndef SQLITE_OMIT_AUTOVACUUM
1240 /* If the magic name ":memory:" will create an in-memory database, then
1241 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1242 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1243 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1244 ** regular file-name. In this case the auto-vacuum applies as per normal.
1245 */
1246 if( zFilename && !isMemdb ){
1247 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1248 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
1249 }
1250#endif
1251 nReserve = 0;
1252 }else{
1253 nReserve = zDbHeader[20];
1254 pBt->maxEmbedFrac = zDbHeader[21];
1255 pBt->minEmbedFrac = zDbHeader[22];
1256 pBt->minLeafFrac = zDbHeader[23];
1257 pBt->pageSizeFixed = 1;
1258#ifndef SQLITE_OMIT_AUTOVACUUM
1259 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1260 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
1261#endif
1262 }
1263 pBt->usableSize = pBt->pageSize - nReserve;
1264 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
1265 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1266
1267#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1268 /* Add the new BtShared object to the linked list sharable BtShareds.
1269 */
1270 if( p->sharable ){
1271 sqlite3_mutex *mutexShared;
1272 pBt->nRef = 1;
1273 mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1274 if( SQLITE_THREADSAFE ){
1275 pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
1276 if( pBt->mutex==0 ){
1277 rc = SQLITE_NOMEM;
1278 pSqlite->mallocFailed = 0;
1279 goto btree_open_out;
1280 }
1281 }
1282 sqlite3_mutex_enter(mutexShared);
1283 pBt->pNext = sqlite3SharedCacheList;
1284 sqlite3SharedCacheList = pBt;
1285 sqlite3_mutex_leave(mutexShared);
1286 }
1287#endif
1288 }
1289
1290#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1291 /* If the new Btree uses a sharable pBtShared, then link the new
1292 ** Btree into the list of all sharable Btrees for the same connection.
1293 ** The list is kept in ascending order by pBt address.
1294 */
1295 if( p->sharable ){
1296 int i;
1297 Btree *pSib;
1298 for(i=0; i<pSqlite->nDb; i++){
1299 if( (pSib = pSqlite->aDb[i].pBt)!=0 && pSib->sharable ){
1300 while( pSib->pPrev ){ pSib = pSib->pPrev; }
1301 if( p->pBt<pSib->pBt ){
1302 p->pNext = pSib;
1303 p->pPrev = 0;
1304 pSib->pPrev = p;
1305 }else{
1306 while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
1307 pSib = pSib->pNext;
1308 }
1309 p->pNext = pSib->pNext;
1310 p->pPrev = pSib;
1311 if( p->pNext ){
1312 p->pNext->pPrev = p;
1313 }
1314 pSib->pNext = p;
1315 }
1316 break;
1317 }
1318 }
1319 }
1320#endif
1321 *ppBtree = p;
1322
1323btree_open_out:
1324 if( rc!=SQLITE_OK ){
1325 if( pBt && pBt->pPager ){
1326 sqlite3PagerClose(pBt->pPager);
1327 }
1328 sqlite3_free(pBt);
1329 sqlite3_free(p);
1330 *ppBtree = 0;
1331 }
1332 return rc;
1333}
1334
1335/*
1336** Decrement the BtShared.nRef counter. When it reaches zero,
1337** remove the BtShared structure from the sharing list. Return
1338** true if the BtShared.nRef counter reaches zero and return
1339** false if it is still positive.
1340*/
1341static int removeFromSharingList(BtShared *pBt){
1342#ifndef SQLITE_OMIT_SHARED_CACHE
1343 sqlite3_mutex *pMaster;
1344 BtShared *pList;
1345 int removed = 0;
1346
1347 assert( sqlite3_mutex_notheld(pBt->mutex) );
1348 pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1349 sqlite3_mutex_enter(pMaster);
1350 pBt->nRef--;
1351 if( pBt->nRef<=0 ){
1352 if( sqlite3SharedCacheList==pBt ){
1353 sqlite3SharedCacheList = pBt->pNext;
1354 }else{
1355 pList = sqlite3SharedCacheList;
1356 while( pList && pList->pNext!=pBt ){
1357 pList=pList->pNext;
1358 }
1359 if( pList ){
1360 pList->pNext = pBt->pNext;
1361 }
1362 }
1363 if( SQLITE_THREADSAFE ){
1364 sqlite3_mutex_free(pBt->mutex);
1365 }
1366 removed = 1;
1367 }
1368 sqlite3_mutex_leave(pMaster);
1369 return removed;
1370#else
1371 return 1;
1372#endif
1373}
1374
1375/*
1376** Close an open database and invalidate all cursors.
1377*/
1378int sqlite3BtreeClose(Btree *p){
1379 BtShared *pBt = p->pBt;
1380 BtCursor *pCur;
1381
1382 /* Close all cursors opened via this handle. */
1383 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1384 sqlite3BtreeEnter(p);
1385 pCur = pBt->pCursor;
1386 while( pCur ){
1387 BtCursor *pTmp = pCur;
1388 pCur = pCur->pNext;
1389 if( pTmp->pBtree==p ){
1390 sqlite3BtreeCloseCursor(pTmp);
1391 }
1392 }
1393
1394 /* Rollback any active transaction and free the handle structure.
1395 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1396 ** this handle.
1397 */
1398 sqlite3BtreeRollback(p);
1399 sqlite3BtreeLeave(p);
1400
1401 /* If there are still other outstanding references to the shared-btree
1402 ** structure, return now. The remainder of this procedure cleans
1403 ** up the shared-btree.
1404 */
1405 assert( p->wantToLock==0 && p->locked==0 );
1406 if( !p->sharable || removeFromSharingList(pBt) ){
1407 /* The pBt is no longer on the sharing list, so we can access
1408 ** it without having to hold the mutex.
1409 **
1410 ** Clean out and delete the BtShared object.
1411 */
1412 assert( !pBt->pCursor );
1413 sqlite3PagerClose(pBt->pPager);
1414 if( pBt->xFreeSchema && pBt->pSchema ){
1415 pBt->xFreeSchema(pBt->pSchema);
1416 }
1417 sqlite3_free(pBt->pSchema);
1418 sqlite3_free(pBt);
1419 }
1420
1421#ifndef SQLITE_OMIT_SHARED_CACHE
1422 assert( p->wantToLock==0 );
1423 assert( p->locked==0 );
1424 if( p->pPrev ) p->pPrev->pNext = p->pNext;
1425 if( p->pNext ) p->pNext->pPrev = p->pPrev;
1426#endif
1427
1428 sqlite3_free(p);
1429 return SQLITE_OK;
1430}
1431
1432/*
1433** Change the busy handler callback function.
1434*/
1435int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
1436 BtShared *pBt = p->pBt;
1437 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1438 sqlite3BtreeEnter(p);
1439 pBt->pBusyHandler = pHandler;
1440 sqlite3PagerSetBusyhandler(pBt->pPager, pHandler);
1441 sqlite3BtreeLeave(p);
1442 return SQLITE_OK;
1443}
1444
1445/*
1446** Change the limit on the number of pages allowed in the cache.
1447**
1448** The maximum number of cache pages is set to the absolute
1449** value of mxPage. If mxPage is negative, the pager will
1450** operate asynchronously - it will not stop to do fsync()s
1451** to insure data is written to the disk surface before
1452** continuing. Transactions still work if synchronous is off,
1453** and the database cannot be corrupted if this program
1454** crashes. But if the operating system crashes or there is
1455** an abrupt power failure when synchronous is off, the database
1456** could be left in an inconsistent and unrecoverable state.
1457** Synchronous is on by default so database corruption is not
1458** normally a worry.
1459*/
1460int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1461 BtShared *pBt = p->pBt;
1462 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1463 sqlite3BtreeEnter(p);
1464 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
1465 sqlite3BtreeLeave(p);
1466 return SQLITE_OK;
1467}
1468
1469/*
1470** Change the way data is synced to disk in order to increase or decrease
1471** how well the database resists damage due to OS crashes and power
1472** failures. Level 1 is the same as asynchronous (no syncs() occur and
1473** there is a high probability of damage) Level 2 is the default. There
1474** is a very low but non-zero probability of damage. Level 3 reduces the
1475** probability of damage to near zero but with a write performance reduction.
1476*/
1477#ifndef SQLITE_OMIT_PAGER_PRAGMAS
1478int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
1479 BtShared *pBt = p->pBt;
1480 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1481 sqlite3BtreeEnter(p);
1482 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
1483 sqlite3BtreeLeave(p);
1484 return SQLITE_OK;
1485}
1486#endif
1487
1488/*
1489** Return TRUE if the given btree is set to safety level 1. In other
1490** words, return TRUE if no sync() occurs on the disk files.
1491*/
1492int sqlite3BtreeSyncDisabled(Btree *p){
1493 BtShared *pBt = p->pBt;
1494 int rc;
1495 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1496 sqlite3BtreeEnter(p);
1497 assert( pBt && pBt->pPager );
1498 rc = sqlite3PagerNosync(pBt->pPager);
1499 sqlite3BtreeLeave(p);
1500 return rc;
1501}
1502
1503#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
1504/*
1505** Change the default pages size and the number of reserved bytes per page.
1506**
1507** The page size must be a power of 2 between 512 and 65536. If the page
1508** size supplied does not meet this constraint then the page size is not
1509** changed.
1510**
1511** Page sizes are constrained to be a power of two so that the region
1512** of the database file used for locking (beginning at PENDING_BYTE,
1513** the first byte past the 1GB boundary, 0x40000000) needs to occur
1514** at the beginning of a page.
1515**
1516** If parameter nReserve is less than zero, then the number of reserved
1517** bytes per page is left unchanged.
1518*/
1519int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
1520 int rc = SQLITE_OK;
1521 BtShared *pBt = p->pBt;
1522 sqlite3BtreeEnter(p);
1523 if( pBt->pageSizeFixed ){
1524 sqlite3BtreeLeave(p);
1525 return SQLITE_READONLY;
1526 }
1527 if( nReserve<0 ){
1528 nReserve = pBt->pageSize - pBt->usableSize;
1529 }
1530 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1531 ((pageSize-1)&pageSize)==0 ){
1532 assert( (pageSize & 7)==0 );
1533 assert( !pBt->pPage1 && !pBt->pCursor );
1534 pBt->pageSize = pageSize;
1535 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1536 }
1537 pBt->usableSize = pBt->pageSize - nReserve;
1538 sqlite3BtreeLeave(p);
1539 return rc;
1540}
1541
1542/*
1543** Return the currently defined page size
1544*/
1545int sqlite3BtreeGetPageSize(Btree *p){
1546 return p->pBt->pageSize;
1547}
1548int sqlite3BtreeGetReserve(Btree *p){
1549 int n;
1550 sqlite3BtreeEnter(p);
1551 n = p->pBt->pageSize - p->pBt->usableSize;
1552 sqlite3BtreeLeave(p);
1553 return n;
1554}
1555
1556/*
1557** Set the maximum page count for a database if mxPage is positive.
1558** No changes are made if mxPage is 0 or negative.
1559** Regardless of the value of mxPage, return the maximum page count.
1560*/
1561int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
1562 int n;
1563 sqlite3BtreeEnter(p);
1564 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
1565 sqlite3BtreeLeave(p);
1566 return n;
1567}
1568#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
1569
1570/*
1571** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1572** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1573** is disabled. The default value for the auto-vacuum property is
1574** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1575*/
1576int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
1577#ifdef SQLITE_OMIT_AUTOVACUUM
1578 return SQLITE_READONLY;
1579#else
1580 BtShared *pBt = p->pBt;
1581 int rc = SQLITE_OK;
1582 int av = (autoVacuum?1:0);
1583
1584 sqlite3BtreeEnter(p);
1585 if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
1586 rc = SQLITE_READONLY;
1587 }else{
1588 pBt->autoVacuum = av;
1589 }
1590 sqlite3BtreeLeave(p);
1591 return rc;
1592#endif
1593}
1594
1595/*
1596** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1597** enabled 1 is returned. Otherwise 0.
1598*/
1599int sqlite3BtreeGetAutoVacuum(Btree *p){
1600#ifdef SQLITE_OMIT_AUTOVACUUM
1601 return BTREE_AUTOVACUUM_NONE;
1602#else
1603 int rc;
1604 sqlite3BtreeEnter(p);
1605 rc = (
1606 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1607 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1608 BTREE_AUTOVACUUM_INCR
1609 );
1610 sqlite3BtreeLeave(p);
1611 return rc;
1612#endif
1613}
1614
1615
1616/*
1617** Get a reference to pPage1 of the database file. This will
1618** also acquire a readlock on that file.
1619**
1620** SQLITE_OK is returned on success. If the file is not a
1621** well-formed database file, then SQLITE_CORRUPT is returned.
1622** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
1623** is returned if we run out of memory.
1624*/
1625static int lockBtree(BtShared *pBt){
1626 int rc, pageSize;
1627 MemPage *pPage1;
1628
1629 assert( sqlite3_mutex_held(pBt->mutex) );
1630 if( pBt->pPage1 ) return SQLITE_OK;
1631 rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
1632 if( rc!=SQLITE_OK ) return rc;
1633
1634
1635 /* Do some checking to help insure the file we opened really is
1636 ** a valid database file.
1637 */
1638 rc = SQLITE_NOTADB;
1639 if( sqlite3PagerPagecount(pBt->pPager)>0 ){
1640 u8 *page1 = pPage1->aData;
1641 if( memcmp(page1, zMagicHeader, 16)!=0 ){
1642 goto page1_init_failed;
1643 }
1644 if( page1[18]>1 ){
1645 pBt->readOnly = 1;
1646 }
1647 if( page1[19]>1 ){
1648 goto page1_init_failed;
1649 }
1650 pageSize = get2byte(&page1[16]);
1651 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
1652 (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
1653 ){
1654 goto page1_init_failed;
1655 }
1656 assert( (pageSize & 7)==0 );
1657 pBt->pageSize = pageSize;
1658 pBt->usableSize = pageSize - page1[20];
1659 if( pBt->usableSize<500 ){
1660 goto page1_init_failed;
1661 }
1662 pBt->maxEmbedFrac = page1[21];
1663 pBt->minEmbedFrac = page1[22];
1664 pBt->minLeafFrac = page1[23];
1665#ifndef SQLITE_OMIT_AUTOVACUUM
1666 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
1667 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
1668#endif
1669 }
1670
1671 /* maxLocal is the maximum amount of payload to store locally for
1672 ** a cell. Make sure it is small enough so that at least minFanout
1673 ** cells can will fit on one page. We assume a 10-byte page header.
1674 ** Besides the payload, the cell must store:
1675 ** 2-byte pointer to the cell
1676 ** 4-byte child pointer
1677 ** 9-byte nKey value
1678 ** 4-byte nData value
1679 ** 4-byte overflow page pointer
1680 ** So a cell consists of a 2-byte poiner, a header which is as much as
1681 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1682 ** page pointer.
1683 */
1684 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1685 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1686 pBt->maxLeaf = pBt->usableSize - 35;
1687 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
1688 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1689 goto page1_init_failed;
1690 }
1691 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
1692 pBt->pPage1 = pPage1;
1693 return SQLITE_OK;
1694
1695page1_init_failed:
1696 releasePage(pPage1);
1697 pBt->pPage1 = 0;
1698 return rc;
1699}
1700
1701/*
1702** This routine works like lockBtree() except that it also invokes the
1703** busy callback if there is lock contention.
1704*/
1705static int lockBtreeWithRetry(Btree *pRef){
1706 int rc = SQLITE_OK;
1707
1708 assert( sqlite3BtreeHoldsMutex(pRef) );
1709 if( pRef->inTrans==TRANS_NONE ){
1710 u8 inTransaction = pRef->pBt->inTransaction;
1711 btreeIntegrity(pRef);
1712 rc = sqlite3BtreeBeginTrans(pRef, 0);
1713 pRef->pBt->inTransaction = inTransaction;
1714 pRef->inTrans = TRANS_NONE;
1715 if( rc==SQLITE_OK ){
1716 pRef->pBt->nTransaction--;
1717 }
1718 btreeIntegrity(pRef);
1719 }
1720 return rc;
1721}
1722
1723
1724/*
1725** If there are no outstanding cursors and we are not in the middle
1726** of a transaction but there is a read lock on the database, then
1727** this routine unrefs the first page of the database file which
1728** has the effect of releasing the read lock.
1729**
1730** If there are any outstanding cursors, this routine is a no-op.
1731**
1732** If there is a transaction in progress, this routine is a no-op.
1733*/
1734static void unlockBtreeIfUnused(BtShared *pBt){
1735 assert( sqlite3_mutex_held(pBt->mutex) );
1736 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
1737 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
1738 if( pBt->pPage1->aData==0 ){
1739 MemPage *pPage = pBt->pPage1;
1740 pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
1741 pPage->pBt = pBt;
1742 pPage->pgno = 1;
1743 }
1744 releasePage(pBt->pPage1);
1745 }
1746 pBt->pPage1 = 0;
1747 pBt->inStmt = 0;
1748 }
1749}
1750
1751/*
1752** Create a new database by initializing the first page of the
1753** file.
1754*/
1755static int newDatabase(BtShared *pBt){
1756 MemPage *pP1;
1757 unsigned char *data;
1758 int rc;
1759
1760 assert( sqlite3_mutex_held(pBt->mutex) );
1761 if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
1762 pP1 = pBt->pPage1;
1763 assert( pP1!=0 );
1764 data = pP1->aData;
1765 rc = sqlite3PagerWrite(pP1->pDbPage);
1766 if( rc ) return rc;
1767 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1768 assert( sizeof(zMagicHeader)==16 );
1769 put2byte(&data[16], pBt->pageSize);
1770 data[18] = 1;
1771 data[19] = 1;
1772 data[20] = pBt->pageSize - pBt->usableSize;
1773 data[21] = pBt->maxEmbedFrac;
1774 data[22] = pBt->minEmbedFrac;
1775 data[23] = pBt->minLeafFrac;
1776 memset(&data[24], 0, 100-24);
1777 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
1778 pBt->pageSizeFixed = 1;
1779#ifndef SQLITE_OMIT_AUTOVACUUM
1780 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
1781 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
1782 put4byte(&data[36 + 4*4], pBt->autoVacuum);
1783 put4byte(&data[36 + 7*4], pBt->incrVacuum);
1784#endif
1785 return SQLITE_OK;
1786}
1787
1788/*
1789** Attempt to start a new transaction. A write-transaction
1790** is started if the second argument is nonzero, otherwise a read-
1791** transaction. If the second argument is 2 or more and exclusive
1792** transaction is started, meaning that no other process is allowed
1793** to access the database. A preexisting transaction may not be
1794** upgraded to exclusive by calling this routine a second time - the
1795** exclusivity flag only works for a new transaction.
1796**
1797** A write-transaction must be started before attempting any
1798** changes to the database. None of the following routines
1799** will work unless a transaction is started first:
1800**
1801** sqlite3BtreeCreateTable()
1802** sqlite3BtreeCreateIndex()
1803** sqlite3BtreeClearTable()
1804** sqlite3BtreeDropTable()
1805** sqlite3BtreeInsert()
1806** sqlite3BtreeDelete()
1807** sqlite3BtreeUpdateMeta()
1808**
1809** If an initial attempt to acquire the lock fails because of lock contention
1810** and the database was previously unlocked, then invoke the busy handler
1811** if there is one. But if there was previously a read-lock, do not
1812** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
1813** returned when there is already a read-lock in order to avoid a deadlock.
1814**
1815** Suppose there are two processes A and B. A has a read lock and B has
1816** a reserved lock. B tries to promote to exclusive but is blocked because
1817** of A's read lock. A tries to promote to reserved but is blocked by B.
1818** One or the other of the two processes must give way or there can be
1819** no progress. By returning SQLITE_BUSY and not invoking the busy callback
1820** when A already has a read lock, we encourage A to give up and let B
1821** proceed.
1822*/
1823int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
1824 BtShared *pBt = p->pBt;
1825 int rc = SQLITE_OK;
1826
1827 sqlite3BtreeEnter(p);
1828 btreeIntegrity(p);
1829
1830 /* If the btree is already in a write-transaction, or it
1831 ** is already in a read-transaction and a read-transaction
1832 ** is requested, this is a no-op.
1833 */
1834 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
1835 goto trans_begun;
1836 }
1837
1838 /* Write transactions are not possible on a read-only database */
1839 if( pBt->readOnly && wrflag ){
1840 rc = SQLITE_READONLY;
1841 goto trans_begun;
1842 }
1843
1844 /* If another database handle has already opened a write transaction
1845 ** on this shared-btree structure and a second write transaction is
1846 ** requested, return SQLITE_BUSY.
1847 */
1848 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
1849 rc = SQLITE_BUSY;
1850 goto trans_begun;
1851 }
1852
1853 do {
1854 if( pBt->pPage1==0 ){
1855 rc = lockBtree(pBt);
1856 }
1857
1858 if( rc==SQLITE_OK && wrflag ){
1859 if( pBt->readOnly ){
1860 rc = SQLITE_READONLY;
1861 }else{
1862 rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
1863 if( rc==SQLITE_OK ){
1864 rc = newDatabase(pBt);
1865 }
1866 }
1867 }
1868
1869 if( rc==SQLITE_OK ){
1870 if( wrflag ) pBt->inStmt = 0;
1871 }else{
1872 unlockBtreeIfUnused(pBt);
1873 }
1874 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
1875 sqlite3InvokeBusyHandler(pBt->pBusyHandler) );
1876
1877 if( rc==SQLITE_OK ){
1878 if( p->inTrans==TRANS_NONE ){
1879 pBt->nTransaction++;
1880 }
1881 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
1882 if( p->inTrans>pBt->inTransaction ){
1883 pBt->inTransaction = p->inTrans;
1884 }
1885 }
1886
1887
1888trans_begun:
1889 btreeIntegrity(p);
1890 sqlite3BtreeLeave(p);
1891 return rc;
1892}
1893
1894#ifndef SQLITE_OMIT_AUTOVACUUM
1895
1896/*
1897** Set the pointer-map entries for all children of page pPage. Also, if
1898** pPage contains cells that point to overflow pages, set the pointer
1899** map entries for the overflow pages as well.
1900*/
1901static int setChildPtrmaps(MemPage *pPage){
1902 int i; /* Counter variable */
1903 int nCell; /* Number of cells in page pPage */
1904 int rc; /* Return code */
1905 BtShared *pBt = pPage->pBt;
1906 int isInitOrig = pPage->isInit;
1907 Pgno pgno = pPage->pgno;
1908
1909 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1910 rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
1911 if( rc!=SQLITE_OK ){
1912 goto set_child_ptrmaps_out;
1913 }
1914 nCell = pPage->nCell;
1915
1916 for(i=0; i<nCell; i++){
1917 u8 *pCell = findCell(pPage, i);
1918
1919 rc = ptrmapPutOvflPtr(pPage, pCell);
1920 if( rc!=SQLITE_OK ){
1921 goto set_child_ptrmaps_out;
1922 }
1923
1924 if( !pPage->leaf ){
1925 Pgno childPgno = get4byte(pCell);
1926 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1927 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1928 }
1929 }
1930
1931 if( !pPage->leaf ){
1932 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1933 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1934 }
1935
1936set_child_ptrmaps_out:
1937 pPage->isInit = isInitOrig;
1938 return rc;
1939}
1940
1941/*
1942** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
1943** page, is a pointer to page iFrom. Modify this pointer so that it points to
1944** iTo. Parameter eType describes the type of pointer to be modified, as
1945** follows:
1946**
1947** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
1948** page of pPage.
1949**
1950** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
1951** page pointed to by one of the cells on pPage.
1952**
1953** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
1954** overflow page in the list.
1955*/
1956static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
1957 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1958 if( eType==PTRMAP_OVERFLOW2 ){
1959 /* The pointer is always the first 4 bytes of the page in this case. */
1960 if( get4byte(pPage->aData)!=iFrom ){
1961 return SQLITE_CORRUPT_BKPT;
1962 }
1963 put4byte(pPage->aData, iTo);
1964 }else{
1965 int isInitOrig = pPage->isInit;
1966 int i;
1967 int nCell;
1968
1969 sqlite3BtreeInitPage(pPage, 0);
1970 nCell = pPage->nCell;
1971
1972 for(i=0; i<nCell; i++){
1973 u8 *pCell = findCell(pPage, i);
1974 if( eType==PTRMAP_OVERFLOW1 ){
1975 CellInfo info;
1976 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1977 if( info.iOverflow ){
1978 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
1979 put4byte(&pCell[info.iOverflow], iTo);
1980 break;
1981 }
1982 }
1983 }else{
1984 if( get4byte(pCell)==iFrom ){
1985 put4byte(pCell, iTo);
1986 break;
1987 }
1988 }
1989 }
1990
1991 if( i==nCell ){
1992 if( eType!=PTRMAP_BTREE ||
1993 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
1994 return SQLITE_CORRUPT_BKPT;
1995 }
1996 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
1997 }
1998
1999 pPage->isInit = isInitOrig;
2000 }
2001 return SQLITE_OK;
2002}
2003
2004
2005/*
2006** Move the open database page pDbPage to location iFreePage in the
2007** database. The pDbPage reference remains valid.
2008*/
2009static int relocatePage(
2010 BtShared *pBt, /* Btree */
2011 MemPage *pDbPage, /* Open page to move */
2012 u8 eType, /* Pointer map 'type' entry for pDbPage */
2013 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
2014 Pgno iFreePage /* The location to move pDbPage to */
2015){
2016 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2017 Pgno iDbPage = pDbPage->pgno;
2018 Pager *pPager = pBt->pPager;
2019 int rc;
2020
2021 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2022 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
2023 assert( sqlite3_mutex_held(pBt->mutex) );
2024 assert( pDbPage->pBt==pBt );
2025
2026 /* Move page iDbPage from it's current location to page number iFreePage */
2027 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2028 iDbPage, iFreePage, iPtrPage, eType));
2029 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
2030 if( rc!=SQLITE_OK ){
2031 return rc;
2032 }
2033 pDbPage->pgno = iFreePage;
2034
2035 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2036 ** that point to overflow pages. The pointer map entries for all these
2037 ** pages need to be changed.
2038 **
2039 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2040 ** pointer to a subsequent overflow page. If this is the case, then
2041 ** the pointer map needs to be updated for the subsequent overflow page.
2042 */
2043 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
2044 rc = setChildPtrmaps(pDbPage);
2045 if( rc!=SQLITE_OK ){
2046 return rc;
2047 }
2048 }else{
2049 Pgno nextOvfl = get4byte(pDbPage->aData);
2050 if( nextOvfl!=0 ){
2051 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2052 if( rc!=SQLITE_OK ){
2053 return rc;
2054 }
2055 }
2056 }
2057
2058 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2059 ** that it points at iFreePage. Also fix the pointer map entry for
2060 ** iPtrPage.
2061 */
2062 if( eType!=PTRMAP_ROOTPAGE ){
2063 rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
2064 if( rc!=SQLITE_OK ){
2065 return rc;
2066 }
2067 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
2068 if( rc!=SQLITE_OK ){
2069 releasePage(pPtrPage);
2070 return rc;
2071 }
2072 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
2073 releasePage(pPtrPage);
2074 if( rc==SQLITE_OK ){
2075 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2076 }
2077 }
2078 return rc;
2079}
2080
2081/* Forward declaration required by incrVacuumStep(). */
2082static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
2083
2084/*
2085** Perform a single step of an incremental-vacuum. If successful,
2086** return SQLITE_OK. If there is no work to do (and therefore no
2087** point in calling this function again), return SQLITE_DONE.
2088**
2089** More specificly, this function attempts to re-organize the
2090** database so that the last page of the file currently in use
2091** is no longer in use.
2092**
2093** If the nFin parameter is non-zero, the implementation assumes
2094** that the caller will keep calling incrVacuumStep() until
2095** it returns SQLITE_DONE or an error, and that nFin is the
2096** number of pages the database file will contain after this
2097** process is complete.
2098*/
2099static int incrVacuumStep(BtShared *pBt, Pgno nFin){
2100 Pgno iLastPg; /* Last page in the database */
2101 Pgno nFreeList; /* Number of pages still on the free-list */
2102
2103 assert( sqlite3_mutex_held(pBt->mutex) );
2104 iLastPg = pBt->nTrunc;
2105 if( iLastPg==0 ){
2106 iLastPg = sqlite3PagerPagecount(pBt->pPager);
2107 }
2108
2109 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
2110 int rc;
2111 u8 eType;
2112 Pgno iPtrPage;
2113
2114 nFreeList = get4byte(&pBt->pPage1->aData[36]);
2115 if( nFreeList==0 || nFin==iLastPg ){
2116 return SQLITE_DONE;
2117 }
2118
2119 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
2120 if( rc!=SQLITE_OK ){
2121 return rc;
2122 }
2123 if( eType==PTRMAP_ROOTPAGE ){
2124 return SQLITE_CORRUPT_BKPT;
2125 }
2126
2127 if( eType==PTRMAP_FREEPAGE ){
2128 if( nFin==0 ){
2129 /* Remove the page from the files free-list. This is not required
2130 ** if nFin is non-zero. In that case, the free-list will be
2131 ** truncated to zero after this function returns, so it doesn't
2132 ** matter if it still contains some garbage entries.
2133 */
2134 Pgno iFreePg;
2135 MemPage *pFreePg;
2136 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
2137 if( rc!=SQLITE_OK ){
2138 return rc;
2139 }
2140 assert( iFreePg==iLastPg );
2141 releasePage(pFreePg);
2142 }
2143 } else {
2144 Pgno iFreePg; /* Index of free page to move pLastPg to */
2145 MemPage *pLastPg;
2146
2147 rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
2148 if( rc!=SQLITE_OK ){
2149 return rc;
2150 }
2151
2152 /* If nFin is zero, this loop runs exactly once and page pLastPg
2153 ** is swapped with the first free page pulled off the free list.
2154 **
2155 ** On the other hand, if nFin is greater than zero, then keep
2156 ** looping until a free-page located within the first nFin pages
2157 ** of the file is found.
2158 */
2159 do {
2160 MemPage *pFreePg;
2161 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
2162 if( rc!=SQLITE_OK ){
2163 releasePage(pLastPg);
2164 return rc;
2165 }
2166 releasePage(pFreePg);
2167 }while( nFin!=0 && iFreePg>nFin );
2168 assert( iFreePg<iLastPg );
2169
2170 rc = sqlite3PagerWrite(pLastPg->pDbPage);
2171 if( rc!=SQLITE_OK ){
2172 return rc;
2173 }
2174 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg);
2175 releasePage(pLastPg);
2176 if( rc!=SQLITE_OK ){
2177 return rc;
2178 }
2179 }
2180 }
2181
2182 pBt->nTrunc = iLastPg - 1;
2183 while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
2184 pBt->nTrunc--;
2185 }
2186 return SQLITE_OK;
2187}
2188
2189/*
2190** A write-transaction must be opened before calling this function.
2191** It performs a single unit of work towards an incremental vacuum.
2192**
2193** If the incremental vacuum is finished after this function has run,
2194** SQLITE_DONE is returned. If it is not finished, but no error occured,
2195** SQLITE_OK is returned. Otherwise an SQLite error code.
2196*/
2197int sqlite3BtreeIncrVacuum(Btree *p){
2198 int rc;
2199 BtShared *pBt = p->pBt;
2200
2201 sqlite3BtreeEnter(p);
2202 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
2203 if( !pBt->autoVacuum ){
2204 rc = SQLITE_DONE;
2205 }else{
2206 invalidateAllOverflowCache(pBt);
2207 rc = incrVacuumStep(pBt, 0);
2208 }
2209 sqlite3BtreeLeave(p);
2210 return rc;
2211}
2212
2213/*
2214** This routine is called prior to sqlite3PagerCommit when a transaction
2215** is commited for an auto-vacuum database.
2216**
2217** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
2218** the database file should be truncated to during the commit process.
2219** i.e. the database has been reorganized so that only the first *pnTrunc
2220** pages are in use.
2221*/
2222static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
2223 int rc = SQLITE_OK;
2224 Pager *pPager = pBt->pPager;
2225#ifndef NDEBUG
2226 int nRef = sqlite3PagerRefcount(pPager);
2227#endif
2228
2229 assert( sqlite3_mutex_held(pBt->mutex) );
2230 invalidateAllOverflowCache(pBt);
2231 assert(pBt->autoVacuum);
2232 if( !pBt->incrVacuum ){
2233 Pgno nFin = 0;
2234
2235 if( pBt->nTrunc==0 ){
2236 Pgno nFree;
2237 Pgno nPtrmap;
2238 const int pgsz = pBt->pageSize;
2239 Pgno nOrig = sqlite3PagerPagecount(pBt->pPager);
2240
2241 if( PTRMAP_ISPAGE(pBt, nOrig) ){
2242 return SQLITE_CORRUPT_BKPT;
2243 }
2244 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
2245 nOrig--;
2246 }
2247 nFree = get4byte(&pBt->pPage1->aData[36]);
2248 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
2249 nFin = nOrig - nFree - nPtrmap;
2250 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
2251 nFin--;
2252 }
2253 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
2254 nFin--;
2255 }
2256 }
2257
2258 while( rc==SQLITE_OK ){
2259 rc = incrVacuumStep(pBt, nFin);
2260 }
2261 if( rc==SQLITE_DONE ){
2262 assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
2263 rc = SQLITE_OK;
2264 if( pBt->nTrunc ){
2265 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
2266 put4byte(&pBt->pPage1->aData[32], 0);
2267 put4byte(&pBt->pPage1->aData[36], 0);
2268 pBt->nTrunc = nFin;
2269 }
2270 }
2271 if( rc!=SQLITE_OK ){
2272 sqlite3PagerRollback(pPager);
2273 }
2274 }
2275
2276 if( rc==SQLITE_OK ){
2277 *pnTrunc = pBt->nTrunc;
2278 pBt->nTrunc = 0;
2279 }
2280 assert( nRef==sqlite3PagerRefcount(pPager) );
2281 return rc;
2282}
2283
2284#endif
2285
2286/*
2287** This routine does the first phase of a two-phase commit. This routine
2288** causes a rollback journal to be created (if it does not already exist)
2289** and populated with enough information so that if a power loss occurs
2290** the database can be restored to its original state by playing back
2291** the journal. Then the contents of the journal are flushed out to
2292** the disk. After the journal is safely on oxide, the changes to the
2293** database are written into the database file and flushed to oxide.
2294** At the end of this call, the rollback journal still exists on the
2295** disk and we are still holding all locks, so the transaction has not
2296** committed. See sqlite3BtreeCommit() for the second phase of the
2297** commit process.
2298**
2299** This call is a no-op if no write-transaction is currently active on pBt.
2300**
2301** Otherwise, sync the database file for the btree pBt. zMaster points to
2302** the name of a master journal file that should be written into the
2303** individual journal file, or is NULL, indicating no master journal file
2304** (single database transaction).
2305**
2306** When this is called, the master journal should already have been
2307** created, populated with this journal pointer and synced to disk.
2308**
2309** Once this is routine has returned, the only thing required to commit
2310** the write-transaction for this database file is to delete the journal.
2311*/
2312int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
2313 int rc = SQLITE_OK;
2314 if( p->inTrans==TRANS_WRITE ){
2315 BtShared *pBt = p->pBt;
2316 Pgno nTrunc = 0;
2317 sqlite3BtreeEnter(p);
2318#ifndef SQLITE_OMIT_AUTOVACUUM
2319 if( pBt->autoVacuum ){
2320 rc = autoVacuumCommit(pBt, &nTrunc);
2321 if( rc!=SQLITE_OK ){
2322 sqlite3BtreeLeave(p);
2323 return rc;
2324 }
2325 }
2326#endif
2327 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc);
2328 sqlite3BtreeLeave(p);
2329 }
2330 return rc;
2331}
2332
2333/*
2334** Commit the transaction currently in progress.
2335**
2336** This routine implements the second phase of a 2-phase commit. The
2337** sqlite3BtreeSync() routine does the first phase and should be invoked
2338** prior to calling this routine. The sqlite3BtreeSync() routine did
2339** all the work of writing information out to disk and flushing the
2340** contents so that they are written onto the disk platter. All this
2341** routine has to do is delete or truncate the rollback journal
2342** (which causes the transaction to commit) and drop locks.
2343**
2344** This will release the write lock on the database file. If there
2345** are no active cursors, it also releases the read lock.
2346*/
2347int sqlite3BtreeCommitPhaseTwo(Btree *p){
2348 BtShared *pBt = p->pBt;
2349
2350 sqlite3BtreeEnter(p);
2351 btreeIntegrity(p);
2352
2353 /* If the handle has a write-transaction open, commit the shared-btrees
2354 ** transaction and set the shared state to TRANS_READ.
2355 */
2356 if( p->inTrans==TRANS_WRITE ){
2357 int rc;
2358 assert( pBt->inTransaction==TRANS_WRITE );
2359 assert( pBt->nTransaction>0 );
2360 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
2361 if( rc!=SQLITE_OK ){
2362 sqlite3BtreeLeave(p);
2363 return rc;
2364 }
2365 pBt->inTransaction = TRANS_READ;
2366 pBt->inStmt = 0;
2367 }
2368 unlockAllTables(p);
2369
2370 /* If the handle has any kind of transaction open, decrement the transaction
2371 ** count of the shared btree. If the transaction count reaches 0, set
2372 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2373 ** will unlock the pager.
2374 */
2375 if( p->inTrans!=TRANS_NONE ){
2376 pBt->nTransaction--;
2377 if( 0==pBt->nTransaction ){
2378 pBt->inTransaction = TRANS_NONE;
2379 }
2380 }
2381
2382 /* Set the handles current transaction state to TRANS_NONE and unlock
2383 ** the pager if this call closed the only read or write transaction.
2384 */
2385 p->inTrans = TRANS_NONE;
2386 unlockBtreeIfUnused(pBt);
2387
2388 btreeIntegrity(p);
2389 sqlite3BtreeLeave(p);
2390 return SQLITE_OK;
2391}
2392
2393/*
2394** Do both phases of a commit.
2395*/
2396int sqlite3BtreeCommit(Btree *p){
2397 int rc;
2398 sqlite3BtreeEnter(p);
2399 rc = sqlite3BtreeCommitPhaseOne(p, 0);
2400 if( rc==SQLITE_OK ){
2401 rc = sqlite3BtreeCommitPhaseTwo(p);
2402 }
2403 sqlite3BtreeLeave(p);
2404 return rc;
2405}
2406
2407#ifndef NDEBUG
2408/*
2409** Return the number of write-cursors open on this handle. This is for use
2410** in assert() expressions, so it is only compiled if NDEBUG is not
2411** defined.
2412**
2413** For the purposes of this routine, a write-cursor is any cursor that
2414** is capable of writing to the databse. That means the cursor was
2415** originally opened for writing and the cursor has not be disabled
2416** by having its state changed to CURSOR_FAULT.
2417*/
2418static int countWriteCursors(BtShared *pBt){
2419 BtCursor *pCur;
2420 int r = 0;
2421 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
2422 if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
2423 }
2424 return r;
2425}
2426#endif
2427
2428/*
2429** This routine sets the state to CURSOR_FAULT and the error
2430** code to errCode for every cursor on BtShared that pBtree
2431** references.
2432**
2433** Every cursor is tripped, including cursors that belong
2434** to other database connections that happen to be sharing
2435** the cache with pBtree.
2436**
2437** This routine gets called when a rollback occurs.
2438** All cursors using the same cache must be tripped
2439** to prevent them from trying to use the btree after
2440** the rollback. The rollback may have deleted tables
2441** or moved root pages, so it is not sufficient to
2442** save the state of the cursor. The cursor must be
2443** invalidated.
2444*/
2445void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
2446 BtCursor *p;
2447 sqlite3BtreeEnter(pBtree);
2448 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
2449 clearCursorPosition(p);
2450 p->eState = CURSOR_FAULT;
2451 p->skip = errCode;
2452 }
2453 sqlite3BtreeLeave(pBtree);
2454}
2455
2456/*
2457** Rollback the transaction in progress. All cursors will be
2458** invalided by this operation. Any attempt to use a cursor
2459** that was open at the beginning of this operation will result
2460** in an error.
2461**
2462** This will release the write lock on the database file. If there
2463** are no active cursors, it also releases the read lock.
2464*/
2465int sqlite3BtreeRollback(Btree *p){
2466 int rc;
2467 BtShared *pBt = p->pBt;
2468 MemPage *pPage1;
2469
2470 sqlite3BtreeEnter(p);
2471 rc = saveAllCursors(pBt, 0, 0);
2472#ifndef SQLITE_OMIT_SHARED_CACHE
2473 if( rc!=SQLITE_OK ){
2474 /* This is a horrible situation. An IO or malloc() error occured whilst
2475 ** trying to save cursor positions. If this is an automatic rollback (as
2476 ** the result of a constraint, malloc() failure or IO error) then
2477 ** the cache may be internally inconsistent (not contain valid trees) so
2478 ** we cannot simply return the error to the caller. Instead, abort
2479 ** all queries that may be using any of the cursors that failed to save.
2480 */
2481 sqlite3BtreeTripAllCursors(p, rc);
2482 }
2483#endif
2484 btreeIntegrity(p);
2485 unlockAllTables(p);
2486
2487 if( p->inTrans==TRANS_WRITE ){
2488 int rc2;
2489
2490#ifndef SQLITE_OMIT_AUTOVACUUM
2491 pBt->nTrunc = 0;
2492#endif
2493
2494 assert( TRANS_WRITE==pBt->inTransaction );
2495 rc2 = sqlite3PagerRollback(pBt->pPager);
2496 if( rc2!=SQLITE_OK ){
2497 rc = rc2;
2498 }
2499
2500 /* The rollback may have destroyed the pPage1->aData value. So
2501 ** call sqlite3BtreeGetPage() on page 1 again to make
2502 ** sure pPage1->aData is set correctly. */
2503 if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
2504 releasePage(pPage1);
2505 }
2506 assert( countWriteCursors(pBt)==0 );
2507 pBt->inTransaction = TRANS_READ;
2508 }
2509
2510 if( p->inTrans!=TRANS_NONE ){
2511 assert( pBt->nTransaction>0 );
2512 pBt->nTransaction--;
2513 if( 0==pBt->nTransaction ){
2514 pBt->inTransaction = TRANS_NONE;
2515 }
2516 }
2517
2518 p->inTrans = TRANS_NONE;
2519 pBt->inStmt = 0;
2520 unlockBtreeIfUnused(pBt);
2521
2522 btreeIntegrity(p);
2523 sqlite3BtreeLeave(p);
2524 return rc;
2525}
2526
2527/*
2528** Start a statement subtransaction. The subtransaction can
2529** can be rolled back independently of the main transaction.
2530** You must start a transaction before starting a subtransaction.
2531** The subtransaction is ended automatically if the main transaction
2532** commits or rolls back.
2533**
2534** Only one subtransaction may be active at a time. It is an error to try
2535** to start a new subtransaction if another subtransaction is already active.
2536**
2537** Statement subtransactions are used around individual SQL statements
2538** that are contained within a BEGIN...COMMIT block. If a constraint
2539** error occurs within the statement, the effect of that one statement
2540** can be rolled back without having to rollback the entire transaction.
2541*/
2542int sqlite3BtreeBeginStmt(Btree *p){
2543 int rc;
2544 BtShared *pBt = p->pBt;
2545 sqlite3BtreeEnter(p);
2546 if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
2547 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
2548 }else{
2549 assert( pBt->inTransaction==TRANS_WRITE );
2550 rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
2551 pBt->inStmt = 1;
2552 }
2553 sqlite3BtreeLeave(p);
2554 return rc;
2555}
2556
2557
2558/*
2559** Commit the statment subtransaction currently in progress. If no
2560** subtransaction is active, this is a no-op.
2561*/
2562int sqlite3BtreeCommitStmt(Btree *p){
2563 int rc;
2564 BtShared *pBt = p->pBt;
2565 sqlite3BtreeEnter(p);
2566 if( pBt->inStmt && !pBt->readOnly ){
2567 rc = sqlite3PagerStmtCommit(pBt->pPager);
2568 }else{
2569 rc = SQLITE_OK;
2570 }
2571 pBt->inStmt = 0;
2572 sqlite3BtreeLeave(p);
2573 return rc;
2574}
2575
2576/*
2577** Rollback the active statement subtransaction. If no subtransaction
2578** is active this routine is a no-op.
2579**
2580** All cursors will be invalidated by this operation. Any attempt
2581** to use a cursor that was open at the beginning of this operation
2582** will result in an error.
2583*/
2584int sqlite3BtreeRollbackStmt(Btree *p){
2585 int rc = SQLITE_OK;
2586 BtShared *pBt = p->pBt;
2587 sqlite3BtreeEnter(p);
2588 if( pBt->inStmt && !pBt->readOnly ){
2589 rc = sqlite3PagerStmtRollback(pBt->pPager);
2590 assert( countWriteCursors(pBt)==0 );
2591 pBt->inStmt = 0;
2592 }
2593 sqlite3BtreeLeave(p);
2594 return rc;
2595}
2596
2597/*
2598** Default key comparison function to be used if no comparison function
2599** is specified on the sqlite3BtreeCursor() call.
2600*/
2601static int dfltCompare(
2602 void *NotUsed, /* User data is not used */
2603 int n1, const void *p1, /* First key to compare */
2604 int n2, const void *p2 /* Second key to compare */
2605){
2606 int c;
2607 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
2608 if( c==0 ){
2609 c = n1 - n2;
2610 }
2611 return c;
2612}
2613
2614/*
2615** Create a new cursor for the BTree whose root is on the page
2616** iTable. The act of acquiring a cursor gets a read lock on
2617** the database file.
2618**
2619** If wrFlag==0, then the cursor can only be used for reading.
2620** If wrFlag==1, then the cursor can be used for reading or for
2621** writing if other conditions for writing are also met. These
2622** are the conditions that must be met in order for writing to
2623** be allowed:
2624**
2625** 1: The cursor must have been opened with wrFlag==1
2626**
2627** 2: Other database connections that share the same pager cache
2628** but which are not in the READ_UNCOMMITTED state may not have
2629** cursors open with wrFlag==0 on the same table. Otherwise
2630** the changes made by this write cursor would be visible to
2631** the read cursors in the other database connection.
2632**
2633** 3: The database must be writable (not on read-only media)
2634**
2635** 4: There must be an active transaction.
2636**
2637** No checking is done to make sure that page iTable really is the
2638** root page of a b-tree. If it is not, then the cursor acquired
2639** will not work correctly.
2640**
2641** The comparison function must be logically the same for every cursor
2642** on a particular table. Changing the comparison function will result
2643** in incorrect operations. If the comparison function is NULL, a
2644** default comparison function is used. The comparison function is
2645** always ignored for INTKEY tables.
2646*/
2647static int btreeCursor(
2648 Btree *p, /* The btree */
2649 int iTable, /* Root page of table to open */
2650 int wrFlag, /* 1 to write. 0 read-only */
2651 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2652 void *pArg, /* First arg to xCompare() */
2653 BtCursor **ppCur /* Write new cursor here */
2654){
2655 int rc;
2656 BtCursor *pCur;
2657 BtShared *pBt = p->pBt;
2658
2659 assert( sqlite3BtreeHoldsMutex(p) );
2660 *ppCur = 0;
2661 if( wrFlag ){
2662 if( pBt->readOnly ){
2663 return SQLITE_READONLY;
2664 }
2665 if( checkReadLocks(p, iTable, 0) ){
2666 return SQLITE_LOCKED;
2667 }
2668 }
2669
2670 if( pBt->pPage1==0 ){
2671 rc = lockBtreeWithRetry(p);
2672 if( rc!=SQLITE_OK ){
2673 return rc;
2674 }
2675 if( pBt->readOnly && wrFlag ){
2676 return SQLITE_READONLY;
2677 }
2678 }
2679 pCur = sqlite3MallocZero( sizeof(*pCur) );
2680 if( pCur==0 ){
2681 rc = SQLITE_NOMEM;
2682 goto create_cursor_exception;
2683 }
2684 pCur->pgnoRoot = (Pgno)iTable;
2685 if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
2686 rc = SQLITE_EMPTY;
2687 goto create_cursor_exception;
2688 }
2689 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
2690 if( rc!=SQLITE_OK ){
2691 goto create_cursor_exception;
2692 }
2693
2694 /* Now that no other errors can occur, finish filling in the BtCursor
2695 ** variables, link the cursor into the BtShared list and set *ppCur (the
2696 ** output argument to this function).
2697 */
2698 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2699 pCur->pArg = pArg;
2700 pCur->pBtree = p;
2701 pCur->pBt = pBt;
2702 pCur->wrFlag = wrFlag;
2703 pCur->pNext = pBt->pCursor;
2704 if( pCur->pNext ){
2705 pCur->pNext->pPrev = pCur;
2706 }
2707 pBt->pCursor = pCur;
2708 pCur->eState = CURSOR_INVALID;
2709 *ppCur = pCur;
2710
2711 return SQLITE_OK;
2712
2713create_cursor_exception:
2714 if( pCur ){
2715 releasePage(pCur->pPage);
2716 sqlite3_free(pCur);
2717 }
2718 unlockBtreeIfUnused(pBt);
2719 return rc;
2720}
2721int sqlite3BtreeCursor(
2722 Btree *p, /* The btree */
2723 int iTable, /* Root page of table to open */
2724 int wrFlag, /* 1 to write. 0 read-only */
2725 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2726 void *pArg, /* First arg to xCompare() */
2727 BtCursor **ppCur /* Write new cursor here */
2728){
2729 int rc;
2730 sqlite3BtreeEnter(p);
2731 rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur);
2732 sqlite3BtreeLeave(p);
2733 return rc;
2734}
2735
2736
2737/*
2738** Close a cursor. The read lock on the database file is released
2739** when the last cursor is closed.
2740*/
2741int sqlite3BtreeCloseCursor(BtCursor *pCur){
2742 BtShared *pBt = pCur->pBt;
2743 Btree *pBtree = pCur->pBtree;
2744
2745 sqlite3BtreeEnter(pBtree);
2746 clearCursorPosition(pCur);
2747 if( pCur->pPrev ){
2748 pCur->pPrev->pNext = pCur->pNext;
2749 }else{
2750 pBt->pCursor = pCur->pNext;
2751 }
2752 if( pCur->pNext ){
2753 pCur->pNext->pPrev = pCur->pPrev;
2754 }
2755 releasePage(pCur->pPage);
2756 unlockBtreeIfUnused(pBt);
2757 invalidateOverflowCache(pCur);
2758 sqlite3_free(pCur);
2759 sqlite3BtreeLeave(pBtree);
2760 return SQLITE_OK;
2761}
2762
2763/*
2764** Make a temporary cursor by filling in the fields of pTempCur.
2765** The temporary cursor is not on the cursor list for the Btree.
2766*/
2767void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
2768 assert( cursorHoldsMutex(pCur) );
2769 memcpy(pTempCur, pCur, sizeof(*pCur));
2770 pTempCur->pNext = 0;
2771 pTempCur->pPrev = 0;
2772 if( pTempCur->pPage ){
2773 sqlite3PagerRef(pTempCur->pPage->pDbPage);
2774 }
2775}
2776
2777/*
2778** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
2779** function above.
2780*/
2781void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
2782 assert( cursorHoldsMutex(pCur) );
2783 if( pCur->pPage ){
2784 sqlite3PagerUnref(pCur->pPage->pDbPage);
2785 }
2786}
2787
2788/*
2789** Make sure the BtCursor* given in the argument has a valid
2790** BtCursor.info structure. If it is not already valid, call
2791** sqlite3BtreeParseCell() to fill it in.
2792**
2793** BtCursor.info is a cache of the information in the current cell.
2794** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
2795**
2796** 2007-06-25: There is a bug in some versions of MSVC that cause the
2797** compiler to crash when getCellInfo() is implemented as a macro.
2798** But there is a measureable speed advantage to using the macro on gcc
2799** (when less compiler optimizations like -Os or -O0 are used and the
2800** compiler is not doing agressive inlining.) So we use a real function
2801** for MSVC and a macro for everything else. Ticket #2457.
2802*/
2803#ifndef NDEBUG
2804 static void assertCellInfo(BtCursor *pCur){
2805 CellInfo info;
2806 memset(&info, 0, sizeof(info));
2807 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info);
2808 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
2809 }
2810#else
2811 #define assertCellInfo(x)
2812#endif
2813#ifdef _MSC_VER
2814 /* Use a real function in MSVC to work around bugs in that compiler. */
2815 static void getCellInfo(BtCursor *pCur){
2816 if( pCur->info.nSize==0 ){
2817 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);
2818 }else{
2819 assertCellInfo(pCur);
2820 }
2821 }
2822#else /* if not _MSC_VER */
2823 /* Use a macro in all other compilers so that the function is inlined */
2824#define getCellInfo(pCur) \
2825 if( pCur->info.nSize==0 ){ \
2826 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \
2827 }else{ \
2828 assertCellInfo(pCur); \
2829 }
2830#endif /* _MSC_VER */
2831
2832/*
2833** Set *pSize to the size of the buffer needed to hold the value of
2834** the key for the current entry. If the cursor is not pointing
2835** to a valid entry, *pSize is set to 0.
2836**
2837** For a table with the INTKEY flag set, this routine returns the key
2838** itself, not the number of bytes in the key.
2839*/
2840int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
2841 int rc;
2842
2843 assert( cursorHoldsMutex(pCur) );
2844 rc = restoreOrClearCursorPosition(pCur);
2845 if( rc==SQLITE_OK ){
2846 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2847 if( pCur->eState==CURSOR_INVALID ){
2848 *pSize = 0;
2849 }else{
2850 getCellInfo(pCur);
2851 *pSize = pCur->info.nKey;
2852 }
2853 }
2854 return rc;
2855}
2856
2857/*
2858** Set *pSize to the number of bytes of data in the entry the
2859** cursor currently points to. Always return SQLITE_OK.
2860** Failure is not possible. If the cursor is not currently
2861** pointing to an entry (which can happen, for example, if
2862** the database is empty) then *pSize is set to 0.
2863*/
2864int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
2865 int rc;
2866
2867 assert( cursorHoldsMutex(pCur) );
2868 rc = restoreOrClearCursorPosition(pCur);
2869 if( rc==SQLITE_OK ){
2870 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2871 if( pCur->eState==CURSOR_INVALID ){
2872 /* Not pointing at a valid entry - set *pSize to 0. */
2873 *pSize = 0;
2874 }else{
2875 getCellInfo(pCur);
2876 *pSize = pCur->info.nData;
2877 }
2878 }
2879 return rc;
2880}
2881
2882/*
2883** Given the page number of an overflow page in the database (parameter
2884** ovfl), this function finds the page number of the next page in the
2885** linked list of overflow pages. If possible, it uses the auto-vacuum
2886** pointer-map data instead of reading the content of page ovfl to do so.
2887**
2888** If an error occurs an SQLite error code is returned. Otherwise:
2889**
2890** Unless pPgnoNext is NULL, the page number of the next overflow
2891** page in the linked list is written to *pPgnoNext. If page ovfl
2892** is the last page in it's linked list, *pPgnoNext is set to zero.
2893**
2894** If ppPage is not NULL, *ppPage is set to the MemPage* handle
2895** for page ovfl. The underlying pager page may have been requested
2896** with the noContent flag set, so the page data accessable via
2897** this handle may not be trusted.
2898*/
2899static int getOverflowPage(
2900 BtShared *pBt,
2901 Pgno ovfl, /* Overflow page */
2902 MemPage **ppPage, /* OUT: MemPage handle */
2903 Pgno *pPgnoNext /* OUT: Next overflow page number */
2904){
2905 Pgno next = 0;
2906 int rc;
2907
2908 assert( sqlite3_mutex_held(pBt->mutex) );
2909 /* One of these must not be NULL. Otherwise, why call this function? */
2910 assert(ppPage || pPgnoNext);
2911
2912 /* If pPgnoNext is NULL, then this function is being called to obtain
2913 ** a MemPage* reference only. No page-data is required in this case.
2914 */
2915 if( !pPgnoNext ){
2916 return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
2917 }
2918
2919#ifndef SQLITE_OMIT_AUTOVACUUM
2920 /* Try to find the next page in the overflow list using the
2921 ** autovacuum pointer-map pages. Guess that the next page in
2922 ** the overflow list is page number (ovfl+1). If that guess turns
2923 ** out to be wrong, fall back to loading the data of page
2924 ** number ovfl to determine the next page number.
2925 */
2926 if( pBt->autoVacuum ){
2927 Pgno pgno;
2928 Pgno iGuess = ovfl+1;
2929 u8 eType;
2930
2931 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
2932 iGuess++;
2933 }
2934
2935 if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){
2936 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
2937 if( rc!=SQLITE_OK ){
2938 return rc;
2939 }
2940 if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
2941 next = iGuess;
2942 }
2943 }
2944 }
2945#endif
2946
2947 if( next==0 || ppPage ){
2948 MemPage *pPage = 0;
2949
2950 rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
2951 assert(rc==SQLITE_OK || pPage==0);
2952 if( next==0 && rc==SQLITE_OK ){
2953 next = get4byte(pPage->aData);
2954 }
2955
2956 if( ppPage ){
2957 *ppPage = pPage;
2958 }else{
2959 releasePage(pPage);
2960 }
2961 }
2962 *pPgnoNext = next;
2963
2964 return rc;
2965}
2966
2967/*
2968** Copy data from a buffer to a page, or from a page to a buffer.
2969**
2970** pPayload is a pointer to data stored on database page pDbPage.
2971** If argument eOp is false, then nByte bytes of data are copied
2972** from pPayload to the buffer pointed at by pBuf. If eOp is true,
2973** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
2974** of data are copied from the buffer pBuf to pPayload.
2975**
2976** SQLITE_OK is returned on success, otherwise an error code.
2977*/
2978static int copyPayload(
2979 void *pPayload, /* Pointer to page data */
2980 void *pBuf, /* Pointer to buffer */
2981 int nByte, /* Number of bytes to copy */
2982 int eOp, /* 0 -> copy from page, 1 -> copy to page */
2983 DbPage *pDbPage /* Page containing pPayload */
2984){
2985 if( eOp ){
2986 /* Copy data from buffer to page (a write operation) */
2987 int rc = sqlite3PagerWrite(pDbPage);
2988 if( rc!=SQLITE_OK ){
2989 return rc;
2990 }
2991 memcpy(pPayload, pBuf, nByte);
2992 }else{
2993 /* Copy data from page to buffer (a read operation) */
2994 memcpy(pBuf, pPayload, nByte);
2995 }
2996 return SQLITE_OK;
2997}
2998
2999/*
3000** This function is used to read or overwrite payload information
3001** for the entry that the pCur cursor is pointing to. If the eOp
3002** parameter is 0, this is a read operation (data copied into
3003** buffer pBuf). If it is non-zero, a write (data copied from
3004** buffer pBuf).
3005**
3006** A total of "amt" bytes are read or written beginning at "offset".
3007** Data is read to or from the buffer pBuf.
3008**
3009** This routine does not make a distinction between key and data.
3010** It just reads or writes bytes from the payload area. Data might
3011** appear on the main page or be scattered out on multiple overflow
3012** pages.
3013**
3014** If the BtCursor.isIncrblobHandle flag is set, and the current
3015** cursor entry uses one or more overflow pages, this function
3016** allocates space for and lazily popluates the overflow page-list
3017** cache array (BtCursor.aOverflow). Subsequent calls use this
3018** cache to make seeking to the supplied offset more efficient.
3019**
3020** Once an overflow page-list cache has been allocated, it may be
3021** invalidated if some other cursor writes to the same table, or if
3022** the cursor is moved to a different row. Additionally, in auto-vacuum
3023** mode, the following events may invalidate an overflow page-list cache.
3024**
3025** * An incremental vacuum,
3026** * A commit in auto_vacuum="full" mode,
3027** * Creating a table (may require moving an overflow page).
3028*/
3029static int accessPayload(
3030 BtCursor *pCur, /* Cursor pointing to entry to read from */
3031 int offset, /* Begin reading this far into payload */
3032 int amt, /* Read this many bytes */
3033 unsigned char *pBuf, /* Write the bytes into this buffer */
3034 int skipKey, /* offset begins at data if this is true */
3035 int eOp /* zero to read. non-zero to write. */
3036){
3037 unsigned char *aPayload;
3038 int rc = SQLITE_OK;
3039 u32 nKey;
3040 int iIdx = 0;
3041 MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */
3042 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
3043
3044 assert( pPage );
3045 assert( pCur->eState==CURSOR_VALID );
3046 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
3047 assert( offset>=0 );
3048 assert( cursorHoldsMutex(pCur) );
3049
3050 getCellInfo(pCur);
3051 aPayload = pCur->info.pCell + pCur->info.nHeader;
3052 nKey = (pPage->intKey ? 0 : pCur->info.nKey);
3053
3054 if( skipKey ){
3055 offset += nKey;
3056 }
3057 if( offset+amt > nKey+pCur->info.nData ){
3058 /* Trying to read or write past the end of the data is an error */
3059 return SQLITE_ERROR;
3060 }
3061
3062 /* Check if data must be read/written to/from the btree page itself. */
3063 if( offset<pCur->info.nLocal ){
3064 int a = amt;
3065 if( a+offset>pCur->info.nLocal ){
3066 a = pCur->info.nLocal - offset;
3067 }
3068 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
3069 offset = 0;
3070 pBuf += a;
3071 amt -= a;
3072 }else{
3073 offset -= pCur->info.nLocal;
3074 }
3075
3076 if( rc==SQLITE_OK && amt>0 ){
3077 const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
3078 Pgno nextPage;
3079
3080 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
3081
3082#ifndef SQLITE_OMIT_INCRBLOB
3083 /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
3084 ** has not been allocated, allocate it now. The array is sized at
3085 ** one entry for each overflow page in the overflow chain. The
3086 ** page number of the first overflow page is stored in aOverflow[0],
3087 ** etc. A value of 0 in the aOverflow[] array means "not yet known"
3088 ** (the cache is lazily populated).
3089 */
3090 if( pCur->isIncrblobHandle && !pCur->aOverflow ){
3091 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
3092 pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
3093 if( nOvfl && !pCur->aOverflow ){
3094 rc = SQLITE_NOMEM;
3095 }
3096 }
3097
3098 /* If the overflow page-list cache has been allocated and the
3099 ** entry for the first required overflow page is valid, skip
3100 ** directly to it.
3101 */
3102 if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
3103 iIdx = (offset/ovflSize);
3104 nextPage = pCur->aOverflow[iIdx];
3105 offset = (offset%ovflSize);
3106 }
3107#endif
3108
3109 for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
3110
3111#ifndef SQLITE_OMIT_INCRBLOB
3112 /* If required, populate the overflow page-list cache. */
3113 if( pCur->aOverflow ){
3114 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
3115 pCur->aOverflow[iIdx] = nextPage;
3116 }
3117#endif
3118
3119 if( offset>=ovflSize ){
3120 /* The only reason to read this page is to obtain the page
3121 ** number for the next page in the overflow chain. The page
3122 ** data is not required. So first try to lookup the overflow
3123 ** page-list cache, if any, then fall back to the getOverflowPage()
3124 ** function.
3125 */
3126#ifndef SQLITE_OMIT_INCRBLOB
3127 if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
3128 nextPage = pCur->aOverflow[iIdx+1];
3129 } else
3130#endif
3131 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
3132 offset -= ovflSize;
3133 }else{
3134 /* Need to read this page properly. It contains some of the
3135 ** range of data that is being read (eOp==0) or written (eOp!=0).
3136 */
3137 DbPage *pDbPage;
3138 int a = amt;
3139 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
3140 if( rc==SQLITE_OK ){
3141 aPayload = sqlite3PagerGetData(pDbPage);
3142 nextPage = get4byte(aPayload);
3143 if( a + offset > ovflSize ){
3144 a = ovflSize - offset;
3145 }
3146 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
3147 sqlite3PagerUnref(pDbPage);
3148 offset = 0;
3149 amt -= a;
3150 pBuf += a;
3151 }
3152 }
3153 }
3154 }
3155
3156 if( rc==SQLITE_OK && amt>0 ){
3157 return SQLITE_CORRUPT_BKPT;
3158 }
3159 return rc;
3160}
3161
3162/*
3163** Read part of the key associated with cursor pCur. Exactly
3164** "amt" bytes will be transfered into pBuf[]. The transfer
3165** begins at "offset".
3166**
3167** Return SQLITE_OK on success or an error code if anything goes
3168** wrong. An error is returned if "offset+amt" is larger than
3169** the available payload.
3170*/
3171int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
3172 int rc;
3173
3174 assert( cursorHoldsMutex(pCur) );
3175 rc = restoreOrClearCursorPosition(pCur);
3176 if( rc==SQLITE_OK ){
3177 assert( pCur->eState==CURSOR_VALID );
3178 assert( pCur->pPage!=0 );
3179 if( pCur->pPage->intKey ){
3180 return SQLITE_CORRUPT_BKPT;
3181 }
3182 assert( pCur->pPage->intKey==0 );
3183 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3184 rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
3185 }
3186 return rc;
3187}
3188
3189/*
3190** Read part of the data associated with cursor pCur. Exactly
3191** "amt" bytes will be transfered into pBuf[]. The transfer
3192** begins at "offset".
3193**
3194** Return SQLITE_OK on success or an error code if anything goes
3195** wrong. An error is returned if "offset+amt" is larger than
3196** the available payload.
3197*/
3198int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
3199 int rc;
3200
3201 assert( cursorHoldsMutex(pCur) );
3202 rc = restoreOrClearCursorPosition(pCur);
3203 if( rc==SQLITE_OK ){
3204 assert( pCur->eState==CURSOR_VALID );
3205 assert( pCur->pPage!=0 );
3206 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3207 rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
3208 }
3209 return rc;
3210}
3211
3212/*
3213** Return a pointer to payload information from the entry that the
3214** pCur cursor is pointing to. The pointer is to the beginning of
3215** the key if skipKey==0 and it points to the beginning of data if
3216** skipKey==1. The number of bytes of available key/data is written
3217** into *pAmt. If *pAmt==0, then the value returned will not be
3218** a valid pointer.
3219**
3220** This routine is an optimization. It is common for the entire key
3221** and data to fit on the local page and for there to be no overflow
3222** pages. When that is so, this routine can be used to access the
3223** key and data without making a copy. If the key and/or data spills
3224** onto overflow pages, then accessPayload() must be used to reassembly
3225** the key/data and copy it into a preallocated buffer.
3226**
3227** The pointer returned by this routine looks directly into the cached
3228** page of the database. The data might change or move the next time
3229** any btree routine is called.
3230*/
3231static const unsigned char *fetchPayload(
3232 BtCursor *pCur, /* Cursor pointing to entry to read from */
3233 int *pAmt, /* Write the number of available bytes here */
3234 int skipKey /* read beginning at data if this is true */
3235){
3236 unsigned char *aPayload;
3237 MemPage *pPage;
3238 u32 nKey;
3239 int nLocal;
3240
3241 assert( pCur!=0 && pCur->pPage!=0 );
3242 assert( pCur->eState==CURSOR_VALID );
3243 assert( cursorHoldsMutex(pCur) );
3244 pPage = pCur->pPage;
3245 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
3246 getCellInfo(pCur);
3247 aPayload = pCur->info.pCell;
3248 aPayload += pCur->info.nHeader;
3249 if( pPage->intKey ){
3250 nKey = 0;
3251 }else{
3252 nKey = pCur->info.nKey;
3253 }
3254 if( skipKey ){
3255 aPayload += nKey;
3256 nLocal = pCur->info.nLocal - nKey;
3257 }else{
3258 nLocal = pCur->info.nLocal;
3259 if( nLocal>nKey ){
3260 nLocal = nKey;
3261 }
3262 }
3263 *pAmt = nLocal;
3264 return aPayload;
3265}
3266
3267
3268/*
3269** For the entry that cursor pCur is point to, return as
3270** many bytes of the key or data as are available on the local
3271** b-tree page. Write the number of available bytes into *pAmt.
3272**
3273** The pointer returned is ephemeral. The key/data may move
3274** or be destroyed on the next call to any Btree routine,
3275** including calls from other threads against the same cache.
3276** Hence, a mutex on the BtShared should be held prior to calling
3277** this routine.
3278**
3279** These routines is used to get quick access to key and data
3280** in the common case where no overflow pages are used.
3281*/
3282const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
3283 assert( cursorHoldsMutex(pCur) );
3284 if( pCur->eState==CURSOR_VALID ){
3285 return (const void*)fetchPayload(pCur, pAmt, 0);
3286 }
3287 return 0;
3288}
3289const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
3290 assert( cursorHoldsMutex(pCur) );
3291 if( pCur->eState==CURSOR_VALID ){
3292 return (const void*)fetchPayload(pCur, pAmt, 1);
3293 }
3294 return 0;
3295}
3296
3297
3298/*
3299** Move the cursor down to a new child page. The newPgno argument is the
3300** page number of the child page to move to.
3301*/
3302static int moveToChild(BtCursor *pCur, u32 newPgno){
3303 int rc;
3304 MemPage *pNewPage;
3305 MemPage *pOldPage;
3306 BtShared *pBt = pCur->pBt;
3307
3308 assert( cursorHoldsMutex(pCur) );
3309 assert( pCur->eState==CURSOR_VALID );
3310 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
3311 if( rc ) return rc;
3312 pNewPage->idxParent = pCur->idx;
3313 pOldPage = pCur->pPage;
3314 pOldPage->idxShift = 0;
3315 releasePage(pOldPage);
3316 pCur->pPage = pNewPage;
3317 pCur->idx = 0;
3318 pCur->info.nSize = 0;
3319 if( pNewPage->nCell<1 ){
3320 return SQLITE_CORRUPT_BKPT;
3321 }
3322 return SQLITE_OK;
3323}
3324
3325/*
3326** Return true if the page is the virtual root of its table.
3327**
3328** The virtual root page is the root page for most tables. But
3329** for the table rooted on page 1, sometime the real root page
3330** is empty except for the right-pointer. In such cases the
3331** virtual root page is the page that the right-pointer of page
3332** 1 is pointing to.
3333*/
3334int sqlite3BtreeIsRootPage(MemPage *pPage){
3335 MemPage *pParent;
3336
3337 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3338 pParent = pPage->pParent;
3339 if( pParent==0 ) return 1;
3340 if( pParent->pgno>1 ) return 0;
3341 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
3342 return 0;
3343}
3344
3345/*
3346** Move the cursor up to the parent page.
3347**
3348** pCur->idx is set to the cell index that contains the pointer
3349** to the page we are coming from. If we are coming from the
3350** right-most child page then pCur->idx is set to one more than
3351** the largest cell index.
3352*/
3353void sqlite3BtreeMoveToParent(BtCursor *pCur){
3354 MemPage *pParent;
3355 MemPage *pPage;
3356 int idxParent;
3357
3358 assert( cursorHoldsMutex(pCur) );
3359 assert( pCur->eState==CURSOR_VALID );
3360 pPage = pCur->pPage;
3361 assert( pPage!=0 );
3362 assert( !sqlite3BtreeIsRootPage(pPage) );
3363 pParent = pPage->pParent;
3364 assert( pParent!=0 );
3365 idxParent = pPage->idxParent;
3366 sqlite3PagerRef(pParent->pDbPage);
3367 releasePage(pPage);
3368 pCur->pPage = pParent;
3369 pCur->info.nSize = 0;
3370 assert( pParent->idxShift==0 );
3371 pCur->idx = idxParent;
3372}
3373
3374/*
3375** Move the cursor to the root page
3376*/
3377static int moveToRoot(BtCursor *pCur){
3378 MemPage *pRoot;
3379 int rc = SQLITE_OK;
3380 Btree *p = pCur->pBtree;
3381 BtShared *pBt = p->pBt;
3382
3383 assert( cursorHoldsMutex(pCur) );
3384 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
3385 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
3386 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
3387 if( pCur->eState>=CURSOR_REQUIRESEEK ){
3388 if( pCur->eState==CURSOR_FAULT ){
3389 return pCur->skip;
3390 }
3391 clearCursorPosition(pCur);
3392 }
3393 pRoot = pCur->pPage;
3394 if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
3395 assert( pRoot->isInit );
3396 }else{
3397 if(
3398 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
3399 ){
3400 pCur->eState = CURSOR_INVALID;
3401 return rc;
3402 }
3403 releasePage(pCur->pPage);
3404 pCur->pPage = pRoot;
3405 }
3406 pCur->idx = 0;
3407 pCur->info.nSize = 0;
3408 if( pRoot->nCell==0 && !pRoot->leaf ){
3409 Pgno subpage;
3410 assert( pRoot->pgno==1 );
3411 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
3412 assert( subpage>0 );
3413 pCur->eState = CURSOR_VALID;
3414 rc = moveToChild(pCur, subpage);
3415 }
3416 pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
3417 return rc;
3418}
3419
3420/*
3421** Move the cursor down to the left-most leaf entry beneath the
3422** entry to which it is currently pointing.
3423**
3424** The left-most leaf is the one with the smallest key - the first
3425** in ascending order.
3426*/
3427static int moveToLeftmost(BtCursor *pCur){
3428 Pgno pgno;
3429 int rc = SQLITE_OK;
3430 MemPage *pPage;
3431
3432 assert( cursorHoldsMutex(pCur) );
3433 assert( pCur->eState==CURSOR_VALID );
3434 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
3435 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
3436 pgno = get4byte(findCell(pPage, pCur->idx));
3437 rc = moveToChild(pCur, pgno);
3438 }
3439 return rc;
3440}
3441
3442/*
3443** Move the cursor down to the right-most leaf entry beneath the
3444** page to which it is currently pointing. Notice the difference
3445** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3446** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3447** finds the right-most entry beneath the *page*.
3448**
3449** The right-most entry is the one with the largest key - the last
3450** key in ascending order.
3451*/
3452static int moveToRightmost(BtCursor *pCur){
3453 Pgno pgno;
3454 int rc = SQLITE_OK;
3455 MemPage *pPage;
3456
3457 assert( cursorHoldsMutex(pCur) );
3458 assert( pCur->eState==CURSOR_VALID );
3459 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
3460 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3461 pCur->idx = pPage->nCell;
3462 rc = moveToChild(pCur, pgno);
3463 }
3464 if( rc==SQLITE_OK ){
3465 pCur->idx = pPage->nCell - 1;
3466 pCur->info.nSize = 0;
3467 }
3468 return SQLITE_OK;
3469}
3470
3471/* Move the cursor to the first entry in the table. Return SQLITE_OK
3472** on success. Set *pRes to 0 if the cursor actually points to something
3473** or set *pRes to 1 if the table is empty.
3474*/
3475int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
3476 int rc;
3477
3478 assert( cursorHoldsMutex(pCur) );
3479 assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
3480 rc = moveToRoot(pCur);
3481 if( rc==SQLITE_OK ){
3482 if( pCur->eState==CURSOR_INVALID ){
3483 assert( pCur->pPage->nCell==0 );
3484 *pRes = 1;
3485 rc = SQLITE_OK;
3486 }else{
3487 assert( pCur->pPage->nCell>0 );
3488 *pRes = 0;
3489 rc = moveToLeftmost(pCur);
3490 }
3491 }
3492 return rc;
3493}
3494
3495/* Move the cursor to the last entry in the table. Return SQLITE_OK
3496** on success. Set *pRes to 0 if the cursor actually points to something
3497** or set *pRes to 1 if the table is empty.
3498*/
3499int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
3500 int rc;
3501
3502 assert( cursorHoldsMutex(pCur) );
3503 assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
3504 rc = moveToRoot(pCur);
3505 if( rc==SQLITE_OK ){
3506 if( CURSOR_INVALID==pCur->eState ){
3507 assert( pCur->pPage->nCell==0 );
3508 *pRes = 1;
3509 }else{
3510 assert( pCur->eState==CURSOR_VALID );
3511 *pRes = 0;
3512 rc = moveToRightmost(pCur);
3513 }
3514 }
3515 return rc;
3516}
3517
3518/* Move the cursor so that it points to an entry near pKey/nKey.
3519** Return a success code.
3520**
3521** For INTKEY tables, only the nKey parameter is used. pKey is
3522** ignored. For other tables, nKey is the number of bytes of data
3523** in pKey. The comparison function specified when the cursor was
3524** created is used to compare keys.
3525**
3526** If an exact match is not found, then the cursor is always
3527** left pointing at a leaf page which would hold the entry if it
3528** were present. The cursor might point to an entry that comes
3529** before or after the key.
3530**
3531** The result of comparing the key with the entry to which the
3532** cursor is written to *pRes if pRes!=NULL. The meaning of
3533** this value is as follows:
3534**
3535** *pRes<0 The cursor is left pointing at an entry that
3536** is smaller than pKey or if the table is empty
3537** and the cursor is therefore left point to nothing.
3538**
3539** *pRes==0 The cursor is left pointing at an entry that
3540** exactly matches pKey.
3541**
3542** *pRes>0 The cursor is left pointing at an entry that
3543** is larger than pKey.
3544**
3545*/
3546int sqlite3BtreeMoveto(
3547 BtCursor *pCur, /* The cursor to be moved */
3548 const void *pKey, /* The key content for indices. Not used by tables */
3549 i64 nKey, /* Size of pKey. Or the key for tables */
3550 int biasRight, /* If true, bias the search to the high end */
3551 int *pRes /* Search result flag */
3552){
3553 int rc;
3554
3555 assert( cursorHoldsMutex(pCur) );
3556 assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
3557 rc = moveToRoot(pCur);
3558 if( rc ){
3559 return rc;
3560 }
3561 assert( pCur->pPage );
3562 assert( pCur->pPage->isInit );
3563 if( pCur->eState==CURSOR_INVALID ){
3564 *pRes = -1;
3565 assert( pCur->pPage->nCell==0 );
3566 return SQLITE_OK;
3567 }
3568 for(;;){
3569 int lwr, upr;
3570 Pgno chldPg;
3571 MemPage *pPage = pCur->pPage;
3572 int c = -1; /* pRes return if table is empty must be -1 */
3573 lwr = 0;
3574 upr = pPage->nCell-1;
3575 if( !pPage->intKey && pKey==0 ){
3576 return SQLITE_CORRUPT_BKPT;
3577 }
3578 if( biasRight ){
3579 pCur->idx = upr;
3580 }else{
3581 pCur->idx = (upr+lwr)/2;
3582 }
3583 if( lwr<=upr ) for(;;){
3584 void *pCellKey;
3585 i64 nCellKey;
3586 pCur->info.nSize = 0;
3587 if( pPage->intKey ){
3588 u8 *pCell;
3589 pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
3590 if( pPage->hasData ){
3591 u32 dummy;
3592 pCell += getVarint32(pCell, &dummy);
3593 }
3594 getVarint(pCell, (u64 *)&nCellKey);
3595 if( nCellKey<nKey ){
3596 c = -1;
3597 }else if( nCellKey>nKey ){
3598 c = +1;
3599 }else{
3600 c = 0;
3601 }
3602 }else{
3603 int available;
3604 pCellKey = (void *)fetchPayload(pCur, &available, 0);
3605 nCellKey = pCur->info.nKey;
3606 if( available>=nCellKey ){
3607 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3608 }else{
3609 pCellKey = sqlite3_malloc( nCellKey );
3610 if( pCellKey==0 ) return SQLITE_NOMEM;
3611 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
3612 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3613 sqlite3_free(pCellKey);
3614 if( rc ){
3615 return rc;
3616 }
3617 }
3618 }
3619 if( c==0 ){
3620 if( pPage->leafData && !pPage->leaf ){
3621 lwr = pCur->idx;
3622 upr = lwr - 1;
3623 break;
3624 }else{
3625 if( pRes ) *pRes = 0;
3626 return SQLITE_OK;
3627 }
3628 }
3629 if( c<0 ){
3630 lwr = pCur->idx+1;
3631 }else{
3632 upr = pCur->idx-1;
3633 }
3634 if( lwr>upr ){
3635 break;
3636 }
3637 pCur->idx = (lwr+upr)/2;
3638 }
3639 assert( lwr==upr+1 );
3640 assert( pPage->isInit );
3641 if( pPage->leaf ){
3642 chldPg = 0;
3643 }else if( lwr>=pPage->nCell ){
3644 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3645 }else{
3646 chldPg = get4byte(findCell(pPage, lwr));
3647 }
3648 if( chldPg==0 ){
3649 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3650 if( pRes ) *pRes = c;
3651 return SQLITE_OK;
3652 }
3653 pCur->idx = lwr;
3654 pCur->info.nSize = 0;
3655 rc = moveToChild(pCur, chldPg);
3656 if( rc ){
3657 return rc;
3658 }
3659 }
3660 /* NOT REACHED */
3661}
3662
3663
3664/*
3665** Return TRUE if the cursor is not pointing at an entry of the table.
3666**
3667** TRUE will be returned after a call to sqlite3BtreeNext() moves
3668** past the last entry in the table or sqlite3BtreePrev() moves past
3669** the first entry. TRUE is also returned if the table is empty.
3670*/
3671int sqlite3BtreeEof(BtCursor *pCur){
3672 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
3673 ** have been deleted? This API will need to change to return an error code
3674 ** as well as the boolean result value.
3675 */
3676 return (CURSOR_VALID!=pCur->eState);
3677}
3678
3679/*
3680** Return the database connection handle for a cursor.
3681*/
3682sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
3683 assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
3684 return pCur->pBtree->pSqlite;
3685}
3686
3687/*
3688** Advance the cursor to the next entry in the database. If
3689** successful then set *pRes=0. If the cursor
3690** was already pointing to the last entry in the database before
3691** this routine was called, then set *pRes=1.
3692*/
3693static int btreeNext(BtCursor *pCur, int *pRes){
3694 int rc;
3695 MemPage *pPage;
3696
3697 assert( cursorHoldsMutex(pCur) );
3698 rc = restoreOrClearCursorPosition(pCur);
3699 if( rc!=SQLITE_OK ){
3700 return rc;
3701 }
3702 assert( pRes!=0 );
3703 pPage = pCur->pPage;
3704 if( CURSOR_INVALID==pCur->eState ){
3705 *pRes = 1;
3706 return SQLITE_OK;
3707 }
3708 if( pCur->skip>0 ){
3709 pCur->skip = 0;
3710 *pRes = 0;
3711 return SQLITE_OK;
3712 }
3713 pCur->skip = 0;
3714
3715 assert( pPage->isInit );
3716 assert( pCur->idx<pPage->nCell );
3717
3718 pCur->idx++;
3719 pCur->info.nSize = 0;
3720 if( pCur->idx>=pPage->nCell ){
3721 if( !pPage->leaf ){
3722 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
3723 if( rc ) return rc;
3724 rc = moveToLeftmost(pCur);
3725 *pRes = 0;
3726 return rc;
3727 }
3728 do{
3729 if( sqlite3BtreeIsRootPage(pPage) ){
3730 *pRes = 1;
3731 pCur->eState = CURSOR_INVALID;
3732 return SQLITE_OK;
3733 }
3734 sqlite3BtreeMoveToParent(pCur);
3735 pPage = pCur->pPage;
3736 }while( pCur->idx>=pPage->nCell );
3737 *pRes = 0;
3738 if( pPage->leafData ){
3739 rc = sqlite3BtreeNext(pCur, pRes);
3740 }else{
3741 rc = SQLITE_OK;
3742 }
3743 return rc;
3744 }
3745 *pRes = 0;
3746 if( pPage->leaf ){
3747 return SQLITE_OK;
3748 }
3749 rc = moveToLeftmost(pCur);
3750 return rc;
3751}
3752int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
3753 int rc;
3754 assert( cursorHoldsMutex(pCur) );
3755 rc = btreeNext(pCur, pRes);
3756 return rc;
3757}
3758
3759
3760/*
3761** Step the cursor to the back to the previous entry in the database. If
3762** successful then set *pRes=0. If the cursor
3763** was already pointing to the first entry in the database before
3764** this routine was called, then set *pRes=1.
3765*/
3766static int btreePrevious(BtCursor *pCur, int *pRes){
3767 int rc;
3768 Pgno pgno;
3769 MemPage *pPage;
3770
3771 assert( cursorHoldsMutex(pCur) );
3772 rc = restoreOrClearCursorPosition(pCur);
3773 if( rc!=SQLITE_OK ){
3774 return rc;
3775 }
3776 if( CURSOR_INVALID==pCur->eState ){
3777 *pRes = 1;
3778 return SQLITE_OK;
3779 }
3780 if( pCur->skip<0 ){
3781 pCur->skip = 0;
3782 *pRes = 0;
3783 return SQLITE_OK;
3784 }
3785 pCur->skip = 0;
3786
3787 pPage = pCur->pPage;
3788 assert( pPage->isInit );
3789 assert( pCur->idx>=0 );
3790 if( !pPage->leaf ){
3791 pgno = get4byte( findCell(pPage, pCur->idx) );
3792 rc = moveToChild(pCur, pgno);
3793 if( rc ){
3794 return rc;
3795 }
3796 rc = moveToRightmost(pCur);
3797 }else{
3798 while( pCur->idx==0 ){
3799 if( sqlite3BtreeIsRootPage(pPage) ){
3800 pCur->eState = CURSOR_INVALID;
3801 *pRes = 1;
3802 return SQLITE_OK;
3803 }
3804 sqlite3BtreeMoveToParent(pCur);
3805 pPage = pCur->pPage;
3806 }
3807 pCur->idx--;
3808 pCur->info.nSize = 0;
3809 if( pPage->leafData && !pPage->leaf ){
3810 rc = sqlite3BtreePrevious(pCur, pRes);
3811 }else{
3812 rc = SQLITE_OK;
3813 }
3814 }
3815 *pRes = 0;
3816 return rc;
3817}
3818int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
3819 int rc;
3820 assert( cursorHoldsMutex(pCur) );
3821 rc = btreePrevious(pCur, pRes);
3822 return rc;
3823}
3824
3825/*
3826** Allocate a new page from the database file.
3827**
3828** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
3829** has already been called on the new page.) The new page has also
3830** been referenced and the calling routine is responsible for calling
3831** sqlite3PagerUnref() on the new page when it is done.
3832**
3833** SQLITE_OK is returned on success. Any other return value indicates
3834** an error. *ppPage and *pPgno are undefined in the event of an error.
3835** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
3836**
3837** If the "nearby" parameter is not 0, then a (feeble) effort is made to
3838** locate a page close to the page number "nearby". This can be used in an
3839** attempt to keep related pages close to each other in the database file,
3840** which in turn can make database access faster.
3841**
3842** If the "exact" parameter is not 0, and the page-number nearby exists
3843** anywhere on the free-list, then it is guarenteed to be returned. This
3844** is only used by auto-vacuum databases when allocating a new table.
3845*/
3846static int allocateBtreePage(
3847 BtShared *pBt,
3848 MemPage **ppPage,
3849 Pgno *pPgno,
3850 Pgno nearby,
3851 u8 exact
3852){
3853 MemPage *pPage1;
3854 int rc;
3855 int n; /* Number of pages on the freelist */
3856 int k; /* Number of leaves on the trunk of the freelist */
3857 MemPage *pTrunk = 0;
3858 MemPage *pPrevTrunk = 0;
3859
3860 assert( sqlite3_mutex_held(pBt->mutex) );
3861 pPage1 = pBt->pPage1;
3862 n = get4byte(&pPage1->aData[36]);
3863 if( n>0 ){
3864 /* There are pages on the freelist. Reuse one of those pages. */
3865 Pgno iTrunk;
3866 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
3867
3868 /* If the 'exact' parameter was true and a query of the pointer-map
3869 ** shows that the page 'nearby' is somewhere on the free-list, then
3870 ** the entire-list will be searched for that page.
3871 */
3872#ifndef SQLITE_OMIT_AUTOVACUUM
3873 if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){
3874 u8 eType;
3875 assert( nearby>0 );
3876 assert( pBt->autoVacuum );
3877 rc = ptrmapGet(pBt, nearby, &eType, 0);
3878 if( rc ) return rc;
3879 if( eType==PTRMAP_FREEPAGE ){
3880 searchList = 1;
3881 }
3882 *pPgno = nearby;
3883 }
3884#endif
3885
3886 /* Decrement the free-list count by 1. Set iTrunk to the index of the
3887 ** first free-list trunk page. iPrevTrunk is initially 1.
3888 */
3889 rc = sqlite3PagerWrite(pPage1->pDbPage);
3890 if( rc ) return rc;
3891 put4byte(&pPage1->aData[36], n-1);
3892
3893 /* The code within this loop is run only once if the 'searchList' variable
3894 ** is not true. Otherwise, it runs once for each trunk-page on the
3895 ** free-list until the page 'nearby' is located.
3896 */
3897 do {
3898 pPrevTrunk = pTrunk;
3899 if( pPrevTrunk ){
3900 iTrunk = get4byte(&pPrevTrunk->aData[0]);
3901 }else{
3902 iTrunk = get4byte(&pPage1->aData[32]);
3903 }
3904 rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
3905 if( rc ){
3906 pTrunk = 0;
3907 goto end_allocate_page;
3908 }
3909
3910 k = get4byte(&pTrunk->aData[4]);
3911 if( k==0 && !searchList ){
3912 /* The trunk has no leaves and the list is not being searched.
3913 ** So extract the trunk page itself and use it as the newly
3914 ** allocated page */
3915 assert( pPrevTrunk==0 );
3916 rc = sqlite3PagerWrite(pTrunk->pDbPage);
3917 if( rc ){
3918 goto end_allocate_page;
3919 }
3920 *pPgno = iTrunk;
3921 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3922 *ppPage = pTrunk;
3923 pTrunk = 0;
3924 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3925 }else if( k>pBt->usableSize/4 - 8 ){
3926 /* Value of k is out of range. Database corruption */
3927 rc = SQLITE_CORRUPT_BKPT;
3928 goto end_allocate_page;
3929#ifndef SQLITE_OMIT_AUTOVACUUM
3930 }else if( searchList && nearby==iTrunk ){
3931 /* The list is being searched and this trunk page is the page
3932 ** to allocate, regardless of whether it has leaves.
3933 */
3934 assert( *pPgno==iTrunk );
3935 *ppPage = pTrunk;
3936 searchList = 0;
3937 rc = sqlite3PagerWrite(pTrunk->pDbPage);
3938 if( rc ){
3939 goto end_allocate_page;
3940 }
3941 if( k==0 ){
3942 if( !pPrevTrunk ){
3943 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3944 }else{
3945 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
3946 }
3947 }else{
3948 /* The trunk page is required by the caller but it contains
3949 ** pointers to free-list leaves. The first leaf becomes a trunk
3950 ** page in this case.
3951 */
3952 MemPage *pNewTrunk;
3953 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
3954 rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
3955 if( rc!=SQLITE_OK ){
3956 goto end_allocate_page;
3957 }
3958 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
3959 if( rc!=SQLITE_OK ){
3960 releasePage(pNewTrunk);
3961 goto end_allocate_page;
3962 }
3963 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
3964 put4byte(&pNewTrunk->aData[4], k-1);
3965 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
3966 releasePage(pNewTrunk);
3967 if( !pPrevTrunk ){
3968 put4byte(&pPage1->aData[32], iNewTrunk);
3969 }else{
3970 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
3971 if( rc ){
3972 goto end_allocate_page;
3973 }
3974 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
3975 }
3976 }
3977 pTrunk = 0;
3978 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3979#endif
3980 }else{
3981 /* Extract a leaf from the trunk */
3982 int closest;
3983 Pgno iPage;
3984 unsigned char *aData = pTrunk->aData;
3985 rc = sqlite3PagerWrite(pTrunk->pDbPage);
3986 if( rc ){
3987 goto end_allocate_page;
3988 }
3989 if( nearby>0 ){
3990 int i, dist;
3991 closest = 0;
3992 dist = get4byte(&aData[8]) - nearby;
3993 if( dist<0 ) dist = -dist;
3994 for(i=1; i<k; i++){
3995 int d2 = get4byte(&aData[8+i*4]) - nearby;
3996 if( d2<0 ) d2 = -d2;
3997 if( d2<dist ){
3998 closest = i;
3999 dist = d2;
4000 }
4001 }
4002 }else{
4003 closest = 0;
4004 }
4005
4006 iPage = get4byte(&aData[8+closest*4]);
4007 if( !searchList || iPage==nearby ){
4008 *pPgno = iPage;
4009 if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
4010 /* Free page off the end of the file */
4011 return SQLITE_CORRUPT_BKPT;
4012 }
4013 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
4014 ": %d more free pages\n",
4015 *pPgno, closest+1, k, pTrunk->pgno, n-1));
4016 if( closest<k-1 ){
4017 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
4018 }
4019 put4byte(&aData[4], k-1);
4020 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
4021 if( rc==SQLITE_OK ){
4022 sqlite3PagerDontRollback((*ppPage)->pDbPage);
4023 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
4024 if( rc!=SQLITE_OK ){
4025 releasePage(*ppPage);
4026 }
4027 }
4028 searchList = 0;
4029 }
4030 }
4031 releasePage(pPrevTrunk);
4032 pPrevTrunk = 0;
4033 }while( searchList );
4034 }else{
4035 /* There are no pages on the freelist, so create a new page at the
4036 ** end of the file */
4037 *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
4038
4039#ifndef SQLITE_OMIT_AUTOVACUUM
4040 if( pBt->nTrunc ){
4041 /* An incr-vacuum has already run within this transaction. So the
4042 ** page to allocate is not from the physical end of the file, but
4043 ** at pBt->nTrunc.
4044 */
4045 *pPgno = pBt->nTrunc+1;
4046 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
4047 (*pPgno)++;
4048 }
4049 }
4050 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
4051 /* If *pPgno refers to a pointer-map page, allocate two new pages
4052 ** at the end of the file instead of one. The first allocated page
4053 ** becomes a new pointer-map page, the second is used by the caller.
4054 */
4055 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
4056 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
4057 (*pPgno)++;
4058 }
4059 if( pBt->nTrunc ){
4060 pBt->nTrunc = *pPgno;
4061 }
4062#endif
4063
4064 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
4065 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
4066 if( rc ) return rc;
4067 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
4068 if( rc!=SQLITE_OK ){
4069 releasePage(*ppPage);
4070 }
4071 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
4072 }
4073
4074 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
4075
4076end_allocate_page:
4077 releasePage(pTrunk);
4078 releasePage(pPrevTrunk);
4079 return rc;
4080}
4081
4082/*
4083** Add a page of the database file to the freelist.
4084**
4085** sqlite3PagerUnref() is NOT called for pPage.
4086*/
4087static int freePage(MemPage *pPage){
4088 BtShared *pBt = pPage->pBt;
4089 MemPage *pPage1 = pBt->pPage1;
4090 int rc, n, k;
4091
4092 /* Prepare the page for freeing */
4093 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4094 assert( pPage->pgno>1 );
4095 pPage->isInit = 0;
4096 releasePage(pPage->pParent);
4097 pPage->pParent = 0;
4098
4099 /* Increment the free page count on pPage1 */
4100 rc = sqlite3PagerWrite(pPage1->pDbPage);
4101 if( rc ) return rc;
4102 n = get4byte(&pPage1->aData[36]);
4103 put4byte(&pPage1->aData[36], n+1);
4104
4105#ifdef SQLITE_SECURE_DELETE
4106 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
4107 ** always fully overwrite deleted information with zeros.
4108 */
4109 rc = sqlite3PagerWrite(pPage->pDbPage);
4110 if( rc ) return rc;
4111 memset(pPage->aData, 0, pPage->pBt->pageSize);
4112#endif
4113
4114#ifndef SQLITE_OMIT_AUTOVACUUM
4115 /* If the database supports auto-vacuum, write an entry in the pointer-map
4116 ** to indicate that the page is free.
4117 */
4118 if( pBt->autoVacuum ){
4119 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
4120 if( rc ) return rc;
4121 }
4122#endif
4123
4124 if( n==0 ){
4125 /* This is the first free page */
4126 rc = sqlite3PagerWrite(pPage->pDbPage);
4127 if( rc ) return rc;
4128 memset(pPage->aData, 0, 8);
4129 put4byte(&pPage1->aData[32], pPage->pgno);
4130 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
4131 }else{
4132 /* Other free pages already exist. Retrive the first trunk page
4133 ** of the freelist and find out how many leaves it has. */
4134 MemPage *pTrunk;
4135 rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
4136 if( rc ) return rc;
4137 k = get4byte(&pTrunk->aData[4]);
4138 if( k>=pBt->usableSize/4 - 8 ){
4139 /* The trunk is full. Turn the page being freed into a new
4140 ** trunk page with no leaves. */
4141 rc = sqlite3PagerWrite(pPage->pDbPage);
4142 if( rc==SQLITE_OK ){
4143 put4byte(pPage->aData, pTrunk->pgno);
4144 put4byte(&pPage->aData[4], 0);
4145 put4byte(&pPage1->aData[32], pPage->pgno);
4146 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
4147 pPage->pgno, pTrunk->pgno));
4148 }
4149 }else if( k<0 ){
4150 rc = SQLITE_CORRUPT;
4151 }else{
4152 /* Add the newly freed page as a leaf on the current trunk */
4153 rc = sqlite3PagerWrite(pTrunk->pDbPage);
4154 if( rc==SQLITE_OK ){
4155 put4byte(&pTrunk->aData[4], k+1);
4156 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
4157#ifndef SQLITE_SECURE_DELETE
4158 sqlite3PagerDontWrite(pPage->pDbPage);
4159#endif
4160 }
4161 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
4162 }
4163 releasePage(pTrunk);
4164 }
4165 return rc;
4166}
4167
4168/*
4169** Free any overflow pages associated with the given Cell.
4170*/
4171static int clearCell(MemPage *pPage, unsigned char *pCell){
4172 BtShared *pBt = pPage->pBt;
4173 CellInfo info;
4174 Pgno ovflPgno;
4175 int rc;
4176 int nOvfl;
4177 int ovflPageSize;
4178
4179 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4180 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
4181 if( info.iOverflow==0 ){
4182 return SQLITE_OK; /* No overflow pages. Return without doing anything */
4183 }
4184 ovflPgno = get4byte(&pCell[info.iOverflow]);
4185 ovflPageSize = pBt->usableSize - 4;
4186 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
4187 assert( ovflPgno==0 || nOvfl>0 );
4188 while( nOvfl-- ){
4189 MemPage *pOvfl;
4190 if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
4191 return SQLITE_CORRUPT_BKPT;
4192 }
4193
4194 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
4195 if( rc ) return rc;
4196 rc = freePage(pOvfl);
4197 sqlite3PagerUnref(pOvfl->pDbPage);
4198 if( rc ) return rc;
4199 }
4200 return SQLITE_OK;
4201}
4202
4203/*
4204** Create the byte sequence used to represent a cell on page pPage
4205** and write that byte sequence into pCell[]. Overflow pages are
4206** allocated and filled in as necessary. The calling procedure
4207** is responsible for making sure sufficient space has been allocated
4208** for pCell[].
4209**
4210** Note that pCell does not necessary need to point to the pPage->aData
4211** area. pCell might point to some temporary storage. The cell will
4212** be constructed in this temporary area then copied into pPage->aData
4213** later.
4214*/
4215static int fillInCell(
4216 MemPage *pPage, /* The page that contains the cell */
4217 unsigned char *pCell, /* Complete text of the cell */
4218 const void *pKey, i64 nKey, /* The key */
4219 const void *pData,int nData, /* The data */
4220 int nZero, /* Extra zero bytes to append to pData */
4221 int *pnSize /* Write cell size here */
4222){
4223 int nPayload;
4224 const u8 *pSrc;
4225 int nSrc, n, rc;
4226 int spaceLeft;
4227 MemPage *pOvfl = 0;
4228 MemPage *pToRelease = 0;
4229 unsigned char *pPrior;
4230 unsigned char *pPayload;
4231 BtShared *pBt = pPage->pBt;
4232 Pgno pgnoOvfl = 0;
4233 int nHeader;
4234 CellInfo info;
4235
4236 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4237
4238 /* Fill in the header. */
4239 nHeader = 0;
4240 if( !pPage->leaf ){
4241 nHeader += 4;
4242 }
4243 if( pPage->hasData ){
4244 nHeader += putVarint(&pCell[nHeader], nData+nZero);
4245 }else{
4246 nData = nZero = 0;
4247 }
4248 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
4249 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
4250 assert( info.nHeader==nHeader );
4251 assert( info.nKey==nKey );
4252 assert( info.nData==nData+nZero );
4253
4254 /* Fill in the payload */
4255 nPayload = nData + nZero;
4256 if( pPage->intKey ){
4257 pSrc = pData;
4258 nSrc = nData;
4259 nData = 0;
4260 }else{
4261 nPayload += nKey;
4262 pSrc = pKey;
4263 nSrc = nKey;
4264 }
4265 *pnSize = info.nSize;
4266 spaceLeft = info.nLocal;
4267 pPayload = &pCell[nHeader];
4268 pPrior = &pCell[info.iOverflow];
4269
4270 while( nPayload>0 ){
4271 if( spaceLeft==0 ){
4272 int isExact = 0;
4273#ifndef SQLITE_OMIT_AUTOVACUUM
4274 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
4275 if( pBt->autoVacuum ){
4276 do{
4277 pgnoOvfl++;
4278 } while(
4279 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
4280 );
4281 if( pgnoOvfl>1 ){
4282 /* isExact = 1; */
4283 }
4284 }
4285#endif
4286 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact);
4287#ifndef SQLITE_OMIT_AUTOVACUUM
4288 /* If the database supports auto-vacuum, and the second or subsequent
4289 ** overflow page is being allocated, add an entry to the pointer-map
4290 ** for that page now.
4291 **
4292 ** If this is the first overflow page, then write a partial entry
4293 ** to the pointer-map. If we write nothing to this pointer-map slot,
4294 ** then the optimistic overflow chain processing in clearCell()
4295 ** may misinterpret the uninitialised values and delete the
4296 ** wrong pages from the database.
4297 */
4298 if( pBt->autoVacuum && rc==SQLITE_OK ){
4299 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
4300 rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
4301 if( rc ){
4302 releasePage(pOvfl);
4303 }
4304 }
4305#endif
4306 if( rc ){
4307 releasePage(pToRelease);
4308 return rc;
4309 }
4310 put4byte(pPrior, pgnoOvfl);
4311 releasePage(pToRelease);
4312 pToRelease = pOvfl;
4313 pPrior = pOvfl->aData;
4314 put4byte(pPrior, 0);
4315 pPayload = &pOvfl->aData[4];
4316 spaceLeft = pBt->usableSize - 4;
4317 }
4318 n = nPayload;
4319 if( n>spaceLeft ) n = spaceLeft;
4320 if( nSrc>0 ){
4321 if( n>nSrc ) n = nSrc;
4322 assert( pSrc );
4323 memcpy(pPayload, pSrc, n);
4324 }else{
4325 memset(pPayload, 0, n);
4326 }
4327 nPayload -= n;
4328 pPayload += n;
4329 pSrc += n;
4330 nSrc -= n;
4331 spaceLeft -= n;
4332 if( nSrc==0 ){
4333 nSrc = nData;
4334 pSrc = pData;
4335 }
4336 }
4337 releasePage(pToRelease);
4338 return SQLITE_OK;
4339}
4340
4341/*
4342** Change the MemPage.pParent pointer on the page whose number is
4343** given in the second argument so that MemPage.pParent holds the
4344** pointer in the third argument.
4345*/
4346static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
4347 MemPage *pThis;
4348 DbPage *pDbPage;
4349
4350 assert( sqlite3_mutex_held(pBt->mutex) );
4351 assert( pNewParent!=0 );
4352 if( pgno==0 ) return SQLITE_OK;
4353 assert( pBt->pPager!=0 );
4354 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
4355 if( pDbPage ){
4356 pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
4357 if( pThis->isInit ){
4358 assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
4359 if( pThis->pParent!=pNewParent ){
4360 if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
4361 pThis->pParent = pNewParent;
4362 sqlite3PagerRef(pNewParent->pDbPage);
4363 }
4364 pThis->idxParent = idx;
4365 }
4366 sqlite3PagerUnref(pDbPage);
4367 }
4368
4369#ifndef SQLITE_OMIT_AUTOVACUUM
4370 if( pBt->autoVacuum ){
4371 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
4372 }
4373#endif
4374 return SQLITE_OK;
4375}
4376
4377
4378
4379/*
4380** Change the pParent pointer of all children of pPage to point back
4381** to pPage.
4382**
4383** In other words, for every child of pPage, invoke reparentPage()
4384** to make sure that each child knows that pPage is its parent.
4385**
4386** This routine gets called after you memcpy() one page into
4387** another.
4388*/
4389static int reparentChildPages(MemPage *pPage){
4390 int i;
4391 BtShared *pBt = pPage->pBt;
4392 int rc = SQLITE_OK;
4393
4394 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4395 if( pPage->leaf ) return SQLITE_OK;
4396
4397 for(i=0; i<pPage->nCell; i++){
4398 u8 *pCell = findCell(pPage, i);
4399 if( !pPage->leaf ){
4400 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
4401 if( rc!=SQLITE_OK ) return rc;
4402 }
4403 }
4404 if( !pPage->leaf ){
4405 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
4406 pPage, i);
4407 pPage->idxShift = 0;
4408 }
4409 return rc;
4410}
4411
4412/*
4413** Remove the i-th cell from pPage. This routine effects pPage only.
4414** The cell content is not freed or deallocated. It is assumed that
4415** the cell content has been copied someplace else. This routine just
4416** removes the reference to the cell from pPage.
4417**
4418** "sz" must be the number of bytes in the cell.
4419*/
4420static void dropCell(MemPage *pPage, int idx, int sz){
4421 int i; /* Loop counter */
4422 int pc; /* Offset to cell content of cell being deleted */
4423 u8 *data; /* pPage->aData */
4424 u8 *ptr; /* Used to move bytes around within data[] */
4425
4426 assert( idx>=0 && idx<pPage->nCell );
4427 assert( sz==cellSize(pPage, idx) );
4428 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4429 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4430 data = pPage->aData;
4431 ptr = &data[pPage->cellOffset + 2*idx];
4432 pc = get2byte(ptr);
4433 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
4434 freeSpace(pPage, pc, sz);
4435 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4436 ptr[0] = ptr[2];
4437 ptr[1] = ptr[3];
4438 }
4439 pPage->nCell--;
4440 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4441 pPage->nFree += 2;
4442 pPage->idxShift = 1;
4443}
4444
4445/*
4446** Insert a new cell on pPage at cell index "i". pCell points to the
4447** content of the cell.
4448**
4449** If the cell content will fit on the page, then put it there. If it
4450** will not fit, then make a copy of the cell content into pTemp if
4451** pTemp is not null. Regardless of pTemp, allocate a new entry
4452** in pPage->aOvfl[] and make it point to the cell content (either
4453** in pTemp or the original pCell) and also record its index.
4454** Allocating a new entry in pPage->aCell[] implies that
4455** pPage->nOverflow is incremented.
4456**
4457** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4458** cell. The caller will overwrite them after this function returns. If
4459** nSkip is non-zero, then pCell may not point to an invalid memory location
4460** (but pCell+nSkip is always valid).
4461*/
4462static int insertCell(
4463 MemPage *pPage, /* Page into which we are copying */
4464 int i, /* New cell becomes the i-th cell of the page */
4465 u8 *pCell, /* Content of the new cell */
4466 int sz, /* Bytes of content in pCell */
4467 u8 *pTemp, /* Temp storage space for pCell, if needed */
4468 u8 nSkip /* Do not write the first nSkip bytes of the cell */
4469){
4470 int idx; /* Where to write new cell content in data[] */
4471 int j; /* Loop counter */
4472 int top; /* First byte of content for any cell in data[] */
4473 int end; /* First byte past the last cell pointer in data[] */
4474 int ins; /* Index in data[] where new cell pointer is inserted */
4475 int hdr; /* Offset into data[] of the page header */
4476 int cellOffset; /* Address of first cell pointer in data[] */
4477 u8 *data; /* The content of the whole page */
4478 u8 *ptr; /* Used for moving information around in data[] */
4479
4480 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
4481 assert( sz==cellSizePtr(pPage, pCell) );
4482 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4483 if( pPage->nOverflow || sz+2>pPage->nFree ){
4484 if( pTemp ){
4485 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
4486 pCell = pTemp;
4487 }
4488 j = pPage->nOverflow++;
4489 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
4490 pPage->aOvfl[j].pCell = pCell;
4491 pPage->aOvfl[j].idx = i;
4492 pPage->nFree = 0;
4493 }else{
4494 int rc = sqlite3PagerWrite(pPage->pDbPage);
4495 if( rc!=SQLITE_OK ){
4496 return rc;
4497 }
4498 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4499 data = pPage->aData;
4500 hdr = pPage->hdrOffset;
4501 top = get2byte(&data[hdr+5]);
4502 cellOffset = pPage->cellOffset;
4503 end = cellOffset + 2*pPage->nCell + 2;
4504 ins = cellOffset + 2*i;
4505 if( end > top - sz ){
4506 rc = defragmentPage(pPage);
4507 if( rc!=SQLITE_OK ) return rc;
4508 top = get2byte(&data[hdr+5]);
4509 assert( end + sz <= top );
4510 }
4511 idx = allocateSpace(pPage, sz);
4512 assert( idx>0 );
4513 assert( end <= get2byte(&data[hdr+5]) );
4514 pPage->nCell++;
4515 pPage->nFree -= 2;
4516 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
4517 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4518 ptr[0] = ptr[-2];
4519 ptr[1] = ptr[-1];
4520 }
4521 put2byte(&data[ins], idx);
4522 put2byte(&data[hdr+3], pPage->nCell);
4523 pPage->idxShift = 1;
4524#ifndef SQLITE_OMIT_AUTOVACUUM
4525 if( pPage->pBt->autoVacuum ){
4526 /* The cell may contain a pointer to an overflow page. If so, write
4527 ** the entry for the overflow page into the pointer map.
4528 */
4529 CellInfo info;
4530 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
4531 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
4532 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4533 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
4534 rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
4535 if( rc!=SQLITE_OK ) return rc;
4536 }
4537 }
4538#endif
4539 }
4540
4541 return SQLITE_OK;
4542}
4543
4544/*
4545** Add a list of cells to a page. The page should be initially empty.
4546** The cells are guaranteed to fit on the page.
4547*/
4548static void assemblePage(
4549 MemPage *pPage, /* The page to be assemblied */
4550 int nCell, /* The number of cells to add to this page */
4551 u8 **apCell, /* Pointers to cell bodies */
4552 int *aSize /* Sizes of the cells */
4553){
4554 int i; /* Loop counter */
4555 int totalSize; /* Total size of all cells */
4556 int hdr; /* Index of page header */
4557 int cellptr; /* Address of next cell pointer */
4558 int cellbody; /* Address of next cell body */
4559 u8 *data; /* Data for the page */
4560
4561 assert( pPage->nOverflow==0 );
4562 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4563 totalSize = 0;
4564 for(i=0; i<nCell; i++){
4565 totalSize += aSize[i];
4566 }
4567 assert( totalSize+2*nCell<=pPage->nFree );
4568 assert( pPage->nCell==0 );
4569 cellptr = pPage->cellOffset;
4570 data = pPage->aData;
4571 hdr = pPage->hdrOffset;
4572 put2byte(&data[hdr+3], nCell);
4573 if( nCell ){
4574 cellbody = allocateSpace(pPage, totalSize);
4575 assert( cellbody>0 );
4576 assert( pPage->nFree >= 2*nCell );
4577 pPage->nFree -= 2*nCell;
4578 for(i=0; i<nCell; i++){
4579 put2byte(&data[cellptr], cellbody);
4580 memcpy(&data[cellbody], apCell[i], aSize[i]);
4581 cellptr += 2;
4582 cellbody += aSize[i];
4583 }
4584 assert( cellbody==pPage->pBt->usableSize );
4585 }
4586 pPage->nCell = nCell;
4587}
4588
4589/*
4590** The following parameters determine how many adjacent pages get involved
4591** in a balancing operation. NN is the number of neighbors on either side
4592** of the page that participate in the balancing operation. NB is the
4593** total number of pages that participate, including the target page and
4594** NN neighbors on either side.
4595**
4596** The minimum value of NN is 1 (of course). Increasing NN above 1
4597** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4598** in exchange for a larger degradation in INSERT and UPDATE performance.
4599** The value of NN appears to give the best results overall.
4600*/
4601#define NN 1 /* Number of neighbors on either side of pPage */
4602#define NB (NN*2+1) /* Total pages involved in the balance */
4603
4604/* Forward reference */
4605static int balance(MemPage*, int);
4606
4607#ifndef SQLITE_OMIT_QUICKBALANCE
4608/*
4609** This version of balance() handles the common special case where
4610** a new entry is being inserted on the extreme right-end of the
4611** tree, in other words, when the new entry will become the largest
4612** entry in the tree.
4613**
4614** Instead of trying balance the 3 right-most leaf pages, just add
4615** a new page to the right-hand side and put the one new entry in
4616** that page. This leaves the right side of the tree somewhat
4617** unbalanced. But odds are that we will be inserting new entries
4618** at the end soon afterwards so the nearly empty page will quickly
4619** fill up. On average.
4620**
4621** pPage is the leaf page which is the right-most page in the tree.
4622** pParent is its parent. pPage must have a single overflow entry
4623** which is also the right-most entry on the page.
4624*/
4625static int balance_quick(MemPage *pPage, MemPage *pParent){
4626 int rc;
4627 MemPage *pNew;
4628 Pgno pgnoNew;
4629 u8 *pCell;
4630 int szCell;
4631 CellInfo info;
4632 BtShared *pBt = pPage->pBt;
4633 int parentIdx = pParent->nCell; /* pParent new divider cell index */
4634 int parentSize; /* Size of new divider cell */
4635 u8 parentCell[64]; /* Space for the new divider cell */
4636
4637 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4638
4639 /* Allocate a new page. Insert the overflow cell from pPage
4640 ** into it. Then remove the overflow cell from pPage.
4641 */
4642 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
4643 if( rc!=SQLITE_OK ){
4644 return rc;
4645 }
4646 pCell = pPage->aOvfl[0].pCell;
4647 szCell = cellSizePtr(pPage, pCell);
4648 zeroPage(pNew, pPage->aData[0]);
4649 assemblePage(pNew, 1, &pCell, &szCell);
4650 pPage->nOverflow = 0;
4651
4652 /* Set the parent of the newly allocated page to pParent. */
4653 pNew->pParent = pParent;
4654 sqlite3PagerRef(pParent->pDbPage);
4655
4656 /* pPage is currently the right-child of pParent. Change this
4657 ** so that the right-child is the new page allocated above and
4658 ** pPage is the next-to-right child.
4659 */
4660 assert( pPage->nCell>0 );
4661 pCell = findCell(pPage, pPage->nCell-1);
4662 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
4663 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
4664 if( rc!=SQLITE_OK ){
4665 return rc;
4666 }
4667 assert( parentSize<64 );
4668 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
4669 if( rc!=SQLITE_OK ){
4670 return rc;
4671 }
4672 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
4673 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4674
4675#ifndef SQLITE_OMIT_AUTOVACUUM
4676 /* If this is an auto-vacuum database, update the pointer map
4677 ** with entries for the new page, and any pointer from the
4678 ** cell on the page to an overflow page.
4679 */
4680 if( pBt->autoVacuum ){
4681 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
4682 if( rc==SQLITE_OK ){
4683 rc = ptrmapPutOvfl(pNew, 0);
4684 }
4685 if( rc!=SQLITE_OK ){
4686 releasePage(pNew);
4687 return rc;
4688 }
4689 }
4690#endif
4691
4692 /* Release the reference to the new page and balance the parent page,
4693 ** in case the divider cell inserted caused it to become overfull.
4694 */
4695 releasePage(pNew);
4696 return balance(pParent, 0);
4697}
4698#endif /* SQLITE_OMIT_QUICKBALANCE */
4699
4700/*
4701** This routine redistributes Cells on pPage and up to NN*2 siblings
4702** of pPage so that all pages have about the same amount of free space.
4703** Usually NN siblings on either side of pPage is used in the balancing,
4704** though more siblings might come from one side if pPage is the first
4705** or last child of its parent. If pPage has fewer than 2*NN siblings
4706** (something which can only happen if pPage is the root page or a
4707** child of root) then all available siblings participate in the balancing.
4708**
4709** The number of siblings of pPage might be increased or decreased by one or
4710** two in an effort to keep pages nearly full but not over full. The root page
4711** is special and is allowed to be nearly empty. If pPage is
4712** the root page, then the depth of the tree might be increased
4713** or decreased by one, as necessary, to keep the root page from being
4714** overfull or completely empty.
4715**
4716** Note that when this routine is called, some of the Cells on pPage
4717** might not actually be stored in pPage->aData[]. This can happen
4718** if the page is overfull. Part of the job of this routine is to
4719** make sure all Cells for pPage once again fit in pPage->aData[].
4720**
4721** In the course of balancing the siblings of pPage, the parent of pPage
4722** might become overfull or underfull. If that happens, then this routine
4723** is called recursively on the parent.
4724**
4725** If this routine fails for any reason, it might leave the database
4726** in a corrupted state. So if this routine fails, the database should
4727** be rolled back.
4728*/
4729static int balance_nonroot(MemPage *pPage){
4730 MemPage *pParent; /* The parent of pPage */
4731 BtShared *pBt; /* The whole database */
4732 int nCell = 0; /* Number of cells in apCell[] */
4733 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
4734 int nOld; /* Number of pages in apOld[] */
4735 int nNew; /* Number of pages in apNew[] */
4736 int nDiv; /* Number of cells in apDiv[] */
4737 int i, j, k; /* Loop counters */
4738 int idx; /* Index of pPage in pParent->aCell[] */
4739 int nxDiv; /* Next divider slot in pParent->aCell[] */
4740 int rc; /* The return code */
4741 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
4742 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
4743 int usableSpace; /* Bytes in pPage beyond the header */
4744 int pageFlags; /* Value of pPage->aData[0] */
4745 int subtotal; /* Subtotal of bytes in cells on one page */
4746 int iSpace = 0; /* First unused byte of aSpace[] */
4747 MemPage *apOld[NB]; /* pPage and up to two siblings */
4748 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
4749 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
4750 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
4751 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
4752 u8 *apDiv[NB]; /* Divider cells in pParent */
4753 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
4754 int szNew[NB+2]; /* Combined size of cells place on i-th page */
4755 u8 **apCell = 0; /* All cells begin balanced */
4756 int *szCell; /* Local size of all cells in apCell[] */
4757 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
4758 u8 *aSpace; /* Space to hold copies of dividers cells */
4759#ifndef SQLITE_OMIT_AUTOVACUUM
4760 u8 *aFrom = 0;
4761#endif
4762
4763 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4764
4765 /*
4766 ** Find the parent page.
4767 */
4768 assert( pPage->isInit );
4769 assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
4770 pBt = pPage->pBt;
4771 pParent = pPage->pParent;
4772 assert( pParent );
4773 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
4774 return rc;
4775 }
4776 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
4777
4778#ifndef SQLITE_OMIT_QUICKBALANCE
4779 /*
4780 ** A special case: If a new entry has just been inserted into a
4781 ** table (that is, a btree with integer keys and all data at the leaves)
4782 ** and the new entry is the right-most entry in the tree (it has the
4783 ** largest key) then use the special balance_quick() routine for
4784 ** balancing. balance_quick() is much faster and results in a tighter
4785 ** packing of data in the common case.
4786 */
4787 if( pPage->leaf &&
4788 pPage->intKey &&
4789 pPage->leafData &&
4790 pPage->nOverflow==1 &&
4791 pPage->aOvfl[0].idx==pPage->nCell &&
4792 pPage->pParent->pgno!=1 &&
4793 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
4794 ){
4795 /*
4796 ** TODO: Check the siblings to the left of pPage. It may be that
4797 ** they are not full and no new page is required.
4798 */
4799 return balance_quick(pPage, pParent);
4800 }
4801#endif
4802
4803 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
4804 return rc;
4805 }
4806
4807 /*
4808 ** Find the cell in the parent page whose left child points back
4809 ** to pPage. The "idx" variable is the index of that cell. If pPage
4810 ** is the rightmost child of pParent then set idx to pParent->nCell
4811 */
4812 if( pParent->idxShift ){
4813 Pgno pgno;
4814 pgno = pPage->pgno;
4815 assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
4816 for(idx=0; idx<pParent->nCell; idx++){
4817 if( get4byte(findCell(pParent, idx))==pgno ){
4818 break;
4819 }
4820 }
4821 assert( idx<pParent->nCell
4822 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
4823 }else{
4824 idx = pPage->idxParent;
4825 }
4826
4827 /*
4828 ** Initialize variables so that it will be safe to jump
4829 ** directly to balance_cleanup at any moment.
4830 */
4831 nOld = nNew = 0;
4832 sqlite3PagerRef(pParent->pDbPage);
4833
4834 /*
4835 ** Find sibling pages to pPage and the cells in pParent that divide
4836 ** the siblings. An attempt is made to find NN siblings on either
4837 ** side of pPage. More siblings are taken from one side, however, if
4838 ** pPage there are fewer than NN siblings on the other side. If pParent
4839 ** has NB or fewer children then all children of pParent are taken.
4840 */
4841 nxDiv = idx - NN;
4842 if( nxDiv + NB > pParent->nCell ){
4843 nxDiv = pParent->nCell - NB + 1;
4844 }
4845 if( nxDiv<0 ){
4846 nxDiv = 0;
4847 }
4848 nDiv = 0;
4849 for(i=0, k=nxDiv; i<NB; i++, k++){
4850 if( k<pParent->nCell ){
4851 apDiv[i] = findCell(pParent, k);
4852 nDiv++;
4853 assert( !pParent->leaf );
4854 pgnoOld[i] = get4byte(apDiv[i]);
4855 }else if( k==pParent->nCell ){
4856 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
4857 }else{
4858 break;
4859 }
4860 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
4861 if( rc ) goto balance_cleanup;
4862 apOld[i]->idxParent = k;
4863 apCopy[i] = 0;
4864 assert( i==nOld );
4865 nOld++;
4866 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
4867 }
4868
4869 /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
4870 ** alignment */
4871 nMaxCells = (nMaxCells + 1)&~1;
4872
4873 /*
4874 ** Allocate space for memory structures
4875 */
4876 apCell = sqlite3_malloc(
4877 nMaxCells*sizeof(u8*) /* apCell */
4878 + nMaxCells*sizeof(int) /* szCell */
4879 + ROUND8(sizeof(MemPage))*NB /* aCopy */
4880 + pBt->pageSize*(5+NB) /* aSpace */
4881 + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
4882 );
4883 if( apCell==0 ){
4884 rc = SQLITE_NOMEM;
4885 goto balance_cleanup;
4886 }
4887 szCell = (int*)&apCell[nMaxCells];
4888 aCopy[0] = (u8*)&szCell[nMaxCells];
4889 assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
4890 for(i=1; i<NB; i++){
4891 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4892 assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
4893 }
4894 aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4895 assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
4896#ifndef SQLITE_OMIT_AUTOVACUUM
4897 if( pBt->autoVacuum ){
4898 aFrom = &aSpace[5*pBt->pageSize];
4899 }
4900#endif
4901
4902 /*
4903 ** Make copies of the content of pPage and its siblings into aOld[].
4904 ** The rest of this function will use data from the copies rather
4905 ** that the original pages since the original pages will be in the
4906 ** process of being overwritten.
4907 */
4908 for(i=0; i<nOld; i++){
4909 MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
4910 memcpy(p, apOld[i], sizeof(MemPage));
4911 p->aData = (void*)&p[1];
4912 memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
4913 }
4914
4915 /*
4916 ** Load pointers to all cells on sibling pages and the divider cells
4917 ** into the local apCell[] array. Make copies of the divider cells
4918 ** into space obtained form aSpace[] and remove the the divider Cells
4919 ** from pParent.
4920 **
4921 ** If the siblings are on leaf pages, then the child pointers of the
4922 ** divider cells are stripped from the cells before they are copied
4923 ** into aSpace[]. In this way, all cells in apCell[] are without
4924 ** child pointers. If siblings are not leaves, then all cell in
4925 ** apCell[] include child pointers. Either way, all cells in apCell[]
4926 ** are alike.
4927 **
4928 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
4929 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
4930 */
4931 nCell = 0;
4932 leafCorrection = pPage->leaf*4;
4933 leafData = pPage->leafData && pPage->leaf;
4934 for(i=0; i<nOld; i++){
4935 MemPage *pOld = apCopy[i];
4936 int limit = pOld->nCell+pOld->nOverflow;
4937 for(j=0; j<limit; j++){
4938 assert( nCell<nMaxCells );
4939 apCell[nCell] = findOverflowCell(pOld, j);
4940 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
4941#ifndef SQLITE_OMIT_AUTOVACUUM
4942 if( pBt->autoVacuum ){
4943 int a;
4944 aFrom[nCell] = i;
4945 for(a=0; a<pOld->nOverflow; a++){
4946 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
4947 aFrom[nCell] = 0xFF;
4948 break;
4949 }
4950 }
4951 }
4952#endif
4953 nCell++;
4954 }
4955 if( i<nOld-1 ){
4956 int sz = cellSizePtr(pParent, apDiv[i]);
4957 if( leafData ){
4958 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
4959 ** are duplicates of keys on the child pages. We need to remove
4960 ** the divider cells from pParent, but the dividers cells are not
4961 ** added to apCell[] because they are duplicates of child cells.
4962 */
4963 dropCell(pParent, nxDiv, sz);
4964 }else{
4965 u8 *pTemp;
4966 assert( nCell<nMaxCells );
4967 szCell[nCell] = sz;
4968 pTemp = &aSpace[iSpace];
4969 iSpace += sz;
4970 assert( iSpace<=pBt->pageSize*5 );
4971 memcpy(pTemp, apDiv[i], sz);
4972 apCell[nCell] = pTemp+leafCorrection;
4973#ifndef SQLITE_OMIT_AUTOVACUUM
4974 if( pBt->autoVacuum ){
4975 aFrom[nCell] = 0xFF;
4976 }
4977#endif
4978 dropCell(pParent, nxDiv, sz);
4979 szCell[nCell] -= leafCorrection;
4980 assert( get4byte(pTemp)==pgnoOld[i] );
4981 if( !pOld->leaf ){
4982 assert( leafCorrection==0 );
4983 /* The right pointer of the child page pOld becomes the left
4984 ** pointer of the divider cell */
4985 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
4986 }else{
4987 assert( leafCorrection==4 );
4988 if( szCell[nCell]<4 ){
4989 /* Do not allow any cells smaller than 4 bytes. */
4990 szCell[nCell] = 4;
4991 }
4992 }
4993 nCell++;
4994 }
4995 }
4996 }
4997
4998 /*
4999 ** Figure out the number of pages needed to hold all nCell cells.
5000 ** Store this number in "k". Also compute szNew[] which is the total
5001 ** size of all cells on the i-th page and cntNew[] which is the index
5002 ** in apCell[] of the cell that divides page i from page i+1.
5003 ** cntNew[k] should equal nCell.
5004 **
5005 ** Values computed by this block:
5006 **
5007 ** k: The total number of sibling pages
5008 ** szNew[i]: Spaced used on the i-th sibling page.
5009 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
5010 ** the right of the i-th sibling page.
5011 ** usableSpace: Number of bytes of space available on each sibling.
5012 **
5013 */
5014 usableSpace = pBt->usableSize - 12 + leafCorrection;
5015 for(subtotal=k=i=0; i<nCell; i++){
5016 assert( i<nMaxCells );
5017 subtotal += szCell[i] + 2;
5018 if( subtotal > usableSpace ){
5019 szNew[k] = subtotal - szCell[i];
5020 cntNew[k] = i;
5021 if( leafData ){ i--; }
5022 subtotal = 0;
5023 k++;
5024 }
5025 }
5026 szNew[k] = subtotal;
5027 cntNew[k] = nCell;
5028 k++;
5029
5030 /*
5031 ** The packing computed by the previous block is biased toward the siblings
5032 ** on the left side. The left siblings are always nearly full, while the
5033 ** right-most sibling might be nearly empty. This block of code attempts
5034 ** to adjust the packing of siblings to get a better balance.
5035 **
5036 ** This adjustment is more than an optimization. The packing above might
5037 ** be so out of balance as to be illegal. For example, the right-most
5038 ** sibling might be completely empty. This adjustment is not optional.
5039 */
5040 for(i=k-1; i>0; i--){
5041 int szRight = szNew[i]; /* Size of sibling on the right */
5042 int szLeft = szNew[i-1]; /* Size of sibling on the left */
5043 int r; /* Index of right-most cell in left sibling */
5044 int d; /* Index of first cell to the left of right sibling */
5045
5046 r = cntNew[i-1] - 1;
5047 d = r + 1 - leafData;
5048 assert( d<nMaxCells );
5049 assert( r<nMaxCells );
5050 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
5051 szRight += szCell[d] + 2;
5052 szLeft -= szCell[r] + 2;
5053 cntNew[i-1]--;
5054 r = cntNew[i-1] - 1;
5055 d = r + 1 - leafData;
5056 }
5057 szNew[i] = szRight;
5058 szNew[i-1] = szLeft;
5059 }
5060
5061 /* Either we found one or more cells (cntnew[0])>0) or we are the
5062 ** a virtual root page. A virtual root page is when the real root
5063 ** page is page 1 and we are the only child of that page.
5064 */
5065 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
5066
5067 /*
5068 ** Allocate k new pages. Reuse old pages where possible.
5069 */
5070 assert( pPage->pgno>1 );
5071 pageFlags = pPage->aData[0];
5072 for(i=0; i<k; i++){
5073 MemPage *pNew;
5074 if( i<nOld ){
5075 pNew = apNew[i] = apOld[i];
5076 pgnoNew[i] = pgnoOld[i];
5077 apOld[i] = 0;
5078 rc = sqlite3PagerWrite(pNew->pDbPage);
5079 nNew++;
5080 if( rc ) goto balance_cleanup;
5081 }else{
5082 assert( i>0 );
5083 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
5084 if( rc ) goto balance_cleanup;
5085 apNew[i] = pNew;
5086 nNew++;
5087 }
5088 zeroPage(pNew, pageFlags);
5089 }
5090
5091 /* Free any old pages that were not reused as new pages.
5092 */
5093 while( i<nOld ){
5094 rc = freePage(apOld[i]);
5095 if( rc ) goto balance_cleanup;
5096 releasePage(apOld[i]);
5097 apOld[i] = 0;
5098 i++;
5099 }
5100
5101 /*
5102 ** Put the new pages in accending order. This helps to
5103 ** keep entries in the disk file in order so that a scan
5104 ** of the table is a linear scan through the file. That
5105 ** in turn helps the operating system to deliver pages
5106 ** from the disk more rapidly.
5107 **
5108 ** An O(n^2) insertion sort algorithm is used, but since
5109 ** n is never more than NB (a small constant), that should
5110 ** not be a problem.
5111 **
5112 ** When NB==3, this one optimization makes the database
5113 ** about 25% faster for large insertions and deletions.
5114 */
5115 for(i=0; i<k-1; i++){
5116 int minV = pgnoNew[i];
5117 int minI = i;
5118 for(j=i+1; j<k; j++){
5119 if( pgnoNew[j]<(unsigned)minV ){
5120 minI = j;
5121 minV = pgnoNew[j];
5122 }
5123 }
5124 if( minI>i ){
5125 int t;
5126 MemPage *pT;
5127 t = pgnoNew[i];
5128 pT = apNew[i];
5129 pgnoNew[i] = pgnoNew[minI];
5130 apNew[i] = apNew[minI];
5131 pgnoNew[minI] = t;
5132 apNew[minI] = pT;
5133 }
5134 }
5135 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
5136 pgnoOld[0],
5137 nOld>=2 ? pgnoOld[1] : 0,
5138 nOld>=3 ? pgnoOld[2] : 0,
5139 pgnoNew[0], szNew[0],
5140 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
5141 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
5142 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
5143 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
5144
5145 /*
5146 ** Evenly distribute the data in apCell[] across the new pages.
5147 ** Insert divider cells into pParent as necessary.
5148 */
5149 j = 0;
5150 for(i=0; i<nNew; i++){
5151 /* Assemble the new sibling page. */
5152 MemPage *pNew = apNew[i];
5153 assert( j<nMaxCells );
5154 assert( pNew->pgno==pgnoNew[i] );
5155 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
5156 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
5157 assert( pNew->nOverflow==0 );
5158
5159#ifndef SQLITE_OMIT_AUTOVACUUM
5160 /* If this is an auto-vacuum database, update the pointer map entries
5161 ** that point to the siblings that were rearranged. These can be: left
5162 ** children of cells, the right-child of the page, or overflow pages
5163 ** pointed to by cells.
5164 */
5165 if( pBt->autoVacuum ){
5166 for(k=j; k<cntNew[i]; k++){
5167 assert( k<nMaxCells );
5168 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
5169 rc = ptrmapPutOvfl(pNew, k-j);
5170 if( rc!=SQLITE_OK ){
5171 goto balance_cleanup;
5172 }
5173 }
5174 }
5175 }
5176#endif
5177
5178 j = cntNew[i];
5179
5180 /* If the sibling page assembled above was not the right-most sibling,
5181 ** insert a divider cell into the parent page.
5182 */
5183 if( i<nNew-1 && j<nCell ){
5184 u8 *pCell;
5185 u8 *pTemp;
5186 int sz;
5187
5188 assert( j<nMaxCells );
5189 pCell = apCell[j];
5190 sz = szCell[j] + leafCorrection;
5191 if( !pNew->leaf ){
5192 memcpy(&pNew->aData[8], pCell, 4);
5193 pTemp = 0;
5194 }else if( leafData ){
5195 /* If the tree is a leaf-data tree, and the siblings are leaves,
5196 ** then there is no divider cell in apCell[]. Instead, the divider
5197 ** cell consists of the integer key for the right-most cell of
5198 ** the sibling-page assembled above only.
5199 */
5200 CellInfo info;
5201 j--;
5202 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
5203 pCell = &aSpace[iSpace];
5204 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
5205 iSpace += sz;
5206 assert( iSpace<=pBt->pageSize*5 );
5207 pTemp = 0;
5208 }else{
5209 pCell -= 4;
5210 pTemp = &aSpace[iSpace];
5211 iSpace += sz;
5212 assert( iSpace<=pBt->pageSize*5 );
5213 /* Obscure case for non-leaf-data trees: If the cell at pCell was
5214 ** previously stored on a leaf node, and it's reported size was 4
5215 ** bytes, then it may actually be smaller than this
5216 ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
5217 ** any cell). But it's important to pass the correct size to
5218 ** insertCell(), so reparse the cell now.
5219 **
5220 ** Note that this can never happen in an SQLite data file, as all
5221 ** cells are at least 4 bytes. It only happens in b-trees used
5222 ** to evaluate "IN (SELECT ...)" and similar clauses.
5223 */
5224 if( szCell[j]==4 ){
5225 assert(leafCorrection==4);
5226 sz = cellSizePtr(pParent, pCell);
5227 }
5228 }
5229 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
5230 if( rc!=SQLITE_OK ) goto balance_cleanup;
5231 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
5232#ifndef SQLITE_OMIT_AUTOVACUUM
5233 /* If this is an auto-vacuum database, and not a leaf-data tree,
5234 ** then update the pointer map with an entry for the overflow page
5235 ** that the cell just inserted points to (if any).
5236 */
5237 if( pBt->autoVacuum && !leafData ){
5238 rc = ptrmapPutOvfl(pParent, nxDiv);
5239 if( rc!=SQLITE_OK ){
5240 goto balance_cleanup;
5241 }
5242 }
5243#endif
5244 j++;
5245 nxDiv++;
5246 }
5247 }
5248 assert( j==nCell );
5249 assert( nOld>0 );
5250 assert( nNew>0 );
5251 if( (pageFlags & PTF_LEAF)==0 ){
5252 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
5253 }
5254 if( nxDiv==pParent->nCell+pParent->nOverflow ){
5255 /* Right-most sibling is the right-most child of pParent */
5256 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
5257 }else{
5258 /* Right-most sibling is the left child of the first entry in pParent
5259 ** past the right-most divider entry */
5260 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
5261 }
5262
5263 /*
5264 ** Reparent children of all cells.
5265 */
5266 for(i=0; i<nNew; i++){
5267 rc = reparentChildPages(apNew[i]);
5268 if( rc!=SQLITE_OK ) goto balance_cleanup;
5269 }
5270 rc = reparentChildPages(pParent);
5271 if( rc!=SQLITE_OK ) goto balance_cleanup;
5272
5273 /*
5274 ** Balance the parent page. Note that the current page (pPage) might
5275 ** have been added to the freelist so it might no longer be initialized.
5276 ** But the parent page will always be initialized.
5277 */
5278 assert( pParent->isInit );
5279 rc = balance(pParent, 0);
5280
5281 /*
5282 ** Cleanup before returning.
5283 */
5284balance_cleanup:
5285 sqlite3_free(apCell);
5286 for(i=0; i<nOld; i++){
5287 releasePage(apOld[i]);
5288 }
5289 for(i=0; i<nNew; i++){
5290 releasePage(apNew[i]);
5291 }
5292 releasePage(pParent);
5293 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
5294 pPage->pgno, nOld, nNew, nCell));
5295 return rc;
5296}
5297
5298/*
5299** This routine is called for the root page of a btree when the root
5300** page contains no cells. This is an opportunity to make the tree
5301** shallower by one level.
5302*/
5303static int balance_shallower(MemPage *pPage){
5304 MemPage *pChild; /* The only child page of pPage */
5305 Pgno pgnoChild; /* Page number for pChild */
5306 int rc = SQLITE_OK; /* Return code from subprocedures */
5307 BtShared *pBt; /* The main BTree structure */
5308 int mxCellPerPage; /* Maximum number of cells per page */
5309 u8 **apCell; /* All cells from pages being balanced */
5310 int *szCell; /* Local size of all cells */
5311
5312 assert( pPage->pParent==0 );
5313 assert( pPage->nCell==0 );
5314 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5315 pBt = pPage->pBt;
5316 mxCellPerPage = MX_CELL(pBt);
5317 apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
5318 if( apCell==0 ) return SQLITE_NOMEM;
5319 szCell = (int*)&apCell[mxCellPerPage];
5320 if( pPage->leaf ){
5321 /* The table is completely empty */
5322 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
5323 }else{
5324 /* The root page is empty but has one child. Transfer the
5325 ** information from that one child into the root page if it
5326 ** will fit. This reduces the depth of the tree by one.
5327 **
5328 ** If the root page is page 1, it has less space available than
5329 ** its child (due to the 100 byte header that occurs at the beginning
5330 ** of the database fle), so it might not be able to hold all of the
5331 ** information currently contained in the child. If this is the
5332 ** case, then do not do the transfer. Leave page 1 empty except
5333 ** for the right-pointer to the child page. The child page becomes
5334 ** the virtual root of the tree.
5335 */
5336 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5337 assert( pgnoChild>0 );
5338 assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
5339 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
5340 if( rc ) goto end_shallow_balance;
5341 if( pPage->pgno==1 ){
5342 rc = sqlite3BtreeInitPage(pChild, pPage);
5343 if( rc ) goto end_shallow_balance;
5344 assert( pChild->nOverflow==0 );
5345 if( pChild->nFree>=100 ){
5346 /* The child information will fit on the root page, so do the
5347 ** copy */
5348 int i;
5349 zeroPage(pPage, pChild->aData[0]);
5350 for(i=0; i<pChild->nCell; i++){
5351 apCell[i] = findCell(pChild,i);
5352 szCell[i] = cellSizePtr(pChild, apCell[i]);
5353 }
5354 assemblePage(pPage, pChild->nCell, apCell, szCell);
5355 /* Copy the right-pointer of the child to the parent. */
5356 put4byte(&pPage->aData[pPage->hdrOffset+8],
5357 get4byte(&pChild->aData[pChild->hdrOffset+8]));
5358 freePage(pChild);
5359 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5360 }else{
5361 /* The child has more information that will fit on the root.
5362 ** The tree is already balanced. Do nothing. */
5363 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5364 }
5365 }else{
5366 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5367 pPage->isInit = 0;
5368 pPage->pParent = 0;
5369 rc = sqlite3BtreeInitPage(pPage, 0);
5370 assert( rc==SQLITE_OK );
5371 freePage(pChild);
5372 TRACE(("BALANCE: transfer child %d into root %d\n",
5373 pChild->pgno, pPage->pgno));
5374 }
5375 rc = reparentChildPages(pPage);
5376 assert( pPage->nOverflow==0 );
5377#ifndef SQLITE_OMIT_AUTOVACUUM
5378 if( pBt->autoVacuum ){
5379 int i;
5380 for(i=0; i<pPage->nCell; i++){
5381 rc = ptrmapPutOvfl(pPage, i);
5382 if( rc!=SQLITE_OK ){
5383 goto end_shallow_balance;
5384 }
5385 }
5386 }
5387#endif
5388 releasePage(pChild);
5389 }
5390end_shallow_balance:
5391 sqlite3_free(apCell);
5392 return rc;
5393}
5394
5395
5396/*
5397** The root page is overfull
5398**
5399** When this happens, Create a new child page and copy the
5400** contents of the root into the child. Then make the root
5401** page an empty page with rightChild pointing to the new
5402** child. Finally, call balance_internal() on the new child
5403** to cause it to split.
5404*/
5405static int balance_deeper(MemPage *pPage){
5406 int rc; /* Return value from subprocedures */
5407 MemPage *pChild; /* Pointer to a new child page */
5408 Pgno pgnoChild; /* Page number of the new child page */
5409 BtShared *pBt; /* The BTree */
5410 int usableSize; /* Total usable size of a page */
5411 u8 *data; /* Content of the parent page */
5412 u8 *cdata; /* Content of the child page */
5413 int hdr; /* Offset to page header in parent */
5414 int brk; /* Offset to content of first cell in parent */
5415
5416 assert( pPage->pParent==0 );
5417 assert( pPage->nOverflow>0 );
5418 pBt = pPage->pBt;
5419 assert( sqlite3_mutex_held(pBt->mutex) );
5420 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
5421 if( rc ) return rc;
5422 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
5423 usableSize = pBt->usableSize;
5424 data = pPage->aData;
5425 hdr = pPage->hdrOffset;
5426 brk = get2byte(&data[hdr+5]);
5427 cdata = pChild->aData;
5428 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
5429 memcpy(&cdata[brk], &data[brk], usableSize-brk);
5430 assert( pChild->isInit==0 );
5431 rc = sqlite3BtreeInitPage(pChild, pPage);
5432 if( rc ) goto balancedeeper_out;
5433 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
5434 pChild->nOverflow = pPage->nOverflow;
5435 if( pChild->nOverflow ){
5436 pChild->nFree = 0;
5437 }
5438 assert( pChild->nCell==pPage->nCell );
5439 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5440 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5441 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
5442#ifndef SQLITE_OMIT_AUTOVACUUM
5443 if( pBt->autoVacuum ){
5444 int i;
5445 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
5446 if( rc ) goto balancedeeper_out;
5447 for(i=0; i<pChild->nCell; i++){
5448 rc = ptrmapPutOvfl(pChild, i);
5449 if( rc!=SQLITE_OK ){
5450 return rc;
5451 }
5452 }
5453 }
5454#endif
5455 rc = balance_nonroot(pChild);
5456
5457balancedeeper_out:
5458 releasePage(pChild);
5459 return rc;
5460}
5461
5462/*
5463** Decide if the page pPage needs to be balanced. If balancing is
5464** required, call the appropriate balancing routine.
5465*/
5466static int balance(MemPage *pPage, int insert){
5467 int rc = SQLITE_OK;
5468 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5469 if( pPage->pParent==0 ){
5470 rc = sqlite3PagerWrite(pPage->pDbPage);
5471 if( rc==SQLITE_OK && pPage->nOverflow>0 ){
5472 rc = balance_deeper(pPage);
5473 }
5474 if( rc==SQLITE_OK && pPage->nCell==0 ){
5475 rc = balance_shallower(pPage);
5476 }
5477 }else{
5478 if( pPage->nOverflow>0 ||
5479 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
5480 rc = balance_nonroot(pPage);
5481 }
5482 }
5483 return rc;
5484}
5485
5486/*
5487** This routine checks all cursors that point to table pgnoRoot.
5488** If any of those cursors were opened with wrFlag==0 in a different
5489** database connection (a database connection that shares the pager
5490** cache with the current connection) and that other connection
5491** is not in the ReadUncommmitted state, then this routine returns
5492** SQLITE_LOCKED.
5493**
5494** In addition to checking for read-locks (where a read-lock
5495** means a cursor opened with wrFlag==0) this routine also moves
5496** all write cursors so that they are pointing to the
5497** first Cell on the root page. This is necessary because an insert
5498** or delete might change the number of cells on a page or delete
5499** a page entirely and we do not want to leave any cursors
5500** pointing to non-existant pages or cells.
5501*/
5502static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
5503 BtCursor *p;
5504 BtShared *pBt = pBtree->pBt;
5505 sqlite3 *db = pBtree->pSqlite;
5506 assert( sqlite3BtreeHoldsMutex(pBtree) );
5507 for(p=pBt->pCursor; p; p=p->pNext){
5508 if( p==pExclude ) continue;
5509 if( p->eState!=CURSOR_VALID ) continue;
5510 if( p->pgnoRoot!=pgnoRoot ) continue;
5511 if( p->wrFlag==0 ){
5512 sqlite3 *dbOther = p->pBtree->pSqlite;
5513 if( dbOther==0 ||
5514 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5515 return SQLITE_LOCKED;
5516 }
5517 }else if( p->pPage->pgno!=p->pgnoRoot ){
5518 moveToRoot(p);
5519 }
5520 }
5521 return SQLITE_OK;
5522}
5523
5524/*
5525** Insert a new record into the BTree. The key is given by (pKey,nKey)
5526** and the data is given by (pData,nData). The cursor is used only to
5527** define what table the record should be inserted into. The cursor
5528** is left pointing at a random location.
5529**
5530** For an INTKEY table, only the nKey value of the key is used. pKey is
5531** ignored. For a ZERODATA table, the pData and nData are both ignored.
5532*/
5533int sqlite3BtreeInsert(
5534 BtCursor *pCur, /* Insert data into the table of this cursor */
5535 const void *pKey, i64 nKey, /* The key of the new record */
5536 const void *pData, int nData, /* The data of the new record */
5537 int nZero, /* Number of extra 0 bytes to append to data */
5538 int appendBias /* True if this is likely an append */
5539){
5540 int rc;
5541 int loc;
5542 int szNew;
5543 MemPage *pPage;
5544 Btree *p = pCur->pBtree;
5545 BtShared *pBt = p->pBt;
5546 unsigned char *oldCell;
5547 unsigned char *newCell = 0;
5548
5549 assert( cursorHoldsMutex(pCur) );
5550 if( pBt->inTransaction!=TRANS_WRITE ){
5551 /* Must start a transaction before doing an insert */
5552 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5553 return rc;
5554 }
5555 assert( !pBt->readOnly );
5556 if( !pCur->wrFlag ){
5557 return SQLITE_PERM; /* Cursor not open for writing */
5558 }
5559 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
5560 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5561 }
5562 if( pCur->eState==CURSOR_FAULT ){
5563 return pCur->skip;
5564 }
5565
5566 /* Save the positions of any other cursors open on this table */
5567 clearCursorPosition(pCur);
5568 if(
5569 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
5570 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
5571 ){
5572 return rc;
5573 }
5574
5575 pPage = pCur->pPage;
5576 assert( pPage->intKey || nKey>=0 );
5577 assert( pPage->leaf || !pPage->leafData );
5578 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5579 pCur->pgnoRoot, nKey, nData, pPage->pgno,
5580 loc==0 ? "overwrite" : "new entry"));
5581 assert( pPage->isInit );
5582 newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
5583 if( newCell==0 ) return SQLITE_NOMEM;
5584 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
5585 if( rc ) goto end_insert;
5586 assert( szNew==cellSizePtr(pPage, newCell) );
5587 assert( szNew<=MX_CELL_SIZE(pBt) );
5588 if( loc==0 && CURSOR_VALID==pCur->eState ){
5589 int szOld;
5590 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
5591 rc = sqlite3PagerWrite(pPage->pDbPage);
5592 if( rc ){
5593 goto end_insert;
5594 }
5595 oldCell = findCell(pPage, pCur->idx);
5596 if( !pPage->leaf ){
5597 memcpy(newCell, oldCell, 4);
5598 }
5599 szOld = cellSizePtr(pPage, oldCell);
5600 rc = clearCell(pPage, oldCell);
5601 if( rc ) goto end_insert;
5602 dropCell(pPage, pCur->idx, szOld);
5603 }else if( loc<0 && pPage->nCell>0 ){
5604 assert( pPage->leaf );
5605 pCur->idx++;
5606 pCur->info.nSize = 0;
5607 }else{
5608 assert( pPage->leaf );
5609 }
5610 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
5611 if( rc!=SQLITE_OK ) goto end_insert;
5612 rc = balance(pPage, 1);
5613 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
5614 /* fflush(stdout); */
5615 if( rc==SQLITE_OK ){
5616 moveToRoot(pCur);
5617 }
5618end_insert:
5619 sqlite3_free(newCell);
5620 return rc;
5621}
5622
5623/*
5624** Delete the entry that the cursor is pointing to. The cursor
5625** is left pointing at a random location.
5626*/
5627int sqlite3BtreeDelete(BtCursor *pCur){
5628 MemPage *pPage = pCur->pPage;
5629 unsigned char *pCell;
5630 int rc;
5631 Pgno pgnoChild = 0;
5632 Btree *p = pCur->pBtree;
5633 BtShared *pBt = p->pBt;
5634
5635 assert( cursorHoldsMutex(pCur) );
5636 assert( pPage->isInit );
5637 if( pBt->inTransaction!=TRANS_WRITE ){
5638 /* Must start a transaction before doing a delete */
5639 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5640 return rc;
5641 }
5642 assert( !pBt->readOnly );
5643 if( pCur->eState==CURSOR_FAULT ){
5644 return pCur->skip;
5645 }
5646 if( pCur->idx >= pPage->nCell ){
5647 return SQLITE_ERROR; /* The cursor is not pointing to anything */
5648 }
5649 if( !pCur->wrFlag ){
5650 return SQLITE_PERM; /* Did not open this cursor for writing */
5651 }
5652 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
5653 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5654 }
5655
5656 /* Restore the current cursor position (a no-op if the cursor is not in
5657 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
5658 ** open on the same table. Then call sqlite3PagerWrite() on the page
5659 ** that the entry will be deleted from.
5660 */
5661 if(
5662 (rc = restoreOrClearCursorPosition(pCur))!=0 ||
5663 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
5664 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
5665 ){
5666 return rc;
5667 }
5668
5669 /* Locate the cell within it's page and leave pCell pointing to the
5670 ** data. The clearCell() call frees any overflow pages associated with the
5671 ** cell. The cell itself is still intact.
5672 */
5673 pCell = findCell(pPage, pCur->idx);
5674 if( !pPage->leaf ){
5675 pgnoChild = get4byte(pCell);
5676 }
5677 rc = clearCell(pPage, pCell);
5678 if( rc ){
5679 return rc;
5680 }
5681
5682 if( !pPage->leaf ){
5683 /*
5684 ** The entry we are about to delete is not a leaf so if we do not
5685 ** do something we will leave a hole on an internal page.
5686 ** We have to fill the hole by moving in a cell from a leaf. The
5687 ** next Cell after the one to be deleted is guaranteed to exist and
5688 ** to be a leaf so we can use it.
5689 */
5690 BtCursor leafCur;
5691 unsigned char *pNext;
5692 int szNext; /* The compiler warning is wrong: szNext is always
5693 ** initialized before use. Adding an extra initialization
5694 ** to silence the compiler slows down the code. */
5695 int notUsed;
5696 unsigned char *tempCell = 0;
5697 assert( !pPage->leafData );
5698 sqlite3BtreeGetTempCursor(pCur, &leafCur);
5699 rc = sqlite3BtreeNext(&leafCur, &notUsed);
5700 if( rc==SQLITE_OK ){
5701 rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
5702 }
5703 if( rc==SQLITE_OK ){
5704 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
5705 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
5706 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
5707 pNext = findCell(leafCur.pPage, leafCur.idx);
5708 szNext = cellSizePtr(leafCur.pPage, pNext);
5709 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
5710 tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
5711 if( tempCell==0 ){
5712 rc = SQLITE_NOMEM;
5713 }
5714 }
5715 if( rc==SQLITE_OK ){
5716 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
5717 }
5718 if( rc==SQLITE_OK ){
5719 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
5720 rc = balance(pPage, 0);
5721 }
5722 if( rc==SQLITE_OK ){
5723 dropCell(leafCur.pPage, leafCur.idx, szNext);
5724 rc = balance(leafCur.pPage, 0);
5725 }
5726 sqlite3_free(tempCell);
5727 sqlite3BtreeReleaseTempCursor(&leafCur);
5728 }else{
5729 TRACE(("DELETE: table=%d delete from leaf %d\n",
5730 pCur->pgnoRoot, pPage->pgno));
5731 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
5732 rc = balance(pPage, 0);
5733 }
5734 if( rc==SQLITE_OK ){
5735 moveToRoot(pCur);
5736 }
5737 return rc;
5738}
5739
5740/*
5741** Create a new BTree table. Write into *piTable the page
5742** number for the root page of the new table.
5743**
5744** The type of type is determined by the flags parameter. Only the
5745** following values of flags are currently in use. Other values for
5746** flags might not work:
5747**
5748** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
5749** BTREE_ZERODATA Used for SQL indices
5750*/
5751static int btreeCreateTable(Btree *p, int *piTable, int flags){
5752 BtShared *pBt = p->pBt;
5753 MemPage *pRoot;
5754 Pgno pgnoRoot;
5755 int rc;
5756
5757 assert( sqlite3BtreeHoldsMutex(p) );
5758 if( pBt->inTransaction!=TRANS_WRITE ){
5759 /* Must start a transaction first */
5760 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5761 return rc;
5762 }
5763 assert( !pBt->readOnly );
5764
5765#ifdef SQLITE_OMIT_AUTOVACUUM
5766 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
5767 if( rc ){
5768 return rc;
5769 }
5770#else
5771 if( pBt->autoVacuum ){
5772 Pgno pgnoMove; /* Move a page here to make room for the root-page */
5773 MemPage *pPageMove; /* The page to move to. */
5774
5775 /* Creating a new table may probably require moving an existing database
5776 ** to make room for the new tables root page. In case this page turns
5777 ** out to be an overflow page, delete all overflow page-map caches
5778 ** held by open cursors.
5779 */
5780 invalidateAllOverflowCache(pBt);
5781
5782 /* Read the value of meta[3] from the database to determine where the
5783 ** root page of the new table should go. meta[3] is the largest root-page
5784 ** created so far, so the new root-page is (meta[3]+1).
5785 */
5786 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
5787 if( rc!=SQLITE_OK ){
5788 return rc;
5789 }
5790 pgnoRoot++;
5791
5792 /* The new root-page may not be allocated on a pointer-map page, or the
5793 ** PENDING_BYTE page.
5794 */
5795 if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
5796 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
5797 pgnoRoot++;
5798 }
5799 assert( pgnoRoot>=3 );
5800
5801 /* Allocate a page. The page that currently resides at pgnoRoot will
5802 ** be moved to the allocated page (unless the allocated page happens
5803 ** to reside at pgnoRoot).
5804 */
5805 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
5806 if( rc!=SQLITE_OK ){
5807 return rc;
5808 }
5809
5810 if( pgnoMove!=pgnoRoot ){
5811 /* pgnoRoot is the page that will be used for the root-page of
5812 ** the new table (assuming an error did not occur). But we were
5813 ** allocated pgnoMove. If required (i.e. if it was not allocated
5814 ** by extending the file), the current page at position pgnoMove
5815 ** is already journaled.
5816 */
5817 u8 eType;
5818 Pgno iPtrPage;
5819
5820 releasePage(pPageMove);
5821
5822 /* Move the page currently at pgnoRoot to pgnoMove. */
5823 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
5824 if( rc!=SQLITE_OK ){
5825 return rc;
5826 }
5827 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
5828 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
5829 releasePage(pRoot);
5830 return rc;
5831 }
5832 assert( eType!=PTRMAP_ROOTPAGE );
5833 assert( eType!=PTRMAP_FREEPAGE );
5834 rc = sqlite3PagerWrite(pRoot->pDbPage);
5835 if( rc!=SQLITE_OK ){
5836 releasePage(pRoot);
5837 return rc;
5838 }
5839 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
5840 releasePage(pRoot);
5841
5842 /* Obtain the page at pgnoRoot */
5843 if( rc!=SQLITE_OK ){
5844 return rc;
5845 }
5846 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
5847 if( rc!=SQLITE_OK ){
5848 return rc;
5849 }
5850 rc = sqlite3PagerWrite(pRoot->pDbPage);
5851 if( rc!=SQLITE_OK ){
5852 releasePage(pRoot);
5853 return rc;
5854 }
5855 }else{
5856 pRoot = pPageMove;
5857 }
5858
5859 /* Update the pointer-map and meta-data with the new root-page number. */
5860 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
5861 if( rc ){
5862 releasePage(pRoot);
5863 return rc;
5864 }
5865 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
5866 if( rc ){
5867 releasePage(pRoot);
5868 return rc;
5869 }
5870
5871 }else{
5872 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
5873 if( rc ) return rc;
5874 }
5875#endif
5876 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
5877 zeroPage(pRoot, flags | PTF_LEAF);
5878 sqlite3PagerUnref(pRoot->pDbPage);
5879 *piTable = (int)pgnoRoot;
5880 return SQLITE_OK;
5881}
5882int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
5883 int rc;
5884 sqlite3BtreeEnter(p);
5885 rc = btreeCreateTable(p, piTable, flags);
5886 sqlite3BtreeLeave(p);
5887 return rc;
5888}
5889
5890/*
5891** Erase the given database page and all its children. Return
5892** the page to the freelist.
5893*/
5894static int clearDatabasePage(
5895 BtShared *pBt, /* The BTree that contains the table */
5896 Pgno pgno, /* Page number to clear */
5897 MemPage *pParent, /* Parent page. NULL for the root */
5898 int freePageFlag /* Deallocate page if true */
5899){
5900 MemPage *pPage = 0;
5901 int rc;
5902 unsigned char *pCell;
5903 int i;
5904
5905 assert( sqlite3_mutex_held(pBt->mutex) );
5906 if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
5907 return SQLITE_CORRUPT_BKPT;
5908 }
5909
5910 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
5911 if( rc ) goto cleardatabasepage_out;
5912 for(i=0; i<pPage->nCell; i++){
5913 pCell = findCell(pPage, i);
5914 if( !pPage->leaf ){
5915 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
5916 if( rc ) goto cleardatabasepage_out;
5917 }
5918 rc = clearCell(pPage, pCell);
5919 if( rc ) goto cleardatabasepage_out;
5920 }
5921 if( !pPage->leaf ){
5922 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
5923 if( rc ) goto cleardatabasepage_out;
5924 }
5925 if( freePageFlag ){
5926 rc = freePage(pPage);
5927 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
5928 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
5929 }
5930
5931cleardatabasepage_out:
5932 releasePage(pPage);
5933 return rc;
5934}
5935
5936/*
5937** Delete all information from a single table in the database. iTable is
5938** the page number of the root of the table. After this routine returns,
5939** the root page is empty, but still exists.
5940**
5941** This routine will fail with SQLITE_LOCKED if there are any open
5942** read cursors on the table. Open write cursors are moved to the
5943** root of the table.
5944*/
5945int sqlite3BtreeClearTable(Btree *p, int iTable){
5946 int rc;
5947 BtShared *pBt = p->pBt;
5948 sqlite3BtreeEnter(p);
5949 if( p->inTrans!=TRANS_WRITE ){
5950 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5951 }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){
5952 /* nothing to do */
5953 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
5954 /* nothing to do */
5955 }else{
5956 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
5957 }
5958 sqlite3BtreeLeave(p);
5959 return rc;
5960}
5961
5962/*
5963** Erase all information in a table and add the root of the table to
5964** the freelist. Except, the root of the principle table (the one on
5965** page 1) is never added to the freelist.
5966**
5967** This routine will fail with SQLITE_LOCKED if there are any open
5968** cursors on the table.
5969**
5970** If AUTOVACUUM is enabled and the page at iTable is not the last
5971** root page in the database file, then the last root page
5972** in the database file is moved into the slot formerly occupied by
5973** iTable and that last slot formerly occupied by the last root page
5974** is added to the freelist instead of iTable. In this say, all
5975** root pages are kept at the beginning of the database file, which
5976** is necessary for AUTOVACUUM to work right. *piMoved is set to the
5977** page number that used to be the last root page in the file before
5978** the move. If no page gets moved, *piMoved is set to 0.
5979** The last root page is recorded in meta[3] and the value of
5980** meta[3] is updated by this procedure.
5981*/
5982static int btreeDropTable(Btree *p, int iTable, int *piMoved){
5983 int rc;
5984 MemPage *pPage = 0;
5985 BtShared *pBt = p->pBt;
5986
5987 assert( sqlite3BtreeHoldsMutex(p) );
5988 if( p->inTrans!=TRANS_WRITE ){
5989 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5990 }
5991
5992 /* It is illegal to drop a table if any cursors are open on the
5993 ** database. This is because in auto-vacuum mode the backend may
5994 ** need to move another root-page to fill a gap left by the deleted
5995 ** root page. If an open cursor was using this page a problem would
5996 ** occur.
5997 */
5998 if( pBt->pCursor ){
5999 return SQLITE_LOCKED;
6000 }
6001
6002 rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
6003 if( rc ) return rc;
6004 rc = sqlite3BtreeClearTable(p, iTable);
6005 if( rc ){
6006 releasePage(pPage);
6007 return rc;
6008 }
6009
6010 *piMoved = 0;
6011
6012 if( iTable>1 ){
6013#ifdef SQLITE_OMIT_AUTOVACUUM
6014 rc = freePage(pPage);
6015 releasePage(pPage);
6016#else
6017 if( pBt->autoVacuum ){
6018 Pgno maxRootPgno;
6019 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
6020 if( rc!=SQLITE_OK ){
6021 releasePage(pPage);
6022 return rc;
6023 }
6024
6025 if( iTable==maxRootPgno ){
6026 /* If the table being dropped is the table with the largest root-page
6027 ** number in the database, put the root page on the free list.
6028 */
6029 rc = freePage(pPage);
6030 releasePage(pPage);
6031 if( rc!=SQLITE_OK ){
6032 return rc;
6033 }
6034 }else{
6035 /* The table being dropped does not have the largest root-page
6036 ** number in the database. So move the page that does into the
6037 ** gap left by the deleted root-page.
6038 */
6039 MemPage *pMove;
6040 releasePage(pPage);
6041 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
6042 if( rc!=SQLITE_OK ){
6043 return rc;
6044 }
6045 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
6046 releasePage(pMove);
6047 if( rc!=SQLITE_OK ){
6048 return rc;
6049 }
6050 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
6051 if( rc!=SQLITE_OK ){
6052 return rc;
6053 }
6054 rc = freePage(pMove);
6055 releasePage(pMove);
6056 if( rc!=SQLITE_OK ){
6057 return rc;
6058 }
6059 *piMoved = maxRootPgno;
6060 }
6061
6062 /* Set the new 'max-root-page' value in the database header. This
6063 ** is the old value less one, less one more if that happens to
6064 ** be a root-page number, less one again if that is the
6065 ** PENDING_BYTE_PAGE.
6066 */
6067 maxRootPgno--;
6068 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
6069 maxRootPgno--;
6070 }
6071 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
6072 maxRootPgno--;
6073 }
6074 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
6075
6076 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
6077 }else{
6078 rc = freePage(pPage);
6079 releasePage(pPage);
6080 }
6081#endif
6082 }else{
6083 /* If sqlite3BtreeDropTable was called on page 1. */
6084 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
6085 releasePage(pPage);
6086 }
6087 return rc;
6088}
6089int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
6090 int rc;
6091 sqlite3BtreeEnter(p);
6092 rc = btreeDropTable(p, iTable, piMoved);
6093 sqlite3BtreeLeave(p);
6094 return rc;
6095}
6096
6097
6098/*
6099** Read the meta-information out of a database file. Meta[0]
6100** is the number of free pages currently in the database. Meta[1]
6101** through meta[15] are available for use by higher layers. Meta[0]
6102** is read-only, the others are read/write.
6103**
6104** The schema layer numbers meta values differently. At the schema
6105** layer (and the SetCookie and ReadCookie opcodes) the number of
6106** free pages is not visible. So Cookie[0] is the same as Meta[1].
6107*/
6108int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
6109 DbPage *pDbPage;
6110 int rc;
6111 unsigned char *pP1;
6112 BtShared *pBt = p->pBt;
6113
6114 sqlite3BtreeEnter(p);
6115
6116 /* Reading a meta-data value requires a read-lock on page 1 (and hence
6117 ** the sqlite_master table. We grab this lock regardless of whether or
6118 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
6119 ** 1 is treated as a special case by queryTableLock() and lockTable()).
6120 */
6121 rc = queryTableLock(p, 1, READ_LOCK);
6122 if( rc!=SQLITE_OK ){
6123 sqlite3BtreeLeave(p);
6124 return rc;
6125 }
6126
6127 assert( idx>=0 && idx<=15 );
6128 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
6129 if( rc ){
6130 sqlite3BtreeLeave(p);
6131 return rc;
6132 }
6133 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
6134 *pMeta = get4byte(&pP1[36 + idx*4]);
6135 sqlite3PagerUnref(pDbPage);
6136
6137 /* If autovacuumed is disabled in this build but we are trying to
6138 ** access an autovacuumed database, then make the database readonly.
6139 */
6140#ifdef SQLITE_OMIT_AUTOVACUUM
6141 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
6142#endif
6143
6144 /* Grab the read-lock on page 1. */
6145 rc = lockTable(p, 1, READ_LOCK);
6146 sqlite3BtreeLeave(p);
6147 return rc;
6148}
6149
6150/*
6151** Write meta-information back into the database. Meta[0] is
6152** read-only and may not be written.
6153*/
6154int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
6155 BtShared *pBt = p->pBt;
6156 unsigned char *pP1;
6157 int rc;
6158 assert( idx>=1 && idx<=15 );
6159 sqlite3BtreeEnter(p);
6160 if( p->inTrans!=TRANS_WRITE ){
6161 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
6162 }else{
6163 assert( pBt->pPage1!=0 );
6164 pP1 = pBt->pPage1->aData;
6165 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6166 if( rc==SQLITE_OK ){
6167 put4byte(&pP1[36 + idx*4], iMeta);
6168#ifndef SQLITE_OMIT_AUTOVACUUM
6169 if( idx==7 ){
6170 assert( pBt->autoVacuum || iMeta==0 );
6171 assert( iMeta==0 || iMeta==1 );
6172 pBt->incrVacuum = iMeta;
6173 }
6174#endif
6175 }
6176 }
6177 sqlite3BtreeLeave(p);
6178 return rc;
6179}
6180
6181/*
6182** Return the flag byte at the beginning of the page that the cursor
6183** is currently pointing to.
6184*/
6185int sqlite3BtreeFlags(BtCursor *pCur){
6186 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
6187 ** restoreOrClearCursorPosition() here.
6188 */
6189 MemPage *pPage = pCur->pPage;
6190 assert( cursorHoldsMutex(pCur) );
6191 assert( pPage->pBt==pCur->pBt );
6192 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
6193}
6194
6195
6196/*
6197** Return the pager associated with a BTree. This routine is used for
6198** testing and debugging only.
6199*/
6200Pager *sqlite3BtreePager(Btree *p){
6201 return p->pBt->pPager;
6202}
6203
6204#ifndef SQLITE_OMIT_INTEGRITY_CHECK
6205/*
6206** Append a message to the error message string.
6207*/
6208static void checkAppendMsg(
6209 IntegrityCk *pCheck,
6210 char *zMsg1,
6211 const char *zFormat,
6212 ...
6213){
6214 va_list ap;
6215 char *zMsg2;
6216 if( !pCheck->mxErr ) return;
6217 pCheck->mxErr--;
6218 pCheck->nErr++;
6219 va_start(ap, zFormat);
6220 zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
6221 va_end(ap);
6222 if( zMsg1==0 ) zMsg1 = "";
6223 if( pCheck->zErrMsg ){
6224 char *zOld = pCheck->zErrMsg;
6225 pCheck->zErrMsg = 0;
6226 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
6227 sqlite3_free(zOld);
6228 }else{
6229 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
6230 }
6231 sqlite3_free(zMsg2);
6232}
6233#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6234
6235#ifndef SQLITE_OMIT_INTEGRITY_CHECK
6236/*
6237** Add 1 to the reference count for page iPage. If this is the second
6238** reference to the page, add an error message to pCheck->zErrMsg.
6239** Return 1 if there are 2 ore more references to the page and 0 if
6240** if this is the first reference to the page.
6241**
6242** Also check that the page number is in bounds.
6243*/
6244static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
6245 if( iPage==0 ) return 1;
6246 if( iPage>pCheck->nPage || iPage<0 ){
6247 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
6248 return 1;
6249 }
6250 if( pCheck->anRef[iPage]==1 ){
6251 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
6252 return 1;
6253 }
6254 return (pCheck->anRef[iPage]++)>1;
6255}
6256
6257#ifndef SQLITE_OMIT_AUTOVACUUM
6258/*
6259** Check that the entry in the pointer-map for page iChild maps to
6260** page iParent, pointer type ptrType. If not, append an error message
6261** to pCheck.
6262*/
6263static void checkPtrmap(
6264 IntegrityCk *pCheck, /* Integrity check context */
6265 Pgno iChild, /* Child page number */
6266 u8 eType, /* Expected pointer map type */
6267 Pgno iParent, /* Expected pointer map parent page number */
6268 char *zContext /* Context description (used for error msg) */
6269){
6270 int rc;
6271 u8 ePtrmapType;
6272 Pgno iPtrmapParent;
6273
6274 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6275 if( rc!=SQLITE_OK ){
6276 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6277 return;
6278 }
6279
6280 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6281 checkAppendMsg(pCheck, zContext,
6282 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6283 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6284 }
6285}
6286#endif
6287
6288/*
6289** Check the integrity of the freelist or of an overflow page list.
6290** Verify that the number of pages on the list is N.
6291*/
6292static void checkList(
6293 IntegrityCk *pCheck, /* Integrity checking context */
6294 int isFreeList, /* True for a freelist. False for overflow page list */
6295 int iPage, /* Page number for first page in the list */
6296 int N, /* Expected number of pages in the list */
6297 char *zContext /* Context for error messages */
6298){
6299 int i;
6300 int expected = N;
6301 int iFirst = iPage;
6302 while( N-- > 0 && pCheck->mxErr ){
6303 DbPage *pOvflPage;
6304 unsigned char *pOvflData;
6305 if( iPage<1 ){
6306 checkAppendMsg(pCheck, zContext,
6307 "%d of %d pages missing from overflow list starting at %d",
6308 N+1, expected, iFirst);
6309 break;
6310 }
6311 if( checkRef(pCheck, iPage, zContext) ) break;
6312 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
6313 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
6314 break;
6315 }
6316 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
6317 if( isFreeList ){
6318 int n = get4byte(&pOvflData[4]);
6319#ifndef SQLITE_OMIT_AUTOVACUUM
6320 if( pCheck->pBt->autoVacuum ){
6321 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6322 }
6323#endif
6324 if( n>pCheck->pBt->usableSize/4-8 ){
6325 checkAppendMsg(pCheck, zContext,
6326 "freelist leaf count too big on page %d", iPage);
6327 N--;
6328 }else{
6329 for(i=0; i<n; i++){
6330 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
6331#ifndef SQLITE_OMIT_AUTOVACUUM
6332 if( pCheck->pBt->autoVacuum ){
6333 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6334 }
6335#endif
6336 checkRef(pCheck, iFreePage, zContext);
6337 }
6338 N -= n;
6339 }
6340 }
6341#ifndef SQLITE_OMIT_AUTOVACUUM
6342 else{
6343 /* If this database supports auto-vacuum and iPage is not the last
6344 ** page in this overflow list, check that the pointer-map entry for
6345 ** the following page matches iPage.
6346 */
6347 if( pCheck->pBt->autoVacuum && N>0 ){
6348 i = get4byte(pOvflData);
6349 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6350 }
6351 }
6352#endif
6353 iPage = get4byte(pOvflData);
6354 sqlite3PagerUnref(pOvflPage);
6355 }
6356}
6357#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6358
6359#ifndef SQLITE_OMIT_INTEGRITY_CHECK
6360/*
6361** Do various sanity checks on a single page of a tree. Return
6362** the tree depth. Root pages return 0. Parents of root pages
6363** return 1, and so forth.
6364**
6365** These checks are done:
6366**
6367** 1. Make sure that cells and freeblocks do not overlap
6368** but combine to completely cover the page.
6369** NO 2. Make sure cell keys are in order.
6370** NO 3. Make sure no key is less than or equal to zLowerBound.
6371** NO 4. Make sure no key is greater than or equal to zUpperBound.
6372** 5. Check the integrity of overflow pages.
6373** 6. Recursively call checkTreePage on all children.
6374** 7. Verify that the depth of all children is the same.
6375** 8. Make sure this page is at least 33% full or else it is
6376** the root of the tree.
6377*/
6378static int checkTreePage(
6379 IntegrityCk *pCheck, /* Context for the sanity check */
6380 int iPage, /* Page number of the page to check */
6381 MemPage *pParent, /* Parent page */
6382 char *zParentContext /* Parent context */
6383){
6384 MemPage *pPage;
6385 int i, rc, depth, d2, pgno, cnt;
6386 int hdr, cellStart;
6387 int nCell;
6388 u8 *data;
6389 BtShared *pBt;
6390 int usableSize;
6391 char zContext[100];
6392 char *hit;
6393
6394 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
6395
6396 /* Check that the page exists
6397 */
6398 pBt = pCheck->pBt;
6399 usableSize = pBt->usableSize;
6400 if( iPage==0 ) return 0;
6401 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
6402 if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
6403 checkAppendMsg(pCheck, zContext,
6404 "unable to get the page. error code=%d", rc);
6405 return 0;
6406 }
6407 if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){
6408 checkAppendMsg(pCheck, zContext,
6409 "sqlite3BtreeInitPage() returns error code %d", rc);
6410 releasePage(pPage);
6411 return 0;
6412 }
6413
6414 /* Check out all the cells.
6415 */
6416 depth = 0;
6417 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
6418 u8 *pCell;
6419 int sz;
6420 CellInfo info;
6421
6422 /* Check payload overflow pages
6423 */
6424 sqlite3_snprintf(sizeof(zContext), zContext,
6425 "On tree page %d cell %d: ", iPage, i);
6426 pCell = findCell(pPage,i);
6427 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
6428 sz = info.nData;
6429 if( !pPage->intKey ) sz += info.nKey;
6430 assert( sz==info.nPayload );
6431 if( sz>info.nLocal ){
6432 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
6433 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6434#ifndef SQLITE_OMIT_AUTOVACUUM
6435 if( pBt->autoVacuum ){
6436 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
6437 }
6438#endif
6439 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
6440 }
6441
6442 /* Check sanity of left child page.
6443 */
6444 if( !pPage->leaf ){
6445 pgno = get4byte(pCell);
6446#ifndef SQLITE_OMIT_AUTOVACUUM
6447 if( pBt->autoVacuum ){
6448 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
6449 }
6450#endif
6451 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
6452 if( i>0 && d2!=depth ){
6453 checkAppendMsg(pCheck, zContext, "Child page depth differs");
6454 }
6455 depth = d2;
6456 }
6457 }
6458 if( !pPage->leaf ){
6459 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
6460 sqlite3_snprintf(sizeof(zContext), zContext,
6461 "On page %d at right child: ", iPage);
6462#ifndef SQLITE_OMIT_AUTOVACUUM
6463 if( pBt->autoVacuum ){
6464 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
6465 }
6466#endif
6467 checkTreePage(pCheck, pgno, pPage, zContext);
6468 }
6469
6470 /* Check for complete coverage of the page
6471 */
6472 data = pPage->aData;
6473 hdr = pPage->hdrOffset;
6474 hit = sqlite3MallocZero( usableSize );
6475 if( hit ){
6476 memset(hit, 1, get2byte(&data[hdr+5]));
6477 nCell = get2byte(&data[hdr+3]);
6478 cellStart = hdr + 12 - 4*pPage->leaf;
6479 for(i=0; i<nCell; i++){
6480 int pc = get2byte(&data[cellStart+i*2]);
6481 int size = cellSizePtr(pPage, &data[pc]);
6482 int j;
6483 if( (pc+size-1)>=usableSize || pc<0 ){
6484 checkAppendMsg(pCheck, 0,
6485 "Corruption detected in cell %d on page %d",i,iPage,0);
6486 }else{
6487 for(j=pc+size-1; j>=pc; j--) hit[j]++;
6488 }
6489 }
6490 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
6491 cnt++){
6492 int size = get2byte(&data[i+2]);
6493 int j;
6494 if( (i+size-1)>=usableSize || i<0 ){
6495 checkAppendMsg(pCheck, 0,
6496 "Corruption detected in cell %d on page %d",i,iPage,0);
6497 }else{
6498 for(j=i+size-1; j>=i; j--) hit[j]++;
6499 }
6500 i = get2byte(&data[i]);
6501 }
6502 for(i=cnt=0; i<usableSize; i++){
6503 if( hit[i]==0 ){
6504 cnt++;
6505 }else if( hit[i]>1 ){
6506 checkAppendMsg(pCheck, 0,
6507 "Multiple uses for byte %d of page %d", i, iPage);
6508 break;
6509 }
6510 }
6511 if( cnt!=data[hdr+7] ){
6512 checkAppendMsg(pCheck, 0,
6513 "Fragmented space is %d byte reported as %d on page %d",
6514 cnt, data[hdr+7], iPage);
6515 }
6516 }
6517 sqlite3_free(hit);
6518
6519 releasePage(pPage);
6520 return depth+1;
6521}
6522#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6523
6524#ifndef SQLITE_OMIT_INTEGRITY_CHECK
6525/*
6526** This routine does a complete check of the given BTree file. aRoot[] is
6527** an array of pages numbers were each page number is the root page of
6528** a table. nRoot is the number of entries in aRoot.
6529**
6530** If everything checks out, this routine returns NULL. If something is
6531** amiss, an error message is written into memory obtained from malloc()
6532** and a pointer to that error message is returned. The calling function
6533** is responsible for freeing the error message when it is done.
6534*/
6535char *sqlite3BtreeIntegrityCheck(
6536 Btree *p, /* The btree to be checked */
6537 int *aRoot, /* An array of root pages numbers for individual trees */
6538 int nRoot, /* Number of entries in aRoot[] */
6539 int mxErr, /* Stop reporting errors after this many */
6540 int *pnErr /* Write number of errors seen to this variable */
6541){
6542 int i;
6543 int nRef;
6544 IntegrityCk sCheck;
6545 BtShared *pBt = p->pBt;
6546
6547 sqlite3BtreeEnter(p);
6548 nRef = sqlite3PagerRefcount(pBt->pPager);
6549 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
6550 sqlite3BtreeLeave(p);
6551 return sqlite3StrDup("Unable to acquire a read lock on the database");
6552 }
6553 sCheck.pBt = pBt;
6554 sCheck.pPager = pBt->pPager;
6555 sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
6556 sCheck.mxErr = mxErr;
6557 sCheck.nErr = 0;
6558 *pnErr = 0;
6559#ifndef SQLITE_OMIT_AUTOVACUUM
6560 if( pBt->nTrunc!=0 ){
6561 sCheck.nPage = pBt->nTrunc;
6562 }
6563#endif
6564 if( sCheck.nPage==0 ){
6565 unlockBtreeIfUnused(pBt);
6566 sqlite3BtreeLeave(p);
6567 return 0;
6568 }
6569 sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
6570 if( !sCheck.anRef ){
6571 unlockBtreeIfUnused(pBt);
6572 *pnErr = 1;
6573 sqlite3BtreeLeave(p);
6574 return sqlite3MPrintf(p->pSqlite, "Unable to malloc %d bytes",
6575 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
6576 }
6577 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
6578 i = PENDING_BYTE_PAGE(pBt);
6579 if( i<=sCheck.nPage ){
6580 sCheck.anRef[i] = 1;
6581 }
6582 sCheck.zErrMsg = 0;
6583
6584 /* Check the integrity of the freelist
6585 */
6586 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
6587 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
6588
6589 /* Check all the tables.
6590 */
6591 for(i=0; i<nRoot && sCheck.mxErr; i++){
6592 if( aRoot[i]==0 ) continue;
6593#ifndef SQLITE_OMIT_AUTOVACUUM
6594 if( pBt->autoVacuum && aRoot[i]>1 ){
6595 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
6596 }
6597#endif
6598 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
6599 }
6600
6601 /* Make sure every page in the file is referenced
6602 */
6603 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
6604#ifdef SQLITE_OMIT_AUTOVACUUM
6605 if( sCheck.anRef[i]==0 ){
6606 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
6607 }
6608#else
6609 /* If the database supports auto-vacuum, make sure no tables contain
6610 ** references to pointer-map pages.
6611 */
6612 if( sCheck.anRef[i]==0 &&
6613 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
6614 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
6615 }
6616 if( sCheck.anRef[i]!=0 &&
6617 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
6618 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
6619 }
6620#endif
6621 }
6622
6623 /* Make sure this analysis did not leave any unref() pages
6624 */
6625 unlockBtreeIfUnused(pBt);
6626 if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
6627 checkAppendMsg(&sCheck, 0,
6628 "Outstanding page count goes from %d to %d during this analysis",
6629 nRef, sqlite3PagerRefcount(pBt->pPager)
6630 );
6631 }
6632
6633 /* Clean up and report errors.
6634 */
6635 sqlite3BtreeLeave(p);
6636 sqlite3_free(sCheck.anRef);
6637 *pnErr = sCheck.nErr;
6638 return sCheck.zErrMsg;
6639}
6640#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6641
6642/*
6643** Return the full pathname of the underlying database file.
6644**
6645** The pager filename is invariant as long as the pager is
6646** open so it is safe to access without the BtShared mutex.
6647*/
6648const char *sqlite3BtreeGetFilename(Btree *p){
6649 assert( p->pBt->pPager!=0 );
6650 return sqlite3PagerFilename(p->pBt->pPager);
6651}
6652
6653/*
6654** Return the pathname of the directory that contains the database file.
6655**
6656** The pager directory name is invariant as long as the pager is
6657** open so it is safe to access without the BtShared mutex.
6658*/
6659const char *sqlite3BtreeGetDirname(Btree *p){
6660 assert( p->pBt->pPager!=0 );
6661 return sqlite3PagerDirname(p->pBt->pPager);
6662}
6663
6664/*
6665** Return the pathname of the journal file for this database. The return
6666** value of this routine is the same regardless of whether the journal file
6667** has been created or not.
6668**
6669** The pager journal filename is invariant as long as the pager is
6670** open so it is safe to access without the BtShared mutex.
6671*/
6672const char *sqlite3BtreeGetJournalname(Btree *p){
6673 assert( p->pBt->pPager!=0 );
6674 return sqlite3PagerJournalname(p->pBt->pPager);
6675}
6676
6677#ifndef SQLITE_OMIT_VACUUM
6678/*
6679** Copy the complete content of pBtFrom into pBtTo. A transaction
6680** must be active for both files.
6681**
6682** The size of file pBtFrom may be reduced by this operation.
6683** If anything goes wrong, the transaction on pBtFrom is rolled back.
6684*/
6685static int btreeCopyFile(Btree *pTo, Btree *pFrom){
6686 int rc = SQLITE_OK;
6687 Pgno i, nPage, nToPage, iSkip;
6688
6689 BtShared *pBtTo = pTo->pBt;
6690 BtShared *pBtFrom = pFrom->pBt;
6691
6692 if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
6693 return SQLITE_ERROR;
6694 }
6695 if( pBtTo->pCursor ) return SQLITE_BUSY;
6696 nToPage = sqlite3PagerPagecount(pBtTo->pPager);
6697 nPage = sqlite3PagerPagecount(pBtFrom->pPager);
6698 iSkip = PENDING_BYTE_PAGE(pBtTo);
6699 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
6700 DbPage *pDbPage;
6701 if( i==iSkip ) continue;
6702 rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage);
6703 if( rc ) break;
6704 rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage));
6705 sqlite3PagerUnref(pDbPage);
6706 }
6707
6708 /* If the file is shrinking, journal the pages that are being truncated
6709 ** so that they can be rolled back if the commit fails.
6710 */
6711 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
6712 DbPage *pDbPage;
6713 if( i==iSkip ) continue;
6714 rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
6715 if( rc ) break;
6716 rc = sqlite3PagerWrite(pDbPage);
6717 sqlite3PagerDontWrite(pDbPage);
6718 /* Yeah. It seems wierd to call DontWrite() right after Write(). But
6719 ** that is because the names of those procedures do not exactly
6720 ** represent what they do. Write() really means "put this page in the
6721 ** rollback journal and mark it as dirty so that it will be written
6722 ** to the database file later." DontWrite() undoes the second part of
6723 ** that and prevents the page from being written to the database. The
6724 ** page is still on the rollback journal, though. And that is the whole
6725 ** point of this loop: to put pages on the rollback journal. */
6726 sqlite3PagerUnref(pDbPage);
6727 }
6728 if( !rc && nPage<nToPage ){
6729 rc = sqlite3PagerTruncate(pBtTo->pPager, nPage);
6730 }
6731
6732 if( rc ){
6733 sqlite3BtreeRollback(pTo);
6734 }
6735 return rc;
6736}
6737int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
6738 int rc;
6739 sqlite3BtreeEnter(pTo);
6740 sqlite3BtreeEnter(pFrom);
6741 rc = btreeCopyFile(pTo, pFrom);
6742 sqlite3BtreeLeave(pFrom);
6743 sqlite3BtreeLeave(pTo);
6744 return rc;
6745}
6746
6747#endif /* SQLITE_OMIT_VACUUM */
6748
6749/*
6750** Return non-zero if a transaction is active.
6751*/
6752int sqlite3BtreeIsInTrans(Btree *p){
6753 assert( p==0 || sqlite3_mutex_held(p->pSqlite->mutex) );
6754 return (p && (p->inTrans==TRANS_WRITE));
6755}
6756
6757/*
6758** Return non-zero if a statement transaction is active.
6759*/
6760int sqlite3BtreeIsInStmt(Btree *p){
6761 assert( sqlite3BtreeHoldsMutex(p) );
6762 return (p->pBt && p->pBt->inStmt);
6763}
6764
6765/*
6766** Return non-zero if a read (or write) transaction is active.
6767*/
6768int sqlite3BtreeIsInReadTrans(Btree *p){
6769 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
6770 return (p && (p->inTrans!=TRANS_NONE));
6771}
6772
6773/*
6774** This function returns a pointer to a blob of memory associated with
6775** a single shared-btree. The memory is used by client code for it's own
6776** purposes (for example, to store a high-level schema associated with
6777** the shared-btree). The btree layer manages reference counting issues.
6778**
6779** The first time this is called on a shared-btree, nBytes bytes of memory
6780** are allocated, zeroed, and returned to the caller. For each subsequent
6781** call the nBytes parameter is ignored and a pointer to the same blob
6782** of memory returned.
6783**
6784** Just before the shared-btree is closed, the function passed as the
6785** xFree argument when the memory allocation was made is invoked on the
6786** blob of allocated memory. This function should not call sqlite3_free()
6787** on the memory, the btree layer does that.
6788*/
6789void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
6790 BtShared *pBt = p->pBt;
6791 sqlite3BtreeEnter(p);
6792 if( !pBt->pSchema ){
6793 pBt->pSchema = sqlite3MallocZero(nBytes);
6794 pBt->xFreeSchema = xFree;
6795 }
6796 sqlite3BtreeLeave(p);
6797 return pBt->pSchema;
6798}
6799
6800/*
6801** Return true if another user of the same shared btree as the argument
6802** handle holds an exclusive lock on the sqlite_master table.
6803*/
6804int sqlite3BtreeSchemaLocked(Btree *p){
6805 int rc;
6806 assert( sqlite3_mutex_held(p->pSqlite->mutex) );
6807 sqlite3BtreeEnter(p);
6808 rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
6809 sqlite3BtreeLeave(p);
6810 return rc;
6811}
6812
6813
6814#ifndef SQLITE_OMIT_SHARED_CACHE
6815/*
6816** Obtain a lock on the table whose root page is iTab. The
6817** lock is a write lock if isWritelock is true or a read lock
6818** if it is false.
6819*/
6820int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
6821 int rc = SQLITE_OK;
6822 u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
6823 sqlite3BtreeEnter(p);
6824 rc = queryTableLock(p, iTab, lockType);
6825 if( rc==SQLITE_OK ){
6826 rc = lockTable(p, iTab, lockType);
6827 }
6828 sqlite3BtreeLeave(p);
6829 return rc;
6830}
6831#endif
6832
6833#ifndef SQLITE_OMIT_INCRBLOB
6834/*
6835** Argument pCsr must be a cursor opened for writing on an
6836** INTKEY table currently pointing at a valid table entry.
6837** This function modifies the data stored as part of that entry.
6838** Only the data content may only be modified, it is not possible
6839** to change the length of the data stored.
6840*/
6841int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
6842 assert( cursorHoldsMutex(pCsr) );
6843 assert( sqlite3_mutex_held(pCsr->pBtree->pSqlite->mutex) );
6844 assert(pCsr->isIncrblobHandle);
6845 if( pCsr->eState>=CURSOR_REQUIRESEEK ){
6846 if( pCsr->eState==CURSOR_FAULT ){
6847 return pCsr->skip;
6848 }else{
6849 return SQLITE_ABORT;
6850 }
6851 }
6852
6853 /* Check some preconditions:
6854 ** (a) the cursor is open for writing,
6855 ** (b) there is no read-lock on the table being modified and
6856 ** (c) the cursor points at a valid row of an intKey table.
6857 */
6858 if( !pCsr->wrFlag ){
6859 return SQLITE_READONLY;
6860 }
6861 assert( !pCsr->pBt->readOnly
6862 && pCsr->pBt->inTransaction==TRANS_WRITE );
6863 if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){
6864 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
6865 }
6866 if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
6867 return SQLITE_ERROR;
6868 }
6869
6870 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
6871}
6872
6873/*
6874** Set a flag on this cursor to cache the locations of pages from the
6875** overflow list for the current row. This is used by cursors opened
6876** for incremental blob IO only.
6877**
6878** This function sets a flag only. The actual page location cache
6879** (stored in BtCursor.aOverflow[]) is allocated and used by function
6880** accessPayload() (the worker function for sqlite3BtreeData() and
6881** sqlite3BtreePutData()).
6882*/
6883void sqlite3BtreeCacheOverflow(BtCursor *pCur){
6884 assert( cursorHoldsMutex(pCur) );
6885 assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
6886 assert(!pCur->isIncrblobHandle);
6887 assert(!pCur->aOverflow);
6888 pCur->isIncrblobHandle = 1;
6889}
6890#endif
diff --git a/libraries/sqlite/win32/btree.h b/libraries/sqlite/win32/btree.h
new file mode 100755
index 0000000..f7bc8e1
--- /dev/null
+++ b/libraries/sqlite/win32/btree.h
@@ -0,0 +1,204 @@
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.
10**
11*************************************************************************
12** This header file defines the interface that the sqlite B-Tree file
13** subsystem. See comments in the source code for a detailed description
14** of what each interface routine does.
15**
16** @(#) $Id: btree.h,v 1.93 2007/09/03 15:19:35 drh Exp $
17*/
18#ifndef _BTREE_H_
19#define _BTREE_H_
20
21/* TODO: This definition is just included so other modules compile. It
22** needs to be revisited.
23*/
24#define SQLITE_N_BTREE_META 10
25
26/*
27** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
28** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
29*/
30#ifndef SQLITE_DEFAULT_AUTOVACUUM
31 #define SQLITE_DEFAULT_AUTOVACUUM 0
32#endif
33
34#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
35#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
36#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
37
38/*
39** Forward declarations of structure
40*/
41typedef struct Btree Btree;
42typedef struct BtCursor BtCursor;
43typedef struct BtShared BtShared;
44typedef struct BtreeMutexArray BtreeMutexArray;
45
46/*
47** This structure records all of the Btrees that need to hold
48** a mutex before we enter sqlite3VdbeExec(). The Btrees are
49** are placed in aBtree[] in order of aBtree[]->pBt. That way,
50** we can always lock and unlock them all quickly.
51*/
52struct BtreeMutexArray {
53 int nMutex;
54 Btree *aBtree[SQLITE_MAX_ATTACHED+1];
55};
56
57
58int sqlite3BtreeOpen(
59 const char *zFilename, /* Name of database file to open */
60 sqlite3 *db, /* Associated database connection */
61 Btree **, /* Return open Btree* here */
62 int flags, /* Flags */
63 int vfsFlags /* Flags passed through to VFS open */
64);
65
66/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
67** following values.
68**
69** NOTE: These values must match the corresponding PAGER_ values in
70** pager.h.
71*/
72#define BTREE_OMIT_JOURNAL 1 /* Do not use journal. No argument */
73#define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
74#define BTREE_MEMORY 4 /* In-memory DB. No argument */
75#define BTREE_READONLY 8 /* Open the database in read-only mode */
76#define BTREE_READWRITE 16 /* Open for both reading and writing */
77#define BTREE_CREATE 32 /* Create the database if it does not exist */
78
79/* Additional values for the 4th argument of sqlite3BtreeOpen that
80** are not associated with PAGER_ values.
81*/
82#define BTREE_PRIVATE 64 /* Never share with other connections */
83
84int sqlite3BtreeClose(Btree*);
85int sqlite3BtreeSetBusyHandler(Btree*,BusyHandler*);
86int sqlite3BtreeSetCacheSize(Btree*,int);
87int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
88int sqlite3BtreeSyncDisabled(Btree*);
89int sqlite3BtreeSetPageSize(Btree*,int,int);
90int sqlite3BtreeGetPageSize(Btree*);
91int sqlite3BtreeMaxPageCount(Btree*,int);
92int sqlite3BtreeGetReserve(Btree*);
93int sqlite3BtreeSetAutoVacuum(Btree *, int);
94int sqlite3BtreeGetAutoVacuum(Btree *);
95int sqlite3BtreeBeginTrans(Btree*,int);
96int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
97int sqlite3BtreeCommitPhaseTwo(Btree*);
98int sqlite3BtreeCommit(Btree*);
99int sqlite3BtreeRollback(Btree*);
100int sqlite3BtreeBeginStmt(Btree*);
101int sqlite3BtreeCommitStmt(Btree*);
102int sqlite3BtreeRollbackStmt(Btree*);
103int sqlite3BtreeCreateTable(Btree*, int*, int flags);
104int sqlite3BtreeIsInTrans(Btree*);
105int sqlite3BtreeIsInStmt(Btree*);
106int sqlite3BtreeIsInReadTrans(Btree*);
107void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
108int sqlite3BtreeSchemaLocked(Btree *);
109int sqlite3BtreeLockTable(Btree *, int, u8);
110
111const char *sqlite3BtreeGetFilename(Btree *);
112const char *sqlite3BtreeGetDirname(Btree *);
113const char *sqlite3BtreeGetJournalname(Btree *);
114int sqlite3BtreeCopyFile(Btree *, Btree *);
115
116int sqlite3BtreeIncrVacuum(Btree *);
117
118/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
119** of the following flags:
120*/
121#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
122#define BTREE_ZERODATA 2 /* Table has keys only - no data */
123#define BTREE_LEAFDATA 4 /* Data stored in leaves only. Implies INTKEY */
124
125int sqlite3BtreeDropTable(Btree*, int, int*);
126int sqlite3BtreeClearTable(Btree*, int);
127int sqlite3BtreeGetMeta(Btree*, int idx, u32 *pValue);
128int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
129void sqlite3BtreeTripAllCursors(Btree*, int);
130
131int sqlite3BtreeCursor(
132 Btree*, /* BTree containing table to open */
133 int iTable, /* Index of root page */
134 int wrFlag, /* 1 for writing. 0 for read-only */
135 int(*)(void*,int,const void*,int,const void*), /* Key comparison function */
136 void*, /* First argument to compare function */
137 BtCursor **ppCursor /* Returned cursor */
138);
139
140int sqlite3BtreeCloseCursor(BtCursor*);
141int sqlite3BtreeMoveto(BtCursor*,const void *pKey,i64 nKey,int bias,int *pRes);
142int sqlite3BtreeDelete(BtCursor*);
143int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
144 const void *pData, int nData,
145 int nZero, int bias);
146int sqlite3BtreeFirst(BtCursor*, int *pRes);
147int sqlite3BtreeLast(BtCursor*, int *pRes);
148int sqlite3BtreeNext(BtCursor*, int *pRes);
149int sqlite3BtreeEof(BtCursor*);
150int sqlite3BtreeFlags(BtCursor*);
151int sqlite3BtreePrevious(BtCursor*, int *pRes);
152int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
153int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
154sqlite3 *sqlite3BtreeCursorDb(const BtCursor*);
155const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
156const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
157int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
158int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
159
160char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
161struct Pager *sqlite3BtreePager(Btree*);
162
163int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
164void sqlite3BtreeCacheOverflow(BtCursor *);
165
166#ifdef SQLITE_TEST
167int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
168void sqlite3BtreeCursorList(Btree*);
169int sqlite3BtreePageDump(Btree*, int, int recursive);
170#endif
171
172/*
173** If we are not using shared cache, then there is no need to
174** use mutexes to access the BtShared structures. So make the
175** Enter and Leave procedures no-ops.
176*/
177#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
178 void sqlite3BtreeEnter(Btree*);
179 void sqlite3BtreeLeave(Btree*);
180 int sqlite3BtreeHoldsMutex(Btree*);
181 void sqlite3BtreeEnterCursor(BtCursor*);
182 void sqlite3BtreeLeaveCursor(BtCursor*);
183 void sqlite3BtreeEnterAll(sqlite3*);
184 void sqlite3BtreeLeaveAll(sqlite3*);
185 int sqlite3BtreeHoldsAllMutexes(sqlite3*);
186 void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
187 void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
188 void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
189#else
190# define sqlite3BtreeEnter(X)
191# define sqlite3BtreeLeave(X)
192# define sqlite3BtreeHoldsMutex(X) 1
193# define sqlite3BtreeEnterCursor(X)
194# define sqlite3BtreeLeaveCursor(X)
195# define sqlite3BtreeEnterAll(X)
196# define sqlite3BtreeLeaveAll(X)
197# define sqlite3BtreeHoldsAllMutexes(X) 1
198# define sqlite3BtreeMutexArrayEnter(X)
199# define sqlite3BtreeMutexArrayLeave(X)
200# define sqlite3BtreeMutexArrayInsert(X,Y)
201#endif
202
203
204#endif /* _BTREE_H_ */
diff --git a/libraries/sqlite/win32/btreeInt.h b/libraries/sqlite/win32/btreeInt.h
new file mode 100755
index 0000000..09f1474
--- /dev/null
+++ b/libraries/sqlite/win32/btreeInt.h
@@ -0,0 +1,648 @@
1/*
2** 2004 April 6
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.
10**
11*************************************************************************
12** $Id: btreeInt.h,v 1.13 2007/08/30 01:19:59 drh Exp $
13**
14** This file implements a external (disk-based) database using BTrees.
15** For a detailed discussion of BTrees, refer to
16**
17** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
18** "Sorting And Searching", pages 473-480. Addison-Wesley
19** Publishing Company, Reading, Massachusetts.
20**
21** The basic idea is that each page of the file contains N database
22** entries and N+1 pointers to subpages.
23**
24** ----------------------------------------------------------------
25** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
26** ----------------------------------------------------------------
27**
28** All of the keys on the page that Ptr(0) points to have values less
29** than Key(0). All of the keys on page Ptr(1) and its subpages have
30** values greater than Key(0) and less than Key(1). All of the keys
31** on Ptr(N) and its subpages have values greater than Key(N-1). And
32** so forth.
33**
34** Finding a particular key requires reading O(log(M)) pages from the
35** disk where M is the number of entries in the tree.
36**
37** In this implementation, a single file can hold one or more separate
38** BTrees. Each BTree is identified by the index of its root page. The
39** key and data for any entry are combined to form the "payload". A
40** fixed amount of payload can be carried directly on the database
41** page. If the payload is larger than the preset amount then surplus
42** bytes are stored on overflow pages. The payload for an entry
43** and the preceding pointer are combined to form a "Cell". Each
44** page has a small header which contains the Ptr(N) pointer and other
45** information such as the size of key and data.
46**
47** FORMAT DETAILS
48**
49** The file is divided into pages. The first page is called page 1,
50** the second is page 2, and so forth. A page number of zero indicates
51** "no such page". The page size can be anything between 512 and 65536.
52** Each page can be either a btree page, a freelist page or an overflow
53** page.
54**
55** The first page is always a btree page. The first 100 bytes of the first
56** page contain a special header (the "file header") that describes the file.
57** The format of the file header is as follows:
58**
59** OFFSET SIZE DESCRIPTION
60** 0 16 Header string: "SQLite format 3\000"
61** 16 2 Page size in bytes.
62** 18 1 File format write version
63** 19 1 File format read version
64** 20 1 Bytes of unused space at the end of each page
65** 21 1 Max embedded payload fraction
66** 22 1 Min embedded payload fraction
67** 23 1 Min leaf payload fraction
68** 24 4 File change counter
69** 28 4 Reserved for future use
70** 32 4 First freelist page
71** 36 4 Number of freelist pages in the file
72** 40 60 15 4-byte meta values passed to higher layers
73**
74** All of the integer values are big-endian (most significant byte first).
75**
76** The file change counter is incremented when the database is changed
77** This counter allows other processes to know when the file has changed
78** and thus when they need to flush their cache.
79**
80** The max embedded payload fraction is the amount of the total usable
81** space in a page that can be consumed by a single cell for standard
82** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
83** is to limit the maximum cell size so that at least 4 cells will fit
84** on one page. Thus the default max embedded payload fraction is 64.
85**
86** If the payload for a cell is larger than the max payload, then extra
87** payload is spilled to overflow pages. Once an overflow page is allocated,
88** as many bytes as possible are moved into the overflow pages without letting
89** the cell size drop below the min embedded payload fraction.
90**
91** The min leaf payload fraction is like the min embedded payload fraction
92** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
93** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
94** not specified in the header.
95**
96** Each btree pages is divided into three sections: The header, the
97** cell pointer array, and the cell content area. Page 1 also has a 100-byte
98** file header that occurs before the page header.
99**
100** |----------------|
101** | file header | 100 bytes. Page 1 only.
102** |----------------|
103** | page header | 8 bytes for leaves. 12 bytes for interior nodes
104** |----------------|
105** | cell pointer | | 2 bytes per cell. Sorted order.
106** | array | | Grows downward
107** | | v
108** |----------------|
109** | unallocated |
110** | space |
111** |----------------| ^ Grows upwards
112** | cell content | | Arbitrary order interspersed with freeblocks.
113** | area | | and free space fragments.
114** |----------------|
115**
116** The page headers looks like this:
117**
118** OFFSET SIZE DESCRIPTION
119** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
120** 1 2 byte offset to the first freeblock
121** 3 2 number of cells on this page
122** 5 2 first byte of the cell content area
123** 7 1 number of fragmented free bytes
124** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
125**
126** The flags define the format of this btree page. The leaf flag means that
127** this page has no children. The zerodata flag means that this page carries
128** only keys and no data. The intkey flag means that the key is a integer
129** which is stored in the key size entry of the cell header rather than in
130** the payload area.
131**
132** The cell pointer array begins on the first byte after the page header.
133** The cell pointer array contains zero or more 2-byte numbers which are
134** offsets from the beginning of the page to the cell content in the cell
135** content area. The cell pointers occur in sorted order. The system strives
136** to keep free space after the last cell pointer so that new cells can
137** be easily added without having to defragment the page.
138**
139** Cell content is stored at the very end of the page and grows toward the
140** beginning of the page.
141**
142** Unused space within the cell content area is collected into a linked list of
143** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
144** to the first freeblock is given in the header. Freeblocks occur in
145** increasing order. Because a freeblock must be at least 4 bytes in size,
146** any group of 3 or fewer unused bytes in the cell content area cannot
147** exist on the freeblock chain. A group of 3 or fewer free bytes is called
148** a fragment. The total number of bytes in all fragments is recorded.
149** in the page header at offset 7.
150**
151** SIZE DESCRIPTION
152** 2 Byte offset of the next freeblock
153** 2 Bytes in this freeblock
154**
155** Cells are of variable length. Cells are stored in the cell content area at
156** the end of the page. Pointers to the cells are in the cell pointer array
157** that immediately follows the page header. Cells is not necessarily
158** contiguous or in order, but cell pointers are contiguous and in order.
159**
160** Cell content makes use of variable length integers. A variable
161** length integer is 1 to 9 bytes where the lower 7 bits of each
162** byte are used. The integer consists of all bytes that have bit 8 set and
163** the first byte with bit 8 clear. The most significant byte of the integer
164** appears first. A variable-length integer may not be more than 9 bytes long.
165** As a special case, all 8 bytes of the 9th byte are used as data. This
166** allows a 64-bit integer to be encoded in 9 bytes.
167**
168** 0x00 becomes 0x00000000
169** 0x7f becomes 0x0000007f
170** 0x81 0x00 becomes 0x00000080
171** 0x82 0x00 becomes 0x00000100
172** 0x80 0x7f becomes 0x0000007f
173** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
174** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
175**
176** Variable length integers are used for rowids and to hold the number of
177** bytes of key and data in a btree cell.
178**
179** The content of a cell looks like this:
180**
181** SIZE DESCRIPTION
182** 4 Page number of the left child. Omitted if leaf flag is set.
183** var Number of bytes of data. Omitted if the zerodata flag is set.
184** var Number of bytes of key. Or the key itself if intkey flag is set.
185** * Payload
186** 4 First page of the overflow chain. Omitted if no overflow
187**
188** Overflow pages form a linked list. Each page except the last is completely
189** filled with data (pagesize - 4 bytes). The last page can have as little
190** as 1 byte of data.
191**
192** SIZE DESCRIPTION
193** 4 Page number of next overflow page
194** * Data
195**
196** Freelist pages come in two subtypes: trunk pages and leaf pages. The
197** file header points to the first in a linked list of trunk page. Each trunk
198** page points to multiple leaf pages. The content of a leaf page is
199** unspecified. A trunk page looks like this:
200**
201** SIZE DESCRIPTION
202** 4 Page number of next trunk page
203** 4 Number of leaf pointers on this page
204** * zero or more pages numbers of leaves
205*/
206#include "sqliteInt.h"
207#include "pager.h"
208#include "btree.h"
209#include "os.h"
210#include <assert.h>
211
212/* Round up a number to the next larger multiple of 8. This is used
213** to force 8-byte alignment on 64-bit architectures.
214*/
215#define ROUND8(x) ((x+7)&~7)
216
217
218/* The following value is the maximum cell size assuming a maximum page
219** size give above.
220*/
221#define MX_CELL_SIZE(pBt) (pBt->pageSize-8)
222
223/* The maximum number of cells on a single page of the database. This
224** assumes a minimum cell size of 3 bytes. Such small cells will be
225** exceedingly rare, but they are possible.
226*/
227#define MX_CELL(pBt) ((pBt->pageSize-8)/3)
228
229/* Forward declarations */
230typedef struct MemPage MemPage;
231typedef struct BtLock BtLock;
232
233/*
234** This is a magic string that appears at the beginning of every
235** SQLite database in order to identify the file as a real database.
236**
237** You can change this value at compile-time by specifying a
238** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
239** header must be exactly 16 bytes including the zero-terminator so
240** the string itself should be 15 characters long. If you change
241** the header, then your custom library will not be able to read
242** databases generated by the standard tools and the standard tools
243** will not be able to read databases created by your custom library.
244*/
245#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
246# define SQLITE_FILE_HEADER "SQLite format 3"
247#endif
248
249/*
250** Page type flags. An ORed combination of these flags appear as the
251** first byte of on-disk image of every BTree page.
252*/
253#define PTF_INTKEY 0x01
254#define PTF_ZERODATA 0x02
255#define PTF_LEAFDATA 0x04
256#define PTF_LEAF 0x08
257
258/*
259** As each page of the file is loaded into memory, an instance of the following
260** structure is appended and initialized to zero. This structure stores
261** information about the page that is decoded from the raw file page.
262**
263** The pParent field points back to the parent page. This allows us to
264** walk up the BTree from any leaf to the root. Care must be taken to
265** unref() the parent page pointer when this page is no longer referenced.
266** The pageDestructor() routine handles that chore.
267**
268** Access to all fields of this structure is controlled by the mutex
269** stored in MemPage.pBt->mutex.
270*/
271struct MemPage {
272 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
273 u8 idxShift; /* True if Cell indices have changed */
274 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
275 u8 intKey; /* True if intkey flag is set */
276 u8 leaf; /* True if leaf flag is set */
277 u8 zeroData; /* True if table stores keys only */
278 u8 leafData; /* True if tables stores data on leaves only */
279 u8 hasData; /* True if this page stores data */
280 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
281 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
282 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
283 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
284 u16 cellOffset; /* Index in aData of first cell pointer */
285 u16 idxParent; /* Index in parent of this node */
286 u16 nFree; /* Number of free bytes on the page */
287 u16 nCell; /* Number of cells on this page, local and ovfl */
288 struct _OvflCell { /* Cells that will not fit on aData[] */
289 u8 *pCell; /* Pointers to the body of the overflow cell */
290 u16 idx; /* Insert this cell before idx-th non-overflow cell */
291 } aOvfl[5];
292 BtShared *pBt; /* Pointer to BtShared that this page is part of */
293 u8 *aData; /* Pointer to disk image of the page data */
294 DbPage *pDbPage; /* Pager page handle */
295 Pgno pgno; /* Page number for this page */
296 MemPage *pParent; /* The parent of this page. NULL for root */
297};
298
299/*
300** The in-memory image of a disk page has the auxiliary information appended
301** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
302** that extra information.
303*/
304#define EXTRA_SIZE sizeof(MemPage)
305
306/* A Btree handle
307**
308** A database connection contains a pointer to an instance of
309** this object for every database file that it has open. This structure
310** is opaque to the database connection. The database connection cannot
311** see the internals of this structure and only deals with pointers to
312** this structure.
313**
314** For some database files, the same underlying database cache might be
315** shared between multiple connections. In that case, each contection
316** has it own pointer to this object. But each instance of this object
317** points to the same BtShared object. The database cache and the
318** schema associated with the database file are all contained within
319** the BtShared object.
320**
321** All fields in this structure are accessed under sqlite3.mutex.
322** The pBt pointer itself may not be changed while there exists cursors
323** in the referenced BtShared that point back to this Btree since those
324** cursors have to do go through this Btree to find their BtShared and
325** they often do so without holding sqlite3.mutex.
326*/
327struct Btree {
328 sqlite3 *pSqlite; /* The database connection holding this btree */
329 BtShared *pBt; /* Sharable content of this btree */
330 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
331 u8 sharable; /* True if we can share pBt with other pSqlite */
332 u8 locked; /* True if pSqlite currently has pBt locked */
333 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
334 Btree *pNext; /* List of other sharable Btrees from the same pSqlite */
335 Btree *pPrev; /* Back pointer of the same list */
336};
337
338/*
339** Btree.inTrans may take one of the following values.
340**
341** If the shared-data extension is enabled, there may be multiple users
342** of the Btree structure. At most one of these may open a write transaction,
343** but any number may have active read transactions.
344*/
345#define TRANS_NONE 0
346#define TRANS_READ 1
347#define TRANS_WRITE 2
348
349/*
350** An instance of this object represents a single database file.
351**
352** A single database file can be in use as the same time by two
353** or more database connections. When two or more connections are
354** sharing the same database file, each connection has it own
355** private Btree object for the file and each of those Btrees points
356** to this one BtShared object. BtShared.nRef is the number of
357** connections currently sharing this database file.
358**
359** Fields in this structure are accessed under the BtShared.mutex
360** mutex, except for nRef and pNext which are accessed under the
361** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
362** may not be modified once it is initially set as long as nRef>0.
363** The pSchema field may be set once under BtShared.mutex and
364** thereafter is unchanged as long as nRef>0.
365*/
366struct BtShared {
367 Pager *pPager; /* The page cache */
368 BtCursor *pCursor; /* A list of all open cursors */
369 MemPage *pPage1; /* First page of the database */
370 u8 inStmt; /* True if we are in a statement subtransaction */
371 u8 readOnly; /* True if the underlying file is readonly */
372 u8 maxEmbedFrac; /* Maximum payload as % of total page size */
373 u8 minEmbedFrac; /* Minimum payload as % of total page size */
374 u8 minLeafFrac; /* Minimum leaf payload as % of total page size */
375 u8 pageSizeFixed; /* True if the page size can no longer be changed */
376#ifndef SQLITE_OMIT_AUTOVACUUM
377 u8 autoVacuum; /* True if auto-vacuum is enabled */
378 u8 incrVacuum; /* True if incr-vacuum is enabled */
379 Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */
380#endif
381 u16 pageSize; /* Total number of bytes on a page */
382 u16 usableSize; /* Number of usable bytes on each page */
383 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
384 int minLocal; /* Minimum local payload in non-LEAFDATA tables */
385 int maxLeaf; /* Maximum local payload in a LEAFDATA table */
386 int minLeaf; /* Minimum local payload in a LEAFDATA table */
387 BusyHandler *pBusyHandler; /* Callback for when there is lock contention */
388 u8 inTransaction; /* Transaction state */
389 int nTransaction; /* Number of open transactions (read + write) */
390 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
391 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
392 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
393#ifndef SQLITE_OMIT_SHARED_CACHE
394 int nRef; /* Number of references to this structure */
395 BtShared *pNext; /* Next on a list of sharable BtShared structs */
396 BtLock *pLock; /* List of locks held on this shared-btree struct */
397#endif
398};
399
400/*
401** An instance of the following structure is used to hold information
402** about a cell. The parseCellPtr() function fills in this structure
403** based on information extract from the raw disk page.
404*/
405typedef struct CellInfo CellInfo;
406struct CellInfo {
407 u8 *pCell; /* Pointer to the start of cell content */
408 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
409 u32 nData; /* Number of bytes of data */
410 u32 nPayload; /* Total amount of payload */
411 u16 nHeader; /* Size of the cell content header in bytes */
412 u16 nLocal; /* Amount of payload held locally */
413 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
414 u16 nSize; /* Size of the cell content on the main b-tree page */
415};
416
417/*
418** A cursor is a pointer to a particular entry within a particular
419** b-tree within a database file.
420**
421** The entry is identified by its MemPage and the index in
422** MemPage.aCell[] of the entry.
423**
424** When a single database file can shared by two more database connections,
425** but cursors cannot be shared. Each cursor is associated with a
426** particular database connection identified BtCursor.pBtree.pSqlite.
427**
428** Fields in this structure are accessed under the BtShared.mutex
429** found at self->pBt->mutex.
430*/
431struct BtCursor {
432 Btree *pBtree; /* The Btree to which this cursor belongs */
433 BtShared *pBt; /* The BtShared this cursor points to */
434 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
435 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
436 void *pArg; /* First arg to xCompare() */
437 Pgno pgnoRoot; /* The root page of this tree */
438 MemPage *pPage; /* Page that contains the entry */
439 int idx; /* Index of the entry in pPage->aCell[] */
440 CellInfo info; /* A parse of the cell we are pointing at */
441 u8 wrFlag; /* True if writable */
442 u8 eState; /* One of the CURSOR_XXX constants (see below) */
443 void *pKey; /* Saved key that was cursor's last known position */
444 i64 nKey; /* Size of pKey, or last integer key */
445 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
446#ifndef SQLITE_OMIT_INCRBLOB
447 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
448 Pgno *aOverflow; /* Cache of overflow page locations */
449#endif
450};
451
452/*
453** Potential values for BtCursor.eState.
454**
455** CURSOR_VALID:
456** Cursor points to a valid entry. getPayload() etc. may be called.
457**
458** CURSOR_INVALID:
459** Cursor does not point to a valid entry. This can happen (for example)
460** because the table is empty or because BtreeCursorFirst() has not been
461** called.
462**
463** CURSOR_REQUIRESEEK:
464** The table that this cursor was opened on still exists, but has been
465** modified since the cursor was last used. The cursor position is saved
466** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
467** this state, restoreOrClearCursorPosition() can be called to attempt to
468** seek the cursor to the saved position.
469**
470** CURSOR_FAULT:
471** A unrecoverable error (an I/O error or a malloc failure) has occurred
472** on a different connection that shares the BtShared cache with this
473** cursor. The error has left the cache in an inconsistent state.
474** Do nothing else with this cursor. Any attempt to use the cursor
475** should return the error code stored in BtCursor.skip
476*/
477#define CURSOR_INVALID 0
478#define CURSOR_VALID 1
479#define CURSOR_REQUIRESEEK 2
480#define CURSOR_FAULT 3
481
482/*
483** The TRACE macro will print high-level status information about the
484** btree operation when the global variable sqlite3_btree_trace is
485** enabled.
486*/
487#if SQLITE_TEST
488# define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); }
489#else
490# define TRACE(X)
491#endif
492
493/*
494** Routines to read and write variable-length integers. These used to
495** be defined locally, but now we use the varint routines in the util.c
496** file.
497*/
498#define getVarint sqlite3GetVarint
499#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
500#define putVarint sqlite3PutVarint
501
502/* The database page the PENDING_BYTE occupies. This page is never used.
503** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
504** should possibly be consolidated (presumably in pager.h).
505**
506** If disk I/O is omitted (meaning that the database is stored purely
507** in memory) then there is no pending byte.
508*/
509#ifdef SQLITE_OMIT_DISKIO
510# define PENDING_BYTE_PAGE(pBt) 0x7fffffff
511#else
512# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
513#endif
514
515/*
516** A linked list of the following structures is stored at BtShared.pLock.
517** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
518** is opened on the table with root page BtShared.iTable. Locks are removed
519** from this list when a transaction is committed or rolled back, or when
520** a btree handle is closed.
521*/
522struct BtLock {
523 Btree *pBtree; /* Btree handle holding this lock */
524 Pgno iTable; /* Root page of table */
525 u8 eLock; /* READ_LOCK or WRITE_LOCK */
526 BtLock *pNext; /* Next in BtShared.pLock list */
527};
528
529/* Candidate values for BtLock.eLock */
530#define READ_LOCK 1
531#define WRITE_LOCK 2
532
533/*
534** These macros define the location of the pointer-map entry for a
535** database page. The first argument to each is the number of usable
536** bytes on each page of the database (often 1024). The second is the
537** page number to look up in the pointer map.
538**
539** PTRMAP_PAGENO returns the database page number of the pointer-map
540** page that stores the required pointer. PTRMAP_PTROFFSET returns
541** the offset of the requested map entry.
542**
543** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
544** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
545** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
546** this test.
547*/
548#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
549#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
550#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
551
552/*
553** The pointer map is a lookup table that identifies the parent page for
554** each child page in the database file. The parent page is the page that
555** contains a pointer to the child. Every page in the database contains
556** 0 or 1 parent pages. (In this context 'database page' refers
557** to any page that is not part of the pointer map itself.) Each pointer map
558** entry consists of a single byte 'type' and a 4 byte parent page number.
559** The PTRMAP_XXX identifiers below are the valid types.
560**
561** The purpose of the pointer map is to facility moving pages from one
562** position in the file to another as part of autovacuum. When a page
563** is moved, the pointer in its parent must be updated to point to the
564** new location. The pointer map is used to locate the parent page quickly.
565**
566** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
567** used in this case.
568**
569** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
570** is not used in this case.
571**
572** PTRMAP_OVERFLOW1: The database page is the first page in a list of
573** overflow pages. The page number identifies the page that
574** contains the cell with a pointer to this overflow page.
575**
576** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
577** overflow pages. The page-number identifies the previous
578** page in the overflow page list.
579**
580** PTRMAP_BTREE: The database page is a non-root btree page. The page number
581** identifies the parent page in the btree.
582*/
583#define PTRMAP_ROOTPAGE 1
584#define PTRMAP_FREEPAGE 2
585#define PTRMAP_OVERFLOW1 3
586#define PTRMAP_OVERFLOW2 4
587#define PTRMAP_BTREE 5
588
589/* A bunch of assert() statements to check the transaction state variables
590** of handle p (type Btree*) are internally consistent.
591*/
592#define btreeIntegrity(p) \
593 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
594 assert( p->pBt->inTransaction>=p->inTrans );
595
596
597/*
598** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
599** if the database supports auto-vacuum or not. Because it is used
600** within an expression that is an argument to another macro
601** (sqliteMallocRaw), it is not possible to use conditional compilation.
602** So, this macro is defined instead.
603*/
604#ifndef SQLITE_OMIT_AUTOVACUUM
605#define ISAUTOVACUUM (pBt->autoVacuum)
606#else
607#define ISAUTOVACUUM 0
608#endif
609
610
611/*
612** This structure is passed around through all the sanity checking routines
613** in order to keep track of some global state information.
614*/
615typedef struct IntegrityCk IntegrityCk;
616struct IntegrityCk {
617 BtShared *pBt; /* The tree being checked out */
618 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
619 int nPage; /* Number of pages in the database */
620 int *anRef; /* Number of times each page is referenced */
621 int mxErr; /* Stop accumulating errors when this reaches zero */
622 char *zErrMsg; /* An error message. NULL if no errors seen. */
623 int nErr; /* Number of messages written to zErrMsg so far */
624};
625
626/*
627** Read or write a two- and four-byte big-endian integer values.
628*/
629#define get2byte(x) ((x)[0]<<8 | (x)[1])
630#define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v))
631#define get4byte sqlite3Get4byte
632#define put4byte sqlite3Put4byte
633
634/*
635** Internal routines that should be accessed by the btree layer only.
636*/
637int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
638int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent);
639void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
640void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
641#ifdef SQLITE_TEST
642u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell);
643#endif
644int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur);
645void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
646void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
647int sqlite3BtreeIsRootPage(MemPage *pPage);
648void sqlite3BtreeMoveToParent(BtCursor *pCur);
diff --git a/libraries/sqlite/win32/build.c b/libraries/sqlite/win32/build.c
new file mode 100755
index 0000000..f46d28e
--- /dev/null
+++ b/libraries/sqlite/win32/build.c
@@ -0,0 +1,3409 @@
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.
10**
11*************************************************************************
12** This file contains C code routines that are called by the SQLite parser
13** when syntax rules are reduced. The routines in this file handle the
14** following kinds of SQL syntax:
15**
16** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
20** creating ID lists
21** BEGIN TRANSACTION
22** COMMIT
23** ROLLBACK
24**
25** $Id: build.c,v 1.444 2007/09/03 15:19:35 drh Exp $
26*/
27#include "sqliteInt.h"
28#include <ctype.h>
29
30/*
31** This routine is called when a new SQL statement is beginning to
32** be parsed. Initialize the pParse structure as needed.
33*/
34void sqlite3BeginParse(Parse *pParse, int explainFlag){
35 pParse->explain = explainFlag;
36 pParse->nVar = 0;
37}
38
39#ifndef SQLITE_OMIT_SHARED_CACHE
40/*
41** The TableLock structure is only used by the sqlite3TableLock() and
42** codeTableLocks() functions.
43*/
44struct TableLock {
45 int iDb; /* The database containing the table to be locked */
46 int iTab; /* The root page of the table to be locked */
47 u8 isWriteLock; /* True for write lock. False for a read lock */
48 const char *zName; /* Name of the table */
49};
50
51/*
52** Record the fact that we want to lock a table at run-time.
53**
54** The table to be locked has root page iTab and is found in database iDb.
55** A read or a write lock can be taken depending on isWritelock.
56**
57** This routine just records the fact that the lock is desired. The
58** code to make the lock occur is generated by a later call to
59** codeTableLocks() which occurs during sqlite3FinishCoding().
60*/
61void sqlite3TableLock(
62 Parse *pParse, /* Parsing context */
63 int iDb, /* Index of the database containing the table to lock */
64 int iTab, /* Root page number of the table to be locked */
65 u8 isWriteLock, /* True for a write lock */
66 const char *zName /* Name of the table to be locked */
67){
68 int i;
69 int nBytes;
70 TableLock *p;
71
72 if( iDb<0 ){
73 return;
74 }
75
76 for(i=0; i<pParse->nTableLock; i++){
77 p = &pParse->aTableLock[i];
78 if( p->iDb==iDb && p->iTab==iTab ){
79 p->isWriteLock = (p->isWriteLock || isWriteLock);
80 return;
81 }
82 }
83
84 nBytes = sizeof(TableLock) * (pParse->nTableLock+1);
85 pParse->aTableLock =
86 sqlite3DbReallocOrFree(pParse->db, pParse->aTableLock, nBytes);
87 if( pParse->aTableLock ){
88 p = &pParse->aTableLock[pParse->nTableLock++];
89 p->iDb = iDb;
90 p->iTab = iTab;
91 p->isWriteLock = isWriteLock;
92 p->zName = zName;
93 }else{
94 pParse->nTableLock = 0;
95 pParse->db->mallocFailed = 1;
96 }
97}
98
99/*
100** Code an OP_TableLock instruction for each table locked by the
101** statement (configured by calls to sqlite3TableLock()).
102*/
103static void codeTableLocks(Parse *pParse){
104 int i;
105 Vdbe *pVdbe;
106
107 if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){
108 return;
109 }
110
111 for(i=0; i<pParse->nTableLock; i++){
112 TableLock *p = &pParse->aTableLock[i];
113 int p1 = p->iDb;
114 if( p->isWriteLock ){
115 p1 = -1*(p1+1);
116 }
117 sqlite3VdbeOp3(pVdbe, OP_TableLock, p1, p->iTab, p->zName, P3_STATIC);
118 }
119}
120#else
121 #define codeTableLocks(x)
122#endif
123
124/*
125** This routine is called after a single SQL statement has been
126** parsed and a VDBE program to execute that statement has been
127** prepared. This routine puts the finishing touches on the
128** VDBE program and resets the pParse structure for the next
129** parse.
130**
131** Note that if an error occurred, it might be the case that
132** no VDBE code was generated.
133*/
134void sqlite3FinishCoding(Parse *pParse){
135 sqlite3 *db;
136 Vdbe *v;
137
138 db = pParse->db;
139 if( db->mallocFailed ) return;
140 if( pParse->nested ) return;
141 if( !pParse->pVdbe ){
142 if( pParse->rc==SQLITE_OK && pParse->nErr ){
143 pParse->rc = SQLITE_ERROR;
144 return;
145 }
146 }
147
148 /* Begin by generating some termination code at the end of the
149 ** vdbe program
150 */
151 v = sqlite3GetVdbe(pParse);
152 if( v ){
153 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
154
155 /* The cookie mask contains one bit for each database file open.
156 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
157 ** set for each database that is used. Generate code to start a
158 ** transaction on each used database and to verify the schema cookie
159 ** on each used database.
160 */
161 if( pParse->cookieGoto>0 ){
162 u32 mask;
163 int iDb;
164 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
165 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
166 if( (mask & pParse->cookieMask)==0 ) continue;
167 sqlite3VdbeUsesBtree(v, iDb);
168 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
169 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
170 }
171#ifndef SQLITE_OMIT_VIRTUALTABLE
172 if( pParse->pVirtualLock ){
173 char *vtab = (char *)pParse->pVirtualLock->pVtab;
174 sqlite3VdbeOp3(v, OP_VBegin, 0, 0, vtab, P3_VTAB);
175 }
176#endif
177
178 /* Once all the cookies have been verified and transactions opened,
179 ** obtain the required table-locks. This is a no-op unless the
180 ** shared-cache feature is enabled.
181 */
182 codeTableLocks(pParse);
183 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
184 }
185
186#ifndef SQLITE_OMIT_TRACE
187 /* Add a No-op that contains the complete text of the compiled SQL
188 ** statement as its P3 argument. This does not change the functionality
189 ** of the program.
190 **
191 ** This is used to implement sqlite3_trace().
192 */
193 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
194#endif /* SQLITE_OMIT_TRACE */
195 }
196
197
198 /* Get the VDBE program ready for execution
199 */
200 if( v && pParse->nErr==0 && !db->mallocFailed ){
201#ifdef SQLITE_DEBUG
202 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
203 sqlite3VdbeTrace(v, trace);
204#endif
205 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
206 pParse->nTab+3, pParse->explain);
207 pParse->rc = SQLITE_DONE;
208 pParse->colNamesSet = 0;
209 }else if( pParse->rc==SQLITE_OK ){
210 pParse->rc = SQLITE_ERROR;
211 }
212 pParse->nTab = 0;
213 pParse->nMem = 0;
214 pParse->nSet = 0;
215 pParse->nVar = 0;
216 pParse->cookieMask = 0;
217 pParse->cookieGoto = 0;
218}
219
220/*
221** Run the parser and code generator recursively in order to generate
222** code for the SQL statement given onto the end of the pParse context
223** currently under construction. When the parser is run recursively
224** this way, the final OP_Halt is not appended and other initialization
225** and finalization steps are omitted because those are handling by the
226** outermost parser.
227**
228** Not everything is nestable. This facility is designed to permit
229** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
230** care if you decide to try to use this routine for some other purposes.
231*/
232void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
233 va_list ap;
234 char *zSql;
235# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
236 char saveBuf[SAVE_SZ];
237
238 if( pParse->nErr ) return;
239 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
240 va_start(ap, zFormat);
241 zSql = sqlite3VMPrintf(pParse->db, zFormat, ap);
242 va_end(ap);
243 if( zSql==0 ){
244 pParse->db->mallocFailed = 1;
245 return; /* A malloc must have failed */
246 }
247 pParse->nested++;
248 memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
249 memset(&pParse->nVar, 0, SAVE_SZ);
250 sqlite3RunParser(pParse, zSql, 0);
251 sqlite3_free(zSql);
252 memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
253 pParse->nested--;
254}
255
256/*
257** Locate the in-memory structure that describes a particular database
258** table given the name of that table and (optionally) the name of the
259** database containing the table. Return NULL if not found.
260**
261** If zDatabase is 0, all databases are searched for the table and the
262** first matching table is returned. (No checking for duplicate table
263** names is done.) The search order is TEMP first, then MAIN, then any
264** auxiliary databases added using the ATTACH command.
265**
266** See also sqlite3LocateTable().
267*/
268Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
269 Table *p = 0;
270 int i;
271 assert( zName!=0 );
272 for(i=OMIT_TEMPDB; i<db->nDb; i++){
273 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
274 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
275 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1);
276 if( p ) break;
277 }
278 return p;
279}
280
281/*
282** Locate the in-memory structure that describes a particular database
283** table given the name of that table and (optionally) the name of the
284** database containing the table. Return NULL if not found. Also leave an
285** error message in pParse->zErrMsg.
286**
287** The difference between this routine and sqlite3FindTable() is that this
288** routine leaves an error message in pParse->zErrMsg where
289** sqlite3FindTable() does not.
290*/
291Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
292 Table *p;
293
294 /* Read the database schema. If an error occurs, leave an error message
295 ** and code in pParse and return NULL. */
296 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
297 return 0;
298 }
299
300 p = sqlite3FindTable(pParse->db, zName, zDbase);
301 if( p==0 ){
302 if( zDbase ){
303 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
304 }else{
305 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
306 }
307 pParse->checkSchema = 1;
308 }
309 return p;
310}
311
312/*
313** Locate the in-memory structure that describes
314** a particular index given the name of that index
315** and the name of the database that contains the index.
316** Return NULL if not found.
317**
318** If zDatabase is 0, all databases are searched for the
319** table and the first matching index is returned. (No checking
320** for duplicate index names is done.) The search order is
321** TEMP first, then MAIN, then any auxiliary databases added
322** using the ATTACH command.
323*/
324Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
325 Index *p = 0;
326 int i;
327 for(i=OMIT_TEMPDB; i<db->nDb; i++){
328 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
329 Schema *pSchema = db->aDb[j].pSchema;
330 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
331 assert( pSchema || (j==1 && !db->aDb[1].pBt) );
332 if( pSchema ){
333 p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1);
334 }
335 if( p ) break;
336 }
337 return p;
338}
339
340/*
341** Reclaim the memory used by an index
342*/
343static void freeIndex(Index *p){
344 sqlite3_free(p->zColAff);
345 sqlite3_free(p);
346}
347
348/*
349** Remove the given index from the index hash table, and free
350** its memory structures.
351**
352** The index is removed from the database hash tables but
353** it is not unlinked from the Table that it indexes.
354** Unlinking from the Table must be done by the calling function.
355*/
356static void sqliteDeleteIndex(Index *p){
357 Index *pOld;
358 const char *zName = p->zName;
359
360 pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen( zName)+1, 0);
361 assert( pOld==0 || pOld==p );
362 freeIndex(p);
363}
364
365/*
366** For the index called zIdxName which is found in the database iDb,
367** unlike that index from its Table then remove the index from
368** the index hash table and free all memory structures associated
369** with the index.
370*/
371void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
372 Index *pIndex;
373 int len;
374 Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
375
376 len = strlen(zIdxName);
377 pIndex = sqlite3HashInsert(pHash, zIdxName, len+1, 0);
378 if( pIndex ){
379 if( pIndex->pTable->pIndex==pIndex ){
380 pIndex->pTable->pIndex = pIndex->pNext;
381 }else{
382 Index *p;
383 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
384 if( p && p->pNext==pIndex ){
385 p->pNext = pIndex->pNext;
386 }
387 }
388 freeIndex(pIndex);
389 }
390 db->flags |= SQLITE_InternChanges;
391}
392
393/*
394** Erase all schema information from the in-memory hash tables of
395** a single database. This routine is called to reclaim memory
396** before the database closes. It is also called during a rollback
397** if there were schema changes during the transaction or if a
398** schema-cookie mismatch occurs.
399**
400** If iDb<=0 then reset the internal schema tables for all database
401** files. If iDb>=2 then reset the internal schema for only the
402** single file indicated.
403*/
404void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
405 int i, j;
406
407 assert( iDb>=0 && iDb<db->nDb );
408 for(i=iDb; i<db->nDb; i++){
409 Db *pDb = &db->aDb[i];
410 if( pDb->pSchema ){
411 sqlite3SchemaFree(pDb->pSchema);
412 }
413 if( iDb>0 ) return;
414 }
415 assert( iDb==0 );
416 db->flags &= ~SQLITE_InternChanges;
417
418 /* If one or more of the auxiliary database files has been closed,
419 ** then remove them from the auxiliary database list. We take the
420 ** opportunity to do this here since we have just deleted all of the
421 ** schema hash tables and therefore do not have to make any changes
422 ** to any of those tables.
423 */
424 for(i=0; i<db->nDb; i++){
425 struct Db *pDb = &db->aDb[i];
426 if( pDb->pBt==0 ){
427 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
428 pDb->pAux = 0;
429 }
430 }
431 for(i=j=2; i<db->nDb; i++){
432 struct Db *pDb = &db->aDb[i];
433 if( pDb->pBt==0 ){
434 sqlite3_free(pDb->zName);
435 pDb->zName = 0;
436 continue;
437 }
438 if( j<i ){
439 db->aDb[j] = db->aDb[i];
440 }
441 j++;
442 }
443 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
444 db->nDb = j;
445 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
446 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
447 sqlite3_free(db->aDb);
448 db->aDb = db->aDbStatic;
449 }
450}
451
452/*
453** This routine is called when a commit occurs.
454*/
455void sqlite3CommitInternalChanges(sqlite3 *db){
456 db->flags &= ~SQLITE_InternChanges;
457}
458
459/*
460** Clear the column names from a table or view.
461*/
462static void sqliteResetColumnNames(Table *pTable){
463 int i;
464 Column *pCol;
465 assert( pTable!=0 );
466 if( (pCol = pTable->aCol)!=0 ){
467 for(i=0; i<pTable->nCol; i++, pCol++){
468 sqlite3_free(pCol->zName);
469 sqlite3ExprDelete(pCol->pDflt);
470 sqlite3_free(pCol->zType);
471 sqlite3_free(pCol->zColl);
472 }
473 sqlite3_free(pTable->aCol);
474 }
475 pTable->aCol = 0;
476 pTable->nCol = 0;
477}
478
479/*
480** Remove the memory data structures associated with the given
481** Table. No changes are made to disk by this routine.
482**
483** This routine just deletes the data structure. It does not unlink
484** the table data structure from the hash table. Nor does it remove
485** foreign keys from the sqlite.aFKey hash table. But it does destroy
486** memory structures of the indices and foreign keys associated with
487** the table.
488*/
489void sqlite3DeleteTable(Table *pTable){
490 Index *pIndex, *pNext;
491 FKey *pFKey, *pNextFKey;
492
493 if( pTable==0 ) return;
494
495 /* Do not delete the table until the reference count reaches zero. */
496 pTable->nRef--;
497 if( pTable->nRef>0 ){
498 return;
499 }
500 assert( pTable->nRef==0 );
501
502 /* Delete all indices associated with this table
503 */
504 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
505 pNext = pIndex->pNext;
506 assert( pIndex->pSchema==pTable->pSchema );
507 sqliteDeleteIndex(pIndex);
508 }
509
510#ifndef SQLITE_OMIT_FOREIGN_KEY
511 /* Delete all foreign keys associated with this table. The keys
512 ** should have already been unlinked from the pSchema->aFKey hash table
513 */
514 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
515 pNextFKey = pFKey->pNextFrom;
516 assert( sqlite3HashFind(&pTable->pSchema->aFKey,
517 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
518 sqlite3_free(pFKey);
519 }
520#endif
521
522 /* Delete the Table structure itself.
523 */
524 sqliteResetColumnNames(pTable);
525 sqlite3_free(pTable->zName);
526 sqlite3_free(pTable->zColAff);
527 sqlite3SelectDelete(pTable->pSelect);
528#ifndef SQLITE_OMIT_CHECK
529 sqlite3ExprDelete(pTable->pCheck);
530#endif
531 sqlite3VtabClear(pTable);
532 sqlite3_free(pTable);
533}
534
535/*
536** Unlink the given table from the hash tables and the delete the
537** table structure with all its indices and foreign keys.
538*/
539void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
540 Table *p;
541 FKey *pF1, *pF2;
542 Db *pDb;
543
544 assert( db!=0 );
545 assert( iDb>=0 && iDb<db->nDb );
546 assert( zTabName && zTabName[0] );
547 pDb = &db->aDb[iDb];
548 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, strlen(zTabName)+1,0);
549 if( p ){
550#ifndef SQLITE_OMIT_FOREIGN_KEY
551 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
552 int nTo = strlen(pF1->zTo) + 1;
553 pF2 = sqlite3HashFind(&pDb->pSchema->aFKey, pF1->zTo, nTo);
554 if( pF2==pF1 ){
555 sqlite3HashInsert(&pDb->pSchema->aFKey, pF1->zTo, nTo, pF1->pNextTo);
556 }else{
557 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
558 if( pF2 ){
559 pF2->pNextTo = pF1->pNextTo;
560 }
561 }
562 }
563#endif
564 sqlite3DeleteTable(p);
565 }
566 db->flags |= SQLITE_InternChanges;
567}
568
569/*
570** Given a token, return a string that consists of the text of that
571** token with any quotations removed. Space to hold the returned string
572** is obtained from sqliteMalloc() and must be freed by the calling
573** function.
574**
575** Tokens are often just pointers into the original SQL text and so
576** are not \000 terminated and are not persistent. The returned string
577** is \000 terminated and is persistent.
578*/
579char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
580 char *zName;
581 if( pName ){
582 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
583 sqlite3Dequote(zName);
584 }else{
585 zName = 0;
586 }
587 return zName;
588}
589
590/*
591** Open the sqlite_master table stored in database number iDb for
592** writing. The table is opened using cursor 0.
593*/
594void sqlite3OpenMasterTable(Parse *p, int iDb){
595 Vdbe *v = sqlite3GetVdbe(p);
596 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
597 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
598 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
599 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
600}
601
602/*
603** The token *pName contains the name of a database (either "main" or
604** "temp" or the name of an attached db). This routine returns the
605** index of the named database in db->aDb[], or -1 if the named db
606** does not exist.
607*/
608int sqlite3FindDb(sqlite3 *db, Token *pName){
609 int i = -1; /* Database number */
610 int n; /* Number of characters in the name */
611 Db *pDb; /* A database whose name space is being searched */
612 char *zName; /* Name we are searching for */
613
614 zName = sqlite3NameFromToken(db, pName);
615 if( zName ){
616 n = strlen(zName);
617 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
618 if( (!OMIT_TEMPDB || i!=1 ) && n==strlen(pDb->zName) &&
619 0==sqlite3StrICmp(pDb->zName, zName) ){
620 break;
621 }
622 }
623 sqlite3_free(zName);
624 }
625 return i;
626}
627
628/* The table or view or trigger name is passed to this routine via tokens
629** pName1 and pName2. If the table name was fully qualified, for example:
630**
631** CREATE TABLE xxx.yyy (...);
632**
633** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
634** the table name is not fully qualified, i.e.:
635**
636** CREATE TABLE yyy(...);
637**
638** Then pName1 is set to "yyy" and pName2 is "".
639**
640** This routine sets the *ppUnqual pointer to point at the token (pName1 or
641** pName2) that stores the unqualified table name. The index of the
642** database "xxx" is returned.
643*/
644int sqlite3TwoPartName(
645 Parse *pParse, /* Parsing and code generating context */
646 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
647 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
648 Token **pUnqual /* Write the unqualified object name here */
649){
650 int iDb; /* Database holding the object */
651 sqlite3 *db = pParse->db;
652
653 if( pName2 && pName2->n>0 ){
654 assert( !db->init.busy );
655 *pUnqual = pName2;
656 iDb = sqlite3FindDb(db, pName1);
657 if( iDb<0 ){
658 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
659 pParse->nErr++;
660 return -1;
661 }
662 }else{
663 assert( db->init.iDb==0 || db->init.busy );
664 iDb = db->init.iDb;
665 *pUnqual = pName1;
666 }
667 return iDb;
668}
669
670/*
671** This routine is used to check if the UTF-8 string zName is a legal
672** unqualified name for a new schema object (table, index, view or
673** trigger). All names are legal except those that begin with the string
674** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
675** is reserved for internal use.
676*/
677int sqlite3CheckObjectName(Parse *pParse, const char *zName){
678 if( !pParse->db->init.busy && pParse->nested==0
679 && (pParse->db->flags & SQLITE_WriteSchema)==0
680 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
681 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
682 return SQLITE_ERROR;
683 }
684 return SQLITE_OK;
685}
686
687/*
688** Begin constructing a new table representation in memory. This is
689** the first of several action routines that get called in response
690** to a CREATE TABLE statement. In particular, this routine is called
691** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
692** flag is true if the table should be stored in the auxiliary database
693** file instead of in the main database file. This is normally the case
694** when the "TEMP" or "TEMPORARY" keyword occurs in between
695** CREATE and TABLE.
696**
697** The new table record is initialized and put in pParse->pNewTable.
698** As more of the CREATE TABLE statement is parsed, additional action
699** routines will be called to add more information to this record.
700** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
701** is called to complete the construction of the new table record.
702*/
703void sqlite3StartTable(
704 Parse *pParse, /* Parser context */
705 Token *pName1, /* First part of the name of the table or view */
706 Token *pName2, /* Second part of the name of the table or view */
707 int isTemp, /* True if this is a TEMP table */
708 int isView, /* True if this is a VIEW */
709 int isVirtual, /* True if this is a VIRTUAL table */
710 int noErr /* Do nothing if table already exists */
711){
712 Table *pTable;
713 char *zName = 0; /* The name of the new table */
714 sqlite3 *db = pParse->db;
715 Vdbe *v;
716 int iDb; /* Database number to create the table in */
717 Token *pName; /* Unqualified name of the table to create */
718
719 /* The table or view name to create is passed to this routine via tokens
720 ** pName1 and pName2. If the table name was fully qualified, for example:
721 **
722 ** CREATE TABLE xxx.yyy (...);
723 **
724 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
725 ** the table name is not fully qualified, i.e.:
726 **
727 ** CREATE TABLE yyy(...);
728 **
729 ** Then pName1 is set to "yyy" and pName2 is "".
730 **
731 ** The call below sets the pName pointer to point at the token (pName1 or
732 ** pName2) that stores the unqualified table name. The variable iDb is
733 ** set to the index of the database that the table or view is to be
734 ** created in.
735 */
736 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
737 if( iDb<0 ) return;
738 if( !OMIT_TEMPDB && isTemp && iDb>1 ){
739 /* If creating a temp table, the name may not be qualified */
740 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
741 return;
742 }
743 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
744
745 pParse->sNameToken = *pName;
746 zName = sqlite3NameFromToken(db, pName);
747 if( zName==0 ) return;
748 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
749 goto begin_table_error;
750 }
751 if( db->init.iDb==1 ) isTemp = 1;
752#ifndef SQLITE_OMIT_AUTHORIZATION
753 assert( (isTemp & 1)==isTemp );
754 {
755 int code;
756 char *zDb = db->aDb[iDb].zName;
757 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
758 goto begin_table_error;
759 }
760 if( isView ){
761 if( !OMIT_TEMPDB && isTemp ){
762 code = SQLITE_CREATE_TEMP_VIEW;
763 }else{
764 code = SQLITE_CREATE_VIEW;
765 }
766 }else{
767 if( !OMIT_TEMPDB && isTemp ){
768 code = SQLITE_CREATE_TEMP_TABLE;
769 }else{
770 code = SQLITE_CREATE_TABLE;
771 }
772 }
773 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
774 goto begin_table_error;
775 }
776 }
777#endif
778
779 /* Make sure the new table name does not collide with an existing
780 ** index or table name in the same database. Issue an error message if
781 ** it does. The exception is if the statement being parsed was passed
782 ** to an sqlite3_declare_vtab() call. In that case only the column names
783 ** and types will be used, so there is no need to test for namespace
784 ** collisions.
785 */
786 if( !IN_DECLARE_VTAB ){
787 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
788 goto begin_table_error;
789 }
790 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
791 if( pTable ){
792 if( !noErr ){
793 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
794 }
795 goto begin_table_error;
796 }
797 if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){
798 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
799 goto begin_table_error;
800 }
801 }
802
803 pTable = sqlite3DbMallocZero(db, sizeof(Table));
804 if( pTable==0 ){
805 db->mallocFailed = 1;
806 pParse->rc = SQLITE_NOMEM;
807 pParse->nErr++;
808 goto begin_table_error;
809 }
810 pTable->zName = zName;
811 pTable->iPKey = -1;
812 pTable->pSchema = db->aDb[iDb].pSchema;
813 pTable->nRef = 1;
814 if( pParse->pNewTable ) sqlite3DeleteTable(pParse->pNewTable);
815 pParse->pNewTable = pTable;
816
817 /* If this is the magic sqlite_sequence table used by autoincrement,
818 ** then record a pointer to this table in the main database structure
819 ** so that INSERT can find the table easily.
820 */
821#ifndef SQLITE_OMIT_AUTOINCREMENT
822 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
823 pTable->pSchema->pSeqTab = pTable;
824 }
825#endif
826
827 /* Begin generating the code that will insert the table record into
828 ** the SQLITE_MASTER table. Note in particular that we must go ahead
829 ** and allocate the record number for the table entry now. Before any
830 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
831 ** indices to be created and the table record must come before the
832 ** indices. Hence, the record number for the table must be allocated
833 ** now.
834 */
835 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
836 int lbl;
837 int fileFormat;
838 sqlite3BeginWriteOperation(pParse, 0, iDb);
839
840#ifndef SQLITE_OMIT_VIRTUALTABLE
841 if( isVirtual ){
842 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0);
843 }
844#endif
845
846 /* If the file format and encoding in the database have not been set,
847 ** set them now.
848 */
849 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); /* file_format */
850 sqlite3VdbeUsesBtree(v, iDb);
851 lbl = sqlite3VdbeMakeLabel(v);
852 sqlite3VdbeAddOp(v, OP_If, 0, lbl);
853 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
854 1 : SQLITE_MAX_FILE_FORMAT;
855 sqlite3VdbeAddOp(v, OP_Integer, fileFormat, 0);
856 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
857 sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0);
858 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
859 sqlite3VdbeResolveLabel(v, lbl);
860
861 /* This just creates a place-holder record in the sqlite_master table.
862 ** The record created does not contain anything yet. It will be replaced
863 ** by the real entry in code generated at sqlite3EndTable().
864 **
865 ** The rowid for the new entry is left on the top of the stack.
866 ** The rowid value is needed by the code that sqlite3EndTable will
867 ** generate.
868 */
869#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
870 if( isView || isVirtual ){
871 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
872 }else
873#endif
874 {
875 sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0);
876 }
877 sqlite3OpenMasterTable(pParse, iDb);
878 sqlite3VdbeAddOp(v, OP_NewRowid, 0, 0);
879 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
880 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
881 sqlite3VdbeAddOp(v, OP_Insert, 0, OPFLAG_APPEND);
882 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
883 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
884 }
885
886 /* Normal (non-error) return. */
887 return;
888
889 /* If an error occurs, we jump here */
890begin_table_error:
891 sqlite3_free(zName);
892 return;
893}
894
895/*
896** This macro is used to compare two strings in a case-insensitive manner.
897** It is slightly faster than calling sqlite3StrICmp() directly, but
898** produces larger code.
899**
900** WARNING: This macro is not compatible with the strcmp() family. It
901** returns true if the two strings are equal, otherwise false.
902*/
903#define STRICMP(x, y) (\
904sqlite3UpperToLower[*(unsigned char *)(x)]== \
905sqlite3UpperToLower[*(unsigned char *)(y)] \
906&& sqlite3StrICmp((x)+1,(y)+1)==0 )
907
908/*
909** Add a new column to the table currently being constructed.
910**
911** The parser calls this routine once for each column declaration
912** in a CREATE TABLE statement. sqlite3StartTable() gets called
913** first to get things going. Then this routine is called for each
914** column.
915*/
916void sqlite3AddColumn(Parse *pParse, Token *pName){
917 Table *p;
918 int i;
919 char *z;
920 Column *pCol;
921 if( (p = pParse->pNewTable)==0 ) return;
922 if( p->nCol+1>SQLITE_MAX_COLUMN ){
923 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
924 return;
925 }
926 z = sqlite3NameFromToken(pParse->db, pName);
927 if( z==0 ) return;
928 for(i=0; i<p->nCol; i++){
929 if( STRICMP(z, p->aCol[i].zName) ){
930 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
931 sqlite3_free(z);
932 return;
933 }
934 }
935 if( (p->nCol & 0x7)==0 ){
936 Column *aNew;
937 aNew = sqlite3DbRealloc(pParse->db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
938 if( aNew==0 ){
939 sqlite3_free(z);
940 return;
941 }
942 p->aCol = aNew;
943 }
944 pCol = &p->aCol[p->nCol];
945 memset(pCol, 0, sizeof(p->aCol[0]));
946 pCol->zName = z;
947
948 /* If there is no type specified, columns have the default affinity
949 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
950 ** be called next to set pCol->affinity correctly.
951 */
952 pCol->affinity = SQLITE_AFF_NONE;
953 p->nCol++;
954}
955
956/*
957** This routine is called by the parser while in the middle of
958** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
959** been seen on a column. This routine sets the notNull flag on
960** the column currently under construction.
961*/
962void sqlite3AddNotNull(Parse *pParse, int onError){
963 Table *p;
964 int i;
965 if( (p = pParse->pNewTable)==0 ) return;
966 i = p->nCol-1;
967 if( i>=0 ) p->aCol[i].notNull = onError;
968}
969
970/*
971** Scan the column type name zType (length nType) and return the
972** associated affinity type.
973**
974** This routine does a case-independent search of zType for the
975** substrings in the following table. If one of the substrings is
976** found, the corresponding affinity is returned. If zType contains
977** more than one of the substrings, entries toward the top of
978** the table take priority. For example, if zType is 'BLOBINT',
979** SQLITE_AFF_INTEGER is returned.
980**
981** Substring | Affinity
982** --------------------------------
983** 'INT' | SQLITE_AFF_INTEGER
984** 'CHAR' | SQLITE_AFF_TEXT
985** 'CLOB' | SQLITE_AFF_TEXT
986** 'TEXT' | SQLITE_AFF_TEXT
987** 'BLOB' | SQLITE_AFF_NONE
988** 'REAL' | SQLITE_AFF_REAL
989** 'FLOA' | SQLITE_AFF_REAL
990** 'DOUB' | SQLITE_AFF_REAL
991**
992** If none of the substrings in the above table are found,
993** SQLITE_AFF_NUMERIC is returned.
994*/
995char sqlite3AffinityType(const Token *pType){
996 u32 h = 0;
997 char aff = SQLITE_AFF_NUMERIC;
998 const unsigned char *zIn = pType->z;
999 const unsigned char *zEnd = &pType->z[pType->n];
1000
1001 while( zIn!=zEnd ){
1002 h = (h<<8) + sqlite3UpperToLower[*zIn];
1003 zIn++;
1004 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
1005 aff = SQLITE_AFF_TEXT;
1006 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
1007 aff = SQLITE_AFF_TEXT;
1008 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
1009 aff = SQLITE_AFF_TEXT;
1010 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
1011 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
1012 aff = SQLITE_AFF_NONE;
1013#ifndef SQLITE_OMIT_FLOATING_POINT
1014 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
1015 && aff==SQLITE_AFF_NUMERIC ){
1016 aff = SQLITE_AFF_REAL;
1017 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
1018 && aff==SQLITE_AFF_NUMERIC ){
1019 aff = SQLITE_AFF_REAL;
1020 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
1021 && aff==SQLITE_AFF_NUMERIC ){
1022 aff = SQLITE_AFF_REAL;
1023#endif
1024 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
1025 aff = SQLITE_AFF_INTEGER;
1026 break;
1027 }
1028 }
1029
1030 return aff;
1031}
1032
1033/*
1034** This routine is called by the parser while in the middle of
1035** parsing a CREATE TABLE statement. The pFirst token is the first
1036** token in the sequence of tokens that describe the type of the
1037** column currently under construction. pLast is the last token
1038** in the sequence. Use this information to construct a string
1039** that contains the typename of the column and store that string
1040** in zType.
1041*/
1042void sqlite3AddColumnType(Parse *pParse, Token *pType){
1043 Table *p;
1044 int i;
1045 Column *pCol;
1046
1047 if( (p = pParse->pNewTable)==0 ) return;
1048 i = p->nCol-1;
1049 if( i<0 ) return;
1050 pCol = &p->aCol[i];
1051 sqlite3_free(pCol->zType);
1052 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
1053 pCol->affinity = sqlite3AffinityType(pType);
1054}
1055
1056/*
1057** The expression is the default value for the most recently added column
1058** of the table currently under construction.
1059**
1060** Default value expressions must be constant. Raise an exception if this
1061** is not the case.
1062**
1063** This routine is called by the parser while in the middle of
1064** parsing a CREATE TABLE statement.
1065*/
1066void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){
1067 Table *p;
1068 Column *pCol;
1069 if( (p = pParse->pNewTable)!=0 ){
1070 pCol = &(p->aCol[p->nCol-1]);
1071 if( !sqlite3ExprIsConstantOrFunction(pExpr) ){
1072 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
1073 pCol->zName);
1074 }else{
1075 Expr *pCopy;
1076 sqlite3 *db = pParse->db;
1077 sqlite3ExprDelete(pCol->pDflt);
1078 pCol->pDflt = pCopy = sqlite3ExprDup(db, pExpr);
1079 if( pCopy ){
1080 sqlite3TokenCopy(db, &pCopy->span, &pExpr->span);
1081 }
1082 }
1083 }
1084 sqlite3ExprDelete(pExpr);
1085}
1086
1087/*
1088** Designate the PRIMARY KEY for the table. pList is a list of names
1089** of columns that form the primary key. If pList is NULL, then the
1090** most recently added column of the table is the primary key.
1091**
1092** A table can have at most one primary key. If the table already has
1093** a primary key (and this is the second primary key) then create an
1094** error.
1095**
1096** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
1097** then we will try to use that column as the rowid. Set the Table.iPKey
1098** field of the table under construction to be the index of the
1099** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
1100** no INTEGER PRIMARY KEY.
1101**
1102** If the key is not an INTEGER PRIMARY KEY, then create a unique
1103** index for the key. No index is created for INTEGER PRIMARY KEYs.
1104*/
1105void sqlite3AddPrimaryKey(
1106 Parse *pParse, /* Parsing context */
1107 ExprList *pList, /* List of field names to be indexed */
1108 int onError, /* What to do with a uniqueness conflict */
1109 int autoInc, /* True if the AUTOINCREMENT keyword is present */
1110 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
1111){
1112 Table *pTab = pParse->pNewTable;
1113 char *zType = 0;
1114 int iCol = -1, i;
1115 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
1116 if( pTab->hasPrimKey ){
1117 sqlite3ErrorMsg(pParse,
1118 "table \"%s\" has more than one primary key", pTab->zName);
1119 goto primary_key_exit;
1120 }
1121 pTab->hasPrimKey = 1;
1122 if( pList==0 ){
1123 iCol = pTab->nCol - 1;
1124 pTab->aCol[iCol].isPrimKey = 1;
1125 }else{
1126 for(i=0; i<pList->nExpr; i++){
1127 for(iCol=0; iCol<pTab->nCol; iCol++){
1128 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
1129 break;
1130 }
1131 }
1132 if( iCol<pTab->nCol ){
1133 pTab->aCol[iCol].isPrimKey = 1;
1134 }
1135 }
1136 if( pList->nExpr>1 ) iCol = -1;
1137 }
1138 if( iCol>=0 && iCol<pTab->nCol ){
1139 zType = pTab->aCol[iCol].zType;
1140 }
1141 if( zType && sqlite3StrICmp(zType, "INTEGER")==0
1142 && sortOrder==SQLITE_SO_ASC ){
1143 pTab->iPKey = iCol;
1144 pTab->keyConf = onError;
1145 pTab->autoInc = autoInc;
1146 }else if( autoInc ){
1147#ifndef SQLITE_OMIT_AUTOINCREMENT
1148 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1149 "INTEGER PRIMARY KEY");
1150#endif
1151 }else{
1152 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
1153 pList = 0;
1154 }
1155
1156primary_key_exit:
1157 sqlite3ExprListDelete(pList);
1158 return;
1159}
1160
1161/*
1162** Add a new CHECK constraint to the table currently under construction.
1163*/
1164void sqlite3AddCheckConstraint(
1165 Parse *pParse, /* Parsing context */
1166 Expr *pCheckExpr /* The check expression */
1167){
1168#ifndef SQLITE_OMIT_CHECK
1169 Table *pTab = pParse->pNewTable;
1170 sqlite3 *db = pParse->db;
1171 if( pTab && !IN_DECLARE_VTAB ){
1172 /* The CHECK expression must be duplicated so that tokens refer
1173 ** to malloced space and not the (ephemeral) text of the CREATE TABLE
1174 ** statement */
1175 pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck,
1176 sqlite3ExprDup(db, pCheckExpr));
1177 }
1178#endif
1179 sqlite3ExprDelete(pCheckExpr);
1180}
1181
1182/*
1183** Set the collation function of the most recently parsed table column
1184** to the CollSeq given.
1185*/
1186void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
1187 Table *p;
1188 int i;
1189
1190 if( (p = pParse->pNewTable)==0 ) return;
1191 i = p->nCol-1;
1192
1193 if( sqlite3LocateCollSeq(pParse, zType, nType) ){
1194 Index *pIdx;
1195 p->aCol[i].zColl = sqlite3DbStrNDup(pParse->db, zType, nType);
1196
1197 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1198 ** then an index may have been created on this column before the
1199 ** collation type was added. Correct this if it is the case.
1200 */
1201 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1202 assert( pIdx->nColumn==1 );
1203 if( pIdx->aiColumn[0]==i ){
1204 pIdx->azColl[0] = p->aCol[i].zColl;
1205 }
1206 }
1207 }
1208}
1209
1210/*
1211** This function returns the collation sequence for database native text
1212** encoding identified by the string zName, length nName.
1213**
1214** If the requested collation sequence is not available, or not available
1215** in the database native encoding, the collation factory is invoked to
1216** request it. If the collation factory does not supply such a sequence,
1217** and the sequence is available in another text encoding, then that is
1218** returned instead.
1219**
1220** If no versions of the requested collations sequence are available, or
1221** another error occurs, NULL is returned and an error message written into
1222** pParse.
1223**
1224** This routine is a wrapper around sqlite3FindCollSeq(). This routine
1225** invokes the collation factory if the named collation cannot be found
1226** and generates an error message.
1227*/
1228CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
1229 sqlite3 *db = pParse->db;
1230 u8 enc = ENC(db);
1231 u8 initbusy = db->init.busy;
1232 CollSeq *pColl;
1233
1234 pColl = sqlite3FindCollSeq(db, enc, zName, nName, initbusy);
1235 if( !initbusy && (!pColl || !pColl->xCmp) ){
1236 pColl = sqlite3GetCollSeq(db, pColl, zName, nName);
1237 if( !pColl ){
1238 if( nName<0 ){
1239 nName = strlen(zName);
1240 }
1241 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
1242 pColl = 0;
1243 }
1244 }
1245
1246 return pColl;
1247}
1248
1249
1250/*
1251** Generate code that will increment the schema cookie.
1252**
1253** The schema cookie is used to determine when the schema for the
1254** database changes. After each schema change, the cookie value
1255** changes. When a process first reads the schema it records the
1256** cookie. Thereafter, whenever it goes to access the database,
1257** it checks the cookie to make sure the schema has not changed
1258** since it was last read.
1259**
1260** This plan is not completely bullet-proof. It is possible for
1261** the schema to change multiple times and for the cookie to be
1262** set back to prior value. But schema changes are infrequent
1263** and the probability of hitting the same cookie value is only
1264** 1 chance in 2^32. So we're safe enough.
1265*/
1266void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
1267 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, 0);
1268 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
1269}
1270
1271/*
1272** Measure the number of characters needed to output the given
1273** identifier. The number returned includes any quotes used
1274** but does not include the null terminator.
1275**
1276** The estimate is conservative. It might be larger that what is
1277** really needed.
1278*/
1279static int identLength(const char *z){
1280 int n;
1281 for(n=0; *z; n++, z++){
1282 if( *z=='"' ){ n++; }
1283 }
1284 return n + 2;
1285}
1286
1287/*
1288** Write an identifier onto the end of the given string. Add
1289** quote characters as needed.
1290*/
1291static void identPut(char *z, int *pIdx, char *zSignedIdent){
1292 unsigned char *zIdent = (unsigned char*)zSignedIdent;
1293 int i, j, needQuote;
1294 i = *pIdx;
1295 for(j=0; zIdent[j]; j++){
1296 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1297 }
1298 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
1299 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
1300 if( needQuote ) z[i++] = '"';
1301 for(j=0; zIdent[j]; j++){
1302 z[i++] = zIdent[j];
1303 if( zIdent[j]=='"' ) z[i++] = '"';
1304 }
1305 if( needQuote ) z[i++] = '"';
1306 z[i] = 0;
1307 *pIdx = i;
1308}
1309
1310/*
1311** Generate a CREATE TABLE statement appropriate for the given
1312** table. Memory to hold the text of the statement is obtained
1313** from sqliteMalloc() and must be freed by the calling function.
1314*/
1315static char *createTableStmt(Table *p, int isTemp){
1316 int i, k, n;
1317 char *zStmt;
1318 char *zSep, *zSep2, *zEnd, *z;
1319 Column *pCol;
1320 n = 0;
1321 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1322 n += identLength(pCol->zName);
1323 z = pCol->zType;
1324 if( z ){
1325 n += (strlen(z) + 1);
1326 }
1327 }
1328 n += identLength(p->zName);
1329 if( n<50 ){
1330 zSep = "";
1331 zSep2 = ",";
1332 zEnd = ")";
1333 }else{
1334 zSep = "\n ";
1335 zSep2 = ",\n ";
1336 zEnd = "\n)";
1337 }
1338 n += 35 + 6*p->nCol;
1339 zStmt = sqlite3_malloc( n );
1340 if( zStmt==0 ) return 0;
1341 sqlite3_snprintf(n, zStmt,
1342 !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE ");
1343 k = strlen(zStmt);
1344 identPut(zStmt, &k, p->zName);
1345 zStmt[k++] = '(';
1346 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
1347 sqlite3_snprintf(n-k, &zStmt[k], zSep);
1348 k += strlen(&zStmt[k]);
1349 zSep = zSep2;
1350 identPut(zStmt, &k, pCol->zName);
1351 if( (z = pCol->zType)!=0 ){
1352 zStmt[k++] = ' ';
1353 assert( strlen(z)+k+1<=n );
1354 sqlite3_snprintf(n-k, &zStmt[k], "%s", z);
1355 k += strlen(z);
1356 }
1357 }
1358 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
1359 return zStmt;
1360}
1361
1362/*
1363** This routine is called to report the final ")" that terminates
1364** a CREATE TABLE statement.
1365**
1366** The table structure that other action routines have been building
1367** is added to the internal hash tables, assuming no errors have
1368** occurred.
1369**
1370** An entry for the table is made in the master table on disk, unless
1371** this is a temporary table or db->init.busy==1. When db->init.busy==1
1372** it means we are reading the sqlite_master table because we just
1373** connected to the database or because the sqlite_master table has
1374** recently changed, so the entry for this table already exists in
1375** the sqlite_master table. We do not want to create it again.
1376**
1377** If the pSelect argument is not NULL, it means that this routine
1378** was called to create a table generated from a
1379** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1380** the new table will match the result set of the SELECT.
1381*/
1382void sqlite3EndTable(
1383 Parse *pParse, /* Parse context */
1384 Token *pCons, /* The ',' token after the last column defn. */
1385 Token *pEnd, /* The final ')' token in the CREATE TABLE */
1386 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
1387){
1388 Table *p;
1389 sqlite3 *db = pParse->db;
1390 int iDb;
1391
1392 if( (pEnd==0 && pSelect==0) || pParse->nErr || db->mallocFailed ) {
1393 return;
1394 }
1395 p = pParse->pNewTable;
1396 if( p==0 ) return;
1397
1398 assert( !db->init.busy || !pSelect );
1399
1400 iDb = sqlite3SchemaToIndex(db, p->pSchema);
1401
1402#ifndef SQLITE_OMIT_CHECK
1403 /* Resolve names in all CHECK constraint expressions.
1404 */
1405 if( p->pCheck ){
1406 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
1407 NameContext sNC; /* Name context for pParse->pNewTable */
1408
1409 memset(&sNC, 0, sizeof(sNC));
1410 memset(&sSrc, 0, sizeof(sSrc));
1411 sSrc.nSrc = 1;
1412 sSrc.a[0].zName = p->zName;
1413 sSrc.a[0].pTab = p;
1414 sSrc.a[0].iCursor = -1;
1415 sNC.pParse = pParse;
1416 sNC.pSrcList = &sSrc;
1417 sNC.isCheck = 1;
1418 if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){
1419 return;
1420 }
1421 }
1422#endif /* !defined(SQLITE_OMIT_CHECK) */
1423
1424 /* If the db->init.busy is 1 it means we are reading the SQL off the
1425 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1426 ** So do not write to the disk again. Extract the root page number
1427 ** for the table from the db->init.newTnum field. (The page number
1428 ** should have been put there by the sqliteOpenCb routine.)
1429 */
1430 if( db->init.busy ){
1431 p->tnum = db->init.newTnum;
1432 }
1433
1434 /* If not initializing, then create a record for the new table
1435 ** in the SQLITE_MASTER table of the database. The record number
1436 ** for the new table entry should already be on the stack.
1437 **
1438 ** If this is a TEMPORARY table, write the entry into the auxiliary
1439 ** file instead of into the main database file.
1440 */
1441 if( !db->init.busy ){
1442 int n;
1443 Vdbe *v;
1444 char *zType; /* "view" or "table" */
1445 char *zType2; /* "VIEW" or "TABLE" */
1446 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
1447
1448 v = sqlite3GetVdbe(pParse);
1449 if( v==0 ) return;
1450
1451 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1452
1453 /* Create the rootpage for the new table and push it onto the stack.
1454 ** A view has no rootpage, so just push a zero onto the stack for
1455 ** views. Initialize zType at the same time.
1456 */
1457 if( p->pSelect==0 ){
1458 /* A regular table */
1459 zType = "table";
1460 zType2 = "TABLE";
1461#ifndef SQLITE_OMIT_VIEW
1462 }else{
1463 /* A view */
1464 zType = "view";
1465 zType2 = "VIEW";
1466#endif
1467 }
1468
1469 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1470 ** statement to populate the new table. The root-page number for the
1471 ** new table is on the top of the vdbe stack.
1472 **
1473 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1474 ** suitable state to query for the column names and types to be used
1475 ** by the new table.
1476 **
1477 ** A shared-cache write-lock is not required to write to the new table,
1478 ** as a schema-lock must have already been obtained to create it. Since
1479 ** a schema-lock excludes all other database users, the write-lock would
1480 ** be redundant.
1481 */
1482 if( pSelect ){
1483 Table *pSelTab;
1484 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1485 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
1486 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1487 pParse->nTab = 2;
1488 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1489 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1490 if( pParse->nErr==0 ){
1491 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1492 if( pSelTab==0 ) return;
1493 assert( p->aCol==0 );
1494 p->nCol = pSelTab->nCol;
1495 p->aCol = pSelTab->aCol;
1496 pSelTab->nCol = 0;
1497 pSelTab->aCol = 0;
1498 sqlite3DeleteTable(pSelTab);
1499 }
1500 }
1501
1502 /* Compute the complete text of the CREATE statement */
1503 if( pSelect ){
1504 zStmt = createTableStmt(p, p->pSchema==db->aDb[1].pSchema);
1505 }else{
1506 n = pEnd->z - pParse->sNameToken.z + 1;
1507 zStmt = sqlite3MPrintf(db,
1508 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
1509 );
1510 }
1511
1512 /* A slot for the record has already been allocated in the
1513 ** SQLITE_MASTER table. We just need to update that slot with all
1514 ** the information we've collected. The rowid for the preallocated
1515 ** slot is the 2nd item on the stack. The top of the stack is the
1516 ** root page for the new table (or a 0 if this is a view).
1517 */
1518 sqlite3NestedParse(pParse,
1519 "UPDATE %Q.%s "
1520 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q "
1521 "WHERE rowid=#1",
1522 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
1523 zType,
1524 p->zName,
1525 p->zName,
1526 zStmt
1527 );
1528 sqlite3_free(zStmt);
1529 sqlite3ChangeCookie(db, v, iDb);
1530
1531#ifndef SQLITE_OMIT_AUTOINCREMENT
1532 /* Check to see if we need to create an sqlite_sequence table for
1533 ** keeping track of autoincrement keys.
1534 */
1535 if( p->autoInc ){
1536 Db *pDb = &db->aDb[iDb];
1537 if( pDb->pSchema->pSeqTab==0 ){
1538 sqlite3NestedParse(pParse,
1539 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1540 pDb->zName
1541 );
1542 }
1543 }
1544#endif
1545
1546 /* Reparse everything to update our internal data structures */
1547 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
1548 sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P3_DYNAMIC);
1549 }
1550
1551
1552 /* Add the table to the in-memory representation of the database.
1553 */
1554 if( db->init.busy && pParse->nErr==0 ){
1555 Table *pOld;
1556 FKey *pFKey;
1557 Schema *pSchema = p->pSchema;
1558 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p);
1559 if( pOld ){
1560 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1561 db->mallocFailed = 1;
1562 return;
1563 }
1564#ifndef SQLITE_OMIT_FOREIGN_KEY
1565 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1566 void *data;
1567 int nTo = strlen(pFKey->zTo) + 1;
1568 pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo);
1569 data = sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey);
1570 if( data==(void *)pFKey ){
1571 db->mallocFailed = 1;
1572 }
1573 }
1574#endif
1575 pParse->pNewTable = 0;
1576 db->nTable++;
1577 db->flags |= SQLITE_InternChanges;
1578
1579#ifndef SQLITE_OMIT_ALTERTABLE
1580 if( !p->pSelect ){
1581 const char *zName = (const char *)pParse->sNameToken.z;
1582 int nName;
1583 assert( !pSelect && pCons && pEnd );
1584 if( pCons->z==0 ){
1585 pCons = pEnd;
1586 }
1587 nName = (const char *)pCons->z - zName;
1588 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
1589 }
1590#endif
1591 }
1592}
1593
1594#ifndef SQLITE_OMIT_VIEW
1595/*
1596** The parser calls this routine in order to create a new VIEW
1597*/
1598void sqlite3CreateView(
1599 Parse *pParse, /* The parsing context */
1600 Token *pBegin, /* The CREATE token that begins the statement */
1601 Token *pName1, /* The token that holds the name of the view */
1602 Token *pName2, /* The token that holds the name of the view */
1603 Select *pSelect, /* A SELECT statement that will become the new view */
1604 int isTemp, /* TRUE for a TEMPORARY view */
1605 int noErr /* Suppress error messages if VIEW already exists */
1606){
1607 Table *p;
1608 int n;
1609 const unsigned char *z;
1610 Token sEnd;
1611 DbFixer sFix;
1612 Token *pName;
1613 int iDb;
1614 sqlite3 *db = pParse->db;
1615
1616 if( pParse->nVar>0 ){
1617 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
1618 sqlite3SelectDelete(pSelect);
1619 return;
1620 }
1621 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
1622 p = pParse->pNewTable;
1623 if( p==0 || pParse->nErr ){
1624 sqlite3SelectDelete(pSelect);
1625 return;
1626 }
1627 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
1628 iDb = sqlite3SchemaToIndex(db, p->pSchema);
1629 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
1630 && sqlite3FixSelect(&sFix, pSelect)
1631 ){
1632 sqlite3SelectDelete(pSelect);
1633 return;
1634 }
1635
1636 /* Make a copy of the entire SELECT statement that defines the view.
1637 ** This will force all the Expr.token.z values to be dynamically
1638 ** allocated rather than point to the input string - which means that
1639 ** they will persist after the current sqlite3_exec() call returns.
1640 */
1641 p->pSelect = sqlite3SelectDup(db, pSelect);
1642 sqlite3SelectDelete(pSelect);
1643 if( db->mallocFailed ){
1644 return;
1645 }
1646 if( !db->init.busy ){
1647 sqlite3ViewGetColumnNames(pParse, p);
1648 }
1649
1650 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1651 ** the end.
1652 */
1653 sEnd = pParse->sLastToken;
1654 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1655 sEnd.z += sEnd.n;
1656 }
1657 sEnd.n = 0;
1658 n = sEnd.z - pBegin->z;
1659 z = (const unsigned char*)pBegin->z;
1660 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1661 sEnd.z = &z[n-1];
1662 sEnd.n = 1;
1663
1664 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1665 sqlite3EndTable(pParse, 0, &sEnd, 0);
1666 return;
1667}
1668#endif /* SQLITE_OMIT_VIEW */
1669
1670#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1671/*
1672** The Table structure pTable is really a VIEW. Fill in the names of
1673** the columns of the view in the pTable structure. Return the number
1674** of errors. If an error is seen leave an error message in pParse->zErrMsg.
1675*/
1676int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
1677 Table *pSelTab; /* A fake table from which we get the result set */
1678 Select *pSel; /* Copy of the SELECT that implements the view */
1679 int nErr = 0; /* Number of errors encountered */
1680 int n; /* Temporarily holds the number of cursors assigned */
1681 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
1682
1683 assert( pTable );
1684
1685#ifndef SQLITE_OMIT_VIRTUALTABLE
1686 if( sqlite3VtabCallConnect(pParse, pTable) ){
1687 return SQLITE_ERROR;
1688 }
1689 if( IsVirtual(pTable) ) return 0;
1690#endif
1691
1692#ifndef SQLITE_OMIT_VIEW
1693 /* A positive nCol means the columns names for this view are
1694 ** already known.
1695 */
1696 if( pTable->nCol>0 ) return 0;
1697
1698 /* A negative nCol is a special marker meaning that we are currently
1699 ** trying to compute the column names. If we enter this routine with
1700 ** a negative nCol, it means two or more views form a loop, like this:
1701 **
1702 ** CREATE VIEW one AS SELECT * FROM two;
1703 ** CREATE VIEW two AS SELECT * FROM one;
1704 **
1705 ** Actually, this error is caught previously and so the following test
1706 ** should always fail. But we will leave it in place just to be safe.
1707 */
1708 if( pTable->nCol<0 ){
1709 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
1710 return 1;
1711 }
1712 assert( pTable->nCol>=0 );
1713
1714 /* If we get this far, it means we need to compute the table names.
1715 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1716 ** "*" elements in the results set of the view and will assign cursors
1717 ** to the elements of the FROM clause. But we do not want these changes
1718 ** to be permanent. So the computation is done on a copy of the SELECT
1719 ** statement that defines the view.
1720 */
1721 assert( pTable->pSelect );
1722 pSel = sqlite3SelectDup(db, pTable->pSelect);
1723 if( pSel ){
1724 n = pParse->nTab;
1725 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
1726 pTable->nCol = -1;
1727 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
1728 pParse->nTab = n;
1729 if( pSelTab ){
1730 assert( pTable->aCol==0 );
1731 pTable->nCol = pSelTab->nCol;
1732 pTable->aCol = pSelTab->aCol;
1733 pSelTab->nCol = 0;
1734 pSelTab->aCol = 0;
1735 sqlite3DeleteTable(pSelTab);
1736 pTable->pSchema->flags |= DB_UnresetViews;
1737 }else{
1738 pTable->nCol = 0;
1739 nErr++;
1740 }
1741 sqlite3SelectDelete(pSel);
1742 } else {
1743 nErr++;
1744 }
1745#endif /* SQLITE_OMIT_VIEW */
1746 return nErr;
1747}
1748#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
1749
1750#ifndef SQLITE_OMIT_VIEW
1751/*
1752** Clear the column names from every VIEW in database idx.
1753*/
1754static void sqliteViewResetAll(sqlite3 *db, int idx){
1755 HashElem *i;
1756 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
1757 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
1758 Table *pTab = sqliteHashData(i);
1759 if( pTab->pSelect ){
1760 sqliteResetColumnNames(pTab);
1761 }
1762 }
1763 DbClearProperty(db, idx, DB_UnresetViews);
1764}
1765#else
1766# define sqliteViewResetAll(A,B)
1767#endif /* SQLITE_OMIT_VIEW */
1768
1769/*
1770** This function is called by the VDBE to adjust the internal schema
1771** used by SQLite when the btree layer moves a table root page. The
1772** root-page of a table or index in database iDb has changed from iFrom
1773** to iTo.
1774**
1775** Ticket #1728: The symbol table might still contain information
1776** on tables and/or indices that are the process of being deleted.
1777** If you are unlucky, one of those deleted indices or tables might
1778** have the same rootpage number as the real table or index that is
1779** being moved. So we cannot stop searching after the first match
1780** because the first match might be for one of the deleted indices
1781** or tables and not the table/index that is actually being moved.
1782** We must continue looping until all tables and indices with
1783** rootpage==iFrom have been converted to have a rootpage of iTo
1784** in order to be certain that we got the right one.
1785*/
1786#ifndef SQLITE_OMIT_AUTOVACUUM
1787void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1788 HashElem *pElem;
1789 Hash *pHash;
1790
1791 pHash = &pDb->pSchema->tblHash;
1792 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
1793 Table *pTab = sqliteHashData(pElem);
1794 if( pTab->tnum==iFrom ){
1795 pTab->tnum = iTo;
1796 }
1797 }
1798 pHash = &pDb->pSchema->idxHash;
1799 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
1800 Index *pIdx = sqliteHashData(pElem);
1801 if( pIdx->tnum==iFrom ){
1802 pIdx->tnum = iTo;
1803 }
1804 }
1805}
1806#endif
1807
1808/*
1809** Write code to erase the table with root-page iTable from database iDb.
1810** Also write code to modify the sqlite_master table and internal schema
1811** if a root-page of another table is moved by the btree-layer whilst
1812** erasing iTable (this can happen with an auto-vacuum database).
1813*/
1814static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1815 Vdbe *v = sqlite3GetVdbe(pParse);
1816 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb);
1817#ifndef SQLITE_OMIT_AUTOVACUUM
1818 /* OP_Destroy pushes an integer onto the stack. If this integer
1819 ** is non-zero, then it is the root page number of a table moved to
1820 ** location iTable. The following code modifies the sqlite_master table to
1821 ** reflect this.
1822 **
1823 ** The "#0" in the SQL is a special constant that means whatever value
1824 ** is on the top of the stack. See sqlite3RegisterExpr().
1825 */
1826 sqlite3NestedParse(pParse,
1827 "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0",
1828 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable);
1829#endif
1830}
1831
1832/*
1833** Write VDBE code to erase table pTab and all associated indices on disk.
1834** Code to update the sqlite_master tables and internal schema definitions
1835** in case a root-page belonging to another table is moved by the btree layer
1836** is also added (this can happen with an auto-vacuum database).
1837*/
1838static void destroyTable(Parse *pParse, Table *pTab){
1839#ifdef SQLITE_OMIT_AUTOVACUUM
1840 Index *pIdx;
1841 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1842 destroyRootPage(pParse, pTab->tnum, iDb);
1843 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1844 destroyRootPage(pParse, pIdx->tnum, iDb);
1845 }
1846#else
1847 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1848 ** is not defined), then it is important to call OP_Destroy on the
1849 ** table and index root-pages in order, starting with the numerically
1850 ** largest root-page number. This guarantees that none of the root-pages
1851 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1852 ** following were coded:
1853 **
1854 ** OP_Destroy 4 0
1855 ** ...
1856 ** OP_Destroy 5 0
1857 **
1858 ** and root page 5 happened to be the largest root-page number in the
1859 ** database, then root page 5 would be moved to page 4 by the
1860 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1861 ** a free-list page.
1862 */
1863 int iTab = pTab->tnum;
1864 int iDestroyed = 0;
1865
1866 while( 1 ){
1867 Index *pIdx;
1868 int iLargest = 0;
1869
1870 if( iDestroyed==0 || iTab<iDestroyed ){
1871 iLargest = iTab;
1872 }
1873 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1874 int iIdx = pIdx->tnum;
1875 assert( pIdx->pSchema==pTab->pSchema );
1876 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1877 iLargest = iIdx;
1878 }
1879 }
1880 if( iLargest==0 ){
1881 return;
1882 }else{
1883 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1884 destroyRootPage(pParse, iLargest, iDb);
1885 iDestroyed = iLargest;
1886 }
1887 }
1888#endif
1889}
1890
1891/*
1892** This routine is called to do the work of a DROP TABLE statement.
1893** pName is the name of the table to be dropped.
1894*/
1895void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
1896 Table *pTab;
1897 Vdbe *v;
1898 sqlite3 *db = pParse->db;
1899 int iDb;
1900
1901 if( pParse->nErr || db->mallocFailed ){
1902 goto exit_drop_table;
1903 }
1904 assert( pName->nSrc==1 );
1905 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1906
1907 if( pTab==0 ){
1908 if( noErr ){
1909 sqlite3ErrorClear(pParse);
1910 }
1911 goto exit_drop_table;
1912 }
1913 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1914 assert( iDb>=0 && iDb<db->nDb );
1915#ifndef SQLITE_OMIT_AUTHORIZATION
1916 {
1917 int code;
1918 const char *zTab = SCHEMA_TABLE(iDb);
1919 const char *zDb = db->aDb[iDb].zName;
1920 const char *zArg2 = 0;
1921 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
1922 goto exit_drop_table;
1923 }
1924 if( isView ){
1925 if( !OMIT_TEMPDB && iDb==1 ){
1926 code = SQLITE_DROP_TEMP_VIEW;
1927 }else{
1928 code = SQLITE_DROP_VIEW;
1929 }
1930#ifndef SQLITE_OMIT_VIRTUALTABLE
1931 }else if( IsVirtual(pTab) ){
1932 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1933 goto exit_drop_table;
1934 }
1935 code = SQLITE_DROP_VTABLE;
1936 zArg2 = pTab->pMod->zName;
1937#endif
1938 }else{
1939 if( !OMIT_TEMPDB && iDb==1 ){
1940 code = SQLITE_DROP_TEMP_TABLE;
1941 }else{
1942 code = SQLITE_DROP_TABLE;
1943 }
1944 }
1945 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
1946 goto exit_drop_table;
1947 }
1948 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1949 goto exit_drop_table;
1950 }
1951 }
1952#endif
1953 if( pTab->readOnly || pTab==db->aDb[iDb].pSchema->pSeqTab ){
1954 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
1955 goto exit_drop_table;
1956 }
1957
1958#ifndef SQLITE_OMIT_VIEW
1959 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
1960 ** on a table.
1961 */
1962 if( isView && pTab->pSelect==0 ){
1963 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1964 goto exit_drop_table;
1965 }
1966 if( !isView && pTab->pSelect ){
1967 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1968 goto exit_drop_table;
1969 }
1970#endif
1971
1972 /* Generate code to remove the table from the master table
1973 ** on disk.
1974 */
1975 v = sqlite3GetVdbe(pParse);
1976 if( v ){
1977 Trigger *pTrigger;
1978 Db *pDb = &db->aDb[iDb];
1979 sqlite3BeginWriteOperation(pParse, 0, iDb);
1980
1981#ifndef SQLITE_OMIT_VIRTUALTABLE
1982 if( IsVirtual(pTab) ){
1983 Vdbe *v = sqlite3GetVdbe(pParse);
1984 if( v ){
1985 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0);
1986 }
1987 }
1988#endif
1989
1990 /* Drop all triggers associated with the table being dropped. Code
1991 ** is generated to remove entries from sqlite_master and/or
1992 ** sqlite_temp_master if required.
1993 */
1994 pTrigger = pTab->pTrigger;
1995 while( pTrigger ){
1996 assert( pTrigger->pSchema==pTab->pSchema ||
1997 pTrigger->pSchema==db->aDb[1].pSchema );
1998 sqlite3DropTriggerPtr(pParse, pTrigger);
1999 pTrigger = pTrigger->pNext;
2000 }
2001
2002#ifndef SQLITE_OMIT_AUTOINCREMENT
2003 /* Remove any entries of the sqlite_sequence table associated with
2004 ** the table being dropped. This is done before the table is dropped
2005 ** at the btree level, in case the sqlite_sequence table needs to
2006 ** move as a result of the drop (can happen in auto-vacuum mode).
2007 */
2008 if( pTab->autoInc ){
2009 sqlite3NestedParse(pParse,
2010 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
2011 pDb->zName, pTab->zName
2012 );
2013 }
2014#endif
2015
2016 /* Drop all SQLITE_MASTER table and index entries that refer to the
2017 ** table. The program name loops through the master table and deletes
2018 ** every row that refers to a table of the same name as the one being
2019 ** dropped. Triggers are handled seperately because a trigger can be
2020 ** created in the temp database that refers to a table in another
2021 ** database.
2022 */
2023 sqlite3NestedParse(pParse,
2024 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
2025 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
2026 if( !isView && !IsVirtual(pTab) ){
2027 destroyTable(pParse, pTab);
2028 }
2029
2030 /* Remove the table entry from SQLite's internal schema and modify
2031 ** the schema cookie.
2032 */
2033 if( IsVirtual(pTab) ){
2034 sqlite3VdbeOp3(v, OP_VDestroy, iDb, 0, pTab->zName, 0);
2035 }
2036 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
2037 sqlite3ChangeCookie(db, v, iDb);
2038 }
2039 sqliteViewResetAll(db, iDb);
2040
2041exit_drop_table:
2042 sqlite3SrcListDelete(pName);
2043}
2044
2045/*
2046** This routine is called to create a new foreign key on the table
2047** currently under construction. pFromCol determines which columns
2048** in the current table point to the foreign key. If pFromCol==0 then
2049** connect the key to the last column inserted. pTo is the name of
2050** the table referred to. pToCol is a list of tables in the other
2051** pTo table that the foreign key points to. flags contains all
2052** information about the conflict resolution algorithms specified
2053** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2054**
2055** An FKey structure is created and added to the table currently
2056** under construction in the pParse->pNewTable field. The new FKey
2057** is not linked into db->aFKey at this point - that does not happen
2058** until sqlite3EndTable().
2059**
2060** The foreign key is set for IMMEDIATE processing. A subsequent call
2061** to sqlite3DeferForeignKey() might change this to DEFERRED.
2062*/
2063void sqlite3CreateForeignKey(
2064 Parse *pParse, /* Parsing context */
2065 ExprList *pFromCol, /* Columns in this table that point to other table */
2066 Token *pTo, /* Name of the other table */
2067 ExprList *pToCol, /* Columns in the other table */
2068 int flags /* Conflict resolution algorithms. */
2069){
2070#ifndef SQLITE_OMIT_FOREIGN_KEY
2071 FKey *pFKey = 0;
2072 Table *p = pParse->pNewTable;
2073 int nByte;
2074 int i;
2075 int nCol;
2076 char *z;
2077
2078 assert( pTo!=0 );
2079 if( p==0 || pParse->nErr || IN_DECLARE_VTAB ) goto fk_end;
2080 if( pFromCol==0 ){
2081 int iCol = p->nCol-1;
2082 if( iCol<0 ) goto fk_end;
2083 if( pToCol && pToCol->nExpr!=1 ){
2084 sqlite3ErrorMsg(pParse, "foreign key on %s"
2085 " should reference only one column of table %T",
2086 p->aCol[iCol].zName, pTo);
2087 goto fk_end;
2088 }
2089 nCol = 1;
2090 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
2091 sqlite3ErrorMsg(pParse,
2092 "number of columns in foreign key does not match the number of "
2093 "columns in the referenced table");
2094 goto fk_end;
2095 }else{
2096 nCol = pFromCol->nExpr;
2097 }
2098 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
2099 if( pToCol ){
2100 for(i=0; i<pToCol->nExpr; i++){
2101 nByte += strlen(pToCol->a[i].zName) + 1;
2102 }
2103 }
2104 pFKey = sqlite3DbMallocZero(pParse->db, nByte );
2105 if( pFKey==0 ){
2106 goto fk_end;
2107 }
2108 pFKey->pFrom = p;
2109 pFKey->pNextFrom = p->pFKey;
2110 z = (char*)&pFKey[1];
2111 pFKey->aCol = (struct sColMap*)z;
2112 z += sizeof(struct sColMap)*nCol;
2113 pFKey->zTo = z;
2114 memcpy(z, pTo->z, pTo->n);
2115 z[pTo->n] = 0;
2116 z += pTo->n+1;
2117 pFKey->pNextTo = 0;
2118 pFKey->nCol = nCol;
2119 if( pFromCol==0 ){
2120 pFKey->aCol[0].iFrom = p->nCol-1;
2121 }else{
2122 for(i=0; i<nCol; i++){
2123 int j;
2124 for(j=0; j<p->nCol; j++){
2125 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
2126 pFKey->aCol[i].iFrom = j;
2127 break;
2128 }
2129 }
2130 if( j>=p->nCol ){
2131 sqlite3ErrorMsg(pParse,
2132 "unknown column \"%s\" in foreign key definition",
2133 pFromCol->a[i].zName);
2134 goto fk_end;
2135 }
2136 }
2137 }
2138 if( pToCol ){
2139 for(i=0; i<nCol; i++){
2140 int n = strlen(pToCol->a[i].zName);
2141 pFKey->aCol[i].zCol = z;
2142 memcpy(z, pToCol->a[i].zName, n);
2143 z[n] = 0;
2144 z += n+1;
2145 }
2146 }
2147 pFKey->isDeferred = 0;
2148 pFKey->deleteConf = flags & 0xff;
2149 pFKey->updateConf = (flags >> 8 ) & 0xff;
2150 pFKey->insertConf = (flags >> 16 ) & 0xff;
2151
2152 /* Link the foreign key to the table as the last step.
2153 */
2154 p->pFKey = pFKey;
2155 pFKey = 0;
2156
2157fk_end:
2158 sqlite3_free(pFKey);
2159#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
2160 sqlite3ExprListDelete(pFromCol);
2161 sqlite3ExprListDelete(pToCol);
2162}
2163
2164/*
2165** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2166** clause is seen as part of a foreign key definition. The isDeferred
2167** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2168** The behavior of the most recently created foreign key is adjusted
2169** accordingly.
2170*/
2171void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
2172#ifndef SQLITE_OMIT_FOREIGN_KEY
2173 Table *pTab;
2174 FKey *pFKey;
2175 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
2176 pFKey->isDeferred = isDeferred;
2177#endif
2178}
2179
2180/*
2181** Generate code that will erase and refill index *pIdx. This is
2182** used to initialize a newly created index or to recompute the
2183** content of an index in response to a REINDEX command.
2184**
2185** if memRootPage is not negative, it means that the index is newly
2186** created. The memory cell specified by memRootPage contains the
2187** root page number of the index. If memRootPage is negative, then
2188** the index already exists and must be cleared before being refilled and
2189** the root page number of the index is taken from pIndex->tnum.
2190*/
2191static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2192 Table *pTab = pIndex->pTable; /* The table that is indexed */
2193 int iTab = pParse->nTab; /* Btree cursor used for pTab */
2194 int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */
2195 int addr1; /* Address of top of loop */
2196 int tnum; /* Root page of index */
2197 Vdbe *v; /* Generate code into this virtual machine */
2198 KeyInfo *pKey; /* KeyInfo for index */
2199 sqlite3 *db = pParse->db; /* The database connection */
2200 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
2201
2202#ifndef SQLITE_OMIT_AUTHORIZATION
2203 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
2204 db->aDb[iDb].zName ) ){
2205 return;
2206 }
2207#endif
2208
2209 /* Require a write-lock on the table to perform this operation */
2210 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
2211
2212 v = sqlite3GetVdbe(pParse);
2213 if( v==0 ) return;
2214 if( memRootPage>=0 ){
2215 sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0);
2216 tnum = 0;
2217 }else{
2218 tnum = pIndex->tnum;
2219 sqlite3VdbeAddOp(v, OP_Clear, tnum, iDb);
2220 }
2221 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
2222 pKey = sqlite3IndexKeyinfo(pParse, pIndex);
2223 sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum, (char *)pKey, P3_KEYINFO_HANDOFF);
2224 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
2225 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
2226 sqlite3GenerateIndexKey(v, pIndex, iTab);
2227 if( pIndex->onError!=OE_None ){
2228 int curaddr = sqlite3VdbeCurrentAddr(v);
2229 int addr2 = curaddr+4;
2230 sqlite3VdbeChangeP2(v, curaddr-1, addr2);
2231 sqlite3VdbeAddOp(v, OP_Rowid, iTab, 0);
2232 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
2233 sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2);
2234 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort,
2235 "indexed columns are not unique", P3_STATIC);
2236 assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) );
2237 }
2238 sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0);
2239 sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1);
2240 sqlite3VdbeJumpHere(v, addr1);
2241 sqlite3VdbeAddOp(v, OP_Close, iTab, 0);
2242 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2243}
2244
2245/*
2246** Create a new index for an SQL table. pName1.pName2 is the name of the index
2247** and pTblList is the name of the table that is to be indexed. Both will
2248** be NULL for a primary key or an index that is created to satisfy a
2249** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
2250** as the table to be indexed. pParse->pNewTable is a table that is
2251** currently being constructed by a CREATE TABLE statement.
2252**
2253** pList is a list of columns to be indexed. pList will be NULL if this
2254** is a primary key or unique-constraint on the most recent column added
2255** to the table currently under construction.
2256*/
2257void sqlite3CreateIndex(
2258 Parse *pParse, /* All information about this parse */
2259 Token *pName1, /* First part of index name. May be NULL */
2260 Token *pName2, /* Second part of index name. May be NULL */
2261 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
2262 ExprList *pList, /* A list of columns to be indexed */
2263 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
2264 Token *pStart, /* The CREATE token that begins this statement */
2265 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */
2266 int sortOrder, /* Sort order of primary key when pList==NULL */
2267 int ifNotExist /* Omit error if index already exists */
2268){
2269 Table *pTab = 0; /* Table to be indexed */
2270 Index *pIndex = 0; /* The index to be created */
2271 char *zName = 0; /* Name of the index */
2272 int nName; /* Number of characters in zName */
2273 int i, j;
2274 Token nullId; /* Fake token for an empty ID list */
2275 DbFixer sFix; /* For assigning database names to pTable */
2276 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
2277 sqlite3 *db = pParse->db;
2278 Db *pDb; /* The specific table containing the indexed database */
2279 int iDb; /* Index of the database that is being written */
2280 Token *pName = 0; /* Unqualified name of the index to create */
2281 struct ExprList_item *pListItem; /* For looping over pList */
2282 int nCol;
2283 int nExtra = 0;
2284 char *zExtra;
2285
2286 if( pParse->nErr || db->mallocFailed || IN_DECLARE_VTAB ){
2287 goto exit_create_index;
2288 }
2289
2290 /*
2291 ** Find the table that is to be indexed. Return early if not found.
2292 */
2293 if( pTblName!=0 ){
2294
2295 /* Use the two-part index name to determine the database
2296 ** to search for the table. 'Fix' the table name to this db
2297 ** before looking up the table.
2298 */
2299 assert( pName1 && pName2 );
2300 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
2301 if( iDb<0 ) goto exit_create_index;
2302
2303#ifndef SQLITE_OMIT_TEMPDB
2304 /* If the index name was unqualified, check if the the table
2305 ** is a temp table. If so, set the database to 1.
2306 */
2307 pTab = sqlite3SrcListLookup(pParse, pTblName);
2308 if( pName2 && pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
2309 iDb = 1;
2310 }
2311#endif
2312
2313 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2314 sqlite3FixSrcList(&sFix, pTblName)
2315 ){
2316 /* Because the parser constructs pTblName from a single identifier,
2317 ** sqlite3FixSrcList can never fail. */
2318 assert(0);
2319 }
2320 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
2321 pTblName->a[0].zDatabase);
2322 if( !pTab ) goto exit_create_index;
2323 assert( db->aDb[iDb].pSchema==pTab->pSchema );
2324 }else{
2325 assert( pName==0 );
2326 pTab = pParse->pNewTable;
2327 if( !pTab ) goto exit_create_index;
2328 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2329 }
2330 pDb = &db->aDb[iDb];
2331
2332 if( pTab==0 || pParse->nErr ) goto exit_create_index;
2333 if( pTab->readOnly ){
2334 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
2335 goto exit_create_index;
2336 }
2337#ifndef SQLITE_OMIT_VIEW
2338 if( pTab->pSelect ){
2339 sqlite3ErrorMsg(pParse, "views may not be indexed");
2340 goto exit_create_index;
2341 }
2342#endif
2343#ifndef SQLITE_OMIT_VIRTUALTABLE
2344 if( IsVirtual(pTab) ){
2345 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
2346 goto exit_create_index;
2347 }
2348#endif
2349
2350 /*
2351 ** Find the name of the index. Make sure there is not already another
2352 ** index or table with the same name.
2353 **
2354 ** Exception: If we are reading the names of permanent indices from the
2355 ** sqlite_master table (because some other process changed the schema) and
2356 ** one of the index names collides with the name of a temporary table or
2357 ** index, then we will continue to process this index.
2358 **
2359 ** If pName==0 it means that we are
2360 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2361 ** own name.
2362 */
2363 if( pName ){
2364 zName = sqlite3NameFromToken(db, pName);
2365 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
2366 if( zName==0 ) goto exit_create_index;
2367 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
2368 goto exit_create_index;
2369 }
2370 if( !db->init.busy ){
2371 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
2372 if( sqlite3FindTable(db, zName, 0)!=0 ){
2373 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2374 goto exit_create_index;
2375 }
2376 }
2377 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
2378 if( !ifNotExist ){
2379 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
2380 }
2381 goto exit_create_index;
2382 }
2383 }else{
2384 char zBuf[30];
2385 int n;
2386 Index *pLoop;
2387 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
2388 sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n);
2389 zName = 0;
2390 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
2391 if( zName==0 ){
2392 db->mallocFailed = 1;
2393 goto exit_create_index;
2394 }
2395 }
2396
2397 /* Check for authorization to create an index.
2398 */
2399#ifndef SQLITE_OMIT_AUTHORIZATION
2400 {
2401 const char *zDb = pDb->zName;
2402 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
2403 goto exit_create_index;
2404 }
2405 i = SQLITE_CREATE_INDEX;
2406 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
2407 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
2408 goto exit_create_index;
2409 }
2410 }
2411#endif
2412
2413 /* If pList==0, it means this routine was called to make a primary
2414 ** key out of the last column added to the table under construction.
2415 ** So create a fake list to simulate this.
2416 */
2417 if( pList==0 ){
2418 nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName;
2419 nullId.n = strlen((char*)nullId.z);
2420 pList = sqlite3ExprListAppend(pParse, 0, 0, &nullId);
2421 if( pList==0 ) goto exit_create_index;
2422 pList->a[0].sortOrder = sortOrder;
2423 }
2424
2425 /* Figure out how many bytes of space are required to store explicitly
2426 ** specified collation sequence names.
2427 */
2428 for(i=0; i<pList->nExpr; i++){
2429 Expr *pExpr = pList->a[i].pExpr;
2430 if( pExpr ){
2431 nExtra += (1 + strlen(pExpr->pColl->zName));
2432 }
2433 }
2434
2435 /*
2436 ** Allocate the index structure.
2437 */
2438 nName = strlen(zName);
2439 nCol = pList->nExpr;
2440 pIndex = sqlite3DbMallocZero(db,
2441 sizeof(Index) + /* Index structure */
2442 sizeof(int)*nCol + /* Index.aiColumn */
2443 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
2444 sizeof(char *)*nCol + /* Index.azColl */
2445 sizeof(u8)*nCol + /* Index.aSortOrder */
2446 nName + 1 + /* Index.zName */
2447 nExtra /* Collation sequence names */
2448 );
2449 if( db->mallocFailed ){
2450 goto exit_create_index;
2451 }
2452 pIndex->azColl = (char**)(&pIndex[1]);
2453 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
2454 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
2455 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
2456 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
2457 zExtra = (char *)(&pIndex->zName[nName+1]);
2458 memcpy(pIndex->zName, zName, nName+1);
2459 pIndex->pTable = pTab;
2460 pIndex->nColumn = pList->nExpr;
2461 pIndex->onError = onError;
2462 pIndex->autoIndex = pName==0;
2463 pIndex->pSchema = db->aDb[iDb].pSchema;
2464
2465 /* Check to see if we should honor DESC requests on index columns
2466 */
2467 if( pDb->pSchema->file_format>=4 ){
2468 sortOrderMask = -1; /* Honor DESC */
2469 }else{
2470 sortOrderMask = 0; /* Ignore DESC */
2471 }
2472
2473 /* Scan the names of the columns of the table to be indexed and
2474 ** load the column indices into the Index structure. Report an error
2475 ** if any column is not found.
2476 */
2477 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
2478 const char *zColName = pListItem->zName;
2479 Column *pTabCol;
2480 int requestedSortOrder;
2481 char *zColl; /* Collation sequence name */
2482
2483 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
2484 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
2485 }
2486 if( j>=pTab->nCol ){
2487 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
2488 pTab->zName, zColName);
2489 goto exit_create_index;
2490 }
2491 /* TODO: Add a test to make sure that the same column is not named
2492 ** more than once within the same index. Only the first instance of
2493 ** the column will ever be used by the optimizer. Note that using the
2494 ** same column more than once cannot be an error because that would
2495 ** break backwards compatibility - it needs to be a warning.
2496 */
2497 pIndex->aiColumn[i] = j;
2498 if( pListItem->pExpr ){
2499 assert( pListItem->pExpr->pColl );
2500 zColl = zExtra;
2501 sqlite3_snprintf(nExtra, zExtra, "%s", pListItem->pExpr->pColl->zName);
2502 zExtra += (strlen(zColl) + 1);
2503 }else{
2504 zColl = pTab->aCol[j].zColl;
2505 if( !zColl ){
2506 zColl = db->pDfltColl->zName;
2507 }
2508 }
2509 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl, -1) ){
2510 goto exit_create_index;
2511 }
2512 pIndex->azColl[i] = zColl;
2513 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
2514 pIndex->aSortOrder[i] = requestedSortOrder;
2515 }
2516 sqlite3DefaultRowEst(pIndex);
2517
2518 if( pTab==pParse->pNewTable ){
2519 /* This routine has been called to create an automatic index as a
2520 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2521 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2522 ** i.e. one of:
2523 **
2524 ** CREATE TABLE t(x PRIMARY KEY, y);
2525 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2526 **
2527 ** Either way, check to see if the table already has such an index. If
2528 ** so, don't bother creating this one. This only applies to
2529 ** automatically created indices. Users can do as they wish with
2530 ** explicit indices.
2531 */
2532 Index *pIdx;
2533 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2534 int k;
2535 assert( pIdx->onError!=OE_None );
2536 assert( pIdx->autoIndex );
2537 assert( pIndex->onError!=OE_None );
2538
2539 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2540 for(k=0; k<pIdx->nColumn; k++){
2541 const char *z1 = pIdx->azColl[k];
2542 const char *z2 = pIndex->azColl[k];
2543 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2544 if( pIdx->aSortOrder[k]!=pIndex->aSortOrder[k] ) break;
2545 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
2546 }
2547 if( k==pIdx->nColumn ){
2548 if( pIdx->onError!=pIndex->onError ){
2549 /* This constraint creates the same index as a previous
2550 ** constraint specified somewhere in the CREATE TABLE statement.
2551 ** However the ON CONFLICT clauses are different. If both this
2552 ** constraint and the previous equivalent constraint have explicit
2553 ** ON CONFLICT clauses this is an error. Otherwise, use the
2554 ** explicitly specified behaviour for the index.
2555 */
2556 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2557 sqlite3ErrorMsg(pParse,
2558 "conflicting ON CONFLICT clauses specified", 0);
2559 }
2560 if( pIdx->onError==OE_Default ){
2561 pIdx->onError = pIndex->onError;
2562 }
2563 }
2564 goto exit_create_index;
2565 }
2566 }
2567 }
2568
2569 /* Link the new Index structure to its table and to the other
2570 ** in-memory database structures.
2571 */
2572 if( db->init.busy ){
2573 Index *p;
2574 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
2575 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
2576 if( p ){
2577 assert( p==pIndex ); /* Malloc must have failed */
2578 db->mallocFailed = 1;
2579 goto exit_create_index;
2580 }
2581 db->flags |= SQLITE_InternChanges;
2582 if( pTblName!=0 ){
2583 pIndex->tnum = db->init.newTnum;
2584 }
2585 }
2586
2587 /* If the db->init.busy is 0 then create the index on disk. This
2588 ** involves writing the index into the master table and filling in the
2589 ** index with the current table contents.
2590 **
2591 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2592 ** command. db->init.busy is 1 when a database is opened and
2593 ** CREATE INDEX statements are read out of the master table. In
2594 ** the latter case the index already exists on disk, which is why
2595 ** we don't want to recreate it.
2596 **
2597 ** If pTblName==0 it means this index is generated as a primary key
2598 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2599 ** has just been created, it contains no data and the index initialization
2600 ** step can be skipped.
2601 */
2602 else if( db->init.busy==0 ){
2603 Vdbe *v;
2604 char *zStmt;
2605 int iMem = pParse->nMem++;
2606
2607 v = sqlite3GetVdbe(pParse);
2608 if( v==0 ) goto exit_create_index;
2609
2610
2611 /* Create the rootpage for the index
2612 */
2613 sqlite3BeginWriteOperation(pParse, 1, iDb);
2614 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
2615 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2616
2617 /* Gather the complete text of the CREATE INDEX statement into
2618 ** the zStmt variable
2619 */
2620 if( pStart && pEnd ){
2621 /* A named index with an explicit CREATE INDEX statement */
2622 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
2623 onError==OE_None ? "" : " UNIQUE",
2624 pEnd->z - pName->z + 1,
2625 pName->z);
2626 }else{
2627 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
2628 /* zStmt = sqlite3MPrintf(""); */
2629 zStmt = 0;
2630 }
2631
2632 /* Add an entry in sqlite_master for this index
2633 */
2634 sqlite3NestedParse(pParse,
2635 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);",
2636 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2637 pIndex->zName,
2638 pTab->zName,
2639 zStmt
2640 );
2641 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2642 sqlite3_free(zStmt);
2643
2644 /* Fill the index with data and reparse the schema. Code an OP_Expire
2645 ** to invalidate all pre-compiled statements.
2646 */
2647 if( pTblName ){
2648 sqlite3RefillIndex(pParse, pIndex, iMem);
2649 sqlite3ChangeCookie(db, v, iDb);
2650 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2651 sqlite3MPrintf(db, "name='%q'", pIndex->zName), P3_DYNAMIC);
2652 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
2653 }
2654 }
2655
2656 /* When adding an index to the list of indices for a table, make
2657 ** sure all indices labeled OE_Replace come after all those labeled
2658 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2659 ** and INSERT.
2660 */
2661 if( db->init.busy || pTblName==0 ){
2662 if( onError!=OE_Replace || pTab->pIndex==0
2663 || pTab->pIndex->onError==OE_Replace){
2664 pIndex->pNext = pTab->pIndex;
2665 pTab->pIndex = pIndex;
2666 }else{
2667 Index *pOther = pTab->pIndex;
2668 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2669 pOther = pOther->pNext;
2670 }
2671 pIndex->pNext = pOther->pNext;
2672 pOther->pNext = pIndex;
2673 }
2674 pIndex = 0;
2675 }
2676
2677 /* Clean up before exiting */
2678exit_create_index:
2679 if( pIndex ){
2680 freeIndex(pIndex);
2681 }
2682 sqlite3ExprListDelete(pList);
2683 sqlite3SrcListDelete(pTblName);
2684 sqlite3_free(zName);
2685 return;
2686}
2687
2688/*
2689** Generate code to make sure the file format number is at least minFormat.
2690** The generated code will increase the file format number if necessary.
2691*/
2692void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
2693 Vdbe *v;
2694 v = sqlite3GetVdbe(pParse);
2695 if( v ){
2696 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);
2697 sqlite3VdbeUsesBtree(v, iDb);
2698 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
2699 sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3);
2700 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
2701 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
2702 }
2703}
2704
2705/*
2706** Fill the Index.aiRowEst[] array with default information - information
2707** to be used when we have not run the ANALYZE command.
2708**
2709** aiRowEst[0] is suppose to contain the number of elements in the index.
2710** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
2711** number of rows in the table that match any particular value of the
2712** first column of the index. aiRowEst[2] is an estimate of the number
2713** of rows that match any particular combiniation of the first 2 columns
2714** of the index. And so forth. It must always be the case that
2715*
2716** aiRowEst[N]<=aiRowEst[N-1]
2717** aiRowEst[N]>=1
2718**
2719** Apart from that, we have little to go on besides intuition as to
2720** how aiRowEst[] should be initialized. The numbers generated here
2721** are based on typical values found in actual indices.
2722*/
2723void sqlite3DefaultRowEst(Index *pIdx){
2724 unsigned *a = pIdx->aiRowEst;
2725 int i;
2726 assert( a!=0 );
2727 a[0] = 1000000;
2728 for(i=pIdx->nColumn; i>=5; i--){
2729 a[i] = 5;
2730 }
2731 while( i>=1 ){
2732 a[i] = 11 - i;
2733 i--;
2734 }
2735 if( pIdx->onError!=OE_None ){
2736 a[pIdx->nColumn] = 1;
2737 }
2738}
2739
2740/*
2741** This routine will drop an existing named index. This routine
2742** implements the DROP INDEX statement.
2743*/
2744void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
2745 Index *pIndex;
2746 Vdbe *v;
2747 sqlite3 *db = pParse->db;
2748 int iDb;
2749
2750 if( pParse->nErr || db->mallocFailed ){
2751 goto exit_drop_index;
2752 }
2753 assert( pName->nSrc==1 );
2754 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
2755 goto exit_drop_index;
2756 }
2757 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
2758 if( pIndex==0 ){
2759 if( !ifExists ){
2760 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
2761 }
2762 pParse->checkSchema = 1;
2763 goto exit_drop_index;
2764 }
2765 if( pIndex->autoIndex ){
2766 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
2767 "or PRIMARY KEY constraint cannot be dropped", 0);
2768 goto exit_drop_index;
2769 }
2770 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
2771#ifndef SQLITE_OMIT_AUTHORIZATION
2772 {
2773 int code = SQLITE_DROP_INDEX;
2774 Table *pTab = pIndex->pTable;
2775 const char *zDb = db->aDb[iDb].zName;
2776 const char *zTab = SCHEMA_TABLE(iDb);
2777 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
2778 goto exit_drop_index;
2779 }
2780 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
2781 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
2782 goto exit_drop_index;
2783 }
2784 }
2785#endif
2786
2787 /* Generate code to remove the index and from the master table */
2788 v = sqlite3GetVdbe(pParse);
2789 if( v ){
2790 sqlite3NestedParse(pParse,
2791 "DELETE FROM %Q.%s WHERE name=%Q",
2792 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2793 pIndex->zName
2794 );
2795 sqlite3ChangeCookie(db, v, iDb);
2796 destroyRootPage(pParse, pIndex->tnum, iDb);
2797 sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0);
2798 }
2799
2800exit_drop_index:
2801 sqlite3SrcListDelete(pName);
2802}
2803
2804/*
2805** pArray is a pointer to an array of objects. Each object in the
2806** array is szEntry bytes in size. This routine allocates a new
2807** object on the end of the array.
2808**
2809** *pnEntry is the number of entries already in use. *pnAlloc is
2810** the previously allocated size of the array. initSize is the
2811** suggested initial array size allocation.
2812**
2813** The index of the new entry is returned in *pIdx.
2814**
2815** This routine returns a pointer to the array of objects. This
2816** might be the same as the pArray parameter or it might be a different
2817** pointer if the array was resized.
2818*/
2819void *sqlite3ArrayAllocate(
2820 sqlite3 *db, /* Connection to notify of malloc failures */
2821 void *pArray, /* Array of objects. Might be reallocated */
2822 int szEntry, /* Size of each object in the array */
2823 int initSize, /* Suggested initial allocation, in elements */
2824 int *pnEntry, /* Number of objects currently in use */
2825 int *pnAlloc, /* Current size of the allocation, in elements */
2826 int *pIdx /* Write the index of a new slot here */
2827){
2828 char *z;
2829 if( *pnEntry >= *pnAlloc ){
2830 void *pNew;
2831 int newSize;
2832 newSize = (*pnAlloc)*2 + initSize;
2833 pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
2834 if( pNew==0 ){
2835 *pIdx = -1;
2836 return pArray;
2837 }
2838 *pnAlloc = newSize;
2839 pArray = pNew;
2840 }
2841 z = (char*)pArray;
2842 memset(&z[*pnEntry * szEntry], 0, szEntry);
2843 *pIdx = *pnEntry;
2844 ++*pnEntry;
2845 return pArray;
2846}
2847
2848/*
2849** Append a new element to the given IdList. Create a new IdList if
2850** need be.
2851**
2852** A new IdList is returned, or NULL if malloc() fails.
2853*/
2854IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
2855 int i;
2856 if( pList==0 ){
2857 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
2858 if( pList==0 ) return 0;
2859 pList->nAlloc = 0;
2860 }
2861 pList->a = sqlite3ArrayAllocate(
2862 db,
2863 pList->a,
2864 sizeof(pList->a[0]),
2865 5,
2866 &pList->nId,
2867 &pList->nAlloc,
2868 &i
2869 );
2870 if( i<0 ){
2871 sqlite3IdListDelete(pList);
2872 return 0;
2873 }
2874 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
2875 return pList;
2876}
2877
2878/*
2879** Delete an IdList.
2880*/
2881void sqlite3IdListDelete(IdList *pList){
2882 int i;
2883 if( pList==0 ) return;
2884 for(i=0; i<pList->nId; i++){
2885 sqlite3_free(pList->a[i].zName);
2886 }
2887 sqlite3_free(pList->a);
2888 sqlite3_free(pList);
2889}
2890
2891/*
2892** Return the index in pList of the identifier named zId. Return -1
2893** if not found.
2894*/
2895int sqlite3IdListIndex(IdList *pList, const char *zName){
2896 int i;
2897 if( pList==0 ) return -1;
2898 for(i=0; i<pList->nId; i++){
2899 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
2900 }
2901 return -1;
2902}
2903
2904/*
2905** Append a new table name to the given SrcList. Create a new SrcList if
2906** need be. A new entry is created in the SrcList even if pToken is NULL.
2907**
2908** A new SrcList is returned, or NULL if malloc() fails.
2909**
2910** If pDatabase is not null, it means that the table has an optional
2911** database name prefix. Like this: "database.table". The pDatabase
2912** points to the table name and the pTable points to the database name.
2913** The SrcList.a[].zName field is filled with the table name which might
2914** come from pTable (if pDatabase is NULL) or from pDatabase.
2915** SrcList.a[].zDatabase is filled with the database name from pTable,
2916** or with NULL if no database is specified.
2917**
2918** In other words, if call like this:
2919**
2920** sqlite3SrcListAppend(D,A,B,0);
2921**
2922** Then B is a table name and the database name is unspecified. If called
2923** like this:
2924**
2925** sqlite3SrcListAppend(D,A,B,C);
2926**
2927** Then C is the table name and B is the database name.
2928*/
2929SrcList *sqlite3SrcListAppend(
2930 sqlite3 *db, /* Connection to notify of malloc failures */
2931 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
2932 Token *pTable, /* Table to append */
2933 Token *pDatabase /* Database of the table */
2934){
2935 struct SrcList_item *pItem;
2936 if( pList==0 ){
2937 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
2938 if( pList==0 ) return 0;
2939 pList->nAlloc = 1;
2940 }
2941 if( pList->nSrc>=pList->nAlloc ){
2942 SrcList *pNew;
2943 pList->nAlloc *= 2;
2944 pNew = sqlite3DbRealloc(db, pList,
2945 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
2946 if( pNew==0 ){
2947 sqlite3SrcListDelete(pList);
2948 return 0;
2949 }
2950 pList = pNew;
2951 }
2952 pItem = &pList->a[pList->nSrc];
2953 memset(pItem, 0, sizeof(pList->a[0]));
2954 if( pDatabase && pDatabase->z==0 ){
2955 pDatabase = 0;
2956 }
2957 if( pDatabase && pTable ){
2958 Token *pTemp = pDatabase;
2959 pDatabase = pTable;
2960 pTable = pTemp;
2961 }
2962 pItem->zName = sqlite3NameFromToken(db, pTable);
2963 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
2964 pItem->iCursor = -1;
2965 pItem->isPopulated = 0;
2966 pList->nSrc++;
2967 return pList;
2968}
2969
2970/*
2971** Assign cursors to all tables in a SrcList
2972*/
2973void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
2974 int i;
2975 struct SrcList_item *pItem;
2976 assert(pList || pParse->db->mallocFailed );
2977 if( pList ){
2978 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
2979 if( pItem->iCursor>=0 ) break;
2980 pItem->iCursor = pParse->nTab++;
2981 if( pItem->pSelect ){
2982 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
2983 }
2984 }
2985 }
2986}
2987
2988/*
2989** Delete an entire SrcList including all its substructure.
2990*/
2991void sqlite3SrcListDelete(SrcList *pList){
2992 int i;
2993 struct SrcList_item *pItem;
2994 if( pList==0 ) return;
2995 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2996 sqlite3_free(pItem->zDatabase);
2997 sqlite3_free(pItem->zName);
2998 sqlite3_free(pItem->zAlias);
2999 sqlite3DeleteTable(pItem->pTab);
3000 sqlite3SelectDelete(pItem->pSelect);
3001 sqlite3ExprDelete(pItem->pOn);
3002 sqlite3IdListDelete(pItem->pUsing);
3003 }
3004 sqlite3_free(pList);
3005}
3006
3007/*
3008** This routine is called by the parser to add a new term to the
3009** end of a growing FROM clause. The "p" parameter is the part of
3010** the FROM clause that has already been constructed. "p" is NULL
3011** if this is the first term of the FROM clause. pTable and pDatabase
3012** are the name of the table and database named in the FROM clause term.
3013** pDatabase is NULL if the database name qualifier is missing - the
3014** usual case. If the term has a alias, then pAlias points to the
3015** alias token. If the term is a subquery, then pSubquery is the
3016** SELECT statement that the subquery encodes. The pTable and
3017** pDatabase parameters are NULL for subqueries. The pOn and pUsing
3018** parameters are the content of the ON and USING clauses.
3019**
3020** Return a new SrcList which encodes is the FROM with the new
3021** term added.
3022*/
3023SrcList *sqlite3SrcListAppendFromTerm(
3024 Parse *pParse, /* Parsing context */
3025 SrcList *p, /* The left part of the FROM clause already seen */
3026 Token *pTable, /* Name of the table to add to the FROM clause */
3027 Token *pDatabase, /* Name of the database containing pTable */
3028 Token *pAlias, /* The right-hand side of the AS subexpression */
3029 Select *pSubquery, /* A subquery used in place of a table name */
3030 Expr *pOn, /* The ON clause of a join */
3031 IdList *pUsing /* The USING clause of a join */
3032){
3033 struct SrcList_item *pItem;
3034 sqlite3 *db = pParse->db;
3035 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
3036 if( p==0 || p->nSrc==0 ){
3037 sqlite3ExprDelete(pOn);
3038 sqlite3IdListDelete(pUsing);
3039 sqlite3SelectDelete(pSubquery);
3040 return p;
3041 }
3042 pItem = &p->a[p->nSrc-1];
3043 if( pAlias && pAlias->n ){
3044 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
3045 }
3046 pItem->pSelect = pSubquery;
3047 pItem->pOn = pOn;
3048 pItem->pUsing = pUsing;
3049 return p;
3050}
3051
3052/*
3053** When building up a FROM clause in the parser, the join operator
3054** is initially attached to the left operand. But the code generator
3055** expects the join operator to be on the right operand. This routine
3056** Shifts all join operators from left to right for an entire FROM
3057** clause.
3058**
3059** Example: Suppose the join is like this:
3060**
3061** A natural cross join B
3062**
3063** The operator is "natural cross join". The A and B operands are stored
3064** in p->a[0] and p->a[1], respectively. The parser initially stores the
3065** operator with A. This routine shifts that operator over to B.
3066*/
3067void sqlite3SrcListShiftJoinType(SrcList *p){
3068 if( p && p->a ){
3069 int i;
3070 for(i=p->nSrc-1; i>0; i--){
3071 p->a[i].jointype = p->a[i-1].jointype;
3072 }
3073 p->a[0].jointype = 0;
3074 }
3075}
3076
3077/*
3078** Begin a transaction
3079*/
3080void sqlite3BeginTransaction(Parse *pParse, int type){
3081 sqlite3 *db;
3082 Vdbe *v;
3083 int i;
3084
3085 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
3086 if( pParse->nErr || db->mallocFailed ) return;
3087 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
3088
3089 v = sqlite3GetVdbe(pParse);
3090 if( !v ) return;
3091 if( type!=TK_DEFERRED ){
3092 for(i=0; i<db->nDb; i++){
3093 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
3094 sqlite3VdbeUsesBtree(v, i);
3095 }
3096 }
3097 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
3098}
3099
3100/*
3101** Commit a transaction
3102*/
3103void sqlite3CommitTransaction(Parse *pParse){
3104 sqlite3 *db;
3105 Vdbe *v;
3106
3107 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
3108 if( pParse->nErr || db->mallocFailed ) return;
3109 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
3110
3111 v = sqlite3GetVdbe(pParse);
3112 if( v ){
3113 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
3114 }
3115}
3116
3117/*
3118** Rollback a transaction
3119*/
3120void sqlite3RollbackTransaction(Parse *pParse){
3121 sqlite3 *db;
3122 Vdbe *v;
3123
3124 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
3125 if( pParse->nErr || db->mallocFailed ) return;
3126 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
3127
3128 v = sqlite3GetVdbe(pParse);
3129 if( v ){
3130 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
3131 }
3132}
3133
3134/*
3135** Make sure the TEMP database is open and available for use. Return
3136** the number of errors. Leave any error messages in the pParse structure.
3137*/
3138int sqlite3OpenTempDatabase(Parse *pParse){
3139 sqlite3 *db = pParse->db;
3140 if( db->aDb[1].pBt==0 && !pParse->explain ){
3141 int rc;
3142 static const int flags =
3143 SQLITE_OPEN_READWRITE |
3144 SQLITE_OPEN_CREATE |
3145 SQLITE_OPEN_EXCLUSIVE |
3146 SQLITE_OPEN_DELETEONCLOSE |
3147 SQLITE_OPEN_TEMP_DB;
3148
3149 rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags,
3150 &db->aDb[1].pBt);
3151 if( rc!=SQLITE_OK ){
3152 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
3153 "file for storing temporary tables");
3154 pParse->rc = rc;
3155 return 1;
3156 }
3157 if( db->flags & !db->autoCommit ){
3158 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
3159 if( rc!=SQLITE_OK ){
3160 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
3161 "the temporary database file");
3162 pParse->rc = rc;
3163 return 1;
3164 }
3165 }
3166 assert( db->aDb[1].pSchema );
3167 }
3168 return 0;
3169}
3170
3171/*
3172** Generate VDBE code that will verify the schema cookie and start
3173** a read-transaction for all named database files.
3174**
3175** It is important that all schema cookies be verified and all
3176** read transactions be started before anything else happens in
3177** the VDBE program. But this routine can be called after much other
3178** code has been generated. So here is what we do:
3179**
3180** The first time this routine is called, we code an OP_Goto that
3181** will jump to a subroutine at the end of the program. Then we
3182** record every database that needs its schema verified in the
3183** pParse->cookieMask field. Later, after all other code has been
3184** generated, the subroutine that does the cookie verifications and
3185** starts the transactions will be coded and the OP_Goto P2 value
3186** will be made to point to that subroutine. The generation of the
3187** cookie verification subroutine code happens in sqlite3FinishCoding().
3188**
3189** If iDb<0 then code the OP_Goto only - don't set flag to verify the
3190** schema on any databases. This can be used to position the OP_Goto
3191** early in the code, before we know if any database tables will be used.
3192*/
3193void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
3194 sqlite3 *db;
3195 Vdbe *v;
3196 int mask;
3197
3198 v = sqlite3GetVdbe(pParse);
3199 if( v==0 ) return; /* This only happens if there was a prior error */
3200 db = pParse->db;
3201 if( pParse->cookieGoto==0 ){
3202 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
3203 }
3204 if( iDb>=0 ){
3205 assert( iDb<db->nDb );
3206 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
3207 assert( iDb<SQLITE_MAX_ATTACHED+2 );
3208 mask = 1<<iDb;
3209 if( (pParse->cookieMask & mask)==0 ){
3210 pParse->cookieMask |= mask;
3211 pParse->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
3212 if( !OMIT_TEMPDB && iDb==1 ){
3213 sqlite3OpenTempDatabase(pParse);
3214 }
3215 }
3216 }
3217}
3218
3219/*
3220** Generate VDBE code that prepares for doing an operation that
3221** might change the database.
3222**
3223** This routine starts a new transaction if we are not already within
3224** a transaction. If we are already within a transaction, then a checkpoint
3225** is set if the setStatement parameter is true. A checkpoint should
3226** be set for operations that might fail (due to a constraint) part of
3227** the way through and which will need to undo some writes without having to
3228** rollback the whole transaction. For operations where all constraints
3229** can be checked before any changes are made to the database, it is never
3230** necessary to undo a write and the checkpoint should not be set.
3231**
3232** Only database iDb and the temp database are made writable by this call.
3233** If iDb==0, then the main and temp databases are made writable. If
3234** iDb==1 then only the temp database is made writable. If iDb>1 then the
3235** specified auxiliary database and the temp database are made writable.
3236*/
3237void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
3238 Vdbe *v = sqlite3GetVdbe(pParse);
3239 if( v==0 ) return;
3240 sqlite3CodeVerifySchema(pParse, iDb);
3241 pParse->writeMask |= 1<<iDb;
3242 if( setStatement && pParse->nested==0 ){
3243 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
3244 }
3245 if( (OMIT_TEMPDB || iDb!=1) && pParse->db->aDb[1].pBt!=0 ){
3246 sqlite3BeginWriteOperation(pParse, setStatement, 1);
3247 }
3248}
3249
3250/*
3251** Check to see if pIndex uses the collating sequence pColl. Return
3252** true if it does and false if it does not.
3253*/
3254#ifndef SQLITE_OMIT_REINDEX
3255static int collationMatch(const char *zColl, Index *pIndex){
3256 int i;
3257 for(i=0; i<pIndex->nColumn; i++){
3258 const char *z = pIndex->azColl[i];
3259 if( z==zColl || (z && zColl && 0==sqlite3StrICmp(z, zColl)) ){
3260 return 1;
3261 }
3262 }
3263 return 0;
3264}
3265#endif
3266
3267/*
3268** Recompute all indices of pTab that use the collating sequence pColl.
3269** If pColl==0 then recompute all indices of pTab.
3270*/
3271#ifndef SQLITE_OMIT_REINDEX
3272static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
3273 Index *pIndex; /* An index associated with pTab */
3274
3275 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
3276 if( zColl==0 || collationMatch(zColl, pIndex) ){
3277 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
3278 sqlite3BeginWriteOperation(pParse, 0, iDb);
3279 sqlite3RefillIndex(pParse, pIndex, -1);
3280 }
3281 }
3282}
3283#endif
3284
3285/*
3286** Recompute all indices of all tables in all databases where the
3287** indices use the collating sequence pColl. If pColl==0 then recompute
3288** all indices everywhere.
3289*/
3290#ifndef SQLITE_OMIT_REINDEX
3291static void reindexDatabases(Parse *pParse, char const *zColl){
3292 Db *pDb; /* A single database */
3293 int iDb; /* The database index number */
3294 sqlite3 *db = pParse->db; /* The database connection */
3295 HashElem *k; /* For looping over tables in pDb */
3296 Table *pTab; /* A table in the database */
3297
3298 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
3299 assert( pDb!=0 );
3300 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
3301 pTab = (Table*)sqliteHashData(k);
3302 reindexTable(pParse, pTab, zColl);
3303 }
3304 }
3305}
3306#endif
3307
3308/*
3309** Generate code for the REINDEX command.
3310**
3311** REINDEX -- 1
3312** REINDEX <collation> -- 2
3313** REINDEX ?<database>.?<tablename> -- 3
3314** REINDEX ?<database>.?<indexname> -- 4
3315**
3316** Form 1 causes all indices in all attached databases to be rebuilt.
3317** Form 2 rebuilds all indices in all databases that use the named
3318** collating function. Forms 3 and 4 rebuild the named index or all
3319** indices associated with the named table.
3320*/
3321#ifndef SQLITE_OMIT_REINDEX
3322void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
3323 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
3324 char *z; /* Name of a table or index */
3325 const char *zDb; /* Name of the database */
3326 Table *pTab; /* A table in the database */
3327 Index *pIndex; /* An index associated with pTab */
3328 int iDb; /* The database index number */
3329 sqlite3 *db = pParse->db; /* The database connection */
3330 Token *pObjName; /* Name of the table or index to be reindexed */
3331
3332 /* Read the database schema. If an error occurs, leave an error message
3333 ** and code in pParse and return NULL. */
3334 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3335 return;
3336 }
3337
3338 if( pName1==0 || pName1->z==0 ){
3339 reindexDatabases(pParse, 0);
3340 return;
3341 }else if( pName2==0 || pName2->z==0 ){
3342 assert( pName1->z );
3343 pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
3344 if( pColl ){
3345 char *zColl = sqlite3DbStrNDup(db, (const char *)pName1->z, pName1->n);
3346 if( zColl ){
3347 reindexDatabases(pParse, zColl);
3348 sqlite3_free(zColl);
3349 }
3350 return;
3351 }
3352 }
3353 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
3354 if( iDb<0 ) return;
3355 z = sqlite3NameFromToken(db, pObjName);
3356 if( z==0 ) return;
3357 zDb = db->aDb[iDb].zName;
3358 pTab = sqlite3FindTable(db, z, zDb);
3359 if( pTab ){
3360 reindexTable(pParse, pTab, 0);
3361 sqlite3_free(z);
3362 return;
3363 }
3364 pIndex = sqlite3FindIndex(db, z, zDb);
3365 sqlite3_free(z);
3366 if( pIndex ){
3367 sqlite3BeginWriteOperation(pParse, 0, iDb);
3368 sqlite3RefillIndex(pParse, pIndex, -1);
3369 return;
3370 }
3371 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
3372}
3373#endif
3374
3375/*
3376** Return a dynamicly allocated KeyInfo structure that can be used
3377** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
3378**
3379** If successful, a pointer to the new structure is returned. In this case
3380** the caller is responsible for calling sqlite3_free() on the returned
3381** pointer. If an error occurs (out of memory or missing collation
3382** sequence), NULL is returned and the state of pParse updated to reflect
3383** the error.
3384*/
3385KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
3386 int i;
3387 int nCol = pIdx->nColumn;
3388 int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
3389 KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(pParse->db, nBytes);
3390
3391 if( pKey ){
3392 pKey->db = pParse->db;
3393 pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
3394 assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
3395 for(i=0; i<nCol; i++){
3396 char *zColl = pIdx->azColl[i];
3397 assert( zColl );
3398 pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl, -1);
3399 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
3400 }
3401 pKey->nField = nCol;
3402 }
3403
3404 if( pParse->nErr ){
3405 sqlite3_free(pKey);
3406 pKey = 0;
3407 }
3408 return pKey;
3409}
diff --git a/libraries/sqlite/win32/callback.c b/libraries/sqlite/win32/callback.c
new file mode 100755
index 0000000..009cfd7
--- /dev/null
+++ b/libraries/sqlite/win32/callback.c
@@ -0,0 +1,378 @@
1/*
2** 2005 May 23
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.
10**
11*************************************************************************
12**
13** This file contains functions used to access the internal hash tables
14** of user defined functions and collation sequences.
15**
16** $Id: callback.c,v 1.23 2007/08/29 12:31:26 danielk1977 Exp $
17*/
18
19#include "sqliteInt.h"
20
21/*
22** Invoke the 'collation needed' callback to request a collation sequence
23** in the database text encoding of name zName, length nName.
24** If the collation sequence
25*/
26static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
27 assert( !db->xCollNeeded || !db->xCollNeeded16 );
28 if( nName<0 ) nName = strlen(zName);
29 if( db->xCollNeeded ){
30 char *zExternal = sqlite3DbStrNDup(db, zName, nName);
31 if( !zExternal ) return;
32 db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
33 sqlite3_free(zExternal);
34 }
35#ifndef SQLITE_OMIT_UTF16
36 if( db->xCollNeeded16 ){
37 char const *zExternal;
38 sqlite3_value *pTmp = sqlite3ValueNew(db);
39 sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC);
40 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
41 if( zExternal ){
42 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
43 }
44 sqlite3ValueFree(pTmp);
45 }
46#endif
47}
48
49/*
50** This routine is called if the collation factory fails to deliver a
51** collation function in the best encoding but there may be other versions
52** of this collation function (for other text encodings) available. Use one
53** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
54** possible.
55*/
56static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
57 CollSeq *pColl2;
58 char *z = pColl->zName;
59 int n = strlen(z);
60 int i;
61 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
62 for(i=0; i<3; i++){
63 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
64 if( pColl2->xCmp!=0 ){
65 memcpy(pColl, pColl2, sizeof(CollSeq));
66 pColl->xDel = 0; /* Do not copy the destructor */
67 return SQLITE_OK;
68 }
69 }
70 return SQLITE_ERROR;
71}
72
73/*
74** This function is responsible for invoking the collation factory callback
75** or substituting a collation sequence of a different encoding when the
76** requested collation sequence is not available in the database native
77** encoding.
78**
79** If it is not NULL, then pColl must point to the database native encoding
80** collation sequence with name zName, length nName.
81**
82** The return value is either the collation sequence to be used in database
83** db for collation type name zName, length nName, or NULL, if no collation
84** sequence can be found.
85*/
86CollSeq *sqlite3GetCollSeq(
87 sqlite3* db,
88 CollSeq *pColl,
89 const char *zName,
90 int nName
91){
92 CollSeq *p;
93
94 p = pColl;
95 if( !p ){
96 p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
97 }
98 if( !p || !p->xCmp ){
99 /* No collation sequence of this type for this encoding is registered.
100 ** Call the collation factory to see if it can supply us with one.
101 */
102 callCollNeeded(db, zName, nName);
103 p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
104 }
105 if( p && !p->xCmp && synthCollSeq(db, p) ){
106 p = 0;
107 }
108 assert( !p || p->xCmp );
109 return p;
110}
111
112/*
113** This routine is called on a collation sequence before it is used to
114** check that it is defined. An undefined collation sequence exists when
115** a database is loaded that contains references to collation sequences
116** that have not been defined by sqlite3_create_collation() etc.
117**
118** If required, this routine calls the 'collation needed' callback to
119** request a definition of the collating sequence. If this doesn't work,
120** an equivalent collating sequence that uses a text encoding different
121** from the main database is substituted, if one is available.
122*/
123int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
124 if( pColl ){
125 const char *zName = pColl->zName;
126 CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName, -1);
127 if( !p ){
128 if( pParse->nErr==0 ){
129 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
130 }
131 pParse->nErr++;
132 return SQLITE_ERROR;
133 }
134 assert( p==pColl );
135 }
136 return SQLITE_OK;
137}
138
139
140
141/*
142** Locate and return an entry from the db.aCollSeq hash table. If the entry
143** specified by zName and nName is not found and parameter 'create' is
144** true, then create a new entry. Otherwise return NULL.
145**
146** Each pointer stored in the sqlite3.aCollSeq hash table contains an
147** array of three CollSeq structures. The first is the collation sequence
148** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
149**
150** Stored immediately after the three collation sequences is a copy of
151** the collation sequence name. A pointer to this string is stored in
152** each collation sequence structure.
153*/
154static CollSeq *findCollSeqEntry(
155 sqlite3 *db,
156 const char *zName,
157 int nName,
158 int create
159){
160 CollSeq *pColl;
161 if( nName<0 ) nName = strlen(zName);
162 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
163
164 if( 0==pColl && create ){
165 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
166 if( pColl ){
167 CollSeq *pDel = 0;
168 pColl[0].zName = (char*)&pColl[3];
169 pColl[0].enc = SQLITE_UTF8;
170 pColl[1].zName = (char*)&pColl[3];
171 pColl[1].enc = SQLITE_UTF16LE;
172 pColl[2].zName = (char*)&pColl[3];
173 pColl[2].enc = SQLITE_UTF16BE;
174 memcpy(pColl[0].zName, zName, nName);
175 pColl[0].zName[nName] = 0;
176 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
177
178 /* If a malloc() failure occured in sqlite3HashInsert(), it will
179 ** return the pColl pointer to be deleted (because it wasn't added
180 ** to the hash table).
181 */
182 assert( pDel==0 || pDel==pColl );
183 if( pDel!=0 ){
184 db->mallocFailed = 1;
185 sqlite3_free(pDel);
186 pColl = 0;
187 }
188 }
189 }
190 return pColl;
191}
192
193/*
194** Parameter zName points to a UTF-8 encoded string nName bytes long.
195** Return the CollSeq* pointer for the collation sequence named zName
196** for the encoding 'enc' from the database 'db'.
197**
198** If the entry specified is not found and 'create' is true, then create a
199** new entry. Otherwise return NULL.
200**
201** A separate function sqlite3LocateCollSeq() is a wrapper around
202** this routine. sqlite3LocateCollSeq() invokes the collation factory
203** if necessary and generates an error message if the collating sequence
204** cannot be found.
205*/
206CollSeq *sqlite3FindCollSeq(
207 sqlite3 *db,
208 u8 enc,
209 const char *zName,
210 int nName,
211 int create
212){
213 CollSeq *pColl;
214 if( zName ){
215 pColl = findCollSeqEntry(db, zName, nName, create);
216 }else{
217 pColl = db->pDfltColl;
218 }
219 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
220 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
221 if( pColl ) pColl += enc-1;
222 return pColl;
223}
224
225/*
226** Locate a user function given a name, a number of arguments and a flag
227** indicating whether the function prefers UTF-16 over UTF-8. Return a
228** pointer to the FuncDef structure that defines that function, or return
229** NULL if the function does not exist.
230**
231** If the createFlag argument is true, then a new (blank) FuncDef
232** structure is created and liked into the "db" structure if a
233** no matching function previously existed. When createFlag is true
234** and the nArg parameter is -1, then only a function that accepts
235** any number of arguments will be returned.
236**
237** If createFlag is false and nArg is -1, then the first valid
238** function found is returned. A function is valid if either xFunc
239** or xStep is non-zero.
240**
241** If createFlag is false, then a function with the required name and
242** number of arguments may be returned even if the eTextRep flag does not
243** match that requested.
244*/
245FuncDef *sqlite3FindFunction(
246 sqlite3 *db, /* An open database */
247 const char *zName, /* Name of the function. Not null-terminated */
248 int nName, /* Number of characters in the name */
249 int nArg, /* Number of arguments. -1 means any number */
250 u8 enc, /* Preferred text encoding */
251 int createFlag /* Create new entry if true and does not otherwise exist */
252){
253 FuncDef *p; /* Iterator variable */
254 FuncDef *pFirst; /* First function with this name */
255 FuncDef *pBest = 0; /* Best match found so far */
256 int bestmatch = 0;
257
258
259 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
260 if( nArg<-1 ) nArg = -1;
261
262 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
263 for(p=pFirst; p; p=p->pNext){
264 /* During the search for the best function definition, bestmatch is set
265 ** as follows to indicate the quality of the match with the definition
266 ** pointed to by pBest:
267 **
268 ** 0: pBest is NULL. No match has been found.
269 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
270 ** encoding is requested, or vice versa.
271 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
272 ** requested, or vice versa.
273 ** 3: A variable arguments function using the same text encoding.
274 ** 4: A function with the exact number of arguments requested that
275 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
276 ** 5: A function with the exact number of arguments requested that
277 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
278 ** 6: An exact match.
279 **
280 ** A larger value of 'matchqual' indicates a more desirable match.
281 */
282 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
283 int match = 1; /* Quality of this match */
284 if( p->nArg==nArg || nArg==-1 ){
285 match = 4;
286 }
287 if( enc==p->iPrefEnc ){
288 match += 2;
289 }
290 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
291 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
292 match += 1;
293 }
294
295 if( match>bestmatch ){
296 pBest = p;
297 bestmatch = match;
298 }
299 }
300 }
301
302 /* If the createFlag parameter is true, and the seach did not reveal an
303 ** exact match for the name, number of arguments and encoding, then add a
304 ** new entry to the hash table and return it.
305 */
306 if( createFlag && bestmatch<6 &&
307 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName))!=0 ){
308 pBest->nArg = nArg;
309 pBest->pNext = pFirst;
310 pBest->iPrefEnc = enc;
311 memcpy(pBest->zName, zName, nName);
312 pBest->zName[nName] = 0;
313 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
314 db->mallocFailed = 1;
315 sqlite3_free(pBest);
316 return 0;
317 }
318 }
319
320 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
321 return pBest;
322 }
323 return 0;
324}
325
326/*
327** Free all resources held by the schema structure. The void* argument points
328** at a Schema struct. This function does not call sqlite3_free() on the
329** pointer itself, it just cleans up subsiduary resources (i.e. the contents
330** of the schema hash tables).
331*/
332void sqlite3SchemaFree(void *p){
333 Hash temp1;
334 Hash temp2;
335 HashElem *pElem;
336 Schema *pSchema = (Schema *)p;
337
338 temp1 = pSchema->tblHash;
339 temp2 = pSchema->trigHash;
340 sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
341 sqlite3HashClear(&pSchema->aFKey);
342 sqlite3HashClear(&pSchema->idxHash);
343 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
344 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
345 }
346 sqlite3HashClear(&temp2);
347 sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
348 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
349 Table *pTab = sqliteHashData(pElem);
350 sqlite3DeleteTable(pTab);
351 }
352 sqlite3HashClear(&temp1);
353 pSchema->pSeqTab = 0;
354 pSchema->flags &= ~DB_SchemaLoaded;
355}
356
357/*
358** Find and return the schema associated with a BTree. Create
359** a new one if necessary.
360*/
361Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
362 Schema * p;
363 if( pBt ){
364 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
365 }else{
366 p = (Schema *)sqlite3MallocZero(sizeof(Schema));
367 }
368 if( !p ){
369 db->mallocFailed = 1;
370 }else if ( 0==p->file_format ){
371 sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
372 sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
373 sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
374 sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
375 p->enc = SQLITE_UTF8;
376 }
377 return p;
378}
diff --git a/libraries/sqlite/win32/complete.c b/libraries/sqlite/win32/complete.c
new file mode 100755
index 0000000..ae61d8a
--- /dev/null
+++ b/libraries/sqlite/win32/complete.c
@@ -0,0 +1,271 @@
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.
10**
11*************************************************************************
12** An tokenizer for SQL
13**
14** This file contains C code that implements the sqlite3_complete() API.
15** This code used to be part of the tokenizer.c source file. But by
16** separating it out, the code will be automatically omitted from
17** static links that do not use it.
18**
19** $Id: complete.c,v 1.6 2007/08/27 23:26:59 drh Exp $
20*/
21#include "sqliteInt.h"
22#ifndef SQLITE_OMIT_COMPLETE
23
24/*
25** This is defined in tokenize.c. We just have to import the definition.
26*/
27#ifndef SQLITE_AMALGAMATION
28#ifdef SQLITE_ASCII
29extern const char sqlite3IsAsciiIdChar[];
30#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
31#endif
32#ifdef SQLITE_EBCDIC
33extern const char sqlite3IsEbcdicIdChar[];
34#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
35#endif
36#endif /* SQLITE_AMALGAMATION */
37
38
39/*
40** Token types used by the sqlite3_complete() routine. See the header
41** comments on that procedure for additional information.
42*/
43#define tkSEMI 0
44#define tkWS 1
45#define tkOTHER 2
46#define tkEXPLAIN 3
47#define tkCREATE 4
48#define tkTEMP 5
49#define tkTRIGGER 6
50#define tkEND 7
51
52/*
53** Return TRUE if the given SQL string ends in a semicolon.
54**
55** Special handling is require for CREATE TRIGGER statements.
56** Whenever the CREATE TRIGGER keywords are seen, the statement
57** must end with ";END;".
58**
59** This implementation uses a state machine with 7 states:
60**
61** (0) START At the beginning or end of an SQL statement. This routine
62** returns 1 if it ends in the START state and 0 if it ends
63** in any other state.
64**
65** (1) NORMAL We are in the middle of statement which ends with a single
66** semicolon.
67**
68** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
69** a statement.
70**
71** (3) CREATE The keyword CREATE has been seen at the beginning of a
72** statement, possibly preceeded by EXPLAIN and/or followed by
73** TEMP or TEMPORARY
74**
75** (4) TRIGGER We are in the middle of a trigger definition that must be
76** ended by a semicolon, the keyword END, and another semicolon.
77**
78** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
79** the end of a trigger definition.
80**
81** (6) END We've seen the ";END" of the ";END;" that occurs at the end
82** of a trigger difinition.
83**
84** Transitions between states above are determined by tokens extracted
85** from the input. The following tokens are significant:
86**
87** (0) tkSEMI A semicolon.
88** (1) tkWS Whitespace
89** (2) tkOTHER Any other SQL token.
90** (3) tkEXPLAIN The "explain" keyword.
91** (4) tkCREATE The "create" keyword.
92** (5) tkTEMP The "temp" or "temporary" keyword.
93** (6) tkTRIGGER The "trigger" keyword.
94** (7) tkEND The "end" keyword.
95**
96** Whitespace never causes a state transition and is always ignored.
97**
98** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
99** to recognize the end of a trigger can be omitted. All we have to do
100** is look for a semicolon that is not part of an string or comment.
101*/
102int sqlite3_complete(const char *zSql){
103 u8 state = 0; /* Current state, using numbers defined in header comment */
104 u8 token; /* Value of the next token */
105
106#ifndef SQLITE_OMIT_TRIGGER
107 /* A complex statement machine used to detect the end of a CREATE TRIGGER
108 ** statement. This is the normal case.
109 */
110 static const u8 trans[7][8] = {
111 /* Token: */
112 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
113 /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, },
114 /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, },
115 /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, },
116 /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, },
117 /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, },
118 /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, },
119 /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, },
120 };
121#else
122 /* If triggers are not suppored by this compile then the statement machine
123 ** used to detect the end of a statement is much simplier
124 */
125 static const u8 trans[2][3] = {
126 /* Token: */
127 /* State: ** SEMI WS OTHER */
128 /* 0 START: */ { 0, 0, 1, },
129 /* 1 NORMAL: */ { 0, 1, 1, },
130 };
131#endif /* SQLITE_OMIT_TRIGGER */
132
133 while( *zSql ){
134 switch( *zSql ){
135 case ';': { /* A semicolon */
136 token = tkSEMI;
137 break;
138 }
139 case ' ':
140 case '\r':
141 case '\t':
142 case '\n':
143 case '\f': { /* White space is ignored */
144 token = tkWS;
145 break;
146 }
147 case '/': { /* C-style comments */
148 if( zSql[1]!='*' ){
149 token = tkOTHER;
150 break;
151 }
152 zSql += 2;
153 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
154 if( zSql[0]==0 ) return 0;
155 zSql++;
156 token = tkWS;
157 break;
158 }
159 case '-': { /* SQL-style comments from "--" to end of line */
160 if( zSql[1]!='-' ){
161 token = tkOTHER;
162 break;
163 }
164 while( *zSql && *zSql!='\n' ){ zSql++; }
165 if( *zSql==0 ) return state==0;
166 token = tkWS;
167 break;
168 }
169 case '[': { /* Microsoft-style identifiers in [...] */
170 zSql++;
171 while( *zSql && *zSql!=']' ){ zSql++; }
172 if( *zSql==0 ) return 0;
173 token = tkOTHER;
174 break;
175 }
176 case '`': /* Grave-accent quoted symbols used by MySQL */
177 case '"': /* single- and double-quoted strings */
178 case '\'': {
179 int c = *zSql;
180 zSql++;
181 while( *zSql && *zSql!=c ){ zSql++; }
182 if( *zSql==0 ) return 0;
183 token = tkOTHER;
184 break;
185 }
186 default: {
187 int c;
188 if( IdChar((u8)*zSql) ){
189 /* Keywords and unquoted identifiers */
190 int nId;
191 for(nId=1; IdChar(zSql[nId]); nId++){}
192#ifdef SQLITE_OMIT_TRIGGER
193 token = tkOTHER;
194#else
195 switch( *zSql ){
196 case 'c': case 'C': {
197 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
198 token = tkCREATE;
199 }else{
200 token = tkOTHER;
201 }
202 break;
203 }
204 case 't': case 'T': {
205 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
206 token = tkTRIGGER;
207 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
208 token = tkTEMP;
209 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
210 token = tkTEMP;
211 }else{
212 token = tkOTHER;
213 }
214 break;
215 }
216 case 'e': case 'E': {
217 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
218 token = tkEND;
219 }else
220#ifndef SQLITE_OMIT_EXPLAIN
221 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
222 token = tkEXPLAIN;
223 }else
224#endif
225 {
226 token = tkOTHER;
227 }
228 break;
229 }
230 default: {
231 token = tkOTHER;
232 break;
233 }
234 }
235#endif /* SQLITE_OMIT_TRIGGER */
236 zSql += nId-1;
237 }else{
238 /* Operators and special symbols */
239 token = tkOTHER;
240 }
241 break;
242 }
243 }
244 state = trans[state][token];
245 zSql++;
246 }
247 return state==0;
248}
249
250#ifndef SQLITE_OMIT_UTF16
251/*
252** This routine is the same as the sqlite3_complete() routine described
253** above, except that the parameter is required to be UTF-16 encoded, not
254** UTF-8.
255*/
256int sqlite3_complete16(const void *zSql){
257 sqlite3_value *pVal;
258 char const *zSql8;
259 int rc = SQLITE_NOMEM;
260
261 pVal = sqlite3ValueNew(0);
262 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
263 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
264 if( zSql8 ){
265 rc = sqlite3_complete(zSql8);
266 }
267 sqlite3ValueFree(pVal);
268 return sqlite3ApiExit(0, rc);
269}
270#endif /* SQLITE_OMIT_UTF16 */
271#endif /* SQLITE_OMIT_COMPLETE */
diff --git a/libraries/sqlite/win32/date.c b/libraries/sqlite/win32/date.c
new file mode 100755
index 0000000..ce1c6c7
--- /dev/null
+++ b/libraries/sqlite/win32/date.c
@@ -0,0 +1,1045 @@
1/*
2** 2003 October 31
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.
10**
11*************************************************************************
12** This file contains the C functions that implement date and time
13** functions for SQLite.
14**
15** There is only one exported symbol in this file - the function
16** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
17** All other code has file scope.
18**
19** $Id: date.c,v 1.73 2007/09/12 17:01:45 danielk1977 Exp $
20**
21** SQLite processes all times and dates as Julian Day numbers. The
22** dates and times are stored as the number of days since noon
23** in Greenwich on November 24, 4714 B.C. according to the Gregorian
24** calendar system.
25**
26** 1970-01-01 00:00:00 is JD 2440587.5
27** 2000-01-01 00:00:00 is JD 2451544.5
28**
29** This implemention requires years to be expressed as a 4-digit number
30** which means that only dates between 0000-01-01 and 9999-12-31 can
31** be represented, even though julian day numbers allow a much wider
32** range of dates.
33**
34** The Gregorian calendar system is used for all dates and times,
35** even those that predate the Gregorian calendar. Historians usually
36** use the Julian calendar for dates prior to 1582-10-15 and for some
37** dates afterwards, depending on locale. Beware of this difference.
38**
39** The conversion algorithms are implemented based on descriptions
40** in the following text:
41**
42** Jean Meeus
43** Astronomical Algorithms, 2nd Edition, 1998
44** ISBM 0-943396-61-1
45** Willmann-Bell, Inc
46** Richmond, Virginia (USA)
47*/
48#include "sqliteInt.h"
49#include <ctype.h>
50#include <stdlib.h>
51#include <assert.h>
52#include <time.h>
53
54#ifndef SQLITE_OMIT_DATETIME_FUNCS
55
56/*
57** A structure for holding a single date and time.
58*/
59typedef struct DateTime DateTime;
60struct DateTime {
61 double rJD; /* The julian day number */
62 int Y, M, D; /* Year, month, and day */
63 int h, m; /* Hour and minutes */
64 int tz; /* Timezone offset in minutes */
65 double s; /* Seconds */
66 char validYMD; /* True if Y,M,D are valid */
67 char validHMS; /* True if h,m,s are valid */
68 char validJD; /* True if rJD is valid */
69 char validTZ; /* True if tz is valid */
70};
71
72
73/*
74** Convert zDate into one or more integers. Additional arguments
75** come in groups of 5 as follows:
76**
77** N number of digits in the integer
78** min minimum allowed value of the integer
79** max maximum allowed value of the integer
80** nextC first character after the integer
81** pVal where to write the integers value.
82**
83** Conversions continue until one with nextC==0 is encountered.
84** The function returns the number of successful conversions.
85*/
86static int getDigits(const char *zDate, ...){
87 va_list ap;
88 int val;
89 int N;
90 int min;
91 int max;
92 int nextC;
93 int *pVal;
94 int cnt = 0;
95 va_start(ap, zDate);
96 do{
97 N = va_arg(ap, int);
98 min = va_arg(ap, int);
99 max = va_arg(ap, int);
100 nextC = va_arg(ap, int);
101 pVal = va_arg(ap, int*);
102 val = 0;
103 while( N-- ){
104 if( !isdigit(*(u8*)zDate) ){
105 goto end_getDigits;
106 }
107 val = val*10 + *zDate - '0';
108 zDate++;
109 }
110 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
111 goto end_getDigits;
112 }
113 *pVal = val;
114 zDate++;
115 cnt++;
116 }while( nextC );
117end_getDigits:
118 va_end(ap);
119 return cnt;
120}
121
122/*
123** Read text from z[] and convert into a floating point number. Return
124** the number of digits converted.
125*/
126#define getValue sqlite3AtoF
127
128/*
129** Parse a timezone extension on the end of a date-time.
130** The extension is of the form:
131**
132** (+/-)HH:MM
133**
134** If the parse is successful, write the number of minutes
135** of change in *pnMin and return 0. If a parser error occurs,
136** return 0.
137**
138** A missing specifier is not considered an error.
139*/
140static int parseTimezone(const char *zDate, DateTime *p){
141 int sgn = 0;
142 int nHr, nMn;
143 while( isspace(*(u8*)zDate) ){ zDate++; }
144 p->tz = 0;
145 if( *zDate=='-' ){
146 sgn = -1;
147 }else if( *zDate=='+' ){
148 sgn = +1;
149 }else{
150 return *zDate!=0;
151 }
152 zDate++;
153 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
154 return 1;
155 }
156 zDate += 5;
157 p->tz = sgn*(nMn + nHr*60);
158 while( isspace(*(u8*)zDate) ){ zDate++; }
159 return *zDate!=0;
160}
161
162/*
163** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
164** The HH, MM, and SS must each be exactly 2 digits. The
165** fractional seconds FFFF can be one or more digits.
166**
167** Return 1 if there is a parsing error and 0 on success.
168*/
169static int parseHhMmSs(const char *zDate, DateTime *p){
170 int h, m, s;
171 double ms = 0.0;
172 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
173 return 1;
174 }
175 zDate += 5;
176 if( *zDate==':' ){
177 zDate++;
178 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
179 return 1;
180 }
181 zDate += 2;
182 if( *zDate=='.' && isdigit((u8)zDate[1]) ){
183 double rScale = 1.0;
184 zDate++;
185 while( isdigit(*(u8*)zDate) ){
186 ms = ms*10.0 + *zDate - '0';
187 rScale *= 10.0;
188 zDate++;
189 }
190 ms /= rScale;
191 }
192 }else{
193 s = 0;
194 }
195 p->validJD = 0;
196 p->validHMS = 1;
197 p->h = h;
198 p->m = m;
199 p->s = s + ms;
200 if( parseTimezone(zDate, p) ) return 1;
201 p->validTZ = p->tz!=0;
202 return 0;
203}
204
205/*
206** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
207** that the YYYY-MM-DD is according to the Gregorian calendar.
208**
209** Reference: Meeus page 61
210*/
211static void computeJD(DateTime *p){
212 int Y, M, D, A, B, X1, X2;
213
214 if( p->validJD ) return;
215 if( p->validYMD ){
216 Y = p->Y;
217 M = p->M;
218 D = p->D;
219 }else{
220 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
221 M = 1;
222 D = 1;
223 }
224 if( M<=2 ){
225 Y--;
226 M += 12;
227 }
228 A = Y/100;
229 B = 2 - A + (A/4);
230 X1 = 365.25*(Y+4716);
231 X2 = 30.6001*(M+1);
232 p->rJD = X1 + X2 + D + B - 1524.5;
233 p->validJD = 1;
234 if( p->validHMS ){
235 p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0;
236 if( p->validTZ ){
237 p->rJD -= p->tz*60/86400.0;
238 p->validYMD = 0;
239 p->validHMS = 0;
240 p->validTZ = 0;
241 }
242 }
243}
244
245/*
246** Parse dates of the form
247**
248** YYYY-MM-DD HH:MM:SS.FFF
249** YYYY-MM-DD HH:MM:SS
250** YYYY-MM-DD HH:MM
251** YYYY-MM-DD
252**
253** Write the result into the DateTime structure and return 0
254** on success and 1 if the input string is not a well-formed
255** date.
256*/
257static int parseYyyyMmDd(const char *zDate, DateTime *p){
258 int Y, M, D, neg;
259
260 if( zDate[0]=='-' ){
261 zDate++;
262 neg = 1;
263 }else{
264 neg = 0;
265 }
266 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
267 return 1;
268 }
269 zDate += 10;
270 while( isspace(*(u8*)zDate) || 'T'==*(u8*)zDate ){ zDate++; }
271 if( parseHhMmSs(zDate, p)==0 ){
272 /* We got the time */
273 }else if( *zDate==0 ){
274 p->validHMS = 0;
275 }else{
276 return 1;
277 }
278 p->validJD = 0;
279 p->validYMD = 1;
280 p->Y = neg ? -Y : Y;
281 p->M = M;
282 p->D = D;
283 if( p->validTZ ){
284 computeJD(p);
285 }
286 return 0;
287}
288
289/*
290** Attempt to parse the given string into a Julian Day Number. Return
291** the number of errors.
292**
293** The following are acceptable forms for the input string:
294**
295** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
296** DDDD.DD
297** now
298**
299** In the first form, the +/-HH:MM is always optional. The fractional
300** seconds extension (the ".FFF") is optional. The seconds portion
301** (":SS.FFF") is option. The year and date can be omitted as long
302** as there is a time string. The time string can be omitted as long
303** as there is a year and date.
304*/
305static int parseDateOrTime(
306 sqlite3_context *context,
307 const char *zDate,
308 DateTime *p
309){
310 memset(p, 0, sizeof(*p));
311 if( parseYyyyMmDd(zDate,p)==0 ){
312 return 0;
313 }else if( parseHhMmSs(zDate, p)==0 ){
314 return 0;
315 }else if( sqlite3StrICmp(zDate,"now")==0){
316 double r;
317 sqlite3OsCurrentTime((sqlite3_vfs *)sqlite3_user_data(context), &r);
318 p->rJD = r;
319 p->validJD = 1;
320 return 0;
321 }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
322 getValue(zDate, &p->rJD);
323 p->validJD = 1;
324 return 0;
325 }
326 return 1;
327}
328
329/*
330** Compute the Year, Month, and Day from the julian day number.
331*/
332static void computeYMD(DateTime *p){
333 int Z, A, B, C, D, E, X1;
334 if( p->validYMD ) return;
335 if( !p->validJD ){
336 p->Y = 2000;
337 p->M = 1;
338 p->D = 1;
339 }else{
340 Z = p->rJD + 0.5;
341 A = (Z - 1867216.25)/36524.25;
342 A = Z + 1 + A - (A/4);
343 B = A + 1524;
344 C = (B - 122.1)/365.25;
345 D = 365.25*C;
346 E = (B-D)/30.6001;
347 X1 = 30.6001*E;
348 p->D = B - D - X1;
349 p->M = E<14 ? E-1 : E-13;
350 p->Y = p->M>2 ? C - 4716 : C - 4715;
351 }
352 p->validYMD = 1;
353}
354
355/*
356** Compute the Hour, Minute, and Seconds from the julian day number.
357*/
358static void computeHMS(DateTime *p){
359 int Z, s;
360 if( p->validHMS ) return;
361 computeJD(p);
362 Z = p->rJD + 0.5;
363 s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
364 p->s = 0.001*s;
365 s = p->s;
366 p->s -= s;
367 p->h = s/3600;
368 s -= p->h*3600;
369 p->m = s/60;
370 p->s += s - p->m*60;
371 p->validHMS = 1;
372}
373
374/*
375** Compute both YMD and HMS
376*/
377static void computeYMD_HMS(DateTime *p){
378 computeYMD(p);
379 computeHMS(p);
380}
381
382/*
383** Clear the YMD and HMS and the TZ
384*/
385static void clearYMD_HMS_TZ(DateTime *p){
386 p->validYMD = 0;
387 p->validHMS = 0;
388 p->validTZ = 0;
389}
390
391/*
392** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
393** for the time value p where p is in UTC.
394*/
395static double localtimeOffset(DateTime *p){
396 DateTime x, y;
397 time_t t;
398 x = *p;
399 computeYMD_HMS(&x);
400 if( x.Y<1971 || x.Y>=2038 ){
401 x.Y = 2000;
402 x.M = 1;
403 x.D = 1;
404 x.h = 0;
405 x.m = 0;
406 x.s = 0.0;
407 } else {
408 int s = x.s + 0.5;
409 x.s = s;
410 }
411 x.tz = 0;
412 x.validJD = 0;
413 computeJD(&x);
414 t = (x.rJD-2440587.5)*86400.0 + 0.5;
415#ifdef HAVE_LOCALTIME_R
416 {
417 struct tm sLocal;
418 localtime_r(&t, &sLocal);
419 y.Y = sLocal.tm_year + 1900;
420 y.M = sLocal.tm_mon + 1;
421 y.D = sLocal.tm_mday;
422 y.h = sLocal.tm_hour;
423 y.m = sLocal.tm_min;
424 y.s = sLocal.tm_sec;
425 }
426#else
427 {
428 struct tm *pTm;
429 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
430 pTm = localtime(&t);
431 y.Y = pTm->tm_year + 1900;
432 y.M = pTm->tm_mon + 1;
433 y.D = pTm->tm_mday;
434 y.h = pTm->tm_hour;
435 y.m = pTm->tm_min;
436 y.s = pTm->tm_sec;
437 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
438 }
439#endif
440 y.validYMD = 1;
441 y.validHMS = 1;
442 y.validJD = 0;
443 y.validTZ = 0;
444 computeJD(&y);
445 return y.rJD - x.rJD;
446}
447
448/*
449** Process a modifier to a date-time stamp. The modifiers are
450** as follows:
451**
452** NNN days
453** NNN hours
454** NNN minutes
455** NNN.NNNN seconds
456** NNN months
457** NNN years
458** start of month
459** start of year
460** start of week
461** start of day
462** weekday N
463** unixepoch
464** localtime
465** utc
466**
467** Return 0 on success and 1 if there is any kind of error.
468*/
469static int parseModifier(const char *zMod, DateTime *p){
470 int rc = 1;
471 int n;
472 double r;
473 char *z, zBuf[30];
474 z = zBuf;
475 for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
476 z[n] = tolower(zMod[n]);
477 }
478 z[n] = 0;
479 switch( z[0] ){
480 case 'l': {
481 /* localtime
482 **
483 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
484 ** show local time.
485 */
486 if( strcmp(z, "localtime")==0 ){
487 computeJD(p);
488 p->rJD += localtimeOffset(p);
489 clearYMD_HMS_TZ(p);
490 rc = 0;
491 }
492 break;
493 }
494 case 'u': {
495 /*
496 ** unixepoch
497 **
498 ** Treat the current value of p->rJD as the number of
499 ** seconds since 1970. Convert to a real julian day number.
500 */
501 if( strcmp(z, "unixepoch")==0 && p->validJD ){
502 p->rJD = p->rJD/86400.0 + 2440587.5;
503 clearYMD_HMS_TZ(p);
504 rc = 0;
505 }else if( strcmp(z, "utc")==0 ){
506 double c1;
507 computeJD(p);
508 c1 = localtimeOffset(p);
509 p->rJD -= c1;
510 clearYMD_HMS_TZ(p);
511 p->rJD += c1 - localtimeOffset(p);
512 rc = 0;
513 }
514 break;
515 }
516 case 'w': {
517 /*
518 ** weekday N
519 **
520 ** Move the date to the same time on the next occurrence of
521 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
522 ** date is already on the appropriate weekday, this is a no-op.
523 */
524 if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
525 && (n=r)==r && n>=0 && r<7 ){
526 int Z;
527 computeYMD_HMS(p);
528 p->validTZ = 0;
529 p->validJD = 0;
530 computeJD(p);
531 Z = p->rJD + 1.5;
532 Z %= 7;
533 if( Z>n ) Z -= 7;
534 p->rJD += n - Z;
535 clearYMD_HMS_TZ(p);
536 rc = 0;
537 }
538 break;
539 }
540 case 's': {
541 /*
542 ** start of TTTTT
543 **
544 ** Move the date backwards to the beginning of the current day,
545 ** or month or year.
546 */
547 if( strncmp(z, "start of ", 9)!=0 ) break;
548 z += 9;
549 computeYMD(p);
550 p->validHMS = 1;
551 p->h = p->m = 0;
552 p->s = 0.0;
553 p->validTZ = 0;
554 p->validJD = 0;
555 if( strcmp(z,"month")==0 ){
556 p->D = 1;
557 rc = 0;
558 }else if( strcmp(z,"year")==0 ){
559 computeYMD(p);
560 p->M = 1;
561 p->D = 1;
562 rc = 0;
563 }else if( strcmp(z,"day")==0 ){
564 rc = 0;
565 }
566 break;
567 }
568 case '+':
569 case '-':
570 case '0':
571 case '1':
572 case '2':
573 case '3':
574 case '4':
575 case '5':
576 case '6':
577 case '7':
578 case '8':
579 case '9': {
580 n = getValue(z, &r);
581 assert( n>=1 );
582 if( z[n]==':' ){
583 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
584 ** specified number of hours, minutes, seconds, and fractional seconds
585 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
586 ** omitted.
587 */
588 const char *z2 = z;
589 DateTime tx;
590 int day;
591 if( !isdigit(*(u8*)z2) ) z2++;
592 memset(&tx, 0, sizeof(tx));
593 if( parseHhMmSs(z2, &tx) ) break;
594 computeJD(&tx);
595 tx.rJD -= 0.5;
596 day = (int)tx.rJD;
597 tx.rJD -= day;
598 if( z[0]=='-' ) tx.rJD = -tx.rJD;
599 computeJD(p);
600 clearYMD_HMS_TZ(p);
601 p->rJD += tx.rJD;
602 rc = 0;
603 break;
604 }
605 z += n;
606 while( isspace(*(u8*)z) ) z++;
607 n = strlen(z);
608 if( n>10 || n<3 ) break;
609 if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
610 computeJD(p);
611 rc = 0;
612 if( n==3 && strcmp(z,"day")==0 ){
613 p->rJD += r;
614 }else if( n==4 && strcmp(z,"hour")==0 ){
615 p->rJD += r/24.0;
616 }else if( n==6 && strcmp(z,"minute")==0 ){
617 p->rJD += r/(24.0*60.0);
618 }else if( n==6 && strcmp(z,"second")==0 ){
619 p->rJD += r/(24.0*60.0*60.0);
620 }else if( n==5 && strcmp(z,"month")==0 ){
621 int x, y;
622 computeYMD_HMS(p);
623 p->M += r;
624 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
625 p->Y += x;
626 p->M -= x*12;
627 p->validJD = 0;
628 computeJD(p);
629 y = r;
630 if( y!=r ){
631 p->rJD += (r - y)*30.0;
632 }
633 }else if( n==4 && strcmp(z,"year")==0 ){
634 computeYMD_HMS(p);
635 p->Y += r;
636 p->validJD = 0;
637 computeJD(p);
638 }else{
639 rc = 1;
640 }
641 clearYMD_HMS_TZ(p);
642 break;
643 }
644 default: {
645 break;
646 }
647 }
648 return rc;
649}
650
651/*
652** Process time function arguments. argv[0] is a date-time stamp.
653** argv[1] and following are modifiers. Parse them all and write
654** the resulting time into the DateTime structure p. Return 0
655** on success and 1 if there are any errors.
656*/
657static int isDate(
658 sqlite3_context *context,
659 int argc,
660 sqlite3_value **argv,
661 DateTime *p
662){
663 int i;
664 const unsigned char *z;
665 if( argc==0 ) return 1;
666 z = sqlite3_value_text(argv[0]);
667 if( !z || parseDateOrTime(context, (char*)z, p) ){
668 return 1;
669 }
670 for(i=1; i<argc; i++){
671 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
672 return 1;
673 }
674 }
675 return 0;
676}
677
678
679/*
680** The following routines implement the various date and time functions
681** of SQLite.
682*/
683
684/*
685** julianday( TIMESTRING, MOD, MOD, ...)
686**
687** Return the julian day number of the date specified in the arguments
688*/
689static void juliandayFunc(
690 sqlite3_context *context,
691 int argc,
692 sqlite3_value **argv
693){
694 DateTime x;
695 if( isDate(context, argc, argv, &x)==0 ){
696 computeJD(&x);
697 sqlite3_result_double(context, x.rJD);
698 }
699}
700
701/*
702** datetime( TIMESTRING, MOD, MOD, ...)
703**
704** Return YYYY-MM-DD HH:MM:SS
705*/
706static void datetimeFunc(
707 sqlite3_context *context,
708 int argc,
709 sqlite3_value **argv
710){
711 DateTime x;
712 if( isDate(context, argc, argv, &x)==0 ){
713 char zBuf[100];
714 computeYMD_HMS(&x);
715 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
716 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
717 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
718 }
719}
720
721/*
722** time( TIMESTRING, MOD, MOD, ...)
723**
724** Return HH:MM:SS
725*/
726static void timeFunc(
727 sqlite3_context *context,
728 int argc,
729 sqlite3_value **argv
730){
731 DateTime x;
732 if( isDate(context, argc, argv, &x)==0 ){
733 char zBuf[100];
734 computeHMS(&x);
735 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
736 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
737 }
738}
739
740/*
741** date( TIMESTRING, MOD, MOD, ...)
742**
743** Return YYYY-MM-DD
744*/
745static void dateFunc(
746 sqlite3_context *context,
747 int argc,
748 sqlite3_value **argv
749){
750 DateTime x;
751 if( isDate(context, argc, argv, &x)==0 ){
752 char zBuf[100];
753 computeYMD(&x);
754 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
755 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
756 }
757}
758
759/*
760** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
761**
762** Return a string described by FORMAT. Conversions as follows:
763**
764** %d day of month
765** %f ** fractional seconds SS.SSS
766** %H hour 00-24
767** %j day of year 000-366
768** %J ** Julian day number
769** %m month 01-12
770** %M minute 00-59
771** %s seconds since 1970-01-01
772** %S seconds 00-59
773** %w day of week 0-6 sunday==0
774** %W week of year 00-53
775** %Y year 0000-9999
776** %% %
777*/
778static void strftimeFunc(
779 sqlite3_context *context,
780 int argc,
781 sqlite3_value **argv
782){
783 DateTime x;
784 u64 n;
785 int i, j;
786 char *z;
787 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
788 char zBuf[100];
789 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
790 for(i=0, n=1; zFmt[i]; i++, n++){
791 if( zFmt[i]=='%' ){
792 switch( zFmt[i+1] ){
793 case 'd':
794 case 'H':
795 case 'm':
796 case 'M':
797 case 'S':
798 case 'W':
799 n++;
800 /* fall thru */
801 case 'w':
802 case '%':
803 break;
804 case 'f':
805 n += 8;
806 break;
807 case 'j':
808 n += 3;
809 break;
810 case 'Y':
811 n += 8;
812 break;
813 case 's':
814 case 'J':
815 n += 50;
816 break;
817 default:
818 return; /* ERROR. return a NULL */
819 }
820 i++;
821 }
822 }
823 if( n<sizeof(zBuf) ){
824 z = zBuf;
825 }else if( n>SQLITE_MAX_LENGTH ){
826 sqlite3_result_error_toobig(context);
827 return;
828 }else{
829 z = sqlite3_malloc( n );
830 if( z==0 ) return;
831 }
832 computeJD(&x);
833 computeYMD_HMS(&x);
834 for(i=j=0; zFmt[i]; i++){
835 if( zFmt[i]!='%' ){
836 z[j++] = zFmt[i];
837 }else{
838 i++;
839 switch( zFmt[i] ){
840 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
841 case 'f': {
842 double s = x.s;
843 if( s>59.999 ) s = 59.999;
844 sqlite3_snprintf(7, &z[j],"%06.3f", s);
845 j += strlen(&z[j]);
846 break;
847 }
848 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
849 case 'W': /* Fall thru */
850 case 'j': {
851 int nDay; /* Number of days since 1st day of year */
852 DateTime y = x;
853 y.validJD = 0;
854 y.M = 1;
855 y.D = 1;
856 computeJD(&y);
857 nDay = x.rJD - y.rJD + 0.5;
858 if( zFmt[i]=='W' ){
859 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
860 wd = ((int)(x.rJD+0.5)) % 7;
861 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
862 j += 2;
863 }else{
864 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
865 j += 3;
866 }
867 break;
868 }
869 case 'J': {
870 sqlite3_snprintf(20, &z[j],"%.16g",x.rJD);
871 j+=strlen(&z[j]);
872 break;
873 }
874 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
875 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
876 case 's': {
877 sqlite3_snprintf(30,&z[j],"%d",
878 (int)((x.rJD-2440587.5)*86400.0 + 0.5));
879 j += strlen(&z[j]);
880 break;
881 }
882 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
883 case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
884 case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break;
885 case '%': z[j++] = '%'; break;
886 }
887 }
888 }
889 z[j] = 0;
890 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
891 if( z!=zBuf ){
892 sqlite3_free(z);
893 }
894}
895
896/*
897** current_time()
898**
899** This function returns the same value as time('now').
900*/
901static void ctimeFunc(
902 sqlite3_context *context,
903 int argc,
904 sqlite3_value **argv
905){
906 sqlite3_value *pVal = sqlite3ValueNew(0);
907 if( pVal ){
908 sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
909 timeFunc(context, 1, &pVal);
910 sqlite3ValueFree(pVal);
911 }
912}
913
914/*
915** current_date()
916**
917** This function returns the same value as date('now').
918*/
919static void cdateFunc(
920 sqlite3_context *context,
921 int argc,
922 sqlite3_value **argv
923){
924 sqlite3_value *pVal = sqlite3ValueNew(0);
925 if( pVal ){
926 sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
927 dateFunc(context, 1, &pVal);
928 sqlite3ValueFree(pVal);
929 }
930}
931
932/*
933** current_timestamp()
934**
935** This function returns the same value as datetime('now').
936*/
937static void ctimestampFunc(
938 sqlite3_context *context,
939 int argc,
940 sqlite3_value **argv
941){
942 sqlite3_value *pVal = sqlite3ValueNew(0);
943 if( pVal ){
944 sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
945 datetimeFunc(context, 1, &pVal);
946 sqlite3ValueFree(pVal);
947 }
948}
949#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
950
951#ifdef SQLITE_OMIT_DATETIME_FUNCS
952/*
953** If the library is compiled to omit the full-scale date and time
954** handling (to get a smaller binary), the following minimal version
955** of the functions current_time(), current_date() and current_timestamp()
956** are included instead. This is to support column declarations that
957** include "DEFAULT CURRENT_TIME" etc.
958**
959** This function uses the C-library functions time(), gmtime()
960** and strftime(). The format string to pass to strftime() is supplied
961** as the user-data for the function.
962*/
963static void currentTimeFunc(
964 sqlite3_context *context,
965 int argc,
966 sqlite3_value **argv
967){
968 time_t t;
969 char *zFormat = (char *)sqlite3_user_data(context);
970 char zBuf[20];
971
972 time(&t);
973#ifdef SQLITE_TEST
974 {
975 extern int sqlite3_current_time; /* See os_XXX.c */
976 if( sqlite3_current_time ){
977 t = sqlite3_current_time;
978 }
979 }
980#endif
981
982#ifdef HAVE_GMTIME_R
983 {
984 struct tm sNow;
985 gmtime_r(&t, &sNow);
986 strftime(zBuf, 20, zFormat, &sNow);
987 }
988#else
989 {
990 struct tm *pTm;
991 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
992 pTm = gmtime(&t);
993 strftime(zBuf, 20, zFormat, pTm);
994 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
995 }
996#endif
997
998 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
999}
1000#endif
1001
1002/*
1003** This function registered all of the above C functions as SQL
1004** functions. This should be the only routine in this file with
1005** external linkage.
1006*/
1007void sqlite3RegisterDateTimeFunctions(sqlite3 *db){
1008#ifndef SQLITE_OMIT_DATETIME_FUNCS
1009 static const struct {
1010 char *zName;
1011 int nArg;
1012 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1013 } aFuncs[] = {
1014 { "julianday", -1, juliandayFunc },
1015 { "date", -1, dateFunc },
1016 { "time", -1, timeFunc },
1017 { "datetime", -1, datetimeFunc },
1018 { "strftime", -1, strftimeFunc },
1019 { "current_time", 0, ctimeFunc },
1020 { "current_timestamp", 0, ctimestampFunc },
1021 { "current_date", 0, cdateFunc },
1022 };
1023 int i;
1024
1025 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1026 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
1027 SQLITE_UTF8, (void *)(db->pVfs), aFuncs[i].xFunc, 0, 0);
1028 }
1029#else
1030 static const struct {
1031 char *zName;
1032 char *zFormat;
1033 } aFuncs[] = {
1034 { "current_time", "%H:%M:%S" },
1035 { "current_date", "%Y-%m-%d" },
1036 { "current_timestamp", "%Y-%m-%d %H:%M:%S" }
1037 };
1038 int i;
1039
1040 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1041 sqlite3CreateFunc(db, aFuncs[i].zName, 0, SQLITE_UTF8,
1042 aFuncs[i].zFormat, currentTimeFunc, 0, 0);
1043 }
1044#endif
1045}
diff --git a/libraries/sqlite/win32/delete.c b/libraries/sqlite/win32/delete.c
new file mode 100755
index 0000000..12e61cd
--- /dev/null
+++ b/libraries/sqlite/win32/delete.c
@@ -0,0 +1,467 @@
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.
10**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** in order to generate code for DELETE FROM statements.
14**
15** $Id: delete.c,v 1.130 2007/08/16 04:30:40 drh Exp $
16*/
17#include "sqliteInt.h"
18
19/*
20** Look up every table that is named in pSrc. If any table is not found,
21** add an error message to pParse->zErrMsg and return NULL. If all tables
22** are found, return a pointer to the last table.
23*/
24Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
25 Table *pTab = 0;
26 int i;
27 struct SrcList_item *pItem;
28 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
29 pTab = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
30 sqlite3DeleteTable(pItem->pTab);
31 pItem->pTab = pTab;
32 if( pTab ){
33 pTab->nRef++;
34 }
35 }
36 return pTab;
37}
38
39/*
40** Check to make sure the given table is writable. If it is not
41** writable, generate an error message and return 1. If it is
42** writable return 0;
43*/
44int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
45 if( (pTab->readOnly && (pParse->db->flags & SQLITE_WriteSchema)==0
46 && pParse->nested==0)
47#ifndef SQLITE_OMIT_VIRTUALTABLE
48 || (pTab->pMod && pTab->pMod->pModule->xUpdate==0)
49#endif
50 ){
51 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
52 return 1;
53 }
54#ifndef SQLITE_OMIT_VIEW
55 if( !viewOk && pTab->pSelect ){
56 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
57 return 1;
58 }
59#endif
60 return 0;
61}
62
63/*
64** Generate code that will open a table for reading.
65*/
66void sqlite3OpenTable(
67 Parse *p, /* Generate code into this VDBE */
68 int iCur, /* The cursor number of the table */
69 int iDb, /* The database index in sqlite3.aDb[] */
70 Table *pTab, /* The table to be opened */
71 int opcode /* OP_OpenRead or OP_OpenWrite */
72){
73 Vdbe *v;
74 if( IsVirtual(pTab) ) return;
75 v = sqlite3GetVdbe(p);
76 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
77 sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName);
78 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
79 VdbeComment((v, "# %s", pTab->zName));
80 sqlite3VdbeAddOp(v, opcode, iCur, pTab->tnum);
81 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
82}
83
84
85/*
86** Generate code for a DELETE FROM statement.
87**
88** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
89** \________/ \________________/
90** pTabList pWhere
91*/
92void sqlite3DeleteFrom(
93 Parse *pParse, /* The parser context */
94 SrcList *pTabList, /* The table from which we should delete things */
95 Expr *pWhere /* The WHERE clause. May be null */
96){
97 Vdbe *v; /* The virtual database engine */
98 Table *pTab; /* The table from which records will be deleted */
99 const char *zDb; /* Name of database holding pTab */
100 int end, addr = 0; /* A couple addresses of generated code */
101 int i; /* Loop counter */
102 WhereInfo *pWInfo; /* Information about the WHERE clause */
103 Index *pIdx; /* For looping over indices of the table */
104 int iCur; /* VDBE Cursor number for pTab */
105 sqlite3 *db; /* Main database structure */
106 AuthContext sContext; /* Authorization context */
107 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
108 NameContext sNC; /* Name context to resolve expressions in */
109 int iDb; /* Database number */
110 int memCnt = 0; /* Memory cell used for change counting */
111
112#ifndef SQLITE_OMIT_TRIGGER
113 int isView; /* True if attempting to delete from a view */
114 int triggers_exist = 0; /* True if any triggers exist */
115#endif
116
117 sContext.pParse = 0;
118 db = pParse->db;
119 if( pParse->nErr || db->mallocFailed ){
120 goto delete_from_cleanup;
121 }
122 assert( pTabList->nSrc==1 );
123
124 /* Locate the table which we want to delete. This table has to be
125 ** put in an SrcList structure because some of the subroutines we
126 ** will be calling are designed to work with multiple tables and expect
127 ** an SrcList* parameter instead of just a Table* parameter.
128 */
129 pTab = sqlite3SrcListLookup(pParse, pTabList);
130 if( pTab==0 ) goto delete_from_cleanup;
131
132 /* Figure out if we have any triggers and if the table being
133 ** deleted from is a view
134 */
135#ifndef SQLITE_OMIT_TRIGGER
136 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0);
137 isView = pTab->pSelect!=0;
138#else
139# define triggers_exist 0
140# define isView 0
141#endif
142#ifdef SQLITE_OMIT_VIEW
143# undef isView
144# define isView 0
145#endif
146
147 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
148 goto delete_from_cleanup;
149 }
150 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151 assert( iDb<db->nDb );
152 zDb = db->aDb[iDb].zName;
153 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
154 goto delete_from_cleanup;
155 }
156
157 /* If pTab is really a view, make sure it has been initialized.
158 */
159 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
160 goto delete_from_cleanup;
161 }
162
163 /* Allocate a cursor used to store the old.* data for a trigger.
164 */
165 if( triggers_exist ){
166 oldIdx = pParse->nTab++;
167 }
168
169 /* Resolve the column names in the WHERE clause.
170 */
171 assert( pTabList->nSrc==1 );
172 iCur = pTabList->a[0].iCursor = pParse->nTab++;
173 memset(&sNC, 0, sizeof(sNC));
174 sNC.pParse = pParse;
175 sNC.pSrcList = pTabList;
176 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
177 goto delete_from_cleanup;
178 }
179
180 /* Start the view context
181 */
182 if( isView ){
183 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
184 }
185
186 /* Begin generating code.
187 */
188 v = sqlite3GetVdbe(pParse);
189 if( v==0 ){
190 goto delete_from_cleanup;
191 }
192 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
193 sqlite3BeginWriteOperation(pParse, triggers_exist, iDb);
194
195 /* If we are trying to delete from a view, realize that view into
196 ** a ephemeral table.
197 */
198 if( isView ){
199 Select *pView = sqlite3SelectDup(db, pTab->pSelect);
200 sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
201 sqlite3SelectDelete(pView);
202 }
203
204 /* Initialize the counter of the number of rows deleted, if
205 ** we are counting rows.
206 */
207 if( db->flags & SQLITE_CountRows ){
208 memCnt = pParse->nMem++;
209 sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt);
210 }
211
212 /* Special case: A DELETE without a WHERE clause deletes everything.
213 ** It is easier just to erase the whole table. Note, however, that
214 ** this means that the row change count will be incorrect.
215 */
216 if( pWhere==0 && !triggers_exist && !IsVirtual(pTab) ){
217 if( db->flags & SQLITE_CountRows ){
218 /* If counting rows deleted, just count the total number of
219 ** entries in the table. */
220 int endOfLoop = sqlite3VdbeMakeLabel(v);
221 int addr2;
222 if( !isView ){
223 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
224 }
225 sqlite3VdbeAddOp(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2);
226 addr2 = sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
227 sqlite3VdbeAddOp(v, OP_Next, iCur, addr2);
228 sqlite3VdbeResolveLabel(v, endOfLoop);
229 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
230 }
231 if( !isView ){
232 sqlite3VdbeAddOp(v, OP_Clear, pTab->tnum, iDb);
233 if( !pParse->nested ){
234 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
235 }
236 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
237 assert( pIdx->pSchema==pTab->pSchema );
238 sqlite3VdbeAddOp(v, OP_Clear, pIdx->tnum, iDb);
239 }
240 }
241 }
242 /* The usual case: There is a WHERE clause so we have to scan through
243 ** the table and pick which records to delete.
244 */
245 else{
246 /* Begin the database scan
247 */
248 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
249 if( pWInfo==0 ) goto delete_from_cleanup;
250
251 /* Remember the rowid of every item to be deleted.
252 */
253 sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
254 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
255 if( db->flags & SQLITE_CountRows ){
256 sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
257 }
258
259 /* End the database scan loop.
260 */
261 sqlite3WhereEnd(pWInfo);
262
263 /* Open the pseudo-table used to store OLD if there are triggers.
264 */
265 if( triggers_exist ){
266 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
267 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
268 }
269
270 /* Delete every item whose key was written to the list during the
271 ** database scan. We have to delete items after the scan is complete
272 ** because deleting an item can change the scan order.
273 */
274 end = sqlite3VdbeMakeLabel(v);
275
276 /* This is the beginning of the delete loop when there are
277 ** row triggers.
278 */
279 if( triggers_exist ){
280 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end);
281 if( !isView ){
282 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
283 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
284 }
285 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
286 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
287 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
288 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
289 if( !isView ){
290 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
291 }
292
293 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab,
294 -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
295 addr);
296 }
297
298 if( !isView ){
299 /* Open cursors for the table we are deleting from and all its
300 ** indices. If there are row triggers, this happens inside the
301 ** OP_FifoRead loop because the cursor have to all be closed
302 ** before the trigger fires. If there are no row triggers, the
303 ** cursors are opened only once on the outside the loop.
304 */
305 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
306
307 /* This is the beginning of the delete loop when there are no
308 ** row triggers */
309 if( !triggers_exist ){
310 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end);
311 }
312
313 /* Delete the row */
314#ifndef SQLITE_OMIT_VIRTUALTABLE
315 if( IsVirtual(pTab) ){
316 pParse->pVirtualLock = pTab;
317 sqlite3VdbeOp3(v, OP_VUpdate, 0, 1, (const char*)pTab->pVtab, P3_VTAB);
318 }else
319#endif
320 {
321 sqlite3GenerateRowDelete(db, v, pTab, iCur, pParse->nested==0);
322 }
323 }
324
325 /* If there are row triggers, close all cursors then invoke
326 ** the AFTER triggers
327 */
328 if( triggers_exist ){
329 if( !isView ){
330 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
331 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
332 }
333 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
334 }
335 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1,
336 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
337 addr);
338 }
339
340 /* End of the delete loop */
341 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
342 sqlite3VdbeResolveLabel(v, end);
343
344 /* Close the cursors after the loop if there are no row triggers */
345 if( !triggers_exist && !IsVirtual(pTab) ){
346 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
347 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
348 }
349 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
350 }
351 }
352
353 /*
354 ** Return the number of rows that were deleted. If this routine is
355 ** generating code because of a call to sqlite3NestedParse(), do not
356 ** invoke the callback function.
357 */
358 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
359 sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0);
360 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
361 sqlite3VdbeSetNumCols(v, 1);
362 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P3_STATIC);
363 }
364
365delete_from_cleanup:
366 sqlite3AuthContextPop(&sContext);
367 sqlite3SrcListDelete(pTabList);
368 sqlite3ExprDelete(pWhere);
369 return;
370}
371
372/*
373** This routine generates VDBE code that causes a single row of a
374** single table to be deleted.
375**
376** The VDBE must be in a particular state when this routine is called.
377** These are the requirements:
378**
379** 1. A read/write cursor pointing to pTab, the table containing the row
380** to be deleted, must be opened as cursor number "base".
381**
382** 2. Read/write cursors for all indices of pTab must be open as
383** cursor number base+i for the i-th index.
384**
385** 3. The record number of the row to be deleted must be on the top
386** of the stack.
387**
388** This routine pops the top of the stack to remove the record number
389** and then generates code to remove both the table record and all index
390** entries that point to that record.
391*/
392void sqlite3GenerateRowDelete(
393 sqlite3 *db, /* The database containing the index */
394 Vdbe *v, /* Generate code into this VDBE */
395 Table *pTab, /* Table containing the row to be deleted */
396 int iCur, /* Cursor number for the table */
397 int count /* Increment the row change counter */
398){
399 int addr;
400 addr = sqlite3VdbeAddOp(v, OP_NotExists, iCur, 0);
401 sqlite3GenerateRowIndexDelete(v, pTab, iCur, 0);
402 sqlite3VdbeAddOp(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
403 if( count ){
404 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
405 }
406 sqlite3VdbeJumpHere(v, addr);
407}
408
409/*
410** This routine generates VDBE code that causes the deletion of all
411** index entries associated with a single row of a single table.
412**
413** The VDBE must be in a particular state when this routine is called.
414** These are the requirements:
415**
416** 1. A read/write cursor pointing to pTab, the table containing the row
417** to be deleted, must be opened as cursor number "iCur".
418**
419** 2. Read/write cursors for all indices of pTab must be open as
420** cursor number iCur+i for the i-th index.
421**
422** 3. The "iCur" cursor must be pointing to the row that is to be
423** deleted.
424*/
425void sqlite3GenerateRowIndexDelete(
426 Vdbe *v, /* Generate code into this VDBE */
427 Table *pTab, /* Table containing the row to be deleted */
428 int iCur, /* Cursor number for the table */
429 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
430){
431 int i;
432 Index *pIdx;
433
434 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
435 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
436 sqlite3GenerateIndexKey(v, pIdx, iCur);
437 sqlite3VdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
438 }
439}
440
441/*
442** Generate code that will assemble an index key and put it on the top
443** of the tack. The key with be for index pIdx which is an index on pTab.
444** iCur is the index of a cursor open on the pTab table and pointing to
445** the entry that needs indexing.
446*/
447void sqlite3GenerateIndexKey(
448 Vdbe *v, /* Generate code into this VDBE */
449 Index *pIdx, /* The index for which to generate a key */
450 int iCur /* Cursor number for the pIdx->pTable table */
451){
452 int j;
453 Table *pTab = pIdx->pTable;
454
455 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
456 for(j=0; j<pIdx->nColumn; j++){
457 int idx = pIdx->aiColumn[j];
458 if( idx==pTab->iPKey ){
459 sqlite3VdbeAddOp(v, OP_Dup, j, 0);
460 }else{
461 sqlite3VdbeAddOp(v, OP_Column, iCur, idx);
462 sqlite3ColumnDefault(v, pTab, idx);
463 }
464 }
465 sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
466 sqlite3IndexAffinityStr(v, pIdx);
467}
diff --git a/libraries/sqlite/win32/expr.c b/libraries/sqlite/win32/expr.c
new file mode 100755
index 0000000..0a7091a
--- /dev/null
+++ b/libraries/sqlite/win32/expr.c
@@ -0,0 +1,2617 @@
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.
10**
11*************************************************************************
12** This file contains routines used for analyzing expressions and
13** for generating VDBE code that evaluates expressions in SQLite.
14**
15** $Id: expr.c,v 1.313 2007/09/18 15:55:07 drh Exp $
16*/
17#include "sqliteInt.h"
18#include <ctype.h>
19
20/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
36char sqlite3ExprAffinity(Expr *pExpr){
37 int op = pExpr->op;
38 if( op==TK_SELECT ){
39 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
40 }
41#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
43 return sqlite3AffinityType(&pExpr->token);
44 }
45#endif
46 return pExpr->affinity;
47}
48
49/*
50** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
52** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
55*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
68** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
71CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
73 if( pExpr ){
74 int op;
75 pColl = pExpr->pColl;
76 op = pExpr->op;
77 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
78 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
79 }
80 }
81 if( sqlite3CheckCollSeq(pParse, pColl) ){
82 pColl = 0;
83 }
84 return pColl;
85}
86
87/*
88** pExpr is an operand of a comparison operator. aff2 is the
89** type affinity of the other operand. This routine returns the
90** type affinity that should be used for the comparison operator.
91*/
92char sqlite3CompareAffinity(Expr *pExpr, char aff2){
93 char aff1 = sqlite3ExprAffinity(pExpr);
94 if( aff1 && aff2 ){
95 /* Both sides of the comparison are columns. If one has numeric
96 ** affinity, use that. Otherwise use no affinity.
97 */
98 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
99 return SQLITE_AFF_NUMERIC;
100 }else{
101 return SQLITE_AFF_NONE;
102 }
103 }else if( !aff1 && !aff2 ){
104 /* Neither side of the comparison is a column. Compare the
105 ** results directly.
106 */
107 return SQLITE_AFF_NONE;
108 }else{
109 /* One side is a column, the other is not. Use the columns affinity. */
110 assert( aff1==0 || aff2==0 );
111 return (aff1 + aff2);
112 }
113}
114
115/*
116** pExpr is a comparison operator. Return the type affinity that should
117** be applied to both operands prior to doing the comparison.
118*/
119static char comparisonAffinity(Expr *pExpr){
120 char aff;
121 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
122 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
123 pExpr->op==TK_NE );
124 assert( pExpr->pLeft );
125 aff = sqlite3ExprAffinity(pExpr->pLeft);
126 if( pExpr->pRight ){
127 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
128 }
129 else if( pExpr->pSelect ){
130 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
131 }
132 else if( !aff ){
133 aff = SQLITE_AFF_NONE;
134 }
135 return aff;
136}
137
138/*
139** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
140** idx_affinity is the affinity of an indexed column. Return true
141** if the index with affinity idx_affinity may be used to implement
142** the comparison in pExpr.
143*/
144int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
145 char aff = comparisonAffinity(pExpr);
146 switch( aff ){
147 case SQLITE_AFF_NONE:
148 return 1;
149 case SQLITE_AFF_TEXT:
150 return idx_affinity==SQLITE_AFF_TEXT;
151 default:
152 return sqlite3IsNumericAffinity(idx_affinity);
153 }
154}
155
156/*
157** Return the P1 value that should be used for a binary comparison
158** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
159** If jumpIfNull is true, then set the low byte of the returned
160** P1 value to tell the opcode to jump if either expression
161** evaluates to NULL.
162*/
163static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
164 char aff = sqlite3ExprAffinity(pExpr2);
165 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
166}
167
168/*
169** Return a pointer to the collation sequence that should be used by
170** a binary comparison operator comparing pLeft and pRight.
171**
172** If the left hand expression has a collating sequence type, then it is
173** used. Otherwise the collation sequence for the right hand expression
174** is used, or the default (BINARY) if neither expression has a collating
175** type.
176**
177** Argument pRight (but not pLeft) may be a null pointer. In this case,
178** it is not considered.
179*/
180CollSeq *sqlite3BinaryCompareCollSeq(
181 Parse *pParse,
182 Expr *pLeft,
183 Expr *pRight
184){
185 CollSeq *pColl;
186 assert( pLeft );
187 if( pLeft->flags & EP_ExpCollate ){
188 assert( pLeft->pColl );
189 pColl = pLeft->pColl;
190 }else if( pRight && pRight->flags & EP_ExpCollate ){
191 assert( pRight->pColl );
192 pColl = pRight->pColl;
193 }else{
194 pColl = sqlite3ExprCollSeq(pParse, pLeft);
195 if( !pColl ){
196 pColl = sqlite3ExprCollSeq(pParse, pRight);
197 }
198 }
199 return pColl;
200}
201
202/*
203** Generate code for a comparison operator.
204*/
205static int codeCompare(
206 Parse *pParse, /* The parsing (and code generating) context */
207 Expr *pLeft, /* The left operand */
208 Expr *pRight, /* The right operand */
209 int opcode, /* The comparison opcode */
210 int dest, /* Jump here if true. */
211 int jumpIfNull /* If true, jump if either operand is NULL */
212){
213 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
214 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
215 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
216}
217
218/*
219** Construct a new expression node and return a pointer to it. Memory
220** for this node is obtained from sqlite3_malloc(). The calling function
221** is responsible for making sure the node eventually gets freed.
222*/
223Expr *sqlite3Expr(
224 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
225 int op, /* Expression opcode */
226 Expr *pLeft, /* Left operand */
227 Expr *pRight, /* Right operand */
228 const Token *pToken /* Argument token */
229){
230 Expr *pNew;
231 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
232 if( pNew==0 ){
233 /* When malloc fails, delete pLeft and pRight. Expressions passed to
234 ** this function must always be allocated with sqlite3Expr() for this
235 ** reason.
236 */
237 sqlite3ExprDelete(pLeft);
238 sqlite3ExprDelete(pRight);
239 return 0;
240 }
241 pNew->op = op;
242 pNew->pLeft = pLeft;
243 pNew->pRight = pRight;
244 pNew->iAgg = -1;
245 if( pToken ){
246 assert( pToken->dyn==0 );
247 pNew->span = pNew->token = *pToken;
248 }else if( pLeft ){
249 if( pRight ){
250 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
251 if( pRight->flags & EP_ExpCollate ){
252 pNew->flags |= EP_ExpCollate;
253 pNew->pColl = pRight->pColl;
254 }
255 }
256 if( pLeft->flags & EP_ExpCollate ){
257 pNew->flags |= EP_ExpCollate;
258 pNew->pColl = pLeft->pColl;
259 }
260 }
261
262 sqlite3ExprSetHeight(pNew);
263 return pNew;
264}
265
266/*
267** Works like sqlite3Expr() except that it takes an extra Parse*
268** argument and notifies the associated connection object if malloc fails.
269*/
270Expr *sqlite3PExpr(
271 Parse *pParse, /* Parsing context */
272 int op, /* Expression opcode */
273 Expr *pLeft, /* Left operand */
274 Expr *pRight, /* Right operand */
275 const Token *pToken /* Argument token */
276){
277 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
278}
279
280/*
281** When doing a nested parse, you can include terms in an expression
282** that look like this: #0 #1 #2 ... These terms refer to elements
283** on the stack. "#0" means the top of the stack.
284** "#1" means the next down on the stack. And so forth.
285**
286** This routine is called by the parser to deal with on of those terms.
287** It immediately generates code to store the value in a memory location.
288** The returns an expression that will code to extract the value from
289** that memory location as needed.
290*/
291Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
292 Vdbe *v = pParse->pVdbe;
293 Expr *p;
294 int depth;
295 if( pParse->nested==0 ){
296 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
297 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
298 }
299 if( v==0 ) return 0;
300 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
301 if( p==0 ){
302 return 0; /* Malloc failed */
303 }
304 depth = atoi((char*)&pToken->z[1]);
305 p->iTable = pParse->nMem++;
306 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
307 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
308 return p;
309}
310
311/*
312** Join two expressions using an AND operator. If either expression is
313** NULL, then just return the other expression.
314*/
315Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
316 if( pLeft==0 ){
317 return pRight;
318 }else if( pRight==0 ){
319 return pLeft;
320 }else{
321 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
322 }
323}
324
325/*
326** Set the Expr.span field of the given expression to span all
327** text between the two given tokens.
328*/
329void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
330 assert( pRight!=0 );
331 assert( pLeft!=0 );
332 if( pExpr && pRight->z && pLeft->z ){
333 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
334 if( pLeft->dyn==0 && pRight->dyn==0 ){
335 pExpr->span.z = pLeft->z;
336 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
337 }else{
338 pExpr->span.z = 0;
339 }
340 }
341}
342
343/*
344** Construct a new expression node for a function with multiple
345** arguments.
346*/
347Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
348 Expr *pNew;
349 assert( pToken );
350 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
351 if( pNew==0 ){
352 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
353 return 0;
354 }
355 pNew->op = TK_FUNCTION;
356 pNew->pList = pList;
357 assert( pToken->dyn==0 );
358 pNew->token = *pToken;
359 pNew->span = pNew->token;
360
361 sqlite3ExprSetHeight(pNew);
362 return pNew;
363}
364
365/*
366** Assign a variable number to an expression that encodes a wildcard
367** in the original SQL statement.
368**
369** Wildcards consisting of a single "?" are assigned the next sequential
370** variable number.
371**
372** Wildcards of the form "?nnn" are assigned the number "nnn". We make
373** sure "nnn" is not too be to avoid a denial of service attack when
374** the SQL statement comes from an external source.
375**
376** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
377** as the previous instance of the same wildcard. Or if this is the first
378** instance of the wildcard, the next sequenial variable number is
379** assigned.
380*/
381void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
382 Token *pToken;
383 sqlite3 *db = pParse->db;
384
385 if( pExpr==0 ) return;
386 pToken = &pExpr->token;
387 assert( pToken->n>=1 );
388 assert( pToken->z!=0 );
389 assert( pToken->z[0]!=0 );
390 if( pToken->n==1 ){
391 /* Wildcard of the form "?". Assign the next variable number */
392 pExpr->iTable = ++pParse->nVar;
393 }else if( pToken->z[0]=='?' ){
394 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
395 ** use it as the variable number */
396 int i;
397 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
398 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
399 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
400 SQLITE_MAX_VARIABLE_NUMBER);
401 }
402 if( i>pParse->nVar ){
403 pParse->nVar = i;
404 }
405 }else{
406 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
407 ** number as the prior appearance of the same name, or if the name
408 ** has never appeared before, reuse the same variable number
409 */
410 int i, n;
411 n = pToken->n;
412 for(i=0; i<pParse->nVarExpr; i++){
413 Expr *pE;
414 if( (pE = pParse->apVarExpr[i])!=0
415 && pE->token.n==n
416 && memcmp(pE->token.z, pToken->z, n)==0 ){
417 pExpr->iTable = pE->iTable;
418 break;
419 }
420 }
421 if( i>=pParse->nVarExpr ){
422 pExpr->iTable = ++pParse->nVar;
423 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
424 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
425 pParse->apVarExpr =
426 sqlite3DbReallocOrFree(
427 db,
428 pParse->apVarExpr,
429 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
430 );
431 }
432 if( !db->mallocFailed ){
433 assert( pParse->apVarExpr!=0 );
434 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
435 }
436 }
437 }
438 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
439 sqlite3ErrorMsg(pParse, "too many SQL variables");
440 }
441}
442
443/*
444** Recursively delete an expression tree.
445*/
446void sqlite3ExprDelete(Expr *p){
447 if( p==0 ) return;
448 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
449 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
450 sqlite3ExprDelete(p->pLeft);
451 sqlite3ExprDelete(p->pRight);
452 sqlite3ExprListDelete(p->pList);
453 sqlite3SelectDelete(p->pSelect);
454 sqlite3_free(p);
455}
456
457/*
458** The Expr.token field might be a string literal that is quoted.
459** If so, remove the quotation marks.
460*/
461void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
462 if( ExprHasAnyProperty(p, EP_Dequoted) ){
463 return;
464 }
465 ExprSetProperty(p, EP_Dequoted);
466 if( p->token.dyn==0 ){
467 sqlite3TokenCopy(db, &p->token, &p->token);
468 }
469 sqlite3Dequote((char*)p->token.z);
470}
471
472
473/*
474** The following group of routines make deep copies of expressions,
475** expression lists, ID lists, and select statements. The copies can
476** be deleted (by being passed to their respective ...Delete() routines)
477** without effecting the originals.
478**
479** The expression list, ID, and source lists return by sqlite3ExprListDup(),
480** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
481** by subsequent calls to sqlite*ListAppend() routines.
482**
483** Any tables that the SrcList might point to are not duplicated.
484*/
485Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
486 Expr *pNew;
487 if( p==0 ) return 0;
488 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
489 if( pNew==0 ) return 0;
490 memcpy(pNew, p, sizeof(*pNew));
491 if( p->token.z!=0 ){
492 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
493 pNew->token.dyn = 1;
494 }else{
495 assert( pNew->token.z==0 );
496 }
497 pNew->span.z = 0;
498 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
499 pNew->pRight = sqlite3ExprDup(db, p->pRight);
500 pNew->pList = sqlite3ExprListDup(db, p->pList);
501 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
502 return pNew;
503}
504void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
505 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
506 if( pFrom->z ){
507 pTo->n = pFrom->n;
508 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
509 pTo->dyn = 1;
510 }else{
511 pTo->z = 0;
512 }
513}
514ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
515 ExprList *pNew;
516 struct ExprList_item *pItem, *pOldItem;
517 int i;
518 if( p==0 ) return 0;
519 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
520 if( pNew==0 ) return 0;
521 pNew->iECursor = 0;
522 pNew->nExpr = pNew->nAlloc = p->nExpr;
523 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
524 if( pItem==0 ){
525 sqlite3_free(pNew);
526 return 0;
527 }
528 pOldItem = p->a;
529 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
530 Expr *pNewExpr, *pOldExpr;
531 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
532 if( pOldExpr->span.z!=0 && pNewExpr ){
533 /* Always make a copy of the span for top-level expressions in the
534 ** expression list. The logic in SELECT processing that determines
535 ** the names of columns in the result set needs this information */
536 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
537 }
538 assert( pNewExpr==0 || pNewExpr->span.z!=0
539 || pOldExpr->span.z==0
540 || db->mallocFailed );
541 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
542 pItem->sortOrder = pOldItem->sortOrder;
543 pItem->isAgg = pOldItem->isAgg;
544 pItem->done = 0;
545 }
546 return pNew;
547}
548
549/*
550** If cursors, triggers, views and subqueries are all omitted from
551** the build, then none of the following routines, except for
552** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
553** called with a NULL argument.
554*/
555#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
556 || !defined(SQLITE_OMIT_SUBQUERY)
557SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
558 SrcList *pNew;
559 int i;
560 int nByte;
561 if( p==0 ) return 0;
562 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
563 pNew = sqlite3DbMallocRaw(db, nByte );
564 if( pNew==0 ) return 0;
565 pNew->nSrc = pNew->nAlloc = p->nSrc;
566 for(i=0; i<p->nSrc; i++){
567 struct SrcList_item *pNewItem = &pNew->a[i];
568 struct SrcList_item *pOldItem = &p->a[i];
569 Table *pTab;
570 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
571 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
572 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
573 pNewItem->jointype = pOldItem->jointype;
574 pNewItem->iCursor = pOldItem->iCursor;
575 pNewItem->isPopulated = pOldItem->isPopulated;
576 pTab = pNewItem->pTab = pOldItem->pTab;
577 if( pTab ){
578 pTab->nRef++;
579 }
580 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
581 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
582 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
583 pNewItem->colUsed = pOldItem->colUsed;
584 }
585 return pNew;
586}
587IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
588 IdList *pNew;
589 int i;
590 if( p==0 ) return 0;
591 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
592 if( pNew==0 ) return 0;
593 pNew->nId = pNew->nAlloc = p->nId;
594 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
595 if( pNew->a==0 ){
596 sqlite3_free(pNew);
597 return 0;
598 }
599 for(i=0; i<p->nId; i++){
600 struct IdList_item *pNewItem = &pNew->a[i];
601 struct IdList_item *pOldItem = &p->a[i];
602 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
603 pNewItem->idx = pOldItem->idx;
604 }
605 return pNew;
606}
607Select *sqlite3SelectDup(sqlite3 *db, Select *p){
608 Select *pNew;
609 if( p==0 ) return 0;
610 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
611 if( pNew==0 ) return 0;
612 pNew->isDistinct = p->isDistinct;
613 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
614 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
615 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
616 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
617 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
618 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
619 pNew->op = p->op;
620 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
621 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
622 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
623 pNew->iLimit = -1;
624 pNew->iOffset = -1;
625 pNew->isResolved = p->isResolved;
626 pNew->isAgg = p->isAgg;
627 pNew->usesEphm = 0;
628 pNew->disallowOrderBy = 0;
629 pNew->pRightmost = 0;
630 pNew->addrOpenEphm[0] = -1;
631 pNew->addrOpenEphm[1] = -1;
632 pNew->addrOpenEphm[2] = -1;
633 return pNew;
634}
635#else
636Select *sqlite3SelectDup(sqlite3 *db, Select *p){
637 assert( p==0 );
638 return 0;
639}
640#endif
641
642
643/*
644** Add a new element to the end of an expression list. If pList is
645** initially NULL, then create a new expression list.
646*/
647ExprList *sqlite3ExprListAppend(
648 Parse *pParse, /* Parsing context */
649 ExprList *pList, /* List to which to append. Might be NULL */
650 Expr *pExpr, /* Expression to be appended */
651 Token *pName /* AS keyword for the expression */
652){
653 sqlite3 *db = pParse->db;
654 if( pList==0 ){
655 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
656 if( pList==0 ){
657 goto no_mem;
658 }
659 assert( pList->nAlloc==0 );
660 }
661 if( pList->nAlloc<=pList->nExpr ){
662 struct ExprList_item *a;
663 int n = pList->nAlloc*2 + 4;
664 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
665 if( a==0 ){
666 goto no_mem;
667 }
668 pList->a = a;
669 pList->nAlloc = n;
670 }
671 assert( pList->a!=0 );
672 if( pExpr || pName ){
673 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
674 memset(pItem, 0, sizeof(*pItem));
675 pItem->zName = sqlite3NameFromToken(db, pName);
676 pItem->pExpr = pExpr;
677 }
678 return pList;
679
680no_mem:
681 /* Avoid leaking memory if malloc has failed. */
682 sqlite3ExprDelete(pExpr);
683 sqlite3ExprListDelete(pList);
684 return 0;
685}
686
687/*
688** If the expression list pEList contains more than iLimit elements,
689** leave an error message in pParse.
690*/
691void sqlite3ExprListCheckLength(
692 Parse *pParse,
693 ExprList *pEList,
694 int iLimit,
695 const char *zObject
696){
697 if( pEList && pEList->nExpr>iLimit ){
698 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
699 }
700}
701
702
703#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
704/* The following three functions, heightOfExpr(), heightOfExprList()
705** and heightOfSelect(), are used to determine the maximum height
706** of any expression tree referenced by the structure passed as the
707** first argument.
708**
709** If this maximum height is greater than the current value pointed
710** to by pnHeight, the second parameter, then set *pnHeight to that
711** value.
712*/
713static void heightOfExpr(Expr *p, int *pnHeight){
714 if( p ){
715 if( p->nHeight>*pnHeight ){
716 *pnHeight = p->nHeight;
717 }
718 }
719}
720static void heightOfExprList(ExprList *p, int *pnHeight){
721 if( p ){
722 int i;
723 for(i=0; i<p->nExpr; i++){
724 heightOfExpr(p->a[i].pExpr, pnHeight);
725 }
726 }
727}
728static void heightOfSelect(Select *p, int *pnHeight){
729 if( p ){
730 heightOfExpr(p->pWhere, pnHeight);
731 heightOfExpr(p->pHaving, pnHeight);
732 heightOfExpr(p->pLimit, pnHeight);
733 heightOfExpr(p->pOffset, pnHeight);
734 heightOfExprList(p->pEList, pnHeight);
735 heightOfExprList(p->pGroupBy, pnHeight);
736 heightOfExprList(p->pOrderBy, pnHeight);
737 heightOfSelect(p->pPrior, pnHeight);
738 }
739}
740
741/*
742** Set the Expr.nHeight variable in the structure passed as an
743** argument. An expression with no children, Expr.pList or
744** Expr.pSelect member has a height of 1. Any other expression
745** has a height equal to the maximum height of any other
746** referenced Expr plus one.
747*/
748void sqlite3ExprSetHeight(Expr *p){
749 int nHeight = 0;
750 heightOfExpr(p->pLeft, &nHeight);
751 heightOfExpr(p->pRight, &nHeight);
752 heightOfExprList(p->pList, &nHeight);
753 heightOfSelect(p->pSelect, &nHeight);
754 p->nHeight = nHeight + 1;
755}
756
757/*
758** Return the maximum height of any expression tree referenced
759** by the select statement passed as an argument.
760*/
761int sqlite3SelectExprHeight(Select *p){
762 int nHeight = 0;
763 heightOfSelect(p, &nHeight);
764 return nHeight;
765}
766#endif
767
768/*
769** Delete an entire expression list.
770*/
771void sqlite3ExprListDelete(ExprList *pList){
772 int i;
773 struct ExprList_item *pItem;
774 if( pList==0 ) return;
775 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
776 assert( pList->nExpr<=pList->nAlloc );
777 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
778 sqlite3ExprDelete(pItem->pExpr);
779 sqlite3_free(pItem->zName);
780 }
781 sqlite3_free(pList->a);
782 sqlite3_free(pList);
783}
784
785/*
786** Walk an expression tree. Call xFunc for each node visited.
787**
788** The return value from xFunc determines whether the tree walk continues.
789** 0 means continue walking the tree. 1 means do not walk children
790** of the current node but continue with siblings. 2 means abandon
791** the tree walk completely.
792**
793** The return value from this routine is 1 to abandon the tree walk
794** and 0 to continue.
795**
796** NOTICE: This routine does *not* descend into subqueries.
797*/
798static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
799static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
800 int rc;
801 if( pExpr==0 ) return 0;
802 rc = (*xFunc)(pArg, pExpr);
803 if( rc==0 ){
804 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
805 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
806 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
807 }
808 return rc>1;
809}
810
811/*
812** Call walkExprTree() for every expression in list p.
813*/
814static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
815 int i;
816 struct ExprList_item *pItem;
817 if( !p ) return 0;
818 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
819 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
820 }
821 return 0;
822}
823
824/*
825** Call walkExprTree() for every expression in Select p, not including
826** expressions that are part of sub-selects in any FROM clause or the LIMIT
827** or OFFSET expressions..
828*/
829static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
830 walkExprList(p->pEList, xFunc, pArg);
831 walkExprTree(p->pWhere, xFunc, pArg);
832 walkExprList(p->pGroupBy, xFunc, pArg);
833 walkExprTree(p->pHaving, xFunc, pArg);
834 walkExprList(p->pOrderBy, xFunc, pArg);
835 if( p->pPrior ){
836 walkSelectExpr(p->pPrior, xFunc, pArg);
837 }
838 return 0;
839}
840
841
842/*
843** This routine is designed as an xFunc for walkExprTree().
844**
845** pArg is really a pointer to an integer. If we can tell by looking
846** at pExpr that the expression that contains pExpr is not a constant
847** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
848** If pExpr does does not disqualify the expression from being a constant
849** then do nothing.
850**
851** After walking the whole tree, if no nodes are found that disqualify
852** the expression as constant, then we assume the whole expression
853** is constant. See sqlite3ExprIsConstant() for additional information.
854*/
855static int exprNodeIsConstant(void *pArg, Expr *pExpr){
856 int *pN = (int*)pArg;
857
858 /* If *pArg is 3 then any term of the expression that comes from
859 ** the ON or USING clauses of a join disqualifies the expression
860 ** from being considered constant. */
861 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
862 *pN = 0;
863 return 2;
864 }
865
866 switch( pExpr->op ){
867 /* Consider functions to be constant if all their arguments are constant
868 ** and *pArg==2 */
869 case TK_FUNCTION:
870 if( (*pN)==2 ) return 0;
871 /* Fall through */
872 case TK_ID:
873 case TK_COLUMN:
874 case TK_DOT:
875 case TK_AGG_FUNCTION:
876 case TK_AGG_COLUMN:
877#ifndef SQLITE_OMIT_SUBQUERY
878 case TK_SELECT:
879 case TK_EXISTS:
880#endif
881 *pN = 0;
882 return 2;
883 case TK_IN:
884 if( pExpr->pSelect ){
885 *pN = 0;
886 return 2;
887 }
888 default:
889 return 0;
890 }
891}
892
893/*
894** Walk an expression tree. Return 1 if the expression is constant
895** and 0 if it involves variables or function calls.
896**
897** For the purposes of this function, a double-quoted string (ex: "abc")
898** is considered a variable but a single-quoted string (ex: 'abc') is
899** a constant.
900*/
901int sqlite3ExprIsConstant(Expr *p){
902 int isConst = 1;
903 walkExprTree(p, exprNodeIsConstant, &isConst);
904 return isConst;
905}
906
907/*
908** Walk an expression tree. Return 1 if the expression is constant
909** that does no originate from the ON or USING clauses of a join.
910** Return 0 if it involves variables or function calls or terms from
911** an ON or USING clause.
912*/
913int sqlite3ExprIsConstantNotJoin(Expr *p){
914 int isConst = 3;
915 walkExprTree(p, exprNodeIsConstant, &isConst);
916 return isConst!=0;
917}
918
919/*
920** Walk an expression tree. Return 1 if the expression is constant
921** or a function call with constant arguments. Return and 0 if there
922** are any variables.
923**
924** For the purposes of this function, a double-quoted string (ex: "abc")
925** is considered a variable but a single-quoted string (ex: 'abc') is
926** a constant.
927*/
928int sqlite3ExprIsConstantOrFunction(Expr *p){
929 int isConst = 2;
930 walkExprTree(p, exprNodeIsConstant, &isConst);
931 return isConst!=0;
932}
933
934/*
935** If the expression p codes a constant integer that is small enough
936** to fit in a 32-bit integer, return 1 and put the value of the integer
937** in *pValue. If the expression is not an integer or if it is too big
938** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
939*/
940int sqlite3ExprIsInteger(Expr *p, int *pValue){
941 switch( p->op ){
942 case TK_INTEGER: {
943 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
944 return 1;
945 }
946 break;
947 }
948 case TK_UPLUS: {
949 return sqlite3ExprIsInteger(p->pLeft, pValue);
950 }
951 case TK_UMINUS: {
952 int v;
953 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
954 *pValue = -v;
955 return 1;
956 }
957 break;
958 }
959 default: break;
960 }
961 return 0;
962}
963
964/*
965** Return TRUE if the given string is a row-id column name.
966*/
967int sqlite3IsRowid(const char *z){
968 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
969 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
970 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
971 return 0;
972}
973
974/*
975** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
976** that name in the set of source tables in pSrcList and make the pExpr
977** expression node refer back to that source column. The following changes
978** are made to pExpr:
979**
980** pExpr->iDb Set the index in db->aDb[] of the database holding
981** the table.
982** pExpr->iTable Set to the cursor number for the table obtained
983** from pSrcList.
984** pExpr->iColumn Set to the column number within the table.
985** pExpr->op Set to TK_COLUMN.
986** pExpr->pLeft Any expression this points to is deleted
987** pExpr->pRight Any expression this points to is deleted.
988**
989** The pDbToken is the name of the database (the "X"). This value may be
990** NULL meaning that name is of the form Y.Z or Z. Any available database
991** can be used. The pTableToken is the name of the table (the "Y"). This
992** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
993** means that the form of the name is Z and that columns from any table
994** can be used.
995**
996** If the name cannot be resolved unambiguously, leave an error message
997** in pParse and return non-zero. Return zero on success.
998*/
999static int lookupName(
1000 Parse *pParse, /* The parsing context */
1001 Token *pDbToken, /* Name of the database containing table, or NULL */
1002 Token *pTableToken, /* Name of table containing column, or NULL */
1003 Token *pColumnToken, /* Name of the column. */
1004 NameContext *pNC, /* The name context used to resolve the name */
1005 Expr *pExpr /* Make this EXPR node point to the selected column */
1006){
1007 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1008 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1009 char *zCol = 0; /* Name of the column. The "Z" */
1010 int i, j; /* Loop counters */
1011 int cnt = 0; /* Number of matching column names */
1012 int cntTab = 0; /* Number of matching table names */
1013 sqlite3 *db = pParse->db; /* The database */
1014 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1015 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
1016 NameContext *pTopNC = pNC; /* First namecontext in the list */
1017 Schema *pSchema = 0; /* Schema of the expression */
1018
1019 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
1020 zDb = sqlite3NameFromToken(db, pDbToken);
1021 zTab = sqlite3NameFromToken(db, pTableToken);
1022 zCol = sqlite3NameFromToken(db, pColumnToken);
1023 if( db->mallocFailed ){
1024 goto lookupname_end;
1025 }
1026
1027 pExpr->iTable = -1;
1028 while( pNC && cnt==0 ){
1029 ExprList *pEList;
1030 SrcList *pSrcList = pNC->pSrcList;
1031
1032 if( pSrcList ){
1033 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
1034 Table *pTab;
1035 int iDb;
1036 Column *pCol;
1037
1038 pTab = pItem->pTab;
1039 assert( pTab!=0 );
1040 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1041 assert( pTab->nCol>0 );
1042 if( zTab ){
1043 if( pItem->zAlias ){
1044 char *zTabName = pItem->zAlias;
1045 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1046 }else{
1047 char *zTabName = pTab->zName;
1048 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1049 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
1050 continue;
1051 }
1052 }
1053 }
1054 if( 0==(cntTab++) ){
1055 pExpr->iTable = pItem->iCursor;
1056 pSchema = pTab->pSchema;
1057 pMatch = pItem;
1058 }
1059 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1060 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1061 const char *zColl = pTab->aCol[j].zColl;
1062 IdList *pUsing;
1063 cnt++;
1064 pExpr->iTable = pItem->iCursor;
1065 pMatch = pItem;
1066 pSchema = pTab->pSchema;
1067 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1068 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1069 pExpr->affinity = pTab->aCol[j].affinity;
1070 if( (pExpr->flags & EP_ExpCollate)==0 ){
1071 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1072 }
1073 if( i<pSrcList->nSrc-1 ){
1074 if( pItem[1].jointype & JT_NATURAL ){
1075 /* If this match occurred in the left table of a natural join,
1076 ** then skip the right table to avoid a duplicate match */
1077 pItem++;
1078 i++;
1079 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1080 /* If this match occurs on a column that is in the USING clause
1081 ** of a join, skip the search of the right table of the join
1082 ** to avoid a duplicate match there. */
1083 int k;
1084 for(k=0; k<pUsing->nId; k++){
1085 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1086 pItem++;
1087 i++;
1088 break;
1089 }
1090 }
1091 }
1092 }
1093 break;
1094 }
1095 }
1096 }
1097 }
1098
1099#ifndef SQLITE_OMIT_TRIGGER
1100 /* If we have not already resolved the name, then maybe
1101 ** it is a new.* or old.* trigger argument reference
1102 */
1103 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1104 TriggerStack *pTriggerStack = pParse->trigStack;
1105 Table *pTab = 0;
1106 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1107 pExpr->iTable = pTriggerStack->newIdx;
1108 assert( pTriggerStack->pTab );
1109 pTab = pTriggerStack->pTab;
1110 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1111 pExpr->iTable = pTriggerStack->oldIdx;
1112 assert( pTriggerStack->pTab );
1113 pTab = pTriggerStack->pTab;
1114 }
1115
1116 if( pTab ){
1117 int iCol;
1118 Column *pCol = pTab->aCol;
1119
1120 pSchema = pTab->pSchema;
1121 cntTab++;
1122 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
1123 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1124 const char *zColl = pTab->aCol[iCol].zColl;
1125 cnt++;
1126 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1127 pExpr->affinity = pTab->aCol[iCol].affinity;
1128 if( (pExpr->flags & EP_ExpCollate)==0 ){
1129 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1130 }
1131 pExpr->pTab = pTab;
1132 break;
1133 }
1134 }
1135 }
1136 }
1137#endif /* !defined(SQLITE_OMIT_TRIGGER) */
1138
1139 /*
1140 ** Perhaps the name is a reference to the ROWID
1141 */
1142 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1143 cnt = 1;
1144 pExpr->iColumn = -1;
1145 pExpr->affinity = SQLITE_AFF_INTEGER;
1146 }
1147
1148 /*
1149 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1150 ** might refer to an result-set alias. This happens, for example, when
1151 ** we are resolving names in the WHERE clause of the following command:
1152 **
1153 ** SELECT a+b AS x FROM table WHERE x<10;
1154 **
1155 ** In cases like this, replace pExpr with a copy of the expression that
1156 ** forms the result set entry ("a+b" in the example) and return immediately.
1157 ** Note that the expression in the result set should have already been
1158 ** resolved by the time the WHERE clause is resolved.
1159 */
1160 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
1161 for(j=0; j<pEList->nExpr; j++){
1162 char *zAs = pEList->a[j].zName;
1163 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1164 Expr *pDup, *pOrig;
1165 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1166 assert( pExpr->pList==0 );
1167 assert( pExpr->pSelect==0 );
1168 pOrig = pEList->a[j].pExpr;
1169 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1170 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
1171 sqlite3_free(zCol);
1172 return 2;
1173 }
1174 pDup = sqlite3ExprDup(db, pOrig);
1175 if( pExpr->flags & EP_ExpCollate ){
1176 pDup->pColl = pExpr->pColl;
1177 pDup->flags |= EP_ExpCollate;
1178 }
1179 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1180 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
1181 memcpy(pExpr, pDup, sizeof(*pExpr));
1182 sqlite3_free(pDup);
1183 cnt = 1;
1184 pMatch = 0;
1185 assert( zTab==0 && zDb==0 );
1186 goto lookupname_end_2;
1187 }
1188 }
1189 }
1190
1191 /* Advance to the next name context. The loop will exit when either
1192 ** we have a match (cnt>0) or when we run out of name contexts.
1193 */
1194 if( cnt==0 ){
1195 pNC = pNC->pNext;
1196 }
1197 }
1198
1199 /*
1200 ** If X and Y are NULL (in other words if only the column name Z is
1201 ** supplied) and the value of Z is enclosed in double-quotes, then
1202 ** Z is a string literal if it doesn't match any column names. In that
1203 ** case, we need to return right away and not make any changes to
1204 ** pExpr.
1205 **
1206 ** Because no reference was made to outer contexts, the pNC->nRef
1207 ** fields are not changed in any context.
1208 */
1209 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1210 sqlite3_free(zCol);
1211 return 0;
1212 }
1213
1214 /*
1215 ** cnt==0 means there was not match. cnt>1 means there were two or
1216 ** more matches. Either way, we have an error.
1217 */
1218 if( cnt!=1 ){
1219 char *z = 0;
1220 char *zErr;
1221 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1222 if( zDb ){
1223 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
1224 }else if( zTab ){
1225 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
1226 }else{
1227 z = sqlite3StrDup(zCol);
1228 }
1229 if( z ){
1230 sqlite3ErrorMsg(pParse, zErr, z);
1231 sqlite3_free(z);
1232 pTopNC->nErr++;
1233 }else{
1234 db->mallocFailed = 1;
1235 }
1236 }
1237
1238 /* If a column from a table in pSrcList is referenced, then record
1239 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1240 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1241 ** column number is greater than the number of bits in the bitmask
1242 ** then set the high-order bit of the bitmask.
1243 */
1244 if( pExpr->iColumn>=0 && pMatch!=0 ){
1245 int n = pExpr->iColumn;
1246 if( n>=sizeof(Bitmask)*8 ){
1247 n = sizeof(Bitmask)*8-1;
1248 }
1249 assert( pMatch->iCursor==pExpr->iTable );
1250 pMatch->colUsed |= ((Bitmask)1)<<n;
1251 }
1252
1253lookupname_end:
1254 /* Clean up and return
1255 */
1256 sqlite3_free(zDb);
1257 sqlite3_free(zTab);
1258 sqlite3ExprDelete(pExpr->pLeft);
1259 pExpr->pLeft = 0;
1260 sqlite3ExprDelete(pExpr->pRight);
1261 pExpr->pRight = 0;
1262 pExpr->op = TK_COLUMN;
1263lookupname_end_2:
1264 sqlite3_free(zCol);
1265 if( cnt==1 ){
1266 assert( pNC!=0 );
1267 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
1268 if( pMatch && !pMatch->pSelect ){
1269 pExpr->pTab = pMatch->pTab;
1270 }
1271 /* Increment the nRef value on all name contexts from TopNC up to
1272 ** the point where the name matched. */
1273 for(;;){
1274 assert( pTopNC!=0 );
1275 pTopNC->nRef++;
1276 if( pTopNC==pNC ) break;
1277 pTopNC = pTopNC->pNext;
1278 }
1279 return 0;
1280 } else {
1281 return 1;
1282 }
1283}
1284
1285/*
1286** This routine is designed as an xFunc for walkExprTree().
1287**
1288** Resolve symbolic names into TK_COLUMN operators for the current
1289** node in the expression tree. Return 0 to continue the search down
1290** the tree or 2 to abort the tree walk.
1291**
1292** This routine also does error checking and name resolution for
1293** function names. The operator for aggregate functions is changed
1294** to TK_AGG_FUNCTION.
1295*/
1296static int nameResolverStep(void *pArg, Expr *pExpr){
1297 NameContext *pNC = (NameContext*)pArg;
1298 Parse *pParse;
1299
1300 if( pExpr==0 ) return 1;
1301 assert( pNC!=0 );
1302 pParse = pNC->pParse;
1303
1304 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1305 ExprSetProperty(pExpr, EP_Resolved);
1306#ifndef NDEBUG
1307 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1308 SrcList *pSrcList = pNC->pSrcList;
1309 int i;
1310 for(i=0; i<pNC->pSrcList->nSrc; i++){
1311 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1312 }
1313 }
1314#endif
1315 switch( pExpr->op ){
1316 /* Double-quoted strings (ex: "abc") are used as identifiers if
1317 ** possible. Otherwise they remain as strings. Single-quoted
1318 ** strings (ex: 'abc') are always string literals.
1319 */
1320 case TK_STRING: {
1321 if( pExpr->token.z[0]=='\'' ) break;
1322 /* Fall thru into the TK_ID case if this is a double-quoted string */
1323 }
1324 /* A lone identifier is the name of a column.
1325 */
1326 case TK_ID: {
1327 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1328 return 1;
1329 }
1330
1331 /* A table name and column name: ID.ID
1332 ** Or a database, table and column: ID.ID.ID
1333 */
1334 case TK_DOT: {
1335 Token *pColumn;
1336 Token *pTable;
1337 Token *pDb;
1338 Expr *pRight;
1339
1340 /* if( pSrcList==0 ) break; */
1341 pRight = pExpr->pRight;
1342 if( pRight->op==TK_ID ){
1343 pDb = 0;
1344 pTable = &pExpr->pLeft->token;
1345 pColumn = &pRight->token;
1346 }else{
1347 assert( pRight->op==TK_DOT );
1348 pDb = &pExpr->pLeft->token;
1349 pTable = &pRight->pLeft->token;
1350 pColumn = &pRight->pRight->token;
1351 }
1352 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1353 return 1;
1354 }
1355
1356 /* Resolve function names
1357 */
1358 case TK_CONST_FUNC:
1359 case TK_FUNCTION: {
1360 ExprList *pList = pExpr->pList; /* The argument list */
1361 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1362 int no_such_func = 0; /* True if no such function exists */
1363 int wrong_num_args = 0; /* True if wrong number of arguments */
1364 int is_agg = 0; /* True if is an aggregate function */
1365 int i;
1366 int auth; /* Authorization to use the function */
1367 int nId; /* Number of characters in function name */
1368 const char *zId; /* The function name. */
1369 FuncDef *pDef; /* Information about the function */
1370 int enc = ENC(pParse->db); /* The database encoding */
1371
1372 zId = (char*)pExpr->token.z;
1373 nId = pExpr->token.n;
1374 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1375 if( pDef==0 ){
1376 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1377 if( pDef==0 ){
1378 no_such_func = 1;
1379 }else{
1380 wrong_num_args = 1;
1381 }
1382 }else{
1383 is_agg = pDef->xFunc==0;
1384 }
1385#ifndef SQLITE_OMIT_AUTHORIZATION
1386 if( pDef ){
1387 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1388 if( auth!=SQLITE_OK ){
1389 if( auth==SQLITE_DENY ){
1390 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1391 pDef->zName);
1392 pNC->nErr++;
1393 }
1394 pExpr->op = TK_NULL;
1395 return 1;
1396 }
1397 }
1398#endif
1399 if( is_agg && !pNC->allowAgg ){
1400 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1401 pNC->nErr++;
1402 is_agg = 0;
1403 }else if( no_such_func ){
1404 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1405 pNC->nErr++;
1406 }else if( wrong_num_args ){
1407 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1408 nId, zId);
1409 pNC->nErr++;
1410 }
1411 if( is_agg ){
1412 pExpr->op = TK_AGG_FUNCTION;
1413 pNC->hasAgg = 1;
1414 }
1415 if( is_agg ) pNC->allowAgg = 0;
1416 for(i=0; pNC->nErr==0 && i<n; i++){
1417 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
1418 }
1419 if( is_agg ) pNC->allowAgg = 1;
1420 /* FIX ME: Compute pExpr->affinity based on the expected return
1421 ** type of the function
1422 */
1423 return is_agg;
1424 }
1425#ifndef SQLITE_OMIT_SUBQUERY
1426 case TK_SELECT:
1427 case TK_EXISTS:
1428#endif
1429 case TK_IN: {
1430 if( pExpr->pSelect ){
1431 int nRef = pNC->nRef;
1432#ifndef SQLITE_OMIT_CHECK
1433 if( pNC->isCheck ){
1434 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1435 }
1436#endif
1437 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1438 assert( pNC->nRef>=nRef );
1439 if( nRef!=pNC->nRef ){
1440 ExprSetProperty(pExpr, EP_VarSelect);
1441 }
1442 }
1443 break;
1444 }
1445#ifndef SQLITE_OMIT_CHECK
1446 case TK_VARIABLE: {
1447 if( pNC->isCheck ){
1448 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1449 }
1450 break;
1451 }
1452#endif
1453 }
1454 return 0;
1455}
1456
1457/*
1458** This routine walks an expression tree and resolves references to
1459** table columns. Nodes of the form ID.ID or ID resolve into an
1460** index to the table in the table list and a column offset. The
1461** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1462** value is changed to the index of the referenced table in pTabList
1463** plus the "base" value. The base value will ultimately become the
1464** VDBE cursor number for a cursor that is pointing into the referenced
1465** table. The Expr.iColumn value is changed to the index of the column
1466** of the referenced table. The Expr.iColumn value for the special
1467** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1468** alias for ROWID.
1469**
1470** Also resolve function names and check the functions for proper
1471** usage. Make sure all function names are recognized and all functions
1472** have the correct number of arguments. Leave an error message
1473** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1474**
1475** If the expression contains aggregate functions then set the EP_Agg
1476** property on the expression.
1477*/
1478int sqlite3ExprResolveNames(
1479 NameContext *pNC, /* Namespace to resolve expressions in. */
1480 Expr *pExpr /* The expression to be analyzed. */
1481){
1482 int savedHasAgg;
1483 if( pExpr==0 ) return 0;
1484#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1485 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1486 sqlite3ErrorMsg(pNC->pParse,
1487 "Expression tree is too large (maximum depth %d)",
1488 SQLITE_MAX_EXPR_DEPTH
1489 );
1490 return 1;
1491 }
1492 pNC->pParse->nHeight += pExpr->nHeight;
1493#endif
1494 savedHasAgg = pNC->hasAgg;
1495 pNC->hasAgg = 0;
1496 walkExprTree(pExpr, nameResolverStep, pNC);
1497#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1498 pNC->pParse->nHeight -= pExpr->nHeight;
1499#endif
1500 if( pNC->nErr>0 ){
1501 ExprSetProperty(pExpr, EP_Error);
1502 }
1503 if( pNC->hasAgg ){
1504 ExprSetProperty(pExpr, EP_Agg);
1505 }else if( savedHasAgg ){
1506 pNC->hasAgg = 1;
1507 }
1508 return ExprHasProperty(pExpr, EP_Error);
1509}
1510
1511/*
1512** A pointer instance of this structure is used to pass information
1513** through walkExprTree into codeSubqueryStep().
1514*/
1515typedef struct QueryCoder QueryCoder;
1516struct QueryCoder {
1517 Parse *pParse; /* The parsing context */
1518 NameContext *pNC; /* Namespace of first enclosing query */
1519};
1520
1521
1522/*
1523** Generate code for scalar subqueries used as an expression
1524** and IN operators. Examples:
1525**
1526** (SELECT a FROM b) -- subquery
1527** EXISTS (SELECT a FROM b) -- EXISTS subquery
1528** x IN (4,5,11) -- IN operator with list on right-hand side
1529** x IN (SELECT a FROM b) -- IN operator with subquery on the right
1530**
1531** The pExpr parameter describes the expression that contains the IN
1532** operator or subquery.
1533*/
1534#ifndef SQLITE_OMIT_SUBQUERY
1535void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1536 int testAddr = 0; /* One-time test address */
1537 Vdbe *v = sqlite3GetVdbe(pParse);
1538 if( v==0 ) return;
1539
1540
1541 /* This code must be run in its entirety every time it is encountered
1542 ** if any of the following is true:
1543 **
1544 ** * The right-hand side is a correlated subquery
1545 ** * The right-hand side is an expression list containing variables
1546 ** * We are inside a trigger
1547 **
1548 ** If all of the above are false, then we can run this code just once
1549 ** save the results, and reuse the same result on subsequent invocations.
1550 */
1551 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1552 int mem = pParse->nMem++;
1553 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1554 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
1555 assert( testAddr>0 || pParse->db->mallocFailed );
1556 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
1557 }
1558
1559 switch( pExpr->op ){
1560 case TK_IN: {
1561 char affinity;
1562 KeyInfo keyInfo;
1563 int addr; /* Address of OP_OpenEphemeral instruction */
1564
1565 affinity = sqlite3ExprAffinity(pExpr->pLeft);
1566
1567 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1568 ** expression it is handled the same way. A virtual table is
1569 ** filled with single-field index keys representing the results
1570 ** from the SELECT or the <exprlist>.
1571 **
1572 ** If the 'x' expression is a column value, or the SELECT...
1573 ** statement returns a column value, then the affinity of that
1574 ** column is used to build the index keys. If both 'x' and the
1575 ** SELECT... statement are columns, then numeric affinity is used
1576 ** if either column has NUMERIC or INTEGER affinity. If neither
1577 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1578 ** is used.
1579 */
1580 pExpr->iTable = pParse->nTab++;
1581 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
1582 memset(&keyInfo, 0, sizeof(keyInfo));
1583 keyInfo.nField = 1;
1584 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
1585
1586 if( pExpr->pSelect ){
1587 /* Case 1: expr IN (SELECT ...)
1588 **
1589 ** Generate code to write the results of the select into the temporary
1590 ** table allocated and opened above.
1591 */
1592 int iParm = pExpr->iTable + (((int)affinity)<<16);
1593 ExprList *pEList;
1594 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
1595 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1596 return;
1597 }
1598 pEList = pExpr->pSelect->pEList;
1599 if( pEList && pEList->nExpr>0 ){
1600 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
1601 pEList->a[0].pExpr);
1602 }
1603 }else if( pExpr->pList ){
1604 /* Case 2: expr IN (exprlist)
1605 **
1606 ** For each expression, build an index key from the evaluation and
1607 ** store it in the temporary table. If <expr> is a column, then use
1608 ** that columns affinity when building index keys. If <expr> is not
1609 ** a column, use numeric affinity.
1610 */
1611 int i;
1612 ExprList *pList = pExpr->pList;
1613 struct ExprList_item *pItem;
1614
1615 if( !affinity ){
1616 affinity = SQLITE_AFF_NONE;
1617 }
1618 keyInfo.aColl[0] = pExpr->pLeft->pColl;
1619
1620 /* Loop through each expression in <exprlist>. */
1621 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1622 Expr *pE2 = pItem->pExpr;
1623
1624 /* If the expression is not constant then we will need to
1625 ** disable the test that was generated above that makes sure
1626 ** this code only executes once. Because for a non-constant
1627 ** expression we need to rerun this code each time.
1628 */
1629 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
1630 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
1631 testAddr = 0;
1632 }
1633
1634 /* Evaluate the expression and insert it into the temp table */
1635 sqlite3ExprCode(pParse, pE2);
1636 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
1637 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
1638 }
1639 }
1640 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1641 break;
1642 }
1643
1644 case TK_EXISTS:
1645 case TK_SELECT: {
1646 /* This has to be a scalar SELECT. Generate code to put the
1647 ** value of this select in a memory cell and record the number
1648 ** of the memory cell in iColumn.
1649 */
1650 static const Token one = { (u8*)"1", 0, 1 };
1651 Select *pSel;
1652 int iMem;
1653 int sop;
1654
1655 pExpr->iColumn = iMem = pParse->nMem++;
1656 pSel = pExpr->pSelect;
1657 if( pExpr->op==TK_SELECT ){
1658 sop = SRT_Mem;
1659 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1660 VdbeComment((v, "# Init subquery result"));
1661 }else{
1662 sop = SRT_Exists;
1663 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1664 VdbeComment((v, "# Init EXISTS result"));
1665 }
1666 sqlite3ExprDelete(pSel->pLimit);
1667 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
1668 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1669 return;
1670 }
1671 break;
1672 }
1673 }
1674
1675 if( testAddr ){
1676 sqlite3VdbeJumpHere(v, testAddr);
1677 }
1678
1679 return;
1680}
1681#endif /* SQLITE_OMIT_SUBQUERY */
1682
1683/*
1684** Generate an instruction that will put the integer describe by
1685** text z[0..n-1] on the stack.
1686*/
1687static void codeInteger(Vdbe *v, const char *z, int n){
1688 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1689 if( z ){
1690 int i;
1691 if( sqlite3GetInt32(z, &i) ){
1692 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1693 }else if( sqlite3FitsIn64Bits(z) ){
1694 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1695 }else{
1696 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1697 }
1698 }
1699}
1700
1701
1702/*
1703** Generate code that will extract the iColumn-th column from
1704** table pTab and push that column value on the stack. There
1705** is an open cursor to pTab in iTable. If iColumn<0 then
1706** code is generated that extracts the rowid.
1707*/
1708void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1709 if( iColumn<0 ){
1710 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1711 sqlite3VdbeAddOp(v, op, iTable, 0);
1712 }else if( pTab==0 ){
1713 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1714 }else{
1715 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1716 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1717 sqlite3ColumnDefault(v, pTab, iColumn);
1718#ifndef SQLITE_OMIT_FLOATING_POINT
1719 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1720 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1721 }
1722#endif
1723 }
1724}
1725
1726/*
1727** Generate code into the current Vdbe to evaluate the given
1728** expression and leave the result on the top of stack.
1729**
1730** This code depends on the fact that certain token values (ex: TK_EQ)
1731** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1732** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1733** the make process cause these values to align. Assert()s in the code
1734** below verify that the numbers are aligned correctly.
1735*/
1736void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1737 Vdbe *v = pParse->pVdbe;
1738 int op;
1739 int stackChng = 1; /* Amount of change to stack depth */
1740
1741 if( v==0 ) return;
1742 if( pExpr==0 ){
1743 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1744 return;
1745 }
1746 op = pExpr->op;
1747 switch( op ){
1748 case TK_AGG_COLUMN: {
1749 AggInfo *pAggInfo = pExpr->pAggInfo;
1750 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1751 if( !pAggInfo->directMode ){
1752 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1753 break;
1754 }else if( pAggInfo->useSortingIdx ){
1755 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1756 pCol->iSorterColumn);
1757 break;
1758 }
1759 /* Otherwise, fall thru into the TK_COLUMN case */
1760 }
1761 case TK_COLUMN: {
1762 if( pExpr->iTable<0 ){
1763 /* This only happens when coding check constraints */
1764 assert( pParse->ckOffset>0 );
1765 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1766 }else{
1767 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
1768 }
1769 break;
1770 }
1771 case TK_INTEGER: {
1772 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
1773 break;
1774 }
1775 case TK_FLOAT:
1776 case TK_STRING: {
1777 assert( TK_FLOAT==OP_Real );
1778 assert( TK_STRING==OP_String8 );
1779 sqlite3DequoteExpr(pParse->db, pExpr);
1780 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
1781 break;
1782 }
1783 case TK_NULL: {
1784 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1785 break;
1786 }
1787#ifndef SQLITE_OMIT_BLOB_LITERAL
1788 case TK_BLOB: {
1789 int n;
1790 const char *z;
1791 assert( TK_BLOB==OP_HexBlob );
1792 n = pExpr->token.n - 3;
1793 z = (char*)pExpr->token.z + 2;
1794 assert( n>=0 );
1795 if( n==0 ){
1796 z = "";
1797 }
1798 sqlite3VdbeOp3(v, op, 0, 0, z, n);
1799 break;
1800 }
1801#endif
1802 case TK_VARIABLE: {
1803 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
1804 if( pExpr->token.n>1 ){
1805 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
1806 }
1807 break;
1808 }
1809 case TK_REGISTER: {
1810 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1811 break;
1812 }
1813#ifndef SQLITE_OMIT_CAST
1814 case TK_CAST: {
1815 /* Expressions of the form: CAST(pLeft AS token) */
1816 int aff, to_op;
1817 sqlite3ExprCode(pParse, pExpr->pLeft);
1818 aff = sqlite3AffinityType(&pExpr->token);
1819 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1820 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1821 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1822 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1823 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1824 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1825 sqlite3VdbeAddOp(v, to_op, 0, 0);
1826 stackChng = 0;
1827 break;
1828 }
1829#endif /* SQLITE_OMIT_CAST */
1830 case TK_LT:
1831 case TK_LE:
1832 case TK_GT:
1833 case TK_GE:
1834 case TK_NE:
1835 case TK_EQ: {
1836 assert( TK_LT==OP_Lt );
1837 assert( TK_LE==OP_Le );
1838 assert( TK_GT==OP_Gt );
1839 assert( TK_GE==OP_Ge );
1840 assert( TK_EQ==OP_Eq );
1841 assert( TK_NE==OP_Ne );
1842 sqlite3ExprCode(pParse, pExpr->pLeft);
1843 sqlite3ExprCode(pParse, pExpr->pRight);
1844 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1845 stackChng = -1;
1846 break;
1847 }
1848 case TK_AND:
1849 case TK_OR:
1850 case TK_PLUS:
1851 case TK_STAR:
1852 case TK_MINUS:
1853 case TK_REM:
1854 case TK_BITAND:
1855 case TK_BITOR:
1856 case TK_SLASH:
1857 case TK_LSHIFT:
1858 case TK_RSHIFT:
1859 case TK_CONCAT: {
1860 assert( TK_AND==OP_And );
1861 assert( TK_OR==OP_Or );
1862 assert( TK_PLUS==OP_Add );
1863 assert( TK_MINUS==OP_Subtract );
1864 assert( TK_REM==OP_Remainder );
1865 assert( TK_BITAND==OP_BitAnd );
1866 assert( TK_BITOR==OP_BitOr );
1867 assert( TK_SLASH==OP_Divide );
1868 assert( TK_LSHIFT==OP_ShiftLeft );
1869 assert( TK_RSHIFT==OP_ShiftRight );
1870 assert( TK_CONCAT==OP_Concat );
1871 sqlite3ExprCode(pParse, pExpr->pLeft);
1872 sqlite3ExprCode(pParse, pExpr->pRight);
1873 sqlite3VdbeAddOp(v, op, 0, 0);
1874 stackChng = -1;
1875 break;
1876 }
1877 case TK_UMINUS: {
1878 Expr *pLeft = pExpr->pLeft;
1879 assert( pLeft );
1880 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1881 Token *p = &pLeft->token;
1882 char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
1883 if( pLeft->op==TK_FLOAT ){
1884 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1885 }else{
1886 codeInteger(v, z, p->n+1);
1887 }
1888 sqlite3_free(z);
1889 break;
1890 }
1891 /* Fall through into TK_NOT */
1892 }
1893 case TK_BITNOT:
1894 case TK_NOT: {
1895 assert( TK_BITNOT==OP_BitNot );
1896 assert( TK_NOT==OP_Not );
1897 sqlite3ExprCode(pParse, pExpr->pLeft);
1898 sqlite3VdbeAddOp(v, op, 0, 0);
1899 stackChng = 0;
1900 break;
1901 }
1902 case TK_ISNULL:
1903 case TK_NOTNULL: {
1904 int dest;
1905 assert( TK_ISNULL==OP_IsNull );
1906 assert( TK_NOTNULL==OP_NotNull );
1907 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1908 sqlite3ExprCode(pParse, pExpr->pLeft);
1909 dest = sqlite3VdbeCurrentAddr(v) + 2;
1910 sqlite3VdbeAddOp(v, op, 1, dest);
1911 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1912 stackChng = 0;
1913 break;
1914 }
1915 case TK_AGG_FUNCTION: {
1916 AggInfo *pInfo = pExpr->pAggInfo;
1917 if( pInfo==0 ){
1918 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1919 &pExpr->span);
1920 }else{
1921 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1922 }
1923 break;
1924 }
1925 case TK_CONST_FUNC:
1926 case TK_FUNCTION: {
1927 ExprList *pList = pExpr->pList;
1928 int nExpr = pList ? pList->nExpr : 0;
1929 FuncDef *pDef;
1930 int nId;
1931 const char *zId;
1932 int constMask = 0;
1933 int i;
1934 sqlite3 *db = pParse->db;
1935 u8 enc = ENC(db);
1936 CollSeq *pColl = 0;
1937
1938 zId = (char*)pExpr->token.z;
1939 nId = pExpr->token.n;
1940 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
1941 assert( pDef!=0 );
1942 nExpr = sqlite3ExprCodeExprList(pParse, pList);
1943#ifndef SQLITE_OMIT_VIRTUALTABLE
1944 /* Possibly overload the function if the first argument is
1945 ** a virtual table column.
1946 **
1947 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1948 ** second argument, not the first, as the argument to test to
1949 ** see if it is a column in a virtual table. This is done because
1950 ** the left operand of infix functions (the operand we want to
1951 ** control overloading) ends up as the second argument to the
1952 ** function. The expression "A glob B" is equivalent to
1953 ** "glob(B,A). We want to use the A in "A glob B" to test
1954 ** for function overloading. But we use the B term in "glob(B,A)".
1955 */
1956 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
1957 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
1958 }else if( nExpr>0 ){
1959 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
1960 }
1961#endif
1962 for(i=0; i<nExpr && i<32; i++){
1963 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1964 constMask |= (1<<i);
1965 }
1966 if( pDef->needCollSeq && !pColl ){
1967 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1968 }
1969 }
1970 if( pDef->needCollSeq ){
1971 if( !pColl ) pColl = pParse->db->pDfltColl;
1972 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1973 }
1974 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
1975 stackChng = 1-nExpr;
1976 break;
1977 }
1978#ifndef SQLITE_OMIT_SUBQUERY
1979 case TK_EXISTS:
1980 case TK_SELECT: {
1981 if( pExpr->iColumn==0 ){
1982 sqlite3CodeSubselect(pParse, pExpr);
1983 }
1984 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
1985 VdbeComment((v, "# load subquery result"));
1986 break;
1987 }
1988 case TK_IN: {
1989 int addr;
1990 char affinity;
1991 int ckOffset = pParse->ckOffset;
1992 sqlite3CodeSubselect(pParse, pExpr);
1993
1994 /* Figure out the affinity to use to create a key from the results
1995 ** of the expression. affinityStr stores a static string suitable for
1996 ** P3 of OP_MakeRecord.
1997 */
1998 affinity = comparisonAffinity(pExpr);
1999
2000 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
2001 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
2002
2003 /* Code the <expr> from "<expr> IN (...)". The temporary table
2004 ** pExpr->iTable contains the values that make up the (...) set.
2005 */
2006 sqlite3ExprCode(pParse, pExpr->pLeft);
2007 addr = sqlite3VdbeCurrentAddr(v);
2008 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
2009 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
2010 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
2011 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
2012 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
2013 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2014 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2015
2016 break;
2017 }
2018#endif
2019 case TK_BETWEEN: {
2020 Expr *pLeft = pExpr->pLeft;
2021 struct ExprList_item *pLItem = pExpr->pList->a;
2022 Expr *pRight = pLItem->pExpr;
2023 sqlite3ExprCode(pParse, pLeft);
2024 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2025 sqlite3ExprCode(pParse, pRight);
2026 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
2027 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
2028 pLItem++;
2029 pRight = pLItem->pExpr;
2030 sqlite3ExprCode(pParse, pRight);
2031 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
2032 sqlite3VdbeAddOp(v, OP_And, 0, 0);
2033 break;
2034 }
2035 case TK_UPLUS: {
2036 sqlite3ExprCode(pParse, pExpr->pLeft);
2037 stackChng = 0;
2038 break;
2039 }
2040 case TK_CASE: {
2041 int expr_end_label;
2042 int jumpInst;
2043 int nExpr;
2044 int i;
2045 ExprList *pEList;
2046 struct ExprList_item *aListelem;
2047
2048 assert(pExpr->pList);
2049 assert((pExpr->pList->nExpr % 2) == 0);
2050 assert(pExpr->pList->nExpr > 0);
2051 pEList = pExpr->pList;
2052 aListelem = pEList->a;
2053 nExpr = pEList->nExpr;
2054 expr_end_label = sqlite3VdbeMakeLabel(v);
2055 if( pExpr->pLeft ){
2056 sqlite3ExprCode(pParse, pExpr->pLeft);
2057 }
2058 for(i=0; i<nExpr; i=i+2){
2059 sqlite3ExprCode(pParse, aListelem[i].pExpr);
2060 if( pExpr->pLeft ){
2061 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
2062 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2063 OP_Ne, 0, 1);
2064 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2065 }else{
2066 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
2067 }
2068 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
2069 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
2070 sqlite3VdbeJumpHere(v, jumpInst);
2071 }
2072 if( pExpr->pLeft ){
2073 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2074 }
2075 if( pExpr->pRight ){
2076 sqlite3ExprCode(pParse, pExpr->pRight);
2077 }else{
2078 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
2079 }
2080 sqlite3VdbeResolveLabel(v, expr_end_label);
2081 break;
2082 }
2083#ifndef SQLITE_OMIT_TRIGGER
2084 case TK_RAISE: {
2085 if( !pParse->trigStack ){
2086 sqlite3ErrorMsg(pParse,
2087 "RAISE() may only be used within a trigger-program");
2088 return;
2089 }
2090 if( pExpr->iColumn!=OE_Ignore ){
2091 assert( pExpr->iColumn==OE_Rollback ||
2092 pExpr->iColumn == OE_Abort ||
2093 pExpr->iColumn == OE_Fail );
2094 sqlite3DequoteExpr(pParse->db, pExpr);
2095 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
2096 (char*)pExpr->token.z, pExpr->token.n);
2097 } else {
2098 assert( pExpr->iColumn == OE_Ignore );
2099 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2100 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2101 VdbeComment((v, "# raise(IGNORE)"));
2102 }
2103 stackChng = 0;
2104 break;
2105 }
2106#endif
2107 }
2108
2109 if( pParse->ckOffset ){
2110 pParse->ckOffset += stackChng;
2111 assert( pParse->ckOffset );
2112 }
2113}
2114
2115#ifndef SQLITE_OMIT_TRIGGER
2116/*
2117** Generate code that evalutes the given expression and leaves the result
2118** on the stack. See also sqlite3ExprCode().
2119**
2120** This routine might also cache the result and modify the pExpr tree
2121** so that it will make use of the cached result on subsequent evaluations
2122** rather than evaluate the whole expression again. Trivial expressions are
2123** not cached. If the expression is cached, its result is stored in a
2124** memory location.
2125*/
2126void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2127 Vdbe *v = pParse->pVdbe;
2128 int iMem;
2129 int addr1, addr2;
2130 if( v==0 ) return;
2131 addr1 = sqlite3VdbeCurrentAddr(v);
2132 sqlite3ExprCode(pParse, pExpr);
2133 addr2 = sqlite3VdbeCurrentAddr(v);
2134 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2135 iMem = pExpr->iTable = pParse->nMem++;
2136 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2137 pExpr->op = TK_REGISTER;
2138 }
2139}
2140#endif
2141
2142/*
2143** Generate code that pushes the value of every element of the given
2144** expression list onto the stack.
2145**
2146** Return the number of elements pushed onto the stack.
2147*/
2148int sqlite3ExprCodeExprList(
2149 Parse *pParse, /* Parsing context */
2150 ExprList *pList /* The expression list to be coded */
2151){
2152 struct ExprList_item *pItem;
2153 int i, n;
2154 if( pList==0 ) return 0;
2155 n = pList->nExpr;
2156 for(pItem=pList->a, i=n; i>0; i--, pItem++){
2157 sqlite3ExprCode(pParse, pItem->pExpr);
2158 }
2159 return n;
2160}
2161
2162/*
2163** Generate code for a boolean expression such that a jump is made
2164** to the label "dest" if the expression is true but execution
2165** continues straight thru if the expression is false.
2166**
2167** If the expression evaluates to NULL (neither true nor false), then
2168** take the jump if the jumpIfNull flag is true.
2169**
2170** This code depends on the fact that certain token values (ex: TK_EQ)
2171** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2172** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2173** the make process cause these values to align. Assert()s in the code
2174** below verify that the numbers are aligned correctly.
2175*/
2176void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
2177 Vdbe *v = pParse->pVdbe;
2178 int op = 0;
2179 int ckOffset = pParse->ckOffset;
2180 if( v==0 || pExpr==0 ) return;
2181 op = pExpr->op;
2182 switch( op ){
2183 case TK_AND: {
2184 int d2 = sqlite3VdbeMakeLabel(v);
2185 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2186 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2187 sqlite3VdbeResolveLabel(v, d2);
2188 break;
2189 }
2190 case TK_OR: {
2191 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2192 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2193 break;
2194 }
2195 case TK_NOT: {
2196 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2197 break;
2198 }
2199 case TK_LT:
2200 case TK_LE:
2201 case TK_GT:
2202 case TK_GE:
2203 case TK_NE:
2204 case TK_EQ: {
2205 assert( TK_LT==OP_Lt );
2206 assert( TK_LE==OP_Le );
2207 assert( TK_GT==OP_Gt );
2208 assert( TK_GE==OP_Ge );
2209 assert( TK_EQ==OP_Eq );
2210 assert( TK_NE==OP_Ne );
2211 sqlite3ExprCode(pParse, pExpr->pLeft);
2212 sqlite3ExprCode(pParse, pExpr->pRight);
2213 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
2214 break;
2215 }
2216 case TK_ISNULL:
2217 case TK_NOTNULL: {
2218 assert( TK_ISNULL==OP_IsNull );
2219 assert( TK_NOTNULL==OP_NotNull );
2220 sqlite3ExprCode(pParse, pExpr->pLeft);
2221 sqlite3VdbeAddOp(v, op, 1, dest);
2222 break;
2223 }
2224 case TK_BETWEEN: {
2225 /* The expression "x BETWEEN y AND z" is implemented as:
2226 **
2227 ** 1 IF (x < y) GOTO 3
2228 ** 2 IF (x <= z) GOTO <dest>
2229 ** 3 ...
2230 */
2231 int addr;
2232 Expr *pLeft = pExpr->pLeft;
2233 Expr *pRight = pExpr->pList->a[0].pExpr;
2234 sqlite3ExprCode(pParse, pLeft);
2235 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2236 sqlite3ExprCode(pParse, pRight);
2237 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
2238
2239 pRight = pExpr->pList->a[1].pExpr;
2240 sqlite3ExprCode(pParse, pRight);
2241 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
2242
2243 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
2244 sqlite3VdbeJumpHere(v, addr);
2245 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2246 break;
2247 }
2248 default: {
2249 sqlite3ExprCode(pParse, pExpr);
2250 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
2251 break;
2252 }
2253 }
2254 pParse->ckOffset = ckOffset;
2255}
2256
2257/*
2258** Generate code for a boolean expression such that a jump is made
2259** to the label "dest" if the expression is false but execution
2260** continues straight thru if the expression is true.
2261**
2262** If the expression evaluates to NULL (neither true nor false) then
2263** jump if jumpIfNull is true or fall through if jumpIfNull is false.
2264*/
2265void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
2266 Vdbe *v = pParse->pVdbe;
2267 int op = 0;
2268 int ckOffset = pParse->ckOffset;
2269 if( v==0 || pExpr==0 ) return;
2270
2271 /* The value of pExpr->op and op are related as follows:
2272 **
2273 ** pExpr->op op
2274 ** --------- ----------
2275 ** TK_ISNULL OP_NotNull
2276 ** TK_NOTNULL OP_IsNull
2277 ** TK_NE OP_Eq
2278 ** TK_EQ OP_Ne
2279 ** TK_GT OP_Le
2280 ** TK_LE OP_Gt
2281 ** TK_GE OP_Lt
2282 ** TK_LT OP_Ge
2283 **
2284 ** For other values of pExpr->op, op is undefined and unused.
2285 ** The value of TK_ and OP_ constants are arranged such that we
2286 ** can compute the mapping above using the following expression.
2287 ** Assert()s verify that the computation is correct.
2288 */
2289 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2290
2291 /* Verify correct alignment of TK_ and OP_ constants
2292 */
2293 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2294 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2295 assert( pExpr->op!=TK_NE || op==OP_Eq );
2296 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2297 assert( pExpr->op!=TK_LT || op==OP_Ge );
2298 assert( pExpr->op!=TK_LE || op==OP_Gt );
2299 assert( pExpr->op!=TK_GT || op==OP_Le );
2300 assert( pExpr->op!=TK_GE || op==OP_Lt );
2301
2302 switch( pExpr->op ){
2303 case TK_AND: {
2304 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2305 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2306 break;
2307 }
2308 case TK_OR: {
2309 int d2 = sqlite3VdbeMakeLabel(v);
2310 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2311 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2312 sqlite3VdbeResolveLabel(v, d2);
2313 break;
2314 }
2315 case TK_NOT: {
2316 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2317 break;
2318 }
2319 case TK_LT:
2320 case TK_LE:
2321 case TK_GT:
2322 case TK_GE:
2323 case TK_NE:
2324 case TK_EQ: {
2325 sqlite3ExprCode(pParse, pExpr->pLeft);
2326 sqlite3ExprCode(pParse, pExpr->pRight);
2327 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
2328 break;
2329 }
2330 case TK_ISNULL:
2331 case TK_NOTNULL: {
2332 sqlite3ExprCode(pParse, pExpr->pLeft);
2333 sqlite3VdbeAddOp(v, op, 1, dest);
2334 break;
2335 }
2336 case TK_BETWEEN: {
2337 /* The expression is "x BETWEEN y AND z". It is implemented as:
2338 **
2339 ** 1 IF (x >= y) GOTO 3
2340 ** 2 GOTO <dest>
2341 ** 3 IF (x > z) GOTO <dest>
2342 */
2343 int addr;
2344 Expr *pLeft = pExpr->pLeft;
2345 Expr *pRight = pExpr->pList->a[0].pExpr;
2346 sqlite3ExprCode(pParse, pLeft);
2347 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2348 sqlite3ExprCode(pParse, pRight);
2349 addr = sqlite3VdbeCurrentAddr(v);
2350 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2351
2352 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2353 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
2354 pRight = pExpr->pList->a[1].pExpr;
2355 sqlite3ExprCode(pParse, pRight);
2356 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
2357 break;
2358 }
2359 default: {
2360 sqlite3ExprCode(pParse, pExpr);
2361 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
2362 break;
2363 }
2364 }
2365 pParse->ckOffset = ckOffset;
2366}
2367
2368/*
2369** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2370** if they are identical and return FALSE if they differ in any way.
2371**
2372** Sometimes this routine will return FALSE even if the two expressions
2373** really are equivalent. If we cannot prove that the expressions are
2374** identical, we return FALSE just to be safe. So if this routine
2375** returns false, then you do not really know for certain if the two
2376** expressions are the same. But if you get a TRUE return, then you
2377** can be sure the expressions are the same. In the places where
2378** this routine is used, it does not hurt to get an extra FALSE - that
2379** just might result in some slightly slower code. But returning
2380** an incorrect TRUE could lead to a malfunction.
2381*/
2382int sqlite3ExprCompare(Expr *pA, Expr *pB){
2383 int i;
2384 if( pA==0||pB==0 ){
2385 return pB==pA;
2386 }
2387 if( pA->op!=pB->op ) return 0;
2388 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
2389 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2390 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
2391 if( pA->pList ){
2392 if( pB->pList==0 ) return 0;
2393 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2394 for(i=0; i<pA->pList->nExpr; i++){
2395 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
2396 return 0;
2397 }
2398 }
2399 }else if( pB->pList ){
2400 return 0;
2401 }
2402 if( pA->pSelect || pB->pSelect ) return 0;
2403 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
2404 if( pA->op!=TK_COLUMN && pA->token.z ){
2405 if( pB->token.z==0 ) return 0;
2406 if( pB->token.n!=pA->token.n ) return 0;
2407 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2408 return 0;
2409 }
2410 }
2411 return 1;
2412}
2413
2414
2415/*
2416** Add a new element to the pAggInfo->aCol[] array. Return the index of
2417** the new element. Return a negative number if malloc fails.
2418*/
2419static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
2420 int i;
2421 pInfo->aCol = sqlite3ArrayAllocate(
2422 db,
2423 pInfo->aCol,
2424 sizeof(pInfo->aCol[0]),
2425 3,
2426 &pInfo->nColumn,
2427 &pInfo->nColumnAlloc,
2428 &i
2429 );
2430 return i;
2431}
2432
2433/*
2434** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2435** the new element. Return a negative number if malloc fails.
2436*/
2437static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
2438 int i;
2439 pInfo->aFunc = sqlite3ArrayAllocate(
2440 db,
2441 pInfo->aFunc,
2442 sizeof(pInfo->aFunc[0]),
2443 3,
2444 &pInfo->nFunc,
2445 &pInfo->nFuncAlloc,
2446 &i
2447 );
2448 return i;
2449}
2450
2451/*
2452** This is an xFunc for walkExprTree() used to implement
2453** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2454** for additional information.
2455**
2456** This routine analyzes the aggregate function at pExpr.
2457*/
2458static int analyzeAggregate(void *pArg, Expr *pExpr){
2459 int i;
2460 NameContext *pNC = (NameContext *)pArg;
2461 Parse *pParse = pNC->pParse;
2462 SrcList *pSrcList = pNC->pSrcList;
2463 AggInfo *pAggInfo = pNC->pAggInfo;
2464
2465 switch( pExpr->op ){
2466 case TK_AGG_COLUMN:
2467 case TK_COLUMN: {
2468 /* Check to see if the column is in one of the tables in the FROM
2469 ** clause of the aggregate query */
2470 if( pSrcList ){
2471 struct SrcList_item *pItem = pSrcList->a;
2472 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2473 struct AggInfo_col *pCol;
2474 if( pExpr->iTable==pItem->iCursor ){
2475 /* If we reach this point, it means that pExpr refers to a table
2476 ** that is in the FROM clause of the aggregate query.
2477 **
2478 ** Make an entry for the column in pAggInfo->aCol[] if there
2479 ** is not an entry there already.
2480 */
2481 int k;
2482 pCol = pAggInfo->aCol;
2483 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
2484 if( pCol->iTable==pExpr->iTable &&
2485 pCol->iColumn==pExpr->iColumn ){
2486 break;
2487 }
2488 }
2489 if( (k>=pAggInfo->nColumn)
2490 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2491 ){
2492 pCol = &pAggInfo->aCol[k];
2493 pCol->pTab = pExpr->pTab;
2494 pCol->iTable = pExpr->iTable;
2495 pCol->iColumn = pExpr->iColumn;
2496 pCol->iMem = pParse->nMem++;
2497 pCol->iSorterColumn = -1;
2498 pCol->pExpr = pExpr;
2499 if( pAggInfo->pGroupBy ){
2500 int j, n;
2501 ExprList *pGB = pAggInfo->pGroupBy;
2502 struct ExprList_item *pTerm = pGB->a;
2503 n = pGB->nExpr;
2504 for(j=0; j<n; j++, pTerm++){
2505 Expr *pE = pTerm->pExpr;
2506 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2507 pE->iColumn==pExpr->iColumn ){
2508 pCol->iSorterColumn = j;
2509 break;
2510 }
2511 }
2512 }
2513 if( pCol->iSorterColumn<0 ){
2514 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2515 }
2516 }
2517 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2518 ** because it was there before or because we just created it).
2519 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2520 ** pAggInfo->aCol[] entry.
2521 */
2522 pExpr->pAggInfo = pAggInfo;
2523 pExpr->op = TK_AGG_COLUMN;
2524 pExpr->iAgg = k;
2525 break;
2526 } /* endif pExpr->iTable==pItem->iCursor */
2527 } /* end loop over pSrcList */
2528 }
2529 return 1;
2530 }
2531 case TK_AGG_FUNCTION: {
2532 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2533 ** to be ignored */
2534 if( pNC->nDepth==0 ){
2535 /* Check to see if pExpr is a duplicate of another aggregate
2536 ** function that is already in the pAggInfo structure
2537 */
2538 struct AggInfo_func *pItem = pAggInfo->aFunc;
2539 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2540 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
2541 break;
2542 }
2543 }
2544 if( i>=pAggInfo->nFunc ){
2545 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2546 */
2547 u8 enc = ENC(pParse->db);
2548 i = addAggInfoFunc(pParse->db, pAggInfo);
2549 if( i>=0 ){
2550 pItem = &pAggInfo->aFunc[i];
2551 pItem->pExpr = pExpr;
2552 pItem->iMem = pParse->nMem++;
2553 pItem->pFunc = sqlite3FindFunction(pParse->db,
2554 (char*)pExpr->token.z, pExpr->token.n,
2555 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2556 if( pExpr->flags & EP_Distinct ){
2557 pItem->iDistinct = pParse->nTab++;
2558 }else{
2559 pItem->iDistinct = -1;
2560 }
2561 }
2562 }
2563 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2564 */
2565 pExpr->iAgg = i;
2566 pExpr->pAggInfo = pAggInfo;
2567 return 1;
2568 }
2569 }
2570 }
2571
2572 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2573 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2574 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2575 */
2576 if( pExpr->pSelect ){
2577 pNC->nDepth++;
2578 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2579 pNC->nDepth--;
2580 }
2581 return 0;
2582}
2583
2584/*
2585** Analyze the given expression looking for aggregate functions and
2586** for variables that need to be added to the pParse->aAgg[] array.
2587** Make additional entries to the pParse->aAgg[] array as necessary.
2588**
2589** This routine should only be called after the expression has been
2590** analyzed by sqlite3ExprResolveNames().
2591**
2592** If errors are seen, leave an error message in zErrMsg and return
2593** the number of errors.
2594*/
2595int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2596 int nErr = pNC->pParse->nErr;
2597 walkExprTree(pExpr, analyzeAggregate, pNC);
2598 return pNC->pParse->nErr - nErr;
2599}
2600
2601/*
2602** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2603** expression list. Return the number of errors.
2604**
2605** If an error is found, the analysis is cut short.
2606*/
2607int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2608 struct ExprList_item *pItem;
2609 int i;
2610 int nErr = 0;
2611 if( pList ){
2612 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2613 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2614 }
2615 }
2616 return nErr;
2617}
diff --git a/libraries/sqlite/win32/fts1.c b/libraries/sqlite/win32/fts1.c
new file mode 100755
index 0000000..5a69965
--- /dev/null
+++ b/libraries/sqlite/win32/fts1.c
@@ -0,0 +1,3344 @@
1/* fts1 has a design flaw which can lead to database corruption (see
2** below). It is recommended not to use it any longer, instead use
3** fts3 (or higher). If you believe that your use of fts1 is safe,
4** add -DSQLITE_ENABLE_BROKEN_FTS1=1 to your CFLAGS.
5*/
6#ifndef SQLITE_ENABLE_BROKEN_FTS1
7#error fts1 has a design flaw and has been deprecated.
8#endif
9/* The flaw is that fts1 uses the content table's unaliased rowid as
10** the unique docid. fts1 embeds the rowid in the index it builds,
11** and expects the rowid to not change. The SQLite VACUUM operation
12** will renumber such rowids, thereby breaking fts1. If you are using
13** fts1 in a system which has disabled VACUUM, then you can continue
14** to use it safely. Note that PRAGMA auto_vacuum does NOT disable
15** VACUUM, though systems using auto_vacuum are unlikely to invoke
16** VACUUM.
17**
18** fts1 should be safe even across VACUUM if you only insert documents
19** and never delete.
20*/
21
22/* The author disclaims copyright to this source code.
23 *
24 * This is an SQLite module implementing full-text search.
25 */
26
27/*
28** The code in this file is only compiled if:
29**
30** * The FTS1 module is being built as an extension
31** (in which case SQLITE_CORE is not defined), or
32**
33** * The FTS1 module is being built into the core of
34** SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
35*/
36#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
37
38#if defined(SQLITE_ENABLE_FTS1) && !defined(SQLITE_CORE)
39# define SQLITE_CORE 1
40#endif
41
42#include <assert.h>
43#include <stdlib.h>
44#include <stdio.h>
45#include <string.h>
46#include <ctype.h>
47
48#include "fts1.h"
49#include "fts1_hash.h"
50#include "fts1_tokenizer.h"
51#include "sqlite3.h"
52#include "sqlite3ext.h"
53SQLITE_EXTENSION_INIT1
54
55
56#if 0
57# define TRACE(A) printf A; fflush(stdout)
58#else
59# define TRACE(A)
60#endif
61
62/* utility functions */
63
64typedef struct StringBuffer {
65 int len; /* length, not including null terminator */
66 int alloced; /* Space allocated for s[] */
67 char *s; /* Content of the string */
68} StringBuffer;
69
70static void initStringBuffer(StringBuffer *sb){
71 sb->len = 0;
72 sb->alloced = 100;
73 sb->s = malloc(100);
74 sb->s[0] = '\0';
75}
76
77static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){
78 if( sb->len + nFrom >= sb->alloced ){
79 sb->alloced = sb->len + nFrom + 100;
80 sb->s = realloc(sb->s, sb->alloced+1);
81 if( sb->s==0 ){
82 initStringBuffer(sb);
83 return;
84 }
85 }
86 memcpy(sb->s + sb->len, zFrom, nFrom);
87 sb->len += nFrom;
88 sb->s[sb->len] = 0;
89}
90static void append(StringBuffer *sb, const char *zFrom){
91 nappend(sb, zFrom, strlen(zFrom));
92}
93
94/* We encode variable-length integers in little-endian order using seven bits
95 * per byte as follows:
96**
97** KEY:
98** A = 0xxxxxxx 7 bits of data and one flag bit
99** B = 1xxxxxxx 7 bits of data and one flag bit
100**
101** 7 bits - A
102** 14 bits - BA
103** 21 bits - BBA
104** and so on.
105*/
106
107/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */
108#define VARINT_MAX 10
109
110/* Write a 64-bit variable-length integer to memory starting at p[0].
111 * The length of data written will be between 1 and VARINT_MAX bytes.
112 * The number of bytes written is returned. */
113static int putVarint(char *p, sqlite_int64 v){
114 unsigned char *q = (unsigned char *) p;
115 sqlite_uint64 vu = v;
116 do{
117 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
118 vu >>= 7;
119 }while( vu!=0 );
120 q[-1] &= 0x7f; /* turn off high bit in final byte */
121 assert( q - (unsigned char *)p <= VARINT_MAX );
122 return (int) (q - (unsigned char *)p);
123}
124
125/* Read a 64-bit variable-length integer from memory starting at p[0].
126 * Return the number of bytes read, or 0 on error.
127 * The value is stored in *v. */
128static int getVarint(const char *p, sqlite_int64 *v){
129 const unsigned char *q = (const unsigned char *) p;
130 sqlite_uint64 x = 0, y = 1;
131 while( (*q & 0x80) == 0x80 ){
132 x += y * (*q++ & 0x7f);
133 y <<= 7;
134 if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */
135 assert( 0 );
136 return 0;
137 }
138 }
139 x += y * (*q++);
140 *v = (sqlite_int64) x;
141 return (int) (q - (unsigned char *)p);
142}
143
144static int getVarint32(const char *p, int *pi){
145 sqlite_int64 i;
146 int ret = getVarint(p, &i);
147 *pi = (int) i;
148 assert( *pi==i );
149 return ret;
150}
151
152/*** Document lists ***
153 *
154 * A document list holds a sorted list of varint-encoded document IDs.
155 *
156 * A doclist with type DL_POSITIONS_OFFSETS is stored like this:
157 *
158 * array {
159 * varint docid;
160 * array {
161 * varint position; (delta from previous position plus POS_BASE)
162 * varint startOffset; (delta from previous startOffset)
163 * varint endOffset; (delta from startOffset)
164 * }
165 * }
166 *
167 * Here, array { X } means zero or more occurrences of X, adjacent in memory.
168 *
169 * A position list may hold positions for text in multiple columns. A position
170 * POS_COLUMN is followed by a varint containing the index of the column for
171 * following positions in the list. Any positions appearing before any
172 * occurrences of POS_COLUMN are for column 0.
173 *
174 * A doclist with type DL_POSITIONS is like the above, but holds only docids
175 * and positions without offset information.
176 *
177 * A doclist with type DL_DOCIDS is like the above, but holds only docids
178 * without positions or offset information.
179 *
180 * On disk, every document list has positions and offsets, so we don't bother
181 * to serialize a doclist's type.
182 *
183 * We don't yet delta-encode document IDs; doing so will probably be a
184 * modest win.
185 *
186 * NOTE(shess) I've thought of a slightly (1%) better offset encoding.
187 * After the first offset, estimate the next offset by using the
188 * current token position and the previous token position and offset,
189 * offset to handle some variance. So the estimate would be
190 * (iPosition*w->iStartOffset/w->iPosition-64), which is delta-encoded
191 * as normal. Offsets more than 64 chars from the estimate are
192 * encoded as the delta to the previous start offset + 128. An
193 * additional tiny increment can be gained by using the end offset of
194 * the previous token to make the estimate a tiny bit more precise.
195*/
196
197/* It is not safe to call isspace(), tolower(), or isalnum() on
198** hi-bit-set characters. This is the same solution used in the
199** tokenizer.
200*/
201/* TODO(shess) The snippet-generation code should be using the
202** tokenizer-generated tokens rather than doing its own local
203** tokenization.
204*/
205/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */
206static int safe_isspace(char c){
207 return (c&0x80)==0 ? isspace(c) : 0;
208}
209static int safe_tolower(char c){
210 return (c&0x80)==0 ? tolower(c) : c;
211}
212static int safe_isalnum(char c){
213 return (c&0x80)==0 ? isalnum(c) : 0;
214}
215
216typedef enum DocListType {
217 DL_DOCIDS, /* docids only */
218 DL_POSITIONS, /* docids + positions */
219 DL_POSITIONS_OFFSETS /* docids + positions + offsets */
220} DocListType;
221
222/*
223** By default, only positions and not offsets are stored in the doclists.
224** To change this so that offsets are stored too, compile with
225**
226** -DDL_DEFAULT=DL_POSITIONS_OFFSETS
227**
228*/
229#ifndef DL_DEFAULT
230# define DL_DEFAULT DL_POSITIONS
231#endif
232
233typedef struct DocList {
234 char *pData;
235 int nData;
236 DocListType iType;
237 int iLastColumn; /* the last column written */
238 int iLastPos; /* the last position written */
239 int iLastOffset; /* the last start offset written */
240} DocList;
241
242enum {
243 POS_END = 0, /* end of this position list */
244 POS_COLUMN, /* followed by new column number */
245 POS_BASE
246};
247
248/* Initialize a new DocList to hold the given data. */
249static void docListInit(DocList *d, DocListType iType,
250 const char *pData, int nData){
251 d->nData = nData;
252 if( nData>0 ){
253 d->pData = malloc(nData);
254 memcpy(d->pData, pData, nData);
255 } else {
256 d->pData = NULL;
257 }
258 d->iType = iType;
259 d->iLastColumn = 0;
260 d->iLastPos = d->iLastOffset = 0;
261}
262
263/* Create a new dynamically-allocated DocList. */
264static DocList *docListNew(DocListType iType){
265 DocList *d = (DocList *) malloc(sizeof(DocList));
266 docListInit(d, iType, 0, 0);
267 return d;
268}
269
270static void docListDestroy(DocList *d){
271 free(d->pData);
272#ifndef NDEBUG
273 memset(d, 0x55, sizeof(*d));
274#endif
275}
276
277static void docListDelete(DocList *d){
278 docListDestroy(d);
279 free(d);
280}
281
282static char *docListEnd(DocList *d){
283 return d->pData + d->nData;
284}
285
286/* Append a varint to a DocList's data. */
287static void appendVarint(DocList *d, sqlite_int64 i){
288 char c[VARINT_MAX];
289 int n = putVarint(c, i);
290 d->pData = realloc(d->pData, d->nData + n);
291 memcpy(d->pData + d->nData, c, n);
292 d->nData += n;
293}
294
295static void docListAddDocid(DocList *d, sqlite_int64 iDocid){
296 appendVarint(d, iDocid);
297 if( d->iType>=DL_POSITIONS ){
298 appendVarint(d, POS_END); /* initially empty position list */
299 d->iLastColumn = 0;
300 d->iLastPos = d->iLastOffset = 0;
301 }
302}
303
304/* helper function for docListAddPos and docListAddPosOffset */
305static void addPos(DocList *d, int iColumn, int iPos){
306 assert( d->nData>0 );
307 --d->nData; /* remove previous terminator */
308 if( iColumn!=d->iLastColumn ){
309 assert( iColumn>d->iLastColumn );
310 appendVarint(d, POS_COLUMN);
311 appendVarint(d, iColumn);
312 d->iLastColumn = iColumn;
313 d->iLastPos = d->iLastOffset = 0;
314 }
315 assert( iPos>=d->iLastPos );
316 appendVarint(d, iPos-d->iLastPos+POS_BASE);
317 d->iLastPos = iPos;
318}
319
320/* Add a position to the last position list in a doclist. */
321static void docListAddPos(DocList *d, int iColumn, int iPos){
322 assert( d->iType==DL_POSITIONS );
323 addPos(d, iColumn, iPos);
324 appendVarint(d, POS_END); /* add new terminator */
325}
326
327/*
328** Add a position and starting and ending offsets to a doclist.
329**
330** If the doclist is setup to handle only positions, then insert
331** the position only and ignore the offsets.
332*/
333static void docListAddPosOffset(
334 DocList *d, /* Doclist under construction */
335 int iColumn, /* Column the inserted term is part of */
336 int iPos, /* Position of the inserted term */
337 int iStartOffset, /* Starting offset of inserted term */
338 int iEndOffset /* Ending offset of inserted term */
339){
340 assert( d->iType>=DL_POSITIONS );
341 addPos(d, iColumn, iPos);
342 if( d->iType==DL_POSITIONS_OFFSETS ){
343 assert( iStartOffset>=d->iLastOffset );
344 appendVarint(d, iStartOffset-d->iLastOffset);
345 d->iLastOffset = iStartOffset;
346 assert( iEndOffset>=iStartOffset );
347 appendVarint(d, iEndOffset-iStartOffset);
348 }
349 appendVarint(d, POS_END); /* add new terminator */
350}
351
352/*
353** A DocListReader object is a cursor into a doclist. Initialize
354** the cursor to the beginning of the doclist by calling readerInit().
355** Then use routines
356**
357** peekDocid()
358** readDocid()
359** readPosition()
360** skipPositionList()
361** and so forth...
362**
363** to read information out of the doclist. When we reach the end
364** of the doclist, atEnd() returns TRUE.
365*/
366typedef struct DocListReader {
367 DocList *pDoclist; /* The document list we are stepping through */
368 char *p; /* Pointer to next unread byte in the doclist */
369 int iLastColumn;
370 int iLastPos; /* the last position read, or -1 when not in a position list */
371} DocListReader;
372
373/*
374** Initialize the DocListReader r to point to the beginning of pDoclist.
375*/
376static void readerInit(DocListReader *r, DocList *pDoclist){
377 r->pDoclist = pDoclist;
378 if( pDoclist!=NULL ){
379 r->p = pDoclist->pData;
380 }
381 r->iLastColumn = -1;
382 r->iLastPos = -1;
383}
384
385/*
386** Return TRUE if we have reached then end of pReader and there is
387** nothing else left to read.
388*/
389static int atEnd(DocListReader *pReader){
390 return pReader->pDoclist==0 || (pReader->p >= docListEnd(pReader->pDoclist));
391}
392
393/* Peek at the next docid without advancing the read pointer.
394*/
395static sqlite_int64 peekDocid(DocListReader *pReader){
396 sqlite_int64 ret;
397 assert( !atEnd(pReader) );
398 assert( pReader->iLastPos==-1 );
399 getVarint(pReader->p, &ret);
400 return ret;
401}
402
403/* Read the next docid. See also nextDocid().
404*/
405static sqlite_int64 readDocid(DocListReader *pReader){
406 sqlite_int64 ret;
407 assert( !atEnd(pReader) );
408 assert( pReader->iLastPos==-1 );
409 pReader->p += getVarint(pReader->p, &ret);
410 if( pReader->pDoclist->iType>=DL_POSITIONS ){
411 pReader->iLastColumn = 0;
412 pReader->iLastPos = 0;
413 }
414 return ret;
415}
416
417/* Read the next position and column index from a position list.
418 * Returns the position, or -1 at the end of the list. */
419static int readPosition(DocListReader *pReader, int *iColumn){
420 int i;
421 int iType = pReader->pDoclist->iType;
422
423 if( pReader->iLastPos==-1 ){
424 return -1;
425 }
426 assert( !atEnd(pReader) );
427
428 if( iType<DL_POSITIONS ){
429 return -1;
430 }
431 pReader->p += getVarint32(pReader->p, &i);
432 if( i==POS_END ){
433 pReader->iLastColumn = pReader->iLastPos = -1;
434 *iColumn = -1;
435 return -1;
436 }
437 if( i==POS_COLUMN ){
438 pReader->p += getVarint32(pReader->p, &pReader->iLastColumn);
439 pReader->iLastPos = 0;
440 pReader->p += getVarint32(pReader->p, &i);
441 assert( i>=POS_BASE );
442 }
443 pReader->iLastPos += ((int) i)-POS_BASE;
444 if( iType>=DL_POSITIONS_OFFSETS ){
445 /* Skip over offsets, ignoring them for now. */
446 int iStart, iEnd;
447 pReader->p += getVarint32(pReader->p, &iStart);
448 pReader->p += getVarint32(pReader->p, &iEnd);
449 }
450 *iColumn = pReader->iLastColumn;
451 return pReader->iLastPos;
452}
453
454/* Skip past the end of a position list. */
455static void skipPositionList(DocListReader *pReader){
456 DocList *p = pReader->pDoclist;
457 if( p && p->iType>=DL_POSITIONS ){
458 int iColumn;
459 while( readPosition(pReader, &iColumn)!=-1 ){}
460 }
461}
462
463/* Skip over a docid, including its position list if the doclist has
464 * positions. */
465static void skipDocument(DocListReader *pReader){
466 readDocid(pReader);
467 skipPositionList(pReader);
468}
469
470/* Skip past all docids which are less than [iDocid]. Returns 1 if a docid
471 * matching [iDocid] was found. */
472static int skipToDocid(DocListReader *pReader, sqlite_int64 iDocid){
473 sqlite_int64 d = 0;
474 while( !atEnd(pReader) && (d=peekDocid(pReader))<iDocid ){
475 skipDocument(pReader);
476 }
477 return !atEnd(pReader) && d==iDocid;
478}
479
480/* Return the first document in a document list.
481*/
482static sqlite_int64 firstDocid(DocList *d){
483 DocListReader r;
484 readerInit(&r, d);
485 return readDocid(&r);
486}
487
488#ifdef SQLITE_DEBUG
489/*
490** This routine is used for debugging purpose only.
491**
492** Write the content of a doclist to standard output.
493*/
494static void printDoclist(DocList *p){
495 DocListReader r;
496 const char *zSep = "";
497
498 readerInit(&r, p);
499 while( !atEnd(&r) ){
500 sqlite_int64 docid = readDocid(&r);
501 if( docid==0 ){
502 skipPositionList(&r);
503 continue;
504 }
505 printf("%s%lld", zSep, docid);
506 zSep = ",";
507 if( p->iType>=DL_POSITIONS ){
508 int iPos, iCol;
509 const char *zDiv = "";
510 printf("(");
511 while( (iPos = readPosition(&r, &iCol))>=0 ){
512 printf("%s%d:%d", zDiv, iCol, iPos);
513 zDiv = ":";
514 }
515 printf(")");
516 }
517 }
518 printf("\n");
519 fflush(stdout);
520}
521#endif /* SQLITE_DEBUG */
522
523/* Trim the given doclist to contain only positions in column
524 * [iRestrictColumn]. */
525static void docListRestrictColumn(DocList *in, int iRestrictColumn){
526 DocListReader r;
527 DocList out;
528
529 assert( in->iType>=DL_POSITIONS );
530 readerInit(&r, in);
531 docListInit(&out, DL_POSITIONS, NULL, 0);
532
533 while( !atEnd(&r) ){
534 sqlite_int64 iDocid = readDocid(&r);
535 int iPos, iColumn;
536
537 docListAddDocid(&out, iDocid);
538 while( (iPos = readPosition(&r, &iColumn)) != -1 ){
539 if( iColumn==iRestrictColumn ){
540 docListAddPos(&out, iColumn, iPos);
541 }
542 }
543 }
544
545 docListDestroy(in);
546 *in = out;
547}
548
549/* Trim the given doclist by discarding any docids without any remaining
550 * positions. */
551static void docListDiscardEmpty(DocList *in) {
552 DocListReader r;
553 DocList out;
554
555 /* TODO: It would be nice to implement this operation in place; that
556 * could save a significant amount of memory in queries with long doclists. */
557 assert( in->iType>=DL_POSITIONS );
558 readerInit(&r, in);
559 docListInit(&out, DL_POSITIONS, NULL, 0);
560
561 while( !atEnd(&r) ){
562 sqlite_int64 iDocid = readDocid(&r);
563 int match = 0;
564 int iPos, iColumn;
565 while( (iPos = readPosition(&r, &iColumn)) != -1 ){
566 if( !match ){
567 docListAddDocid(&out, iDocid);
568 match = 1;
569 }
570 docListAddPos(&out, iColumn, iPos);
571 }
572 }
573
574 docListDestroy(in);
575 *in = out;
576}
577
578/* Helper function for docListUpdate() and docListAccumulate().
579** Splices a doclist element into the doclist represented by r,
580** leaving r pointing after the newly spliced element.
581*/
582static void docListSpliceElement(DocListReader *r, sqlite_int64 iDocid,
583 const char *pSource, int nSource){
584 DocList *d = r->pDoclist;
585 char *pTarget;
586 int nTarget, found;
587
588 found = skipToDocid(r, iDocid);
589
590 /* Describe slice in d to place pSource/nSource. */
591 pTarget = r->p;
592 if( found ){
593 skipDocument(r);
594 nTarget = r->p-pTarget;
595 }else{
596 nTarget = 0;
597 }
598
599 /* The sense of the following is that there are three possibilities.
600 ** If nTarget==nSource, we should not move any memory nor realloc.
601 ** If nTarget>nSource, trim target and realloc.
602 ** If nTarget<nSource, realloc then expand target.
603 */
604 if( nTarget>nSource ){
605 memmove(pTarget+nSource, pTarget+nTarget, docListEnd(d)-(pTarget+nTarget));
606 }
607 if( nTarget!=nSource ){
608 int iDoclist = pTarget-d->pData;
609 d->pData = realloc(d->pData, d->nData+nSource-nTarget);
610 pTarget = d->pData+iDoclist;
611 }
612 if( nTarget<nSource ){
613 memmove(pTarget+nSource, pTarget+nTarget, docListEnd(d)-(pTarget+nTarget));
614 }
615
616 memcpy(pTarget, pSource, nSource);
617 d->nData += nSource-nTarget;
618 r->p = pTarget+nSource;
619}
620
621/* Insert/update pUpdate into the doclist. */
622static void docListUpdate(DocList *d, DocList *pUpdate){
623 DocListReader reader;
624
625 assert( d!=NULL && pUpdate!=NULL );
626 assert( d->iType==pUpdate->iType);
627
628 readerInit(&reader, d);
629 docListSpliceElement(&reader, firstDocid(pUpdate),
630 pUpdate->pData, pUpdate->nData);
631}
632
633/* Propagate elements from pUpdate to pAcc, overwriting elements with
634** matching docids.
635*/
636static void docListAccumulate(DocList *pAcc, DocList *pUpdate){
637 DocListReader accReader, updateReader;
638
639 /* Handle edge cases where one doclist is empty. */
640 assert( pAcc!=NULL );
641 if( pUpdate==NULL || pUpdate->nData==0 ) return;
642 if( pAcc->nData==0 ){
643 pAcc->pData = malloc(pUpdate->nData);
644 memcpy(pAcc->pData, pUpdate->pData, pUpdate->nData);
645 pAcc->nData = pUpdate->nData;
646 return;
647 }
648
649 readerInit(&accReader, pAcc);
650 readerInit(&updateReader, pUpdate);
651
652 while( !atEnd(&updateReader) ){
653 char *pSource = updateReader.p;
654 sqlite_int64 iDocid = readDocid(&updateReader);
655 skipPositionList(&updateReader);
656 docListSpliceElement(&accReader, iDocid, pSource, updateReader.p-pSource);
657 }
658}
659
660/*
661** Read the next docid off of pIn. Return 0 if we reach the end.
662*
663* TODO: This assumes that docids are never 0, but they may actually be 0 since
664* users can choose docids when inserting into a full-text table. Fix this.
665*/
666static sqlite_int64 nextDocid(DocListReader *pIn){
667 skipPositionList(pIn);
668 return atEnd(pIn) ? 0 : readDocid(pIn);
669}
670
671/*
672** pLeft and pRight are two DocListReaders that are pointing to
673** positions lists of the same document: iDocid.
674**
675** If there are no instances in pLeft or pRight where the position
676** of pLeft is one less than the position of pRight, then this
677** routine adds nothing to pOut.
678**
679** If there are one or more instances where positions from pLeft
680** are exactly one less than positions from pRight, then add a new
681** document record to pOut. If pOut wants to hold positions, then
682** include the positions from pRight that are one more than a
683** position in pLeft. In other words: pRight.iPos==pLeft.iPos+1.
684**
685** pLeft and pRight are left pointing at the next document record.
686*/
687static void mergePosList(
688 DocListReader *pLeft, /* Left position list */
689 DocListReader *pRight, /* Right position list */
690 sqlite_int64 iDocid, /* The docid from pLeft and pRight */
691 DocList *pOut /* Write the merged document record here */
692){
693 int iLeftCol, iLeftPos = readPosition(pLeft, &iLeftCol);
694 int iRightCol, iRightPos = readPosition(pRight, &iRightCol);
695 int match = 0;
696
697 /* Loop until we've reached the end of both position lists. */
698 while( iLeftPos!=-1 && iRightPos!=-1 ){
699 if( iLeftCol==iRightCol && iLeftPos+1==iRightPos ){
700 if( !match ){
701 docListAddDocid(pOut, iDocid);
702 match = 1;
703 }
704 if( pOut->iType>=DL_POSITIONS ){
705 docListAddPos(pOut, iRightCol, iRightPos);
706 }
707 iLeftPos = readPosition(pLeft, &iLeftCol);
708 iRightPos = readPosition(pRight, &iRightCol);
709 }else if( iRightCol<iLeftCol ||
710 (iRightCol==iLeftCol && iRightPos<iLeftPos+1) ){
711 iRightPos = readPosition(pRight, &iRightCol);
712 }else{
713 iLeftPos = readPosition(pLeft, &iLeftCol);
714 }
715 }
716 if( iLeftPos>=0 ) skipPositionList(pLeft);
717 if( iRightPos>=0 ) skipPositionList(pRight);
718}
719
720/* We have two doclists: pLeft and pRight.
721** Write the phrase intersection of these two doclists into pOut.
722**
723** A phrase intersection means that two documents only match
724** if pLeft.iPos+1==pRight.iPos.
725**
726** The output pOut may or may not contain positions. If pOut
727** does contain positions, they are the positions of pRight.
728*/
729static void docListPhraseMerge(
730 DocList *pLeft, /* Doclist resulting from the words on the left */
731 DocList *pRight, /* Doclist for the next word to the right */
732 DocList *pOut /* Write the combined doclist here */
733){
734 DocListReader left, right;
735 sqlite_int64 docidLeft, docidRight;
736
737 readerInit(&left, pLeft);
738 readerInit(&right, pRight);
739 docidLeft = nextDocid(&left);
740 docidRight = nextDocid(&right);
741
742 while( docidLeft>0 && docidRight>0 ){
743 if( docidLeft<docidRight ){
744 docidLeft = nextDocid(&left);
745 }else if( docidRight<docidLeft ){
746 docidRight = nextDocid(&right);
747 }else{
748 mergePosList(&left, &right, docidLeft, pOut);
749 docidLeft = nextDocid(&left);
750 docidRight = nextDocid(&right);
751 }
752 }
753}
754
755/* We have two doclists: pLeft and pRight.
756** Write the intersection of these two doclists into pOut.
757** Only docids are matched. Position information is ignored.
758**
759** The output pOut never holds positions.
760*/
761static void docListAndMerge(
762 DocList *pLeft, /* Doclist resulting from the words on the left */
763 DocList *pRight, /* Doclist for the next word to the right */
764 DocList *pOut /* Write the combined doclist here */
765){
766 DocListReader left, right;
767 sqlite_int64 docidLeft, docidRight;
768
769 assert( pOut->iType<DL_POSITIONS );
770
771 readerInit(&left, pLeft);
772 readerInit(&right, pRight);
773 docidLeft = nextDocid(&left);
774 docidRight = nextDocid(&right);
775
776 while( docidLeft>0 && docidRight>0 ){
777 if( docidLeft<docidRight ){
778 docidLeft = nextDocid(&left);
779 }else if( docidRight<docidLeft ){
780 docidRight = nextDocid(&right);
781 }else{
782 docListAddDocid(pOut, docidLeft);
783 docidLeft = nextDocid(&left);
784 docidRight = nextDocid(&right);
785 }
786 }
787}
788
789/* We have two doclists: pLeft and pRight.
790** Write the union of these two doclists into pOut.
791** Only docids are matched. Position information is ignored.
792**
793** The output pOut never holds positions.
794*/
795static void docListOrMerge(
796 DocList *pLeft, /* Doclist resulting from the words on the left */
797 DocList *pRight, /* Doclist for the next word to the right */
798 DocList *pOut /* Write the combined doclist here */
799){
800 DocListReader left, right;
801 sqlite_int64 docidLeft, docidRight, priorLeft;
802
803 readerInit(&left, pLeft);
804 readerInit(&right, pRight);
805 docidLeft = nextDocid(&left);
806 docidRight = nextDocid(&right);
807
808 while( docidLeft>0 && docidRight>0 ){
809 if( docidLeft<=docidRight ){
810 docListAddDocid(pOut, docidLeft);
811 }else{
812 docListAddDocid(pOut, docidRight);
813 }
814 priorLeft = docidLeft;
815 if( docidLeft<=docidRight ){
816 docidLeft = nextDocid(&left);
817 }
818 if( docidRight>0 && docidRight<=priorLeft ){
819 docidRight = nextDocid(&right);
820 }
821 }
822 while( docidLeft>0 ){
823 docListAddDocid(pOut, docidLeft);
824 docidLeft = nextDocid(&left);
825 }
826 while( docidRight>0 ){
827 docListAddDocid(pOut, docidRight);
828 docidRight = nextDocid(&right);
829 }
830}
831
832/* We have two doclists: pLeft and pRight.
833** Write into pOut all documents that occur in pLeft but not
834** in pRight.
835**
836** Only docids are matched. Position information is ignored.
837**
838** The output pOut never holds positions.
839*/
840static void docListExceptMerge(
841 DocList *pLeft, /* Doclist resulting from the words on the left */
842 DocList *pRight, /* Doclist for the next word to the right */
843 DocList *pOut /* Write the combined doclist here */
844){
845 DocListReader left, right;
846 sqlite_int64 docidLeft, docidRight, priorLeft;
847
848 readerInit(&left, pLeft);
849 readerInit(&right, pRight);
850 docidLeft = nextDocid(&left);
851 docidRight = nextDocid(&right);
852
853 while( docidLeft>0 && docidRight>0 ){
854 priorLeft = docidLeft;
855 if( docidLeft<docidRight ){
856 docListAddDocid(pOut, docidLeft);
857 }
858 if( docidLeft<=docidRight ){
859 docidLeft = nextDocid(&left);
860 }
861 if( docidRight>0 && docidRight<=priorLeft ){
862 docidRight = nextDocid(&right);
863 }
864 }
865 while( docidLeft>0 ){
866 docListAddDocid(pOut, docidLeft);
867 docidLeft = nextDocid(&left);
868 }
869}
870
871static char *string_dup_n(const char *s, int n){
872 char *str = malloc(n + 1);
873 memcpy(str, s, n);
874 str[n] = '\0';
875 return str;
876}
877
878/* Duplicate a string; the caller must free() the returned string.
879 * (We don't use strdup() since it's not part of the standard C library and
880 * may not be available everywhere.) */
881static char *string_dup(const char *s){
882 return string_dup_n(s, strlen(s));
883}
884
885/* Format a string, replacing each occurrence of the % character with
886 * zDb.zName. This may be more convenient than sqlite_mprintf()
887 * when one string is used repeatedly in a format string.
888 * The caller must free() the returned string. */
889static char *string_format(const char *zFormat,
890 const char *zDb, const char *zName){
891 const char *p;
892 size_t len = 0;
893 size_t nDb = strlen(zDb);
894 size_t nName = strlen(zName);
895 size_t nFullTableName = nDb+1+nName;
896 char *result;
897 char *r;
898
899 /* first compute length needed */
900 for(p = zFormat ; *p ; ++p){
901 len += (*p=='%' ? nFullTableName : 1);
902 }
903 len += 1; /* for null terminator */
904
905 r = result = malloc(len);
906 for(p = zFormat; *p; ++p){
907 if( *p=='%' ){
908 memcpy(r, zDb, nDb);
909 r += nDb;
910 *r++ = '.';
911 memcpy(r, zName, nName);
912 r += nName;
913 } else {
914 *r++ = *p;
915 }
916 }
917 *r++ = '\0';
918 assert( r == result + len );
919 return result;
920}
921
922static int sql_exec(sqlite3 *db, const char *zDb, const char *zName,
923 const char *zFormat){
924 char *zCommand = string_format(zFormat, zDb, zName);
925 int rc;
926 TRACE(("FTS1 sql: %s\n", zCommand));
927 rc = sqlite3_exec(db, zCommand, NULL, 0, NULL);
928 free(zCommand);
929 return rc;
930}
931
932static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName,
933 sqlite3_stmt **ppStmt, const char *zFormat){
934 char *zCommand = string_format(zFormat, zDb, zName);
935 int rc;
936 TRACE(("FTS1 prepare: %s\n", zCommand));
937 rc = sqlite3_prepare(db, zCommand, -1, ppStmt, NULL);
938 free(zCommand);
939 return rc;
940}
941
942/* end utility functions */
943
944/* Forward reference */
945typedef struct fulltext_vtab fulltext_vtab;
946
947/* A single term in a query is represented by an instances of
948** the following structure.
949*/
950typedef struct QueryTerm {
951 short int nPhrase; /* How many following terms are part of the same phrase */
952 short int iPhrase; /* This is the i-th term of a phrase. */
953 short int iColumn; /* Column of the index that must match this term */
954 signed char isOr; /* this term is preceded by "OR" */
955 signed char isNot; /* this term is preceded by "-" */
956 char *pTerm; /* text of the term. '\000' terminated. malloced */
957 int nTerm; /* Number of bytes in pTerm[] */
958} QueryTerm;
959
960
961/* A query string is parsed into a Query structure.
962 *
963 * We could, in theory, allow query strings to be complicated
964 * nested expressions with precedence determined by parentheses.
965 * But none of the major search engines do this. (Perhaps the
966 * feeling is that an parenthesized expression is two complex of
967 * an idea for the average user to grasp.) Taking our lead from
968 * the major search engines, we will allow queries to be a list
969 * of terms (with an implied AND operator) or phrases in double-quotes,
970 * with a single optional "-" before each non-phrase term to designate
971 * negation and an optional OR connector.
972 *
973 * OR binds more tightly than the implied AND, which is what the
974 * major search engines seem to do. So, for example:
975 *
976 * [one two OR three] ==> one AND (two OR three)
977 * [one OR two three] ==> (one OR two) AND three
978 *
979 * A "-" before a term matches all entries that lack that term.
980 * The "-" must occur immediately before the term with in intervening
981 * space. This is how the search engines do it.
982 *
983 * A NOT term cannot be the right-hand operand of an OR. If this
984 * occurs in the query string, the NOT is ignored:
985 *
986 * [one OR -two] ==> one OR two
987 *
988 */
989typedef struct Query {
990 fulltext_vtab *pFts; /* The full text index */
991 int nTerms; /* Number of terms in the query */
992 QueryTerm *pTerms; /* Array of terms. Space obtained from malloc() */
993 int nextIsOr; /* Set the isOr flag on the next inserted term */
994 int nextColumn; /* Next word parsed must be in this column */
995 int dfltColumn; /* The default column */
996} Query;
997
998
999/*
1000** An instance of the following structure keeps track of generated
1001** matching-word offset information and snippets.
1002*/
1003typedef struct Snippet {
1004 int nMatch; /* Total number of matches */
1005 int nAlloc; /* Space allocated for aMatch[] */
1006 struct snippetMatch { /* One entry for each matching term */
1007 char snStatus; /* Status flag for use while constructing snippets */
1008 short int iCol; /* The column that contains the match */
1009 short int iTerm; /* The index in Query.pTerms[] of the matching term */
1010 short int nByte; /* Number of bytes in the term */
1011 int iStart; /* The offset to the first character of the term */
1012 } *aMatch; /* Points to space obtained from malloc */
1013 char *zOffset; /* Text rendering of aMatch[] */
1014 int nOffset; /* strlen(zOffset) */
1015 char *zSnippet; /* Snippet text */
1016 int nSnippet; /* strlen(zSnippet) */
1017} Snippet;
1018
1019
1020typedef enum QueryType {
1021 QUERY_GENERIC, /* table scan */
1022 QUERY_ROWID, /* lookup by rowid */
1023 QUERY_FULLTEXT /* QUERY_FULLTEXT + [i] is a full-text search for column i*/
1024} QueryType;
1025
1026/* TODO(shess) CHUNK_MAX controls how much data we allow in segment 0
1027** before we start aggregating into larger segments. Lower CHUNK_MAX
1028** means that for a given input we have more individual segments per
1029** term, which means more rows in the table and a bigger index (due to
1030** both more rows and bigger rowids). But it also reduces the average
1031** cost of adding new elements to the segment 0 doclist, and it seems
1032** to reduce the number of pages read and written during inserts. 256
1033** was chosen by measuring insertion times for a certain input (first
1034** 10k documents of Enron corpus), though including query performance
1035** in the decision may argue for a larger value.
1036*/
1037#define CHUNK_MAX 256
1038
1039typedef enum fulltext_statement {
1040 CONTENT_INSERT_STMT,
1041 CONTENT_SELECT_STMT,
1042 CONTENT_UPDATE_STMT,
1043 CONTENT_DELETE_STMT,
1044
1045 TERM_SELECT_STMT,
1046 TERM_SELECT_ALL_STMT,
1047 TERM_INSERT_STMT,
1048 TERM_UPDATE_STMT,
1049 TERM_DELETE_STMT,
1050
1051 MAX_STMT /* Always at end! */
1052} fulltext_statement;
1053
1054/* These must exactly match the enum above. */
1055/* TODO(adam): Is there some risk that a statement (in particular,
1056** pTermSelectStmt) will be used in two cursors at once, e.g. if a
1057** query joins a virtual table to itself? If so perhaps we should
1058** move some of these to the cursor object.
1059*/
1060static const char *const fulltext_zStatement[MAX_STMT] = {
1061 /* CONTENT_INSERT */ NULL, /* generated in contentInsertStatement() */
1062 /* CONTENT_SELECT */ "select * from %_content where rowid = ?",
1063 /* CONTENT_UPDATE */ NULL, /* generated in contentUpdateStatement() */
1064 /* CONTENT_DELETE */ "delete from %_content where rowid = ?",
1065
1066 /* TERM_SELECT */
1067 "select rowid, doclist from %_term where term = ? and segment = ?",
1068 /* TERM_SELECT_ALL */
1069 "select doclist from %_term where term = ? order by segment",
1070 /* TERM_INSERT */
1071 "insert into %_term (rowid, term, segment, doclist) values (?, ?, ?, ?)",
1072 /* TERM_UPDATE */ "update %_term set doclist = ? where rowid = ?",
1073 /* TERM_DELETE */ "delete from %_term where rowid = ?",
1074};
1075
1076/*
1077** A connection to a fulltext index is an instance of the following
1078** structure. The xCreate and xConnect methods create an instance
1079** of this structure and xDestroy and xDisconnect free that instance.
1080** All other methods receive a pointer to the structure as one of their
1081** arguments.
1082*/
1083struct fulltext_vtab {
1084 sqlite3_vtab base; /* Base class used by SQLite core */
1085 sqlite3 *db; /* The database connection */
1086 const char *zDb; /* logical database name */
1087 const char *zName; /* virtual table name */
1088 int nColumn; /* number of columns in virtual table */
1089 char **azColumn; /* column names. malloced */
1090 char **azContentColumn; /* column names in content table; malloced */
1091 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
1092
1093 /* Precompiled statements which we keep as long as the table is
1094 ** open.
1095 */
1096 sqlite3_stmt *pFulltextStatements[MAX_STMT];
1097};
1098
1099/*
1100** When the core wants to do a query, it create a cursor using a
1101** call to xOpen. This structure is an instance of a cursor. It
1102** is destroyed by xClose.
1103*/
1104typedef struct fulltext_cursor {
1105 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
1106 QueryType iCursorType; /* Copy of sqlite3_index_info.idxNum */
1107 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
1108 int eof; /* True if at End Of Results */
1109 Query q; /* Parsed query string */
1110 Snippet snippet; /* Cached snippet for the current row */
1111 int iColumn; /* Column being searched */
1112 DocListReader result; /* used when iCursorType == QUERY_FULLTEXT */
1113} fulltext_cursor;
1114
1115static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){
1116 return (fulltext_vtab *) c->base.pVtab;
1117}
1118
1119static const sqlite3_module fulltextModule; /* forward declaration */
1120
1121/* Append a list of strings separated by commas to a StringBuffer. */
1122static void appendList(StringBuffer *sb, int nString, char **azString){
1123 int i;
1124 for(i=0; i<nString; ++i){
1125 if( i>0 ) append(sb, ", ");
1126 append(sb, azString[i]);
1127 }
1128}
1129
1130/* Return a dynamically generated statement of the form
1131 * insert into %_content (rowid, ...) values (?, ...)
1132 */
1133static const char *contentInsertStatement(fulltext_vtab *v){
1134 StringBuffer sb;
1135 int i;
1136
1137 initStringBuffer(&sb);
1138 append(&sb, "insert into %_content (rowid, ");
1139 appendList(&sb, v->nColumn, v->azContentColumn);
1140 append(&sb, ") values (?");
1141 for(i=0; i<v->nColumn; ++i)
1142 append(&sb, ", ?");
1143 append(&sb, ")");
1144 return sb.s;
1145}
1146
1147/* Return a dynamically generated statement of the form
1148 * update %_content set [col_0] = ?, [col_1] = ?, ...
1149 * where rowid = ?
1150 */
1151static const char *contentUpdateStatement(fulltext_vtab *v){
1152 StringBuffer sb;
1153 int i;
1154
1155 initStringBuffer(&sb);
1156 append(&sb, "update %_content set ");
1157 for(i=0; i<v->nColumn; ++i) {
1158 if( i>0 ){
1159 append(&sb, ", ");
1160 }
1161 append(&sb, v->azContentColumn[i]);
1162 append(&sb, " = ?");
1163 }
1164 append(&sb, " where rowid = ?");
1165 return sb.s;
1166}
1167
1168/* Puts a freshly-prepared statement determined by iStmt in *ppStmt.
1169** If the indicated statement has never been prepared, it is prepared
1170** and cached, otherwise the cached version is reset.
1171*/
1172static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt,
1173 sqlite3_stmt **ppStmt){
1174 assert( iStmt<MAX_STMT );
1175 if( v->pFulltextStatements[iStmt]==NULL ){
1176 const char *zStmt;
1177 int rc;
1178 switch( iStmt ){
1179 case CONTENT_INSERT_STMT:
1180 zStmt = contentInsertStatement(v); break;
1181 case CONTENT_UPDATE_STMT:
1182 zStmt = contentUpdateStatement(v); break;
1183 default:
1184 zStmt = fulltext_zStatement[iStmt];
1185 }
1186 rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt],
1187 zStmt);
1188 if( zStmt != fulltext_zStatement[iStmt]) free((void *) zStmt);
1189 if( rc!=SQLITE_OK ) return rc;
1190 } else {
1191 int rc = sqlite3_reset(v->pFulltextStatements[iStmt]);
1192 if( rc!=SQLITE_OK ) return rc;
1193 }
1194
1195 *ppStmt = v->pFulltextStatements[iStmt];
1196 return SQLITE_OK;
1197}
1198
1199/* Step the indicated statement, handling errors SQLITE_BUSY (by
1200** retrying) and SQLITE_SCHEMA (by re-preparing and transferring
1201** bindings to the new statement).
1202** TODO(adam): We should extend this function so that it can work with
1203** statements declared locally, not only globally cached statements.
1204*/
1205static int sql_step_statement(fulltext_vtab *v, fulltext_statement iStmt,
1206 sqlite3_stmt **ppStmt){
1207 int rc;
1208 sqlite3_stmt *s = *ppStmt;
1209 assert( iStmt<MAX_STMT );
1210 assert( s==v->pFulltextStatements[iStmt] );
1211
1212 while( (rc=sqlite3_step(s))!=SQLITE_DONE && rc!=SQLITE_ROW ){
1213 if( rc==SQLITE_BUSY ) continue;
1214 if( rc!=SQLITE_ERROR ) return rc;
1215
1216 /* If an SQLITE_SCHEMA error has occured, then finalizing this
1217 * statement is going to delete the fulltext_vtab structure. If
1218 * the statement just executed is in the pFulltextStatements[]
1219 * array, it will be finalized twice. So remove it before
1220 * calling sqlite3_finalize().
1221 */
1222 v->pFulltextStatements[iStmt] = NULL;
1223 rc = sqlite3_finalize(s);
1224 break;
1225 }
1226 return rc;
1227
1228 err:
1229 sqlite3_finalize(s);
1230 return rc;
1231}
1232
1233/* Like sql_step_statement(), but convert SQLITE_DONE to SQLITE_OK.
1234** Useful for statements like UPDATE, where we expect no results.
1235*/
1236static int sql_single_step_statement(fulltext_vtab *v,
1237 fulltext_statement iStmt,
1238 sqlite3_stmt **ppStmt){
1239 int rc = sql_step_statement(v, iStmt, ppStmt);
1240 return (rc==SQLITE_DONE) ? SQLITE_OK : rc;
1241}
1242
1243/* insert into %_content (rowid, ...) values ([rowid], [pValues]) */
1244static int content_insert(fulltext_vtab *v, sqlite3_value *rowid,
1245 sqlite3_value **pValues){
1246 sqlite3_stmt *s;
1247 int i;
1248 int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s);
1249 if( rc!=SQLITE_OK ) return rc;
1250
1251 rc = sqlite3_bind_value(s, 1, rowid);
1252 if( rc!=SQLITE_OK ) return rc;
1253
1254 for(i=0; i<v->nColumn; ++i){
1255 rc = sqlite3_bind_value(s, 2+i, pValues[i]);
1256 if( rc!=SQLITE_OK ) return rc;
1257 }
1258
1259 return sql_single_step_statement(v, CONTENT_INSERT_STMT, &s);
1260}
1261
1262/* update %_content set col0 = pValues[0], col1 = pValues[1], ...
1263 * where rowid = [iRowid] */
1264static int content_update(fulltext_vtab *v, sqlite3_value **pValues,
1265 sqlite_int64 iRowid){
1266 sqlite3_stmt *s;
1267 int i;
1268 int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s);
1269 if( rc!=SQLITE_OK ) return rc;
1270
1271 for(i=0; i<v->nColumn; ++i){
1272 rc = sqlite3_bind_value(s, 1+i, pValues[i]);
1273 if( rc!=SQLITE_OK ) return rc;
1274 }
1275
1276 rc = sqlite3_bind_int64(s, 1+v->nColumn, iRowid);
1277 if( rc!=SQLITE_OK ) return rc;
1278
1279 return sql_single_step_statement(v, CONTENT_UPDATE_STMT, &s);
1280}
1281
1282static void freeStringArray(int nString, const char **pString){
1283 int i;
1284
1285 for (i=0 ; i < nString ; ++i) {
1286 if( pString[i]!=NULL ) free((void *) pString[i]);
1287 }
1288 free((void *) pString);
1289}
1290
1291/* select * from %_content where rowid = [iRow]
1292 * The caller must delete the returned array and all strings in it.
1293 * null fields will be NULL in the returned array.
1294 *
1295 * TODO: Perhaps we should return pointer/length strings here for consistency
1296 * with other code which uses pointer/length. */
1297static int content_select(fulltext_vtab *v, sqlite_int64 iRow,
1298 const char ***pValues){
1299 sqlite3_stmt *s;
1300 const char **values;
1301 int i;
1302 int rc;
1303
1304 *pValues = NULL;
1305
1306 rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s);
1307 if( rc!=SQLITE_OK ) return rc;
1308
1309 rc = sqlite3_bind_int64(s, 1, iRow);
1310 if( rc!=SQLITE_OK ) return rc;
1311
1312 rc = sql_step_statement(v, CONTENT_SELECT_STMT, &s);
1313 if( rc!=SQLITE_ROW ) return rc;
1314
1315 values = (const char **) malloc(v->nColumn * sizeof(const char *));
1316 for(i=0; i<v->nColumn; ++i){
1317 if( sqlite3_column_type(s, i)==SQLITE_NULL ){
1318 values[i] = NULL;
1319 }else{
1320 values[i] = string_dup((char*)sqlite3_column_text(s, i));
1321 }
1322 }
1323
1324 /* We expect only one row. We must execute another sqlite3_step()
1325 * to complete the iteration; otherwise the table will remain locked. */
1326 rc = sqlite3_step(s);
1327 if( rc==SQLITE_DONE ){
1328 *pValues = values;
1329 return SQLITE_OK;
1330 }
1331
1332 freeStringArray(v->nColumn, values);
1333 return rc;
1334}
1335
1336/* delete from %_content where rowid = [iRow ] */
1337static int content_delete(fulltext_vtab *v, sqlite_int64 iRow){
1338 sqlite3_stmt *s;
1339 int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s);
1340 if( rc!=SQLITE_OK ) return rc;
1341
1342 rc = sqlite3_bind_int64(s, 1, iRow);
1343 if( rc!=SQLITE_OK ) return rc;
1344
1345 return sql_single_step_statement(v, CONTENT_DELETE_STMT, &s);
1346}
1347
1348/* select rowid, doclist from %_term
1349 * where term = [pTerm] and segment = [iSegment]
1350 * If found, returns SQLITE_ROW; the caller must free the
1351 * returned doclist. If no rows found, returns SQLITE_DONE. */
1352static int term_select(fulltext_vtab *v, const char *pTerm, int nTerm,
1353 int iSegment,
1354 sqlite_int64 *rowid, DocList *out){
1355 sqlite3_stmt *s;
1356 int rc = sql_get_statement(v, TERM_SELECT_STMT, &s);
1357 if( rc!=SQLITE_OK ) return rc;
1358
1359 rc = sqlite3_bind_text(s, 1, pTerm, nTerm, SQLITE_STATIC);
1360 if( rc!=SQLITE_OK ) return rc;
1361
1362 rc = sqlite3_bind_int(s, 2, iSegment);
1363 if( rc!=SQLITE_OK ) return rc;
1364
1365 rc = sql_step_statement(v, TERM_SELECT_STMT, &s);
1366 if( rc!=SQLITE_ROW ) return rc;
1367
1368 *rowid = sqlite3_column_int64(s, 0);
1369 docListInit(out, DL_DEFAULT,
1370 sqlite3_column_blob(s, 1), sqlite3_column_bytes(s, 1));
1371
1372 /* We expect only one row. We must execute another sqlite3_step()
1373 * to complete the iteration; otherwise the table will remain locked. */
1374 rc = sqlite3_step(s);
1375 return rc==SQLITE_DONE ? SQLITE_ROW : rc;
1376}
1377
1378/* Load the segment doclists for term pTerm and merge them in
1379** appropriate order into out. Returns SQLITE_OK if successful. If
1380** there are no segments for pTerm, successfully returns an empty
1381** doclist in out.
1382**
1383** Each document consists of 1 or more "columns". The number of
1384** columns is v->nColumn. If iColumn==v->nColumn, then return
1385** position information about all columns. If iColumn<v->nColumn,
1386** then only return position information about the iColumn-th column
1387** (where the first column is 0).
1388*/
1389static int term_select_all(
1390 fulltext_vtab *v, /* The fulltext index we are querying against */
1391 int iColumn, /* If <nColumn, only look at the iColumn-th column */
1392 const char *pTerm, /* The term whose posting lists we want */
1393 int nTerm, /* Number of bytes in pTerm */
1394 DocList *out /* Write the resulting doclist here */
1395){
1396 DocList doclist;
1397 sqlite3_stmt *s;
1398 int rc = sql_get_statement(v, TERM_SELECT_ALL_STMT, &s);
1399 if( rc!=SQLITE_OK ) return rc;
1400
1401 rc = sqlite3_bind_text(s, 1, pTerm, nTerm, SQLITE_STATIC);
1402 if( rc!=SQLITE_OK ) return rc;
1403
1404 docListInit(&doclist, DL_DEFAULT, 0, 0);
1405
1406 /* TODO(shess) Handle schema and busy errors. */
1407 while( (rc=sql_step_statement(v, TERM_SELECT_ALL_STMT, &s))==SQLITE_ROW ){
1408 DocList old;
1409
1410 /* TODO(shess) If we processed doclists from oldest to newest, we
1411 ** could skip the malloc() involved with the following call. For
1412 ** now, I'd rather keep this logic similar to index_insert_term().
1413 ** We could additionally drop elements when we see deletes, but
1414 ** that would require a distinct version of docListAccumulate().
1415 */
1416 docListInit(&old, DL_DEFAULT,
1417 sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0));
1418
1419 if( iColumn<v->nColumn ){ /* querying a single column */
1420 docListRestrictColumn(&old, iColumn);
1421 }
1422
1423 /* doclist contains the newer data, so write it over old. Then
1424 ** steal accumulated result for doclist.
1425 */
1426 docListAccumulate(&old, &doclist);
1427 docListDestroy(&doclist);
1428 doclist = old;
1429 }
1430 if( rc!=SQLITE_DONE ){
1431 docListDestroy(&doclist);
1432 return rc;
1433 }
1434
1435 docListDiscardEmpty(&doclist);
1436 *out = doclist;
1437 return SQLITE_OK;
1438}
1439
1440/* insert into %_term (rowid, term, segment, doclist)
1441 values ([piRowid], [pTerm], [iSegment], [doclist])
1442** Lets sqlite select rowid if piRowid is NULL, else uses *piRowid.
1443**
1444** NOTE(shess) piRowid is IN, with values of "space of int64" plus
1445** null, it is not used to pass data back to the caller.
1446*/
1447static int term_insert(fulltext_vtab *v, sqlite_int64 *piRowid,
1448 const char *pTerm, int nTerm,
1449 int iSegment, DocList *doclist){
1450 sqlite3_stmt *s;
1451 int rc = sql_get_statement(v, TERM_INSERT_STMT, &s);
1452 if( rc!=SQLITE_OK ) return rc;
1453
1454 if( piRowid==NULL ){
1455 rc = sqlite3_bind_null(s, 1);
1456 }else{
1457 rc = sqlite3_bind_int64(s, 1, *piRowid);
1458 }
1459 if( rc!=SQLITE_OK ) return rc;
1460
1461 rc = sqlite3_bind_text(s, 2, pTerm, nTerm, SQLITE_STATIC);
1462 if( rc!=SQLITE_OK ) return rc;
1463
1464 rc = sqlite3_bind_int(s, 3, iSegment);
1465 if( rc!=SQLITE_OK ) return rc;
1466
1467 rc = sqlite3_bind_blob(s, 4, doclist->pData, doclist->nData, SQLITE_STATIC);
1468 if( rc!=SQLITE_OK ) return rc;
1469
1470 return sql_single_step_statement(v, TERM_INSERT_STMT, &s);
1471}
1472
1473/* update %_term set doclist = [doclist] where rowid = [rowid] */
1474static int term_update(fulltext_vtab *v, sqlite_int64 rowid,
1475 DocList *doclist){
1476 sqlite3_stmt *s;
1477 int rc = sql_get_statement(v, TERM_UPDATE_STMT, &s);
1478 if( rc!=SQLITE_OK ) return rc;
1479
1480 rc = sqlite3_bind_blob(s, 1, doclist->pData, doclist->nData, SQLITE_STATIC);
1481 if( rc!=SQLITE_OK ) return rc;
1482
1483 rc = sqlite3_bind_int64(s, 2, rowid);
1484 if( rc!=SQLITE_OK ) return rc;
1485
1486 return sql_single_step_statement(v, TERM_UPDATE_STMT, &s);
1487}
1488
1489static int term_delete(fulltext_vtab *v, sqlite_int64 rowid){
1490 sqlite3_stmt *s;
1491 int rc = sql_get_statement(v, TERM_DELETE_STMT, &s);
1492 if( rc!=SQLITE_OK ) return rc;
1493
1494 rc = sqlite3_bind_int64(s, 1, rowid);
1495 if( rc!=SQLITE_OK ) return rc;
1496
1497 return sql_single_step_statement(v, TERM_DELETE_STMT, &s);
1498}
1499
1500/*
1501** Free the memory used to contain a fulltext_vtab structure.
1502*/
1503static void fulltext_vtab_destroy(fulltext_vtab *v){
1504 int iStmt, i;
1505
1506 TRACE(("FTS1 Destroy %p\n", v));
1507 for( iStmt=0; iStmt<MAX_STMT; iStmt++ ){
1508 if( v->pFulltextStatements[iStmt]!=NULL ){
1509 sqlite3_finalize(v->pFulltextStatements[iStmt]);
1510 v->pFulltextStatements[iStmt] = NULL;
1511 }
1512 }
1513
1514 if( v->pTokenizer!=NULL ){
1515 v->pTokenizer->pModule->xDestroy(v->pTokenizer);
1516 v->pTokenizer = NULL;
1517 }
1518
1519 free(v->azColumn);
1520 for(i = 0; i < v->nColumn; ++i) {
1521 sqlite3_free(v->azContentColumn[i]);
1522 }
1523 free(v->azContentColumn);
1524 free(v);
1525}
1526
1527/*
1528** Token types for parsing the arguments to xConnect or xCreate.
1529*/
1530#define TOKEN_EOF 0 /* End of file */
1531#define TOKEN_SPACE 1 /* Any kind of whitespace */
1532#define TOKEN_ID 2 /* An identifier */
1533#define TOKEN_STRING 3 /* A string literal */
1534#define TOKEN_PUNCT 4 /* A single punctuation character */
1535
1536/*
1537** If X is a character that can be used in an identifier then
1538** IdChar(X) will be true. Otherwise it is false.
1539**
1540** For ASCII, any character with the high-order bit set is
1541** allowed in an identifier. For 7-bit characters,
1542** sqlite3IsIdChar[X] must be 1.
1543**
1544** Ticket #1066. the SQL standard does not allow '$' in the
1545** middle of identfiers. But many SQL implementations do.
1546** SQLite will allow '$' in identifiers for compatibility.
1547** But the feature is undocumented.
1548*/
1549static const char isIdChar[] = {
1550/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1551 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
1552 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
1553 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
1554 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
1555 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
1556 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
1557};
1558#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20]))
1559
1560
1561/*
1562** Return the length of the token that begins at z[0].
1563** Store the token type in *tokenType before returning.
1564*/
1565static int getToken(const char *z, int *tokenType){
1566 int i, c;
1567 switch( *z ){
1568 case 0: {
1569 *tokenType = TOKEN_EOF;
1570 return 0;
1571 }
1572 case ' ': case '\t': case '\n': case '\f': case '\r': {
1573 for(i=1; safe_isspace(z[i]); i++){}
1574 *tokenType = TOKEN_SPACE;
1575 return i;
1576 }
1577 case '`':
1578 case '\'':
1579 case '"': {
1580 int delim = z[0];
1581 for(i=1; (c=z[i])!=0; i++){
1582 if( c==delim ){
1583 if( z[i+1]==delim ){
1584 i++;
1585 }else{
1586 break;
1587 }
1588 }
1589 }
1590 *tokenType = TOKEN_STRING;
1591 return i + (c!=0);
1592 }
1593 case '[': {
1594 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
1595 *tokenType = TOKEN_ID;
1596 return i;
1597 }
1598 default: {
1599 if( !IdChar(*z) ){
1600 break;
1601 }
1602 for(i=1; IdChar(z[i]); i++){}
1603 *tokenType = TOKEN_ID;
1604 return i;
1605 }
1606 }
1607 *tokenType = TOKEN_PUNCT;
1608 return 1;
1609}
1610
1611/*
1612** A token extracted from a string is an instance of the following
1613** structure.
1614*/
1615typedef struct Token {
1616 const char *z; /* Pointer to token text. Not '\000' terminated */
1617 short int n; /* Length of the token text in bytes. */
1618} Token;
1619
1620/*
1621** Given a input string (which is really one of the argv[] parameters
1622** passed into xConnect or xCreate) split the string up into tokens.
1623** Return an array of pointers to '\000' terminated strings, one string
1624** for each non-whitespace token.
1625**
1626** The returned array is terminated by a single NULL pointer.
1627**
1628** Space to hold the returned array is obtained from a single
1629** malloc and should be freed by passing the return value to free().
1630** The individual strings within the token list are all a part of
1631** the single memory allocation and will all be freed at once.
1632*/
1633static char **tokenizeString(const char *z, int *pnToken){
1634 int nToken = 0;
1635 Token *aToken = malloc( strlen(z) * sizeof(aToken[0]) );
1636 int n = 1;
1637 int e, i;
1638 int totalSize = 0;
1639 char **azToken;
1640 char *zCopy;
1641 while( n>0 ){
1642 n = getToken(z, &e);
1643 if( e!=TOKEN_SPACE ){
1644 aToken[nToken].z = z;
1645 aToken[nToken].n = n;
1646 nToken++;
1647 totalSize += n+1;
1648 }
1649 z += n;
1650 }
1651 azToken = (char**)malloc( nToken*sizeof(char*) + totalSize );
1652 zCopy = (char*)&azToken[nToken];
1653 nToken--;
1654 for(i=0; i<nToken; i++){
1655 azToken[i] = zCopy;
1656 n = aToken[i].n;
1657 memcpy(zCopy, aToken[i].z, n);
1658 zCopy[n] = 0;
1659 zCopy += n+1;
1660 }
1661 azToken[nToken] = 0;
1662 free(aToken);
1663 *pnToken = nToken;
1664 return azToken;
1665}
1666
1667/*
1668** Convert an SQL-style quoted string into a normal string by removing
1669** the quote characters. The conversion is done in-place. If the
1670** input does not begin with a quote character, then this routine
1671** is a no-op.
1672**
1673** Examples:
1674**
1675** "abc" becomes abc
1676** 'xyz' becomes xyz
1677** [pqr] becomes pqr
1678** `mno` becomes mno
1679*/
1680static void dequoteString(char *z){
1681 int quote;
1682 int i, j;
1683 if( z==0 ) return;
1684 quote = z[0];
1685 switch( quote ){
1686 case '\'': break;
1687 case '"': break;
1688 case '`': break; /* For MySQL compatibility */
1689 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
1690 default: return;
1691 }
1692 for(i=1, j=0; z[i]; i++){
1693 if( z[i]==quote ){
1694 if( z[i+1]==quote ){
1695 z[j++] = quote;
1696 i++;
1697 }else{
1698 z[j++] = 0;
1699 break;
1700 }
1701 }else{
1702 z[j++] = z[i];
1703 }
1704 }
1705}
1706
1707/*
1708** The input azIn is a NULL-terminated list of tokens. Remove the first
1709** token and all punctuation tokens. Remove the quotes from
1710** around string literal tokens.
1711**
1712** Example:
1713**
1714** input: tokenize chinese ( 'simplifed' , 'mixed' )
1715** output: chinese simplifed mixed
1716**
1717** Another example:
1718**
1719** input: delimiters ( '[' , ']' , '...' )
1720** output: [ ] ...
1721*/
1722static void tokenListToIdList(char **azIn){
1723 int i, j;
1724 if( azIn ){
1725 for(i=0, j=-1; azIn[i]; i++){
1726 if( safe_isalnum(azIn[i][0]) || azIn[i][1] ){
1727 dequoteString(azIn[i]);
1728 if( j>=0 ){
1729 azIn[j] = azIn[i];
1730 }
1731 j++;
1732 }
1733 }
1734 azIn[j] = 0;
1735 }
1736}
1737
1738
1739/*
1740** Find the first alphanumeric token in the string zIn. Null-terminate
1741** this token. Remove any quotation marks. And return a pointer to
1742** the result.
1743*/
1744static char *firstToken(char *zIn, char **pzTail){
1745 int n, ttype;
1746 while(1){
1747 n = getToken(zIn, &ttype);
1748 if( ttype==TOKEN_SPACE ){
1749 zIn += n;
1750 }else if( ttype==TOKEN_EOF ){
1751 *pzTail = zIn;
1752 return 0;
1753 }else{
1754 zIn[n] = 0;
1755 *pzTail = &zIn[1];
1756 dequoteString(zIn);
1757 return zIn;
1758 }
1759 }
1760 /*NOTREACHED*/
1761}
1762
1763/* Return true if...
1764**
1765** * s begins with the string t, ignoring case
1766** * s is longer than t
1767** * The first character of s beyond t is not a alphanumeric
1768**
1769** Ignore leading space in *s.
1770**
1771** To put it another way, return true if the first token of
1772** s[] is t[].
1773*/
1774static int startsWith(const char *s, const char *t){
1775 while( safe_isspace(*s) ){ s++; }
1776 while( *t ){
1777 if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0;
1778 }
1779 return *s!='_' && !safe_isalnum(*s);
1780}
1781
1782/*
1783** An instance of this structure defines the "spec" of a
1784** full text index. This structure is populated by parseSpec
1785** and use by fulltextConnect and fulltextCreate.
1786*/
1787typedef struct TableSpec {
1788 const char *zDb; /* Logical database name */
1789 const char *zName; /* Name of the full-text index */
1790 int nColumn; /* Number of columns to be indexed */
1791 char **azColumn; /* Original names of columns to be indexed */
1792 char **azContentColumn; /* Column names for %_content */
1793 char **azTokenizer; /* Name of tokenizer and its arguments */
1794} TableSpec;
1795
1796/*
1797** Reclaim all of the memory used by a TableSpec
1798*/
1799static void clearTableSpec(TableSpec *p) {
1800 free(p->azColumn);
1801 free(p->azContentColumn);
1802 free(p->azTokenizer);
1803}
1804
1805/* Parse a CREATE VIRTUAL TABLE statement, which looks like this:
1806 *
1807 * CREATE VIRTUAL TABLE email
1808 * USING fts1(subject, body, tokenize mytokenizer(myarg))
1809 *
1810 * We return parsed information in a TableSpec structure.
1811 *
1812 */
1813static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv,
1814 char**pzErr){
1815 int i, n;
1816 char *z, *zDummy;
1817 char **azArg;
1818 const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */
1819
1820 assert( argc>=3 );
1821 /* Current interface:
1822 ** argv[0] - module name
1823 ** argv[1] - database name
1824 ** argv[2] - table name
1825 ** argv[3..] - columns, optionally followed by tokenizer specification
1826 ** and snippet delimiters specification.
1827 */
1828
1829 /* Make a copy of the complete argv[][] array in a single allocation.
1830 ** The argv[][] array is read-only and transient. We can write to the
1831 ** copy in order to modify things and the copy is persistent.
1832 */
1833 memset(pSpec, 0, sizeof(*pSpec));
1834 for(i=n=0; i<argc; i++){
1835 n += strlen(argv[i]) + 1;
1836 }
1837 azArg = malloc( sizeof(char*)*argc + n );
1838 if( azArg==0 ){
1839 return SQLITE_NOMEM;
1840 }
1841 z = (char*)&azArg[argc];
1842 for(i=0; i<argc; i++){
1843 azArg[i] = z;
1844 strcpy(z, argv[i]);
1845 z += strlen(z)+1;
1846 }
1847
1848 /* Identify the column names and the tokenizer and delimiter arguments
1849 ** in the argv[][] array.
1850 */
1851 pSpec->zDb = azArg[1];
1852 pSpec->zName = azArg[2];
1853 pSpec->nColumn = 0;
1854 pSpec->azColumn = azArg;
1855 zTokenizer = "tokenize simple";
1856 for(i=3; i<argc; ++i){
1857 if( startsWith(azArg[i],"tokenize") ){
1858 zTokenizer = azArg[i];
1859 }else{
1860 z = azArg[pSpec->nColumn] = firstToken(azArg[i], &zDummy);
1861 pSpec->nColumn++;
1862 }
1863 }
1864 if( pSpec->nColumn==0 ){
1865 azArg[0] = "content";
1866 pSpec->nColumn = 1;
1867 }
1868
1869 /*
1870 ** Construct the list of content column names.
1871 **
1872 ** Each content column name will be of the form cNNAAAA
1873 ** where NN is the column number and AAAA is the sanitized
1874 ** column name. "sanitized" means that special characters are
1875 ** converted to "_". The cNN prefix guarantees that all column
1876 ** names are unique.
1877 **
1878 ** The AAAA suffix is not strictly necessary. It is included
1879 ** for the convenience of people who might examine the generated
1880 ** %_content table and wonder what the columns are used for.
1881 */
1882 pSpec->azContentColumn = malloc( pSpec->nColumn * sizeof(char *) );
1883 if( pSpec->azContentColumn==0 ){
1884 clearTableSpec(pSpec);
1885 return SQLITE_NOMEM;
1886 }
1887 for(i=0; i<pSpec->nColumn; i++){
1888 char *p;
1889 pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]);
1890 for (p = pSpec->azContentColumn[i]; *p ; ++p) {
1891 if( !safe_isalnum(*p) ) *p = '_';
1892 }
1893 }
1894
1895 /*
1896 ** Parse the tokenizer specification string.
1897 */
1898 pSpec->azTokenizer = tokenizeString(zTokenizer, &n);
1899 tokenListToIdList(pSpec->azTokenizer);
1900
1901 return SQLITE_OK;
1902}
1903
1904/*
1905** Generate a CREATE TABLE statement that describes the schema of
1906** the virtual table. Return a pointer to this schema string.
1907**
1908** Space is obtained from sqlite3_mprintf() and should be freed
1909** using sqlite3_free().
1910*/
1911static char *fulltextSchema(
1912 int nColumn, /* Number of columns */
1913 const char *const* azColumn, /* List of columns */
1914 const char *zTableName /* Name of the table */
1915){
1916 int i;
1917 char *zSchema, *zNext;
1918 const char *zSep = "(";
1919 zSchema = sqlite3_mprintf("CREATE TABLE x");
1920 for(i=0; i<nColumn; i++){
1921 zNext = sqlite3_mprintf("%s%s%Q", zSchema, zSep, azColumn[i]);
1922 sqlite3_free(zSchema);
1923 zSchema = zNext;
1924 zSep = ",";
1925 }
1926 zNext = sqlite3_mprintf("%s,%Q)", zSchema, zTableName);
1927 sqlite3_free(zSchema);
1928 return zNext;
1929}
1930
1931/*
1932** Build a new sqlite3_vtab structure that will describe the
1933** fulltext index defined by spec.
1934*/
1935static int constructVtab(
1936 sqlite3 *db, /* The SQLite database connection */
1937 TableSpec *spec, /* Parsed spec information from parseSpec() */
1938 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
1939 char **pzErr /* Write any error message here */
1940){
1941 int rc;
1942 int n;
1943 fulltext_vtab *v = 0;
1944 const sqlite3_tokenizer_module *m = NULL;
1945 char *schema;
1946
1947 v = (fulltext_vtab *) malloc(sizeof(fulltext_vtab));
1948 if( v==0 ) return SQLITE_NOMEM;
1949 memset(v, 0, sizeof(*v));
1950 /* sqlite will initialize v->base */
1951 v->db = db;
1952 v->zDb = spec->zDb; /* Freed when azColumn is freed */
1953 v->zName = spec->zName; /* Freed when azColumn is freed */
1954 v->nColumn = spec->nColumn;
1955 v->azContentColumn = spec->azContentColumn;
1956 spec->azContentColumn = 0;
1957 v->azColumn = spec->azColumn;
1958 spec->azColumn = 0;
1959
1960 if( spec->azTokenizer==0 ){
1961 return SQLITE_NOMEM;
1962 }
1963 /* TODO(shess) For now, add new tokenizers as else if clauses. */
1964 if( spec->azTokenizer[0]==0 || startsWith(spec->azTokenizer[0], "simple") ){
1965 sqlite3Fts1SimpleTokenizerModule(&m);
1966 }else if( startsWith(spec->azTokenizer[0], "porter") ){
1967 sqlite3Fts1PorterTokenizerModule(&m);
1968 }else{
1969 *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]);
1970 rc = SQLITE_ERROR;
1971 goto err;
1972 }
1973 for(n=0; spec->azTokenizer[n]; n++){}
1974 if( n ){
1975 rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1],
1976 &v->pTokenizer);
1977 }else{
1978 rc = m->xCreate(0, 0, &v->pTokenizer);
1979 }
1980 if( rc!=SQLITE_OK ) goto err;
1981 v->pTokenizer->pModule = m;
1982
1983 /* TODO: verify the existence of backing tables foo_content, foo_term */
1984
1985 schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn,
1986 spec->zName);
1987 rc = sqlite3_declare_vtab(db, schema);
1988 sqlite3_free(schema);
1989 if( rc!=SQLITE_OK ) goto err;
1990
1991 memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements));
1992
1993 *ppVTab = &v->base;
1994 TRACE(("FTS1 Connect %p\n", v));
1995
1996 return rc;
1997
1998err:
1999 fulltext_vtab_destroy(v);
2000 return rc;
2001}
2002
2003static int fulltextConnect(
2004 sqlite3 *db,
2005 void *pAux,
2006 int argc, const char *const*argv,
2007 sqlite3_vtab **ppVTab,
2008 char **pzErr
2009){
2010 TableSpec spec;
2011 int rc = parseSpec(&spec, argc, argv, pzErr);
2012 if( rc!=SQLITE_OK ) return rc;
2013
2014 rc = constructVtab(db, &spec, ppVTab, pzErr);
2015 clearTableSpec(&spec);
2016 return rc;
2017}
2018
2019 /* The %_content table holds the text of each document, with
2020 ** the rowid used as the docid.
2021 **
2022 ** The %_term table maps each term to a document list blob
2023 ** containing elements sorted by ascending docid, each element
2024 ** encoded as:
2025 **
2026 ** docid varint-encoded
2027 ** token elements:
2028 ** position+1 varint-encoded as delta from previous position
2029 ** start offset varint-encoded as delta from previous start offset
2030 ** end offset varint-encoded as delta from start offset
2031 **
2032 ** The sentinel position of 0 indicates the end of the token list.
2033 **
2034 ** Additionally, doclist blobs are chunked into multiple segments,
2035 ** using segment to order the segments. New elements are added to
2036 ** the segment at segment 0, until it exceeds CHUNK_MAX. Then
2037 ** segment 0 is deleted, and the doclist is inserted at segment 1.
2038 ** If there is already a doclist at segment 1, the segment 0 doclist
2039 ** is merged with it, the segment 1 doclist is deleted, and the
2040 ** merged doclist is inserted at segment 2, repeating those
2041 ** operations until an insert succeeds.
2042 **
2043 ** Since this structure doesn't allow us to update elements in place
2044 ** in case of deletion or update, these are simply written to
2045 ** segment 0 (with an empty token list in case of deletion), with
2046 ** docListAccumulate() taking care to retain lower-segment
2047 ** information in preference to higher-segment information.
2048 */
2049 /* TODO(shess) Provide a VACUUM type operation which both removes
2050 ** deleted elements which are no longer necessary, and duplicated
2051 ** elements. I suspect this will probably not be necessary in
2052 ** practice, though.
2053 */
2054static int fulltextCreate(sqlite3 *db, void *pAux,
2055 int argc, const char * const *argv,
2056 sqlite3_vtab **ppVTab, char **pzErr){
2057 int rc;
2058 TableSpec spec;
2059 StringBuffer schema;
2060 TRACE(("FTS1 Create\n"));
2061
2062 rc = parseSpec(&spec, argc, argv, pzErr);
2063 if( rc!=SQLITE_OK ) return rc;
2064
2065 initStringBuffer(&schema);
2066 append(&schema, "CREATE TABLE %_content(");
2067 appendList(&schema, spec.nColumn, spec.azContentColumn);
2068 append(&schema, ")");
2069 rc = sql_exec(db, spec.zDb, spec.zName, schema.s);
2070 free(schema.s);
2071 if( rc!=SQLITE_OK ) goto out;
2072
2073 rc = sql_exec(db, spec.zDb, spec.zName,
2074 "create table %_term(term text, segment integer, doclist blob, "
2075 "primary key(term, segment));");
2076 if( rc!=SQLITE_OK ) goto out;
2077
2078 rc = constructVtab(db, &spec, ppVTab, pzErr);
2079
2080out:
2081 clearTableSpec(&spec);
2082 return rc;
2083}
2084
2085/* Decide how to handle an SQL query. */
2086static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
2087 int i;
2088 TRACE(("FTS1 BestIndex\n"));
2089
2090 for(i=0; i<pInfo->nConstraint; ++i){
2091 const struct sqlite3_index_constraint *pConstraint;
2092 pConstraint = &pInfo->aConstraint[i];
2093 if( pConstraint->usable ) {
2094 if( pConstraint->iColumn==-1 &&
2095 pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
2096 pInfo->idxNum = QUERY_ROWID; /* lookup by rowid */
2097 TRACE(("FTS1 QUERY_ROWID\n"));
2098 } else if( pConstraint->iColumn>=0 &&
2099 pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
2100 /* full-text search */
2101 pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn;
2102 TRACE(("FTS1 QUERY_FULLTEXT %d\n", pConstraint->iColumn));
2103 } else continue;
2104
2105 pInfo->aConstraintUsage[i].argvIndex = 1;
2106 pInfo->aConstraintUsage[i].omit = 1;
2107
2108 /* An arbitrary value for now.
2109 * TODO: Perhaps rowid matches should be considered cheaper than
2110 * full-text searches. */
2111 pInfo->estimatedCost = 1.0;
2112
2113 return SQLITE_OK;
2114 }
2115 }
2116 pInfo->idxNum = QUERY_GENERIC;
2117 return SQLITE_OK;
2118}
2119
2120static int fulltextDisconnect(sqlite3_vtab *pVTab){
2121 TRACE(("FTS1 Disconnect %p\n", pVTab));
2122 fulltext_vtab_destroy((fulltext_vtab *)pVTab);
2123 return SQLITE_OK;
2124}
2125
2126static int fulltextDestroy(sqlite3_vtab *pVTab){
2127 fulltext_vtab *v = (fulltext_vtab *)pVTab;
2128 int rc;
2129
2130 TRACE(("FTS1 Destroy %p\n", pVTab));
2131 rc = sql_exec(v->db, v->zDb, v->zName,
2132 "drop table if exists %_content;"
2133 "drop table if exists %_term;"
2134 );
2135 if( rc!=SQLITE_OK ) return rc;
2136
2137 fulltext_vtab_destroy((fulltext_vtab *)pVTab);
2138 return SQLITE_OK;
2139}
2140
2141static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
2142 fulltext_cursor *c;
2143
2144 c = (fulltext_cursor *) calloc(sizeof(fulltext_cursor), 1);
2145 /* sqlite will initialize c->base */
2146 *ppCursor = &c->base;
2147 TRACE(("FTS1 Open %p: %p\n", pVTab, c));
2148
2149 return SQLITE_OK;
2150}
2151
2152
2153/* Free all of the dynamically allocated memory held by *q
2154*/
2155static void queryClear(Query *q){
2156 int i;
2157 for(i = 0; i < q->nTerms; ++i){
2158 free(q->pTerms[i].pTerm);
2159 }
2160 free(q->pTerms);
2161 memset(q, 0, sizeof(*q));
2162}
2163
2164/* Free all of the dynamically allocated memory held by the
2165** Snippet
2166*/
2167static void snippetClear(Snippet *p){
2168 free(p->aMatch);
2169 free(p->zOffset);
2170 free(p->zSnippet);
2171 memset(p, 0, sizeof(*p));
2172}
2173/*
2174** Append a single entry to the p->aMatch[] log.
2175*/
2176static void snippetAppendMatch(
2177 Snippet *p, /* Append the entry to this snippet */
2178 int iCol, int iTerm, /* The column and query term */
2179 int iStart, int nByte /* Offset and size of the match */
2180){
2181 int i;
2182 struct snippetMatch *pMatch;
2183 if( p->nMatch+1>=p->nAlloc ){
2184 p->nAlloc = p->nAlloc*2 + 10;
2185 p->aMatch = realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) );
2186 if( p->aMatch==0 ){
2187 p->nMatch = 0;
2188 p->nAlloc = 0;
2189 return;
2190 }
2191 }
2192 i = p->nMatch++;
2193 pMatch = &p->aMatch[i];
2194 pMatch->iCol = iCol;
2195 pMatch->iTerm = iTerm;
2196 pMatch->iStart = iStart;
2197 pMatch->nByte = nByte;
2198}
2199
2200/*
2201** Sizing information for the circular buffer used in snippetOffsetsOfColumn()
2202*/
2203#define FTS1_ROTOR_SZ (32)
2204#define FTS1_ROTOR_MASK (FTS1_ROTOR_SZ-1)
2205
2206/*
2207** Add entries to pSnippet->aMatch[] for every match that occurs against
2208** document zDoc[0..nDoc-1] which is stored in column iColumn.
2209*/
2210static void snippetOffsetsOfColumn(
2211 Query *pQuery,
2212 Snippet *pSnippet,
2213 int iColumn,
2214 const char *zDoc,
2215 int nDoc
2216){
2217 const sqlite3_tokenizer_module *pTModule; /* The tokenizer module */
2218 sqlite3_tokenizer *pTokenizer; /* The specific tokenizer */
2219 sqlite3_tokenizer_cursor *pTCursor; /* Tokenizer cursor */
2220 fulltext_vtab *pVtab; /* The full text index */
2221 int nColumn; /* Number of columns in the index */
2222 const QueryTerm *aTerm; /* Query string terms */
2223 int nTerm; /* Number of query string terms */
2224 int i, j; /* Loop counters */
2225 int rc; /* Return code */
2226 unsigned int match, prevMatch; /* Phrase search bitmasks */
2227 const char *zToken; /* Next token from the tokenizer */
2228 int nToken; /* Size of zToken */
2229 int iBegin, iEnd, iPos; /* Offsets of beginning and end */
2230
2231 /* The following variables keep a circular buffer of the last
2232 ** few tokens */
2233 unsigned int iRotor = 0; /* Index of current token */
2234 int iRotorBegin[FTS1_ROTOR_SZ]; /* Beginning offset of token */
2235 int iRotorLen[FTS1_ROTOR_SZ]; /* Length of token */
2236
2237 pVtab = pQuery->pFts;
2238 nColumn = pVtab->nColumn;
2239 pTokenizer = pVtab->pTokenizer;
2240 pTModule = pTokenizer->pModule;
2241 rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor);
2242 if( rc ) return;
2243 pTCursor->pTokenizer = pTokenizer;
2244 aTerm = pQuery->pTerms;
2245 nTerm = pQuery->nTerms;
2246 if( nTerm>=FTS1_ROTOR_SZ ){
2247 nTerm = FTS1_ROTOR_SZ - 1;
2248 }
2249 prevMatch = 0;
2250 while(1){
2251 rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos);
2252 if( rc ) break;
2253 iRotorBegin[iRotor&FTS1_ROTOR_MASK] = iBegin;
2254 iRotorLen[iRotor&FTS1_ROTOR_MASK] = iEnd-iBegin;
2255 match = 0;
2256 for(i=0; i<nTerm; i++){
2257 int iCol;
2258 iCol = aTerm[i].iColumn;
2259 if( iCol>=0 && iCol<nColumn && iCol!=iColumn ) continue;
2260 if( aTerm[i].nTerm!=nToken ) continue;
2261 if( memcmp(aTerm[i].pTerm, zToken, nToken) ) continue;
2262 if( aTerm[i].iPhrase>1 && (prevMatch & (1<<i))==0 ) continue;
2263 match |= 1<<i;
2264 if( i==nTerm-1 || aTerm[i+1].iPhrase==1 ){
2265 for(j=aTerm[i].iPhrase-1; j>=0; j--){
2266 int k = (iRotor-j) & FTS1_ROTOR_MASK;
2267 snippetAppendMatch(pSnippet, iColumn, i-j,
2268 iRotorBegin[k], iRotorLen[k]);
2269 }
2270 }
2271 }
2272 prevMatch = match<<1;
2273 iRotor++;
2274 }
2275 pTModule->xClose(pTCursor);
2276}
2277
2278
2279/*
2280** Compute all offsets for the current row of the query.
2281** If the offsets have already been computed, this routine is a no-op.
2282*/
2283static void snippetAllOffsets(fulltext_cursor *p){
2284 int nColumn;
2285 int iColumn, i;
2286 int iFirst, iLast;
2287 fulltext_vtab *pFts;
2288
2289 if( p->snippet.nMatch ) return;
2290 if( p->q.nTerms==0 ) return;
2291 pFts = p->q.pFts;
2292 nColumn = pFts->nColumn;
2293 iColumn = p->iCursorType - QUERY_FULLTEXT;
2294 if( iColumn<0 || iColumn>=nColumn ){
2295 iFirst = 0;
2296 iLast = nColumn-1;
2297 }else{
2298 iFirst = iColumn;
2299 iLast = iColumn;
2300 }
2301 for(i=iFirst; i<=iLast; i++){
2302 const char *zDoc;
2303 int nDoc;
2304 zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1);
2305 nDoc = sqlite3_column_bytes(p->pStmt, i+1);
2306 snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc);
2307 }
2308}
2309
2310/*
2311** Convert the information in the aMatch[] array of the snippet
2312** into the string zOffset[0..nOffset-1].
2313*/
2314static void snippetOffsetText(Snippet *p){
2315 int i;
2316 int cnt = 0;
2317 StringBuffer sb;
2318 char zBuf[200];
2319 if( p->zOffset ) return;
2320 initStringBuffer(&sb);
2321 for(i=0; i<p->nMatch; i++){
2322 struct snippetMatch *pMatch = &p->aMatch[i];
2323 zBuf[0] = ' ';
2324 sprintf(&zBuf[cnt>0], "%d %d %d %d", pMatch->iCol,
2325 pMatch->iTerm, pMatch->iStart, pMatch->nByte);
2326 append(&sb, zBuf);
2327 cnt++;
2328 }
2329 p->zOffset = sb.s;
2330 p->nOffset = sb.len;
2331}
2332
2333/*
2334** zDoc[0..nDoc-1] is phrase of text. aMatch[0..nMatch-1] are a set
2335** of matching words some of which might be in zDoc. zDoc is column
2336** number iCol.
2337**
2338** iBreak is suggested spot in zDoc where we could begin or end an
2339** excerpt. Return a value similar to iBreak but possibly adjusted
2340** to be a little left or right so that the break point is better.
2341*/
2342static int wordBoundary(
2343 int iBreak, /* The suggested break point */
2344 const char *zDoc, /* Document text */
2345 int nDoc, /* Number of bytes in zDoc[] */
2346 struct snippetMatch *aMatch, /* Matching words */
2347 int nMatch, /* Number of entries in aMatch[] */
2348 int iCol /* The column number for zDoc[] */
2349){
2350 int i;
2351 if( iBreak<=10 ){
2352 return 0;
2353 }
2354 if( iBreak>=nDoc-10 ){
2355 return nDoc;
2356 }
2357 for(i=0; i<nMatch && aMatch[i].iCol<iCol; i++){}
2358 while( i<nMatch && aMatch[i].iStart+aMatch[i].nByte<iBreak ){ i++; }
2359 if( i<nMatch ){
2360 if( aMatch[i].iStart<iBreak+10 ){
2361 return aMatch[i].iStart;
2362 }
2363 if( i>0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){
2364 return aMatch[i-1].iStart;
2365 }
2366 }
2367 for(i=1; i<=10; i++){
2368 if( safe_isspace(zDoc[iBreak-i]) ){
2369 return iBreak - i + 1;
2370 }
2371 if( safe_isspace(zDoc[iBreak+i]) ){
2372 return iBreak + i + 1;
2373 }
2374 }
2375 return iBreak;
2376}
2377
2378/*
2379** If the StringBuffer does not end in white space, add a single
2380** space character to the end.
2381*/
2382static void appendWhiteSpace(StringBuffer *p){
2383 if( p->len==0 ) return;
2384 if( safe_isspace(p->s[p->len-1]) ) return;
2385 append(p, " ");
2386}
2387
2388/*
2389** Remove white space from teh end of the StringBuffer
2390*/
2391static void trimWhiteSpace(StringBuffer *p){
2392 while( p->len>0 && safe_isspace(p->s[p->len-1]) ){
2393 p->len--;
2394 }
2395}
2396
2397
2398
2399/*
2400** Allowed values for Snippet.aMatch[].snStatus
2401*/
2402#define SNIPPET_IGNORE 0 /* It is ok to omit this match from the snippet */
2403#define SNIPPET_DESIRED 1 /* We want to include this match in the snippet */
2404
2405/*
2406** Generate the text of a snippet.
2407*/
2408static void snippetText(
2409 fulltext_cursor *pCursor, /* The cursor we need the snippet for */
2410 const char *zStartMark, /* Markup to appear before each match */
2411 const char *zEndMark, /* Markup to appear after each match */
2412 const char *zEllipsis /* Ellipsis mark */
2413){
2414 int i, j;
2415 struct snippetMatch *aMatch;
2416 int nMatch;
2417 int nDesired;
2418 StringBuffer sb;
2419 int tailCol;
2420 int tailOffset;
2421 int iCol;
2422 int nDoc;
2423 const char *zDoc;
2424 int iStart, iEnd;
2425 int tailEllipsis = 0;
2426 int iMatch;
2427
2428
2429 free(pCursor->snippet.zSnippet);
2430 pCursor->snippet.zSnippet = 0;
2431 aMatch = pCursor->snippet.aMatch;
2432 nMatch = pCursor->snippet.nMatch;
2433 initStringBuffer(&sb);
2434
2435 for(i=0; i<nMatch; i++){
2436 aMatch[i].snStatus = SNIPPET_IGNORE;
2437 }
2438 nDesired = 0;
2439 for(i=0; i<pCursor->q.nTerms; i++){
2440 for(j=0; j<nMatch; j++){
2441 if( aMatch[j].iTerm==i ){
2442 aMatch[j].snStatus = SNIPPET_DESIRED;
2443 nDesired++;
2444 break;
2445 }
2446 }
2447 }
2448
2449 iMatch = 0;
2450 tailCol = -1;
2451 tailOffset = 0;
2452 for(i=0; i<nMatch && nDesired>0; i++){
2453 if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue;
2454 nDesired--;
2455 iCol = aMatch[i].iCol;
2456 zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1);
2457 nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1);
2458 iStart = aMatch[i].iStart - 40;
2459 iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol);
2460 if( iStart<=10 ){
2461 iStart = 0;
2462 }
2463 if( iCol==tailCol && iStart<=tailOffset+20 ){
2464 iStart = tailOffset;
2465 }
2466 if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){
2467 trimWhiteSpace(&sb);
2468 appendWhiteSpace(&sb);
2469 append(&sb, zEllipsis);
2470 appendWhiteSpace(&sb);
2471 }
2472 iEnd = aMatch[i].iStart + aMatch[i].nByte + 40;
2473 iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol);
2474 if( iEnd>=nDoc-10 ){
2475 iEnd = nDoc;
2476 tailEllipsis = 0;
2477 }else{
2478 tailEllipsis = 1;
2479 }
2480 while( iMatch<nMatch && aMatch[iMatch].iCol<iCol ){ iMatch++; }
2481 while( iStart<iEnd ){
2482 while( iMatch<nMatch && aMatch[iMatch].iStart<iStart
2483 && aMatch[iMatch].iCol<=iCol ){
2484 iMatch++;
2485 }
2486 if( iMatch<nMatch && aMatch[iMatch].iStart<iEnd
2487 && aMatch[iMatch].iCol==iCol ){
2488 nappend(&sb, &zDoc[iStart], aMatch[iMatch].iStart - iStart);
2489 iStart = aMatch[iMatch].iStart;
2490 append(&sb, zStartMark);
2491 nappend(&sb, &zDoc[iStart], aMatch[iMatch].nByte);
2492 append(&sb, zEndMark);
2493 iStart += aMatch[iMatch].nByte;
2494 for(j=iMatch+1; j<nMatch; j++){
2495 if( aMatch[j].iTerm==aMatch[iMatch].iTerm
2496 && aMatch[j].snStatus==SNIPPET_DESIRED ){
2497 nDesired--;
2498 aMatch[j].snStatus = SNIPPET_IGNORE;
2499 }
2500 }
2501 }else{
2502 nappend(&sb, &zDoc[iStart], iEnd - iStart);
2503 iStart = iEnd;
2504 }
2505 }
2506 tailCol = iCol;
2507 tailOffset = iEnd;
2508 }
2509 trimWhiteSpace(&sb);
2510 if( tailEllipsis ){
2511 appendWhiteSpace(&sb);
2512 append(&sb, zEllipsis);
2513 }
2514 pCursor->snippet.zSnippet = sb.s;
2515 pCursor->snippet.nSnippet = sb.len;
2516}
2517
2518
2519/*
2520** Close the cursor. For additional information see the documentation
2521** on the xClose method of the virtual table interface.
2522*/
2523static int fulltextClose(sqlite3_vtab_cursor *pCursor){
2524 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2525 TRACE(("FTS1 Close %p\n", c));
2526 sqlite3_finalize(c->pStmt);
2527 queryClear(&c->q);
2528 snippetClear(&c->snippet);
2529 if( c->result.pDoclist!=NULL ){
2530 docListDelete(c->result.pDoclist);
2531 }
2532 free(c);
2533 return SQLITE_OK;
2534}
2535
2536static int fulltextNext(sqlite3_vtab_cursor *pCursor){
2537 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2538 sqlite_int64 iDocid;
2539 int rc;
2540
2541 TRACE(("FTS1 Next %p\n", pCursor));
2542 snippetClear(&c->snippet);
2543 if( c->iCursorType < QUERY_FULLTEXT ){
2544 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
2545 rc = sqlite3_step(c->pStmt);
2546 switch( rc ){
2547 case SQLITE_ROW:
2548 c->eof = 0;
2549 return SQLITE_OK;
2550 case SQLITE_DONE:
2551 c->eof = 1;
2552 return SQLITE_OK;
2553 default:
2554 c->eof = 1;
2555 return rc;
2556 }
2557 } else { /* full-text query */
2558 rc = sqlite3_reset(c->pStmt);
2559 if( rc!=SQLITE_OK ) return rc;
2560
2561 iDocid = nextDocid(&c->result);
2562 if( iDocid==0 ){
2563 c->eof = 1;
2564 return SQLITE_OK;
2565 }
2566 rc = sqlite3_bind_int64(c->pStmt, 1, iDocid);
2567 if( rc!=SQLITE_OK ) return rc;
2568 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
2569 rc = sqlite3_step(c->pStmt);
2570 if( rc==SQLITE_ROW ){ /* the case we expect */
2571 c->eof = 0;
2572 return SQLITE_OK;
2573 }
2574 /* an error occurred; abort */
2575 return rc==SQLITE_DONE ? SQLITE_ERROR : rc;
2576 }
2577}
2578
2579
2580/* Return a DocList corresponding to the query term *pTerm. If *pTerm
2581** is the first term of a phrase query, go ahead and evaluate the phrase
2582** query and return the doclist for the entire phrase query.
2583**
2584** The result is stored in pTerm->doclist.
2585*/
2586static int docListOfTerm(
2587 fulltext_vtab *v, /* The full text index */
2588 int iColumn, /* column to restrict to. No restrition if >=nColumn */
2589 QueryTerm *pQTerm, /* Term we are looking for, or 1st term of a phrase */
2590 DocList **ppResult /* Write the result here */
2591){
2592 DocList *pLeft, *pRight, *pNew;
2593 int i, rc;
2594
2595 pLeft = docListNew(DL_POSITIONS);
2596 rc = term_select_all(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pLeft);
2597 if( rc ){
2598 docListDelete(pLeft);
2599 return rc;
2600 }
2601 for(i=1; i<=pQTerm->nPhrase; i++){
2602 pRight = docListNew(DL_POSITIONS);
2603 rc = term_select_all(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, pRight);
2604 if( rc ){
2605 docListDelete(pLeft);
2606 return rc;
2607 }
2608 pNew = docListNew(i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS);
2609 docListPhraseMerge(pLeft, pRight, pNew);
2610 docListDelete(pLeft);
2611 docListDelete(pRight);
2612 pLeft = pNew;
2613 }
2614 *ppResult = pLeft;
2615 return SQLITE_OK;
2616}
2617
2618/* Add a new term pTerm[0..nTerm-1] to the query *q.
2619*/
2620static void queryAdd(Query *q, const char *pTerm, int nTerm){
2621 QueryTerm *t;
2622 ++q->nTerms;
2623 q->pTerms = realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0]));
2624 if( q->pTerms==0 ){
2625 q->nTerms = 0;
2626 return;
2627 }
2628 t = &q->pTerms[q->nTerms - 1];
2629 memset(t, 0, sizeof(*t));
2630 t->pTerm = malloc(nTerm+1);
2631 memcpy(t->pTerm, pTerm, nTerm);
2632 t->pTerm[nTerm] = 0;
2633 t->nTerm = nTerm;
2634 t->isOr = q->nextIsOr;
2635 q->nextIsOr = 0;
2636 t->iColumn = q->nextColumn;
2637 q->nextColumn = q->dfltColumn;
2638}
2639
2640/*
2641** Check to see if the string zToken[0...nToken-1] matches any
2642** column name in the virtual table. If it does,
2643** return the zero-indexed column number. If not, return -1.
2644*/
2645static int checkColumnSpecifier(
2646 fulltext_vtab *pVtab, /* The virtual table */
2647 const char *zToken, /* Text of the token */
2648 int nToken /* Number of characters in the token */
2649){
2650 int i;
2651 for(i=0; i<pVtab->nColumn; i++){
2652 if( memcmp(pVtab->azColumn[i], zToken, nToken)==0
2653 && pVtab->azColumn[i][nToken]==0 ){
2654 return i;
2655 }
2656 }
2657 return -1;
2658}
2659
2660/*
2661** Parse the text at pSegment[0..nSegment-1]. Add additional terms
2662** to the query being assemblied in pQuery.
2663**
2664** inPhrase is true if pSegment[0..nSegement-1] is contained within
2665** double-quotes. If inPhrase is true, then the first term
2666** is marked with the number of terms in the phrase less one and
2667** OR and "-" syntax is ignored. If inPhrase is false, then every
2668** term found is marked with nPhrase=0 and OR and "-" syntax is significant.
2669*/
2670static int tokenizeSegment(
2671 sqlite3_tokenizer *pTokenizer, /* The tokenizer to use */
2672 const char *pSegment, int nSegment, /* Query expression being parsed */
2673 int inPhrase, /* True if within "..." */
2674 Query *pQuery /* Append results here */
2675){
2676 const sqlite3_tokenizer_module *pModule = pTokenizer->pModule;
2677 sqlite3_tokenizer_cursor *pCursor;
2678 int firstIndex = pQuery->nTerms;
2679 int iCol;
2680 int nTerm = 1;
2681
2682 int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor);
2683 if( rc!=SQLITE_OK ) return rc;
2684 pCursor->pTokenizer = pTokenizer;
2685
2686 while( 1 ){
2687 const char *pToken;
2688 int nToken, iBegin, iEnd, iPos;
2689
2690 rc = pModule->xNext(pCursor,
2691 &pToken, &nToken,
2692 &iBegin, &iEnd, &iPos);
2693 if( rc!=SQLITE_OK ) break;
2694 if( !inPhrase &&
2695 pSegment[iEnd]==':' &&
2696 (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){
2697 pQuery->nextColumn = iCol;
2698 continue;
2699 }
2700 if( !inPhrase && pQuery->nTerms>0 && nToken==2
2701 && pSegment[iBegin]=='O' && pSegment[iBegin+1]=='R' ){
2702 pQuery->nextIsOr = 1;
2703 continue;
2704 }
2705 queryAdd(pQuery, pToken, nToken);
2706 if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){
2707 pQuery->pTerms[pQuery->nTerms-1].isNot = 1;
2708 }
2709 pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm;
2710 if( inPhrase ){
2711 nTerm++;
2712 }
2713 }
2714
2715 if( inPhrase && pQuery->nTerms>firstIndex ){
2716 pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1;
2717 }
2718
2719 return pModule->xClose(pCursor);
2720}
2721
2722/* Parse a query string, yielding a Query object pQuery.
2723**
2724** The calling function will need to queryClear() to clean up
2725** the dynamically allocated memory held by pQuery.
2726*/
2727static int parseQuery(
2728 fulltext_vtab *v, /* The fulltext index */
2729 const char *zInput, /* Input text of the query string */
2730 int nInput, /* Size of the input text */
2731 int dfltColumn, /* Default column of the index to match against */
2732 Query *pQuery /* Write the parse results here. */
2733){
2734 int iInput, inPhrase = 0;
2735
2736 if( zInput==0 ) nInput = 0;
2737 if( nInput<0 ) nInput = strlen(zInput);
2738 pQuery->nTerms = 0;
2739 pQuery->pTerms = NULL;
2740 pQuery->nextIsOr = 0;
2741 pQuery->nextColumn = dfltColumn;
2742 pQuery->dfltColumn = dfltColumn;
2743 pQuery->pFts = v;
2744
2745 for(iInput=0; iInput<nInput; ++iInput){
2746 int i;
2747 for(i=iInput; i<nInput && zInput[i]!='"'; ++i){}
2748 if( i>iInput ){
2749 tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase,
2750 pQuery);
2751 }
2752 iInput = i;
2753 if( i<nInput ){
2754 assert( zInput[i]=='"' );
2755 inPhrase = !inPhrase;
2756 }
2757 }
2758
2759 if( inPhrase ){
2760 /* unmatched quote */
2761 queryClear(pQuery);
2762 return SQLITE_ERROR;
2763 }
2764 return SQLITE_OK;
2765}
2766
2767/* Perform a full-text query using the search expression in
2768** zInput[0..nInput-1]. Return a list of matching documents
2769** in pResult.
2770**
2771** Queries must match column iColumn. Or if iColumn>=nColumn
2772** they are allowed to match against any column.
2773*/
2774static int fulltextQuery(
2775 fulltext_vtab *v, /* The full text index */
2776 int iColumn, /* Match against this column by default */
2777 const char *zInput, /* The query string */
2778 int nInput, /* Number of bytes in zInput[] */
2779 DocList **pResult, /* Write the result doclist here */
2780 Query *pQuery /* Put parsed query string here */
2781){
2782 int i, iNext, rc;
2783 DocList *pLeft = NULL;
2784 DocList *pRight, *pNew, *pOr;
2785 int nNot = 0;
2786 QueryTerm *aTerm;
2787
2788 rc = parseQuery(v, zInput, nInput, iColumn, pQuery);
2789 if( rc!=SQLITE_OK ) return rc;
2790
2791 /* Merge AND terms. */
2792 aTerm = pQuery->pTerms;
2793 for(i = 0; i<pQuery->nTerms; i=iNext){
2794 if( aTerm[i].isNot ){
2795 /* Handle all NOT terms in a separate pass */
2796 nNot++;
2797 iNext = i + aTerm[i].nPhrase+1;
2798 continue;
2799 }
2800 iNext = i + aTerm[i].nPhrase + 1;
2801 rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight);
2802 if( rc ){
2803 queryClear(pQuery);
2804 return rc;
2805 }
2806 while( iNext<pQuery->nTerms && aTerm[iNext].isOr ){
2807 rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &pOr);
2808 iNext += aTerm[iNext].nPhrase + 1;
2809 if( rc ){
2810 queryClear(pQuery);
2811 return rc;
2812 }
2813 pNew = docListNew(DL_DOCIDS);
2814 docListOrMerge(pRight, pOr, pNew);
2815 docListDelete(pRight);
2816 docListDelete(pOr);
2817 pRight = pNew;
2818 }
2819 if( pLeft==0 ){
2820 pLeft = pRight;
2821 }else{
2822 pNew = docListNew(DL_DOCIDS);
2823 docListAndMerge(pLeft, pRight, pNew);
2824 docListDelete(pRight);
2825 docListDelete(pLeft);
2826 pLeft = pNew;
2827 }
2828 }
2829
2830 if( nNot && pLeft==0 ){
2831 /* We do not yet know how to handle a query of only NOT terms */
2832 return SQLITE_ERROR;
2833 }
2834
2835 /* Do the EXCEPT terms */
2836 for(i=0; i<pQuery->nTerms; i += aTerm[i].nPhrase + 1){
2837 if( !aTerm[i].isNot ) continue;
2838 rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight);
2839 if( rc ){
2840 queryClear(pQuery);
2841 docListDelete(pLeft);
2842 return rc;
2843 }
2844 pNew = docListNew(DL_DOCIDS);
2845 docListExceptMerge(pLeft, pRight, pNew);
2846 docListDelete(pRight);
2847 docListDelete(pLeft);
2848 pLeft = pNew;
2849 }
2850
2851 *pResult = pLeft;
2852 return rc;
2853}
2854
2855/*
2856** This is the xFilter interface for the virtual table. See
2857** the virtual table xFilter method documentation for additional
2858** information.
2859**
2860** If idxNum==QUERY_GENERIC then do a full table scan against
2861** the %_content table.
2862**
2863** If idxNum==QUERY_ROWID then do a rowid lookup for a single entry
2864** in the %_content table.
2865**
2866** If idxNum>=QUERY_FULLTEXT then use the full text index. The
2867** column on the left-hand side of the MATCH operator is column
2868** number idxNum-QUERY_FULLTEXT, 0 indexed. argv[0] is the right-hand
2869** side of the MATCH operator.
2870*/
2871/* TODO(shess) Upgrade the cursor initialization and destruction to
2872** account for fulltextFilter() being called multiple times on the
2873** same cursor. The current solution is very fragile. Apply fix to
2874** fts2 as appropriate.
2875*/
2876static int fulltextFilter(
2877 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
2878 int idxNum, const char *idxStr, /* Which indexing scheme to use */
2879 int argc, sqlite3_value **argv /* Arguments for the indexing scheme */
2880){
2881 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2882 fulltext_vtab *v = cursor_vtab(c);
2883 int rc;
2884 char *zSql;
2885
2886 TRACE(("FTS1 Filter %p\n",pCursor));
2887
2888 zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
2889 idxNum==QUERY_GENERIC ? "" : "where rowid=?");
2890 sqlite3_finalize(c->pStmt);
2891 rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
2892 sqlite3_free(zSql);
2893 if( rc!=SQLITE_OK ) return rc;
2894
2895 c->iCursorType = idxNum;
2896 switch( idxNum ){
2897 case QUERY_GENERIC:
2898 break;
2899
2900 case QUERY_ROWID:
2901 rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0]));
2902 if( rc!=SQLITE_OK ) return rc;
2903 break;
2904
2905 default: /* full-text search */
2906 {
2907 const char *zQuery = (const char *)sqlite3_value_text(argv[0]);
2908 DocList *pResult;
2909 assert( idxNum<=QUERY_FULLTEXT+v->nColumn);
2910 assert( argc==1 );
2911 queryClear(&c->q);
2912 rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &pResult, &c->q);
2913 if( rc!=SQLITE_OK ) return rc;
2914 if( c->result.pDoclist!=NULL ) docListDelete(c->result.pDoclist);
2915 readerInit(&c->result, pResult);
2916 break;
2917 }
2918 }
2919
2920 return fulltextNext(pCursor);
2921}
2922
2923/* This is the xEof method of the virtual table. The SQLite core
2924** calls this routine to find out if it has reached the end of
2925** a query's results set.
2926*/
2927static int fulltextEof(sqlite3_vtab_cursor *pCursor){
2928 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2929 return c->eof;
2930}
2931
2932/* This is the xColumn method of the virtual table. The SQLite
2933** core calls this method during a query when it needs the value
2934** of a column from the virtual table. This method needs to use
2935** one of the sqlite3_result_*() routines to store the requested
2936** value back in the pContext.
2937*/
2938static int fulltextColumn(sqlite3_vtab_cursor *pCursor,
2939 sqlite3_context *pContext, int idxCol){
2940 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2941 fulltext_vtab *v = cursor_vtab(c);
2942
2943 if( idxCol<v->nColumn ){
2944 sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1);
2945 sqlite3_result_value(pContext, pVal);
2946 }else if( idxCol==v->nColumn ){
2947 /* The extra column whose name is the same as the table.
2948 ** Return a blob which is a pointer to the cursor
2949 */
2950 sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT);
2951 }
2952 return SQLITE_OK;
2953}
2954
2955/* This is the xRowid method. The SQLite core calls this routine to
2956** retrive the rowid for the current row of the result set. The
2957** rowid should be written to *pRowid.
2958*/
2959static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
2960 fulltext_cursor *c = (fulltext_cursor *) pCursor;
2961
2962 *pRowid = sqlite3_column_int64(c->pStmt, 0);
2963 return SQLITE_OK;
2964}
2965
2966/* Add all terms in [zText] to the given hash table. If [iColumn] > 0,
2967 * we also store positions and offsets in the hash table using the given
2968 * column number. */
2969static int buildTerms(fulltext_vtab *v, fts1Hash *terms, sqlite_int64 iDocid,
2970 const char *zText, int iColumn){
2971 sqlite3_tokenizer *pTokenizer = v->pTokenizer;
2972 sqlite3_tokenizer_cursor *pCursor;
2973 const char *pToken;
2974 int nTokenBytes;
2975 int iStartOffset, iEndOffset, iPosition;
2976 int rc;
2977
2978 rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor);
2979 if( rc!=SQLITE_OK ) return rc;
2980
2981 pCursor->pTokenizer = pTokenizer;
2982 while( SQLITE_OK==pTokenizer->pModule->xNext(pCursor,
2983 &pToken, &nTokenBytes,
2984 &iStartOffset, &iEndOffset,
2985 &iPosition) ){
2986 DocList *p;
2987
2988 /* Positions can't be negative; we use -1 as a terminator internally. */
2989 if( iPosition<0 ){
2990 pTokenizer->pModule->xClose(pCursor);
2991 return SQLITE_ERROR;
2992 }
2993
2994 p = fts1HashFind(terms, pToken, nTokenBytes);
2995 if( p==NULL ){
2996 p = docListNew(DL_DEFAULT);
2997 docListAddDocid(p, iDocid);
2998 fts1HashInsert(terms, pToken, nTokenBytes, p);
2999 }
3000 if( iColumn>=0 ){
3001 docListAddPosOffset(p, iColumn, iPosition, iStartOffset, iEndOffset);
3002 }
3003 }
3004
3005 /* TODO(shess) Check return? Should this be able to cause errors at
3006 ** this point? Actually, same question about sqlite3_finalize(),
3007 ** though one could argue that failure there means that the data is
3008 ** not durable. *ponder*
3009 */
3010 pTokenizer->pModule->xClose(pCursor);
3011 return rc;
3012}
3013
3014/* Update the %_terms table to map the term [pTerm] to the given rowid. */
3015static int index_insert_term(fulltext_vtab *v, const char *pTerm, int nTerm,
3016 DocList *d){
3017 sqlite_int64 iIndexRow;
3018 DocList doclist;
3019 int iSegment = 0, rc;
3020
3021 rc = term_select(v, pTerm, nTerm, iSegment, &iIndexRow, &doclist);
3022 if( rc==SQLITE_DONE ){
3023 docListInit(&doclist, DL_DEFAULT, 0, 0);
3024 docListUpdate(&doclist, d);
3025 /* TODO(shess) Consider length(doclist)>CHUNK_MAX? */
3026 rc = term_insert(v, NULL, pTerm, nTerm, iSegment, &doclist);
3027 goto err;
3028 }
3029 if( rc!=SQLITE_ROW ) return SQLITE_ERROR;
3030
3031 docListUpdate(&doclist, d);
3032 if( doclist.nData<=CHUNK_MAX ){
3033 rc = term_update(v, iIndexRow, &doclist);
3034 goto err;
3035 }
3036
3037 /* Doclist doesn't fit, delete what's there, and accumulate
3038 ** forward.
3039 */
3040 rc = term_delete(v, iIndexRow);
3041 if( rc!=SQLITE_OK ) goto err;
3042
3043 /* Try to insert the doclist into a higher segment bucket. On
3044 ** failure, accumulate existing doclist with the doclist from that
3045 ** bucket, and put results in the next bucket.
3046 */
3047 iSegment++;
3048 while( (rc=term_insert(v, &iIndexRow, pTerm, nTerm, iSegment,
3049 &doclist))!=SQLITE_OK ){
3050 sqlite_int64 iSegmentRow;
3051 DocList old;
3052 int rc2;
3053
3054 /* Retain old error in case the term_insert() error was really an
3055 ** error rather than a bounced insert.
3056 */
3057 rc2 = term_select(v, pTerm, nTerm, iSegment, &iSegmentRow, &old);
3058 if( rc2!=SQLITE_ROW ) goto err;
3059
3060 rc = term_delete(v, iSegmentRow);
3061 if( rc!=SQLITE_OK ) goto err;
3062
3063 /* Reusing lowest-number deleted row keeps the index smaller. */
3064 if( iSegmentRow<iIndexRow ) iIndexRow = iSegmentRow;
3065
3066 /* doclist contains the newer data, so accumulate it over old.
3067 ** Then steal accumulated data for doclist.
3068 */
3069 docListAccumulate(&old, &doclist);
3070 docListDestroy(&doclist);
3071 doclist = old;
3072
3073 iSegment++;
3074 }
3075
3076 err:
3077 docListDestroy(&doclist);
3078 return rc;
3079}
3080
3081/* Add doclists for all terms in [pValues] to the hash table [terms]. */
3082static int insertTerms(fulltext_vtab *v, fts1Hash *terms, sqlite_int64 iRowid,
3083 sqlite3_value **pValues){
3084 int i;
3085 for(i = 0; i < v->nColumn ; ++i){
3086 char *zText = (char*)sqlite3_value_text(pValues[i]);
3087 int rc = buildTerms(v, terms, iRowid, zText, i);
3088 if( rc!=SQLITE_OK ) return rc;
3089 }
3090 return SQLITE_OK;
3091}
3092
3093/* Add empty doclists for all terms in the given row's content to the hash
3094 * table [pTerms]. */
3095static int deleteTerms(fulltext_vtab *v, fts1Hash *pTerms, sqlite_int64 iRowid){
3096 const char **pValues;
3097 int i;
3098
3099 int rc = content_select(v, iRowid, &pValues);
3100 if( rc!=SQLITE_OK ) return rc;
3101
3102 for(i = 0 ; i < v->nColumn; ++i) {
3103 rc = buildTerms(v, pTerms, iRowid, pValues[i], -1);
3104 if( rc!=SQLITE_OK ) break;
3105 }
3106
3107 freeStringArray(v->nColumn, pValues);
3108 return SQLITE_OK;
3109}
3110
3111/* Insert a row into the %_content table; set *piRowid to be the ID of the
3112 * new row. Fill [pTerms] with new doclists for the %_term table. */
3113static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestRowid,
3114 sqlite3_value **pValues,
3115 sqlite_int64 *piRowid, fts1Hash *pTerms){
3116 int rc;
3117
3118 rc = content_insert(v, pRequestRowid, pValues); /* execute an SQL INSERT */
3119 if( rc!=SQLITE_OK ) return rc;
3120 *piRowid = sqlite3_last_insert_rowid(v->db);
3121 return insertTerms(v, pTerms, *piRowid, pValues);
3122}
3123
3124/* Delete a row from the %_content table; fill [pTerms] with empty doclists
3125 * to be written to the %_term table. */
3126static int index_delete(fulltext_vtab *v, sqlite_int64 iRow, fts1Hash *pTerms){
3127 int rc = deleteTerms(v, pTerms, iRow);
3128 if( rc!=SQLITE_OK ) return rc;
3129 return content_delete(v, iRow); /* execute an SQL DELETE */
3130}
3131
3132/* Update a row in the %_content table; fill [pTerms] with new doclists for the
3133 * %_term table. */
3134static int index_update(fulltext_vtab *v, sqlite_int64 iRow,
3135 sqlite3_value **pValues, fts1Hash *pTerms){
3136 /* Generate an empty doclist for each term that previously appeared in this
3137 * row. */
3138 int rc = deleteTerms(v, pTerms, iRow);
3139 if( rc!=SQLITE_OK ) return rc;
3140
3141 rc = content_update(v, pValues, iRow); /* execute an SQL UPDATE */
3142 if( rc!=SQLITE_OK ) return rc;
3143
3144 /* Now add positions for terms which appear in the updated row. */
3145 return insertTerms(v, pTerms, iRow, pValues);
3146}
3147
3148/* This function implements the xUpdate callback; it's the top-level entry
3149 * point for inserting, deleting or updating a row in a full-text table. */
3150static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg,
3151 sqlite_int64 *pRowid){
3152 fulltext_vtab *v = (fulltext_vtab *) pVtab;
3153 fts1Hash terms; /* maps term string -> PosList */
3154 int rc;
3155 fts1HashElem *e;
3156
3157 TRACE(("FTS1 Update %p\n", pVtab));
3158
3159 fts1HashInit(&terms, FTS1_HASH_STRING, 1);
3160
3161 if( nArg<2 ){
3162 rc = index_delete(v, sqlite3_value_int64(ppArg[0]), &terms);
3163 } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){
3164 /* An update:
3165 * ppArg[0] = old rowid
3166 * ppArg[1] = new rowid
3167 * ppArg[2..2+v->nColumn-1] = values
3168 * ppArg[2+v->nColumn] = value for magic column (we ignore this)
3169 */
3170 sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]);
3171 if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER ||
3172 sqlite3_value_int64(ppArg[1]) != rowid ){
3173 rc = SQLITE_ERROR; /* we don't allow changing the rowid */
3174 } else {
3175 assert( nArg==2+v->nColumn+1);
3176 rc = index_update(v, rowid, &ppArg[2], &terms);
3177 }
3178 } else {
3179 /* An insert:
3180 * ppArg[1] = requested rowid
3181 * ppArg[2..2+v->nColumn-1] = values
3182 * ppArg[2+v->nColumn] = value for magic column (we ignore this)
3183 */
3184 assert( nArg==2+v->nColumn+1);
3185 rc = index_insert(v, ppArg[1], &ppArg[2], pRowid, &terms);
3186 }
3187
3188 if( rc==SQLITE_OK ){
3189 /* Write updated doclists to disk. */
3190 for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){
3191 DocList *p = fts1HashData(e);
3192 rc = index_insert_term(v, fts1HashKey(e), fts1HashKeysize(e), p);
3193 if( rc!=SQLITE_OK ) break;
3194 }
3195 }
3196
3197 /* clean up */
3198 for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){
3199 DocList *p = fts1HashData(e);
3200 docListDelete(p);
3201 }
3202 fts1HashClear(&terms);
3203
3204 return rc;
3205}
3206
3207/*
3208** Implementation of the snippet() function for FTS1
3209*/
3210static void snippetFunc(
3211 sqlite3_context *pContext,
3212 int argc,
3213 sqlite3_value **argv
3214){
3215 fulltext_cursor *pCursor;
3216 if( argc<1 ) return;
3217 if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
3218 sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
3219 sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1);
3220 }else{
3221 const char *zStart = "<b>";
3222 const char *zEnd = "</b>";
3223 const char *zEllipsis = "<b>...</b>";
3224 memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
3225 if( argc>=2 ){
3226 zStart = (const char*)sqlite3_value_text(argv[1]);
3227 if( argc>=3 ){
3228 zEnd = (const char*)sqlite3_value_text(argv[2]);
3229 if( argc>=4 ){
3230 zEllipsis = (const char*)sqlite3_value_text(argv[3]);
3231 }
3232 }
3233 }
3234 snippetAllOffsets(pCursor);
3235 snippetText(pCursor, zStart, zEnd, zEllipsis);
3236 sqlite3_result_text(pContext, pCursor->snippet.zSnippet,
3237 pCursor->snippet.nSnippet, SQLITE_STATIC);
3238 }
3239}
3240
3241/*
3242** Implementation of the offsets() function for FTS1
3243*/
3244static void snippetOffsetsFunc(
3245 sqlite3_context *pContext,
3246 int argc,
3247 sqlite3_value **argv
3248){
3249 fulltext_cursor *pCursor;
3250 if( argc<1 ) return;
3251 if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
3252 sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
3253 sqlite3_result_error(pContext, "illegal first argument to offsets",-1);
3254 }else{
3255 memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
3256 snippetAllOffsets(pCursor);
3257 snippetOffsetText(&pCursor->snippet);
3258 sqlite3_result_text(pContext,
3259 pCursor->snippet.zOffset, pCursor->snippet.nOffset,
3260 SQLITE_STATIC);
3261 }
3262}
3263
3264/*
3265** This routine implements the xFindFunction method for the FTS1
3266** virtual table.
3267*/
3268static int fulltextFindFunction(
3269 sqlite3_vtab *pVtab,
3270 int nArg,
3271 const char *zName,
3272 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
3273 void **ppArg
3274){
3275 if( strcmp(zName,"snippet")==0 ){
3276 *pxFunc = snippetFunc;
3277 return 1;
3278 }else if( strcmp(zName,"offsets")==0 ){
3279 *pxFunc = snippetOffsetsFunc;
3280 return 1;
3281 }
3282 return 0;
3283}
3284
3285/*
3286** Rename an fts1 table.
3287*/
3288static int fulltextRename(
3289 sqlite3_vtab *pVtab,
3290 const char *zName
3291){
3292 fulltext_vtab *p = (fulltext_vtab *)pVtab;
3293 int rc = SQLITE_NOMEM;
3294 char *zSql = sqlite3_mprintf(
3295 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';"
3296 "ALTER TABLE %Q.'%q_term' RENAME TO '%q_term';"
3297 , p->zDb, p->zName, zName
3298 , p->zDb, p->zName, zName
3299 );
3300 if( zSql ){
3301 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
3302 sqlite3_free(zSql);
3303 }
3304 return rc;
3305}
3306
3307static const sqlite3_module fulltextModule = {
3308 /* iVersion */ 0,
3309 /* xCreate */ fulltextCreate,
3310 /* xConnect */ fulltextConnect,
3311 /* xBestIndex */ fulltextBestIndex,
3312 /* xDisconnect */ fulltextDisconnect,
3313 /* xDestroy */ fulltextDestroy,
3314 /* xOpen */ fulltextOpen,
3315 /* xClose */ fulltextClose,
3316 /* xFilter */ fulltextFilter,
3317 /* xNext */ fulltextNext,
3318 /* xEof */ fulltextEof,
3319 /* xColumn */ fulltextColumn,
3320 /* xRowid */ fulltextRowid,
3321 /* xUpdate */ fulltextUpdate,
3322 /* xBegin */ 0,
3323 /* xSync */ 0,
3324 /* xCommit */ 0,
3325 /* xRollback */ 0,
3326 /* xFindFunction */ fulltextFindFunction,
3327 /* xRename */ fulltextRename,
3328};
3329
3330int sqlite3Fts1Init(sqlite3 *db){
3331 sqlite3_overload_function(db, "snippet", -1);
3332 sqlite3_overload_function(db, "offsets", -1);
3333 return sqlite3_create_module(db, "fts1", &fulltextModule, 0);
3334}
3335
3336#if !SQLITE_CORE
3337int sqlite3_extension_init(sqlite3 *db, char **pzErrMsg,
3338 const sqlite3_api_routines *pApi){
3339 SQLITE_EXTENSION_INIT2(pApi)
3340 return sqlite3Fts1Init(db);
3341}
3342#endif
3343
3344#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */
diff --git a/libraries/sqlite/win32/fts1.h b/libraries/sqlite/win32/fts1.h
new file mode 100755
index 0000000..d55e689
--- /dev/null
+++ b/libraries/sqlite/win32/fts1.h
@@ -0,0 +1,11 @@
1#include "sqlite3.h"
2
3#ifdef __cplusplus
4extern "C" {
5#endif /* __cplusplus */
6
7int sqlite3Fts1Init(sqlite3 *db);
8
9#ifdef __cplusplus
10} /* extern "C" */
11#endif /* __cplusplus */
diff --git a/libraries/sqlite/win32/fts1_hash.c b/libraries/sqlite/win32/fts1_hash.c
new file mode 100755
index 0000000..463a52b
--- /dev/null
+++ b/libraries/sqlite/win32/fts1_hash.c
@@ -0,0 +1,369 @@
1/*
2** 2001 September 22
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.
10**
11*************************************************************************
12** This is the implementation of generic hash-tables used in SQLite.
13** We've modified it slightly to serve as a standalone hash table
14** implementation for the full-text indexing module.
15*/
16#include <assert.h>
17#include <stdlib.h>
18#include <string.h>
19
20/*
21** The code in this file is only compiled if:
22**
23** * The FTS1 module is being built as an extension
24** (in which case SQLITE_CORE is not defined), or
25**
26** * The FTS1 module is being built into the core of
27** SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
28*/
29#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
30
31
32#include "fts1_hash.h"
33
34static void *malloc_and_zero(int n){
35 void *p = malloc(n);
36 if( p ){
37 memset(p, 0, n);
38 }
39 return p;
40}
41
42/* Turn bulk memory into a hash table object by initializing the
43** fields of the Hash structure.
44**
45** "pNew" is a pointer to the hash table that is to be initialized.
46** keyClass is one of the constants
47** FTS1_HASH_BINARY or FTS1_HASH_STRING. The value of keyClass
48** determines what kind of key the hash table will use. "copyKey" is
49** true if the hash table should make its own private copy of keys and
50** false if it should just use the supplied pointer.
51*/
52void sqlite3Fts1HashInit(fts1Hash *pNew, int keyClass, int copyKey){
53 assert( pNew!=0 );
54 assert( keyClass>=FTS1_HASH_STRING && keyClass<=FTS1_HASH_BINARY );
55 pNew->keyClass = keyClass;
56 pNew->copyKey = copyKey;
57 pNew->first = 0;
58 pNew->count = 0;
59 pNew->htsize = 0;
60 pNew->ht = 0;
61 pNew->xMalloc = malloc_and_zero;
62 pNew->xFree = free;
63}
64
65/* Remove all entries from a hash table. Reclaim all memory.
66** Call this routine to delete a hash table or to reset a hash table
67** to the empty state.
68*/
69void sqlite3Fts1HashClear(fts1Hash *pH){
70 fts1HashElem *elem; /* For looping over all elements of the table */
71
72 assert( pH!=0 );
73 elem = pH->first;
74 pH->first = 0;
75 if( pH->ht ) pH->xFree(pH->ht);
76 pH->ht = 0;
77 pH->htsize = 0;
78 while( elem ){
79 fts1HashElem *next_elem = elem->next;
80 if( pH->copyKey && elem->pKey ){
81 pH->xFree(elem->pKey);
82 }
83 pH->xFree(elem);
84 elem = next_elem;
85 }
86 pH->count = 0;
87}
88
89/*
90** Hash and comparison functions when the mode is FTS1_HASH_STRING
91*/
92static int strHash(const void *pKey, int nKey){
93 const char *z = (const char *)pKey;
94 int h = 0;
95 if( nKey<=0 ) nKey = (int) strlen(z);
96 while( nKey > 0 ){
97 h = (h<<3) ^ h ^ *z++;
98 nKey--;
99 }
100 return h & 0x7fffffff;
101}
102static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
103 if( n1!=n2 ) return 1;
104 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
105}
106
107/*
108** Hash and comparison functions when the mode is FTS1_HASH_BINARY
109*/
110static int binHash(const void *pKey, int nKey){
111 int h = 0;
112 const char *z = (const char *)pKey;
113 while( nKey-- > 0 ){
114 h = (h<<3) ^ h ^ *(z++);
115 }
116 return h & 0x7fffffff;
117}
118static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
119 if( n1!=n2 ) return 1;
120 return memcmp(pKey1,pKey2,n1);
121}
122
123/*
124** Return a pointer to the appropriate hash function given the key class.
125**
126** The C syntax in this function definition may be unfamilar to some
127** programmers, so we provide the following additional explanation:
128**
129** The name of the function is "hashFunction". The function takes a
130** single parameter "keyClass". The return value of hashFunction()
131** is a pointer to another function. Specifically, the return value
132** of hashFunction() is a pointer to a function that takes two parameters
133** with types "const void*" and "int" and returns an "int".
134*/
135static int (*hashFunction(int keyClass))(const void*,int){
136 if( keyClass==FTS1_HASH_STRING ){
137 return &strHash;
138 }else{
139 assert( keyClass==FTS1_HASH_BINARY );
140 return &binHash;
141 }
142}
143
144/*
145** Return a pointer to the appropriate hash function given the key class.
146**
147** For help in interpreted the obscure C code in the function definition,
148** see the header comment on the previous function.
149*/
150static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
151 if( keyClass==FTS1_HASH_STRING ){
152 return &strCompare;
153 }else{
154 assert( keyClass==FTS1_HASH_BINARY );
155 return &binCompare;
156 }
157}
158
159/* Link an element into the hash table
160*/
161static void insertElement(
162 fts1Hash *pH, /* The complete hash table */
163 struct _fts1ht *pEntry, /* The entry into which pNew is inserted */
164 fts1HashElem *pNew /* The element to be inserted */
165){
166 fts1HashElem *pHead; /* First element already in pEntry */
167 pHead = pEntry->chain;
168 if( pHead ){
169 pNew->next = pHead;
170 pNew->prev = pHead->prev;
171 if( pHead->prev ){ pHead->prev->next = pNew; }
172 else { pH->first = pNew; }
173 pHead->prev = pNew;
174 }else{
175 pNew->next = pH->first;
176 if( pH->first ){ pH->first->prev = pNew; }
177 pNew->prev = 0;
178 pH->first = pNew;
179 }
180 pEntry->count++;
181 pEntry->chain = pNew;
182}
183
184
185/* Resize the hash table so that it cantains "new_size" buckets.
186** "new_size" must be a power of 2. The hash table might fail
187** to resize if sqliteMalloc() fails.
188*/
189static void rehash(fts1Hash *pH, int new_size){
190 struct _fts1ht *new_ht; /* The new hash table */
191 fts1HashElem *elem, *next_elem; /* For looping over existing elements */
192 int (*xHash)(const void*,int); /* The hash function */
193
194 assert( (new_size & (new_size-1))==0 );
195 new_ht = (struct _fts1ht *)pH->xMalloc( new_size*sizeof(struct _fts1ht) );
196 if( new_ht==0 ) return;
197 if( pH->ht ) pH->xFree(pH->ht);
198 pH->ht = new_ht;
199 pH->htsize = new_size;
200 xHash = hashFunction(pH->keyClass);
201 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
202 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
203 next_elem = elem->next;
204 insertElement(pH, &new_ht[h], elem);
205 }
206}
207
208/* This function (for internal use only) locates an element in an
209** hash table that matches the given key. The hash for this key has
210** already been computed and is passed as the 4th parameter.
211*/
212static fts1HashElem *findElementGivenHash(
213 const fts1Hash *pH, /* The pH to be searched */
214 const void *pKey, /* The key we are searching for */
215 int nKey,
216 int h /* The hash for this key. */
217){
218 fts1HashElem *elem; /* Used to loop thru the element list */
219 int count; /* Number of elements left to test */
220 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
221
222 if( pH->ht ){
223 struct _fts1ht *pEntry = &pH->ht[h];
224 elem = pEntry->chain;
225 count = pEntry->count;
226 xCompare = compareFunction(pH->keyClass);
227 while( count-- && elem ){
228 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
229 return elem;
230 }
231 elem = elem->next;
232 }
233 }
234 return 0;
235}
236
237/* Remove a single entry from the hash table given a pointer to that
238** element and a hash on the element's key.
239*/
240static void removeElementGivenHash(
241 fts1Hash *pH, /* The pH containing "elem" */
242 fts1HashElem* elem, /* The element to be removed from the pH */
243 int h /* Hash value for the element */
244){
245 struct _fts1ht *pEntry;
246 if( elem->prev ){
247 elem->prev->next = elem->next;
248 }else{
249 pH->first = elem->next;
250 }
251 if( elem->next ){
252 elem->next->prev = elem->prev;
253 }
254 pEntry = &pH->ht[h];
255 if( pEntry->chain==elem ){
256 pEntry->chain = elem->next;
257 }
258 pEntry->count--;
259 if( pEntry->count<=0 ){
260 pEntry->chain = 0;
261 }
262 if( pH->copyKey && elem->pKey ){
263 pH->xFree(elem->pKey);
264 }
265 pH->xFree( elem );
266 pH->count--;
267 if( pH->count<=0 ){
268 assert( pH->first==0 );
269 assert( pH->count==0 );
270 fts1HashClear(pH);
271 }
272}
273
274/* Attempt to locate an element of the hash table pH with a key
275** that matches pKey,nKey. Return the data for this element if it is
276** found, or NULL if there is no match.
277*/
278void *sqlite3Fts1HashFind(const fts1Hash *pH, const void *pKey, int nKey){
279 int h; /* A hash on key */
280 fts1HashElem *elem; /* The element that matches key */
281 int (*xHash)(const void*,int); /* The hash function */
282
283 if( pH==0 || pH->ht==0 ) return 0;
284 xHash = hashFunction(pH->keyClass);
285 assert( xHash!=0 );
286 h = (*xHash)(pKey,nKey);
287 assert( (pH->htsize & (pH->htsize-1))==0 );
288 elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
289 return elem ? elem->data : 0;
290}
291
292/* Insert an element into the hash table pH. The key is pKey,nKey
293** and the data is "data".
294**
295** If no element exists with a matching key, then a new
296** element is created. A copy of the key is made if the copyKey
297** flag is set. NULL is returned.
298**
299** If another element already exists with the same key, then the
300** new data replaces the old data and the old data is returned.
301** The key is not copied in this instance. If a malloc fails, then
302** the new data is returned and the hash table is unchanged.
303**
304** If the "data" parameter to this function is NULL, then the
305** element corresponding to "key" is removed from the hash table.
306*/
307void *sqlite3Fts1HashInsert(
308 fts1Hash *pH, /* The hash table to insert into */
309 const void *pKey, /* The key */
310 int nKey, /* Number of bytes in the key */
311 void *data /* The data */
312){
313 int hraw; /* Raw hash value of the key */
314 int h; /* the hash of the key modulo hash table size */
315 fts1HashElem *elem; /* Used to loop thru the element list */
316 fts1HashElem *new_elem; /* New element added to the pH */
317 int (*xHash)(const void*,int); /* The hash function */
318
319 assert( pH!=0 );
320 xHash = hashFunction(pH->keyClass);
321 assert( xHash!=0 );
322 hraw = (*xHash)(pKey, nKey);
323 assert( (pH->htsize & (pH->htsize-1))==0 );
324 h = hraw & (pH->htsize-1);
325 elem = findElementGivenHash(pH,pKey,nKey,h);
326 if( elem ){
327 void *old_data = elem->data;
328 if( data==0 ){
329 removeElementGivenHash(pH,elem,h);
330 }else{
331 elem->data = data;
332 }
333 return old_data;
334 }
335 if( data==0 ) return 0;
336 new_elem = (fts1HashElem*)pH->xMalloc( sizeof(fts1HashElem) );
337 if( new_elem==0 ) return data;
338 if( pH->copyKey && pKey!=0 ){
339 new_elem->pKey = pH->xMalloc( nKey );
340 if( new_elem->pKey==0 ){
341 pH->xFree(new_elem);
342 return data;
343 }
344 memcpy((void*)new_elem->pKey, pKey, nKey);
345 }else{
346 new_elem->pKey = (void*)pKey;
347 }
348 new_elem->nKey = nKey;
349 pH->count++;
350 if( pH->htsize==0 ){
351 rehash(pH,8);
352 if( pH->htsize==0 ){
353 pH->count = 0;
354 pH->xFree(new_elem);
355 return data;
356 }
357 }
358 if( pH->count > pH->htsize ){
359 rehash(pH,pH->htsize*2);
360 }
361 assert( pH->htsize>0 );
362 assert( (pH->htsize & (pH->htsize-1))==0 );
363 h = hraw & (pH->htsize-1);
364 insertElement(pH, &pH->ht[h], new_elem);
365 new_elem->data = data;
366 return 0;
367}
368
369#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */
diff --git a/libraries/sqlite/win32/fts1_hash.h b/libraries/sqlite/win32/fts1_hash.h
new file mode 100755
index 0000000..c31c430
--- /dev/null
+++ b/libraries/sqlite/win32/fts1_hash.h
@@ -0,0 +1,112 @@
1/*
2** 2001 September 22
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.
10**
11*************************************************************************
12** This is the header file for the generic hash-table implemenation
13** used in SQLite. We've modified it slightly to serve as a standalone
14** hash table implementation for the full-text indexing module.
15**
16*/
17#ifndef _FTS1_HASH_H_
18#define _FTS1_HASH_H_
19
20/* Forward declarations of structures. */
21typedef struct fts1Hash fts1Hash;
22typedef struct fts1HashElem fts1HashElem;
23
24/* A complete hash table is an instance of the following structure.
25** The internals of this structure are intended to be opaque -- client
26** code should not attempt to access or modify the fields of this structure
27** directly. Change this structure only by using the routines below.
28** However, many of the "procedures" and "functions" for modifying and
29** accessing this structure are really macros, so we can't really make
30** this structure opaque.
31*/
32struct fts1Hash {
33 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
34 char copyKey; /* True if copy of key made on insert */
35 int count; /* Number of entries in this table */
36 fts1HashElem *first; /* The first element of the array */
37 void *(*xMalloc)(int); /* malloc() function to use */
38 void (*xFree)(void *); /* free() function to use */
39 int htsize; /* Number of buckets in the hash table */
40 struct _fts1ht { /* the hash table */
41 int count; /* Number of entries with this hash */
42 fts1HashElem *chain; /* Pointer to first entry with this hash */
43 } *ht;
44};
45
46/* Each element in the hash table is an instance of the following
47** structure. All elements are stored on a single doubly-linked list.
48**
49** Again, this structure is intended to be opaque, but it can't really
50** be opaque because it is used by macros.
51*/
52struct fts1HashElem {
53 fts1HashElem *next, *prev; /* Next and previous elements in the table */
54 void *data; /* Data associated with this element */
55 void *pKey; int nKey; /* Key associated with this element */
56};
57
58/*
59** There are 2 different modes of operation for a hash table:
60**
61** FTS1_HASH_STRING pKey points to a string that is nKey bytes long
62** (including the null-terminator, if any). Case
63** is respected in comparisons.
64**
65** FTS1_HASH_BINARY pKey points to binary data nKey bytes long.
66** memcmp() is used to compare keys.
67**
68** A copy of the key is made if the copyKey parameter to fts1HashInit is 1.
69*/
70#define FTS1_HASH_STRING 1
71#define FTS1_HASH_BINARY 2
72
73/*
74** Access routines. To delete, insert a NULL pointer.
75*/
76void sqlite3Fts1HashInit(fts1Hash*, int keytype, int copyKey);
77void *sqlite3Fts1HashInsert(fts1Hash*, const void *pKey, int nKey, void *pData);
78void *sqlite3Fts1HashFind(const fts1Hash*, const void *pKey, int nKey);
79void sqlite3Fts1HashClear(fts1Hash*);
80
81/*
82** Shorthand for the functions above
83*/
84#define fts1HashInit sqlite3Fts1HashInit
85#define fts1HashInsert sqlite3Fts1HashInsert
86#define fts1HashFind sqlite3Fts1HashFind
87#define fts1HashClear sqlite3Fts1HashClear
88
89/*
90** Macros for looping over all elements of a hash table. The idiom is
91** like this:
92**
93** fts1Hash h;
94** fts1HashElem *p;
95** ...
96** for(p=fts1HashFirst(&h); p; p=fts1HashNext(p)){
97** SomeStructure *pData = fts1HashData(p);
98** // do something with pData
99** }
100*/
101#define fts1HashFirst(H) ((H)->first)
102#define fts1HashNext(E) ((E)->next)
103#define fts1HashData(E) ((E)->data)
104#define fts1HashKey(E) ((E)->pKey)
105#define fts1HashKeysize(E) ((E)->nKey)
106
107/*
108** Number of entries in a hash table
109*/
110#define fts1HashCount(H) ((H)->count)
111
112#endif /* _FTS1_HASH_H_ */
diff --git a/libraries/sqlite/win32/fts1_porter.c b/libraries/sqlite/win32/fts1_porter.c
new file mode 100755
index 0000000..1d26236
--- /dev/null
+++ b/libraries/sqlite/win32/fts1_porter.c
@@ -0,0 +1,643 @@
1/*
2** 2006 September 30
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.
10**
11*************************************************************************
12** Implementation of the full-text-search tokenizer that implements
13** a Porter stemmer.
14*/
15
16/*
17** The code in this file is only compiled if:
18**
19** * The FTS1 module is being built as an extension
20** (in which case SQLITE_CORE is not defined), or
21**
22** * The FTS1 module is being built into the core of
23** SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
24*/
25#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
26
27
28#include <assert.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <ctype.h>
33
34#include "fts1_tokenizer.h"
35
36/*
37** Class derived from sqlite3_tokenizer
38*/
39typedef struct porter_tokenizer {
40 sqlite3_tokenizer base; /* Base class */
41} porter_tokenizer;
42
43/*
44** Class derived from sqlit3_tokenizer_cursor
45*/
46typedef struct porter_tokenizer_cursor {
47 sqlite3_tokenizer_cursor base;
48 const char *zInput; /* input we are tokenizing */
49 int nInput; /* size of the input */
50 int iOffset; /* current position in zInput */
51 int iToken; /* index of next token to be returned */
52 char *zToken; /* storage for current token */
53 int nAllocated; /* space allocated to zToken buffer */
54} porter_tokenizer_cursor;
55
56
57/* Forward declaration */
58static const sqlite3_tokenizer_module porterTokenizerModule;
59
60
61/*
62** Create a new tokenizer instance.
63*/
64static int porterCreate(
65 int argc, const char * const *argv,
66 sqlite3_tokenizer **ppTokenizer
67){
68 porter_tokenizer *t;
69 t = (porter_tokenizer *) calloc(sizeof(*t), 1);
70 if( t==NULL ) return SQLITE_NOMEM;
71
72 *ppTokenizer = &t->base;
73 return SQLITE_OK;
74}
75
76/*
77** Destroy a tokenizer
78*/
79static int porterDestroy(sqlite3_tokenizer *pTokenizer){
80 free(pTokenizer);
81 return SQLITE_OK;
82}
83
84/*
85** Prepare to begin tokenizing a particular string. The input
86** string to be tokenized is zInput[0..nInput-1]. A cursor
87** used to incrementally tokenize this string is returned in
88** *ppCursor.
89*/
90static int porterOpen(
91 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
92 const char *zInput, int nInput, /* String to be tokenized */
93 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
94){
95 porter_tokenizer_cursor *c;
96
97 c = (porter_tokenizer_cursor *) malloc(sizeof(*c));
98 if( c==NULL ) return SQLITE_NOMEM;
99
100 c->zInput = zInput;
101 if( zInput==0 ){
102 c->nInput = 0;
103 }else if( nInput<0 ){
104 c->nInput = (int)strlen(zInput);
105 }else{
106 c->nInput = nInput;
107 }
108 c->iOffset = 0; /* start tokenizing at the beginning */
109 c->iToken = 0;
110 c->zToken = NULL; /* no space allocated, yet. */
111 c->nAllocated = 0;
112
113 *ppCursor = &c->base;
114 return SQLITE_OK;
115}
116
117/*
118** Close a tokenization cursor previously opened by a call to
119** porterOpen() above.
120*/
121static int porterClose(sqlite3_tokenizer_cursor *pCursor){
122 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
123 free(c->zToken);
124 free(c);
125 return SQLITE_OK;
126}
127/*
128** Vowel or consonant
129*/
130static const char cType[] = {
131 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
132 1, 1, 1, 2, 1
133};
134
135/*
136** isConsonant() and isVowel() determine if their first character in
137** the string they point to is a consonant or a vowel, according
138** to Porter ruls.
139**
140** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
141** 'Y' is a consonant unless it follows another consonant,
142** in which case it is a vowel.
143**
144** In these routine, the letters are in reverse order. So the 'y' rule
145** is that 'y' is a consonant unless it is followed by another
146** consonent.
147*/
148static int isVowel(const char*);
149static int isConsonant(const char *z){
150 int j;
151 char x = *z;
152 if( x==0 ) return 0;
153 assert( x>='a' && x<='z' );
154 j = cType[x-'a'];
155 if( j<2 ) return j;
156 return z[1]==0 || isVowel(z + 1);
157}
158static int isVowel(const char *z){
159 int j;
160 char x = *z;
161 if( x==0 ) return 0;
162 assert( x>='a' && x<='z' );
163 j = cType[x-'a'];
164 if( j<2 ) return 1-j;
165 return isConsonant(z + 1);
166}
167
168/*
169** Let any sequence of one or more vowels be represented by V and let
170** C be sequence of one or more consonants. Then every word can be
171** represented as:
172**
173** [C] (VC){m} [V]
174**
175** In prose: A word is an optional consonant followed by zero or
176** vowel-consonant pairs followed by an optional vowel. "m" is the
177** number of vowel consonant pairs. This routine computes the value
178** of m for the first i bytes of a word.
179**
180** Return true if the m-value for z is 1 or more. In other words,
181** return true if z contains at least one vowel that is followed
182** by a consonant.
183**
184** In this routine z[] is in reverse order. So we are really looking
185** for an instance of of a consonant followed by a vowel.
186*/
187static int m_gt_0(const char *z){
188 while( isVowel(z) ){ z++; }
189 if( *z==0 ) return 0;
190 while( isConsonant(z) ){ z++; }
191 return *z!=0;
192}
193
194/* Like mgt0 above except we are looking for a value of m which is
195** exactly 1
196*/
197static int m_eq_1(const char *z){
198 while( isVowel(z) ){ z++; }
199 if( *z==0 ) return 0;
200 while( isConsonant(z) ){ z++; }
201 if( *z==0 ) return 0;
202 while( isVowel(z) ){ z++; }
203 if( *z==0 ) return 1;
204 while( isConsonant(z) ){ z++; }
205 return *z==0;
206}
207
208/* Like mgt0 above except we are looking for a value of m>1 instead
209** or m>0
210*/
211static int m_gt_1(const char *z){
212 while( isVowel(z) ){ z++; }
213 if( *z==0 ) return 0;
214 while( isConsonant(z) ){ z++; }
215 if( *z==0 ) return 0;
216 while( isVowel(z) ){ z++; }
217 if( *z==0 ) return 0;
218 while( isConsonant(z) ){ z++; }
219 return *z!=0;
220}
221
222/*
223** Return TRUE if there is a vowel anywhere within z[0..n-1]
224*/
225static int hasVowel(const char *z){
226 while( isConsonant(z) ){ z++; }
227 return *z!=0;
228}
229
230/*
231** Return TRUE if the word ends in a double consonant.
232**
233** The text is reversed here. So we are really looking at
234** the first two characters of z[].
235*/
236static int doubleConsonant(const char *z){
237 return isConsonant(z) && z[0]==z[1] && isConsonant(z+1);
238}
239
240/*
241** Return TRUE if the word ends with three letters which
242** are consonant-vowel-consonent and where the final consonant
243** is not 'w', 'x', or 'y'.
244**
245** The word is reversed here. So we are really checking the
246** first three letters and the first one cannot be in [wxy].
247*/
248static int star_oh(const char *z){
249 return
250 z[0]!=0 && isConsonant(z) &&
251 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
252 z[1]!=0 && isVowel(z+1) &&
253 z[2]!=0 && isConsonant(z+2);
254}
255
256/*
257** If the word ends with zFrom and xCond() is true for the stem
258** of the word that preceeds the zFrom ending, then change the
259** ending to zTo.
260**
261** The input word *pz and zFrom are both in reverse order. zTo
262** is in normal order.
263**
264** Return TRUE if zFrom matches. Return FALSE if zFrom does not
265** match. Not that TRUE is returned even if xCond() fails and
266** no substitution occurs.
267*/
268static int stem(
269 char **pz, /* The word being stemmed (Reversed) */
270 const char *zFrom, /* If the ending matches this... (Reversed) */
271 const char *zTo, /* ... change the ending to this (not reversed) */
272 int (*xCond)(const char*) /* Condition that must be true */
273){
274 char *z = *pz;
275 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
276 if( *zFrom!=0 ) return 0;
277 if( xCond && !xCond(z) ) return 1;
278 while( *zTo ){
279 *(--z) = *(zTo++);
280 }
281 *pz = z;
282 return 1;
283}
284
285/*
286** This is the fallback stemmer used when the porter stemmer is
287** inappropriate. The input word is copied into the output with
288** US-ASCII case folding. If the input word is too long (more
289** than 20 bytes if it contains no digits or more than 6 bytes if
290** it contains digits) then word is truncated to 20 or 6 bytes
291** by taking 10 or 3 bytes from the beginning and end.
292*/
293static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
294 int i, mx, j;
295 int hasDigit = 0;
296 for(i=0; i<nIn; i++){
297 int c = zIn[i];
298 if( c>='A' && c<='Z' ){
299 zOut[i] = c - 'A' + 'a';
300 }else{
301 if( c>='0' && c<='9' ) hasDigit = 1;
302 zOut[i] = c;
303 }
304 }
305 mx = hasDigit ? 3 : 10;
306 if( nIn>mx*2 ){
307 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
308 zOut[j] = zOut[i];
309 }
310 i = j;
311 }
312 zOut[i] = 0;
313 *pnOut = i;
314}
315
316
317/*
318** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
319** zOut is at least big enough to hold nIn bytes. Write the actual
320** size of the output word (exclusive of the '\0' terminator) into *pnOut.
321**
322** Any upper-case characters in the US-ASCII character set ([A-Z])
323** are converted to lower case. Upper-case UTF characters are
324** unchanged.
325**
326** Words that are longer than about 20 bytes are stemmed by retaining
327** a few bytes from the beginning and the end of the word. If the
328** word contains digits, 3 bytes are taken from the beginning and
329** 3 bytes from the end. For long words without digits, 10 bytes
330** are taken from each end. US-ASCII case folding still applies.
331**
332** If the input word contains not digits but does characters not
333** in [a-zA-Z] then no stemming is attempted and this routine just
334** copies the input into the input into the output with US-ASCII
335** case folding.
336**
337** Stemming never increases the length of the word. So there is
338** no chance of overflowing the zOut buffer.
339*/
340static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
341 int i, j, c;
342 char zReverse[28];
343 char *z, *z2;
344 if( nIn<3 || nIn>=sizeof(zReverse)-7 ){
345 /* The word is too big or too small for the porter stemmer.
346 ** Fallback to the copy stemmer */
347 copy_stemmer(zIn, nIn, zOut, pnOut);
348 return;
349 }
350 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
351 c = zIn[i];
352 if( c>='A' && c<='Z' ){
353 zReverse[j] = c + 'a' - 'A';
354 }else if( c>='a' && c<='z' ){
355 zReverse[j] = c;
356 }else{
357 /* The use of a character not in [a-zA-Z] means that we fallback
358 ** to the copy stemmer */
359 copy_stemmer(zIn, nIn, zOut, pnOut);
360 return;
361 }
362 }
363 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
364 z = &zReverse[j+1];
365
366
367 /* Step 1a */
368 if( z[0]=='s' ){
369 if(
370 !stem(&z, "sess", "ss", 0) &&
371 !stem(&z, "sei", "i", 0) &&
372 !stem(&z, "ss", "ss", 0)
373 ){
374 z++;
375 }
376 }
377
378 /* Step 1b */
379 z2 = z;
380 if( stem(&z, "dee", "ee", m_gt_0) ){
381 /* Do nothing. The work was all in the test */
382 }else if(
383 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
384 && z!=z2
385 ){
386 if( stem(&z, "ta", "ate", 0) ||
387 stem(&z, "lb", "ble", 0) ||
388 stem(&z, "zi", "ize", 0) ){
389 /* Do nothing. The work was all in the test */
390 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
391 z++;
392 }else if( m_eq_1(z) && star_oh(z) ){
393 *(--z) = 'e';
394 }
395 }
396
397 /* Step 1c */
398 if( z[0]=='y' && hasVowel(z+1) ){
399 z[0] = 'i';
400 }
401
402 /* Step 2 */
403 switch( z[1] ){
404 case 'a':
405 stem(&z, "lanoita", "ate", m_gt_0) ||
406 stem(&z, "lanoit", "tion", m_gt_0);
407 break;
408 case 'c':
409 stem(&z, "icne", "ence", m_gt_0) ||
410 stem(&z, "icna", "ance", m_gt_0);
411 break;
412 case 'e':
413 stem(&z, "rezi", "ize", m_gt_0);
414 break;
415 case 'g':
416 stem(&z, "igol", "log", m_gt_0);
417 break;
418 case 'l':
419 stem(&z, "ilb", "ble", m_gt_0) ||
420 stem(&z, "illa", "al", m_gt_0) ||
421 stem(&z, "iltne", "ent", m_gt_0) ||
422 stem(&z, "ile", "e", m_gt_0) ||
423 stem(&z, "ilsuo", "ous", m_gt_0);
424 break;
425 case 'o':
426 stem(&z, "noitazi", "ize", m_gt_0) ||
427 stem(&z, "noita", "ate", m_gt_0) ||
428 stem(&z, "rota", "ate", m_gt_0);
429 break;
430 case 's':
431 stem(&z, "msila", "al", m_gt_0) ||
432 stem(&z, "ssenevi", "ive", m_gt_0) ||
433 stem(&z, "ssenluf", "ful", m_gt_0) ||
434 stem(&z, "ssensuo", "ous", m_gt_0);
435 break;
436 case 't':
437 stem(&z, "itila", "al", m_gt_0) ||
438 stem(&z, "itivi", "ive", m_gt_0) ||
439 stem(&z, "itilib", "ble", m_gt_0);
440 break;
441 }
442
443 /* Step 3 */
444 switch( z[0] ){
445 case 'e':
446 stem(&z, "etaci", "ic", m_gt_0) ||
447 stem(&z, "evita", "", m_gt_0) ||
448 stem(&z, "ezila", "al", m_gt_0);
449 break;
450 case 'i':
451 stem(&z, "itici", "ic", m_gt_0);
452 break;
453 case 'l':
454 stem(&z, "laci", "ic", m_gt_0) ||
455 stem(&z, "luf", "", m_gt_0);
456 break;
457 case 's':
458 stem(&z, "ssen", "", m_gt_0);
459 break;
460 }
461
462 /* Step 4 */
463 switch( z[1] ){
464 case 'a':
465 if( z[0]=='l' && m_gt_1(z+2) ){
466 z += 2;
467 }
468 break;
469 case 'c':
470 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
471 z += 4;
472 }
473 break;
474 case 'e':
475 if( z[0]=='r' && m_gt_1(z+2) ){
476 z += 2;
477 }
478 break;
479 case 'i':
480 if( z[0]=='c' && m_gt_1(z+2) ){
481 z += 2;
482 }
483 break;
484 case 'l':
485 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
486 z += 4;
487 }
488 break;
489 case 'n':
490 if( z[0]=='t' ){
491 if( z[2]=='a' ){
492 if( m_gt_1(z+3) ){
493 z += 3;
494 }
495 }else if( z[2]=='e' ){
496 stem(&z, "tneme", "", m_gt_1) ||
497 stem(&z, "tnem", "", m_gt_1) ||
498 stem(&z, "tne", "", m_gt_1);
499 }
500 }
501 break;
502 case 'o':
503 if( z[0]=='u' ){
504 if( m_gt_1(z+2) ){
505 z += 2;
506 }
507 }else if( z[3]=='s' || z[3]=='t' ){
508 stem(&z, "noi", "", m_gt_1);
509 }
510 break;
511 case 's':
512 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
513 z += 3;
514 }
515 break;
516 case 't':
517 stem(&z, "eta", "", m_gt_1) ||
518 stem(&z, "iti", "", m_gt_1);
519 break;
520 case 'u':
521 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
522 z += 3;
523 }
524 break;
525 case 'v':
526 case 'z':
527 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
528 z += 3;
529 }
530 break;
531 }
532
533 /* Step 5a */
534 if( z[0]=='e' ){
535 if( m_gt_1(z+1) ){
536 z++;
537 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
538 z++;
539 }
540 }
541
542 /* Step 5b */
543 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
544 z++;
545 }
546
547 /* z[] is now the stemmed word in reverse order. Flip it back
548 ** around into forward order and return.
549 */
550 *pnOut = i = strlen(z);
551 zOut[i] = 0;
552 while( *z ){
553 zOut[--i] = *(z++);
554 }
555}
556
557/*
558** Characters that can be part of a token. We assume any character
559** whose value is greater than 0x80 (any UTF character) can be
560** part of a token. In other words, delimiters all must have
561** values of 0x7f or lower.
562*/
563static const char isIdChar[] = {
564/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
565 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
566 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
568 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
570};
571#define idChar(C) (((ch=C)&0x80)!=0 || (ch>0x2f && isIdChar[ch-0x30]))
572#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !isIdChar[ch-0x30]))
573
574/*
575** Extract the next token from a tokenization cursor. The cursor must
576** have been opened by a prior call to porterOpen().
577*/
578static int porterNext(
579 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
580 const char **pzToken, /* OUT: *pzToken is the token text */
581 int *pnBytes, /* OUT: Number of bytes in token */
582 int *piStartOffset, /* OUT: Starting offset of token */
583 int *piEndOffset, /* OUT: Ending offset of token */
584 int *piPosition /* OUT: Position integer of token */
585){
586 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
587 const char *z = c->zInput;
588
589 while( c->iOffset<c->nInput ){
590 int iStartOffset, ch;
591
592 /* Scan past delimiter characters */
593 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
594 c->iOffset++;
595 }
596
597 /* Count non-delimiter characters. */
598 iStartOffset = c->iOffset;
599 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
600 c->iOffset++;
601 }
602
603 if( c->iOffset>iStartOffset ){
604 int n = c->iOffset-iStartOffset;
605 if( n>c->nAllocated ){
606 c->nAllocated = n+20;
607 c->zToken = realloc(c->zToken, c->nAllocated);
608 if( c->zToken==NULL ) return SQLITE_NOMEM;
609 }
610 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
611 *pzToken = c->zToken;
612 *piStartOffset = iStartOffset;
613 *piEndOffset = c->iOffset;
614 *piPosition = c->iToken++;
615 return SQLITE_OK;
616 }
617 }
618 return SQLITE_DONE;
619}
620
621/*
622** The set of routines that implement the porter-stemmer tokenizer
623*/
624static const sqlite3_tokenizer_module porterTokenizerModule = {
625 0,
626 porterCreate,
627 porterDestroy,
628 porterOpen,
629 porterClose,
630 porterNext,
631};
632
633/*
634** Allocate a new porter tokenizer. Return a pointer to the new
635** tokenizer in *ppModule
636*/
637void sqlite3Fts1PorterTokenizerModule(
638 sqlite3_tokenizer_module const**ppModule
639){
640 *ppModule = &porterTokenizerModule;
641}
642
643#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */
diff --git a/libraries/sqlite/win32/fts1_tokenizer.h b/libraries/sqlite/win32/fts1_tokenizer.h
new file mode 100755
index 0000000..a48cb74
--- /dev/null
+++ b/libraries/sqlite/win32/fts1_tokenizer.h
@@ -0,0 +1,90 @@
1/*
2** 2006 July 10
3**
4** The author disclaims copyright to this source code.
5**
6*************************************************************************
7** Defines the interface to tokenizers used by fulltext-search. There
8** are three basic components:
9**
10** sqlite3_tokenizer_module is a singleton defining the tokenizer
11** interface functions. This is essentially the class structure for
12** tokenizers.
13**
14** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
15** including customization information defined at creation time.
16**
17** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
18** tokens from a particular input.
19*/
20#ifndef _FTS1_TOKENIZER_H_
21#define _FTS1_TOKENIZER_H_
22
23/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
24** If tokenizers are to be allowed to call sqlite3_*() functions, then
25** we will need a way to register the API consistently.
26*/
27#include "sqlite3.h"
28
29/*
30** Structures used by the tokenizer interface.
31*/
32typedef struct sqlite3_tokenizer sqlite3_tokenizer;
33typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
34typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
35
36struct sqlite3_tokenizer_module {
37 int iVersion; /* currently 0 */
38
39 /*
40 ** Create and destroy a tokenizer. argc/argv are passed down from
41 ** the fulltext virtual table creation to allow customization.
42 */
43 int (*xCreate)(int argc, const char *const*argv,
44 sqlite3_tokenizer **ppTokenizer);
45 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
46
47 /*
48 ** Tokenize a particular input. Call xOpen() to prepare to
49 ** tokenize, xNext() repeatedly until it returns SQLITE_DONE, then
50 ** xClose() to free any internal state. The pInput passed to
51 ** xOpen() must exist until the cursor is closed. The ppToken
52 ** result from xNext() is only valid until the next call to xNext()
53 ** or until xClose() is called.
54 */
55 /* TODO(shess) current implementation requires pInput to be
56 ** nul-terminated. This should either be fixed, or pInput/nBytes
57 ** should be converted to zInput.
58 */
59 int (*xOpen)(sqlite3_tokenizer *pTokenizer,
60 const char *pInput, int nBytes,
61 sqlite3_tokenizer_cursor **ppCursor);
62 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
63 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
64 const char **ppToken, int *pnBytes,
65 int *piStartOffset, int *piEndOffset, int *piPosition);
66};
67
68struct sqlite3_tokenizer {
69 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
70 /* Tokenizer implementations will typically add additional fields */
71};
72
73struct sqlite3_tokenizer_cursor {
74 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
75 /* Tokenizer implementations will typically add additional fields */
76};
77
78/*
79** Get the module for a tokenizer which generates tokens based on a
80** set of non-token characters. The default is to break tokens at any
81** non-alnum character, though the set of delimiters can also be
82** specified by the first argv argument to xCreate().
83*/
84/* TODO(shess) This doesn't belong here. Need some sort of
85** registration process.
86*/
87void sqlite3Fts1SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
88void sqlite3Fts1PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
89
90#endif /* _FTS1_TOKENIZER_H_ */
diff --git a/libraries/sqlite/win32/fts1_tokenizer1.c b/libraries/sqlite/win32/fts1_tokenizer1.c
new file mode 100755
index 0000000..f58fba8
--- /dev/null
+++ b/libraries/sqlite/win32/fts1_tokenizer1.c
@@ -0,0 +1,221 @@
1/*
2** The author disclaims copyright to this source code.
3**
4*************************************************************************
5** Implementation of the "simple" full-text-search tokenizer.
6*/
7
8/*
9** The code in this file is only compiled if:
10**
11** * The FTS1 module is being built as an extension
12** (in which case SQLITE_CORE is not defined), or
13**
14** * The FTS1 module is being built into the core of
15** SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
16*/
17#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
18
19
20#include <assert.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <ctype.h>
25
26#include "fts1_tokenizer.h"
27
28typedef struct simple_tokenizer {
29 sqlite3_tokenizer base;
30 char delim[128]; /* flag ASCII delimiters */
31} simple_tokenizer;
32
33typedef struct simple_tokenizer_cursor {
34 sqlite3_tokenizer_cursor base;
35 const char *pInput; /* input we are tokenizing */
36 int nBytes; /* size of the input */
37 int iOffset; /* current position in pInput */
38 int iToken; /* index of next token to be returned */
39 char *pToken; /* storage for current token */
40 int nTokenAllocated; /* space allocated to zToken buffer */
41} simple_tokenizer_cursor;
42
43
44/* Forward declaration */
45static const sqlite3_tokenizer_module simpleTokenizerModule;
46
47static int isDelim(simple_tokenizer *t, unsigned char c){
48 return c<0x80 && t->delim[c];
49}
50
51/*
52** Create a new tokenizer instance.
53*/
54static int simpleCreate(
55 int argc, const char * const *argv,
56 sqlite3_tokenizer **ppTokenizer
57){
58 simple_tokenizer *t;
59
60 t = (simple_tokenizer *) calloc(sizeof(*t), 1);
61 if( t==NULL ) return SQLITE_NOMEM;
62
63 /* TODO(shess) Delimiters need to remain the same from run to run,
64 ** else we need to reindex. One solution would be a meta-table to
65 ** track such information in the database, then we'd only want this
66 ** information on the initial create.
67 */
68 if( argc>1 ){
69 int i, n = strlen(argv[1]);
70 for(i=0; i<n; i++){
71 unsigned char ch = argv[1][i];
72 /* We explicitly don't support UTF-8 delimiters for now. */
73 if( ch>=0x80 ){
74 free(t);
75 return SQLITE_ERROR;
76 }
77 t->delim[ch] = 1;
78 }
79 } else {
80 /* Mark non-alphanumeric ASCII characters as delimiters */
81 int i;
82 for(i=1; i<0x80; i++){
83 t->delim[i] = !isalnum(i);
84 }
85 }
86
87 *ppTokenizer = &t->base;
88 return SQLITE_OK;
89}
90
91/*
92** Destroy a tokenizer
93*/
94static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
95 free(pTokenizer);
96 return SQLITE_OK;
97}
98
99/*
100** Prepare to begin tokenizing a particular string. The input
101** string to be tokenized is pInput[0..nBytes-1]. A cursor
102** used to incrementally tokenize this string is returned in
103** *ppCursor.
104*/
105static int simpleOpen(
106 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
107 const char *pInput, int nBytes, /* String to be tokenized */
108 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
109){
110 simple_tokenizer_cursor *c;
111
112 c = (simple_tokenizer_cursor *) malloc(sizeof(*c));
113 if( c==NULL ) return SQLITE_NOMEM;
114
115 c->pInput = pInput;
116 if( pInput==0 ){
117 c->nBytes = 0;
118 }else if( nBytes<0 ){
119 c->nBytes = (int)strlen(pInput);
120 }else{
121 c->nBytes = nBytes;
122 }
123 c->iOffset = 0; /* start tokenizing at the beginning */
124 c->iToken = 0;
125 c->pToken = NULL; /* no space allocated, yet. */
126 c->nTokenAllocated = 0;
127
128 *ppCursor = &c->base;
129 return SQLITE_OK;
130}
131
132/*
133** Close a tokenization cursor previously opened by a call to
134** simpleOpen() above.
135*/
136static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
137 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
138 free(c->pToken);
139 free(c);
140 return SQLITE_OK;
141}
142
143/*
144** Extract the next token from a tokenization cursor. The cursor must
145** have been opened by a prior call to simpleOpen().
146*/
147static int simpleNext(
148 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
149 const char **ppToken, /* OUT: *ppToken is the token text */
150 int *pnBytes, /* OUT: Number of bytes in token */
151 int *piStartOffset, /* OUT: Starting offset of token */
152 int *piEndOffset, /* OUT: Ending offset of token */
153 int *piPosition /* OUT: Position integer of token */
154){
155 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
156 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
157 unsigned char *p = (unsigned char *)c->pInput;
158
159 while( c->iOffset<c->nBytes ){
160 int iStartOffset;
161
162 /* Scan past delimiter characters */
163 while( c->iOffset<c->nBytes && isDelim(t, p[c->iOffset]) ){
164 c->iOffset++;
165 }
166
167 /* Count non-delimiter characters. */
168 iStartOffset = c->iOffset;
169 while( c->iOffset<c->nBytes && !isDelim(t, p[c->iOffset]) ){
170 c->iOffset++;
171 }
172
173 if( c->iOffset>iStartOffset ){
174 int i, n = c->iOffset-iStartOffset;
175 if( n>c->nTokenAllocated ){
176 c->nTokenAllocated = n+20;
177 c->pToken = realloc(c->pToken, c->nTokenAllocated);
178 if( c->pToken==NULL ) return SQLITE_NOMEM;
179 }
180 for(i=0; i<n; i++){
181 /* TODO(shess) This needs expansion to handle UTF-8
182 ** case-insensitivity.
183 */
184 unsigned char ch = p[iStartOffset+i];
185 c->pToken[i] = ch<0x80 ? tolower(ch) : ch;
186 }
187 *ppToken = c->pToken;
188 *pnBytes = n;
189 *piStartOffset = iStartOffset;
190 *piEndOffset = c->iOffset;
191 *piPosition = c->iToken++;
192
193 return SQLITE_OK;
194 }
195 }
196 return SQLITE_DONE;
197}
198
199/*
200** The set of routines that implement the simple tokenizer
201*/
202static const sqlite3_tokenizer_module simpleTokenizerModule = {
203 0,
204 simpleCreate,
205 simpleDestroy,
206 simpleOpen,
207 simpleClose,
208 simpleNext,
209};
210
211/*
212** Allocate a new simple tokenizer. Return a pointer to the new
213** tokenizer in *ppModule
214*/
215void sqlite3Fts1SimpleTokenizerModule(
216 sqlite3_tokenizer_module const**ppModule
217){
218 *ppModule = &simpleTokenizerModule;
219}
220
221#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */
diff --git a/libraries/sqlite/win32/func.c b/libraries/sqlite/win32/func.c
new file mode 100755
index 0000000..1760626
--- /dev/null
+++ b/libraries/sqlite/win32/func.c
@@ -0,0 +1,1509 @@
1/*
2** 2002 February 23
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.
10**
11*************************************************************************
12** This file contains the C functions that implement various SQL
13** functions of SQLite.
14**
15** There is only one exported symbol in this file - the function
16** sqliteRegisterBuildinFunctions() found at the bottom of the file.
17** All other code has file scope.
18**
19** $Id: func.c,v 1.174 2007/09/03 11:04:22 danielk1977 Exp $
20*/
21#include "sqliteInt.h"
22#include <ctype.h>
23#include <stdlib.h>
24#include <assert.h>
25#include "vdbeInt.h"
26
27
28/*
29** Return the collating function associated with a function.
30*/
31static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
32 return context->pColl;
33}
34
35/*
36** Implementation of the non-aggregate min() and max() functions
37*/
38static void minmaxFunc(
39 sqlite3_context *context,
40 int argc,
41 sqlite3_value **argv
42){
43 int i;
44 int mask; /* 0 for min() or 0xffffffff for max() */
45 int iBest;
46 CollSeq *pColl;
47
48 if( argc==0 ) return;
49 mask = sqlite3_user_data(context)==0 ? 0 : -1;
50 pColl = sqlite3GetFuncCollSeq(context);
51 assert( pColl );
52 assert( mask==-1 || mask==0 );
53 iBest = 0;
54 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
55 for(i=1; i<argc; i++){
56 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
57 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
58 iBest = i;
59 }
60 }
61 sqlite3_result_value(context, argv[iBest]);
62}
63
64/*
65** Return the type of the argument.
66*/
67static void typeofFunc(
68 sqlite3_context *context,
69 int argc,
70 sqlite3_value **argv
71){
72 const char *z = 0;
73 switch( sqlite3_value_type(argv[0]) ){
74 case SQLITE_NULL: z = "null"; break;
75 case SQLITE_INTEGER: z = "integer"; break;
76 case SQLITE_TEXT: z = "text"; break;
77 case SQLITE_FLOAT: z = "real"; break;
78 case SQLITE_BLOB: z = "blob"; break;
79 }
80 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
81}
82
83
84/*
85** Implementation of the length() function
86*/
87static void lengthFunc(
88 sqlite3_context *context,
89 int argc,
90 sqlite3_value **argv
91){
92 int len;
93
94 assert( argc==1 );
95 switch( sqlite3_value_type(argv[0]) ){
96 case SQLITE_BLOB:
97 case SQLITE_INTEGER:
98 case SQLITE_FLOAT: {
99 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
100 break;
101 }
102 case SQLITE_TEXT: {
103 const unsigned char *z = sqlite3_value_text(argv[0]);
104 if( z==0 ) return;
105 len = 0;
106 while( *z ){
107 len++;
108 SQLITE_SKIP_UTF8(z);
109 }
110 sqlite3_result_int(context, len);
111 break;
112 }
113 default: {
114 sqlite3_result_null(context);
115 break;
116 }
117 }
118}
119
120/*
121** Implementation of the abs() function
122*/
123static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
124 assert( argc==1 );
125 switch( sqlite3_value_type(argv[0]) ){
126 case SQLITE_INTEGER: {
127 i64 iVal = sqlite3_value_int64(argv[0]);
128 if( iVal<0 ){
129 if( (iVal<<1)==0 ){
130 sqlite3_result_error(context, "integer overflow", -1);
131 return;
132 }
133 iVal = -iVal;
134 }
135 sqlite3_result_int64(context, iVal);
136 break;
137 }
138 case SQLITE_NULL: {
139 sqlite3_result_null(context);
140 break;
141 }
142 default: {
143 double rVal = sqlite3_value_double(argv[0]);
144 if( rVal<0 ) rVal = -rVal;
145 sqlite3_result_double(context, rVal);
146 break;
147 }
148 }
149}
150
151/*
152** Implementation of the substr() function.
153**
154** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
155** p1 is 1-indexed. So substr(x,1,1) returns the first character
156** of x. If x is text, then we actually count UTF-8 characters.
157** If x is a blob, then we count bytes.
158**
159** If p1 is negative, then we begin abs(p1) from the end of x[].
160*/
161static void substrFunc(
162 sqlite3_context *context,
163 int argc,
164 sqlite3_value **argv
165){
166 const unsigned char *z;
167 const unsigned char *z2;
168 int len;
169 int p0type;
170 i64 p1, p2;
171
172 assert( argc==3 );
173 p0type = sqlite3_value_type(argv[0]);
174 if( p0type==SQLITE_BLOB ){
175 len = sqlite3_value_bytes(argv[0]);
176 z = sqlite3_value_blob(argv[0]);
177 if( z==0 ) return;
178 assert( len==sqlite3_value_bytes(argv[0]) );
179 }else{
180 z = sqlite3_value_text(argv[0]);
181 if( z==0 ) return;
182 len = 0;
183 for(z2=z; *z2; len++){
184 SQLITE_SKIP_UTF8(z2);
185 }
186 }
187 p1 = sqlite3_value_int(argv[1]);
188 p2 = sqlite3_value_int(argv[2]);
189 if( p1<0 ){
190 p1 += len;
191 if( p1<0 ){
192 p2 += p1;
193 p1 = 0;
194 }
195 }else if( p1>0 ){
196 p1--;
197 }
198 if( p1+p2>len ){
199 p2 = len-p1;
200 }
201 if( p0type!=SQLITE_BLOB ){
202 while( *z && p1 ){
203 SQLITE_SKIP_UTF8(z);
204 p1--;
205 }
206 for(z2=z; *z2 && p2; p2--){
207 SQLITE_SKIP_UTF8(z2);
208 }
209 sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT);
210 }else{
211 if( p2<0 ) p2 = 0;
212 sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
213 }
214}
215
216/*
217** Implementation of the round() function
218*/
219static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
220 int n = 0;
221 double r;
222 char zBuf[500]; /* larger than the %f representation of the largest double */
223 assert( argc==1 || argc==2 );
224 if( argc==2 ){
225 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
226 n = sqlite3_value_int(argv[1]);
227 if( n>30 ) n = 30;
228 if( n<0 ) n = 0;
229 }
230 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
231 r = sqlite3_value_double(argv[0]);
232 sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
233 sqlite3AtoF(zBuf, &r);
234 sqlite3_result_double(context, r);
235}
236
237/*
238** Allocate nByte bytes of space using sqlite3_malloc(). If the
239** allocation fails, call sqlite3_result_error_nomem() to notify
240** the database handle that malloc() has failed.
241*/
242static void *contextMalloc(sqlite3_context *context, int nByte){
243 char *z = sqlite3_malloc(nByte);
244 if( !z && nByte>0 ){
245 sqlite3_result_error_nomem(context);
246 }
247 return z;
248}
249
250/*
251** Implementation of the upper() and lower() SQL functions.
252*/
253static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
254 char *z1;
255 const char *z2;
256 int i, n;
257 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
258 z2 = (char*)sqlite3_value_text(argv[0]);
259 n = sqlite3_value_bytes(argv[0]);
260 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
261 assert( z2==(char*)sqlite3_value_text(argv[0]) );
262 if( z2 ){
263 z1 = contextMalloc(context, n+1);
264 if( z1 ){
265 memcpy(z1, z2, n+1);
266 for(i=0; z1[i]; i++){
267 z1[i] = toupper(z1[i]);
268 }
269 sqlite3_result_text(context, z1, -1, sqlite3_free);
270 }
271 }
272}
273static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
274 char *z1;
275 const char *z2;
276 int i, n;
277 if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
278 z2 = (char*)sqlite3_value_text(argv[0]);
279 n = sqlite3_value_bytes(argv[0]);
280 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
281 assert( z2==(char*)sqlite3_value_text(argv[0]) );
282 if( z2 ){
283 z1 = contextMalloc(context, n+1);
284 if( z1 ){
285 memcpy(z1, z2, n+1);
286 for(i=0; z1[i]; i++){
287 z1[i] = tolower(z1[i]);
288 }
289 sqlite3_result_text(context, z1, -1, sqlite3_free);
290 }
291 }
292}
293
294/*
295** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
296** All three do the same thing. They return the first non-NULL
297** argument.
298*/
299static void ifnullFunc(
300 sqlite3_context *context,
301 int argc,
302 sqlite3_value **argv
303){
304 int i;
305 for(i=0; i<argc; i++){
306 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
307 sqlite3_result_value(context, argv[i]);
308 break;
309 }
310 }
311}
312
313/*
314** Implementation of random(). Return a random integer.
315*/
316static void randomFunc(
317 sqlite3_context *context,
318 int argc,
319 sqlite3_value **argv
320){
321 sqlite_int64 r;
322 sqlite3Randomness(sizeof(r), &r);
323 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
324 /* can always do abs() of the result */
325 sqlite3_result_int64(context, r);
326}
327
328/*
329** Implementation of randomblob(N). Return a random blob
330** that is N bytes long.
331*/
332static void randomBlob(
333 sqlite3_context *context,
334 int argc,
335 sqlite3_value **argv
336){
337 int n;
338 unsigned char *p;
339 assert( argc==1 );
340 n = sqlite3_value_int(argv[0]);
341 if( n<1 ){
342 n = 1;
343 }
344 if( n>SQLITE_MAX_LENGTH ){
345 sqlite3_result_error_toobig(context);
346 return;
347 }
348 p = contextMalloc(context, n);
349 if( p ){
350 sqlite3Randomness(n, p);
351 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
352 }
353}
354
355/*
356** Implementation of the last_insert_rowid() SQL function. The return
357** value is the same as the sqlite3_last_insert_rowid() API function.
358*/
359static void last_insert_rowid(
360 sqlite3_context *context,
361 int arg,
362 sqlite3_value **argv
363){
364 sqlite3 *db = sqlite3_user_data(context);
365 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
366}
367
368/*
369** Implementation of the changes() SQL function. The return value is the
370** same as the sqlite3_changes() API function.
371*/
372static void changes(
373 sqlite3_context *context,
374 int arg,
375 sqlite3_value **argv
376){
377 sqlite3 *db = sqlite3_user_data(context);
378 sqlite3_result_int(context, sqlite3_changes(db));
379}
380
381/*
382** Implementation of the total_changes() SQL function. The return value is
383** the same as the sqlite3_total_changes() API function.
384*/
385static void total_changes(
386 sqlite3_context *context,
387 int arg,
388 sqlite3_value **argv
389){
390 sqlite3 *db = sqlite3_user_data(context);
391 sqlite3_result_int(context, sqlite3_total_changes(db));
392}
393
394/*
395** A structure defining how to do GLOB-style comparisons.
396*/
397struct compareInfo {
398 u8 matchAll;
399 u8 matchOne;
400 u8 matchSet;
401 u8 noCase;
402};
403
404static const struct compareInfo globInfo = { '*', '?', '[', 0 };
405/* The correct SQL-92 behavior is for the LIKE operator to ignore
406** case. Thus 'a' LIKE 'A' would be true. */
407static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
408/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
409** is case sensitive causing 'a' LIKE 'A' to be false */
410static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
411
412/*
413** Compare two UTF-8 strings for equality where the first string can
414** potentially be a "glob" expression. Return true (1) if they
415** are the same and false (0) if they are different.
416**
417** Globbing rules:
418**
419** '*' Matches any sequence of zero or more characters.
420**
421** '?' Matches exactly one character.
422**
423** [...] Matches one character from the enclosed list of
424** characters.
425**
426** [^...] Matches one character not in the enclosed list.
427**
428** With the [...] and [^...] matching, a ']' character can be included
429** in the list by making it the first character after '[' or '^'. A
430** range of characters can be specified using '-'. Example:
431** "[a-z]" matches any single lower-case letter. To match a '-', make
432** it the last character in the list.
433**
434** This routine is usually quick, but can be N**2 in the worst case.
435**
436** Hints: to match '*' or '?', put them in "[]". Like this:
437**
438** abc[*]xyz Matches "abc*xyz" only
439*/
440static int patternCompare(
441 const u8 *zPattern, /* The glob pattern */
442 const u8 *zString, /* The string to compare against the glob */
443 const struct compareInfo *pInfo, /* Information about how to do the compare */
444 const int esc /* The escape character */
445){
446 int c, c2;
447 int invert;
448 int seen;
449 u8 matchOne = pInfo->matchOne;
450 u8 matchAll = pInfo->matchAll;
451 u8 matchSet = pInfo->matchSet;
452 u8 noCase = pInfo->noCase;
453 int prevEscape = 0; /* True if the previous character was 'escape' */
454
455 while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){
456 if( !prevEscape && c==matchAll ){
457 while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll
458 || c == matchOne ){
459 if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){
460 return 0;
461 }
462 }
463 if( c==0 ){
464 return 1;
465 }else if( c==esc ){
466 c = sqlite3Utf8Read(zPattern, 0, &zPattern);
467 if( c==0 ){
468 return 0;
469 }
470 }else if( c==matchSet ){
471 assert( esc==0 ); /* This is GLOB, not LIKE */
472 assert( matchSet<0x80 ); /* '[' is a single-byte character */
473 while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
474 SQLITE_SKIP_UTF8(zString);
475 }
476 return *zString!=0;
477 }
478 while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){
479 if( noCase ){
480 c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
481 c = c<0x80 ? sqlite3UpperToLower[c] : c;
482 while( c2 != 0 && c2 != c ){
483 c2 = sqlite3Utf8Read(zString, 0, &zString);
484 if( c2<0x80 ) c2 = sqlite3UpperToLower[c2];
485 }
486 }else{
487 while( c2 != 0 && c2 != c ){
488 c2 = sqlite3Utf8Read(zString, 0, &zString);
489 }
490 }
491 if( c2==0 ) return 0;
492 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
493 }
494 return 0;
495 }else if( !prevEscape && c==matchOne ){
496 if( sqlite3Utf8Read(zString, 0, &zString)==0 ){
497 return 0;
498 }
499 }else if( c==matchSet ){
500 int prior_c = 0;
501 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
502 seen = 0;
503 invert = 0;
504 c = sqlite3Utf8Read(zString, 0, &zString);
505 if( c==0 ) return 0;
506 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
507 if( c2=='^' ){
508 invert = 1;
509 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
510 }
511 if( c2==']' ){
512 if( c==']' ) seen = 1;
513 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
514 }
515 while( c2 && c2!=']' ){
516 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
517 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
518 if( c>=prior_c && c<=c2 ) seen = 1;
519 prior_c = 0;
520 }else{
521 if( c==c2 ){
522 seen = 1;
523 }
524 prior_c = c2;
525 }
526 c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
527 }
528 if( c2==0 || (seen ^ invert)==0 ){
529 return 0;
530 }
531 }else if( esc==c && !prevEscape ){
532 prevEscape = 1;
533 }else{
534 c2 = sqlite3Utf8Read(zString, 0, &zString);
535 if( noCase ){
536 c = c<0x80 ? sqlite3UpperToLower[c] : c;
537 c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
538 }
539 if( c!=c2 ){
540 return 0;
541 }
542 prevEscape = 0;
543 }
544 }
545 return *zString==0;
546}
547
548/*
549** Count the number of times that the LIKE operator (or GLOB which is
550** just a variation of LIKE) gets called. This is used for testing
551** only.
552*/
553#ifdef SQLITE_TEST
554int sqlite3_like_count = 0;
555#endif
556
557
558/*
559** Implementation of the like() SQL function. This function implements
560** the build-in LIKE operator. The first argument to the function is the
561** pattern and the second argument is the string. So, the SQL statements:
562**
563** A LIKE B
564**
565** is implemented as like(B,A).
566**
567** This same function (with a different compareInfo structure) computes
568** the GLOB operator.
569*/
570static void likeFunc(
571 sqlite3_context *context,
572 int argc,
573 sqlite3_value **argv
574){
575 const unsigned char *zA, *zB;
576 int escape = 0;
577
578 zB = sqlite3_value_text(argv[0]);
579 zA = sqlite3_value_text(argv[1]);
580
581 /* Limit the length of the LIKE or GLOB pattern to avoid problems
582 ** of deep recursion and N*N behavior in patternCompare().
583 */
584 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
585 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
586 return;
587 }
588 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
589
590 if( argc==3 ){
591 /* The escape character string must consist of a single UTF-8 character.
592 ** Otherwise, return an error.
593 */
594 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
595 if( zEsc==0 ) return;
596 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
597 sqlite3_result_error(context,
598 "ESCAPE expression must be a single character", -1);
599 return;
600 }
601 escape = sqlite3Utf8Read(zEsc, 0, &zEsc);
602 }
603 if( zA && zB ){
604 struct compareInfo *pInfo = sqlite3_user_data(context);
605#ifdef SQLITE_TEST
606 sqlite3_like_count++;
607#endif
608
609 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
610 }
611}
612
613/*
614** Implementation of the NULLIF(x,y) function. The result is the first
615** argument if the arguments are different. The result is NULL if the
616** arguments are equal to each other.
617*/
618static void nullifFunc(
619 sqlite3_context *context,
620 int argc,
621 sqlite3_value **argv
622){
623 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
624 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
625 sqlite3_result_value(context, argv[0]);
626 }
627}
628
629/*
630** Implementation of the VERSION(*) function. The result is the version
631** of the SQLite library that is running.
632*/
633static void versionFunc(
634 sqlite3_context *context,
635 int argc,
636 sqlite3_value **argv
637){
638 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
639}
640
641/* Array for converting from half-bytes (nybbles) into ASCII hex
642** digits. */
643static const char hexdigits[] = {
644 '0', '1', '2', '3', '4', '5', '6', '7',
645 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
646};
647
648/*
649** EXPERIMENTAL - This is not an official function. The interface may
650** change. This function may disappear. Do not write code that depends
651** on this function.
652**
653** Implementation of the QUOTE() function. This function takes a single
654** argument. If the argument is numeric, the return value is the same as
655** the argument. If the argument is NULL, the return value is the string
656** "NULL". Otherwise, the argument is enclosed in single quotes with
657** single-quote escapes.
658*/
659static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
660 if( argc<1 ) return;
661 switch( sqlite3_value_type(argv[0]) ){
662 case SQLITE_NULL: {
663 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
664 break;
665 }
666 case SQLITE_INTEGER:
667 case SQLITE_FLOAT: {
668 sqlite3_result_value(context, argv[0]);
669 break;
670 }
671 case SQLITE_BLOB: {
672 char *zText = 0;
673 char const *zBlob = sqlite3_value_blob(argv[0]);
674 int nBlob = sqlite3_value_bytes(argv[0]);
675 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
676
677 if( 2*nBlob+4>SQLITE_MAX_LENGTH ){
678 sqlite3_result_error_toobig(context);
679 return;
680 }
681 zText = (char *)contextMalloc(context, (2*nBlob)+4);
682 if( zText ){
683 int i;
684 for(i=0; i<nBlob; i++){
685 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
686 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
687 }
688 zText[(nBlob*2)+2] = '\'';
689 zText[(nBlob*2)+3] = '\0';
690 zText[0] = 'X';
691 zText[1] = '\'';
692 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
693 sqlite3_free(zText);
694 }
695 break;
696 }
697 case SQLITE_TEXT: {
698 int i,j;
699 u64 n;
700 const unsigned char *zArg = sqlite3_value_text(argv[0]);
701 char *z;
702
703 if( zArg==0 ) return;
704 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
705 if( i+n+3>SQLITE_MAX_LENGTH ){
706 sqlite3_result_error_toobig(context);
707 return;
708 }
709 z = contextMalloc(context, i+n+3);
710 if( z ){
711 z[0] = '\'';
712 for(i=0, j=1; zArg[i]; i++){
713 z[j++] = zArg[i];
714 if( zArg[i]=='\'' ){
715 z[j++] = '\'';
716 }
717 }
718 z[j++] = '\'';
719 z[j] = 0;
720 sqlite3_result_text(context, z, j, sqlite3_free);
721 }
722 }
723 }
724}
725
726/*
727** The hex() function. Interpret the argument as a blob. Return
728** a hexadecimal rendering as text.
729*/
730static void hexFunc(
731 sqlite3_context *context,
732 int argc,
733 sqlite3_value **argv
734){
735 int i, n;
736 const unsigned char *pBlob;
737 char *zHex, *z;
738 assert( argc==1 );
739 pBlob = sqlite3_value_blob(argv[0]);
740 n = sqlite3_value_bytes(argv[0]);
741 if( n*2+1>SQLITE_MAX_LENGTH ){
742 sqlite3_result_error_toobig(context);
743 return;
744 }
745 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
746 z = zHex = contextMalloc(context, n*2 + 1);
747 if( zHex ){
748 for(i=0; i<n; i++, pBlob++){
749 unsigned char c = *pBlob;
750 *(z++) = hexdigits[(c>>4)&0xf];
751 *(z++) = hexdigits[c&0xf];
752 }
753 *z = 0;
754 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
755 }
756}
757
758/*
759** The zeroblob(N) function returns a zero-filled blob of size N bytes.
760*/
761static void zeroblobFunc(
762 sqlite3_context *context,
763 int argc,
764 sqlite3_value **argv
765){
766 i64 n;
767 assert( argc==1 );
768 n = sqlite3_value_int64(argv[0]);
769 if( n>SQLITE_MAX_LENGTH ){
770 sqlite3_result_error_toobig(context);
771 }else{
772 sqlite3_result_zeroblob(context, n);
773 }
774}
775
776/*
777** The replace() function. Three arguments are all strings: call
778** them A, B, and C. The result is also a string which is derived
779** from A by replacing every occurance of B with C. The match
780** must be exact. Collating sequences are not used.
781*/
782static void replaceFunc(
783 sqlite3_context *context,
784 int argc,
785 sqlite3_value **argv
786){
787 const unsigned char *zStr; /* The input string A */
788 const unsigned char *zPattern; /* The pattern string B */
789 const unsigned char *zRep; /* The replacement string C */
790 unsigned char *zOut; /* The output */
791 int nStr; /* Size of zStr */
792 int nPattern; /* Size of zPattern */
793 int nRep; /* Size of zRep */
794 i64 nOut; /* Maximum size of zOut */
795 int loopLimit; /* Last zStr[] that might match zPattern[] */
796 int i, j; /* Loop counters */
797
798 assert( argc==3 );
799 zStr = sqlite3_value_text(argv[0]);
800 if( zStr==0 ) return;
801 nStr = sqlite3_value_bytes(argv[0]);
802 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
803 zPattern = sqlite3_value_text(argv[1]);
804 if( zPattern==0 || zPattern[0]==0 ) return;
805 nPattern = sqlite3_value_bytes(argv[1]);
806 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
807 zRep = sqlite3_value_text(argv[2]);
808 if( zRep==0 ) return;
809 nRep = sqlite3_value_bytes(argv[2]);
810 assert( zRep==sqlite3_value_text(argv[2]) );
811 nOut = nStr + 1;
812 assert( nOut<SQLITE_MAX_LENGTH );
813 zOut = contextMalloc(context, (int)nOut);
814 if( zOut==0 ){
815 return;
816 }
817 loopLimit = nStr - nPattern;
818 for(i=j=0; i<=loopLimit; i++){
819 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
820 zOut[j++] = zStr[i];
821 }else{
822 u8 *zOld;
823 nOut += nRep - nPattern;
824 if( nOut>=SQLITE_MAX_LENGTH ){
825 sqlite3_result_error_toobig(context);
826 sqlite3_free(zOut);
827 return;
828 }
829 zOld = zOut;
830 zOut = sqlite3_realloc(zOut, (int)nOut);
831 if( zOut==0 ){
832 sqlite3_result_error_nomem(context);
833 sqlite3_free(zOld);
834 return;
835 }
836 memcpy(&zOut[j], zRep, nRep);
837 j += nRep;
838 i += nPattern-1;
839 }
840 }
841 assert( j+nStr-i+1==nOut );
842 memcpy(&zOut[j], &zStr[i], nStr-i);
843 j += nStr - i;
844 assert( j<=nOut );
845 zOut[j] = 0;
846 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
847}
848
849/*
850** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
851** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
852*/
853static void trimFunc(
854 sqlite3_context *context,
855 int argc,
856 sqlite3_value **argv
857){
858 const unsigned char *zIn; /* Input string */
859 const unsigned char *zCharSet; /* Set of characters to trim */
860 int nIn; /* Number of bytes in input */
861 int flags; /* 1: trimleft 2: trimright 3: trim */
862 int i; /* Loop counter */
863 unsigned char *aLen; /* Length of each character in zCharSet */
864 const unsigned char **azChar; /* Individual characters in zCharSet */
865 int nChar; /* Number of characters in zCharSet */
866
867 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
868 return;
869 }
870 zIn = sqlite3_value_text(argv[0]);
871 if( zIn==0 ) return;
872 nIn = sqlite3_value_bytes(argv[0]);
873 assert( zIn==sqlite3_value_text(argv[0]) );
874 if( argc==1 ){
875 static const unsigned char lenOne[] = { 1 };
876 static const unsigned char *azOne[] = { (u8*)" " };
877 nChar = 1;
878 aLen = (u8*)lenOne;
879 azChar = azOne;
880 zCharSet = 0;
881 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
882 return;
883 }else{
884 const unsigned char *z;
885 for(z=zCharSet, nChar=0; *z; nChar++){
886 SQLITE_SKIP_UTF8(z);
887 }
888 if( nChar>0 ){
889 azChar = contextMalloc(context, nChar*(sizeof(char*)+1));
890 if( azChar==0 ){
891 return;
892 }
893 aLen = (unsigned char*)&azChar[nChar];
894 for(z=zCharSet, nChar=0; *z; nChar++){
895 azChar[nChar] = z;
896 SQLITE_SKIP_UTF8(z);
897 aLen[nChar] = z - azChar[nChar];
898 }
899 }
900 }
901 if( nChar>0 ){
902 flags = (int)sqlite3_user_data(context);
903 if( flags & 1 ){
904 while( nIn>0 ){
905 int len;
906 for(i=0; i<nChar; i++){
907 len = aLen[i];
908 if( memcmp(zIn, azChar[i], len)==0 ) break;
909 }
910 if( i>=nChar ) break;
911 zIn += len;
912 nIn -= len;
913 }
914 }
915 if( flags & 2 ){
916 while( nIn>0 ){
917 int len;
918 for(i=0; i<nChar; i++){
919 len = aLen[i];
920 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
921 }
922 if( i>=nChar ) break;
923 nIn -= len;
924 }
925 }
926 if( zCharSet ){
927 sqlite3_free(azChar);
928 }
929 }
930 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
931}
932
933#ifdef SQLITE_SOUNDEX
934/*
935** Compute the soundex encoding of a word.
936*/
937static void soundexFunc(
938 sqlite3_context *context,
939 int argc,
940 sqlite3_value **argv
941){
942 char zResult[8];
943 const u8 *zIn;
944 int i, j;
945 static const unsigned char iCode[] = {
946 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
947 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
948 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
949 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
950 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
951 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
952 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
953 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
954 };
955 assert( argc==1 );
956 zIn = (u8*)sqlite3_value_text(argv[0]);
957 if( zIn==0 ) zIn = (u8*)"";
958 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
959 if( zIn[i] ){
960 u8 prevcode = iCode[zIn[i]&0x7f];
961 zResult[0] = toupper(zIn[i]);
962 for(j=1; j<4 && zIn[i]; i++){
963 int code = iCode[zIn[i]&0x7f];
964 if( code>0 ){
965 if( code!=prevcode ){
966 prevcode = code;
967 zResult[j++] = code + '0';
968 }
969 }else{
970 prevcode = 0;
971 }
972 }
973 while( j<4 ){
974 zResult[j++] = '0';
975 }
976 zResult[j] = 0;
977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
978 }else{
979 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
980 }
981}
982#endif
983
984#ifndef SQLITE_OMIT_LOAD_EXTENSION
985/*
986** A function that loads a shared-library extension then returns NULL.
987*/
988static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
989 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
990 const char *zProc;
991 sqlite3 *db = sqlite3_user_data(context);
992 char *zErrMsg = 0;
993
994 if( argc==2 ){
995 zProc = (const char *)sqlite3_value_text(argv[1]);
996 }else{
997 zProc = 0;
998 }
999 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
1000 sqlite3_result_error(context, zErrMsg, -1);
1001 sqlite3_free(zErrMsg);
1002 }
1003}
1004#endif
1005
1006#ifdef SQLITE_TEST
1007/*
1008** This function generates a string of random characters. Used for
1009** generating test data.
1010*/
1011static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
1012 static const unsigned char zSrc[] =
1013 "abcdefghijklmnopqrstuvwxyz"
1014 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1015 "0123456789"
1016 ".-!,:*^+=_|?/<> ";
1017 int iMin, iMax, n, r, i;
1018 unsigned char zBuf[1000];
1019
1020 /* It used to be possible to call randstr() with any number of arguments,
1021 ** but now it is registered with SQLite as requiring exactly 2.
1022 */
1023 assert(argc==2);
1024
1025 iMin = sqlite3_value_int(argv[0]);
1026 if( iMin<0 ) iMin = 0;
1027 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
1028 iMax = sqlite3_value_int(argv[1]);
1029 if( iMax<iMin ) iMax = iMin;
1030 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
1031 n = iMin;
1032 if( iMax>iMin ){
1033 sqlite3Randomness(sizeof(r), &r);
1034 r &= 0x7fffffff;
1035 n += r%(iMax + 1 - iMin);
1036 }
1037 assert( n<sizeof(zBuf) );
1038 sqlite3Randomness(n, zBuf);
1039 for(i=0; i<n; i++){
1040 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
1041 }
1042 zBuf[n] = 0;
1043 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
1044}
1045#endif /* SQLITE_TEST */
1046
1047#ifdef SQLITE_TEST
1048/*
1049** The following two SQL functions are used to test returning a text
1050** result with a destructor. Function 'test_destructor' takes one argument
1051** and returns the same argument interpreted as TEXT. A destructor is
1052** passed with the sqlite3_result_text() call.
1053**
1054** SQL function 'test_destructor_count' returns the number of outstanding
1055** allocations made by 'test_destructor';
1056**
1057** WARNING: Not threadsafe.
1058*/
1059static int test_destructor_count_var = 0;
1060static void destructor(void *p){
1061 char *zVal = (char *)p;
1062 assert(zVal);
1063 zVal--;
1064 sqlite3_free(zVal);
1065 test_destructor_count_var--;
1066}
1067static void test_destructor(
1068 sqlite3_context *pCtx,
1069 int nArg,
1070 sqlite3_value **argv
1071){
1072 char *zVal;
1073 int len;
1074 sqlite3 *db = sqlite3_user_data(pCtx);
1075
1076 test_destructor_count_var++;
1077 assert( nArg==1 );
1078 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1079 len = sqlite3ValueBytes(argv[0], ENC(db));
1080 zVal = contextMalloc(pCtx, len+3);
1081 if( !zVal ){
1082 return;
1083 }
1084 zVal[len+1] = 0;
1085 zVal[len+2] = 0;
1086 zVal++;
1087 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
1088 if( ENC(db)==SQLITE_UTF8 ){
1089 sqlite3_result_text(pCtx, zVal, -1, destructor);
1090#ifndef SQLITE_OMIT_UTF16
1091 }else if( ENC(db)==SQLITE_UTF16LE ){
1092 sqlite3_result_text16le(pCtx, zVal, -1, destructor);
1093 }else{
1094 sqlite3_result_text16be(pCtx, zVal, -1, destructor);
1095#endif /* SQLITE_OMIT_UTF16 */
1096 }
1097}
1098static void test_destructor_count(
1099 sqlite3_context *pCtx,
1100 int nArg,
1101 sqlite3_value **argv
1102){
1103 sqlite3_result_int(pCtx, test_destructor_count_var);
1104}
1105#endif /* SQLITE_TEST */
1106
1107#ifdef SQLITE_TEST
1108/*
1109** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
1110** interface.
1111**
1112** The test_auxdata() SQL function attempts to register each of its arguments
1113** as auxiliary data. If there are no prior registrations of aux data for
1114** that argument (meaning the argument is not a constant or this is its first
1115** call) then the result for that argument is 0. If there is a prior
1116** registration, the result for that argument is 1. The overall result
1117** is the individual argument results separated by spaces.
1118*/
1119static void free_test_auxdata(void *p) {sqlite3_free(p);}
1120static void test_auxdata(
1121 sqlite3_context *pCtx,
1122 int nArg,
1123 sqlite3_value **argv
1124){
1125 int i;
1126 char *zRet = contextMalloc(pCtx, nArg*2);
1127 if( !zRet ) return;
1128 memset(zRet, 0, nArg*2);
1129 for(i=0; i<nArg; i++){
1130 char const *z = (char*)sqlite3_value_text(argv[i]);
1131 if( z ){
1132 char *zAux = sqlite3_get_auxdata(pCtx, i);
1133 if( zAux ){
1134 zRet[i*2] = '1';
1135 if( strcmp(zAux, z) ){
1136 sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
1137 return;
1138 }
1139 }else {
1140 zRet[i*2] = '0';
1141 }
1142
1143 zAux = contextMalloc(pCtx, strlen(z)+1);
1144 if( zAux ){
1145 strcpy(zAux, z);
1146 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
1147 }
1148 zRet[i*2+1] = ' ';
1149 }
1150 }
1151 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
1152}
1153#endif /* SQLITE_TEST */
1154
1155#ifdef SQLITE_TEST
1156/*
1157** A function to test error reporting from user functions. This function
1158** returns a copy of it's first argument as an error.
1159*/
1160static void test_error(
1161 sqlite3_context *pCtx,
1162 int nArg,
1163 sqlite3_value **argv
1164){
1165 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
1166}
1167#endif /* SQLITE_TEST */
1168
1169/*
1170** An instance of the following structure holds the context of a
1171** sum() or avg() aggregate computation.
1172*/
1173typedef struct SumCtx SumCtx;
1174struct SumCtx {
1175 double rSum; /* Floating point sum */
1176 i64 iSum; /* Integer sum */
1177 i64 cnt; /* Number of elements summed */
1178 u8 overflow; /* True if integer overflow seen */
1179 u8 approx; /* True if non-integer value was input to the sum */
1180};
1181
1182/*
1183** Routines used to compute the sum, average, and total.
1184**
1185** The SUM() function follows the (broken) SQL standard which means
1186** that it returns NULL if it sums over no inputs. TOTAL returns
1187** 0.0 in that case. In addition, TOTAL always returns a float where
1188** SUM might return an integer if it never encounters a floating point
1189** value. TOTAL never fails, but SUM might through an exception if
1190** it overflows an integer.
1191*/
1192static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1193 SumCtx *p;
1194 int type;
1195 assert( argc==1 );
1196 p = sqlite3_aggregate_context(context, sizeof(*p));
1197 type = sqlite3_value_numeric_type(argv[0]);
1198 if( p && type!=SQLITE_NULL ){
1199 p->cnt++;
1200 if( type==SQLITE_INTEGER ){
1201 i64 v = sqlite3_value_int64(argv[0]);
1202 p->rSum += v;
1203 if( (p->approx|p->overflow)==0 ){
1204 i64 iNewSum = p->iSum + v;
1205 int s1 = p->iSum >> (sizeof(i64)*8-1);
1206 int s2 = v >> (sizeof(i64)*8-1);
1207 int s3 = iNewSum >> (sizeof(i64)*8-1);
1208 p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
1209 p->iSum = iNewSum;
1210 }
1211 }else{
1212 p->rSum += sqlite3_value_double(argv[0]);
1213 p->approx = 1;
1214 }
1215 }
1216}
1217static void sumFinalize(sqlite3_context *context){
1218 SumCtx *p;
1219 p = sqlite3_aggregate_context(context, 0);
1220 if( p && p->cnt>0 ){
1221 if( p->overflow ){
1222 sqlite3_result_error(context,"integer overflow",-1);
1223 }else if( p->approx ){
1224 sqlite3_result_double(context, p->rSum);
1225 }else{
1226 sqlite3_result_int64(context, p->iSum);
1227 }
1228 }
1229}
1230static void avgFinalize(sqlite3_context *context){
1231 SumCtx *p;
1232 p = sqlite3_aggregate_context(context, 0);
1233 if( p && p->cnt>0 ){
1234 sqlite3_result_double(context, p->rSum/(double)p->cnt);
1235 }
1236}
1237static void totalFinalize(sqlite3_context *context){
1238 SumCtx *p;
1239 p = sqlite3_aggregate_context(context, 0);
1240 sqlite3_result_double(context, p ? p->rSum : 0.0);
1241}
1242
1243/*
1244** The following structure keeps track of state information for the
1245** count() aggregate function.
1246*/
1247typedef struct CountCtx CountCtx;
1248struct CountCtx {
1249 i64 n;
1250};
1251
1252/*
1253** Routines to implement the count() aggregate function.
1254*/
1255static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1256 CountCtx *p;
1257 p = sqlite3_aggregate_context(context, sizeof(*p));
1258 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
1259 p->n++;
1260 }
1261}
1262static void countFinalize(sqlite3_context *context){
1263 CountCtx *p;
1264 p = sqlite3_aggregate_context(context, 0);
1265 sqlite3_result_int64(context, p ? p->n : 0);
1266}
1267
1268/*
1269** Routines to implement min() and max() aggregate functions.
1270*/
1271static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1272 Mem *pArg = (Mem *)argv[0];
1273 Mem *pBest;
1274
1275 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1276 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
1277 if( !pBest ) return;
1278
1279 if( pBest->flags ){
1280 int max;
1281 int cmp;
1282 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
1283 /* This step function is used for both the min() and max() aggregates,
1284 ** the only difference between the two being that the sense of the
1285 ** comparison is inverted. For the max() aggregate, the
1286 ** sqlite3_user_data() function returns (void *)-1. For min() it
1287 ** returns (void *)db, where db is the sqlite3* database pointer.
1288 ** Therefore the next statement sets variable 'max' to 1 for the max()
1289 ** aggregate, or 0 for min().
1290 */
1291 max = sqlite3_user_data(context)!=0;
1292 cmp = sqlite3MemCompare(pBest, pArg, pColl);
1293 if( (max && cmp<0) || (!max && cmp>0) ){
1294 sqlite3VdbeMemCopy(pBest, pArg);
1295 }
1296 }else{
1297 sqlite3VdbeMemCopy(pBest, pArg);
1298 }
1299}
1300static void minMaxFinalize(sqlite3_context *context){
1301 sqlite3_value *pRes;
1302 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
1303 if( pRes ){
1304 if( pRes->flags ){
1305 sqlite3_result_value(context, pRes);
1306 }
1307 sqlite3VdbeMemRelease(pRes);
1308 }
1309}
1310
1311
1312/*
1313** This function registered all of the above C functions as SQL
1314** functions. This should be the only routine in this file with
1315** external linkage.
1316*/
1317void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
1318 static const struct {
1319 char *zName;
1320 signed char nArg;
1321 u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */
1322 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
1323 u8 needCollSeq;
1324 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
1325 } aFuncs[] = {
1326 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc },
1327 { "min", 0, 0, SQLITE_UTF8, 1, 0 },
1328 { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc },
1329 { "max", 0, 1, SQLITE_UTF8, 1, 0 },
1330 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc },
1331 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc },
1332 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc },
1333 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc },
1334 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc },
1335 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc },
1336 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc },
1337 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc },
1338 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
1339 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
1340 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
1341 { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc },
1342 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
1343 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
1344 { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob },
1345 { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
1346 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
1347 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },
1348 { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid },
1349 { "changes", 0, 0xff, SQLITE_UTF8, 0, changes },
1350 { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes },
1351 { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc },
1352 { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc },
1353 { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc },
1354 { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc },
1355 { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc },
1356 { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc },
1357 { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc },
1358 { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc },
1359#ifdef SQLITE_SOUNDEX
1360 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc},
1361#endif
1362#ifndef SQLITE_OMIT_LOAD_EXTENSION
1363 { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt },
1364 { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt },
1365#endif
1366#ifdef SQLITE_TEST
1367 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr },
1368 { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor},
1369 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
1370 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata},
1371 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error},
1372#endif
1373 };
1374 static const struct {
1375 char *zName;
1376 signed char nArg;
1377 u8 argType;
1378 u8 needCollSeq;
1379 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1380 void (*xFinalize)(sqlite3_context*);
1381 } aAggs[] = {
1382 { "min", 1, 0, 1, minmaxStep, minMaxFinalize },
1383 { "max", 1, 1, 1, minmaxStep, minMaxFinalize },
1384 { "sum", 1, 0, 0, sumStep, sumFinalize },
1385 { "total", 1, 0, 0, sumStep, totalFinalize },
1386 { "avg", 1, 0, 0, sumStep, avgFinalize },
1387 { "count", 0, 0, 0, countStep, countFinalize },
1388 { "count", 1, 0, 0, countStep, countFinalize },
1389 };
1390 int i;
1391
1392 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1393 void *pArg;
1394 u8 argType = aFuncs[i].argType;
1395 if( argType==0xff ){
1396 pArg = db;
1397 }else{
1398 pArg = (void*)(int)argType;
1399 }
1400 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
1401 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
1402 if( aFuncs[i].needCollSeq ){
1403 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1404 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1405 if( pFunc && aFuncs[i].needCollSeq ){
1406 pFunc->needCollSeq = 1;
1407 }
1408 }
1409 }
1410#ifndef SQLITE_OMIT_ALTERTABLE
1411 sqlite3AlterFunctions(db);
1412#endif
1413#ifndef SQLITE_OMIT_PARSER
1414 sqlite3AttachFunctions(db);
1415#endif
1416 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
1417 void *pArg = (void*)(int)aAggs[i].argType;
1418 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
1419 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
1420 if( aAggs[i].needCollSeq ){
1421 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
1422 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
1423 if( pFunc && aAggs[i].needCollSeq ){
1424 pFunc->needCollSeq = 1;
1425 }
1426 }
1427 }
1428 sqlite3RegisterDateTimeFunctions(db);
1429 if( !db->mallocFailed ){
1430 int rc = sqlite3_overload_function(db, "MATCH", 2);
1431 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
1432 if( rc==SQLITE_NOMEM ){
1433 db->mallocFailed = 1;
1434 }
1435 }
1436#ifdef SQLITE_SSE
1437 (void)sqlite3SseFunctions(db);
1438#endif
1439#ifdef SQLITE_CASE_SENSITIVE_LIKE
1440 sqlite3RegisterLikeFunctions(db, 1);
1441#else
1442 sqlite3RegisterLikeFunctions(db, 0);
1443#endif
1444}
1445
1446/*
1447** Set the LIKEOPT flag on the 2-argument function with the given name.
1448*/
1449static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
1450 FuncDef *pDef;
1451 pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
1452 if( pDef ){
1453 pDef->flags = flagVal;
1454 }
1455}
1456
1457/*
1458** Register the built-in LIKE and GLOB functions. The caseSensitive
1459** parameter determines whether or not the LIKE operator is case
1460** sensitive. GLOB is always case sensitive.
1461*/
1462void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
1463 struct compareInfo *pInfo;
1464 if( caseSensitive ){
1465 pInfo = (struct compareInfo*)&likeInfoAlt;
1466 }else{
1467 pInfo = (struct compareInfo*)&likeInfoNorm;
1468 }
1469 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1470 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
1471 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
1472 (struct compareInfo*)&globInfo, likeFunc, 0,0);
1473 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
1474 setLikeOptFlag(db, "like",
1475 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
1476}
1477
1478/*
1479** pExpr points to an expression which implements a function. If
1480** it is appropriate to apply the LIKE optimization to that function
1481** then set aWc[0] through aWc[2] to the wildcard characters and
1482** return TRUE. If the function is not a LIKE-style function then
1483** return FALSE.
1484*/
1485int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
1486 FuncDef *pDef;
1487 if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){
1488 return 0;
1489 }
1490 if( pExpr->pList->nExpr!=2 ){
1491 return 0;
1492 }
1493 pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
1494 SQLITE_UTF8, 0);
1495 if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
1496 return 0;
1497 }
1498
1499 /* The memcpy() statement assumes that the wildcard characters are
1500 ** the first three statements in the compareInfo structure. The
1501 ** asserts() that follow verify that assumption
1502 */
1503 memcpy(aWc, pDef->pUserData, 3);
1504 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
1505 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
1506 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
1507 *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
1508 return 1;
1509}
diff --git a/libraries/sqlite/win32/hash.c b/libraries/sqlite/win32/hash.c
new file mode 100755
index 0000000..a88d16b
--- /dev/null
+++ b/libraries/sqlite/win32/hash.c
@@ -0,0 +1,418 @@
1/*
2** 2001 September 22
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.
10**
11*************************************************************************
12** This is the implementation of generic hash-tables
13** used in SQLite.
14**
15** $Id: hash.c,v 1.24 2007/09/04 14:31:47 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18#include <assert.h>
19
20/* Turn bulk memory into a hash table object by initializing the
21** fields of the Hash structure.
22**
23** "pNew" is a pointer to the hash table that is to be initialized.
24** keyClass is one of the constants SQLITE_HASH_INT, SQLITE_HASH_POINTER,
25** SQLITE_HASH_BINARY, or SQLITE_HASH_STRING. The value of keyClass
26** determines what kind of key the hash table will use. "copyKey" is
27** true if the hash table should make its own private copy of keys and
28** false if it should just use the supplied pointer. CopyKey only makes
29** sense for SQLITE_HASH_STRING and SQLITE_HASH_BINARY and is ignored
30** for other key classes.
31*/
32void sqlite3HashInit(Hash *pNew, int keyClass, int copyKey){
33 assert( pNew!=0 );
34 assert( keyClass>=SQLITE_HASH_STRING && keyClass<=SQLITE_HASH_BINARY );
35 pNew->keyClass = keyClass;
36#if 0
37 if( keyClass==SQLITE_HASH_POINTER || keyClass==SQLITE_HASH_INT ) copyKey = 0;
38#endif
39 pNew->copyKey = copyKey;
40 pNew->first = 0;
41 pNew->count = 0;
42 pNew->htsize = 0;
43 pNew->ht = 0;
44}
45
46/* Remove all entries from a hash table. Reclaim all memory.
47** Call this routine to delete a hash table or to reset a hash table
48** to the empty state.
49*/
50void sqlite3HashClear(Hash *pH){
51 HashElem *elem; /* For looping over all elements of the table */
52
53 assert( pH!=0 );
54 elem = pH->first;
55 pH->first = 0;
56 if( pH->ht ) sqlite3_free(pH->ht);
57 pH->ht = 0;
58 pH->htsize = 0;
59 while( elem ){
60 HashElem *next_elem = elem->next;
61 if( pH->copyKey && elem->pKey ){
62 sqlite3_free(elem->pKey);
63 }
64 sqlite3_free(elem);
65 elem = next_elem;
66 }
67 pH->count = 0;
68}
69
70#if 0 /* NOT USED */
71/*
72** Hash and comparison functions when the mode is SQLITE_HASH_INT
73*/
74static int intHash(const void *pKey, int nKey){
75 return nKey ^ (nKey<<8) ^ (nKey>>8);
76}
77static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
78 return n2 - n1;
79}
80#endif
81
82#if 0 /* NOT USED */
83/*
84** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
85*/
86static int ptrHash(const void *pKey, int nKey){
87 uptr x = Addr(pKey);
88 return x ^ (x<<8) ^ (x>>8);
89}
90static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
91 if( pKey1==pKey2 ) return 0;
92 if( pKey1<pKey2 ) return -1;
93 return 1;
94}
95#endif
96
97/*
98** Hash and comparison functions when the mode is SQLITE_HASH_STRING
99*/
100static int strHash(const void *pKey, int nKey){
101 const char *z = (const char *)pKey;
102 int h = 0;
103 if( nKey<=0 ) nKey = strlen(z);
104 while( nKey > 0 ){
105 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
106 nKey--;
107 }
108 return h & 0x7fffffff;
109}
110static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
111 if( n1!=n2 ) return 1;
112 return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1);
113}
114
115/*
116** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
117*/
118static int binHash(const void *pKey, int nKey){
119 int h = 0;
120 const char *z = (const char *)pKey;
121 while( nKey-- > 0 ){
122 h = (h<<3) ^ h ^ *(z++);
123 }
124 return h & 0x7fffffff;
125}
126static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
127 if( n1!=n2 ) return 1;
128 return memcmp(pKey1,pKey2,n1);
129}
130
131/*
132** Return a pointer to the appropriate hash function given the key class.
133**
134** The C syntax in this function definition may be unfamilar to some
135** programmers, so we provide the following additional explanation:
136**
137** The name of the function is "hashFunction". The function takes a
138** single parameter "keyClass". The return value of hashFunction()
139** is a pointer to another function. Specifically, the return value
140** of hashFunction() is a pointer to a function that takes two parameters
141** with types "const void*" and "int" and returns an "int".
142*/
143static int (*hashFunction(int keyClass))(const void*,int){
144#if 0 /* HASH_INT and HASH_POINTER are never used */
145 switch( keyClass ){
146 case SQLITE_HASH_INT: return &intHash;
147 case SQLITE_HASH_POINTER: return &ptrHash;
148 case SQLITE_HASH_STRING: return &strHash;
149 case SQLITE_HASH_BINARY: return &binHash;;
150 default: break;
151 }
152 return 0;
153#else
154 if( keyClass==SQLITE_HASH_STRING ){
155 return &strHash;
156 }else{
157 assert( keyClass==SQLITE_HASH_BINARY );
158 return &binHash;
159 }
160#endif
161}
162
163/*
164** Return a pointer to the appropriate hash function given the key class.
165**
166** For help in interpreted the obscure C code in the function definition,
167** see the header comment on the previous function.
168*/
169static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
170#if 0 /* HASH_INT and HASH_POINTER are never used */
171 switch( keyClass ){
172 case SQLITE_HASH_INT: return &intCompare;
173 case SQLITE_HASH_POINTER: return &ptrCompare;
174 case SQLITE_HASH_STRING: return &strCompare;
175 case SQLITE_HASH_BINARY: return &binCompare;
176 default: break;
177 }
178 return 0;
179#else
180 if( keyClass==SQLITE_HASH_STRING ){
181 return &strCompare;
182 }else{
183 assert( keyClass==SQLITE_HASH_BINARY );
184 return &binCompare;
185 }
186#endif
187}
188
189/* Link an element into the hash table
190*/
191static void insertElement(
192 Hash *pH, /* The complete hash table */
193 struct _ht *pEntry, /* The entry into which pNew is inserted */
194 HashElem *pNew /* The element to be inserted */
195){
196 HashElem *pHead; /* First element already in pEntry */
197 pHead = pEntry->chain;
198 if( pHead ){
199 pNew->next = pHead;
200 pNew->prev = pHead->prev;
201 if( pHead->prev ){ pHead->prev->next = pNew; }
202 else { pH->first = pNew; }
203 pHead->prev = pNew;
204 }else{
205 pNew->next = pH->first;
206 if( pH->first ){ pH->first->prev = pNew; }
207 pNew->prev = 0;
208 pH->first = pNew;
209 }
210 pEntry->count++;
211 pEntry->chain = pNew;
212}
213
214
215/* Resize the hash table so that it cantains "new_size" buckets.
216** "new_size" must be a power of 2. The hash table might fail
217** to resize if sqlite3_malloc() fails.
218*/
219static void rehash(Hash *pH, int new_size){
220 struct _ht *new_ht; /* The new hash table */
221 HashElem *elem, *next_elem; /* For looping over existing elements */
222 int (*xHash)(const void*,int); /* The hash function */
223
224 assert( (new_size & (new_size-1))==0 );
225
226 /* There is a call to sqlite3_malloc() inside rehash(). If there is
227 ** already an allocation at pH->ht, then if this malloc() fails it
228 ** is benign (since failing to resize a hash table is a performance
229 ** hit only, not a fatal error).
230 */
231 sqlite3MallocBenignFailure(pH->htsize>0);
232
233 new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) );
234 if( new_ht==0 ) return;
235 if( pH->ht ) sqlite3_free(pH->ht);
236 pH->ht = new_ht;
237 pH->htsize = new_size;
238 xHash = hashFunction(pH->keyClass);
239 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
240 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
241 next_elem = elem->next;
242 insertElement(pH, &new_ht[h], elem);
243 }
244}
245
246/* This function (for internal use only) locates an element in an
247** hash table that matches the given key. The hash for this key has
248** already been computed and is passed as the 4th parameter.
249*/
250static HashElem *findElementGivenHash(
251 const Hash *pH, /* The pH to be searched */
252 const void *pKey, /* The key we are searching for */
253 int nKey,
254 int h /* The hash for this key. */
255){
256 HashElem *elem; /* Used to loop thru the element list */
257 int count; /* Number of elements left to test */
258 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
259
260 if( pH->ht ){
261 struct _ht *pEntry = &pH->ht[h];
262 elem = pEntry->chain;
263 count = pEntry->count;
264 xCompare = compareFunction(pH->keyClass);
265 while( count-- && elem ){
266 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
267 return elem;
268 }
269 elem = elem->next;
270 }
271 }
272 return 0;
273}
274
275/* Remove a single entry from the hash table given a pointer to that
276** element and a hash on the element's key.
277*/
278static void removeElementGivenHash(
279 Hash *pH, /* The pH containing "elem" */
280 HashElem* elem, /* The element to be removed from the pH */
281 int h /* Hash value for the element */
282){
283 struct _ht *pEntry;
284 if( elem->prev ){
285 elem->prev->next = elem->next;
286 }else{
287 pH->first = elem->next;
288 }
289 if( elem->next ){
290 elem->next->prev = elem->prev;
291 }
292 pEntry = &pH->ht[h];
293 if( pEntry->chain==elem ){
294 pEntry->chain = elem->next;
295 }
296 pEntry->count--;
297 if( pEntry->count<=0 ){
298 pEntry->chain = 0;
299 }
300 if( pH->copyKey ){
301 sqlite3_free(elem->pKey);
302 }
303 sqlite3_free( elem );
304 pH->count--;
305 if( pH->count<=0 ){
306 assert( pH->first==0 );
307 assert( pH->count==0 );
308 sqlite3HashClear(pH);
309 }
310}
311
312/* Attempt to locate an element of the hash table pH with a key
313** that matches pKey,nKey. Return a pointer to the corresponding
314** HashElem structure for this element if it is found, or NULL
315** otherwise.
316*/
317HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, int nKey){
318 int h; /* A hash on key */
319 HashElem *elem; /* The element that matches key */
320 int (*xHash)(const void*,int); /* The hash function */
321
322 if( pH==0 || pH->ht==0 ) return 0;
323 xHash = hashFunction(pH->keyClass);
324 assert( xHash!=0 );
325 h = (*xHash)(pKey,nKey);
326 assert( (pH->htsize & (pH->htsize-1))==0 );
327 elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
328 return elem;
329}
330
331/* Attempt to locate an element of the hash table pH with a key
332** that matches pKey,nKey. Return the data for this element if it is
333** found, or NULL if there is no match.
334*/
335void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
336 HashElem *elem; /* The element that matches key */
337 elem = sqlite3HashFindElem(pH, pKey, nKey);
338 return elem ? elem->data : 0;
339}
340
341/* Insert an element into the hash table pH. The key is pKey,nKey
342** and the data is "data".
343**
344** If no element exists with a matching key, then a new
345** element is created. A copy of the key is made if the copyKey
346** flag is set. NULL is returned.
347**
348** If another element already exists with the same key, then the
349** new data replaces the old data and the old data is returned.
350** The key is not copied in this instance. If a malloc fails, then
351** the new data is returned and the hash table is unchanged.
352**
353** If the "data" parameter to this function is NULL, then the
354** element corresponding to "key" is removed from the hash table.
355*/
356void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
357 int hraw; /* Raw hash value of the key */
358 int h; /* the hash of the key modulo hash table size */
359 HashElem *elem; /* Used to loop thru the element list */
360 HashElem *new_elem; /* New element added to the pH */
361 int (*xHash)(const void*,int); /* The hash function */
362
363 assert( pH!=0 );
364 xHash = hashFunction(pH->keyClass);
365 assert( xHash!=0 );
366 hraw = (*xHash)(pKey, nKey);
367 assert( (pH->htsize & (pH->htsize-1))==0 );
368 h = hraw & (pH->htsize-1);
369 elem = findElementGivenHash(pH,pKey,nKey,h);
370 if( elem ){
371 void *old_data = elem->data;
372 if( data==0 ){
373 removeElementGivenHash(pH,elem,h);
374 }else{
375 elem->data = data;
376 if( !pH->copyKey ){
377 elem->pKey = (void *)pKey;
378 }
379 assert(nKey==elem->nKey);
380 }
381 return old_data;
382 }
383 if( data==0 ) return 0;
384 new_elem = (HashElem*)sqlite3_malloc( sizeof(HashElem) );
385 if( new_elem==0 ) return data;
386 if( pH->copyKey && pKey!=0 ){
387 new_elem->pKey = sqlite3_malloc( nKey );
388 if( new_elem->pKey==0 ){
389 sqlite3_free(new_elem);
390 return data;
391 }
392 memcpy((void*)new_elem->pKey, pKey, nKey);
393 }else{
394 new_elem->pKey = (void*)pKey;
395 }
396 new_elem->nKey = nKey;
397 pH->count++;
398 if( pH->htsize==0 ){
399 rehash(pH,8);
400 if( pH->htsize==0 ){
401 pH->count = 0;
402 if( pH->copyKey ){
403 sqlite3_free(new_elem->pKey);
404 }
405 sqlite3_free(new_elem);
406 return data;
407 }
408 }
409 if( pH->count > pH->htsize ){
410 rehash(pH,pH->htsize*2);
411 }
412 assert( pH->htsize>0 );
413 assert( (pH->htsize & (pH->htsize-1))==0 );
414 h = hraw & (pH->htsize-1);
415 insertElement(pH, &pH->ht[h], new_elem);
416 new_elem->data = data;
417 return 0;
418}
diff --git a/libraries/sqlite/win32/hash.h b/libraries/sqlite/win32/hash.h
new file mode 100755
index 0000000..e3274e9
--- /dev/null
+++ b/libraries/sqlite/win32/hash.h
@@ -0,0 +1,110 @@
1/*
2** 2001 September 22
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.
10**
11*************************************************************************
12** This is the header file for the generic hash-table implemenation
13** used in SQLite.
14**
15** $Id: hash.h,v 1.11 2007/09/04 14:31:47 danielk1977 Exp $
16*/
17#ifndef _SQLITE_HASH_H_
18#define _SQLITE_HASH_H_
19
20/* Forward declarations of structures. */
21typedef struct Hash Hash;
22typedef struct HashElem HashElem;
23
24/* A complete hash table is an instance of the following structure.
25** The internals of this structure are intended to be opaque -- client
26** code should not attempt to access or modify the fields of this structure
27** directly. Change this structure only by using the routines below.
28** However, many of the "procedures" and "functions" for modifying and
29** accessing this structure are really macros, so we can't really make
30** this structure opaque.
31*/
32struct Hash {
33 char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
34 char copyKey; /* True if copy of key made on insert */
35 int count; /* Number of entries in this table */
36 int htsize; /* Number of buckets in the hash table */
37 HashElem *first; /* The first element of the array */
38 struct _ht { /* the hash table */
39 int count; /* Number of entries with this hash */
40 HashElem *chain; /* Pointer to first entry with this hash */
41 } *ht;
42};
43
44/* Each element in the hash table is an instance of the following
45** structure. All elements are stored on a single doubly-linked list.
46**
47** Again, this structure is intended to be opaque, but it can't really
48** be opaque because it is used by macros.
49*/
50struct HashElem {
51 HashElem *next, *prev; /* Next and previous elements in the table */
52 void *data; /* Data associated with this element */
53 void *pKey; int nKey; /* Key associated with this element */
54};
55
56/*
57** There are 4 different modes of operation for a hash table:
58**
59** SQLITE_HASH_INT nKey is used as the key and pKey is ignored.
60**
61** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored.
62**
63** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long
64** (including the null-terminator, if any). Case
65** is ignored in comparisons.
66**
67** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long.
68** memcmp() is used to compare keys.
69**
70** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY
71** if the copyKey parameter to HashInit is 1.
72*/
73/* #define SQLITE_HASH_INT 1 // NOT USED */
74/* #define SQLITE_HASH_POINTER 2 // NOT USED */
75#define SQLITE_HASH_STRING 3
76#define SQLITE_HASH_BINARY 4
77
78/*
79** Access routines. To delete, insert a NULL pointer.
80*/
81void sqlite3HashInit(Hash*, int keytype, int copyKey);
82void *sqlite3HashInsert(Hash*, const void *pKey, int nKey, void *pData);
83void *sqlite3HashFind(const Hash*, const void *pKey, int nKey);
84HashElem *sqlite3HashFindElem(const Hash*, const void *pKey, int nKey);
85void sqlite3HashClear(Hash*);
86
87/*
88** Macros for looping over all elements of a hash table. The idiom is
89** like this:
90**
91** Hash h;
92** HashElem *p;
93** ...
94** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
95** SomeStructure *pData = sqliteHashData(p);
96** // do something with pData
97** }
98*/
99#define sqliteHashFirst(H) ((H)->first)
100#define sqliteHashNext(E) ((E)->next)
101#define sqliteHashData(E) ((E)->data)
102#define sqliteHashKey(E) ((E)->pKey)
103#define sqliteHashKeysize(E) ((E)->nKey)
104
105/*
106** Number of entries in a hash table
107*/
108#define sqliteHashCount(H) ((H)->count)
109
110#endif /* _SQLITE_HASH_H_ */
diff --git a/libraries/sqlite/win32/insert.c b/libraries/sqlite/win32/insert.c
new file mode 100755
index 0000000..9d68c95
--- /dev/null
+++ b/libraries/sqlite/win32/insert.c
@@ -0,0 +1,1605 @@
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.
10**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle INSERT statements in SQLite.
14**
15** $Id: insert.c,v 1.192 2007/09/03 17:30:07 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18
19/*
20** Set P3 of the most recently inserted opcode to a column affinity
21** string for index pIdx. A column affinity string has one character
22** for each column in the table, according to the affinity of the column:
23**
24** Character Column affinity
25** ------------------------------
26** 'a' TEXT
27** 'b' NONE
28** 'c' NUMERIC
29** 'd' INTEGER
30** 'e' REAL
31*/
32void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
33 if( !pIdx->zColAff ){
34 /* The first time a column affinity string for a particular index is
35 ** required, it is allocated and populated here. It is then stored as
36 ** a member of the Index structure for subsequent use.
37 **
38 ** The column affinity string will eventually be deleted by
39 ** sqliteDeleteIndex() when the Index structure itself is cleaned
40 ** up.
41 */
42 int n;
43 Table *pTab = pIdx->pTable;
44 sqlite3 *db = sqlite3VdbeDb(v);
45 pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+1);
46 if( !pIdx->zColAff ){
47 return;
48 }
49 for(n=0; n<pIdx->nColumn; n++){
50 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
51 }
52 pIdx->zColAff[pIdx->nColumn] = '\0';
53 }
54
55 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
56}
57
58/*
59** Set P3 of the most recently inserted opcode to a column affinity
60** string for table pTab. A column affinity string has one character
61** for each column indexed by the index, according to the affinity of the
62** column:
63**
64** Character Column affinity
65** ------------------------------
66** 'a' TEXT
67** 'b' NONE
68** 'c' NUMERIC
69** 'd' INTEGER
70** 'e' REAL
71*/
72void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
73 /* The first time a column affinity string for a particular table
74 ** is required, it is allocated and populated here. It is then
75 ** stored as a member of the Table structure for subsequent use.
76 **
77 ** The column affinity string will eventually be deleted by
78 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
79 */
80 if( !pTab->zColAff ){
81 char *zColAff;
82 int i;
83 sqlite3 *db = sqlite3VdbeDb(v);
84
85 zColAff = (char *)sqlite3DbMallocZero(db, pTab->nCol+1);
86 if( !zColAff ){
87 return;
88 }
89
90 for(i=0; i<pTab->nCol; i++){
91 zColAff[i] = pTab->aCol[i].affinity;
92 }
93 zColAff[pTab->nCol] = '\0';
94
95 pTab->zColAff = zColAff;
96 }
97
98 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
99}
100
101/*
102** Return non-zero if SELECT statement p opens the table with rootpage
103** iTab in database iDb. This is used to see if a statement of the form
104** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
105** table for the results of the SELECT.
106**
107** No checking is done for sub-selects that are part of expressions.
108*/
109static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
110 int i;
111 struct SrcList_item *pItem;
112 if( p->pSrc==0 ) return 0;
113 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
114 if( pItem->pSelect ){
115 if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
116 }else{
117 if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
118 }
119 }
120 return 0;
121}
122
123#ifndef SQLITE_OMIT_AUTOINCREMENT
124/*
125** Write out code to initialize the autoincrement logic. This code
126** looks up the current autoincrement value in the sqlite_sequence
127** table and stores that value in a memory cell. Code generated by
128** autoIncStep() will keep that memory cell holding the largest
129** rowid value. Code generated by autoIncEnd() will write the new
130** largest value of the counter back into the sqlite_sequence table.
131**
132** This routine returns the index of the mem[] cell that contains
133** the maximum rowid counter.
134**
135** Two memory cells are allocated. The next memory cell after the
136** one returned holds the rowid in sqlite_sequence where we will
137** write back the revised maximum rowid.
138*/
139static int autoIncBegin(
140 Parse *pParse, /* Parsing context */
141 int iDb, /* Index of the database holding pTab */
142 Table *pTab /* The table we are writing to */
143){
144 int memId = 0;
145 if( pTab->autoInc ){
146 Vdbe *v = pParse->pVdbe;
147 Db *pDb = &pParse->db->aDb[iDb];
148 int iCur = pParse->nTab;
149 int addr;
150 assert( v );
151 addr = sqlite3VdbeCurrentAddr(v);
152 memId = pParse->nMem+1;
153 pParse->nMem += 2;
154 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
155 sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13);
156 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
157 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
158 sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12);
159 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
160 sqlite3VdbeAddOp(v, OP_MemStore, memId-1, 1);
161 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
162 sqlite3VdbeAddOp(v, OP_MemStore, memId, 1);
163 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13);
164 sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4);
165 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
166 }
167 return memId;
168}
169
170/*
171** Update the maximum rowid for an autoincrement calculation.
172**
173** This routine should be called when the top of the stack holds a
174** new rowid that is about to be inserted. If that new rowid is
175** larger than the maximum rowid in the memId memory cell, then the
176** memory cell is updated. The stack is unchanged.
177*/
178static void autoIncStep(Parse *pParse, int memId){
179 if( memId>0 ){
180 sqlite3VdbeAddOp(pParse->pVdbe, OP_MemMax, memId, 0);
181 }
182}
183
184/*
185** After doing one or more inserts, the maximum rowid is stored
186** in mem[memId]. Generate code to write this value back into the
187** the sqlite_sequence table.
188*/
189static void autoIncEnd(
190 Parse *pParse, /* The parsing context */
191 int iDb, /* Index of the database holding pTab */
192 Table *pTab, /* Table we are inserting into */
193 int memId /* Memory cell holding the maximum rowid */
194){
195 if( pTab->autoInc ){
196 int iCur = pParse->nTab;
197 Vdbe *v = pParse->pVdbe;
198 Db *pDb = &pParse->db->aDb[iDb];
199 int addr;
200 assert( v );
201 addr = sqlite3VdbeCurrentAddr(v);
202 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
203 sqlite3VdbeAddOp(v, OP_MemLoad, memId-1, 0);
204 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7);
205 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
206 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
207 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
208 sqlite3VdbeAddOp(v, OP_MemLoad, memId, 0);
209 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
210 sqlite3VdbeAddOp(v, OP_Insert, iCur, OPFLAG_APPEND);
211 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
212 }
213}
214#else
215/*
216** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
217** above are all no-ops
218*/
219# define autoIncBegin(A,B,C) (0)
220# define autoIncStep(A,B)
221# define autoIncEnd(A,B,C,D)
222#endif /* SQLITE_OMIT_AUTOINCREMENT */
223
224
225/* Forward declaration */
226static int xferOptimization(
227 Parse *pParse, /* Parser context */
228 Table *pDest, /* The table we are inserting into */
229 Select *pSelect, /* A SELECT statement to use as the data source */
230 int onError, /* How to handle constraint errors */
231 int iDbDest /* The database of pDest */
232);
233
234/*
235** This routine is call to handle SQL of the following forms:
236**
237** insert into TABLE (IDLIST) values(EXPRLIST)
238** insert into TABLE (IDLIST) select
239**
240** The IDLIST following the table name is always optional. If omitted,
241** then a list of all columns for the table is substituted. The IDLIST
242** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
243**
244** The pList parameter holds EXPRLIST in the first form of the INSERT
245** statement above, and pSelect is NULL. For the second form, pList is
246** NULL and pSelect is a pointer to the select statement used to generate
247** data for the insert.
248**
249** The code generated follows one of four templates. For a simple
250** select with data coming from a VALUES clause, the code executes
251** once straight down through. The template looks like this:
252**
253** open write cursor to <table> and its indices
254** puts VALUES clause expressions onto the stack
255** write the resulting record into <table>
256** cleanup
257**
258** The three remaining templates assume the statement is of the form
259**
260** INSERT INTO <table> SELECT ...
261**
262** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
263** in other words if the SELECT pulls all columns from a single table
264** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
265** if <table2> and <table1> are distinct tables but have identical
266** schemas, including all the same indices, then a special optimization
267** is invoked that copies raw records from <table2> over to <table1>.
268** See the xferOptimization() function for the implementation of this
269** template. This is the second template.
270**
271** open a write cursor to <table>
272** open read cursor on <table2>
273** transfer all records in <table2> over to <table>
274** close cursors
275** foreach index on <table>
276** open a write cursor on the <table> index
277** open a read cursor on the corresponding <table2> index
278** transfer all records from the read to the write cursors
279** close cursors
280** end foreach
281**
282** The third template is for when the second template does not apply
283** and the SELECT clause does not read from <table> at any time.
284** The generated code follows this template:
285**
286** goto B
287** A: setup for the SELECT
288** loop over the rows in the SELECT
289** gosub C
290** end loop
291** cleanup after the SELECT
292** goto D
293** B: open write cursor to <table> and its indices
294** goto A
295** C: insert the select result into <table>
296** return
297** D: cleanup
298**
299** The fourth template is used if the insert statement takes its
300** values from a SELECT but the data is being inserted into a table
301** that is also read as part of the SELECT. In the third form,
302** we have to use a intermediate table to store the results of
303** the select. The template is like this:
304**
305** goto B
306** A: setup for the SELECT
307** loop over the tables in the SELECT
308** gosub C
309** end loop
310** cleanup after the SELECT
311** goto D
312** C: insert the select result into the intermediate table
313** return
314** B: open a cursor to an intermediate table
315** goto A
316** D: open write cursor to <table> and its indices
317** loop over the intermediate table
318** transfer values form intermediate table into <table>
319** end the loop
320** cleanup
321*/
322void sqlite3Insert(
323 Parse *pParse, /* Parser context */
324 SrcList *pTabList, /* Name of table into which we are inserting */
325 ExprList *pList, /* List of values to be inserted */
326 Select *pSelect, /* A SELECT statement to use as the data source */
327 IdList *pColumn, /* Column names corresponding to IDLIST. */
328 int onError /* How to handle constraint errors */
329){
330 Table *pTab; /* The table to insert into */
331 char *zTab; /* Name of the table into which we are inserting */
332 const char *zDb; /* Name of the database holding this table */
333 int i, j, idx; /* Loop counters */
334 Vdbe *v; /* Generate code into this virtual machine */
335 Index *pIdx; /* For looping over indices of the table */
336 int nColumn; /* Number of columns in the data */
337 int base = 0; /* VDBE Cursor number for pTab */
338 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
339 sqlite3 *db; /* The main database structure */
340 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
341 int endOfLoop; /* Label for the end of the insertion loop */
342 int useTempTable = 0; /* Store SELECT results in intermediate table */
343 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
344 int iSelectLoop = 0; /* Address of code that implements the SELECT */
345 int iCleanup = 0; /* Address of the cleanup code */
346 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
347 int iCntMem = 0; /* Memory cell used for the row counter */
348 int newIdx = -1; /* Cursor for the NEW table */
349 Db *pDb; /* The database containing table being inserted into */
350 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
351 int appendFlag = 0; /* True if the insert is likely to be an append */
352 int iDb;
353
354 int nHidden = 0;
355
356#ifndef SQLITE_OMIT_TRIGGER
357 int isView; /* True if attempting to insert into a view */
358 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
359#endif
360
361 db = pParse->db;
362 if( pParse->nErr || db->mallocFailed ){
363 goto insert_cleanup;
364 }
365
366 /* Locate the table into which we will be inserting new information.
367 */
368 assert( pTabList->nSrc==1 );
369 zTab = pTabList->a[0].zName;
370 if( zTab==0 ) goto insert_cleanup;
371 pTab = sqlite3SrcListLookup(pParse, pTabList);
372 if( pTab==0 ){
373 goto insert_cleanup;
374 }
375 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
376 assert( iDb<db->nDb );
377 pDb = &db->aDb[iDb];
378 zDb = pDb->zName;
379 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
380 goto insert_cleanup;
381 }
382
383 /* Figure out if we have any triggers and if the table being
384 ** inserted into is a view
385 */
386#ifndef SQLITE_OMIT_TRIGGER
387 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
388 isView = pTab->pSelect!=0;
389#else
390# define triggers_exist 0
391# define isView 0
392#endif
393#ifdef SQLITE_OMIT_VIEW
394# undef isView
395# define isView 0
396#endif
397
398 /* Ensure that:
399 * (a) the table is not read-only,
400 * (b) that if it is a view then ON INSERT triggers exist
401 */
402 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
403 goto insert_cleanup;
404 }
405 assert( pTab!=0 );
406
407 /* If pTab is really a view, make sure it has been initialized.
408 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
409 ** module table).
410 */
411 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
412 goto insert_cleanup;
413 }
414
415 /* Allocate a VDBE
416 */
417 v = sqlite3GetVdbe(pParse);
418 if( v==0 ) goto insert_cleanup;
419 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
420 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
421
422 /* if there are row triggers, allocate a temp table for new.* references. */
423 if( triggers_exist ){
424 newIdx = pParse->nTab++;
425 }
426
427#ifndef SQLITE_OMIT_XFER_OPT
428 /* If the statement is of the form
429 **
430 ** INSERT INTO <table1> SELECT * FROM <table2>;
431 **
432 ** Then special optimizations can be applied that make the transfer
433 ** very fast and which reduce fragmentation of indices.
434 */
435 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
436 assert( !triggers_exist );
437 assert( pList==0 );
438 goto insert_cleanup;
439 }
440#endif /* SQLITE_OMIT_XFER_OPT */
441
442 /* If this is an AUTOINCREMENT table, look up the sequence number in the
443 ** sqlite_sequence table and store it in memory cell counterMem. Also
444 ** remember the rowid of the sqlite_sequence table entry in memory cell
445 ** counterRowid.
446 */
447 counterMem = autoIncBegin(pParse, iDb, pTab);
448
449 /* Figure out how many columns of data are supplied. If the data
450 ** is coming from a SELECT statement, then this step also generates
451 ** all the code to implement the SELECT statement and invoke a subroutine
452 ** to process each row of the result. (Template 2.) If the SELECT
453 ** statement uses the the table that is being inserted into, then the
454 ** subroutine is also coded here. That subroutine stores the SELECT
455 ** results in a temporary table. (Template 3.)
456 */
457 if( pSelect ){
458 /* Data is coming from a SELECT. Generate code to implement that SELECT
459 */
460 int rc, iInitCode;
461 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
462 iSelectLoop = sqlite3VdbeCurrentAddr(v);
463 iInsertBlock = sqlite3VdbeMakeLabel(v);
464
465 /* Resolve the expressions in the SELECT statement and execute it. */
466 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
467 if( rc || pParse->nErr || db->mallocFailed ){
468 goto insert_cleanup;
469 }
470
471 iCleanup = sqlite3VdbeMakeLabel(v);
472 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
473 assert( pSelect->pEList );
474 nColumn = pSelect->pEList->nExpr;
475
476 /* Set useTempTable to TRUE if the result of the SELECT statement
477 ** should be written into a temporary table. Set to FALSE if each
478 ** row of the SELECT can be written directly into the result table.
479 **
480 ** A temp table must be used if the table being updated is also one
481 ** of the tables being read by the SELECT statement. Also use a
482 ** temp table in the case of row triggers.
483 */
484 if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
485 useTempTable = 1;
486 }
487
488 if( useTempTable ){
489 /* Generate the subroutine that SELECT calls to process each row of
490 ** the result. Store the result in a temporary table
491 */
492 srcTab = pParse->nTab++;
493 sqlite3VdbeResolveLabel(v, iInsertBlock);
494 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
495 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
496 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
497 sqlite3VdbeAddOp(v, OP_Insert, srcTab, OPFLAG_APPEND);
498 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
499
500 /* The following code runs first because the GOTO at the very top
501 ** of the program jumps to it. Create the temporary table, then jump
502 ** back up and execute the SELECT code above.
503 */
504 sqlite3VdbeJumpHere(v, iInitCode);
505 sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0);
506 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
507 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
508 sqlite3VdbeResolveLabel(v, iCleanup);
509 }else{
510 sqlite3VdbeJumpHere(v, iInitCode);
511 }
512 }else{
513 /* This is the case if the data for the INSERT is coming from a VALUES
514 ** clause
515 */
516 NameContext sNC;
517 memset(&sNC, 0, sizeof(sNC));
518 sNC.pParse = pParse;
519 srcTab = -1;
520 useTempTable = 0;
521 nColumn = pList ? pList->nExpr : 0;
522 for(i=0; i<nColumn; i++){
523 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
524 goto insert_cleanup;
525 }
526 }
527 }
528
529 /* Make sure the number of columns in the source data matches the number
530 ** of columns to be inserted into the table.
531 */
532 if( IsVirtual(pTab) ){
533 for(i=0; i<pTab->nCol; i++){
534 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
535 }
536 }
537 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
538 sqlite3ErrorMsg(pParse,
539 "table %S has %d columns but %d values were supplied",
540 pTabList, 0, pTab->nCol, nColumn);
541 goto insert_cleanup;
542 }
543 if( pColumn!=0 && nColumn!=pColumn->nId ){
544 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
545 goto insert_cleanup;
546 }
547
548 /* If the INSERT statement included an IDLIST term, then make sure
549 ** all elements of the IDLIST really are columns of the table and
550 ** remember the column indices.
551 **
552 ** If the table has an INTEGER PRIMARY KEY column and that column
553 ** is named in the IDLIST, then record in the keyColumn variable
554 ** the index into IDLIST of the primary key column. keyColumn is
555 ** the index of the primary key as it appears in IDLIST, not as
556 ** is appears in the original table. (The index of the primary
557 ** key in the original table is pTab->iPKey.)
558 */
559 if( pColumn ){
560 for(i=0; i<pColumn->nId; i++){
561 pColumn->a[i].idx = -1;
562 }
563 for(i=0; i<pColumn->nId; i++){
564 for(j=0; j<pTab->nCol; j++){
565 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
566 pColumn->a[i].idx = j;
567 if( j==pTab->iPKey ){
568 keyColumn = i;
569 }
570 break;
571 }
572 }
573 if( j>=pTab->nCol ){
574 if( sqlite3IsRowid(pColumn->a[i].zName) ){
575 keyColumn = i;
576 }else{
577 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
578 pTabList, 0, pColumn->a[i].zName);
579 pParse->nErr++;
580 goto insert_cleanup;
581 }
582 }
583 }
584 }
585
586 /* If there is no IDLIST term but the table has an integer primary
587 ** key, the set the keyColumn variable to the primary key column index
588 ** in the original table definition.
589 */
590 if( pColumn==0 && nColumn>0 ){
591 keyColumn = pTab->iPKey;
592 }
593
594 /* Open the temp table for FOR EACH ROW triggers
595 */
596 if( triggers_exist ){
597 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
598 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
599 }
600
601 /* Initialize the count of rows to be inserted
602 */
603 if( db->flags & SQLITE_CountRows ){
604 iCntMem = pParse->nMem++;
605 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
606 }
607
608 /* Open tables and indices if there are no row triggers */
609 if( !triggers_exist ){
610 base = pParse->nTab;
611 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
612 }
613
614 /* If the data source is a temporary table, then we have to create
615 ** a loop because there might be multiple rows of data. If the data
616 ** source is a subroutine call from the SELECT statement, then we need
617 ** to launch the SELECT statement processing.
618 */
619 if( useTempTable ){
620 iBreak = sqlite3VdbeMakeLabel(v);
621 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
622 iCont = sqlite3VdbeCurrentAddr(v);
623 }else if( pSelect ){
624 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
625 sqlite3VdbeResolveLabel(v, iInsertBlock);
626 }
627
628 /* Run the BEFORE and INSTEAD OF triggers, if there are any
629 */
630 endOfLoop = sqlite3VdbeMakeLabel(v);
631 if( triggers_exist & TRIGGER_BEFORE ){
632
633 /* build the NEW.* reference row. Note that if there is an INTEGER
634 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
635 ** translated into a unique ID for the row. But on a BEFORE trigger,
636 ** we do not know what the unique ID will be (because the insert has
637 ** not happened yet) so we substitute a rowid of -1
638 */
639 if( keyColumn<0 ){
640 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
641 }else if( useTempTable ){
642 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
643 }else{
644 assert( pSelect==0 ); /* Otherwise useTempTable is true */
645 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
646 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
647 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
648 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
649 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
650 }
651
652 /* Cannot have triggers on a virtual table. If it were possible,
653 ** this block would have to account for hidden column.
654 */
655 assert(!IsVirtual(pTab));
656
657 /* Create the new column data
658 */
659 for(i=0; i<pTab->nCol; i++){
660 if( pColumn==0 ){
661 j = i;
662 }else{
663 for(j=0; j<pColumn->nId; j++){
664 if( pColumn->a[j].idx==i ) break;
665 }
666 }
667 if( pColumn && j>=pColumn->nId ){
668 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
669 }else if( useTempTable ){
670 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
671 }else{
672 assert( pSelect==0 ); /* Otherwise useTempTable is true */
673 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
674 }
675 }
676 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
677
678 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
679 ** do not attempt any conversions before assembling the record.
680 ** If this is a real table, attempt conversions as required by the
681 ** table column affinities.
682 */
683 if( !isView ){
684 sqlite3TableAffinityStr(v, pTab);
685 }
686 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
687
688 /* Fire BEFORE or INSTEAD OF triggers */
689 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
690 newIdx, -1, onError, endOfLoop) ){
691 goto insert_cleanup;
692 }
693 }
694
695 /* If any triggers exists, the opening of tables and indices is deferred
696 ** until now.
697 */
698 if( triggers_exist && !isView ){
699 base = pParse->nTab;
700 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
701 }
702
703 /* Push the record number for the new entry onto the stack. The
704 ** record number is a randomly generate integer created by NewRowid
705 ** except when the table has an INTEGER PRIMARY KEY column, in which
706 ** case the record number is the same as that column.
707 */
708 if( !isView ){
709 if( IsVirtual(pTab) ){
710 /* The row that the VUpdate opcode will delete: none */
711 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
712 }
713 if( keyColumn>=0 ){
714 if( useTempTable ){
715 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
716 }else if( pSelect ){
717 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
718 }else{
719 VdbeOp *pOp;
720 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
721 pOp = sqlite3VdbeGetOp(v, sqlite3VdbeCurrentAddr(v) - 1);
722 if( pOp && pOp->opcode==OP_Null ){
723 appendFlag = 1;
724 pOp->opcode = OP_NewRowid;
725 pOp->p1 = base;
726 pOp->p2 = counterMem;
727 }
728 }
729 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
730 ** to generate a unique primary key value.
731 */
732 if( !appendFlag ){
733 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
734 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
735 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
736 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
737 }
738 }else if( IsVirtual(pTab) ){
739 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
740 }else{
741 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
742 appendFlag = 1;
743 }
744 autoIncStep(pParse, counterMem);
745
746 /* Push onto the stack, data for all columns of the new entry, beginning
747 ** with the first column.
748 */
749 nHidden = 0;
750 for(i=0; i<pTab->nCol; i++){
751 if( i==pTab->iPKey ){
752 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
753 ** Whenever this column is read, the record number will be substituted
754 ** in its place. So will fill this column with a NULL to avoid
755 ** taking up data space with information that will never be used. */
756 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
757 continue;
758 }
759 if( pColumn==0 ){
760 if( IsHiddenColumn(&pTab->aCol[i]) ){
761 assert( IsVirtual(pTab) );
762 j = -1;
763 nHidden++;
764 }else{
765 j = i - nHidden;
766 }
767 }else{
768 for(j=0; j<pColumn->nId; j++){
769 if( pColumn->a[j].idx==i ) break;
770 }
771 }
772 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
773 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
774 }else if( useTempTable ){
775 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
776 }else if( pSelect ){
777 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1);
778 }else{
779 sqlite3ExprCode(pParse, pList->a[j].pExpr);
780 }
781 }
782
783 /* Generate code to check constraints and generate index keys and
784 ** do the insertion.
785 */
786#ifndef SQLITE_OMIT_VIRTUALTABLE
787 if( IsVirtual(pTab) ){
788 pParse->pVirtualLock = pTab;
789 sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2,
790 (const char*)pTab->pVtab, P3_VTAB);
791 }else
792#endif
793 {
794 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
795 0, onError, endOfLoop);
796 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
797 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
798 appendFlag);
799 }
800 }
801
802 /* Update the count of rows that are inserted
803 */
804 if( (db->flags & SQLITE_CountRows)!=0 ){
805 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
806 }
807
808 if( triggers_exist ){
809 /* Close all tables opened */
810 if( !isView ){
811 sqlite3VdbeAddOp(v, OP_Close, base, 0);
812 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
813 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
814 }
815 }
816
817 /* Code AFTER triggers */
818 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
819 newIdx, -1, onError, endOfLoop) ){
820 goto insert_cleanup;
821 }
822 }
823
824 /* The bottom of the loop, if the data source is a SELECT statement
825 */
826 sqlite3VdbeResolveLabel(v, endOfLoop);
827 if( useTempTable ){
828 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
829 sqlite3VdbeResolveLabel(v, iBreak);
830 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
831 }else if( pSelect ){
832 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
833 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
834 sqlite3VdbeResolveLabel(v, iCleanup);
835 }
836
837 if( !triggers_exist && !IsVirtual(pTab) ){
838 /* Close all tables opened */
839 sqlite3VdbeAddOp(v, OP_Close, base, 0);
840 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
841 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
842 }
843 }
844
845 /* Update the sqlite_sequence table by storing the content of the
846 ** counter value in memory counterMem back into the sqlite_sequence
847 ** table.
848 */
849 autoIncEnd(pParse, iDb, pTab, counterMem);
850
851 /*
852 ** Return the number of rows inserted. If this routine is
853 ** generating code because of a call to sqlite3NestedParse(), do not
854 ** invoke the callback function.
855 */
856 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
857 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
858 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
859 sqlite3VdbeSetNumCols(v, 1);
860 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC);
861 }
862
863insert_cleanup:
864 sqlite3SrcListDelete(pTabList);
865 sqlite3ExprListDelete(pList);
866 sqlite3SelectDelete(pSelect);
867 sqlite3IdListDelete(pColumn);
868}
869
870/*
871** Generate code to do a constraint check prior to an INSERT or an UPDATE.
872**
873** When this routine is called, the stack contains (from bottom to top)
874** the following values:
875**
876** 1. The rowid of the row to be updated before the update. This
877** value is omitted unless we are doing an UPDATE that involves a
878** change to the record number.
879**
880** 2. The rowid of the row after the update.
881**
882** 3. The data in the first column of the entry after the update.
883**
884** i. Data from middle columns...
885**
886** N. The data in the last column of the entry after the update.
887**
888** The old rowid shown as entry (1) above is omitted unless both isUpdate
889** and rowidChng are 1. isUpdate is true for UPDATEs and false for
890** INSERTs and rowidChng is true if the record number is being changed.
891**
892** The code generated by this routine pushes additional entries onto
893** the stack which are the keys for new index entries for the new record.
894** The order of index keys is the same as the order of the indices on
895** the pTable->pIndex list. A key is only created for index i if
896** aIdxUsed!=0 and aIdxUsed[i]!=0.
897**
898** This routine also generates code to check constraints. NOT NULL,
899** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
900** then the appropriate action is performed. There are five possible
901** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
902**
903** Constraint type Action What Happens
904** --------------- ---------- ----------------------------------------
905** any ROLLBACK The current transaction is rolled back and
906** sqlite3_exec() returns immediately with a
907** return code of SQLITE_CONSTRAINT.
908**
909** any ABORT Back out changes from the current command
910** only (do not do a complete rollback) then
911** cause sqlite3_exec() to return immediately
912** with SQLITE_CONSTRAINT.
913**
914** any FAIL Sqlite_exec() returns immediately with a
915** return code of SQLITE_CONSTRAINT. The
916** transaction is not rolled back and any
917** prior changes are retained.
918**
919** any IGNORE The record number and data is popped from
920** the stack and there is an immediate jump
921** to label ignoreDest.
922**
923** NOT NULL REPLACE The NULL value is replace by the default
924** value for that column. If the default value
925** is NULL, the action is the same as ABORT.
926**
927** UNIQUE REPLACE The other row that conflicts with the row
928** being inserted is removed.
929**
930** CHECK REPLACE Illegal. The results in an exception.
931**
932** Which action to take is determined by the overrideError parameter.
933** Or if overrideError==OE_Default, then the pParse->onError parameter
934** is used. Or if pParse->onError==OE_Default then the onError value
935** for the constraint is used.
936**
937** The calling routine must open a read/write cursor for pTab with
938** cursor number "base". All indices of pTab must also have open
939** read/write cursors with cursor number base+i for the i-th cursor.
940** Except, if there is no possibility of a REPLACE action then
941** cursors do not need to be open for indices where aIdxUsed[i]==0.
942**
943** If the isUpdate flag is true, it means that the "base" cursor is
944** initially pointing to an entry that is being updated. The isUpdate
945** flag causes extra code to be generated so that the "base" cursor
946** is still pointing at the same entry after the routine returns.
947** Without the isUpdate flag, the "base" cursor might be moved.
948*/
949void sqlite3GenerateConstraintChecks(
950 Parse *pParse, /* The parser context */
951 Table *pTab, /* the table into which we are inserting */
952 int base, /* Index of a read/write cursor pointing at pTab */
953 char *aIdxUsed, /* Which indices are used. NULL means all are used */
954 int rowidChng, /* True if the record number will change */
955 int isUpdate, /* True for UPDATE, False for INSERT */
956 int overrideError, /* Override onError to this if not OE_Default */
957 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
958){
959 int i;
960 Vdbe *v;
961 int nCol;
962 int onError;
963 int addr;
964 int extra;
965 int iCur;
966 Index *pIdx;
967 int seenReplace = 0;
968 int jumpInst1=0, jumpInst2;
969 int hasTwoRowids = (isUpdate && rowidChng);
970
971 v = sqlite3GetVdbe(pParse);
972 assert( v!=0 );
973 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
974 nCol = pTab->nCol;
975
976 /* Test all NOT NULL constraints.
977 */
978 for(i=0; i<nCol; i++){
979 if( i==pTab->iPKey ){
980 continue;
981 }
982 onError = pTab->aCol[i].notNull;
983 if( onError==OE_None ) continue;
984 if( overrideError!=OE_Default ){
985 onError = overrideError;
986 }else if( onError==OE_Default ){
987 onError = OE_Abort;
988 }
989 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
990 onError = OE_Abort;
991 }
992 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
993 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
994 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
995 || onError==OE_Ignore || onError==OE_Replace );
996 switch( onError ){
997 case OE_Rollback:
998 case OE_Abort:
999 case OE_Fail: {
1000 char *zMsg = 0;
1001 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
1002 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
1003 " may not be NULL", (char*)0);
1004 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
1005 break;
1006 }
1007 case OE_Ignore: {
1008 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1009 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1010 break;
1011 }
1012 case OE_Replace: {
1013 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
1014 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
1015 break;
1016 }
1017 }
1018 sqlite3VdbeJumpHere(v, addr);
1019 }
1020
1021 /* Test all CHECK constraints
1022 */
1023#ifndef SQLITE_OMIT_CHECK
1024 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
1025 int allOk = sqlite3VdbeMakeLabel(v);
1026 assert( pParse->ckOffset==0 );
1027 pParse->ckOffset = nCol;
1028 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
1029 assert( pParse->ckOffset==nCol );
1030 pParse->ckOffset = 0;
1031 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1032 if( onError==OE_Ignore ){
1033 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1034 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1035 }else{
1036 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
1037 }
1038 sqlite3VdbeResolveLabel(v, allOk);
1039 }
1040#endif /* !defined(SQLITE_OMIT_CHECK) */
1041
1042 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1043 ** of the new record does not previously exist. Except, if this
1044 ** is an UPDATE and the primary key is not changing, that is OK.
1045 */
1046 if( rowidChng ){
1047 onError = pTab->keyConf;
1048 if( overrideError!=OE_Default ){
1049 onError = overrideError;
1050 }else if( onError==OE_Default ){
1051 onError = OE_Abort;
1052 }
1053
1054 if( isUpdate ){
1055 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1056 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1057 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
1058 }
1059 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
1060 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
1061 switch( onError ){
1062 default: {
1063 onError = OE_Abort;
1064 /* Fall thru into the next case */
1065 }
1066 case OE_Rollback:
1067 case OE_Abort:
1068 case OE_Fail: {
1069 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1070 "PRIMARY KEY must be unique", P3_STATIC);
1071 break;
1072 }
1073 case OE_Replace: {
1074 sqlite3GenerateRowIndexDelete(v, pTab, base, 0);
1075 if( isUpdate ){
1076 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
1077 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1078 }
1079 seenReplace = 1;
1080 break;
1081 }
1082 case OE_Ignore: {
1083 assert( seenReplace==0 );
1084 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1085 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1086 break;
1087 }
1088 }
1089 sqlite3VdbeJumpHere(v, jumpInst2);
1090 if( isUpdate ){
1091 sqlite3VdbeJumpHere(v, jumpInst1);
1092 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1093 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1094 }
1095 }
1096
1097 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1098 ** index and making sure that duplicate entries do not already exist.
1099 ** Add the new records to the indices as we go.
1100 */
1101 extra = -1;
1102 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
1103 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
1104 extra++;
1105
1106 /* Create a key for accessing the index entry */
1107 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
1108 for(i=0; i<pIdx->nColumn; i++){
1109 int idx = pIdx->aiColumn[i];
1110 if( idx==pTab->iPKey ){
1111 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
1112 }else{
1113 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
1114 }
1115 }
1116 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
1117 sqlite3IndexAffinityStr(v, pIdx);
1118
1119 /* Find out what action to take in case there is an indexing conflict */
1120 onError = pIdx->onError;
1121 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
1122 if( overrideError!=OE_Default ){
1123 onError = overrideError;
1124 }else if( onError==OE_Default ){
1125 onError = OE_Abort;
1126 }
1127 if( seenReplace ){
1128 if( onError==OE_Ignore ) onError = OE_Replace;
1129 else if( onError==OE_Fail ) onError = OE_Abort;
1130 }
1131
1132
1133 /* Check to see if the new index entry will be unique */
1134 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
1135 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
1136
1137 /* Generate code that executes if the new index entry is not unique */
1138 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1139 || onError==OE_Ignore || onError==OE_Replace );
1140 switch( onError ){
1141 case OE_Rollback:
1142 case OE_Abort:
1143 case OE_Fail: {
1144 int j, n1, n2;
1145 char zErrMsg[200];
1146 sqlite3_snprintf(sizeof(zErrMsg), zErrMsg,
1147 pIdx->nColumn>1 ? "columns " : "column ");
1148 n1 = strlen(zErrMsg);
1149 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
1150 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
1151 n2 = strlen(zCol);
1152 if( j>0 ){
1153 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", ");
1154 n1 += 2;
1155 }
1156 if( n1+n2>sizeof(zErrMsg)-30 ){
1157 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "...");
1158 n1 += 3;
1159 break;
1160 }else{
1161 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
1162 n1 += n2;
1163 }
1164 }
1165 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1],
1166 pIdx->nColumn>1 ? " are not unique" : " is not unique");
1167 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
1168 break;
1169 }
1170 case OE_Ignore: {
1171 assert( seenReplace==0 );
1172 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
1173 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1174 break;
1175 }
1176 case OE_Replace: {
1177 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
1178 if( isUpdate ){
1179 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
1180 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1181 }
1182 seenReplace = 1;
1183 break;
1184 }
1185 }
1186#if NULL_DISTINCT_FOR_UNIQUE
1187 sqlite3VdbeJumpHere(v, jumpInst1);
1188#endif
1189 sqlite3VdbeJumpHere(v, jumpInst2);
1190 }
1191}
1192
1193/*
1194** This routine generates code to finish the INSERT or UPDATE operation
1195** that was started by a prior call to sqlite3GenerateConstraintChecks.
1196** The stack must contain keys for all active indices followed by data
1197** and the rowid for the new entry. This routine creates the new
1198** entries in all indices and in the main table.
1199**
1200** The arguments to this routine should be the same as the first six
1201** arguments to sqlite3GenerateConstraintChecks.
1202*/
1203void sqlite3CompleteInsertion(
1204 Parse *pParse, /* The parser context */
1205 Table *pTab, /* the table into which we are inserting */
1206 int base, /* Index of a read/write cursor pointing at pTab */
1207 char *aIdxUsed, /* Which indices are used. NULL means all are used */
1208 int rowidChng, /* True if the record number will change */
1209 int isUpdate, /* True for UPDATE, False for INSERT */
1210 int newIdx, /* Index of NEW table for triggers. -1 if none */
1211 int appendBias /* True if this is likely to be an append */
1212){
1213 int i;
1214 Vdbe *v;
1215 int nIdx;
1216 Index *pIdx;
1217 int pik_flags;
1218
1219 v = sqlite3GetVdbe(pParse);
1220 assert( v!=0 );
1221 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
1222 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1223 for(i=nIdx-1; i>=0; i--){
1224 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
1225 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
1226 }
1227 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
1228 sqlite3TableAffinityStr(v, pTab);
1229#ifndef SQLITE_OMIT_TRIGGER
1230 if( newIdx>=0 ){
1231 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1232 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1233 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
1234 }
1235#endif
1236 if( pParse->nested ){
1237 pik_flags = 0;
1238 }else{
1239 pik_flags = OPFLAG_NCHANGE;
1240 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
1241 }
1242 if( appendBias ){
1243 pik_flags |= OPFLAG_APPEND;
1244 }
1245 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
1246 if( !pParse->nested ){
1247 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
1248 }
1249
1250 if( isUpdate && rowidChng ){
1251 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1252 }
1253}
1254
1255/*
1256** Generate code that will open cursors for a table and for all
1257** indices of that table. The "base" parameter is the cursor number used
1258** for the table. Indices are opened on subsequent cursors.
1259*/
1260void sqlite3OpenTableAndIndices(
1261 Parse *pParse, /* Parsing context */
1262 Table *pTab, /* Table to be opened */
1263 int base, /* Cursor number assigned to the table */
1264 int op /* OP_OpenRead or OP_OpenWrite */
1265){
1266 int i;
1267 int iDb;
1268 Index *pIdx;
1269 Vdbe *v;
1270
1271 if( IsVirtual(pTab) ) return;
1272 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1273 v = sqlite3GetVdbe(pParse);
1274 assert( v!=0 );
1275 sqlite3OpenTable(pParse, base, iDb, pTab, op);
1276 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1277 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1278 assert( pIdx->pSchema==pTab->pSchema );
1279 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
1280 VdbeComment((v, "# %s", pIdx->zName));
1281 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
1282 }
1283 if( pParse->nTab<=base+i ){
1284 pParse->nTab = base+i;
1285 }
1286}
1287
1288
1289#ifdef SQLITE_TEST
1290/*
1291** The following global variable is incremented whenever the
1292** transfer optimization is used. This is used for testing
1293** purposes only - to make sure the transfer optimization really
1294** is happening when it is suppose to.
1295*/
1296int sqlite3_xferopt_count;
1297#endif /* SQLITE_TEST */
1298
1299
1300#ifndef SQLITE_OMIT_XFER_OPT
1301/*
1302** Check to collation names to see if they are compatible.
1303*/
1304static int xferCompatibleCollation(const char *z1, const char *z2){
1305 if( z1==0 ){
1306 return z2==0;
1307 }
1308 if( z2==0 ){
1309 return 0;
1310 }
1311 return sqlite3StrICmp(z1, z2)==0;
1312}
1313
1314
1315/*
1316** Check to see if index pSrc is compatible as a source of data
1317** for index pDest in an insert transfer optimization. The rules
1318** for a compatible index:
1319**
1320** * The index is over the same set of columns
1321** * The same DESC and ASC markings occurs on all columns
1322** * The same onError processing (OE_Abort, OE_Ignore, etc)
1323** * The same collating sequence on each column
1324*/
1325static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1326 int i;
1327 assert( pDest && pSrc );
1328 assert( pDest->pTable!=pSrc->pTable );
1329 if( pDest->nColumn!=pSrc->nColumn ){
1330 return 0; /* Different number of columns */
1331 }
1332 if( pDest->onError!=pSrc->onError ){
1333 return 0; /* Different conflict resolution strategies */
1334 }
1335 for(i=0; i<pSrc->nColumn; i++){
1336 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1337 return 0; /* Different columns indexed */
1338 }
1339 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1340 return 0; /* Different sort orders */
1341 }
1342 if( pSrc->azColl[i]!=pDest->azColl[i] ){
1343 return 0; /* Different sort orders */
1344 }
1345 }
1346
1347 /* If no test above fails then the indices must be compatible */
1348 return 1;
1349}
1350
1351/*
1352** Attempt the transfer optimization on INSERTs of the form
1353**
1354** INSERT INTO tab1 SELECT * FROM tab2;
1355**
1356** This optimization is only attempted if
1357**
1358** (1) tab1 and tab2 have identical schemas including all the
1359** same indices and constraints
1360**
1361** (2) tab1 and tab2 are different tables
1362**
1363** (3) There must be no triggers on tab1
1364**
1365** (4) The result set of the SELECT statement is "*"
1366**
1367** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1368** or LIMIT clause.
1369**
1370** (6) The SELECT statement is a simple (not a compound) select that
1371** contains only tab2 in its FROM clause
1372**
1373** This method for implementing the INSERT transfers raw records from
1374** tab2 over to tab1. The columns are not decoded. Raw records from
1375** the indices of tab2 are transfered to tab1 as well. In so doing,
1376** the resulting tab1 has much less fragmentation.
1377**
1378** This routine returns TRUE if the optimization is attempted. If any
1379** of the conditions above fail so that the optimization should not
1380** be attempted, then this routine returns FALSE.
1381*/
1382static int xferOptimization(
1383 Parse *pParse, /* Parser context */
1384 Table *pDest, /* The table we are inserting into */
1385 Select *pSelect, /* A SELECT statement to use as the data source */
1386 int onError, /* How to handle constraint errors */
1387 int iDbDest /* The database of pDest */
1388){
1389 ExprList *pEList; /* The result set of the SELECT */
1390 Table *pSrc; /* The table in the FROM clause of SELECT */
1391 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
1392 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
1393 int i; /* Loop counter */
1394 int iDbSrc; /* The database of pSrc */
1395 int iSrc, iDest; /* Cursors from source and destination */
1396 int addr1, addr2; /* Loop addresses */
1397 int emptyDestTest; /* Address of test for empty pDest */
1398 int emptySrcTest; /* Address of test for empty pSrc */
1399 Vdbe *v; /* The VDBE we are building */
1400 KeyInfo *pKey; /* Key information for an index */
1401 int counterMem; /* Memory register used by AUTOINC */
1402 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
1403
1404 if( pSelect==0 ){
1405 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
1406 }
1407 if( pDest->pTrigger ){
1408 return 0; /* tab1 must not have triggers */
1409 }
1410#ifndef SQLITE_OMIT_VIRTUALTABLE
1411 if( pDest->isVirtual ){
1412 return 0; /* tab1 must not be a virtual table */
1413 }
1414#endif
1415 if( onError==OE_Default ){
1416 onError = OE_Abort;
1417 }
1418 if( onError!=OE_Abort && onError!=OE_Rollback ){
1419 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1420 }
1421 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
1422 if( pSelect->pSrc->nSrc!=1 ){
1423 return 0; /* FROM clause must have exactly one term */
1424 }
1425 if( pSelect->pSrc->a[0].pSelect ){
1426 return 0; /* FROM clause cannot contain a subquery */
1427 }
1428 if( pSelect->pWhere ){
1429 return 0; /* SELECT may not have a WHERE clause */
1430 }
1431 if( pSelect->pOrderBy ){
1432 return 0; /* SELECT may not have an ORDER BY clause */
1433 }
1434 /* Do not need to test for a HAVING clause. If HAVING is present but
1435 ** there is no ORDER BY, we will get an error. */
1436 if( pSelect->pGroupBy ){
1437 return 0; /* SELECT may not have a GROUP BY clause */
1438 }
1439 if( pSelect->pLimit ){
1440 return 0; /* SELECT may not have a LIMIT clause */
1441 }
1442 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
1443 if( pSelect->pPrior ){
1444 return 0; /* SELECT may not be a compound query */
1445 }
1446 if( pSelect->isDistinct ){
1447 return 0; /* SELECT may not be DISTINCT */
1448 }
1449 pEList = pSelect->pEList;
1450 assert( pEList!=0 );
1451 if( pEList->nExpr!=1 ){
1452 return 0; /* The result set must have exactly one column */
1453 }
1454 assert( pEList->a[0].pExpr );
1455 if( pEList->a[0].pExpr->op!=TK_ALL ){
1456 return 0; /* The result set must be the special operator "*" */
1457 }
1458
1459 /* At this point we have established that the statement is of the
1460 ** correct syntactic form to participate in this optimization. Now
1461 ** we have to check the semantics.
1462 */
1463 pItem = pSelect->pSrc->a;
1464 pSrc = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
1465 if( pSrc==0 ){
1466 return 0; /* FROM clause does not contain a real table */
1467 }
1468 if( pSrc==pDest ){
1469 return 0; /* tab1 and tab2 may not be the same table */
1470 }
1471#ifndef SQLITE_OMIT_VIRTUALTABLE
1472 if( pSrc->isVirtual ){
1473 return 0; /* tab2 must not be a virtual table */
1474 }
1475#endif
1476 if( pSrc->pSelect ){
1477 return 0; /* tab2 may not be a view */
1478 }
1479 if( pDest->nCol!=pSrc->nCol ){
1480 return 0; /* Number of columns must be the same in tab1 and tab2 */
1481 }
1482 if( pDest->iPKey!=pSrc->iPKey ){
1483 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
1484 }
1485 for(i=0; i<pDest->nCol; i++){
1486 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1487 return 0; /* Affinity must be the same on all columns */
1488 }
1489 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1490 return 0; /* Collating sequence must be the same on all columns */
1491 }
1492 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1493 return 0; /* tab2 must be NOT NULL if tab1 is */
1494 }
1495 }
1496 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1497 if( pDestIdx->onError!=OE_None ){
1498 destHasUniqueIdx = 1;
1499 }
1500 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1501 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1502 }
1503 if( pSrcIdx==0 ){
1504 return 0; /* pDestIdx has no corresponding index in pSrc */
1505 }
1506 }
1507#ifndef SQLITE_OMIT_CHECK
1508 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
1509 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1510 }
1511#endif
1512
1513 /* If we get this far, it means either:
1514 **
1515 ** * We can always do the transfer if the table contains an
1516 ** an integer primary key
1517 **
1518 ** * We can conditionally do the transfer if the destination
1519 ** table is empty.
1520 */
1521#ifdef SQLITE_TEST
1522 sqlite3_xferopt_count++;
1523#endif
1524 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1525 v = sqlite3GetVdbe(pParse);
1526 sqlite3CodeVerifySchema(pParse, iDbSrc);
1527 iSrc = pParse->nTab++;
1528 iDest = pParse->nTab++;
1529 counterMem = autoIncBegin(pParse, iDbDest, pDest);
1530 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
1531 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
1532 /* If tables do not have an INTEGER PRIMARY KEY and there
1533 ** are indices to be copied and the destination is not empty,
1534 ** we have to disallow the transfer optimization because the
1535 ** the rowids might change which will mess up indexing.
1536 **
1537 ** Or if the destination has a UNIQUE index and is not empty,
1538 ** we also disallow the transfer optimization because we cannot
1539 ** insure that all entries in the union of DEST and SRC will be
1540 ** unique.
1541 */
1542 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iDest, 0);
1543 emptyDestTest = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
1544 sqlite3VdbeJumpHere(v, addr1);
1545 }else{
1546 emptyDestTest = 0;
1547 }
1548 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
1549 emptySrcTest = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1550 if( pDest->iPKey>=0 ){
1551 addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1552 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1553 addr2 = sqlite3VdbeAddOp(v, OP_NotExists, iDest, 0);
1554 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1555 "PRIMARY KEY must be unique", P3_STATIC);
1556 sqlite3VdbeJumpHere(v, addr2);
1557 autoIncStep(pParse, counterMem);
1558 }else if( pDest->pIndex==0 ){
1559 addr1 = sqlite3VdbeAddOp(v, OP_NewRowid, iDest, 0);
1560 }else{
1561 addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1562 assert( pDest->autoInc==0 );
1563 }
1564 sqlite3VdbeAddOp(v, OP_RowData, iSrc, 0);
1565 sqlite3VdbeOp3(v, OP_Insert, iDest,
1566 OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND,
1567 pDest->zName, 0);
1568 sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1);
1569 autoIncEnd(pParse, iDbDest, pDest, counterMem);
1570 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1571 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1572 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1573 }
1574 assert( pSrcIdx );
1575 sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1576 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1577 sqlite3VdbeAddOp(v, OP_Integer, iDbSrc, 0);
1578 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
1579 VdbeComment((v, "# %s", pSrcIdx->zName));
1580 sqlite3VdbeOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum,
1581 (char*)pKey, P3_KEYINFO_HANDOFF);
1582 sqlite3VdbeAddOp(v, OP_Integer, iDbDest, 0);
1583 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
1584 VdbeComment((v, "# %s", pDestIdx->zName));
1585 sqlite3VdbeOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum,
1586 (char*)pKey, P3_KEYINFO_HANDOFF);
1587 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1588 sqlite3VdbeAddOp(v, OP_RowKey, iSrc, 0);
1589 sqlite3VdbeAddOp(v, OP_IdxInsert, iDest, 1);
1590 sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1+1);
1591 sqlite3VdbeJumpHere(v, addr1);
1592 }
1593 sqlite3VdbeJumpHere(v, emptySrcTest);
1594 sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1595 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1596 if( emptyDestTest ){
1597 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_OK, 0);
1598 sqlite3VdbeJumpHere(v, emptyDestTest);
1599 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1600 return 0;
1601 }else{
1602 return 1;
1603 }
1604}
1605#endif /* SQLITE_OMIT_XFER_OPT */
diff --git a/libraries/sqlite/win32/journal.c b/libraries/sqlite/win32/journal.c
new file mode 100755
index 0000000..7cbe5bc
--- /dev/null
+++ b/libraries/sqlite/win32/journal.c
@@ -0,0 +1,238 @@
1/*
2** 2007 August 22
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.
10**
11*************************************************************************
12**
13** @(#) $Id: journal.c,v 1.7 2007/09/06 13:49:37 drh Exp $
14*/
15
16#ifdef SQLITE_ENABLE_ATOMIC_WRITE
17
18/*
19** This file implements a special kind of sqlite3_file object used
20** by SQLite to create journal files if the atomic-write optimization
21** is enabled.
22**
23** The distinctive characteristic of this sqlite3_file is that the
24** actual on disk file is created lazily. When the file is created,
25** the caller specifies a buffer size for an in-memory buffer to
26** be used to service read() and write() requests. The actual file
27** on disk is not created or populated until either:
28**
29** 1) The in-memory representation grows too large for the allocated
30** buffer, or
31** 2) The xSync() method is called.
32*/
33
34#include "sqliteInt.h"
35
36
37/*
38** A JournalFile object is a subclass of sqlite3_file used by
39** as an open file handle for journal files.
40*/
41struct JournalFile {
42 sqlite3_io_methods *pMethod; /* I/O methods on journal files */
43 int nBuf; /* Size of zBuf[] in bytes */
44 char *zBuf; /* Space to buffer journal writes */
45 int iSize; /* Amount of zBuf[] currently used */
46 int flags; /* xOpen flags */
47 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
48 sqlite3_file *pReal; /* The "real" underlying file descriptor */
49 const char *zJournal; /* Name of the journal file */
50};
51typedef struct JournalFile JournalFile;
52
53/*
54** If it does not already exists, create and populate the on-disk file
55** for JournalFile p.
56*/
57static int createFile(JournalFile *p){
58 int rc = SQLITE_OK;
59 if( !p->pReal ){
60 sqlite3_file *pReal = (sqlite3_file *)&p[1];
61 rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
62 if( rc==SQLITE_OK ){
63 p->pReal = pReal;
64 if( p->iSize>0 ){
65 assert(p->iSize<=p->nBuf);
66 rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
67 }
68 }
69 }
70 return rc;
71}
72
73/*
74** Close the file.
75*/
76static int jrnlClose(sqlite3_file *pJfd){
77 JournalFile *p = (JournalFile *)pJfd;
78 if( p->pReal ){
79 sqlite3OsClose(p->pReal);
80 }
81 sqlite3_free(p->zBuf);
82 return SQLITE_OK;
83}
84
85/*
86** Read data from the file.
87*/
88static int jrnlRead(
89 sqlite3_file *pJfd, /* The journal file from which to read */
90 void *zBuf, /* Put the results here */
91 int iAmt, /* Number of bytes to read */
92 sqlite_int64 iOfst /* Begin reading at this offset */
93){
94 int rc = SQLITE_OK;
95 JournalFile *p = (JournalFile *)pJfd;
96 if( p->pReal ){
97 rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
98 }else{
99 assert( iAmt+iOfst<=p->iSize );
100 memcpy(zBuf, &p->zBuf[iOfst], iAmt);
101 }
102 return rc;
103}
104
105/*
106** Write data to the file.
107*/
108static int jrnlWrite(
109 sqlite3_file *pJfd, /* The journal file into which to write */
110 const void *zBuf, /* Take data to be written from here */
111 int iAmt, /* Number of bytes to write */
112 sqlite_int64 iOfst /* Begin writing at this offset into the file */
113){
114 int rc = SQLITE_OK;
115 JournalFile *p = (JournalFile *)pJfd;
116 if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
117 rc = createFile(p);
118 }
119 if( rc==SQLITE_OK ){
120 if( p->pReal ){
121 rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
122 }else{
123 memcpy(&p->zBuf[iOfst], zBuf, iAmt);
124 if( p->iSize<(iOfst+iAmt) ){
125 p->iSize = (iOfst+iAmt);
126 }
127 }
128 }
129 return rc;
130}
131
132/*
133** Truncate the file.
134*/
135static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
136 int rc = SQLITE_OK;
137 JournalFile *p = (JournalFile *)pJfd;
138 if( p->pReal ){
139 rc = sqlite3OsTruncate(p->pReal, size);
140 }else if( size<p->iSize ){
141 p->iSize = size;
142 }
143 return rc;
144}
145
146/*
147** Sync the file.
148*/
149static int jrnlSync(sqlite3_file *pJfd, int flags){
150 int rc;
151 JournalFile *p = (JournalFile *)pJfd;
152 rc = createFile(p);
153 if( rc==SQLITE_OK ){
154 rc = sqlite3OsSync(p->pReal, flags);
155 }
156 return rc;
157}
158
159/*
160** Query the size of the file in bytes.
161*/
162static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
163 int rc = SQLITE_OK;
164 JournalFile *p = (JournalFile *)pJfd;
165 if( p->pReal ){
166 rc = sqlite3OsFileSize(p->pReal, pSize);
167 }else{
168 *pSize = (sqlite_int64) p->iSize;
169 }
170 return rc;
171}
172
173/*
174** Table of methods for JournalFile sqlite3_file object.
175*/
176static struct sqlite3_io_methods JournalFileMethods = {
177 1, /* iVersion */
178 jrnlClose, /* xClose */
179 jrnlRead, /* xRead */
180 jrnlWrite, /* xWrite */
181 jrnlTruncate, /* xTruncate */
182 jrnlSync, /* xSync */
183 jrnlFileSize, /* xFileSize */
184 0, /* xLock */
185 0, /* xUnlock */
186 0, /* xCheckReservedLock */
187 0, /* xFileControl */
188 0, /* xSectorSize */
189 0 /* xDeviceCharacteristics */
190};
191
192/*
193** Open a journal file.
194*/
195int sqlite3JournalOpen(
196 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
197 const char *zName, /* Name of the journal file */
198 sqlite3_file *pJfd, /* Preallocated, blank file handle */
199 int flags, /* Opening flags */
200 int nBuf /* Bytes buffered before opening the file */
201){
202 JournalFile *p = (JournalFile *)pJfd;
203 memset(p, 0, sqlite3JournalSize(pVfs));
204 if( nBuf>0 ){
205 p->zBuf = sqlite3MallocZero(nBuf);
206 if( !p->zBuf ){
207 return SQLITE_NOMEM;
208 }
209 }else{
210 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
211 }
212 p->pMethod = &JournalFileMethods;
213 p->nBuf = nBuf;
214 p->flags = flags;
215 p->zJournal = zName;
216 p->pVfs = pVfs;
217 return SQLITE_OK;
218}
219
220/*
221** If the argument p points to a JournalFile structure, and the underlying
222** file has not yet been created, create it now.
223*/
224int sqlite3JournalCreate(sqlite3_file *p){
225 if( p->pMethods!=&JournalFileMethods ){
226 return SQLITE_OK;
227 }
228 return createFile((JournalFile *)p);
229}
230
231/*
232** Return the number of bytes required to store a JournalFile that uses vfs
233** pVfs to create the underlying on-disk files.
234*/
235int sqlite3JournalSize(sqlite3_vfs *pVfs){
236 return (pVfs->szOsFile+sizeof(JournalFile));
237}
238#endif
diff --git a/libraries/sqlite/win32/keywordhash.h b/libraries/sqlite/win32/keywordhash.h
new file mode 100755
index 0000000..609ff20
--- /dev/null
+++ b/libraries/sqlite/win32/keywordhash.h
@@ -0,0 +1,112 @@
1/***** This file contains automatically generated code ******
2**
3** The code in this file has been automatically generated by
4**
5** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.31 2007/07/30 18:26:20 rse Exp $
6**
7** The code in this file implements a function that determines whether
8** or not a given identifier is really an SQL keyword. The same thing
9** might be implemented more directly using a hand-written hash table.
10** But by using this automatically generated code, the size of the code
11** is substantially reduced. This is important for embedded applications
12** on platforms with limited memory.
13*/
14/* Hash score: 165 */
15static int keywordCode(const char *z, int n){
16 /* zText[] encodes 775 bytes of keywords in 526 bytes */
17 static const char zText[526] =
18 "BEFOREIGNOREGEXPLAINSTEADDESCAPEACHECKEYCONSTRAINTERSECTABLEFT"
19 "HENDATABASELECTRANSACTIONATURALTERAISELSEXCEPTRIGGEREFERENCES"
20 "UNIQUERYATTACHAVINGROUPDATEMPORARYBEGINNEREINDEXCLUSIVEXISTSBETWEEN"
21 "OTNULLIKECASCADEFERRABLECASECOLLATECREATECURRENT_DATEDELETEDETACH"
22 "IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN"
23 "WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICT"
24 "CROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOB"
25 "YIFINTOFFSETISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUM"
26 "VIEWINITIALLY";
27 static const unsigned char aHash[127] = {
28 63, 92, 109, 61, 0, 38, 0, 0, 69, 0, 64, 0, 0,
29 102, 4, 65, 7, 0, 108, 72, 103, 99, 0, 22, 0, 0,
30 113, 0, 111, 106, 0, 18, 80, 0, 1, 0, 0, 56, 57,
31 0, 55, 11, 0, 33, 77, 89, 0, 110, 88, 0, 0, 45,
32 0, 90, 54, 0, 20, 0, 114, 34, 19, 0, 10, 97, 28,
33 83, 0, 0, 116, 93, 47, 115, 41, 12, 44, 0, 78, 0,
34 87, 29, 0, 86, 0, 0, 0, 82, 79, 84, 75, 96, 6,
35 14, 95, 0, 68, 0, 21, 76, 98, 27, 0, 112, 67, 104,
36 49, 40, 71, 0, 0, 81, 100, 0, 107, 0, 15, 0, 0,
37 24, 0, 73, 42, 50, 0, 16, 48, 0, 37,
38 };
39 static const unsigned char aNext[116] = {
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0,
41 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0,
43 17, 0, 0, 0, 36, 39, 0, 0, 25, 0, 0, 31, 0,
44 0, 0, 43, 52, 0, 0, 0, 53, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 51, 0, 0, 0, 0, 26, 0, 8, 46,
46 2, 0, 0, 0, 0, 0, 0, 0, 3, 58, 66, 0, 13,
47 0, 91, 85, 0, 94, 0, 74, 0, 0, 62, 0, 35, 101,
48 0, 0, 105, 23, 30, 60, 70, 0, 0, 59, 0, 0,
49 };
50 static const unsigned char aLen[116] = {
51 6, 7, 3, 6, 6, 7, 7, 3, 4, 6, 4, 5, 3,
52 10, 9, 5, 4, 4, 3, 8, 2, 6, 11, 2, 7, 5,
53 5, 4, 6, 7, 10, 6, 5, 6, 6, 5, 6, 4, 9,
54 2, 5, 5, 7, 5, 9, 6, 7, 7, 3, 4, 4, 7,
55 3, 10, 4, 7, 6, 12, 6, 6, 9, 4, 6, 5, 4,
56 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7,
57 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8,
58 2, 4, 4, 4, 4, 4, 2, 2, 4, 6, 2, 3, 6,
59 5, 8, 5, 5, 8, 3, 5, 5, 6, 4, 9, 3,
60 };
61 static const unsigned short int aOffset[116] = {
62 0, 2, 2, 6, 10, 13, 18, 23, 25, 26, 31, 33, 37,
63 40, 47, 55, 58, 61, 63, 65, 70, 71, 76, 85, 86, 91,
64 95, 99, 102, 107, 113, 123, 126, 131, 136, 141, 144, 148, 148,
65 152, 157, 160, 164, 166, 169, 177, 183, 189, 189, 192, 195, 199,
66 200, 204, 214, 218, 225, 231, 243, 249, 255, 264, 266, 272, 277,
67 279, 286, 291, 296, 302, 308, 313, 317, 320, 326, 330, 337, 339,
68 346, 348, 350, 359, 363, 369, 375, 383, 388, 388, 404, 411, 418,
69 419, 426, 430, 434, 438, 442, 445, 447, 449, 452, 452, 455, 458,
70 464, 468, 476, 480, 485, 493, 496, 501, 506, 512, 516, 521,
71 };
72 static const unsigned char aCode[116] = {
73 TK_BEFORE, TK_FOREIGN, TK_FOR, TK_IGNORE, TK_LIKE_KW,
74 TK_EXPLAIN, TK_INSTEAD, TK_ADD, TK_DESC, TK_ESCAPE,
75 TK_EACH, TK_CHECK, TK_KEY, TK_CONSTRAINT, TK_INTERSECT,
76 TK_TABLE, TK_JOIN_KW, TK_THEN, TK_END, TK_DATABASE,
77 TK_AS, TK_SELECT, TK_TRANSACTION,TK_ON, TK_JOIN_KW,
78 TK_ALTER, TK_RAISE, TK_ELSE, TK_EXCEPT, TK_TRIGGER,
79 TK_REFERENCES, TK_UNIQUE, TK_QUERY, TK_ATTACH, TK_HAVING,
80 TK_GROUP, TK_UPDATE, TK_TEMP, TK_TEMP, TK_OR,
81 TK_BEGIN, TK_JOIN_KW, TK_REINDEX, TK_INDEX, TK_EXCLUSIVE,
82 TK_EXISTS, TK_BETWEEN, TK_NOTNULL, TK_NOT, TK_NULL,
83 TK_LIKE_KW, TK_CASCADE, TK_ASC, TK_DEFERRABLE, TK_CASE,
84 TK_COLLATE, TK_CREATE, TK_CTIME_KW, TK_DELETE, TK_DETACH,
85 TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_MATCH, TK_PLAN,
86 TK_ANALYZE, TK_PRAGMA, TK_ABORT, TK_VALUES, TK_VIRTUAL,
87 TK_LIMIT, TK_WHEN, TK_WHERE, TK_RENAME, TK_AFTER,
88 TK_REPLACE, TK_AND, TK_DEFAULT, TK_AUTOINCR, TK_TO,
89 TK_IN, TK_CAST, TK_COLUMNKW, TK_COMMIT, TK_CONFLICT,
90 TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED,
91 TK_DISTINCT, TK_IS, TK_DROP, TK_FAIL, TK_FROM,
92 TK_JOIN_KW, TK_LIKE_KW, TK_BY, TK_IF, TK_INTO,
93 TK_OFFSET, TK_OF, TK_SET, TK_ISNULL, TK_ORDER,
94 TK_RESTRICT, TK_JOIN_KW, TK_JOIN_KW, TK_ROLLBACK, TK_ROW,
95 TK_UNION, TK_USING, TK_VACUUM, TK_VIEW, TK_INITIALLY,
96 TK_ALL,
97 };
98 int h, i;
99 if( n<2 ) return TK_ID;
100 h = ((charMap(z[0])*4) ^
101 (charMap(z[n-1])*3) ^
102 n) % 127;
103 for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
104 if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
105 return aCode[i];
106 }
107 }
108 return TK_ID;
109}
110int sqlite3KeywordCode(const unsigned char *z, int n){
111 return keywordCode((char*)z, n);
112}
diff --git a/libraries/sqlite/win32/legacy.c b/libraries/sqlite/win32/legacy.c
new file mode 100755
index 0000000..c004b89
--- /dev/null
+++ b/libraries/sqlite/win32/legacy.c
@@ -0,0 +1,134 @@
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.
10**
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.
16**
17** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $
18*/
19
20#include "sqliteInt.h"
21#include <ctype.h>
22
23/*
24** Execute SQL code. Return one of the SQLITE_ success/failure
25** codes. Also write an error message into memory obtained from
26** malloc() and make *pzErrMsg point to that message.
27**
28** If the SQL is a query, then for each row in the query result
29** the xCallback() function is called. pArg becomes the first
30** argument to xCallback(). If xCallback=NULL then no callback
31** is invoked, even for queries.
32*/
33int sqlite3_exec(
34 sqlite3 *db, /* The database on which the SQL executes */
35 const char *zSql, /* The SQL to be executed */
36 sqlite3_callback xCallback, /* Invoke this callback routine */
37 void *pArg, /* First argument to xCallback() */
38 char **pzErrMsg /* Write error messages here */
39){
40 int rc = SQLITE_OK;
41 const char *zLeftover;
42 sqlite3_stmt *pStmt = 0;
43 char **azCols = 0;
44
45 int nRetry = 0;
46 int nCallback;
47
48 if( zSql==0 ) return SQLITE_OK;
49
50 sqlite3_mutex_enter(db->mutex);
51 while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
52 int nCol;
53 char **azVals = 0;
54
55 pStmt = 0;
56 rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
57 assert( rc==SQLITE_OK || pStmt==0 );
58 if( rc!=SQLITE_OK ){
59 continue;
60 }
61 if( !pStmt ){
62 /* this happens for a comment or white-space */
63 zSql = zLeftover;
64 continue;
65 }
66
67 nCallback = 0;
68
69 nCol = sqlite3_column_count(pStmt);
70 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1);
71 if( azCols==0 ){
72 goto exec_out;
73 }
74
75 while( 1 ){
76 int i;
77 rc = sqlite3_step(pStmt);
78
79 /* Invoke the callback function if required */
80 if( xCallback && (SQLITE_ROW==rc ||
81 (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
82 if( 0==nCallback ){
83 for(i=0; i<nCol; i++){
84 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
85 }
86 nCallback++;
87 }
88 if( rc==SQLITE_ROW ){
89 azVals = &azCols[nCol];
90 for(i=0; i<nCol; i++){
91 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
92 }
93 }
94 if( xCallback(pArg, nCol, azVals, azCols) ){
95 rc = SQLITE_ABORT;
96 goto exec_out;
97 }
98 }
99
100 if( rc!=SQLITE_ROW ){
101 rc = sqlite3_finalize(pStmt);
102 pStmt = 0;
103 if( rc!=SQLITE_SCHEMA ){
104 nRetry = 0;
105 zSql = zLeftover;
106 while( isspace((unsigned char)zSql[0]) ) zSql++;
107 }
108 break;
109 }
110 }
111
112 sqlite3_free(azCols);
113 azCols = 0;
114 }
115
116exec_out:
117 if( pStmt ) sqlite3_finalize(pStmt);
118 if( azCols ) sqlite3_free(azCols);
119
120 rc = sqlite3ApiExit(db, rc);
121 if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
122 int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
123 *pzErrMsg = sqlite3_malloc(nErrMsg);
124 if( *pzErrMsg ){
125 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
126 }
127 }else if( pzErrMsg ){
128 *pzErrMsg = 0;
129 }
130
131 assert( (rc&db->errMask)==rc );
132 sqlite3_mutex_leave(db->mutex);
133 return rc;
134}
diff --git a/libraries/sqlite/win32/loadext.c b/libraries/sqlite/win32/loadext.c
new file mode 100755
index 0000000..dc60562
--- /dev/null
+++ b/libraries/sqlite/win32/loadext.c
@@ -0,0 +1,516 @@
1/*
2** 2006 June 7
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.
10**
11*************************************************************************
12** This file contains code used to dynamically load extensions into
13** the SQLite library.
14*/
15#ifndef SQLITE_OMIT_LOAD_EXTENSION
16
17#define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
18#include "sqlite3ext.h"
19#include "sqliteInt.h"
20#include <string.h>
21#include <ctype.h>
22
23/*
24** Some API routines are omitted when various features are
25** excluded from a build of SQLite. Substitute a NULL pointer
26** for any missing APIs.
27*/
28#ifndef SQLITE_ENABLE_COLUMN_METADATA
29# define sqlite3_column_database_name 0
30# define sqlite3_column_database_name16 0
31# define sqlite3_column_table_name 0
32# define sqlite3_column_table_name16 0
33# define sqlite3_column_origin_name 0
34# define sqlite3_column_origin_name16 0
35# define sqlite3_table_column_metadata 0
36#endif
37
38#ifdef SQLITE_OMIT_AUTHORIZATION
39# define sqlite3_set_authorizer 0
40#endif
41
42#ifdef SQLITE_OMIT_UTF16
43# define sqlite3_bind_text16 0
44# define sqlite3_collation_needed16 0
45# define sqlite3_column_decltype16 0
46# define sqlite3_column_name16 0
47# define sqlite3_column_text16 0
48# define sqlite3_complete16 0
49# define sqlite3_create_collation16 0
50# define sqlite3_create_function16 0
51# define sqlite3_errmsg16 0
52# define sqlite3_open16 0
53# define sqlite3_prepare16 0
54# define sqlite3_prepare16_v2 0
55# define sqlite3_result_error16 0
56# define sqlite3_result_text16 0
57# define sqlite3_result_text16be 0
58# define sqlite3_result_text16le 0
59# define sqlite3_value_text16 0
60# define sqlite3_value_text16be 0
61# define sqlite3_value_text16le 0
62# define sqlite3_column_database_name16 0
63# define sqlite3_column_table_name16 0
64# define sqlite3_column_origin_name16 0
65#endif
66
67#ifdef SQLITE_OMIT_COMPLETE
68# define sqlite3_complete 0
69# define sqlite3_complete16 0
70#endif
71
72#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
73# define sqlite3_progress_handler 0
74#endif
75
76#ifdef SQLITE_OMIT_VIRTUALTABLE
77# define sqlite3_create_module 0
78# define sqlite3_create_module_v2 0
79# define sqlite3_declare_vtab 0
80#endif
81
82#ifdef SQLITE_OMIT_SHARED_CACHE
83# define sqlite3_enable_shared_cache 0
84#endif
85
86#ifdef SQLITE_OMIT_TRACE
87# define sqlite3_profile 0
88# define sqlite3_trace 0
89#endif
90
91#ifdef SQLITE_OMIT_GET_TABLE
92# define sqlite3_free_table 0
93# define sqlite3_get_table 0
94#endif
95
96#ifdef SQLITE_OMIT_INCRBLOB
97#define sqlite3_bind_zeroblob 0
98#define sqlite3_blob_bytes 0
99#define sqlite3_blob_close 0
100#define sqlite3_blob_open 0
101#define sqlite3_blob_read 0
102#define sqlite3_blob_write 0
103#endif
104
105/*
106** The following structure contains pointers to all SQLite API routines.
107** A pointer to this structure is passed into extensions when they are
108** loaded so that the extension can make calls back into the SQLite
109** library.
110**
111** When adding new APIs, add them to the bottom of this structure
112** in order to preserve backwards compatibility.
113**
114** Extensions that use newer APIs should first call the
115** sqlite3_libversion_number() to make sure that the API they
116** intend to use is supported by the library. Extensions should
117** also check to make sure that the pointer to the function is
118** not NULL before calling it.
119*/
120const sqlite3_api_routines sqlite3_apis = {
121 sqlite3_aggregate_context,
122 sqlite3_aggregate_count,
123 sqlite3_bind_blob,
124 sqlite3_bind_double,
125 sqlite3_bind_int,
126 sqlite3_bind_int64,
127 sqlite3_bind_null,
128 sqlite3_bind_parameter_count,
129 sqlite3_bind_parameter_index,
130 sqlite3_bind_parameter_name,
131 sqlite3_bind_text,
132 sqlite3_bind_text16,
133 sqlite3_bind_value,
134 sqlite3_busy_handler,
135 sqlite3_busy_timeout,
136 sqlite3_changes,
137 sqlite3_close,
138 sqlite3_collation_needed,
139 sqlite3_collation_needed16,
140 sqlite3_column_blob,
141 sqlite3_column_bytes,
142 sqlite3_column_bytes16,
143 sqlite3_column_count,
144 sqlite3_column_database_name,
145 sqlite3_column_database_name16,
146 sqlite3_column_decltype,
147 sqlite3_column_decltype16,
148 sqlite3_column_double,
149 sqlite3_column_int,
150 sqlite3_column_int64,
151 sqlite3_column_name,
152 sqlite3_column_name16,
153 sqlite3_column_origin_name,
154 sqlite3_column_origin_name16,
155 sqlite3_column_table_name,
156 sqlite3_column_table_name16,
157 sqlite3_column_text,
158 sqlite3_column_text16,
159 sqlite3_column_type,
160 sqlite3_column_value,
161 sqlite3_commit_hook,
162 sqlite3_complete,
163 sqlite3_complete16,
164 sqlite3_create_collation,
165 sqlite3_create_collation16,
166 sqlite3_create_function,
167 sqlite3_create_function16,
168 sqlite3_create_module,
169 sqlite3_data_count,
170 sqlite3_db_handle,
171 sqlite3_declare_vtab,
172 sqlite3_enable_shared_cache,
173 sqlite3_errcode,
174 sqlite3_errmsg,
175 sqlite3_errmsg16,
176 sqlite3_exec,
177 sqlite3_expired,
178 sqlite3_finalize,
179 sqlite3_free,
180 sqlite3_free_table,
181 sqlite3_get_autocommit,
182 sqlite3_get_auxdata,
183 sqlite3_get_table,
184 0, /* Was sqlite3_global_recover(), but that function is deprecated */
185 sqlite3_interrupt,
186 sqlite3_last_insert_rowid,
187 sqlite3_libversion,
188 sqlite3_libversion_number,
189 sqlite3_malloc,
190 sqlite3_mprintf,
191 sqlite3_open,
192 sqlite3_open16,
193 sqlite3_prepare,
194 sqlite3_prepare16,
195 sqlite3_profile,
196 sqlite3_progress_handler,
197 sqlite3_realloc,
198 sqlite3_reset,
199 sqlite3_result_blob,
200 sqlite3_result_double,
201 sqlite3_result_error,
202 sqlite3_result_error16,
203 sqlite3_result_int,
204 sqlite3_result_int64,
205 sqlite3_result_null,
206 sqlite3_result_text,
207 sqlite3_result_text16,
208 sqlite3_result_text16be,
209 sqlite3_result_text16le,
210 sqlite3_result_value,
211 sqlite3_rollback_hook,
212 sqlite3_set_authorizer,
213 sqlite3_set_auxdata,
214 sqlite3_snprintf,
215 sqlite3_step,
216 sqlite3_table_column_metadata,
217 sqlite3_thread_cleanup,
218 sqlite3_total_changes,
219 sqlite3_trace,
220 sqlite3_transfer_bindings,
221 sqlite3_update_hook,
222 sqlite3_user_data,
223 sqlite3_value_blob,
224 sqlite3_value_bytes,
225 sqlite3_value_bytes16,
226 sqlite3_value_double,
227 sqlite3_value_int,
228 sqlite3_value_int64,
229 sqlite3_value_numeric_type,
230 sqlite3_value_text,
231 sqlite3_value_text16,
232 sqlite3_value_text16be,
233 sqlite3_value_text16le,
234 sqlite3_value_type,
235 sqlite3_vmprintf,
236 /*
237 ** The original API set ends here. All extensions can call any
238 ** of the APIs above provided that the pointer is not NULL. But
239 ** before calling APIs that follow, extension should check the
240 ** sqlite3_libversion_number() to make sure they are dealing with
241 ** a library that is new enough to support that API.
242 *************************************************************************
243 */
244 sqlite3_overload_function,
245
246 /*
247 ** Added after 3.3.13
248 */
249 sqlite3_prepare_v2,
250 sqlite3_prepare16_v2,
251 sqlite3_clear_bindings,
252
253 /*
254 ** Added for 3.4.1
255 */
256 sqlite3_create_module_v2,
257
258 /*
259 ** Added for 3.5.0
260 */
261 sqlite3_bind_zeroblob,
262 sqlite3_blob_bytes,
263 sqlite3_blob_close,
264 sqlite3_blob_open,
265 sqlite3_blob_read,
266 sqlite3_blob_write,
267 sqlite3_create_collation_v2,
268 sqlite3_file_control,
269 sqlite3_memory_highwater,
270 sqlite3_memory_used,
271#ifdef SQLITE_MUTEX_NOOP
272 0,
273 0,
274 0,
275 0,
276 0,
277#else
278 sqlite3_mutex_alloc,
279 sqlite3_mutex_enter,
280 sqlite3_mutex_free,
281 sqlite3_mutex_leave,
282 sqlite3_mutex_try,
283#endif
284 sqlite3_open_v2,
285 sqlite3_release_memory,
286 sqlite3_result_error_nomem,
287 sqlite3_result_error_toobig,
288 sqlite3_sleep,
289 sqlite3_soft_heap_limit,
290 sqlite3_vfs_find,
291 sqlite3_vfs_register,
292 sqlite3_vfs_unregister,
293};
294
295/*
296** Attempt to load an SQLite extension library contained in the file
297** zFile. The entry point is zProc. zProc may be 0 in which case a
298** default entry point name (sqlite3_extension_init) is used. Use
299** of the default name is recommended.
300**
301** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
302**
303** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
304** error message text. The calling function should free this memory
305** by calling sqlite3_free().
306*/
307static int sqlite3LoadExtension(
308 sqlite3 *db, /* Load the extension into this database connection */
309 const char *zFile, /* Name of the shared library containing extension */
310 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
311 char **pzErrMsg /* Put error message here if not 0 */
312){
313 sqlite3_vfs *pVfs = db->pVfs;
314 void *handle;
315 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
316 char *zErrmsg = 0;
317 void **aHandle;
318
319 /* Ticket #1863. To avoid a creating security problems for older
320 ** applications that relink against newer versions of SQLite, the
321 ** ability to run load_extension is turned off by default. One
322 ** must call sqlite3_enable_load_extension() to turn on extension
323 ** loading. Otherwise you get the following error.
324 */
325 if( (db->flags & SQLITE_LoadExtension)==0 ){
326 if( pzErrMsg ){
327 *pzErrMsg = sqlite3_mprintf("not authorized");
328 }
329 return SQLITE_ERROR;
330 }
331
332 if( zProc==0 ){
333 zProc = "sqlite3_extension_init";
334 }
335
336 handle = sqlite3OsDlOpen(pVfs, zFile);
337 if( handle==0 ){
338 if( pzErrMsg ){
339 char zErr[256];
340 zErr[sizeof(zErr)-1] = '\0';
341 sqlite3_snprintf(sizeof(zErr)-1, zErr,
342 "unable to open shared library [%s]", zFile);
343 sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
344 *pzErrMsg = sqlite3DbStrDup(db, zErr);
345 }
346 return SQLITE_ERROR;
347 }
348 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
349 sqlite3OsDlSym(pVfs, handle, zProc);
350 if( xInit==0 ){
351 if( pzErrMsg ){
352 char zErr[256];
353 zErr[sizeof(zErr)-1] = '\0';
354 sqlite3_snprintf(sizeof(zErr)-1, zErr,
355 "no entry point [%s] in shared library [%s]", zProc,zFile);
356 sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
357 *pzErrMsg = sqlite3DbStrDup(db, zErr);
358 sqlite3OsDlClose(pVfs, handle);
359 }
360 return SQLITE_ERROR;
361 }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
362 if( pzErrMsg ){
363 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
364 }
365 sqlite3_free(zErrmsg);
366 sqlite3OsDlClose(pVfs, handle);
367 return SQLITE_ERROR;
368 }
369
370 /* Append the new shared library handle to the db->aExtension array. */
371 db->nExtension++;
372 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*db->nExtension);
373 if( aHandle==0 ){
374 return SQLITE_NOMEM;
375 }
376 if( db->nExtension>0 ){
377 memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
378 }
379 sqlite3_free(db->aExtension);
380 db->aExtension = aHandle;
381
382 db->aExtension[db->nExtension-1] = handle;
383 return SQLITE_OK;
384}
385int sqlite3_load_extension(
386 sqlite3 *db, /* Load the extension into this database connection */
387 const char *zFile, /* Name of the shared library containing extension */
388 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
389 char **pzErrMsg /* Put error message here if not 0 */
390){
391 int rc;
392 sqlite3_mutex_enter(db->mutex);
393 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
394 sqlite3_mutex_leave(db->mutex);
395 return rc;
396}
397
398/*
399** Call this routine when the database connection is closing in order
400** to clean up loaded extensions
401*/
402void sqlite3CloseExtensions(sqlite3 *db){
403 int i;
404 assert( sqlite3_mutex_held(db->mutex) );
405 for(i=0; i<db->nExtension; i++){
406 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
407 }
408 sqlite3_free(db->aExtension);
409}
410
411/*
412** Enable or disable extension loading. Extension loading is disabled by
413** default so as not to open security holes in older applications.
414*/
415int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
416 sqlite3_mutex_enter(db->mutex);
417 if( onoff ){
418 db->flags |= SQLITE_LoadExtension;
419 }else{
420 db->flags &= ~SQLITE_LoadExtension;
421 }
422 sqlite3_mutex_leave(db->mutex);
423 return SQLITE_OK;
424}
425
426/*
427** The following object holds the list of automatically loaded
428** extensions.
429**
430** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
431** mutex must be held while accessing this list.
432*/
433static struct {
434 int nExt; /* Number of entries in aExt[] */
435 void **aExt; /* Pointers to the extension init functions */
436} autoext = { 0, 0 };
437
438
439/*
440** Register a statically linked extension that is automatically
441** loaded by every new database connection.
442*/
443int sqlite3_auto_extension(void *xInit){
444 int i;
445 int rc = SQLITE_OK;
446 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
447 sqlite3_mutex_enter(mutex);
448 for(i=0; i<autoext.nExt; i++){
449 if( autoext.aExt[i]==xInit ) break;
450 }
451 if( i==autoext.nExt ){
452 int nByte = (autoext.nExt+1)*sizeof(autoext.aExt[0]);
453 void **aNew;
454 aNew = sqlite3_realloc(autoext.aExt, nByte);
455 if( aNew==0 ){
456 rc = SQLITE_NOMEM;
457 }else{
458 autoext.aExt = aNew;
459 autoext.aExt[autoext.nExt] = xInit;
460 autoext.nExt++;
461 }
462 }
463 sqlite3_mutex_leave(mutex);
464 assert( (rc&0xff)==rc );
465 return rc;
466}
467
468/*
469** Reset the automatic extension loading mechanism.
470*/
471void sqlite3_reset_auto_extension(void){
472 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
473 sqlite3_mutex_enter(mutex);
474 sqlite3_free(autoext.aExt);
475 autoext.aExt = 0;
476 autoext.nExt = 0;
477 sqlite3_mutex_leave(mutex);
478}
479
480/*
481** Load all automatic extensions.
482*/
483int sqlite3AutoLoadExtensions(sqlite3 *db){
484 int i;
485 int go = 1;
486 int rc = SQLITE_OK;
487 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
488
489 if( autoext.nExt==0 ){
490 /* Common case: early out without every having to acquire a mutex */
491 return SQLITE_OK;
492 }
493 for(i=0; go; i++){
494 char *zErrmsg = 0;
495 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
496 sqlite3_mutex_enter(mutex);
497 if( i>=autoext.nExt ){
498 xInit = 0;
499 go = 0;
500 }else{
501 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
502 autoext.aExt[i];
503 }
504 sqlite3_mutex_leave(mutex);
505 if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
506 sqlite3Error(db, SQLITE_ERROR,
507 "automatic extension loading failed: %s", zErrmsg);
508 go = 0;
509 rc = SQLITE_ERROR;
510 sqlite3_free(zErrmsg);
511 }
512 }
513 return rc;
514}
515
516#endif /* SQLITE_OMIT_LOAD_EXTENSION */
diff --git a/libraries/sqlite/win32/main.c b/libraries/sqlite/win32/main.c
new file mode 100755
index 0000000..f61fe80
--- /dev/null
+++ b/libraries/sqlite/win32/main.c
@@ -0,0 +1,1485 @@
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.
10**
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.
16**
17** $Id: main.c,v 1.406 2007/10/03 21:10:58 drh Exp $
18*/
19#include "sqliteInt.h"
20#include <ctype.h>
21
22/*
23** The version of the library
24*/
25const char sqlite3_version[] = SQLITE_VERSION;
26const char *sqlite3_libversion(void){ return sqlite3_version; }
27int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
28int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
29
30/*
31** If the following function pointer is not NULL and if
32** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
33** I/O active are written using this function. These messages
34** are intended for debugging activity only.
35*/
36void (*sqlite3_io_trace)(const char*, ...) = 0;
37
38/*
39** If the following global variable points to a string which is the
40** name of a directory, then that directory will be used to store
41** temporary files.
42**
43** See also the "PRAGMA temp_store_directory" SQL command.
44*/
45char *sqlite3_temp_directory = 0;
46
47
48/*
49** This is the default collating function named "BINARY" which is always
50** available.
51*/
52static int binCollFunc(
53 void *NotUsed,
54 int nKey1, const void *pKey1,
55 int nKey2, const void *pKey2
56){
57 int rc, n;
58 n = nKey1<nKey2 ? nKey1 : nKey2;
59 rc = memcmp(pKey1, pKey2, n);
60 if( rc==0 ){
61 rc = nKey1 - nKey2;
62 }
63 return rc;
64}
65
66/*
67** Another built-in collating sequence: NOCASE.
68**
69** This collating sequence is intended to be used for "case independant
70** comparison". SQLite's knowledge of upper and lower case equivalents
71** extends only to the 26 characters used in the English language.
72**
73** At the moment there is only a UTF-8 implementation.
74*/
75static int nocaseCollatingFunc(
76 void *NotUsed,
77 int nKey1, const void *pKey1,
78 int nKey2, const void *pKey2
79){
80 int r = sqlite3StrNICmp(
81 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
82 if( 0==r ){
83 r = nKey1-nKey2;
84 }
85 return r;
86}
87
88/*
89** Return the ROWID of the most recent insert
90*/
91sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
92 return db->lastRowid;
93}
94
95/*
96** Return the number of changes in the most recent call to sqlite3_exec().
97*/
98int sqlite3_changes(sqlite3 *db){
99 return db->nChange;
100}
101
102/*
103** Return the number of changes since the database handle was opened.
104*/
105int sqlite3_total_changes(sqlite3 *db){
106 return db->nTotalChange;
107}
108
109/*
110** Close an existing SQLite database
111*/
112int sqlite3_close(sqlite3 *db){
113 HashElem *i;
114 int j;
115
116 if( !db ){
117 return SQLITE_OK;
118 }
119 if( sqlite3SafetyCheck(db) ){
120 return SQLITE_MISUSE;
121 }
122 sqlite3_mutex_enter(db->mutex);
123
124#ifdef SQLITE_SSE
125 {
126 extern void sqlite3SseCleanup(sqlite3*);
127 sqlite3SseCleanup(db);
128 }
129#endif
130
131 sqlite3ResetInternalSchema(db, 0);
132
133 /* If a transaction is open, the ResetInternalSchema() call above
134 ** will not have called the xDisconnect() method on any virtual
135 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
136 ** call will do so. We need to do this before the check for active
137 ** SQL statements below, as the v-table implementation may be storing
138 ** some prepared statements internally.
139 */
140 sqlite3VtabRollback(db);
141
142 /* If there are any outstanding VMs, return SQLITE_BUSY. */
143 if( db->pVdbe ){
144 sqlite3Error(db, SQLITE_BUSY,
145 "Unable to close due to unfinalised statements");
146 sqlite3_mutex_leave(db->mutex);
147 return SQLITE_BUSY;
148 }
149 assert( !sqlite3SafetyCheck(db) );
150
151 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
152 ** cannot be opened for some reason. So this routine needs to run in
153 ** that case. But maybe there should be an extra magic value for the
154 ** "failed to open" state.
155 **
156 ** TODO: Coverage tests do not test the case where this condition is
157 ** true. It's hard to see how to cause it without messing with threads.
158 */
159 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
160 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
161 sqlite3_mutex_leave(db->mutex);
162 return SQLITE_ERROR;
163 }
164
165 for(j=0; j<db->nDb; j++){
166 struct Db *pDb = &db->aDb[j];
167 if( pDb->pBt ){
168 sqlite3BtreeClose(pDb->pBt);
169 pDb->pBt = 0;
170 if( j!=1 ){
171 pDb->pSchema = 0;
172 }
173 }
174 }
175 sqlite3ResetInternalSchema(db, 0);
176 assert( db->nDb<=2 );
177 assert( db->aDb==db->aDbStatic );
178 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
179 FuncDef *pFunc, *pNext;
180 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
181 pNext = pFunc->pNext;
182 sqlite3_free(pFunc);
183 }
184 }
185
186 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
187 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
188 /* Invoke any destructors registered for collation sequence user data. */
189 for(j=0; j<3; j++){
190 if( pColl[j].xDel ){
191 pColl[j].xDel(pColl[j].pUser);
192 }
193 }
194 sqlite3_free(pColl);
195 }
196 sqlite3HashClear(&db->aCollSeq);
197#ifndef SQLITE_OMIT_VIRTUALTABLE
198 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
199 Module *pMod = (Module *)sqliteHashData(i);
200 if( pMod->xDestroy ){
201 pMod->xDestroy(pMod->pAux);
202 }
203 sqlite3_free(pMod);
204 }
205 sqlite3HashClear(&db->aModule);
206#endif
207
208 sqlite3HashClear(&db->aFunc);
209 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
210 if( db->pErr ){
211 sqlite3ValueFree(db->pErr);
212 }
213 sqlite3CloseExtensions(db);
214
215 db->magic = SQLITE_MAGIC_ERROR;
216
217 /* The temp-database schema is allocated differently from the other schema
218 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
219 ** So it needs to be freed here. Todo: Why not roll the temp schema into
220 ** the same sqliteMalloc() as the one that allocates the database
221 ** structure?
222 */
223 sqlite3_free(db->aDb[1].pSchema);
224 sqlite3_mutex_leave(db->mutex);
225 sqlite3_mutex_free(db->mutex);
226 sqlite3_free(db);
227 return SQLITE_OK;
228}
229
230/*
231** Rollback all database files.
232*/
233void sqlite3RollbackAll(sqlite3 *db){
234 int i;
235 int inTrans = 0;
236 assert( sqlite3_mutex_held(db->mutex) );
237 sqlite3MallocEnterBenignBlock(1); /* Enter benign region */
238 for(i=0; i<db->nDb; i++){
239 if( db->aDb[i].pBt ){
240 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
241 inTrans = 1;
242 }
243 sqlite3BtreeRollback(db->aDb[i].pBt);
244 db->aDb[i].inTrans = 0;
245 }
246 }
247 sqlite3VtabRollback(db);
248 sqlite3MallocLeaveBenignBlock(); /* Leave benign region */
249
250 if( db->flags&SQLITE_InternChanges ){
251 sqlite3ExpirePreparedStatements(db);
252 sqlite3ResetInternalSchema(db, 0);
253 }
254
255 /* If one has been configured, invoke the rollback-hook callback */
256 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
257 db->xRollbackCallback(db->pRollbackArg);
258 }
259}
260
261/*
262** Return a static string that describes the kind of error specified in the
263** argument.
264*/
265const char *sqlite3ErrStr(int rc){
266 const char *z;
267 switch( rc & 0xff ){
268 case SQLITE_ROW:
269 case SQLITE_DONE:
270 case SQLITE_OK: z = "not an error"; break;
271 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
272 case SQLITE_PERM: z = "access permission denied"; break;
273 case SQLITE_ABORT: z = "callback requested query abort"; break;
274 case SQLITE_BUSY: z = "database is locked"; break;
275 case SQLITE_LOCKED: z = "database table is locked"; break;
276 case SQLITE_NOMEM: z = "out of memory"; break;
277 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
278 case SQLITE_INTERRUPT: z = "interrupted"; break;
279 case SQLITE_IOERR: z = "disk I/O error"; break;
280 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
281 case SQLITE_FULL: z = "database or disk is full"; break;
282 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
283 case SQLITE_EMPTY: z = "table contains no data"; break;
284 case SQLITE_SCHEMA: z = "database schema has changed"; break;
285 case SQLITE_TOOBIG: z = "String or BLOB exceeded size limit"; break;
286 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
287 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
288 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
289 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
290 case SQLITE_AUTH: z = "authorization denied"; break;
291 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
292 case SQLITE_RANGE: z = "bind or column index out of range"; break;
293 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
294 default: z = "unknown error"; break;
295 }
296 return z;
297}
298
299/*
300** This routine implements a busy callback that sleeps and tries
301** again until a timeout value is reached. The timeout value is
302** an integer number of milliseconds passed in as the first
303** argument.
304*/
305static int sqliteDefaultBusyCallback(
306 void *ptr, /* Database connection */
307 int count /* Number of times table has been busy */
308){
309#if OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
310 static const u8 delays[] =
311 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
312 static const u8 totals[] =
313 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
314# define NDELAY (sizeof(delays)/sizeof(delays[0]))
315 sqlite3 *db = (sqlite3 *)ptr;
316 int timeout = db->busyTimeout;
317 int delay, prior;
318
319 assert( count>=0 );
320 if( count < NDELAY ){
321 delay = delays[count];
322 prior = totals[count];
323 }else{
324 delay = delays[NDELAY-1];
325 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
326 }
327 if( prior + delay > timeout ){
328 delay = timeout - prior;
329 if( delay<=0 ) return 0;
330 }
331 sqlite3OsSleep(db->pVfs, delay*1000);
332 return 1;
333#else
334 sqlite3 *db = (sqlite3 *)ptr;
335 int timeout = ((sqlite3 *)ptr)->busyTimeout;
336 if( (count+1)*1000 > timeout ){
337 return 0;
338 }
339 sqlite3OsSleep(db->pVfs, 1000000);
340 return 1;
341#endif
342}
343
344/*
345** Invoke the given busy handler.
346**
347** This routine is called when an operation failed with a lock.
348** If this routine returns non-zero, the lock is retried. If it
349** returns 0, the operation aborts with an SQLITE_BUSY error.
350*/
351int sqlite3InvokeBusyHandler(BusyHandler *p){
352 int rc;
353 if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0;
354 rc = p->xFunc(p->pArg, p->nBusy);
355 if( rc==0 ){
356 p->nBusy = -1;
357 }else{
358 p->nBusy++;
359 }
360 return rc;
361}
362
363/*
364** This routine sets the busy callback for an Sqlite database to the
365** given callback function with the given argument.
366*/
367int sqlite3_busy_handler(
368 sqlite3 *db,
369 int (*xBusy)(void*,int),
370 void *pArg
371){
372 if( sqlite3SafetyCheck(db) ){
373 return SQLITE_MISUSE;
374 }
375 sqlite3_mutex_enter(db->mutex);
376 db->busyHandler.xFunc = xBusy;
377 db->busyHandler.pArg = pArg;
378 db->busyHandler.nBusy = 0;
379 sqlite3_mutex_leave(db->mutex);
380 return SQLITE_OK;
381}
382
383#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
384/*
385** This routine sets the progress callback for an Sqlite database to the
386** given callback function with the given argument. The progress callback will
387** be invoked every nOps opcodes.
388*/
389void sqlite3_progress_handler(
390 sqlite3 *db,
391 int nOps,
392 int (*xProgress)(void*),
393 void *pArg
394){
395 if( !sqlite3SafetyCheck(db) ){
396 sqlite3_mutex_enter(db->mutex);
397 if( nOps>0 ){
398 db->xProgress = xProgress;
399 db->nProgressOps = nOps;
400 db->pProgressArg = pArg;
401 }else{
402 db->xProgress = 0;
403 db->nProgressOps = 0;
404 db->pProgressArg = 0;
405 }
406 sqlite3_mutex_leave(db->mutex);
407 }
408}
409#endif
410
411
412/*
413** This routine installs a default busy handler that waits for the
414** specified number of milliseconds before returning 0.
415*/
416int sqlite3_busy_timeout(sqlite3 *db, int ms){
417 if( sqlite3SafetyCheck(db) ){
418 return SQLITE_MISUSE;
419 }
420 if( ms>0 ){
421 db->busyTimeout = ms;
422 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
423 }else{
424 sqlite3_busy_handler(db, 0, 0);
425 }
426 return SQLITE_OK;
427}
428
429/*
430** Cause any pending operation to stop at its earliest opportunity.
431*/
432void sqlite3_interrupt(sqlite3 *db){
433 if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
434 db->u1.isInterrupted = 1;
435 }
436}
437
438
439/*
440** This function is exactly the same as sqlite3_create_function(), except
441** that it is designed to be called by internal code. The difference is
442** that if a malloc() fails in sqlite3_create_function(), an error code
443** is returned and the mallocFailed flag cleared.
444*/
445int sqlite3CreateFunc(
446 sqlite3 *db,
447 const char *zFunctionName,
448 int nArg,
449 int enc,
450 void *pUserData,
451 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
452 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
453 void (*xFinal)(sqlite3_context*)
454){
455 FuncDef *p;
456 int nName;
457
458 assert( sqlite3_mutex_held(db->mutex) );
459 if( sqlite3SafetyCheck(db) ){
460 return SQLITE_MISUSE;
461 }
462 if( zFunctionName==0 ||
463 (xFunc && (xFinal || xStep)) ||
464 (!xFunc && (xFinal && !xStep)) ||
465 (!xFunc && (!xFinal && xStep)) ||
466 (nArg<-1 || nArg>127) ||
467 (255<(nName = strlen(zFunctionName))) ){
468 sqlite3Error(db, SQLITE_ERROR, "bad parameters");
469 return SQLITE_ERROR;
470 }
471
472#ifndef SQLITE_OMIT_UTF16
473 /* If SQLITE_UTF16 is specified as the encoding type, transform this
474 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
475 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
476 **
477 ** If SQLITE_ANY is specified, add three versions of the function
478 ** to the hash table.
479 */
480 if( enc==SQLITE_UTF16 ){
481 enc = SQLITE_UTF16NATIVE;
482 }else if( enc==SQLITE_ANY ){
483 int rc;
484 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
485 pUserData, xFunc, xStep, xFinal);
486 if( rc==SQLITE_OK ){
487 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
488 pUserData, xFunc, xStep, xFinal);
489 }
490 if( rc!=SQLITE_OK ){
491 return rc;
492 }
493 enc = SQLITE_UTF16BE;
494 }
495#else
496 enc = SQLITE_UTF8;
497#endif
498
499 /* Check if an existing function is being overridden or deleted. If so,
500 ** and there are active VMs, then return SQLITE_BUSY. If a function
501 ** is being overridden/deleted but there are no active VMs, allow the
502 ** operation to continue but invalidate all precompiled statements.
503 */
504 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
505 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
506 if( db->activeVdbeCnt ){
507 sqlite3Error(db, SQLITE_BUSY,
508 "Unable to delete/modify user-function due to active statements");
509 assert( !db->mallocFailed );
510 return SQLITE_BUSY;
511 }else{
512 sqlite3ExpirePreparedStatements(db);
513 }
514 }
515
516 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
517 assert(p || db->mallocFailed);
518 if( !p ){
519 return SQLITE_NOMEM;
520 }
521 p->flags = 0;
522 p->xFunc = xFunc;
523 p->xStep = xStep;
524 p->xFinalize = xFinal;
525 p->pUserData = pUserData;
526 p->nArg = nArg;
527 return SQLITE_OK;
528}
529
530/*
531** Create new user functions.
532*/
533int sqlite3_create_function(
534 sqlite3 *db,
535 const char *zFunctionName,
536 int nArg,
537 int enc,
538 void *p,
539 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
540 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
541 void (*xFinal)(sqlite3_context*)
542){
543 int rc;
544 sqlite3_mutex_enter(db->mutex);
545 assert( !db->mallocFailed );
546 rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
547 rc = sqlite3ApiExit(db, rc);
548 sqlite3_mutex_leave(db->mutex);
549 return rc;
550}
551
552#ifndef SQLITE_OMIT_UTF16
553int sqlite3_create_function16(
554 sqlite3 *db,
555 const void *zFunctionName,
556 int nArg,
557 int eTextRep,
558 void *p,
559 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
560 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
561 void (*xFinal)(sqlite3_context*)
562){
563 int rc;
564 char *zFunc8;
565 sqlite3_mutex_enter(db->mutex);
566 assert( !db->mallocFailed );
567 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
568 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
569 sqlite3_free(zFunc8);
570 rc = sqlite3ApiExit(db, rc);
571 sqlite3_mutex_leave(db->mutex);
572 return rc;
573}
574#endif
575
576
577/*
578** Declare that a function has been overloaded by a virtual table.
579**
580** If the function already exists as a regular global function, then
581** this routine is a no-op. If the function does not exist, then create
582** a new one that always throws a run-time error.
583**
584** When virtual tables intend to provide an overloaded function, they
585** should call this routine to make sure the global function exists.
586** A global function must exist in order for name resolution to work
587** properly.
588*/
589int sqlite3_overload_function(
590 sqlite3 *db,
591 const char *zName,
592 int nArg
593){
594 int nName = strlen(zName);
595 int rc;
596 sqlite3_mutex_enter(db->mutex);
597 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
598 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
599 0, sqlite3InvalidFunction, 0, 0);
600 }
601 rc = sqlite3ApiExit(db, SQLITE_OK);
602 sqlite3_mutex_leave(db->mutex);
603 return rc;
604}
605
606#ifndef SQLITE_OMIT_TRACE
607/*
608** Register a trace function. The pArg from the previously registered trace
609** is returned.
610**
611** A NULL trace function means that no tracing is executes. A non-NULL
612** trace is a pointer to a function that is invoked at the start of each
613** SQL statement.
614*/
615void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
616 void *pOld;
617 sqlite3_mutex_enter(db->mutex);
618 pOld = db->pTraceArg;
619 db->xTrace = xTrace;
620 db->pTraceArg = pArg;
621 sqlite3_mutex_leave(db->mutex);
622 return pOld;
623}
624/*
625** Register a profile function. The pArg from the previously registered
626** profile function is returned.
627**
628** A NULL profile function means that no profiling is executes. A non-NULL
629** profile is a pointer to a function that is invoked at the conclusion of
630** each SQL statement that is run.
631*/
632void *sqlite3_profile(
633 sqlite3 *db,
634 void (*xProfile)(void*,const char*,sqlite_uint64),
635 void *pArg
636){
637 void *pOld;
638 sqlite3_mutex_enter(db->mutex);
639 pOld = db->pProfileArg;
640 db->xProfile = xProfile;
641 db->pProfileArg = pArg;
642 sqlite3_mutex_leave(db->mutex);
643 return pOld;
644}
645#endif /* SQLITE_OMIT_TRACE */
646
647/*** EXPERIMENTAL ***
648**
649** Register a function to be invoked when a transaction comments.
650** If the invoked function returns non-zero, then the commit becomes a
651** rollback.
652*/
653void *sqlite3_commit_hook(
654 sqlite3 *db, /* Attach the hook to this database */
655 int (*xCallback)(void*), /* Function to invoke on each commit */
656 void *pArg /* Argument to the function */
657){
658 void *pOld;
659 sqlite3_mutex_enter(db->mutex);
660 pOld = db->pCommitArg;
661 db->xCommitCallback = xCallback;
662 db->pCommitArg = pArg;
663 sqlite3_mutex_leave(db->mutex);
664 return pOld;
665}
666
667/*
668** Register a callback to be invoked each time a row is updated,
669** inserted or deleted using this database connection.
670*/
671void *sqlite3_update_hook(
672 sqlite3 *db, /* Attach the hook to this database */
673 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
674 void *pArg /* Argument to the function */
675){
676 void *pRet;
677 sqlite3_mutex_enter(db->mutex);
678 pRet = db->pUpdateArg;
679 db->xUpdateCallback = xCallback;
680 db->pUpdateArg = pArg;
681 sqlite3_mutex_leave(db->mutex);
682 return pRet;
683}
684
685/*
686** Register a callback to be invoked each time a transaction is rolled
687** back by this database connection.
688*/
689void *sqlite3_rollback_hook(
690 sqlite3 *db, /* Attach the hook to this database */
691 void (*xCallback)(void*), /* Callback function */
692 void *pArg /* Argument to the function */
693){
694 void *pRet;
695 sqlite3_mutex_enter(db->mutex);
696 pRet = db->pRollbackArg;
697 db->xRollbackCallback = xCallback;
698 db->pRollbackArg = pArg;
699 sqlite3_mutex_leave(db->mutex);
700 return pRet;
701}
702
703/*
704** This routine is called to create a connection to a database BTree
705** driver. If zFilename is the name of a file, then that file is
706** opened and used. If zFilename is the magic name ":memory:" then
707** the database is stored in memory (and is thus forgotten as soon as
708** the connection is closed.) If zFilename is NULL then the database
709** is a "virtual" database for transient use only and is deleted as
710** soon as the connection is closed.
711**
712** A virtual database can be either a disk file (that is automatically
713** deleted when the file is closed) or it an be held entirely in memory,
714** depending on the values of the TEMP_STORE compile-time macro and the
715** db->temp_store variable, according to the following chart:
716**
717** TEMP_STORE db->temp_store Location of temporary database
718** ---------- -------------- ------------------------------
719** 0 any file
720** 1 1 file
721** 1 2 memory
722** 1 0 file
723** 2 1 file
724** 2 2 memory
725** 2 0 memory
726** 3 any memory
727*/
728int sqlite3BtreeFactory(
729 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
730 const char *zFilename, /* Name of the file containing the BTree database */
731 int omitJournal, /* if TRUE then do not journal this file */
732 int nCache, /* How many pages in the page cache */
733 int vfsFlags, /* Flags passed through to vfsOpen */
734 Btree **ppBtree /* Pointer to new Btree object written here */
735){
736 int btFlags = 0;
737 int rc;
738
739 assert( sqlite3_mutex_held(db->mutex) );
740 assert( ppBtree != 0);
741 if( omitJournal ){
742 btFlags |= BTREE_OMIT_JOURNAL;
743 }
744 if( db->flags & SQLITE_NoReadlock ){
745 btFlags |= BTREE_NO_READLOCK;
746 }
747 if( zFilename==0 ){
748#if TEMP_STORE==0
749 /* Do nothing */
750#endif
751#ifndef SQLITE_OMIT_MEMORYDB
752#if TEMP_STORE==1
753 if( db->temp_store==2 ) zFilename = ":memory:";
754#endif
755#if TEMP_STORE==2
756 if( db->temp_store!=1 ) zFilename = ":memory:";
757#endif
758#if TEMP_STORE==3
759 zFilename = ":memory:";
760#endif
761#endif /* SQLITE_OMIT_MEMORYDB */
762 }
763
764 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
765 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
766 }
767 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
768 if( rc==SQLITE_OK ){
769 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
770 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
771 }
772 return rc;
773}
774
775/*
776** Return UTF-8 encoded English language explanation of the most recent
777** error.
778*/
779const char *sqlite3_errmsg(sqlite3 *db){
780 const char *z;
781 if( !db ){
782 return sqlite3ErrStr(SQLITE_NOMEM);
783 }
784 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
785 return sqlite3ErrStr(SQLITE_MISUSE);
786 }
787 sqlite3_mutex_enter(db->mutex);
788 assert( !db->mallocFailed );
789 z = (char*)sqlite3_value_text(db->pErr);
790 if( z==0 ){
791 z = sqlite3ErrStr(db->errCode);
792 }
793 sqlite3_mutex_leave(db->mutex);
794 return z;
795}
796
797#ifndef SQLITE_OMIT_UTF16
798/*
799** Return UTF-16 encoded English language explanation of the most recent
800** error.
801*/
802const void *sqlite3_errmsg16(sqlite3 *db){
803 /* Because all the characters in the string are in the unicode
804 ** range 0x00-0xFF, if we pad the big-endian string with a
805 ** zero byte, we can obtain the little-endian string with
806 ** &big_endian[1].
807 */
808 static const char outOfMemBe[] = {
809 0, 'o', 0, 'u', 0, 't', 0, ' ',
810 0, 'o', 0, 'f', 0, ' ',
811 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
812 };
813 static const char misuseBe [] = {
814 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
815 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
816 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
817 0, 'o', 0, 'u', 0, 't', 0, ' ',
818 0, 'o', 0, 'f', 0, ' ',
819 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
820 };
821
822 const void *z;
823 if( !db ){
824 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
825 }
826 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
827 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
828 }
829 sqlite3_mutex_enter(db->mutex);
830 assert( !db->mallocFailed );
831 z = sqlite3_value_text16(db->pErr);
832 if( z==0 ){
833 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
834 SQLITE_UTF8, SQLITE_STATIC);
835 z = sqlite3_value_text16(db->pErr);
836 }
837 sqlite3ApiExit(0, 0);
838 sqlite3_mutex_leave(db->mutex);
839 return z;
840}
841#endif /* SQLITE_OMIT_UTF16 */
842
843/*
844** Return the most recent error code generated by an SQLite routine. If NULL is
845** passed to this function, we assume a malloc() failed during sqlite3_open().
846*/
847int sqlite3_errcode(sqlite3 *db){
848 if( !db || db->mallocFailed ){
849 return SQLITE_NOMEM;
850 }
851 if( sqlite3SafetyCheck(db) ){
852 return SQLITE_MISUSE;
853 }
854 return db->errCode & db->errMask;
855}
856
857/*
858** Create a new collating function for database "db". The name is zName
859** and the encoding is enc.
860*/
861static int createCollation(
862 sqlite3* db,
863 const char *zName,
864 int enc,
865 void* pCtx,
866 int(*xCompare)(void*,int,const void*,int,const void*),
867 void(*xDel)(void*)
868){
869 CollSeq *pColl;
870 int enc2;
871
872 if( sqlite3SafetyCheck(db) ){
873 return SQLITE_MISUSE;
874 }
875 assert( sqlite3_mutex_held(db->mutex) );
876
877 /* If SQLITE_UTF16 is specified as the encoding type, transform this
878 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
879 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
880 */
881 enc2 = enc & ~SQLITE_UTF16_ALIGNED;
882 if( enc2==SQLITE_UTF16 ){
883 enc2 = SQLITE_UTF16NATIVE;
884 }
885
886 if( (enc2&~3)!=0 ){
887 sqlite3Error(db, SQLITE_ERROR, "unknown encoding");
888 return SQLITE_ERROR;
889 }
890
891 /* Check if this call is removing or replacing an existing collation
892 ** sequence. If so, and there are active VMs, return busy. If there
893 ** are no active VMs, invalidate any pre-compiled statements.
894 */
895 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0);
896 if( pColl && pColl->xCmp ){
897 if( db->activeVdbeCnt ){
898 sqlite3Error(db, SQLITE_BUSY,
899 "Unable to delete/modify collation sequence due to active statements");
900 return SQLITE_BUSY;
901 }
902 sqlite3ExpirePreparedStatements(db);
903
904 /* If collation sequence pColl was created directly by a call to
905 ** sqlite3_create_collation, and not generated by synthCollSeq(),
906 ** then any copies made by synthCollSeq() need to be invalidated.
907 ** Also, collation destructor - CollSeq.xDel() - function may need
908 ** to be called.
909 */
910 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
911 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName));
912 int j;
913 for(j=0; j<3; j++){
914 CollSeq *p = &aColl[j];
915 if( p->enc==pColl->enc ){
916 if( p->xDel ){
917 p->xDel(p->pUser);
918 }
919 p->xCmp = 0;
920 }
921 }
922 }
923 }
924
925 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1);
926 if( pColl ){
927 pColl->xCmp = xCompare;
928 pColl->pUser = pCtx;
929 pColl->xDel = xDel;
930 pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED);
931 }
932 sqlite3Error(db, SQLITE_OK, 0);
933 return SQLITE_OK;
934}
935
936
937/*
938** This routine does the work of opening a database on behalf of
939** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
940** is UTF-8 encoded.
941*/
942static int openDatabase(
943 const char *zFilename, /* Database filename UTF-8 encoded */
944 sqlite3 **ppDb, /* OUT: Returned database handle */
945 unsigned flags, /* Operational flags */
946 const char *zVfs /* Name of the VFS to use */
947){
948 sqlite3 *db;
949 int rc;
950 CollSeq *pColl;
951
952 /* Allocate the sqlite data structure */
953 db = sqlite3MallocZero( sizeof(sqlite3) );
954 if( db==0 ) goto opendb_out;
955 db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
956 if( db->mutex==0 ){
957 sqlite3_free(db);
958 db = 0;
959 goto opendb_out;
960 }
961 sqlite3_mutex_enter(db->mutex);
962 db->errMask = 0xff;
963 db->priorNewRowid = 0;
964 db->nDb = 2;
965 db->magic = SQLITE_MAGIC_BUSY;
966 db->aDb = db->aDbStatic;
967 db->autoCommit = 1;
968 db->flags |= SQLITE_ShortColNames
969#if SQLITE_DEFAULT_FILE_FORMAT<4
970 | SQLITE_LegacyFileFmt
971#endif
972#ifdef SQLITE_ENABLE_LOAD_EXTENSION
973 | SQLITE_LoadExtension
974#endif
975 ;
976 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
977 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
978#ifndef SQLITE_OMIT_VIRTUALTABLE
979 sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
980#endif
981
982 db->pVfs = sqlite3_vfs_find(zVfs);
983 if( !db->pVfs ){
984 rc = SQLITE_ERROR;
985 db->magic = SQLITE_MAGIC_CLOSED;
986 sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
987 goto opendb_out;
988 }
989
990 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
991 ** and UTF-16, so add a version for each to avoid any unnecessary
992 ** conversions. The only error that can occur here is a malloc() failure.
993 */
994 if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) ||
995 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
996 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
997 (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0
998 ){
999 assert( db->mallocFailed );
1000 db->magic = SQLITE_MAGIC_CLOSED;
1001 goto opendb_out;
1002 }
1003
1004 /* Also add a UTF-8 case-insensitive collation sequence. */
1005 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
1006
1007 /* Set flags on the built-in collating sequences */
1008 db->pDfltColl->type = SQLITE_COLL_BINARY;
1009 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
1010 if( pColl ){
1011 pColl->type = SQLITE_COLL_NOCASE;
1012 }
1013
1014 /* Open the backend database driver */
1015 db->openFlags = flags;
1016 rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
1017 flags | SQLITE_OPEN_MAIN_DB,
1018 &db->aDb[0].pBt);
1019 if( rc!=SQLITE_OK ){
1020 sqlite3Error(db, rc, 0);
1021 db->magic = SQLITE_MAGIC_CLOSED;
1022 goto opendb_out;
1023 }
1024 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
1025 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
1026
1027
1028 /* The default safety_level for the main database is 'full'; for the temp
1029 ** database it is 'NONE'. This matches the pager layer defaults.
1030 */
1031 db->aDb[0].zName = "main";
1032 db->aDb[0].safety_level = 3;
1033#ifndef SQLITE_OMIT_TEMPDB
1034 db->aDb[1].zName = "temp";
1035 db->aDb[1].safety_level = 1;
1036#endif
1037
1038 db->magic = SQLITE_MAGIC_OPEN;
1039 if( db->mallocFailed ){
1040 goto opendb_out;
1041 }
1042
1043 /* Register all built-in functions, but do not attempt to read the
1044 ** database schema yet. This is delayed until the first time the database
1045 ** is accessed.
1046 */
1047 sqlite3Error(db, SQLITE_OK, 0);
1048 sqlite3RegisterBuiltinFunctions(db);
1049
1050 /* Load automatic extensions - extensions that have been registered
1051 ** using the sqlite3_automatic_extension() API.
1052 */
1053 (void)sqlite3AutoLoadExtensions(db);
1054 if( sqlite3_errcode(db)!=SQLITE_OK ){
1055 goto opendb_out;
1056 }
1057
1058#ifdef SQLITE_ENABLE_FTS1
1059 if( !db->mallocFailed ){
1060 extern int sqlite3Fts1Init(sqlite3*);
1061 rc = sqlite3Fts1Init(db);
1062 }
1063#endif
1064
1065#ifdef SQLITE_ENABLE_FTS2
1066 if( !db->mallocFailed && rc==SQLITE_OK ){
1067 extern int sqlite3Fts2Init(sqlite3*);
1068 rc = sqlite3Fts2Init(db);
1069 }
1070#endif
1071
1072#ifdef SQLITE_ENABLE_FTS3
1073 if( !db->mallocFailed && rc==SQLITE_OK ){
1074 extern int sqlite3Fts3Init(sqlite3*);
1075 rc = sqlite3Fts3Init(db);
1076 }
1077#endif
1078
1079#ifdef SQLITE_ENABLE_ICU
1080 if( !db->mallocFailed && rc==SQLITE_OK ){
1081 extern int sqlite3IcuInit(sqlite3*);
1082 rc = sqlite3IcuInit(db);
1083 }
1084#endif
1085 sqlite3Error(db, rc, 0);
1086
1087 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
1088 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
1089 ** mode. Doing nothing at all also makes NORMAL the default.
1090 */
1091#ifdef SQLITE_DEFAULT_LOCKING_MODE
1092 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
1093 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
1094 SQLITE_DEFAULT_LOCKING_MODE);
1095#endif
1096
1097opendb_out:
1098 if( db && db->mutex ){
1099 sqlite3_mutex_leave(db->mutex);
1100 }
1101 if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
1102 sqlite3_close(db);
1103 db = 0;
1104 }
1105 *ppDb = db;
1106 return sqlite3ApiExit(0, rc);
1107}
1108
1109/*
1110** Open a new database handle.
1111*/
1112int sqlite3_open(
1113 const char *zFilename,
1114 sqlite3 **ppDb
1115){
1116 return openDatabase(zFilename, ppDb,
1117 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1118}
1119int sqlite3_open_v2(
1120 const char *filename, /* Database filename (UTF-8) */
1121 sqlite3 **ppDb, /* OUT: SQLite db handle */
1122 int flags, /* Flags */
1123 const char *zVfs /* Name of VFS module to use */
1124){
1125 return openDatabase(filename, ppDb, flags, zVfs);
1126}
1127
1128#ifndef SQLITE_OMIT_UTF16
1129/*
1130** Open a new database handle.
1131*/
1132int sqlite3_open16(
1133 const void *zFilename,
1134 sqlite3 **ppDb
1135){
1136 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1137 sqlite3_value *pVal;
1138 int rc = SQLITE_NOMEM;
1139
1140 assert( zFilename );
1141 assert( ppDb );
1142 *ppDb = 0;
1143 pVal = sqlite3ValueNew(0);
1144 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1145 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1146 if( zFilename8 ){
1147 rc = openDatabase(zFilename8, ppDb,
1148 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1149 if( rc==SQLITE_OK && *ppDb ){
1150 rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1151 if( rc!=SQLITE_OK ){
1152 sqlite3_close(*ppDb);
1153 *ppDb = 0;
1154 }
1155 }
1156 }
1157 sqlite3ValueFree(pVal);
1158
1159 return sqlite3ApiExit(0, rc);
1160}
1161#endif /* SQLITE_OMIT_UTF16 */
1162
1163/*
1164** Register a new collation sequence with the database handle db.
1165*/
1166int sqlite3_create_collation(
1167 sqlite3* db,
1168 const char *zName,
1169 int enc,
1170 void* pCtx,
1171 int(*xCompare)(void*,int,const void*,int,const void*)
1172){
1173 int rc;
1174 sqlite3_mutex_enter(db->mutex);
1175 assert( !db->mallocFailed );
1176 rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
1177 rc = sqlite3ApiExit(db, rc);
1178 sqlite3_mutex_leave(db->mutex);
1179 return rc;
1180}
1181
1182/*
1183** Register a new collation sequence with the database handle db.
1184*/
1185int sqlite3_create_collation_v2(
1186 sqlite3* db,
1187 const char *zName,
1188 int enc,
1189 void* pCtx,
1190 int(*xCompare)(void*,int,const void*,int,const void*),
1191 void(*xDel)(void*)
1192){
1193 int rc;
1194 sqlite3_mutex_enter(db->mutex);
1195 assert( !db->mallocFailed );
1196 rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
1197 rc = sqlite3ApiExit(db, rc);
1198 sqlite3_mutex_leave(db->mutex);
1199 return rc;
1200}
1201
1202#ifndef SQLITE_OMIT_UTF16
1203/*
1204** Register a new collation sequence with the database handle db.
1205*/
1206int sqlite3_create_collation16(
1207 sqlite3* db,
1208 const char *zName,
1209 int enc,
1210 void* pCtx,
1211 int(*xCompare)(void*,int,const void*,int,const void*)
1212){
1213 int rc = SQLITE_OK;
1214 char *zName8;
1215 sqlite3_mutex_enter(db->mutex);
1216 assert( !db->mallocFailed );
1217 zName8 = sqlite3Utf16to8(db, zName, -1);
1218 if( zName8 ){
1219 rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
1220 sqlite3_free(zName8);
1221 }
1222 rc = sqlite3ApiExit(db, rc);
1223 sqlite3_mutex_leave(db->mutex);
1224 return rc;
1225}
1226#endif /* SQLITE_OMIT_UTF16 */
1227
1228/*
1229** Register a collation sequence factory callback with the database handle
1230** db. Replace any previously installed collation sequence factory.
1231*/
1232int sqlite3_collation_needed(
1233 sqlite3 *db,
1234 void *pCollNeededArg,
1235 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1236){
1237 if( sqlite3SafetyCheck(db) ){
1238 return SQLITE_MISUSE;
1239 }
1240 sqlite3_mutex_enter(db->mutex);
1241 db->xCollNeeded = xCollNeeded;
1242 db->xCollNeeded16 = 0;
1243 db->pCollNeededArg = pCollNeededArg;
1244 sqlite3_mutex_leave(db->mutex);
1245 return SQLITE_OK;
1246}
1247
1248#ifndef SQLITE_OMIT_UTF16
1249/*
1250** Register a collation sequence factory callback with the database handle
1251** db. Replace any previously installed collation sequence factory.
1252*/
1253int sqlite3_collation_needed16(
1254 sqlite3 *db,
1255 void *pCollNeededArg,
1256 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1257){
1258 if( sqlite3SafetyCheck(db) ){
1259 return SQLITE_MISUSE;
1260 }
1261 sqlite3_mutex_enter(db->mutex);
1262 db->xCollNeeded = 0;
1263 db->xCollNeeded16 = xCollNeeded16;
1264 db->pCollNeededArg = pCollNeededArg;
1265 sqlite3_mutex_leave(db->mutex);
1266 return SQLITE_OK;
1267}
1268#endif /* SQLITE_OMIT_UTF16 */
1269
1270#ifndef SQLITE_OMIT_GLOBALRECOVER
1271/*
1272** This function is now an anachronism. It used to be used to recover from a
1273** malloc() failure, but SQLite now does this automatically.
1274*/
1275int sqlite3_global_recover(){
1276 return SQLITE_OK;
1277}
1278#endif
1279
1280/*
1281** Test to see whether or not the database connection is in autocommit
1282** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
1283** by default. Autocommit is disabled by a BEGIN statement and reenabled
1284** by the next COMMIT or ROLLBACK.
1285**
1286******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1287*/
1288int sqlite3_get_autocommit(sqlite3 *db){
1289 return db->autoCommit;
1290}
1291
1292#ifdef SQLITE_DEBUG
1293/*
1294** The following routine is subtituted for constant SQLITE_CORRUPT in
1295** debugging builds. This provides a way to set a breakpoint for when
1296** corruption is first detected.
1297*/
1298int sqlite3Corrupt(void){
1299 return SQLITE_CORRUPT;
1300}
1301#endif
1302
1303/*
1304** This is a convenience routine that makes sure that all thread-specific
1305** data for this thread has been deallocated.
1306**
1307** SQLite no longer uses thread-specific data so this routine is now a
1308** no-op. It is retained for historical compatibility.
1309*/
1310void sqlite3_thread_cleanup(void){
1311}
1312
1313/*
1314** Return meta information about a specific column of a database table.
1315** See comment in sqlite3.h (sqlite.h.in) for details.
1316*/
1317#ifdef SQLITE_ENABLE_COLUMN_METADATA
1318int sqlite3_table_column_metadata(
1319 sqlite3 *db, /* Connection handle */
1320 const char *zDbName, /* Database name or NULL */
1321 const char *zTableName, /* Table name */
1322 const char *zColumnName, /* Column name */
1323 char const **pzDataType, /* OUTPUT: Declared data type */
1324 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
1325 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
1326 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
1327 int *pAutoinc /* OUTPUT: True if colums is auto-increment */
1328){
1329 int rc;
1330 char *zErrMsg = 0;
1331 Table *pTab = 0;
1332 Column *pCol = 0;
1333 int iCol;
1334
1335 char const *zDataType = 0;
1336 char const *zCollSeq = 0;
1337 int notnull = 0;
1338 int primarykey = 0;
1339 int autoinc = 0;
1340
1341 /* Ensure the database schema has been loaded */
1342 if( sqlite3SafetyOn(db) ){
1343 return SQLITE_MISUSE;
1344 }
1345 sqlite3_mutex_enter(db->mutex);
1346 rc = sqlite3Init(db, &zErrMsg);
1347 if( SQLITE_OK!=rc ){
1348 goto error_out;
1349 }
1350
1351 /* Locate the table in question */
1352 pTab = sqlite3FindTable(db, zTableName, zDbName);
1353 if( !pTab || pTab->pSelect ){
1354 pTab = 0;
1355 goto error_out;
1356 }
1357
1358 /* Find the column for which info is requested */
1359 if( sqlite3IsRowid(zColumnName) ){
1360 iCol = pTab->iPKey;
1361 if( iCol>=0 ){
1362 pCol = &pTab->aCol[iCol];
1363 }
1364 }else{
1365 for(iCol=0; iCol<pTab->nCol; iCol++){
1366 pCol = &pTab->aCol[iCol];
1367 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
1368 break;
1369 }
1370 }
1371 if( iCol==pTab->nCol ){
1372 pTab = 0;
1373 goto error_out;
1374 }
1375 }
1376
1377 /* The following block stores the meta information that will be returned
1378 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
1379 ** and autoinc. At this point there are two possibilities:
1380 **
1381 ** 1. The specified column name was rowid", "oid" or "_rowid_"
1382 ** and there is no explicitly declared IPK column.
1383 **
1384 ** 2. The table is not a view and the column name identified an
1385 ** explicitly declared column. Copy meta information from *pCol.
1386 */
1387 if( pCol ){
1388 zDataType = pCol->zType;
1389 zCollSeq = pCol->zColl;
1390 notnull = (pCol->notNull?1:0);
1391 primarykey = (pCol->isPrimKey?1:0);
1392 autoinc = ((pTab->iPKey==iCol && pTab->autoInc)?1:0);
1393 }else{
1394 zDataType = "INTEGER";
1395 primarykey = 1;
1396 }
1397 if( !zCollSeq ){
1398 zCollSeq = "BINARY";
1399 }
1400
1401error_out:
1402 if( sqlite3SafetyOff(db) ){
1403 rc = SQLITE_MISUSE;
1404 }
1405
1406 /* Whether the function call succeeded or failed, set the output parameters
1407 ** to whatever their local counterparts contain. If an error did occur,
1408 ** this has the effect of zeroing all output parameters.
1409 */
1410 if( pzDataType ) *pzDataType = zDataType;
1411 if( pzCollSeq ) *pzCollSeq = zCollSeq;
1412 if( pNotNull ) *pNotNull = notnull;
1413 if( pPrimaryKey ) *pPrimaryKey = primarykey;
1414 if( pAutoinc ) *pAutoinc = autoinc;
1415
1416 if( SQLITE_OK==rc && !pTab ){
1417 sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".",
1418 zColumnName, 0);
1419 rc = SQLITE_ERROR;
1420 }
1421 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
1422 sqlite3_free(zErrMsg);
1423 rc = sqlite3ApiExit(db, rc);
1424 sqlite3_mutex_leave(db->mutex);
1425 return rc;
1426}
1427#endif
1428
1429/*
1430** Sleep for a little while. Return the amount of time slept.
1431*/
1432int sqlite3_sleep(int ms){
1433 sqlite3_vfs *pVfs;
1434 int rc;
1435 pVfs = sqlite3_vfs_find(0);
1436
1437 /* This function works in milliseconds, but the underlying OsSleep()
1438 ** API uses microseconds. Hence the 1000's.
1439 */
1440 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
1441 return rc;
1442}
1443
1444/*
1445** Enable or disable the extended result codes.
1446*/
1447int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
1448 sqlite3_mutex_enter(db->mutex);
1449 db->errMask = onoff ? 0xffffffff : 0xff;
1450 sqlite3_mutex_leave(db->mutex);
1451 return SQLITE_OK;
1452}
1453
1454/*
1455** Invoke the xFileControl method on a particular database.
1456*/
1457int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
1458 int rc = SQLITE_ERROR;
1459 int iDb;
1460 sqlite3_mutex_enter(db->mutex);
1461 if( zDbName==0 ){
1462 iDb = 0;
1463 }else{
1464 for(iDb=0; iDb<db->nDb; iDb++){
1465 if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
1466 }
1467 }
1468 if( iDb<db->nDb ){
1469 Btree *pBtree = db->aDb[iDb].pBt;
1470 if( pBtree ){
1471 Pager *pPager;
1472 sqlite3BtreeEnter(pBtree);
1473 pPager = sqlite3BtreePager(pBtree);
1474 if( pPager ){
1475 sqlite3_file *fd = sqlite3PagerFile(pPager);
1476 if( fd ){
1477 rc = sqlite3OsFileControl(fd, op, pArg);
1478 }
1479 }
1480 sqlite3BtreeLeave(pBtree);
1481 }
1482 }
1483 sqlite3_mutex_leave(db->mutex);
1484 return rc;
1485}
diff --git a/libraries/sqlite/win32/malloc.c b/libraries/sqlite/win32/malloc.c
new file mode 100755
index 0000000..bc321ab
--- /dev/null
+++ b/libraries/sqlite/win32/malloc.c
@@ -0,0 +1,240 @@
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.
10**
11*************************************************************************
12** Memory allocation functions used throughout sqlite.
13**
14**
15** $Id: malloc.c,v 1.13 2007/08/29 14:06:23 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18#include <stdarg.h>
19#include <ctype.h>
20
21/*
22** This routine runs when the memory allocator sees that the
23** total memory allocation is about to exceed the soft heap
24** limit.
25*/
26static void softHeapLimitEnforcer(
27 void *NotUsed,
28 sqlite3_int64 inUse,
29 int allocSize
30){
31 sqlite3_release_memory(allocSize);
32}
33
34/*
35** Set the soft heap-size limit for the current thread. Passing a
36** zero or negative value indicates no limit.
37*/
38void sqlite3_soft_heap_limit(int n){
39 sqlite3_uint64 iLimit;
40 int overage;
41 if( n<0 ){
42 iLimit = 0;
43 }else{
44 iLimit = n;
45 }
46 if( iLimit>0 ){
47 sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
48 }else{
49 sqlite3_memory_alarm(0, 0, 0);
50 }
51 overage = sqlite3_memory_used() - n;
52 if( overage>0 ){
53 sqlite3_release_memory(overage);
54 }
55}
56
57/*
58** Release memory held by SQLite instances created by the current thread.
59*/
60int sqlite3_release_memory(int n){
61#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
62 return sqlite3PagerReleaseMemory(n);
63#else
64 return SQLITE_OK;
65#endif
66}
67
68
69/*
70** Allocate and zero memory.
71*/
72void *sqlite3MallocZero(unsigned n){
73 void *p = sqlite3_malloc(n);
74 if( p ){
75 memset(p, 0, n);
76 }
77 return p;
78}
79
80/*
81** Allocate and zero memory. If the allocation fails, make
82** the mallocFailed flag in the connection pointer.
83*/
84void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
85 void *p = sqlite3DbMallocRaw(db, n);
86 if( p ){
87 memset(p, 0, n);
88 }
89 return p;
90}
91
92/*
93** Allocate and zero memory. If the allocation fails, make
94** the mallocFailed flag in the connection pointer.
95*/
96void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
97 void *p = 0;
98 if( !db || db->mallocFailed==0 ){
99 p = sqlite3_malloc(n);
100 if( !p && db ){
101 db->mallocFailed = 1;
102 }
103 }
104 return p;
105}
106
107/*
108** Resize the block of memory pointed to by p to n bytes. If the
109** resize fails, set the mallocFailed flag inthe connection object.
110*/
111void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
112 void *pNew = 0;
113 if( db->mallocFailed==0 ){
114 pNew = sqlite3_realloc(p, n);
115 if( !pNew ){
116 db->mallocFailed = 1;
117 }
118 }
119 return pNew;
120}
121
122/*
123** Attempt to reallocate p. If the reallocation fails, then free p
124** and set the mallocFailed flag in the database connection.
125*/
126void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
127 void *pNew;
128 pNew = sqlite3DbRealloc(db, p, n);
129 if( !pNew ){
130 sqlite3_free(p);
131 }
132 return pNew;
133}
134
135/*
136** Make a copy of a string in memory obtained from sqliteMalloc(). These
137** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
138** is because when memory debugging is turned on, these two functions are
139** called via macros that record the current file and line number in the
140** ThreadData structure.
141*/
142char *sqlite3StrDup(const char *z){
143 char *zNew;
144 int n;
145 if( z==0 ) return 0;
146 n = strlen(z)+1;
147 zNew = sqlite3_malloc(n);
148 if( zNew ) memcpy(zNew, z, n);
149 return zNew;
150}
151char *sqlite3StrNDup(const char *z, int n){
152 char *zNew;
153 if( z==0 ) return 0;
154 zNew = sqlite3_malloc(n+1);
155 if( zNew ){
156 memcpy(zNew, z, n);
157 zNew[n] = 0;
158 }
159 return zNew;
160}
161
162char *sqlite3DbStrDup(sqlite3 *db, const char *z){
163 char *zNew = sqlite3StrDup(z);
164 if( z && !zNew ){
165 db->mallocFailed = 1;
166 }
167 return zNew;
168}
169char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
170 char *zNew = sqlite3StrNDup(z, n);
171 if( z && !zNew ){
172 db->mallocFailed = 1;
173 }
174 return zNew;
175}
176
177/*
178** Create a string from the 2nd and subsequent arguments (up to the
179** first NULL argument), store the string in memory obtained from
180** sqliteMalloc() and make the pointer indicated by the 1st argument
181** point to that string. The 1st argument must either be NULL or
182** point to memory obtained from sqliteMalloc().
183*/
184void sqlite3SetString(char **pz, ...){
185 va_list ap;
186 int nByte;
187 const char *z;
188 char *zResult;
189
190 assert( pz!=0 );
191 nByte = 1;
192 va_start(ap, pz);
193 while( (z = va_arg(ap, const char*))!=0 ){
194 nByte += strlen(z);
195 }
196 va_end(ap);
197 sqlite3_free(*pz);
198 *pz = zResult = sqlite3_malloc(nByte);
199 if( zResult==0 ){
200 return;
201 }
202 *zResult = 0;
203 va_start(ap, pz);
204 while( (z = va_arg(ap, const char*))!=0 ){
205 int n = strlen(z);
206 memcpy(zResult, z, n);
207 zResult += n;
208 }
209 zResult[0] = 0;
210 va_end(ap);
211}
212
213
214/*
215** This function must be called before exiting any API function (i.e.
216** returning control to the user) that has called sqlite3_malloc or
217** sqlite3_realloc.
218**
219** The returned value is normally a copy of the second argument to this
220** function. However, if a malloc() failure has occured since the previous
221** invocation SQLITE_NOMEM is returned instead.
222**
223** If the first argument, db, is not NULL and a malloc() error has occured,
224** then the connection error-code (the value returned by sqlite3_errcode())
225** is set to SQLITE_NOMEM.
226*/
227int sqlite3ApiExit(sqlite3* db, int rc){
228 /* If the db handle is not NULL, then we must hold the connection handle
229 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
230 ** is unsafe, as is the call to sqlite3Error().
231 */
232 assert( !db || sqlite3_mutex_held(db->mutex) );
233 if( db && db->mallocFailed ){
234 sqlite3Error(db, SQLITE_NOMEM, 0);
235 db->mallocFailed = 0;
236 rc = SQLITE_NOMEM;
237 }
238 return rc & (db ? db->errMask : 0xff);
239}
240
diff --git a/libraries/sqlite/win32/mem1.c b/libraries/sqlite/win32/mem1.c
new file mode 100755
index 0000000..1e9fcfa
--- /dev/null
+++ b/libraries/sqlite/win32/mem1.c
@@ -0,0 +1,229 @@
1/*
2** 2007 August 14
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.
10**
11*************************************************************************
12** This file contains the C functions that implement a memory
13** allocation subsystem for use by SQLite.
14**
15** $Id: mem1.c,v 1.10 2007/09/02 17:50:35 drh Exp $
16*/
17
18/*
19** This version of the memory allocator is the default. It is
20** used when no other memory allocator is specified using compile-time
21** macros.
22*/
23#if !defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
24
25/*
26** We will eventually construct multiple memory allocation subsystems
27** suitable for use in various contexts:
28**
29** * Normal multi-threaded builds
30** * Normal single-threaded builds
31** * Debugging builds
32**
33** This initial version is suitable for use in normal multi-threaded
34** builds. We envision that alternative versions will be stored in
35** separate source files. #ifdefs will be used to select the code from
36** one of the various memN.c source files for use in any given build.
37*/
38#include "sqliteInt.h"
39
40/*
41** All of the static variables used by this module are collected
42** into a single structure named "mem". This is to keep the
43** static variables organized and to reduce namespace pollution
44** when this module is combined with other in the amalgamation.
45*/
46static struct {
47 /*
48 ** The alarm callback and its arguments. The mem.mutex lock will
49 ** be held while the callback is running. Recursive calls into
50 ** the memory subsystem are allowed, but no new callbacks will be
51 ** issued. The alarmBusy variable is set to prevent recursive
52 ** callbacks.
53 */
54 sqlite3_int64 alarmThreshold;
55 void (*alarmCallback)(void*, sqlite3_int64,int);
56 void *alarmArg;
57 int alarmBusy;
58
59 /*
60 ** Mutex to control access to the memory allocation subsystem.
61 */
62 sqlite3_mutex *mutex;
63
64 /*
65 ** Current allocation and high-water mark.
66 */
67 sqlite3_int64 nowUsed;
68 sqlite3_int64 mxUsed;
69
70
71} mem;
72
73/*
74** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
75*/
76static void enterMem(void){
77 if( mem.mutex==0 ){
78 mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
79 }
80 sqlite3_mutex_enter(mem.mutex);
81}
82
83/*
84** Return the amount of memory currently checked out.
85*/
86sqlite3_int64 sqlite3_memory_used(void){
87 sqlite3_int64 n;
88 enterMem();
89 n = mem.nowUsed;
90 sqlite3_mutex_leave(mem.mutex);
91 return n;
92}
93
94/*
95** Return the maximum amount of memory that has ever been
96** checked out since either the beginning of this process
97** or since the most recent reset.
98*/
99sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
100 sqlite3_int64 n;
101 enterMem();
102 n = mem.mxUsed;
103 if( resetFlag ){
104 mem.mxUsed = mem.nowUsed;
105 }
106 sqlite3_mutex_leave(mem.mutex);
107 return n;
108}
109
110/*
111** Change the alarm callback
112*/
113int sqlite3_memory_alarm(
114 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
115 void *pArg,
116 sqlite3_int64 iThreshold
117){
118 enterMem();
119 mem.alarmCallback = xCallback;
120 mem.alarmArg = pArg;
121 mem.alarmThreshold = iThreshold;
122 sqlite3_mutex_leave(mem.mutex);
123 return SQLITE_OK;
124}
125
126/*
127** Trigger the alarm
128*/
129static void sqlite3MemsysAlarm(int nByte){
130 void (*xCallback)(void*,sqlite3_int64,int);
131 sqlite3_int64 nowUsed;
132 void *pArg;
133 if( mem.alarmCallback==0 || mem.alarmBusy ) return;
134 mem.alarmBusy = 1;
135 xCallback = mem.alarmCallback;
136 nowUsed = mem.nowUsed;
137 pArg = mem.alarmArg;
138 sqlite3_mutex_leave(mem.mutex);
139 xCallback(pArg, nowUsed, nByte);
140 sqlite3_mutex_enter(mem.mutex);
141 mem.alarmBusy = 0;
142}
143
144/*
145** Allocate nBytes of memory
146*/
147void *sqlite3_malloc(int nBytes){
148 sqlite3_int64 *p = 0;
149 if( nBytes>0 ){
150 enterMem();
151 if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){
152 sqlite3MemsysAlarm(nBytes);
153 }
154 p = malloc(nBytes+8);
155 if( p==0 ){
156 sqlite3MemsysAlarm(nBytes);
157 p = malloc(nBytes+8);
158 }
159 if( p ){
160 p[0] = nBytes;
161 p++;
162 mem.nowUsed += nBytes;
163 if( mem.nowUsed>mem.mxUsed ){
164 mem.mxUsed = mem.nowUsed;
165 }
166 }
167 sqlite3_mutex_leave(mem.mutex);
168 }
169 return (void*)p;
170}
171
172/*
173** Free memory.
174*/
175void sqlite3_free(void *pPrior){
176 sqlite3_int64 *p;
177 int nByte;
178 if( pPrior==0 ){
179 return;
180 }
181 assert( mem.mutex!=0 );
182 p = pPrior;
183 p--;
184 nByte = (int)*p;
185 sqlite3_mutex_enter(mem.mutex);
186 mem.nowUsed -= nByte;
187 free(p);
188 sqlite3_mutex_leave(mem.mutex);
189}
190
191/*
192** Change the size of an existing memory allocation
193*/
194void *sqlite3_realloc(void *pPrior, int nBytes){
195 int nOld;
196 sqlite3_int64 *p;
197 if( pPrior==0 ){
198 return sqlite3_malloc(nBytes);
199 }
200 if( nBytes<=0 ){
201 sqlite3_free(pPrior);
202 return 0;
203 }
204 p = pPrior;
205 p--;
206 nOld = (int)p[0];
207 assert( mem.mutex!=0 );
208 sqlite3_mutex_enter(mem.mutex);
209 if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){
210 sqlite3MemsysAlarm(nBytes-nOld);
211 }
212 p = realloc(p, nBytes+8);
213 if( p==0 ){
214 sqlite3MemsysAlarm(nBytes);
215 p = realloc(p, nBytes+8);
216 }
217 if( p ){
218 p[0] = nBytes;
219 p++;
220 mem.nowUsed += nBytes-nOld;
221 if( mem.nowUsed>mem.mxUsed ){
222 mem.mxUsed = mem.nowUsed;
223 }
224 }
225 sqlite3_mutex_leave(mem.mutex);
226 return (void*)p;
227}
228
229#endif /* !SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */
diff --git a/libraries/sqlite/win32/mem2.c b/libraries/sqlite/win32/mem2.c
new file mode 100755
index 0000000..7c509ff
--- /dev/null
+++ b/libraries/sqlite/win32/mem2.c
@@ -0,0 +1,546 @@
1/*
2** 2007 August 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.
10**
11*************************************************************************
12** This file contains the C functions that implement a memory
13** allocation subsystem for use by SQLite.
14**
15** $Id: mem2.c,v 1.14 2007/10/03 08:46:45 danielk1977 Exp $
16*/
17
18/*
19** This version of the memory allocator is used only if the
20** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
21** is not defined.
22*/
23#if defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
24
25/*
26** We will eventually construct multiple memory allocation subsystems
27** suitable for use in various contexts:
28**
29** * Normal multi-threaded builds
30** * Normal single-threaded builds
31** * Debugging builds
32**
33** This version is suitable for use in debugging builds.
34**
35** Features:
36**
37** * Every allocate has guards at both ends.
38** * New allocations are initialized with randomness
39** * Allocations are overwritten with randomness when freed
40** * Optional logs of malloc activity generated
41** * Summary of outstanding allocations with backtraces to the
42** point of allocation.
43** * The ability to simulate memory allocation failure
44*/
45#include "sqliteInt.h"
46#include <stdio.h>
47
48/*
49** The backtrace functionality is only available with GLIBC
50*/
51#ifdef __GLIBC__
52 extern int backtrace(void**,int);
53 extern void backtrace_symbols_fd(void*const*,int,int);
54#else
55# define backtrace(A,B) 0
56# define backtrace_symbols_fd(A,B,C)
57#endif
58
59/*
60** Each memory allocation looks like this:
61**
62** ------------------------------------------------------------------------
63** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
64** ------------------------------------------------------------------------
65**
66** The application code sees only a pointer to the allocation. We have
67** to back up from the allocation pointer to find the MemBlockHdr. The
68** MemBlockHdr tells us the size of the allocation and the number of
69** backtrace pointers. There is also a guard word at the end of the
70** MemBlockHdr.
71*/
72struct MemBlockHdr {
73 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
74 int iSize; /* Size of this allocation */
75 char nBacktrace; /* Number of backtraces on this alloc */
76 char nBacktraceSlots; /* Available backtrace slots */
77 short nTitle; /* Bytes of title; includes '\0' */
78 int iForeGuard; /* Guard word for sanity */
79};
80
81/*
82** Guard words
83*/
84#define FOREGUARD 0x80F5E153
85#define REARGUARD 0xE4676B53
86
87/*
88** All of the static variables used by this module are collected
89** into a single structure named "mem". This is to keep the
90** static variables organized and to reduce namespace pollution
91** when this module is combined with other in the amalgamation.
92*/
93static struct {
94 /*
95 ** The alarm callback and its arguments. The mem.mutex lock will
96 ** be held while the callback is running. Recursive calls into
97 ** the memory subsystem are allowed, but no new callbacks will be
98 ** issued. The alarmBusy variable is set to prevent recursive
99 ** callbacks.
100 */
101 sqlite3_int64 alarmThreshold;
102 void (*alarmCallback)(void*, sqlite3_int64, int);
103 void *alarmArg;
104 int alarmBusy;
105
106 /*
107 ** Mutex to control access to the memory allocation subsystem.
108 */
109 sqlite3_mutex *mutex;
110
111 /*
112 ** Current allocation and high-water mark.
113 */
114 sqlite3_int64 nowUsed;
115 sqlite3_int64 mxUsed;
116
117 /*
118 ** Head and tail of a linked list of all outstanding allocations
119 */
120 struct MemBlockHdr *pFirst;
121 struct MemBlockHdr *pLast;
122
123 /*
124 ** The number of levels of backtrace to save in new allocations.
125 */
126 int nBacktrace;
127
128 /*
129 ** Title text to insert in front of each block
130 */
131 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
132 char zTitle[100]; /* The title text */
133
134 /*
135 ** These values are used to simulate malloc failures. When
136 ** iFail is 1, simulate a malloc failures and reset the value
137 ** to iReset.
138 */
139 int iFail; /* Decrement and fail malloc when this is 1 */
140 int iReset; /* When malloc fails set iiFail to this value */
141 int iFailCnt; /* Number of failures */
142 int iBenignFailCnt; /* Number of benign failures */
143 int iNextIsBenign; /* True if the next call to malloc may fail benignly */
144 int iIsBenign; /* All malloc calls may fail benignly */
145
146 /*
147 ** sqlite3MallocDisallow() increments the following counter.
148 ** sqlite3MallocAllow() decrements it.
149 */
150 int disallow; /* Do not allow memory allocation */
151
152
153} mem;
154
155
156/*
157** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
158*/
159static void enterMem(void){
160 if( mem.mutex==0 ){
161 mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
162 }
163 sqlite3_mutex_enter(mem.mutex);
164}
165
166/*
167** Return the amount of memory currently checked out.
168*/
169sqlite3_int64 sqlite3_memory_used(void){
170 sqlite3_int64 n;
171 enterMem();
172 n = mem.nowUsed;
173 sqlite3_mutex_leave(mem.mutex);
174 return n;
175}
176
177/*
178** Return the maximum amount of memory that has ever been
179** checked out since either the beginning of this process
180** or since the most recent reset.
181*/
182sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
183 sqlite3_int64 n;
184 enterMem();
185 n = mem.mxUsed;
186 if( resetFlag ){
187 mem.mxUsed = mem.nowUsed;
188 }
189 sqlite3_mutex_leave(mem.mutex);
190 return n;
191}
192
193/*
194** Change the alarm callback
195*/
196int sqlite3_memory_alarm(
197 void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
198 void *pArg,
199 sqlite3_int64 iThreshold
200){
201 enterMem();
202 mem.alarmCallback = xCallback;
203 mem.alarmArg = pArg;
204 mem.alarmThreshold = iThreshold;
205 sqlite3_mutex_leave(mem.mutex);
206 return SQLITE_OK;
207}
208
209/*
210** Trigger the alarm
211*/
212static void sqlite3MemsysAlarm(int nByte){
213 void (*xCallback)(void*,sqlite3_int64,int);
214 sqlite3_int64 nowUsed;
215 void *pArg;
216 if( mem.alarmCallback==0 || mem.alarmBusy ) return;
217 mem.alarmBusy = 1;
218 xCallback = mem.alarmCallback;
219 nowUsed = mem.nowUsed;
220 pArg = mem.alarmArg;
221 sqlite3_mutex_leave(mem.mutex);
222 xCallback(pArg, nowUsed, nByte);
223 sqlite3_mutex_enter(mem.mutex);
224 mem.alarmBusy = 0;
225}
226
227/*
228** Given an allocation, find the MemBlockHdr for that allocation.
229**
230** This routine checks the guards at either end of the allocation and
231** if they are incorrect it asserts.
232*/
233static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
234 struct MemBlockHdr *p;
235 int *pInt;
236
237 p = (struct MemBlockHdr*)pAllocation;
238 p--;
239 assert( p->iForeGuard==FOREGUARD );
240 assert( (p->iSize & 3)==0 );
241 pInt = (int*)pAllocation;
242 assert( pInt[p->iSize/sizeof(int)]==REARGUARD );
243 return p;
244}
245
246/*
247** This routine is called once the first time a simulated memory
248** failure occurs. The sole purpose of this routine is to provide
249** a convenient place to set a debugger breakpoint when debugging
250** errors related to malloc() failures.
251*/
252static void sqlite3MemsysFailed(void){
253 mem.iFailCnt = 0;
254 mem.iBenignFailCnt = 0;
255}
256
257/*
258** Allocate nByte bytes of memory.
259*/
260void *sqlite3_malloc(int nByte){
261 struct MemBlockHdr *pHdr;
262 void **pBt;
263 char *z;
264 int *pInt;
265 void *p = 0;
266 int totalSize;
267
268 if( nByte>0 ){
269 enterMem();
270 assert( mem.disallow==0 );
271 if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
272 sqlite3MemsysAlarm(nByte);
273 }
274 nByte = (nByte+3)&~3;
275 totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
276 mem.nBacktrace*sizeof(void*) + mem.nTitle;
277 if( mem.iFail>0 ){
278 if( mem.iFail==1 ){
279 p = 0;
280 mem.iFail = mem.iReset;
281 if( mem.iFailCnt==0 ){
282 sqlite3MemsysFailed(); /* A place to set a breakpoint */
283 }
284 mem.iFailCnt++;
285 if( mem.iNextIsBenign || mem.iIsBenign ){
286 mem.iBenignFailCnt++;
287 }
288 }else{
289 p = malloc(totalSize);
290 mem.iFail--;
291 }
292 }else{
293 p = malloc(totalSize);
294 if( p==0 ){
295 sqlite3MemsysAlarm(nByte);
296 p = malloc(totalSize);
297 }
298 }
299 if( p ){
300 z = p;
301 pBt = (void**)&z[mem.nTitle];
302 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
303 pHdr->pNext = 0;
304 pHdr->pPrev = mem.pLast;
305 if( mem.pLast ){
306 mem.pLast->pNext = pHdr;
307 }else{
308 mem.pFirst = pHdr;
309 }
310 mem.pLast = pHdr;
311 pHdr->iForeGuard = FOREGUARD;
312 pHdr->nBacktraceSlots = mem.nBacktrace;
313 pHdr->nTitle = mem.nTitle;
314 if( mem.nBacktrace ){
315 void *aAddr[40];
316 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
317 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
318 }else{
319 pHdr->nBacktrace = 0;
320 }
321 if( mem.nTitle ){
322 memcpy(z, mem.zTitle, mem.nTitle);
323 }
324 pHdr->iSize = nByte;
325 pInt = (int*)&pHdr[1];
326 pInt[nByte/sizeof(int)] = REARGUARD;
327 memset(pInt, 0x65, nByte);
328 mem.nowUsed += nByte;
329 if( mem.nowUsed>mem.mxUsed ){
330 mem.mxUsed = mem.nowUsed;
331 }
332 p = (void*)pInt;
333 }
334 sqlite3_mutex_leave(mem.mutex);
335 }
336 mem.iNextIsBenign = 0;
337 return p;
338}
339
340/*
341** Free memory.
342*/
343void sqlite3_free(void *pPrior){
344 struct MemBlockHdr *pHdr;
345 void **pBt;
346 char *z;
347 if( pPrior==0 ){
348 return;
349 }
350 assert( mem.mutex!=0 );
351 pHdr = sqlite3MemsysGetHeader(pPrior);
352 pBt = (void**)pHdr;
353 pBt -= pHdr->nBacktraceSlots;
354 sqlite3_mutex_enter(mem.mutex);
355 mem.nowUsed -= pHdr->iSize;
356 if( pHdr->pPrev ){
357 assert( pHdr->pPrev->pNext==pHdr );
358 pHdr->pPrev->pNext = pHdr->pNext;
359 }else{
360 assert( mem.pFirst==pHdr );
361 mem.pFirst = pHdr->pNext;
362 }
363 if( pHdr->pNext ){
364 assert( pHdr->pNext->pPrev==pHdr );
365 pHdr->pNext->pPrev = pHdr->pPrev;
366 }else{
367 assert( mem.pLast==pHdr );
368 mem.pLast = pHdr->pPrev;
369 }
370 z = (char*)pBt;
371 z -= pHdr->nTitle;
372 memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
373 pHdr->iSize + sizeof(int) + pHdr->nTitle);
374 free(z);
375 sqlite3_mutex_leave(mem.mutex);
376}
377
378/*
379** Change the size of an existing memory allocation.
380**
381** For this debugging implementation, we *always* make a copy of the
382** allocation into a new place in memory. In this way, if the
383** higher level code is using pointer to the old allocation, it is
384** much more likely to break and we are much more liking to find
385** the error.
386*/
387void *sqlite3_realloc(void *pPrior, int nByte){
388 struct MemBlockHdr *pOldHdr;
389 void *pNew;
390 if( pPrior==0 ){
391 return sqlite3_malloc(nByte);
392 }
393 if( nByte<=0 ){
394 sqlite3_free(pPrior);
395 return 0;
396 }
397 assert( mem.disallow==0 );
398 pOldHdr = sqlite3MemsysGetHeader(pPrior);
399 pNew = sqlite3_malloc(nByte);
400 if( pNew ){
401 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
402 if( nByte>pOldHdr->iSize ){
403 memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
404 }
405 sqlite3_free(pPrior);
406 }
407 return pNew;
408}
409
410/*
411** Set the number of backtrace levels kept for each allocation.
412** A value of zero turns of backtracing. The number is always rounded
413** up to a multiple of 2.
414*/
415void sqlite3_memdebug_backtrace(int depth){
416 if( depth<0 ){ depth = 0; }
417 if( depth>20 ){ depth = 20; }
418 depth = (depth+1)&0xfe;
419 mem.nBacktrace = depth;
420}
421
422/*
423** Set the title string for subsequent allocations.
424*/
425void sqlite3_memdebug_settitle(const char *zTitle){
426 int n = strlen(zTitle) + 1;
427 enterMem();
428 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
429 memcpy(mem.zTitle, zTitle, n);
430 mem.zTitle[n] = 0;
431 mem.nTitle = (n+3)&~3;
432 sqlite3_mutex_leave(mem.mutex);
433}
434
435/*
436** Open the file indicated and write a log of all unfreed memory
437** allocations into that log.
438*/
439void sqlite3_memdebug_dump(const char *zFilename){
440 FILE *out;
441 struct MemBlockHdr *pHdr;
442 void **pBt;
443 out = fopen(zFilename, "w");
444 if( out==0 ){
445 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
446 zFilename);
447 return;
448 }
449 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
450 char *z = (char*)pHdr;
451 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
452 fprintf(out, "**** %d bytes at %p from %s ****\n",
453 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
454 if( pHdr->nBacktrace ){
455 fflush(out);
456 pBt = (void**)pHdr;
457 pBt -= pHdr->nBacktraceSlots;
458 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
459 fprintf(out, "\n");
460 }
461 }
462 fclose(out);
463}
464
465/*
466** This routine is used to simulate malloc failures.
467**
468** After calling this routine, there will be iFail successful
469** memory allocations and then a failure. If iRepeat is 1
470** all subsequent memory allocations will fail. If iRepeat is
471** 0, only a single allocation will fail. If iRepeat is negative
472** then the previous setting for iRepeat is unchanged.
473**
474** Each call to this routine overrides the previous. To disable
475** the simulated allocation failure mechanism, set iFail to -1.
476**
477** This routine returns the number of simulated failures that have
478** occurred since the previous call.
479*/
480int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){
481 int n = mem.iFailCnt;
482 if( piBenign ){
483 *piBenign = mem.iBenignFailCnt;
484 }
485 mem.iFail = iFail+1;
486 if( iRepeat>=0 ){
487 mem.iReset = iRepeat;
488 }
489 mem.iFailCnt = 0;
490 mem.iBenignFailCnt = 0;
491 return n;
492}
493
494int sqlite3_memdebug_pending(){
495 return (mem.iFail-1);
496}
497
498/*
499** The following three functions are used to indicate to the test
500** infrastructure which malloc() calls may fail benignly without
501** affecting functionality. This can happen when resizing hash tables
502** (failing to resize a hash-table is a performance hit, but not an
503** error) or sometimes during a rollback operation.
504**
505** If the argument is true, sqlite3MallocBenignFailure() indicates that the
506** next call to allocate memory may fail benignly.
507**
508** If sqlite3MallocEnterBenignBlock() is called with a non-zero argument,
509** then all memory allocations requested before the next call to
510** sqlite3MallocLeaveBenignBlock() may fail benignly.
511*/
512void sqlite3MallocBenignFailure(int isBenign){
513 if( isBenign ){
514 mem.iNextIsBenign = 1;
515 }
516}
517void sqlite3MallocEnterBenignBlock(int isBenign){
518 if( isBenign ){
519 mem.iIsBenign = 1;
520 }
521}
522void sqlite3MallocLeaveBenignBlock(){
523 mem.iIsBenign = 0;
524}
525
526/*
527** The following two routines are used to assert that no memory
528** allocations occur between one call and the next. The use of
529** these routines does not change the computed results in any way.
530** These routines are like asserts.
531*/
532void sqlite3MallocDisallow(void){
533 assert( mem.mutex!=0 );
534 sqlite3_mutex_enter(mem.mutex);
535 mem.disallow++;
536 sqlite3_mutex_leave(mem.mutex);
537}
538void sqlite3MallocAllow(void){
539 assert( mem.mutex );
540 sqlite3_mutex_enter(mem.mutex);
541 assert( mem.disallow>0 );
542 mem.disallow--;
543 sqlite3_mutex_leave(mem.mutex);
544}
545
546#endif /* SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */
diff --git a/libraries/sqlite/win32/mutex.c b/libraries/sqlite/win32/mutex.c
new file mode 100755
index 0000000..5815ff2
--- /dev/null
+++ b/libraries/sqlite/win32/mutex.c
@@ -0,0 +1,126 @@
1/*
2** 2007 August 14
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.
10**
11*************************************************************************
12** This file contains the C functions that implement mutexes.
13**
14** The implementation in this file does not provide any mutual
15** exclusion and is thus suitable for use only in applications
16** that use SQLite in a single thread. But this implementation
17** does do a lot of error checking on mutexes to make sure they
18** are called correctly and at appropriate times. Hence, this
19** implementation is suitable for testing.
20** debugging purposes
21**
22** $Id: mutex.c,v 1.16 2007/09/10 16:13:00 danielk1977 Exp $
23*/
24#include "sqliteInt.h"
25
26#ifdef SQLITE_MUTEX_NOOP_DEBUG
27/*
28** In this implementation, mutexes do not provide any mutual exclusion.
29** But the error checking is provided. This implementation is useful
30** for test purposes.
31*/
32
33/*
34** The mutex object
35*/
36struct sqlite3_mutex {
37 int id; /* The mutex type */
38 int cnt; /* Number of entries without a matching leave */
39};
40
41/*
42** The sqlite3_mutex_alloc() routine allocates a new
43** mutex and returns a pointer to it. If it returns NULL
44** that means that a mutex could not be allocated.
45*/
46sqlite3_mutex *sqlite3_mutex_alloc(int id){
47 static sqlite3_mutex aStatic[5];
48 sqlite3_mutex *pNew = 0;
49 switch( id ){
50 case SQLITE_MUTEX_FAST:
51 case SQLITE_MUTEX_RECURSIVE: {
52 pNew = sqlite3_malloc(sizeof(*pNew));
53 if( pNew ){
54 pNew->id = id;
55 pNew->cnt = 0;
56 }
57 break;
58 }
59 default: {
60 assert( id-2 >= 0 );
61 assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
62 pNew = &aStatic[id-2];
63 pNew->id = id;
64 break;
65 }
66 }
67 return pNew;
68}
69
70/*
71** This routine deallocates a previously allocated mutex.
72*/
73void sqlite3_mutex_free(sqlite3_mutex *p){
74 assert( p );
75 assert( p->cnt==0 );
76 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
77 sqlite3_free(p);
78}
79
80/*
81** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
82** to enter a mutex. If another thread is already within the mutex,
83** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
84** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
85** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
86** be entered multiple times by the same thread. In such cases the,
87** mutex must be exited an equal number of times before another thread
88** can enter. If the same thread tries to enter any other kind of mutex
89** more than once, the behavior is undefined.
90*/
91void sqlite3_mutex_enter(sqlite3_mutex *p){
92 assert( p );
93 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
94 p->cnt++;
95}
96int sqlite3_mutex_try(sqlite3_mutex *p){
97 assert( p );
98 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
99 p->cnt++;
100 return SQLITE_OK;
101}
102
103/*
104** The sqlite3_mutex_leave() routine exits a mutex that was
105** previously entered by the same thread. The behavior
106** is undefined if the mutex is not currently entered or
107** is not currently allocated. SQLite will never do either.
108*/
109void sqlite3_mutex_leave(sqlite3_mutex *p){
110 assert( p );
111 assert( sqlite3_mutex_held(p) );
112 p->cnt--;
113 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
114}
115
116/*
117** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
118** intended for use inside assert() statements.
119*/
120int sqlite3_mutex_held(sqlite3_mutex *p){
121 return p==0 || p->cnt>0;
122}
123int sqlite3_mutex_notheld(sqlite3_mutex *p){
124 return p==0 || p->cnt==0;
125}
126#endif /* SQLITE_MUTEX_NOOP_DEBUG */
diff --git a/libraries/sqlite/win32/mutex.h b/libraries/sqlite/win32/mutex.h
new file mode 100755
index 0000000..e7ec9d2
--- /dev/null
+++ b/libraries/sqlite/win32/mutex.h
@@ -0,0 +1,82 @@
1/*
2** 2007 August 28
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.
10**
11*************************************************************************
12**
13** This file contains the common header for all mutex implementations.
14** The sqliteInt.h header #includes this file so that it is available
15** to all source files. We break it out in an effort to keep the code
16** better organized.
17**
18** NOTE: source files should *not* #include this header file directly.
19** Source files should #include the sqliteInt.h file and let that file
20** include this one indirectly.
21**
22** $Id: mutex.h,v 1.2 2007/08/30 14:10:30 drh Exp $
23*/
24
25
26#ifdef SQLITE_MUTEX_APPDEF
27/*
28** If SQLITE_MUTEX_APPDEF is defined, then this whole module is
29** omitted and equivalent functionality must be provided by the
30** application that links against the SQLite library.
31*/
32#else
33/*
34** Figure out what version of the code to use. The choices are
35**
36** SQLITE_MUTEX_NOOP For single-threaded applications that
37** do not desire error checking.
38**
39** SQLITE_MUTEX_NOOP_DEBUG For single-threaded applications with
40** error checking to help verify that mutexes
41** are being used correctly even though they
42** are not needed. Used when SQLITE_DEBUG is
43** defined on single-threaded builds.
44**
45** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
46**
47** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
48**
49** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
50*/
51#define SQLITE_MUTEX_NOOP 1 /* The default */
52#if defined(SQLITE_DEBUG) && !SQLITE_THREADSAFE
53# undef SQLITE_MUTEX_NOOP
54# define SQLITE_MUTEX_NOOP_DEBUG
55#endif
56#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_UNIX
57# undef SQLITE_MUTEX_NOOP
58# define SQLITE_MUTEX_PTHREADS
59#endif
60#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_WIN
61# undef SQLITE_MUTEX_NOOP
62# define SQLITE_MUTEX_W32
63#endif
64#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_OS2
65# undef SQLITE_MUTEX_NOOP
66# define SQLITE_MUTEX_OS2
67#endif
68
69#ifdef SQLITE_MUTEX_NOOP
70/*
71** If this is a no-op implementation, implement everything as macros.
72*/
73#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
74#define sqlite3_mutex_free(X)
75#define sqlite3_mutex_enter(X)
76#define sqlite3_mutex_try(X) SQLITE_OK
77#define sqlite3_mutex_leave(X)
78#define sqlite3_mutex_held(X) 1
79#define sqlite3_mutex_notheld(X) 1
80#endif
81
82#endif /* SQLITE_MUTEX_APPDEF */
diff --git a/libraries/sqlite/win32/mutex_os2.c b/libraries/sqlite/win32/mutex_os2.c
new file mode 100755
index 0000000..e0258c7
--- /dev/null
+++ b/libraries/sqlite/win32/mutex_os2.c
@@ -0,0 +1,236 @@
1/*
2** 2007 August 28
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.
10**
11*************************************************************************
12** This file contains the C functions that implement mutexes for OS/2
13**
14** $Id: mutex_os2.c,v 1.3 2007/10/02 19:56:04 pweilbacher Exp $
15*/
16#include "sqliteInt.h"
17
18/*
19** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
20** See the mutex.h file for details.
21*/
22#ifdef SQLITE_MUTEX_OS2
23
24/********************** OS/2 Mutex Implementation **********************
25**
26** This implementation of mutexes is built using the OS/2 API.
27*/
28
29/*
30** The mutex object
31** Each recursive mutex is an instance of the following structure.
32*/
33struct sqlite3_mutex {
34 PSZ mutexName; /* Mutex name controlling the lock */
35 HMTX mutex; /* Mutex controlling the lock */
36 int id; /* Mutex type */
37 int nRef; /* Number of references */
38 TID owner; /* Thread holding this mutex */
39};
40
41/*
42** The sqlite3_mutex_alloc() routine allocates a new
43** mutex and returns a pointer to it. If it returns NULL
44** that means that a mutex could not be allocated.
45** SQLite will unwind its stack and return an error. The argument
46** to sqlite3_mutex_alloc() is one of these integer constants:
47**
48** <ul>
49** <li> SQLITE_MUTEX_FAST 0
50** <li> SQLITE_MUTEX_RECURSIVE 1
51** <li> SQLITE_MUTEX_STATIC_MASTER 2
52** <li> SQLITE_MUTEX_STATIC_MEM 3
53** <li> SQLITE_MUTEX_STATIC_PRNG 4
54** </ul>
55**
56** The first two constants cause sqlite3_mutex_alloc() to create
57** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
58** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
59** The mutex implementation does not need to make a distinction
60** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
61** not want to. But SQLite will only request a recursive mutex in
62** cases where it really needs one. If a faster non-recursive mutex
63** implementation is available on the host platform, the mutex subsystem
64** might return such a mutex in response to SQLITE_MUTEX_FAST.
65**
66** The other allowed parameters to sqlite3_mutex_alloc() each return
67** a pointer to a static preexisting mutex. Three static mutexes are
68** used by the current version of SQLite. Future versions of SQLite
69** may add additional static mutexes. Static mutexes are for internal
70** use by SQLite only. Applications that use SQLite mutexes should
71** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
72** SQLITE_MUTEX_RECURSIVE.
73**
74** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
75** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
76** returns a different mutex on every call. But for the static
77** mutex types, the same mutex is returned on every call that has
78** the same type number.
79*/
80sqlite3_mutex *sqlite3_mutex_alloc(int iType){
81 PSZ mutex_name = "\\SEM32\\SQLITE\\MUTEX";
82 int mutex_name_len = strlen(mutex_name) + 1; /* name length + null byte */
83 sqlite3_mutex *p;
84
85 switch( iType ){
86 case SQLITE_MUTEX_FAST:
87 case SQLITE_MUTEX_RECURSIVE: {
88 p = sqlite3MallocZero( sizeof(*p) );
89 if( p ){
90 p->mutexName = (PSZ)malloc(mutex_name_len);
91 sqlite3_snprintf(mutex_name_len, p->mutexName, "%s", mutex_name);
92 p->id = iType;
93 DosCreateMutexSem(p->mutexName, &p->mutex, 0, FALSE);
94 DosOpenMutexSem(p->mutexName, &p->mutex);
95 }
96 break;
97 }
98 default: {
99 static sqlite3_mutex staticMutexes[5];
100 static int isInit = 0;
101 while( !isInit ) {
102 static long lock = 0;
103 DosEnterCritSec();
104 lock++;
105 if( lock == 1 ) {
106 DosExitCritSec();
107 int i;
108 for(i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++) {
109 staticMutexes[i].mutexName = (PSZ)malloc(mutex_name_len + 1);
110 sqlite3_snprintf(mutex_name_len + 1, /* one more for the number */
111 staticMutexes[i].mutexName, "%s%1d", mutex_name, i);
112 DosCreateMutexSem(staticMutexes[i].mutexName,
113 &staticMutexes[i].mutex, 0, FALSE);
114 DosOpenMutexSem(staticMutexes[i].mutexName,
115 &staticMutexes[i].mutex);
116 }
117 isInit = 1;
118 } else {
119 DosExitCritSec();
120 DosSleep(1);
121 }
122 }
123 assert( iType-2 >= 0 );
124 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
125 p = &staticMutexes[iType-2];
126 p->id = iType;
127 break;
128 }
129 }
130 return p;
131}
132
133
134/*
135** This routine deallocates a previously allocated mutex.
136** SQLite is careful to deallocate every mutex that it allocates.
137*/
138void sqlite3_mutex_free(sqlite3_mutex *p){
139 assert( p );
140 assert( p->nRef==0 );
141 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
142 DosCloseMutexSem(p->mutex);
143 free(p->mutexName);
144 sqlite3_free(p);
145}
146
147/*
148** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
149** to enter a mutex. If another thread is already within the mutex,
150** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
151** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
152** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
153** be entered multiple times by the same thread. In such cases the,
154** mutex must be exited an equal number of times before another thread
155** can enter. If the same thread tries to enter any other kind of mutex
156** more than once, the behavior is undefined.
157*/
158void sqlite3_mutex_enter(sqlite3_mutex *p){
159 TID tid;
160 PID holder1;
161 ULONG holder2;
162 assert( p );
163 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
164 DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
165 DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
166 p->owner = tid;
167 p->nRef++;
168}
169int sqlite3_mutex_try(sqlite3_mutex *p){
170 int rc;
171 TID tid;
172 PID holder1;
173 ULONG holder2;
174 assert( p );
175 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
176 if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR) {
177 DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
178 p->owner = tid;
179 p->nRef++;
180 rc = SQLITE_OK;
181 } else {
182 rc = SQLITE_BUSY;
183 }
184
185 return rc;
186}
187
188/*
189** The sqlite3_mutex_leave() routine exits a mutex that was
190** previously entered by the same thread. The behavior
191** is undefined if the mutex is not currently entered or
192** is not currently allocated. SQLite will never do either.
193*/
194void sqlite3_mutex_leave(sqlite3_mutex *p){
195 TID tid;
196 PID holder1;
197 ULONG holder2;
198 assert( p->nRef>0 );
199 DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
200 assert( p->owner==tid );
201 p->nRef--;
202 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
203 DosReleaseMutexSem(p->mutex);
204}
205
206/*
207** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
208** intended for use inside assert() statements.
209*/
210int sqlite3_mutex_held(sqlite3_mutex *p){
211 TID tid;
212 PID pid;
213 ULONG ulCount;
214 PTIB ptib;
215 if( p!=0 ) {
216 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
217 } else {
218 DosGetInfoBlocks(&ptib, NULL);
219 tid = ptib->tib_ptib2->tib2_ultid;
220 }
221 return p==0 || (p->nRef!=0 && p->owner==tid);
222}
223int sqlite3_mutex_notheld(sqlite3_mutex *p){
224 TID tid;
225 PID pid;
226 ULONG ulCount;
227 PTIB ptib;
228 if( p!= 0 ) {
229 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
230 } else {
231 DosGetInfoBlocks(&ptib, NULL);
232 tid = ptib->tib_ptib2->tib2_ultid;
233 }
234 return p==0 || p->nRef==0 || p->owner!=tid;
235}
236#endif /* SQLITE_MUTEX_OS2 */
diff --git a/libraries/sqlite/win32/mutex_unix.c b/libraries/sqlite/win32/mutex_unix.c
new file mode 100755
index 0000000..ff088fb
--- /dev/null
+++ b/libraries/sqlite/win32/mutex_unix.c
@@ -0,0 +1,223 @@
1/*
2** 2007 August 28
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.
10**
11*************************************************************************
12** This file contains the C functions that implement mutexes for pthreads
13**
14** $Id: mutex_unix.c,v 1.2 2007/08/28 22:24:35 drh Exp $
15*/
16#include "sqliteInt.h"
17
18/*
19** The code in this file is only used if we are compiling threadsafe
20** under unix with pthreads.
21**
22** Note that this implementation requires a version of pthreads that
23** supports recursive mutexes.
24*/
25#ifdef SQLITE_MUTEX_PTHREADS
26
27#include <pthread.h>
28
29/*
30** Each recursive mutex is an instance of the following structure.
31*/
32struct sqlite3_mutex {
33 pthread_mutex_t mutex; /* Mutex controlling the lock */
34 int id; /* Mutex type */
35 int nRef; /* Number of entrances */
36 pthread_t owner; /* Thread that is within this mutex */
37#ifdef SQLITE_DEBUG
38 int trace; /* True to trace changes */
39#endif
40};
41
42/*
43** The sqlite3_mutex_alloc() routine allocates a new
44** mutex and returns a pointer to it. If it returns NULL
45** that means that a mutex could not be allocated. SQLite
46** will unwind its stack and return an error. The argument
47** to sqlite3_mutex_alloc() is one of these integer constants:
48**
49** <ul>
50** <li> SQLITE_MUTEX_FAST
51** <li> SQLITE_MUTEX_RECURSIVE
52** <li> SQLITE_MUTEX_STATIC_MASTER
53** <li> SQLITE_MUTEX_STATIC_MEM
54** <li> SQLITE_MUTEX_STATIC_MEM2
55** <li> SQLITE_MUTEX_STATIC_PRNG
56** <li> SQLITE_MUTEX_STATIC_LRU
57** </ul>
58**
59** The first two constants cause sqlite3_mutex_alloc() to create
60** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
61** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
62** The mutex implementation does not need to make a distinction
63** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
64** not want to. But SQLite will only request a recursive mutex in
65** cases where it really needs one. If a faster non-recursive mutex
66** implementation is available on the host platform, the mutex subsystem
67** might return such a mutex in response to SQLITE_MUTEX_FAST.
68**
69** The other allowed parameters to sqlite3_mutex_alloc() each return
70** a pointer to a static preexisting mutex. Three static mutexes are
71** used by the current version of SQLite. Future versions of SQLite
72** may add additional static mutexes. Static mutexes are for internal
73** use by SQLite only. Applications that use SQLite mutexes should
74** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
75** SQLITE_MUTEX_RECURSIVE.
76**
77** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
78** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
79** returns a different mutex on every call. But for the static
80** mutex types, the same mutex is returned on every call that has
81** the same type number.
82*/
83sqlite3_mutex *sqlite3_mutex_alloc(int iType){
84 static sqlite3_mutex staticMutexes[] = {
85 { PTHREAD_MUTEX_INITIALIZER, },
86 { PTHREAD_MUTEX_INITIALIZER, },
87 { PTHREAD_MUTEX_INITIALIZER, },
88 { PTHREAD_MUTEX_INITIALIZER, },
89 { PTHREAD_MUTEX_INITIALIZER, },
90 };
91 sqlite3_mutex *p;
92 switch( iType ){
93 case SQLITE_MUTEX_RECURSIVE: {
94 p = sqlite3MallocZero( sizeof(*p) );
95 if( p ){
96 pthread_mutexattr_t recursiveAttr;
97 pthread_mutexattr_init(&recursiveAttr);
98 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
99 pthread_mutex_init(&p->mutex, &recursiveAttr);
100 pthread_mutexattr_destroy(&recursiveAttr);
101 p->id = iType;
102 }
103 break;
104 }
105 case SQLITE_MUTEX_FAST: {
106 p = sqlite3MallocZero( sizeof(*p) );
107 if( p ){
108 p->id = iType;
109 pthread_mutex_init(&p->mutex, 0);
110 }
111 break;
112 }
113 default: {
114 assert( iType-2 >= 0 );
115 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
116 p = &staticMutexes[iType-2];
117 p->id = iType;
118 break;
119 }
120 }
121 return p;
122}
123
124
125/*
126** This routine deallocates a previously
127** allocated mutex. SQLite is careful to deallocate every
128** mutex that it allocates.
129*/
130void sqlite3_mutex_free(sqlite3_mutex *p){
131 assert( p );
132 assert( p->nRef==0 );
133 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
134 pthread_mutex_destroy(&p->mutex);
135 sqlite3_free(p);
136}
137
138/*
139** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
140** to enter a mutex. If another thread is already within the mutex,
141** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
142** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
143** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
144** be entered multiple times by the same thread. In such cases the,
145** mutex must be exited an equal number of times before another thread
146** can enter. If the same thread tries to enter any other kind of mutex
147** more than once, the behavior is undefined.
148*/
149void sqlite3_mutex_enter(sqlite3_mutex *p){
150 assert( p );
151 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
152 pthread_mutex_lock(&p->mutex);
153 p->owner = pthread_self();
154 p->nRef++;
155#ifdef SQLITE_DEBUG
156 if( p->trace ){
157 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
158 }
159#endif
160}
161int sqlite3_mutex_try(sqlite3_mutex *p){
162 int rc;
163 assert( p );
164 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
165 if( pthread_mutex_trylock(&p->mutex)==0 ){
166 p->owner = pthread_self();
167 p->nRef++;
168 rc = SQLITE_OK;
169#ifdef SQLITE_DEBUG
170 if( p->trace ){
171 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
172 }
173#endif
174 }else{
175 rc = SQLITE_BUSY;
176 }
177 return rc;
178}
179
180/*
181** The sqlite3_mutex_leave() routine exits a mutex that was
182** previously entered by the same thread. The behavior
183** is undefined if the mutex is not currently entered or
184** is not currently allocated. SQLite will never do either.
185*/
186void sqlite3_mutex_leave(sqlite3_mutex *p){
187 assert( p );
188 assert( sqlite3_mutex_held(p) );
189 p->nRef--;
190 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
191#ifdef SQLITE_DEBUG
192 if( p->trace ){
193 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
194 }
195#endif
196 pthread_mutex_unlock(&p->mutex);
197}
198
199/*
200** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
201** intended for use only inside assert() statements. On some platforms,
202** there might be race conditions that can cause these routines to
203** deliver incorrect results. In particular, if pthread_equal() is
204** not an atomic operation, then these routines might delivery
205** incorrect results. On most platforms, pthread_equal() is a
206** comparison of two integers and is therefore atomic. But we are
207** told that HPUX is not such a platform. If so, then these routines
208** will not always work correctly on HPUX.
209**
210** On those platforms where pthread_equal() is not atomic, SQLite
211** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
212** make sure no assert() statements are evaluated and hence these
213** routines are never called.
214*/
215#ifndef NDEBUG
216int sqlite3_mutex_held(sqlite3_mutex *p){
217 return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
218}
219int sqlite3_mutex_notheld(sqlite3_mutex *p){
220 return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
221}
222#endif
223#endif /* SQLITE_MUTEX_PTHREAD */
diff --git a/libraries/sqlite/win32/mutex_w32.c b/libraries/sqlite/win32/mutex_w32.c
new file mode 100755
index 0000000..3153cd0
--- /dev/null
+++ b/libraries/sqlite/win32/mutex_w32.c
@@ -0,0 +1,210 @@
1/*
2** 2007 August 14
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.
10**
11*************************************************************************
12** This file contains the C functions that implement mutexes for win32
13**
14** $Id: mutex_w32.c,v 1.4 2007/09/05 14:30:42 drh Exp $
15*/
16#define _WIN32_WINNT 0x0400
17#include <Windows.h>
18#include "sqliteInt.h"
19
20/*
21** The code in this file is only used if we are compiling multithreaded
22** on a win32 system.
23*/
24#ifdef SQLITE_MUTEX_W32
25
26/*
27** Each recursive mutex is an instance of the following structure.
28*/
29struct sqlite3_mutex {
30 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
31 int id; /* Mutex type */
32 int nRef; /* Number of enterances */
33 DWORD owner; /* Thread holding this mutex */
34};
35
36/*
37** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
38** or WinCE. Return false (zero) for Win95, Win98, or WinME.
39**
40** Here is an interesting observation: Win95, Win98, and WinME lack
41** the LockFileEx() API. But we can still statically link against that
42** API as long as we don't call it win running Win95/98/ME. A call to
43** this routine is used to determine if the host is Win95/98/ME or
44** WinNT/2K/XP so that we will know whether or not we can safely call
45** the LockFileEx() API.
46*/
47#if OS_WINCE
48# define mutexIsNT() (1)
49#else
50 static int mutexIsNT(void){
51 static int osType = 0;
52 if( osType==0 ){
53 OSVERSIONINFO sInfo;
54 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
55 GetVersionEx(&sInfo);
56 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
57 }
58 return osType==2;
59 }
60#endif /* OS_WINCE */
61
62
63/*
64** The sqlite3_mutex_alloc() routine allocates a new
65** mutex and returns a pointer to it. If it returns NULL
66** that means that a mutex could not be allocated. SQLite
67** will unwind its stack and return an error. The argument
68** to sqlite3_mutex_alloc() is one of these integer constants:
69**
70** <ul>
71** <li> SQLITE_MUTEX_FAST 0
72** <li> SQLITE_MUTEX_RECURSIVE 1
73** <li> SQLITE_MUTEX_STATIC_MASTER 2
74** <li> SQLITE_MUTEX_STATIC_MEM 3
75** <li> SQLITE_MUTEX_STATIC_PRNG 4
76** </ul>
77**
78** The first two constants cause sqlite3_mutex_alloc() to create
79** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
80** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
81** The mutex implementation does not need to make a distinction
82** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
83** not want to. But SQLite will only request a recursive mutex in
84** cases where it really needs one. If a faster non-recursive mutex
85** implementation is available on the host platform, the mutex subsystem
86** might return such a mutex in response to SQLITE_MUTEX_FAST.
87**
88** The other allowed parameters to sqlite3_mutex_alloc() each return
89** a pointer to a static preexisting mutex. Three static mutexes are
90** used by the current version of SQLite. Future versions of SQLite
91** may add additional static mutexes. Static mutexes are for internal
92** use by SQLite only. Applications that use SQLite mutexes should
93** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
94** SQLITE_MUTEX_RECURSIVE.
95**
96** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
97** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
98** returns a different mutex on every call. But for the static
99** mutex types, the same mutex is returned on every call that has
100** the same type number.
101*/
102sqlite3_mutex *sqlite3_mutex_alloc(int iType){
103 sqlite3_mutex *p;
104
105 switch( iType ){
106 case SQLITE_MUTEX_FAST:
107 case SQLITE_MUTEX_RECURSIVE: {
108 p = sqlite3MallocZero( sizeof(*p) );
109 if( p ){
110 p->id = iType;
111 InitializeCriticalSection(&p->mutex);
112 }
113 break;
114 }
115 default: {
116 static sqlite3_mutex staticMutexes[5];
117 static int isInit = 0;
118 while( !isInit ){
119 static long lock = 0;
120 if( InterlockedIncrement(&lock)==1 ){
121 int i;
122 for(i=0; i<sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++){
123 InitializeCriticalSection(&staticMutexes[i].mutex);
124 }
125 isInit = 1;
126 }else{
127 Sleep(1);
128 }
129 }
130 assert( iType-2 >= 0 );
131 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
132 p = &staticMutexes[iType-2];
133 p->id = iType;
134 break;
135 }
136 }
137 return p;
138}
139
140
141/*
142** This routine deallocates a previously
143** allocated mutex. SQLite is careful to deallocate every
144** mutex that it allocates.
145*/
146void sqlite3_mutex_free(sqlite3_mutex *p){
147 assert( p );
148 assert( p->nRef==0 );
149 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
150 DeleteCriticalSection(&p->mutex);
151 sqlite3_free(p);
152}
153
154/*
155** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
156** to enter a mutex. If another thread is already within the mutex,
157** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
158** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
159** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
160** be entered multiple times by the same thread. In such cases the,
161** mutex must be exited an equal number of times before another thread
162** can enter. If the same thread tries to enter any other kind of mutex
163** more than once, the behavior is undefined.
164*/
165void sqlite3_mutex_enter(sqlite3_mutex *p){
166 assert( p );
167 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
168 EnterCriticalSection(&p->mutex);
169 p->owner = GetCurrentThreadId();
170 p->nRef++;
171}
172int sqlite3_mutex_try(sqlite3_mutex *p){
173 int rc;
174 assert( p );
175 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
176 if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
177 p->owner = GetCurrentThreadId();
178 p->nRef++;
179 rc = SQLITE_OK;
180 }else{
181 rc = SQLITE_BUSY;
182 }
183 return rc;
184}
185
186/*
187** The sqlite3_mutex_leave() routine exits a mutex that was
188** previously entered by the same thread. The behavior
189** is undefined if the mutex is not currently entered or
190** is not currently allocated. SQLite will never do either.
191*/
192void sqlite3_mutex_leave(sqlite3_mutex *p){
193 assert( p->nRef>0 );
194 assert( p->owner==GetCurrentThreadId() );
195 p->nRef--;
196 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
197 LeaveCriticalSection(&p->mutex);
198}
199
200/*
201** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
202** intended for use only inside assert() statements.
203*/
204int sqlite3_mutex_held(sqlite3_mutex *p){
205 return p==0 || (p->nRef!=0 && p->owner==GetCurrentThreadId());
206}
207int sqlite3_mutex_notheld(sqlite3_mutex *p){
208 return p==0 || p->nRef==0 || p->owner!=GetCurrentThreadId();
209}
210#endif /* SQLITE_MUTEX_W32 */
diff --git a/libraries/sqlite/win32/opcodes.c b/libraries/sqlite/win32/opcodes.c
new file mode 100755
index 0000000..5a6bd45
--- /dev/null
+++ b/libraries/sqlite/win32/opcodes.c
@@ -0,0 +1,151 @@
1/* Automatically generated. Do not edit */
2/* See the mkopcodec.awk script for details. */
3#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
4const char *sqlite3OpcodeName(int i){
5 static const char *const azName[] = { "?",
6 /* 1 */ "VCreate",
7 /* 2 */ "MemMax",
8 /* 3 */ "LoadAnalysis",
9 /* 4 */ "RowData",
10 /* 5 */ "CreateIndex",
11 /* 6 */ "Variable",
12 /* 7 */ "MemStore",
13 /* 8 */ "Clear",
14 /* 9 */ "Last",
15 /* 10 */ "MoveGe",
16 /* 11 */ "Sequence",
17 /* 12 */ "Int64",
18 /* 13 */ "VBegin",
19 /* 14 */ "RowKey",
20 /* 15 */ "MemInt",
21 /* 16 */ "Not",
22 /* 17 */ "ResetCount",
23 /* 18 */ "Delete",
24 /* 19 */ "Rowid",
25 /* 20 */ "OpenRead",
26 /* 21 */ "Sort",
27 /* 22 */ "VerifyCookie",
28 /* 23 */ "VColumn",
29 /* 24 */ "MemMove",
30 /* 25 */ "Next",
31 /* 26 */ "Insert",
32 /* 27 */ "Prev",
33 /* 28 */ "IdxGE",
34 /* 29 */ "VRename",
35 /* 30 */ "DropTable",
36 /* 31 */ "MakeRecord",
37 /* 32 */ "Null",
38 /* 33 */ "IdxInsert",
39 /* 34 */ "ReadCookie",
40 /* 35 */ "VDestroy",
41 /* 36 */ "DropIndex",
42 /* 37 */ "MustBeInt",
43 /* 38 */ "Callback",
44 /* 39 */ "IntegrityCk",
45 /* 40 */ "MoveGt",
46 /* 41 */ "MoveLe",
47 /* 42 */ "CollSeq",
48 /* 43 */ "OpenEphemeral",
49 /* 44 */ "VNext",
50 /* 45 */ "Found",
51 /* 46 */ "If",
52 /* 47 */ "Dup",
53 /* 48 */ "Goto",
54 /* 49 */ "Function",
55 /* 50 */ "Pop",
56 /* 51 */ "Blob",
57 /* 52 */ "MemIncr",
58 /* 53 */ "IfMemPos",
59 /* 54 */ "FifoWrite",
60 /* 55 */ "IdxGT",
61 /* 56 */ "NullRow",
62 /* 57 */ "Transaction",
63 /* 58 */ "VUpdate",
64 /* 59 */ "TableLock",
65 /* 60 */ "Or",
66 /* 61 */ "And",
67 /* 62 */ "IdxRowid",
68 /* 63 */ "SetCookie",
69 /* 64 */ "ContextPush",
70 /* 65 */ "IsNull",
71 /* 66 */ "NotNull",
72 /* 67 */ "Ne",
73 /* 68 */ "Eq",
74 /* 69 */ "Gt",
75 /* 70 */ "Le",
76 /* 71 */ "Lt",
77 /* 72 */ "Ge",
78 /* 73 */ "DropTrigger",
79 /* 74 */ "BitAnd",
80 /* 75 */ "BitOr",
81 /* 76 */ "ShiftLeft",
82 /* 77 */ "ShiftRight",
83 /* 78 */ "Add",
84 /* 79 */ "Subtract",
85 /* 80 */ "Multiply",
86 /* 81 */ "Divide",
87 /* 82 */ "Remainder",
88 /* 83 */ "Concat",
89 /* 84 */ "MoveLt",
90 /* 85 */ "Negative",
91 /* 86 */ "AutoCommit",
92 /* 87 */ "BitNot",
93 /* 88 */ "String8",
94 /* 89 */ "Column",
95 /* 90 */ "AbsValue",
96 /* 91 */ "AddImm",
97 /* 92 */ "ContextPop",
98 /* 93 */ "IdxDelete",
99 /* 94 */ "IncrVacuum",
100 /* 95 */ "AggFinal",
101 /* 96 */ "RealAffinity",
102 /* 97 */ "Return",
103 /* 98 */ "Expire",
104 /* 99 */ "Rewind",
105 /* 100 */ "Statement",
106 /* 101 */ "Integer",
107 /* 102 */ "IfMemZero",
108 /* 103 */ "Destroy",
109 /* 104 */ "IdxLT",
110 /* 105 */ "MakeIdxRec",
111 /* 106 */ "Vacuum",
112 /* 107 */ "MemNull",
113 /* 108 */ "IfNot",
114 /* 109 */ "Pull",
115 /* 110 */ "FifoRead",
116 /* 111 */ "ParseSchema",
117 /* 112 */ "NewRowid",
118 /* 113 */ "SetNumColumns",
119 /* 114 */ "Explain",
120 /* 115 */ "String",
121 /* 116 */ "AggStep",
122 /* 117 */ "VRowid",
123 /* 118 */ "VOpen",
124 /* 119 */ "NotExists",
125 /* 120 */ "Close",
126 /* 121 */ "Halt",
127 /* 122 */ "Noop",
128 /* 123 */ "VFilter",
129 /* 124 */ "OpenPseudo",
130 /* 125 */ "Real",
131 /* 126 */ "HexBlob",
132 /* 127 */ "IfMemNeg",
133 /* 128 */ "IsUnique",
134 /* 129 */ "ForceInt",
135 /* 130 */ "OpenWrite",
136 /* 131 */ "Gosub",
137 /* 132 */ "Distinct",
138 /* 133 */ "MemLoad",
139 /* 134 */ "NotFound",
140 /* 135 */ "CreateTable",
141 /* 136 */ "Push",
142 /* 137 */ "NotUsed_137",
143 /* 138 */ "ToText",
144 /* 139 */ "ToBlob",
145 /* 140 */ "ToNumeric",
146 /* 141 */ "ToInt",
147 /* 142 */ "ToReal",
148 };
149 return azName[i];
150}
151#endif
diff --git a/libraries/sqlite/win32/opcodes.h b/libraries/sqlite/win32/opcodes.h
new file mode 100755
index 0000000..cf8111c
--- /dev/null
+++ b/libraries/sqlite/win32/opcodes.h
@@ -0,0 +1,160 @@
1/* Automatically generated. Do not edit */
2/* See the mkopcodeh.awk script for details */
3#define OP_VCreate 1
4#define OP_MemMax 2
5#define OP_LoadAnalysis 3
6#define OP_RowData 4
7#define OP_CreateIndex 5
8#define OP_Variable 6
9#define OP_MemStore 7
10#define OP_Clear 8
11#define OP_Last 9
12#define OP_Add 78 /* same as TK_PLUS */
13#define OP_MoveGe 10
14#define OP_Sequence 11
15#define OP_Int64 12
16#define OP_VBegin 13
17#define OP_RowKey 14
18#define OP_Divide 81 /* same as TK_SLASH */
19#define OP_MemInt 15
20#define OP_ResetCount 17
21#define OP_Delete 18
22#define OP_Rowid 19
23#define OP_OpenRead 20
24#define OP_Sort 21
25#define OP_VerifyCookie 22
26#define OP_VColumn 23
27#define OP_MemMove 24
28#define OP_Next 25
29#define OP_Insert 26
30#define OP_Prev 27
31#define OP_IdxGE 28
32#define OP_Not 16 /* same as TK_NOT */
33#define OP_Ge 72 /* same as TK_GE */
34#define OP_VRename 29
35#define OP_DropTable 30
36#define OP_MakeRecord 31
37#define OP_Null 32
38#define OP_IdxInsert 33
39#define OP_ReadCookie 34
40#define OP_VDestroy 35
41#define OP_DropIndex 36
42#define OP_IsNull 65 /* same as TK_ISNULL */
43#define OP_MustBeInt 37
44#define OP_Callback 38
45#define OP_IntegrityCk 39
46#define OP_MoveGt 40
47#define OP_MoveLe 41
48#define OP_CollSeq 42
49#define OP_OpenEphemeral 43
50#define OP_HexBlob 126 /* same as TK_BLOB */
51#define OP_VNext 44
52#define OP_Eq 68 /* same as TK_EQ */
53#define OP_String8 88 /* same as TK_STRING */
54#define OP_Found 45
55#define OP_If 46
56#define OP_ToBlob 139 /* same as TK_TO_BLOB */
57#define OP_Multiply 80 /* same as TK_STAR */
58#define OP_Dup 47
59#define OP_ShiftRight 77 /* same as TK_RSHIFT */
60#define OP_Goto 48
61#define OP_Function 49
62#define OP_Pop 50
63#define OP_Blob 51
64#define OP_MemIncr 52
65#define OP_BitNot 87 /* same as TK_BITNOT */
66#define OP_IfMemPos 53
67#define OP_FifoWrite 54
68#define OP_IdxGT 55
69#define OP_Gt 69 /* same as TK_GT */
70#define OP_Le 70 /* same as TK_LE */
71#define OP_NullRow 56
72#define OP_Transaction 57
73#define OP_VUpdate 58
74#define OP_TableLock 59
75#define OP_IdxRowid 62
76#define OP_SetCookie 63
77#define OP_Negative 85 /* same as TK_UMINUS */
78#define OP_And 61 /* same as TK_AND */
79#define OP_ToNumeric 140 /* same as TK_TO_NUMERIC*/
80#define OP_ToText 138 /* same as TK_TO_TEXT */
81#define OP_ContextPush 64
82#define OP_DropTrigger 73
83#define OP_MoveLt 84
84#define OP_AutoCommit 86
85#define OP_Column 89
86#define OP_AbsValue 90
87#define OP_AddImm 91
88#define OP_Remainder 82 /* same as TK_REM */
89#define OP_ContextPop 92
90#define OP_IdxDelete 93
91#define OP_Ne 67 /* same as TK_NE */
92#define OP_ToInt 141 /* same as TK_TO_INT */
93#define OP_IncrVacuum 94
94#define OP_AggFinal 95
95#define OP_RealAffinity 96
96#define OP_Concat 83 /* same as TK_CONCAT */
97#define OP_Return 97
98#define OP_Expire 98
99#define OP_Rewind 99
100#define OP_Statement 100
101#define OP_BitOr 75 /* same as TK_BITOR */
102#define OP_Integer 101
103#define OP_IfMemZero 102
104#define OP_Destroy 103
105#define OP_IdxLT 104
106#define OP_MakeIdxRec 105
107#define OP_Lt 71 /* same as TK_LT */
108#define OP_Subtract 79 /* same as TK_MINUS */
109#define OP_Vacuum 106
110#define OP_MemNull 107
111#define OP_IfNot 108
112#define OP_Pull 109
113#define OP_FifoRead 110
114#define OP_ParseSchema 111
115#define OP_NewRowid 112
116#define OP_SetNumColumns 113
117#define OP_Explain 114
118#define OP_BitAnd 74 /* same as TK_BITAND */
119#define OP_String 115
120#define OP_AggStep 116
121#define OP_VRowid 117
122#define OP_VOpen 118
123#define OP_NotExists 119
124#define OP_Close 120
125#define OP_Halt 121
126#define OP_Noop 122
127#define OP_VFilter 123
128#define OP_OpenPseudo 124
129#define OP_Or 60 /* same as TK_OR */
130#define OP_ShiftLeft 76 /* same as TK_LSHIFT */
131#define OP_IfMemNeg 127
132#define OP_ToReal 142 /* same as TK_TO_REAL */
133#define OP_IsUnique 128
134#define OP_ForceInt 129
135#define OP_OpenWrite 130
136#define OP_Gosub 131
137#define OP_Real 125 /* same as TK_FLOAT */
138#define OP_Distinct 132
139#define OP_NotNull 66 /* same as TK_NOTNULL */
140#define OP_MemLoad 133
141#define OP_NotFound 134
142#define OP_CreateTable 135
143#define OP_Push 136
144
145/* The following opcode values are never used */
146#define OP_NotUsed_137 137
147
148/* Opcodes that are guaranteed to never push a value onto the stack
149** contain a 1 their corresponding position of the following mask
150** set. See the opcodeNoPush() function in vdbeaux.c */
151#define NOPUSH_MASK_0 0x278e
152#define NOPUSH_MASK_1 0x7e77
153#define NOPUSH_MASK_2 0x7f7a
154#define NOPUSH_MASK_3 0xbff5
155#define NOPUSH_MASK_4 0xffff
156#define NOPUSH_MASK_5 0xf8f7
157#define NOPUSH_MASK_6 0xb55f
158#define NOPUSH_MASK_7 0x9fd2
159#define NOPUSH_MASK_8 0x7d5f
160#define NOPUSH_MASK_9 0x0000
diff --git a/libraries/sqlite/win32/opensim_build_notes.txt b/libraries/sqlite/win32/opensim_build_notes.txt
new file mode 100644
index 0000000..3347f68
--- /dev/null
+++ b/libraries/sqlite/win32/opensim_build_notes.txt
@@ -0,0 +1,18 @@
1downloaded tarball from:
2http://www.sqlite.org/sqlite-3.5.1.tar.gz
3
4source tree derived (in unix!) by running:
5make target_source
6in unix/build
7
8two files removed: tclsqlite.c, fts1.c
9
10in mutex_w32.c, added:
11#define _WIN32_WINNT 0x0400
12#include <Windows.h>
13
14created sqlite_vs2005\sqlite_vs2005.sln
15
16build all
17
18copy sqlite_vs2005\release\sqlite_vs2005.dll to bin\sqlite3.dll
diff --git a/libraries/sqlite/win32/os.c b/libraries/sqlite/win32/os.c
new file mode 100755
index 0000000..3b6ca7b
--- /dev/null
+++ b/libraries/sqlite/win32/os.c
@@ -0,0 +1,282 @@
1 /*
2** 2005 November 29
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.
10**
11******************************************************************************
12**
13** This file contains OS interface code that is common to all
14** architectures.
15*/
16#define _SQLITE_OS_C_ 1
17#include "sqliteInt.h"
18#undef _SQLITE_OS_C_
19
20/*
21** The default SQLite sqlite3_vfs implementations do not allocate
22** memory (actually, os_unix.c allocates a small amount of memory
23** from within OsOpen()), but some third-party implementations may.
24** So we test the effects of a malloc() failing and the sqlite3OsXXX()
25** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
26**
27** The following functions are instrumented for malloc() failure
28** testing:
29**
30** sqlite3OsOpen()
31** sqlite3OsRead()
32** sqlite3OsWrite()
33** sqlite3OsSync()
34** sqlite3OsLock()
35**
36*/
37#ifdef SQLITE_TEST
38 #define DO_OS_MALLOC_TEST if (1) { \
39 void *pTstAlloc = sqlite3_malloc(10); \
40 if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \
41 sqlite3_free(pTstAlloc); \
42 }
43#else
44 #define DO_OS_MALLOC_TEST
45#endif
46
47/*
48** The following routines are convenience wrappers around methods
49** of the sqlite3_file object. This is mostly just syntactic sugar. All
50** of this would be completely automatic if SQLite were coded using
51** C++ instead of plain old C.
52*/
53int sqlite3OsClose(sqlite3_file *pId){
54 int rc = SQLITE_OK;
55 if( pId->pMethods ){
56 rc = pId->pMethods->xClose(pId);
57 pId->pMethods = 0;
58 }
59 return rc;
60}
61int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
62 DO_OS_MALLOC_TEST;
63 return id->pMethods->xRead(id, pBuf, amt, offset);
64}
65int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
66 DO_OS_MALLOC_TEST;
67 return id->pMethods->xWrite(id, pBuf, amt, offset);
68}
69int sqlite3OsTruncate(sqlite3_file *id, i64 size){
70 return id->pMethods->xTruncate(id, size);
71}
72int sqlite3OsSync(sqlite3_file *id, int flags){
73 DO_OS_MALLOC_TEST;
74 return id->pMethods->xSync(id, flags);
75}
76int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
77 return id->pMethods->xFileSize(id, pSize);
78}
79int sqlite3OsLock(sqlite3_file *id, int lockType){
80 DO_OS_MALLOC_TEST;
81 return id->pMethods->xLock(id, lockType);
82}
83int sqlite3OsUnlock(sqlite3_file *id, int lockType){
84 return id->pMethods->xUnlock(id, lockType);
85}
86int sqlite3OsCheckReservedLock(sqlite3_file *id){
87 return id->pMethods->xCheckReservedLock(id);
88}
89int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
90 return id->pMethods->xFileControl(id,op,pArg);
91}
92
93#ifdef SQLITE_TEST
94 /* The following two variables are used to override the values returned
95 ** by the xSectorSize() and xDeviceCharacteristics() vfs methods for
96 ** testing purposes. They are usually set by a test command implemented
97 ** in test6.c.
98 */
99 int sqlite3_test_sector_size = 0;
100 int sqlite3_test_device_characteristics = 0;
101 int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
102 int dc = id->pMethods->xDeviceCharacteristics(id);
103 return dc | sqlite3_test_device_characteristics;
104 }
105 int sqlite3OsSectorSize(sqlite3_file *id){
106 if( sqlite3_test_sector_size==0 ){
107 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
108 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
109 }
110 return sqlite3_test_sector_size;
111 }
112#else
113 int sqlite3OsSectorSize(sqlite3_file *id){
114 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
115 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
116 }
117 int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
118 return id->pMethods->xDeviceCharacteristics(id);
119 }
120#endif
121
122/*
123** The next group of routines are convenience wrappers around the
124** VFS methods.
125*/
126int sqlite3OsOpen(
127 sqlite3_vfs *pVfs,
128 const char *zPath,
129 sqlite3_file *pFile,
130 int flags,
131 int *pFlagsOut
132){
133 DO_OS_MALLOC_TEST;
134 return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut);
135}
136int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
137 return pVfs->xDelete(pVfs, zPath, dirSync);
138}
139int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
140 return pVfs->xAccess(pVfs, zPath, flags);
141}
142int sqlite3OsGetTempname(sqlite3_vfs *pVfs, int nBufOut, char *zBufOut){
143 return pVfs->xGetTempname(pVfs, nBufOut, zBufOut);
144}
145int sqlite3OsFullPathname(
146 sqlite3_vfs *pVfs,
147 const char *zPath,
148 int nPathOut,
149 char *zPathOut
150){
151 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
152}
153void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
154 return pVfs->xDlOpen(pVfs, zPath);
155}
156void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
157 pVfs->xDlError(pVfs, nByte, zBufOut);
158}
159void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
160 return pVfs->xDlSym(pVfs, pHandle, zSymbol);
161}
162void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
163 pVfs->xDlClose(pVfs, pHandle);
164}
165int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
166 return pVfs->xRandomness(pVfs, nByte, zBufOut);
167}
168int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
169 return pVfs->xSleep(pVfs, nMicro);
170}
171int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
172 return pVfs->xCurrentTime(pVfs, pTimeOut);
173}
174
175int sqlite3OsOpenMalloc(
176 sqlite3_vfs *pVfs,
177 const char *zFile,
178 sqlite3_file **ppFile,
179 int flags,
180 int *pOutFlags
181){
182 int rc = SQLITE_NOMEM;
183 sqlite3_file *pFile;
184 pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
185 if( pFile ){
186 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
187 if( rc!=SQLITE_OK ){
188 sqlite3_free(pFile);
189 }else{
190 *ppFile = pFile;
191 }
192 }
193 return rc;
194}
195int sqlite3OsCloseFree(sqlite3_file *pFile){
196 int rc = SQLITE_OK;
197 if( pFile ){
198 rc = sqlite3OsClose(pFile);
199 sqlite3_free(pFile);
200 }
201 return rc;
202}
203
204/*
205** The list of all registered VFS implementations. This list is
206** initialized to the single VFS returned by sqlite3OsDefaultVfs()
207** upon the first call to sqlite3_vfs_find().
208*/
209static sqlite3_vfs *vfsList = 0;
210
211/*
212** Locate a VFS by name. If no name is given, simply return the
213** first VFS on the list.
214*/
215sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
216 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
217 sqlite3_vfs *pVfs;
218 static int isInit = 0;
219 sqlite3_mutex_enter(mutex);
220 if( !isInit ){
221 vfsList = sqlite3OsDefaultVfs();
222 isInit = 1;
223 }
224 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
225 if( zVfs==0 ) break;
226 if( strcmp(zVfs, pVfs->zName)==0 ) break;
227 }
228 sqlite3_mutex_leave(mutex);
229 return pVfs;
230}
231
232/*
233** Unlink a VFS from the linked list
234*/
235static void vfsUnlink(sqlite3_vfs *pVfs){
236 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) );
237 if( vfsList==pVfs ){
238 vfsList = pVfs->pNext;
239 }else{
240 sqlite3_vfs *p = vfsList;
241 while( p->pNext && p->pNext!=pVfs ){
242 p = p->pNext;
243 }
244 if( p->pNext==pVfs ){
245 p->pNext = pVfs->pNext;
246 }
247 }
248}
249
250/*
251** Register a VFS with the system. It is harmless to register the same
252** VFS multiple times. The new VFS becomes the default if makeDflt is
253** true.
254*/
255int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
256 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
257 sqlite3_vfs_find(0); /* Make sure we are initialized */
258 sqlite3_mutex_enter(mutex);
259 vfsUnlink(pVfs);
260 if( makeDflt || vfsList==0 ){
261 pVfs->pNext = vfsList;
262 vfsList = pVfs;
263 }else{
264 pVfs->pNext = vfsList->pNext;
265 vfsList->pNext = pVfs;
266 }
267 assert(vfsList);
268 sqlite3_mutex_leave(mutex);
269 return SQLITE_OK;
270}
271
272/*
273** Unregister a VFS so that it is no longer accessible.
274*/
275int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
276 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
277 sqlite3_mutex_enter(mutex);
278 vfsUnlink(pVfs);
279 assert(vfsList);
280 sqlite3_mutex_leave(mutex);
281 return SQLITE_OK;
282}
diff --git a/libraries/sqlite/win32/os.h b/libraries/sqlite/win32/os.h
new file mode 100755
index 0000000..554952d
--- /dev/null
+++ b/libraries/sqlite/win32/os.h
@@ -0,0 +1,284 @@
1/*
2** 2001 September 16
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.
10**
11******************************************************************************
12**
13** This header file (together with is companion C source-code file
14** "os.c") attempt to abstract the underlying operating system so that
15** the SQLite library will work on both POSIX and windows systems.
16**
17** This header file is #include-ed by sqliteInt.h and thus ends up
18** being included by every source file.
19*/
20#ifndef _SQLITE_OS_H_
21#define _SQLITE_OS_H_
22
23/*
24** Figure out if we are dealing with Unix, Windows, or some other
25** operating system. After the following block of preprocess macros,
26** all of OS_UNIX, OS_WIN, OS_OS2, and OS_OTHER will defined to either
27** 1 or 0. One of the four will be 1. The other three will be 0.
28*/
29#if defined(OS_OTHER)
30# if OS_OTHER==1
31# undef OS_UNIX
32# define OS_UNIX 0
33# undef OS_WIN
34# define OS_WIN 0
35# undef OS_OS2
36# define OS_OS2 0
37# else
38# undef OS_OTHER
39# endif
40#endif
41#if !defined(OS_UNIX) && !defined(OS_OTHER)
42# define OS_OTHER 0
43# ifndef OS_WIN
44# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
45# define OS_WIN 1
46# define OS_UNIX 0
47# define OS_OS2 0
48# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
49# define OS_WIN 0
50# define OS_UNIX 0
51# define OS_OS2 1
52# else
53# define OS_WIN 0
54# define OS_UNIX 1
55# define OS_OS2 0
56# endif
57# else
58# define OS_UNIX 0
59# define OS_OS2 0
60# endif
61#else
62# ifndef OS_WIN
63# define OS_WIN 0
64# endif
65#endif
66
67
68
69/*
70** Define the maximum size of a temporary filename
71*/
72#if OS_WIN
73# include <windows.h>
74# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
75#elif OS_OS2
76# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
77# include <os2safe.h> /* has to be included before os2.h for linking to work */
78# endif
79# define INCL_DOSDATETIME
80# define INCL_DOSFILEMGR
81# define INCL_DOSERRORS
82# define INCL_DOSMISC
83# define INCL_DOSPROCESS
84# define INCL_DOSMODULEMGR
85# include <os2.h>
86# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
87#else
88# define SQLITE_TEMPNAME_SIZE 200
89#endif
90
91/* If the SET_FULLSYNC macro is not defined above, then make it
92** a no-op
93*/
94#ifndef SET_FULLSYNC
95# define SET_FULLSYNC(x,y)
96#endif
97
98/*
99** The default size of a disk sector
100*/
101#ifndef SQLITE_DEFAULT_SECTOR_SIZE
102# define SQLITE_DEFAULT_SECTOR_SIZE 512
103#endif
104
105/*
106** Temporary files are named starting with this prefix followed by 16 random
107** alphanumeric characters, and no file extension. They are stored in the
108** OS's standard temporary file directory, and are deleted prior to exit.
109** If sqlite is being embedded in another program, you may wish to change the
110** prefix to reflect your program's name, so that if your program exits
111** prematurely, old temporary files can be easily identified. This can be done
112** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
113**
114** 2006-10-31: The default prefix used to be "sqlite_". But then
115** Mcafee started using SQLite in their anti-virus product and it
116** started putting files with the "sqlite" name in the c:/temp folder.
117** This annoyed many windows users. Those users would then do a
118** Google search for "sqlite", find the telephone numbers of the
119** developers and call to wake them up at night and complain.
120** For this reason, the default name prefix is changed to be "sqlite"
121** spelled backwards. So the temp files are still identified, but
122** anybody smart enough to figure out the code is also likely smart
123** enough to know that calling the developer will not help get rid
124** of the file.
125*/
126#ifndef SQLITE_TEMP_FILE_PREFIX
127# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
128#endif
129
130/*
131** If using an alternative OS interface, then we must have an "os_other.h"
132** header file available for that interface. Presumably the "os_other.h"
133** header file contains #defines similar to those above.
134*/
135#if OS_OTHER
136# include "os_other.h"
137#endif
138
139
140/*
141** The following values may be passed as the second argument to
142** sqlite3OsLock(). The various locks exhibit the following semantics:
143**
144** SHARED: Any number of processes may hold a SHARED lock simultaneously.
145** RESERVED: A single process may hold a RESERVED lock on a file at
146** any time. Other processes may hold and obtain new SHARED locks.
147** PENDING: A single process may hold a PENDING lock on a file at
148** any one time. Existing SHARED locks may persist, but no new
149** SHARED locks may be obtained by other processes.
150** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
151**
152** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
153** process that requests an EXCLUSIVE lock may actually obtain a PENDING
154** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
155** sqlite3OsLock().
156*/
157#define NO_LOCK 0
158#define SHARED_LOCK 1
159#define RESERVED_LOCK 2
160#define PENDING_LOCK 3
161#define EXCLUSIVE_LOCK 4
162
163/*
164** File Locking Notes: (Mostly about windows but also some info for Unix)
165**
166** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
167** those functions are not available. So we use only LockFile() and
168** UnlockFile().
169**
170** LockFile() prevents not just writing but also reading by other processes.
171** A SHARED_LOCK is obtained by locking a single randomly-chosen
172** byte out of a specific range of bytes. The lock byte is obtained at
173** random so two separate readers can probably access the file at the
174** same time, unless they are unlucky and choose the same lock byte.
175** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
176** There can only be one writer. A RESERVED_LOCK is obtained by locking
177** a single byte of the file that is designated as the reserved lock byte.
178** A PENDING_LOCK is obtained by locking a designated byte different from
179** the RESERVED_LOCK byte.
180**
181** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
182** which means we can use reader/writer locks. When reader/writer locks
183** are used, the lock is placed on the same range of bytes that is used
184** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
185** will support two or more Win95 readers or two or more WinNT readers.
186** But a single Win95 reader will lock out all WinNT readers and a single
187** WinNT reader will lock out all other Win95 readers.
188**
189** The following #defines specify the range of bytes used for locking.
190** SHARED_SIZE is the number of bytes available in the pool from which
191** a random byte is selected for a shared lock. The pool of bytes for
192** shared locks begins at SHARED_FIRST.
193**
194** These #defines are available in sqlite_aux.h so that adaptors for
195** connecting SQLite to other operating systems can use the same byte
196** ranges for locking. In particular, the same locking strategy and
197** byte ranges are used for Unix. This leaves open the possiblity of having
198** clients on win95, winNT, and unix all talking to the same shared file
199** and all locking correctly. To do so would require that samba (or whatever
200** tool is being used for file sharing) implements locks correctly between
201** windows and unix. I'm guessing that isn't likely to happen, but by
202** using the same locking range we are at least open to the possibility.
203**
204** Locking in windows is manditory. For this reason, we cannot store
205** actual data in the bytes used for locking. The pager never allocates
206** the pages involved in locking therefore. SHARED_SIZE is selected so
207** that all locks will fit on a single page even at the minimum page size.
208** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
209** is set high so that we don't have to allocate an unused page except
210** for very large databases. But one should test the page skipping logic
211** by setting PENDING_BYTE low and running the entire regression suite.
212**
213** Changing the value of PENDING_BYTE results in a subtly incompatible
214** file format. Depending on how it is changed, you might not notice
215** the incompatibility right away, even running a full regression test.
216** The default location of PENDING_BYTE is the first byte past the
217** 1GB boundary.
218**
219*/
220#ifndef SQLITE_TEST
221#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
222#else
223extern unsigned int sqlite3_pending_byte;
224#define PENDING_BYTE sqlite3_pending_byte
225#endif
226
227#define RESERVED_BYTE (PENDING_BYTE+1)
228#define SHARED_FIRST (PENDING_BYTE+2)
229#define SHARED_SIZE 510
230
231/*
232** Functions for accessing sqlite3_file methods
233*/
234int sqlite3OsClose(sqlite3_file*);
235int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
236int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
237int sqlite3OsTruncate(sqlite3_file*, i64 size);
238int sqlite3OsSync(sqlite3_file*, int);
239int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
240int sqlite3OsLock(sqlite3_file*, int);
241int sqlite3OsUnlock(sqlite3_file*, int);
242int sqlite3OsCheckReservedLock(sqlite3_file *id);
243int sqlite3OsFileControl(sqlite3_file*,int,void*);
244int sqlite3OsSectorSize(sqlite3_file *id);
245int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
246
247/*
248** Functions for accessing sqlite3_vfs methods
249*/
250int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
251int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
252int sqlite3OsAccess(sqlite3_vfs *, const char *, int);
253int sqlite3OsGetTempname(sqlite3_vfs *, int, char *);
254int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
255void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
256void sqlite3OsDlError(sqlite3_vfs *, int, char *);
257void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *);
258void sqlite3OsDlClose(sqlite3_vfs *, void *);
259int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
260int sqlite3OsSleep(sqlite3_vfs *, int);
261int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
262
263/*
264** Convenience functions for opening and closing files using
265** sqlite3_malloc() to obtain space for the file-handle structure.
266*/
267int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
268int sqlite3OsCloseFree(sqlite3_file *);
269
270/*
271** Each OS-specific backend defines an instance of the following
272** structure for returning a pointer to its sqlite3_vfs. If OS_OTHER
273** is defined (meaning that the application-defined OS interface layer
274** is used) then there is no default VFS. The application must
275** register one or more VFS structures using sqlite3_vfs_register()
276** before attempting to use SQLite.
277*/
278#if OS_UNIX || OS_WIN || OS_OS2
279sqlite3_vfs *sqlite3OsDefaultVfs(void);
280#else
281# define sqlite3OsDefaultVfs(X) 0
282#endif
283
284#endif /* _SQLITE_OS_H_ */
diff --git a/libraries/sqlite/win32/os_common.h b/libraries/sqlite/win32/os_common.h
new file mode 100755
index 0000000..8de4be9
--- /dev/null
+++ b/libraries/sqlite/win32/os_common.h
@@ -0,0 +1,127 @@
1/*
2** 2004 May 22
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.
10**
11******************************************************************************
12**
13** This file contains macros and a little bit of code that is common to
14** all of the platform-specific files (os_*.c) and is #included into those
15** files.
16**
17** This file should be #included by the os_*.c files only. It is not a
18** general purpose header file.
19*/
20
21/*
22** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24** switch. The following code should catch this problem at compile-time.
25*/
26#ifdef MEMORY_DEBUG
27# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
28#endif
29
30
31/*
32 * When testing, this global variable stores the location of the
33 * pending-byte in the database file.
34 */
35#ifdef SQLITE_TEST
36unsigned int sqlite3_pending_byte = 0x40000000;
37#endif
38
39#ifdef SQLITE_DEBUG
40int sqlite3_os_trace = 0;
41#define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
42#define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
43#define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
44#define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
45#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
46#define OSTRACE6(X,Y,Z,A,B,C) \
47 if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
48#define OSTRACE7(X,Y,Z,A,B,C,D) \
49 if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
50#else
51#define OSTRACE1(X)
52#define OSTRACE2(X,Y)
53#define OSTRACE3(X,Y,Z)
54#define OSTRACE4(X,Y,Z,A)
55#define OSTRACE5(X,Y,Z,A,B)
56#define OSTRACE6(X,Y,Z,A,B,C)
57#define OSTRACE7(X,Y,Z,A,B,C,D)
58#endif
59
60/*
61** Macros for performance tracing. Normally turned off. Only works
62** on i486 hardware.
63*/
64#ifdef SQLITE_PERFORMANCE_TRACE
65__inline__ unsigned long long int hwtime(void){
66 unsigned long long int x;
67 __asm__("rdtsc\n\t"
68 "mov %%edx, %%ecx\n\t"
69 :"=A" (x));
70 return x;
71}
72static unsigned long long int g_start;
73static unsigned int elapse;
74#define TIMER_START g_start=hwtime()
75#define TIMER_END elapse=hwtime()-g_start
76#define TIMER_ELAPSED elapse
77#else
78#define TIMER_START
79#define TIMER_END
80#define TIMER_ELAPSED 0
81#endif
82
83/*
84** If we compile with the SQLITE_TEST macro set, then the following block
85** of code will give us the ability to simulate a disk I/O error. This
86** is used for testing the I/O recovery logic.
87*/
88#ifdef SQLITE_TEST
89int sqlite3_io_error_hit = 0;
90int sqlite3_io_error_pending = 0;
91int sqlite3_io_error_persist = 0;
92int sqlite3_diskfull_pending = 0;
93int sqlite3_diskfull = 0;
94#define SimulateIOError(CODE) \
95 if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
96 if( sqlite3_io_error_pending-- == 1 \
97 || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
98 { local_ioerr(); CODE; }
99static void local_ioerr(){
100 IOTRACE(("IOERR\n"));
101 sqlite3_io_error_hit = 1;
102}
103#define SimulateDiskfullError(CODE) \
104 if( sqlite3_diskfull_pending ){ \
105 if( sqlite3_diskfull_pending == 1 ){ \
106 local_ioerr(); \
107 sqlite3_diskfull = 1; \
108 sqlite3_io_error_hit = 1; \
109 CODE; \
110 }else{ \
111 sqlite3_diskfull_pending--; \
112 } \
113 }
114#else
115#define SimulateIOError(A)
116#define SimulateDiskfullError(A)
117#endif
118
119/*
120** When testing, keep a count of the number of open files.
121*/
122#ifdef SQLITE_TEST
123int sqlite3_open_file_count = 0;
124#define OpenCounter(X) sqlite3_open_file_count+=(X)
125#else
126#define OpenCounter(X)
127#endif
diff --git a/libraries/sqlite/win32/os_os2.c b/libraries/sqlite/win32/os_os2.c
new file mode 100755
index 0000000..c4fbe66
--- /dev/null
+++ b/libraries/sqlite/win32/os_os2.c
@@ -0,0 +1,1032 @@
1/*
2** 2006 Feb 14
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.
10**
11******************************************************************************
12**
13** This file contains code that is specific to OS/2.
14*/
15
16#include "sqliteInt.h"
17
18#if OS_OS2
19
20/*
21** Macros used to determine whether or not to use threads.
22*/
23#if defined(THREADSAFE) && THREADSAFE
24# define SQLITE_OS2_THREADS 1
25#endif
26
27/*
28** Include code that is common to all os_*.c files
29*/
30#include "os_common.h"
31
32/*
33** The os2File structure is subclass of OsFile specific for the OS/2
34** protability layer.
35*/
36typedef struct os2File os2File;
37struct os2File {
38 IoMethod const *pMethod; /* Always the first entry */
39 HFILE h; /* Handle for accessing the file */
40 int delOnClose; /* True if file is to be deleted on close */
41 char* pathToDel; /* Name of file to delete on close */
42 unsigned char locktype; /* Type of lock currently held on this file */
43};
44
45/*
46** Do not include any of the File I/O interface procedures if the
47** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
48** will be in-memory only)
49*/
50#ifndef SQLITE_OMIT_DISKIO
51
52/*
53** Delete the named file
54*/
55int sqlite3Os2Delete( const char *zFilename ){
56 APIRET rc = NO_ERROR;
57
58 rc = DosDelete( (PSZ)zFilename );
59 OSTRACE2( "DELETE \"%s\"\n", zFilename );
60 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
61}
62
63/*
64** Return TRUE if the named file exists.
65*/
66int sqlite3Os2FileExists( const char *zFilename ){
67 FILESTATUS3 fsts3ConfigInfo;
68 memset(&fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo));
69 return DosQueryPathInfo( (PSZ)zFilename, FIL_STANDARD,
70 &fsts3ConfigInfo, sizeof(FILESTATUS3) ) == NO_ERROR;
71}
72
73/* Forward declaration */
74int allocateOs2File( os2File *pInit, OsFile **pld );
75
76/*
77** Attempt to open a file for both reading and writing. If that
78** fails, try opening it read-only. If the file does not exist,
79** try to create it.
80**
81** On success, a handle for the open file is written to *id
82** and *pReadonly is set to 0 if the file was opened for reading and
83** writing or 1 if the file was opened read-only. The function returns
84** SQLITE_OK.
85**
86** On failure, the function returns SQLITE_CANTOPEN and leaves
87** *id and *pReadonly unchanged.
88*/
89int sqlite3Os2OpenReadWrite(
90 const char *zFilename,
91 OsFile **pld,
92 int *pReadonly
93){
94 os2File f;
95 HFILE hf;
96 ULONG ulAction;
97 APIRET rc = NO_ERROR;
98
99 assert( *pld == 0 );
100 rc = DosOpen( (PSZ)zFilename, &hf, &ulAction, 0L,
101 FILE_ARCHIVED | FILE_NORMAL,
102 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
103 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM |
104 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE, (PEAOP2)NULL );
105 if( rc != NO_ERROR ){
106 rc = DosOpen( (PSZ)zFilename, &hf, &ulAction, 0L,
107 FILE_ARCHIVED | FILE_NORMAL,
108 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
109 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM |
110 OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY, (PEAOP2)NULL );
111 if( rc != NO_ERROR ){
112 return SQLITE_CANTOPEN;
113 }
114 *pReadonly = 1;
115 }
116 else{
117 *pReadonly = 0;
118 }
119 f.h = hf;
120 f.locktype = NO_LOCK;
121 f.delOnClose = 0;
122 f.pathToDel = NULL;
123 OpenCounter(+1);
124 OSTRACE3( "OPEN R/W %d \"%s\"\n", hf, zFilename );
125 return allocateOs2File( &f, pld );
126}
127
128
129/*
130** Attempt to open a new file for exclusive access by this process.
131** The file will be opened for both reading and writing. To avoid
132** a potential security problem, we do not allow the file to have
133** previously existed. Nor do we allow the file to be a symbolic
134** link.
135**
136** If delFlag is true, then make arrangements to automatically delete
137** the file when it is closed.
138**
139** On success, write the file handle into *id and return SQLITE_OK.
140**
141** On failure, return SQLITE_CANTOPEN.
142*/
143int sqlite3Os2OpenExclusive( const char *zFilename, OsFile **pld, int delFlag ){
144 os2File f;
145 HFILE hf;
146 ULONG ulAction;
147 APIRET rc = NO_ERROR;
148
149 assert( *pld == 0 );
150 rc = DosOpen( (PSZ)zFilename, &hf, &ulAction, 0L, FILE_NORMAL,
151 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
152 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM |
153 OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE, (PEAOP2)NULL );
154 if( rc != NO_ERROR ){
155 return SQLITE_CANTOPEN;
156 }
157
158 f.h = hf;
159 f.locktype = NO_LOCK;
160 f.delOnClose = delFlag ? 1 : 0;
161 f.pathToDel = delFlag ? sqlite3OsFullPathname( zFilename ) : NULL;
162 OpenCounter( +1 );
163 if( delFlag ) DosForceDelete( (PSZ)sqlite3OsFullPathname( zFilename ) );
164 OSTRACE3( "OPEN EX %d \"%s\"\n", hf, sqlite3OsFullPathname ( zFilename ) );
165 return allocateOs2File( &f, pld );
166}
167
168/*
169** Attempt to open a new file for read-only access.
170**
171** On success, write the file handle into *id and return SQLITE_OK.
172**
173** On failure, return SQLITE_CANTOPEN.
174*/
175int sqlite3Os2OpenReadOnly( const char *zFilename, OsFile **pld ){
176 os2File f;
177 HFILE hf;
178 ULONG ulAction;
179 APIRET rc = NO_ERROR;
180
181 assert( *pld == 0 );
182 rc = DosOpen( (PSZ)zFilename, &hf, &ulAction, 0L,
183 FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
184 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM |
185 OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY, (PEAOP2)NULL );
186 if( rc != NO_ERROR ){
187 return SQLITE_CANTOPEN;
188 }
189 f.h = hf;
190 f.locktype = NO_LOCK;
191 f.delOnClose = 0;
192 f.pathToDel = NULL;
193 OpenCounter( +1 );
194 OSTRACE3( "OPEN RO %d \"%s\"\n", hf, zFilename );
195 return allocateOs2File( &f, pld );
196}
197
198/*
199** Attempt to open a file descriptor for the directory that contains a
200** file. This file descriptor can be used to fsync() the directory
201** in order to make sure the creation of a new file is actually written
202** to disk.
203**
204** This routine is only meaningful for Unix. It is a no-op under
205** OS/2 since OS/2 does not support hard links.
206**
207** On success, a handle for a previously open file is at *id is
208** updated with the new directory file descriptor and SQLITE_OK is
209** returned.
210**
211** On failure, the function returns SQLITE_CANTOPEN and leaves
212** *id unchanged.
213*/
214int os2OpenDirectory(
215 OsFile *id,
216 const char *zDirname
217){
218 return SQLITE_OK;
219}
220
221/*
222** Create a temporary file name in zBuf. zBuf must be big enough to
223** hold at least SQLITE_TEMPNAME_SIZE characters.
224*/
225int sqlite3Os2TempFileName( char *zBuf ){
226 static const unsigned char zChars[] =
227 "abcdefghijklmnopqrstuvwxyz"
228 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
229 "0123456789";
230 int i, j;
231 PSZ zTempPath = 0;
232 if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){
233 if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){
234 if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){
235 ULONG ulDriveNum = 0, ulDriveMap = 0;
236 DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
237 sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) );
238 }
239 }
240 }
241 /* strip off a trailing slashes or backslashes, otherwise we would get *
242 * multiple (back)slashes which causes DosOpen() to fail */
243 j = strlen(zTempPath);
244 while( j > 0 && zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ){
245 j--;
246 }
247 zTempPath[j] = '\0';
248 for(;;){
249 sprintf( zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath );
250 j = strlen( zBuf );
251 sqlite3Randomness( 15, &zBuf[j] );
252 for( i = 0; i < 15; i++, j++ ){
253 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
254 }
255 zBuf[j] = 0;
256 if( !sqlite3OsFileExists( zBuf ) ) break;
257 }
258 OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
259 return SQLITE_OK;
260}
261
262/*
263** Close a file.
264*/
265int os2Close( OsFile **pld ){
266 os2File *pFile;
267 APIRET rc = NO_ERROR;
268 if( pld && (pFile = (os2File*)*pld) != 0 ){
269 OSTRACE2( "CLOSE %d\n", pFile->h );
270 rc = DosClose( pFile->h );
271 pFile->locktype = NO_LOCK;
272 if( pFile->delOnClose != 0 ){
273 rc = DosForceDelete( (PSZ)pFile->pathToDel );
274 }
275 *pld = 0;
276 OpenCounter( -1 );
277 }
278
279 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
280}
281
282/*
283** Read data from a file into a buffer. Return SQLITE_OK if all
284** bytes were read successfully and SQLITE_IOERR if anything goes
285** wrong.
286*/
287int os2Read( OsFile *id, void *pBuf, int amt ){
288 ULONG got;
289 assert( id!=0 );
290 SimulateIOError( return SQLITE_IOERR );
291 OSTRACE3( "READ %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
292 DosRead( ((os2File*)id)->h, pBuf, amt, &got );
293 if (got == (ULONG)amt)
294 return SQLITE_OK;
295 else if (got == 0)
296 return SQLITE_IOERR_READ;
297 else {
298 memset(&((char*)pBuf)[got], 0, amt-got);
299 return SQLITE_IOERR_SHORT_READ;
300 }
301}
302
303/*
304** Write data from a buffer into a file. Return SQLITE_OK on success
305** or some other error code on failure.
306*/
307int os2Write( OsFile *id, const void *pBuf, int amt ){
308 APIRET rc = NO_ERROR;
309 ULONG wrote;
310 assert( id!=0 );
311 SimulateIOError( return SQLITE_IOERR );
312 SimulateDiskfullError( return SQLITE_FULL );
313 OSTRACE3( "WRITE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
314 while( amt > 0 &&
315 (rc = DosWrite( ((os2File*)id)->h, (PVOID)pBuf, amt, &wrote )) && wrote > 0 ){
316 amt -= wrote;
317 pBuf = &((char*)pBuf)[wrote];
318 }
319
320 return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
321}
322
323/*
324** Move the read/write pointer in a file.
325*/
326int os2Seek( OsFile *id, i64 offset ){
327 APIRET rc = NO_ERROR;
328 ULONG filePointer = 0L;
329 assert( id!=0 );
330 rc = DosSetFilePtr( ((os2File*)id)->h, offset, FILE_BEGIN, &filePointer );
331 OSTRACE3( "SEEK %d %lld\n", ((os2File*)id)->h, offset );
332 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
333}
334
335/*
336** Make sure all writes to a particular file are committed to disk.
337*/
338int os2Sync( OsFile *id, int dataOnly ){
339 assert( id!=0 );
340 OSTRACE3( "SYNC %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
341 return DosResetBuffer( ((os2File*)id)->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
342}
343
344/*
345** Sync the directory zDirname. This is a no-op on operating systems other
346** than UNIX.
347*/
348int sqlite3Os2SyncDirectory( const char *zDirname ){
349 SimulateIOError( return SQLITE_IOERR );
350 return SQLITE_OK;
351}
352
353/*
354** Truncate an open file to a specified size
355*/
356int os2Truncate( OsFile *id, i64 nByte ){
357 APIRET rc = NO_ERROR;
358 ULONG upperBits = nByte>>32;
359 assert( id!=0 );
360 OSTRACE3( "TRUNCATE %d %lld\n", ((os2File*)id)->h, nByte );
361 SimulateIOError( return SQLITE_IOERR );
362 rc = DosSetFilePtr( ((os2File*)id)->h, nByte, FILE_BEGIN, &upperBits );
363 if( rc != NO_ERROR ){
364 return SQLITE_IOERR;
365 }
366 rc = DosSetFilePtr( ((os2File*)id)->h, 0L, FILE_END, &upperBits );
367 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
368}
369
370/*
371** Determine the current size of a file in bytes
372*/
373int os2FileSize( OsFile *id, i64 *pSize ){
374 APIRET rc = NO_ERROR;
375 FILESTATUS3 fsts3FileInfo;
376 memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
377 assert( id!=0 );
378 SimulateIOError( return SQLITE_IOERR );
379 rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
380 if( rc == NO_ERROR ){
381 *pSize = fsts3FileInfo.cbFile;
382 return SQLITE_OK;
383 }
384 else{
385 return SQLITE_IOERR;
386 }
387}
388
389/*
390** Acquire a reader lock.
391*/
392static int getReadLock( os2File *id ){
393 FILELOCK LockArea,
394 UnlockArea;
395 memset(&LockArea, 0, sizeof(LockArea));
396 memset(&UnlockArea, 0, sizeof(UnlockArea));
397 LockArea.lOffset = SHARED_FIRST;
398 LockArea.lRange = SHARED_SIZE;
399 UnlockArea.lOffset = 0L;
400 UnlockArea.lRange = 0L;
401 return DosSetFileLocks( id->h, &UnlockArea, &LockArea, 2000L, 1L );
402}
403
404/*
405** Undo a readlock
406*/
407static int unlockReadLock( os2File *id ){
408 FILELOCK LockArea,
409 UnlockArea;
410 memset(&LockArea, 0, sizeof(LockArea));
411 memset(&UnlockArea, 0, sizeof(UnlockArea));
412 LockArea.lOffset = 0L;
413 LockArea.lRange = 0L;
414 UnlockArea.lOffset = SHARED_FIRST;
415 UnlockArea.lRange = SHARED_SIZE;
416 return DosSetFileLocks( id->h, &UnlockArea, &LockArea, 2000L, 1L );
417}
418
419#ifndef SQLITE_OMIT_PAGER_PRAGMAS
420/*
421** Check that a given pathname is a directory and is writable
422**
423*/
424int sqlite3Os2IsDirWritable( char *zDirname ){
425 FILESTATUS3 fsts3ConfigInfo;
426 APIRET rc = NO_ERROR;
427 memset(&fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo));
428 if( zDirname==0 ) return 0;
429 if( strlen(zDirname)>CCHMAXPATH ) return 0;
430 rc = DosQueryPathInfo( (PSZ)zDirname, FIL_STANDARD, &fsts3ConfigInfo, sizeof(FILESTATUS3) );
431 if( rc != NO_ERROR ) return 0;
432 if( (fsts3ConfigInfo.attrFile & FILE_DIRECTORY) != FILE_DIRECTORY ) return 0;
433
434 return 1;
435}
436#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
437
438/*
439** Lock the file with the lock specified by parameter locktype - one
440** of the following:
441**
442** (1) SHARED_LOCK
443** (2) RESERVED_LOCK
444** (3) PENDING_LOCK
445** (4) EXCLUSIVE_LOCK
446**
447** Sometimes when requesting one lock state, additional lock states
448** are inserted in between. The locking might fail on one of the later
449** transitions leaving the lock state different from what it started but
450** still short of its goal. The following chart shows the allowed
451** transitions and the inserted intermediate states:
452**
453** UNLOCKED -> SHARED
454** SHARED -> RESERVED
455** SHARED -> (PENDING) -> EXCLUSIVE
456** RESERVED -> (PENDING) -> EXCLUSIVE
457** PENDING -> EXCLUSIVE
458**
459** This routine will only increase a lock. The os2Unlock() routine
460** erases all locks at once and returns us immediately to locking level 0.
461** It is not possible to lower the locking level one step at a time. You
462** must go straight to locking level 0.
463*/
464int os2Lock( OsFile *id, int locktype ){
465 APIRET rc = SQLITE_OK; /* Return code from subroutines */
466 APIRET res = NO_ERROR; /* Result of an OS/2 lock call */
467 int newLocktype; /* Set id->locktype to this value before exiting */
468 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
469 FILELOCK LockArea,
470 UnlockArea;
471 os2File *pFile = (os2File*)id;
472 memset(&LockArea, 0, sizeof(LockArea));
473 memset(&UnlockArea, 0, sizeof(UnlockArea));
474 assert( pFile!=0 );
475 OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
476
477 /* If there is already a lock of this type or more restrictive on the
478 ** OsFile, do nothing. Don't use the end_lock: exit path, as
479 ** sqlite3OsEnterMutex() hasn't been called yet.
480 */
481 if( pFile->locktype>=locktype ){
482 return SQLITE_OK;
483 }
484
485 /* Make sure the locking sequence is correct
486 */
487 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
488 assert( locktype!=PENDING_LOCK );
489 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
490
491 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
492 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
493 ** the PENDING_LOCK byte is temporary.
494 */
495 newLocktype = pFile->locktype;
496 if( pFile->locktype==NO_LOCK
497 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
498 ){
499 int cnt = 3;
500
501 LockArea.lOffset = PENDING_BYTE;
502 LockArea.lRange = 1L;
503 UnlockArea.lOffset = 0L;
504 UnlockArea.lRange = 0L;
505
506 while( cnt-->0 && (res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L) )!=NO_ERROR ){
507 /* Try 3 times to get the pending lock. The pending lock might be
508 ** held by another reader process who will release it momentarily.
509 */
510 OSTRACE2( "could not get a PENDING lock. cnt=%d\n", cnt );
511 DosSleep(1);
512 }
513 gotPendingLock = res;
514 }
515
516 /* Acquire a shared lock
517 */
518 if( locktype==SHARED_LOCK && res ){
519 assert( pFile->locktype==NO_LOCK );
520 res = getReadLock(pFile);
521 if( res == NO_ERROR ){
522 newLocktype = SHARED_LOCK;
523 }
524 }
525
526 /* Acquire a RESERVED lock
527 */
528 if( locktype==RESERVED_LOCK && res ){
529 assert( pFile->locktype==SHARED_LOCK );
530 LockArea.lOffset = RESERVED_BYTE;
531 LockArea.lRange = 1L;
532 UnlockArea.lOffset = 0L;
533 UnlockArea.lRange = 0L;
534 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
535 if( res == NO_ERROR ){
536 newLocktype = RESERVED_LOCK;
537 }
538 }
539
540 /* Acquire a PENDING lock
541 */
542 if( locktype==EXCLUSIVE_LOCK && res ){
543 newLocktype = PENDING_LOCK;
544 gotPendingLock = 0;
545 }
546
547 /* Acquire an EXCLUSIVE lock
548 */
549 if( locktype==EXCLUSIVE_LOCK && res ){
550 assert( pFile->locktype>=SHARED_LOCK );
551 res = unlockReadLock(pFile);
552 OSTRACE2( "unreadlock = %d\n", res );
553 LockArea.lOffset = SHARED_FIRST;
554 LockArea.lRange = SHARED_SIZE;
555 UnlockArea.lOffset = 0L;
556 UnlockArea.lRange = 0L;
557 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
558 if( res == NO_ERROR ){
559 newLocktype = EXCLUSIVE_LOCK;
560 }else{
561 OSTRACE2( "error-code = %d\n", res );
562 }
563 }
564
565 /* If we are holding a PENDING lock that ought to be released, then
566 ** release it now.
567 */
568 if( gotPendingLock && locktype==SHARED_LOCK ){
569 LockArea.lOffset = 0L;
570 LockArea.lRange = 0L;
571 UnlockArea.lOffset = PENDING_BYTE;
572 UnlockArea.lRange = 1L;
573 DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
574 }
575
576 /* Update the state of the lock has held in the file descriptor then
577 ** return the appropriate result code.
578 */
579 if( res == NO_ERROR ){
580 rc = SQLITE_OK;
581 }else{
582 OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
583 locktype, newLocktype );
584 rc = SQLITE_BUSY;
585 }
586 pFile->locktype = newLocktype;
587 return rc;
588}
589
590/*
591** This routine checks if there is a RESERVED lock held on the specified
592** file by this or any other process. If such a lock is held, return
593** non-zero, otherwise zero.
594*/
595int os2CheckReservedLock( OsFile *id ){
596 APIRET rc = NO_ERROR;
597 os2File *pFile = (os2File*)id;
598 assert( pFile!=0 );
599 if( pFile->locktype>=RESERVED_LOCK ){
600 rc = 1;
601 OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
602 }else{
603 FILELOCK LockArea,
604 UnlockArea;
605 memset(&LockArea, 0, sizeof(LockArea));
606 memset(&UnlockArea, 0, sizeof(UnlockArea));
607 LockArea.lOffset = RESERVED_BYTE;
608 LockArea.lRange = 1L;
609 UnlockArea.lOffset = 0L;
610 UnlockArea.lRange = 0L;
611 rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
612 if( rc == NO_ERROR ){
613 LockArea.lOffset = 0L;
614 LockArea.lRange = 0L;
615 UnlockArea.lOffset = RESERVED_BYTE;
616 UnlockArea.lRange = 1L;
617 rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
618 }
619 OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
620 }
621 return rc;
622}
623
624/*
625** Lower the locking level on file descriptor id to locktype. locktype
626** must be either NO_LOCK or SHARED_LOCK.
627**
628** If the locking level of the file descriptor is already at or below
629** the requested locking level, this routine is a no-op.
630**
631** It is not possible for this routine to fail if the second argument
632** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
633** might return SQLITE_IOERR;
634*/
635int os2Unlock( OsFile *id, int locktype ){
636 int type;
637 APIRET rc = SQLITE_OK;
638 os2File *pFile = (os2File*)id;
639 FILELOCK LockArea,
640 UnlockArea;
641 memset(&LockArea, 0, sizeof(LockArea));
642 memset(&UnlockArea, 0, sizeof(UnlockArea));
643 assert( pFile!=0 );
644 assert( locktype<=SHARED_LOCK );
645 OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
646 type = pFile->locktype;
647 if( type>=EXCLUSIVE_LOCK ){
648 LockArea.lOffset = 0L;
649 LockArea.lRange = 0L;
650 UnlockArea.lOffset = SHARED_FIRST;
651 UnlockArea.lRange = SHARED_SIZE;
652 DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
653 if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
654 /* This should never happen. We should always be able to
655 ** reacquire the read lock */
656 rc = SQLITE_IOERR;
657 }
658 }
659 if( type>=RESERVED_LOCK ){
660 LockArea.lOffset = 0L;
661 LockArea.lRange = 0L;
662 UnlockArea.lOffset = RESERVED_BYTE;
663 UnlockArea.lRange = 1L;
664 DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
665 }
666 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
667 unlockReadLock(pFile);
668 }
669 if( type>=PENDING_LOCK ){
670 LockArea.lOffset = 0L;
671 LockArea.lRange = 0L;
672 UnlockArea.lOffset = PENDING_BYTE;
673 UnlockArea.lRange = 1L;
674 DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
675 }
676 pFile->locktype = locktype;
677 return rc;
678}
679
680/*
681** Turn a relative pathname into a full pathname. Return a pointer
682** to the full pathname stored in space obtained from sqliteMalloc().
683** The calling function is responsible for freeing this space once it
684** is no longer needed.
685*/
686char *sqlite3Os2FullPathname( const char *zRelative ){
687 char *zFull = 0;
688 if( strchr(zRelative, ':') ){
689 sqlite3SetString( &zFull, zRelative, (char*)0 );
690 }else{
691 ULONG ulDriveNum = 0;
692 ULONG ulDriveMap = 0;
693 ULONG cbzBufLen = SQLITE_TEMPNAME_SIZE;
694 char zDrive[2];
695 char *zBuff;
696
697 zBuff = sqliteMalloc( cbzBufLen );
698 if( zBuff != 0 ){
699 DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
700 if( DosQueryCurrentDir( ulDriveNum, (PBYTE)zBuff, &cbzBufLen ) == NO_ERROR ){
701 sprintf( zDrive, "%c", (char)('A' + ulDriveNum - 1) );
702 sqlite3SetString( &zFull, zDrive, ":\\", zBuff,
703 "\\", zRelative, (char*)0 );
704 }
705 sqliteFree( zBuff );
706 }
707 }
708 return zFull;
709}
710
711/*
712** The fullSync option is meaningless on os2, or correct me if I'm wrong. This is a no-op.
713** From os_unix.c: Change the value of the fullsync flag in the given file descriptor.
714** From os_unix.c: ((unixFile*)id)->fullSync = v;
715*/
716static void os2SetFullSync( OsFile *id, int v ){
717 return;
718}
719
720/*
721** Return the underlying file handle for an OsFile
722*/
723static int os2FileHandle( OsFile *id ){
724 return (int)((os2File*)id)->h;
725}
726
727/*
728** Return an integer that indices the type of lock currently held
729** by this handle. (Used for testing and analysis only.)
730*/
731static int os2LockState( OsFile *id ){
732 return ((os2File*)id)->locktype;
733}
734
735/*
736** Return the sector size in bytes of the underlying block device for
737** the specified file. This is almost always 512 bytes, but may be
738** larger for some devices.
739**
740** SQLite code assumes this function cannot fail. It also assumes that
741** if two files are created in the same file-system directory (i.e.
742** a database and it's journal file) that the sector size will be the
743** same for both.
744*/
745static int os2SectorSize(OsFile *id){
746 return SQLITE_DEFAULT_SECTOR_SIZE;
747}
748
749/*
750** This vector defines all the methods that can operate on an OsFile
751** for os2.
752*/
753static const IoMethod sqlite3Os2IoMethod = {
754 os2Close,
755 os2OpenDirectory,
756 os2Read,
757 os2Write,
758 os2Seek,
759 os2Truncate,
760 os2Sync,
761 os2SetFullSync,
762 os2FileHandle,
763 os2FileSize,
764 os2Lock,
765 os2Unlock,
766 os2LockState,
767 os2CheckReservedLock,
768 os2SectorSize,
769};
770
771/*
772** Allocate memory for an OsFile. Initialize the new OsFile
773** to the value given in pInit and return a pointer to the new
774** OsFile. If we run out of memory, close the file and return NULL.
775*/
776int allocateOs2File( os2File *pInit, OsFile **pld ){
777 os2File *pNew;
778 pNew = sqliteMalloc( sizeof(*pNew) );
779 if( pNew==0 ){
780 DosClose( pInit->h );
781 *pld = 0;
782 return SQLITE_NOMEM;
783 }else{
784 *pNew = *pInit;
785 pNew->pMethod = &sqlite3Os2IoMethod;
786 pNew->locktype = NO_LOCK;
787 *pld = (OsFile*)pNew;
788 OpenCounter(+1);
789 return SQLITE_OK;
790 }
791}
792
793#endif /* SQLITE_OMIT_DISKIO */
794/***************************************************************************
795** Everything above deals with file I/O. Everything that follows deals
796** with other miscellanous aspects of the operating system interface
797****************************************************************************/
798
799#ifndef SQLITE_OMIT_LOAD_EXTENSION
800/*
801** Interfaces for opening a shared library, finding entry points
802** within the shared library, and closing the shared library.
803*/
804void *sqlite3Os2Dlopen(const char *zFilename){
805 UCHAR loadErr[256];
806 HMODULE hmod;
807 APIRET rc;
808 rc = DosLoadModule((PSZ)loadErr, sizeof(loadErr), zFilename, &hmod);
809 if (rc != NO_ERROR) return 0;
810 return (void*)hmod;
811}
812void *sqlite3Os2Dlsym(void *pHandle, const char *zSymbol){
813 PFN pfn;
814 APIRET rc;
815 rc = DosQueryProcAddr((HMODULE)pHandle, 0L, zSymbol, &pfn);
816 if (rc != NO_ERROR) {
817 /* if the symbol itself was not found, search again for the same
818 * symbol with an extra underscore, that might be needed depending
819 * on the calling convention */
820 char _zSymbol[256] = "_";
821 strncat(_zSymbol, zSymbol, 255);
822 rc = DosQueryProcAddr((HMODULE)pHandle, 0L, _zSymbol, &pfn);
823 }
824 if (rc != NO_ERROR) return 0;
825 return (void *)pfn;
826}
827int sqlite3Os2Dlclose(void *pHandle){
828 return DosFreeModule((HMODULE)pHandle);
829}
830#endif /* SQLITE_OMIT_LOAD_EXTENSION */
831
832
833/*
834** Get information to seed the random number generator. The seed
835** is written into the buffer zBuf[256]. The calling function must
836** supply a sufficiently large buffer.
837*/
838int sqlite3Os2RandomSeed( char *zBuf ){
839 /* We have to initialize zBuf to prevent valgrind from reporting
840 ** errors. The reports issued by valgrind are incorrect - we would
841 ** prefer that the randomness be increased by making use of the
842 ** uninitialized space in zBuf - but valgrind errors tend to worry
843 ** some users. Rather than argue, it seems easier just to initialize
844 ** the whole array and silence valgrind, even if that means less randomness
845 ** in the random seed.
846 **
847 ** When testing, initializing zBuf[] to zero is all we do. That means
848 ** that we always use the same random number sequence. This makes the
849 ** tests repeatable.
850 */
851 memset( zBuf, 0, 256 );
852 DosGetDateTime( (PDATETIME)zBuf );
853 return SQLITE_OK;
854}
855
856/*
857** Sleep for a little while. Return the amount of time slept.
858*/
859int sqlite3Os2Sleep( int ms ){
860 DosSleep( ms );
861 return ms;
862}
863
864/*
865** Static variables used for thread synchronization
866*/
867static int inMutex = 0;
868#ifdef SQLITE_OS2_THREADS
869static ULONG mutexOwner;
870#endif
871
872/*
873** The following pair of routines implement mutual exclusion for
874** multi-threaded processes. Only a single thread is allowed to
875** executed code that is surrounded by EnterMutex() and LeaveMutex().
876**
877** SQLite uses only a single Mutex. There is not much critical
878** code and what little there is executes quickly and without blocking.
879*/
880void sqlite3Os2EnterMutex(){
881#ifdef SQLITE_OS2_THREADS
882 PTIB ptib;
883 DosEnterCritSec();
884 DosGetInfoBlocks( &ptib, NULL );
885 mutexOwner = ptib->tib_ptib2->tib2_ultid;
886#endif
887 assert( !inMutex );
888 inMutex = 1;
889}
890void sqlite3Os2LeaveMutex(){
891#ifdef SQLITE_OS2_THREADS
892 PTIB ptib;
893#endif
894 assert( inMutex );
895 inMutex = 0;
896#ifdef SQLITE_OS2_THREADS
897 DosGetInfoBlocks( &ptib, NULL );
898 assert( mutexOwner == ptib->tib_ptib2->tib2_ultid );
899 DosExitCritSec();
900#endif
901}
902
903/*
904** Return TRUE if the mutex is currently held.
905**
906** If the thisThreadOnly parameter is true, return true if and only if the
907** calling thread holds the mutex. If the parameter is false, return
908** true if any thread holds the mutex.
909*/
910int sqlite3Os2InMutex( int thisThreadOnly ){
911#ifdef SQLITE_OS2_THREADS
912 PTIB ptib;
913 DosGetInfoBlocks( &ptib, NULL );
914 return inMutex>0 && (thisThreadOnly==0 || mutexOwner==ptib->tib_ptib2->tib2_ultid);
915#else
916 return inMutex>0;
917#endif
918}
919
920/*
921** The following variable, if set to a non-zero value, becomes the result
922** returned from sqlite3OsCurrentTime(). This is used for testing.
923*/
924#ifdef SQLITE_TEST
925int sqlite3_current_time = 0;
926#endif
927
928/*
929** Find the current time (in Universal Coordinated Time). Write the
930** current time and date as a Julian Day number into *prNow and
931** return 0. Return 1 if the time and date cannot be found.
932*/
933int sqlite3Os2CurrentTime( double *prNow ){
934 double now;
935 USHORT second, minute, hour,
936 day, month, year;
937 DATETIME dt;
938 DosGetDateTime( &dt );
939 second = (USHORT)dt.seconds;
940 minute = (USHORT)dt.minutes + dt.timezone;
941 hour = (USHORT)dt.hours;
942 day = (USHORT)dt.day;
943 month = (USHORT)dt.month;
944 year = (USHORT)dt.year;
945
946 /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
947 http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c */
948 /* Calculate the Julian days */
949 now = day - 32076 +
950 1461*(year + 4800 + (month - 14)/12)/4 +
951 367*(month - 2 - (month - 14)/12*12)/12 -
952 3*((year + 4900 + (month - 14)/12)/100)/4;
953
954 /* Add the fractional hours, mins and seconds */
955 now += (hour + 12.0)/24.0;
956 now += minute/1440.0;
957 now += second/86400.0;
958 *prNow = now;
959#ifdef SQLITE_TEST
960 if( sqlite3_current_time ){
961 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
962 }
963#endif
964 return 0;
965}
966
967/*
968** Remember the number of thread-specific-data blocks allocated.
969** Use this to verify that we are not leaking thread-specific-data.
970** Ticket #1601
971*/
972#ifdef SQLITE_TEST
973int sqlite3_tsd_count = 0;
974# define TSD_COUNTER_INCR InterlockedIncrement( &sqlite3_tsd_count )
975# define TSD_COUNTER_DECR InterlockedDecrement( &sqlite3_tsd_count )
976#else
977# define TSD_COUNTER_INCR /* no-op */
978# define TSD_COUNTER_DECR /* no-op */
979#endif
980
981/*
982** If called with allocateFlag>1, then return a pointer to thread
983** specific data for the current thread. Allocate and zero the
984** thread-specific data if it does not already exist necessary.
985**
986** If called with allocateFlag==0, then check the current thread
987** specific data. Return it if it exists. If it does not exist,
988** then return NULL.
989**
990** If called with allocateFlag<0, check to see if the thread specific
991** data is allocated and is all zero. If it is then deallocate it.
992** Return a pointer to the thread specific data or NULL if it is
993** unallocated or gets deallocated.
994*/
995ThreadData *sqlite3Os2ThreadSpecificData( int allocateFlag ){
996 static ThreadData **s_ppTsd = NULL;
997 static const ThreadData zeroData = {0, 0, 0};
998 ThreadData *pTsd;
999
1000 if( !s_ppTsd ){
1001 sqlite3OsEnterMutex();
1002 if( !s_ppTsd ){
1003 PULONG pul;
1004 APIRET rc = DosAllocThreadLocalMemory(1, &pul);
1005 if( rc != NO_ERROR ){
1006 sqlite3OsLeaveMutex();
1007 return 0;
1008 }
1009 s_ppTsd = (ThreadData **)pul;
1010 }
1011 sqlite3OsLeaveMutex();
1012 }
1013 pTsd = *s_ppTsd;
1014 if( allocateFlag>0 ){
1015 if( !pTsd ){
1016 pTsd = sqlite3OsMalloc( sizeof(zeroData) );
1017 if( pTsd ){
1018 *pTsd = zeroData;
1019 *s_ppTsd = pTsd;
1020 TSD_COUNTER_INCR;
1021 }
1022 }
1023 }else if( pTsd!=0 && allocateFlag<0
1024 && memcmp( pTsd, &zeroData, sizeof(ThreadData) )==0 ){
1025 sqlite3OsFree(pTsd);
1026 *s_ppTsd = NULL;
1027 TSD_COUNTER_DECR;
1028 pTsd = 0;
1029 }
1030 return pTsd;
1031}
1032#endif /* OS_OS2 */
diff --git a/libraries/sqlite/win32/os_unix.c b/libraries/sqlite/win32/os_unix.c
new file mode 100755
index 0000000..e95435e
--- /dev/null
+++ b/libraries/sqlite/win32/os_unix.c
@@ -0,0 +1,2749 @@
1/*
2** 2004 May 22
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.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
14*/
15#include "sqliteInt.h"
16#if OS_UNIX /* This file is used on unix only */
17
18/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
19
20/*
21** These #defines should enable >2GB file support on Posix if the
22** underlying operating system supports it. If the OS lacks
23** large file support, these should be no-ops.
24**
25** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
26** on the compiler command line. This is necessary if you are compiling
27** on a recent machine (ex: RedHat 7.2) but you want your code to work
28** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
29** without this option, LFS is enable. But LFS does not exist in the kernel
30** in RedHat 6.0, so the code won't work. Hence, for maximum binary
31** portability you should omit LFS.
32*/
33#ifndef SQLITE_DISABLE_LFS
34# define _LARGE_FILE 1
35# ifndef _FILE_OFFSET_BITS
36# define _FILE_OFFSET_BITS 64
37# endif
38# define _LARGEFILE_SOURCE 1
39#endif
40
41/*
42** standard include files.
43*/
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <fcntl.h>
47#include <unistd.h>
48#include <time.h>
49#include <sys/time.h>
50#include <errno.h>
51#ifdef SQLITE_ENABLE_LOCKING_STYLE
52#include <sys/ioctl.h>
53#include <sys/param.h>
54#include <sys/mount.h>
55#endif /* SQLITE_ENABLE_LOCKING_STYLE */
56
57/*
58** If we are to be thread-safe, include the pthreads header and define
59** the SQLITE_UNIX_THREADS macro.
60*/
61#if SQLITE_THREADSAFE
62# include <pthread.h>
63# define SQLITE_UNIX_THREADS 1
64#endif
65
66/*
67** Default permissions when creating a new file
68*/
69#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
70# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
71#endif
72
73/*
74** Maximum supported path-length.
75*/
76#define MAX_PATHNAME 512
77
78
79/*
80** The unixFile structure is subclass of sqlite3_file specific for the unix
81** protability layer.
82*/
83typedef struct unixFile unixFile;
84struct unixFile {
85 sqlite3_io_methods const *pMethod; /* Always the first entry */
86#ifdef SQLITE_TEST
87 /* In test mode, increase the size of this structure a bit so that
88 ** it is larger than the struct CrashFile defined in test6.c.
89 */
90 char aPadding[32];
91#endif
92 struct openCnt *pOpen; /* Info about all open fd's on this inode */
93 struct lockInfo *pLock; /* Info about locks on this inode */
94#ifdef SQLITE_ENABLE_LOCKING_STYLE
95 void *lockingContext; /* Locking style specific state */
96#endif /* SQLITE_ENABLE_LOCKING_STYLE */
97 int h; /* The file descriptor */
98 unsigned char locktype; /* The type of lock held on this fd */
99 int dirfd; /* File descriptor for the directory */
100#if SQLITE_THREADSAFE
101 pthread_t tid; /* The thread that "owns" this unixFile */
102#endif
103};
104
105/*
106** Include code that is common to all os_*.c files
107*/
108#include "os_common.h"
109
110/*
111** Define various macros that are missing from some systems.
112*/
113#ifndef O_LARGEFILE
114# define O_LARGEFILE 0
115#endif
116#ifdef SQLITE_DISABLE_LFS
117# undef O_LARGEFILE
118# define O_LARGEFILE 0
119#endif
120#ifndef O_NOFOLLOW
121# define O_NOFOLLOW 0
122#endif
123#ifndef O_BINARY
124# define O_BINARY 0
125#endif
126
127/*
128** The DJGPP compiler environment looks mostly like Unix, but it
129** lacks the fcntl() system call. So redefine fcntl() to be something
130** that always succeeds. This means that locking does not occur under
131** DJGPP. But it's DOS - what did you expect?
132*/
133#ifdef __DJGPP__
134# define fcntl(A,B,C) 0
135#endif
136
137/*
138** The threadid macro resolves to the thread-id or to 0. Used for
139** testing and debugging only.
140*/
141#if SQLITE_THREADSAFE
142#define threadid pthread_self()
143#else
144#define threadid 0
145#endif
146
147/*
148** Set or check the unixFile.tid field. This field is set when an unixFile
149** is first opened. All subsequent uses of the unixFile verify that the
150** same thread is operating on the unixFile. Some operating systems do
151** not allow locks to be overridden by other threads and that restriction
152** means that sqlite3* database handles cannot be moved from one thread
153** to another. This logic makes sure a user does not try to do that
154** by mistake.
155**
156** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
157** another as long as we are running on a system that supports threads
158** overriding each others locks (which now the most common behavior)
159** or if no locks are held. But the unixFile.pLock field needs to be
160** recomputed because its key includes the thread-id. See the
161** transferOwnership() function below for additional information
162*/
163#if SQLITE_THREADSAFE
164# define SET_THREADID(X) (X)->tid = pthread_self()
165# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
166 !pthread_equal((X)->tid, pthread_self()))
167#else
168# define SET_THREADID(X)
169# define CHECK_THREADID(X) 0
170#endif
171
172/*
173** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
174** section 6.5.2.2 lines 483 through 490 specify that when a process
175** sets or clears a lock, that operation overrides any prior locks set
176** by the same process. It does not explicitly say so, but this implies
177** that it overrides locks set by the same process using a different
178** file descriptor. Consider this test case:
179**
180** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
181** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
182**
183** Suppose ./file1 and ./file2 are really the same file (because
184** one is a hard or symbolic link to the other) then if you set
185** an exclusive lock on fd1, then try to get an exclusive lock
186** on fd2, it works. I would have expected the second lock to
187** fail since there was already a lock on the file due to fd1.
188** But not so. Since both locks came from the same process, the
189** second overrides the first, even though they were on different
190** file descriptors opened on different file names.
191**
192** Bummer. If you ask me, this is broken. Badly broken. It means
193** that we cannot use POSIX locks to synchronize file access among
194** competing threads of the same process. POSIX locks will work fine
195** to synchronize access for threads in separate processes, but not
196** threads within the same process.
197**
198** To work around the problem, SQLite has to manage file locks internally
199** on its own. Whenever a new database is opened, we have to find the
200** specific inode of the database file (the inode is determined by the
201** st_dev and st_ino fields of the stat structure that fstat() fills in)
202** and check for locks already existing on that inode. When locks are
203** created or removed, we have to look at our own internal record of the
204** locks to see if another thread has previously set a lock on that same
205** inode.
206**
207** The sqlite3_file structure for POSIX is no longer just an integer file
208** descriptor. It is now a structure that holds the integer file
209** descriptor and a pointer to a structure that describes the internal
210** locks on the corresponding inode. There is one locking structure
211** per inode, so if the same inode is opened twice, both unixFile structures
212** point to the same locking structure. The locking structure keeps
213** a reference count (so we will know when to delete it) and a "cnt"
214** field that tells us its internal lock status. cnt==0 means the
215** file is unlocked. cnt==-1 means the file has an exclusive lock.
216** cnt>0 means there are cnt shared locks on the file.
217**
218** Any attempt to lock or unlock a file first checks the locking
219** structure. The fcntl() system call is only invoked to set a
220** POSIX lock if the internal lock structure transitions between
221** a locked and an unlocked state.
222**
223** 2004-Jan-11:
224** More recent discoveries about POSIX advisory locks. (The more
225** I discover, the more I realize the a POSIX advisory locks are
226** an abomination.)
227**
228** If you close a file descriptor that points to a file that has locks,
229** all locks on that file that are owned by the current process are
230** released. To work around this problem, each unixFile structure contains
231** a pointer to an openCnt structure. There is one openCnt structure
232** per open inode, which means that multiple unixFile can point to a single
233** openCnt. When an attempt is made to close an unixFile, if there are
234** other unixFile open on the same inode that are holding locks, the call
235** to close() the file descriptor is deferred until all of the locks clear.
236** The openCnt structure keeps a list of file descriptors that need to
237** be closed and that list is walked (and cleared) when the last lock
238** clears.
239**
240** First, under Linux threads, because each thread has a separate
241** process ID, lock operations in one thread do not override locks
242** to the same file in other threads. Linux threads behave like
243** separate processes in this respect. But, if you close a file
244** descriptor in linux threads, all locks are cleared, even locks
245** on other threads and even though the other threads have different
246** process IDs. Linux threads is inconsistent in this respect.
247** (I'm beginning to think that linux threads is an abomination too.)
248** The consequence of this all is that the hash table for the lockInfo
249** structure has to include the process id as part of its key because
250** locks in different threads are treated as distinct. But the
251** openCnt structure should not include the process id in its
252** key because close() clears lock on all threads, not just the current
253** thread. Were it not for this goofiness in linux threads, we could
254** combine the lockInfo and openCnt structures into a single structure.
255**
256** 2004-Jun-28:
257** On some versions of linux, threads can override each others locks.
258** On others not. Sometimes you can change the behavior on the same
259** system by setting the LD_ASSUME_KERNEL environment variable. The
260** POSIX standard is silent as to which behavior is correct, as far
261** as I can tell, so other versions of unix might show the same
262** inconsistency. There is no little doubt in my mind that posix
263** advisory locks and linux threads are profoundly broken.
264**
265** To work around the inconsistencies, we have to test at runtime
266** whether or not threads can override each others locks. This test
267** is run once, the first time any lock is attempted. A static
268** variable is set to record the results of this test for future
269** use.
270*/
271
272/*
273** An instance of the following structure serves as the key used
274** to locate a particular lockInfo structure given its inode.
275**
276** If threads cannot override each others locks, then we set the
277** lockKey.tid field to the thread ID. If threads can override
278** each others locks then tid is always set to zero. tid is omitted
279** if we compile without threading support.
280*/
281struct lockKey {
282 dev_t dev; /* Device number */
283 ino_t ino; /* Inode number */
284#if SQLITE_THREADSAFE
285 pthread_t tid; /* Thread ID or zero if threads can override each other */
286#endif
287};
288
289/*
290** An instance of the following structure is allocated for each open
291** inode on each thread with a different process ID. (Threads have
292** different process IDs on linux, but not on most other unixes.)
293**
294** A single inode can have multiple file descriptors, so each unixFile
295** structure contains a pointer to an instance of this object and this
296** object keeps a count of the number of unixFile pointing to it.
297*/
298struct lockInfo {
299 struct lockKey key; /* The lookup key */
300 int cnt; /* Number of SHARED locks held */
301 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
302 int nRef; /* Number of pointers to this structure */
303};
304
305/*
306** An instance of the following structure serves as the key used
307** to locate a particular openCnt structure given its inode. This
308** is the same as the lockKey except that the thread ID is omitted.
309*/
310struct openKey {
311 dev_t dev; /* Device number */
312 ino_t ino; /* Inode number */
313};
314
315/*
316** An instance of the following structure is allocated for each open
317** inode. This structure keeps track of the number of locks on that
318** inode. If a close is attempted against an inode that is holding
319** locks, the close is deferred until all locks clear by adding the
320** file descriptor to be closed to the pending list.
321*/
322struct openCnt {
323 struct openKey key; /* The lookup key */
324 int nRef; /* Number of pointers to this structure */
325 int nLock; /* Number of outstanding locks */
326 int nPending; /* Number of pending close() operations */
327 int *aPending; /* Malloced space holding fd's awaiting a close() */
328};
329
330/*
331** These hash tables map inodes and file descriptors (really, lockKey and
332** openKey structures) into lockInfo and openCnt structures. Access to
333** these hash tables must be protected by a mutex.
334*/
335static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
336static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
337
338#ifdef SQLITE_ENABLE_LOCKING_STYLE
339/*
340** The locking styles are associated with the different file locking
341** capabilities supported by different file systems.
342**
343** POSIX locking style fully supports shared and exclusive byte-range locks
344** ADP locking only supports exclusive byte-range locks
345** FLOCK only supports a single file-global exclusive lock
346** DOTLOCK isn't a true locking style, it refers to the use of a special
347** file named the same as the database file with a '.lock' extension, this
348** can be used on file systems that do not offer any reliable file locking
349** NO locking means that no locking will be attempted, this is only used for
350** read-only file systems currently
351** UNSUPPORTED means that no locking will be attempted, this is only used for
352** file systems that are known to be unsupported
353*/
354typedef enum {
355 posixLockingStyle = 0, /* standard posix-advisory locks */
356 afpLockingStyle, /* use afp locks */
357 flockLockingStyle, /* use flock() */
358 dotlockLockingStyle, /* use <file>.lock files */
359 noLockingStyle, /* useful for read-only file system */
360 unsupportedLockingStyle /* indicates unsupported file system */
361} sqlite3LockingStyle;
362#endif /* SQLITE_ENABLE_LOCKING_STYLE */
363
364/*
365** Helper functions to obtain and relinquish the global mutex.
366*/
367static void enterMutex(){
368 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
369}
370static void leaveMutex(){
371 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
372}
373
374#if SQLITE_THREADSAFE
375/*
376** This variable records whether or not threads can override each others
377** locks.
378**
379** 0: No. Threads cannot override each others locks.
380** 1: Yes. Threads can override each others locks.
381** -1: We don't know yet.
382**
383** On some systems, we know at compile-time if threads can override each
384** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
385** will be set appropriately. On other systems, we have to check at
386** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
387** undefined.
388**
389** This variable normally has file scope only. But during testing, we make
390** it a global so that the test code can change its value in order to verify
391** that the right stuff happens in either case.
392*/
393#ifndef SQLITE_THREAD_OVERRIDE_LOCK
394# define SQLITE_THREAD_OVERRIDE_LOCK -1
395#endif
396#ifdef SQLITE_TEST
397int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
398#else
399static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
400#endif
401
402/*
403** This structure holds information passed into individual test
404** threads by the testThreadLockingBehavior() routine.
405*/
406struct threadTestData {
407 int fd; /* File to be locked */
408 struct flock lock; /* The locking operation */
409 int result; /* Result of the locking operation */
410};
411
412#ifdef SQLITE_LOCK_TRACE
413/*
414** Print out information about all locking operations.
415**
416** This routine is used for troubleshooting locks on multithreaded
417** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
418** command-line option on the compiler. This code is normally
419** turned off.
420*/
421static int lockTrace(int fd, int op, struct flock *p){
422 char *zOpName, *zType;
423 int s;
424 int savedErrno;
425 if( op==F_GETLK ){
426 zOpName = "GETLK";
427 }else if( op==F_SETLK ){
428 zOpName = "SETLK";
429 }else{
430 s = fcntl(fd, op, p);
431 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
432 return s;
433 }
434 if( p->l_type==F_RDLCK ){
435 zType = "RDLCK";
436 }else if( p->l_type==F_WRLCK ){
437 zType = "WRLCK";
438 }else if( p->l_type==F_UNLCK ){
439 zType = "UNLCK";
440 }else{
441 assert( 0 );
442 }
443 assert( p->l_whence==SEEK_SET );
444 s = fcntl(fd, op, p);
445 savedErrno = errno;
446 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
447 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
448 (int)p->l_pid, s);
449 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
450 struct flock l2;
451 l2 = *p;
452 fcntl(fd, F_GETLK, &l2);
453 if( l2.l_type==F_RDLCK ){
454 zType = "RDLCK";
455 }else if( l2.l_type==F_WRLCK ){
456 zType = "WRLCK";
457 }else if( l2.l_type==F_UNLCK ){
458 zType = "UNLCK";
459 }else{
460 assert( 0 );
461 }
462 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
463 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
464 }
465 errno = savedErrno;
466 return s;
467}
468#define fcntl lockTrace
469#endif /* SQLITE_LOCK_TRACE */
470
471/*
472** The testThreadLockingBehavior() routine launches two separate
473** threads on this routine. This routine attempts to lock a file
474** descriptor then returns. The success or failure of that attempt
475** allows the testThreadLockingBehavior() procedure to determine
476** whether or not threads can override each others locks.
477*/
478static void *threadLockingTest(void *pArg){
479 struct threadTestData *pData = (struct threadTestData*)pArg;
480 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
481 return pArg;
482}
483
484/*
485** This procedure attempts to determine whether or not threads
486** can override each others locks then sets the
487** threadsOverrideEachOthersLocks variable appropriately.
488*/
489static void testThreadLockingBehavior(int fd_orig){
490 int fd;
491 struct threadTestData d[2];
492 pthread_t t[2];
493
494 fd = dup(fd_orig);
495 if( fd<0 ) return;
496 memset(d, 0, sizeof(d));
497 d[0].fd = fd;
498 d[0].lock.l_type = F_RDLCK;
499 d[0].lock.l_len = 1;
500 d[0].lock.l_start = 0;
501 d[0].lock.l_whence = SEEK_SET;
502 d[1] = d[0];
503 d[1].lock.l_type = F_WRLCK;
504 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
505 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
506 pthread_join(t[0], 0);
507 pthread_join(t[1], 0);
508 close(fd);
509 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
510}
511#endif /* SQLITE_THREADSAFE */
512
513/*
514** Release a lockInfo structure previously allocated by findLockInfo().
515*/
516static void releaseLockInfo(struct lockInfo *pLock){
517 if (pLock == NULL)
518 return;
519 pLock->nRef--;
520 if( pLock->nRef==0 ){
521 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
522 sqlite3_free(pLock);
523 }
524}
525
526/*
527** Release a openCnt structure previously allocated by findLockInfo().
528*/
529static void releaseOpenCnt(struct openCnt *pOpen){
530 if (pOpen == NULL)
531 return;
532 pOpen->nRef--;
533 if( pOpen->nRef==0 ){
534 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
535 free(pOpen->aPending);
536 sqlite3_free(pOpen);
537 }
538}
539
540#ifdef SQLITE_ENABLE_LOCKING_STYLE
541/*
542** Tests a byte-range locking query to see if byte range locks are
543** supported, if not we fall back to dotlockLockingStyle.
544*/
545static sqlite3LockingStyle sqlite3TestLockingStyle(
546 const char *filePath,
547 int fd
548){
549 /* test byte-range lock using fcntl */
550 struct flock lockInfo;
551
552 lockInfo.l_len = 1;
553 lockInfo.l_start = 0;
554 lockInfo.l_whence = SEEK_SET;
555 lockInfo.l_type = F_RDLCK;
556
557 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
558 return posixLockingStyle;
559 }
560
561 /* testing for flock can give false positives. So if if the above test
562 ** fails, then we fall back to using dot-lock style locking.
563 */
564 return dotlockLockingStyle;
565}
566
567/*
568** Examines the f_fstypename entry in the statfs structure as returned by
569** stat() for the file system hosting the database file, assigns the
570** appropriate locking style based on it's value. These values and
571** assignments are based on Darwin/OSX behavior and have not been tested on
572** other systems.
573*/
574static sqlite3LockingStyle sqlite3DetectLockingStyle(
575 const char *filePath,
576 int fd
577){
578
579#ifdef SQLITE_FIXED_LOCKING_STYLE
580 return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
581#else
582 struct statfs fsInfo;
583
584 if (statfs(filePath, &fsInfo) == -1)
585 return sqlite3TestLockingStyle(filePath, fd);
586
587 if (fsInfo.f_flags & MNT_RDONLY)
588 return noLockingStyle;
589
590 if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
591 (!strcmp(fsInfo.f_fstypename, "ufs")) )
592 return posixLockingStyle;
593
594 if(!strcmp(fsInfo.f_fstypename, "afpfs"))
595 return afpLockingStyle;
596
597 if(!strcmp(fsInfo.f_fstypename, "nfs"))
598 return sqlite3TestLockingStyle(filePath, fd);
599
600 if(!strcmp(fsInfo.f_fstypename, "smbfs"))
601 return flockLockingStyle;
602
603 if(!strcmp(fsInfo.f_fstypename, "msdos"))
604 return dotlockLockingStyle;
605
606 if(!strcmp(fsInfo.f_fstypename, "webdav"))
607 return unsupportedLockingStyle;
608
609 return sqlite3TestLockingStyle(filePath, fd);
610#endif /* SQLITE_FIXED_LOCKING_STYLE */
611}
612
613#endif /* SQLITE_ENABLE_LOCKING_STYLE */
614
615/*
616** Given a file descriptor, locate lockInfo and openCnt structures that
617** describes that file descriptor. Create new ones if necessary. The
618** return values might be uninitialized if an error occurs.
619**
620** Return the number of errors.
621*/
622static int findLockInfo(
623 int fd, /* The file descriptor used in the key */
624 struct lockInfo **ppLock, /* Return the lockInfo structure here */
625 struct openCnt **ppOpen /* Return the openCnt structure here */
626){
627 int rc;
628 struct lockKey key1;
629 struct openKey key2;
630 struct stat statbuf;
631 struct lockInfo *pLock;
632 struct openCnt *pOpen;
633 rc = fstat(fd, &statbuf);
634 if( rc!=0 ) return 1;
635
636 memset(&key1, 0, sizeof(key1));
637 key1.dev = statbuf.st_dev;
638 key1.ino = statbuf.st_ino;
639#if SQLITE_THREADSAFE
640 if( threadsOverrideEachOthersLocks<0 ){
641 testThreadLockingBehavior(fd);
642 }
643 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
644#endif
645 memset(&key2, 0, sizeof(key2));
646 key2.dev = statbuf.st_dev;
647 key2.ino = statbuf.st_ino;
648 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
649 if( pLock==0 ){
650 struct lockInfo *pOld;
651 pLock = sqlite3_malloc( sizeof(*pLock) );
652 if( pLock==0 ){
653 rc = 1;
654 goto exit_findlockinfo;
655 }
656 pLock->key = key1;
657 pLock->nRef = 1;
658 pLock->cnt = 0;
659 pLock->locktype = 0;
660 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
661 if( pOld!=0 ){
662 assert( pOld==pLock );
663 sqlite3_free(pLock);
664 rc = 1;
665 goto exit_findlockinfo;
666 }
667 }else{
668 pLock->nRef++;
669 }
670 *ppLock = pLock;
671 if( ppOpen!=0 ){
672 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
673 if( pOpen==0 ){
674 struct openCnt *pOld;
675 pOpen = sqlite3_malloc( sizeof(*pOpen) );
676 if( pOpen==0 ){
677 releaseLockInfo(pLock);
678 rc = 1;
679 goto exit_findlockinfo;
680 }
681 pOpen->key = key2;
682 pOpen->nRef = 1;
683 pOpen->nLock = 0;
684 pOpen->nPending = 0;
685 pOpen->aPending = 0;
686 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
687 if( pOld!=0 ){
688 assert( pOld==pOpen );
689 sqlite3_free(pOpen);
690 releaseLockInfo(pLock);
691 rc = 1;
692 goto exit_findlockinfo;
693 }
694 }else{
695 pOpen->nRef++;
696 }
697 *ppOpen = pOpen;
698 }
699
700exit_findlockinfo:
701 return rc;
702}
703
704#ifdef SQLITE_DEBUG
705/*
706** Helper function for printing out trace information from debugging
707** binaries. This returns the string represetation of the supplied
708** integer lock-type.
709*/
710static const char *locktypeName(int locktype){
711 switch( locktype ){
712 case NO_LOCK: return "NONE";
713 case SHARED_LOCK: return "SHARED";
714 case RESERVED_LOCK: return "RESERVED";
715 case PENDING_LOCK: return "PENDING";
716 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
717 }
718 return "ERROR";
719}
720#endif
721
722/*
723** If we are currently in a different thread than the thread that the
724** unixFile argument belongs to, then transfer ownership of the unixFile
725** over to the current thread.
726**
727** A unixFile is only owned by a thread on systems where one thread is
728** unable to override locks created by a different thread. RedHat9 is
729** an example of such a system.
730**
731** Ownership transfer is only allowed if the unixFile is currently unlocked.
732** If the unixFile is locked and an ownership is wrong, then return
733** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
734*/
735#if SQLITE_THREADSAFE
736static int transferOwnership(unixFile *pFile){
737 int rc;
738 pthread_t hSelf;
739 if( threadsOverrideEachOthersLocks ){
740 /* Ownership transfers not needed on this system */
741 return SQLITE_OK;
742 }
743 hSelf = pthread_self();
744 if( pthread_equal(pFile->tid, hSelf) ){
745 /* We are still in the same thread */
746 OSTRACE1("No-transfer, same thread\n");
747 return SQLITE_OK;
748 }
749 if( pFile->locktype!=NO_LOCK ){
750 /* We cannot change ownership while we are holding a lock! */
751 return SQLITE_MISUSE;
752 }
753 OSTRACE4("Transfer ownership of %d from %d to %d\n",
754 pFile->h, pFile->tid, hSelf);
755 pFile->tid = hSelf;
756 if (pFile->pLock != NULL) {
757 releaseLockInfo(pFile->pLock);
758 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
759 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
760 locktypeName(pFile->locktype),
761 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
762 return rc;
763 } else {
764 return SQLITE_OK;
765 }
766}
767#else
768 /* On single-threaded builds, ownership transfer is a no-op */
769# define transferOwnership(X) SQLITE_OK
770#endif
771
772/*
773** Seek to the offset passed as the second argument, then read cnt
774** bytes into pBuf. Return the number of bytes actually read.
775*/
776static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
777 int got;
778 i64 newOffset;
779 TIMER_START;
780#if defined(USE_PREAD)
781 got = pread(id->h, pBuf, cnt, offset);
782 SimulateIOError( got = -1 );
783#elif defined(USE_PREAD64)
784 got = pread64(id->h, pBuf, cnt, offset);
785 SimulateIOError( got = -1 );
786#else
787 newOffset = lseek(id->h, offset, SEEK_SET);
788 SimulateIOError( newOffset-- );
789 if( newOffset!=offset ){
790 return -1;
791 }
792 got = read(id->h, pBuf, cnt);
793#endif
794 TIMER_END;
795 OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
796 return got;
797}
798
799/*
800** Read data from a file into a buffer. Return SQLITE_OK if all
801** bytes were read successfully and SQLITE_IOERR if anything goes
802** wrong.
803*/
804static int unixRead(
805 sqlite3_file *id,
806 void *pBuf,
807 int amt,
808 sqlite3_int64 offset
809){
810 int got;
811 assert( id );
812 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
813 if( got==amt ){
814 return SQLITE_OK;
815 }else if( got<0 ){
816 return SQLITE_IOERR_READ;
817 }else{
818 memset(&((char*)pBuf)[got], 0, amt-got);
819 return SQLITE_IOERR_SHORT_READ;
820 }
821}
822
823/*
824** Seek to the offset in id->offset then read cnt bytes into pBuf.
825** Return the number of bytes actually read. Update the offset.
826*/
827static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
828 int got;
829 i64 newOffset;
830 TIMER_START;
831#if defined(USE_PREAD)
832 got = pwrite(id->h, pBuf, cnt, offset);
833#elif defined(USE_PREAD64)
834 got = pwrite64(id->h, pBuf, cnt, offset);
835#else
836 newOffset = lseek(id->h, offset, SEEK_SET);
837 if( newOffset!=offset ){
838 return -1;
839 }
840 got = write(id->h, pBuf, cnt);
841#endif
842 TIMER_END;
843 OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
844 return got;
845}
846
847
848/*
849** Write data from a buffer into a file. Return SQLITE_OK on success
850** or some other error code on failure.
851*/
852static int unixWrite(
853 sqlite3_file *id,
854 const void *pBuf,
855 int amt,
856 sqlite3_int64 offset
857){
858 int wrote = 0;
859 assert( id );
860 assert( amt>0 );
861 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
862 amt -= wrote;
863 offset += wrote;
864 pBuf = &((char*)pBuf)[wrote];
865 }
866 SimulateIOError(( wrote=(-1), amt=1 ));
867 SimulateDiskfullError(( wrote=0, amt=1 ));
868 if( amt>0 ){
869 if( wrote<0 ){
870 return SQLITE_IOERR_WRITE;
871 }else{
872 return SQLITE_FULL;
873 }
874 }
875 return SQLITE_OK;
876}
877
878#ifdef SQLITE_TEST
879/*
880** Count the number of fullsyncs and normal syncs. This is used to test
881** that syncs and fullsyncs are occuring at the right times.
882*/
883int sqlite3_sync_count = 0;
884int sqlite3_fullsync_count = 0;
885#endif
886
887/*
888** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
889** Otherwise use fsync() in its place.
890*/
891#ifndef HAVE_FDATASYNC
892# define fdatasync fsync
893#endif
894
895/*
896** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
897** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
898** only available on Mac OS X. But that could change.
899*/
900#ifdef F_FULLFSYNC
901# define HAVE_FULLFSYNC 1
902#else
903# define HAVE_FULLFSYNC 0
904#endif
905
906
907/*
908** The fsync() system call does not work as advertised on many
909** unix systems. The following procedure is an attempt to make
910** it work better.
911**
912** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
913** for testing when we want to run through the test suite quickly.
914** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
915** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
916** or power failure will likely corrupt the database file.
917*/
918static int full_fsync(int fd, int fullSync, int dataOnly){
919 int rc;
920
921 /* Record the number of times that we do a normal fsync() and
922 ** FULLSYNC. This is used during testing to verify that this procedure
923 ** gets called with the correct arguments.
924 */
925#ifdef SQLITE_TEST
926 if( fullSync ) sqlite3_fullsync_count++;
927 sqlite3_sync_count++;
928#endif
929
930 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
931 ** no-op
932 */
933#ifdef SQLITE_NO_SYNC
934 rc = SQLITE_OK;
935#else
936
937#if HAVE_FULLFSYNC
938 if( fullSync ){
939 rc = fcntl(fd, F_FULLFSYNC, 0);
940 }else{
941 rc = 1;
942 }
943 /* If the FULLFSYNC failed, fall back to attempting an fsync().
944 * It shouldn't be possible for fullfsync to fail on the local
945 * file system (on OSX), so failure indicates that FULLFSYNC
946 * isn't supported for this file system. So, attempt an fsync
947 * and (for now) ignore the overhead of a superfluous fcntl call.
948 * It'd be better to detect fullfsync support once and avoid
949 * the fcntl call every time sync is called.
950 */
951 if( rc ) rc = fsync(fd);
952
953#else
954 if( dataOnly ){
955 rc = fdatasync(fd);
956 }else{
957 rc = fsync(fd);
958 }
959#endif /* HAVE_FULLFSYNC */
960#endif /* defined(SQLITE_NO_SYNC) */
961
962 return rc;
963}
964
965/*
966** Make sure all writes to a particular file are committed to disk.
967**
968** If dataOnly==0 then both the file itself and its metadata (file
969** size, access time, etc) are synced. If dataOnly!=0 then only the
970** file data is synced.
971**
972** Under Unix, also make sure that the directory entry for the file
973** has been created by fsync-ing the directory that contains the file.
974** If we do not do this and we encounter a power failure, the directory
975** entry for the journal might not exist after we reboot. The next
976** SQLite to access the file will not know that the journal exists (because
977** the directory entry for the journal was never created) and the transaction
978** will not roll back - possibly leading to database corruption.
979*/
980static int unixSync(sqlite3_file *id, int flags){
981 int rc;
982 unixFile *pFile = (unixFile*)id;
983
984 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
985 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
986
987 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
988 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
989 || (flags&0x0F)==SQLITE_SYNC_FULL
990 );
991
992 assert( pFile );
993 OSTRACE2("SYNC %-3d\n", pFile->h);
994 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
995 SimulateIOError( rc=1 );
996 if( rc ){
997 return SQLITE_IOERR_FSYNC;
998 }
999 if( pFile->dirfd>=0 ){
1000 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
1001 HAVE_FULLFSYNC, isFullsync);
1002#ifndef SQLITE_DISABLE_DIRSYNC
1003 /* The directory sync is only attempted if full_fsync is
1004 ** turned off or unavailable. If a full_fsync occurred above,
1005 ** then the directory sync is superfluous.
1006 */
1007 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
1008 /*
1009 ** We have received multiple reports of fsync() returning
1010 ** errors when applied to directories on certain file systems.
1011 ** A failed directory sync is not a big deal. So it seems
1012 ** better to ignore the error. Ticket #1657
1013 */
1014 /* return SQLITE_IOERR; */
1015 }
1016#endif
1017 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1018 pFile->dirfd = -1; /* when we are done. */
1019 }
1020 return SQLITE_OK;
1021}
1022
1023/*
1024** Truncate an open file to a specified size
1025*/
1026static int unixTruncate(sqlite3_file *id, i64 nByte){
1027 int rc;
1028 assert( id );
1029 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
1030 SimulateIOError( rc=1 );
1031 if( rc ){
1032 return SQLITE_IOERR_TRUNCATE;
1033 }else{
1034 return SQLITE_OK;
1035 }
1036}
1037
1038/*
1039** Determine the current size of a file in bytes
1040*/
1041static int unixFileSize(sqlite3_file *id, i64 *pSize){
1042 int rc;
1043 struct stat buf;
1044 assert( id );
1045 rc = fstat(((unixFile*)id)->h, &buf);
1046 SimulateIOError( rc=1 );
1047 if( rc!=0 ){
1048 return SQLITE_IOERR_FSTAT;
1049 }
1050 *pSize = buf.st_size;
1051 return SQLITE_OK;
1052}
1053
1054/*
1055** This routine checks if there is a RESERVED lock held on the specified
1056** file by this or any other process. If such a lock is held, return
1057** non-zero. If the file is unlocked or holds only SHARED locks, then
1058** return zero.
1059*/
1060static int unixCheckReservedLock(sqlite3_file *id){
1061 int r = 0;
1062 unixFile *pFile = (unixFile*)id;
1063
1064 assert( pFile );
1065 enterMutex(); /* Because pFile->pLock is shared across threads */
1066
1067 /* Check if a thread in this process holds such a lock */
1068 if( pFile->pLock->locktype>SHARED_LOCK ){
1069 r = 1;
1070 }
1071
1072 /* Otherwise see if some other process holds it.
1073 */
1074 if( !r ){
1075 struct flock lock;
1076 lock.l_whence = SEEK_SET;
1077 lock.l_start = RESERVED_BYTE;
1078 lock.l_len = 1;
1079 lock.l_type = F_WRLCK;
1080 fcntl(pFile->h, F_GETLK, &lock);
1081 if( lock.l_type!=F_UNLCK ){
1082 r = 1;
1083 }
1084 }
1085
1086 leaveMutex();
1087 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
1088
1089 return r;
1090}
1091
1092/*
1093** Lock the file with the lock specified by parameter locktype - one
1094** of the following:
1095**
1096** (1) SHARED_LOCK
1097** (2) RESERVED_LOCK
1098** (3) PENDING_LOCK
1099** (4) EXCLUSIVE_LOCK
1100**
1101** Sometimes when requesting one lock state, additional lock states
1102** are inserted in between. The locking might fail on one of the later
1103** transitions leaving the lock state different from what it started but
1104** still short of its goal. The following chart shows the allowed
1105** transitions and the inserted intermediate states:
1106**
1107** UNLOCKED -> SHARED
1108** SHARED -> RESERVED
1109** SHARED -> (PENDING) -> EXCLUSIVE
1110** RESERVED -> (PENDING) -> EXCLUSIVE
1111** PENDING -> EXCLUSIVE
1112**
1113** This routine will only increase a lock. Use the sqlite3OsUnlock()
1114** routine to lower a locking level.
1115*/
1116static int unixLock(sqlite3_file *id, int locktype){
1117 /* The following describes the implementation of the various locks and
1118 ** lock transitions in terms of the POSIX advisory shared and exclusive
1119 ** lock primitives (called read-locks and write-locks below, to avoid
1120 ** confusion with SQLite lock names). The algorithms are complicated
1121 ** slightly in order to be compatible with windows systems simultaneously
1122 ** accessing the same database file, in case that is ever required.
1123 **
1124 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1125 ** byte', each single bytes at well known offsets, and the 'shared byte
1126 ** range', a range of 510 bytes at a well known offset.
1127 **
1128 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1129 ** byte'. If this is successful, a random byte from the 'shared byte
1130 ** range' is read-locked and the lock on the 'pending byte' released.
1131 **
1132 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1133 ** A RESERVED lock is implemented by grabbing a write-lock on the
1134 ** 'reserved byte'.
1135 **
1136 ** A process may only obtain a PENDING lock after it has obtained a
1137 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1138 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1139 ** obtained, but existing SHARED locks are allowed to persist. A process
1140 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1141 ** This property is used by the algorithm for rolling back a journal file
1142 ** after a crash.
1143 **
1144 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1145 ** implemented by obtaining a write-lock on the entire 'shared byte
1146 ** range'. Since all other locks require a read-lock on one of the bytes
1147 ** within this range, this ensures that no other locks are held on the
1148 ** database.
1149 **
1150 ** The reason a single byte cannot be used instead of the 'shared byte
1151 ** range' is that some versions of windows do not support read-locks. By
1152 ** locking a random byte from a range, concurrent SHARED locks may exist
1153 ** even if the locking primitive used is always a write-lock.
1154 */
1155 int rc = SQLITE_OK;
1156 unixFile *pFile = (unixFile*)id;
1157 struct lockInfo *pLock = pFile->pLock;
1158 struct flock lock;
1159 int s;
1160
1161 assert( pFile );
1162 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
1163 locktypeName(locktype), locktypeName(pFile->locktype),
1164 locktypeName(pLock->locktype), pLock->cnt , getpid());
1165
1166 /* If there is already a lock of this type or more restrictive on the
1167 ** unixFile, do nothing. Don't use the end_lock: exit path, as
1168 ** enterMutex() hasn't been called yet.
1169 */
1170 if( pFile->locktype>=locktype ){
1171 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
1172 locktypeName(locktype));
1173 return SQLITE_OK;
1174 }
1175
1176 /* Make sure the locking sequence is correct
1177 */
1178 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1179 assert( locktype!=PENDING_LOCK );
1180 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1181
1182 /* This mutex is needed because pFile->pLock is shared across threads
1183 */
1184 enterMutex();
1185
1186 /* Make sure the current thread owns the pFile.
1187 */
1188 rc = transferOwnership(pFile);
1189 if( rc!=SQLITE_OK ){
1190 leaveMutex();
1191 return rc;
1192 }
1193 pLock = pFile->pLock;
1194
1195 /* If some thread using this PID has a lock via a different unixFile*
1196 ** handle that precludes the requested lock, return BUSY.
1197 */
1198 if( (pFile->locktype!=pLock->locktype &&
1199 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
1200 ){
1201 rc = SQLITE_BUSY;
1202 goto end_lock;
1203 }
1204
1205 /* If a SHARED lock is requested, and some thread using this PID already
1206 ** has a SHARED or RESERVED lock, then increment reference counts and
1207 ** return SQLITE_OK.
1208 */
1209 if( locktype==SHARED_LOCK &&
1210 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1211 assert( locktype==SHARED_LOCK );
1212 assert( pFile->locktype==0 );
1213 assert( pLock->cnt>0 );
1214 pFile->locktype = SHARED_LOCK;
1215 pLock->cnt++;
1216 pFile->pOpen->nLock++;
1217 goto end_lock;
1218 }
1219
1220 lock.l_len = 1L;
1221
1222 lock.l_whence = SEEK_SET;
1223
1224 /* A PENDING lock is needed before acquiring a SHARED lock and before
1225 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1226 ** be released.
1227 */
1228 if( locktype==SHARED_LOCK
1229 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1230 ){
1231 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
1232 lock.l_start = PENDING_BYTE;
1233 s = fcntl(pFile->h, F_SETLK, &lock);
1234 if( s==(-1) ){
1235 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1236 goto end_lock;
1237 }
1238 }
1239
1240
1241 /* If control gets to this point, then actually go ahead and make
1242 ** operating system calls for the specified lock.
1243 */
1244 if( locktype==SHARED_LOCK ){
1245 assert( pLock->cnt==0 );
1246 assert( pLock->locktype==0 );
1247
1248 /* Now get the read-lock */
1249 lock.l_start = SHARED_FIRST;
1250 lock.l_len = SHARED_SIZE;
1251 s = fcntl(pFile->h, F_SETLK, &lock);
1252
1253 /* Drop the temporary PENDING lock */
1254 lock.l_start = PENDING_BYTE;
1255 lock.l_len = 1L;
1256 lock.l_type = F_UNLCK;
1257 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
1258 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1259 goto end_lock;
1260 }
1261 if( s==(-1) ){
1262 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1263 }else{
1264 pFile->locktype = SHARED_LOCK;
1265 pFile->pOpen->nLock++;
1266 pLock->cnt = 1;
1267 }
1268 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1269 /* We are trying for an exclusive lock but another thread in this
1270 ** same process is still holding a shared lock. */
1271 rc = SQLITE_BUSY;
1272 }else{
1273 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1274 ** assumed that there is a SHARED or greater lock on the file
1275 ** already.
1276 */
1277 assert( 0!=pFile->locktype );
1278 lock.l_type = F_WRLCK;
1279 switch( locktype ){
1280 case RESERVED_LOCK:
1281 lock.l_start = RESERVED_BYTE;
1282 break;
1283 case EXCLUSIVE_LOCK:
1284 lock.l_start = SHARED_FIRST;
1285 lock.l_len = SHARED_SIZE;
1286 break;
1287 default:
1288 assert(0);
1289 }
1290 s = fcntl(pFile->h, F_SETLK, &lock);
1291 if( s==(-1) ){
1292 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1293 }
1294 }
1295
1296 if( rc==SQLITE_OK ){
1297 pFile->locktype = locktype;
1298 pLock->locktype = locktype;
1299 }else if( locktype==EXCLUSIVE_LOCK ){
1300 pFile->locktype = PENDING_LOCK;
1301 pLock->locktype = PENDING_LOCK;
1302 }
1303
1304end_lock:
1305 leaveMutex();
1306 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
1307 rc==SQLITE_OK ? "ok" : "failed");
1308 return rc;
1309}
1310
1311/*
1312** Lower the locking level on file descriptor pFile to locktype. locktype
1313** must be either NO_LOCK or SHARED_LOCK.
1314**
1315** If the locking level of the file descriptor is already at or below
1316** the requested locking level, this routine is a no-op.
1317*/
1318static int unixUnlock(sqlite3_file *id, int locktype){
1319 struct lockInfo *pLock;
1320 struct flock lock;
1321 int rc = SQLITE_OK;
1322 unixFile *pFile = (unixFile*)id;
1323
1324 assert( pFile );
1325 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
1326 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
1327
1328 assert( locktype<=SHARED_LOCK );
1329 if( pFile->locktype<=locktype ){
1330 return SQLITE_OK;
1331 }
1332 if( CHECK_THREADID(pFile) ){
1333 return SQLITE_MISUSE;
1334 }
1335 enterMutex();
1336 pLock = pFile->pLock;
1337 assert( pLock->cnt!=0 );
1338 if( pFile->locktype>SHARED_LOCK ){
1339 assert( pLock->locktype==pFile->locktype );
1340 if( locktype==SHARED_LOCK ){
1341 lock.l_type = F_RDLCK;
1342 lock.l_whence = SEEK_SET;
1343 lock.l_start = SHARED_FIRST;
1344 lock.l_len = SHARED_SIZE;
1345 if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
1346 /* This should never happen */
1347 rc = SQLITE_IOERR_RDLOCK;
1348 }
1349 }
1350 lock.l_type = F_UNLCK;
1351 lock.l_whence = SEEK_SET;
1352 lock.l_start = PENDING_BYTE;
1353 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
1354 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
1355 pLock->locktype = SHARED_LOCK;
1356 }else{
1357 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1358 }
1359 }
1360 if( locktype==NO_LOCK ){
1361 struct openCnt *pOpen;
1362
1363 /* Decrement the shared lock counter. Release the lock using an
1364 ** OS call only when all threads in this same process have released
1365 ** the lock.
1366 */
1367 pLock->cnt--;
1368 if( pLock->cnt==0 ){
1369 lock.l_type = F_UNLCK;
1370 lock.l_whence = SEEK_SET;
1371 lock.l_start = lock.l_len = 0L;
1372 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
1373 pLock->locktype = NO_LOCK;
1374 }else{
1375 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1376 }
1377 }
1378
1379 /* Decrement the count of locks against this same file. When the
1380 ** count reaches zero, close any other file descriptors whose close
1381 ** was deferred because of outstanding locks.
1382 */
1383 pOpen = pFile->pOpen;
1384 pOpen->nLock--;
1385 assert( pOpen->nLock>=0 );
1386 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1387 int i;
1388 for(i=0; i<pOpen->nPending; i++){
1389 close(pOpen->aPending[i]);
1390 }
1391 free(pOpen->aPending);
1392 pOpen->nPending = 0;
1393 pOpen->aPending = 0;
1394 }
1395 }
1396 leaveMutex();
1397 pFile->locktype = locktype;
1398 return rc;
1399}
1400
1401/*
1402** Close a file.
1403*/
1404static int unixClose(sqlite3_file *id){
1405 unixFile *pFile = (unixFile *)id;
1406 if( !pFile ) return SQLITE_OK;
1407 unixUnlock(id, NO_LOCK);
1408 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1409 pFile->dirfd = -1;
1410 enterMutex();
1411
1412 if( pFile->pOpen->nLock ){
1413 /* If there are outstanding locks, do not actually close the file just
1414 ** yet because that would clear those locks. Instead, add the file
1415 ** descriptor to pOpen->aPending. It will be automatically closed when
1416 ** the last lock is cleared.
1417 */
1418 int *aNew;
1419 struct openCnt *pOpen = pFile->pOpen;
1420 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
1421 if( aNew==0 ){
1422 /* If a malloc fails, just leak the file descriptor */
1423 }else{
1424 pOpen->aPending = aNew;
1425 pOpen->aPending[pOpen->nPending] = pFile->h;
1426 pOpen->nPending++;
1427 }
1428 }else{
1429 /* There are no outstanding locks so we can close the file immediately */
1430 close(pFile->h);
1431 }
1432 releaseLockInfo(pFile->pLock);
1433 releaseOpenCnt(pFile->pOpen);
1434
1435 leaveMutex();
1436 OSTRACE2("CLOSE %-3d\n", pFile->h);
1437 OpenCounter(-1);
1438 memset(pFile, 0, sizeof(unixFile));
1439 return SQLITE_OK;
1440}
1441
1442
1443#ifdef SQLITE_ENABLE_LOCKING_STYLE
1444#pragma mark AFP Support
1445
1446/*
1447 ** The afpLockingContext structure contains all afp lock specific state
1448 */
1449typedef struct afpLockingContext afpLockingContext;
1450struct afpLockingContext {
1451 unsigned long long sharedLockByte;
1452 char *filePath;
1453};
1454
1455struct ByteRangeLockPB2
1456{
1457 unsigned long long offset; /* offset to first byte to lock */
1458 unsigned long long length; /* nbr of bytes to lock */
1459 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1460 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1461 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1462 int fd; /* file desc to assoc this lock with */
1463};
1464
1465#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
1466
1467/*
1468** Return 0 on success, 1 on failure. To match the behavior of the
1469** normal posix file locking (used in unixLock for example), we should
1470** provide 'richer' return codes - specifically to differentiate between
1471** 'file busy' and 'file system error' results.
1472*/
1473static int _AFPFSSetLock(
1474 const char *path,
1475 int fd,
1476 unsigned long long offset,
1477 unsigned long long length,
1478 int setLockFlag
1479){
1480 struct ByteRangeLockPB2 pb;
1481 int err;
1482
1483 pb.unLockFlag = setLockFlag ? 0 : 1;
1484 pb.startEndFlag = 0;
1485 pb.offset = offset;
1486 pb.length = length;
1487 pb.fd = fd;
1488 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
1489 (setLockFlag?"ON":"OFF"), fd, offset, length);
1490 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1491 if ( err==-1 ) {
1492 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
1493 strerror(errno));
1494 return 1; /* error */
1495 } else {
1496 return 0;
1497 }
1498}
1499
1500/*
1501 ** This routine checks if there is a RESERVED lock held on the specified
1502 ** file by this or any other process. If such a lock is held, return
1503 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1504 ** return zero.
1505 */
1506static int afpUnixCheckReservedLock(sqlite3_file *id){
1507 int r = 0;
1508 unixFile *pFile = (unixFile*)id;
1509
1510 assert( pFile );
1511 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1512
1513 /* Check if a thread in this process holds such a lock */
1514 if( pFile->locktype>SHARED_LOCK ){
1515 r = 1;
1516 }
1517
1518 /* Otherwise see if some other process holds it.
1519 */
1520 if ( !r ) {
1521 /* lock the byte */
1522 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1523 if (failed) {
1524 /* if we failed to get the lock then someone else must have it */
1525 r = 1;
1526 } else {
1527 /* if we succeeded in taking the reserved lock, unlock it to restore
1528 ** the original state */
1529 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1530 }
1531 }
1532 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
1533
1534 return r;
1535}
1536
1537/* AFP-style locking following the behavior of unixLock, see the unixLock
1538** function comments for details of lock management. */
1539static int afpUnixLock(sqlite3_file *id, int locktype)
1540{
1541 int rc = SQLITE_OK;
1542 unixFile *pFile = (unixFile*)id;
1543 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1544 int gotPendingLock = 0;
1545
1546 assert( pFile );
1547 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
1548 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1549 /* If there is already a lock of this type or more restrictive on the
1550 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
1551 ** enterMutex() hasn't been called yet.
1552 */
1553 if( pFile->locktype>=locktype ){
1554 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
1555 locktypeName(locktype));
1556 return SQLITE_OK;
1557 }
1558
1559 /* Make sure the locking sequence is correct
1560 */
1561 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1562 assert( locktype!=PENDING_LOCK );
1563 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1564
1565 /* This mutex is needed because pFile->pLock is shared across threads
1566 */
1567 enterMutex();
1568
1569 /* Make sure the current thread owns the pFile.
1570 */
1571 rc = transferOwnership(pFile);
1572 if( rc!=SQLITE_OK ){
1573 leaveMutex();
1574 return rc;
1575 }
1576
1577 /* A PENDING lock is needed before acquiring a SHARED lock and before
1578 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1579 ** be released.
1580 */
1581 if( locktype==SHARED_LOCK
1582 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1583 ){
1584 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1585 PENDING_BYTE, 1, 1);
1586 if (failed) {
1587 rc = SQLITE_BUSY;
1588 goto afp_end_lock;
1589 }
1590 }
1591
1592 /* If control gets to this point, then actually go ahead and make
1593 ** operating system calls for the specified lock.
1594 */
1595 if( locktype==SHARED_LOCK ){
1596 int lk, failed;
1597 int tries = 0;
1598
1599 /* Now get the read-lock */
1600 /* note that the quality of the randomness doesn't matter that much */
1601 lk = random();
1602 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1603 failed = _AFPFSSetLock(context->filePath, pFile->h,
1604 SHARED_FIRST+context->sharedLockByte, 1, 1);
1605
1606 /* Drop the temporary PENDING lock */
1607 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1608 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1609 goto afp_end_lock;
1610 }
1611
1612 if( failed ){
1613 rc = SQLITE_BUSY;
1614 } else {
1615 pFile->locktype = SHARED_LOCK;
1616 }
1617 }else{
1618 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1619 ** assumed that there is a SHARED or greater lock on the file
1620 ** already.
1621 */
1622 int failed = 0;
1623 assert( 0!=pFile->locktype );
1624 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1625 /* Acquire a RESERVED lock */
1626 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1627 }
1628 if (!failed && locktype == EXCLUSIVE_LOCK) {
1629 /* Acquire an EXCLUSIVE lock */
1630
1631 /* Remove the shared lock before trying the range. we'll need to
1632 ** reestablish the shared lock if we can't get the afpUnixUnlock
1633 */
1634 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1635 context->sharedLockByte, 1, 0)) {
1636 /* now attemmpt to get the exclusive lock range */
1637 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1638 SHARED_SIZE, 1);
1639 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1640 context->sharedLockByte, 1, 1)) {
1641 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1642 }
1643 } else {
1644 /* */
1645 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1646 }
1647 }
1648 if( failed && rc == SQLITE_OK){
1649 rc = SQLITE_BUSY;
1650 }
1651 }
1652
1653 if( rc==SQLITE_OK ){
1654 pFile->locktype = locktype;
1655 }else if( locktype==EXCLUSIVE_LOCK ){
1656 pFile->locktype = PENDING_LOCK;
1657 }
1658
1659afp_end_lock:
1660 leaveMutex();
1661 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
1662 rc==SQLITE_OK ? "ok" : "failed");
1663 return rc;
1664}
1665
1666/*
1667 ** Lower the locking level on file descriptor pFile to locktype. locktype
1668 ** must be either NO_LOCK or SHARED_LOCK.
1669 **
1670 ** If the locking level of the file descriptor is already at or below
1671 ** the requested locking level, this routine is a no-op.
1672 */
1673static int afpUnixUnlock(sqlite3_file *id, int locktype) {
1674 struct flock lock;
1675 int rc = SQLITE_OK;
1676 unixFile *pFile = (unixFile*)id;
1677 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1678
1679 assert( pFile );
1680 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1681 pFile->locktype, getpid());
1682
1683 assert( locktype<=SHARED_LOCK );
1684 if( pFile->locktype<=locktype ){
1685 return SQLITE_OK;
1686 }
1687 if( CHECK_THREADID(pFile) ){
1688 return SQLITE_MISUSE;
1689 }
1690 enterMutex();
1691 if( pFile->locktype>SHARED_LOCK ){
1692 if( locktype==SHARED_LOCK ){
1693 int failed = 0;
1694
1695 /* unlock the exclusive range - then re-establish the shared lock */
1696 if (pFile->locktype==EXCLUSIVE_LOCK) {
1697 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1698 SHARED_SIZE, 0);
1699 if (!failed) {
1700 /* successfully removed the exclusive lock */
1701 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1702 context->sharedLockByte, 1, 1)) {
1703 /* failed to re-establish our shared lock */
1704 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1705 }
1706 } else {
1707 /* This should never happen - failed to unlock the exclusive range */
1708 rc = SQLITE_IOERR_UNLOCK;
1709 }
1710 }
1711 }
1712 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1713 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1714 /* failed to release the pending lock */
1715 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1716 }
1717 }
1718 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1719 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1720 /* failed to release the reserved lock */
1721 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1722 }
1723 }
1724 }
1725 if( locktype==NO_LOCK ){
1726 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1727 SHARED_FIRST + context->sharedLockByte, 1, 0);
1728 if (failed) {
1729 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1730 }
1731 }
1732 if (rc == SQLITE_OK)
1733 pFile->locktype = locktype;
1734 leaveMutex();
1735 return rc;
1736}
1737
1738/*
1739 ** Close a file & cleanup AFP specific locking context
1740 */
1741static int afpUnixClose(sqlite3_file *id) {
1742 unixFile *pFile = (unixFile*)pId;
1743
1744 if( !pFile ) return SQLITE_OK;
1745 afpUnixUnlock(*pId, NO_LOCK);
1746 /* free the AFP locking structure */
1747 if (pFile->lockingContext != NULL) {
1748 if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
1749 sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
1750 sqlite3_free(pFile->lockingContext);
1751 }
1752
1753 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1754 pFile->dirfd = -1;
1755 close(pFile->h);
1756 OSTRACE2("CLOSE %-3d\n", pFile->h);
1757 OpenCounter(-1);
1758 return SQLITE_OK;
1759}
1760
1761
1762#pragma mark flock() style locking
1763
1764/*
1765 ** The flockLockingContext is not used
1766 */
1767typedef void flockLockingContext;
1768
1769static int flockUnixCheckReservedLock(sqlite3_file *id) {
1770 unixFile *pFile = (unixFile*)id;
1771
1772 if (pFile->locktype == RESERVED_LOCK) {
1773 return 1; /* already have a reserved lock */
1774 } else {
1775 /* attempt to get the lock */
1776 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1777 if (!rc) {
1778 /* got the lock, unlock it */
1779 flock(pFile->h, LOCK_UN);
1780 return 0; /* no one has it reserved */
1781 }
1782 return 1; /* someone else might have it reserved */
1783 }
1784}
1785
1786static int flockUnixLock(sqlite3_file *id, int locktype) {
1787 unixFile *pFile = (unixFile*)id;
1788
1789 /* if we already have a lock, it is exclusive.
1790 ** Just adjust level and punt on outta here. */
1791 if (pFile->locktype > NO_LOCK) {
1792 pFile->locktype = locktype;
1793 return SQLITE_OK;
1794 }
1795
1796 /* grab an exclusive lock */
1797 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1798 if (rc) {
1799 /* didn't get, must be busy */
1800 return SQLITE_BUSY;
1801 } else {
1802 /* got it, set the type and return ok */
1803 pFile->locktype = locktype;
1804 return SQLITE_OK;
1805 }
1806}
1807
1808static int flockUnixUnlock(sqlite3_file *id, int locktype) {
1809 unixFile *pFile = (unixFile*)id;
1810
1811 assert( locktype<=SHARED_LOCK );
1812
1813 /* no-op if possible */
1814 if( pFile->locktype==locktype ){
1815 return SQLITE_OK;
1816 }
1817
1818 /* shared can just be set because we always have an exclusive */
1819 if (locktype==SHARED_LOCK) {
1820 pFile->locktype = locktype;
1821 return SQLITE_OK;
1822 }
1823
1824 /* no, really, unlock. */
1825 int rc = flock(pFile->h, LOCK_UN);
1826 if (rc)
1827 return SQLITE_IOERR_UNLOCK;
1828 else {
1829 pFile->locktype = NO_LOCK;
1830 return SQLITE_OK;
1831 }
1832}
1833
1834/*
1835 ** Close a file.
1836 */
1837static int flockUnixClose(sqlite3_file *pId) {
1838 unixFile *pFile = (unixFile*)*pId;
1839
1840 if( !pFile ) return SQLITE_OK;
1841 flockUnixUnlock(*pId, NO_LOCK);
1842
1843 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1844 pFile->dirfd = -1;
1845 enterMutex();
1846
1847 close(pFile->h);
1848 leaveMutex();
1849 OSTRACE2("CLOSE %-3d\n", pFile->h);
1850 OpenCounter(-1);
1851 return SQLITE_OK;
1852}
1853
1854#pragma mark Old-School .lock file based locking
1855
1856/*
1857 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
1858 ** specific state
1859 */
1860typedef struct dotlockLockingContext dotlockLockingContext;
1861struct dotlockLockingContext {
1862 char *lockPath;
1863};
1864
1865
1866static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
1867 unixFile *pFile = (unixFile*)id;
1868 dotlockLockingContext *context =
1869 (dotlockLockingContext *) pFile->lockingContext;
1870
1871 if (pFile->locktype == RESERVED_LOCK) {
1872 return 1; /* already have a reserved lock */
1873 } else {
1874 struct stat statBuf;
1875 if (lstat(context->lockPath,&statBuf) == 0)
1876 /* file exists, someone else has the lock */
1877 return 1;
1878 else
1879 /* file does not exist, we could have it if we want it */
1880 return 0;
1881 }
1882}
1883
1884static int dotlockUnixLock(sqlite3_file *id, int locktype) {
1885 unixFile *pFile = (unixFile*)id;
1886 dotlockLockingContext *context =
1887 (dotlockLockingContext *) pFile->lockingContext;
1888
1889 /* if we already have a lock, it is exclusive.
1890 ** Just adjust level and punt on outta here. */
1891 if (pFile->locktype > NO_LOCK) {
1892 pFile->locktype = locktype;
1893
1894 /* Always update the timestamp on the old file */
1895 utimes(context->lockPath,NULL);
1896 return SQLITE_OK;
1897 }
1898
1899 /* check to see if lock file already exists */
1900 struct stat statBuf;
1901 if (lstat(context->lockPath,&statBuf) == 0){
1902 return SQLITE_BUSY; /* it does, busy */
1903 }
1904
1905 /* grab an exclusive lock */
1906 int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
1907 if (fd < 0) {
1908 /* failed to open/create the file, someone else may have stolen the lock */
1909 return SQLITE_BUSY;
1910 }
1911 close(fd);
1912
1913 /* got it, set the type and return ok */
1914 pFile->locktype = locktype;
1915 return SQLITE_OK;
1916}
1917
1918static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
1919 unixFile *pFile = (unixFile*)id;
1920 dotlockLockingContext *context =
1921 (dotlockLockingContext *) pFile->lockingContext;
1922
1923 assert( locktype<=SHARED_LOCK );
1924
1925 /* no-op if possible */
1926 if( pFile->locktype==locktype ){
1927 return SQLITE_OK;
1928 }
1929
1930 /* shared can just be set because we always have an exclusive */
1931 if (locktype==SHARED_LOCK) {
1932 pFile->locktype = locktype;
1933 return SQLITE_OK;
1934 }
1935
1936 /* no, really, unlock. */
1937 unlink(context->lockPath);
1938 pFile->locktype = NO_LOCK;
1939 return SQLITE_OK;
1940}
1941
1942/*
1943 ** Close a file.
1944 */
1945static int dotlockUnixClose(sqlite3_file *id) {
1946 unixFile *pFile = (unixFile*)id;
1947
1948 if( !pFile ) return SQLITE_OK;
1949 dotlockUnixUnlock(*pId, NO_LOCK);
1950 /* free the dotlock locking structure */
1951 if (pFile->lockingContext != NULL) {
1952 if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
1953 sqlite3_free( ( (dotlockLockingContext *)
1954 pFile->lockingContext)->lockPath);
1955 sqlite3_free(pFile->lockingContext);
1956 }
1957
1958 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1959 pFile->dirfd = -1;
1960 enterMutex();
1961
1962 close(pFile->h);
1963
1964 leaveMutex();
1965 OSTRACE2("CLOSE %-3d\n", pFile->h);
1966 OpenCounter(-1);
1967 return SQLITE_OK;
1968}
1969
1970
1971#pragma mark No locking
1972
1973/*
1974 ** The nolockLockingContext is void
1975 */
1976typedef void nolockLockingContext;
1977
1978static int nolockUnixCheckReservedLock(sqlite3_file *id) {
1979 return 0;
1980}
1981
1982static int nolockUnixLock(sqlite3_file *id, int locktype) {
1983 return SQLITE_OK;
1984}
1985
1986static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
1987 return SQLITE_OK;
1988}
1989
1990/*
1991 ** Close a file.
1992 */
1993static int nolockUnixClose(sqlite3_file *id) {
1994 unixFile *pFile = (unixFile*)id;
1995
1996 if( !pFile ) return SQLITE_OK;
1997 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1998 pFile->dirfd = -1;
1999 enterMutex();
2000
2001 close(pFile->h);
2002
2003 leaveMutex();
2004 OSTRACE2("CLOSE %-3d\n", pFile->h);
2005 OpenCounter(-1);
2006 return SQLITE_OK;
2007}
2008
2009#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2010
2011
2012/*
2013** Information and control of an open file handle.
2014*/
2015static int unixFileControl(sqlite3_file *id, int op, void *pArg){
2016 switch( op ){
2017 case SQLITE_FCNTL_LOCKSTATE: {
2018 *(int*)pArg = ((unixFile*)id)->locktype;
2019 return SQLITE_OK;
2020 }
2021 }
2022 return SQLITE_ERROR;
2023}
2024
2025/*
2026** Return the sector size in bytes of the underlying block device for
2027** the specified file. This is almost always 512 bytes, but may be
2028** larger for some devices.
2029**
2030** SQLite code assumes this function cannot fail. It also assumes that
2031** if two files are created in the same file-system directory (i.e.
2032** a database and it's journal file) that the sector size will be the
2033** same for both.
2034*/
2035static int unixSectorSize(sqlite3_file *id){
2036 return SQLITE_DEFAULT_SECTOR_SIZE;
2037}
2038
2039/*
2040** Return the device characteristics for the file. This is always 0.
2041*/
2042static int unixDeviceCharacteristics(sqlite3_file *id){
2043 return 0;
2044}
2045
2046/*
2047** This vector defines all the methods that can operate on an sqlite3_file
2048** for unix.
2049*/
2050static const sqlite3_io_methods sqlite3UnixIoMethod = {
2051 1, /* iVersion */
2052 unixClose,
2053 unixRead,
2054 unixWrite,
2055 unixTruncate,
2056 unixSync,
2057 unixFileSize,
2058 unixLock,
2059 unixUnlock,
2060 unixCheckReservedLock,
2061 unixFileControl,
2062 unixSectorSize,
2063 unixDeviceCharacteristics
2064};
2065
2066#ifdef SQLITE_ENABLE_LOCKING_STYLE
2067/*
2068** This vector defines all the methods that can operate on an sqlite3_file
2069** for unix with AFP style file locking.
2070*/
2071static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
2072 1, /* iVersion */
2073 unixClose,
2074 unixRead,
2075 unixWrite,
2076 unixTruncate,
2077 unixSync,
2078 unixFileSize,
2079 afpUnixLock,
2080 afpUnixUnlock,
2081 afpUnixCheckReservedLock,
2082 unixFileControl,
2083 unixSectorSize,
2084 unixDeviceCharacteristics
2085};
2086
2087/*
2088** This vector defines all the methods that can operate on an sqlite3_file
2089** for unix with flock() style file locking.
2090*/
2091static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
2092 1, /* iVersion */
2093 flockUnixClose,
2094 unixRead,
2095 unixWrite,
2096 unixTruncate,
2097 unixSync,
2098 unixFileSize,
2099 flockUnixLock,
2100 flockUnixUnlock,
2101 flockUnixCheckReservedLock,
2102 unixFileControl,
2103 unixSectorSize,
2104 unixDeviceCharacteristics
2105};
2106
2107/*
2108** This vector defines all the methods that can operate on an sqlite3_file
2109** for unix with dotlock style file locking.
2110*/
2111static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
2112 1, /* iVersion */
2113 dotlockUnixClose,
2114 unixRead,
2115 unixWrite,
2116 unixTruncate,
2117 unixSync,
2118 unixFileSize,
2119 dotlockUnixLock,
2120 dotlockUnixUnlock,
2121 dotlockUnixCheckReservedLock,
2122 unixFileControl,
2123 unixSectorSize,
2124 unixDeviceCharacteristics
2125};
2126
2127/*
2128** This vector defines all the methods that can operate on an sqlite3_file
2129** for unix with dotlock style file locking.
2130*/
2131static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
2132 1, /* iVersion */
2133 nolockUnixClose,
2134 unixRead,
2135 unixWrite,
2136 unixTruncate,
2137 unixSync,
2138 unixFileSize,
2139 nolockUnixLock,
2140 nolockUnixUnlock,
2141 nolockUnixCheckReservedLock,
2142 unixFileControl,
2143 unixSectorSize,
2144 unixDeviceCharacteristics
2145};
2146
2147#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2148
2149/*
2150** Allocate memory for a new unixFile and initialize that unixFile.
2151** Write a pointer to the new unixFile into *pId.
2152** If we run out of memory, close the file and return an error.
2153*/
2154#ifdef SQLITE_ENABLE_LOCKING_STYLE
2155/*
2156** When locking extensions are enabled, the filepath and locking style
2157** are needed to determine the unixFile pMethod to use for locking operations.
2158** The locking-style specific lockingContext data structure is created
2159** and assigned here also.
2160*/
2161static int fillInUnixFile(
2162 int h, /* Open file descriptor of file being opened */
2163 int dirfd, /* Directory file descriptor */
2164 sqlite3_file *pId, /* Write completed initialization here */
2165 const char *zFilename, /* Name of the file being opened */
2166){
2167 sqlite3LockingStyle lockingStyle;
2168 unixFile *pNew = (unixFile *)pId;
2169 int rc;
2170
2171 memset(pNew, 0, sizeof(unixFile));
2172 lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
2173 if ( lockingStyle == posixLockingStyle ) {
2174 enterMutex();
2175 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2176 leaveMutex();
2177 if( rc ){
2178 close(h);
2179 unlink(zFilename);
2180 return SQLITE_NOMEM;
2181 }
2182 } else {
2183 /* pLock and pOpen are only used for posix advisory locking */
2184 pNew->pLock = NULL;
2185 pNew->pOpen = NULL;
2186 }
2187 pNew->dirfd = -1;
2188 pNew->h = h;
2189 SET_THREADID(pNew);
2190 pNew = sqlite3_malloc( sizeof(unixFile) );
2191 if( pNew==0 ){
2192 close(h);
2193 enterMutex();
2194 releaseLockInfo(pNew->pLock);
2195 releaseOpenCnt(pNew->pOpen);
2196 leaveMutex();
2197 return SQLITE_NOMEM;
2198 }else{
2199 switch(lockingStyle) {
2200 case afpLockingStyle: {
2201 /* afp locking uses the file path so it needs to be included in
2202 ** the afpLockingContext */
2203 int nFilename;
2204 pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
2205 pNew->lockingContext =
2206 sqlite3_malloc(sizeof(afpLockingContext));
2207 nFilename = strlen(zFilename)+1;
2208 ((afpLockingContext *)pNew->lockingContext)->filePath =
2209 sqlite3_malloc(nFilename);
2210 memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
2211 zFilename, nFilename);
2212 srandomdev();
2213 break;
2214 }
2215 case flockLockingStyle:
2216 /* flock locking doesn't need additional lockingContext information */
2217 pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
2218 break;
2219 case dotlockLockingStyle: {
2220 /* dotlock locking uses the file path so it needs to be included in
2221 ** the dotlockLockingContext */
2222 int nFilename;
2223 pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
2224 pNew->lockingContext = sqlite3_malloc(
2225 sizeof(dotlockLockingContext));
2226 nFilename = strlen(zFilename) + 6;
2227 ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
2228 sqlite3_malloc( nFilename );
2229 sqlite3_snprintf(nFilename,
2230 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
2231 "%s.lock", zFilename);
2232 break;
2233 }
2234 case posixLockingStyle:
2235 /* posix locking doesn't need additional lockingContext information */
2236 pNew->pMethod = &sqlite3UnixIoMethod;
2237 break;
2238 case noLockingStyle:
2239 case unsupportedLockingStyle:
2240 default:
2241 pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
2242 }
2243 OpenCounter(+1);
2244 return SQLITE_OK;
2245 }
2246}
2247#else /* SQLITE_ENABLE_LOCKING_STYLE */
2248static int fillInUnixFile(
2249 int h, /* Open file descriptor on file being opened */
2250 int dirfd,
2251 sqlite3_file *pId, /* Write to the unixFile structure here */
2252 const char *zFilename /* Name of the file being opened */
2253){
2254 unixFile *pNew = (unixFile *)pId;
2255 int rc;
2256
2257#ifdef FD_CLOEXEC
2258 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
2259#endif
2260
2261 enterMutex();
2262 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2263 leaveMutex();
2264 if( rc ){
2265 close(h);
2266 return SQLITE_NOMEM;
2267 }
2268
2269 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
2270 pNew->dirfd = -1;
2271 pNew->h = h;
2272 pNew->dirfd = dirfd;
2273 SET_THREADID(pNew);
2274
2275 pNew->pMethod = &sqlite3UnixIoMethod;
2276 OpenCounter(+1);
2277 return SQLITE_OK;
2278}
2279#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2280
2281/*
2282** Open a file descriptor to the directory containing file zFilename.
2283** If successful, *pFd is set to the opened file descriptor and
2284** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2285** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2286** value.
2287**
2288** If SQLITE_OK is returned, the caller is responsible for closing
2289** the file descriptor *pFd using close().
2290*/
2291static int openDirectory(const char *zFilename, int *pFd){
2292 int ii;
2293 int fd = -1;
2294 char zDirname[MAX_PATHNAME+1];
2295
2296 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
2297 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2298 if( ii>0 ){
2299 zDirname[ii] = '\0';
2300 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
2301 if( fd>=0 ){
2302#ifdef FD_CLOEXEC
2303 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2304#endif
2305 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2306 }
2307 }
2308 *pFd = fd;
2309 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
2310}
2311
2312/*
2313** Open the file zPath.
2314**
2315** Previously, the SQLite OS layer used three functions in place of this
2316** one:
2317**
2318** sqlite3OsOpenReadWrite();
2319** sqlite3OsOpenReadOnly();
2320** sqlite3OsOpenExclusive();
2321**
2322** These calls correspond to the following combinations of flags:
2323**
2324** ReadWrite() -> (READWRITE | CREATE)
2325** ReadOnly() -> (READONLY)
2326** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2327**
2328** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2329** true, the file was configured to be automatically deleted when the
2330** file handle closed. To achieve the same effect using this new
2331** interface, add the DELETEONCLOSE flag to those specified above for
2332** OpenExclusive().
2333*/
2334static int unixOpen(
2335 sqlite3_vfs *pVfs,
2336 const char *zPath,
2337 sqlite3_file *pFile,
2338 int flags,
2339 int *pOutFlags
2340){
2341 int fd = 0; /* File descriptor returned by open() */
2342 int dirfd = -1; /* Directory file descriptor */
2343 int oflags = 0; /* Flags to pass to open() */
2344 int eType = flags&0xFFFFFF00; /* Type of file to open */
2345
2346 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2347 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2348 int isCreate = (flags & SQLITE_OPEN_CREATE);
2349 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2350 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2351
2352 /* If creating a master or main-file journal, this function will open
2353 ** a file-descriptor on the directory too. The first time unixSync()
2354 ** is called the directory file descriptor will be fsync()ed and close()d.
2355 */
2356 int isOpenDirectory = (isCreate &&
2357 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2358 );
2359
2360 /* Check the following statements are true:
2361 **
2362 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2363 ** (b) if CREATE is set, then READWRITE must also be set, and
2364 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
2365 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
2366 */
2367 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
2368 assert(isCreate==0 || isReadWrite);
2369 assert(isExclusive==0 || isCreate);
2370 assert(isDelete==0 || isCreate);
2371
2372
2373 /* The main DB, main journal, and master journal are never automatically
2374 ** deleted
2375 */
2376 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
2377 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
2378 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
2379
2380 /* Assert that the upper layer has set one of the "file-type" flags. */
2381 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2382 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2383 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
2384 || eType==SQLITE_OPEN_TRANSIENT_DB
2385 );
2386
2387 if( isReadonly ) oflags |= O_RDONLY;
2388 if( isReadWrite ) oflags |= O_RDWR;
2389 if( isCreate ) oflags |= O_CREAT;
2390 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2391 oflags |= (O_LARGEFILE|O_BINARY);
2392
2393 memset(pFile, 0, sizeof(unixFile));
2394 fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
2395 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
2396 /* Failed to open the file for read/write access. Try read-only. */
2397 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2398 flags |= SQLITE_OPEN_READONLY;
2399 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
2400 }
2401 if( fd<0 ){
2402 return SQLITE_CANTOPEN;
2403 }
2404 if( isDelete ){
2405 unlink(zPath);
2406 }
2407 if( pOutFlags ){
2408 *pOutFlags = flags;
2409 }
2410
2411 assert(fd!=0);
2412 if( isOpenDirectory ){
2413 int rc = openDirectory(zPath, &dirfd);
2414 if( rc!=SQLITE_OK ){
2415 close(fd);
2416 return rc;
2417 }
2418 }
2419 return fillInUnixFile(fd, dirfd, pFile, zPath);
2420}
2421
2422/*
2423** Delete the file at zPath. If the dirSync argument is true, fsync()
2424** the directory after deleting the file.
2425*/
2426static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
2427 int rc = SQLITE_OK;
2428 SimulateIOError(return SQLITE_IOERR_DELETE);
2429 unlink(zPath);
2430 if( dirSync ){
2431 int fd;
2432 rc = openDirectory(zPath, &fd);
2433 if( rc==SQLITE_OK ){
2434 if( fsync(fd) ){
2435 rc = SQLITE_IOERR_DIR_FSYNC;
2436 }
2437 close(fd);
2438 }
2439 }
2440 return rc;
2441}
2442
2443/*
2444** Test the existance of or access permissions of file zPath. The
2445** test performed depends on the value of flags:
2446**
2447** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2448** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2449** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2450**
2451** Otherwise return 0.
2452*/
2453static int unixAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
2454 int amode = 0;
2455 switch( flags ){
2456 case SQLITE_ACCESS_EXISTS:
2457 amode = F_OK;
2458 break;
2459 case SQLITE_ACCESS_READWRITE:
2460 amode = W_OK|R_OK;
2461 break;
2462 case SQLITE_ACCESS_READ:
2463 amode = R_OK;
2464 break;
2465
2466 default:
2467 assert(!"Invalid flags argument");
2468 }
2469 return (access(zPath, amode)==0);
2470}
2471
2472/*
2473** Create a temporary file name in zBuf. zBuf must be allocated
2474** by the calling process and must be big enough to hold at least
2475** pVfs->mxPathname bytes.
2476*/
2477static int unixGetTempname(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
2478 static const char *azDirs[] = {
2479 0,
2480 "/var/tmp",
2481 "/usr/tmp",
2482 "/tmp",
2483 ".",
2484 };
2485 static const unsigned char zChars[] =
2486 "abcdefghijklmnopqrstuvwxyz"
2487 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2488 "0123456789";
2489 int i, j;
2490 struct stat buf;
2491 const char *zDir = ".";
2492
2493 /* It's odd to simulate an io-error here, but really this is just
2494 ** using the io-error infrastructure to test that SQLite handles this
2495 ** function failing.
2496 */
2497 SimulateIOError( return SQLITE_ERROR );
2498
2499 azDirs[0] = sqlite3_temp_directory;
2500 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2501 if( azDirs[i]==0 ) continue;
2502 if( stat(azDirs[i], &buf) ) continue;
2503 if( !S_ISDIR(buf.st_mode) ) continue;
2504 if( access(azDirs[i], 07) ) continue;
2505 zDir = azDirs[i];
2506 break;
2507 }
2508 do{
2509 assert( pVfs->mxPathname==MAX_PATHNAME );
2510 assert( nBuf>=MAX_PATHNAME );
2511 sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2512 j = strlen(zBuf);
2513 sqlite3Randomness(15, &zBuf[j]);
2514 for(i=0; i<15; i++, j++){
2515 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2516 }
2517 zBuf[j] = 0;
2518 }while( access(zBuf,0)==0 );
2519 return SQLITE_OK;
2520}
2521
2522
2523/*
2524** Turn a relative pathname into a full pathname. The relative path
2525** is stored as a nul-terminated string in the buffer pointed to by
2526** zPath.
2527**
2528** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2529** (in this case, MAX_PATHNAME bytes). The full-path is written to
2530** this buffer before returning.
2531*/
2532static int unixFullPathname(
2533 sqlite3_vfs *pVfs, /* Pointer to vfs object */
2534 const char *zPath, /* Possibly relative input path */
2535 int nOut, /* Size of output buffer in bytes */
2536 char *zOut /* Output buffer */
2537){
2538
2539 /* It's odd to simulate an io-error here, but really this is just
2540 ** using the io-error infrastructure to test that SQLite handles this
2541 ** function failing. This function could fail if, for example, the
2542 ** current working directly has been unlinked.
2543 */
2544 SimulateIOError( return SQLITE_ERROR );
2545
2546 assert( pVfs->mxPathname==MAX_PATHNAME );
2547 zOut[MAX_PATHNAME-1] = '\0';
2548 if( zPath[0]=='/' ){
2549 sqlite3_snprintf(MAX_PATHNAME, zOut, "%s", zPath);
2550 }else{
2551 int nCwd;
2552 if( getcwd(zOut, MAX_PATHNAME-1)==0 ){
2553 return SQLITE_CANTOPEN;
2554 }
2555 nCwd = strlen(zOut);
2556 sqlite3_snprintf(MAX_PATHNAME-nCwd, &zOut[nCwd], "/%s", zPath);
2557 }
2558 return SQLITE_OK;
2559
2560#if 0
2561 /*
2562 ** Remove "/./" path elements and convert "/A/./" path elements
2563 ** to just "/".
2564 */
2565 if( zFull ){
2566 int i, j;
2567 for(i=j=0; zFull[i]; i++){
2568 if( zFull[i]=='/' ){
2569 if( zFull[i+1]=='/' ) continue;
2570 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2571 i += 1;
2572 continue;
2573 }
2574 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2575 while( j>0 && zFull[j-1]!='/' ){ j--; }
2576 i += 3;
2577 continue;
2578 }
2579 }
2580 zFull[j++] = zFull[i];
2581 }
2582 zFull[j] = 0;
2583 }
2584#endif
2585}
2586
2587
2588#ifndef SQLITE_OMIT_LOAD_EXTENSION
2589/*
2590** Interfaces for opening a shared library, finding entry points
2591** within the shared library, and closing the shared library.
2592*/
2593#include <dlfcn.h>
2594static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
2595 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2596}
2597
2598/*
2599** SQLite calls this function immediately after a call to unixDlSym() or
2600** unixDlOpen() fails (returns a null pointer). If a more detailed error
2601** message is available, it is written to zBufOut. If no error message
2602** is available, zBufOut is left unmodified and SQLite uses a default
2603** error message.
2604*/
2605static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
2606 char *zErr;
2607 enterMutex();
2608 zErr = dlerror();
2609 if( zErr ){
2610 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
2611 }
2612 leaveMutex();
2613}
2614static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
2615 return dlsym(pHandle, zSymbol);
2616}
2617static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
2618 dlclose(pHandle);
2619}
2620#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2621 #define unixDlOpen 0
2622 #define unixDlError 0
2623 #define unixDlSym 0
2624 #define unixDlClose 0
2625#endif
2626
2627/*
2628** Write nBuf bytes of random data to the supplied buffer zBuf.
2629*/
2630static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
2631
2632 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2633
2634 /* We have to initialize zBuf to prevent valgrind from reporting
2635 ** errors. The reports issued by valgrind are incorrect - we would
2636 ** prefer that the randomness be increased by making use of the
2637 ** uninitialized space in zBuf - but valgrind errors tend to worry
2638 ** some users. Rather than argue, it seems easier just to initialize
2639 ** the whole array and silence valgrind, even if that means less randomness
2640 ** in the random seed.
2641 **
2642 ** When testing, initializing zBuf[] to zero is all we do. That means
2643 ** that we always use the same random number sequence. This makes the
2644 ** tests repeatable.
2645 */
2646 memset(zBuf, 0, nBuf);
2647#if !defined(SQLITE_TEST)
2648 {
2649 int pid, fd;
2650 fd = open("/dev/urandom", O_RDONLY);
2651 if( fd<0 ){
2652 time_t t;
2653 time(&t);
2654 memcpy(zBuf, &t, sizeof(t));
2655 pid = getpid();
2656 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
2657 }else{
2658 read(fd, zBuf, nBuf);
2659 close(fd);
2660 }
2661 }
2662#endif
2663 return SQLITE_OK;
2664}
2665
2666
2667/*
2668** Sleep for a little while. Return the amount of time slept.
2669** The argument is the number of microseconds we want to sleep.
2670** The return value is the number of microseconds of sleep actually
2671** requested from the underlying operating system, a number which
2672** might be greater than or equal to the argument, but not less
2673** than the argument.
2674*/
2675static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
2676#if defined(HAVE_USLEEP) && HAVE_USLEEP
2677 usleep(microseconds);
2678 return microseconds;
2679#else
2680 int seconds = (microseconds+999999)/1000000;
2681 sleep(seconds);
2682 return seconds*1000000;
2683#endif
2684}
2685
2686/*
2687** The following variable, if set to a non-zero value, becomes the result
2688** returned from sqlite3OsCurrentTime(). This is used for testing.
2689*/
2690#ifdef SQLITE_TEST
2691int sqlite3_current_time = 0;
2692#endif
2693
2694/*
2695** Find the current time (in Universal Coordinated Time). Write the
2696** current time and date as a Julian Day number into *prNow and
2697** return 0. Return 1 if the time and date cannot be found.
2698*/
2699static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
2700#ifdef NO_GETTOD
2701 time_t t;
2702 time(&t);
2703 *prNow = t/86400.0 + 2440587.5;
2704#else
2705 struct timeval sNow;
2706 gettimeofday(&sNow, 0);
2707 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2708#endif
2709#ifdef SQLITE_TEST
2710 if( sqlite3_current_time ){
2711 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2712 }
2713#endif
2714 return 0;
2715}
2716
2717/*
2718** Return a pointer to the sqlite3DefaultVfs structure. We use
2719** a function rather than give the structure global scope because
2720** some compilers (MSVC) do not allow forward declarations of
2721** initialized structures.
2722*/
2723sqlite3_vfs *sqlite3OsDefaultVfs(void){
2724 static sqlite3_vfs unixVfs = {
2725 1, /* iVersion */
2726 sizeof(unixFile), /* szOsFile */
2727 MAX_PATHNAME, /* mxPathname */
2728 0, /* pNext */
2729 "unix", /* zName */
2730 0, /* pAppData */
2731
2732 unixOpen, /* xOpen */
2733 unixDelete, /* xDelete */
2734 unixAccess, /* xAccess */
2735 unixGetTempname, /* xGetTempName */
2736 unixFullPathname, /* xFullPathname */
2737 unixDlOpen, /* xDlOpen */
2738 unixDlError, /* xDlError */
2739 unixDlSym, /* xDlSym */
2740 unixDlClose, /* xDlClose */
2741 unixRandomness, /* xRandomness */
2742 unixSleep, /* xSleep */
2743 unixCurrentTime /* xCurrentTime */
2744 };
2745
2746 return &unixVfs;
2747}
2748
2749#endif /* OS_UNIX */
diff --git a/libraries/sqlite/win32/os_win.c b/libraries/sqlite/win32/os_win.c
new file mode 100755
index 0000000..bcb1c6e
--- /dev/null
+++ b/libraries/sqlite/win32/os_win.c
@@ -0,0 +1,1545 @@
1/*
2** 2004 May 22
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.
10**
11******************************************************************************
12**
13** This file contains code that is specific to windows.
14*/
15#include "sqliteInt.h"
16#if OS_WIN /* This file is used for windows only */
17
18
19/*
20** A Note About Memory Allocation:
21**
22** This driver uses malloc()/free() directly rather than going through
23** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
24** are designed for use on embedded systems where memory is scarce and
25** malloc failures happen frequently. Win32 does not typically run on
26** embedded systems, and when it does the developers normally have bigger
27** problems to worry about than running out of memory. So there is not
28** a compelling need to use the wrappers.
29**
30** But there is a good reason to not use the wrappers. If we use the
31** wrappers then we will get simulated malloc() failures within this
32** driver. And that causes all kinds of problems for our tests. We
33** could enhance SQLite to deal with simulated malloc failures within
34** the OS driver, but the code to deal with those failure would not
35** be exercised on Linux (which does not need to malloc() in the driver)
36** and so we would have difficulty writing coverage tests for that
37** code. Better to leave the code out, we think.
38**
39** The point of this discussion is as follows: When creating a new
40** OS layer for an embedded system, if you use this file as an example,
41** avoid the use of malloc()/free(). Those routines work ok on windows
42** desktops but not so well in embedded systems.
43*/
44
45#include <winbase.h>
46
47#ifdef __CYGWIN__
48# include <sys/cygwin.h>
49#endif
50
51/*
52** Macros used to determine whether or not to use threads.
53*/
54#if defined(THREADSAFE) && THREADSAFE
55# define SQLITE_W32_THREADS 1
56#endif
57
58/*
59** Include code that is common to all os_*.c files
60*/
61#include "os_common.h"
62
63/*
64** Determine if we are dealing with WindowsCE - which has a much
65** reduced API.
66*/
67#if defined(_WIN32_WCE)
68# define OS_WINCE 1
69# define AreFileApisANSI() 1
70#else
71# define OS_WINCE 0
72#endif
73
74/*
75** WinCE lacks native support for file locking so we have to fake it
76** with some code of our own.
77*/
78#if OS_WINCE
79typedef struct winceLock {
80 int nReaders; /* Number of reader locks obtained */
81 BOOL bPending; /* Indicates a pending lock has been obtained */
82 BOOL bReserved; /* Indicates a reserved lock has been obtained */
83 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
84} winceLock;
85#endif
86
87/*
88** The winFile structure is a subclass of sqlite3_file* specific to the win32
89** portability layer.
90*/
91typedef struct winFile winFile;
92struct winFile {
93 const sqlite3_io_methods *pMethod;/* Must be first */
94 HANDLE h; /* Handle for accessing the file */
95 unsigned char locktype; /* Type of lock currently held on this file */
96 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
97#if OS_WINCE
98 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
99 HANDLE hMutex; /* Mutex used to control access to shared lock */
100 HANDLE hShared; /* Shared memory segment used for locking */
101 winceLock local; /* Locks obtained by this instance of winFile */
102 winceLock *shared; /* Global shared lock memory for the file */
103#endif
104};
105
106
107/*
108** The following variable is (normally) set once and never changes
109** thereafter. It records whether the operating system is Win95
110** or WinNT.
111**
112** 0: Operating system unknown.
113** 1: Operating system is Win95.
114** 2: Operating system is WinNT.
115**
116** In order to facilitate testing on a WinNT system, the test fixture
117** can manually set this value to 1 to emulate Win98 behavior.
118*/
119#ifdef SQLITE_TEST
120int sqlite3_os_type = 0;
121#else
122static int sqlite3_os_type = 0;
123#endif
124
125/*
126** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
127** or WinCE. Return false (zero) for Win95, Win98, or WinME.
128**
129** Here is an interesting observation: Win95, Win98, and WinME lack
130** the LockFileEx() API. But we can still statically link against that
131** API as long as we don't call it win running Win95/98/ME. A call to
132** this routine is used to determine if the host is Win95/98/ME or
133** WinNT/2K/XP so that we will know whether or not we can safely call
134** the LockFileEx() API.
135*/
136#if OS_WINCE
137# define isNT() (1)
138#else
139 static int isNT(void){
140 if( sqlite3_os_type==0 ){
141 OSVERSIONINFO sInfo;
142 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
143 GetVersionEx(&sInfo);
144 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
145 }
146 return sqlite3_os_type==2;
147 }
148#endif /* OS_WINCE */
149
150/*
151** Convert a UTF-8 string to microsoft unicode (UTF-16?).
152**
153** Space to hold the returned string is obtained from malloc.
154*/
155static WCHAR *utf8ToUnicode(const char *zFilename){
156 int nChar;
157 WCHAR *zWideFilename;
158
159 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
160 zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
161 if( zWideFilename==0 ){
162 return 0;
163 }
164 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
165 if( nChar==0 ){
166 free(zWideFilename);
167 zWideFilename = 0;
168 }
169 return zWideFilename;
170}
171
172/*
173** Convert microsoft unicode to UTF-8. Space to hold the returned string is
174** obtained from malloc().
175*/
176static char *unicodeToUtf8(const WCHAR *zWideFilename){
177 int nByte;
178 char *zFilename;
179
180 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
181 zFilename = malloc( nByte );
182 if( zFilename==0 ){
183 return 0;
184 }
185 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
186 0, 0);
187 if( nByte == 0 ){
188 free(zFilename);
189 zFilename = 0;
190 }
191 return zFilename;
192}
193
194/*
195** Convert an ansi string to microsoft unicode, based on the
196** current codepage settings for file apis.
197**
198** Space to hold the returned string is obtained
199** from malloc.
200*/
201static WCHAR *mbcsToUnicode(const char *zFilename){
202 int nByte;
203 WCHAR *zMbcsFilename;
204 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
205
206 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
207 zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
208 if( zMbcsFilename==0 ){
209 return 0;
210 }
211 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
212 if( nByte==0 ){
213 free(zMbcsFilename);
214 zMbcsFilename = 0;
215 }
216 return zMbcsFilename;
217}
218
219/*
220** Convert microsoft unicode to multibyte character string, based on the
221** user's Ansi codepage.
222**
223** Space to hold the returned string is obtained from
224** malloc().
225*/
226static char *unicodeToMbcs(const WCHAR *zWideFilename){
227 int nByte;
228 char *zFilename;
229 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
230
231 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
232 zFilename = malloc( nByte );
233 if( zFilename==0 ){
234 return 0;
235 }
236 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
237 0, 0);
238 if( nByte == 0 ){
239 free(zFilename);
240 zFilename = 0;
241 }
242 return zFilename;
243}
244
245/*
246** Convert multibyte character string to UTF-8. Space to hold the
247** returned string is obtained from malloc().
248*/
249static char *mbcsToUtf8(const char *zFilename){
250 char *zFilenameUtf8;
251 WCHAR *zTmpWide;
252
253 zTmpWide = mbcsToUnicode(zFilename);
254 if( zTmpWide==0 ){
255 return 0;
256 }
257 zFilenameUtf8 = unicodeToUtf8(zTmpWide);
258 free(zTmpWide);
259 return zFilenameUtf8;
260}
261
262/*
263** Convert UTF-8 to multibyte character string. Space to hold the
264** returned string is obtained from malloc().
265*/
266static char *utf8ToMbcs(const char *zFilename){
267 char *zFilenameMbcs;
268 WCHAR *zTmpWide;
269
270 zTmpWide = utf8ToUnicode(zFilename);
271 if( zTmpWide==0 ){
272 return 0;
273 }
274 zFilenameMbcs = unicodeToMbcs(zTmpWide);
275 free(zTmpWide);
276 return zFilenameMbcs;
277}
278
279#if OS_WINCE
280/*************************************************************************
281** This section contains code for WinCE only.
282*/
283/*
284** WindowsCE does not have a localtime() function. So create a
285** substitute.
286*/
287#include <time.h>
288struct tm *__cdecl localtime(const time_t *t)
289{
290 static struct tm y;
291 FILETIME uTm, lTm;
292 SYSTEMTIME pTm;
293 sqlite3_int64 t64;
294 t64 = *t;
295 t64 = (t64 + 11644473600)*10000000;
296 uTm.dwLowDateTime = t64 & 0xFFFFFFFF;
297 uTm.dwHighDateTime= t64 >> 32;
298 FileTimeToLocalFileTime(&uTm,&lTm);
299 FileTimeToSystemTime(&lTm,&pTm);
300 y.tm_year = pTm.wYear - 1900;
301 y.tm_mon = pTm.wMonth - 1;
302 y.tm_wday = pTm.wDayOfWeek;
303 y.tm_mday = pTm.wDay;
304 y.tm_hour = pTm.wHour;
305 y.tm_min = pTm.wMinute;
306 y.tm_sec = pTm.wSecond;
307 return &y;
308}
309
310/* This will never be called, but defined to make the code compile */
311#define GetTempPathA(a,b)
312
313#define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
314#define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
315#define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
316
317#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)]
318
319/*
320** Acquire a lock on the handle h
321*/
322static void winceMutexAcquire(HANDLE h){
323 DWORD dwErr;
324 do {
325 dwErr = WaitForSingleObject(h, INFINITE);
326 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
327}
328/*
329** Release a lock acquired by winceMutexAcquire()
330*/
331#define winceMutexRelease(h) ReleaseMutex(h)
332
333/*
334** Create the mutex and shared memory used for locking in the file
335** descriptor pFile
336*/
337static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
338 WCHAR *zTok;
339 WCHAR *zName = utf8ToUnicode(zFilename);
340 BOOL bInit = TRUE;
341
342 /* Initialize the local lockdata */
343 ZeroMemory(&pFile->local, sizeof(pFile->local));
344
345 /* Replace the backslashes from the filename and lowercase it
346 ** to derive a mutex name. */
347 zTok = CharLowerW(zName);
348 for (;*zTok;zTok++){
349 if (*zTok == '\\') *zTok = '_';
350 }
351
352 /* Create/open the named mutex */
353 pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
354 if (!pFile->hMutex){
355 free(zName);
356 return FALSE;
357 }
358
359 /* Acquire the mutex before continuing */
360 winceMutexAcquire(pFile->hMutex);
361
362 /* Since the names of named mutexes, semaphores, file mappings etc are
363 ** case-sensitive, take advantage of that by uppercasing the mutex name
364 ** and using that as the shared filemapping name.
365 */
366 CharUpperW(zName);
367 pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
368 PAGE_READWRITE, 0, sizeof(winceLock),
369 zName);
370
371 /* Set a flag that indicates we're the first to create the memory so it
372 ** must be zero-initialized */
373 if (GetLastError() == ERROR_ALREADY_EXISTS){
374 bInit = FALSE;
375 }
376
377 free(zName);
378
379 /* If we succeeded in making the shared memory handle, map it. */
380 if (pFile->hShared){
381 pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
382 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
383 /* If mapping failed, close the shared memory handle and erase it */
384 if (!pFile->shared){
385 CloseHandle(pFile->hShared);
386 pFile->hShared = NULL;
387 }
388 }
389
390 /* If shared memory could not be created, then close the mutex and fail */
391 if (pFile->hShared == NULL){
392 winceMutexRelease(pFile->hMutex);
393 CloseHandle(pFile->hMutex);
394 pFile->hMutex = NULL;
395 return FALSE;
396 }
397
398 /* Initialize the shared memory if we're supposed to */
399 if (bInit) {
400 ZeroMemory(pFile->shared, sizeof(winceLock));
401 }
402
403 winceMutexRelease(pFile->hMutex);
404 return TRUE;
405}
406
407/*
408** Destroy the part of winFile that deals with wince locks
409*/
410static void winceDestroyLock(winFile *pFile){
411 if (pFile->hMutex){
412 /* Acquire the mutex */
413 winceMutexAcquire(pFile->hMutex);
414
415 /* The following blocks should probably assert in debug mode, but they
416 are to cleanup in case any locks remained open */
417 if (pFile->local.nReaders){
418 pFile->shared->nReaders --;
419 }
420 if (pFile->local.bReserved){
421 pFile->shared->bReserved = FALSE;
422 }
423 if (pFile->local.bPending){
424 pFile->shared->bPending = FALSE;
425 }
426 if (pFile->local.bExclusive){
427 pFile->shared->bExclusive = FALSE;
428 }
429
430 /* De-reference and close our copy of the shared memory handle */
431 UnmapViewOfFile(pFile->shared);
432 CloseHandle(pFile->hShared);
433
434 if( pFile->zDeleteOnClose ){
435 DeleteFileW(pFile->zDeleteOnClose);
436 free(pFile->zDeleteOnClose);
437 pFile->zDeleteOnClose = 0;
438 }
439
440 /* Done with the mutex */
441 winceMutexRelease(pFile->hMutex);
442 CloseHandle(pFile->hMutex);
443 pFile->hMutex = NULL;
444 }
445}
446
447/*
448** An implementation of the LockFile() API of windows for wince
449*/
450static BOOL winceLockFile(
451 HANDLE *phFile,
452 DWORD dwFileOffsetLow,
453 DWORD dwFileOffsetHigh,
454 DWORD nNumberOfBytesToLockLow,
455 DWORD nNumberOfBytesToLockHigh
456){
457 winFile *pFile = HANDLE_TO_WINFILE(phFile);
458 BOOL bReturn = FALSE;
459
460 if (!pFile->hMutex) return TRUE;
461 winceMutexAcquire(pFile->hMutex);
462
463 /* Wanting an exclusive lock? */
464 if (dwFileOffsetLow == SHARED_FIRST
465 && nNumberOfBytesToLockLow == SHARED_SIZE){
466 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
467 pFile->shared->bExclusive = TRUE;
468 pFile->local.bExclusive = TRUE;
469 bReturn = TRUE;
470 }
471 }
472
473 /* Want a read-only lock? */
474 else if ((dwFileOffsetLow >= SHARED_FIRST &&
475 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) &&
476 nNumberOfBytesToLockLow == 1){
477 if (pFile->shared->bExclusive == 0){
478 pFile->local.nReaders ++;
479 if (pFile->local.nReaders == 1){
480 pFile->shared->nReaders ++;
481 }
482 bReturn = TRUE;
483 }
484 }
485
486 /* Want a pending lock? */
487 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){
488 /* If no pending lock has been acquired, then acquire it */
489 if (pFile->shared->bPending == 0) {
490 pFile->shared->bPending = TRUE;
491 pFile->local.bPending = TRUE;
492 bReturn = TRUE;
493 }
494 }
495 /* Want a reserved lock? */
496 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
497 if (pFile->shared->bReserved == 0) {
498 pFile->shared->bReserved = TRUE;
499 pFile->local.bReserved = TRUE;
500 bReturn = TRUE;
501 }
502 }
503
504 winceMutexRelease(pFile->hMutex);
505 return bReturn;
506}
507
508/*
509** An implementation of the UnlockFile API of windows for wince
510*/
511static BOOL winceUnlockFile(
512 HANDLE *phFile,
513 DWORD dwFileOffsetLow,
514 DWORD dwFileOffsetHigh,
515 DWORD nNumberOfBytesToUnlockLow,
516 DWORD nNumberOfBytesToUnlockHigh
517){
518 winFile *pFile = HANDLE_TO_WINFILE(phFile);
519 BOOL bReturn = FALSE;
520
521 if (!pFile->hMutex) return TRUE;
522 winceMutexAcquire(pFile->hMutex);
523
524 /* Releasing a reader lock or an exclusive lock */
525 if (dwFileOffsetLow >= SHARED_FIRST &&
526 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){
527 /* Did we have an exclusive lock? */
528 if (pFile->local.bExclusive){
529 pFile->local.bExclusive = FALSE;
530 pFile->shared->bExclusive = FALSE;
531 bReturn = TRUE;
532 }
533
534 /* Did we just have a reader lock? */
535 else if (pFile->local.nReaders){
536 pFile->local.nReaders --;
537 if (pFile->local.nReaders == 0)
538 {
539 pFile->shared->nReaders --;
540 }
541 bReturn = TRUE;
542 }
543 }
544
545 /* Releasing a pending lock */
546 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
547 if (pFile->local.bPending){
548 pFile->local.bPending = FALSE;
549 pFile->shared->bPending = FALSE;
550 bReturn = TRUE;
551 }
552 }
553 /* Releasing a reserved lock */
554 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
555 if (pFile->local.bReserved) {
556 pFile->local.bReserved = FALSE;
557 pFile->shared->bReserved = FALSE;
558 bReturn = TRUE;
559 }
560 }
561
562 winceMutexRelease(pFile->hMutex);
563 return bReturn;
564}
565
566/*
567** An implementation of the LockFileEx() API of windows for wince
568*/
569static BOOL winceLockFileEx(
570 HANDLE *phFile,
571 DWORD dwFlags,
572 DWORD dwReserved,
573 DWORD nNumberOfBytesToLockLow,
574 DWORD nNumberOfBytesToLockHigh,
575 LPOVERLAPPED lpOverlapped
576){
577 /* If the caller wants a shared read lock, forward this call
578 ** to winceLockFile */
579 if (lpOverlapped->Offset == SHARED_FIRST &&
580 dwFlags == 1 &&
581 nNumberOfBytesToLockLow == SHARED_SIZE){
582 return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
583 }
584 return FALSE;
585}
586/*
587** End of the special code for wince
588*****************************************************************************/
589#endif /* OS_WINCE */
590
591/*****************************************************************************
592** The next group of routines implement the I/O methods specified
593** by the sqlite3_io_methods object.
594******************************************************************************/
595
596/*
597** Close a file.
598**
599** It is reported that an attempt to close a handle might sometimes
600** fail. This is a very unreasonable result, but windows is notorious
601** for being unreasonable so I do not doubt that it might happen. If
602** the close fails, we pause for 100 milliseconds and try again. As
603** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
604** giving up and returning an error.
605*/
606#define MX_CLOSE_ATTEMPT 3
607static int winClose(sqlite3_file *id){
608 int rc, cnt = 0;
609 winFile *pFile = (winFile*)id;
610 OSTRACE2("CLOSE %d\n", pFile->h);
611 do{
612 rc = CloseHandle(pFile->h);
613 }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
614#if OS_WINCE
615 winceDestroyLock(pFile);
616#endif
617 OpenCounter(-1);
618 return rc ? SQLITE_OK : SQLITE_IOERR;
619}
620
621/*
622** Some microsoft compilers lack this definition.
623*/
624#ifndef INVALID_SET_FILE_POINTER
625# define INVALID_SET_FILE_POINTER ((DWORD)-1)
626#endif
627
628/*
629** Read data from a file into a buffer. Return SQLITE_OK if all
630** bytes were read successfully and SQLITE_IOERR if anything goes
631** wrong.
632*/
633static int winRead(
634 sqlite3_file *id, /* File to read from */
635 void *pBuf, /* Write content into this buffer */
636 int amt, /* Number of bytes to read */
637 sqlite3_int64 offset /* Begin reading at this offset */
638){
639 LONG upperBits = (offset>>32) & 0x7fffffff;
640 LONG lowerBits = offset & 0xffffffff;
641 DWORD rc;
642 DWORD got;
643 winFile *pFile = (winFile*)id;
644 assert( id!=0 );
645 SimulateIOError(return SQLITE_IOERR_READ);
646 OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype);
647 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
648 if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
649 return SQLITE_FULL;
650 }
651 if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){
652 return SQLITE_IOERR_READ;
653 }
654 if( got==(DWORD)amt ){
655 return SQLITE_OK;
656 }else{
657 memset(&((char*)pBuf)[got], 0, amt-got);
658 return SQLITE_IOERR_SHORT_READ;
659 }
660}
661
662/*
663** Write data from a buffer into a file. Return SQLITE_OK on success
664** or some other error code on failure.
665*/
666static int winWrite(
667 sqlite3_file *id, /* File to write into */
668 const void *pBuf, /* The bytes to be written */
669 int amt, /* Number of bytes to write */
670 sqlite3_int64 offset /* Offset into the file to begin writing at */
671){
672 LONG upperBits = (offset>>32) & 0x7fffffff;
673 LONG lowerBits = offset & 0xffffffff;
674 DWORD rc;
675 DWORD wrote;
676 winFile *pFile = (winFile*)id;
677 assert( id!=0 );
678 SimulateIOError(return SQLITE_IOERR_WRITE);
679 SimulateDiskfullError(return SQLITE_FULL);
680 OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype);
681 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
682 if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
683 return SQLITE_FULL;
684 }
685 assert( amt>0 );
686 while(
687 amt>0
688 && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0
689 && wrote>0
690 ){
691 amt -= wrote;
692 pBuf = &((char*)pBuf)[wrote];
693 }
694 if( !rc || amt>(int)wrote ){
695 return SQLITE_FULL;
696 }
697 return SQLITE_OK;
698}
699
700/*
701** Truncate an open file to a specified size
702*/
703static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
704 LONG upperBits = (nByte>>32) & 0x7fffffff;
705 LONG lowerBits = nByte & 0xffffffff;
706 winFile *pFile = (winFile*)id;
707 OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte);
708 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
709 SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
710 SetEndOfFile(pFile->h);
711 return SQLITE_OK;
712}
713
714#ifdef SQLITE_TEST
715/*
716** Count the number of fullsyncs and normal syncs. This is used to test
717** that syncs and fullsyncs are occuring at the right times.
718*/
719int sqlite3_sync_count = 0;
720int sqlite3_fullsync_count = 0;
721#endif
722
723/*
724** Make sure all writes to a particular file are committed to disk.
725*/
726static int winSync(sqlite3_file *id, int flags){
727 winFile *pFile = (winFile*)id;
728 OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype);
729#ifdef SQLITE_TEST
730 if( flags & SQLITE_SYNC_FULL ){
731 sqlite3_fullsync_count++;
732 }
733 sqlite3_sync_count++;
734#endif
735 if( FlushFileBuffers(pFile->h) ){
736 return SQLITE_OK;
737 }else{
738 return SQLITE_IOERR;
739 }
740}
741
742/*
743** Determine the current size of a file in bytes
744*/
745static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
746 winFile *pFile = (winFile*)id;
747 DWORD upperBits, lowerBits;
748 SimulateIOError(return SQLITE_IOERR_FSTAT);
749 lowerBits = GetFileSize(pFile->h, &upperBits);
750 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
751 return SQLITE_OK;
752}
753
754/*
755** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
756*/
757#ifndef LOCKFILE_FAIL_IMMEDIATELY
758# define LOCKFILE_FAIL_IMMEDIATELY 1
759#endif
760
761/*
762** Acquire a reader lock.
763** Different API routines are called depending on whether or not this
764** is Win95 or WinNT.
765*/
766static int getReadLock(winFile *pFile){
767 int res;
768 if( isNT() ){
769 OVERLAPPED ovlp;
770 ovlp.Offset = SHARED_FIRST;
771 ovlp.OffsetHigh = 0;
772 ovlp.hEvent = 0;
773 res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
774 0, SHARED_SIZE, 0, &ovlp);
775 }else{
776 int lk;
777 sqlite3Randomness(sizeof(lk), &lk);
778 pFile->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
779 res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
780 }
781 return res;
782}
783
784/*
785** Undo a readlock
786*/
787static int unlockReadLock(winFile *pFile){
788 int res;
789 if( isNT() ){
790 res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
791 }else{
792 res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
793 }
794 return res;
795}
796
797/*
798** Lock the file with the lock specified by parameter locktype - one
799** of the following:
800**
801** (1) SHARED_LOCK
802** (2) RESERVED_LOCK
803** (3) PENDING_LOCK
804** (4) EXCLUSIVE_LOCK
805**
806** Sometimes when requesting one lock state, additional lock states
807** are inserted in between. The locking might fail on one of the later
808** transitions leaving the lock state different from what it started but
809** still short of its goal. The following chart shows the allowed
810** transitions and the inserted intermediate states:
811**
812** UNLOCKED -> SHARED
813** SHARED -> RESERVED
814** SHARED -> (PENDING) -> EXCLUSIVE
815** RESERVED -> (PENDING) -> EXCLUSIVE
816** PENDING -> EXCLUSIVE
817**
818** This routine will only increase a lock. The winUnlock() routine
819** erases all locks at once and returns us immediately to locking level 0.
820** It is not possible to lower the locking level one step at a time. You
821** must go straight to locking level 0.
822*/
823static int winLock(sqlite3_file *id, int locktype){
824 int rc = SQLITE_OK; /* Return code from subroutines */
825 int res = 1; /* Result of a windows lock call */
826 int newLocktype; /* Set pFile->locktype to this value before exiting */
827 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
828 winFile *pFile = (winFile*)id;
829
830 assert( pFile!=0 );
831 OSTRACE5("LOCK %d %d was %d(%d)\n",
832 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
833
834 /* If there is already a lock of this type or more restrictive on the
835 ** OsFile, do nothing. Don't use the end_lock: exit path, as
836 ** sqlite3OsEnterMutex() hasn't been called yet.
837 */
838 if( pFile->locktype>=locktype ){
839 return SQLITE_OK;
840 }
841
842 /* Make sure the locking sequence is correct
843 */
844 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
845 assert( locktype!=PENDING_LOCK );
846 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
847
848 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
849 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
850 ** the PENDING_LOCK byte is temporary.
851 */
852 newLocktype = pFile->locktype;
853 if( pFile->locktype==NO_LOCK
854 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
855 ){
856 int cnt = 3;
857 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
858 /* Try 3 times to get the pending lock. The pending lock might be
859 ** held by another reader process who will release it momentarily.
860 */
861 OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
862 Sleep(1);
863 }
864 gotPendingLock = res;
865 }
866
867 /* Acquire a shared lock
868 */
869 if( locktype==SHARED_LOCK && res ){
870 assert( pFile->locktype==NO_LOCK );
871 res = getReadLock(pFile);
872 if( res ){
873 newLocktype = SHARED_LOCK;
874 }
875 }
876
877 /* Acquire a RESERVED lock
878 */
879 if( locktype==RESERVED_LOCK && res ){
880 assert( pFile->locktype==SHARED_LOCK );
881 res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
882 if( res ){
883 newLocktype = RESERVED_LOCK;
884 }
885 }
886
887 /* Acquire a PENDING lock
888 */
889 if( locktype==EXCLUSIVE_LOCK && res ){
890 newLocktype = PENDING_LOCK;
891 gotPendingLock = 0;
892 }
893
894 /* Acquire an EXCLUSIVE lock
895 */
896 if( locktype==EXCLUSIVE_LOCK && res ){
897 assert( pFile->locktype>=SHARED_LOCK );
898 res = unlockReadLock(pFile);
899 OSTRACE2("unreadlock = %d\n", res);
900 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
901 if( res ){
902 newLocktype = EXCLUSIVE_LOCK;
903 }else{
904 OSTRACE2("error-code = %d\n", GetLastError());
905 getReadLock(pFile);
906 }
907 }
908
909 /* If we are holding a PENDING lock that ought to be released, then
910 ** release it now.
911 */
912 if( gotPendingLock && locktype==SHARED_LOCK ){
913 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
914 }
915
916 /* Update the state of the lock has held in the file descriptor then
917 ** return the appropriate result code.
918 */
919 if( res ){
920 rc = SQLITE_OK;
921 }else{
922 OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
923 locktype, newLocktype);
924 rc = SQLITE_BUSY;
925 }
926 pFile->locktype = newLocktype;
927 return rc;
928}
929
930/*
931** This routine checks if there is a RESERVED lock held on the specified
932** file by this or any other process. If such a lock is held, return
933** non-zero, otherwise zero.
934*/
935static int winCheckReservedLock(sqlite3_file *id){
936 int rc;
937 winFile *pFile = (winFile*)id;
938 assert( pFile!=0 );
939 if( pFile->locktype>=RESERVED_LOCK ){
940 rc = 1;
941 OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
942 }else{
943 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
944 if( rc ){
945 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
946 }
947 rc = !rc;
948 OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
949 }
950 return rc;
951}
952
953/*
954** Lower the locking level on file descriptor id to locktype. locktype
955** must be either NO_LOCK or SHARED_LOCK.
956**
957** If the locking level of the file descriptor is already at or below
958** the requested locking level, this routine is a no-op.
959**
960** It is not possible for this routine to fail if the second argument
961** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
962** might return SQLITE_IOERR;
963*/
964static int winUnlock(sqlite3_file *id, int locktype){
965 int type;
966 winFile *pFile = (winFile*)id;
967 int rc = SQLITE_OK;
968 assert( pFile!=0 );
969 assert( locktype<=SHARED_LOCK );
970 OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
971 pFile->locktype, pFile->sharedLockByte);
972 type = pFile->locktype;
973 if( type>=EXCLUSIVE_LOCK ){
974 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
975 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
976 /* This should never happen. We should always be able to
977 ** reacquire the read lock */
978 rc = SQLITE_IOERR_UNLOCK;
979 }
980 }
981 if( type>=RESERVED_LOCK ){
982 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
983 }
984 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
985 unlockReadLock(pFile);
986 }
987 if( type>=PENDING_LOCK ){
988 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
989 }
990 pFile->locktype = locktype;
991 return rc;
992}
993
994/*
995** Control and query of the open file handle.
996*/
997static int winFileControl(sqlite3_file *id, int op, void *pArg){
998 switch( op ){
999 case SQLITE_FCNTL_LOCKSTATE: {
1000 *(int*)pArg = ((winFile*)id)->locktype;
1001 return SQLITE_OK;
1002 }
1003 }
1004 return SQLITE_ERROR;
1005}
1006
1007/*
1008** Return the sector size in bytes of the underlying block device for
1009** the specified file. This is almost always 512 bytes, but may be
1010** larger for some devices.
1011**
1012** SQLite code assumes this function cannot fail. It also assumes that
1013** if two files are created in the same file-system directory (i.e.
1014** a database and it's journal file) that the sector size will be the
1015** same for both.
1016*/
1017static int winSectorSize(sqlite3_file *id){
1018 return SQLITE_DEFAULT_SECTOR_SIZE;
1019}
1020
1021/*
1022** Return a vector of device characteristics.
1023*/
1024static int winDeviceCharacteristics(sqlite3_file *id){
1025 return 0;
1026}
1027
1028/*
1029** This vector defines all the methods that can operate on an
1030** sqlite3_file for win32.
1031*/
1032static const sqlite3_io_methods winIoMethod = {
1033 1, /* iVersion */
1034 winClose,
1035 winRead,
1036 winWrite,
1037 winTruncate,
1038 winSync,
1039 winFileSize,
1040 winLock,
1041 winUnlock,
1042 winCheckReservedLock,
1043 winFileControl,
1044 winSectorSize,
1045 winDeviceCharacteristics
1046};
1047
1048/***************************************************************************
1049** Here ends the I/O methods that form the sqlite3_io_methods object.
1050**
1051** The next block of code implements the VFS methods.
1052****************************************************************************/
1053
1054/*
1055** Convert a UTF-8 filename into whatever form the underlying
1056** operating system wants filenames in. Space to hold the result
1057** is obtained from malloc and must be freed by the calling
1058** function.
1059*/
1060static void *convertUtf8Filename(const char *zFilename){
1061 void *zConverted = 0;
1062 if( isNT() ){
1063 zConverted = utf8ToUnicode(zFilename);
1064 }else{
1065 zConverted = utf8ToMbcs(zFilename);
1066 }
1067 /* caller will handle out of memory */
1068 return zConverted;
1069}
1070
1071/*
1072** Open a file.
1073*/
1074static int winOpen(
1075 sqlite3_vfs *pVfs, /* Not used */
1076 const char *zName, /* Name of the file (UTF-8) */
1077 sqlite3_file *id, /* Write the SQLite file handle here */
1078 int flags, /* Open mode flags */
1079 int *pOutFlags /* Status return flags */
1080){
1081 HANDLE h;
1082 DWORD dwDesiredAccess;
1083 DWORD dwShareMode;
1084 DWORD dwCreationDisposition;
1085 DWORD dwFlagsAndAttributes = 0;
1086 winFile *pFile = (winFile*)id;
1087 void *zConverted = convertUtf8Filename(zName);
1088 if( zConverted==0 ){
1089 return SQLITE_NOMEM;
1090 }
1091
1092 if( flags & SQLITE_OPEN_READWRITE ){
1093 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
1094 }else{
1095 dwDesiredAccess = GENERIC_READ;
1096 }
1097 if( flags & SQLITE_OPEN_CREATE ){
1098 dwCreationDisposition = OPEN_ALWAYS;
1099 }else{
1100 dwCreationDisposition = OPEN_EXISTING;
1101 }
1102 if( flags & SQLITE_OPEN_MAIN_DB ){
1103 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
1104 }else{
1105 dwShareMode = 0;
1106 }
1107 if( flags & (SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_TEMP_JOURNAL
1108 | SQLITE_OPEN_SUBJOURNAL) ){
1109 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
1110 | FILE_ATTRIBUTE_HIDDEN
1111 | FILE_FLAG_DELETE_ON_CLOSE;
1112 }else{
1113 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
1114 }
1115 if( flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ){
1116 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
1117 }else{
1118 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
1119 }
1120 if( isNT() ){
1121 h = CreateFileW((WCHAR*)zConverted,
1122 dwDesiredAccess,
1123 dwShareMode,
1124 NULL,
1125 dwCreationDisposition,
1126 dwFlagsAndAttributes,
1127 NULL
1128 );
1129 }else{
1130#if OS_WINCE
1131 return SQLITE_NOMEM;
1132#else
1133 h = CreateFileA((char*)zConverted,
1134 dwDesiredAccess,
1135 dwShareMode,
1136 NULL,
1137 dwCreationDisposition,
1138 dwFlagsAndAttributes,
1139 NULL
1140 );
1141#endif
1142 }
1143 if( h==INVALID_HANDLE_VALUE ){
1144 free(zConverted);
1145 if( flags & SQLITE_OPEN_READWRITE ){
1146 return winOpen(0, zName, id,
1147 ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
1148 }else{
1149 return SQLITE_CANTOPEN;
1150 }
1151 }
1152 if( pOutFlags ){
1153 if( flags & SQLITE_OPEN_READWRITE ){
1154 *pOutFlags = SQLITE_OPEN_READWRITE;
1155 }else{
1156 *pOutFlags = SQLITE_OPEN_READONLY;
1157 }
1158 }
1159 memset(pFile, 0, sizeof(*pFile));
1160 pFile->pMethod = &winIoMethod;
1161 pFile->h = h;
1162#if OS_WINCE
1163 if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
1164 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
1165 && !winceCreateLock(zFilename, pFile)
1166 ){
1167 CloseHandle(h);
1168 free(zConverted);
1169 return SQLITE_CANTOPEN;
1170 }
1171 if( dwFlagsAndAttributes & FILE_FLAG_DELETE_ON_CLOSE ){
1172 pFile->zDeleteOnClose = zConverted;
1173 }else
1174#endif
1175 {
1176 free(zConverted);
1177 }
1178 OpenCounter(+1);
1179 return SQLITE_OK;
1180}
1181
1182/*
1183** Delete the named file.
1184**
1185** Note that windows does not allow a file to be deleted if some other
1186** process has it open. Sometimes a virus scanner or indexing program
1187** will open a journal file shortly after it is created in order to do
1188** whatever it is it does. While this other process is holding the
1189** file open, we will be unable to delete it. To work around this
1190** problem, we delay 100 milliseconds and try to delete again. Up
1191** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
1192** up and returning an error.
1193*/
1194#define MX_DELETION_ATTEMPTS 3
1195static int winDelete(
1196 sqlite3_vfs *pVfs, /* Not used on win32 */
1197 const char *zFilename, /* Name of file to delete */
1198 int syncDir /* Not used on win32 */
1199){
1200 int cnt = 0;
1201 int rc;
1202 void *zConverted = convertUtf8Filename(zFilename);
1203 if( zConverted==0 ){
1204 return SQLITE_NOMEM;
1205 }
1206 SimulateIOError(return SQLITE_IOERR_DELETE);
1207 if( isNT() ){
1208 do{
1209 rc = DeleteFileW(zConverted);
1210 }while( rc==0 && GetFileAttributesW(zConverted)!=0xffffffff
1211 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
1212 }else{
1213#if OS_WINCE
1214 return SQLITE_NOMEM;
1215#else
1216 do{
1217 rc = DeleteFileA(zConverted);
1218 }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff
1219 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
1220#endif
1221 }
1222 free(zConverted);
1223 OSTRACE2("DELETE \"%s\"\n", zFilename);
1224 return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
1225}
1226
1227/*
1228** Check the existance and status of a file.
1229*/
1230static int winAccess(
1231 sqlite3_vfs *pVfs, /* Not used on win32 */
1232 const char *zFilename, /* Name of file to check */
1233 int flags /* Type of test to make on this file */
1234){
1235 DWORD attr;
1236 int rc;
1237 void *zConverted = convertUtf8Filename(zFilename);
1238 if( zConverted==0 ){
1239 return SQLITE_NOMEM;
1240 }
1241 if( isNT() ){
1242 attr = GetFileAttributesW((WCHAR*)zConverted);
1243 }else{
1244#if OS_WINCE
1245 return SQLITE_NOMEM;
1246#else
1247 attr = GetFileAttributesA((char*)zConverted);
1248#endif
1249 }
1250 free(zConverted);
1251 switch( flags ){
1252 case SQLITE_ACCESS_READ:
1253 case SQLITE_ACCESS_EXISTS:
1254 rc = attr!=0xffffffff;
1255 break;
1256 case SQLITE_ACCESS_READWRITE:
1257 rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
1258 break;
1259 default:
1260 assert(!"Invalid flags argument");
1261 }
1262 return rc;
1263}
1264
1265
1266/*
1267** Create a temporary file name in zBuf. zBuf must be big enough to
1268** hold at pVfs->mxPathname characters.
1269*/
1270static int winGetTempname(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
1271 static char zChars[] =
1272 "abcdefghijklmnopqrstuvwxyz"
1273 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1274 "0123456789";
1275 int i, j;
1276 char zTempPath[MAX_PATH+1];
1277 if( sqlite3_temp_directory ){
1278 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
1279 }else if( isNT() ){
1280 char *zMulti;
1281 WCHAR zWidePath[MAX_PATH];
1282 GetTempPathW(MAX_PATH-30, zWidePath);
1283 zMulti = unicodeToUtf8(zWidePath);
1284 if( zMulti ){
1285 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
1286 free(zMulti);
1287 }else{
1288 return SQLITE_NOMEM;
1289 }
1290 }else{
1291 char *zUtf8;
1292 char zMbcsPath[MAX_PATH];
1293 GetTempPathA(MAX_PATH-30, zMbcsPath);
1294 zUtf8 = mbcsToUtf8(zMbcsPath);
1295 if( zUtf8 ){
1296 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
1297 free(zUtf8);
1298 }else{
1299 return SQLITE_NOMEM;
1300 }
1301 }
1302 for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
1303 zTempPath[i] = 0;
1304 sqlite3_snprintf(pVfs->mxPathname-30, zBuf,
1305 "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
1306 j = strlen(zBuf);
1307 sqlite3Randomness(20, &zBuf[j]);
1308 for(i=0; i<20; i++, j++){
1309 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
1310 }
1311 zBuf[j] = 0;
1312 OSTRACE2("TEMP FILENAME: %s\n", zBuf);
1313 return SQLITE_OK;
1314}
1315
1316/*
1317** Turn a relative pathname into a full pathname. Write the full
1318** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
1319** bytes in size.
1320*/
1321static int winFullPathname(
1322 sqlite3_vfs *pVfs, /* Pointer to vfs object */
1323 const char *zRelative, /* Possibly relative input path */
1324 int nFull, /* Size of output buffer in bytes */
1325 char *zFull /* Output buffer */
1326){
1327
1328#if defined(__CYGWIN__)
1329 cygwin_conv_to_full_win32_path(zRelative, zFull);
1330 return SQLITE_OK;
1331#endif
1332
1333#if OS_WINCE
1334 /* WinCE has no concept of a relative pathname, or so I am told. */
1335 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
1336#endif
1337
1338#if !OS_WINCE && !defined(__CYGWIN__)
1339 int nByte;
1340 void *zConverted;
1341 char *zOut;
1342 zConverted = convertUtf8Filename(zRelative);
1343 if( isNT() ){
1344 WCHAR *zTemp;
1345 nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
1346 zTemp = malloc( nByte*sizeof(zTemp[0]) );
1347 if( zTemp==0 ){
1348 free(zConverted);
1349 return SQLITE_NOMEM;
1350 }
1351 GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
1352 free(zConverted);
1353 zOut = unicodeToUtf8(zTemp);
1354 free(zTemp);
1355 }else{
1356 char *zTemp;
1357 nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
1358 zTemp = malloc( nByte*sizeof(zTemp[0]) );
1359 if( zTemp==0 ){
1360 free(zConverted);
1361 return SQLITE_NOMEM;
1362 }
1363 GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
1364 free(zConverted);
1365 zOut = mbcsToUtf8(zTemp);
1366 free(zTemp);
1367 }
1368 if( zOut ){
1369 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
1370 free(zOut);
1371 return SQLITE_OK;
1372 }else{
1373 return SQLITE_NOMEM;
1374 }
1375#endif
1376}
1377
1378#ifndef SQLITE_OMIT_LOAD_EXTENSION
1379/*
1380** Interfaces for opening a shared library, finding entry points
1381** within the shared library, and closing the shared library.
1382*/
1383/*
1384** Interfaces for opening a shared library, finding entry points
1385** within the shared library, and closing the shared library.
1386*/
1387static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
1388 HANDLE h;
1389 void *zConverted = convertUtf8Filename(zFilename);
1390 if( zConverted==0 ){
1391 return 0;
1392 }
1393 if( isNT() ){
1394 h = LoadLibraryW((WCHAR*)zConverted);
1395 }else{
1396#if OS_WINCE
1397 return 0;
1398#else
1399 h = LoadLibraryA((char*)zConverted);
1400#endif
1401 }
1402 free(zConverted);
1403 return (void*)h;
1404}
1405static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
1406 FormatMessage(
1407 FORMAT_MESSAGE_FROM_SYSTEM,
1408 NULL,
1409 GetLastError(),
1410 0,
1411 zBufOut,
1412 nBuf-1,
1413 0
1414 );
1415}
1416void *winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
1417#if OS_WINCE
1418 /* The GetProcAddressA() routine is only available on wince. */
1419 return GetProcAddressA((HANDLE)pHandle, zSymbol);
1420#else
1421 /* All other windows platforms expect GetProcAddress() to take
1422 ** an Ansi string regardless of the _UNICODE setting */
1423 return GetProcAddress((HANDLE)pHandle, zSymbol);
1424#endif
1425}
1426void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
1427 FreeLibrary((HANDLE)pHandle);
1428}
1429#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
1430 #define winDlOpen 0
1431 #define winDlError 0
1432 #define winDlSym 0
1433 #define winDlClose 0
1434#endif
1435
1436
1437/*
1438** Write up to nBuf bytes of randomness into zBuf.
1439*/
1440static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
1441 int n = 0;
1442 if( sizeof(SYSTEMTIME)<=nBuf-n ){
1443 SYSTEMTIME x;
1444 GetSystemTime(&x);
1445 memcpy(&zBuf[n], &x, sizeof(x));
1446 n += sizeof(x);
1447 }
1448 if( sizeof(DWORD)<=nBuf-n ){
1449 DWORD pid = GetCurrentProcessId();
1450 memcpy(&zBuf[n], &pid, sizeof(pid));
1451 n += sizeof(pid);
1452 }
1453 if( sizeof(DWORD)<=nBuf-n ){
1454 DWORD cnt = GetTickCount();
1455 memcpy(&zBuf[n], &cnt, sizeof(cnt));
1456 n += sizeof(cnt);
1457 }
1458 if( sizeof(LARGE_INTEGER)<=nBuf-n ){
1459 LARGE_INTEGER i;
1460 QueryPerformanceCounter(&i);
1461 memcpy(&zBuf[n], &i, sizeof(i));
1462 n += sizeof(i);
1463 }
1464 return n;
1465}
1466
1467
1468/*
1469** Sleep for a little while. Return the amount of time slept.
1470*/
1471static int winSleep(sqlite3_vfs *pVfs, int microsec){
1472 Sleep((microsec+999)/1000);
1473 return ((microsec+999)/1000)*1000;
1474}
1475
1476/*
1477** The following variable, if set to a non-zero value, becomes the result
1478** returned from sqlite3OsCurrentTime(). This is used for testing.
1479*/
1480#ifdef SQLITE_TEST
1481int sqlite3_current_time = 0;
1482#endif
1483
1484/*
1485** Find the current time (in Universal Coordinated Time). Write the
1486** current time and date as a Julian Day number into *prNow and
1487** return 0. Return 1 if the time and date cannot be found.
1488*/
1489int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
1490 FILETIME ft;
1491 /* FILETIME structure is a 64-bit value representing the number of
1492 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
1493 */
1494 double now;
1495#if OS_WINCE
1496 SYSTEMTIME time;
1497 GetSystemTime(&time);
1498 SystemTimeToFileTime(&time,&ft);
1499#else
1500 GetSystemTimeAsFileTime( &ft );
1501#endif
1502 now = ((double)ft.dwHighDateTime) * 4294967296.0;
1503 *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5;
1504#ifdef SQLITE_TEST
1505 if( sqlite3_current_time ){
1506 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
1507 }
1508#endif
1509 return 0;
1510}
1511
1512
1513/*
1514** Return a pointer to the sqlite3DefaultVfs structure. We use
1515** a function rather than give the structure global scope because
1516** some compilers (MSVC) do not allow forward declarations of
1517** initialized structures.
1518*/
1519sqlite3_vfs *sqlite3OsDefaultVfs(void){
1520 static sqlite3_vfs winVfs = {
1521 1, /* iVersion */
1522 sizeof(winFile), /* szOsFile */
1523 MAX_PATH, /* mxPathname */
1524 0, /* pNext */
1525 "win32", /* zName */
1526 0, /* pAppData */
1527
1528 winOpen, /* xOpen */
1529 winDelete, /* xDelete */
1530 winAccess, /* xAccess */
1531 winGetTempname, /* xGetTempName */
1532 winFullPathname, /* xFullPathname */
1533 winDlOpen, /* xDlOpen */
1534 winDlError, /* xDlError */
1535 winDlSym, /* xDlSym */
1536 winDlClose, /* xDlClose */
1537 winRandomness, /* xRandomness */
1538 winSleep, /* xSleep */
1539 winCurrentTime /* xCurrentTime */
1540 };
1541
1542 return &winVfs;
1543}
1544
1545#endif /* OS_WIN */
diff --git a/libraries/sqlite/win32/pager.c b/libraries/sqlite/win32/pager.c
new file mode 100755
index 0000000..b0ad715
--- /dev/null
+++ b/libraries/sqlite/win32/pager.c
@@ -0,0 +1,5104 @@
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.
10**
11*************************************************************************
12** This is the implementation of the page cache subsystem or "pager".
13**
14** The pager is used to access a database disk file. It implements
15** atomic commit and rollback through the use of a journal file that
16** is separate from the database file. The pager also implements file
17** locking to prevent two processes from writing the same database
18** file simultaneously, or one process from reading the database while
19** another is writing.
20**
21** @(#) $Id: pager.c,v 1.392 2007/10/03 15:22:26 danielk1977 Exp $
22*/
23#ifndef SQLITE_OMIT_DISKIO
24#include "sqliteInt.h"
25#include <assert.h>
26#include <string.h>
27
28/*
29** Macros for troubleshooting. Normally turned off
30*/
31#if 0
32#define sqlite3DebugPrintf printf
33#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
38#else
39#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
44#endif
45
46/*
47** The following two macros are used within the PAGERTRACEX() macros above
48** to print out file-descriptors.
49**
50** PAGERID() takes a pointer to a Pager struct as it's argument. The
51** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
52** struct as it's argument.
53*/
54#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
56
57/*
58** The page cache as a whole is always in one of the following
59** states:
60**
61** PAGER_UNLOCK The page cache is not currently reading or
62** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
66** PAGER_SHARED The page cache is reading the database.
67** Writing is not permitted. There can be
68** multiple readers accessing the same database
69** file at the same time.
70**
71** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
76** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
79** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
83** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
86** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
89**
90** The page cache comes up in PAGER_UNLOCK. The first time a
91** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
92** After all pages have been released using sqlite_page_unref(),
93** the state transitions back to PAGER_UNLOCK. The first time
94** that sqlite3PagerWrite() is called, the state transitions to
95** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
96** called on an outstanding page which means that the pager must
97** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
98** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
104*/
105#define PAGER_UNLOCK 0
106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
110
111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
127
128/*
129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
134typedef struct PgHdr PgHdr;
135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
148** fsync() operation before it's memory can be reclaimed. If no such
149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
168/*
169** Each in-memory image of a page begins with the following header.
170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
172**
173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
183** is cleared again when the page content is written back to the original
184** database file.
185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
216** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
217**
218** The pPager->aInJournal[] array is only valid for the original
219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
262 PagerLruLink free; /* Next and previous free pages */
263 PgHdr *pNextAll; /* A list of all pages */
264 u8 inJournal; /* TRUE if has been written to journal */
265 u8 dirty; /* TRUE if we need to write back changes */
266 u8 needSync; /* Sync journal before writing this page */
267 u8 alwaysRollback; /* Disable DontRollback() for this page */
268 u8 needRead; /* Read content if PagerWrite() is called */
269 short int nRef; /* Number of users of this page */
270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
279};
280
281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
298};
299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
306#else
307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
309#endif
310
311/*
312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
314*/
315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
317#define PGHDR_TO_HIST(P,PGR) \
318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
319
320/*
321** A open page cache is an instance of the following structure.
322**
323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
329** APIs, they may still be used successfully.
330*/
331struct Pager {
332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
333 u8 journalOpen; /* True if journal file descriptors is valid */
334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
336 u8 noReadlock; /* Do not bother to obtain readlocks */
337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
340 u8 noSync; /* Do not sync the journal if true */
341 u8 fullSync; /* Do extra syncs of the journal for robustness */
342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
347 u8 dirtyCache; /* True if cached pages have changed */
348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
349 u8 memDb; /* True to inhibit all file I/O */
350 u8 setMaster; /* True if a m-j name has been written to jrnl */
351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
353 u8 changeCountDone; /* Set after incrementing the change-counter */
354 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
355 int errCode; /* One of several kinds of errors */
356 int dbSize; /* Number of pages in the file */
357 int origDbSize; /* dbSize before the current change */
358 int stmtSize; /* Size of database (in pages) at stmt_begin() */
359 int nRec; /* Number of pages written to the journal */
360 u32 cksumInit; /* Quasi-random value added to every checksum */
361 int stmtNRec; /* Number of records in stmt subjournal */
362 int nExtra; /* Add this many bytes to each in-memory page */
363 int pageSize; /* Number of bytes in a page */
364 int nPage; /* Total number of in-memory pages */
365 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
366 int mxPage; /* Maximum number of pages to hold in cache */
367 Pgno mxPgno; /* Maximum allowed size of the database */
368 u8 *aInJournal; /* One bit for each page in the database file */
369 u8 *aInStmt; /* One bit for each page in the database */
370 char *zFilename; /* Name of the database file */
371 char *zJournal; /* Name of the journal file */
372 char *zDirectory; /* Directory hold database and journal files */
373 char *zStmtJrnl; /* Name of the statement journal file */
374 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
375 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
376 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
377 PagerLruList lru; /* LRU list of free pages */
378 PgHdr *pAll; /* List of all pages */
379 PgHdr *pStmt; /* List of pages in the statement subjournal */
380 PgHdr *pDirty; /* List of all dirty pages */
381 i64 journalOff; /* Current byte offset in the journal file */
382 i64 journalHdr; /* Byte offset to previous journal header */
383 i64 stmtHdrOff; /* First journal header written this statement */
384 i64 stmtCksum; /* cksumInit when statement was started */
385 i64 stmtJSize; /* Size of journal at stmt_begin() */
386 int sectorSize; /* Assumed sector size during rollback */
387#ifdef SQLITE_TEST
388 int nHit, nMiss; /* Cache hits and missing */
389 int nRead, nWrite; /* Database pages read/written */
390#endif
391 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
392 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
393#ifdef SQLITE_HAS_CODEC
394 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
395 void *pCodecArg; /* First argument to xCodec() */
396#endif
397 int nHash; /* Size of the pager hash table */
398 PgHdr **aHash; /* Hash table to map page number to PgHdr */
399#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
400 Pager *pNext; /* Doubly linked list of pagers on which */
401 Pager *pPrev; /* sqlite3_release_memory() will work */
402 int iInUseMM; /* Non-zero if unavailable to MM */
403 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
404#endif
405 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
406 char dbFileVers[16]; /* Changes whenever database file changes */
407};
408
409/*
410** The following global variables hold counters used for
411** testing purposes only. These variables do not exist in
412** a non-testing build. These variables are not thread-safe.
413*/
414#ifdef SQLITE_TEST
415int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
416int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
417int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
418int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
419# define PAGER_INCR(v) v++
420#else
421# define PAGER_INCR(v)
422#endif
423
424/*
425** The following variable points to the head of a double-linked list
426** of all pagers that are eligible for page stealing by the
427** sqlite3_release_memory() interface. Access to this list is
428** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
429*/
430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
431static Pager *sqlite3PagerList = 0;
432static PagerLruList sqlite3LruPageList = {0, 0, 0};
433#endif
434
435
436/*
437** Journal files begin with the following magic string. The data
438** was obtained from /dev/random. It is used only as a sanity check.
439**
440** Since version 2.8.0, the journal format contains additional sanity
441** checking information. If the power fails while the journal is begin
442** written, semi-random garbage data might appear in the journal
443** file after power is restored. If an attempt is then made
444** to roll the journal back, the database could be corrupted. The additional
445** sanity checking data is an attempt to discover the garbage in the
446** journal and ignore it.
447**
448** The sanity checking information for the new journal format consists
449** of a 32-bit checksum on each page of data. The checksum covers both
450** the page number and the pPager->pageSize bytes of data for the page.
451** This cksum is initialized to a 32-bit random value that appears in the
452** journal file right after the header. The random initializer is important,
453** because garbage data that appears at the end of a journal is likely
454** data that was once in other files that have now been deleted. If the
455** garbage data came from an obsolete journal file, the checksums might
456** be correct. But by initializing the checksum to random value which
457** is different for every journal, we minimize that risk.
458*/
459static const unsigned char aJournalMagic[] = {
460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
461};
462
463/*
464** The size of the header and of each page in the journal is determined
465** by the following macros.
466*/
467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
468
469/*
470** The journal header size for this pager. In the future, this could be
471** set to some value read from the disk controller. The important
472** characteristic is that it is the same size as a disk sector.
473*/
474#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
475
476/*
477** The macro MEMDB is true if we are dealing with an in-memory database.
478** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
479** the value of MEMDB will be a constant and the compiler will optimize
480** out code that would never execute.
481*/
482#ifdef SQLITE_OMIT_MEMORYDB
483# define MEMDB 0
484#else
485# define MEMDB pPager->memDb
486#endif
487
488/*
489** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
490** reserved for working around a windows/posix incompatibility). It is
491** used in the journal to signify that the remainder of the journal file
492** is devoted to storing a master journal name - there are no more pages to
493** roll back. See comments for function writeMasterJournal() for details.
494*/
495/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
496#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
497
498/*
499** The maximum legal page number is (2^31 - 1).
500*/
501#define PAGER_MAX_PGNO 2147483647
502
503/*
504** The pagerEnter() and pagerLeave() routines acquire and release
505** a mutex on each pager. The mutex is recursive.
506**
507** This is a special-purpose mutex. It only provides mutual exclusion
508** between the Btree and the Memory Management sqlite3_release_memory()
509** function. It does not prevent, for example, two Btrees from accessing
510** the same pager at the same time. Other general-purpose mutexes in
511** the btree layer handle that chore.
512*/
513#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
514 static void pagerEnter(Pager *p){
515 p->iInUseDB++;
516 if( p->iInUseMM && p->iInUseDB==1 ){
517 sqlite3_mutex *mutex;
518 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
519 p->iInUseDB = 0;
520 sqlite3_mutex_enter(mutex);
521 p->iInUseDB = 1;
522 sqlite3_mutex_leave(mutex);
523 }
524 assert( p->iInUseMM==0 );
525 }
526 static void pagerLeave(Pager *p){
527 p->iInUseDB--;
528 assert( p->iInUseDB>=0 );
529 }
530#else
531# define pagerEnter(X)
532# define pagerLeave(X)
533#endif
534
535/*
536** Enable reference count tracking (for debugging) here:
537*/
538#ifdef SQLITE_DEBUG
539 int pager3_refinfo_enable = 0;
540 static void pager_refinfo(PgHdr *p){
541 static int cnt = 0;
542 if( !pager3_refinfo_enable ) return;
543 sqlite3DebugPrintf(
544 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
545 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
546 );
547 cnt++; /* Something to set a breakpoint on */
548 }
549# define REFINFO(X) pager_refinfo(X)
550#else
551# define REFINFO(X)
552#endif
553
554/*
555** Add page pPg to the end of the linked list managed by structure
556** pList (pPg becomes the last entry in the list - the most recently
557** used). Argument pLink should point to either pPg->free or pPg->gfree,
558** depending on whether pPg is being added to the pager-specific or
559** global LRU list.
560*/
561static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
562 pLink->pNext = 0;
563 pLink->pPrev = pList->pLast;
564
565#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
566 assert(pLink==&pPg->free || pLink==&pPg->gfree);
567 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
568#endif
569
570 if( pList->pLast ){
571 int iOff = (char *)pLink - (char *)pPg;
572 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
573 pLastLink->pNext = pPg;
574 }else{
575 assert(!pList->pFirst);
576 pList->pFirst = pPg;
577 }
578
579 pList->pLast = pPg;
580 if( !pList->pFirstSynced && pPg->needSync==0 ){
581 pList->pFirstSynced = pPg;
582 }
583}
584
585/*
586** Remove pPg from the list managed by the structure pointed to by pList.
587**
588** Argument pLink should point to either pPg->free or pPg->gfree, depending
589** on whether pPg is being added to the pager-specific or global LRU list.
590*/
591static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
592 int iOff = (char *)pLink - (char *)pPg;
593
594#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
595 assert(pLink==&pPg->free || pLink==&pPg->gfree);
596 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
597#endif
598
599 if( pPg==pList->pFirst ){
600 pList->pFirst = pLink->pNext;
601 }
602 if( pPg==pList->pLast ){
603 pList->pLast = pLink->pPrev;
604 }
605 if( pLink->pPrev ){
606 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
607 pPrevLink->pNext = pLink->pNext;
608 }
609 if( pLink->pNext ){
610 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
611 pNextLink->pPrev = pLink->pPrev;
612 }
613 if( pPg==pList->pFirstSynced ){
614 PgHdr *p = pLink->pNext;
615 while( p && p->needSync ){
616 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
617 p = pL->pNext;
618 }
619 pList->pFirstSynced = p;
620 }
621
622 pLink->pNext = pLink->pPrev = 0;
623}
624
625/*
626** Add page pPg to the list of free pages for the pager. If
627** memory-management is enabled, also add the page to the global
628** list of free pages.
629*/
630static void lruListAdd(PgHdr *pPg){
631 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
632#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
633 if( !pPg->pPager->memDb ){
634 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
635 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
636 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
637 }
638#endif
639}
640
641/*
642** Remove page pPg from the list of free pages for the associated pager.
643** If memory-management is enabled, also remove pPg from the global list
644** of free pages.
645*/
646static void lruListRemove(PgHdr *pPg){
647 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
648#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
649 if( !pPg->pPager->memDb ){
650 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
651 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
652 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
653 }
654#endif
655}
656
657/*
658** This function is called just after the needSync flag has been cleared
659** from all pages managed by pPager (usually because the journal file
660** has just been synced). It updates the pPager->lru.pFirstSynced variable
661** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
662** variable also.
663*/
664static void lruListSetFirstSynced(Pager *pPager){
665 pPager->lru.pFirstSynced = pPager->lru.pFirst;
666#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
667 if( !pPager->memDb ){
668 PgHdr *p;
669 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
670 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
671 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
672 sqlite3LruPageList.pFirstSynced = p;
673 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
674 }
675#endif
676}
677
678/*
679** Return true if page *pPg has already been written to the statement
680** journal (or statement snapshot has been created, if *pPg is part
681** of an in-memory database).
682*/
683static int pageInStatement(PgHdr *pPg){
684 Pager *pPager = pPg->pPager;
685 if( MEMDB ){
686 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
687 }else{
688 Pgno pgno = pPg->pgno;
689 u8 *a = pPager->aInStmt;
690 return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
691 }
692}
693
694/*
695** Change the size of the pager hash table to N. N must be a power
696** of two.
697*/
698static void pager_resize_hash_table(Pager *pPager, int N){
699 PgHdr **aHash, *pPg;
700 assert( N>0 && (N&(N-1))==0 );
701 pagerLeave(pPager);
702 sqlite3MallocBenignFailure((int)pPager->aHash);
703 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
704 pagerEnter(pPager);
705 if( aHash==0 ){
706 /* Failure to rehash is not an error. It is only a performance hit. */
707 return;
708 }
709 sqlite3_free(pPager->aHash);
710 pPager->nHash = N;
711 pPager->aHash = aHash;
712 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
713 int h;
714 if( pPg->pgno==0 ){
715 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
716 continue;
717 }
718 h = pPg->pgno & (N-1);
719 pPg->pNextHash = aHash[h];
720 if( aHash[h] ){
721 aHash[h]->pPrevHash = pPg;
722 }
723 aHash[h] = pPg;
724 pPg->pPrevHash = 0;
725 }
726}
727
728/*
729** Read a 32-bit integer from the given file descriptor. Store the integer
730** that is read in *pRes. Return SQLITE_OK if everything worked, or an
731** error code is something goes wrong.
732**
733** All values are stored on disk as big-endian.
734*/
735static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
736 unsigned char ac[4];
737 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
738 if( rc==SQLITE_OK ){
739 *pRes = sqlite3Get4byte(ac);
740 }
741 return rc;
742}
743
744/*
745** Write a 32-bit integer into a string buffer in big-endian byte order.
746*/
747#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
748
749/*
750** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
751** on success or an error code is something goes wrong.
752*/
753static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
754 char ac[4];
755 put32bits(ac, val);
756 return sqlite3OsWrite(fd, ac, 4, offset);
757}
758
759/*
760** If file pFd is open, call sqlite3OsUnlock() on it.
761*/
762static int osUnlock(sqlite3_file *pFd, int eLock){
763 if( !pFd->pMethods ){
764 return SQLITE_OK;
765 }
766 return sqlite3OsUnlock(pFd, eLock);
767}
768
769/*
770** This function determines whether or not the atomic-write optimization
771** can be used with this pager. The optimization can be used if:
772**
773** (a) the value returned by OsDeviceCharacteristics() indicates that
774** a database page may be written atomically, and
775** (b) the value returned by OsSectorSize() is less than or equal
776** to the page size.
777**
778** If the optimization cannot be used, 0 is returned. If it can be used,
779** then the value returned is the size of the journal file when it
780** contains rollback data for exactly one page.
781*/
782#ifdef SQLITE_ENABLE_ATOMIC_WRITE
783static int jrnlBufferSize(Pager *pPager){
784 int dc; /* Device characteristics */
785 int nSector; /* Sector size */
786 int nPage; /* Page size */
787 sqlite3_file *fd = pPager->fd;
788
789 if( fd->pMethods ){
790 dc = sqlite3OsDeviceCharacteristics(fd);
791 nSector = sqlite3OsSectorSize(fd);
792 nPage = pPager->pageSize;
793 }
794
795 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
796 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
797
798 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
799 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
800 }
801 return 0;
802}
803#endif
804
805/*
806** This function should be called when an error occurs within the pager
807** code. The first argument is a pointer to the pager structure, the
808** second the error-code about to be returned by a pager API function.
809** The value returned is a copy of the second argument to this function.
810**
811** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
812** the error becomes persistent. Until the persisten error is cleared,
813** subsequent API calls on this Pager will immediately return the same
814** error code.
815**
816** A persistent error indicates that the contents of the pager-cache
817** cannot be trusted. This state can be cleared by completely discarding
818** the contents of the pager-cache. If a transaction was active when
819** the persistent error occured, then the rollback journal may need
820** to be replayed.
821*/
822static void pager_unlock(Pager *pPager);
823static int pager_error(Pager *pPager, int rc){
824 int rc2 = rc & 0xff;
825 assert(
826 pPager->errCode==SQLITE_FULL ||
827 pPager->errCode==SQLITE_OK ||
828 (pPager->errCode & 0xff)==SQLITE_IOERR
829 );
830 if(
831 rc2==SQLITE_FULL ||
832 rc2==SQLITE_IOERR ||
833 rc2==SQLITE_CORRUPT
834 ){
835 pPager->errCode = rc;
836 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
837 /* If the pager is already unlocked, call pager_unlock() now to
838 ** clear the error state and ensure that the pager-cache is
839 ** completely empty.
840 */
841 pager_unlock(pPager);
842 }
843 }
844 return rc;
845}
846
847/*
848** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
849** on the cache using a hash function. This is used for testing
850** and debugging only.
851*/
852#ifdef SQLITE_CHECK_PAGES
853/*
854** Return a 32-bit hash of the page data for pPage.
855*/
856static u32 pager_datahash(int nByte, unsigned char *pData){
857 u32 hash = 0;
858 int i;
859 for(i=0; i<nByte; i++){
860 hash = (hash*1039) + pData[i];
861 }
862 return hash;
863}
864static u32 pager_pagehash(PgHdr *pPage){
865 return pager_datahash(pPage->pPager->pageSize,
866 (unsigned char *)PGHDR_TO_DATA(pPage));
867}
868
869/*
870** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
871** is defined, and NDEBUG is not defined, an assert() statement checks
872** that the page is either dirty or still matches the calculated page-hash.
873*/
874#define CHECK_PAGE(x) checkPage(x)
875static void checkPage(PgHdr *pPg){
876 Pager *pPager = pPg->pPager;
877 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
878 pPg->pageHash==pager_pagehash(pPg) );
879}
880
881#else
882#define pager_datahash(X,Y) 0
883#define pager_pagehash(X) 0
884#define CHECK_PAGE(x)
885#endif
886
887/*
888** When this is called the journal file for pager pPager must be open.
889** The master journal file name is read from the end of the file and
890** written into memory supplied by the caller.
891**
892** zMaster must point to a buffer of at least nMaster bytes allocated by
893** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
894** enough space to write the master journal name). If the master journal
895** name in the journal is longer than nMaster bytes (including a
896** nul-terminator), then this is handled as if no master journal name
897** were present in the journal.
898**
899** If no master journal file name is present zMaster[0] is set to 0 and
900** SQLITE_OK returned.
901*/
902static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
903 int rc;
904 u32 len;
905 i64 szJ;
906 u32 cksum;
907 int i;
908 unsigned char aMagic[8]; /* A buffer to hold the magic header */
909
910 zMaster[0] = '\0';
911
912 rc = sqlite3OsFileSize(pJrnl, &szJ);
913 if( rc!=SQLITE_OK || szJ<16 ) return rc;
914
915 rc = read32bits(pJrnl, szJ-16, &len);
916 if( rc!=SQLITE_OK ) return rc;
917
918 if( len>=nMaster ){
919 return SQLITE_OK;
920 }
921
922 rc = read32bits(pJrnl, szJ-12, &cksum);
923 if( rc!=SQLITE_OK ) return rc;
924
925 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
926 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
927
928 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
929 if( rc!=SQLITE_OK ){
930 return rc;
931 }
932 zMaster[len] = '\0';
933
934 /* See if the checksum matches the master journal name */
935 for(i=0; i<len; i++){
936 cksum -= zMaster[i];
937 }
938 if( cksum ){
939 /* If the checksum doesn't add up, then one or more of the disk sectors
940 ** containing the master journal filename is corrupted. This means
941 ** definitely roll back, so just return SQLITE_OK and report a (nul)
942 ** master-journal filename.
943 */
944 zMaster[0] = '\0';
945 }
946
947 return SQLITE_OK;
948}
949
950/*
951** Seek the journal file descriptor to the next sector boundary where a
952** journal header may be read or written. Pager.journalOff is updated with
953** the new seek offset.
954**
955** i.e for a sector size of 512:
956**
957** Input Offset Output Offset
958** ---------------------------------------
959** 0 0
960** 512 512
961** 100 512
962** 2000 2048
963**
964*/
965static void seekJournalHdr(Pager *pPager){
966 i64 offset = 0;
967 i64 c = pPager->journalOff;
968 if( c ){
969 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
970 }
971 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
972 assert( offset>=c );
973 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
974 pPager->journalOff = offset;
975}
976
977/*
978** The journal file must be open when this routine is called. A journal
979** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
980** current location.
981**
982** The format for the journal header is as follows:
983** - 8 bytes: Magic identifying journal format.
984** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
985** - 4 bytes: Random number used for page hash.
986** - 4 bytes: Initial database page count.
987** - 4 bytes: Sector size used by the process that wrote this journal.
988**
989** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
990*/
991static int writeJournalHdr(Pager *pPager){
992 char zHeader[sizeof(aJournalMagic)+16];
993 int rc;
994
995 if( pPager->stmtHdrOff==0 ){
996 pPager->stmtHdrOff = pPager->journalOff;
997 }
998
999 seekJournalHdr(pPager);
1000 pPager->journalHdr = pPager->journalOff;
1001
1002 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
1003
1004 /*
1005 ** Write the nRec Field - the number of page records that follow this
1006 ** journal header. Normally, zero is written to this value at this time.
1007 ** After the records are added to the journal (and the journal synced,
1008 ** if in full-sync mode), the zero is overwritten with the true number
1009 ** of records (see syncJournal()).
1010 **
1011 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1012 ** reading the journal this value tells SQLite to assume that the
1013 ** rest of the journal file contains valid page records. This assumption
1014 ** is dangerous, as if a failure occured whilst writing to the journal
1015 ** file it may contain some garbage data. There are two scenarios
1016 ** where this risk can be ignored:
1017 **
1018 ** * When the pager is in no-sync mode. Corruption can follow a
1019 ** power failure in this case anyway.
1020 **
1021 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1022 ** that garbage data is never appended to the journal file.
1023 */
1024 assert(pPager->fd->pMethods||pPager->noSync);
1025 if( (pPager->noSync)
1026 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1027 ){
1028 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1029 }else{
1030 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1031 }
1032
1033 /* The random check-hash initialiser */
1034 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1035 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1036 /* The initial database size */
1037 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1038 /* The assumed sector size for this process */
1039 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
1040 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
1041 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1042 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
1043
1044 /* The journal header has been written successfully. Seek the journal
1045 ** file descriptor to the end of the journal header sector.
1046 */
1047 if( rc==SQLITE_OK ){
1048 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
1049 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
1050 }
1051 return rc;
1052}
1053
1054/*
1055** The journal file must be open when this is called. A journal header file
1056** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1057** file. See comments above function writeJournalHdr() for a description of
1058** the journal header format.
1059**
1060** If the header is read successfully, *nRec is set to the number of
1061** page records following this header and *dbSize is set to the size of the
1062** database before the transaction began, in pages. Also, pPager->cksumInit
1063** is set to the value read from the journal header. SQLITE_OK is returned
1064** in this case.
1065**
1066** If the journal header file appears to be corrupted, SQLITE_DONE is
1067** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1068** cannot be read from the journal file an error code is returned.
1069*/
1070static int readJournalHdr(
1071 Pager *pPager,
1072 i64 journalSize,
1073 u32 *pNRec,
1074 u32 *pDbSize
1075){
1076 int rc;
1077 unsigned char aMagic[8]; /* A buffer to hold the magic header */
1078 i64 jrnlOff;
1079
1080 seekJournalHdr(pPager);
1081 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1082 return SQLITE_DONE;
1083 }
1084 jrnlOff = pPager->journalOff;
1085
1086 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
1087 if( rc ) return rc;
1088 jrnlOff += sizeof(aMagic);
1089
1090 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1091 return SQLITE_DONE;
1092 }
1093
1094 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
1095 if( rc ) return rc;
1096
1097 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
1098 if( rc ) return rc;
1099
1100 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
1101 if( rc ) return rc;
1102
1103 /* Update the assumed sector-size to match the value used by
1104 ** the process that created this journal. If this journal was
1105 ** created by a process other than this one, then this routine
1106 ** is being called from within pager_playback(). The local value
1107 ** of Pager.sectorSize is restored at the end of that routine.
1108 */
1109 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
1110 if( rc ) return rc;
1111
1112 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
1113 return SQLITE_OK;
1114}
1115
1116
1117/*
1118** Write the supplied master journal name into the journal file for pager
1119** pPager at the current location. The master journal name must be the last
1120** thing written to a journal file. If the pager is in full-sync mode, the
1121** journal file descriptor is advanced to the next sector boundary before
1122** anything is written. The format is:
1123**
1124** + 4 bytes: PAGER_MJ_PGNO.
1125** + N bytes: length of master journal name.
1126** + 4 bytes: N
1127** + 4 bytes: Master journal name checksum.
1128** + 8 bytes: aJournalMagic[].
1129**
1130** The master journal page checksum is the sum of the bytes in the master
1131** journal name.
1132**
1133** If zMaster is a NULL pointer (occurs for a single database transaction),
1134** this call is a no-op.
1135*/
1136static int writeMasterJournal(Pager *pPager, const char *zMaster){
1137 int rc;
1138 int len;
1139 int i;
1140 i64 jrnlOff;
1141 u32 cksum = 0;
1142 char zBuf[sizeof(aJournalMagic)+2*4];
1143
1144 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1145 pPager->setMaster = 1;
1146
1147 len = strlen(zMaster);
1148 for(i=0; i<len; i++){
1149 cksum += zMaster[i];
1150 }
1151
1152 /* If in full-sync mode, advance to the next disk sector before writing
1153 ** the master journal name. This is in case the previous page written to
1154 ** the journal has already been synced.
1155 */
1156 if( pPager->fullSync ){
1157 seekJournalHdr(pPager);
1158 }
1159 jrnlOff = pPager->journalOff;
1160 pPager->journalOff += (len+20);
1161
1162 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
1163 if( rc!=SQLITE_OK ) return rc;
1164 jrnlOff += 4;
1165
1166 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
1167 if( rc!=SQLITE_OK ) return rc;
1168 jrnlOff += len;
1169
1170 put32bits(zBuf, len);
1171 put32bits(&zBuf[4], cksum);
1172 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
1173 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
1174 pPager->needSync = !pPager->noSync;
1175 return rc;
1176}
1177
1178/*
1179** Add or remove a page from the list of all pages that are in the
1180** statement journal.
1181**
1182** The Pager keeps a separate list of pages that are currently in
1183** the statement journal. This helps the sqlite3PagerStmtCommit()
1184** routine run MUCH faster for the common case where there are many
1185** pages in memory but only a few are in the statement journal.
1186*/
1187static void page_add_to_stmt_list(PgHdr *pPg){
1188 Pager *pPager = pPg->pPager;
1189 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1190 assert( MEMDB );
1191 if( !pHist->inStmt ){
1192 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1193 if( pPager->pStmt ){
1194 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1195 }
1196 pHist->pNextStmt = pPager->pStmt;
1197 pPager->pStmt = pPg;
1198 pHist->inStmt = 1;
1199 }
1200}
1201
1202/*
1203** Find a page in the hash table given its page number. Return
1204** a pointer to the page or NULL if not found.
1205*/
1206static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
1207 PgHdr *p;
1208 if( pPager->aHash==0 ) return 0;
1209 p = pPager->aHash[pgno & (pPager->nHash-1)];
1210 while( p && p->pgno!=pgno ){
1211 p = p->pNextHash;
1212 }
1213 return p;
1214}
1215
1216/*
1217** Clear the in-memory cache. This routine
1218** sets the state of the pager back to what it was when it was first
1219** opened. Any outstanding pages are invalidated and subsequent attempts
1220** to access those pages will likely result in a coredump.
1221*/
1222static void pager_reset(Pager *pPager){
1223 PgHdr *pPg, *pNext;
1224 if( pPager->errCode ) return;
1225 for(pPg=pPager->pAll; pPg; pPg=pNext){
1226 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1227 PAGER_INCR(sqlite3_pager_pgfree_count);
1228 pNext = pPg->pNextAll;
1229 lruListRemove(pPg);
1230 sqlite3_free(pPg->pData);
1231 sqlite3_free(pPg);
1232 }
1233 assert(pPager->lru.pFirst==0);
1234 assert(pPager->lru.pFirstSynced==0);
1235 assert(pPager->lru.pLast==0);
1236 pPager->pStmt = 0;
1237 pPager->pAll = 0;
1238 pPager->pDirty = 0;
1239 pPager->nHash = 0;
1240 sqlite3_free(pPager->aHash);
1241 pPager->nPage = 0;
1242 pPager->aHash = 0;
1243 pPager->nRef = 0;
1244}
1245
1246/*
1247** Unlock the database file.
1248**
1249** If the pager is currently in error state, discard the contents of
1250** the cache and reset the Pager structure internal state. If there is
1251** an open journal-file, then the next time a shared-lock is obtained
1252** on the pager file (by this or any other process), it will be
1253** treated as a hot-journal and rolled back.
1254*/
1255static void pager_unlock(Pager *pPager){
1256 if( !pPager->exclusiveMode ){
1257 if( !MEMDB ){
1258 if( pPager->fd->pMethods ){
1259 osUnlock(pPager->fd, NO_LOCK);
1260 }
1261 pPager->dbSize = -1;
1262 IOTRACE(("UNLOCK %p\n", pPager))
1263
1264 /* If Pager.errCode is set, the contents of the pager cache cannot be
1265 ** trusted. Now that the pager file is unlocked, the contents of the
1266 ** cache can be discarded and the error code safely cleared.
1267 */
1268 if( pPager->errCode ){
1269 pPager->errCode = SQLITE_OK;
1270 pager_reset(pPager);
1271 if( pPager->stmtOpen ){
1272 sqlite3OsClose(pPager->stfd);
1273 sqlite3_free(pPager->aInStmt);
1274 pPager->aInStmt = 0;
1275 }
1276 if( pPager->journalOpen ){
1277 sqlite3OsClose(pPager->jfd);
1278 pPager->journalOpen = 0;
1279 sqlite3_free(pPager->aInJournal);
1280 pPager->aInJournal = 0;
1281 }
1282 pPager->stmtOpen = 0;
1283 pPager->stmtInUse = 0;
1284 pPager->journalOff = 0;
1285 pPager->journalStarted = 0;
1286 pPager->stmtAutoopen = 0;
1287 pPager->origDbSize = 0;
1288 }
1289 }
1290
1291 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1292 pPager->state = PAGER_UNLOCK;
1293 pPager->changeCountDone = 0;
1294 }
1295 }
1296}
1297
1298/*
1299** Execute a rollback if a transaction is active and unlock the
1300** database file. If the pager has already entered the error state,
1301** do not attempt the rollback.
1302*/
1303static void pagerUnlockAndRollback(Pager *p){
1304 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
1305 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1306 sqlite3PagerRollback(p);
1307 }
1308 pager_unlock(p);
1309 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1310 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
1311}
1312
1313/*
1314** This routine ends a transaction. A transaction is ended by either
1315** a COMMIT or a ROLLBACK.
1316**
1317** When this routine is called, the pager has the journal file open and
1318** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1319** the database lock and acquires a SHARED lock in its place if that is
1320** the appropriate thing to do. Release locks usually is appropriate,
1321** unless we are in exclusive access mode or unless this is a
1322** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1323**
1324** The journal file is either deleted or truncated.
1325**
1326** TODO: Consider keeping the journal file open for temporary databases.
1327** This might give a performance improvement on windows where opening
1328** a file is an expensive operation.
1329*/
1330static int pager_end_transaction(Pager *pPager){
1331 PgHdr *pPg;
1332 int rc = SQLITE_OK;
1333 int rc2 = SQLITE_OK;
1334 assert( !MEMDB );
1335 if( pPager->state<PAGER_RESERVED ){
1336 return SQLITE_OK;
1337 }
1338 sqlite3PagerStmtCommit(pPager);
1339 if( pPager->stmtOpen && !pPager->exclusiveMode ){
1340 sqlite3OsClose(pPager->stfd);
1341 pPager->stmtOpen = 0;
1342 }
1343 if( pPager->journalOpen ){
1344 if( pPager->exclusiveMode
1345 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
1346 pPager->journalOff = 0;
1347 pPager->journalStarted = 0;
1348 }else{
1349 sqlite3OsClose(pPager->jfd);
1350 pPager->journalOpen = 0;
1351 if( rc==SQLITE_OK ){
1352 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1353 }
1354 }
1355 sqlite3_free( pPager->aInJournal );
1356 pPager->aInJournal = 0;
1357 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1358 pPg->inJournal = 0;
1359 pPg->dirty = 0;
1360 pPg->needSync = 0;
1361 pPg->alwaysRollback = 0;
1362#ifdef SQLITE_CHECK_PAGES
1363 pPg->pageHash = pager_pagehash(pPg);
1364#endif
1365 }
1366 pPager->pDirty = 0;
1367 pPager->dirtyCache = 0;
1368 pPager->nRec = 0;
1369 }else{
1370 assert( pPager->aInJournal==0 );
1371 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
1372 }
1373
1374 if( !pPager->exclusiveMode ){
1375 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
1376 pPager->state = PAGER_SHARED;
1377 }else if( pPager->state==PAGER_SYNCED ){
1378 pPager->state = PAGER_EXCLUSIVE;
1379 }
1380 pPager->origDbSize = 0;
1381 pPager->setMaster = 0;
1382 pPager->needSync = 0;
1383 lruListSetFirstSynced(pPager);
1384 pPager->dbSize = -1;
1385
1386 return (rc==SQLITE_OK?rc2:rc);
1387}
1388
1389/*
1390** Compute and return a checksum for the page of data.
1391**
1392** This is not a real checksum. It is really just the sum of the
1393** random initial value and the page number. We experimented with
1394** a checksum of the entire data, but that was found to be too slow.
1395**
1396** Note that the page number is stored at the beginning of data and
1397** the checksum is stored at the end. This is important. If journal
1398** corruption occurs due to a power failure, the most likely scenario
1399** is that one end or the other of the record will be changed. It is
1400** much less likely that the two ends of the journal record will be
1401** correct and the middle be corrupt. Thus, this "checksum" scheme,
1402** though fast and simple, catches the mostly likely kind of corruption.
1403**
1404** FIX ME: Consider adding every 200th (or so) byte of the data to the
1405** checksum. That way if a single page spans 3 or more disk sectors and
1406** only the middle sector is corrupt, we will still have a reasonable
1407** chance of failing the checksum and thus detecting the problem.
1408*/
1409static u32 pager_cksum(Pager *pPager, const u8 *aData){
1410 u32 cksum = pPager->cksumInit;
1411 int i = pPager->pageSize-200;
1412 while( i>0 ){
1413 cksum += aData[i];
1414 i -= 200;
1415 }
1416 return cksum;
1417}
1418
1419/* Forward declaration */
1420static void makeClean(PgHdr*);
1421
1422/*
1423** Read a single page from the journal file opened on file descriptor
1424** jfd. Playback this one page.
1425**
1426** If useCksum==0 it means this journal does not use checksums. Checksums
1427** are not used in statement journals because statement journals do not
1428** need to survive power failures.
1429*/
1430static int pager_playback_one_page(
1431 Pager *pPager,
1432 sqlite3_file *jfd,
1433 i64 offset,
1434 int useCksum
1435){
1436 int rc;
1437 PgHdr *pPg; /* An existing page in the cache */
1438 Pgno pgno; /* The page number of a page in journal */
1439 u32 cksum; /* Checksum used for sanity checking */
1440 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
1441
1442 /* useCksum should be true for the main journal and false for
1443 ** statement journals. Verify that this is always the case
1444 */
1445 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
1446 assert( aData );
1447
1448 rc = read32bits(jfd, offset, &pgno);
1449 if( rc!=SQLITE_OK ) return rc;
1450 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
1451 if( rc!=SQLITE_OK ) return rc;
1452 pPager->journalOff += pPager->pageSize + 4;
1453
1454 /* Sanity checking on the page. This is more important that I originally
1455 ** thought. If a power failure occurs while the journal is being written,
1456 ** it could cause invalid data to be written into the journal. We need to
1457 ** detect this invalid data (with high probability) and ignore it.
1458 */
1459 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
1460 return SQLITE_DONE;
1461 }
1462 if( pgno>(unsigned)pPager->dbSize ){
1463 return SQLITE_OK;
1464 }
1465 if( useCksum ){
1466 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
1467 if( rc ) return rc;
1468 pPager->journalOff += 4;
1469 if( pager_cksum(pPager, aData)!=cksum ){
1470 return SQLITE_DONE;
1471 }
1472 }
1473
1474 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
1475
1476 /* If the pager is in RESERVED state, then there must be a copy of this
1477 ** page in the pager cache. In this case just update the pager cache,
1478 ** not the database file. The page is left marked dirty in this case.
1479 **
1480 ** An exception to the above rule: If the database is in no-sync mode
1481 ** and a page is moved during an incremental vacuum then the page may
1482 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1483 ** during a Movepage() call, then the page may not be in the cache
1484 ** either. So the condition described in the above paragraph is not
1485 ** assert()able.
1486 **
1487 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1488 ** and the main file. The page is then marked not dirty.
1489 **
1490 ** Ticket #1171: The statement journal might contain page content that is
1491 ** different from the page content at the start of the transaction.
1492 ** This occurs when a page is changed prior to the start of a statement
1493 ** then changed again within the statement. When rolling back such a
1494 ** statement we must not write to the original database unless we know
1495 ** for certain that original page contents are synced into the main rollback
1496 ** journal. Otherwise, a power loss might leave modified data in the
1497 ** database file without an entry in the rollback journal that can
1498 ** restore the database to its original form. Two conditions must be
1499 ** met before writing to the database files. (1) the database must be
1500 ** locked. (2) we know that the original page content is fully synced
1501 ** in the main journal either because the page is not in cache or else
1502 ** the page is marked as needSync==0.
1503 */
1504 pPg = pager_lookup(pPager, pgno);
1505 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1506 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
1507 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
1508 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1509 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
1510 if( pPg ){
1511 makeClean(pPg);
1512 }
1513 }
1514 if( pPg ){
1515 /* No page should ever be explicitly rolled back that is in use, except
1516 ** for page 1 which is held in use in order to keep the lock on the
1517 ** database active. However such a page may be rolled back as a result
1518 ** of an internal error resulting in an automatic call to
1519 ** sqlite3PagerRollback().
1520 */
1521 void *pData;
1522 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
1523 pData = PGHDR_TO_DATA(pPg);
1524 memcpy(pData, aData, pPager->pageSize);
1525 if( pPager->xReiniter ){
1526 pPager->xReiniter(pPg, pPager->pageSize);
1527 }
1528#ifdef SQLITE_CHECK_PAGES
1529 pPg->pageHash = pager_pagehash(pPg);
1530#endif
1531 /* If this was page 1, then restore the value of Pager.dbFileVers.
1532 ** Do this before any decoding. */
1533 if( pgno==1 ){
1534 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
1535 }
1536
1537 /* Decode the page just read from disk */
1538 CODEC1(pPager, pData, pPg->pgno, 3);
1539 }
1540 return rc;
1541}
1542
1543/*
1544** Parameter zMaster is the name of a master journal file. A single journal
1545** file that referred to the master journal file has just been rolled back.
1546** This routine checks if it is possible to delete the master journal file,
1547** and does so if it is.
1548**
1549** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1550** available for use within this function.
1551**
1552**
1553** The master journal file contains the names of all child journals.
1554** To tell if a master journal can be deleted, check to each of the
1555** children. If all children are either missing or do not refer to
1556** a different master journal, then this master journal can be deleted.
1557*/
1558static int pager_delmaster(Pager *pPager, const char *zMaster){
1559 sqlite3_vfs *pVfs = pPager->pVfs;
1560 int rc;
1561 int master_open = 0;
1562 sqlite3_file *pMaster;
1563 sqlite3_file *pJournal;
1564 char *zMasterJournal = 0; /* Contents of master journal file */
1565 i64 nMasterJournal; /* Size of master journal file */
1566
1567 /* Open the master journal file exclusively in case some other process
1568 ** is running this routine also. Not that it makes too much difference.
1569 */
1570 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
1571 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
1572 if( !pMaster ){
1573 rc = SQLITE_NOMEM;
1574 }else{
1575 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1576 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
1577 }
1578 if( rc!=SQLITE_OK ) goto delmaster_out;
1579 master_open = 1;
1580
1581 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
1582 if( rc!=SQLITE_OK ) goto delmaster_out;
1583
1584 if( nMasterJournal>0 ){
1585 char *zJournal;
1586 char *zMasterPtr = 0;
1587 int nMasterPtr = pPager->pVfs->mxPathname+1;
1588
1589 /* Load the entire master journal file into space obtained from
1590 ** sqlite3_malloc() and pointed to by zMasterJournal.
1591 */
1592 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
1593 if( !zMasterJournal ){
1594 rc = SQLITE_NOMEM;
1595 goto delmaster_out;
1596 }
1597 zMasterPtr = &zMasterJournal[nMasterJournal];
1598 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
1599 if( rc!=SQLITE_OK ) goto delmaster_out;
1600
1601 zJournal = zMasterJournal;
1602 while( (zJournal-zMasterJournal)<nMasterJournal ){
1603 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
1604 /* One of the journals pointed to by the master journal exists.
1605 ** Open it and check if it points at the master journal. If
1606 ** so, return without deleting the master journal file.
1607 */
1608 int c;
1609 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1610 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
1611 if( rc!=SQLITE_OK ){
1612 goto delmaster_out;
1613 }
1614
1615 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
1616 sqlite3OsClose(pJournal);
1617 if( rc!=SQLITE_OK ){
1618 goto delmaster_out;
1619 }
1620
1621 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
1622 if( c ){
1623 /* We have a match. Do not delete the master journal file. */
1624 goto delmaster_out;
1625 }
1626 }
1627 zJournal += (strlen(zJournal)+1);
1628 }
1629 }
1630
1631 rc = sqlite3OsDelete(pVfs, zMaster, 0);
1632
1633delmaster_out:
1634 if( zMasterJournal ){
1635 sqlite3_free(zMasterJournal);
1636 }
1637 if( master_open ){
1638 sqlite3OsClose(pMaster);
1639 }
1640 sqlite3_free(pMaster);
1641 return rc;
1642}
1643
1644
1645static void pager_truncate_cache(Pager *pPager);
1646
1647/*
1648** Truncate the main file of the given pager to the number of pages
1649** indicated. Also truncate the cached representation of the file.
1650*/
1651static int pager_truncate(Pager *pPager, int nPage){
1652 int rc = SQLITE_OK;
1653 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
1654 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1655 }
1656 if( rc==SQLITE_OK ){
1657 pPager->dbSize = nPage;
1658 pager_truncate_cache(pPager);
1659 }
1660 return rc;
1661}
1662
1663/*
1664** Set the sectorSize for the given pager.
1665**
1666** The sector size is the larger of the sector size reported
1667** by sqlite3OsSectorSize() and the pageSize.
1668*/
1669static void setSectorSize(Pager *pPager){
1670 assert(pPager->fd->pMethods||pPager->tempFile);
1671 if( !pPager->tempFile ){
1672 /* Sector size doesn't matter for temporary files. Also, the file
1673 ** may not have been opened yet, in whcih case the OsSectorSize()
1674 ** call will segfault.
1675 */
1676 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1677 }
1678 if( pPager->sectorSize<pPager->pageSize ){
1679 pPager->sectorSize = pPager->pageSize;
1680 }
1681}
1682
1683/*
1684** Playback the journal and thus restore the database file to
1685** the state it was in before we started making changes.
1686**
1687** The journal file format is as follows:
1688**
1689** (1) 8 byte prefix. A copy of aJournalMagic[].
1690** (2) 4 byte big-endian integer which is the number of valid page records
1691** in the journal. If this value is 0xffffffff, then compute the
1692** number of page records from the journal size.
1693** (3) 4 byte big-endian integer which is the initial value for the
1694** sanity checksum.
1695** (4) 4 byte integer which is the number of pages to truncate the
1696** database to during a rollback.
1697** (5) 4 byte integer which is the number of bytes in the master journal
1698** name. The value may be zero (indicate that there is no master
1699** journal.)
1700** (6) N bytes of the master journal name. The name will be nul-terminated
1701** and might be shorter than the value read from (5). If the first byte
1702** of the name is \000 then there is no master journal. The master
1703** journal name is stored in UTF-8.
1704** (7) Zero or more pages instances, each as follows:
1705** + 4 byte page number.
1706** + pPager->pageSize bytes of data.
1707** + 4 byte checksum
1708**
1709** When we speak of the journal header, we mean the first 6 items above.
1710** Each entry in the journal is an instance of the 7th item.
1711**
1712** Call the value from the second bullet "nRec". nRec is the number of
1713** valid page entries in the journal. In most cases, you can compute the
1714** value of nRec from the size of the journal file. But if a power
1715** failure occurred while the journal was being written, it could be the
1716** case that the size of the journal file had already been increased but
1717** the extra entries had not yet made it safely to disk. In such a case,
1718** the value of nRec computed from the file size would be too large. For
1719** that reason, we always use the nRec value in the header.
1720**
1721** If the nRec value is 0xffffffff it means that nRec should be computed
1722** from the file size. This value is used when the user selects the
1723** no-sync option for the journal. A power failure could lead to corruption
1724** in this case. But for things like temporary table (which will be
1725** deleted when the power is restored) we don't care.
1726**
1727** If the file opened as the journal file is not a well-formed
1728** journal file then all pages up to the first corrupted page are rolled
1729** back (or no pages if the journal header is corrupted). The journal file
1730** is then deleted and SQLITE_OK returned, just as if no corruption had
1731** been encountered.
1732**
1733** If an I/O or malloc() error occurs, the journal-file is not deleted
1734** and an error code is returned.
1735*/
1736static int pager_playback(Pager *pPager, int isHot){
1737 sqlite3_vfs *pVfs = pPager->pVfs;
1738 i64 szJ; /* Size of the journal file in bytes */
1739 u32 nRec; /* Number of Records in the journal */
1740 int i; /* Loop counter */
1741 Pgno mxPg = 0; /* Size of the original file in pages */
1742 int rc; /* Result code of a subroutine */
1743 char *zMaster = 0; /* Name of master journal file if any */
1744
1745 /* Figure out how many records are in the journal. Abort early if
1746 ** the journal is empty.
1747 */
1748 assert( pPager->journalOpen );
1749 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
1750 if( rc!=SQLITE_OK || szJ==0 ){
1751 goto end_playback;
1752 }
1753
1754 /* Read the master journal name from the journal, if it is present.
1755 ** If a master journal file name is specified, but the file is not
1756 ** present on disk, then the journal is not hot and does not need to be
1757 ** played back.
1758 */
1759 zMaster = pPager->pTmpSpace;
1760 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1761 assert( rc!=SQLITE_DONE );
1762 if( rc!=SQLITE_OK
1763 || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
1764 ){
1765 zMaster = 0;
1766 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
1767 goto end_playback;
1768 }
1769 pPager->journalOff = 0;
1770 zMaster = 0;
1771
1772 /* This loop terminates either when the readJournalHdr() call returns
1773 ** SQLITE_DONE or an IO error occurs. */
1774 while( 1 ){
1775
1776 /* Read the next journal header from the journal file. If there are
1777 ** not enough bytes left in the journal file for a complete header, or
1778 ** it is corrupted, then a process must of failed while writing it.
1779 ** This indicates nothing more needs to be rolled back.
1780 */
1781 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1782 if( rc!=SQLITE_OK ){
1783 if( rc==SQLITE_DONE ){
1784 rc = SQLITE_OK;
1785 }
1786 goto end_playback;
1787 }
1788
1789 /* If nRec is 0xffffffff, then this journal was created by a process
1790 ** working in no-sync mode. This means that the rest of the journal
1791 ** file consists of pages, there are no more journal headers. Compute
1792 ** the value of nRec based on this assumption.
1793 */
1794 if( nRec==0xffffffff ){
1795 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1796 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1797 }
1798
1799 /* If nRec is 0 and this rollback is of a transaction created by this
1800 ** process and if this is the final header in the journal, then it means
1801 ** that this part of the journal was being filled but has not yet been
1802 ** synced to disk. Compute the number of pages based on the remaining
1803 ** size of the file.
1804 **
1805 ** The third term of the test was added to fix ticket #2565.
1806 */
1807 if( nRec==0 && !isHot &&
1808 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
1809 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1810 }
1811
1812 /* If this is the first header read from the journal, truncate the
1813 ** database file back to it's original size.
1814 */
1815 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
1816 rc = pager_truncate(pPager, mxPg);
1817 if( rc!=SQLITE_OK ){
1818 goto end_playback;
1819 }
1820 }
1821
1822 /* Copy original pages out of the journal and back into the database file.
1823 */
1824 for(i=0; i<nRec; i++){
1825 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
1826 if( rc!=SQLITE_OK ){
1827 if( rc==SQLITE_DONE ){
1828 rc = SQLITE_OK;
1829 pPager->journalOff = szJ;
1830 break;
1831 }else{
1832 goto end_playback;
1833 }
1834 }
1835 }
1836 }
1837 /*NOTREACHED*/
1838 assert( 0 );
1839
1840end_playback:
1841 if( rc==SQLITE_OK ){
1842 zMaster = pPager->pTmpSpace;
1843 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1844 }
1845 if( rc==SQLITE_OK ){
1846 rc = pager_end_transaction(pPager);
1847 }
1848 if( rc==SQLITE_OK && zMaster[0] ){
1849 /* If there was a master journal and this routine will return success,
1850 ** see if it is possible to delete the master journal.
1851 */
1852 rc = pager_delmaster(pPager, zMaster);
1853 }
1854
1855 /* The Pager.sectorSize variable may have been updated while rolling
1856 ** back a journal created by a process with a different sector size
1857 ** value. Reset it to the correct value for this process.
1858 */
1859 setSectorSize(pPager);
1860 return rc;
1861}
1862
1863/*
1864** Playback the statement journal.
1865**
1866** This is similar to playing back the transaction journal but with
1867** a few extra twists.
1868**
1869** (1) The number of pages in the database file at the start of
1870** the statement is stored in pPager->stmtSize, not in the
1871** journal file itself.
1872**
1873** (2) In addition to playing back the statement journal, also
1874** playback all pages of the transaction journal beginning
1875** at offset pPager->stmtJSize.
1876*/
1877static int pager_stmt_playback(Pager *pPager){
1878 i64 szJ; /* Size of the full journal */
1879 i64 hdrOff;
1880 int nRec; /* Number of Records */
1881 int i; /* Loop counter */
1882 int rc;
1883
1884 szJ = pPager->journalOff;
1885#ifndef NDEBUG
1886 {
1887 i64 os_szJ;
1888 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
1889 if( rc!=SQLITE_OK ) return rc;
1890 assert( szJ==os_szJ );
1891 }
1892#endif
1893
1894 /* Set hdrOff to be the offset just after the end of the last journal
1895 ** page written before the first journal-header for this statement
1896 ** transaction was written, or the end of the file if no journal
1897 ** header was written.
1898 */
1899 hdrOff = pPager->stmtHdrOff;
1900 assert( pPager->fullSync || !hdrOff );
1901 if( !hdrOff ){
1902 hdrOff = szJ;
1903 }
1904
1905 /* Truncate the database back to its original size.
1906 */
1907 rc = pager_truncate(pPager, pPager->stmtSize);
1908 assert( pPager->state>=PAGER_SHARED );
1909
1910 /* Figure out how many records are in the statement journal.
1911 */
1912 assert( pPager->stmtInUse && pPager->journalOpen );
1913 nRec = pPager->stmtNRec;
1914
1915 /* Copy original pages out of the statement journal and back into the
1916 ** database file. Note that the statement journal omits checksums from
1917 ** each record since power-failure recovery is not important to statement
1918 ** journals.
1919 */
1920 for(i=0; i<nRec; i++){
1921 i64 offset = i*(4+pPager->pageSize);
1922 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
1923 assert( rc!=SQLITE_DONE );
1924 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1925 }
1926
1927 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1928 ** was the size of the journal file when this statement was started, so
1929 ** everything after that needs to be rolled back, either into the
1930 ** database, the memory cache, or both.
1931 **
1932 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1933 ** of the first journal header written during this statement transaction.
1934 */
1935 pPager->journalOff = pPager->stmtJSize;
1936 pPager->cksumInit = pPager->stmtCksum;
1937 while( pPager->journalOff < hdrOff ){
1938 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
1939 assert( rc!=SQLITE_DONE );
1940 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1941 }
1942
1943 while( pPager->journalOff < szJ ){
1944 u32 nJRec; /* Number of Journal Records */
1945 u32 dummy;
1946 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
1947 if( rc!=SQLITE_OK ){
1948 assert( rc!=SQLITE_DONE );
1949 goto end_stmt_playback;
1950 }
1951 if( nJRec==0 ){
1952 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
1953 }
1954 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
1955 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
1956 assert( rc!=SQLITE_DONE );
1957 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1958 }
1959 }
1960
1961 pPager->journalOff = szJ;
1962
1963end_stmt_playback:
1964 if( rc==SQLITE_OK) {
1965 pPager->journalOff = szJ;
1966 /* pager_reload_cache(pPager); */
1967 }
1968 return rc;
1969}
1970
1971/*
1972** Change the maximum number of in-memory pages that are allowed.
1973*/
1974void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
1975 if( mxPage>10 ){
1976 pPager->mxPage = mxPage;
1977 }else{
1978 pPager->mxPage = 10;
1979 }
1980}
1981
1982/*
1983** Adjust the robustness of the database to damage due to OS crashes
1984** or power failures by changing the number of syncs()s when writing
1985** the rollback journal. There are three levels:
1986**
1987** OFF sqlite3OsSync() is never called. This is the default
1988** for temporary and transient files.
1989**
1990** NORMAL The journal is synced once before writes begin on the
1991** database. This is normally adequate protection, but
1992** it is theoretically possible, though very unlikely,
1993** that an inopertune power failure could leave the journal
1994** in a state which would cause damage to the database
1995** when it is rolled back.
1996**
1997** FULL The journal is synced twice before writes begin on the
1998** database (with some additional information - the nRec field
1999** of the journal header - being written in between the two
2000** syncs). If we assume that writing a
2001** single disk sector is atomic, then this mode provides
2002** assurance that the journal will not be corrupted to the
2003** point of causing damage to the database during rollback.
2004**
2005** Numeric values associated with these states are OFF==1, NORMAL=2,
2006** and FULL=3.
2007*/
2008#ifndef SQLITE_OMIT_PAGER_PRAGMAS
2009void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
2010 pPager->noSync = level==1 || pPager->tempFile;
2011 pPager->fullSync = level==3 && !pPager->tempFile;
2012 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
2013 if( pPager->noSync ) pPager->needSync = 0;
2014}
2015#endif
2016
2017/*
2018** The following global variable is incremented whenever the library
2019** attempts to open a temporary file. This information is used for
2020** testing and analysis only.
2021*/
2022#ifdef SQLITE_TEST
2023int sqlite3_opentemp_count = 0;
2024#endif
2025
2026/*
2027** Open a temporary file.
2028**
2029** Write the file descriptor into *fd. Return SQLITE_OK on success or some
2030** other error code if we fail. The OS will automatically delete the temporary
2031** file when it is closed.
2032*/
2033static int sqlite3PagerOpentemp(
2034 sqlite3_vfs *pVfs, /* The virtual file system layer */
2035 sqlite3_file *pFile, /* Write the file descriptor here */
2036 char *zFilename, /* Name of the file. Might be NULL */
2037 int vfsFlags /* Flags passed through to the VFS */
2038){
2039 int rc;
2040 assert( zFilename!=0 );
2041
2042#ifdef SQLITE_TEST
2043 sqlite3_opentemp_count++; /* Used for testing and analysis only */
2044#endif
2045
2046 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2047 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2048 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
2049 assert( rc!=SQLITE_OK || pFile->pMethods );
2050 return rc;
2051}
2052
2053/*
2054** Create a new page cache and put a pointer to the page cache in *ppPager.
2055** The file to be cached need not exist. The file is not locked until
2056** the first call to sqlite3PagerGet() and is only held open until the
2057** last page is released using sqlite3PagerUnref().
2058**
2059** If zFilename is NULL then a randomly-named temporary file is created
2060** and used as the file to be cached. The file will be deleted
2061** automatically when it is closed.
2062**
2063** If zFilename is ":memory:" then all information is held in cache.
2064** It is never written to disk. This can be used to implement an
2065** in-memory database.
2066*/
2067int sqlite3PagerOpen(
2068 sqlite3_vfs *pVfs, /* The virtual file system to use */
2069 Pager **ppPager, /* Return the Pager structure here */
2070 const char *zFilename, /* Name of the database file to open */
2071 int nExtra, /* Extra bytes append to each in-memory page */
2072 int flags, /* flags controlling this file */
2073 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
2074){
2075 u8 *pPtr;
2076 Pager *pPager = 0;
2077 int rc = SQLITE_OK;
2078 int i;
2079 int tempFile = 0;
2080 int memDb = 0;
2081 int readOnly = 0;
2082 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2083 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
2084 int journalFileSize = sqlite3JournalSize(pVfs);
2085 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2086 char *zPathname;
2087 int nPathname;
2088
2089 /* The default return is a NULL pointer */
2090 *ppPager = 0;
2091
2092 /* Compute the full pathname */
2093 nPathname = pVfs->mxPathname+1;
2094 zPathname = sqlite3_malloc(nPathname);
2095 if( zPathname==0 ){
2096 return SQLITE_NOMEM;
2097 }
2098 if( zFilename && zFilename[0] ){
2099#ifndef SQLITE_OMIT_MEMORYDB
2100 if( strcmp(zFilename,":memory:")==0 ){
2101 memDb = 1;
2102 zPathname[0] = 0;
2103 }else
2104#endif
2105 {
2106 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
2107 }
2108 }else{
2109 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
2110 }
2111 if( rc!=SQLITE_OK ){
2112 sqlite3_free(zPathname);
2113 return rc;
2114 }
2115 nPathname = strlen(zPathname);
2116
2117 /* Allocate memory for the pager structure */
2118 pPager = sqlite3MallocZero(
2119 sizeof(*pPager) + /* Pager structure */
2120 journalFileSize + /* The journal file structure */
2121 pVfs->szOsFile * 2 + /* The db and stmt journal files */
2122 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */
2123 );
2124 if( !pPager ){
2125 sqlite3_free(zPathname);
2126 return SQLITE_NOMEM;
2127 }
2128 pPtr = (u8 *)&pPager[1];
2129 pPager->vfsFlags = vfsFlags;
2130 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
2131 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2132 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2133 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
2134 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2135 pPager->zJournal = &pPager->zDirectory[nPathname+1];
2136 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
2137 pPager->pVfs = pVfs;
2138 memcpy(pPager->zFilename, zPathname, nPathname+1);
2139 sqlite3_free(zPathname);
2140
2141 /* Open the pager file.
2142 */
2143 if( zFilename && zFilename[0] && !memDb ){
2144 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2145 rc = SQLITE_CANTOPEN;
2146 }else{
2147 int fout = 0;
2148 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2149 pPager->vfsFlags, &fout);
2150 readOnly = (fout&SQLITE_OPEN_READONLY);
2151
2152 /* If the file was successfully opened for read/write access,
2153 ** choose a default page size in case we have to create the
2154 ** database file. The default page size is the maximum of:
2155 **
2156 ** + SQLITE_DEFAULT_PAGE_SIZE,
2157 ** + The value returned by sqlite3OsSectorSize()
2158 ** + The largest page size that can be written atomically.
2159 */
2160 if( rc==SQLITE_OK && !readOnly ){
2161 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2162 if( nDefaultPage<iSectorSize ){
2163 nDefaultPage = iSectorSize;
2164 }
2165#ifdef SQLITE_ENABLE_ATOMIC_WRITE
2166 {
2167 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2168 int ii;
2169 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2170 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2171 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2172 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2173 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
2174 }
2175 }
2176#endif
2177 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2178 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2179 }
2180 }
2181 }
2182 }else if( !memDb ){
2183 /* If a temporary file is requested, it is not opened immediately.
2184 ** In this case we accept the default page size and delay actually
2185 ** opening the file until the first call to OsWrite().
2186 */
2187 tempFile = 1;
2188 pPager->state = PAGER_EXCLUSIVE;
2189 }
2190
2191 if( pPager && rc==SQLITE_OK ){
2192 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
2193 }
2194
2195 /* If an error occured in either of the blocks above.
2196 ** Free the Pager structure and close the file.
2197 ** Since the pager is not allocated there is no need to set
2198 ** any Pager.errMask variables.
2199 */
2200 if( !pPager || !pPager->pTmpSpace ){
2201 sqlite3OsClose(pPager->fd);
2202 sqlite3_free(pPager);
2203 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
2204 }
2205
2206 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2207 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
2208
2209 /* Fill in Pager.zDirectory[] */
2210 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
2211 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
2212 if( i>0 ) pPager->zDirectory[i-1] = 0;
2213
2214 /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
2215 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2216 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
2217 memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
2218 memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
2219
2220 /* pPager->journalOpen = 0; */
2221 pPager->useJournal = useJournal && !memDb;
2222 pPager->noReadlock = noReadlock && readOnly;
2223 /* pPager->stmtOpen = 0; */
2224 /* pPager->stmtInUse = 0; */
2225 /* pPager->nRef = 0; */
2226 pPager->dbSize = memDb-1;
2227 pPager->pageSize = nDefaultPage;
2228 /* pPager->stmtSize = 0; */
2229 /* pPager->stmtJSize = 0; */
2230 /* pPager->nPage = 0; */
2231 pPager->mxPage = 100;
2232 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
2233 /* pPager->state = PAGER_UNLOCK; */
2234 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
2235 /* pPager->errMask = 0; */
2236 pPager->tempFile = tempFile;
2237 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2238 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2239 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2240 pPager->exclusiveMode = tempFile;
2241 pPager->memDb = memDb;
2242 pPager->readOnly = readOnly;
2243 /* pPager->needSync = 0; */
2244 pPager->noSync = pPager->tempFile || !useJournal;
2245 pPager->fullSync = (pPager->noSync?0:1);
2246 pPager->sync_flags = SQLITE_SYNC_NORMAL;
2247 /* pPager->pFirst = 0; */
2248 /* pPager->pFirstSynced = 0; */
2249 /* pPager->pLast = 0; */
2250 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
2251 assert(pPager->fd->pMethods||memDb||tempFile);
2252 if( !memDb ){
2253 setSectorSize(pPager);
2254 }
2255 /* pPager->pBusyHandler = 0; */
2256 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
2257 *ppPager = pPager;
2258#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2259 pPager->iInUseMM = 0;
2260 pPager->iInUseDB = 0;
2261 if( !memDb ){
2262 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2263 sqlite3_mutex_enter(mutex);
2264 pPager->pNext = sqlite3PagerList;
2265 if( sqlite3PagerList ){
2266 assert( sqlite3PagerList->pPrev==0 );
2267 sqlite3PagerList->pPrev = pPager;
2268 }
2269 pPager->pPrev = 0;
2270 sqlite3PagerList = pPager;
2271 sqlite3_mutex_leave(mutex);
2272 }
2273#endif
2274 return SQLITE_OK;
2275}
2276
2277/*
2278** Set the busy handler function.
2279*/
2280void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
2281 pPager->pBusyHandler = pBusyHandler;
2282}
2283
2284/*
2285** Set the destructor for this pager. If not NULL, the destructor is called
2286** when the reference count on each page reaches zero. The destructor can
2287** be used to clean up information in the extra segment appended to each page.
2288**
2289** The destructor is not called as a result sqlite3PagerClose().
2290** Destructors are only called by sqlite3PagerUnref().
2291*/
2292void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
2293 pPager->xDestructor = xDesc;
2294}
2295
2296/*
2297** Set the reinitializer for this pager. If not NULL, the reinitializer
2298** is called when the content of a page in cache is restored to its original
2299** value as a result of a rollback. The callback gives higher-level code
2300** an opportunity to restore the EXTRA section to agree with the restored
2301** page data.
2302*/
2303void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
2304 pPager->xReiniter = xReinit;
2305}
2306
2307/*
2308** Set the page size to *pPageSize. If the suggest new page size is
2309** inappropriate, then an alternative page size is set to that
2310** value before returning.
2311*/
2312int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2313 int rc = SQLITE_OK;
2314 u16 pageSize = *pPageSize;
2315 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
2316 if( pageSize && pageSize!=pPager->pageSize
2317 && !pPager->memDb && pPager->nRef==0
2318 ){
2319 char *pNew = (char *)sqlite3_malloc(pageSize);
2320 if( !pNew ){
2321 rc = SQLITE_NOMEM;
2322 }else{
2323 pagerEnter(pPager);
2324 pager_reset(pPager);
2325 pPager->pageSize = pageSize;
2326 setSectorSize(pPager);
2327 sqlite3_free(pPager->pTmpSpace);
2328 pPager->pTmpSpace = pNew;
2329 pagerLeave(pPager);
2330 }
2331 }
2332 *pPageSize = pPager->pageSize;
2333 return rc;
2334}
2335
2336/*
2337** Attempt to set the maximum database page count if mxPage is positive.
2338** Make no changes if mxPage is zero or negative. And never reduce the
2339** maximum page count below the current size of the database.
2340**
2341** Regardless of mxPage, return the current maximum page count.
2342*/
2343int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2344 if( mxPage>0 ){
2345 pPager->mxPgno = mxPage;
2346 }
2347 sqlite3PagerPagecount(pPager);
2348 return pPager->mxPgno;
2349}
2350
2351/*
2352** The following set of routines are used to disable the simulated
2353** I/O error mechanism. These routines are used to avoid simulated
2354** errors in places where we do not care about errors.
2355**
2356** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2357** and generate no code.
2358*/
2359#ifdef SQLITE_TEST
2360extern int sqlite3_io_error_pending;
2361extern int sqlite3_io_error_hit;
2362static int saved_cnt;
2363void disable_simulated_io_errors(void){
2364 saved_cnt = sqlite3_io_error_pending;
2365 sqlite3_io_error_pending = -1;
2366}
2367void enable_simulated_io_errors(void){
2368 sqlite3_io_error_pending = saved_cnt;
2369}
2370#else
2371# define disable_simulated_io_errors()
2372# define enable_simulated_io_errors()
2373#endif
2374
2375/*
2376** Read the first N bytes from the beginning of the file into memory
2377** that pDest points to.
2378**
2379** No error checking is done. The rational for this is that this function
2380** may be called even if the file does not exist or contain a header. In
2381** these cases sqlite3OsRead() will return an error, to which the correct
2382** response is to zero the memory at pDest and continue. A real IO error
2383** will presumably recur and be picked up later (Todo: Think about this).
2384*/
2385int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
2386 int rc = SQLITE_OK;
2387 memset(pDest, 0, N);
2388 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2389 if( pPager->fd->pMethods ){
2390 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
2391 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
2392 if( rc==SQLITE_IOERR_SHORT_READ ){
2393 rc = SQLITE_OK;
2394 }
2395 }
2396 return rc;
2397}
2398
2399/*
2400** Return the total number of pages in the disk file associated with
2401** pPager.
2402**
2403** If the PENDING_BYTE lies on the page directly after the end of the
2404** file, then consider this page part of the file too. For example, if
2405** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2406** file is 4096 bytes, 5 is returned instead of 4.
2407*/
2408int sqlite3PagerPagecount(Pager *pPager){
2409 i64 n = 0;
2410 int rc;
2411 assert( pPager!=0 );
2412 if( pPager->errCode ){
2413 return 0;
2414 }
2415 if( pPager->dbSize>=0 ){
2416 n = pPager->dbSize;
2417 } else {
2418 assert(pPager->fd->pMethods||pPager->tempFile);
2419 if( (pPager->fd->pMethods)
2420 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
2421 pPager->nRef++;
2422 pager_error(pPager, rc);
2423 pPager->nRef--;
2424 return 0;
2425 }
2426 if( n>0 && n<pPager->pageSize ){
2427 n = 1;
2428 }else{
2429 n /= pPager->pageSize;
2430 }
2431 if( pPager->state!=PAGER_UNLOCK ){
2432 pPager->dbSize = n;
2433 }
2434 }
2435 if( n==(PENDING_BYTE/pPager->pageSize) ){
2436 n++;
2437 }
2438 if( n>pPager->mxPgno ){
2439 pPager->mxPgno = n;
2440 }
2441 return n;
2442}
2443
2444
2445#ifndef SQLITE_OMIT_MEMORYDB
2446/*
2447** Clear a PgHistory block
2448*/
2449static void clearHistory(PgHistory *pHist){
2450 sqlite3_free(pHist->pOrig);
2451 sqlite3_free(pHist->pStmt);
2452 pHist->pOrig = 0;
2453 pHist->pStmt = 0;
2454}
2455#else
2456#define clearHistory(x)
2457#endif
2458
2459/*
2460** Forward declaration
2461*/
2462static int syncJournal(Pager*);
2463
2464/*
2465** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2466** that the page is not part of any hash chain. This is required because the
2467** sqlite3PagerMovepage() routine can leave a page in the
2468** pNextFree/pPrevFree list that is not a part of any hash-chain.
2469*/
2470static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2471 if( pPg->pgno==0 ){
2472 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
2473 return;
2474 }
2475 if( pPg->pNextHash ){
2476 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2477 }
2478 if( pPg->pPrevHash ){
2479 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
2480 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2481 }else{
2482 int h = pPg->pgno & (pPager->nHash-1);
2483 pPager->aHash[h] = pPg->pNextHash;
2484 }
2485 if( MEMDB ){
2486 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2487 }
2488 pPg->pgno = 0;
2489 pPg->pNextHash = pPg->pPrevHash = 0;
2490}
2491
2492/*
2493** Unlink a page from the free list (the list of all pages where nRef==0)
2494** and from its hash collision chain.
2495*/
2496static void unlinkPage(PgHdr *pPg){
2497 Pager *pPager = pPg->pPager;
2498
2499 /* Unlink from free page list */
2500 lruListRemove(pPg);
2501
2502 /* Unlink from the pgno hash table */
2503 unlinkHashChain(pPager, pPg);
2504}
2505
2506/*
2507** This routine is used to truncate the cache when a database
2508** is truncated. Drop from the cache all pages whose pgno is
2509** larger than pPager->dbSize and is unreferenced.
2510**
2511** Referenced pages larger than pPager->dbSize are zeroed.
2512**
2513** Actually, at the point this routine is called, it would be
2514** an error to have a referenced page. But rather than delete
2515** that page and guarantee a subsequent segfault, it seems better
2516** to zero it and hope that we error out sanely.
2517*/
2518static void pager_truncate_cache(Pager *pPager){
2519 PgHdr *pPg;
2520 PgHdr **ppPg;
2521 int dbSize = pPager->dbSize;
2522
2523 ppPg = &pPager->pAll;
2524 while( (pPg = *ppPg)!=0 ){
2525 if( pPg->pgno<=dbSize ){
2526 ppPg = &pPg->pNextAll;
2527 }else if( pPg->nRef>0 ){
2528 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2529 ppPg = &pPg->pNextAll;
2530 }else{
2531 *ppPg = pPg->pNextAll;
2532 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2533 PAGER_INCR(sqlite3_pager_pgfree_count);
2534 unlinkPage(pPg);
2535 makeClean(pPg);
2536 sqlite3_free(pPg->pData);
2537 sqlite3_free(pPg);
2538 pPager->nPage--;
2539 }
2540 }
2541}
2542
2543/*
2544** Try to obtain a lock on a file. Invoke the busy callback if the lock
2545** is currently not available. Repeat until the busy callback returns
2546** false or until the lock succeeds.
2547**
2548** Return SQLITE_OK on success and an error code if we cannot obtain
2549** the lock.
2550*/
2551static int pager_wait_on_lock(Pager *pPager, int locktype){
2552 int rc;
2553
2554 /* The OS lock values must be the same as the Pager lock values */
2555 assert( PAGER_SHARED==SHARED_LOCK );
2556 assert( PAGER_RESERVED==RESERVED_LOCK );
2557 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
2558
2559 /* If the file is currently unlocked then the size must be unknown */
2560 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2561
2562 if( pPager->state>=locktype ){
2563 rc = SQLITE_OK;
2564 }else{
2565 do {
2566 rc = sqlite3OsLock(pPager->fd, locktype);
2567 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
2568 if( rc==SQLITE_OK ){
2569 pPager->state = locktype;
2570 IOTRACE(("LOCK %p %d\n", pPager, locktype))
2571 }
2572 }
2573 return rc;
2574}
2575
2576/*
2577** Truncate the file to the number of pages specified.
2578*/
2579int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
2580 int rc;
2581 assert( pPager->state>=PAGER_SHARED || MEMDB );
2582 sqlite3PagerPagecount(pPager);
2583 if( pPager->errCode ){
2584 rc = pPager->errCode;
2585 return rc;
2586 }
2587 if( nPage>=(unsigned)pPager->dbSize ){
2588 return SQLITE_OK;
2589 }
2590 if( MEMDB ){
2591 pPager->dbSize = nPage;
2592 pager_truncate_cache(pPager);
2593 return SQLITE_OK;
2594 }
2595 pagerEnter(pPager);
2596 rc = syncJournal(pPager);
2597 pagerLeave(pPager);
2598 if( rc!=SQLITE_OK ){
2599 return rc;
2600 }
2601
2602 /* Get an exclusive lock on the database before truncating. */
2603 pagerEnter(pPager);
2604 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2605 pagerLeave(pPager);
2606 if( rc!=SQLITE_OK ){
2607 return rc;
2608 }
2609
2610 rc = pager_truncate(pPager, nPage);
2611 return rc;
2612}
2613
2614/*
2615** Shutdown the page cache. Free all memory and close all files.
2616**
2617** If a transaction was in progress when this routine is called, that
2618** transaction is rolled back. All outstanding pages are invalidated
2619** and their memory is freed. Any attempt to use a page associated
2620** with this page cache after this function returns will likely
2621** result in a coredump.
2622**
2623** This function always succeeds. If a transaction is active an attempt
2624** is made to roll it back. If an error occurs during the rollback
2625** a hot journal may be left in the filesystem but no error is returned
2626** to the caller.
2627*/
2628int sqlite3PagerClose(Pager *pPager){
2629#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2630 if( !MEMDB ){
2631 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2632 sqlite3_mutex_enter(mutex);
2633 if( pPager->pPrev ){
2634 pPager->pPrev->pNext = pPager->pNext;
2635 }else{
2636 sqlite3PagerList = pPager->pNext;
2637 }
2638 if( pPager->pNext ){
2639 pPager->pNext->pPrev = pPager->pPrev;
2640 }
2641 sqlite3_mutex_leave(mutex);
2642 }
2643#endif
2644
2645 disable_simulated_io_errors();
2646 pPager->errCode = 0;
2647 pPager->exclusiveMode = 0;
2648 pager_reset(pPager);
2649 pagerUnlockAndRollback(pPager);
2650 enable_simulated_io_errors();
2651 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
2652 IOTRACE(("CLOSE %p\n", pPager))
2653 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
2654 if( pPager->journalOpen ){
2655 sqlite3OsClose(pPager->jfd);
2656 }
2657 sqlite3_free(pPager->aInJournal);
2658 if( pPager->stmtOpen ){
2659 sqlite3OsClose(pPager->stfd);
2660 }
2661 sqlite3OsClose(pPager->fd);
2662 /* Temp files are automatically deleted by the OS
2663 ** if( pPager->tempFile ){
2664 ** sqlite3OsDelete(pPager->zFilename);
2665 ** }
2666 */
2667
2668 sqlite3_free(pPager->aHash);
2669 sqlite3_free(pPager->pTmpSpace);
2670 sqlite3_free(pPager);
2671 return SQLITE_OK;
2672}
2673
2674#if !defined(NDEBUG) || defined(SQLITE_TEST)
2675/*
2676** Return the page number for the given page data.
2677*/
2678Pgno sqlite3PagerPagenumber(DbPage *p){
2679 return p->pgno;
2680}
2681#endif
2682
2683/*
2684** The page_ref() function increments the reference count for a page.
2685** If the page is currently on the freelist (the reference count is zero) then
2686** remove it from the freelist.
2687**
2688** For non-test systems, page_ref() is a macro that calls _page_ref()
2689** online of the reference count is zero. For test systems, page_ref()
2690** is a real function so that we can set breakpoints and trace it.
2691*/
2692static void _page_ref(PgHdr *pPg){
2693 if( pPg->nRef==0 ){
2694 /* The page is currently on the freelist. Remove it. */
2695 lruListRemove(pPg);
2696 pPg->pPager->nRef++;
2697 }
2698 pPg->nRef++;
2699 REFINFO(pPg);
2700}
2701#ifdef SQLITE_DEBUG
2702 static void page_ref(PgHdr *pPg){
2703 if( pPg->nRef==0 ){
2704 _page_ref(pPg);
2705 }else{
2706 pPg->nRef++;
2707 REFINFO(pPg);
2708 }
2709 }
2710#else
2711# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2712#endif
2713
2714/*
2715** Increment the reference count for a page. The input pointer is
2716** a reference to the page data.
2717*/
2718int sqlite3PagerRef(DbPage *pPg){
2719 pagerEnter(pPg->pPager);
2720 page_ref(pPg);
2721 pagerLeave(pPg->pPager);
2722 return SQLITE_OK;
2723}
2724
2725/*
2726** Sync the journal. In other words, make sure all the pages that have
2727** been written to the journal have actually reached the surface of the
2728** disk. It is not safe to modify the original database file until after
2729** the journal has been synced. If the original database is modified before
2730** the journal is synced and a power failure occurs, the unsynced journal
2731** data would be lost and we would be unable to completely rollback the
2732** database changes. Database corruption would occur.
2733**
2734** This routine also updates the nRec field in the header of the journal.
2735** (See comments on the pager_playback() routine for additional information.)
2736** If the sync mode is FULL, two syncs will occur. First the whole journal
2737** is synced, then the nRec field is updated, then a second sync occurs.
2738**
2739** For temporary databases, we do not care if we are able to rollback
2740** after a power failure, so no sync occurs.
2741**
2742** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2743** the database is stored, then OsSync() is never called on the journal
2744** file. In this case all that is required is to update the nRec field in
2745** the journal header.
2746**
2747** This routine clears the needSync field of every page current held in
2748** memory.
2749*/
2750static int syncJournal(Pager *pPager){
2751 PgHdr *pPg;
2752 int rc = SQLITE_OK;
2753
2754
2755 /* Sync the journal before modifying the main database
2756 ** (assuming there is a journal and it needs to be synced.)
2757 */
2758 if( pPager->needSync ){
2759 if( !pPager->tempFile ){
2760 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2761 assert( pPager->journalOpen );
2762
2763 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2764 ** was turned off after the transaction was started. Ticket #615 */
2765#ifndef NDEBUG
2766 {
2767 /* Make sure the pPager->nRec counter we are keeping agrees
2768 ** with the nRec computed from the size of the journal file.
2769 */
2770 i64 jSz;
2771 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
2772 if( rc!=0 ) return rc;
2773 assert( pPager->journalOff==jSz );
2774 }
2775#endif
2776 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
2777 /* Write the nRec value into the journal file header. If in
2778 ** full-synchronous mode, sync the journal first. This ensures that
2779 ** all data has really hit the disk before nRec is updated to mark
2780 ** it as a candidate for rollback.
2781 **
2782 ** This is not required if the persistent media supports the
2783 ** SAFE_APPEND property. Because in this case it is not possible
2784 ** for garbage data to be appended to the file, the nRec field
2785 ** is populated with 0xFFFFFFFF when the journal header is written
2786 ** and never needs to be updated.
2787 */
2788 i64 jrnlOff;
2789 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2790 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2791 IOTRACE(("JSYNC %p\n", pPager))
2792 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
2793 if( rc!=0 ) return rc;
2794 }
2795
2796 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2797 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2798 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
2799 if( rc ) return rc;
2800 }
2801 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2802 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2803 IOTRACE(("JSYNC %p\n", pPager))
2804 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2805 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2806 );
2807 if( rc!=0 ) return rc;
2808 }
2809 pPager->journalStarted = 1;
2810 }
2811 pPager->needSync = 0;
2812
2813 /* Erase the needSync flag from every page.
2814 */
2815 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2816 pPg->needSync = 0;
2817 }
2818 lruListSetFirstSynced(pPager);
2819 }
2820
2821#ifndef NDEBUG
2822 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2823 ** flag must also be clear for all pages. Verify that this
2824 ** invariant is true.
2825 */
2826 else{
2827 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2828 assert( pPg->needSync==0 );
2829 }
2830 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
2831 }
2832#endif
2833
2834 return rc;
2835}
2836
2837/*
2838** Merge two lists of pages connected by pDirty and in pgno order.
2839** Do not both fixing the pPrevDirty pointers.
2840*/
2841static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2842 PgHdr result, *pTail;
2843 pTail = &result;
2844 while( pA && pB ){
2845 if( pA->pgno<pB->pgno ){
2846 pTail->pDirty = pA;
2847 pTail = pA;
2848 pA = pA->pDirty;
2849 }else{
2850 pTail->pDirty = pB;
2851 pTail = pB;
2852 pB = pB->pDirty;
2853 }
2854 }
2855 if( pA ){
2856 pTail->pDirty = pA;
2857 }else if( pB ){
2858 pTail->pDirty = pB;
2859 }else{
2860 pTail->pDirty = 0;
2861 }
2862 return result.pDirty;
2863}
2864
2865/*
2866** Sort the list of pages in accending order by pgno. Pages are
2867** connected by pDirty pointers. The pPrevDirty pointers are
2868** corrupted by this sort.
2869*/
2870#define N_SORT_BUCKET_ALLOC 25
2871#define N_SORT_BUCKET 25
2872#ifdef SQLITE_TEST
2873 int sqlite3_pager_n_sort_bucket = 0;
2874 #undef N_SORT_BUCKET
2875 #define N_SORT_BUCKET \
2876 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2877#endif
2878static PgHdr *sort_pagelist(PgHdr *pIn){
2879 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
2880 int i;
2881 memset(a, 0, sizeof(a));
2882 while( pIn ){
2883 p = pIn;
2884 pIn = p->pDirty;
2885 p->pDirty = 0;
2886 for(i=0; i<N_SORT_BUCKET-1; i++){
2887 if( a[i]==0 ){
2888 a[i] = p;
2889 break;
2890 }else{
2891 p = merge_pagelist(a[i], p);
2892 a[i] = 0;
2893 }
2894 }
2895 if( i==N_SORT_BUCKET-1 ){
2896 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2897 ** elements in the input list. This is possible, but impractical.
2898 ** Testing this line is the point of global variable
2899 ** sqlite3_pager_n_sort_bucket.
2900 */
2901 a[i] = merge_pagelist(a[i], p);
2902 }
2903 }
2904 p = a[0];
2905 for(i=1; i<N_SORT_BUCKET; i++){
2906 p = merge_pagelist(p, a[i]);
2907 }
2908 return p;
2909}
2910
2911/*
2912** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2913** every one of those pages out to the database file and mark them all
2914** as clean.
2915*/
2916static int pager_write_pagelist(PgHdr *pList){
2917 Pager *pPager;
2918 PgHdr *p;
2919 int rc;
2920
2921 if( pList==0 ) return SQLITE_OK;
2922 pPager = pList->pPager;
2923
2924 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2925 ** database file. If there is already an EXCLUSIVE lock, the following
2926 ** calls to sqlite3OsLock() are no-ops.
2927 **
2928 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2929 ** through an intermediate state PENDING. A PENDING lock prevents new
2930 ** readers from attaching to the database but is unsufficient for us to
2931 ** write. The idea of a PENDING lock is to prevent new readers from
2932 ** coming in while we wait for existing readers to clear.
2933 **
2934 ** While the pager is in the RESERVED state, the original database file
2935 ** is unchanged and we can rollback without having to playback the
2936 ** journal into the original database file. Once we transition to
2937 ** EXCLUSIVE, it means the database file has been changed and any rollback
2938 ** will require a journal playback.
2939 */
2940 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
2941 if( rc!=SQLITE_OK ){
2942 return rc;
2943 }
2944
2945 pList = sort_pagelist(pList);
2946 for(p=pList; p; p=p->pDirty){
2947 assert( p->dirty );
2948 p->dirty = 0;
2949 }
2950 while( pList ){
2951
2952 /* If the file has not yet been opened, open it now. */
2953 if( !pPager->fd->pMethods ){
2954 assert(pPager->tempFile);
2955 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
2956 pPager->vfsFlags);
2957 if( rc ) return rc;
2958 }
2959
2960 /* If there are dirty pages in the page cache with page numbers greater
2961 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
2962 ** make the file smaller (presumably by auto-vacuum code). Do not write
2963 ** any such pages to the file.
2964 */
2965 if( pList->pgno<=pPager->dbSize ){
2966 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
2967 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
2968 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2969 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
2970 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
2971 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
2972 PAGER_INCR(sqlite3_pager_writedb_count);
2973 PAGER_INCR(pPager->nWrite);
2974 if( pList->pgno==1 ){
2975 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2976 }
2977 }
2978#ifndef NDEBUG
2979 else{
2980 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
2981 }
2982#endif
2983 if( rc ) return rc;
2984#ifdef SQLITE_CHECK_PAGES
2985 pList->pageHash = pager_pagehash(pList);
2986#endif
2987 pList = pList->pDirty;
2988 }
2989 return SQLITE_OK;
2990}
2991
2992/*
2993** Collect every dirty page into a dirty list and
2994** return a pointer to the head of that list. All pages are
2995** collected even if they are still in use.
2996*/
2997static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
2998 return pPager->pDirty;
2999}
3000
3001/*
3002** Return TRUE if there is a hot journal on the given pager.
3003** A hot journal is one that needs to be played back.
3004**
3005** If the current size of the database file is 0 but a journal file
3006** exists, that is probably an old journal left over from a prior
3007** database with the same name. Just delete the journal.
3008*/
3009static int hasHotJournal(Pager *pPager){
3010 sqlite3_vfs *pVfs = pPager->pVfs;
3011 if( !pPager->useJournal ) return 0;
3012 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3013 return 0;
3014 }
3015 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3016 return 0;
3017 }
3018 if( sqlite3PagerPagecount(pPager)==0 ){
3019 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3020 return 0;
3021 }else{
3022 return 1;
3023 }
3024}
3025
3026/*
3027** Try to find a page in the cache that can be recycled.
3028**
3029** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
3030** does not set the pPager->errCode variable.
3031*/
3032static int pager_recycle(Pager *pPager, PgHdr **ppPg){
3033 PgHdr *pPg;
3034 *ppPg = 0;
3035
3036 /* It is illegal to call this function unless the pager object
3037 ** pointed to by pPager has at least one free page (page with nRef==0).
3038 */
3039 assert(!MEMDB);
3040 assert(pPager->lru.pFirst);
3041
3042 /* Find a page to recycle. Try to locate a page that does not
3043 ** require us to do an fsync() on the journal.
3044 */
3045 pPg = pPager->lru.pFirstSynced;
3046
3047 /* If we could not find a page that does not require an fsync()
3048 ** on the journal file then fsync the journal file. This is a
3049 ** very slow operation, so we work hard to avoid it. But sometimes
3050 ** it can't be helped.
3051 */
3052 if( pPg==0 && pPager->lru.pFirst){
3053 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3054 int rc = syncJournal(pPager);
3055 if( rc!=0 ){
3056 return rc;
3057 }
3058 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
3059 /* If in full-sync mode, write a new journal header into the
3060 ** journal file. This is done to avoid ever modifying a journal
3061 ** header that is involved in the rollback of pages that have
3062 ** already been written to the database (in case the header is
3063 ** trashed when the nRec field is updated).
3064 */
3065 pPager->nRec = 0;
3066 assert( pPager->journalOff > 0 );
3067 assert( pPager->doNotSync==0 );
3068 rc = writeJournalHdr(pPager);
3069 if( rc!=0 ){
3070 return rc;
3071 }
3072 }
3073 pPg = pPager->lru.pFirst;
3074 }
3075
3076 assert( pPg->nRef==0 );
3077
3078 /* Write the page to the database file if it is dirty.
3079 */
3080 if( pPg->dirty ){
3081 int rc;
3082 assert( pPg->needSync==0 );
3083 makeClean(pPg);
3084 pPg->dirty = 1;
3085 pPg->pDirty = 0;
3086 rc = pager_write_pagelist( pPg );
3087 pPg->dirty = 0;
3088 if( rc!=SQLITE_OK ){
3089 return rc;
3090 }
3091 }
3092 assert( pPg->dirty==0 );
3093
3094 /* If the page we are recycling is marked as alwaysRollback, then
3095 ** set the global alwaysRollback flag, thus disabling the
3096 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
3097 ** It is necessary to do this because the page marked alwaysRollback
3098 ** might be reloaded at a later time but at that point we won't remember
3099 ** that is was marked alwaysRollback. This means that all pages must
3100 ** be marked as alwaysRollback from here on out.
3101 */
3102 if( pPg->alwaysRollback ){
3103 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
3104 pPager->alwaysRollback = 1;
3105 }
3106
3107 /* Unlink the old page from the free list and the hash table
3108 */
3109 unlinkPage(pPg);
3110 assert( pPg->pgno==0 );
3111
3112 *ppPg = pPg;
3113 return SQLITE_OK;
3114}
3115
3116#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
3117/*
3118** This function is called to free superfluous dynamically allocated memory
3119** held by the pager system. Memory in use by any SQLite pager allocated
3120** by the current thread may be sqlite3_free()ed.
3121**
3122** nReq is the number of bytes of memory required. Once this much has
3123** been released, the function returns. The return value is the total number
3124** of bytes of memory released.
3125*/
3126int sqlite3PagerReleaseMemory(int nReq){
3127 int nReleased = 0; /* Bytes of memory released so far */
3128 sqlite3_mutex *mutex; /* The MEM2 mutex */
3129 Pager *pPager; /* For looping over pagers */
3130 int rc = SQLITE_OK;
3131
3132 /* Acquire the memory-management mutex
3133 */
3134 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3135 sqlite3_mutex_enter(mutex);
3136
3137 /* Signal all database connections that memory management wants
3138 ** to have access to the pagers.
3139 */
3140 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3141 pPager->iInUseMM = 1;
3142 }
3143
3144 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3145 PgHdr *pPg;
3146 PgHdr *pRecycled;
3147
3148 /* Try to find a page to recycle that does not require a sync(). If
3149 ** this is not possible, find one that does require a sync().
3150 */
3151 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3152 pPg = sqlite3LruPageList.pFirstSynced;
3153 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3154 pPg = pPg->gfree.pNext;
3155 }
3156 if( !pPg ){
3157 pPg = sqlite3LruPageList.pFirst;
3158 while( pPg && pPg->pPager->iInUseDB ){
3159 pPg = pPg->gfree.pNext;
3160 }
3161 }
3162 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3163
3164 /* If pPg==0, then the block above has failed to find a page to
3165 ** recycle. In this case return early - no further memory will
3166 ** be released.
3167 */
3168 if( !pPg ) break;
3169
3170 pPager = pPg->pPager;
3171 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3172 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3173
3174 rc = pager_recycle(pPager, &pRecycled);
3175 assert(pRecycled==pPg || rc!=SQLITE_OK);
3176 if( rc==SQLITE_OK ){
3177 /* We've found a page to free. At this point the page has been
3178 ** removed from the page hash-table, free-list and synced-list
3179 ** (pFirstSynced). It is still in the all pages (pAll) list.
3180 ** Remove it from this list before freeing.
3181 **
3182 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3183 ** probably is though.
3184 */
3185 PgHdr *pTmp;
3186 assert( pPg );
3187 if( pPg==pPager->pAll ){
3188 pPager->pAll = pPg->pNextAll;
3189 }else{
3190 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3191 pTmp->pNextAll = pPg->pNextAll;
3192 }
3193 nReleased += (
3194 sizeof(*pPg) + pPager->pageSize
3195 + sizeof(u32) + pPager->nExtra
3196 + MEMDB*sizeof(PgHistory)
3197 );
3198 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3199 PAGER_INCR(sqlite3_pager_pgfree_count);
3200 sqlite3_free(pPg->pData);
3201 sqlite3_free(pPg);
3202 pPager->nPage--;
3203 }else{
3204 /* An error occured whilst writing to the database file or
3205 ** journal in pager_recycle(). The error is not returned to the
3206 ** caller of this function. Instead, set the Pager.errCode variable.
3207 ** The error will be returned to the user (or users, in the case
3208 ** of a shared pager cache) of the pager for which the error occured.
3209 */
3210 assert(
3211 (rc&0xff)==SQLITE_IOERR ||
3212 rc==SQLITE_FULL ||
3213 rc==SQLITE_BUSY
3214 );
3215 assert( pPager->state>=PAGER_RESERVED );
3216 pager_error(pPager, rc);
3217 }
3218 }
3219
3220 /* Clear the memory management flags and release the mutex
3221 */
3222 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3223 pPager->iInUseMM = 0;
3224 }
3225 sqlite3_mutex_leave(mutex);
3226
3227 /* Return the number of bytes released
3228 */
3229 return nReleased;
3230}
3231#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
3232
3233/*
3234** Read the content of page pPg out of the database file.
3235*/
3236static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3237 int rc;
3238 i64 offset;
3239 assert( MEMDB==0 );
3240 assert(pPager->fd->pMethods||pPager->tempFile);
3241 if( !pPager->fd->pMethods ){
3242 return SQLITE_IOERR_SHORT_READ;
3243 }
3244 offset = (pgno-1)*(i64)pPager->pageSize;
3245 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
3246 PAGER_INCR(sqlite3_pager_readdb_count);
3247 PAGER_INCR(pPager->nRead);
3248 IOTRACE(("PGIN %p %d\n", pPager, pgno));
3249 if( pgno==1 ){
3250 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3251 sizeof(pPager->dbFileVers));
3252 }
3253 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
3254 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3255 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
3256 return rc;
3257}
3258
3259
3260/*
3261** This function is called to obtain the shared lock required before
3262** data may be read from the pager cache. If the shared lock has already
3263** been obtained, this function is a no-op.
3264**
3265** Immediately after obtaining the shared lock (if required), this function
3266** checks for a hot-journal file. If one is found, an emergency rollback
3267** is performed immediately.
3268*/
3269static int pagerSharedLock(Pager *pPager){
3270 int rc = SQLITE_OK;
3271 int isHot = 0;
3272
3273 /* If this database is opened for exclusive access, has no outstanding
3274 ** page references and is in an error-state, now is the chance to clear
3275 ** the error. Discard the contents of the pager-cache and treat any
3276 ** open journal file as a hot-journal.
3277 */
3278 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3279 if( pPager->journalOpen ){
3280 isHot = 1;
3281 }
3282 pager_reset(pPager);
3283 pPager->errCode = SQLITE_OK;
3284 }
3285
3286 /* If the pager is still in an error state, do not proceed. The error
3287 ** state will be cleared at some point in the future when all page
3288 ** references are dropped and the cache can be discarded.
3289 */
3290 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3291 return pPager->errCode;
3292 }
3293
3294 if( pPager->state==PAGER_UNLOCK || isHot ){
3295 sqlite3_vfs *pVfs = pPager->pVfs;
3296 if( !MEMDB ){
3297 assert( pPager->nRef==0 );
3298 if( !pPager->noReadlock ){
3299 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3300 if( rc!=SQLITE_OK ){
3301 return pager_error(pPager, rc);
3302 }
3303 assert( pPager->state>=SHARED_LOCK );
3304 }
3305
3306 /* If a journal file exists, and there is no RESERVED lock on the
3307 ** database file, then it either needs to be played back or deleted.
3308 */
3309 if( hasHotJournal(pPager) || isHot ){
3310 /* Get an EXCLUSIVE lock on the database file. At this point it is
3311 ** important that a RESERVED lock is not obtained on the way to the
3312 ** EXCLUSIVE lock. If it were, another process might open the
3313 ** database file, detect the RESERVED lock, and conclude that the
3314 ** database is safe to read while this process is still rolling it
3315 ** back.
3316 **
3317 ** Because the intermediate RESERVED lock is not requested, the
3318 ** second process will get to this point in the code and fail to
3319 ** obtain it's own EXCLUSIVE lock on the database file.
3320 */
3321 if( pPager->state<EXCLUSIVE_LOCK ){
3322 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3323 if( rc!=SQLITE_OK ){
3324 pager_unlock(pPager);
3325 return pager_error(pPager, rc);
3326 }
3327 pPager->state = PAGER_EXCLUSIVE;
3328 }
3329
3330 /* Open the journal for reading only. Return SQLITE_BUSY if
3331 ** we are unable to open the journal file.
3332 **
3333 ** The journal file does not need to be locked itself. The
3334 ** journal file is never open unless the main database file holds
3335 ** a write lock, so there is never any chance of two or more
3336 ** processes opening the journal at the same time.
3337 **
3338 ** Open the journal for read/write access. This is because in
3339 ** exclusive-access mode the file descriptor will be kept open and
3340 ** possibly used for a transaction later on. On some systems, the
3341 ** OsTruncate() call used in exclusive-access mode also requires
3342 ** a read/write file handle.
3343 */
3344 if( !isHot ){
3345 rc = SQLITE_BUSY;
3346 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3347 int fout = 0;
3348 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3349 assert( !pPager->tempFile );
3350 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3351 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3352 if( fout&SQLITE_OPEN_READONLY ){
3353 rc = SQLITE_BUSY;
3354 sqlite3OsClose(pPager->jfd);
3355 }
3356 }
3357 }
3358 if( rc!=SQLITE_OK ){
3359 pager_unlock(pPager);
3360 return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY);
3361 }
3362 pPager->journalOpen = 1;
3363 pPager->journalStarted = 0;
3364 pPager->journalOff = 0;
3365 pPager->setMaster = 0;
3366 pPager->journalHdr = 0;
3367
3368 /* Playback and delete the journal. Drop the database write
3369 ** lock and reacquire the read lock.
3370 */
3371 rc = pager_playback(pPager, 1);
3372 if( rc!=SQLITE_OK ){
3373 return pager_error(pPager, rc);
3374 }
3375 assert(pPager->state==PAGER_SHARED ||
3376 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3377 );
3378 }
3379
3380 if( pPager->pAll ){
3381 /* The shared-lock has just been acquired on the database file
3382 ** and there are already pages in the cache (from a previous
3383 ** read or write transaction). Check to see if the database
3384 ** has been modified. If the database has changed, flush the
3385 ** cache.
3386 **
3387 ** Database changes is detected by looking at 15 bytes beginning
3388 ** at offset 24 into the file. The first 4 of these 16 bytes are
3389 ** a 32-bit counter that is incremented with each change. The
3390 ** other bytes change randomly with each file change when
3391 ** a codec is in use.
3392 **
3393 ** There is a vanishingly small chance that a change will not be
3394 ** detected. The chance of an undetected change is so small that
3395 ** it can be neglected.
3396 */
3397 char dbFileVers[sizeof(pPager->dbFileVers)];
3398 sqlite3PagerPagecount(pPager);
3399
3400 if( pPager->errCode ){
3401 return pPager->errCode;
3402 }
3403
3404 if( pPager->dbSize>0 ){
3405 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
3406 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
3407 if( rc!=SQLITE_OK ){
3408 return rc;
3409 }
3410 }else{
3411 memset(dbFileVers, 0, sizeof(dbFileVers));
3412 }
3413
3414 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3415 pager_reset(pPager);
3416 }
3417 }
3418 }
3419 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3420 if( pPager->state==PAGER_UNLOCK ){
3421 pPager->state = PAGER_SHARED;
3422 }
3423 }
3424
3425 return rc;
3426}
3427
3428/*
3429** Allocate a PgHdr object. Either create a new one or reuse
3430** an existing one that is not otherwise in use.
3431**
3432** A new PgHdr structure is created if any of the following are
3433** true:
3434**
3435** (1) We have not exceeded our maximum allocated cache size
3436** as set by the "PRAGMA cache_size" command.
3437**
3438** (2) There are no unused PgHdr objects available at this time.
3439**
3440** (3) This is an in-memory database.
3441**
3442** (4) There are no PgHdr objects that do not require a journal
3443** file sync and a sync of the journal file is currently
3444** prohibited.
3445**
3446** Otherwise, reuse an existing PgHdr. In other words, reuse an
3447** existing PgHdr if all of the following are true:
3448**
3449** (1) We have reached or exceeded the maximum cache size
3450** allowed by "PRAGMA cache_size".
3451**
3452** (2) There is a PgHdr available with PgHdr->nRef==0
3453**
3454** (3) We are not in an in-memory database
3455**
3456** (4) Either there is an available PgHdr that does not need
3457** to be synced to disk or else disk syncing is currently
3458** allowed.
3459*/
3460static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3461 int rc = SQLITE_OK;
3462 PgHdr *pPg;
3463 void *pData;
3464
3465 /* Create a new PgHdr if any of the four conditions defined
3466 ** above are met: */
3467 if( pPager->nPage<pPager->mxPage
3468 || pPager->lru.pFirst==0
3469 || MEMDB
3470 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
3471 ){
3472 if( pPager->nPage>=pPager->nHash ){
3473 pager_resize_hash_table(pPager,
3474 pPager->nHash<256 ? 256 : pPager->nHash*2);
3475 if( pPager->nHash==0 ){
3476 rc = SQLITE_NOMEM;
3477 goto pager_allocate_out;
3478 }
3479 }
3480 pagerLeave(pPager);
3481 pPg = sqlite3_malloc( sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3482 + MEMDB*sizeof(PgHistory) );
3483 if( pPg ){
3484 pData = sqlite3_malloc( pPager->pageSize );
3485 if( pData==0 ){
3486 sqlite3_free(pPg);
3487 pPg = 0;
3488 }
3489 }
3490 pagerEnter(pPager);
3491 if( pPg==0 ){
3492 rc = SQLITE_NOMEM;
3493 goto pager_allocate_out;
3494 }
3495 memset(pPg, 0, sizeof(*pPg));
3496 if( MEMDB ){
3497 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3498 }
3499 pPg->pData = pData;
3500 pPg->pPager = pPager;
3501 pPg->pNextAll = pPager->pAll;
3502 pPager->pAll = pPg;
3503 pPager->nPage++;
3504 }else{
3505 /* Recycle an existing page with a zero ref-count. */
3506 rc = pager_recycle(pPager, &pPg);
3507 if( rc==SQLITE_BUSY ){
3508 rc = SQLITE_IOERR_BLOCKED;
3509 }
3510 if( rc!=SQLITE_OK ){
3511 goto pager_allocate_out;
3512 }
3513 assert( pPager->state>=SHARED_LOCK );
3514 assert(pPg);
3515 }
3516 *ppPg = pPg;
3517
3518pager_allocate_out:
3519 return rc;
3520}
3521
3522/*
3523** Make sure we have the content for a page. If the page was
3524** previously acquired with noContent==1, then the content was
3525** just initialized to zeros instead of being read from disk.
3526** But now we need the real data off of disk. So make sure we
3527** have it. Read it in if we do not have it already.
3528*/
3529static int pager_get_content(PgHdr *pPg){
3530 if( pPg->needRead ){
3531 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3532 if( rc==SQLITE_OK ){
3533 pPg->needRead = 0;
3534 }else{
3535 return rc;
3536 }
3537 }
3538 return SQLITE_OK;
3539}
3540
3541/*
3542** Acquire a page.
3543**
3544** A read lock on the disk file is obtained when the first page is acquired.
3545** This read lock is dropped when the last page is released.
3546**
3547** This routine works for any page number greater than 0. If the database
3548** file is smaller than the requested page, then no actual disk
3549** read occurs and the memory image of the page is initialized to
3550** all zeros. The extra data appended to a page is always initialized
3551** to zeros the first time a page is loaded into memory.
3552**
3553** The acquisition might fail for several reasons. In all cases,
3554** an appropriate error code is returned and *ppPage is set to NULL.
3555**
3556** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
3557** to find a page in the in-memory cache first. If the page is not already
3558** in memory, this routine goes to disk to read it in whereas Lookup()
3559** just returns 0. This routine acquires a read-lock the first time it
3560** has to go to disk, and could also playback an old journal if necessary.
3561** Since Lookup() never goes to disk, it never has to deal with locks
3562** or journal files.
3563**
3564** If noContent is false, the page contents are actually read from disk.
3565** If noContent is true, it means that we do not care about the contents
3566** of the page at this time, so do not do a disk read. Just fill in the
3567** page content with zeros. But mark the fact that we have not read the
3568** content by setting the PgHdr.needRead flag. Later on, if
3569** sqlite3PagerWrite() is called on this page or if this routine is
3570** called again with noContent==0, that means that the content is needed
3571** and the disk read should occur at that point.
3572*/
3573static int pagerAcquire(
3574 Pager *pPager, /* The pager open on the database file */
3575 Pgno pgno, /* Page number to fetch */
3576 DbPage **ppPage, /* Write a pointer to the page here */
3577 int noContent /* Do not bother reading content from disk if true */
3578){
3579 PgHdr *pPg;
3580 int rc;
3581
3582 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3583
3584 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3585 ** number greater than this, or zero, is requested.
3586 */
3587 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
3588 return SQLITE_CORRUPT_BKPT;
3589 }
3590
3591 /* Make sure we have not hit any critical errors.
3592 */
3593 assert( pPager!=0 );
3594 *ppPage = 0;
3595
3596 /* If this is the first page accessed, then get a SHARED lock
3597 ** on the database file. pagerSharedLock() is a no-op if
3598 ** a database lock is already held.
3599 */
3600 rc = pagerSharedLock(pPager);
3601 if( rc!=SQLITE_OK ){
3602 return rc;
3603 }
3604 assert( pPager->state!=PAGER_UNLOCK );
3605
3606 pPg = pager_lookup(pPager, pgno);
3607 if( pPg==0 ){
3608 /* The requested page is not in the page cache. */
3609 int nMax;
3610 int h;
3611 PAGER_INCR(pPager->nMiss);
3612 rc = pagerAllocatePage(pPager, &pPg);
3613 if( rc!=SQLITE_OK ){
3614 return rc;
3615 }
3616
3617 pPg->pgno = pgno;
3618 assert( !MEMDB || pgno>pPager->stmtSize );
3619 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
3620#if 0
3621 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
3622#endif
3623 assert( pPager->journalOpen );
3624 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
3625 pPg->needSync = 0;
3626 }else{
3627 pPg->inJournal = 0;
3628 pPg->needSync = 0;
3629 }
3630
3631 makeClean(pPg);
3632 pPg->nRef = 1;
3633 REFINFO(pPg);
3634
3635 pPager->nRef++;
3636 if( pPager->nExtra>0 ){
3637 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
3638 }
3639 nMax = sqlite3PagerPagecount(pPager);
3640 if( pPager->errCode ){
3641 rc = pPager->errCode;
3642 sqlite3PagerUnref(pPg);
3643 return rc;
3644 }
3645
3646 /* Populate the page with data, either by reading from the database
3647 ** file, or by setting the entire page to zero.
3648 */
3649 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
3650 if( pgno>pPager->mxPgno ){
3651 sqlite3PagerUnref(pPg);
3652 return SQLITE_FULL;
3653 }
3654 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
3655 pPg->needRead = noContent && !pPager->alwaysRollback;
3656 IOTRACE(("ZERO %p %d\n", pPager, pgno));
3657 }else{
3658 rc = readDbPage(pPager, pPg, pgno);
3659 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3660 pPg->pgno = 0;
3661 sqlite3PagerUnref(pPg);
3662 return rc;
3663 }
3664 pPg->needRead = 0;
3665 }
3666
3667 /* Link the page into the page hash table */
3668 h = pgno & (pPager->nHash-1);
3669 assert( pgno!=0 );
3670 pPg->pNextHash = pPager->aHash[h];
3671 pPager->aHash[h] = pPg;
3672 if( pPg->pNextHash ){
3673 assert( pPg->pNextHash->pPrevHash==0 );
3674 pPg->pNextHash->pPrevHash = pPg;
3675 }
3676
3677#ifdef SQLITE_CHECK_PAGES
3678 pPg->pageHash = pager_pagehash(pPg);
3679#endif
3680 }else{
3681 /* The requested page is in the page cache. */
3682 assert(pPager->nRef>0 || pgno==1);
3683 PAGER_INCR(pPager->nHit);
3684 if( !noContent ){
3685 rc = pager_get_content(pPg);
3686 if( rc ){
3687 return rc;
3688 }
3689 }
3690 page_ref(pPg);
3691 }
3692 *ppPage = pPg;
3693 return SQLITE_OK;
3694}
3695int sqlite3PagerAcquire(
3696 Pager *pPager, /* The pager open on the database file */
3697 Pgno pgno, /* Page number to fetch */
3698 DbPage **ppPage, /* Write a pointer to the page here */
3699 int noContent /* Do not bother reading content from disk if true */
3700){
3701 int rc;
3702 pagerEnter(pPager);
3703 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3704 pagerLeave(pPager);
3705 return rc;
3706}
3707
3708
3709/*
3710** Acquire a page if it is already in the in-memory cache. Do
3711** not read the page from disk. Return a pointer to the page,
3712** or 0 if the page is not in cache.
3713**
3714** See also sqlite3PagerGet(). The difference between this routine
3715** and sqlite3PagerGet() is that _get() will go to the disk and read
3716** in the page if the page is not already in cache. This routine
3717** returns NULL if the page is not in cache or if a disk I/O error
3718** has ever happened.
3719*/
3720DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
3721 PgHdr *pPg = 0;
3722
3723 assert( pPager!=0 );
3724 assert( pgno!=0 );
3725
3726 pagerEnter(pPager);
3727 if( pPager->state==PAGER_UNLOCK ){
3728 assert( !pPager->pAll || pPager->exclusiveMode );
3729 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3730 /* Do nothing */
3731 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3732 page_ref(pPg);
3733 }
3734 pagerLeave(pPager);
3735 return pPg;
3736}
3737
3738/*
3739** Release a page.
3740**
3741** If the number of references to the page drop to zero, then the
3742** page is added to the LRU list. When all references to all pages
3743** are released, a rollback occurs and the lock on the database is
3744** removed.
3745*/
3746int sqlite3PagerUnref(DbPage *pPg){
3747 Pager *pPager = pPg->pPager;
3748
3749 /* Decrement the reference count for this page
3750 */
3751 assert( pPg->nRef>0 );
3752 pagerEnter(pPg->pPager);
3753 pPg->nRef--;
3754 REFINFO(pPg);
3755
3756 CHECK_PAGE(pPg);
3757
3758 /* When the number of references to a page reach 0, call the
3759 ** destructor and add the page to the freelist.
3760 */
3761 if( pPg->nRef==0 ){
3762
3763 lruListAdd(pPg);
3764 if( pPager->xDestructor ){
3765 pPager->xDestructor(pPg, pPager->pageSize);
3766 }
3767
3768 /* When all pages reach the freelist, drop the read lock from
3769 ** the database file.
3770 */
3771 pPager->nRef--;
3772 assert( pPager->nRef>=0 );
3773 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
3774 pagerUnlockAndRollback(pPager);
3775 }
3776 }
3777 pagerLeave(pPager);
3778 return SQLITE_OK;
3779}
3780
3781/*
3782** Create a journal file for pPager. There should already be a RESERVED
3783** or EXCLUSIVE lock on the database file when this routine is called.
3784**
3785** Return SQLITE_OK if everything. Return an error code and release the
3786** write lock if anything goes wrong.
3787*/
3788static int pager_open_journal(Pager *pPager){
3789 sqlite3_vfs *pVfs = pPager->pVfs;
3790 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3791
3792 int rc;
3793 assert( !MEMDB );
3794 assert( pPager->state>=PAGER_RESERVED );
3795 assert( pPager->journalOpen==0 );
3796 assert( pPager->useJournal );
3797 assert( pPager->aInJournal==0 );
3798 sqlite3PagerPagecount(pPager);
3799 pagerLeave(pPager);
3800 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
3801 pagerEnter(pPager);
3802 if( pPager->aInJournal==0 ){
3803 rc = SQLITE_NOMEM;
3804 goto failed_to_open_journal;
3805 }
3806
3807 if( pPager->tempFile ){
3808 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3809 }else{
3810 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
3811 }
3812#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3813 rc = sqlite3JournalOpen(
3814 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3815 );
3816#else
3817 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3818#endif
3819 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3820 pPager->journalOff = 0;
3821 pPager->setMaster = 0;
3822 pPager->journalHdr = 0;
3823 if( rc!=SQLITE_OK ){
3824 if( rc==SQLITE_NOMEM ){
3825 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3826 }
3827 goto failed_to_open_journal;
3828 }
3829 pPager->journalOpen = 1;
3830 pPager->journalStarted = 0;
3831 pPager->needSync = 0;
3832 pPager->alwaysRollback = 0;
3833 pPager->nRec = 0;
3834 if( pPager->errCode ){
3835 rc = pPager->errCode;
3836 goto failed_to_open_journal;
3837 }
3838 pPager->origDbSize = pPager->dbSize;
3839
3840 rc = writeJournalHdr(pPager);
3841
3842 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
3843 rc = sqlite3PagerStmtBegin(pPager);
3844 }
3845 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
3846 rc = pager_end_transaction(pPager);
3847 if( rc==SQLITE_OK ){
3848 rc = SQLITE_FULL;
3849 }
3850 }
3851 return rc;
3852
3853failed_to_open_journal:
3854 sqlite3_free(pPager->aInJournal);
3855 pPager->aInJournal = 0;
3856 return rc;
3857}
3858
3859/*
3860** Acquire a write-lock on the database. The lock is removed when
3861** the any of the following happen:
3862**
3863** * sqlite3PagerCommitPhaseTwo() is called.
3864** * sqlite3PagerRollback() is called.
3865** * sqlite3PagerClose() is called.
3866** * sqlite3PagerUnref() is called to on every outstanding page.
3867**
3868** The first parameter to this routine is a pointer to any open page of the
3869** database file. Nothing changes about the page - it is used merely to
3870** acquire a pointer to the Pager structure and as proof that there is
3871** already a read-lock on the database.
3872**
3873** The second parameter indicates how much space in bytes to reserve for a
3874** master journal file-name at the start of the journal when it is created.
3875**
3876** A journal file is opened if this is not a temporary file. For temporary
3877** files, the opening of the journal file is deferred until there is an
3878** actual need to write to the journal.
3879**
3880** If the database is already reserved for writing, this routine is a no-op.
3881**
3882** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3883** immediately instead of waiting until we try to flush the cache. The
3884** exFlag is ignored if a transaction is already active.
3885*/
3886int sqlite3PagerBegin(DbPage *pPg, int exFlag){
3887 Pager *pPager = pPg->pPager;
3888 int rc = SQLITE_OK;
3889 pagerEnter(pPager);
3890 assert( pPg->nRef>0 );
3891 assert( pPager->state!=PAGER_UNLOCK );
3892 if( pPager->state==PAGER_SHARED ){
3893 assert( pPager->aInJournal==0 );
3894 if( MEMDB ){
3895 pPager->state = PAGER_EXCLUSIVE;
3896 pPager->origDbSize = pPager->dbSize;
3897 }else{
3898 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3899 if( rc==SQLITE_OK ){
3900 pPager->state = PAGER_RESERVED;
3901 if( exFlag ){
3902 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3903 }
3904 }
3905 if( rc!=SQLITE_OK ){
3906 pagerLeave(pPager);
3907 return rc;
3908 }
3909 pPager->dirtyCache = 0;
3910 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
3911 if( pPager->useJournal && !pPager->tempFile ){
3912 rc = pager_open_journal(pPager);
3913 }
3914 }
3915 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3916 /* This happens when the pager was in exclusive-access mode last
3917 ** time a (read or write) transaction was successfully concluded
3918 ** by this connection. Instead of deleting the journal file it was
3919 ** kept open and truncated to 0 bytes.
3920 */
3921 assert( pPager->nRec==0 );
3922 assert( pPager->origDbSize==0 );
3923 assert( pPager->aInJournal==0 );
3924 sqlite3PagerPagecount(pPager);
3925 pagerLeave(pPager);
3926 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
3927 pagerEnter(pPager);
3928 if( !pPager->aInJournal ){
3929 rc = SQLITE_NOMEM;
3930 }else{
3931 pPager->origDbSize = pPager->dbSize;
3932 rc = writeJournalHdr(pPager);
3933 }
3934 }
3935 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
3936 pagerLeave(pPager);
3937 return rc;
3938}
3939
3940/*
3941** Make a page dirty. Set its dirty flag and add it to the dirty
3942** page list.
3943*/
3944static void makeDirty(PgHdr *pPg){
3945 if( pPg->dirty==0 ){
3946 Pager *pPager = pPg->pPager;
3947 pPg->dirty = 1;
3948 pPg->pDirty = pPager->pDirty;
3949 if( pPager->pDirty ){
3950 pPager->pDirty->pPrevDirty = pPg;
3951 }
3952 pPg->pPrevDirty = 0;
3953 pPager->pDirty = pPg;
3954 }
3955}
3956
3957/*
3958** Make a page clean. Clear its dirty bit and remove it from the
3959** dirty page list.
3960*/
3961static void makeClean(PgHdr *pPg){
3962 if( pPg->dirty ){
3963 pPg->dirty = 0;
3964 if( pPg->pDirty ){
3965 assert( pPg->pDirty->pPrevDirty==pPg );
3966 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3967 }
3968 if( pPg->pPrevDirty ){
3969 assert( pPg->pPrevDirty->pDirty==pPg );
3970 pPg->pPrevDirty->pDirty = pPg->pDirty;
3971 }else{
3972 assert( pPg->pPager->pDirty==pPg );
3973 pPg->pPager->pDirty = pPg->pDirty;
3974 }
3975 }
3976}
3977
3978
3979/*
3980** Mark a data page as writeable. The page is written into the journal
3981** if it is not there already. This routine must be called before making
3982** changes to a page.
3983**
3984** The first time this routine is called, the pager creates a new
3985** journal and acquires a RESERVED lock on the database. If the RESERVED
3986** lock could not be acquired, this routine returns SQLITE_BUSY. The
3987** calling routine must check for that return value and be careful not to
3988** change any page data until this routine returns SQLITE_OK.
3989**
3990** If the journal file could not be written because the disk is full,
3991** then this routine returns SQLITE_FULL and does an immediate rollback.
3992** All subsequent write attempts also return SQLITE_FULL until there
3993** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
3994** reset.
3995*/
3996static int pager_write(PgHdr *pPg){
3997 void *pData = PGHDR_TO_DATA(pPg);
3998 Pager *pPager = pPg->pPager;
3999 int rc = SQLITE_OK;
4000
4001 /* Check for errors
4002 */
4003 if( pPager->errCode ){
4004 return pPager->errCode;
4005 }
4006 if( pPager->readOnly ){
4007 return SQLITE_PERM;
4008 }
4009
4010 assert( !pPager->setMaster );
4011
4012 CHECK_PAGE(pPg);
4013
4014 /* If this page was previously acquired with noContent==1, that means
4015 ** we didn't really read in the content of the page. This can happen
4016 ** (for example) when the page is being moved to the freelist. But
4017 ** now we are (perhaps) moving the page off of the freelist for
4018 ** reuse and we need to know its original content so that content
4019 ** can be stored in the rollback journal. So do the read at this
4020 ** time.
4021 */
4022 rc = pager_get_content(pPg);
4023 if( rc ){
4024 return rc;
4025 }
4026
4027 /* Mark the page as dirty. If the page has already been written
4028 ** to the journal then we can return right away.
4029 */
4030 makeDirty(pPg);
4031 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
4032 pPager->dirtyCache = 1;
4033 }else{
4034
4035 /* If we get this far, it means that the page needs to be
4036 ** written to the transaction journal or the ckeckpoint journal
4037 ** or both.
4038 **
4039 ** First check to see that the transaction journal exists and
4040 ** create it if it does not.
4041 */
4042 assert( pPager->state!=PAGER_UNLOCK );
4043 rc = sqlite3PagerBegin(pPg, 0);
4044 if( rc!=SQLITE_OK ){
4045 return rc;
4046 }
4047 assert( pPager->state>=PAGER_RESERVED );
4048 if( !pPager->journalOpen && pPager->useJournal ){
4049 rc = pager_open_journal(pPager);
4050 if( rc!=SQLITE_OK ) return rc;
4051 }
4052 assert( pPager->journalOpen || !pPager->useJournal );
4053 pPager->dirtyCache = 1;
4054
4055 /* The transaction journal now exists and we have a RESERVED or an
4056 ** EXCLUSIVE lock on the main database file. Write the current page to
4057 ** the transaction journal if it is not there already.
4058 */
4059 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
4060 if( (int)pPg->pgno <= pPager->origDbSize ){
4061 if( MEMDB ){
4062 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4063 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
4064 assert( pHist->pOrig==0 );
4065 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
4066 if( pHist->pOrig ){
4067 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
4068 }
4069 }else{
4070 u32 cksum;
4071 char *pData2;
4072
4073 /* We should never write to the journal file the page that
4074 ** contains the database locks. The following assert verifies
4075 ** that we do not. */
4076 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
4077 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4078 cksum = pager_cksum(pPager, (u8*)pData2);
4079 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4080 if( rc==SQLITE_OK ){
4081 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4082 pPager->journalOff + 4);
4083 pPager->journalOff += pPager->pageSize+4;
4084 }
4085 if( rc==SQLITE_OK ){
4086 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4087 pPager->journalOff += 4;
4088 }
4089 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
4090 pPager->journalOff, pPager->pageSize));
4091 PAGER_INCR(sqlite3_pager_writej_count);
4092 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4093 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
4094
4095 /* An error has occured writing to the journal file. The
4096 ** transaction will be rolled back by the layer above.
4097 */
4098 if( rc!=SQLITE_OK ){
4099 return rc;
4100 }
4101
4102 pPager->nRec++;
4103 assert( pPager->aInJournal!=0 );
4104 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4105 pPg->needSync = !pPager->noSync;
4106 if( pPager->stmtInUse ){
4107 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4108 }
4109 }
4110 }else{
4111 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
4112 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
4113 PAGERID(pPager), pPg->pgno, pPg->needSync);
4114 }
4115 if( pPg->needSync ){
4116 pPager->needSync = 1;
4117 }
4118 pPg->inJournal = 1;
4119 }
4120
4121 /* If the statement journal is open and the page is not in it,
4122 ** then write the current page to the statement journal. Note that
4123 ** the statement journal format differs from the standard journal format
4124 ** in that it omits the checksums and the header.
4125 */
4126 if( pPager->stmtInUse
4127 && !pageInStatement(pPg)
4128 && (int)pPg->pgno<=pPager->stmtSize
4129 ){
4130 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4131 if( MEMDB ){
4132 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4133 assert( pHist->pStmt==0 );
4134 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
4135 if( pHist->pStmt ){
4136 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4137 }
4138 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
4139 page_add_to_stmt_list(pPg);
4140 }else{
4141 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
4142 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4143 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4144 if( rc==SQLITE_OK ){
4145 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4146 }
4147 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
4148 if( rc!=SQLITE_OK ){
4149 return rc;
4150 }
4151 pPager->stmtNRec++;
4152 assert( pPager->aInStmt!=0 );
4153 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4154 }
4155 }
4156 }
4157
4158 /* Update the database size and return.
4159 */
4160 assert( pPager->state>=PAGER_SHARED );
4161 if( pPager->dbSize<(int)pPg->pgno ){
4162 pPager->dbSize = pPg->pgno;
4163 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
4164 pPager->dbSize++;
4165 }
4166 }
4167 return rc;
4168}
4169
4170/*
4171** This function is used to mark a data-page as writable. It uses
4172** pager_write() to open a journal file (if it is not already open)
4173** and write the page *pData to the journal.
4174**
4175** The difference between this function and pager_write() is that this
4176** function also deals with the special case where 2 or more pages
4177** fit on a single disk sector. In this case all co-resident pages
4178** must have been written to the journal file before returning.
4179*/
4180int sqlite3PagerWrite(DbPage *pDbPage){
4181 int rc = SQLITE_OK;
4182
4183 PgHdr *pPg = pDbPage;
4184 Pager *pPager = pPg->pPager;
4185 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4186
4187 pagerEnter(pPager);
4188 if( !MEMDB && nPagePerSector>1 ){
4189 Pgno nPageCount; /* Total number of pages in database file */
4190 Pgno pg1; /* First page of the sector pPg is located on. */
4191 int nPage; /* Number of pages starting at pg1 to journal */
4192 int ii;
4193 int needSync = 0;
4194
4195 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4196 ** header to be written between the pages journaled by this function.
4197 */
4198 assert( pPager->doNotSync==0 );
4199 pPager->doNotSync = 1;
4200
4201 /* This trick assumes that both the page-size and sector-size are
4202 ** an integer power of 2. It sets variable pg1 to the identifier
4203 ** of the first page of the sector pPg is located on.
4204 */
4205 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4206
4207 nPageCount = sqlite3PagerPagecount(pPager);
4208 if( pPg->pgno>nPageCount ){
4209 nPage = (pPg->pgno - pg1)+1;
4210 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4211 nPage = nPageCount+1-pg1;
4212 }else{
4213 nPage = nPagePerSector;
4214 }
4215 assert(nPage>0);
4216 assert(pg1<=pPg->pgno);
4217 assert((pg1+nPage)>pPg->pgno);
4218
4219 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4220 Pgno pg = pg1+ii;
4221 PgHdr *pPage;
4222 if( !pPager->aInJournal || pg==pPg->pgno ||
4223 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
4224 ) {
4225 if( pg!=PAGER_MJ_PGNO(pPager) ){
4226 rc = sqlite3PagerGet(pPager, pg, &pPage);
4227 if( rc==SQLITE_OK ){
4228 rc = pager_write(pPage);
4229 if( pPage->needSync ){
4230 needSync = 1;
4231 }
4232 sqlite3PagerUnref(pPage);
4233 }
4234 }
4235 }else if( (pPage = pager_lookup(pPager, pg)) ){
4236 if( pPage->needSync ){
4237 needSync = 1;
4238 }
4239 }
4240 }
4241
4242 /* If the PgHdr.needSync flag is set for any of the nPage pages
4243 ** starting at pg1, then it needs to be set for all of them. Because
4244 ** writing to any of these nPage pages may damage the others, the
4245 ** journal file must contain sync()ed copies of all of them
4246 ** before any of them can be written out to the database file.
4247 */
4248 if( needSync ){
4249 for(ii=0; ii<nPage && needSync; ii++){
4250 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4251 if( pPage ) pPage->needSync = 1;
4252 }
4253 assert(pPager->needSync);
4254 }
4255
4256 assert( pPager->doNotSync==1 );
4257 pPager->doNotSync = 0;
4258 }else{
4259 rc = pager_write(pDbPage);
4260 }
4261 pagerLeave(pPager);
4262 return rc;
4263}
4264
4265/*
4266** Return TRUE if the page given in the argument was previously passed
4267** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
4268** to change the content of the page.
4269*/
4270#ifndef NDEBUG
4271int sqlite3PagerIswriteable(DbPage *pPg){
4272 return pPg->dirty;
4273}
4274#endif
4275
4276#ifndef SQLITE_OMIT_VACUUM
4277/*
4278** Replace the content of a single page with the information in the third
4279** argument.
4280*/
4281int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4282 PgHdr *pPg;
4283 int rc;
4284
4285 pagerEnter(pPager);
4286 rc = sqlite3PagerGet(pPager, pgno, &pPg);
4287 if( rc==SQLITE_OK ){
4288 rc = sqlite3PagerWrite(pPg);
4289 if( rc==SQLITE_OK ){
4290 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
4291 }
4292 sqlite3PagerUnref(pPg);
4293 }
4294 pagerLeave(pPager);
4295 return rc;
4296}
4297#endif
4298
4299/*
4300** A call to this routine tells the pager that it is not necessary to
4301** write the information on page pPg back to the disk, even though
4302** that page might be marked as dirty.
4303**
4304** The overlying software layer calls this routine when all of the data
4305** on the given page is unused. The pager marks the page as clean so
4306** that it does not get written to disk.
4307**
4308** Tests show that this optimization, together with the
4309** sqlite3PagerDontRollback() below, more than double the speed
4310** of large INSERT operations and quadruple the speed of large DELETEs.
4311**
4312** When this routine is called, set the alwaysRollback flag to true.
4313** Subsequent calls to sqlite3PagerDontRollback() for the same page
4314** will thereafter be ignored. This is necessary to avoid a problem
4315** where a page with data is added to the freelist during one part of
4316** a transaction then removed from the freelist during a later part
4317** of the same transaction and reused for some other purpose. When it
4318** is first added to the freelist, this routine is called. When reused,
4319** the sqlite3PagerDontRollback() routine is called. But because the
4320** page contains critical data, we still need to be sure it gets
4321** rolled back in spite of the sqlite3PagerDontRollback() call.
4322*/
4323void sqlite3PagerDontWrite(DbPage *pDbPage){
4324 PgHdr *pPg = pDbPage;
4325 Pager *pPager = pPg->pPager;
4326
4327 if( MEMDB ) return;
4328 pagerEnter(pPager);
4329 pPg->alwaysRollback = 1;
4330 if( pPg->dirty && !pPager->stmtInUse ){
4331 assert( pPager->state>=PAGER_SHARED );
4332 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4333 /* If this pages is the last page in the file and the file has grown
4334 ** during the current transaction, then do NOT mark the page as clean.
4335 ** When the database file grows, we must make sure that the last page
4336 ** gets written at least once so that the disk file will be the correct
4337 ** size. If you do not write this page and the size of the file
4338 ** on the disk ends up being too small, that can lead to database
4339 ** corruption during the next transaction.
4340 */
4341 }else{
4342 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4343 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
4344 makeClean(pPg);
4345#ifdef SQLITE_CHECK_PAGES
4346 pPg->pageHash = pager_pagehash(pPg);
4347#endif
4348 }
4349 }
4350 pagerLeave(pPager);
4351}
4352
4353/*
4354** A call to this routine tells the pager that if a rollback occurs,
4355** it is not necessary to restore the data on the given page. This
4356** means that the pager does not have to record the given page in the
4357** rollback journal.
4358**
4359** If we have not yet actually read the content of this page (if
4360** the PgHdr.needRead flag is set) then this routine acts as a promise
4361** that we will never need to read the page content in the future.
4362** so the needRead flag can be cleared at this point.
4363*/
4364void sqlite3PagerDontRollback(DbPage *pPg){
4365 Pager *pPager = pPg->pPager;
4366
4367 pagerEnter(pPager);
4368 assert( pPager->state>=PAGER_RESERVED );
4369 if( pPager->journalOpen==0 ) return;
4370 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
4371 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
4372 assert( pPager->aInJournal!=0 );
4373 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4374 pPg->inJournal = 1;
4375 pPg->needRead = 0;
4376 if( pPager->stmtInUse ){
4377 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4378 }
4379 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4380 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
4381 }
4382 if( pPager->stmtInUse
4383 && !pageInStatement(pPg)
4384 && (int)pPg->pgno<=pPager->stmtSize
4385 ){
4386 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4387 assert( pPager->aInStmt!=0 );
4388 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4389 }
4390 pagerLeave(pPager);
4391}
4392
4393
4394/*
4395** This routine is called to increment the database file change-counter,
4396** stored at byte 24 of the pager file.
4397*/
4398static int pager_incr_changecounter(Pager *pPager, int isDirect){
4399 PgHdr *pPgHdr;
4400 u32 change_counter;
4401 int rc = SQLITE_OK;
4402
4403 if( !pPager->changeCountDone ){
4404 /* Open page 1 of the file for writing. */
4405 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4406 if( rc!=SQLITE_OK ) return rc;
4407
4408 if( !isDirect ){
4409 rc = sqlite3PagerWrite(pPgHdr);
4410 if( rc!=SQLITE_OK ){
4411 sqlite3PagerUnref(pPgHdr);
4412 return rc;
4413 }
4414 }
4415
4416 /* Increment the value just read and write it back to byte 24. */
4417 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
4418 change_counter++;
4419 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
4420
4421 if( isDirect && pPager->fd->pMethods ){
4422 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4423 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4424 }
4425
4426 /* Release the page reference. */
4427 sqlite3PagerUnref(pPgHdr);
4428 pPager->changeCountDone = 1;
4429 }
4430 return rc;
4431}
4432
4433/*
4434** Sync the database file for the pager pPager. zMaster points to the name
4435** of a master journal file that should be written into the individual
4436** journal file. zMaster may be NULL, which is interpreted as no master
4437** journal (a single database transaction).
4438**
4439** This routine ensures that the journal is synced, all dirty pages written
4440** to the database file and the database file synced. The only thing that
4441** remains to commit the transaction is to delete the journal file (or
4442** master journal file if specified).
4443**
4444** Note that if zMaster==NULL, this does not overwrite a previous value
4445** passed to an sqlite3PagerCommitPhaseOne() call.
4446**
4447** If parameter nTrunc is non-zero, then the pager file is truncated to
4448** nTrunc pages (this is used by auto-vacuum databases).
4449*/
4450int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4451 int rc = SQLITE_OK;
4452
4453 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4454 pPager->zFilename, zMaster, nTrunc);
4455 pagerEnter(pPager);
4456
4457 /* If this is an in-memory db, or no pages have been written to, or this
4458 ** function has already been called, it is a no-op.
4459 */
4460 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4461 PgHdr *pPg;
4462
4463#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4464 /* The atomic-write optimization can be used if all of the
4465 ** following are true:
4466 **
4467 ** + The file-system supports the atomic-write property for
4468 ** blocks of size page-size, and
4469 ** + This commit is not part of a multi-file transaction, and
4470 ** + Exactly one page has been modified and store in the journal file.
4471 **
4472 ** If the optimization can be used, then the journal file will never
4473 ** be created for this transaction.
4474 */
4475 int useAtomicWrite = (
4476 !zMaster &&
4477 pPager->journalOff==jrnlBufferSize(pPager) &&
4478 nTrunc==0 &&
4479 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4480 );
4481 if( useAtomicWrite ){
4482 /* Update the nRec field in the journal file. */
4483 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4484 assert(pPager->nRec==1);
4485 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4486
4487 /* Update the db file change counter. The following call will modify
4488 ** the in-memory representation of page 1 to include the updated
4489 ** change counter and then write page 1 directly to the database
4490 ** file. Because of the atomic-write property of the host file-system,
4491 ** this is safe.
4492 */
4493 if( rc==SQLITE_OK ){
4494 rc = pager_incr_changecounter(pPager, 1);
4495 }
4496 }else{
4497 rc = sqlite3JournalCreate(pPager->jfd);
4498 }
4499
4500 if( !useAtomicWrite && rc==SQLITE_OK )
4501#endif
4502
4503 /* If a master journal file name has already been written to the
4504 ** journal file, then no sync is required. This happens when it is
4505 ** written, then the process fails to upgrade from a RESERVED to an
4506 ** EXCLUSIVE lock. The next time the process tries to commit the
4507 ** transaction the m-j name will have already been written.
4508 */
4509 if( !pPager->setMaster ){
4510 assert( pPager->journalOpen );
4511 rc = pager_incr_changecounter(pPager, 0);
4512 if( rc!=SQLITE_OK ) goto sync_exit;
4513#ifndef SQLITE_OMIT_AUTOVACUUM
4514 if( nTrunc!=0 ){
4515 /* If this transaction has made the database smaller, then all pages
4516 ** being discarded by the truncation must be written to the journal
4517 ** file.
4518 */
4519 Pgno i;
4520 int iSkip = PAGER_MJ_PGNO(pPager);
4521 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4522 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4523 rc = sqlite3PagerGet(pPager, i, &pPg);
4524 if( rc!=SQLITE_OK ) goto sync_exit;
4525 rc = sqlite3PagerWrite(pPg);
4526 sqlite3PagerUnref(pPg);
4527 if( rc!=SQLITE_OK ) goto sync_exit;
4528 }
4529 }
4530 }
4531#endif
4532 rc = writeMasterJournal(pPager, zMaster);
4533 if( rc!=SQLITE_OK ) goto sync_exit;
4534 rc = syncJournal(pPager);
4535 }
4536 if( rc!=SQLITE_OK ) goto sync_exit;
4537
4538#ifndef SQLITE_OMIT_AUTOVACUUM
4539 if( nTrunc!=0 ){
4540 rc = sqlite3PagerTruncate(pPager, nTrunc);
4541 if( rc!=SQLITE_OK ) goto sync_exit;
4542 }
4543#endif
4544
4545 /* Write all dirty pages to the database file */
4546 pPg = pager_get_all_dirty_pages(pPager);
4547 rc = pager_write_pagelist(pPg);
4548 if( rc!=SQLITE_OK ){
4549 while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
4550 pPager->pDirty = pPg;
4551 goto sync_exit;
4552 }
4553 pPager->pDirty = 0;
4554
4555 /* Sync the database file. */
4556 if( !pPager->noSync ){
4557 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4558 }
4559 IOTRACE(("DBSYNC %p\n", pPager))
4560
4561 pPager->state = PAGER_SYNCED;
4562 }else if( MEMDB && nTrunc!=0 ){
4563 rc = sqlite3PagerTruncate(pPager, nTrunc);
4564 }
4565
4566sync_exit:
4567 if( rc==SQLITE_IOERR_BLOCKED ){
4568 /* pager_incr_changecounter() may attempt to obtain an exclusive
4569 * lock to spill the cache and return IOERR_BLOCKED. But since
4570 * there is no chance the cache is inconsistent, it's
4571 * better to return SQLITE_BUSY.
4572 */
4573 rc = SQLITE_BUSY;
4574 }
4575 pagerLeave(pPager);
4576 return rc;
4577}
4578
4579
4580/*
4581** Commit all changes to the database and release the write lock.
4582**
4583** If the commit fails for any reason, a rollback attempt is made
4584** and an error code is returned. If the commit worked, SQLITE_OK
4585** is returned.
4586*/
4587int sqlite3PagerCommitPhaseTwo(Pager *pPager){
4588 int rc;
4589 PgHdr *pPg;
4590
4591 if( pPager->errCode ){
4592 return pPager->errCode;
4593 }
4594 if( pPager->state<PAGER_RESERVED ){
4595 return SQLITE_ERROR;
4596 }
4597 pagerEnter(pPager);
4598 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
4599 if( MEMDB ){
4600 pPg = pager_get_all_dirty_pages(pPager);
4601 while( pPg ){
4602 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4603 clearHistory(pHist);
4604 pPg->dirty = 0;
4605 pPg->inJournal = 0;
4606 pHist->inStmt = 0;
4607 pPg->needSync = 0;
4608 pHist->pPrevStmt = pHist->pNextStmt = 0;
4609 pPg = pPg->pDirty;
4610 }
4611 pPager->pDirty = 0;
4612#ifndef NDEBUG
4613 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4614 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4615 assert( !pPg->alwaysRollback );
4616 assert( !pHist->pOrig );
4617 assert( !pHist->pStmt );
4618 }
4619#endif
4620 pPager->pStmt = 0;
4621 pPager->state = PAGER_SHARED;
4622 return SQLITE_OK;
4623 }
4624 assert( pPager->journalOpen || !pPager->dirtyCache );
4625 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4626 rc = pager_end_transaction(pPager);
4627 rc = pager_error(pPager, rc);
4628 pagerLeave(pPager);
4629 return rc;
4630}
4631
4632/*
4633** Rollback all changes. The database falls back to PAGER_SHARED mode.
4634** All in-memory cache pages revert to their original data contents.
4635** The journal is deleted.
4636**
4637** This routine cannot fail unless some other process is not following
4638** the correct locking protocol or unless some other
4639** process is writing trash into the journal file (SQLITE_CORRUPT) or
4640** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4641** codes are returned for all these occasions. Otherwise,
4642** SQLITE_OK is returned.
4643*/
4644int sqlite3PagerRollback(Pager *pPager){
4645 int rc;
4646 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
4647 if( MEMDB ){
4648 PgHdr *p;
4649 for(p=pPager->pAll; p; p=p->pNextAll){
4650 PgHistory *pHist;
4651 assert( !p->alwaysRollback );
4652 if( !p->dirty ){
4653 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4654 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4655 continue;
4656 }
4657
4658 pHist = PGHDR_TO_HIST(p, pPager);
4659 if( pHist->pOrig ){
4660 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
4661 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
4662 }else{
4663 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
4664 }
4665 clearHistory(pHist);
4666 p->dirty = 0;
4667 p->inJournal = 0;
4668 pHist->inStmt = 0;
4669 pHist->pPrevStmt = pHist->pNextStmt = 0;
4670 if( pPager->xReiniter ){
4671 pPager->xReiniter(p, pPager->pageSize);
4672 }
4673 }
4674 pPager->pDirty = 0;
4675 pPager->pStmt = 0;
4676 pPager->dbSize = pPager->origDbSize;
4677 pager_truncate_cache(pPager);
4678 pPager->stmtInUse = 0;
4679 pPager->state = PAGER_SHARED;
4680 return SQLITE_OK;
4681 }
4682
4683 pagerEnter(pPager);
4684 if( !pPager->dirtyCache || !pPager->journalOpen ){
4685 rc = pager_end_transaction(pPager);
4686 pagerLeave(pPager);
4687 return rc;
4688 }
4689
4690 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
4691 if( pPager->state>=PAGER_EXCLUSIVE ){
4692 pager_playback(pPager, 0);
4693 }
4694 pagerLeave(pPager);
4695 return pPager->errCode;
4696 }
4697 if( pPager->state==PAGER_RESERVED ){
4698 int rc2;
4699 rc = pager_playback(pPager, 0);
4700 rc2 = pager_end_transaction(pPager);
4701 if( rc==SQLITE_OK ){
4702 rc = rc2;
4703 }
4704 }else{
4705 rc = pager_playback(pPager, 0);
4706 }
4707 /* pager_reset(pPager); */
4708 pPager->dbSize = -1;
4709
4710 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4711 ** cache. So call pager_error() on the way out to make any error
4712 ** persistent.
4713 */
4714 rc = pager_error(pPager, rc);
4715 pagerLeave(pPager);
4716 return rc;
4717}
4718
4719/*
4720** Return TRUE if the database file is opened read-only. Return FALSE
4721** if the database is (in theory) writable.
4722*/
4723int sqlite3PagerIsreadonly(Pager *pPager){
4724 return pPager->readOnly;
4725}
4726
4727/*
4728** Return the number of references to the pager.
4729*/
4730int sqlite3PagerRefcount(Pager *pPager){
4731 return pPager->nRef;
4732}
4733
4734#ifdef SQLITE_TEST
4735/*
4736** This routine is used for testing and analysis only.
4737*/
4738int *sqlite3PagerStats(Pager *pPager){
4739 static int a[11];
4740 a[0] = pPager->nRef;
4741 a[1] = pPager->nPage;
4742 a[2] = pPager->mxPage;
4743 a[3] = pPager->dbSize;
4744 a[4] = pPager->state;
4745 a[5] = pPager->errCode;
4746 a[6] = pPager->nHit;
4747 a[7] = pPager->nMiss;
4748 a[8] = 0; /* Used to be pPager->nOvfl */
4749 a[9] = pPager->nRead;
4750 a[10] = pPager->nWrite;
4751 return a;
4752}
4753#endif
4754
4755/*
4756** Set the statement rollback point.
4757**
4758** This routine should be called with the transaction journal already
4759** open. A new statement journal is created that can be used to rollback
4760** changes of a single SQL command within a larger transaction.
4761*/
4762static int pagerStmtBegin(Pager *pPager){
4763 int rc;
4764 assert( !pPager->stmtInUse );
4765 assert( pPager->state>=PAGER_SHARED );
4766 assert( pPager->dbSize>=0 );
4767 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
4768 if( MEMDB ){
4769 pPager->stmtInUse = 1;
4770 pPager->stmtSize = pPager->dbSize;
4771 return SQLITE_OK;
4772 }
4773 if( !pPager->journalOpen ){
4774 pPager->stmtAutoopen = 1;
4775 return SQLITE_OK;
4776 }
4777 assert( pPager->journalOpen );
4778 pagerLeave(pPager);
4779 assert( pPager->aInStmt==0 );
4780 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
4781 pagerEnter(pPager);
4782 if( pPager->aInStmt==0 ){
4783 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
4784 return SQLITE_NOMEM;
4785 }
4786#ifndef NDEBUG
4787 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
4788 if( rc ) goto stmt_begin_failed;
4789 assert( pPager->stmtJSize == pPager->journalOff );
4790#endif
4791 pPager->stmtJSize = pPager->journalOff;
4792 pPager->stmtSize = pPager->dbSize;
4793 pPager->stmtHdrOff = 0;
4794 pPager->stmtCksum = pPager->cksumInit;
4795 if( !pPager->stmtOpen ){
4796 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
4797 SQLITE_OPEN_SUBJOURNAL);
4798 if( rc ){
4799 goto stmt_begin_failed;
4800 }
4801 pPager->stmtOpen = 1;
4802 pPager->stmtNRec = 0;
4803 }
4804 pPager->stmtInUse = 1;
4805 return SQLITE_OK;
4806
4807stmt_begin_failed:
4808 if( pPager->aInStmt ){
4809 sqlite3_free(pPager->aInStmt);
4810 pPager->aInStmt = 0;
4811 }
4812 return rc;
4813}
4814int sqlite3PagerStmtBegin(Pager *pPager){
4815 int rc;
4816 pagerEnter(pPager);
4817 rc = pagerStmtBegin(pPager);
4818 pagerLeave(pPager);
4819 return rc;
4820}
4821
4822/*
4823** Commit a statement.
4824*/
4825int sqlite3PagerStmtCommit(Pager *pPager){
4826 pagerEnter(pPager);
4827 if( pPager->stmtInUse ){
4828 PgHdr *pPg, *pNext;
4829 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
4830 if( !MEMDB ){
4831 /* sqlite3OsTruncate(pPager->stfd, 0); */
4832 sqlite3_free( pPager->aInStmt );
4833 pPager->aInStmt = 0;
4834 }else{
4835 for(pPg=pPager->pStmt; pPg; pPg=pNext){
4836 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4837 pNext = pHist->pNextStmt;
4838 assert( pHist->inStmt );
4839 pHist->inStmt = 0;
4840 pHist->pPrevStmt = pHist->pNextStmt = 0;
4841 sqlite3_free(pHist->pStmt);
4842 pHist->pStmt = 0;
4843 }
4844 }
4845 pPager->stmtNRec = 0;
4846 pPager->stmtInUse = 0;
4847 pPager->pStmt = 0;
4848 }
4849 pPager->stmtAutoopen = 0;
4850 pagerLeave(pPager);
4851 return SQLITE_OK;
4852}
4853
4854/*
4855** Rollback a statement.
4856*/
4857int sqlite3PagerStmtRollback(Pager *pPager){
4858 int rc;
4859 pagerEnter(pPager);
4860 if( pPager->stmtInUse ){
4861 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
4862 if( MEMDB ){
4863 PgHdr *pPg;
4864 PgHistory *pHist;
4865 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4866 pHist = PGHDR_TO_HIST(pPg, pPager);
4867 if( pHist->pStmt ){
4868 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
4869 sqlite3_free(pHist->pStmt);
4870 pHist->pStmt = 0;
4871 }
4872 }
4873 pPager->dbSize = pPager->stmtSize;
4874 pager_truncate_cache(pPager);
4875 rc = SQLITE_OK;
4876 }else{
4877 rc = pager_stmt_playback(pPager);
4878 }
4879 sqlite3PagerStmtCommit(pPager);
4880 }else{
4881 rc = SQLITE_OK;
4882 }
4883 pPager->stmtAutoopen = 0;
4884 pagerLeave(pPager);
4885 return rc;
4886}
4887
4888/*
4889** Return the full pathname of the database file.
4890*/
4891const char *sqlite3PagerFilename(Pager *pPager){
4892 return pPager->zFilename;
4893}
4894
4895/*
4896** Return the VFS structure for the pager.
4897*/
4898const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4899 return pPager->pVfs;
4900}
4901
4902/*
4903** Return the file handle for the database file associated
4904** with the pager. This might return NULL if the file has
4905** not yet been opened.
4906*/
4907sqlite3_file *sqlite3PagerFile(Pager *pPager){
4908 return pPager->fd;
4909}
4910
4911/*
4912** Return the directory of the database file.
4913*/
4914const char *sqlite3PagerDirname(Pager *pPager){
4915 return pPager->zDirectory;
4916}
4917
4918/*
4919** Return the full pathname of the journal file.
4920*/
4921const char *sqlite3PagerJournalname(Pager *pPager){
4922 return pPager->zJournal;
4923}
4924
4925/*
4926** Return true if fsync() calls are disabled for this pager. Return FALSE
4927** if fsync()s are executed normally.
4928*/
4929int sqlite3PagerNosync(Pager *pPager){
4930 return pPager->noSync;
4931}
4932
4933#ifdef SQLITE_HAS_CODEC
4934/*
4935** Set the codec for this pager
4936*/
4937void sqlite3PagerSetCodec(
4938 Pager *pPager,
4939 void *(*xCodec)(void*,void*,Pgno,int),
4940 void *pCodecArg
4941){
4942 pPager->xCodec = xCodec;
4943 pPager->pCodecArg = pCodecArg;
4944}
4945#endif
4946
4947#ifndef SQLITE_OMIT_AUTOVACUUM
4948/*
4949** Move the page pPg to location pgno in the file.
4950**
4951** There must be no references to the page previously located at
4952** pgno (which we call pPgOld) though that page is allowed to be
4953** in cache. If the page previous located at pgno is not already
4954** in the rollback journal, it is not put there by by this routine.
4955**
4956** References to the page pPg remain valid. Updating any
4957** meta-data associated with pPg (i.e. data stored in the nExtra bytes
4958** allocated along with the page) is the responsibility of the caller.
4959**
4960** A transaction must be active when this routine is called. It used to be
4961** required that a statement transaction was not active, but this restriction
4962** has been removed (CREATE INDEX needs to move a page when a statement
4963** transaction is active).
4964*/
4965int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
4966 PgHdr *pPgOld; /* The page being overwritten. */
4967 int h;
4968 Pgno needSyncPgno = 0;
4969
4970 pagerEnter(pPager);
4971 assert( pPg->nRef>0 );
4972
4973 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
4974 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
4975 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
4976
4977 pager_get_content(pPg);
4978 if( pPg->needSync ){
4979 needSyncPgno = pPg->pgno;
4980 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
4981 assert( pPg->dirty );
4982 assert( pPager->needSync );
4983 }
4984
4985 /* Unlink pPg from it's hash-chain */
4986 unlinkHashChain(pPager, pPg);
4987
4988 /* If the cache contains a page with page-number pgno, remove it
4989 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4990 ** page pgno before the 'move' operation, it needs to be retained
4991 ** for the page moved there.
4992 */
4993 pPg->needSync = 0;
4994 pPgOld = pager_lookup(pPager, pgno);
4995 if( pPgOld ){
4996 assert( pPgOld->nRef==0 );
4997 unlinkHashChain(pPager, pPgOld);
4998 makeClean(pPgOld);
4999 pPg->needSync = pPgOld->needSync;
5000 }else{
5001 pPg->needSync = 0;
5002 }
5003 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
5004 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
5005 }else{
5006 pPg->inJournal = 0;
5007 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
5008 }
5009
5010 /* Change the page number for pPg and insert it into the new hash-chain. */
5011 assert( pgno!=0 );
5012 pPg->pgno = pgno;
5013 h = pgno & (pPager->nHash-1);
5014 if( pPager->aHash[h] ){
5015 assert( pPager->aHash[h]->pPrevHash==0 );
5016 pPager->aHash[h]->pPrevHash = pPg;
5017 }
5018 pPg->pNextHash = pPager->aHash[h];
5019 pPager->aHash[h] = pPg;
5020 pPg->pPrevHash = 0;
5021
5022 makeDirty(pPg);
5023 pPager->dirtyCache = 1;
5024
5025 if( needSyncPgno ){
5026 /* If needSyncPgno is non-zero, then the journal file needs to be
5027 ** sync()ed before any data is written to database file page needSyncPgno.
5028 ** Currently, no such page exists in the page-cache and the
5029 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
5030 ** the page into the pager-cache and setting the PgHdr.needSync flag.
5031 **
5032 ** The sqlite3PagerGet() call may cause the journal to sync. So make
5033 ** sure the Pager.needSync flag is set too.
5034 */
5035 int rc;
5036 PgHdr *pPgHdr;
5037 assert( pPager->needSync );
5038 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
5039 if( rc!=SQLITE_OK ) return rc;
5040 pPager->needSync = 1;
5041 pPgHdr->needSync = 1;
5042 pPgHdr->inJournal = 1;
5043 makeDirty(pPgHdr);
5044 sqlite3PagerUnref(pPgHdr);
5045 }
5046
5047 pagerLeave(pPager);
5048 return SQLITE_OK;
5049}
5050#endif
5051
5052/*
5053** Return a pointer to the data for the specified page.
5054*/
5055void *sqlite3PagerGetData(DbPage *pPg){
5056 return PGHDR_TO_DATA(pPg);
5057}
5058
5059/*
5060** Return a pointer to the Pager.nExtra bytes of "extra" space
5061** allocated along with the specified page.
5062*/
5063void *sqlite3PagerGetExtra(DbPage *pPg){
5064 Pager *pPager = pPg->pPager;
5065 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5066}
5067
5068/*
5069** Get/set the locking-mode for this pager. Parameter eMode must be one
5070** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5071** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5072** the locking-mode is set to the value specified.
5073**
5074** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5075** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5076** locking-mode.
5077*/
5078int sqlite3PagerLockingMode(Pager *pPager, int eMode){
5079 assert( eMode==PAGER_LOCKINGMODE_QUERY
5080 || eMode==PAGER_LOCKINGMODE_NORMAL
5081 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5082 assert( PAGER_LOCKINGMODE_QUERY<0 );
5083 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5084 if( eMode>=0 && !pPager->tempFile ){
5085 pPager->exclusiveMode = eMode;
5086 }
5087 return (int)pPager->exclusiveMode;
5088}
5089
5090#ifdef SQLITE_DEBUG
5091/*
5092** Print a listing of all referenced pages and their ref count.
5093*/
5094void sqlite3PagerRefdump(Pager *pPager){
5095 PgHdr *pPg;
5096 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5097 if( pPg->nRef<=0 ) continue;
5098 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5099 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
5100 }
5101}
5102#endif
5103
5104#endif /* SQLITE_OMIT_DISKIO */
diff --git a/libraries/sqlite/win32/pager.h b/libraries/sqlite/win32/pager.h
new file mode 100755
index 0000000..cf05b11
--- /dev/null
+++ b/libraries/sqlite/win32/pager.h
@@ -0,0 +1,125 @@
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.
10**
11*************************************************************************
12** This header file defines the interface that the sqlite page cache
13** subsystem. The page cache subsystem reads and writes a file a page
14** at a time and provides a journal for rollback.
15**
16** @(#) $Id: pager.h,v 1.67 2007/09/03 15:19:35 drh Exp $
17*/
18
19#ifndef _PAGER_H_
20#define _PAGER_H_
21
22/*
23** The type used to represent a page number. The first page in a file
24** is called page 1. 0 is used to represent "not a page".
25*/
26typedef unsigned int Pgno;
27
28/*
29** Each open file is managed by a separate instance of the "Pager" structure.
30*/
31typedef struct Pager Pager;
32
33/*
34** Handle type for pages.
35*/
36typedef struct PgHdr DbPage;
37
38/*
39** Allowed values for the flags parameter to sqlite3PagerOpen().
40**
41** NOTE: This values must match the corresponding BTREE_ values in btree.h.
42*/
43#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
44#define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
45
46/*
47** Valid values for the second argument to sqlite3PagerLockingMode().
48*/
49#define PAGER_LOCKINGMODE_QUERY -1
50#define PAGER_LOCKINGMODE_NORMAL 0
51#define PAGER_LOCKINGMODE_EXCLUSIVE 1
52
53/*
54** See source code comments for a detailed description of the following
55** routines:
56*/
57int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char*, int,int,int);
58void sqlite3PagerSetBusyhandler(Pager*, BusyHandler *pBusyHandler);
59void sqlite3PagerSetDestructor(Pager*, void(*)(DbPage*,int));
60void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*,int));
61int sqlite3PagerSetPagesize(Pager*, u16*);
62int sqlite3PagerMaxPageCount(Pager*, int);
63int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
64void sqlite3PagerSetCachesize(Pager*, int);
65int sqlite3PagerClose(Pager *pPager);
66int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
67#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
68DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
69int sqlite3PagerRef(DbPage*);
70int sqlite3PagerUnref(DbPage*);
71int sqlite3PagerWrite(DbPage*);
72int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void*);
73int sqlite3PagerPagecount(Pager*);
74int sqlite3PagerTruncate(Pager*,Pgno);
75int sqlite3PagerBegin(DbPage*, int exFlag);
76int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, Pgno);
77int sqlite3PagerCommitPhaseTwo(Pager*);
78int sqlite3PagerRollback(Pager*);
79int sqlite3PagerIsreadonly(Pager*);
80int sqlite3PagerStmtBegin(Pager*);
81int sqlite3PagerStmtCommit(Pager*);
82int sqlite3PagerStmtRollback(Pager*);
83void sqlite3PagerDontRollback(DbPage*);
84void sqlite3PagerDontWrite(DbPage*);
85int sqlite3PagerRefcount(Pager*);
86void sqlite3PagerSetSafetyLevel(Pager*,int,int);
87const char *sqlite3PagerFilename(Pager*);
88const sqlite3_vfs *sqlite3PagerVfs(Pager*);
89sqlite3_file *sqlite3PagerFile(Pager*);
90const char *sqlite3PagerDirname(Pager*);
91const char *sqlite3PagerJournalname(Pager*);
92int sqlite3PagerNosync(Pager*);
93int sqlite3PagerMovepage(Pager*,DbPage*,Pgno);
94void *sqlite3PagerGetData(DbPage *);
95void *sqlite3PagerGetExtra(DbPage *);
96int sqlite3PagerLockingMode(Pager *, int);
97
98#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
99 int sqlite3PagerReleaseMemory(int);
100#endif
101
102#ifdef SQLITE_HAS_CODEC
103 void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*);
104#endif
105
106#if !defined(NDEBUG) || defined(SQLITE_TEST)
107 Pgno sqlite3PagerPagenumber(DbPage*);
108 int sqlite3PagerIswriteable(DbPage*);
109#endif
110
111#ifdef SQLITE_TEST
112 int *sqlite3PagerStats(Pager*);
113 void sqlite3PagerRefdump(Pager*);
114 int pager3_refinfo_enable;
115#endif
116
117#ifdef SQLITE_TEST
118void disable_simulated_io_errors(void);
119void enable_simulated_io_errors(void);
120#else
121# define disable_simulated_io_errors()
122# define enable_simulated_io_errors()
123#endif
124
125#endif /* _PAGER_H_ */
diff --git a/libraries/sqlite/win32/parse.c b/libraries/sqlite/win32/parse.c
new file mode 100755
index 0000000..4fb4866
--- /dev/null
+++ b/libraries/sqlite/win32/parse.c
@@ -0,0 +1,3505 @@
1/* Driver template for the LEMON parser generator.
2** The author disclaims copyright to this source code.
3*/
4/* First off, code is include which follows the "include" declaration
5** in the input file. */
6#include <stdio.h>
7#line 56 "parse.y"
8
9#include "sqliteInt.h"
10
11/*
12** An instance of this structure holds information about the
13** LIMIT clause of a SELECT statement.
14*/
15struct LimitVal {
16 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
17 Expr *pOffset; /* The OFFSET expression. NULL if there is none */
18};
19
20/*
21** An instance of this structure is used to store the LIKE,
22** GLOB, NOT LIKE, and NOT GLOB operators.
23*/
24struct LikeOp {
25 Token eOperator; /* "like" or "glob" or "regexp" */
26 int not; /* True if the NOT keyword is present */
27};
28
29/*
30** An instance of the following structure describes the event of a
31** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
32** TK_DELETE, or TK_INSTEAD. If the event is of the form
33**
34** UPDATE ON (a,b,c)
35**
36** Then the "b" IdList records the list "a,b,c".
37*/
38struct TrigEvent { int a; IdList * b; };
39
40/*
41** An instance of this structure holds the ATTACH key and the key type.
42*/
43struct AttachKey { int type; Token key; };
44
45#line 47 "parse.c"
46/* Next is all token values, in a form suitable for use by makeheaders.
47** This section will be null unless lemon is run with the -m switch.
48*/
49/*
50** These constants (all generated automatically by the parser generator)
51** specify the various kinds of tokens (terminals) that the parser
52** understands.
53**
54** Each symbol here is a terminal symbol in the grammar.
55*/
56/* Make sure the INTERFACE macro is defined.
57*/
58#ifndef INTERFACE
59# define INTERFACE 1
60#endif
61/* The next thing included is series of defines which control
62** various aspects of the generated parser.
63** YYCODETYPE is the data type used for storing terminal
64** and nonterminal numbers. "unsigned char" is
65** used if there are fewer than 250 terminals
66** and nonterminals. "int" is used otherwise.
67** YYNOCODE is a number of type YYCODETYPE which corresponds
68** to no legal terminal or nonterminal number. This
69** number is used to fill in empty slots of the hash
70** table.
71** YYFALLBACK If defined, this indicates that one or more tokens
72** have fall-back values which should be used if the
73** original value of the token will not parse.
74** YYACTIONTYPE is the data type used for storing terminal
75** and nonterminal numbers. "unsigned char" is
76** used if there are fewer than 250 rules and
77** states combined. "int" is used otherwise.
78** sqlite3ParserTOKENTYPE is the data type used for minor tokens given
79** directly to the parser from the tokenizer.
80** YYMINORTYPE is the data type used for all minor tokens.
81** This is typically a union of many types, one of
82** which is sqlite3ParserTOKENTYPE. The entry in the union
83** for base tokens is called "yy0".
84** YYSTACKDEPTH is the maximum depth of the parser's stack. If
85** zero the stack is dynamically sized using realloc()
86** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
87** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
88** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
89** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
90** YYNSTATE the combined number of states.
91** YYNRULE the number of rules in the grammar
92** YYERRORSYMBOL is the code number of the error symbol. If not
93** defined, then do no error processing.
94*/
95#define YYCODETYPE unsigned char
96#define YYNOCODE 248
97#define YYACTIONTYPE unsigned short int
98#define YYWILDCARD 59
99#define sqlite3ParserTOKENTYPE Token
100typedef union {
101 sqlite3ParserTOKENTYPE yy0;
102 int yy46;
103 struct LikeOp yy72;
104 Expr* yy172;
105 ExprList* yy174;
106 Select* yy219;
107 struct LimitVal yy234;
108 TriggerStep* yy243;
109 struct TrigEvent yy370;
110 SrcList* yy373;
111 Expr * yy386;
112 struct {int value; int mask;} yy405;
113 Token yy410;
114 IdList* yy432;
115 int yy495;
116} YYMINORTYPE;
117#ifndef YYSTACKDEPTH
118#define YYSTACKDEPTH 100
119#endif
120#define sqlite3ParserARG_SDECL Parse *pParse;
121#define sqlite3ParserARG_PDECL ,Parse *pParse
122#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
123#define sqlite3ParserARG_STORE yypParser->pParse = pParse
124#define YYNSTATE 586
125#define YYNRULE 311
126#define YYERRORSYMBOL 138
127#define YYERRSYMDT yy495
128#define YYFALLBACK 1
129#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
130#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
131#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
132
133/* Next are that tables used to determine what action to take based on the
134** current state and lookahead token. These tables are used to implement
135** functions that take a state number and lookahead value and return an
136** action integer.
137**
138** Suppose the action integer is N. Then the action is determined as
139** follows
140**
141** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
142** token onto the stack and goto state N.
143**
144** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
145**
146** N == YYNSTATE+YYNRULE A syntax error has occurred.
147**
148** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
149**
150** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
151** slots in the yy_action[] table.
152**
153** The action table is constructed as a single large table named yy_action[].
154** Given state S and lookahead X, the action is computed as
155**
156** yy_action[ yy_shift_ofst[S] + X ]
157**
158** If the index value yy_shift_ofst[S]+X is out of range or if the value
159** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
160** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
161** and that yy_default[S] should be used instead.
162**
163** The formula above is for computing the action when the lookahead is
164** a terminal symbol. If the lookahead is a non-terminal (as occurs after
165** a reduce action) then the yy_reduce_ofst[] array is used in place of
166** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
167** YY_SHIFT_USE_DFLT.
168**
169** The following are the tables generated in this section:
170**
171** yy_action[] A single table containing all actions.
172** yy_lookahead[] A table containing the lookahead for each entry in
173** yy_action. Used to detect hash collisions.
174** yy_shift_ofst[] For each state, the offset into yy_action for
175** shifting terminals.
176** yy_reduce_ofst[] For each state, the offset into yy_action for
177** shifting non-terminals after a reduce.
178** yy_default[] Default action for each state.
179*/
180static const YYACTIONTYPE yy_action[] = {
181 /* 0 */ 290, 898, 122, 585, 407, 170, 2, 437, 61, 61,
182 /* 10 */ 61, 61, 517, 63, 63, 63, 63, 64, 64, 65,
183 /* 20 */ 65, 65, 66, 231, 445, 209, 422, 428, 68, 63,
184 /* 30 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 231,
185 /* 40 */ 389, 386, 394, 449, 60, 59, 295, 432, 433, 429,
186 /* 50 */ 429, 62, 62, 61, 61, 61, 61, 261, 63, 63,
187 /* 60 */ 63, 63, 64, 64, 65, 65, 65, 66, 231, 290,
188 /* 70 */ 491, 492, 437, 487, 206, 81, 67, 417, 69, 152,
189 /* 80 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66,
190 /* 90 */ 231, 67, 460, 69, 152, 422, 428, 571, 262, 58,
191 /* 100 */ 64, 64, 65, 65, 65, 66, 231, 395, 396, 419,
192 /* 110 */ 419, 419, 290, 60, 59, 295, 432, 433, 429, 429,
193 /* 120 */ 62, 62, 61, 61, 61, 61, 315, 63, 63, 63,
194 /* 130 */ 63, 64, 64, 65, 65, 65, 66, 231, 422, 428,
195 /* 140 */ 93, 65, 65, 65, 66, 231, 394, 231, 412, 34,
196 /* 150 */ 56, 296, 440, 441, 408, 486, 60, 59, 295, 432,
197 /* 160 */ 433, 429, 429, 62, 62, 61, 61, 61, 61, 488,
198 /* 170 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66,
199 /* 180 */ 231, 290, 255, 522, 293, 569, 112, 406, 520, 449,
200 /* 190 */ 329, 315, 405, 20, 437, 338, 517, 394, 530, 529,
201 /* 200 */ 503, 445, 209, 568, 567, 206, 528, 422, 428, 147,
202 /* 210 */ 148, 395, 396, 412, 41, 208, 149, 531, 370, 487,
203 /* 220 */ 259, 566, 257, 417, 290, 60, 59, 295, 432, 433,
204 /* 230 */ 429, 429, 62, 62, 61, 61, 61, 61, 315, 63,
205 /* 240 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 231,
206 /* 250 */ 422, 428, 445, 331, 212, 419, 419, 419, 361, 437,
207 /* 260 */ 412, 41, 395, 396, 364, 565, 208, 290, 60, 59,
208 /* 270 */ 295, 432, 433, 429, 429, 62, 62, 61, 61, 61,
209 /* 280 */ 61, 394, 63, 63, 63, 63, 64, 64, 65, 65,
210 /* 290 */ 65, 66, 231, 422, 428, 489, 298, 522, 472, 66,
211 /* 300 */ 231, 211, 472, 224, 409, 284, 532, 20, 447, 521,
212 /* 310 */ 166, 60, 59, 295, 432, 433, 429, 429, 62, 62,
213 /* 320 */ 61, 61, 61, 61, 472, 63, 63, 63, 63, 64,
214 /* 330 */ 64, 65, 65, 65, 66, 231, 207, 478, 315, 76,
215 /* 340 */ 290, 235, 298, 55, 482, 225, 395, 396, 179, 545,
216 /* 350 */ 492, 343, 346, 347, 67, 150, 69, 152, 337, 522,
217 /* 360 */ 412, 35, 348, 237, 249, 368, 422, 428, 576, 20,
218 /* 370 */ 162, 116, 239, 341, 244, 342, 174, 320, 440, 441,
219 /* 380 */ 412, 3, 79, 250, 60, 59, 295, 432, 433, 429,
220 /* 390 */ 429, 62, 62, 61, 61, 61, 61, 172, 63, 63,
221 /* 400 */ 63, 63, 64, 64, 65, 65, 65, 66, 231, 290,
222 /* 410 */ 249, 548, 232, 485, 508, 351, 315, 116, 239, 341,
223 /* 420 */ 244, 342, 174, 179, 315, 523, 343, 346, 347, 250,
224 /* 430 */ 218, 413, 153, 462, 509, 422, 428, 348, 412, 34,
225 /* 440 */ 463, 208, 175, 173, 158, 233, 412, 34, 336, 547,
226 /* 450 */ 447, 321, 166, 60, 59, 295, 432, 433, 429, 429,
227 /* 460 */ 62, 62, 61, 61, 61, 61, 413, 63, 63, 63,
228 /* 470 */ 63, 64, 64, 65, 65, 65, 66, 231, 290, 540,
229 /* 480 */ 333, 515, 502, 539, 454, 569, 300, 19, 329, 142,
230 /* 490 */ 315, 388, 315, 328, 2, 360, 455, 292, 481, 371,
231 /* 500 */ 267, 266, 250, 568, 422, 428, 586, 389, 386, 456,
232 /* 510 */ 206, 493, 412, 49, 412, 49, 301, 583, 889, 157,
233 /* 520 */ 889, 494, 60, 59, 295, 432, 433, 429, 429, 62,
234 /* 530 */ 62, 61, 61, 61, 61, 199, 63, 63, 63, 63,
235 /* 540 */ 64, 64, 65, 65, 65, 66, 231, 290, 315, 179,
236 /* 550 */ 436, 253, 343, 346, 347, 368, 151, 580, 306, 248,
237 /* 560 */ 307, 450, 75, 348, 77, 380, 208, 423, 424, 413,
238 /* 570 */ 412, 27, 317, 422, 428, 438, 1, 22, 583, 888,
239 /* 580 */ 394, 888, 542, 476, 318, 261, 435, 435, 426, 427,
240 /* 590 */ 413, 60, 59, 295, 432, 433, 429, 429, 62, 62,
241 /* 600 */ 61, 61, 61, 61, 326, 63, 63, 63, 63, 64,
242 /* 610 */ 64, 65, 65, 65, 66, 231, 290, 425, 580, 372,
243 /* 620 */ 219, 92, 515, 9, 334, 394, 555, 394, 454, 67,
244 /* 630 */ 394, 69, 152, 397, 398, 399, 318, 234, 435, 435,
245 /* 640 */ 455, 316, 422, 428, 297, 395, 396, 318, 430, 435,
246 /* 650 */ 435, 579, 289, 456, 220, 325, 5, 217, 544, 290,
247 /* 660 */ 60, 59, 295, 432, 433, 429, 429, 62, 62, 61,
248 /* 670 */ 61, 61, 61, 393, 63, 63, 63, 63, 64, 64,
249 /* 680 */ 65, 65, 65, 66, 231, 422, 428, 480, 311, 390,
250 /* 690 */ 395, 396, 395, 396, 205, 395, 396, 821, 271, 515,
251 /* 700 */ 248, 198, 290, 60, 59, 295, 432, 433, 429, 429,
252 /* 710 */ 62, 62, 61, 61, 61, 61, 468, 63, 63, 63,
253 /* 720 */ 63, 64, 64, 65, 65, 65, 66, 231, 422, 428,
254 /* 730 */ 169, 158, 261, 261, 302, 413, 274, 117, 272, 261,
255 /* 740 */ 515, 515, 261, 515, 190, 290, 60, 70, 295, 432,
256 /* 750 */ 433, 429, 429, 62, 62, 61, 61, 61, 61, 377,
257 /* 760 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66,
258 /* 770 */ 231, 422, 428, 382, 557, 303, 304, 248, 413, 318,
259 /* 780 */ 558, 435, 435, 559, 538, 358, 538, 385, 290, 194,
260 /* 790 */ 59, 295, 432, 433, 429, 429, 62, 62, 61, 61,
261 /* 800 */ 61, 61, 369, 63, 63, 63, 63, 64, 64, 65,
262 /* 810 */ 65, 65, 66, 231, 422, 428, 394, 273, 248, 248,
263 /* 820 */ 170, 246, 437, 413, 384, 365, 176, 177, 178, 467,
264 /* 830 */ 309, 121, 154, 126, 295, 432, 433, 429, 429, 62,
265 /* 840 */ 62, 61, 61, 61, 61, 315, 63, 63, 63, 63,
266 /* 850 */ 64, 64, 65, 65, 65, 66, 231, 72, 322, 175,
267 /* 860 */ 4, 315, 261, 315, 294, 261, 413, 412, 28, 315,
268 /* 870 */ 261, 315, 319, 72, 322, 315, 4, 418, 443, 443,
269 /* 880 */ 294, 395, 396, 412, 23, 412, 32, 437, 319, 324,
270 /* 890 */ 327, 412, 53, 412, 52, 315, 156, 412, 97, 449,
271 /* 900 */ 315, 192, 315, 275, 315, 324, 376, 469, 500, 315,
272 /* 910 */ 476, 277, 476, 163, 292, 449, 315, 412, 95, 74,
273 /* 920 */ 73, 467, 412, 100, 412, 101, 412, 111, 72, 313,
274 /* 930 */ 314, 412, 113, 417, 446, 74, 73, 479, 412, 16,
275 /* 940 */ 379, 315, 181, 465, 72, 313, 314, 72, 322, 417,
276 /* 950 */ 4, 206, 315, 184, 294, 315, 497, 498, 474, 206,
277 /* 960 */ 171, 339, 319, 412, 98, 419, 419, 419, 420, 421,
278 /* 970 */ 11, 359, 378, 305, 412, 33, 413, 412, 96, 324,
279 /* 980 */ 458, 419, 419, 419, 420, 421, 11, 413, 411, 449,
280 /* 990 */ 411, 160, 410, 315, 410, 466, 221, 222, 223, 103,
281 /* 1000 */ 83, 471, 315, 507, 506, 315, 620, 475, 315, 74,
282 /* 1010 */ 73, 245, 203, 21, 279, 412, 24, 437, 72, 313,
283 /* 1020 */ 314, 280, 315, 417, 412, 54, 505, 412, 114, 315,
284 /* 1030 */ 412, 115, 504, 201, 145, 547, 240, 510, 524, 200,
285 /* 1040 */ 315, 511, 202, 315, 412, 25, 315, 241, 315, 18,
286 /* 1050 */ 315, 412, 36, 315, 254, 419, 419, 419, 420, 421,
287 /* 1060 */ 11, 256, 412, 37, 258, 412, 26, 315, 412, 38,
288 /* 1070 */ 412, 39, 412, 40, 260, 412, 42, 315, 512, 315,
289 /* 1080 */ 126, 315, 437, 315, 187, 375, 276, 266, 265, 412,
290 /* 1090 */ 43, 291, 315, 252, 315, 126, 206, 581, 8, 412,
291 /* 1100 */ 44, 412, 29, 412, 30, 412, 45, 350, 363, 126,
292 /* 1110 */ 315, 543, 315, 126, 412, 46, 412, 47, 315, 355,
293 /* 1120 */ 381, 551, 315, 171, 552, 315, 90, 562, 578, 90,
294 /* 1130 */ 288, 366, 412, 48, 412, 31, 582, 367, 268, 269,
295 /* 1140 */ 412, 10, 270, 554, 412, 50, 564, 412, 51, 278,
296 /* 1150 */ 281, 282, 575, 144, 442, 403, 323, 226, 444, 461,
297 /* 1160 */ 464, 242, 503, 550, 561, 513, 161, 392, 400, 516,
298 /* 1170 */ 401, 345, 402, 7, 312, 83, 227, 332, 228, 82,
299 /* 1180 */ 330, 57, 408, 416, 168, 78, 459, 123, 210, 414,
300 /* 1190 */ 84, 335, 340, 299, 496, 500, 490, 229, 495, 243,
301 /* 1200 */ 104, 247, 499, 501, 230, 285, 415, 215, 514, 518,
302 /* 1210 */ 525, 526, 519, 236, 527, 473, 238, 352, 477, 286,
303 /* 1220 */ 182, 356, 533, 354, 119, 183, 185, 87, 546, 130,
304 /* 1230 */ 186, 535, 188, 140, 362, 191, 553, 216, 373, 374,
305 /* 1240 */ 131, 560, 308, 132, 133, 572, 577, 136, 263, 134,
306 /* 1250 */ 139, 536, 573, 391, 91, 94, 404, 574, 99, 214,
307 /* 1260 */ 102, 621, 622, 431, 164, 434, 165, 71, 141, 17,
308 /* 1270 */ 439, 448, 451, 155, 6, 452, 470, 110, 167, 453,
309 /* 1280 */ 457, 124, 13, 213, 120, 80, 12, 125, 159, 483,
310 /* 1290 */ 484, 85, 310, 105, 180, 251, 106, 118, 86, 107,
311 /* 1300 */ 241, 344, 349, 353, 143, 534, 127, 357, 171, 189,
312 /* 1310 */ 264, 108, 287, 128, 549, 129, 193, 537, 541, 14,
313 /* 1320 */ 195, 88, 196, 556, 197, 137, 138, 135, 15, 563,
314 /* 1330 */ 570, 109, 283, 146, 204, 383, 387, 899, 584, 899,
315 /* 1340 */ 899, 899, 899, 899, 89,
316};
317static const YYCODETYPE yy_lookahead[] = {
318 /* 0 */ 16, 139, 140, 141, 168, 21, 144, 23, 69, 70,
319 /* 10 */ 71, 72, 176, 74, 75, 76, 77, 78, 79, 80,
320 /* 20 */ 81, 82, 83, 84, 78, 79, 42, 43, 73, 74,
321 /* 30 */ 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
322 /* 40 */ 1, 2, 23, 58, 60, 61, 62, 63, 64, 65,
323 /* 50 */ 66, 67, 68, 69, 70, 71, 72, 147, 74, 75,
324 /* 60 */ 76, 77, 78, 79, 80, 81, 82, 83, 84, 16,
325 /* 70 */ 185, 186, 88, 88, 110, 22, 217, 92, 219, 220,
326 /* 80 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
327 /* 90 */ 84, 217, 218, 219, 220, 42, 43, 238, 188, 46,
328 /* 100 */ 78, 79, 80, 81, 82, 83, 84, 88, 89, 124,
329 /* 110 */ 125, 126, 16, 60, 61, 62, 63, 64, 65, 66,
330 /* 120 */ 67, 68, 69, 70, 71, 72, 147, 74, 75, 76,
331 /* 130 */ 77, 78, 79, 80, 81, 82, 83, 84, 42, 43,
332 /* 140 */ 44, 80, 81, 82, 83, 84, 23, 84, 169, 170,
333 /* 150 */ 19, 164, 165, 166, 23, 169, 60, 61, 62, 63,
334 /* 160 */ 64, 65, 66, 67, 68, 69, 70, 71, 72, 169,
335 /* 170 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
336 /* 180 */ 84, 16, 14, 147, 150, 147, 21, 167, 168, 58,
337 /* 190 */ 211, 147, 156, 157, 23, 216, 176, 23, 181, 176,
338 /* 200 */ 177, 78, 79, 165, 166, 110, 183, 42, 43, 78,
339 /* 210 */ 79, 88, 89, 169, 170, 228, 180, 181, 123, 88,
340 /* 220 */ 52, 98, 54, 92, 16, 60, 61, 62, 63, 64,
341 /* 230 */ 65, 66, 67, 68, 69, 70, 71, 72, 147, 74,
342 /* 240 */ 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
343 /* 250 */ 42, 43, 78, 209, 210, 124, 125, 126, 224, 88,
344 /* 260 */ 169, 170, 88, 89, 230, 227, 228, 16, 60, 61,
345 /* 270 */ 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
346 /* 280 */ 72, 23, 74, 75, 76, 77, 78, 79, 80, 81,
347 /* 290 */ 82, 83, 84, 42, 43, 160, 16, 147, 161, 83,
348 /* 300 */ 84, 210, 161, 153, 169, 158, 156, 157, 161, 162,
349 /* 310 */ 163, 60, 61, 62, 63, 64, 65, 66, 67, 68,
350 /* 320 */ 69, 70, 71, 72, 161, 74, 75, 76, 77, 78,
351 /* 330 */ 79, 80, 81, 82, 83, 84, 192, 200, 147, 131,
352 /* 340 */ 16, 200, 16, 199, 20, 190, 88, 89, 90, 185,
353 /* 350 */ 186, 93, 94, 95, 217, 22, 219, 220, 147, 147,
354 /* 360 */ 169, 170, 104, 200, 84, 147, 42, 43, 156, 157,
355 /* 370 */ 90, 91, 92, 93, 94, 95, 96, 164, 165, 166,
356 /* 380 */ 169, 170, 131, 103, 60, 61, 62, 63, 64, 65,
357 /* 390 */ 66, 67, 68, 69, 70, 71, 72, 155, 74, 75,
358 /* 400 */ 76, 77, 78, 79, 80, 81, 82, 83, 84, 16,
359 /* 410 */ 84, 11, 221, 20, 30, 16, 147, 91, 92, 93,
360 /* 420 */ 94, 95, 96, 90, 147, 181, 93, 94, 95, 103,
361 /* 430 */ 212, 189, 155, 27, 50, 42, 43, 104, 169, 170,
362 /* 440 */ 34, 228, 43, 201, 202, 147, 169, 170, 206, 49,
363 /* 450 */ 161, 162, 163, 60, 61, 62, 63, 64, 65, 66,
364 /* 460 */ 67, 68, 69, 70, 71, 72, 189, 74, 75, 76,
365 /* 470 */ 77, 78, 79, 80, 81, 82, 83, 84, 16, 25,
366 /* 480 */ 211, 147, 20, 29, 12, 147, 102, 19, 211, 21,
367 /* 490 */ 147, 141, 147, 216, 144, 41, 24, 98, 20, 99,
368 /* 500 */ 100, 101, 103, 165, 42, 43, 0, 1, 2, 37,
369 /* 510 */ 110, 39, 169, 170, 169, 170, 182, 19, 20, 147,
370 /* 520 */ 22, 49, 60, 61, 62, 63, 64, 65, 66, 67,
371 /* 530 */ 68, 69, 70, 71, 72, 155, 74, 75, 76, 77,
372 /* 540 */ 78, 79, 80, 81, 82, 83, 84, 16, 147, 90,
373 /* 550 */ 20, 20, 93, 94, 95, 147, 155, 59, 215, 225,
374 /* 560 */ 215, 20, 130, 104, 132, 227, 228, 42, 43, 189,
375 /* 570 */ 169, 170, 16, 42, 43, 20, 19, 22, 19, 20,
376 /* 580 */ 23, 22, 18, 147, 106, 147, 108, 109, 63, 64,
377 /* 590 */ 189, 60, 61, 62, 63, 64, 65, 66, 67, 68,
378 /* 600 */ 69, 70, 71, 72, 186, 74, 75, 76, 77, 78,
379 /* 610 */ 79, 80, 81, 82, 83, 84, 16, 92, 59, 55,
380 /* 620 */ 212, 21, 147, 19, 147, 23, 188, 23, 12, 217,
381 /* 630 */ 23, 219, 220, 7, 8, 9, 106, 147, 108, 109,
382 /* 640 */ 24, 147, 42, 43, 208, 88, 89, 106, 92, 108,
383 /* 650 */ 109, 244, 245, 37, 145, 39, 191, 182, 94, 16,
384 /* 660 */ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
385 /* 670 */ 70, 71, 72, 147, 74, 75, 76, 77, 78, 79,
386 /* 680 */ 80, 81, 82, 83, 84, 42, 43, 80, 142, 143,
387 /* 690 */ 88, 89, 88, 89, 148, 88, 89, 133, 14, 147,
388 /* 700 */ 225, 155, 16, 60, 61, 62, 63, 64, 65, 66,
389 /* 710 */ 67, 68, 69, 70, 71, 72, 114, 74, 75, 76,
390 /* 720 */ 77, 78, 79, 80, 81, 82, 83, 84, 42, 43,
391 /* 730 */ 201, 202, 147, 147, 182, 189, 52, 147, 54, 147,
392 /* 740 */ 147, 147, 147, 147, 155, 16, 60, 61, 62, 63,
393 /* 750 */ 64, 65, 66, 67, 68, 69, 70, 71, 72, 213,
394 /* 760 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
395 /* 770 */ 84, 42, 43, 188, 188, 182, 182, 225, 189, 106,
396 /* 780 */ 188, 108, 109, 188, 99, 100, 101, 241, 16, 155,
397 /* 790 */ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
398 /* 800 */ 71, 72, 213, 74, 75, 76, 77, 78, 79, 80,
399 /* 810 */ 81, 82, 83, 84, 42, 43, 23, 133, 225, 225,
400 /* 820 */ 21, 225, 23, 189, 239, 236, 99, 100, 101, 22,
401 /* 830 */ 242, 243, 155, 22, 62, 63, 64, 65, 66, 67,
402 /* 840 */ 68, 69, 70, 71, 72, 147, 74, 75, 76, 77,
403 /* 850 */ 78, 79, 80, 81, 82, 83, 84, 16, 17, 43,
404 /* 860 */ 19, 147, 147, 147, 23, 147, 189, 169, 170, 147,
405 /* 870 */ 147, 147, 31, 16, 17, 147, 19, 147, 124, 125,
406 /* 880 */ 23, 88, 89, 169, 170, 169, 170, 88, 31, 48,
407 /* 890 */ 147, 169, 170, 169, 170, 147, 89, 169, 170, 58,
408 /* 900 */ 147, 22, 147, 188, 147, 48, 188, 114, 97, 147,
409 /* 910 */ 147, 188, 147, 19, 98, 58, 147, 169, 170, 78,
410 /* 920 */ 79, 114, 169, 170, 169, 170, 169, 170, 87, 88,
411 /* 930 */ 89, 169, 170, 92, 161, 78, 79, 80, 169, 170,
412 /* 940 */ 91, 147, 155, 22, 87, 88, 89, 16, 17, 92,
413 /* 950 */ 19, 110, 147, 155, 23, 147, 7, 8, 20, 110,
414 /* 960 */ 22, 80, 31, 169, 170, 124, 125, 126, 127, 128,
415 /* 970 */ 129, 208, 123, 208, 169, 170, 189, 169, 170, 48,
416 /* 980 */ 147, 124, 125, 126, 127, 128, 129, 189, 107, 58,
417 /* 990 */ 107, 5, 111, 147, 111, 203, 10, 11, 12, 13,
418 /* 1000 */ 121, 147, 147, 91, 92, 147, 112, 147, 147, 78,
419 /* 1010 */ 79, 147, 26, 19, 28, 169, 170, 23, 87, 88,
420 /* 1020 */ 89, 35, 147, 92, 169, 170, 178, 169, 170, 147,
421 /* 1030 */ 169, 170, 147, 47, 113, 49, 92, 178, 147, 53,
422 /* 1040 */ 147, 178, 56, 147, 169, 170, 147, 103, 147, 19,
423 /* 1050 */ 147, 169, 170, 147, 147, 124, 125, 126, 127, 128,
424 /* 1060 */ 129, 147, 169, 170, 147, 169, 170, 147, 169, 170,
425 /* 1070 */ 169, 170, 169, 170, 147, 169, 170, 147, 20, 147,
426 /* 1080 */ 22, 147, 88, 147, 232, 99, 100, 101, 147, 169,
427 /* 1090 */ 170, 105, 147, 20, 147, 22, 110, 20, 68, 169,
428 /* 1100 */ 170, 169, 170, 169, 170, 169, 170, 20, 147, 22,
429 /* 1110 */ 147, 20, 147, 22, 169, 170, 169, 170, 147, 233,
430 /* 1120 */ 134, 20, 147, 22, 20, 147, 22, 20, 20, 22,
431 /* 1130 */ 22, 147, 169, 170, 169, 170, 59, 147, 147, 147,
432 /* 1140 */ 169, 170, 147, 147, 169, 170, 147, 169, 170, 147,
433 /* 1150 */ 147, 147, 147, 191, 229, 149, 223, 193, 229, 172,
434 /* 1160 */ 172, 172, 177, 194, 194, 172, 6, 146, 146, 172,
435 /* 1170 */ 146, 173, 146, 22, 154, 121, 194, 118, 195, 119,
436 /* 1180 */ 116, 120, 23, 160, 112, 130, 152, 152, 222, 189,
437 /* 1190 */ 98, 115, 98, 40, 179, 97, 171, 196, 171, 171,
438 /* 1200 */ 19, 84, 173, 171, 197, 174, 198, 226, 160, 160,
439 /* 1210 */ 171, 171, 179, 204, 171, 205, 204, 15, 205, 174,
440 /* 1220 */ 151, 38, 152, 152, 60, 151, 151, 130, 184, 19,
441 /* 1230 */ 152, 152, 151, 214, 152, 184, 194, 226, 152, 15,
442 /* 1240 */ 187, 194, 152, 187, 187, 33, 137, 184, 234, 187,
443 /* 1250 */ 214, 235, 152, 1, 237, 237, 20, 152, 159, 175,
444 /* 1260 */ 175, 112, 112, 92, 112, 107, 112, 19, 19, 231,
445 /* 1270 */ 20, 20, 11, 19, 117, 20, 114, 240, 22, 20,
446 /* 1280 */ 20, 19, 117, 44, 243, 22, 22, 20, 112, 20,
447 /* 1290 */ 20, 19, 246, 19, 96, 20, 19, 32, 19, 19,
448 /* 1300 */ 103, 44, 44, 16, 21, 17, 98, 36, 22, 98,
449 /* 1310 */ 133, 19, 5, 45, 1, 102, 122, 51, 45, 19,
450 /* 1320 */ 113, 68, 14, 17, 115, 102, 122, 113, 19, 123,
451 /* 1330 */ 20, 14, 136, 19, 135, 57, 3, 247, 4, 247,
452 /* 1340 */ 247, 247, 247, 247, 68,
453};
454#define YY_SHIFT_USE_DFLT (-62)
455#define YY_SHIFT_MAX 387
456static const short yy_shift_ofst[] = {
457 /* 0 */ 39, 841, 986, -16, 841, 931, 931, 258, 123, -36,
458 /* 10 */ 96, 931, 931, 931, 931, 931, -45, 400, 174, 19,
459 /* 20 */ 171, -54, -54, 53, 165, 208, 251, 324, 393, 462,
460 /* 30 */ 531, 600, 643, 686, 643, 643, 643, 643, 643, 643,
461 /* 40 */ 643, 643, 643, 643, 643, 643, 643, 643, 643, 643,
462 /* 50 */ 643, 643, 729, 772, 772, 857, 931, 931, 931, 931,
463 /* 60 */ 931, 931, 931, 931, 931, 931, 931, 931, 931, 931,
464 /* 70 */ 931, 931, 931, 931, 931, 931, 931, 931, 931, 931,
465 /* 80 */ 931, 931, 931, 931, 931, 931, 931, 931, 931, 931,
466 /* 90 */ 931, 931, 931, 931, 931, -61, -61, 6, 6, 280,
467 /* 100 */ 22, 61, 399, 564, 19, 19, 19, 19, 19, 19,
468 /* 110 */ 19, 216, 171, 63, -62, -62, 131, 326, 472, 472,
469 /* 120 */ 498, 559, 506, 799, 19, 799, 19, 19, 19, 19,
470 /* 130 */ 19, 19, 19, 19, 19, 19, 19, 19, 19, 849,
471 /* 140 */ 95, -36, -36, -36, -62, -62, -62, -15, -15, 333,
472 /* 150 */ 459, 478, 557, 530, 541, 616, 602, 793, 604, 607,
473 /* 160 */ 626, 19, 19, 881, 19, 19, 994, 19, 19, 807,
474 /* 170 */ 19, 19, 673, 807, 19, 19, 384, 384, 384, 19,
475 /* 180 */ 19, 673, 19, 19, 673, 19, 454, 685, 19, 19,
476 /* 190 */ 673, 19, 19, 19, 673, 19, 19, 19, 673, 673,
477 /* 200 */ 19, 19, 19, 19, 19, 468, 883, 921, 754, 754,
478 /* 210 */ 432, 406, 406, 406, 816, 406, 406, 811, 879, 879,
479 /* 220 */ 1160, 1160, 1160, 1160, 1151, -36, 1054, 1059, 1060, 1064,
480 /* 230 */ 1061, 1159, 1055, 1072, 1072, 1092, 1076, 1092, 1076, 1094,
481 /* 240 */ 1094, 1153, 1094, 1098, 1094, 1181, 1117, 1159, 1117, 1159,
482 /* 250 */ 1153, 1094, 1094, 1094, 1181, 1202, 1072, 1202, 1072, 1202,
483 /* 260 */ 1072, 1072, 1183, 1097, 1202, 1072, 1164, 1164, 1210, 1054,
484 /* 270 */ 1072, 1224, 1224, 1224, 1224, 1054, 1164, 1210, 1072, 1212,
485 /* 280 */ 1212, 1072, 1072, 1109, -62, -62, -62, -62, -62, -62,
486 /* 290 */ 525, 684, 727, 168, 894, 556, 555, 938, 944, 949,
487 /* 300 */ 912, 1058, 1073, 1087, 1091, 1101, 1104, 1107, 1030, 1108,
488 /* 310 */ 1077, 1252, 1236, 1149, 1150, 1152, 1154, 1171, 1158, 1248,
489 /* 320 */ 1250, 1251, 1249, 1261, 1254, 1255, 1256, 1259, 1260, 1263,
490 /* 330 */ 1157, 1264, 1165, 1263, 1162, 1262, 1267, 1176, 1269, 1270,
491 /* 340 */ 1265, 1239, 1272, 1257, 1274, 1275, 1277, 1279, 1258, 1280,
492 /* 350 */ 1198, 1197, 1287, 1288, 1283, 1208, 1271, 1266, 1268, 1286,
493 /* 360 */ 1273, 1177, 1211, 1292, 1307, 1313, 1213, 1253, 1276, 1194,
494 /* 370 */ 1300, 1207, 1308, 1209, 1306, 1214, 1223, 1204, 1309, 1206,
495 /* 380 */ 1310, 1317, 1278, 1199, 1196, 1314, 1333, 1334,
496};
497#define YY_REDUCE_USE_DFLT (-165)
498#define YY_REDUCE_MAX 289
499static const short yy_reduce_ofst[] = {
500 /* 0 */ -138, 277, 546, 137, 401, -21, 44, 36, 38, 242,
501 /* 10 */ -141, 191, 91, 269, 343, 345, -126, 589, 338, 150,
502 /* 20 */ 147, -13, 213, 412, 412, 412, 412, 412, 412, 412,
503 /* 30 */ 412, 412, 412, 412, 412, 412, 412, 412, 412, 412,
504 /* 40 */ 412, 412, 412, 412, 412, 412, 412, 412, 412, 412,
505 /* 50 */ 412, 412, 412, 412, 412, 211, 698, 714, 716, 722,
506 /* 60 */ 724, 728, 748, 753, 755, 757, 762, 769, 794, 805,
507 /* 70 */ 808, 846, 855, 858, 861, 875, 882, 893, 896, 899,
508 /* 80 */ 901, 903, 906, 920, 930, 932, 934, 936, 945, 947,
509 /* 90 */ 963, 965, 971, 975, 978, 412, 412, 412, 412, 20,
510 /* 100 */ 412, 412, 23, 34, 334, 475, 552, 593, 594, 585,
511 /* 110 */ 212, 412, 289, 412, 412, 412, 135, -164, -115, 164,
512 /* 120 */ 407, 407, 350, 141, 436, 163, 596, -90, 763, 218,
513 /* 130 */ 765, 438, 586, 592, 595, 715, 718, 408, 723, 380,
514 /* 140 */ 634, 677, 787, 798, 144, 529, 588, -14, 0, 17,
515 /* 150 */ 244, 155, 298, 155, 155, 418, 372, 477, 490, 494,
516 /* 160 */ 509, 526, 590, 465, 494, 730, 773, 743, 833, 792,
517 /* 170 */ 854, 860, 155, 792, 864, 885, 848, 859, 863, 891,
518 /* 180 */ 907, 155, 914, 917, 155, 927, 852, 886, 941, 961,
519 /* 190 */ 155, 984, 990, 991, 155, 992, 995, 996, 155, 155,
520 /* 200 */ 999, 1002, 1003, 1004, 1005, 1006, 962, 964, 925, 929,
521 /* 210 */ 933, 987, 988, 989, 985, 993, 997, 998, 969, 970,
522 /* 220 */ 1021, 1022, 1024, 1026, 1020, 1000, 982, 983, 1001, 1007,
523 /* 230 */ 1008, 1023, 966, 1034, 1035, 1009, 1010, 1012, 1013, 1025,
524 /* 240 */ 1027, 1015, 1028, 1029, 1032, 1031, 981, 1048, 1011, 1049,
525 /* 250 */ 1033, 1039, 1040, 1043, 1045, 1069, 1070, 1074, 1071, 1075,
526 /* 260 */ 1078, 1079, 1014, 1016, 1081, 1082, 1044, 1051, 1019, 1042,
527 /* 270 */ 1086, 1053, 1056, 1057, 1062, 1047, 1063, 1036, 1090, 1017,
528 /* 280 */ 1018, 1100, 1105, 1037, 1099, 1084, 1085, 1038, 1041, 1046,
529};
530static const YYACTIONTYPE yy_default[] = {
531 /* 0 */ 592, 816, 897, 707, 897, 816, 897, 897, 843, 711,
532 /* 10 */ 872, 814, 897, 897, 897, 897, 789, 897, 843, 897,
533 /* 20 */ 623, 843, 843, 740, 897, 897, 897, 897, 897, 897,
534 /* 30 */ 897, 897, 741, 897, 818, 813, 809, 811, 810, 817,
535 /* 40 */ 742, 731, 738, 745, 723, 856, 747, 748, 754, 755,
536 /* 50 */ 873, 871, 777, 776, 795, 897, 897, 897, 897, 897,
537 /* 60 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
538 /* 70 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
539 /* 80 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
540 /* 90 */ 897, 897, 897, 897, 897, 779, 800, 778, 788, 616,
541 /* 100 */ 780, 781, 676, 611, 897, 897, 897, 897, 897, 897,
542 /* 110 */ 897, 782, 897, 783, 796, 797, 897, 897, 897, 897,
543 /* 120 */ 897, 897, 592, 707, 897, 707, 897, 897, 897, 897,
544 /* 130 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
545 /* 140 */ 897, 897, 897, 897, 701, 711, 890, 897, 897, 667,
546 /* 150 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
547 /* 160 */ 599, 597, 897, 699, 897, 897, 625, 897, 897, 709,
548 /* 170 */ 897, 897, 714, 715, 897, 897, 897, 897, 897, 897,
549 /* 180 */ 897, 613, 897, 897, 688, 897, 849, 897, 897, 897,
550 /* 190 */ 863, 897, 897, 897, 861, 897, 897, 897, 690, 750,
551 /* 200 */ 830, 897, 876, 878, 897, 897, 699, 708, 897, 897,
552 /* 210 */ 812, 734, 734, 734, 646, 734, 734, 649, 744, 744,
553 /* 220 */ 596, 596, 596, 596, 666, 897, 744, 735, 737, 727,
554 /* 230 */ 739, 897, 897, 716, 716, 724, 726, 724, 726, 678,
555 /* 240 */ 678, 663, 678, 649, 678, 822, 827, 897, 827, 897,
556 /* 250 */ 663, 678, 678, 678, 822, 608, 716, 608, 716, 608,
557 /* 260 */ 716, 716, 853, 855, 608, 716, 680, 680, 756, 744,
558 /* 270 */ 716, 687, 687, 687, 687, 744, 680, 756, 716, 875,
559 /* 280 */ 875, 716, 716, 883, 633, 651, 651, 858, 890, 895,
560 /* 290 */ 897, 897, 897, 897, 763, 897, 897, 897, 897, 897,
561 /* 300 */ 897, 897, 897, 897, 897, 897, 897, 897, 836, 897,
562 /* 310 */ 897, 897, 897, 768, 764, 897, 765, 897, 693, 897,
563 /* 320 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 815,
564 /* 330 */ 897, 728, 897, 736, 897, 897, 897, 897, 897, 897,
565 /* 340 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
566 /* 350 */ 897, 897, 897, 897, 897, 897, 897, 897, 851, 852,
567 /* 360 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
568 /* 370 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
569 /* 380 */ 897, 897, 882, 897, 897, 885, 593, 897, 587, 590,
570 /* 390 */ 589, 591, 595, 598, 620, 621, 622, 600, 601, 602,
571 /* 400 */ 603, 604, 605, 606, 612, 614, 632, 634, 618, 636,
572 /* 410 */ 697, 698, 760, 691, 692, 696, 771, 762, 766, 767,
573 /* 420 */ 769, 770, 784, 785, 787, 793, 799, 802, 786, 791,
574 /* 430 */ 792, 794, 798, 801, 694, 695, 805, 619, 626, 627,
575 /* 440 */ 630, 631, 839, 841, 840, 842, 629, 628, 772, 775,
576 /* 450 */ 807, 808, 864, 865, 866, 867, 868, 803, 717, 806,
577 /* 460 */ 790, 729, 732, 733, 730, 700, 710, 719, 720, 721,
578 /* 470 */ 722, 705, 706, 712, 725, 758, 759, 713, 702, 703,
579 /* 480 */ 704, 804, 761, 773, 774, 637, 638, 768, 639, 640,
580 /* 490 */ 641, 679, 682, 683, 684, 642, 661, 664, 665, 643,
581 /* 500 */ 650, 644, 645, 652, 653, 654, 657, 658, 659, 660,
582 /* 510 */ 655, 656, 823, 824, 828, 826, 825, 647, 648, 662,
583 /* 520 */ 635, 624, 617, 668, 671, 672, 673, 674, 675, 677,
584 /* 530 */ 669, 670, 615, 607, 609, 718, 845, 854, 850, 846,
585 /* 540 */ 847, 848, 610, 819, 820, 681, 752, 753, 844, 857,
586 /* 550 */ 859, 757, 860, 862, 887, 685, 686, 689, 829, 869,
587 /* 560 */ 743, 746, 749, 751, 831, 832, 833, 834, 837, 838,
588 /* 570 */ 835, 870, 874, 877, 879, 880, 881, 884, 886, 891,
589 /* 580 */ 892, 893, 896, 894, 594, 588,
590};
591#define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
592
593/* The next table maps tokens into fallback tokens. If a construct
594** like the following:
595**
596** %fallback ID X Y Z.
597**
598** appears in the grammer, then ID becomes a fallback token for X, Y,
599** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
600** but it does not parse, the type of the token is changed to ID and
601** the parse is retried before an error is thrown.
602*/
603#ifdef YYFALLBACK
604static const YYCODETYPE yyFallback[] = {
605 0, /* $ => nothing */
606 0, /* SEMI => nothing */
607 23, /* EXPLAIN => ID */
608 23, /* QUERY => ID */
609 23, /* PLAN => ID */
610 23, /* BEGIN => ID */
611 0, /* TRANSACTION => nothing */
612 23, /* DEFERRED => ID */
613 23, /* IMMEDIATE => ID */
614 23, /* EXCLUSIVE => ID */
615 0, /* COMMIT => nothing */
616 23, /* END => ID */
617 0, /* ROLLBACK => nothing */
618 0, /* CREATE => nothing */
619 0, /* TABLE => nothing */
620 23, /* IF => ID */
621 0, /* NOT => nothing */
622 0, /* EXISTS => nothing */
623 23, /* TEMP => ID */
624 0, /* LP => nothing */
625 0, /* RP => nothing */
626 0, /* AS => nothing */
627 0, /* COMMA => nothing */
628 0, /* ID => nothing */
629 23, /* ABORT => ID */
630 23, /* AFTER => ID */
631 23, /* ANALYZE => ID */
632 23, /* ASC => ID */
633 23, /* ATTACH => ID */
634 23, /* BEFORE => ID */
635 23, /* CASCADE => ID */
636 23, /* CAST => ID */
637 23, /* CONFLICT => ID */
638 23, /* DATABASE => ID */
639 23, /* DESC => ID */
640 23, /* DETACH => ID */
641 23, /* EACH => ID */
642 23, /* FAIL => ID */
643 23, /* FOR => ID */
644 23, /* IGNORE => ID */
645 23, /* INITIALLY => ID */
646 23, /* INSTEAD => ID */
647 23, /* LIKE_KW => ID */
648 23, /* MATCH => ID */
649 23, /* KEY => ID */
650 23, /* OF => ID */
651 23, /* OFFSET => ID */
652 23, /* PRAGMA => ID */
653 23, /* RAISE => ID */
654 23, /* REPLACE => ID */
655 23, /* RESTRICT => ID */
656 23, /* ROW => ID */
657 23, /* TRIGGER => ID */
658 23, /* VACUUM => ID */
659 23, /* VIEW => ID */
660 23, /* VIRTUAL => ID */
661 23, /* REINDEX => ID */
662 23, /* RENAME => ID */
663 23, /* CTIME_KW => ID */
664 0, /* ANY => nothing */
665 0, /* OR => nothing */
666 0, /* AND => nothing */
667 0, /* IS => nothing */
668 0, /* BETWEEN => nothing */
669 0, /* IN => nothing */
670 0, /* ISNULL => nothing */
671 0, /* NOTNULL => nothing */
672 0, /* NE => nothing */
673 0, /* EQ => nothing */
674 0, /* GT => nothing */
675 0, /* LE => nothing */
676 0, /* LT => nothing */
677 0, /* GE => nothing */
678 0, /* ESCAPE => nothing */
679 0, /* BITAND => nothing */
680 0, /* BITOR => nothing */
681 0, /* LSHIFT => nothing */
682 0, /* RSHIFT => nothing */
683 0, /* PLUS => nothing */
684 0, /* MINUS => nothing */
685 0, /* STAR => nothing */
686 0, /* SLASH => nothing */
687 0, /* REM => nothing */
688 0, /* CONCAT => nothing */
689 0, /* COLLATE => nothing */
690 0, /* UMINUS => nothing */
691 0, /* UPLUS => nothing */
692 0, /* BITNOT => nothing */
693 0, /* STRING => nothing */
694 0, /* JOIN_KW => nothing */
695 0, /* CONSTRAINT => nothing */
696 0, /* DEFAULT => nothing */
697 0, /* NULL => nothing */
698 0, /* PRIMARY => nothing */
699 0, /* UNIQUE => nothing */
700 0, /* CHECK => nothing */
701 0, /* REFERENCES => nothing */
702 0, /* AUTOINCR => nothing */
703 0, /* ON => nothing */
704 0, /* DELETE => nothing */
705 0, /* UPDATE => nothing */
706 0, /* INSERT => nothing */
707 0, /* SET => nothing */
708 0, /* DEFERRABLE => nothing */
709 0, /* FOREIGN => nothing */
710 0, /* DROP => nothing */
711 0, /* UNION => nothing */
712 0, /* ALL => nothing */
713 0, /* EXCEPT => nothing */
714 0, /* INTERSECT => nothing */
715 0, /* SELECT => nothing */
716 0, /* DISTINCT => nothing */
717 0, /* DOT => nothing */
718 0, /* FROM => nothing */
719 0, /* JOIN => nothing */
720 0, /* USING => nothing */
721 0, /* ORDER => nothing */
722 0, /* BY => nothing */
723 0, /* GROUP => nothing */
724 0, /* HAVING => nothing */
725 0, /* LIMIT => nothing */
726 0, /* WHERE => nothing */
727 0, /* INTO => nothing */
728 0, /* VALUES => nothing */
729 0, /* INTEGER => nothing */
730 0, /* FLOAT => nothing */
731 0, /* BLOB => nothing */
732 0, /* REGISTER => nothing */
733 0, /* VARIABLE => nothing */
734 0, /* CASE => nothing */
735 0, /* WHEN => nothing */
736 0, /* THEN => nothing */
737 0, /* ELSE => nothing */
738 0, /* INDEX => nothing */
739 0, /* ALTER => nothing */
740 0, /* TO => nothing */
741 0, /* ADD => nothing */
742 0, /* COLUMNKW => nothing */
743};
744#endif /* YYFALLBACK */
745
746/* The following structure represents a single element of the
747** parser's stack. Information stored includes:
748**
749** + The state number for the parser at this level of the stack.
750**
751** + The value of the token stored at this level of the stack.
752** (In other words, the "major" token.)
753**
754** + The semantic value stored at this level of the stack. This is
755** the information used by the action routines in the grammar.
756** It is sometimes called the "minor" token.
757*/
758struct yyStackEntry {
759 int stateno; /* The state-number */
760 int major; /* The major token value. This is the code
761 ** number for the token at this stack level */
762 YYMINORTYPE minor; /* The user-supplied minor token value. This
763 ** is the value of the token */
764};
765typedef struct yyStackEntry yyStackEntry;
766
767/* The state of the parser is completely contained in an instance of
768** the following structure */
769struct yyParser {
770 int yyidx; /* Index of top element in stack */
771 int yyerrcnt; /* Shifts left before out of the error */
772 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
773#if YYSTACKDEPTH<=0
774 int yystksz; /* Current side of the stack */
775 yyStackEntry *yystack; /* The parser's stack */
776#else
777 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
778#endif
779};
780typedef struct yyParser yyParser;
781
782#ifndef NDEBUG
783#include <stdio.h>
784static FILE *yyTraceFILE = 0;
785static char *yyTracePrompt = 0;
786#endif /* NDEBUG */
787
788#ifndef NDEBUG
789/*
790** Turn parser tracing on by giving a stream to which to write the trace
791** and a prompt to preface each trace message. Tracing is turned off
792** by making either argument NULL
793**
794** Inputs:
795** <ul>
796** <li> A FILE* to which trace output should be written.
797** If NULL, then tracing is turned off.
798** <li> A prefix string written at the beginning of every
799** line of trace output. If NULL, then tracing is
800** turned off.
801** </ul>
802**
803** Outputs:
804** None.
805*/
806void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
807 yyTraceFILE = TraceFILE;
808 yyTracePrompt = zTracePrompt;
809 if( yyTraceFILE==0 ) yyTracePrompt = 0;
810 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
811}
812#endif /* NDEBUG */
813
814#ifndef NDEBUG
815/* For tracing shifts, the names of all terminals and nonterminals
816** are required. The following table supplies these names */
817static const char *const yyTokenName[] = {
818 "$", "SEMI", "EXPLAIN", "QUERY",
819 "PLAN", "BEGIN", "TRANSACTION", "DEFERRED",
820 "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END",
821 "ROLLBACK", "CREATE", "TABLE", "IF",
822 "NOT", "EXISTS", "TEMP", "LP",
823 "RP", "AS", "COMMA", "ID",
824 "ABORT", "AFTER", "ANALYZE", "ASC",
825 "ATTACH", "BEFORE", "CASCADE", "CAST",
826 "CONFLICT", "DATABASE", "DESC", "DETACH",
827 "EACH", "FAIL", "FOR", "IGNORE",
828 "INITIALLY", "INSTEAD", "LIKE_KW", "MATCH",
829 "KEY", "OF", "OFFSET", "PRAGMA",
830 "RAISE", "REPLACE", "RESTRICT", "ROW",
831 "TRIGGER", "VACUUM", "VIEW", "VIRTUAL",
832 "REINDEX", "RENAME", "CTIME_KW", "ANY",
833 "OR", "AND", "IS", "BETWEEN",
834 "IN", "ISNULL", "NOTNULL", "NE",
835 "EQ", "GT", "LE", "LT",
836 "GE", "ESCAPE", "BITAND", "BITOR",
837 "LSHIFT", "RSHIFT", "PLUS", "MINUS",
838 "STAR", "SLASH", "REM", "CONCAT",
839 "COLLATE", "UMINUS", "UPLUS", "BITNOT",
840 "STRING", "JOIN_KW", "CONSTRAINT", "DEFAULT",
841 "NULL", "PRIMARY", "UNIQUE", "CHECK",
842 "REFERENCES", "AUTOINCR", "ON", "DELETE",
843 "UPDATE", "INSERT", "SET", "DEFERRABLE",
844 "FOREIGN", "DROP", "UNION", "ALL",
845 "EXCEPT", "INTERSECT", "SELECT", "DISTINCT",
846 "DOT", "FROM", "JOIN", "USING",
847 "ORDER", "BY", "GROUP", "HAVING",
848 "LIMIT", "WHERE", "INTO", "VALUES",
849 "INTEGER", "FLOAT", "BLOB", "REGISTER",
850 "VARIABLE", "CASE", "WHEN", "THEN",
851 "ELSE", "INDEX", "ALTER", "TO",
852 "ADD", "COLUMNKW", "error", "input",
853 "cmdlist", "ecmd", "cmdx", "cmd",
854 "explain", "transtype", "trans_opt", "nm",
855 "create_table", "create_table_args", "temp", "ifnotexists",
856 "dbnm", "columnlist", "conslist_opt", "select",
857 "column", "columnid", "type", "carglist",
858 "id", "ids", "typetoken", "typename",
859 "signed", "plus_num", "minus_num", "carg",
860 "ccons", "term", "expr", "onconf",
861 "sortorder", "autoinc", "idxlist_opt", "refargs",
862 "defer_subclause", "refarg", "refact", "init_deferred_pred_opt",
863 "conslist", "tcons", "idxlist", "defer_subclause_opt",
864 "orconf", "resolvetype", "raisetype", "ifexists",
865 "fullname", "oneselect", "multiselect_op", "distinct",
866 "selcollist", "from", "where_opt", "groupby_opt",
867 "having_opt", "orderby_opt", "limit_opt", "sclp",
868 "as", "seltablist", "stl_prefix", "joinop",
869 "on_opt", "using_opt", "seltablist_paren", "joinop2",
870 "inscollist", "sortlist", "sortitem", "nexprlist",
871 "setlist", "insert_cmd", "inscollist_opt", "itemlist",
872 "exprlist", "likeop", "escape", "between_op",
873 "in_op", "case_operand", "case_exprlist", "case_else",
874 "uniqueflag", "idxitem", "collate", "nmnum",
875 "plus_opt", "number", "trigger_decl", "trigger_cmd_list",
876 "trigger_time", "trigger_event", "foreach_clause", "when_clause",
877 "trigger_cmd", "database_kw_opt", "key_opt", "add_column_fullname",
878 "kwcolumn_opt", "create_vtab", "vtabarglist", "vtabarg",
879 "vtabargtoken", "lp", "anylist",
880};
881#endif /* NDEBUG */
882
883#ifndef NDEBUG
884/* For tracing reduce actions, the names of all rules are required.
885*/
886static const char *const yyRuleName[] = {
887 /* 0 */ "input ::= cmdlist",
888 /* 1 */ "cmdlist ::= cmdlist ecmd",
889 /* 2 */ "cmdlist ::= ecmd",
890 /* 3 */ "cmdx ::= cmd",
891 /* 4 */ "ecmd ::= SEMI",
892 /* 5 */ "ecmd ::= explain cmdx SEMI",
893 /* 6 */ "explain ::=",
894 /* 7 */ "explain ::= EXPLAIN",
895 /* 8 */ "explain ::= EXPLAIN QUERY PLAN",
896 /* 9 */ "cmd ::= BEGIN transtype trans_opt",
897 /* 10 */ "trans_opt ::=",
898 /* 11 */ "trans_opt ::= TRANSACTION",
899 /* 12 */ "trans_opt ::= TRANSACTION nm",
900 /* 13 */ "transtype ::=",
901 /* 14 */ "transtype ::= DEFERRED",
902 /* 15 */ "transtype ::= IMMEDIATE",
903 /* 16 */ "transtype ::= EXCLUSIVE",
904 /* 17 */ "cmd ::= COMMIT trans_opt",
905 /* 18 */ "cmd ::= END trans_opt",
906 /* 19 */ "cmd ::= ROLLBACK trans_opt",
907 /* 20 */ "cmd ::= create_table create_table_args",
908 /* 21 */ "create_table ::= CREATE temp TABLE ifnotexists nm dbnm",
909 /* 22 */ "ifnotexists ::=",
910 /* 23 */ "ifnotexists ::= IF NOT EXISTS",
911 /* 24 */ "temp ::= TEMP",
912 /* 25 */ "temp ::=",
913 /* 26 */ "create_table_args ::= LP columnlist conslist_opt RP",
914 /* 27 */ "create_table_args ::= AS select",
915 /* 28 */ "columnlist ::= columnlist COMMA column",
916 /* 29 */ "columnlist ::= column",
917 /* 30 */ "column ::= columnid type carglist",
918 /* 31 */ "columnid ::= nm",
919 /* 32 */ "id ::= ID",
920 /* 33 */ "ids ::= ID|STRING",
921 /* 34 */ "nm ::= ID",
922 /* 35 */ "nm ::= STRING",
923 /* 36 */ "nm ::= JOIN_KW",
924 /* 37 */ "type ::=",
925 /* 38 */ "type ::= typetoken",
926 /* 39 */ "typetoken ::= typename",
927 /* 40 */ "typetoken ::= typename LP signed RP",
928 /* 41 */ "typetoken ::= typename LP signed COMMA signed RP",
929 /* 42 */ "typename ::= ids",
930 /* 43 */ "typename ::= typename ids",
931 /* 44 */ "signed ::= plus_num",
932 /* 45 */ "signed ::= minus_num",
933 /* 46 */ "carglist ::= carglist carg",
934 /* 47 */ "carglist ::=",
935 /* 48 */ "carg ::= CONSTRAINT nm ccons",
936 /* 49 */ "carg ::= ccons",
937 /* 50 */ "ccons ::= DEFAULT term",
938 /* 51 */ "ccons ::= DEFAULT LP expr RP",
939 /* 52 */ "ccons ::= DEFAULT PLUS term",
940 /* 53 */ "ccons ::= DEFAULT MINUS term",
941 /* 54 */ "ccons ::= DEFAULT id",
942 /* 55 */ "ccons ::= NULL onconf",
943 /* 56 */ "ccons ::= NOT NULL onconf",
944 /* 57 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
945 /* 58 */ "ccons ::= UNIQUE onconf",
946 /* 59 */ "ccons ::= CHECK LP expr RP",
947 /* 60 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
948 /* 61 */ "ccons ::= defer_subclause",
949 /* 62 */ "ccons ::= COLLATE id",
950 /* 63 */ "autoinc ::=",
951 /* 64 */ "autoinc ::= AUTOINCR",
952 /* 65 */ "refargs ::=",
953 /* 66 */ "refargs ::= refargs refarg",
954 /* 67 */ "refarg ::= MATCH nm",
955 /* 68 */ "refarg ::= ON DELETE refact",
956 /* 69 */ "refarg ::= ON UPDATE refact",
957 /* 70 */ "refarg ::= ON INSERT refact",
958 /* 71 */ "refact ::= SET NULL",
959 /* 72 */ "refact ::= SET DEFAULT",
960 /* 73 */ "refact ::= CASCADE",
961 /* 74 */ "refact ::= RESTRICT",
962 /* 75 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
963 /* 76 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
964 /* 77 */ "init_deferred_pred_opt ::=",
965 /* 78 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
966 /* 79 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
967 /* 80 */ "conslist_opt ::=",
968 /* 81 */ "conslist_opt ::= COMMA conslist",
969 /* 82 */ "conslist ::= conslist COMMA tcons",
970 /* 83 */ "conslist ::= conslist tcons",
971 /* 84 */ "conslist ::= tcons",
972 /* 85 */ "tcons ::= CONSTRAINT nm",
973 /* 86 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
974 /* 87 */ "tcons ::= UNIQUE LP idxlist RP onconf",
975 /* 88 */ "tcons ::= CHECK LP expr RP onconf",
976 /* 89 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
977 /* 90 */ "defer_subclause_opt ::=",
978 /* 91 */ "defer_subclause_opt ::= defer_subclause",
979 /* 92 */ "onconf ::=",
980 /* 93 */ "onconf ::= ON CONFLICT resolvetype",
981 /* 94 */ "orconf ::=",
982 /* 95 */ "orconf ::= OR resolvetype",
983 /* 96 */ "resolvetype ::= raisetype",
984 /* 97 */ "resolvetype ::= IGNORE",
985 /* 98 */ "resolvetype ::= REPLACE",
986 /* 99 */ "cmd ::= DROP TABLE ifexists fullname",
987 /* 100 */ "ifexists ::= IF EXISTS",
988 /* 101 */ "ifexists ::=",
989 /* 102 */ "cmd ::= CREATE temp VIEW ifnotexists nm dbnm AS select",
990 /* 103 */ "cmd ::= DROP VIEW ifexists fullname",
991 /* 104 */ "cmd ::= select",
992 /* 105 */ "select ::= oneselect",
993 /* 106 */ "select ::= select multiselect_op oneselect",
994 /* 107 */ "multiselect_op ::= UNION",
995 /* 108 */ "multiselect_op ::= UNION ALL",
996 /* 109 */ "multiselect_op ::= EXCEPT|INTERSECT",
997 /* 110 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
998 /* 111 */ "distinct ::= DISTINCT",
999 /* 112 */ "distinct ::= ALL",
1000 /* 113 */ "distinct ::=",
1001 /* 114 */ "sclp ::= selcollist COMMA",
1002 /* 115 */ "sclp ::=",
1003 /* 116 */ "selcollist ::= sclp expr as",
1004 /* 117 */ "selcollist ::= sclp STAR",
1005 /* 118 */ "selcollist ::= sclp nm DOT STAR",
1006 /* 119 */ "as ::= AS nm",
1007 /* 120 */ "as ::= ids",
1008 /* 121 */ "as ::=",
1009 /* 122 */ "from ::=",
1010 /* 123 */ "from ::= FROM seltablist",
1011 /* 124 */ "stl_prefix ::= seltablist joinop",
1012 /* 125 */ "stl_prefix ::=",
1013 /* 126 */ "seltablist ::= stl_prefix nm dbnm as on_opt using_opt",
1014 /* 127 */ "seltablist ::= stl_prefix LP seltablist_paren RP as on_opt using_opt",
1015 /* 128 */ "seltablist_paren ::= select",
1016 /* 129 */ "seltablist_paren ::= seltablist",
1017 /* 130 */ "dbnm ::=",
1018 /* 131 */ "dbnm ::= DOT nm",
1019 /* 132 */ "fullname ::= nm dbnm",
1020 /* 133 */ "joinop ::= COMMA|JOIN",
1021 /* 134 */ "joinop ::= JOIN_KW JOIN",
1022 /* 135 */ "joinop ::= JOIN_KW nm JOIN",
1023 /* 136 */ "joinop ::= JOIN_KW nm nm JOIN",
1024 /* 137 */ "on_opt ::= ON expr",
1025 /* 138 */ "on_opt ::=",
1026 /* 139 */ "using_opt ::= USING LP inscollist RP",
1027 /* 140 */ "using_opt ::=",
1028 /* 141 */ "orderby_opt ::=",
1029 /* 142 */ "orderby_opt ::= ORDER BY sortlist",
1030 /* 143 */ "sortlist ::= sortlist COMMA sortitem sortorder",
1031 /* 144 */ "sortlist ::= sortitem sortorder",
1032 /* 145 */ "sortitem ::= expr",
1033 /* 146 */ "sortorder ::= ASC",
1034 /* 147 */ "sortorder ::= DESC",
1035 /* 148 */ "sortorder ::=",
1036 /* 149 */ "groupby_opt ::=",
1037 /* 150 */ "groupby_opt ::= GROUP BY nexprlist",
1038 /* 151 */ "having_opt ::=",
1039 /* 152 */ "having_opt ::= HAVING expr",
1040 /* 153 */ "limit_opt ::=",
1041 /* 154 */ "limit_opt ::= LIMIT expr",
1042 /* 155 */ "limit_opt ::= LIMIT expr OFFSET expr",
1043 /* 156 */ "limit_opt ::= LIMIT expr COMMA expr",
1044 /* 157 */ "cmd ::= DELETE FROM fullname where_opt",
1045 /* 158 */ "where_opt ::=",
1046 /* 159 */ "where_opt ::= WHERE expr",
1047 /* 160 */ "cmd ::= UPDATE orconf fullname SET setlist where_opt",
1048 /* 161 */ "setlist ::= setlist COMMA nm EQ expr",
1049 /* 162 */ "setlist ::= nm EQ expr",
1050 /* 163 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
1051 /* 164 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
1052 /* 165 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
1053 /* 166 */ "insert_cmd ::= INSERT orconf",
1054 /* 167 */ "insert_cmd ::= REPLACE",
1055 /* 168 */ "itemlist ::= itemlist COMMA expr",
1056 /* 169 */ "itemlist ::= expr",
1057 /* 170 */ "inscollist_opt ::=",
1058 /* 171 */ "inscollist_opt ::= LP inscollist RP",
1059 /* 172 */ "inscollist ::= inscollist COMMA nm",
1060 /* 173 */ "inscollist ::= nm",
1061 /* 174 */ "expr ::= term",
1062 /* 175 */ "expr ::= LP expr RP",
1063 /* 176 */ "term ::= NULL",
1064 /* 177 */ "expr ::= ID",
1065 /* 178 */ "expr ::= JOIN_KW",
1066 /* 179 */ "expr ::= nm DOT nm",
1067 /* 180 */ "expr ::= nm DOT nm DOT nm",
1068 /* 181 */ "term ::= INTEGER|FLOAT|BLOB",
1069 /* 182 */ "term ::= STRING",
1070 /* 183 */ "expr ::= REGISTER",
1071 /* 184 */ "expr ::= VARIABLE",
1072 /* 185 */ "expr ::= expr COLLATE id",
1073 /* 186 */ "expr ::= CAST LP expr AS typetoken RP",
1074 /* 187 */ "expr ::= ID LP distinct exprlist RP",
1075 /* 188 */ "expr ::= ID LP STAR RP",
1076 /* 189 */ "term ::= CTIME_KW",
1077 /* 190 */ "expr ::= expr AND expr",
1078 /* 191 */ "expr ::= expr OR expr",
1079 /* 192 */ "expr ::= expr LT|GT|GE|LE expr",
1080 /* 193 */ "expr ::= expr EQ|NE expr",
1081 /* 194 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
1082 /* 195 */ "expr ::= expr PLUS|MINUS expr",
1083 /* 196 */ "expr ::= expr STAR|SLASH|REM expr",
1084 /* 197 */ "expr ::= expr CONCAT expr",
1085 /* 198 */ "likeop ::= LIKE_KW",
1086 /* 199 */ "likeop ::= NOT LIKE_KW",
1087 /* 200 */ "likeop ::= MATCH",
1088 /* 201 */ "likeop ::= NOT MATCH",
1089 /* 202 */ "escape ::= ESCAPE expr",
1090 /* 203 */ "escape ::=",
1091 /* 204 */ "expr ::= expr likeop expr escape",
1092 /* 205 */ "expr ::= expr ISNULL|NOTNULL",
1093 /* 206 */ "expr ::= expr IS NULL",
1094 /* 207 */ "expr ::= expr NOT NULL",
1095 /* 208 */ "expr ::= expr IS NOT NULL",
1096 /* 209 */ "expr ::= NOT|BITNOT expr",
1097 /* 210 */ "expr ::= MINUS expr",
1098 /* 211 */ "expr ::= PLUS expr",
1099 /* 212 */ "between_op ::= BETWEEN",
1100 /* 213 */ "between_op ::= NOT BETWEEN",
1101 /* 214 */ "expr ::= expr between_op expr AND expr",
1102 /* 215 */ "in_op ::= IN",
1103 /* 216 */ "in_op ::= NOT IN",
1104 /* 217 */ "expr ::= expr in_op LP exprlist RP",
1105 /* 218 */ "expr ::= LP select RP",
1106 /* 219 */ "expr ::= expr in_op LP select RP",
1107 /* 220 */ "expr ::= expr in_op nm dbnm",
1108 /* 221 */ "expr ::= EXISTS LP select RP",
1109 /* 222 */ "expr ::= CASE case_operand case_exprlist case_else END",
1110 /* 223 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
1111 /* 224 */ "case_exprlist ::= WHEN expr THEN expr",
1112 /* 225 */ "case_else ::= ELSE expr",
1113 /* 226 */ "case_else ::=",
1114 /* 227 */ "case_operand ::= expr",
1115 /* 228 */ "case_operand ::=",
1116 /* 229 */ "exprlist ::= nexprlist",
1117 /* 230 */ "exprlist ::=",
1118 /* 231 */ "nexprlist ::= nexprlist COMMA expr",
1119 /* 232 */ "nexprlist ::= expr",
1120 /* 233 */ "cmd ::= CREATE uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
1121 /* 234 */ "uniqueflag ::= UNIQUE",
1122 /* 235 */ "uniqueflag ::=",
1123 /* 236 */ "idxlist_opt ::=",
1124 /* 237 */ "idxlist_opt ::= LP idxlist RP",
1125 /* 238 */ "idxlist ::= idxlist COMMA idxitem collate sortorder",
1126 /* 239 */ "idxlist ::= idxitem collate sortorder",
1127 /* 240 */ "idxitem ::= nm",
1128 /* 241 */ "collate ::=",
1129 /* 242 */ "collate ::= COLLATE id",
1130 /* 243 */ "cmd ::= DROP INDEX ifexists fullname",
1131 /* 244 */ "cmd ::= VACUUM",
1132 /* 245 */ "cmd ::= VACUUM nm",
1133 /* 246 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
1134 /* 247 */ "cmd ::= PRAGMA nm dbnm EQ ON",
1135 /* 248 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
1136 /* 249 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
1137 /* 250 */ "cmd ::= PRAGMA nm dbnm",
1138 /* 251 */ "nmnum ::= plus_num",
1139 /* 252 */ "nmnum ::= nm",
1140 /* 253 */ "plus_num ::= plus_opt number",
1141 /* 254 */ "minus_num ::= MINUS number",
1142 /* 255 */ "number ::= INTEGER|FLOAT",
1143 /* 256 */ "plus_opt ::= PLUS",
1144 /* 257 */ "plus_opt ::=",
1145 /* 258 */ "cmd ::= CREATE trigger_decl BEGIN trigger_cmd_list END",
1146 /* 259 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
1147 /* 260 */ "trigger_time ::= BEFORE",
1148 /* 261 */ "trigger_time ::= AFTER",
1149 /* 262 */ "trigger_time ::= INSTEAD OF",
1150 /* 263 */ "trigger_time ::=",
1151 /* 264 */ "trigger_event ::= DELETE|INSERT",
1152 /* 265 */ "trigger_event ::= UPDATE",
1153 /* 266 */ "trigger_event ::= UPDATE OF inscollist",
1154 /* 267 */ "foreach_clause ::=",
1155 /* 268 */ "foreach_clause ::= FOR EACH ROW",
1156 /* 269 */ "when_clause ::=",
1157 /* 270 */ "when_clause ::= WHEN expr",
1158 /* 271 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
1159 /* 272 */ "trigger_cmd_list ::=",
1160 /* 273 */ "trigger_cmd ::= UPDATE orconf nm SET setlist where_opt",
1161 /* 274 */ "trigger_cmd ::= insert_cmd INTO nm inscollist_opt VALUES LP itemlist RP",
1162 /* 275 */ "trigger_cmd ::= insert_cmd INTO nm inscollist_opt select",
1163 /* 276 */ "trigger_cmd ::= DELETE FROM nm where_opt",
1164 /* 277 */ "trigger_cmd ::= select",
1165 /* 278 */ "expr ::= RAISE LP IGNORE RP",
1166 /* 279 */ "expr ::= RAISE LP raisetype COMMA nm RP",
1167 /* 280 */ "raisetype ::= ROLLBACK",
1168 /* 281 */ "raisetype ::= ABORT",
1169 /* 282 */ "raisetype ::= FAIL",
1170 /* 283 */ "cmd ::= DROP TRIGGER ifexists fullname",
1171 /* 284 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
1172 /* 285 */ "cmd ::= DETACH database_kw_opt expr",
1173 /* 286 */ "key_opt ::=",
1174 /* 287 */ "key_opt ::= KEY expr",
1175 /* 288 */ "database_kw_opt ::= DATABASE",
1176 /* 289 */ "database_kw_opt ::=",
1177 /* 290 */ "cmd ::= REINDEX",
1178 /* 291 */ "cmd ::= REINDEX nm dbnm",
1179 /* 292 */ "cmd ::= ANALYZE",
1180 /* 293 */ "cmd ::= ANALYZE nm dbnm",
1181 /* 294 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
1182 /* 295 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
1183 /* 296 */ "add_column_fullname ::= fullname",
1184 /* 297 */ "kwcolumn_opt ::=",
1185 /* 298 */ "kwcolumn_opt ::= COLUMNKW",
1186 /* 299 */ "cmd ::= create_vtab",
1187 /* 300 */ "cmd ::= create_vtab LP vtabarglist RP",
1188 /* 301 */ "create_vtab ::= CREATE VIRTUAL TABLE nm dbnm USING nm",
1189 /* 302 */ "vtabarglist ::= vtabarg",
1190 /* 303 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
1191 /* 304 */ "vtabarg ::=",
1192 /* 305 */ "vtabarg ::= vtabarg vtabargtoken",
1193 /* 306 */ "vtabargtoken ::= ANY",
1194 /* 307 */ "vtabargtoken ::= lp anylist RP",
1195 /* 308 */ "lp ::= LP",
1196 /* 309 */ "anylist ::=",
1197 /* 310 */ "anylist ::= anylist ANY",
1198};
1199#endif /* NDEBUG */
1200
1201
1202#if YYSTACKDEPTH<=0
1203/*
1204** Try to increase the size of the parser stack.
1205*/
1206static void yyGrowStack(yyParser *p){
1207 int newSize;
1208 yyStackEntry *pNew;
1209
1210 newSize = p->yystksz*2 + 100;
1211 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
1212 if( pNew ){
1213 p->yystack = pNew;
1214 p->yystksz = newSize;
1215#ifndef NDEBUG
1216 if( yyTraceFILE ){
1217 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
1218 yyTracePrompt, p->yystksz);
1219 }
1220#endif
1221 }
1222}
1223#endif
1224
1225/*
1226** This function allocates a new parser.
1227** The only argument is a pointer to a function which works like
1228** malloc.
1229**
1230** Inputs:
1231** A pointer to the function used to allocate memory.
1232**
1233** Outputs:
1234** A pointer to a parser. This pointer is used in subsequent calls
1235** to sqlite3Parser and sqlite3ParserFree.
1236*/
1237void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
1238 yyParser *pParser;
1239 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
1240 if( pParser ){
1241 pParser->yyidx = -1;
1242#if YYSTACKDEPTH<=0
1243 yyGrowStack(pParser);
1244#endif
1245 }
1246 return pParser;
1247}
1248
1249/* The following function deletes the value associated with a
1250** symbol. The symbol can be either a terminal or nonterminal.
1251** "yymajor" is the symbol code, and "yypminor" is a pointer to
1252** the value.
1253*/
1254static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
1255 switch( yymajor ){
1256 /* Here is inserted the actions which take place when a
1257 ** terminal or non-terminal is destroyed. This can happen
1258 ** when the symbol is popped from the stack during a
1259 ** reduce or during error processing or when a parser is
1260 ** being destroyed before it is finished parsing.
1261 **
1262 ** Note: during a reduce, the only symbols destroyed are those
1263 ** which appear on the RHS of the rule, but which are not used
1264 ** inside the C code.
1265 */
1266 case 155:
1267 case 189:
1268 case 206:
1269#line 373 "parse.y"
1270{sqlite3SelectDelete((yypminor->yy219));}
1271#line 1273 "parse.c"
1272 break;
1273 case 169:
1274 case 170:
1275 case 194:
1276 case 196:
1277 case 204:
1278 case 210:
1279 case 218:
1280 case 221:
1281 case 223:
1282 case 235:
1283#line 633 "parse.y"
1284{sqlite3ExprDelete((yypminor->yy172));}
1285#line 1287 "parse.c"
1286 break;
1287 case 174:
1288 case 182:
1289 case 192:
1290 case 195:
1291 case 197:
1292 case 199:
1293 case 209:
1294 case 211:
1295 case 212:
1296 case 215:
1297 case 216:
1298 case 222:
1299#line 887 "parse.y"
1300{sqlite3ExprListDelete((yypminor->yy174));}
1301#line 1303 "parse.c"
1302 break;
1303 case 188:
1304 case 193:
1305 case 201:
1306 case 202:
1307#line 490 "parse.y"
1308{sqlite3SrcListDelete((yypminor->yy373));}
1309#line 1311 "parse.c"
1310 break;
1311 case 205:
1312 case 208:
1313 case 214:
1314#line 507 "parse.y"
1315{sqlite3IdListDelete((yypminor->yy432));}
1316#line 1318 "parse.c"
1317 break;
1318 case 231:
1319 case 236:
1320#line 990 "parse.y"
1321{sqlite3DeleteTriggerStep((yypminor->yy243));}
1322#line 1324 "parse.c"
1323 break;
1324 case 233:
1325#line 976 "parse.y"
1326{sqlite3IdListDelete((yypminor->yy370).b);}
1327#line 1329 "parse.c"
1328 break;
1329 case 238:
1330#line 1063 "parse.y"
1331{sqlite3ExprDelete((yypminor->yy386));}
1332#line 1334 "parse.c"
1333 break;
1334 default: break; /* If no destructor action specified: do nothing */
1335 }
1336}
1337
1338/*
1339** Pop the parser's stack once.
1340**
1341** If there is a destructor routine associated with the token which
1342** is popped from the stack, then call it.
1343**
1344** Return the major token number for the symbol popped.
1345*/
1346static int yy_pop_parser_stack(yyParser *pParser){
1347 YYCODETYPE yymajor;
1348 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
1349
1350 if( pParser->yyidx<0 ) return 0;
1351#ifndef NDEBUG
1352 if( yyTraceFILE && pParser->yyidx>=0 ){
1353 fprintf(yyTraceFILE,"%sPopping %s\n",
1354 yyTracePrompt,
1355 yyTokenName[yytos->major]);
1356 }
1357#endif
1358 yymajor = yytos->major;
1359 yy_destructor( yymajor, &yytos->minor);
1360 pParser->yyidx--;
1361 return yymajor;
1362}
1363
1364/*
1365** Deallocate and destroy a parser. Destructors are all called for
1366** all stack elements before shutting the parser down.
1367**
1368** Inputs:
1369** <ul>
1370** <li> A pointer to the parser. This should be a pointer
1371** obtained from sqlite3ParserAlloc.
1372** <li> A pointer to a function used to reclaim memory obtained
1373** from malloc.
1374** </ul>
1375*/
1376void sqlite3ParserFree(
1377 void *p, /* The parser to be deleted */
1378 void (*freeProc)(void*) /* Function used to reclaim memory */
1379){
1380 yyParser *pParser = (yyParser*)p;
1381 if( pParser==0 ) return;
1382 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
1383#if YYSTACKDEPTH<=0
1384 free(pParser->yystack);
1385#endif
1386 (*freeProc)((void*)pParser);
1387}
1388
1389/*
1390** Find the appropriate action for a parser given the terminal
1391** look-ahead token iLookAhead.
1392**
1393** If the look-ahead token is YYNOCODE, then check to see if the action is
1394** independent of the look-ahead. If it is, return the action, otherwise
1395** return YY_NO_ACTION.
1396*/
1397static int yy_find_shift_action(
1398 yyParser *pParser, /* The parser */
1399 YYCODETYPE iLookAhead /* The look-ahead token */
1400){
1401 int i;
1402 int stateno = pParser->yystack[pParser->yyidx].stateno;
1403
1404 if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
1405 return yy_default[stateno];
1406 }
1407 if( iLookAhead==YYNOCODE ){
1408 return YY_NO_ACTION;
1409 }
1410 i += iLookAhead;
1411 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
1412 if( iLookAhead>0 ){
1413#ifdef YYFALLBACK
1414 int iFallback; /* Fallback token */
1415 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
1416 && (iFallback = yyFallback[iLookAhead])!=0 ){
1417#ifndef NDEBUG
1418 if( yyTraceFILE ){
1419 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
1420 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
1421 }
1422#endif
1423 return yy_find_shift_action(pParser, iFallback);
1424 }
1425#endif
1426#ifdef YYWILDCARD
1427 {
1428 int j = i - iLookAhead + YYWILDCARD;
1429 if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
1430#ifndef NDEBUG
1431 if( yyTraceFILE ){
1432 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
1433 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
1434 }
1435#endif /* NDEBUG */
1436 return yy_action[j];
1437 }
1438 }
1439#endif /* YYWILDCARD */
1440 }
1441 return yy_default[stateno];
1442 }else{
1443 return yy_action[i];
1444 }
1445}
1446
1447/*
1448** Find the appropriate action for a parser given the non-terminal
1449** look-ahead token iLookAhead.
1450**
1451** If the look-ahead token is YYNOCODE, then check to see if the action is
1452** independent of the look-ahead. If it is, return the action, otherwise
1453** return YY_NO_ACTION.
1454*/
1455static int yy_find_reduce_action(
1456 int stateno, /* Current state number */
1457 YYCODETYPE iLookAhead /* The look-ahead token */
1458){
1459 int i;
1460 /* int stateno = pParser->yystack[pParser->yyidx].stateno; */
1461
1462 if( stateno>YY_REDUCE_MAX ||
1463 (i = yy_reduce_ofst[stateno])==YY_REDUCE_USE_DFLT ){
1464 return yy_default[stateno];
1465 }
1466 if( iLookAhead==YYNOCODE ){
1467 return YY_NO_ACTION;
1468 }
1469 i += iLookAhead;
1470 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
1471 return yy_default[stateno];
1472 }else{
1473 return yy_action[i];
1474 }
1475}
1476
1477/*
1478** The following routine is called if the stack overflows.
1479*/
1480static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
1481 sqlite3ParserARG_FETCH;
1482 yypParser->yyidx--;
1483#ifndef NDEBUG
1484 if( yyTraceFILE ){
1485 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
1486 }
1487#endif
1488 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
1489 /* Here code is inserted which will execute if the parser
1490 ** stack every overflows */
1491#line 44 "parse.y"
1492
1493 sqlite3ErrorMsg(pParse, "parser stack overflow");
1494 pParse->parseError = 1;
1495#line 1498 "parse.c"
1496 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
1497}
1498
1499/*
1500** Perform a shift action.
1501*/
1502static void yy_shift(
1503 yyParser *yypParser, /* The parser to be shifted */
1504 int yyNewState, /* The new state to shift in */
1505 int yyMajor, /* The major token to shift in */
1506 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
1507){
1508 yyStackEntry *yytos;
1509 yypParser->yyidx++;
1510#if YYSTACKDEPTH>0
1511 if( yypParser->yyidx>=YYSTACKDEPTH ){
1512 yyStackOverflow(yypParser, yypMinor);
1513 return;
1514 }
1515#else
1516 if( yypParser->yyidx>=yypParser->yystksz ){
1517 yyGrowStack(yypParser);
1518 if( yypParser->yyidx>=yypParser->yystksz ){
1519 yyStackOverflow(yypParser, yypMinor);
1520 return;
1521 }
1522 }
1523#endif
1524 yytos = &yypParser->yystack[yypParser->yyidx];
1525 yytos->stateno = yyNewState;
1526 yytos->major = yyMajor;
1527 yytos->minor = *yypMinor;
1528#ifndef NDEBUG
1529 if( yyTraceFILE && yypParser->yyidx>0 ){
1530 int i;
1531 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
1532 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
1533 for(i=1; i<=yypParser->yyidx; i++)
1534 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
1535 fprintf(yyTraceFILE,"\n");
1536 }
1537#endif
1538}
1539
1540/* The following table contains information about every rule that
1541** is used during the reduce.
1542*/
1543static const struct {
1544 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
1545 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
1546} yyRuleInfo[] = {
1547 { 139, 1 },
1548 { 140, 2 },
1549 { 140, 1 },
1550 { 142, 1 },
1551 { 141, 1 },
1552 { 141, 3 },
1553 { 144, 0 },
1554 { 144, 1 },
1555 { 144, 3 },
1556 { 143, 3 },
1557 { 146, 0 },
1558 { 146, 1 },
1559 { 146, 2 },
1560 { 145, 0 },
1561 { 145, 1 },
1562 { 145, 1 },
1563 { 145, 1 },
1564 { 143, 2 },
1565 { 143, 2 },
1566 { 143, 2 },
1567 { 143, 2 },
1568 { 148, 6 },
1569 { 151, 0 },
1570 { 151, 3 },
1571 { 150, 1 },
1572 { 150, 0 },
1573 { 149, 4 },
1574 { 149, 2 },
1575 { 153, 3 },
1576 { 153, 1 },
1577 { 156, 3 },
1578 { 157, 1 },
1579 { 160, 1 },
1580 { 161, 1 },
1581 { 147, 1 },
1582 { 147, 1 },
1583 { 147, 1 },
1584 { 158, 0 },
1585 { 158, 1 },
1586 { 162, 1 },
1587 { 162, 4 },
1588 { 162, 6 },
1589 { 163, 1 },
1590 { 163, 2 },
1591 { 164, 1 },
1592 { 164, 1 },
1593 { 159, 2 },
1594 { 159, 0 },
1595 { 167, 3 },
1596 { 167, 1 },
1597 { 168, 2 },
1598 { 168, 4 },
1599 { 168, 3 },
1600 { 168, 3 },
1601 { 168, 2 },
1602 { 168, 2 },
1603 { 168, 3 },
1604 { 168, 5 },
1605 { 168, 2 },
1606 { 168, 4 },
1607 { 168, 4 },
1608 { 168, 1 },
1609 { 168, 2 },
1610 { 173, 0 },
1611 { 173, 1 },
1612 { 175, 0 },
1613 { 175, 2 },
1614 { 177, 2 },
1615 { 177, 3 },
1616 { 177, 3 },
1617 { 177, 3 },
1618 { 178, 2 },
1619 { 178, 2 },
1620 { 178, 1 },
1621 { 178, 1 },
1622 { 176, 3 },
1623 { 176, 2 },
1624 { 179, 0 },
1625 { 179, 2 },
1626 { 179, 2 },
1627 { 154, 0 },
1628 { 154, 2 },
1629 { 180, 3 },
1630 { 180, 2 },
1631 { 180, 1 },
1632 { 181, 2 },
1633 { 181, 7 },
1634 { 181, 5 },
1635 { 181, 5 },
1636 { 181, 10 },
1637 { 183, 0 },
1638 { 183, 1 },
1639 { 171, 0 },
1640 { 171, 3 },
1641 { 184, 0 },
1642 { 184, 2 },
1643 { 185, 1 },
1644 { 185, 1 },
1645 { 185, 1 },
1646 { 143, 4 },
1647 { 187, 2 },
1648 { 187, 0 },
1649 { 143, 8 },
1650 { 143, 4 },
1651 { 143, 1 },
1652 { 155, 1 },
1653 { 155, 3 },
1654 { 190, 1 },
1655 { 190, 2 },
1656 { 190, 1 },
1657 { 189, 9 },
1658 { 191, 1 },
1659 { 191, 1 },
1660 { 191, 0 },
1661 { 199, 2 },
1662 { 199, 0 },
1663 { 192, 3 },
1664 { 192, 2 },
1665 { 192, 4 },
1666 { 200, 2 },
1667 { 200, 1 },
1668 { 200, 0 },
1669 { 193, 0 },
1670 { 193, 2 },
1671 { 202, 2 },
1672 { 202, 0 },
1673 { 201, 6 },
1674 { 201, 7 },
1675 { 206, 1 },
1676 { 206, 1 },
1677 { 152, 0 },
1678 { 152, 2 },
1679 { 188, 2 },
1680 { 203, 1 },
1681 { 203, 2 },
1682 { 203, 3 },
1683 { 203, 4 },
1684 { 204, 2 },
1685 { 204, 0 },
1686 { 205, 4 },
1687 { 205, 0 },
1688 { 197, 0 },
1689 { 197, 3 },
1690 { 209, 4 },
1691 { 209, 2 },
1692 { 210, 1 },
1693 { 172, 1 },
1694 { 172, 1 },
1695 { 172, 0 },
1696 { 195, 0 },
1697 { 195, 3 },
1698 { 196, 0 },
1699 { 196, 2 },
1700 { 198, 0 },
1701 { 198, 2 },
1702 { 198, 4 },
1703 { 198, 4 },
1704 { 143, 4 },
1705 { 194, 0 },
1706 { 194, 2 },
1707 { 143, 6 },
1708 { 212, 5 },
1709 { 212, 3 },
1710 { 143, 8 },
1711 { 143, 5 },
1712 { 143, 6 },
1713 { 213, 2 },
1714 { 213, 1 },
1715 { 215, 3 },
1716 { 215, 1 },
1717 { 214, 0 },
1718 { 214, 3 },
1719 { 208, 3 },
1720 { 208, 1 },
1721 { 170, 1 },
1722 { 170, 3 },
1723 { 169, 1 },
1724 { 170, 1 },
1725 { 170, 1 },
1726 { 170, 3 },
1727 { 170, 5 },
1728 { 169, 1 },
1729 { 169, 1 },
1730 { 170, 1 },
1731 { 170, 1 },
1732 { 170, 3 },
1733 { 170, 6 },
1734 { 170, 5 },
1735 { 170, 4 },
1736 { 169, 1 },
1737 { 170, 3 },
1738 { 170, 3 },
1739 { 170, 3 },
1740 { 170, 3 },
1741 { 170, 3 },
1742 { 170, 3 },
1743 { 170, 3 },
1744 { 170, 3 },
1745 { 217, 1 },
1746 { 217, 2 },
1747 { 217, 1 },
1748 { 217, 2 },
1749 { 218, 2 },
1750 { 218, 0 },
1751 { 170, 4 },
1752 { 170, 2 },
1753 { 170, 3 },
1754 { 170, 3 },
1755 { 170, 4 },
1756 { 170, 2 },
1757 { 170, 2 },
1758 { 170, 2 },
1759 { 219, 1 },
1760 { 219, 2 },
1761 { 170, 5 },
1762 { 220, 1 },
1763 { 220, 2 },
1764 { 170, 5 },
1765 { 170, 3 },
1766 { 170, 5 },
1767 { 170, 4 },
1768 { 170, 4 },
1769 { 170, 5 },
1770 { 222, 5 },
1771 { 222, 4 },
1772 { 223, 2 },
1773 { 223, 0 },
1774 { 221, 1 },
1775 { 221, 0 },
1776 { 216, 1 },
1777 { 216, 0 },
1778 { 211, 3 },
1779 { 211, 1 },
1780 { 143, 11 },
1781 { 224, 1 },
1782 { 224, 0 },
1783 { 174, 0 },
1784 { 174, 3 },
1785 { 182, 5 },
1786 { 182, 3 },
1787 { 225, 1 },
1788 { 226, 0 },
1789 { 226, 2 },
1790 { 143, 4 },
1791 { 143, 1 },
1792 { 143, 2 },
1793 { 143, 5 },
1794 { 143, 5 },
1795 { 143, 5 },
1796 { 143, 6 },
1797 { 143, 3 },
1798 { 227, 1 },
1799 { 227, 1 },
1800 { 165, 2 },
1801 { 166, 2 },
1802 { 229, 1 },
1803 { 228, 1 },
1804 { 228, 0 },
1805 { 143, 5 },
1806 { 230, 11 },
1807 { 232, 1 },
1808 { 232, 1 },
1809 { 232, 2 },
1810 { 232, 0 },
1811 { 233, 1 },
1812 { 233, 1 },
1813 { 233, 3 },
1814 { 234, 0 },
1815 { 234, 3 },
1816 { 235, 0 },
1817 { 235, 2 },
1818 { 231, 3 },
1819 { 231, 0 },
1820 { 236, 6 },
1821 { 236, 8 },
1822 { 236, 5 },
1823 { 236, 4 },
1824 { 236, 1 },
1825 { 170, 4 },
1826 { 170, 6 },
1827 { 186, 1 },
1828 { 186, 1 },
1829 { 186, 1 },
1830 { 143, 4 },
1831 { 143, 6 },
1832 { 143, 3 },
1833 { 238, 0 },
1834 { 238, 2 },
1835 { 237, 1 },
1836 { 237, 0 },
1837 { 143, 1 },
1838 { 143, 3 },
1839 { 143, 1 },
1840 { 143, 3 },
1841 { 143, 6 },
1842 { 143, 6 },
1843 { 239, 1 },
1844 { 240, 0 },
1845 { 240, 1 },
1846 { 143, 1 },
1847 { 143, 4 },
1848 { 241, 7 },
1849 { 242, 1 },
1850 { 242, 3 },
1851 { 243, 0 },
1852 { 243, 2 },
1853 { 244, 1 },
1854 { 244, 3 },
1855 { 245, 1 },
1856 { 246, 0 },
1857 { 246, 2 },
1858};
1859
1860static void yy_accept(yyParser*); /* Forward Declaration */
1861
1862/*
1863** Perform a reduce action and the shift that must immediately
1864** follow the reduce.
1865*/
1866static void yy_reduce(
1867 yyParser *yypParser, /* The parser */
1868 int yyruleno /* Number of the rule by which to reduce */
1869){
1870 int yygoto; /* The next state */
1871 int yyact; /* The next action */
1872 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
1873 yyStackEntry *yymsp; /* The top of the parser's stack */
1874 int yysize; /* Amount to pop the stack */
1875 sqlite3ParserARG_FETCH;
1876 yymsp = &yypParser->yystack[yypParser->yyidx];
1877#ifndef NDEBUG
1878 if( yyTraceFILE && yyruleno>=0
1879 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
1880 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
1881 yyRuleName[yyruleno]);
1882 }
1883#endif /* NDEBUG */
1884
1885 /* Silence complaints from purify about yygotominor being uninitialized
1886 ** in some cases when it is copied into the stack after the following
1887 ** switch. yygotominor is uninitialized when a rule reduces that does
1888 ** not set the value of its left-hand side nonterminal. Leaving the
1889 ** value of the nonterminal uninitialized is utterly harmless as long
1890 ** as the value is never used. So really the only thing this code
1891 ** accomplishes is to quieten purify.
1892 **
1893 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
1894 ** without this code, their parser segfaults. I'm not sure what there
1895 ** parser is doing to make this happen. This is the second bug report
1896 ** from wireshark this week. Clearly they are stressing Lemon in ways
1897 ** that it has not been previously stressed... (SQLite ticket #2172)
1898 */
1899 memset(&yygotominor, 0, sizeof(yygotominor));
1900
1901
1902 switch( yyruleno ){
1903 /* Beginning here are the reduction cases. A typical example
1904 ** follows:
1905 ** case 0:
1906 ** #line <lineno> <grammarfile>
1907 ** { ... } // User supplied code
1908 ** #line <lineno> <thisfile>
1909 ** break;
1910 */
1911 case 0:
1912 case 1:
1913 case 2:
1914 case 4:
1915 case 5:
1916 case 10:
1917 case 11:
1918 case 12:
1919 case 20:
1920 case 28:
1921 case 29:
1922 case 37:
1923 case 44:
1924 case 45:
1925 case 46:
1926 case 47:
1927 case 48:
1928 case 49:
1929 case 55:
1930 case 82:
1931 case 83:
1932 case 84:
1933 case 85:
1934 case 256:
1935 case 257:
1936 case 267:
1937 case 268:
1938 case 288:
1939 case 289:
1940 case 297:
1941 case 298:
1942 case 302:
1943 case 303:
1944 case 305:
1945 case 309:
1946#line 96 "parse.y"
1947{
1948}
1949#line 1952 "parse.c"
1950 break;
1951 case 3:
1952#line 99 "parse.y"
1953{ sqlite3FinishCoding(pParse); }
1954#line 1957 "parse.c"
1955 break;
1956 case 6:
1957#line 102 "parse.y"
1958{ sqlite3BeginParse(pParse, 0); }
1959#line 1962 "parse.c"
1960 break;
1961 case 7:
1962#line 104 "parse.y"
1963{ sqlite3BeginParse(pParse, 1); }
1964#line 1967 "parse.c"
1965 break;
1966 case 8:
1967#line 105 "parse.y"
1968{ sqlite3BeginParse(pParse, 2); }
1969#line 1972 "parse.c"
1970 break;
1971 case 9:
1972#line 111 "parse.y"
1973{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy46);}
1974#line 1977 "parse.c"
1975 break;
1976 case 13:
1977#line 116 "parse.y"
1978{yygotominor.yy46 = TK_DEFERRED;}
1979#line 1982 "parse.c"
1980 break;
1981 case 14:
1982 case 15:
1983 case 16:
1984 case 107:
1985 case 109:
1986#line 117 "parse.y"
1987{yygotominor.yy46 = yymsp[0].major;}
1988#line 1991 "parse.c"
1989 break;
1990 case 17:
1991 case 18:
1992#line 120 "parse.y"
1993{sqlite3CommitTransaction(pParse);}
1994#line 1997 "parse.c"
1995 break;
1996 case 19:
1997#line 122 "parse.y"
1998{sqlite3RollbackTransaction(pParse);}
1999#line 2002 "parse.c"
2000 break;
2001 case 21:
2002#line 127 "parse.y"
2003{
2004 sqlite3StartTable(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410,yymsp[-4].minor.yy46,0,0,yymsp[-2].minor.yy46);
2005}
2006#line 2009 "parse.c"
2007 break;
2008 case 22:
2009 case 25:
2010 case 63:
2011 case 77:
2012 case 79:
2013 case 90:
2014 case 101:
2015 case 112:
2016 case 113:
2017 case 212:
2018 case 215:
2019#line 131 "parse.y"
2020{yygotominor.yy46 = 0;}
2021#line 2024 "parse.c"
2022 break;
2023 case 23:
2024 case 24:
2025 case 64:
2026 case 78:
2027 case 100:
2028 case 111:
2029 case 213:
2030 case 216:
2031#line 132 "parse.y"
2032{yygotominor.yy46 = 1;}
2033#line 2036 "parse.c"
2034 break;
2035 case 26:
2036#line 138 "parse.y"
2037{
2038 sqlite3EndTable(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy0,0);
2039}
2040#line 2043 "parse.c"
2041 break;
2042 case 27:
2043#line 141 "parse.y"
2044{
2045 sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy219);
2046 sqlite3SelectDelete(yymsp[0].minor.yy219);
2047}
2048#line 2051 "parse.c"
2049 break;
2050 case 30:
2051#line 153 "parse.y"
2052{
2053 yygotominor.yy410.z = yymsp[-2].minor.yy410.z;
2054 yygotominor.yy410.n = (pParse->sLastToken.z-yymsp[-2].minor.yy410.z) + pParse->sLastToken.n;
2055}
2056#line 2059 "parse.c"
2057 break;
2058 case 31:
2059#line 157 "parse.y"
2060{
2061 sqlite3AddColumn(pParse,&yymsp[0].minor.yy410);
2062 yygotominor.yy410 = yymsp[0].minor.yy410;
2063}
2064#line 2067 "parse.c"
2065 break;
2066 case 32:
2067 case 33:
2068 case 34:
2069 case 35:
2070 case 36:
2071 case 255:
2072#line 167 "parse.y"
2073{yygotominor.yy410 = yymsp[0].minor.yy0;}
2074#line 2077 "parse.c"
2075 break;
2076 case 38:
2077#line 228 "parse.y"
2078{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy410);}
2079#line 2082 "parse.c"
2080 break;
2081 case 39:
2082 case 42:
2083 case 119:
2084 case 120:
2085 case 131:
2086 case 240:
2087 case 242:
2088 case 251:
2089 case 252:
2090 case 253:
2091 case 254:
2092#line 229 "parse.y"
2093{yygotominor.yy410 = yymsp[0].minor.yy410;}
2094#line 2097 "parse.c"
2095 break;
2096 case 40:
2097#line 230 "parse.y"
2098{
2099 yygotominor.yy410.z = yymsp[-3].minor.yy410.z;
2100 yygotominor.yy410.n = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy410.z;
2101}
2102#line 2105 "parse.c"
2103 break;
2104 case 41:
2105#line 234 "parse.y"
2106{
2107 yygotominor.yy410.z = yymsp[-5].minor.yy410.z;
2108 yygotominor.yy410.n = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy410.z;
2109}
2110#line 2113 "parse.c"
2111 break;
2112 case 43:
2113#line 240 "parse.y"
2114{yygotominor.yy410.z=yymsp[-1].minor.yy410.z; yygotominor.yy410.n=yymsp[0].minor.yy410.n+(yymsp[0].minor.yy410.z-yymsp[-1].minor.yy410.z);}
2115#line 2118 "parse.c"
2116 break;
2117 case 50:
2118 case 52:
2119#line 251 "parse.y"
2120{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy172);}
2121#line 2124 "parse.c"
2122 break;
2123 case 51:
2124#line 252 "parse.y"
2125{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy172);}
2126#line 2129 "parse.c"
2127 break;
2128 case 53:
2129#line 254 "parse.y"
2130{
2131 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
2132 sqlite3AddDefaultValue(pParse,p);
2133}
2134#line 2137 "parse.c"
2135 break;
2136 case 54:
2137#line 258 "parse.y"
2138{
2139 Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &yymsp[0].minor.yy410);
2140 sqlite3AddDefaultValue(pParse,p);
2141}
2142#line 2145 "parse.c"
2143 break;
2144 case 56:
2145#line 267 "parse.y"
2146{sqlite3AddNotNull(pParse, yymsp[0].minor.yy46);}
2147#line 2150 "parse.c"
2148 break;
2149 case 57:
2150#line 269 "parse.y"
2151{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy46,yymsp[0].minor.yy46,yymsp[-2].minor.yy46);}
2152#line 2155 "parse.c"
2153 break;
2154 case 58:
2155#line 270 "parse.y"
2156{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy46,0,0,0,0);}
2157#line 2160 "parse.c"
2158 break;
2159 case 59:
2160#line 271 "parse.y"
2161{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy172);}
2162#line 2165 "parse.c"
2163 break;
2164 case 60:
2165#line 273 "parse.y"
2166{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy410,yymsp[-1].minor.yy174,yymsp[0].minor.yy46);}
2167#line 2170 "parse.c"
2168 break;
2169 case 61:
2170#line 274 "parse.y"
2171{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy46);}
2172#line 2175 "parse.c"
2173 break;
2174 case 62:
2175#line 275 "parse.y"
2176{sqlite3AddCollateType(pParse, (char*)yymsp[0].minor.yy410.z, yymsp[0].minor.yy410.n);}
2177#line 2180 "parse.c"
2178 break;
2179 case 65:
2180#line 288 "parse.y"
2181{ yygotominor.yy46 = OE_Restrict * 0x010101; }
2182#line 2185 "parse.c"
2183 break;
2184 case 66:
2185#line 289 "parse.y"
2186{ yygotominor.yy46 = (yymsp[-1].minor.yy46 & yymsp[0].minor.yy405.mask) | yymsp[0].minor.yy405.value; }
2187#line 2190 "parse.c"
2188 break;
2189 case 67:
2190#line 291 "parse.y"
2191{ yygotominor.yy405.value = 0; yygotominor.yy405.mask = 0x000000; }
2192#line 2195 "parse.c"
2193 break;
2194 case 68:
2195#line 292 "parse.y"
2196{ yygotominor.yy405.value = yymsp[0].minor.yy46; yygotominor.yy405.mask = 0x0000ff; }
2197#line 2200 "parse.c"
2198 break;
2199 case 69:
2200#line 293 "parse.y"
2201{ yygotominor.yy405.value = yymsp[0].minor.yy46<<8; yygotominor.yy405.mask = 0x00ff00; }
2202#line 2205 "parse.c"
2203 break;
2204 case 70:
2205#line 294 "parse.y"
2206{ yygotominor.yy405.value = yymsp[0].minor.yy46<<16; yygotominor.yy405.mask = 0xff0000; }
2207#line 2210 "parse.c"
2208 break;
2209 case 71:
2210#line 296 "parse.y"
2211{ yygotominor.yy46 = OE_SetNull; }
2212#line 2215 "parse.c"
2213 break;
2214 case 72:
2215#line 297 "parse.y"
2216{ yygotominor.yy46 = OE_SetDflt; }
2217#line 2220 "parse.c"
2218 break;
2219 case 73:
2220#line 298 "parse.y"
2221{ yygotominor.yy46 = OE_Cascade; }
2222#line 2225 "parse.c"
2223 break;
2224 case 74:
2225#line 299 "parse.y"
2226{ yygotominor.yy46 = OE_Restrict; }
2227#line 2230 "parse.c"
2228 break;
2229 case 75:
2230 case 76:
2231 case 91:
2232 case 93:
2233 case 95:
2234 case 96:
2235 case 166:
2236#line 301 "parse.y"
2237{yygotominor.yy46 = yymsp[0].minor.yy46;}
2238#line 2241 "parse.c"
2239 break;
2240 case 80:
2241#line 311 "parse.y"
2242{yygotominor.yy410.n = 0; yygotominor.yy410.z = 0;}
2243#line 2246 "parse.c"
2244 break;
2245 case 81:
2246#line 312 "parse.y"
2247{yygotominor.yy410 = yymsp[-1].minor.yy0;}
2248#line 2251 "parse.c"
2249 break;
2250 case 86:
2251#line 318 "parse.y"
2252{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy174,yymsp[0].minor.yy46,yymsp[-2].minor.yy46,0);}
2253#line 2256 "parse.c"
2254 break;
2255 case 87:
2256#line 320 "parse.y"
2257{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy174,yymsp[0].minor.yy46,0,0,0,0);}
2258#line 2261 "parse.c"
2259 break;
2260 case 88:
2261#line 321 "parse.y"
2262{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy172);}
2263#line 2266 "parse.c"
2264 break;
2265 case 89:
2266#line 323 "parse.y"
2267{
2268 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy174, &yymsp[-3].minor.yy410, yymsp[-2].minor.yy174, yymsp[-1].minor.yy46);
2269 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy46);
2270}
2271#line 2274 "parse.c"
2272 break;
2273 case 92:
2274 case 94:
2275#line 337 "parse.y"
2276{yygotominor.yy46 = OE_Default;}
2277#line 2280 "parse.c"
2278 break;
2279 case 97:
2280#line 342 "parse.y"
2281{yygotominor.yy46 = OE_Ignore;}
2282#line 2285 "parse.c"
2283 break;
2284 case 98:
2285 case 167:
2286#line 343 "parse.y"
2287{yygotominor.yy46 = OE_Replace;}
2288#line 2291 "parse.c"
2289 break;
2290 case 99:
2291#line 347 "parse.y"
2292{
2293 sqlite3DropTable(pParse, yymsp[0].minor.yy373, 0, yymsp[-1].minor.yy46);
2294}
2295#line 2298 "parse.c"
2296 break;
2297 case 102:
2298#line 357 "parse.y"
2299{
2300 sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy410, &yymsp[-2].minor.yy410, yymsp[0].minor.yy219, yymsp[-6].minor.yy46, yymsp[-4].minor.yy46);
2301}
2302#line 2305 "parse.c"
2303 break;
2304 case 103:
2305#line 360 "parse.y"
2306{
2307 sqlite3DropTable(pParse, yymsp[0].minor.yy373, 1, yymsp[-1].minor.yy46);
2308}
2309#line 2312 "parse.c"
2310 break;
2311 case 104:
2312#line 367 "parse.y"
2313{
2314 sqlite3Select(pParse, yymsp[0].minor.yy219, SRT_Callback, 0, 0, 0, 0, 0);
2315 sqlite3SelectDelete(yymsp[0].minor.yy219);
2316}
2317#line 2320 "parse.c"
2318 break;
2319 case 105:
2320 case 128:
2321#line 377 "parse.y"
2322{yygotominor.yy219 = yymsp[0].minor.yy219;}
2323#line 2326 "parse.c"
2324 break;
2325 case 106:
2326#line 379 "parse.y"
2327{
2328 if( yymsp[0].minor.yy219 ){
2329 yymsp[0].minor.yy219->op = yymsp[-1].minor.yy46;
2330 yymsp[0].minor.yy219->pPrior = yymsp[-2].minor.yy219;
2331 }else{
2332 sqlite3SelectDelete(yymsp[-2].minor.yy219);
2333 }
2334 yygotominor.yy219 = yymsp[0].minor.yy219;
2335}
2336#line 2339 "parse.c"
2337 break;
2338 case 108:
2339#line 390 "parse.y"
2340{yygotominor.yy46 = TK_ALL;}
2341#line 2344 "parse.c"
2342 break;
2343 case 110:
2344#line 394 "parse.y"
2345{
2346 yygotominor.yy219 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy174,yymsp[-5].minor.yy373,yymsp[-4].minor.yy172,yymsp[-3].minor.yy174,yymsp[-2].minor.yy172,yymsp[-1].minor.yy174,yymsp[-7].minor.yy46,yymsp[0].minor.yy234.pLimit,yymsp[0].minor.yy234.pOffset);
2347}
2348#line 2351 "parse.c"
2349 break;
2350 case 114:
2351 case 237:
2352#line 415 "parse.y"
2353{yygotominor.yy174 = yymsp[-1].minor.yy174;}
2354#line 2357 "parse.c"
2355 break;
2356 case 115:
2357 case 141:
2358 case 149:
2359 case 230:
2360 case 236:
2361#line 416 "parse.y"
2362{yygotominor.yy174 = 0;}
2363#line 2366 "parse.c"
2364 break;
2365 case 116:
2366#line 417 "parse.y"
2367{
2368 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[-1].minor.yy172,yymsp[0].minor.yy410.n?&yymsp[0].minor.yy410:0);
2369}
2370#line 2373 "parse.c"
2371 break;
2372 case 117:
2373#line 420 "parse.y"
2374{
2375 Expr *p = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
2376 yygotominor.yy174 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy174, p, 0);
2377}
2378#line 2381 "parse.c"
2379 break;
2380 case 118:
2381#line 424 "parse.y"
2382{
2383 Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
2384 Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
2385 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
2386 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174, pDot, 0);
2387}
2388#line 2391 "parse.c"
2389 break;
2390 case 121:
2391#line 437 "parse.y"
2392{yygotominor.yy410.n = 0;}
2393#line 2396 "parse.c"
2394 break;
2395 case 122:
2396#line 449 "parse.y"
2397{yygotominor.yy373 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy373));}
2398#line 2401 "parse.c"
2399 break;
2400 case 123:
2401#line 450 "parse.y"
2402{
2403 yygotominor.yy373 = yymsp[0].minor.yy373;
2404 sqlite3SrcListShiftJoinType(yygotominor.yy373);
2405}
2406#line 2409 "parse.c"
2407 break;
2408 case 124:
2409#line 458 "parse.y"
2410{
2411 yygotominor.yy373 = yymsp[-1].minor.yy373;
2412 if( yygotominor.yy373 && yygotominor.yy373->nSrc>0 ) yygotominor.yy373->a[yygotominor.yy373->nSrc-1].jointype = yymsp[0].minor.yy46;
2413}
2414#line 2417 "parse.c"
2415 break;
2416 case 125:
2417#line 462 "parse.y"
2418{yygotominor.yy373 = 0;}
2419#line 2422 "parse.c"
2420 break;
2421 case 126:
2422#line 463 "parse.y"
2423{
2424 yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy373,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,0,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
2425}
2426#line 2429 "parse.c"
2427 break;
2428 case 127:
2429#line 468 "parse.y"
2430{
2431 yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy373,0,0,&yymsp[-2].minor.yy410,yymsp[-4].minor.yy219,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
2432 }
2433#line 2436 "parse.c"
2434 break;
2435 case 129:
2436#line 479 "parse.y"
2437{
2438 sqlite3SrcListShiftJoinType(yymsp[0].minor.yy373);
2439 yygotominor.yy219 = sqlite3SelectNew(pParse,0,yymsp[0].minor.yy373,0,0,0,0,0,0,0);
2440 }
2441#line 2444 "parse.c"
2442 break;
2443 case 130:
2444#line 486 "parse.y"
2445{yygotominor.yy410.z=0; yygotominor.yy410.n=0;}
2446#line 2449 "parse.c"
2447 break;
2448 case 132:
2449#line 491 "parse.y"
2450{yygotominor.yy373 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);}
2451#line 2454 "parse.c"
2452 break;
2453 case 133:
2454#line 495 "parse.y"
2455{ yygotominor.yy46 = JT_INNER; }
2456#line 2459 "parse.c"
2457 break;
2458 case 134:
2459#line 496 "parse.y"
2460{ yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
2461#line 2464 "parse.c"
2462 break;
2463 case 135:
2464#line 497 "parse.y"
2465{ yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy410,0); }
2466#line 2469 "parse.c"
2467 break;
2468 case 136:
2469#line 499 "parse.y"
2470{ yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy410,&yymsp[-1].minor.yy410); }
2471#line 2474 "parse.c"
2472 break;
2473 case 137:
2474 case 145:
2475 case 152:
2476 case 159:
2477 case 174:
2478 case 202:
2479 case 225:
2480 case 227:
2481#line 503 "parse.y"
2482{yygotominor.yy172 = yymsp[0].minor.yy172;}
2483#line 2486 "parse.c"
2484 break;
2485 case 138:
2486 case 151:
2487 case 158:
2488 case 203:
2489 case 226:
2490 case 228:
2491#line 504 "parse.y"
2492{yygotominor.yy172 = 0;}
2493#line 2496 "parse.c"
2494 break;
2495 case 139:
2496 case 171:
2497#line 508 "parse.y"
2498{yygotominor.yy432 = yymsp[-1].minor.yy432;}
2499#line 2502 "parse.c"
2500 break;
2501 case 140:
2502 case 170:
2503#line 509 "parse.y"
2504{yygotominor.yy432 = 0;}
2505#line 2508 "parse.c"
2506 break;
2507 case 142:
2508 case 150:
2509 case 229:
2510#line 520 "parse.y"
2511{yygotominor.yy174 = yymsp[0].minor.yy174;}
2512#line 2515 "parse.c"
2513 break;
2514 case 143:
2515#line 521 "parse.y"
2516{
2517 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174,yymsp[-1].minor.yy172,0);
2518 if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
2519}
2520#line 2523 "parse.c"
2521 break;
2522 case 144:
2523#line 525 "parse.y"
2524{
2525 yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy172,0);
2526 if( yygotominor.yy174 && yygotominor.yy174->a ) yygotominor.yy174->a[0].sortOrder = yymsp[0].minor.yy46;
2527}
2528#line 2531 "parse.c"
2529 break;
2530 case 146:
2531 case 148:
2532#line 533 "parse.y"
2533{yygotominor.yy46 = SQLITE_SO_ASC;}
2534#line 2537 "parse.c"
2535 break;
2536 case 147:
2537#line 534 "parse.y"
2538{yygotominor.yy46 = SQLITE_SO_DESC;}
2539#line 2542 "parse.c"
2540 break;
2541 case 153:
2542#line 560 "parse.y"
2543{yygotominor.yy234.pLimit = 0; yygotominor.yy234.pOffset = 0;}
2544#line 2547 "parse.c"
2545 break;
2546 case 154:
2547#line 561 "parse.y"
2548{yygotominor.yy234.pLimit = yymsp[0].minor.yy172; yygotominor.yy234.pOffset = 0;}
2549#line 2552 "parse.c"
2550 break;
2551 case 155:
2552#line 563 "parse.y"
2553{yygotominor.yy234.pLimit = yymsp[-2].minor.yy172; yygotominor.yy234.pOffset = yymsp[0].minor.yy172;}
2554#line 2557 "parse.c"
2555 break;
2556 case 156:
2557#line 565 "parse.y"
2558{yygotominor.yy234.pOffset = yymsp[-2].minor.yy172; yygotominor.yy234.pLimit = yymsp[0].minor.yy172;}
2559#line 2562 "parse.c"
2560 break;
2561 case 157:
2562#line 569 "parse.y"
2563{sqlite3DeleteFrom(pParse,yymsp[-1].minor.yy373,yymsp[0].minor.yy172);}
2564#line 2567 "parse.c"
2565 break;
2566 case 160:
2567#line 579 "parse.y"
2568{
2569 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy174,SQLITE_MAX_COLUMN,"set list");
2570 sqlite3Update(pParse,yymsp[-3].minor.yy373,yymsp[-1].minor.yy174,yymsp[0].minor.yy172,yymsp[-4].minor.yy46);
2571}
2572#line 2575 "parse.c"
2573 break;
2574 case 161:
2575#line 588 "parse.y"
2576{yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
2577#line 2580 "parse.c"
2578 break;
2579 case 162:
2580#line 590 "parse.y"
2581{yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
2582#line 2585 "parse.c"
2583 break;
2584 case 163:
2585#line 596 "parse.y"
2586{sqlite3Insert(pParse, yymsp[-5].minor.yy373, yymsp[-1].minor.yy174, 0, yymsp[-4].minor.yy432, yymsp[-7].minor.yy46);}
2587#line 2590 "parse.c"
2588 break;
2589 case 164:
2590#line 598 "parse.y"
2591{sqlite3Insert(pParse, yymsp[-2].minor.yy373, 0, yymsp[0].minor.yy219, yymsp[-1].minor.yy432, yymsp[-4].minor.yy46);}
2592#line 2595 "parse.c"
2593 break;
2594 case 165:
2595#line 600 "parse.y"
2596{sqlite3Insert(pParse, yymsp[-3].minor.yy373, 0, 0, yymsp[-2].minor.yy432, yymsp[-5].minor.yy46);}
2597#line 2600 "parse.c"
2598 break;
2599 case 168:
2600 case 231:
2601#line 611 "parse.y"
2602{yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[0].minor.yy172,0);}
2603#line 2606 "parse.c"
2604 break;
2605 case 169:
2606 case 232:
2607#line 613 "parse.y"
2608{yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,0);}
2609#line 2612 "parse.c"
2610 break;
2611 case 172:
2612#line 623 "parse.y"
2613{yygotominor.yy432 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy432,&yymsp[0].minor.yy410);}
2614#line 2617 "parse.c"
2615 break;
2616 case 173:
2617#line 625 "parse.y"
2618{yygotominor.yy432 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy410);}
2619#line 2622 "parse.c"
2620 break;
2621 case 175:
2622#line 636 "parse.y"
2623{yygotominor.yy172 = yymsp[-1].minor.yy172; sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); }
2624#line 2627 "parse.c"
2625 break;
2626 case 176:
2627 case 181:
2628 case 182:
2629#line 637 "parse.y"
2630{yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, 0, 0, &yymsp[0].minor.yy0);}
2631#line 2634 "parse.c"
2632 break;
2633 case 177:
2634 case 178:
2635#line 638 "parse.y"
2636{yygotominor.yy172 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);}
2637#line 2640 "parse.c"
2638 break;
2639 case 179:
2640#line 640 "parse.y"
2641{
2642 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
2643 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy410);
2644 yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
2645}
2646#line 2649 "parse.c"
2647 break;
2648 case 180:
2649#line 645 "parse.y"
2650{
2651 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy410);
2652 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
2653 Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy410);
2654 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
2655 yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
2656}
2657#line 2660 "parse.c"
2658 break;
2659 case 183:
2660#line 654 "parse.y"
2661{yygotominor.yy172 = sqlite3RegisterExpr(pParse, &yymsp[0].minor.yy0);}
2662#line 2665 "parse.c"
2663 break;
2664 case 184:
2665#line 655 "parse.y"
2666{
2667 Token *pToken = &yymsp[0].minor.yy0;
2668 Expr *pExpr = yygotominor.yy172 = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken);
2669 sqlite3ExprAssignVarNumber(pParse, pExpr);
2670}
2671#line 2674 "parse.c"
2672 break;
2673 case 185:
2674#line 660 "parse.y"
2675{
2676 yygotominor.yy172 = sqlite3ExprSetColl(pParse, yymsp[-2].minor.yy172, &yymsp[0].minor.yy410);
2677}
2678#line 2681 "parse.c"
2679 break;
2680 case 186:
2681#line 664 "parse.y"
2682{
2683 yygotominor.yy172 = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy172, 0, &yymsp[-1].minor.yy410);
2684 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
2685}
2686#line 2689 "parse.c"
2687 break;
2688 case 187:
2689#line 669 "parse.y"
2690{
2691 if( yymsp[-1].minor.yy174 && yymsp[-1].minor.yy174->nExpr>SQLITE_MAX_FUNCTION_ARG ){
2692 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
2693 }
2694 yygotominor.yy172 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy174, &yymsp[-4].minor.yy0);
2695 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
2696 if( yymsp[-2].minor.yy46 && yygotominor.yy172 ){
2697 yygotominor.yy172->flags |= EP_Distinct;
2698 }
2699}
2700#line 2703 "parse.c"
2701 break;
2702 case 188:
2703#line 679 "parse.y"
2704{
2705 yygotominor.yy172 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
2706 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
2707}
2708#line 2711 "parse.c"
2709 break;
2710 case 189:
2711#line 683 "parse.y"
2712{
2713 /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
2714 ** treated as functions that return constants */
2715 yygotominor.yy172 = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
2716 if( yygotominor.yy172 ){
2717 yygotominor.yy172->op = TK_CONST_FUNC;
2718 yygotominor.yy172->span = yymsp[0].minor.yy0;
2719 }
2720}
2721#line 2724 "parse.c"
2722 break;
2723 case 190:
2724 case 191:
2725 case 192:
2726 case 193:
2727 case 194:
2728 case 195:
2729 case 196:
2730 case 197:
2731#line 692 "parse.y"
2732{yygotominor.yy172 = sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy172,yymsp[0].minor.yy172,0);}
2733#line 2736 "parse.c"
2734 break;
2735 case 198:
2736 case 200:
2737#line 704 "parse.y"
2738{yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 0;}
2739#line 2742 "parse.c"
2740 break;
2741 case 199:
2742 case 201:
2743#line 705 "parse.y"
2744{yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 1;}
2745#line 2748 "parse.c"
2746 break;
2747 case 204:
2748#line 712 "parse.y"
2749{
2750 ExprList *pList;
2751 pList = sqlite3ExprListAppend(pParse,0, yymsp[-1].minor.yy172, 0);
2752 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-3].minor.yy172, 0);
2753 if( yymsp[0].minor.yy172 ){
2754 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy172, 0);
2755 }
2756 yygotominor.yy172 = sqlite3ExprFunction(pParse, pList, &yymsp[-2].minor.yy72.eOperator);
2757 if( yymsp[-2].minor.yy72.not ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
2758 sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy172->span, &yymsp[-1].minor.yy172->span);
2759 if( yygotominor.yy172 ) yygotominor.yy172->flags |= EP_InfixFunc;
2760}
2761#line 2764 "parse.c"
2762 break;
2763 case 205:
2764#line 725 "parse.y"
2765{
2766 yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, yymsp[-1].minor.yy172, 0, 0);
2767 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy172->span,&yymsp[0].minor.yy0);
2768}
2769#line 2772 "parse.c"
2770 break;
2771 case 206:
2772#line 729 "parse.y"
2773{
2774 yygotominor.yy172 = sqlite3PExpr(pParse, TK_ISNULL, yymsp[-2].minor.yy172, 0, 0);
2775 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0);
2776}
2777#line 2780 "parse.c"
2778 break;
2779 case 207:
2780#line 733 "parse.y"
2781{
2782 yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-2].minor.yy172, 0, 0);
2783 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0);
2784}
2785#line 2788 "parse.c"
2786 break;
2787 case 208:
2788#line 737 "parse.y"
2789{
2790 yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-3].minor.yy172, 0, 0);
2791 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,&yymsp[0].minor.yy0);
2792}
2793#line 2796 "parse.c"
2794 break;
2795 case 209:
2796#line 741 "parse.y"
2797{
2798 yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy172, 0, 0);
2799 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
2800}
2801#line 2804 "parse.c"
2802 break;
2803 case 210:
2804#line 745 "parse.y"
2805{
2806 yygotominor.yy172 = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
2807 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
2808}
2809#line 2812 "parse.c"
2810 break;
2811 case 211:
2812#line 749 "parse.y"
2813{
2814 yygotominor.yy172 = sqlite3PExpr(pParse, TK_UPLUS, yymsp[0].minor.yy172, 0, 0);
2815 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
2816}
2817#line 2820 "parse.c"
2818 break;
2819 case 214:
2820#line 756 "parse.y"
2821{
2822 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0);
2823 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy172, 0);
2824 yygotominor.yy172 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy172, 0, 0);
2825 if( yygotominor.yy172 ){
2826 yygotominor.yy172->pList = pList;
2827 }else{
2828 sqlite3ExprListDelete(pList);
2829 }
2830 if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
2831 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy172->span);
2832}
2833#line 2836 "parse.c"
2834 break;
2835 case 217:
2836#line 772 "parse.y"
2837{
2838 yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0);
2839 if( yygotominor.yy172 ){
2840 yygotominor.yy172->pList = yymsp[-1].minor.yy174;
2841 sqlite3ExprSetHeight(yygotominor.yy172);
2842 }else{
2843 sqlite3ExprListDelete(yymsp[-1].minor.yy174);
2844 }
2845 if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
2846 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0);
2847 }
2848#line 2851 "parse.c"
2849 break;
2850 case 218:
2851#line 783 "parse.y"
2852{
2853 yygotominor.yy172 = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
2854 if( yygotominor.yy172 ){
2855 yygotominor.yy172->pSelect = yymsp[-1].minor.yy219;
2856 sqlite3ExprSetHeight(yygotominor.yy172);
2857 }else{
2858 sqlite3SelectDelete(yymsp[-1].minor.yy219);
2859 }
2860 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
2861 }
2862#line 2865 "parse.c"
2863 break;
2864 case 219:
2865#line 793 "parse.y"
2866{
2867 yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0);
2868 if( yygotominor.yy172 ){
2869 yygotominor.yy172->pSelect = yymsp[-1].minor.yy219;
2870 sqlite3ExprSetHeight(yygotominor.yy172);
2871 }else{
2872 sqlite3SelectDelete(yymsp[-1].minor.yy219);
2873 }
2874 if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
2875 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0);
2876 }
2877#line 2880 "parse.c"
2878 break;
2879 case 220:
2880#line 804 "parse.y"
2881{
2882 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);
2883 yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy172, 0, 0);
2884 if( yygotominor.yy172 ){
2885 yygotominor.yy172->pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
2886 sqlite3ExprSetHeight(yygotominor.yy172);
2887 }else{
2888 sqlite3SrcListDelete(pSrc);
2889 }
2890 if( yymsp[-2].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
2891 sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,yymsp[0].minor.yy410.z?&yymsp[0].minor.yy410:&yymsp[-1].minor.yy410);
2892 }
2893#line 2896 "parse.c"
2894 break;
2895 case 221:
2896#line 816 "parse.y"
2897{
2898 Expr *p = yygotominor.yy172 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
2899 if( p ){
2900 p->pSelect = yymsp[-1].minor.yy219;
2901 sqlite3ExprSpan(p,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
2902 sqlite3ExprSetHeight(yygotominor.yy172);
2903 }else{
2904 sqlite3SelectDelete(yymsp[-1].minor.yy219);
2905 }
2906 }
2907#line 2910 "parse.c"
2908 break;
2909 case 222:
2910#line 829 "parse.y"
2911{
2912 yygotominor.yy172 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, 0);
2913 if( yygotominor.yy172 ){
2914 yygotominor.yy172->pList = yymsp[-2].minor.yy174;
2915 sqlite3ExprSetHeight(yygotominor.yy172);
2916 }else{
2917 sqlite3ExprListDelete(yymsp[-2].minor.yy174);
2918 }
2919 sqlite3ExprSpan(yygotominor.yy172, &yymsp[-4].minor.yy0, &yymsp[0].minor.yy0);
2920}
2921#line 2924 "parse.c"
2922 break;
2923 case 223:
2924#line 841 "parse.y"
2925{
2926 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174, yymsp[-2].minor.yy172, 0);
2927 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0);
2928}
2929#line 2932 "parse.c"
2930 break;
2931 case 224:
2932#line 845 "parse.y"
2933{
2934 yygotominor.yy174 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0);
2935 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0);
2936}
2937#line 2940 "parse.c"
2938 break;
2939 case 233:
2940#line 874 "parse.y"
2941{
2942 sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy410, &yymsp[-5].minor.yy410,
2943 sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy410,0), yymsp[-1].minor.yy174, yymsp[-9].minor.yy46,
2944 &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy46);
2945}
2946#line 2949 "parse.c"
2947 break;
2948 case 234:
2949 case 281:
2950#line 881 "parse.y"
2951{yygotominor.yy46 = OE_Abort;}
2952#line 2955 "parse.c"
2953 break;
2954 case 235:
2955#line 882 "parse.y"
2956{yygotominor.yy46 = OE_None;}
2957#line 2960 "parse.c"
2958 break;
2959 case 238:
2960#line 892 "parse.y"
2961{
2962 Expr *p = 0;
2963 if( yymsp[-1].minor.yy410.n>0 ){
2964 p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
2965 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n);
2966 }
2967 yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174, p, &yymsp[-2].minor.yy410);
2968 sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index");
2969 if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
2970}
2971#line 2974 "parse.c"
2972 break;
2973 case 239:
2974#line 902 "parse.y"
2975{
2976 Expr *p = 0;
2977 if( yymsp[-1].minor.yy410.n>0 ){
2978 p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
2979 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n);
2980 }
2981 yygotominor.yy174 = sqlite3ExprListAppend(pParse,0, p, &yymsp[-2].minor.yy410);
2982 sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index");
2983 if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
2984}
2985#line 2988 "parse.c"
2986 break;
2987 case 241:
2988#line 915 "parse.y"
2989{yygotominor.yy410.z = 0; yygotominor.yy410.n = 0;}
2990#line 2993 "parse.c"
2991 break;
2992 case 243:
2993#line 921 "parse.y"
2994{sqlite3DropIndex(pParse, yymsp[0].minor.yy373, yymsp[-1].minor.yy46);}
2995#line 2998 "parse.c"
2996 break;
2997 case 244:
2998 case 245:
2999#line 927 "parse.y"
3000{sqlite3Vacuum(pParse);}
3001#line 3004 "parse.c"
3002 break;
3003 case 246:
3004#line 935 "parse.y"
3005{sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,0);}
3006#line 3009 "parse.c"
3007 break;
3008 case 247:
3009#line 936 "parse.y"
3010{sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy0,0);}
3011#line 3014 "parse.c"
3012 break;
3013 case 248:
3014#line 937 "parse.y"
3015{
3016 sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,1);
3017}
3018#line 3021 "parse.c"
3019 break;
3020 case 249:
3021#line 940 "parse.y"
3022{sqlite3Pragma(pParse,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-1].minor.yy410,0);}
3023#line 3026 "parse.c"
3024 break;
3025 case 250:
3026#line 941 "parse.y"
3027{sqlite3Pragma(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410,0,0);}
3028#line 3031 "parse.c"
3029 break;
3030 case 258:
3031#line 955 "parse.y"
3032{
3033 Token all;
3034 all.z = yymsp[-3].minor.yy410.z;
3035 all.n = (yymsp[0].minor.yy0.z - yymsp[-3].minor.yy410.z) + yymsp[0].minor.yy0.n;
3036 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy243, &all);
3037}
3038#line 3041 "parse.c"
3039 break;
3040 case 259:
3041#line 964 "parse.y"
3042{
3043 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy410, &yymsp[-6].minor.yy410, yymsp[-5].minor.yy46, yymsp[-4].minor.yy370.a, yymsp[-4].minor.yy370.b, yymsp[-2].minor.yy373, yymsp[0].minor.yy172, yymsp[-10].minor.yy46, yymsp[-8].minor.yy46);
3044 yygotominor.yy410 = (yymsp[-6].minor.yy410.n==0?yymsp[-7].minor.yy410:yymsp[-6].minor.yy410);
3045}
3046#line 3049 "parse.c"
3047 break;
3048 case 260:
3049 case 263:
3050#line 970 "parse.y"
3051{ yygotominor.yy46 = TK_BEFORE; }
3052#line 3055 "parse.c"
3053 break;
3054 case 261:
3055#line 971 "parse.y"
3056{ yygotominor.yy46 = TK_AFTER; }
3057#line 3060 "parse.c"
3058 break;
3059 case 262:
3060#line 972 "parse.y"
3061{ yygotominor.yy46 = TK_INSTEAD;}
3062#line 3065 "parse.c"
3063 break;
3064 case 264:
3065 case 265:
3066#line 977 "parse.y"
3067{yygotominor.yy370.a = yymsp[0].major; yygotominor.yy370.b = 0;}
3068#line 3071 "parse.c"
3069 break;
3070 case 266:
3071#line 979 "parse.y"
3072{yygotominor.yy370.a = TK_UPDATE; yygotominor.yy370.b = yymsp[0].minor.yy432;}
3073#line 3076 "parse.c"
3074 break;
3075 case 269:
3076#line 986 "parse.y"
3077{ yygotominor.yy172 = 0; }
3078#line 3081 "parse.c"
3079 break;
3080 case 270:
3081#line 987 "parse.y"
3082{ yygotominor.yy172 = yymsp[0].minor.yy172; }
3083#line 3086 "parse.c"
3084 break;
3085 case 271:
3086#line 991 "parse.y"
3087{
3088 if( yymsp[-2].minor.yy243 ){
3089 yymsp[-2].minor.yy243->pLast->pNext = yymsp[-1].minor.yy243;
3090 }else{
3091 yymsp[-2].minor.yy243 = yymsp[-1].minor.yy243;
3092 }
3093 yymsp[-2].minor.yy243->pLast = yymsp[-1].minor.yy243;
3094 yygotominor.yy243 = yymsp[-2].minor.yy243;
3095}
3096#line 3099 "parse.c"
3097 break;
3098 case 272:
3099#line 1000 "parse.y"
3100{ yygotominor.yy243 = 0; }
3101#line 3104 "parse.c"
3102 break;
3103 case 273:
3104#line 1006 "parse.y"
3105{ yygotominor.yy243 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-3].minor.yy410, yymsp[-1].minor.yy174, yymsp[0].minor.yy172, yymsp[-4].minor.yy46); }
3106#line 3109 "parse.c"
3107 break;
3108 case 274:
3109#line 1011 "parse.y"
3110{yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy410, yymsp[-4].minor.yy432, yymsp[-1].minor.yy174, 0, yymsp[-7].minor.yy46);}
3111#line 3114 "parse.c"
3112 break;
3113 case 275:
3114#line 1014 "parse.y"
3115{yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy410, yymsp[-1].minor.yy432, 0, yymsp[0].minor.yy219, yymsp[-4].minor.yy46);}
3116#line 3119 "parse.c"
3117 break;
3118 case 276:
3119#line 1018 "parse.y"
3120{yygotominor.yy243 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-1].minor.yy410, yymsp[0].minor.yy172);}
3121#line 3124 "parse.c"
3122 break;
3123 case 277:
3124#line 1021 "parse.y"
3125{yygotominor.yy243 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy219); }
3126#line 3129 "parse.c"
3127 break;
3128 case 278:
3129#line 1024 "parse.y"
3130{
3131 yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
3132 if( yygotominor.yy172 ){
3133 yygotominor.yy172->iColumn = OE_Ignore;
3134 sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0);
3135 }
3136}
3137#line 3140 "parse.c"
3138 break;
3139 case 279:
3140#line 1031 "parse.y"
3141{
3142 yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy410);
3143 if( yygotominor.yy172 ) {
3144 yygotominor.yy172->iColumn = yymsp[-3].minor.yy46;
3145 sqlite3ExprSpan(yygotominor.yy172, &yymsp[-5].minor.yy0, &yymsp[0].minor.yy0);
3146 }
3147}
3148#line 3151 "parse.c"
3149 break;
3150 case 280:
3151#line 1041 "parse.y"
3152{yygotominor.yy46 = OE_Rollback;}
3153#line 3156 "parse.c"
3154 break;
3155 case 282:
3156#line 1043 "parse.y"
3157{yygotominor.yy46 = OE_Fail;}
3158#line 3161 "parse.c"
3159 break;
3160 case 283:
3161#line 1048 "parse.y"
3162{
3163 sqlite3DropTrigger(pParse,yymsp[0].minor.yy373,yymsp[-1].minor.yy46);
3164}
3165#line 3168 "parse.c"
3166 break;
3167 case 284:
3168#line 1055 "parse.y"
3169{
3170 sqlite3Attach(pParse, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, yymsp[0].minor.yy386);
3171}
3172#line 3175 "parse.c"
3173 break;
3174 case 285:
3175#line 1058 "parse.y"
3176{
3177 sqlite3Detach(pParse, yymsp[0].minor.yy172);
3178}
3179#line 3182 "parse.c"
3180 break;
3181 case 286:
3182#line 1064 "parse.y"
3183{ yygotominor.yy386 = 0; }
3184#line 3187 "parse.c"
3185 break;
3186 case 287:
3187#line 1065 "parse.y"
3188{ yygotominor.yy386 = yymsp[0].minor.yy172; }
3189#line 3192 "parse.c"
3190 break;
3191 case 290:
3192#line 1073 "parse.y"
3193{sqlite3Reindex(pParse, 0, 0);}
3194#line 3197 "parse.c"
3195 break;
3196 case 291:
3197#line 1074 "parse.y"
3198{sqlite3Reindex(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);}
3199#line 3202 "parse.c"
3200 break;
3201 case 292:
3202#line 1079 "parse.y"
3203{sqlite3Analyze(pParse, 0, 0);}
3204#line 3207 "parse.c"
3205 break;
3206 case 293:
3207#line 1080 "parse.y"
3208{sqlite3Analyze(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);}
3209#line 3212 "parse.c"
3210 break;
3211 case 294:
3212#line 1085 "parse.y"
3213{
3214 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy373,&yymsp[0].minor.yy410);
3215}
3216#line 3219 "parse.c"
3217 break;
3218 case 295:
3219#line 1088 "parse.y"
3220{
3221 sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy410);
3222}
3223#line 3226 "parse.c"
3224 break;
3225 case 296:
3226#line 1091 "parse.y"
3227{
3228 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy373);
3229}
3230#line 3233 "parse.c"
3231 break;
3232 case 299:
3233#line 1100 "parse.y"
3234{sqlite3VtabFinishParse(pParse,0);}
3235#line 3238 "parse.c"
3236 break;
3237 case 300:
3238#line 1101 "parse.y"
3239{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
3240#line 3243 "parse.c"
3241 break;
3242 case 301:
3243#line 1102 "parse.y"
3244{
3245 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy410, &yymsp[-2].minor.yy410, &yymsp[0].minor.yy410);
3246}
3247#line 3250 "parse.c"
3248 break;
3249 case 304:
3250#line 1107 "parse.y"
3251{sqlite3VtabArgInit(pParse);}
3252#line 3255 "parse.c"
3253 break;
3254 case 306:
3255 case 307:
3256 case 308:
3257 case 310:
3258#line 1109 "parse.y"
3259{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
3260#line 3263 "parse.c"
3261 break;
3262 };
3263 yygoto = yyRuleInfo[yyruleno].lhs;
3264 yysize = yyRuleInfo[yyruleno].nrhs;
3265 yypParser->yyidx -= yysize;
3266 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto);
3267 if( yyact < YYNSTATE ){
3268#ifdef NDEBUG
3269 /* If we are not debugging and the reduce action popped at least
3270 ** one element off the stack, then we can push the new element back
3271 ** onto the stack here, and skip the stack overflow test in yy_shift().
3272 ** That gives a significant speed improvement. */
3273 if( yysize ){
3274 yypParser->yyidx++;
3275 yymsp -= yysize-1;
3276 yymsp->stateno = yyact;
3277 yymsp->major = yygoto;
3278 yymsp->minor = yygotominor;
3279 }else
3280#endif
3281 {
3282 yy_shift(yypParser,yyact,yygoto,&yygotominor);
3283 }
3284 }else if( yyact == YYNSTATE + YYNRULE + 1 ){
3285 yy_accept(yypParser);
3286 }
3287}
3288
3289/*
3290** The following code executes when the parse fails
3291*/
3292static void yy_parse_failed(
3293 yyParser *yypParser /* The parser */
3294){
3295 sqlite3ParserARG_FETCH;
3296#ifndef NDEBUG
3297 if( yyTraceFILE ){
3298 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
3299 }
3300#endif
3301 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
3302 /* Here code is inserted which will be executed whenever the
3303 ** parser fails */
3304 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
3305}
3306
3307/*
3308** The following code executes when a syntax error first occurs.
3309*/
3310static void yy_syntax_error(
3311 yyParser *yypParser, /* The parser */
3312 int yymajor, /* The major type of the error token */
3313 YYMINORTYPE yyminor /* The minor type of the error token */
3314){
3315 sqlite3ParserARG_FETCH;
3316#define TOKEN (yyminor.yy0)
3317#line 34 "parse.y"
3318
3319 if( !pParse->parseError ){
3320 if( TOKEN.z[0] ){
3321 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
3322 }else{
3323 sqlite3ErrorMsg(pParse, "incomplete SQL statement");
3324 }
3325 pParse->parseError = 1;
3326 }
3327#line 3331 "parse.c"
3328 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
3329}
3330
3331/*
3332** The following is executed when the parser accepts
3333*/
3334static void yy_accept(
3335 yyParser *yypParser /* The parser */
3336){
3337 sqlite3ParserARG_FETCH;
3338#ifndef NDEBUG
3339 if( yyTraceFILE ){
3340 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
3341 }
3342#endif
3343 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
3344 /* Here code is inserted which will be executed whenever the
3345 ** parser accepts */
3346 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
3347}
3348
3349/* The main parser program.
3350** The first argument is a pointer to a structure obtained from
3351** "sqlite3ParserAlloc" which describes the current state of the parser.
3352** The second argument is the major token number. The third is
3353** the minor token. The fourth optional argument is whatever the
3354** user wants (and specified in the grammar) and is available for
3355** use by the action routines.
3356**
3357** Inputs:
3358** <ul>
3359** <li> A pointer to the parser (an opaque structure.)
3360** <li> The major token number.
3361** <li> The minor token number.
3362** <li> An option argument of a grammar-specified type.
3363** </ul>
3364**
3365** Outputs:
3366** None.
3367*/
3368void sqlite3Parser(
3369 void *yyp, /* The parser */
3370 int yymajor, /* The major token code number */
3371 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
3372 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
3373){
3374 YYMINORTYPE yyminorunion;
3375 int yyact; /* The parser action. */
3376 int yyendofinput; /* True if we are at the end of input */
3377 int yyerrorhit = 0; /* True if yymajor has invoked an error */
3378 yyParser *yypParser; /* The parser */
3379
3380 /* (re)initialize the parser, if necessary */
3381 yypParser = (yyParser*)yyp;
3382 if( yypParser->yyidx<0 ){
3383#if YYSTACKDEPTH<=0
3384 if( yypParser->yystksz <=0 ){
3385 memset(&yyminorunion, 0, sizeof(yyminorunion));
3386 yyStackOverflow(yypParser, &yyminorunion);
3387 return;
3388 }
3389#endif
3390 yypParser->yyidx = 0;
3391 yypParser->yyerrcnt = -1;
3392 yypParser->yystack[0].stateno = 0;
3393 yypParser->yystack[0].major = 0;
3394 }
3395 yyminorunion.yy0 = yyminor;
3396 yyendofinput = (yymajor==0);
3397 sqlite3ParserARG_STORE;
3398
3399#ifndef NDEBUG
3400 if( yyTraceFILE ){
3401 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
3402 }
3403#endif
3404
3405 do{
3406 yyact = yy_find_shift_action(yypParser,yymajor);
3407 if( yyact<YYNSTATE ){
3408 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
3409 yypParser->yyerrcnt--;
3410 if( yyendofinput && yypParser->yyidx>=0 ){
3411 yymajor = 0;
3412 }else{
3413 yymajor = YYNOCODE;
3414 }
3415 }else if( yyact < YYNSTATE + YYNRULE ){
3416 yy_reduce(yypParser,yyact-YYNSTATE);
3417 }else if( yyact == YY_ERROR_ACTION ){
3418 int yymx;
3419#ifndef NDEBUG
3420 if( yyTraceFILE ){
3421 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
3422 }
3423#endif
3424#ifdef YYERRORSYMBOL
3425 /* A syntax error has occurred.
3426 ** The response to an error depends upon whether or not the
3427 ** grammar defines an error token "ERROR".
3428 **
3429 ** This is what we do if the grammar does define ERROR:
3430 **
3431 ** * Call the %syntax_error function.
3432 **
3433 ** * Begin popping the stack until we enter a state where
3434 ** it is legal to shift the error symbol, then shift
3435 ** the error symbol.
3436 **
3437 ** * Set the error count to three.
3438 **
3439 ** * Begin accepting and shifting new tokens. No new error
3440 ** processing will occur until three tokens have been
3441 ** shifted successfully.
3442 **
3443 */
3444 if( yypParser->yyerrcnt<0 ){
3445 yy_syntax_error(yypParser,yymajor,yyminorunion);
3446 }
3447 yymx = yypParser->yystack[yypParser->yyidx].major;
3448 if( yymx==YYERRORSYMBOL || yyerrorhit ){
3449#ifndef NDEBUG
3450 if( yyTraceFILE ){
3451 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
3452 yyTracePrompt,yyTokenName[yymajor]);
3453 }
3454#endif
3455 yy_destructor(yymajor,&yyminorunion);
3456 yymajor = YYNOCODE;
3457 }else{
3458 while(
3459 yypParser->yyidx >= 0 &&
3460 yymx != YYERRORSYMBOL &&
3461 (yyact = yy_find_reduce_action(
3462 yypParser->yystack[yypParser->yyidx].stateno,
3463 YYERRORSYMBOL)) >= YYNSTATE
3464 ){
3465 yy_pop_parser_stack(yypParser);
3466 }
3467 if( yypParser->yyidx < 0 || yymajor==0 ){
3468 yy_destructor(yymajor,&yyminorunion);
3469 yy_parse_failed(yypParser);
3470 yymajor = YYNOCODE;
3471 }else if( yymx!=YYERRORSYMBOL ){
3472 YYMINORTYPE u2;
3473 u2.YYERRSYMDT = 0;
3474 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
3475 }
3476 }
3477 yypParser->yyerrcnt = 3;
3478 yyerrorhit = 1;
3479#else /* YYERRORSYMBOL is not defined */
3480 /* This is what we do if the grammar does not define ERROR:
3481 **
3482 ** * Report an error message, and throw away the input token.
3483 **
3484 ** * If the input token is $, then fail the parse.
3485 **
3486 ** As before, subsequent error messages are suppressed until
3487 ** three input tokens have been successfully shifted.
3488 */
3489 if( yypParser->yyerrcnt<=0 ){
3490 yy_syntax_error(yypParser,yymajor,yyminorunion);
3491 }
3492 yypParser->yyerrcnt = 3;
3493 yy_destructor(yymajor,&yyminorunion);
3494 if( yyendofinput ){
3495 yy_parse_failed(yypParser);
3496 }
3497 yymajor = YYNOCODE;
3498#endif
3499 }else{
3500 yy_accept(yypParser);
3501 yymajor = YYNOCODE;
3502 }
3503 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
3504 return;
3505}
diff --git a/libraries/sqlite/win32/parse.h b/libraries/sqlite/win32/parse.h
new file mode 100755
index 0000000..ed848ec
--- /dev/null
+++ b/libraries/sqlite/win32/parse.h
@@ -0,0 +1,152 @@
1#define TK_SEMI 1
2#define TK_EXPLAIN 2
3#define TK_QUERY 3
4#define TK_PLAN 4
5#define TK_BEGIN 5
6#define TK_TRANSACTION 6
7#define TK_DEFERRED 7
8#define TK_IMMEDIATE 8
9#define TK_EXCLUSIVE 9
10#define TK_COMMIT 10
11#define TK_END 11
12#define TK_ROLLBACK 12
13#define TK_CREATE 13
14#define TK_TABLE 14
15#define TK_IF 15
16#define TK_NOT 16
17#define TK_EXISTS 17
18#define TK_TEMP 18
19#define TK_LP 19
20#define TK_RP 20
21#define TK_AS 21
22#define TK_COMMA 22
23#define TK_ID 23
24#define TK_ABORT 24
25#define TK_AFTER 25
26#define TK_ANALYZE 26
27#define TK_ASC 27
28#define TK_ATTACH 28
29#define TK_BEFORE 29
30#define TK_CASCADE 30
31#define TK_CAST 31
32#define TK_CONFLICT 32
33#define TK_DATABASE 33
34#define TK_DESC 34
35#define TK_DETACH 35
36#define TK_EACH 36
37#define TK_FAIL 37
38#define TK_FOR 38
39#define TK_IGNORE 39
40#define TK_INITIALLY 40
41#define TK_INSTEAD 41
42#define TK_LIKE_KW 42
43#define TK_MATCH 43
44#define TK_KEY 44
45#define TK_OF 45
46#define TK_OFFSET 46
47#define TK_PRAGMA 47
48#define TK_RAISE 48
49#define TK_REPLACE 49
50#define TK_RESTRICT 50
51#define TK_ROW 51
52#define TK_TRIGGER 52
53#define TK_VACUUM 53
54#define TK_VIEW 54
55#define TK_VIRTUAL 55
56#define TK_REINDEX 56
57#define TK_RENAME 57
58#define TK_CTIME_KW 58
59#define TK_ANY 59
60#define TK_OR 60
61#define TK_AND 61
62#define TK_IS 62
63#define TK_BETWEEN 63
64#define TK_IN 64
65#define TK_ISNULL 65
66#define TK_NOTNULL 66
67#define TK_NE 67
68#define TK_EQ 68
69#define TK_GT 69
70#define TK_LE 70
71#define TK_LT 71
72#define TK_GE 72
73#define TK_ESCAPE 73
74#define TK_BITAND 74
75#define TK_BITOR 75
76#define TK_LSHIFT 76
77#define TK_RSHIFT 77
78#define TK_PLUS 78
79#define TK_MINUS 79
80#define TK_STAR 80
81#define TK_SLASH 81
82#define TK_REM 82
83#define TK_CONCAT 83
84#define TK_COLLATE 84
85#define TK_UMINUS 85
86#define TK_UPLUS 86
87#define TK_BITNOT 87
88#define TK_STRING 88
89#define TK_JOIN_KW 89
90#define TK_CONSTRAINT 90
91#define TK_DEFAULT 91
92#define TK_NULL 92
93#define TK_PRIMARY 93
94#define TK_UNIQUE 94
95#define TK_CHECK 95
96#define TK_REFERENCES 96
97#define TK_AUTOINCR 97
98#define TK_ON 98
99#define TK_DELETE 99
100#define TK_UPDATE 100
101#define TK_INSERT 101
102#define TK_SET 102
103#define TK_DEFERRABLE 103
104#define TK_FOREIGN 104
105#define TK_DROP 105
106#define TK_UNION 106
107#define TK_ALL 107
108#define TK_EXCEPT 108
109#define TK_INTERSECT 109
110#define TK_SELECT 110
111#define TK_DISTINCT 111
112#define TK_DOT 112
113#define TK_FROM 113
114#define TK_JOIN 114
115#define TK_USING 115
116#define TK_ORDER 116
117#define TK_BY 117
118#define TK_GROUP 118
119#define TK_HAVING 119
120#define TK_LIMIT 120
121#define TK_WHERE 121
122#define TK_INTO 122
123#define TK_VALUES 123
124#define TK_INTEGER 124
125#define TK_FLOAT 125
126#define TK_BLOB 126
127#define TK_REGISTER 127
128#define TK_VARIABLE 128
129#define TK_CASE 129
130#define TK_WHEN 130
131#define TK_THEN 131
132#define TK_ELSE 132
133#define TK_INDEX 133
134#define TK_ALTER 134
135#define TK_TO 135
136#define TK_ADD 136
137#define TK_COLUMNKW 137
138#define TK_TO_TEXT 138
139#define TK_TO_BLOB 139
140#define TK_TO_NUMERIC 140
141#define TK_TO_INT 141
142#define TK_TO_REAL 142
143#define TK_END_OF_FILE 143
144#define TK_ILLEGAL 144
145#define TK_SPACE 145
146#define TK_UNCLOSED_STRING 146
147#define TK_COMMENT 147
148#define TK_FUNCTION 148
149#define TK_COLUMN 149
150#define TK_AGG_FUNCTION 150
151#define TK_AGG_COLUMN 151
152#define TK_CONST_FUNC 152
diff --git a/libraries/sqlite/win32/pragma.c b/libraries/sqlite/win32/pragma.c
new file mode 100755
index 0000000..b4d9774
--- /dev/null
+++ b/libraries/sqlite/win32/pragma.c
@@ -0,0 +1,1186 @@
1/*
2** 2003 April 6
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.
10**
11*************************************************************************
12** This file contains code used to implement the PRAGMA command.
13**
14** $Id: pragma.c,v 1.149 2007/08/31 18:34:59 drh Exp $
15*/
16#include "sqliteInt.h"
17#include <ctype.h>
18
19/* Ignore this whole file if pragmas are disabled
20*/
21#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
22
23/*
24** Interpret the given string as a safety level. Return 0 for OFF,
25** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
26** unrecognized string argument.
27**
28** Note that the values returned are one less that the values that
29** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
30** to support legacy SQL code. The safety level used to be boolean
31** and older scripts may have used numbers 0 for OFF and 1 for ON.
32*/
33static int getSafetyLevel(const char *z){
34 /* 123456789 123456789 */
35 static const char zText[] = "onoffalseyestruefull";
36 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
37 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
38 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
39 int i, n;
40 if( isdigit(*z) ){
41 return atoi(z);
42 }
43 n = strlen(z);
44 for(i=0; i<sizeof(iLength); i++){
45 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
46 return iValue[i];
47 }
48 }
49 return 1;
50}
51
52/*
53** Interpret the given string as a boolean value.
54*/
55static int getBoolean(const char *z){
56 return getSafetyLevel(z)&1;
57}
58
59/*
60** Interpret the given string as a locking mode value.
61*/
62static int getLockingMode(const char *z){
63 if( z ){
64 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
66 }
67 return PAGER_LOCKINGMODE_QUERY;
68}
69
70#ifndef SQLITE_OMIT_AUTOVACUUM
71/*
72** Interpret the given string as an auto-vacuum mode value.
73**
74** The following strings, "none", "full" and "incremental" are
75** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
76*/
77static int getAutoVacuum(const char *z){
78 int i;
79 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
82 i = atoi(z);
83 return ((i>=0&&i<=2)?i:0);
84}
85#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
86
87#ifndef SQLITE_OMIT_PAGER_PRAGMAS
88/*
89** Interpret the given string as a temp db location. Return 1 for file
90** backed temporary databases, 2 for the Red-Black tree in memory database
91** and 0 to use the compile-time default.
92*/
93static int getTempStore(const char *z){
94 if( z[0]>='0' && z[0]<='2' ){
95 return z[0] - '0';
96 }else if( sqlite3StrICmp(z, "file")==0 ){
97 return 1;
98 }else if( sqlite3StrICmp(z, "memory")==0 ){
99 return 2;
100 }else{
101 return 0;
102 }
103}
104#endif /* SQLITE_PAGER_PRAGMAS */
105
106#ifndef SQLITE_OMIT_PAGER_PRAGMAS
107/*
108** Invalidate temp storage, either when the temp storage is changed
109** from default, or when 'file' and the temp_store_directory has changed
110*/
111static int invalidateTempStorage(Parse *pParse){
112 sqlite3 *db = pParse->db;
113 if( db->aDb[1].pBt!=0 ){
114 if( !db->autoCommit ){
115 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116 "from within a transaction");
117 return SQLITE_ERROR;
118 }
119 sqlite3BtreeClose(db->aDb[1].pBt);
120 db->aDb[1].pBt = 0;
121 sqlite3ResetInternalSchema(db, 0);
122 }
123 return SQLITE_OK;
124}
125#endif /* SQLITE_PAGER_PRAGMAS */
126
127#ifndef SQLITE_OMIT_PAGER_PRAGMAS
128/*
129** If the TEMP database is open, close it and mark the database schema
130** as needing reloading. This must be done when using the TEMP_STORE
131** or DEFAULT_TEMP_STORE pragmas.
132*/
133static int changeTempStorage(Parse *pParse, const char *zStorageType){
134 int ts = getTempStore(zStorageType);
135 sqlite3 *db = pParse->db;
136 if( db->temp_store==ts ) return SQLITE_OK;
137 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
138 return SQLITE_ERROR;
139 }
140 db->temp_store = ts;
141 return SQLITE_OK;
142}
143#endif /* SQLITE_PAGER_PRAGMAS */
144
145/*
146** Generate code to return a single integer value.
147*/
148static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
149 Vdbe *v = sqlite3GetVdbe(pParse);
150 sqlite3VdbeAddOp(v, OP_Integer, value, 0);
151 if( pParse->explain==0 ){
152 sqlite3VdbeSetNumCols(v, 1);
153 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC);
154 }
155 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
156}
157
158#ifndef SQLITE_OMIT_FLAG_PRAGMAS
159/*
160** Check to see if zRight and zLeft refer to a pragma that queries
161** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
162** Also, implement the pragma.
163*/
164static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
165 static const struct sPragmaType {
166 const char *zName; /* Name of the pragma */
167 int mask; /* Mask for the db->flags value */
168 } aPragma[] = {
169 { "full_column_names", SQLITE_FullColNames },
170 { "short_column_names", SQLITE_ShortColNames },
171 { "count_changes", SQLITE_CountRows },
172 { "empty_result_callbacks", SQLITE_NullCallback },
173 { "legacy_file_format", SQLITE_LegacyFileFmt },
174 { "fullfsync", SQLITE_FullFSync },
175#ifdef SQLITE_DEBUG
176 { "sql_trace", SQLITE_SqlTrace },
177 { "vdbe_listing", SQLITE_VdbeListing },
178 { "vdbe_trace", SQLITE_VdbeTrace },
179#endif
180#ifndef SQLITE_OMIT_CHECK
181 { "ignore_check_constraints", SQLITE_IgnoreChecks },
182#endif
183 /* The following is VERY experimental */
184 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
185 { "omit_readlock", SQLITE_NoReadlock },
186
187 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
188 ** flag if there are any active statements. */
189 { "read_uncommitted", SQLITE_ReadUncommitted },
190 };
191 int i;
192 const struct sPragmaType *p;
193 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
194 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
195 sqlite3 *db = pParse->db;
196 Vdbe *v;
197 v = sqlite3GetVdbe(pParse);
198 if( v ){
199 if( zRight==0 ){
200 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
201 }else{
202 if( getBoolean(zRight) ){
203 db->flags |= p->mask;
204 }else{
205 db->flags &= ~p->mask;
206 }
207 }
208 }
209 return 1;
210 }
211 }
212 return 0;
213}
214#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
215
216/*
217** Process a pragma statement.
218**
219** Pragmas are of this form:
220**
221** PRAGMA [database.]id [= value]
222**
223** The identifier might also be a string. The value is a string, and
224** identifier, or a number. If minusFlag is true, then the value is
225** a number that was preceded by a minus sign.
226**
227** If the left side is "database.id" then pId1 is the database name
228** and pId2 is the id. If the left side is just "id" then pId1 is the
229** id and pId2 is any empty string.
230*/
231void sqlite3Pragma(
232 Parse *pParse,
233 Token *pId1, /* First part of [database.]id field */
234 Token *pId2, /* Second part of [database.]id field, or NULL */
235 Token *pValue, /* Token for <value>, or NULL */
236 int minusFlag /* True if a '-' sign preceded <value> */
237){
238 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
239 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
240 const char *zDb = 0; /* The database name */
241 Token *pId; /* Pointer to <id> token */
242 int iDb; /* Database index for <database> */
243 sqlite3 *db = pParse->db;
244 Db *pDb;
245 Vdbe *v = sqlite3GetVdbe(pParse);
246 if( v==0 ) return;
247
248 /* Interpret the [database.] part of the pragma statement. iDb is the
249 ** index of the database this pragma is being applied to in db.aDb[]. */
250 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
251 if( iDb<0 ) return;
252 pDb = &db->aDb[iDb];
253
254 /* If the temp database has been explicitly named as part of the
255 ** pragma, make sure it is open.
256 */
257 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
258 return;
259 }
260
261 zLeft = sqlite3NameFromToken(db, pId);
262 if( !zLeft ) return;
263 if( minusFlag ){
264 zRight = sqlite3MPrintf(db, "-%T", pValue);
265 }else{
266 zRight = sqlite3NameFromToken(db, pValue);
267 }
268
269 zDb = ((iDb>0)?pDb->zName:0);
270 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
271 goto pragma_out;
272 }
273
274#ifndef SQLITE_OMIT_PAGER_PRAGMAS
275 /*
276 ** PRAGMA [database.]default_cache_size
277 ** PRAGMA [database.]default_cache_size=N
278 **
279 ** The first form reports the current persistent setting for the
280 ** page cache size. The value returned is the maximum number of
281 ** pages in the page cache. The second form sets both the current
282 ** page cache size value and the persistent page cache size value
283 ** stored in the database file.
284 **
285 ** The default cache size is stored in meta-value 2 of page 1 of the
286 ** database file. The cache size is actually the absolute value of
287 ** this memory location. The sign of meta-value 2 determines the
288 ** synchronous setting. A negative value means synchronous is off
289 ** and a positive value means synchronous is on.
290 */
291 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
292 static const VdbeOpList getCacheSize[] = {
293 { OP_ReadCookie, 0, 2, 0}, /* 0 */
294 { OP_AbsValue, 0, 0, 0},
295 { OP_Dup, 0, 0, 0},
296 { OP_Integer, 0, 0, 0},
297 { OP_Ne, 0, 6, 0},
298 { OP_Integer, 0, 0, 0}, /* 5 */
299 { OP_Callback, 1, 0, 0},
300 };
301 int addr;
302 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
303 sqlite3VdbeUsesBtree(v, iDb);
304 if( !zRight ){
305 sqlite3VdbeSetNumCols(v, 1);
306 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC);
307 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
308 sqlite3VdbeChangeP1(v, addr, iDb);
309 sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
310 }else{
311 int size = atoi(zRight);
312 if( size<0 ) size = -size;
313 sqlite3BeginWriteOperation(pParse, 0, iDb);
314 sqlite3VdbeAddOp(v, OP_Integer, size, 0);
315 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
316 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
317 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
318 sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
319 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
320 pDb->pSchema->cache_size = size;
321 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
322 }
323 }else
324
325 /*
326 ** PRAGMA [database.]page_size
327 ** PRAGMA [database.]page_size=N
328 **
329 ** The first form reports the current setting for the
330 ** database page size in bytes. The second form sets the
331 ** database page size value. The value can only be set if
332 ** the database has not yet been created.
333 */
334 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
335 Btree *pBt = pDb->pBt;
336 if( !zRight ){
337 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
338 returnSingleInt(pParse, "page_size", size);
339 }else{
340 /* Malloc may fail when setting the page-size, as there is an internal
341 ** buffer that the pager module resizes using sqlite3_realloc().
342 */
343 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1) ){
344 db->mallocFailed = 1;
345 }
346 }
347 }else
348
349 /*
350 ** PRAGMA [database.]max_page_count
351 ** PRAGMA [database.]max_page_count=N
352 **
353 ** The first form reports the current setting for the
354 ** maximum number of pages in the database file. The
355 ** second form attempts to change this setting. Both
356 ** forms return the current setting.
357 */
358 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
359 Btree *pBt = pDb->pBt;
360 int newMax = 0;
361 if( zRight ){
362 newMax = atoi(zRight);
363 }
364 if( pBt ){
365 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
366 }
367 returnSingleInt(pParse, "max_page_count", newMax);
368 }else
369
370 /*
371 ** PRAGMA [database.]locking_mode
372 ** PRAGMA [database.]locking_mode = (normal|exclusive)
373 */
374 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
375 const char *zRet = "normal";
376 int eMode = getLockingMode(zRight);
377
378 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
379 /* Simple "PRAGMA locking_mode;" statement. This is a query for
380 ** the current default locking mode (which may be different to
381 ** the locking-mode of the main database).
382 */
383 eMode = db->dfltLockMode;
384 }else{
385 Pager *pPager;
386 if( pId2->n==0 ){
387 /* This indicates that no database name was specified as part
388 ** of the PRAGMA command. In this case the locking-mode must be
389 ** set on all attached databases, as well as the main db file.
390 **
391 ** Also, the sqlite3.dfltLockMode variable is set so that
392 ** any subsequently attached databases also use the specified
393 ** locking mode.
394 */
395 int ii;
396 assert(pDb==&db->aDb[0]);
397 for(ii=2; ii<db->nDb; ii++){
398 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
399 sqlite3PagerLockingMode(pPager, eMode);
400 }
401 db->dfltLockMode = eMode;
402 }
403 pPager = sqlite3BtreePager(pDb->pBt);
404 eMode = sqlite3PagerLockingMode(pPager, eMode);
405 }
406
407 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
408 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
409 zRet = "exclusive";
410 }
411 sqlite3VdbeSetNumCols(v, 1);
412 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P3_STATIC);
413 sqlite3VdbeOp3(v, OP_String8, 0, 0, zRet, 0);
414 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
415 }else
416#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
417
418 /*
419 ** PRAGMA [database.]auto_vacuum
420 ** PRAGMA [database.]auto_vacuum=N
421 **
422 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
423 */
424#ifndef SQLITE_OMIT_AUTOVACUUM
425 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
426 Btree *pBt = pDb->pBt;
427 if( sqlite3ReadSchema(pParse) ){
428 goto pragma_out;
429 }
430 if( !zRight ){
431 int auto_vacuum =
432 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
433 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
434 }else{
435 int eAuto = getAutoVacuum(zRight);
436 if( eAuto>=0 ){
437 /* Call SetAutoVacuum() to set initialize the internal auto and
438 ** incr-vacuum flags. This is required in case this connection
439 ** creates the database file. It is important that it is created
440 ** as an auto-vacuum capable db.
441 */
442 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
443 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
444 /* When setting the auto_vacuum mode to either "full" or
445 ** "incremental", write the value of meta[6] in the database
446 ** file. Before writing to meta[6], check that meta[3] indicates
447 ** that this really is an auto-vacuum capable database.
448 */
449 static const VdbeOpList setMeta6[] = {
450 { OP_Transaction, 0, 1, 0}, /* 0 */
451 { OP_ReadCookie, 0, 3, 0}, /* 1 */
452 { OP_If, 0, 0, 0}, /* 2 */
453 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
454 { OP_Integer, 0, 0, 0}, /* 4 */
455 { OP_SetCookie, 0, 6, 0}, /* 5 */
456 };
457 int iAddr;
458 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
459 sqlite3VdbeChangeP1(v, iAddr, iDb);
460 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
461 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
462 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
463 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
464 sqlite3VdbeUsesBtree(v, iDb);
465 }
466 }
467 }
468 }else
469#endif
470
471 /*
472 ** PRAGMA [database.]incremental_vacuum(N)
473 **
474 ** Do N steps of incremental vacuuming on a database.
475 */
476#ifndef SQLITE_OMIT_AUTOVACUUM
477 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
478 int iLimit, addr;
479 if( sqlite3ReadSchema(pParse) ){
480 goto pragma_out;
481 }
482 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
483 iLimit = 0x7fffffff;
484 }
485 sqlite3BeginWriteOperation(pParse, 0, iDb);
486 sqlite3VdbeAddOp(v, OP_MemInt, iLimit, 0);
487 addr = sqlite3VdbeAddOp(v, OP_IncrVacuum, iDb, 0);
488 sqlite3VdbeAddOp(v, OP_Callback, 0, 0);
489 sqlite3VdbeAddOp(v, OP_MemIncr, -1, 0);
490 sqlite3VdbeAddOp(v, OP_IfMemPos, 0, addr);
491 sqlite3VdbeJumpHere(v, addr);
492 }else
493#endif
494
495#ifndef SQLITE_OMIT_PAGER_PRAGMAS
496 /*
497 ** PRAGMA [database.]cache_size
498 ** PRAGMA [database.]cache_size=N
499 **
500 ** The first form reports the current local setting for the
501 ** page cache size. The local setting can be different from
502 ** the persistent cache size value that is stored in the database
503 ** file itself. The value returned is the maximum number of
504 ** pages in the page cache. The second form sets the local
505 ** page cache size value. It does not change the persistent
506 ** cache size stored on the disk so the cache size will revert
507 ** to its default value when the database is closed and reopened.
508 ** N should be a positive integer.
509 */
510 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
511 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
512 if( !zRight ){
513 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
514 }else{
515 int size = atoi(zRight);
516 if( size<0 ) size = -size;
517 pDb->pSchema->cache_size = size;
518 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
519 }
520 }else
521
522 /*
523 ** PRAGMA temp_store
524 ** PRAGMA temp_store = "default"|"memory"|"file"
525 **
526 ** Return or set the local value of the temp_store flag. Changing
527 ** the local value does not make changes to the disk file and the default
528 ** value will be restored the next time the database is opened.
529 **
530 ** Note that it is possible for the library compile-time options to
531 ** override this setting
532 */
533 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
534 if( !zRight ){
535 returnSingleInt(pParse, "temp_store", db->temp_store);
536 }else{
537 changeTempStorage(pParse, zRight);
538 }
539 }else
540
541 /*
542 ** PRAGMA temp_store_directory
543 ** PRAGMA temp_store_directory = ""|"directory_name"
544 **
545 ** Return or set the local value of the temp_store_directory flag. Changing
546 ** the value sets a specific directory to be used for temporary files.
547 ** Setting to a null string reverts to the default temporary directory search.
548 ** If temporary directory is changed, then invalidateTempStorage.
549 **
550 */
551 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
552 if( !zRight ){
553 if( sqlite3_temp_directory ){
554 sqlite3VdbeSetNumCols(v, 1);
555 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
556 "temp_store_directory", P3_STATIC);
557 sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
558 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
559 }
560 }else{
561 if( zRight[0]
562 && !sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE)
563 ){
564 sqlite3ErrorMsg(pParse, "not a writable directory");
565 goto pragma_out;
566 }
567 if( TEMP_STORE==0
568 || (TEMP_STORE==1 && db->temp_store<=1)
569 || (TEMP_STORE==2 && db->temp_store==1)
570 ){
571 invalidateTempStorage(pParse);
572 }
573 sqlite3_free(sqlite3_temp_directory);
574 if( zRight[0] ){
575 sqlite3_temp_directory = zRight;
576 zRight = 0;
577 }else{
578 sqlite3_temp_directory = 0;
579 }
580 }
581 }else
582
583 /*
584 ** PRAGMA [database.]synchronous
585 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
586 **
587 ** Return or set the local value of the synchronous flag. Changing
588 ** the local value does not make changes to the disk file and the
589 ** default value will be restored the next time the database is
590 ** opened.
591 */
592 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
593 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
594 if( !zRight ){
595 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
596 }else{
597 if( !db->autoCommit ){
598 sqlite3ErrorMsg(pParse,
599 "Safety level may not be changed inside a transaction");
600 }else{
601 pDb->safety_level = getSafetyLevel(zRight)+1;
602 }
603 }
604 }else
605#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
606
607#ifndef SQLITE_OMIT_FLAG_PRAGMAS
608 if( flagPragma(pParse, zLeft, zRight) ){
609 /* The flagPragma() subroutine also generates any necessary code
610 ** there is nothing more to do here */
611 }else
612#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
613
614#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
615 /*
616 ** PRAGMA table_info(<table>)
617 **
618 ** Return a single row for each column of the named table. The columns of
619 ** the returned data set are:
620 **
621 ** cid: Column id (numbered from left to right, starting at 0)
622 ** name: Column name
623 ** type: Column declaration type.
624 ** notnull: True if 'NOT NULL' is part of column declaration
625 ** dflt_value: The default value for the column, if any.
626 */
627 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
628 Table *pTab;
629 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
630 pTab = sqlite3FindTable(db, zRight, zDb);
631 if( pTab ){
632 int i;
633 int nHidden = 0;
634 Column *pCol;
635 sqlite3VdbeSetNumCols(v, 6);
636 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC);
637 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
638 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC);
639 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC);
640 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC);
641 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC);
642 sqlite3ViewGetColumnNames(pParse, pTab);
643 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
644 const Token *pDflt;
645 if( IsHiddenColumn(pCol) ){
646 nHidden++;
647 continue;
648 }
649 sqlite3VdbeAddOp(v, OP_Integer, i-nHidden, 0);
650 sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0);
651 sqlite3VdbeOp3(v, OP_String8, 0, 0,
652 pCol->zType ? pCol->zType : "", 0);
653 sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0);
654 if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
655 sqlite3VdbeOp3(v, OP_String8, 0, 0, (char*)pDflt->z, pDflt->n);
656 }else{
657 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
658 }
659 sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0);
660 sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
661 }
662 }
663 }else
664
665 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
666 Index *pIdx;
667 Table *pTab;
668 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
669 pIdx = sqlite3FindIndex(db, zRight, zDb);
670 if( pIdx ){
671 int i;
672 pTab = pIdx->pTable;
673 sqlite3VdbeSetNumCols(v, 3);
674 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC);
675 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC);
676 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC);
677 for(i=0; i<pIdx->nColumn; i++){
678 int cnum = pIdx->aiColumn[i];
679 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
680 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
681 assert( pTab->nCol>cnum );
682 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
683 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
684 }
685 }
686 }else
687
688 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
689 Index *pIdx;
690 Table *pTab;
691 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
692 pTab = sqlite3FindTable(db, zRight, zDb);
693 if( pTab ){
694 v = sqlite3GetVdbe(pParse);
695 pIdx = pTab->pIndex;
696 if( pIdx ){
697 int i = 0;
698 sqlite3VdbeSetNumCols(v, 3);
699 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
700 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
701 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC);
702 while(pIdx){
703 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
704 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
705 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
706 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
707 ++i;
708 pIdx = pIdx->pNext;
709 }
710 }
711 }
712 }else
713
714 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
715 int i;
716 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
717 sqlite3VdbeSetNumCols(v, 3);
718 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
719 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
720 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC);
721 for(i=0; i<db->nDb; i++){
722 if( db->aDb[i].pBt==0 ) continue;
723 assert( db->aDb[i].zName!=0 );
724 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
725 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
726 sqlite3VdbeOp3(v, OP_String8, 0, 0,
727 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
728 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
729 }
730 }else
731
732 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
733 int i = 0;
734 HashElem *p;
735 sqlite3VdbeSetNumCols(v, 2);
736 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
737 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
738 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
739 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
740 sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
741 sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
742 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
743 }
744 }else
745#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
746
747#ifndef SQLITE_OMIT_FOREIGN_KEY
748 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
749 FKey *pFK;
750 Table *pTab;
751 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
752 pTab = sqlite3FindTable(db, zRight, zDb);
753 if( pTab ){
754 v = sqlite3GetVdbe(pParse);
755 pFK = pTab->pFKey;
756 if( pFK ){
757 int i = 0;
758 sqlite3VdbeSetNumCols(v, 5);
759 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC);
760 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC);
761 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC);
762 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC);
763 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC);
764 while(pFK){
765 int j;
766 for(j=0; j<pFK->nCol; j++){
767 char *zCol = pFK->aCol[j].zCol;
768 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
769 sqlite3VdbeAddOp(v, OP_Integer, j, 0);
770 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
771 sqlite3VdbeOp3(v, OP_String8, 0, 0,
772 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
773 sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0);
774 sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
775 }
776 ++i;
777 pFK = pFK->pNextFrom;
778 }
779 }
780 }
781 }else
782#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
783
784#ifndef NDEBUG
785 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
786 if( zRight ){
787 if( getBoolean(zRight) ){
788 sqlite3ParserTrace(stderr, "parser: ");
789 }else{
790 sqlite3ParserTrace(0, 0);
791 }
792 }
793 }else
794#endif
795
796 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
797 ** used will be case sensitive or not depending on the RHS.
798 */
799 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
800 if( zRight ){
801 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
802 }
803 }else
804
805#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
806# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
807#endif
808
809#ifndef SQLITE_OMIT_INTEGRITY_CHECK
810 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
811 int i, j, addr, mxErr;
812
813 /* Code that appears at the end of the integrity check. If no error
814 ** messages have been generated, output OK. Otherwise output the
815 ** error message
816 */
817 static const VdbeOpList endCode[] = {
818 { OP_MemLoad, 0, 0, 0},
819 { OP_Integer, 0, 0, 0},
820 { OP_Ne, 0, 0, 0}, /* 2 */
821 { OP_String8, 0, 0, "ok"},
822 { OP_Callback, 1, 0, 0},
823 };
824
825 /* Initialize the VDBE program */
826 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
827 sqlite3VdbeSetNumCols(v, 1);
828 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC);
829
830 /* Set the maximum error count */
831 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
832 if( zRight ){
833 mxErr = atoi(zRight);
834 if( mxErr<=0 ){
835 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
836 }
837 }
838 sqlite3VdbeAddOp(v, OP_MemInt, mxErr, 0);
839
840 /* Do an integrity check on each database file */
841 for(i=0; i<db->nDb; i++){
842 HashElem *x;
843 Hash *pTbls;
844 int cnt = 0;
845
846 if( OMIT_TEMPDB && i==1 ) continue;
847
848 sqlite3CodeVerifySchema(pParse, i);
849 addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
850 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
851 sqlite3VdbeJumpHere(v, addr);
852
853 /* Do an integrity check of the B-Tree
854 */
855 pTbls = &db->aDb[i].pSchema->tblHash;
856 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
857 Table *pTab = sqliteHashData(x);
858 Index *pIdx;
859 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
860 cnt++;
861 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
862 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
863 cnt++;
864 }
865 }
866 if( cnt==0 ) continue;
867 sqlite3VdbeAddOp(v, OP_IntegrityCk, 0, i);
868 addr = sqlite3VdbeAddOp(v, OP_IsNull, -1, 0);
869 sqlite3VdbeOp3(v, OP_String8, 0, 0,
870 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
871 P3_DYNAMIC);
872 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
873 sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
874 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
875 sqlite3VdbeJumpHere(v, addr);
876
877 /* Make sure all the indices are constructed correctly.
878 */
879 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
880 Table *pTab = sqliteHashData(x);
881 Index *pIdx;
882 int loopTop;
883
884 if( pTab->pIndex==0 ) continue;
885 addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
886 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
887 sqlite3VdbeJumpHere(v, addr);
888 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
889 sqlite3VdbeAddOp(v, OP_MemInt, 0, 1);
890 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
891 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1);
892 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
893 int jmp2;
894 static const VdbeOpList idxErr[] = {
895 { OP_MemIncr, -1, 0, 0},
896 { OP_String8, 0, 0, "rowid "},
897 { OP_Rowid, 1, 0, 0},
898 { OP_String8, 0, 0, " missing from index "},
899 { OP_String8, 0, 0, 0}, /* 4 */
900 { OP_Concat, 2, 0, 0},
901 { OP_Callback, 1, 0, 0},
902 };
903 sqlite3GenerateIndexKey(v, pIdx, 1);
904 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
905 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
906 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
907 sqlite3VdbeJumpHere(v, jmp2);
908 }
909 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
910 sqlite3VdbeJumpHere(v, loopTop);
911 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
912 static const VdbeOpList cntIdx[] = {
913 { OP_MemInt, 0, 2, 0},
914 { OP_Rewind, 0, 0, 0}, /* 1 */
915 { OP_MemIncr, 1, 2, 0},
916 { OP_Next, 0, 0, 0}, /* 3 */
917 { OP_MemLoad, 1, 0, 0},
918 { OP_MemLoad, 2, 0, 0},
919 { OP_Eq, 0, 0, 0}, /* 6 */
920 { OP_MemIncr, -1, 0, 0},
921 { OP_String8, 0, 0, "wrong # of entries in index "},
922 { OP_String8, 0, 0, 0}, /* 9 */
923 { OP_Concat, 0, 0, 0},
924 { OP_Callback, 1, 0, 0},
925 };
926 if( pIdx->tnum==0 ) continue;
927 addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
928 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
929 sqlite3VdbeJumpHere(v, addr);
930 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
931 sqlite3VdbeChangeP1(v, addr+1, j+2);
932 sqlite3VdbeChangeP2(v, addr+1, addr+4);
933 sqlite3VdbeChangeP1(v, addr+3, j+2);
934 sqlite3VdbeChangeP2(v, addr+3, addr+2);
935 sqlite3VdbeJumpHere(v, addr+6);
936 sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC);
937 }
938 }
939 }
940 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
941 sqlite3VdbeChangeP1(v, addr+1, mxErr);
942 sqlite3VdbeJumpHere(v, addr+2);
943 }else
944#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
945
946#ifndef SQLITE_OMIT_UTF16
947 /*
948 ** PRAGMA encoding
949 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
950 **
951 ** In it's first form, this pragma returns the encoding of the main
952 ** database. If the database is not initialized, it is initialized now.
953 **
954 ** The second form of this pragma is a no-op if the main database file
955 ** has not already been initialized. In this case it sets the default
956 ** encoding that will be used for the main database file if a new file
957 ** is created. If an existing main database file is opened, then the
958 ** default text encoding for the existing database is used.
959 **
960 ** In all cases new databases created using the ATTACH command are
961 ** created to use the same default text encoding as the main database. If
962 ** the main database has not been initialized and/or created when ATTACH
963 ** is executed, this is done before the ATTACH operation.
964 **
965 ** In the second form this pragma sets the text encoding to be used in
966 ** new database files created using this database handle. It is only
967 ** useful if invoked immediately after the main database i
968 */
969 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
970 static const struct EncName {
971 char *zName;
972 u8 enc;
973 } encnames[] = {
974 { "UTF-8", SQLITE_UTF8 },
975 { "UTF8", SQLITE_UTF8 },
976 { "UTF-16le", SQLITE_UTF16LE },
977 { "UTF16le", SQLITE_UTF16LE },
978 { "UTF-16be", SQLITE_UTF16BE },
979 { "UTF16be", SQLITE_UTF16BE },
980 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
981 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
982 { 0, 0 }
983 };
984 const struct EncName *pEnc;
985 if( !zRight ){ /* "PRAGMA encoding" */
986 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
987 sqlite3VdbeSetNumCols(v, 1);
988 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC);
989 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
990 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
991 if( pEnc->enc==ENC(pParse->db) ){
992 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
993 break;
994 }
995 }
996 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
997 }else{ /* "PRAGMA encoding = XXX" */
998 /* Only change the value of sqlite.enc if the database handle is not
999 ** initialized. If the main database exists, the new sqlite.enc value
1000 ** will be overwritten when the schema is next loaded. If it does not
1001 ** already exists, it will be created to use the new encoding value.
1002 */
1003 if(
1004 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1005 DbHasProperty(db, 0, DB_Empty)
1006 ){
1007 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1008 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
1009 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1010 break;
1011 }
1012 }
1013 if( !pEnc->zName ){
1014 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
1015 }
1016 }
1017 }
1018 }else
1019#endif /* SQLITE_OMIT_UTF16 */
1020
1021#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
1022 /*
1023 ** PRAGMA [database.]schema_version
1024 ** PRAGMA [database.]schema_version = <integer>
1025 **
1026 ** PRAGMA [database.]user_version
1027 ** PRAGMA [database.]user_version = <integer>
1028 **
1029 ** The pragma's schema_version and user_version are used to set or get
1030 ** the value of the schema-version and user-version, respectively. Both
1031 ** the schema-version and the user-version are 32-bit signed integers
1032 ** stored in the database header.
1033 **
1034 ** The schema-cookie is usually only manipulated internally by SQLite. It
1035 ** is incremented by SQLite whenever the database schema is modified (by
1036 ** creating or dropping a table or index). The schema version is used by
1037 ** SQLite each time a query is executed to ensure that the internal cache
1038 ** of the schema used when compiling the SQL query matches the schema of
1039 ** the database against which the compiled query is actually executed.
1040 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1041 ** the schema-version is potentially dangerous and may lead to program
1042 ** crashes or database corruption. Use with caution!
1043 **
1044 ** The user-version is not used internally by SQLite. It may be used by
1045 ** applications for any purpose.
1046 */
1047 if( sqlite3StrICmp(zLeft, "schema_version")==0
1048 || sqlite3StrICmp(zLeft, "user_version")==0
1049 || sqlite3StrICmp(zLeft, "freelist_count")==0
1050 ){
1051
1052 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
1053 sqlite3VdbeUsesBtree(v, iDb);
1054 switch( zLeft[0] ){
1055 case 's': case 'S':
1056 iCookie = 0;
1057 break;
1058 case 'f': case 'F':
1059 iCookie = 1;
1060 iDb = (-1*(iDb+1));
1061 assert(iDb<=0);
1062 break;
1063 default:
1064 iCookie = 5;
1065 break;
1066 }
1067
1068 if( zRight && iDb>=0 ){
1069 /* Write the specified cookie value */
1070 static const VdbeOpList setCookie[] = {
1071 { OP_Transaction, 0, 1, 0}, /* 0 */
1072 { OP_Integer, 0, 0, 0}, /* 1 */
1073 { OP_SetCookie, 0, 0, 0}, /* 2 */
1074 };
1075 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1076 sqlite3VdbeChangeP1(v, addr, iDb);
1077 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1078 sqlite3VdbeChangeP1(v, addr+2, iDb);
1079 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1080 }else{
1081 /* Read the specified cookie value */
1082 static const VdbeOpList readCookie[] = {
1083 { OP_ReadCookie, 0, 0, 0}, /* 0 */
1084 { OP_Callback, 1, 0, 0}
1085 };
1086 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1087 sqlite3VdbeChangeP1(v, addr, iDb);
1088 sqlite3VdbeChangeP2(v, addr, iCookie);
1089 sqlite3VdbeSetNumCols(v, 1);
1090 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P3_TRANSIENT);
1091 }
1092 }else
1093#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
1094
1095#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1096 /*
1097 ** Report the current state of file logs for all databases
1098 */
1099 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
1100 static const char *const azLockName[] = {
1101 "unlocked", "shared", "reserved", "pending", "exclusive"
1102 };
1103 int i;
1104 Vdbe *v = sqlite3GetVdbe(pParse);
1105 sqlite3VdbeSetNumCols(v, 2);
1106 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC);
1107 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC);
1108 for(i=0; i<db->nDb; i++){
1109 Btree *pBt;
1110 Pager *pPager;
1111 const char *zState = "unknown";
1112 int j;
1113 if( db->aDb[i].zName==0 ) continue;
1114 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
1115 pBt = db->aDb[i].pBt;
1116 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
1117 zState = "closed";
1118 }else if( sqlite3_file_control(db, db->aDb[i].zName,
1119 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1120 zState = azLockName[j];
1121 }
1122 sqlite3VdbeOp3(v, OP_String8, 0, 0, zState, P3_STATIC);
1123 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
1124 }
1125 }else
1126#endif
1127
1128#ifdef SQLITE_SSE
1129 /*
1130 ** Check to see if the sqlite_statements table exists. Create it
1131 ** if it does not.
1132 */
1133 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
1134 extern int sqlite3CreateStatementsTable(Parse*);
1135 sqlite3CreateStatementsTable(pParse);
1136 }else
1137#endif
1138
1139#if SQLITE_HAS_CODEC
1140 if( sqlite3StrICmp(zLeft, "key")==0 ){
1141 sqlite3_key(db, zRight, strlen(zRight));
1142 }else
1143#endif
1144#if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1145 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1146#if SQLITE_HAS_CODEC
1147 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1148 extern void sqlite3_activate_see(const char*);
1149 sqlite3_activate_see(&zRight[4]);
1150 }
1151#endif
1152#ifdef SQLITE_ENABLE_CEROD
1153 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1154 extern void sqlite3_activate_cerod(const char*);
1155 sqlite3_activate_cerod(&zRight[6]);
1156 }
1157#endif
1158 }
1159#endif
1160
1161 {}
1162
1163 if( v ){
1164 /* Code an OP_Expire at the end of each PRAGMA program to cause
1165 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1166 ** are only valid for a single execution.
1167 */
1168 sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
1169
1170 /*
1171 ** Reset the safety level, in case the fullfsync flag or synchronous
1172 ** setting changed.
1173 */
1174#ifndef SQLITE_OMIT_PAGER_PRAGMAS
1175 if( db->autoCommit ){
1176 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1177 (db->flags&SQLITE_FullFSync)!=0);
1178 }
1179#endif
1180 }
1181pragma_out:
1182 sqlite3_free(zLeft);
1183 sqlite3_free(zRight);
1184}
1185
1186#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */
diff --git a/libraries/sqlite/win32/prepare.c b/libraries/sqlite/win32/prepare.c
new file mode 100755
index 0000000..a1a0a40
--- /dev/null
+++ b/libraries/sqlite/win32/prepare.c
@@ -0,0 +1,742 @@
1/*
2** 2005 May 25
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.
10**
11*************************************************************************
12** This file contains the implementation of the sqlite3_prepare()
13** interface, and routines that contribute to loading the database schema
14** from disk.
15**
16** $Id: prepare.c,v 1.61 2007/10/03 08:46:45 danielk1977 Exp $
17*/
18#include "sqliteInt.h"
19#include <ctype.h>
20
21/*
22** Fill the InitData structure with an error message that indicates
23** that the database is corrupt.
24*/
25static void corruptSchema(InitData *pData, const char *zExtra){
26 if( !pData->db->mallocFailed ){
27 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
28 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
29 }
30 pData->rc = SQLITE_CORRUPT;
31}
32
33/*
34** This is the callback routine for the code that initializes the
35** database. See sqlite3Init() below for additional information.
36** This routine is also called from the OP_ParseSchema opcode of the VDBE.
37**
38** Each callback contains the following information:
39**
40** argv[0] = name of thing being created
41** argv[1] = root page number for table or index. 0 for trigger or view.
42** argv[2] = SQL text for the CREATE statement.
43**
44*/
45int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
46 InitData *pData = (InitData*)pInit;
47 sqlite3 *db = pData->db;
48 int iDb = pData->iDb;
49
50 assert( sqlite3_mutex_held(db->mutex) );
51 pData->rc = SQLITE_OK;
52 DbClearProperty(db, iDb, DB_Empty);
53 if( db->mallocFailed ){
54 corruptSchema(pData, 0);
55 return SQLITE_NOMEM;
56 }
57
58 assert( argc==3 );
59 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
60 if( argv[1]==0 ){
61 corruptSchema(pData, 0);
62 return 1;
63 }
64 assert( iDb>=0 && iDb<db->nDb );
65 if( argv[2] && argv[2][0] ){
66 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
67 ** But because db->init.busy is set to 1, no VDBE code is generated
68 ** or executed. All the parser does is build the internal data
69 ** structures that describe the table, index, or view.
70 */
71 char *zErr;
72 int rc;
73 assert( db->init.busy );
74 db->init.iDb = iDb;
75 db->init.newTnum = atoi(argv[1]);
76 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
77 db->init.iDb = 0;
78 assert( rc!=SQLITE_OK || zErr==0 );
79 if( SQLITE_OK!=rc ){
80 pData->rc = rc;
81 if( rc==SQLITE_NOMEM ){
82 db->mallocFailed = 1;
83 }else if( rc!=SQLITE_INTERRUPT ){
84 corruptSchema(pData, zErr);
85 }
86 sqlite3_free(zErr);
87 return 1;
88 }
89 }else{
90 /* If the SQL column is blank it means this is an index that
91 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
92 ** constraint for a CREATE TABLE. The index should have already
93 ** been created when we processed the CREATE TABLE. All we have
94 ** to do here is record the root page number for that index.
95 */
96 Index *pIndex;
97 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
98 if( pIndex==0 || pIndex->tnum!=0 ){
99 /* This can occur if there exists an index on a TEMP table which
100 ** has the same name as another index on a permanent index. Since
101 ** the permanent table is hidden by the TEMP table, we can also
102 ** safely ignore the index on the permanent table.
103 */
104 /* Do Nothing */;
105 }else{
106 pIndex->tnum = atoi(argv[1]);
107 }
108 }
109 return 0;
110}
111
112/*
113** Attempt to read the database schema and initialize internal
114** data structures for a single database file. The index of the
115** database file is given by iDb. iDb==0 is used for the main
116** database. iDb==1 should never be used. iDb>=2 is used for
117** auxiliary databases. Return one of the SQLITE_ error codes to
118** indicate success or failure.
119*/
120static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
121 int rc;
122 BtCursor *curMain;
123 int size;
124 Table *pTab;
125 Db *pDb;
126 char const *azArg[4];
127 int meta[10];
128 InitData initData;
129 char const *zMasterSchema;
130 char const *zMasterName = SCHEMA_TABLE(iDb);
131
132 /*
133 ** The master database table has a structure like this
134 */
135 static const char master_schema[] =
136 "CREATE TABLE sqlite_master(\n"
137 " type text,\n"
138 " name text,\n"
139 " tbl_name text,\n"
140 " rootpage integer,\n"
141 " sql text\n"
142 ")"
143 ;
144#ifndef SQLITE_OMIT_TEMPDB
145 static const char temp_master_schema[] =
146 "CREATE TEMP TABLE sqlite_temp_master(\n"
147 " type text,\n"
148 " name text,\n"
149 " tbl_name text,\n"
150 " rootpage integer,\n"
151 " sql text\n"
152 ")"
153 ;
154#else
155 #define temp_master_schema 0
156#endif
157
158 assert( iDb>=0 && iDb<db->nDb );
159 assert( db->aDb[iDb].pSchema );
160 assert( sqlite3_mutex_held(db->mutex) );
161
162 /* zMasterSchema and zInitScript are set to point at the master schema
163 ** and initialisation script appropriate for the database being
164 ** initialised. zMasterName is the name of the master table.
165 */
166 if( !OMIT_TEMPDB && iDb==1 ){
167 zMasterSchema = temp_master_schema;
168 }else{
169 zMasterSchema = master_schema;
170 }
171 zMasterName = SCHEMA_TABLE(iDb);
172
173 /* Construct the schema tables. */
174 sqlite3SafetyOff(db);
175 azArg[0] = zMasterName;
176 azArg[1] = "1";
177 azArg[2] = zMasterSchema;
178 azArg[3] = 0;
179 initData.db = db;
180 initData.iDb = iDb;
181 initData.pzErrMsg = pzErrMsg;
182 rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
183 if( rc ){
184 sqlite3SafetyOn(db);
185 rc = initData.rc;
186 goto error_out;
187 }
188 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
189 if( pTab ){
190 pTab->readOnly = 1;
191 }
192 sqlite3SafetyOn(db);
193
194 /* Create a cursor to hold the database open
195 */
196 pDb = &db->aDb[iDb];
197 if( pDb->pBt==0 ){
198 if( !OMIT_TEMPDB && iDb==1 ){
199 DbSetProperty(db, 1, DB_SchemaLoaded);
200 }
201 return SQLITE_OK;
202 }
203 sqlite3BtreeEnter(pDb->pBt);
204 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
205 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
206 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
207 sqlite3BtreeLeave(pDb->pBt);
208 goto error_out;
209 }
210
211 /* Get the database meta information.
212 **
213 ** Meta values are as follows:
214 ** meta[0] Schema cookie. Changes with each schema change.
215 ** meta[1] File format of schema layer.
216 ** meta[2] Size of the page cache.
217 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
218 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
219 ** meta[5] The user cookie. Used by the application.
220 ** meta[6] Incremental-vacuum flag.
221 ** meta[7]
222 ** meta[8]
223 ** meta[9]
224 **
225 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
226 ** the possible values of meta[4].
227 */
228 if( rc==SQLITE_OK ){
229 int i;
230 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
231 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
232 }
233 if( rc ){
234 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
235 sqlite3BtreeCloseCursor(curMain);
236 sqlite3BtreeLeave(pDb->pBt);
237 goto error_out;
238 }
239 }else{
240 memset(meta, 0, sizeof(meta));
241 }
242 pDb->pSchema->schema_cookie = meta[0];
243
244 /* If opening a non-empty database, check the text encoding. For the
245 ** main database, set sqlite3.enc to the encoding of the main database.
246 ** For an attached db, it is an error if the encoding is not the same
247 ** as sqlite3.enc.
248 */
249 if( meta[4] ){ /* text encoding */
250 if( iDb==0 ){
251 /* If opening the main database, set ENC(db). */
252 ENC(db) = (u8)meta[4];
253 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
254 }else{
255 /* If opening an attached database, the encoding much match ENC(db) */
256 if( meta[4]!=ENC(db) ){
257 sqlite3BtreeCloseCursor(curMain);
258 sqlite3SetString(pzErrMsg, "attached databases must use the same"
259 " text encoding as main database", (char*)0);
260 sqlite3BtreeLeave(pDb->pBt);
261 return SQLITE_ERROR;
262 }
263 }
264 }else{
265 DbSetProperty(db, iDb, DB_Empty);
266 }
267 pDb->pSchema->enc = ENC(db);
268
269 size = meta[2];
270 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
271 pDb->pSchema->cache_size = size;
272 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
273
274 /*
275 ** file_format==1 Version 3.0.0.
276 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
277 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
278 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
279 */
280 pDb->pSchema->file_format = meta[1];
281 if( pDb->pSchema->file_format==0 ){
282 pDb->pSchema->file_format = 1;
283 }
284 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
285 sqlite3BtreeCloseCursor(curMain);
286 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
287 sqlite3BtreeLeave(pDb->pBt);
288 return SQLITE_ERROR;
289 }
290
291
292 /* Read the schema information out of the schema tables
293 */
294 assert( db->init.busy );
295 if( rc==SQLITE_EMPTY ){
296 /* For an empty database, there is nothing to read */
297 rc = SQLITE_OK;
298 }else{
299 char *zSql;
300 zSql = sqlite3MPrintf(db,
301 "SELECT name, rootpage, sql FROM '%q'.%s",
302 db->aDb[iDb].zName, zMasterName);
303 sqlite3SafetyOff(db);
304 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
305 if( rc==SQLITE_ABORT ) rc = initData.rc;
306 sqlite3SafetyOn(db);
307 sqlite3_free(zSql);
308#ifndef SQLITE_OMIT_ANALYZE
309 if( rc==SQLITE_OK ){
310 sqlite3AnalysisLoad(db, iDb);
311 }
312#endif
313 sqlite3BtreeCloseCursor(curMain);
314 }
315 if( db->mallocFailed ){
316 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
317 rc = SQLITE_NOMEM;
318 sqlite3ResetInternalSchema(db, 0);
319 }
320 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
321 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
322 ** the schema loaded, even if errors occured. In this situation the
323 ** current sqlite3_prepare() operation will fail, but the following one
324 ** will attempt to compile the supplied statement against whatever subset
325 ** of the schema was loaded before the error occured. The primary
326 ** purpose of this is to allow access to the sqlite_master table
327 ** even when it's contents have been corrupted.
328 */
329 DbSetProperty(db, iDb, DB_SchemaLoaded);
330 rc = SQLITE_OK;
331 }
332 sqlite3BtreeLeave(pDb->pBt);
333
334error_out:
335 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
336 db->mallocFailed = 1;
337 }
338 return rc;
339}
340
341/*
342** Initialize all database files - the main database file, the file
343** used to store temporary tables, and any additional database files
344** created using ATTACH statements. Return a success code. If an
345** error occurs, write an error message into *pzErrMsg.
346**
347** After a database is initialized, the DB_SchemaLoaded bit is set
348** bit is set in the flags field of the Db structure. If the database
349** file was of zero-length, then the DB_Empty flag is also set.
350*/
351int sqlite3Init(sqlite3 *db, char **pzErrMsg){
352 int i, rc;
353 int commit_internal = !(db->flags&SQLITE_InternChanges);
354
355 assert( sqlite3_mutex_held(db->mutex) );
356 if( db->init.busy ) return SQLITE_OK;
357 rc = SQLITE_OK;
358 db->init.busy = 1;
359 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
360 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
361 rc = sqlite3InitOne(db, i, pzErrMsg);
362 if( rc ){
363 sqlite3ResetInternalSchema(db, i);
364 }
365 }
366
367 /* Once all the other databases have been initialised, load the schema
368 ** for the TEMP database. This is loaded last, as the TEMP database
369 ** schema may contain references to objects in other databases.
370 */
371#ifndef SQLITE_OMIT_TEMPDB
372 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
373 rc = sqlite3InitOne(db, 1, pzErrMsg);
374 if( rc ){
375 sqlite3ResetInternalSchema(db, 1);
376 }
377 }
378#endif
379
380 db->init.busy = 0;
381 if( rc==SQLITE_OK && commit_internal ){
382 sqlite3CommitInternalChanges(db);
383 }
384
385 return rc;
386}
387
388/*
389** This routine is a no-op if the database schema is already initialised.
390** Otherwise, the schema is loaded. An error code is returned.
391*/
392int sqlite3ReadSchema(Parse *pParse){
393 int rc = SQLITE_OK;
394 sqlite3 *db = pParse->db;
395 assert( sqlite3_mutex_held(db->mutex) );
396 if( !db->init.busy ){
397 rc = sqlite3Init(db, &pParse->zErrMsg);
398 }
399 if( rc!=SQLITE_OK ){
400 pParse->rc = rc;
401 pParse->nErr++;
402 }
403 return rc;
404}
405
406
407/*
408** Check schema cookies in all databases. If any cookie is out
409** of date, return 0. If all schema cookies are current, return 1.
410*/
411static int schemaIsValid(sqlite3 *db){
412 int iDb;
413 int rc;
414 BtCursor *curTemp;
415 int cookie;
416 int allOk = 1;
417
418 assert( sqlite3_mutex_held(db->mutex) );
419 for(iDb=0; allOk && iDb<db->nDb; iDb++){
420 Btree *pBt;
421 pBt = db->aDb[iDb].pBt;
422 if( pBt==0 ) continue;
423 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
424 if( rc==SQLITE_OK ){
425 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
426 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
427 allOk = 0;
428 }
429 sqlite3BtreeCloseCursor(curTemp);
430 }
431 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
432 db->mallocFailed = 1;
433 }
434 }
435 return allOk;
436}
437
438/*
439** Convert a schema pointer into the iDb index that indicates
440** which database file in db->aDb[] the schema refers to.
441**
442** If the same database is attached more than once, the first
443** attached database is returned.
444*/
445int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
446 int i = -1000000;
447
448 /* If pSchema is NULL, then return -1000000. This happens when code in
449 ** expr.c is trying to resolve a reference to a transient table (i.e. one
450 ** created by a sub-select). In this case the return value of this
451 ** function should never be used.
452 **
453 ** We return -1000000 instead of the more usual -1 simply because using
454 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
455 ** more likely to cause a segfault than -1 (of course there are assert()
456 ** statements too, but it never hurts to play the odds).
457 */
458 assert( sqlite3_mutex_held(db->mutex) );
459 if( pSchema ){
460 for(i=0; i<db->nDb; i++){
461 if( db->aDb[i].pSchema==pSchema ){
462 break;
463 }
464 }
465 assert( i>=0 &&i>=0 && i<db->nDb );
466 }
467 return i;
468}
469
470/*
471** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
472*/
473int sqlite3Prepare(
474 sqlite3 *db, /* Database handle. */
475 const char *zSql, /* UTF-8 encoded SQL statement. */
476 int nBytes, /* Length of zSql in bytes. */
477 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
478 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
479 const char **pzTail /* OUT: End of parsed string */
480){
481 Parse sParse;
482 char *zErrMsg = 0;
483 int rc = SQLITE_OK;
484 int i;
485
486 assert( ppStmt );
487 *ppStmt = 0;
488 if( sqlite3SafetyOn(db) ){
489 return SQLITE_MISUSE;
490 }
491 assert( !db->mallocFailed );
492 assert( sqlite3_mutex_held(db->mutex) );
493
494 /* If any attached database schemas are locked, do not proceed with
495 ** compilation. Instead return SQLITE_LOCKED immediately.
496 */
497 for(i=0; i<db->nDb; i++) {
498 Btree *pBt = db->aDb[i].pBt;
499 if( pBt ){
500 int rc;
501 rc = sqlite3BtreeSchemaLocked(pBt);
502 if( rc ){
503 const char *zDb = db->aDb[i].zName;
504 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
505 sqlite3SafetyOff(db);
506 return SQLITE_LOCKED;
507 }
508 }
509 }
510
511 memset(&sParse, 0, sizeof(sParse));
512 sParse.db = db;
513 if( nBytes>=0 && zSql[nBytes]!=0 ){
514 char *zSqlCopy;
515 if( nBytes>SQLITE_MAX_SQL_LENGTH ){
516 return SQLITE_TOOBIG;
517 }
518 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
519 if( zSqlCopy ){
520 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
521 sqlite3_free(zSqlCopy);
522 }
523 sParse.zTail = &zSql[nBytes];
524 }else{
525 sqlite3RunParser(&sParse, zSql, &zErrMsg);
526 }
527
528 if( db->mallocFailed ){
529 sParse.rc = SQLITE_NOMEM;
530 }
531 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
532 if( sParse.checkSchema && !schemaIsValid(db) ){
533 sParse.rc = SQLITE_SCHEMA;
534 }
535 if( sParse.rc==SQLITE_SCHEMA ){
536 sqlite3ResetInternalSchema(db, 0);
537 }
538 if( db->mallocFailed ){
539 sParse.rc = SQLITE_NOMEM;
540 }
541 if( pzTail ){
542 *pzTail = sParse.zTail;
543 }
544 rc = sParse.rc;
545
546#ifndef SQLITE_OMIT_EXPLAIN
547 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
548 if( sParse.explain==2 ){
549 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
550 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
551 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
552 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
553 }else{
554 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
555 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
556 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
557 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
558 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
559 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
560 }
561 }
562#endif
563
564 if( sqlite3SafetyOff(db) ){
565 rc = SQLITE_MISUSE;
566 }
567
568 if( saveSqlFlag ){
569 sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
570 }
571 if( rc!=SQLITE_OK || db->mallocFailed ){
572 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
573 assert(!(*ppStmt));
574 }else{
575 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
576 }
577
578 if( zErrMsg ){
579 sqlite3Error(db, rc, "%s", zErrMsg);
580 sqlite3_free(zErrMsg);
581 }else{
582 sqlite3Error(db, rc, 0);
583 }
584
585 rc = sqlite3ApiExit(db, rc);
586 /* sqlite3ReleaseThreadData(); */
587 assert( (rc&db->errMask)==rc );
588 return rc;
589}
590static int sqlite3LockAndPrepare(
591 sqlite3 *db, /* Database handle. */
592 const char *zSql, /* UTF-8 encoded SQL statement. */
593 int nBytes, /* Length of zSql in bytes. */
594 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
595 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
596 const char **pzTail /* OUT: End of parsed string */
597){
598 int rc;
599 if( sqlite3SafetyCheck(db) ){
600 return SQLITE_MISUSE;
601 }
602 sqlite3_mutex_enter(db->mutex);
603 sqlite3BtreeEnterAll(db);
604 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
605 sqlite3BtreeLeaveAll(db);
606 sqlite3_mutex_leave(db->mutex);
607 return rc;
608}
609
610/*
611** Rerun the compilation of a statement after a schema change.
612** Return true if the statement was recompiled successfully.
613** Return false if there is an error of some kind.
614*/
615int sqlite3Reprepare(Vdbe *p){
616 int rc;
617 sqlite3_stmt *pNew;
618 const char *zSql;
619 sqlite3 *db;
620
621 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
622 zSql = sqlite3VdbeGetSql(p);
623 if( zSql==0 ){
624 return 0;
625 }
626 db = sqlite3VdbeDb(p);
627 assert( sqlite3_mutex_held(db->mutex) );
628 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0);
629 if( rc ){
630 assert( pNew==0 );
631 return 0;
632 }else{
633 assert( pNew!=0 );
634 }
635 sqlite3VdbeSwap((Vdbe*)pNew, p);
636 sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
637 sqlite3VdbeResetStepResult((Vdbe*)pNew);
638 sqlite3VdbeFinalize((Vdbe*)pNew);
639 return 1;
640}
641
642
643/*
644** Two versions of the official API. Legacy and new use. In the legacy
645** version, the original SQL text is not saved in the prepared statement
646** and so if a schema change occurs, SQLITE_SCHEMA is returned by
647** sqlite3_step(). In the new version, the original SQL text is retained
648** and the statement is automatically recompiled if an schema change
649** occurs.
650*/
651int sqlite3_prepare(
652 sqlite3 *db, /* Database handle. */
653 const char *zSql, /* UTF-8 encoded SQL statement. */
654 int nBytes, /* Length of zSql in bytes. */
655 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
656 const char **pzTail /* OUT: End of parsed string */
657){
658 return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
659}
660int sqlite3_prepare_v2(
661 sqlite3 *db, /* Database handle. */
662 const char *zSql, /* UTF-8 encoded SQL statement. */
663 int nBytes, /* Length of zSql in bytes. */
664 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
665 const char **pzTail /* OUT: End of parsed string */
666){
667 return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
668}
669
670
671#ifndef SQLITE_OMIT_UTF16
672/*
673** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
674*/
675static int sqlite3Prepare16(
676 sqlite3 *db, /* Database handle. */
677 const void *zSql, /* UTF-8 encoded SQL statement. */
678 int nBytes, /* Length of zSql in bytes. */
679 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
680 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
681 const void **pzTail /* OUT: End of parsed string */
682){
683 /* This function currently works by first transforming the UTF-16
684 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
685 ** tricky bit is figuring out the pointer to return in *pzTail.
686 */
687 char *zSql8;
688 const char *zTail8 = 0;
689 int rc = SQLITE_OK;
690
691 if( sqlite3SafetyCheck(db) ){
692 return SQLITE_MISUSE;
693 }
694 sqlite3_mutex_enter(db->mutex);
695 zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
696 if( zSql8 ){
697 rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
698 }
699
700 if( zTail8 && pzTail ){
701 /* If sqlite3_prepare returns a tail pointer, we calculate the
702 ** equivalent pointer into the UTF-16 string by counting the unicode
703 ** characters between zSql8 and zTail8, and then returning a pointer
704 ** the same number of characters into the UTF-16 string.
705 */
706 int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
707 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
708 }
709 sqlite3_free(zSql8);
710 rc = sqlite3ApiExit(db, rc);
711 sqlite3_mutex_leave(db->mutex);
712 return rc;
713}
714
715/*
716** Two versions of the official API. Legacy and new use. In the legacy
717** version, the original SQL text is not saved in the prepared statement
718** and so if a schema change occurs, SQLITE_SCHEMA is returned by
719** sqlite3_step(). In the new version, the original SQL text is retained
720** and the statement is automatically recompiled if an schema change
721** occurs.
722*/
723int sqlite3_prepare16(
724 sqlite3 *db, /* Database handle. */
725 const void *zSql, /* UTF-8 encoded SQL statement. */
726 int nBytes, /* Length of zSql in bytes. */
727 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
728 const void **pzTail /* OUT: End of parsed string */
729){
730 return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
731}
732int sqlite3_prepare16_v2(
733 sqlite3 *db, /* Database handle. */
734 const void *zSql, /* UTF-8 encoded SQL statement. */
735 int nBytes, /* Length of zSql in bytes. */
736 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
737 const void **pzTail /* OUT: End of parsed string */
738){
739 return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
740}
741
742#endif /* SQLITE_OMIT_UTF16 */
diff --git a/libraries/sqlite/win32/printf.c b/libraries/sqlite/win32/printf.c
new file mode 100755
index 0000000..bea91e2
--- /dev/null
+++ b/libraries/sqlite/win32/printf.c
@@ -0,0 +1,907 @@
1/*
2** The "printf" code that follows dates from the 1980's. It is in
3** the public domain. The original comments are included here for
4** completeness. They are very out-of-date but might be useful as
5** an historical reference. Most of the "enhancements" have been backed
6** out so that the functionality is now the same as standard printf().
7**
8**************************************************************************
9**
10** The following modules is an enhanced replacement for the "printf" subroutines
11** found in the standard C library. The following enhancements are
12** supported:
13**
14** + Additional functions. The standard set of "printf" functions
15** includes printf, fprintf, sprintf, vprintf, vfprintf, and
16** vsprintf. This module adds the following:
17**
18** * snprintf -- Works like sprintf, but has an extra argument
19** which is the size of the buffer written to.
20**
21** * mprintf -- Similar to sprintf. Writes output to memory
22** obtained from malloc.
23**
24** * xprintf -- Calls a function to dispose of output.
25**
26** * nprintf -- No output, but returns the number of characters
27** that would have been output by printf.
28**
29** * A v- version (ex: vsnprintf) of every function is also
30** supplied.
31**
32** + A few extensions to the formatting notation are supported:
33**
34** * The "=" flag (similar to "-") causes the output to be
35** be centered in the appropriately sized field.
36**
37** * The %b field outputs an integer in binary notation.
38**
39** * The %c field now accepts a precision. The character output
40** is repeated by the number of times the precision specifies.
41**
42** * The %' field works like %c, but takes as its character the
43** next character of the format string, instead of the next
44** argument. For example, printf("%.78'-") prints 78 minus
45** signs, the same as printf("%.78c",'-').
46**
47** + When compiled using GCC on a SPARC, this version of printf is
48** faster than the library printf for SUN OS 4.1.
49**
50** + All functions are fully reentrant.
51**
52*/
53#include "sqliteInt.h"
54#include <math.h>
55
56/*
57** Conversion types fall into various categories as defined by the
58** following enumeration.
59*/
60#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
61#define etFLOAT 2 /* Floating point. %f */
62#define etEXP 3 /* Exponentional notation. %e and %E */
63#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
64#define etSIZE 5 /* Return number of characters processed so far. %n */
65#define etSTRING 6 /* Strings. %s */
66#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
67#define etPERCENT 8 /* Percent symbol. %% */
68#define etCHARX 9 /* Characters. %c */
69/* The rest are extensions, not normally found in printf() */
70#define etCHARLIT 10 /* Literal characters. %' */
71#define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '',
73 NULL pointers replaced by SQL NULL. %Q */
74#define etTOKEN 13 /* a pointer to a Token structure */
75#define etSRCLIST 14 /* a pointer to a SrcList */
76#define etPOINTER 15 /* The %p conversion */
77#define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */
78
79
80/*
81** An "etByte" is an 8-bit unsigned value.
82*/
83typedef unsigned char etByte;
84
85/*
86** Each builtin conversion character (ex: the 'd' in "%d") is described
87** by an instance of the following structure
88*/
89typedef struct et_info { /* Information about each format field */
90 char fmttype; /* The format field code letter */
91 etByte base; /* The base for radix conversion */
92 etByte flags; /* One or more of FLAG_ constants below */
93 etByte type; /* Conversion paradigm */
94 etByte charset; /* Offset into aDigits[] of the digits string */
95 etByte prefix; /* Offset into aPrefix[] of the prefix string */
96} et_info;
97
98/*
99** Allowed values for et_info.flags
100*/
101#define FLAG_SIGNED 1 /* True if the value to convert is signed */
102#define FLAG_INTERN 2 /* True if for internal use only */
103#define FLAG_STRING 4 /* Allow infinity precision */
104
105
106/*
107** The following table is searched linearly, so it is good to put the
108** most frequently used conversion types first.
109*/
110static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
111static const char aPrefix[] = "-x0\000X0";
112static const et_info fmtinfo[] = {
113 { 'd', 10, 1, etRADIX, 0, 0 },
114 { 's', 0, 4, etSTRING, 0, 0 },
115 { 'g', 0, 1, etGENERIC, 30, 0 },
116 { 'z', 0, 4, etDYNSTRING, 0, 0 },
117 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
118 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
119 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
120 { 'c', 0, 0, etCHARX, 0, 0 },
121 { 'o', 8, 0, etRADIX, 0, 2 },
122 { 'u', 10, 0, etRADIX, 0, 0 },
123 { 'x', 16, 0, etRADIX, 16, 1 },
124 { 'X', 16, 0, etRADIX, 0, 4 },
125#ifndef SQLITE_OMIT_FLOATING_POINT
126 { 'f', 0, 1, etFLOAT, 0, 0 },
127 { 'e', 0, 1, etEXP, 30, 0 },
128 { 'E', 0, 1, etEXP, 14, 0 },
129 { 'G', 0, 1, etGENERIC, 14, 0 },
130#endif
131 { 'i', 10, 1, etRADIX, 0, 0 },
132 { 'n', 0, 0, etSIZE, 0, 0 },
133 { '%', 0, 0, etPERCENT, 0, 0 },
134 { 'p', 16, 0, etPOINTER, 0, 1 },
135 { 'T', 0, 2, etTOKEN, 0, 0 },
136 { 'S', 0, 2, etSRCLIST, 0, 0 },
137};
138#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
139
140/*
141** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
142** conversions will work.
143*/
144#ifndef SQLITE_OMIT_FLOATING_POINT
145/*
146** "*val" is a double such that 0.1 <= *val < 10.0
147** Return the ascii code for the leading digit of *val, then
148** multiply "*val" by 10.0 to renormalize.
149**
150** Example:
151** input: *val = 3.14159
152** output: *val = 1.4159 function return = '3'
153**
154** The counter *cnt is incremented each time. After counter exceeds
155** 16 (the number of significant digits in a 64-bit float) '0' is
156** always returned.
157*/
158static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
159 int digit;
160 LONGDOUBLE_TYPE d;
161 if( (*cnt)++ >= 16 ) return '0';
162 digit = (int)*val;
163 d = digit;
164 digit += '0';
165 *val = (*val - d)*10.0;
166 return digit;
167}
168#endif /* SQLITE_OMIT_FLOATING_POINT */
169
170/*
171** On machines with a small stack size, you can redefine the
172** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
173** smaller values some %f conversions may go into an infinite loop.
174*/
175#ifndef SQLITE_PRINT_BUF_SIZE
176# define SQLITE_PRINT_BUF_SIZE 350
177#endif
178#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
179
180/*
181** The root program. All variations call this core.
182**
183** INPUTS:
184** func This is a pointer to a function taking three arguments
185** 1. A pointer to anything. Same as the "arg" parameter.
186** 2. A pointer to the list of characters to be output
187** (Note, this list is NOT null terminated.)
188** 3. An integer number of characters to be output.
189** (Note: This number might be zero.)
190**
191** arg This is the pointer to anything which will be passed as the
192** first argument to "func". Use it for whatever you like.
193**
194** fmt This is the format string, as in the usual print.
195**
196** ap This is a pointer to a list of arguments. Same as in
197** vfprint.
198**
199** OUTPUTS:
200** The return value is the total number of characters sent to
201** the function "func". Returns -1 on a error.
202**
203** Note that the order in which automatic variables are declared below
204** seems to make a big difference in determining how fast this beast
205** will run.
206*/
207static int vxprintf(
208 void (*func)(void*,const char*,int), /* Consumer of text */
209 void *arg, /* First argument to the consumer */
210 int useExtended, /* Allow extended %-conversions */
211 const char *fmt, /* Format string */
212 va_list ap /* arguments */
213){
214 int c; /* Next character in the format string */
215 char *bufpt; /* Pointer to the conversion buffer */
216 int precision; /* Precision of the current field */
217 int length; /* Length of the field */
218 int idx; /* A general purpose loop counter */
219 int count; /* Total number of characters output */
220 int width; /* Width of the current field */
221 etByte flag_leftjustify; /* True if "-" flag is present */
222 etByte flag_plussign; /* True if "+" flag is present */
223 etByte flag_blanksign; /* True if " " flag is present */
224 etByte flag_alternateform; /* True if "#" flag is present */
225 etByte flag_altform2; /* True if "!" flag is present */
226 etByte flag_zeropad; /* True if field width constant starts with zero */
227 etByte flag_long; /* True if "l" flag is present */
228 etByte flag_longlong; /* True if the "ll" flag is present */
229 etByte done; /* Loop termination flag */
230 sqlite_uint64 longvalue; /* Value for integer types */
231 LONGDOUBLE_TYPE realvalue; /* Value for real types */
232 const et_info *infop; /* Pointer to the appropriate info structure */
233 char buf[etBUFSIZE]; /* Conversion buffer */
234 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
235 etByte errorflag = 0; /* True if an error is encountered */
236 etByte xtype; /* Conversion paradigm */
237 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
238 static const char spaces[] =
239 " ";
240#define etSPACESIZE (sizeof(spaces)-1)
241#ifndef SQLITE_OMIT_FLOATING_POINT
242 int exp, e2; /* exponent of real numbers */
243 double rounder; /* Used for rounding floating point values */
244 etByte flag_dp; /* True if decimal point should be shown */
245 etByte flag_rtz; /* True if trailing zeros should be removed */
246 etByte flag_exp; /* True to force display of the exponent */
247 int nsd; /* Number of significant digits returned */
248#endif
249
250 func(arg,"",0);
251 count = length = 0;
252 bufpt = 0;
253 for(; (c=(*fmt))!=0; ++fmt){
254 if( c!='%' ){
255 int amt;
256 bufpt = (char *)fmt;
257 amt = 1;
258 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
259 (*func)(arg,bufpt,amt);
260 count += amt;
261 if( c==0 ) break;
262 }
263 if( (c=(*++fmt))==0 ){
264 errorflag = 1;
265 (*func)(arg,"%",1);
266 count++;
267 break;
268 }
269 /* Find out what flags are present */
270 flag_leftjustify = flag_plussign = flag_blanksign =
271 flag_alternateform = flag_altform2 = flag_zeropad = 0;
272 done = 0;
273 do{
274 switch( c ){
275 case '-': flag_leftjustify = 1; break;
276 case '+': flag_plussign = 1; break;
277 case ' ': flag_blanksign = 1; break;
278 case '#': flag_alternateform = 1; break;
279 case '!': flag_altform2 = 1; break;
280 case '0': flag_zeropad = 1; break;
281 default: done = 1; break;
282 }
283 }while( !done && (c=(*++fmt))!=0 );
284 /* Get the field width */
285 width = 0;
286 if( c=='*' ){
287 width = va_arg(ap,int);
288 if( width<0 ){
289 flag_leftjustify = 1;
290 width = -width;
291 }
292 c = *++fmt;
293 }else{
294 while( c>='0' && c<='9' ){
295 width = width*10 + c - '0';
296 c = *++fmt;
297 }
298 }
299 if( width > etBUFSIZE-10 ){
300 width = etBUFSIZE-10;
301 }
302 /* Get the precision */
303 if( c=='.' ){
304 precision = 0;
305 c = *++fmt;
306 if( c=='*' ){
307 precision = va_arg(ap,int);
308 if( precision<0 ) precision = -precision;
309 c = *++fmt;
310 }else{
311 while( c>='0' && c<='9' ){
312 precision = precision*10 + c - '0';
313 c = *++fmt;
314 }
315 }
316 }else{
317 precision = -1;
318 }
319 /* Get the conversion type modifier */
320 if( c=='l' ){
321 flag_long = 1;
322 c = *++fmt;
323 if( c=='l' ){
324 flag_longlong = 1;
325 c = *++fmt;
326 }else{
327 flag_longlong = 0;
328 }
329 }else{
330 flag_long = flag_longlong = 0;
331 }
332 /* Fetch the info entry for the field */
333 infop = 0;
334 for(idx=0; idx<etNINFO; idx++){
335 if( c==fmtinfo[idx].fmttype ){
336 infop = &fmtinfo[idx];
337 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
338 xtype = infop->type;
339 }else{
340 return -1;
341 }
342 break;
343 }
344 }
345 zExtra = 0;
346 if( infop==0 ){
347 return -1;
348 }
349
350
351 /* Limit the precision to prevent overflowing buf[] during conversion */
352 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
353 precision = etBUFSIZE-40;
354 }
355
356 /*
357 ** At this point, variables are initialized as follows:
358 **
359 ** flag_alternateform TRUE if a '#' is present.
360 ** flag_altform2 TRUE if a '!' is present.
361 ** flag_plussign TRUE if a '+' is present.
362 ** flag_leftjustify TRUE if a '-' is present or if the
363 ** field width was negative.
364 ** flag_zeropad TRUE if the width began with 0.
365 ** flag_long TRUE if the letter 'l' (ell) prefixed
366 ** the conversion character.
367 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
368 ** the conversion character.
369 ** flag_blanksign TRUE if a ' ' is present.
370 ** width The specified field width. This is
371 ** always non-negative. Zero is the default.
372 ** precision The specified precision. The default
373 ** is -1.
374 ** xtype The class of the conversion.
375 ** infop Pointer to the appropriate info struct.
376 */
377 switch( xtype ){
378 case etPOINTER:
379 flag_longlong = sizeof(char*)==sizeof(i64);
380 flag_long = sizeof(char*)==sizeof(long int);
381 /* Fall through into the next case */
382 case etRADIX:
383 if( infop->flags & FLAG_SIGNED ){
384 i64 v;
385 if( flag_longlong ) v = va_arg(ap,i64);
386 else if( flag_long ) v = va_arg(ap,long int);
387 else v = va_arg(ap,int);
388 if( v<0 ){
389 longvalue = -v;
390 prefix = '-';
391 }else{
392 longvalue = v;
393 if( flag_plussign ) prefix = '+';
394 else if( flag_blanksign ) prefix = ' ';
395 else prefix = 0;
396 }
397 }else{
398 if( flag_longlong ) longvalue = va_arg(ap,u64);
399 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
400 else longvalue = va_arg(ap,unsigned int);
401 prefix = 0;
402 }
403 if( longvalue==0 ) flag_alternateform = 0;
404 if( flag_zeropad && precision<width-(prefix!=0) ){
405 precision = width-(prefix!=0);
406 }
407 bufpt = &buf[etBUFSIZE-1];
408 {
409 register const char *cset; /* Use registers for speed */
410 register int base;
411 cset = &aDigits[infop->charset];
412 base = infop->base;
413 do{ /* Convert to ascii */
414 *(--bufpt) = cset[longvalue%base];
415 longvalue = longvalue/base;
416 }while( longvalue>0 );
417 }
418 length = &buf[etBUFSIZE-1]-bufpt;
419 for(idx=precision-length; idx>0; idx--){
420 *(--bufpt) = '0'; /* Zero pad */
421 }
422 if( prefix ) *(--bufpt) = prefix; /* Add sign */
423 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
424 const char *pre;
425 char x;
426 pre = &aPrefix[infop->prefix];
427 if( *bufpt!=pre[0] ){
428 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
429 }
430 }
431 length = &buf[etBUFSIZE-1]-bufpt;
432 break;
433 case etFLOAT:
434 case etEXP:
435 case etGENERIC:
436 realvalue = va_arg(ap,double);
437#ifndef SQLITE_OMIT_FLOATING_POINT
438 if( precision<0 ) precision = 6; /* Set default precision */
439 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
440 if( realvalue<0.0 ){
441 realvalue = -realvalue;
442 prefix = '-';
443 }else{
444 if( flag_plussign ) prefix = '+';
445 else if( flag_blanksign ) prefix = ' ';
446 else prefix = 0;
447 }
448 if( xtype==etGENERIC && precision>0 ) precision--;
449#if 0
450 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
451 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
452#else
453 /* It makes more sense to use 0.5 */
454 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
455#endif
456 if( xtype==etFLOAT ) realvalue += rounder;
457 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
458 exp = 0;
459 if( sqlite3_isnan(realvalue) ){
460 bufpt = "NaN";
461 length = 3;
462 break;
463 }
464 if( realvalue>0.0 ){
465 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
466 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
467 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
468 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
469 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
470 if( exp>350 || exp<-350 ){
471 if( prefix=='-' ){
472 bufpt = "-Inf";
473 }else if( prefix=='+' ){
474 bufpt = "+Inf";
475 }else{
476 bufpt = "Inf";
477 }
478 length = strlen(bufpt);
479 break;
480 }
481 }
482 bufpt = buf;
483 /*
484 ** If the field type is etGENERIC, then convert to either etEXP
485 ** or etFLOAT, as appropriate.
486 */
487 flag_exp = xtype==etEXP;
488 if( xtype!=etFLOAT ){
489 realvalue += rounder;
490 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
491 }
492 if( xtype==etGENERIC ){
493 flag_rtz = !flag_alternateform;
494 if( exp<-4 || exp>precision ){
495 xtype = etEXP;
496 }else{
497 precision = precision - exp;
498 xtype = etFLOAT;
499 }
500 }else{
501 flag_rtz = 0;
502 }
503 if( xtype==etEXP ){
504 e2 = 0;
505 }else{
506 e2 = exp;
507 }
508 nsd = 0;
509 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
510 /* The sign in front of the number */
511 if( prefix ){
512 *(bufpt++) = prefix;
513 }
514 /* Digits prior to the decimal point */
515 if( e2<0 ){
516 *(bufpt++) = '0';
517 }else{
518 for(; e2>=0; e2--){
519 *(bufpt++) = et_getdigit(&realvalue,&nsd);
520 }
521 }
522 /* The decimal point */
523 if( flag_dp ){
524 *(bufpt++) = '.';
525 }
526 /* "0" digits after the decimal point but before the first
527 ** significant digit of the number */
528 for(e2++; e2<0 && precision>0; precision--, e2++){
529 *(bufpt++) = '0';
530 }
531 /* Significant digits after the decimal point */
532 while( (precision--)>0 ){
533 *(bufpt++) = et_getdigit(&realvalue,&nsd);
534 }
535 /* Remove trailing zeros and the "." if no digits follow the "." */
536 if( flag_rtz && flag_dp ){
537 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
538 assert( bufpt>buf );
539 if( bufpt[-1]=='.' ){
540 if( flag_altform2 ){
541 *(bufpt++) = '0';
542 }else{
543 *(--bufpt) = 0;
544 }
545 }
546 }
547 /* Add the "eNNN" suffix */
548 if( flag_exp || (xtype==etEXP && exp) ){
549 *(bufpt++) = aDigits[infop->charset];
550 if( exp<0 ){
551 *(bufpt++) = '-'; exp = -exp;
552 }else{
553 *(bufpt++) = '+';
554 }
555 if( exp>=100 ){
556 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
557 exp %= 100;
558 }
559 *(bufpt++) = exp/10+'0'; /* 10's digit */
560 *(bufpt++) = exp%10+'0'; /* 1's digit */
561 }
562 *bufpt = 0;
563
564 /* The converted number is in buf[] and zero terminated. Output it.
565 ** Note that the number is in the usual order, not reversed as with
566 ** integer conversions. */
567 length = bufpt-buf;
568 bufpt = buf;
569
570 /* Special case: Add leading zeros if the flag_zeropad flag is
571 ** set and we are not left justified */
572 if( flag_zeropad && !flag_leftjustify && length < width){
573 int i;
574 int nPad = width - length;
575 for(i=width; i>=nPad; i--){
576 bufpt[i] = bufpt[i-nPad];
577 }
578 i = prefix!=0;
579 while( nPad-- ) bufpt[i++] = '0';
580 length = width;
581 }
582#endif
583 break;
584 case etSIZE:
585 *(va_arg(ap,int*)) = count;
586 length = width = 0;
587 break;
588 case etPERCENT:
589 buf[0] = '%';
590 bufpt = buf;
591 length = 1;
592 break;
593 case etCHARLIT:
594 case etCHARX:
595 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
596 if( precision>=0 ){
597 for(idx=1; idx<precision; idx++) buf[idx] = c;
598 length = precision;
599 }else{
600 length =1;
601 }
602 bufpt = buf;
603 break;
604 case etSTRING:
605 case etDYNSTRING:
606 bufpt = va_arg(ap,char*);
607 if( bufpt==0 ){
608 bufpt = "";
609 }else if( xtype==etDYNSTRING ){
610 zExtra = bufpt;
611 }
612 length = strlen(bufpt);
613 if( precision>=0 && precision<length ) length = precision;
614 break;
615 case etSQLESCAPE:
616 case etSQLESCAPE2:
617 case etSQLESCAPE3: {
618 int i, j, n, ch, isnull;
619 int needQuote;
620 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
621 char *escarg = va_arg(ap,char*);
622 isnull = escarg==0;
623 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
624 for(i=n=0; (ch=escarg[i])!=0; i++){
625 if( ch==q ) n++;
626 }
627 needQuote = !isnull && xtype==etSQLESCAPE2;
628 n += i + 1 + needQuote*2;
629 if( n>etBUFSIZE ){
630 bufpt = zExtra = sqlite3_malloc( n );
631 if( bufpt==0 ) return -1;
632 }else{
633 bufpt = buf;
634 }
635 j = 0;
636 if( needQuote ) bufpt[j++] = q;
637 for(i=0; (ch=escarg[i])!=0; i++){
638 bufpt[j++] = ch;
639 if( ch==q ) bufpt[j++] = ch;
640 }
641 if( needQuote ) bufpt[j++] = q;
642 bufpt[j] = 0;
643 length = j;
644 /* The precision is ignored on %q and %Q */
645 /* if( precision>=0 && precision<length ) length = precision; */
646 break;
647 }
648 case etTOKEN: {
649 Token *pToken = va_arg(ap, Token*);
650 if( pToken && pToken->z ){
651 (*func)(arg, (char*)pToken->z, pToken->n);
652 }
653 length = width = 0;
654 break;
655 }
656 case etSRCLIST: {
657 SrcList *pSrc = va_arg(ap, SrcList*);
658 int k = va_arg(ap, int);
659 struct SrcList_item *pItem = &pSrc->a[k];
660 assert( k>=0 && k<pSrc->nSrc );
661 if( pItem->zDatabase && pItem->zDatabase[0] ){
662 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
663 (*func)(arg, ".", 1);
664 }
665 (*func)(arg, pItem->zName, strlen(pItem->zName));
666 length = width = 0;
667 break;
668 }
669 }/* End switch over the format type */
670 /*
671 ** The text of the conversion is pointed to by "bufpt" and is
672 ** "length" characters long. The field width is "width". Do
673 ** the output.
674 */
675 if( !flag_leftjustify ){
676 register int nspace;
677 nspace = width-length;
678 if( nspace>0 ){
679 count += nspace;
680 while( nspace>=etSPACESIZE ){
681 (*func)(arg,spaces,etSPACESIZE);
682 nspace -= etSPACESIZE;
683 }
684 if( nspace>0 ) (*func)(arg,spaces,nspace);
685 }
686 }
687 if( length>0 ){
688 (*func)(arg,bufpt,length);
689 count += length;
690 }
691 if( flag_leftjustify ){
692 register int nspace;
693 nspace = width-length;
694 if( nspace>0 ){
695 count += nspace;
696 while( nspace>=etSPACESIZE ){
697 (*func)(arg,spaces,etSPACESIZE);
698 nspace -= etSPACESIZE;
699 }
700 if( nspace>0 ) (*func)(arg,spaces,nspace);
701 }
702 }
703 if( zExtra ){
704 sqlite3_free(zExtra);
705 }
706 }/* End for loop over the format string */
707 return errorflag ? -1 : count;
708} /* End of function */
709
710
711/* This structure is used to store state information about the
712** write to memory that is currently in progress.
713*/
714struct sgMprintf {
715 char *zBase; /* A base allocation */
716 char *zText; /* The string collected so far */
717 int nChar; /* Length of the string so far */
718 int nTotal; /* Output size if unconstrained */
719 int nAlloc; /* Amount of space allocated in zText */
720 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
721 int iMallocFailed; /* True if xRealloc() has failed */
722};
723
724/*
725** This function implements the callback from vxprintf.
726**
727** This routine add nNewChar characters of text in zNewText to
728** the sgMprintf structure pointed to by "arg".
729*/
730static void mout(void *arg, const char *zNewText, int nNewChar){
731 struct sgMprintf *pM = (struct sgMprintf*)arg;
732 if( pM->iMallocFailed ) return;
733 pM->nTotal += nNewChar;
734 if( pM->zText ){
735 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
736 if( pM->xRealloc==0 ){
737 nNewChar = pM->nAlloc - pM->nChar - 1;
738 }else{
739 int nAlloc = pM->nChar + nNewChar*2 + 1;
740 if( pM->zText==pM->zBase ){
741 pM->zText = pM->xRealloc(0, nAlloc);
742 if( pM->zText==0 ){
743 pM->nAlloc = 0;
744 pM->iMallocFailed = 1;
745 return;
746 }else if( pM->nChar ){
747 memcpy(pM->zText, pM->zBase, pM->nChar);
748 }
749 }else{
750 char *zNew;
751 zNew = pM->xRealloc(pM->zText, nAlloc);
752 if( zNew ){
753 pM->zText = zNew;
754 }else{
755 pM->iMallocFailed = 1;
756 pM->xRealloc(pM->zText, 0);
757 pM->zText = 0;
758 pM->nAlloc = 0;
759 return;
760 }
761 }
762 pM->nAlloc = nAlloc;
763 }
764 }
765 if( nNewChar>0 ){
766 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
767 pM->nChar += nNewChar;
768 }
769 pM->zText[pM->nChar] = 0;
770 }
771}
772
773/*
774** This routine is a wrapper around xprintf() that invokes mout() as
775** the consumer.
776*/
777static char *base_vprintf(
778 void *(*xRealloc)(void*, int), /* realloc() function. May be NULL */
779 int useInternal, /* Use internal %-conversions if true */
780 char *zInitBuf, /* Initially write here, before mallocing */
781 int nInitBuf, /* Size of zInitBuf[] */
782 const char *zFormat, /* format string */
783 va_list ap /* arguments */
784){
785 struct sgMprintf sM;
786 sM.zBase = sM.zText = zInitBuf;
787 sM.nChar = sM.nTotal = 0;
788 sM.nAlloc = nInitBuf;
789 sM.xRealloc = xRealloc;
790 sM.iMallocFailed = 0;
791 vxprintf(mout, &sM, useInternal, zFormat, ap);
792 assert(sM.iMallocFailed==0 || sM.zText==0);
793 if( xRealloc && !sM.iMallocFailed ){
794 if( sM.zText==sM.zBase ){
795 sM.zText = xRealloc(0, sM.nChar+1);
796 if( sM.zText ){
797 memcpy(sM.zText, sM.zBase, sM.nChar+1);
798 }
799 }else if( sM.nAlloc>sM.nChar+10 ){
800 char *zNew;
801 sqlite3MallocBenignFailure(1);
802 zNew = xRealloc(sM.zText, sM.nChar+1);
803 if( zNew ){
804 sM.zText = zNew;
805 }
806 }
807 }
808 return sM.zText;
809}
810
811/*
812** Realloc that is a real function, not a macro.
813*/
814static void *printf_realloc(void *old, int size){
815 return sqlite3_realloc(old, size);
816}
817
818/*
819** Print into memory obtained from sqliteMalloc(). Use the internal
820** %-conversion extensions.
821*/
822char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
823 char *z;
824 char zBase[SQLITE_PRINT_BUF_SIZE];
825 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
826 if( z==0 && db!=0 ){
827 db->mallocFailed = 1;
828 }
829 return z;
830}
831
832/*
833** Print into memory obtained from sqliteMalloc(). Use the internal
834** %-conversion extensions.
835*/
836char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
837 va_list ap;
838 char *z;
839 char zBase[SQLITE_PRINT_BUF_SIZE];
840 va_start(ap, zFormat);
841 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
842 va_end(ap);
843 if( z==0 && db!=0 ){
844 db->mallocFailed = 1;
845 }
846 return z;
847}
848
849/*
850** Print into memory obtained from sqlite3_malloc(). Omit the internal
851** %-conversion extensions.
852*/
853char *sqlite3_vmprintf(const char *zFormat, va_list ap){
854 char zBase[SQLITE_PRINT_BUF_SIZE];
855 return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
856}
857
858/*
859** Print into memory obtained from sqlite3_malloc()(). Omit the internal
860** %-conversion extensions.
861*/
862char *sqlite3_mprintf(const char *zFormat, ...){
863 va_list ap;
864 char *z;
865 va_start(ap, zFormat);
866 z = sqlite3_vmprintf(zFormat, ap);
867 va_end(ap);
868 return z;
869}
870
871/*
872** sqlite3_snprintf() works like snprintf() except that it ignores the
873** current locale settings. This is important for SQLite because we
874** are not able to use a "," as the decimal point in place of "." as
875** specified by some locales.
876*/
877char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
878 char *z;
879 va_list ap;
880
881 if( n<=0 ){
882 return zBuf;
883 }
884 zBuf[0] = 0;
885 va_start(ap,zFormat);
886 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
887 va_end(ap);
888 return z;
889}
890
891#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
892/*
893** A version of printf() that understands %lld. Used for debugging.
894** The printf() built into some versions of windows does not understand %lld
895** and segfaults if you give it a long long int.
896*/
897void sqlite3DebugPrintf(const char *zFormat, ...){
898 extern int getpid(void);
899 va_list ap;
900 char zBuf[500];
901 va_start(ap, zFormat);
902 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
903 va_end(ap);
904 fprintf(stdout,"%s", zBuf);
905 fflush(stdout);
906}
907#endif
diff --git a/libraries/sqlite/win32/random.c b/libraries/sqlite/win32/random.c
new file mode 100755
index 0000000..19b284f
--- /dev/null
+++ b/libraries/sqlite/win32/random.c
@@ -0,0 +1,103 @@
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.
10**
11*************************************************************************
12** This file contains code to implement a pseudo-random number
13** generator (PRNG) for SQLite.
14**
15** Random numbers are used by some of the database backends in order
16** to generate random integer keys for tables or random filenames.
17**
18** $Id: random.c,v 1.20 2007/08/21 13:51:23 drh Exp $
19*/
20#include "sqliteInt.h"
21
22
23/*
24** Get a single 8-bit random value from the RC4 PRNG. The Mutex
25** must be held while executing this routine.
26**
27** Why not just use a library random generator like lrand48() for this?
28** Because the OP_NewRowid opcode in the VDBE depends on having a very
29** good source of random numbers. The lrand48() library function may
30** well be good enough. But maybe not. Or maybe lrand48() has some
31** subtle problems on some systems that could cause problems. It is hard
32** to know. To minimize the risk of problems due to bad lrand48()
33** implementations, SQLite uses this random number generator based
34** on RC4, which we know works very well.
35**
36** (Later): Actually, OP_NewRowid does not depend on a good source of
37** randomness any more. But we will leave this code in all the same.
38*/
39static int randomByte(void){
40 unsigned char t;
41
42 /* All threads share a single random number generator.
43 ** This structure is the current state of the generator.
44 */
45 static struct {
46 unsigned char isInit; /* True if initialized */
47 unsigned char i, j; /* State variables */
48 unsigned char s[256]; /* State variables */
49 } prng;
50
51 /* Initialize the state of the random number generator once,
52 ** the first time this routine is called. The seed value does
53 ** not need to contain a lot of randomness since we are not
54 ** trying to do secure encryption or anything like that...
55 **
56 ** Nothing in this file or anywhere else in SQLite does any kind of
57 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
58 ** number generator) not as an encryption device.
59 */
60 if( !prng.isInit ){
61 int i;
62 char k[256];
63 prng.j = 0;
64 prng.i = 0;
65 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
66 for(i=0; i<256; i++){
67 prng.s[i] = i;
68 }
69 for(i=0; i<256; i++){
70 prng.j += prng.s[i] + k[i];
71 t = prng.s[prng.j];
72 prng.s[prng.j] = prng.s[i];
73 prng.s[i] = t;
74 }
75 prng.isInit = 1;
76 }
77
78 /* Generate and return single random byte
79 */
80 prng.i++;
81 t = prng.s[prng.i];
82 prng.j += t;
83 prng.s[prng.i] = prng.s[prng.j];
84 prng.s[prng.j] = t;
85 t += prng.s[prng.i];
86 return prng.s[t];
87}
88
89/*
90** Return N random bytes.
91*/
92void sqlite3Randomness(int N, void *pBuf){
93 unsigned char *zBuf = pBuf;
94 static sqlite3_mutex *mutex = 0;
95 if( mutex==0 ){
96 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
97 }
98 sqlite3_mutex_enter(mutex);
99 while( N-- ){
100 *(zBuf++) = randomByte();
101 }
102 sqlite3_mutex_leave(mutex);
103}
diff --git a/libraries/sqlite/win32/select.c b/libraries/sqlite/win32/select.c
new file mode 100755
index 0000000..fbe1b06
--- /dev/null
+++ b/libraries/sqlite/win32/select.c
@@ -0,0 +1,3539 @@
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.
10**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle SELECT statements in SQLite.
14**
15** $Id: select.c,v 1.359 2007/08/31 17:42:48 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18
19
20/*
21** Delete all the content of a Select structure but do not deallocate
22** the select structure itself.
23*/
24static void clearSelect(Select *p){
25 sqlite3ExprListDelete(p->pEList);
26 sqlite3SrcListDelete(p->pSrc);
27 sqlite3ExprDelete(p->pWhere);
28 sqlite3ExprListDelete(p->pGroupBy);
29 sqlite3ExprDelete(p->pHaving);
30 sqlite3ExprListDelete(p->pOrderBy);
31 sqlite3SelectDelete(p->pPrior);
32 sqlite3ExprDelete(p->pLimit);
33 sqlite3ExprDelete(p->pOffset);
34}
35
36
37/*
38** Allocate a new Select structure and return a pointer to that
39** structure.
40*/
41Select *sqlite3SelectNew(
42 Parse *pParse, /* Parsing context */
43 ExprList *pEList, /* which columns to include in the result */
44 SrcList *pSrc, /* the FROM clause -- which tables to scan */
45 Expr *pWhere, /* the WHERE clause */
46 ExprList *pGroupBy, /* the GROUP BY clause */
47 Expr *pHaving, /* the HAVING clause */
48 ExprList *pOrderBy, /* the ORDER BY clause */
49 int isDistinct, /* true if the DISTINCT keyword is present */
50 Expr *pLimit, /* LIMIT value. NULL means not used */
51 Expr *pOffset /* OFFSET value. NULL means no offset */
52){
53 Select *pNew;
54 Select standin;
55 sqlite3 *db = pParse->db;
56 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
57 assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */
58 if( pNew==0 ){
59 pNew = &standin;
60 memset(pNew, 0, sizeof(*pNew));
61 }
62 if( pEList==0 ){
63 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0);
64 }
65 pNew->pEList = pEList;
66 pNew->pSrc = pSrc;
67 pNew->pWhere = pWhere;
68 pNew->pGroupBy = pGroupBy;
69 pNew->pHaving = pHaving;
70 pNew->pOrderBy = pOrderBy;
71 pNew->isDistinct = isDistinct;
72 pNew->op = TK_SELECT;
73 assert( pOffset==0 || pLimit!=0 );
74 pNew->pLimit = pLimit;
75 pNew->pOffset = pOffset;
76 pNew->iLimit = -1;
77 pNew->iOffset = -1;
78 pNew->addrOpenEphm[0] = -1;
79 pNew->addrOpenEphm[1] = -1;
80 pNew->addrOpenEphm[2] = -1;
81 if( pNew==&standin) {
82 clearSelect(pNew);
83 pNew = 0;
84 }
85 return pNew;
86}
87
88/*
89** Delete the given Select structure and all of its substructures.
90*/
91void sqlite3SelectDelete(Select *p){
92 if( p ){
93 clearSelect(p);
94 sqlite3_free(p);
95 }
96}
97
98/*
99** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
100** type of join. Return an integer constant that expresses that type
101** in terms of the following bit values:
102**
103** JT_INNER
104** JT_CROSS
105** JT_OUTER
106** JT_NATURAL
107** JT_LEFT
108** JT_RIGHT
109**
110** A full outer join is the combination of JT_LEFT and JT_RIGHT.
111**
112** If an illegal or unsupported join type is seen, then still return
113** a join type, but put an error in the pParse structure.
114*/
115int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
116 int jointype = 0;
117 Token *apAll[3];
118 Token *p;
119 static const struct {
120 const char zKeyword[8];
121 u8 nChar;
122 u8 code;
123 } keywords[] = {
124 { "natural", 7, JT_NATURAL },
125 { "left", 4, JT_LEFT|JT_OUTER },
126 { "right", 5, JT_RIGHT|JT_OUTER },
127 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
128 { "outer", 5, JT_OUTER },
129 { "inner", 5, JT_INNER },
130 { "cross", 5, JT_INNER|JT_CROSS },
131 };
132 int i, j;
133 apAll[0] = pA;
134 apAll[1] = pB;
135 apAll[2] = pC;
136 for(i=0; i<3 && apAll[i]; i++){
137 p = apAll[i];
138 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
139 if( p->n==keywords[j].nChar
140 && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
141 jointype |= keywords[j].code;
142 break;
143 }
144 }
145 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
146 jointype |= JT_ERROR;
147 break;
148 }
149 }
150 if(
151 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
152 (jointype & JT_ERROR)!=0
153 ){
154 const char *zSp1 = " ";
155 const char *zSp2 = " ";
156 if( pB==0 ){ zSp1++; }
157 if( pC==0 ){ zSp2++; }
158 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
159 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
160 jointype = JT_INNER;
161 }else if( jointype & JT_RIGHT ){
162 sqlite3ErrorMsg(pParse,
163 "RIGHT and FULL OUTER JOINs are not currently supported");
164 jointype = JT_INNER;
165 }
166 return jointype;
167}
168
169/*
170** Return the index of a column in a table. Return -1 if the column
171** is not contained in the table.
172*/
173static int columnIndex(Table *pTab, const char *zCol){
174 int i;
175 for(i=0; i<pTab->nCol; i++){
176 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
177 }
178 return -1;
179}
180
181/*
182** Set the value of a token to a '\000'-terminated string.
183*/
184static void setToken(Token *p, const char *z){
185 p->z = (u8*)z;
186 p->n = z ? strlen(z) : 0;
187 p->dyn = 0;
188}
189
190/*
191** Set the token to the double-quoted and escaped version of the string pointed
192** to by z. For example;
193**
194** {a"bc} -> {"a""bc"}
195*/
196static void setQuotedToken(Parse *pParse, Token *p, const char *z){
197 p->z = (u8 *)sqlite3MPrintf(0, "\"%w\"", z);
198 p->dyn = 1;
199 if( p->z ){
200 p->n = strlen((char *)p->z);
201 }else{
202 pParse->db->mallocFailed = 1;
203 }
204}
205
206/*
207** Create an expression node for an identifier with the name of zName
208*/
209Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
210 Token dummy;
211 setToken(&dummy, zName);
212 return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy);
213}
214
215
216/*
217** Add a term to the WHERE expression in *ppExpr that requires the
218** zCol column to be equal in the two tables pTab1 and pTab2.
219*/
220static void addWhereTerm(
221 Parse *pParse, /* Parsing context */
222 const char *zCol, /* Name of the column */
223 const Table *pTab1, /* First table */
224 const char *zAlias1, /* Alias for first table. May be NULL */
225 const Table *pTab2, /* Second table */
226 const char *zAlias2, /* Alias for second table. May be NULL */
227 int iRightJoinTable, /* VDBE cursor for the right table */
228 Expr **ppExpr /* Add the equality term to this expression */
229){
230 Expr *pE1a, *pE1b, *pE1c;
231 Expr *pE2a, *pE2b, *pE2c;
232 Expr *pE;
233
234 pE1a = sqlite3CreateIdExpr(pParse, zCol);
235 pE2a = sqlite3CreateIdExpr(pParse, zCol);
236 if( zAlias1==0 ){
237 zAlias1 = pTab1->zName;
238 }
239 pE1b = sqlite3CreateIdExpr(pParse, zAlias1);
240 if( zAlias2==0 ){
241 zAlias2 = pTab2->zName;
242 }
243 pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
244 pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
245 pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
246 pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
247 if( pE ){
248 ExprSetProperty(pE, EP_FromJoin);
249 pE->iRightJoinTable = iRightJoinTable;
250 }
251 pE = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
252 if( pE ){
253 *ppExpr = pE;
254 }
255}
256
257/*
258** Set the EP_FromJoin property on all terms of the given expression.
259** And set the Expr.iRightJoinTable to iTable for every term in the
260** expression.
261**
262** The EP_FromJoin property is used on terms of an expression to tell
263** the LEFT OUTER JOIN processing logic that this term is part of the
264** join restriction specified in the ON or USING clause and not a part
265** of the more general WHERE clause. These terms are moved over to the
266** WHERE clause during join processing but we need to remember that they
267** originated in the ON or USING clause.
268**
269** The Expr.iRightJoinTable tells the WHERE clause processing that the
270** expression depends on table iRightJoinTable even if that table is not
271** explicitly mentioned in the expression. That information is needed
272** for cases like this:
273**
274** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
275**
276** The where clause needs to defer the handling of the t1.x=5
277** term until after the t2 loop of the join. In that way, a
278** NULL t2 row will be inserted whenever t1.x!=5. If we do not
279** defer the handling of t1.x=5, it will be processed immediately
280** after the t1 loop and rows with t1.x!=5 will never appear in
281** the output, which is incorrect.
282*/
283static void setJoinExpr(Expr *p, int iTable){
284 while( p ){
285 ExprSetProperty(p, EP_FromJoin);
286 p->iRightJoinTable = iTable;
287 setJoinExpr(p->pLeft, iTable);
288 p = p->pRight;
289 }
290}
291
292/*
293** This routine processes the join information for a SELECT statement.
294** ON and USING clauses are converted into extra terms of the WHERE clause.
295** NATURAL joins also create extra WHERE clause terms.
296**
297** The terms of a FROM clause are contained in the Select.pSrc structure.
298** The left most table is the first entry in Select.pSrc. The right-most
299** table is the last entry. The join operator is held in the entry to
300** the left. Thus entry 0 contains the join operator for the join between
301** entries 0 and 1. Any ON or USING clauses associated with the join are
302** also attached to the left entry.
303**
304** This routine returns the number of errors encountered.
305*/
306static int sqliteProcessJoin(Parse *pParse, Select *p){
307 SrcList *pSrc; /* All tables in the FROM clause */
308 int i, j; /* Loop counters */
309 struct SrcList_item *pLeft; /* Left table being joined */
310 struct SrcList_item *pRight; /* Right table being joined */
311
312 pSrc = p->pSrc;
313 pLeft = &pSrc->a[0];
314 pRight = &pLeft[1];
315 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
316 Table *pLeftTab = pLeft->pTab;
317 Table *pRightTab = pRight->pTab;
318
319 if( pLeftTab==0 || pRightTab==0 ) continue;
320
321 /* When the NATURAL keyword is present, add WHERE clause terms for
322 ** every column that the two tables have in common.
323 */
324 if( pRight->jointype & JT_NATURAL ){
325 if( pRight->pOn || pRight->pUsing ){
326 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
327 "an ON or USING clause", 0);
328 return 1;
329 }
330 for(j=0; j<pLeftTab->nCol; j++){
331 char *zName = pLeftTab->aCol[j].zName;
332 if( columnIndex(pRightTab, zName)>=0 ){
333 addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
334 pRightTab, pRight->zAlias,
335 pRight->iCursor, &p->pWhere);
336
337 }
338 }
339 }
340
341 /* Disallow both ON and USING clauses in the same join
342 */
343 if( pRight->pOn && pRight->pUsing ){
344 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
345 "clauses in the same join");
346 return 1;
347 }
348
349 /* Add the ON clause to the end of the WHERE clause, connected by
350 ** an AND operator.
351 */
352 if( pRight->pOn ){
353 setJoinExpr(pRight->pOn, pRight->iCursor);
354 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
355 pRight->pOn = 0;
356 }
357
358 /* Create extra terms on the WHERE clause for each column named
359 ** in the USING clause. Example: If the two tables to be joined are
360 ** A and B and the USING clause names X, Y, and Z, then add this
361 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
362 ** Report an error if any column mentioned in the USING clause is
363 ** not contained in both tables to be joined.
364 */
365 if( pRight->pUsing ){
366 IdList *pList = pRight->pUsing;
367 for(j=0; j<pList->nId; j++){
368 char *zName = pList->a[j].zName;
369 if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){
370 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
371 "not present in both tables", zName);
372 return 1;
373 }
374 addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
375 pRightTab, pRight->zAlias,
376 pRight->iCursor, &p->pWhere);
377 }
378 }
379 }
380 return 0;
381}
382
383/*
384** Insert code into "v" that will push the record on the top of the
385** stack into the sorter.
386*/
387static void pushOntoSorter(
388 Parse *pParse, /* Parser context */
389 ExprList *pOrderBy, /* The ORDER BY clause */
390 Select *pSelect /* The whole SELECT statement */
391){
392 Vdbe *v = pParse->pVdbe;
393 sqlite3ExprCodeExprList(pParse, pOrderBy);
394 sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iECursor, 0);
395 sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0);
396 sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0);
397 sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iECursor, 0);
398 if( pSelect->iLimit>=0 ){
399 int addr1, addr2;
400 addr1 = sqlite3VdbeAddOp(v, OP_IfMemZero, pSelect->iLimit+1, 0);
401 sqlite3VdbeAddOp(v, OP_MemIncr, -1, pSelect->iLimit+1);
402 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
403 sqlite3VdbeJumpHere(v, addr1);
404 sqlite3VdbeAddOp(v, OP_Last, pOrderBy->iECursor, 0);
405 sqlite3VdbeAddOp(v, OP_Delete, pOrderBy->iECursor, 0);
406 sqlite3VdbeJumpHere(v, addr2);
407 pSelect->iLimit = -1;
408 }
409}
410
411/*
412** Add code to implement the OFFSET
413*/
414static void codeOffset(
415 Vdbe *v, /* Generate code into this VM */
416 Select *p, /* The SELECT statement being coded */
417 int iContinue, /* Jump here to skip the current record */
418 int nPop /* Number of times to pop stack when jumping */
419){
420 if( p->iOffset>=0 && iContinue!=0 ){
421 int addr;
422 sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iOffset);
423 addr = sqlite3VdbeAddOp(v, OP_IfMemNeg, p->iOffset, 0);
424 if( nPop>0 ){
425 sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
426 }
427 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
428 VdbeComment((v, "# skip OFFSET records"));
429 sqlite3VdbeJumpHere(v, addr);
430 }
431}
432
433/*
434** Add code that will check to make sure the top N elements of the
435** stack are distinct. iTab is a sorting index that holds previously
436** seen combinations of the N values. A new entry is made in iTab
437** if the current N values are new.
438**
439** A jump to addrRepeat is made and the N+1 values are popped from the
440** stack if the top N elements are not distinct.
441*/
442static void codeDistinct(
443 Vdbe *v, /* Generate code into this VM */
444 int iTab, /* A sorting index used to test for distinctness */
445 int addrRepeat, /* Jump to here if not distinct */
446 int N /* The top N elements of the stack must be distinct */
447){
448 sqlite3VdbeAddOp(v, OP_MakeRecord, -N, 0);
449 sqlite3VdbeAddOp(v, OP_Distinct, iTab, sqlite3VdbeCurrentAddr(v)+3);
450 sqlite3VdbeAddOp(v, OP_Pop, N+1, 0);
451 sqlite3VdbeAddOp(v, OP_Goto, 0, addrRepeat);
452 VdbeComment((v, "# skip indistinct records"));
453 sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0);
454}
455
456/*
457** Generate an error message when a SELECT is used within a subexpression
458** (example: "a IN (SELECT * FROM table)") but it has more than 1 result
459** column. We do this in a subroutine because the error occurs in multiple
460** places.
461*/
462static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){
463 if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
464 sqlite3ErrorMsg(pParse, "only a single result allowed for "
465 "a SELECT that is part of an expression");
466 return 1;
467 }else{
468 return 0;
469 }
470}
471
472/*
473** This routine generates the code for the inside of the inner loop
474** of a SELECT.
475**
476** If srcTab and nColumn are both zero, then the pEList expressions
477** are evaluated in order to get the data for this row. If nColumn>0
478** then data is pulled from srcTab and pEList is used only to get the
479** datatypes for each column.
480*/
481static int selectInnerLoop(
482 Parse *pParse, /* The parser context */
483 Select *p, /* The complete select statement being coded */
484 ExprList *pEList, /* List of values being extracted */
485 int srcTab, /* Pull data from this table */
486 int nColumn, /* Number of columns in the source table */
487 ExprList *pOrderBy, /* If not NULL, sort results using this key */
488 int distinct, /* If >=0, make sure results are distinct */
489 int eDest, /* How to dispose of the results */
490 int iParm, /* An argument to the disposal method */
491 int iContinue, /* Jump here to continue with next row */
492 int iBreak, /* Jump here to break out of the inner loop */
493 char *aff /* affinity string if eDest is SRT_Union */
494){
495 Vdbe *v = pParse->pVdbe;
496 int i;
497 int hasDistinct; /* True if the DISTINCT keyword is present */
498
499 if( v==0 ) return 0;
500 assert( pEList!=0 );
501
502 /* If there was a LIMIT clause on the SELECT statement, then do the check
503 ** to see if this row should be output.
504 */
505 hasDistinct = distinct>=0 && pEList->nExpr>0;
506 if( pOrderBy==0 && !hasDistinct ){
507 codeOffset(v, p, iContinue, 0);
508 }
509
510 /* Pull the requested columns.
511 */
512 if( nColumn>0 ){
513 for(i=0; i<nColumn; i++){
514 sqlite3VdbeAddOp(v, OP_Column, srcTab, i);
515 }
516 }else{
517 nColumn = pEList->nExpr;
518 sqlite3ExprCodeExprList(pParse, pEList);
519 }
520
521 /* If the DISTINCT keyword was present on the SELECT statement
522 ** and this row has been seen before, then do not make this row
523 ** part of the result.
524 */
525 if( hasDistinct ){
526 assert( pEList!=0 );
527 assert( pEList->nExpr==nColumn );
528 codeDistinct(v, distinct, iContinue, nColumn);
529 if( pOrderBy==0 ){
530 codeOffset(v, p, iContinue, nColumn);
531 }
532 }
533
534 if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
535 return 0;
536 }
537
538 switch( eDest ){
539 /* In this mode, write each query result to the key of the temporary
540 ** table iParm.
541 */
542#ifndef SQLITE_OMIT_COMPOUND_SELECT
543 case SRT_Union: {
544 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
545 if( aff ){
546 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
547 }
548 sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0);
549 break;
550 }
551
552 /* Construct a record from the query result, but instead of
553 ** saving that record, use it as a key to delete elements from
554 ** the temporary table iParm.
555 */
556 case SRT_Except: {
557 int addr;
558 addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
559 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
560 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3);
561 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0);
562 break;
563 }
564#endif
565
566 /* Store the result as data using a unique key.
567 */
568 case SRT_Table:
569 case SRT_EphemTab: {
570 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
571 if( pOrderBy ){
572 pushOntoSorter(pParse, pOrderBy, p);
573 }else{
574 sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
575 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
576 sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
577 }
578 break;
579 }
580
581#ifndef SQLITE_OMIT_SUBQUERY
582 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
583 ** then there should be a single item on the stack. Write this
584 ** item into the set table with bogus data.
585 */
586 case SRT_Set: {
587 int addr1 = sqlite3VdbeCurrentAddr(v);
588 int addr2;
589
590 assert( nColumn==1 );
591 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
592 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
593 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
594 p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr,(iParm>>16)&0xff);
595 if( pOrderBy ){
596 /* At first glance you would think we could optimize out the
597 ** ORDER BY in this case since the order of entries in the set
598 ** does not matter. But there might be a LIMIT clause, in which
599 ** case the order does matter */
600 pushOntoSorter(pParse, pOrderBy, p);
601 }else{
602 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
603 sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
604 }
605 sqlite3VdbeJumpHere(v, addr2);
606 break;
607 }
608
609 /* If any row exist in the result set, record that fact and abort.
610 */
611 case SRT_Exists: {
612 sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm);
613 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
614 /* The LIMIT clause will terminate the loop for us */
615 break;
616 }
617
618 /* If this is a scalar select that is part of an expression, then
619 ** store the results in the appropriate memory cell and break out
620 ** of the scan loop.
621 */
622 case SRT_Mem: {
623 assert( nColumn==1 );
624 if( pOrderBy ){
625 pushOntoSorter(pParse, pOrderBy, p);
626 }else{
627 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
628 /* The LIMIT clause will jump out of the loop for us */
629 }
630 break;
631 }
632#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
633
634 /* Send the data to the callback function or to a subroutine. In the
635 ** case of a subroutine, the subroutine itself is responsible for
636 ** popping the data from the stack.
637 */
638 case SRT_Subroutine:
639 case SRT_Callback: {
640 if( pOrderBy ){
641 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
642 pushOntoSorter(pParse, pOrderBy, p);
643 }else if( eDest==SRT_Subroutine ){
644 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
645 }else{
646 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
647 }
648 break;
649 }
650
651#if !defined(SQLITE_OMIT_TRIGGER)
652 /* Discard the results. This is used for SELECT statements inside
653 ** the body of a TRIGGER. The purpose of such selects is to call
654 ** user-defined functions that have side effects. We do not care
655 ** about the actual results of the select.
656 */
657 default: {
658 assert( eDest==SRT_Discard );
659 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
660 break;
661 }
662#endif
663 }
664
665 /* Jump to the end of the loop if the LIMIT is reached.
666 */
667 if( p->iLimit>=0 && pOrderBy==0 ){
668 sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
669 sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, iBreak);
670 }
671 return 0;
672}
673
674/*
675** Given an expression list, generate a KeyInfo structure that records
676** the collating sequence for each expression in that expression list.
677**
678** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
679** KeyInfo structure is appropriate for initializing a virtual index to
680** implement that clause. If the ExprList is the result set of a SELECT
681** then the KeyInfo structure is appropriate for initializing a virtual
682** index to implement a DISTINCT test.
683**
684** Space to hold the KeyInfo structure is obtain from malloc. The calling
685** function is responsible for seeing that this structure is eventually
686** freed. Add the KeyInfo structure to the P3 field of an opcode using
687** P3_KEYINFO_HANDOFF is the usual way of dealing with this.
688*/
689static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
690 sqlite3 *db = pParse->db;
691 int nExpr;
692 KeyInfo *pInfo;
693 struct ExprList_item *pItem;
694 int i;
695
696 nExpr = pList->nExpr;
697 pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
698 if( pInfo ){
699 pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
700 pInfo->nField = nExpr;
701 pInfo->enc = ENC(db);
702 for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
703 CollSeq *pColl;
704 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
705 if( !pColl ){
706 pColl = db->pDfltColl;
707 }
708 pInfo->aColl[i] = pColl;
709 pInfo->aSortOrder[i] = pItem->sortOrder;
710 }
711 }
712 return pInfo;
713}
714
715
716/*
717** If the inner loop was generated using a non-null pOrderBy argument,
718** then the results were placed in a sorter. After the loop is terminated
719** we need to run the sorter and output the results. The following
720** routine generates the code needed to do that.
721*/
722static void generateSortTail(
723 Parse *pParse, /* Parsing context */
724 Select *p, /* The SELECT statement */
725 Vdbe *v, /* Generate code into this VDBE */
726 int nColumn, /* Number of columns of data */
727 int eDest, /* Write the sorted results here */
728 int iParm /* Optional parameter associated with eDest */
729){
730 int brk = sqlite3VdbeMakeLabel(v);
731 int cont = sqlite3VdbeMakeLabel(v);
732 int addr;
733 int iTab;
734 int pseudoTab = 0;
735 ExprList *pOrderBy = p->pOrderBy;
736
737 iTab = pOrderBy->iECursor;
738 if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
739 pseudoTab = pParse->nTab++;
740 sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0);
741 sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn);
742 }
743 addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk);
744 codeOffset(v, p, cont, 0);
745 if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
746 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
747 }
748 sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1);
749 switch( eDest ){
750 case SRT_Table:
751 case SRT_EphemTab: {
752 sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
753 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
754 sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
755 break;
756 }
757#ifndef SQLITE_OMIT_SUBQUERY
758 case SRT_Set: {
759 assert( nColumn==1 );
760 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
761 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
762 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
763 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
764 sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
765 break;
766 }
767 case SRT_Mem: {
768 assert( nColumn==1 );
769 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
770 /* The LIMIT clause will terminate the loop for us */
771 break;
772 }
773#endif
774 case SRT_Callback:
775 case SRT_Subroutine: {
776 int i;
777 sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0);
778 for(i=0; i<nColumn; i++){
779 sqlite3VdbeAddOp(v, OP_Column, pseudoTab, i);
780 }
781 if( eDest==SRT_Callback ){
782 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
783 }else{
784 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
785 }
786 break;
787 }
788 default: {
789 /* Do nothing */
790 break;
791 }
792 }
793
794 /* Jump to the end of the loop when the LIMIT is reached
795 */
796 if( p->iLimit>=0 ){
797 sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
798 sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, brk);
799 }
800
801 /* The bottom of the loop
802 */
803 sqlite3VdbeResolveLabel(v, cont);
804 sqlite3VdbeAddOp(v, OP_Next, iTab, addr);
805 sqlite3VdbeResolveLabel(v, brk);
806 if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
807 sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0);
808 }
809
810}
811
812/*
813** Return a pointer to a string containing the 'declaration type' of the
814** expression pExpr. The string may be treated as static by the caller.
815**
816** The declaration type is the exact datatype definition extracted from the
817** original CREATE TABLE statement if the expression is a column. The
818** declaration type for a ROWID field is INTEGER. Exactly when an expression
819** is considered a column can be complex in the presence of subqueries. The
820** result-set expression in all of the following SELECT statements is
821** considered a column by this function.
822**
823** SELECT col FROM tbl;
824** SELECT (SELECT col FROM tbl;
825** SELECT (SELECT col FROM tbl);
826** SELECT abc FROM (SELECT col AS abc FROM tbl);
827**
828** The declaration type for any expression other than a column is NULL.
829*/
830static const char *columnType(
831 NameContext *pNC,
832 Expr *pExpr,
833 const char **pzOriginDb,
834 const char **pzOriginTab,
835 const char **pzOriginCol
836){
837 char const *zType = 0;
838 char const *zOriginDb = 0;
839 char const *zOriginTab = 0;
840 char const *zOriginCol = 0;
841 int j;
842 if( pExpr==0 || pNC->pSrcList==0 ) return 0;
843
844 switch( pExpr->op ){
845 case TK_AGG_COLUMN:
846 case TK_COLUMN: {
847 /* The expression is a column. Locate the table the column is being
848 ** extracted from in NameContext.pSrcList. This table may be real
849 ** database table or a subquery.
850 */
851 Table *pTab = 0; /* Table structure column is extracted from */
852 Select *pS = 0; /* Select the column is extracted from */
853 int iCol = pExpr->iColumn; /* Index of column in pTab */
854 while( pNC && !pTab ){
855 SrcList *pTabList = pNC->pSrcList;
856 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
857 if( j<pTabList->nSrc ){
858 pTab = pTabList->a[j].pTab;
859 pS = pTabList->a[j].pSelect;
860 }else{
861 pNC = pNC->pNext;
862 }
863 }
864
865 if( pTab==0 ){
866 /* FIX ME:
867 ** This can occurs if you have something like "SELECT new.x;" inside
868 ** a trigger. In other words, if you reference the special "new"
869 ** table in the result set of a select. We do not have a good way
870 ** to find the actual table type, so call it "TEXT". This is really
871 ** something of a bug, but I do not know how to fix it.
872 **
873 ** This code does not produce the correct answer - it just prevents
874 ** a segfault. See ticket #1229.
875 */
876 zType = "TEXT";
877 break;
878 }
879
880 assert( pTab );
881 if( pS ){
882 /* The "table" is actually a sub-select or a view in the FROM clause
883 ** of the SELECT statement. Return the declaration type and origin
884 ** data for the result-set column of the sub-select.
885 */
886 if( iCol>=0 && iCol<pS->pEList->nExpr ){
887 /* If iCol is less than zero, then the expression requests the
888 ** rowid of the sub-select or view. This expression is legal (see
889 ** test case misc2.2.2) - it always evaluates to NULL.
890 */
891 NameContext sNC;
892 Expr *p = pS->pEList->a[iCol].pExpr;
893 sNC.pSrcList = pS->pSrc;
894 sNC.pNext = 0;
895 sNC.pParse = pNC->pParse;
896 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
897 }
898 }else if( pTab->pSchema ){
899 /* A real table */
900 assert( !pS );
901 if( iCol<0 ) iCol = pTab->iPKey;
902 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
903 if( iCol<0 ){
904 zType = "INTEGER";
905 zOriginCol = "rowid";
906 }else{
907 zType = pTab->aCol[iCol].zType;
908 zOriginCol = pTab->aCol[iCol].zName;
909 }
910 zOriginTab = pTab->zName;
911 if( pNC->pParse ){
912 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
913 zOriginDb = pNC->pParse->db->aDb[iDb].zName;
914 }
915 }
916 break;
917 }
918#ifndef SQLITE_OMIT_SUBQUERY
919 case TK_SELECT: {
920 /* The expression is a sub-select. Return the declaration type and
921 ** origin info for the single column in the result set of the SELECT
922 ** statement.
923 */
924 NameContext sNC;
925 Select *pS = pExpr->pSelect;
926 Expr *p = pS->pEList->a[0].pExpr;
927 sNC.pSrcList = pS->pSrc;
928 sNC.pNext = pNC;
929 sNC.pParse = pNC->pParse;
930 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
931 break;
932 }
933#endif
934 }
935
936 if( pzOriginDb ){
937 assert( pzOriginTab && pzOriginCol );
938 *pzOriginDb = zOriginDb;
939 *pzOriginTab = zOriginTab;
940 *pzOriginCol = zOriginCol;
941 }
942 return zType;
943}
944
945/*
946** Generate code that will tell the VDBE the declaration types of columns
947** in the result set.
948*/
949static void generateColumnTypes(
950 Parse *pParse, /* Parser context */
951 SrcList *pTabList, /* List of tables */
952 ExprList *pEList /* Expressions defining the result set */
953){
954 Vdbe *v = pParse->pVdbe;
955 int i;
956 NameContext sNC;
957 sNC.pSrcList = pTabList;
958 sNC.pParse = pParse;
959 for(i=0; i<pEList->nExpr; i++){
960 Expr *p = pEList->a[i].pExpr;
961 const char *zOrigDb = 0;
962 const char *zOrigTab = 0;
963 const char *zOrigCol = 0;
964 const char *zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
965
966 /* The vdbe must make it's own copy of the column-type and other
967 ** column specific strings, in case the schema is reset before this
968 ** virtual machine is deleted.
969 */
970 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT);
971 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT);
972 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT);
973 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT);
974 }
975}
976
977/*
978** Generate code that will tell the VDBE the names of columns
979** in the result set. This information is used to provide the
980** azCol[] values in the callback.
981*/
982static void generateColumnNames(
983 Parse *pParse, /* Parser context */
984 SrcList *pTabList, /* List of tables */
985 ExprList *pEList /* Expressions defining the result set */
986){
987 Vdbe *v = pParse->pVdbe;
988 int i, j;
989 sqlite3 *db = pParse->db;
990 int fullNames, shortNames;
991
992#ifndef SQLITE_OMIT_EXPLAIN
993 /* If this is an EXPLAIN, skip this step */
994 if( pParse->explain ){
995 return;
996 }
997#endif
998
999 assert( v!=0 );
1000 if( pParse->colNamesSet || v==0 || db->mallocFailed ) return;
1001 pParse->colNamesSet = 1;
1002 fullNames = (db->flags & SQLITE_FullColNames)!=0;
1003 shortNames = (db->flags & SQLITE_ShortColNames)!=0;
1004 sqlite3VdbeSetNumCols(v, pEList->nExpr);
1005 for(i=0; i<pEList->nExpr; i++){
1006 Expr *p;
1007 p = pEList->a[i].pExpr;
1008 if( p==0 ) continue;
1009 if( pEList->a[i].zName ){
1010 char *zName = pEList->a[i].zName;
1011 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, strlen(zName));
1012 continue;
1013 }
1014 if( p->op==TK_COLUMN && pTabList ){
1015 Table *pTab;
1016 char *zCol;
1017 int iCol = p->iColumn;
1018 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
1019 assert( j<pTabList->nSrc );
1020 pTab = pTabList->a[j].pTab;
1021 if( iCol<0 ) iCol = pTab->iPKey;
1022 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
1023 if( iCol<0 ){
1024 zCol = "rowid";
1025 }else{
1026 zCol = pTab->aCol[iCol].zName;
1027 }
1028 if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
1029 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
1030 }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
1031 char *zName = 0;
1032 char *zTab;
1033
1034 zTab = pTabList->a[j].zAlias;
1035 if( fullNames || zTab==0 ) zTab = pTab->zName;
1036 sqlite3SetString(&zName, zTab, ".", zCol, (char*)0);
1037 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC);
1038 }else{
1039 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol));
1040 }
1041 }else if( p->span.z && p->span.z[0] ){
1042 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
1043 /* sqlite3VdbeCompressSpace(v, addr); */
1044 }else{
1045 char zName[30];
1046 assert( p->op!=TK_COLUMN || pTabList==0 );
1047 sqlite3_snprintf(sizeof(zName), zName, "column%d", i+1);
1048 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0);
1049 }
1050 }
1051 generateColumnTypes(pParse, pTabList, pEList);
1052}
1053
1054#ifndef SQLITE_OMIT_COMPOUND_SELECT
1055/*
1056** Name of the connection operator, used for error messages.
1057*/
1058static const char *selectOpName(int id){
1059 char *z;
1060 switch( id ){
1061 case TK_ALL: z = "UNION ALL"; break;
1062 case TK_INTERSECT: z = "INTERSECT"; break;
1063 case TK_EXCEPT: z = "EXCEPT"; break;
1064 default: z = "UNION"; break;
1065 }
1066 return z;
1067}
1068#endif /* SQLITE_OMIT_COMPOUND_SELECT */
1069
1070/*
1071** Forward declaration
1072*/
1073static int prepSelectStmt(Parse*, Select*);
1074
1075/*
1076** Given a SELECT statement, generate a Table structure that describes
1077** the result set of that SELECT.
1078*/
1079Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
1080 Table *pTab;
1081 int i, j;
1082 ExprList *pEList;
1083 Column *aCol, *pCol;
1084 sqlite3 *db = pParse->db;
1085
1086 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
1087 if( prepSelectStmt(pParse, pSelect) ){
1088 return 0;
1089 }
1090 if( sqlite3SelectResolve(pParse, pSelect, 0) ){
1091 return 0;
1092 }
1093 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
1094 if( pTab==0 ){
1095 return 0;
1096 }
1097 pTab->nRef = 1;
1098 pTab->zName = zTabName ? sqlite3DbStrDup(db, zTabName) : 0;
1099 pEList = pSelect->pEList;
1100 pTab->nCol = pEList->nExpr;
1101 assert( pTab->nCol>0 );
1102 pTab->aCol = aCol = sqlite3DbMallocZero(db, sizeof(pTab->aCol[0])*pTab->nCol);
1103 for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
1104 Expr *p, *pR;
1105 char *zType;
1106 char *zName;
1107 int nName;
1108 CollSeq *pColl;
1109 int cnt;
1110 NameContext sNC;
1111
1112 /* Get an appropriate name for the column
1113 */
1114 p = pEList->a[i].pExpr;
1115 assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
1116 if( (zName = pEList->a[i].zName)!=0 ){
1117 /* If the column contains an "AS <name>" phrase, use <name> as the name */
1118 zName = sqlite3DbStrDup(db, zName);
1119 }else if( p->op==TK_DOT
1120 && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
1121 /* For columns of the from A.B use B as the name */
1122 zName = sqlite3MPrintf(db, "%T", &pR->token);
1123 }else if( p->span.z && p->span.z[0] ){
1124 /* Use the original text of the column expression as its name */
1125 zName = sqlite3MPrintf(db, "%T", &p->span);
1126 }else{
1127 /* If all else fails, make up a name */
1128 zName = sqlite3MPrintf(db, "column%d", i+1);
1129 }
1130 if( !zName || db->mallocFailed ){
1131 db->mallocFailed = 1;
1132 sqlite3_free(zName);
1133 sqlite3DeleteTable(pTab);
1134 return 0;
1135 }
1136 sqlite3Dequote(zName);
1137
1138 /* Make sure the column name is unique. If the name is not unique,
1139 ** append a integer to the name so that it becomes unique.
1140 */
1141 nName = strlen(zName);
1142 for(j=cnt=0; j<i; j++){
1143 if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
1144 zName[nName] = 0;
1145 zName = sqlite3MPrintf(db, "%z:%d", zName, ++cnt);
1146 j = -1;
1147 if( zName==0 ) break;
1148 }
1149 }
1150 pCol->zName = zName;
1151
1152 /* Get the typename, type affinity, and collating sequence for the
1153 ** column.
1154 */
1155 memset(&sNC, 0, sizeof(sNC));
1156 sNC.pSrcList = pSelect->pSrc;
1157 zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
1158 pCol->zType = zType;
1159 pCol->affinity = sqlite3ExprAffinity(p);
1160 pColl = sqlite3ExprCollSeq(pParse, p);
1161 if( pColl ){
1162 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
1163 }
1164 }
1165 pTab->iPKey = -1;
1166 return pTab;
1167}
1168
1169/*
1170** Prepare a SELECT statement for processing by doing the following
1171** things:
1172**
1173** (1) Make sure VDBE cursor numbers have been assigned to every
1174** element of the FROM clause.
1175**
1176** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
1177** defines FROM clause. When views appear in the FROM clause,
1178** fill pTabList->a[].pSelect with a copy of the SELECT statement
1179** that implements the view. A copy is made of the view's SELECT
1180** statement so that we can freely modify or delete that statement
1181** without worrying about messing up the presistent representation
1182** of the view.
1183**
1184** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword
1185** on joins and the ON and USING clause of joins.
1186**
1187** (4) Scan the list of columns in the result set (pEList) looking
1188** for instances of the "*" operator or the TABLE.* operator.
1189** If found, expand each "*" to be every column in every table
1190** and TABLE.* to be every column in TABLE.
1191**
1192** Return 0 on success. If there are problems, leave an error message
1193** in pParse and return non-zero.
1194*/
1195static int prepSelectStmt(Parse *pParse, Select *p){
1196 int i, j, k, rc;
1197 SrcList *pTabList;
1198 ExprList *pEList;
1199 struct SrcList_item *pFrom;
1200 sqlite3 *db = pParse->db;
1201
1202 if( p==0 || p->pSrc==0 || db->mallocFailed ){
1203 return 1;
1204 }
1205 pTabList = p->pSrc;
1206 pEList = p->pEList;
1207
1208 /* Make sure cursor numbers have been assigned to all entries in
1209 ** the FROM clause of the SELECT statement.
1210 */
1211 sqlite3SrcListAssignCursors(pParse, p->pSrc);
1212
1213 /* Look up every table named in the FROM clause of the select. If
1214 ** an entry of the FROM clause is a subquery instead of a table or view,
1215 ** then create a transient table structure to describe the subquery.
1216 */
1217 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
1218 Table *pTab;
1219 if( pFrom->pTab!=0 ){
1220 /* This statement has already been prepared. There is no need
1221 ** to go further. */
1222 assert( i==0 );
1223 return 0;
1224 }
1225 if( pFrom->zName==0 ){
1226#ifndef SQLITE_OMIT_SUBQUERY
1227 /* A sub-query in the FROM clause of a SELECT */
1228 assert( pFrom->pSelect!=0 );
1229 if( pFrom->zAlias==0 ){
1230 pFrom->zAlias =
1231 sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pFrom->pSelect);
1232 }
1233 assert( pFrom->pTab==0 );
1234 pFrom->pTab = pTab =
1235 sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect);
1236 if( pTab==0 ){
1237 return 1;
1238 }
1239 /* The isEphem flag indicates that the Table structure has been
1240 ** dynamically allocated and may be freed at any time. In other words,
1241 ** pTab is not pointing to a persistent table structure that defines
1242 ** part of the schema. */
1243 pTab->isEphem = 1;
1244#endif
1245 }else{
1246 /* An ordinary table or view name in the FROM clause */
1247 assert( pFrom->pTab==0 );
1248 pFrom->pTab = pTab =
1249 sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase);
1250 if( pTab==0 ){
1251 return 1;
1252 }
1253 pTab->nRef++;
1254#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
1255 if( pTab->pSelect || IsVirtual(pTab) ){
1256 /* We reach here if the named table is a really a view */
1257 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1258 return 1;
1259 }
1260 /* If pFrom->pSelect!=0 it means we are dealing with a
1261 ** view within a view. The SELECT structure has already been
1262 ** copied by the outer view so we can skip the copy step here
1263 ** in the inner view.
1264 */
1265 if( pFrom->pSelect==0 ){
1266 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect);
1267 }
1268 }
1269#endif
1270 }
1271 }
1272
1273 /* Process NATURAL keywords, and ON and USING clauses of joins.
1274 */
1275 if( sqliteProcessJoin(pParse, p) ) return 1;
1276
1277 /* For every "*" that occurs in the column list, insert the names of
1278 ** all columns in all tables. And for every TABLE.* insert the names
1279 ** of all columns in TABLE. The parser inserted a special expression
1280 ** with the TK_ALL operator for each "*" that it found in the column list.
1281 ** The following code just has to locate the TK_ALL expressions and expand
1282 ** each one to the list of all columns in all tables.
1283 **
1284 ** The first loop just checks to see if there are any "*" operators
1285 ** that need expanding.
1286 */
1287 for(k=0; k<pEList->nExpr; k++){
1288 Expr *pE = pEList->a[k].pExpr;
1289 if( pE->op==TK_ALL ) break;
1290 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
1291 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
1292 }
1293 rc = 0;
1294 if( k<pEList->nExpr ){
1295 /*
1296 ** If we get here it means the result set contains one or more "*"
1297 ** operators that need to be expanded. Loop through each expression
1298 ** in the result set and expand them one by one.
1299 */
1300 struct ExprList_item *a = pEList->a;
1301 ExprList *pNew = 0;
1302 int flags = pParse->db->flags;
1303 int longNames = (flags & SQLITE_FullColNames)!=0 &&
1304 (flags & SQLITE_ShortColNames)==0;
1305
1306 for(k=0; k<pEList->nExpr; k++){
1307 Expr *pE = a[k].pExpr;
1308 if( pE->op!=TK_ALL &&
1309 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
1310 /* This particular expression does not need to be expanded.
1311 */
1312 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
1313 if( pNew ){
1314 pNew->a[pNew->nExpr-1].zName = a[k].zName;
1315 }else{
1316 rc = 1;
1317 }
1318 a[k].pExpr = 0;
1319 a[k].zName = 0;
1320 }else{
1321 /* This expression is a "*" or a "TABLE.*" and needs to be
1322 ** expanded. */
1323 int tableSeen = 0; /* Set to 1 when TABLE matches */
1324 char *zTName; /* text of name of TABLE */
1325 if( pE->op==TK_DOT && pE->pLeft ){
1326 zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
1327 }else{
1328 zTName = 0;
1329 }
1330 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
1331 Table *pTab = pFrom->pTab;
1332 char *zTabName = pFrom->zAlias;
1333 if( zTabName==0 || zTabName[0]==0 ){
1334 zTabName = pTab->zName;
1335 }
1336 if( zTName && (zTabName==0 || zTabName[0]==0 ||
1337 sqlite3StrICmp(zTName, zTabName)!=0) ){
1338 continue;
1339 }
1340 tableSeen = 1;
1341 for(j=0; j<pTab->nCol; j++){
1342 Expr *pExpr, *pRight;
1343 char *zName = pTab->aCol[j].zName;
1344
1345 /* If a column is marked as 'hidden' (currently only possible
1346 ** for virtual tables), do not include it in the expanded
1347 ** result-set list.
1348 */
1349 if( IsHiddenColumn(&pTab->aCol[j]) ){
1350 assert(IsVirtual(pTab));
1351 continue;
1352 }
1353
1354 if( i>0 ){
1355 struct SrcList_item *pLeft = &pTabList->a[i-1];
1356 if( (pLeft[1].jointype & JT_NATURAL)!=0 &&
1357 columnIndex(pLeft->pTab, zName)>=0 ){
1358 /* In a NATURAL join, omit the join columns from the
1359 ** table on the right */
1360 continue;
1361 }
1362 if( sqlite3IdListIndex(pLeft[1].pUsing, zName)>=0 ){
1363 /* In a join with a USING clause, omit columns in the
1364 ** using clause from the table on the right. */
1365 continue;
1366 }
1367 }
1368 pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
1369 if( pRight==0 ) break;
1370 setQuotedToken(pParse, &pRight->token, zName);
1371 if( zTabName && (longNames || pTabList->nSrc>1) ){
1372 Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
1373 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
1374 if( pExpr==0 ) break;
1375 setQuotedToken(pParse, &pLeft->token, zTabName);
1376 setToken(&pExpr->span,
1377 sqlite3MPrintf(db, "%s.%s", zTabName, zName));
1378 pExpr->span.dyn = 1;
1379 pExpr->token.z = 0;
1380 pExpr->token.n = 0;
1381 pExpr->token.dyn = 0;
1382 }else{
1383 pExpr = pRight;
1384 pExpr->span = pExpr->token;
1385 pExpr->span.dyn = 0;
1386 }
1387 if( longNames ){
1388 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span);
1389 }else{
1390 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token);
1391 }
1392 }
1393 }
1394 if( !tableSeen ){
1395 if( zTName ){
1396 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
1397 }else{
1398 sqlite3ErrorMsg(pParse, "no tables specified");
1399 }
1400 rc = 1;
1401 }
1402 sqlite3_free(zTName);
1403 }
1404 }
1405 sqlite3ExprListDelete(pEList);
1406 p->pEList = pNew;
1407 }
1408 if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){
1409 sqlite3ErrorMsg(pParse, "too many columns in result set");
1410 rc = SQLITE_ERROR;
1411 }
1412 if( db->mallocFailed ){
1413 rc = SQLITE_NOMEM;
1414 }
1415 return rc;
1416}
1417
1418#ifndef SQLITE_OMIT_COMPOUND_SELECT
1419/*
1420** This routine associates entries in an ORDER BY expression list with
1421** columns in a result. For each ORDER BY expression, the opcode of
1422** the top-level node is changed to TK_COLUMN and the iColumn value of
1423** the top-level node is filled in with column number and the iTable
1424** value of the top-level node is filled with iTable parameter.
1425**
1426** If there are prior SELECT clauses, they are processed first. A match
1427** in an earlier SELECT takes precedence over a later SELECT.
1428**
1429** Any entry that does not match is flagged as an error. The number
1430** of errors is returned.
1431*/
1432static int matchOrderbyToColumn(
1433 Parse *pParse, /* A place to leave error messages */
1434 Select *pSelect, /* Match to result columns of this SELECT */
1435 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
1436 int iTable, /* Insert this value in iTable */
1437 int mustComplete /* If TRUE all ORDER BYs must match */
1438){
1439 int nErr = 0;
1440 int i, j;
1441 ExprList *pEList;
1442 sqlite3 *db = pParse->db;
1443
1444 if( pSelect==0 || pOrderBy==0 ) return 1;
1445 if( mustComplete ){
1446 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1447 }
1448 if( prepSelectStmt(pParse, pSelect) ){
1449 return 1;
1450 }
1451 if( pSelect->pPrior ){
1452 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1453 return 1;
1454 }
1455 }
1456 pEList = pSelect->pEList;
1457 for(i=0; i<pOrderBy->nExpr; i++){
1458 struct ExprList_item *pItem;
1459 Expr *pE = pOrderBy->a[i].pExpr;
1460 int iCol = -1;
1461 char *zLabel;
1462
1463 if( pOrderBy->a[i].done ) continue;
1464 if( sqlite3ExprIsInteger(pE, &iCol) ){
1465 if( iCol<=0 || iCol>pEList->nExpr ){
1466 sqlite3ErrorMsg(pParse,
1467 "ORDER BY position %d should be between 1 and %d",
1468 iCol, pEList->nExpr);
1469 nErr++;
1470 break;
1471 }
1472 if( !mustComplete ) continue;
1473 iCol--;
1474 }
1475 if( iCol<0 && (zLabel = sqlite3NameFromToken(db, &pE->token))!=0 ){
1476 for(j=0, pItem=pEList->a; j<pEList->nExpr; j++, pItem++){
1477 char *zName;
1478 int isMatch;
1479 if( pItem->zName ){
1480 zName = sqlite3DbStrDup(db, pItem->zName);
1481 }else{
1482 zName = sqlite3NameFromToken(db, &pItem->pExpr->token);
1483 }
1484 isMatch = zName && sqlite3StrICmp(zName, zLabel)==0;
1485 sqlite3_free(zName);
1486 if( isMatch ){
1487 iCol = j;
1488 break;
1489 }
1490 }
1491 sqlite3_free(zLabel);
1492 }
1493 if( iCol>=0 ){
1494 pE->op = TK_COLUMN;
1495 pE->iColumn = iCol;
1496 pE->iTable = iTable;
1497 pE->iAgg = -1;
1498 pOrderBy->a[i].done = 1;
1499 }else if( mustComplete ){
1500 sqlite3ErrorMsg(pParse,
1501 "ORDER BY term number %d does not match any result column", i+1);
1502 nErr++;
1503 break;
1504 }
1505 }
1506 return nErr;
1507}
1508#endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */
1509
1510/*
1511** Get a VDBE for the given parser context. Create a new one if necessary.
1512** If an error occurs, return NULL and leave a message in pParse.
1513*/
1514Vdbe *sqlite3GetVdbe(Parse *pParse){
1515 Vdbe *v = pParse->pVdbe;
1516 if( v==0 ){
1517 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
1518 }
1519 return v;
1520}
1521
1522
1523/*
1524** Compute the iLimit and iOffset fields of the SELECT based on the
1525** pLimit and pOffset expressions. pLimit and pOffset hold the expressions
1526** that appear in the original SQL statement after the LIMIT and OFFSET
1527** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
1528** are the integer memory register numbers for counters used to compute
1529** the limit and offset. If there is no limit and/or offset, then
1530** iLimit and iOffset are negative.
1531**
1532** This routine changes the values of iLimit and iOffset only if
1533** a limit or offset is defined by pLimit and pOffset. iLimit and
1534** iOffset should have been preset to appropriate default values
1535** (usually but not always -1) prior to calling this routine.
1536** Only if pLimit!=0 or pOffset!=0 do the limit registers get
1537** redefined. The UNION ALL operator uses this property to force
1538** the reuse of the same limit and offset registers across multiple
1539** SELECT statements.
1540*/
1541static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
1542 Vdbe *v = 0;
1543 int iLimit = 0;
1544 int iOffset;
1545 int addr1, addr2;
1546
1547 /*
1548 ** "LIMIT -1" always shows all rows. There is some
1549 ** contraversy about what the correct behavior should be.
1550 ** The current implementation interprets "LIMIT 0" to mean
1551 ** no rows.
1552 */
1553 if( p->pLimit ){
1554 p->iLimit = iLimit = pParse->nMem;
1555 pParse->nMem += 2;
1556 v = sqlite3GetVdbe(pParse);
1557 if( v==0 ) return;
1558 sqlite3ExprCode(pParse, p->pLimit);
1559 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
1560 sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1);
1561 VdbeComment((v, "# LIMIT counter"));
1562 sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak);
1563 sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0);
1564 }
1565 if( p->pOffset ){
1566 p->iOffset = iOffset = pParse->nMem++;
1567 v = sqlite3GetVdbe(pParse);
1568 if( v==0 ) return;
1569 sqlite3ExprCode(pParse, p->pOffset);
1570 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
1571 sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0);
1572 VdbeComment((v, "# OFFSET counter"));
1573 addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0);
1574 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1575 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1576 sqlite3VdbeJumpHere(v, addr1);
1577 if( p->pLimit ){
1578 sqlite3VdbeAddOp(v, OP_Add, 0, 0);
1579 }
1580 }
1581 if( p->pLimit ){
1582 addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0);
1583 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1584 sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1);
1585 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
1586 sqlite3VdbeJumpHere(v, addr1);
1587 sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1);
1588 VdbeComment((v, "# LIMIT+OFFSET"));
1589 sqlite3VdbeJumpHere(v, addr2);
1590 }
1591}
1592
1593/*
1594** Allocate a virtual index to use for sorting.
1595*/
1596static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){
1597 if( pOrderBy ){
1598 int addr;
1599 assert( pOrderBy->iECursor==0 );
1600 pOrderBy->iECursor = pParse->nTab++;
1601 addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral,
1602 pOrderBy->iECursor, pOrderBy->nExpr+1);
1603 assert( p->addrOpenEphm[2] == -1 );
1604 p->addrOpenEphm[2] = addr;
1605 }
1606}
1607
1608#ifndef SQLITE_OMIT_COMPOUND_SELECT
1609/*
1610** Return the appropriate collating sequence for the iCol-th column of
1611** the result set for the compound-select statement "p". Return NULL if
1612** the column has no default collating sequence.
1613**
1614** The collating sequence for the compound select is taken from the
1615** left-most term of the select that has a collating sequence.
1616*/
1617static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
1618 CollSeq *pRet;
1619 if( p->pPrior ){
1620 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
1621 }else{
1622 pRet = 0;
1623 }
1624 if( pRet==0 ){
1625 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
1626 }
1627 return pRet;
1628}
1629#endif /* SQLITE_OMIT_COMPOUND_SELECT */
1630
1631#ifndef SQLITE_OMIT_COMPOUND_SELECT
1632/*
1633** This routine is called to process a query that is really the union
1634** or intersection of two or more separate queries.
1635**
1636** "p" points to the right-most of the two queries. the query on the
1637** left is p->pPrior. The left query could also be a compound query
1638** in which case this routine will be called recursively.
1639**
1640** The results of the total query are to be written into a destination
1641** of type eDest with parameter iParm.
1642**
1643** Example 1: Consider a three-way compound SQL statement.
1644**
1645** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1646**
1647** This statement is parsed up as follows:
1648**
1649** SELECT c FROM t3
1650** |
1651** `-----> SELECT b FROM t2
1652** |
1653** `------> SELECT a FROM t1
1654**
1655** The arrows in the diagram above represent the Select.pPrior pointer.
1656** So if this routine is called with p equal to the t3 query, then
1657** pPrior will be the t2 query. p->op will be TK_UNION in this case.
1658**
1659** Notice that because of the way SQLite parses compound SELECTs, the
1660** individual selects always group from left to right.
1661*/
1662static int multiSelect(
1663 Parse *pParse, /* Parsing context */
1664 Select *p, /* The right-most of SELECTs to be coded */
1665 int eDest, /* \___ Store query results as specified */
1666 int iParm, /* / by these two parameters. */
1667 char *aff /* If eDest is SRT_Union, the affinity string */
1668){
1669 int rc = SQLITE_OK; /* Success code from a subroutine */
1670 Select *pPrior; /* Another SELECT immediately to our left */
1671 Vdbe *v; /* Generate code to this VDBE */
1672 int nCol; /* Number of columns in the result set */
1673 ExprList *pOrderBy; /* The ORDER BY clause on p */
1674 int aSetP2[2]; /* Set P2 value of these op to number of columns */
1675 int nSetP2 = 0; /* Number of slots in aSetP2[] used */
1676
1677 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
1678 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
1679 */
1680 if( p==0 || p->pPrior==0 ){
1681 rc = 1;
1682 goto multi_select_end;
1683 }
1684 pPrior = p->pPrior;
1685 assert( pPrior->pRightmost!=pPrior );
1686 assert( pPrior->pRightmost==p->pRightmost );
1687 if( pPrior->pOrderBy ){
1688 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
1689 selectOpName(p->op));
1690 rc = 1;
1691 goto multi_select_end;
1692 }
1693 if( pPrior->pLimit ){
1694 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
1695 selectOpName(p->op));
1696 rc = 1;
1697 goto multi_select_end;
1698 }
1699
1700 /* Make sure we have a valid query engine. If not, create a new one.
1701 */
1702 v = sqlite3GetVdbe(pParse);
1703 if( v==0 ){
1704 rc = 1;
1705 goto multi_select_end;
1706 }
1707
1708 /* Create the destination temporary table if necessary
1709 */
1710 if( eDest==SRT_EphemTab ){
1711 assert( p->pEList );
1712 assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
1713 aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 0);
1714 eDest = SRT_Table;
1715 }
1716
1717 /* Generate code for the left and right SELECT statements.
1718 */
1719 pOrderBy = p->pOrderBy;
1720 switch( p->op ){
1721 case TK_ALL: {
1722 if( pOrderBy==0 ){
1723 int addr = 0;
1724 assert( !pPrior->pLimit );
1725 pPrior->pLimit = p->pLimit;
1726 pPrior->pOffset = p->pOffset;
1727 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);
1728 p->pLimit = 0;
1729 p->pOffset = 0;
1730 if( rc ){
1731 goto multi_select_end;
1732 }
1733 p->pPrior = 0;
1734 p->iLimit = pPrior->iLimit;
1735 p->iOffset = pPrior->iOffset;
1736 if( p->iLimit>=0 ){
1737 addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0);
1738 VdbeComment((v, "# Jump ahead if LIMIT reached"));
1739 }
1740 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);
1741 p->pPrior = pPrior;
1742 if( rc ){
1743 goto multi_select_end;
1744 }
1745 if( addr ){
1746 sqlite3VdbeJumpHere(v, addr);
1747 }
1748 break;
1749 }
1750 /* For UNION ALL ... ORDER BY fall through to the next case */
1751 }
1752 case TK_EXCEPT:
1753 case TK_UNION: {
1754 int unionTab; /* Cursor number of the temporary table holding result */
1755 int op = 0; /* One of the SRT_ operations to apply to self */
1756 int priorOp; /* The SRT_ operation to apply to prior selects */
1757 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
1758 int addr;
1759
1760 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
1761 if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){
1762 /* We can reuse a temporary table generated by a SELECT to our
1763 ** right.
1764 */
1765 unionTab = iParm;
1766 }else{
1767 /* We will need to create our own temporary table to hold the
1768 ** intermediate results.
1769 */
1770 unionTab = pParse->nTab++;
1771 if( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){
1772 rc = 1;
1773 goto multi_select_end;
1774 }
1775 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0);
1776 if( priorOp==SRT_Table ){
1777 assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
1778 aSetP2[nSetP2++] = addr;
1779 }else{
1780 assert( p->addrOpenEphm[0] == -1 );
1781 p->addrOpenEphm[0] = addr;
1782 p->pRightmost->usesEphm = 1;
1783 }
1784 createSortingIndex(pParse, p, pOrderBy);
1785 assert( p->pEList );
1786 }
1787
1788 /* Code the SELECT statements to our left
1789 */
1790 assert( !pPrior->pOrderBy );
1791 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff);
1792 if( rc ){
1793 goto multi_select_end;
1794 }
1795
1796 /* Code the current SELECT statement
1797 */
1798 switch( p->op ){
1799 case TK_EXCEPT: op = SRT_Except; break;
1800 case TK_UNION: op = SRT_Union; break;
1801 case TK_ALL: op = SRT_Table; break;
1802 }
1803 p->pPrior = 0;
1804 p->pOrderBy = 0;
1805 p->disallowOrderBy = pOrderBy!=0;
1806 pLimit = p->pLimit;
1807 p->pLimit = 0;
1808 pOffset = p->pOffset;
1809 p->pOffset = 0;
1810 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);
1811 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
1812 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
1813 sqlite3ExprListDelete(p->pOrderBy);
1814 p->pPrior = pPrior;
1815 p->pOrderBy = pOrderBy;
1816 sqlite3ExprDelete(p->pLimit);
1817 p->pLimit = pLimit;
1818 p->pOffset = pOffset;
1819 p->iLimit = -1;
1820 p->iOffset = -1;
1821 if( rc ){
1822 goto multi_select_end;
1823 }
1824
1825
1826 /* Convert the data in the temporary table into whatever form
1827 ** it is that we currently need.
1828 */
1829 if( eDest!=priorOp || unionTab!=iParm ){
1830 int iCont, iBreak, iStart;
1831 assert( p->pEList );
1832 if( eDest==SRT_Callback ){
1833 Select *pFirst = p;
1834 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
1835 generateColumnNames(pParse, 0, pFirst->pEList);
1836 }
1837 iBreak = sqlite3VdbeMakeLabel(v);
1838 iCont = sqlite3VdbeMakeLabel(v);
1839 computeLimitRegisters(pParse, p, iBreak);
1840 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1841 iStart = sqlite3VdbeCurrentAddr(v);
1842 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
1843 pOrderBy, -1, eDest, iParm,
1844 iCont, iBreak, 0);
1845 if( rc ){
1846 rc = 1;
1847 goto multi_select_end;
1848 }
1849 sqlite3VdbeResolveLabel(v, iCont);
1850 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);
1851 sqlite3VdbeResolveLabel(v, iBreak);
1852 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);
1853 }
1854 break;
1855 }
1856 case TK_INTERSECT: {
1857 int tab1, tab2;
1858 int iCont, iBreak, iStart;
1859 Expr *pLimit, *pOffset;
1860 int addr;
1861
1862 /* INTERSECT is different from the others since it requires
1863 ** two temporary tables. Hence it has its own case. Begin
1864 ** by allocating the tables we will need.
1865 */
1866 tab1 = pParse->nTab++;
1867 tab2 = pParse->nTab++;
1868 if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){
1869 rc = 1;
1870 goto multi_select_end;
1871 }
1872 createSortingIndex(pParse, p, pOrderBy);
1873
1874 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0);
1875 assert( p->addrOpenEphm[0] == -1 );
1876 p->addrOpenEphm[0] = addr;
1877 p->pRightmost->usesEphm = 1;
1878 assert( p->pEList );
1879
1880 /* Code the SELECTs to our left into temporary table "tab1".
1881 */
1882 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff);
1883 if( rc ){
1884 goto multi_select_end;
1885 }
1886
1887 /* Code the current SELECT into temporary table "tab2"
1888 */
1889 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0);
1890 assert( p->addrOpenEphm[1] == -1 );
1891 p->addrOpenEphm[1] = addr;
1892 p->pPrior = 0;
1893 pLimit = p->pLimit;
1894 p->pLimit = 0;
1895 pOffset = p->pOffset;
1896 p->pOffset = 0;
1897 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff);
1898 p->pPrior = pPrior;
1899 sqlite3ExprDelete(p->pLimit);
1900 p->pLimit = pLimit;
1901 p->pOffset = pOffset;
1902 if( rc ){
1903 goto multi_select_end;
1904 }
1905
1906 /* Generate code to take the intersection of the two temporary
1907 ** tables.
1908 */
1909 assert( p->pEList );
1910 if( eDest==SRT_Callback ){
1911 Select *pFirst = p;
1912 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
1913 generateColumnNames(pParse, 0, pFirst->pEList);
1914 }
1915 iBreak = sqlite3VdbeMakeLabel(v);
1916 iCont = sqlite3VdbeMakeLabel(v);
1917 computeLimitRegisters(pParse, p, iBreak);
1918 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);
1919 iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0);
1920 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);
1921 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
1922 pOrderBy, -1, eDest, iParm,
1923 iCont, iBreak, 0);
1924 if( rc ){
1925 rc = 1;
1926 goto multi_select_end;
1927 }
1928 sqlite3VdbeResolveLabel(v, iCont);
1929 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);
1930 sqlite3VdbeResolveLabel(v, iBreak);
1931 sqlite3VdbeAddOp(v, OP_Close, tab2, 0);
1932 sqlite3VdbeAddOp(v, OP_Close, tab1, 0);
1933 break;
1934 }
1935 }
1936
1937 /* Make sure all SELECTs in the statement have the same number of elements
1938 ** in their result sets.
1939 */
1940 assert( p->pEList && pPrior->pEList );
1941 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
1942 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
1943 " do not have the same number of result columns", selectOpName(p->op));
1944 rc = 1;
1945 goto multi_select_end;
1946 }
1947
1948 /* Set the number of columns in temporary tables
1949 */
1950 nCol = p->pEList->nExpr;
1951 while( nSetP2 ){
1952 sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol);
1953 }
1954
1955 /* Compute collating sequences used by either the ORDER BY clause or
1956 ** by any temporary tables needed to implement the compound select.
1957 ** Attach the KeyInfo structure to all temporary tables. Invoke the
1958 ** ORDER BY processing if there is an ORDER BY clause.
1959 **
1960 ** This section is run by the right-most SELECT statement only.
1961 ** SELECT statements to the left always skip this part. The right-most
1962 ** SELECT might also skip this part if it has no ORDER BY clause and
1963 ** no temp tables are required.
1964 */
1965 if( pOrderBy || p->usesEphm ){
1966 int i; /* Loop counter */
1967 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
1968 Select *pLoop; /* For looping through SELECT statements */
1969 int nKeyCol; /* Number of entries in pKeyInfo->aCol[] */
1970 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
1971 CollSeq **aCopy; /* A copy of pKeyInfo->aColl[] */
1972
1973 assert( p->pRightmost==p );
1974 nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0);
1975 pKeyInfo = sqlite3DbMallocZero(pParse->db,
1976 sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));
1977 if( !pKeyInfo ){
1978 rc = SQLITE_NOMEM;
1979 goto multi_select_end;
1980 }
1981
1982 pKeyInfo->enc = ENC(pParse->db);
1983 pKeyInfo->nField = nCol;
1984
1985 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
1986 *apColl = multiSelectCollSeq(pParse, p, i);
1987 if( 0==*apColl ){
1988 *apColl = pParse->db->pDfltColl;
1989 }
1990 }
1991
1992 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
1993 for(i=0; i<2; i++){
1994 int addr = pLoop->addrOpenEphm[i];
1995 if( addr<0 ){
1996 /* If [0] is unused then [1] is also unused. So we can
1997 ** always safely abort as soon as the first unused slot is found */
1998 assert( pLoop->addrOpenEphm[1]<0 );
1999 break;
2000 }
2001 sqlite3VdbeChangeP2(v, addr, nCol);
2002 sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO);
2003 pLoop->addrOpenEphm[i] = -1;
2004 }
2005 }
2006
2007 if( pOrderBy ){
2008 struct ExprList_item *pOTerm = pOrderBy->a;
2009 int nOrderByExpr = pOrderBy->nExpr;
2010 int addr;
2011 u8 *pSortOrder;
2012
2013 /* Reuse the same pKeyInfo for the ORDER BY as was used above for
2014 ** the compound select statements. Except we have to change out the
2015 ** pKeyInfo->aColl[] values. Some of the aColl[] values will be
2016 ** reused when constructing the pKeyInfo for the ORDER BY, so make
2017 ** a copy. Sufficient space to hold both the nCol entries for
2018 ** the compound select and the nOrderbyExpr entries for the ORDER BY
2019 ** was allocated above. But we need to move the compound select
2020 ** entries out of the way before constructing the ORDER BY entries.
2021 ** Move the compound select entries into aCopy[] where they can be
2022 ** accessed and reused when constructing the ORDER BY entries.
2023 ** Because nCol might be greater than or less than nOrderByExpr
2024 ** we have to use memmove() when doing the copy.
2025 */
2026 aCopy = &pKeyInfo->aColl[nOrderByExpr];
2027 pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol];
2028 memmove(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*));
2029
2030 apColl = pKeyInfo->aColl;
2031 for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){
2032 Expr *pExpr = pOTerm->pExpr;
2033 if( (pExpr->flags & EP_ExpCollate) ){
2034 assert( pExpr->pColl!=0 );
2035 *apColl = pExpr->pColl;
2036 }else{
2037 *apColl = aCopy[pExpr->iColumn];
2038 }
2039 *pSortOrder = pOTerm->sortOrder;
2040 }
2041 assert( p->pRightmost==p );
2042 assert( p->addrOpenEphm[2]>=0 );
2043 addr = p->addrOpenEphm[2];
2044 sqlite3VdbeChangeP2(v, addr, p->pOrderBy->nExpr+2);
2045 pKeyInfo->nField = nOrderByExpr;
2046 sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
2047 pKeyInfo = 0;
2048 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
2049 }
2050
2051 sqlite3_free(pKeyInfo);
2052 }
2053
2054multi_select_end:
2055 return rc;
2056}
2057#endif /* SQLITE_OMIT_COMPOUND_SELECT */
2058
2059#ifndef SQLITE_OMIT_VIEW
2060/* Forward Declarations */
2061static void substExprList(sqlite3*, ExprList*, int, ExprList*);
2062static void substSelect(sqlite3*, Select *, int, ExprList *);
2063
2064/*
2065** Scan through the expression pExpr. Replace every reference to
2066** a column in table number iTable with a copy of the iColumn-th
2067** entry in pEList. (But leave references to the ROWID column
2068** unchanged.)
2069**
2070** This routine is part of the flattening procedure. A subquery
2071** whose result set is defined by pEList appears as entry in the
2072** FROM clause of a SELECT such that the VDBE cursor assigned to that
2073** FORM clause entry is iTable. This routine make the necessary
2074** changes to pExpr so that it refers directly to the source table
2075** of the subquery rather the result set of the subquery.
2076*/
2077static void substExpr(
2078 sqlite3 *db, /* Report malloc errors to this connection */
2079 Expr *pExpr, /* Expr in which substitution occurs */
2080 int iTable, /* Table to be substituted */
2081 ExprList *pEList /* Substitute expressions */
2082){
2083 if( pExpr==0 ) return;
2084 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
2085 if( pExpr->iColumn<0 ){
2086 pExpr->op = TK_NULL;
2087 }else{
2088 Expr *pNew;
2089 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
2090 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
2091 pNew = pEList->a[pExpr->iColumn].pExpr;
2092 assert( pNew!=0 );
2093 pExpr->op = pNew->op;
2094 assert( pExpr->pLeft==0 );
2095 pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft);
2096 assert( pExpr->pRight==0 );
2097 pExpr->pRight = sqlite3ExprDup(db, pNew->pRight);
2098 assert( pExpr->pList==0 );
2099 pExpr->pList = sqlite3ExprListDup(db, pNew->pList);
2100 pExpr->iTable = pNew->iTable;
2101 pExpr->pTab = pNew->pTab;
2102 pExpr->iColumn = pNew->iColumn;
2103 pExpr->iAgg = pNew->iAgg;
2104 sqlite3TokenCopy(db, &pExpr->token, &pNew->token);
2105 sqlite3TokenCopy(db, &pExpr->span, &pNew->span);
2106 pExpr->pSelect = sqlite3SelectDup(db, pNew->pSelect);
2107 pExpr->flags = pNew->flags;
2108 }
2109 }else{
2110 substExpr(db, pExpr->pLeft, iTable, pEList);
2111 substExpr(db, pExpr->pRight, iTable, pEList);
2112 substSelect(db, pExpr->pSelect, iTable, pEList);
2113 substExprList(db, pExpr->pList, iTable, pEList);
2114 }
2115}
2116static void substExprList(
2117 sqlite3 *db, /* Report malloc errors here */
2118 ExprList *pList, /* List to scan and in which to make substitutes */
2119 int iTable, /* Table to be substituted */
2120 ExprList *pEList /* Substitute values */
2121){
2122 int i;
2123 if( pList==0 ) return;
2124 for(i=0; i<pList->nExpr; i++){
2125 substExpr(db, pList->a[i].pExpr, iTable, pEList);
2126 }
2127}
2128static void substSelect(
2129 sqlite3 *db, /* Report malloc errors here */
2130 Select *p, /* SELECT statement in which to make substitutions */
2131 int iTable, /* Table to be replaced */
2132 ExprList *pEList /* Substitute values */
2133){
2134 if( !p ) return;
2135 substExprList(db, p->pEList, iTable, pEList);
2136 substExprList(db, p->pGroupBy, iTable, pEList);
2137 substExprList(db, p->pOrderBy, iTable, pEList);
2138 substExpr(db, p->pHaving, iTable, pEList);
2139 substExpr(db, p->pWhere, iTable, pEList);
2140 substSelect(db, p->pPrior, iTable, pEList);
2141}
2142#endif /* !defined(SQLITE_OMIT_VIEW) */
2143
2144#ifndef SQLITE_OMIT_VIEW
2145/*
2146** This routine attempts to flatten subqueries in order to speed
2147** execution. It returns 1 if it makes changes and 0 if no flattening
2148** occurs.
2149**
2150** To understand the concept of flattening, consider the following
2151** query:
2152**
2153** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
2154**
2155** The default way of implementing this query is to execute the
2156** subquery first and store the results in a temporary table, then
2157** run the outer query on that temporary table. This requires two
2158** passes over the data. Furthermore, because the temporary table
2159** has no indices, the WHERE clause on the outer query cannot be
2160** optimized.
2161**
2162** This routine attempts to rewrite queries such as the above into
2163** a single flat select, like this:
2164**
2165** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
2166**
2167** The code generated for this simpification gives the same result
2168** but only has to scan the data once. And because indices might
2169** exist on the table t1, a complete scan of the data might be
2170** avoided.
2171**
2172** Flattening is only attempted if all of the following are true:
2173**
2174** (1) The subquery and the outer query do not both use aggregates.
2175**
2176** (2) The subquery is not an aggregate or the outer query is not a join.
2177**
2178** (3) The subquery is not the right operand of a left outer join, or
2179** the subquery is not itself a join. (Ticket #306)
2180**
2181** (4) The subquery is not DISTINCT or the outer query is not a join.
2182**
2183** (5) The subquery is not DISTINCT or the outer query does not use
2184** aggregates.
2185**
2186** (6) The subquery does not use aggregates or the outer query is not
2187** DISTINCT.
2188**
2189** (7) The subquery has a FROM clause.
2190**
2191** (8) The subquery does not use LIMIT or the outer query is not a join.
2192**
2193** (9) The subquery does not use LIMIT or the outer query does not use
2194** aggregates.
2195**
2196** (10) The subquery does not use aggregates or the outer query does not
2197** use LIMIT.
2198**
2199** (11) The subquery and the outer query do not both have ORDER BY clauses.
2200**
2201** (12) The subquery is not the right term of a LEFT OUTER JOIN or the
2202** subquery has no WHERE clause. (added by ticket #350)
2203**
2204** (13) The subquery and outer query do not both use LIMIT
2205**
2206** (14) The subquery does not use OFFSET
2207**
2208** (15) The outer query is not part of a compound select or the
2209** subquery does not have both an ORDER BY and a LIMIT clause.
2210** (See ticket #2339)
2211**
2212** In this routine, the "p" parameter is a pointer to the outer query.
2213** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
2214** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
2215**
2216** If flattening is not attempted, this routine is a no-op and returns 0.
2217** If flattening is attempted this routine returns 1.
2218**
2219** All of the expression analysis must occur on both the outer query and
2220** the subquery before this routine runs.
2221*/
2222static int flattenSubquery(
2223 sqlite3 *db, /* Database connection */
2224 Select *p, /* The parent or outer SELECT statement */
2225 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
2226 int isAgg, /* True if outer SELECT uses aggregate functions */
2227 int subqueryIsAgg /* True if the subquery uses aggregate functions */
2228){
2229 Select *pSub; /* The inner query or "subquery" */
2230 SrcList *pSrc; /* The FROM clause of the outer query */
2231 SrcList *pSubSrc; /* The FROM clause of the subquery */
2232 ExprList *pList; /* The result set of the outer query */
2233 int iParent; /* VDBE cursor number of the pSub result set temp table */
2234 int i; /* Loop counter */
2235 Expr *pWhere; /* The WHERE clause */
2236 struct SrcList_item *pSubitem; /* The subquery */
2237
2238 /* Check to see if flattening is permitted. Return 0 if not.
2239 */
2240 if( p==0 ) return 0;
2241 pSrc = p->pSrc;
2242 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
2243 pSubitem = &pSrc->a[iFrom];
2244 pSub = pSubitem->pSelect;
2245 assert( pSub!=0 );
2246 if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */
2247 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */
2248 pSubSrc = pSub->pSrc;
2249 assert( pSubSrc );
2250 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
2251 ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
2252 ** because they could be computed at compile-time. But when LIMIT and OFFSET
2253 ** became arbitrary expressions, we were forced to add restrictions (13)
2254 ** and (14). */
2255 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
2256 if( pSub->pOffset ) return 0; /* Restriction (14) */
2257 if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
2258 return 0; /* Restriction (15) */
2259 }
2260 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
2261 if( (pSub->isDistinct || pSub->pLimit)
2262 && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
2263 return 0;
2264 }
2265 if( p->isDistinct && subqueryIsAgg ) return 0; /* Restriction (6) */
2266 if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){
2267 return 0; /* Restriction (11) */
2268 }
2269
2270 /* Restriction 3: If the subquery is a join, make sure the subquery is
2271 ** not used as the right operand of an outer join. Examples of why this
2272 ** is not allowed:
2273 **
2274 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
2275 **
2276 ** If we flatten the above, we would get
2277 **
2278 ** (t1 LEFT OUTER JOIN t2) JOIN t3
2279 **
2280 ** which is not at all the same thing.
2281 */
2282 if( pSubSrc->nSrc>1 && (pSubitem->jointype & JT_OUTER)!=0 ){
2283 return 0;
2284 }
2285
2286 /* Restriction 12: If the subquery is the right operand of a left outer
2287 ** join, make sure the subquery has no WHERE clause.
2288 ** An examples of why this is not allowed:
2289 **
2290 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
2291 **
2292 ** If we flatten the above, we would get
2293 **
2294 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
2295 **
2296 ** But the t2.x>0 test will always fail on a NULL row of t2, which
2297 ** effectively converts the OUTER JOIN into an INNER JOIN.
2298 */
2299 if( (pSubitem->jointype & JT_OUTER)!=0 && pSub->pWhere!=0 ){
2300 return 0;
2301 }
2302
2303 /* If we reach this point, it means flattening is permitted for the
2304 ** iFrom-th entry of the FROM clause in the outer query.
2305 */
2306
2307 /* Move all of the FROM elements of the subquery into the
2308 ** the FROM clause of the outer query. Before doing this, remember
2309 ** the cursor number for the original outer query FROM element in
2310 ** iParent. The iParent cursor will never be used. Subsequent code
2311 ** will scan expressions looking for iParent references and replace
2312 ** those references with expressions that resolve to the subquery FROM
2313 ** elements we are now copying in.
2314 */
2315 iParent = pSubitem->iCursor;
2316 {
2317 int nSubSrc = pSubSrc->nSrc;
2318 int jointype = pSubitem->jointype;
2319
2320 sqlite3DeleteTable(pSubitem->pTab);
2321 sqlite3_free(pSubitem->zDatabase);
2322 sqlite3_free(pSubitem->zName);
2323 sqlite3_free(pSubitem->zAlias);
2324 if( nSubSrc>1 ){
2325 int extra = nSubSrc - 1;
2326 for(i=1; i<nSubSrc; i++){
2327 pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
2328 }
2329 p->pSrc = pSrc;
2330 for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
2331 pSrc->a[i] = pSrc->a[i-extra];
2332 }
2333 }
2334 for(i=0; i<nSubSrc; i++){
2335 pSrc->a[i+iFrom] = pSubSrc->a[i];
2336 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
2337 }
2338 pSrc->a[iFrom].jointype = jointype;
2339 }
2340
2341 /* Now begin substituting subquery result set expressions for
2342 ** references to the iParent in the outer query.
2343 **
2344 ** Example:
2345 **
2346 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
2347 ** \ \_____________ subquery __________/ /
2348 ** \_____________________ outer query ______________________________/
2349 **
2350 ** We look at every expression in the outer query and every place we see
2351 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
2352 */
2353 pList = p->pEList;
2354 for(i=0; i<pList->nExpr; i++){
2355 Expr *pExpr;
2356 if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
2357 pList->a[i].zName =
2358 sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n);
2359 }
2360 }
2361 substExprList(db, p->pEList, iParent, pSub->pEList);
2362 if( isAgg ){
2363 substExprList(db, p->pGroupBy, iParent, pSub->pEList);
2364 substExpr(db, p->pHaving, iParent, pSub->pEList);
2365 }
2366 if( pSub->pOrderBy ){
2367 assert( p->pOrderBy==0 );
2368 p->pOrderBy = pSub->pOrderBy;
2369 pSub->pOrderBy = 0;
2370 }else if( p->pOrderBy ){
2371 substExprList(db, p->pOrderBy, iParent, pSub->pEList);
2372 }
2373 if( pSub->pWhere ){
2374 pWhere = sqlite3ExprDup(db, pSub->pWhere);
2375 }else{
2376 pWhere = 0;
2377 }
2378 if( subqueryIsAgg ){
2379 assert( p->pHaving==0 );
2380 p->pHaving = p->pWhere;
2381 p->pWhere = pWhere;
2382 substExpr(db, p->pHaving, iParent, pSub->pEList);
2383 p->pHaving = sqlite3ExprAnd(db, p->pHaving,
2384 sqlite3ExprDup(db, pSub->pHaving));
2385 assert( p->pGroupBy==0 );
2386 p->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy);
2387 }else{
2388 substExpr(db, p->pWhere, iParent, pSub->pEList);
2389 p->pWhere = sqlite3ExprAnd(db, p->pWhere, pWhere);
2390 }
2391
2392 /* The flattened query is distinct if either the inner or the
2393 ** outer query is distinct.
2394 */
2395 p->isDistinct = p->isDistinct || pSub->isDistinct;
2396
2397 /*
2398 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
2399 **
2400 ** One is tempted to try to add a and b to combine the limits. But this
2401 ** does not work if either limit is negative.
2402 */
2403 if( pSub->pLimit ){
2404 p->pLimit = pSub->pLimit;
2405 pSub->pLimit = 0;
2406 }
2407
2408 /* Finially, delete what is left of the subquery and return
2409 ** success.
2410 */
2411 sqlite3SelectDelete(pSub);
2412 return 1;
2413}
2414#endif /* SQLITE_OMIT_VIEW */
2415
2416/*
2417** Analyze the SELECT statement passed in as an argument to see if it
2418** is a simple min() or max() query. If it is and this query can be
2419** satisfied using a single seek to the beginning or end of an index,
2420** then generate the code for this SELECT and return 1. If this is not a
2421** simple min() or max() query, then return 0;
2422**
2423** A simply min() or max() query looks like this:
2424**
2425** SELECT min(a) FROM table;
2426** SELECT max(a) FROM table;
2427**
2428** The query may have only a single table in its FROM argument. There
2429** can be no GROUP BY or HAVING or WHERE clauses. The result set must
2430** be the min() or max() of a single column of the table. The column
2431** in the min() or max() function must be indexed.
2432**
2433** The parameters to this routine are the same as for sqlite3Select().
2434** See the header comment on that routine for additional information.
2435*/
2436static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
2437 Expr *pExpr;
2438 int iCol;
2439 Table *pTab;
2440 Index *pIdx;
2441 int base;
2442 Vdbe *v;
2443 int seekOp;
2444 ExprList *pEList, *pList, eList;
2445 struct ExprList_item eListItem;
2446 SrcList *pSrc;
2447 int brk;
2448 int iDb;
2449
2450 /* Check to see if this query is a simple min() or max() query. Return
2451 ** zero if it is not.
2452 */
2453 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
2454 pSrc = p->pSrc;
2455 if( pSrc->nSrc!=1 ) return 0;
2456 pEList = p->pEList;
2457 if( pEList->nExpr!=1 ) return 0;
2458 pExpr = pEList->a[0].pExpr;
2459 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
2460 pList = pExpr->pList;
2461 if( pList==0 || pList->nExpr!=1 ) return 0;
2462 if( pExpr->token.n!=3 ) return 0;
2463 if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
2464 seekOp = OP_Rewind;
2465 }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
2466 seekOp = OP_Last;
2467 }else{
2468 return 0;
2469 }
2470 pExpr = pList->a[0].pExpr;
2471 if( pExpr->op!=TK_COLUMN ) return 0;
2472 iCol = pExpr->iColumn;
2473 pTab = pSrc->a[0].pTab;
2474
2475 /* This optimization cannot be used with virtual tables. */
2476 if( IsVirtual(pTab) ) return 0;
2477
2478 /* If we get to here, it means the query is of the correct form.
2479 ** Check to make sure we have an index and make pIdx point to the
2480 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
2481 ** key column, no index is necessary so set pIdx to NULL. If no
2482 ** usable index is found, return 0.
2483 */
2484 if( iCol<0 ){
2485 pIdx = 0;
2486 }else{
2487 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
2488 if( pColl==0 ) return 0;
2489 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2490 assert( pIdx->nColumn>=1 );
2491 if( pIdx->aiColumn[0]==iCol &&
2492 0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){
2493 break;
2494 }
2495 }
2496 if( pIdx==0 ) return 0;
2497 }
2498
2499 /* Identify column types if we will be using the callback. This
2500 ** step is skipped if the output is going to a table or a memory cell.
2501 ** The column names have already been generated in the calling function.
2502 */
2503 v = sqlite3GetVdbe(pParse);
2504 if( v==0 ) return 0;
2505
2506 /* If the output is destined for a temporary table, open that table.
2507 */
2508 if( eDest==SRT_EphemTab ){
2509 sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 1);
2510 }
2511
2512 /* Generating code to find the min or the max. Basically all we have
2513 ** to do is find the first or the last entry in the chosen index. If
2514 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
2515 ** or last entry in the main table.
2516 */
2517 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2518 assert( iDb>=0 || pTab->isEphem );
2519 sqlite3CodeVerifySchema(pParse, iDb);
2520 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
2521 base = pSrc->a[0].iCursor;
2522 brk = sqlite3VdbeMakeLabel(v);
2523 computeLimitRegisters(pParse, p, brk);
2524 if( pSrc->a[0].pSelect==0 ){
2525 sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead);
2526 }
2527 if( pIdx==0 ){
2528 sqlite3VdbeAddOp(v, seekOp, base, 0);
2529 }else{
2530 /* Even though the cursor used to open the index here is closed
2531 ** as soon as a single value has been read from it, allocate it
2532 ** using (pParse->nTab++) to prevent the cursor id from being
2533 ** reused. This is important for statements of the form
2534 ** "INSERT INTO x SELECT max() FROM x".
2535 */
2536 int iIdx;
2537 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
2538 iIdx = pParse->nTab++;
2539 assert( pIdx->pSchema==pTab->pSchema );
2540 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
2541 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum,
2542 (char*)pKey, P3_KEYINFO_HANDOFF);
2543 if( seekOp==OP_Rewind ){
2544 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
2545 sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
2546 seekOp = OP_MoveGt;
2547 }
2548 if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){
2549 /* Ticket #2514: invert the seek operator if we are using
2550 ** a descending index. */
2551 if( seekOp==OP_Last ){
2552 seekOp = OP_Rewind;
2553 }else{
2554 assert( seekOp==OP_MoveGt );
2555 seekOp = OP_MoveLt;
2556 }
2557 }
2558 sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
2559 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0);
2560 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2561 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
2562 }
2563 eList.nExpr = 1;
2564 memset(&eListItem, 0, sizeof(eListItem));
2565 eList.a = &eListItem;
2566 eList.a[0].pExpr = pExpr;
2567 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0);
2568 sqlite3VdbeResolveLabel(v, brk);
2569 sqlite3VdbeAddOp(v, OP_Close, base, 0);
2570
2571 return 1;
2572}
2573
2574/*
2575** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return
2576** the number of errors seen.
2577**
2578** An ORDER BY or GROUP BY is a list of expressions. If any expression
2579** is an integer constant, then that expression is replaced by the
2580** corresponding entry in the result set.
2581*/
2582static int processOrderGroupBy(
2583 NameContext *pNC, /* Name context of the SELECT statement. */
2584 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
2585 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
2586){
2587 int i;
2588 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */
2589 Parse *pParse = pNC->pParse; /* The result set of the SELECT */
2590 assert( pEList );
2591
2592 if( pOrderBy==0 ) return 0;
2593 if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){
2594 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
2595 return 1;
2596 }
2597 for(i=0; i<pOrderBy->nExpr; i++){
2598 int iCol;
2599 Expr *pE = pOrderBy->a[i].pExpr;
2600 if( sqlite3ExprIsInteger(pE, &iCol) ){
2601 if( iCol>0 && iCol<=pEList->nExpr ){
2602 CollSeq *pColl = pE->pColl;
2603 int flags = pE->flags & EP_ExpCollate;
2604 sqlite3ExprDelete(pE);
2605 pE = sqlite3ExprDup(pParse->db, pEList->a[iCol-1].pExpr);
2606 pOrderBy->a[i].pExpr = pE;
2607 if( pColl && flags ){
2608 pE->pColl = pColl;
2609 pE->flags |= flags;
2610 }
2611 }else{
2612 sqlite3ErrorMsg(pParse,
2613 "%s BY column number %d out of range - should be "
2614 "between 1 and %d", zType, iCol, pEList->nExpr);
2615 return 1;
2616 }
2617 }
2618 if( sqlite3ExprResolveNames(pNC, pE) ){
2619 return 1;
2620 }
2621 }
2622 return 0;
2623}
2624
2625/*
2626** This routine resolves any names used in the result set of the
2627** supplied SELECT statement. If the SELECT statement being resolved
2628** is a sub-select, then pOuterNC is a pointer to the NameContext
2629** of the parent SELECT.
2630*/
2631int sqlite3SelectResolve(
2632 Parse *pParse, /* The parser context */
2633 Select *p, /* The SELECT statement being coded. */
2634 NameContext *pOuterNC /* The outer name context. May be NULL. */
2635){
2636 ExprList *pEList; /* Result set. */
2637 int i; /* For-loop variable used in multiple places */
2638 NameContext sNC; /* Local name-context */
2639 ExprList *pGroupBy; /* The group by clause */
2640
2641 /* If this routine has run before, return immediately. */
2642 if( p->isResolved ){
2643 assert( !pOuterNC );
2644 return SQLITE_OK;
2645 }
2646 p->isResolved = 1;
2647
2648 /* If there have already been errors, do nothing. */
2649 if( pParse->nErr>0 ){
2650 return SQLITE_ERROR;
2651 }
2652
2653 /* Prepare the select statement. This call will allocate all cursors
2654 ** required to handle the tables and subqueries in the FROM clause.
2655 */
2656 if( prepSelectStmt(pParse, p) ){
2657 return SQLITE_ERROR;
2658 }
2659
2660 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
2661 ** are not allowed to refer to any names, so pass an empty NameContext.
2662 */
2663 memset(&sNC, 0, sizeof(sNC));
2664 sNC.pParse = pParse;
2665 if( sqlite3ExprResolveNames(&sNC, p->pLimit) ||
2666 sqlite3ExprResolveNames(&sNC, p->pOffset) ){
2667 return SQLITE_ERROR;
2668 }
2669
2670 /* Set up the local name-context to pass to ExprResolveNames() to
2671 ** resolve the expression-list.
2672 */
2673 sNC.allowAgg = 1;
2674 sNC.pSrcList = p->pSrc;
2675 sNC.pNext = pOuterNC;
2676
2677 /* Resolve names in the result set. */
2678 pEList = p->pEList;
2679 if( !pEList ) return SQLITE_ERROR;
2680 for(i=0; i<pEList->nExpr; i++){
2681 Expr *pX = pEList->a[i].pExpr;
2682 if( sqlite3ExprResolveNames(&sNC, pX) ){
2683 return SQLITE_ERROR;
2684 }
2685 }
2686
2687 /* If there are no aggregate functions in the result-set, and no GROUP BY
2688 ** expression, do not allow aggregates in any of the other expressions.
2689 */
2690 assert( !p->isAgg );
2691 pGroupBy = p->pGroupBy;
2692 if( pGroupBy || sNC.hasAgg ){
2693 p->isAgg = 1;
2694 }else{
2695 sNC.allowAgg = 0;
2696 }
2697
2698 /* If a HAVING clause is present, then there must be a GROUP BY clause.
2699 */
2700 if( p->pHaving && !pGroupBy ){
2701 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
2702 return SQLITE_ERROR;
2703 }
2704
2705 /* Add the expression list to the name-context before parsing the
2706 ** other expressions in the SELECT statement. This is so that
2707 ** expressions in the WHERE clause (etc.) can refer to expressions by
2708 ** aliases in the result set.
2709 **
2710 ** Minor point: If this is the case, then the expression will be
2711 ** re-evaluated for each reference to it.
2712 */
2713 sNC.pEList = p->pEList;
2714 if( sqlite3ExprResolveNames(&sNC, p->pWhere) ||
2715 sqlite3ExprResolveNames(&sNC, p->pHaving) ){
2716 return SQLITE_ERROR;
2717 }
2718 if( p->pPrior==0 ){
2719 if( processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") ||
2720 processOrderGroupBy(&sNC, pGroupBy, "GROUP") ){
2721 return SQLITE_ERROR;
2722 }
2723 }
2724
2725 if( pParse->db->mallocFailed ){
2726 return SQLITE_NOMEM;
2727 }
2728
2729 /* Make sure the GROUP BY clause does not contain aggregate functions.
2730 */
2731 if( pGroupBy ){
2732 struct ExprList_item *pItem;
2733
2734 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
2735 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
2736 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
2737 "the GROUP BY clause");
2738 return SQLITE_ERROR;
2739 }
2740 }
2741 }
2742
2743 /* If this is one SELECT of a compound, be sure to resolve names
2744 ** in the other SELECTs.
2745 */
2746 if( p->pPrior ){
2747 return sqlite3SelectResolve(pParse, p->pPrior, pOuterNC);
2748 }else{
2749 return SQLITE_OK;
2750 }
2751}
2752
2753/*
2754** Reset the aggregate accumulator.
2755**
2756** The aggregate accumulator is a set of memory cells that hold
2757** intermediate results while calculating an aggregate. This
2758** routine simply stores NULLs in all of those memory cells.
2759*/
2760static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
2761 Vdbe *v = pParse->pVdbe;
2762 int i;
2763 struct AggInfo_func *pFunc;
2764 if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
2765 return;
2766 }
2767 for(i=0; i<pAggInfo->nColumn; i++){
2768 sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0);
2769 }
2770 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
2771 sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0);
2772 if( pFunc->iDistinct>=0 ){
2773 Expr *pE = pFunc->pExpr;
2774 if( pE->pList==0 || pE->pList->nExpr!=1 ){
2775 sqlite3ErrorMsg(pParse, "DISTINCT in aggregate must be followed "
2776 "by an expression");
2777 pFunc->iDistinct = -1;
2778 }else{
2779 KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList);
2780 sqlite3VdbeOp3(v, OP_OpenEphemeral, pFunc->iDistinct, 0,
2781 (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
2782 }
2783 }
2784 }
2785}
2786
2787/*
2788** Invoke the OP_AggFinalize opcode for every aggregate function
2789** in the AggInfo structure.
2790*/
2791static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
2792 Vdbe *v = pParse->pVdbe;
2793 int i;
2794 struct AggInfo_func *pF;
2795 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
2796 ExprList *pList = pF->pExpr->pList;
2797 sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0,
2798 (void*)pF->pFunc, P3_FUNCDEF);
2799 }
2800}
2801
2802/*
2803** Update the accumulator memory cells for an aggregate based on
2804** the current cursor position.
2805*/
2806static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
2807 Vdbe *v = pParse->pVdbe;
2808 int i;
2809 struct AggInfo_func *pF;
2810 struct AggInfo_col *pC;
2811
2812 pAggInfo->directMode = 1;
2813 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
2814 int nArg;
2815 int addrNext = 0;
2816 ExprList *pList = pF->pExpr->pList;
2817 if( pList ){
2818 nArg = pList->nExpr;
2819 sqlite3ExprCodeExprList(pParse, pList);
2820 }else{
2821 nArg = 0;
2822 }
2823 if( pF->iDistinct>=0 ){
2824 addrNext = sqlite3VdbeMakeLabel(v);
2825 assert( nArg==1 );
2826 codeDistinct(v, pF->iDistinct, addrNext, 1);
2827 }
2828 if( pF->pFunc->needCollSeq ){
2829 CollSeq *pColl = 0;
2830 struct ExprList_item *pItem;
2831 int j;
2832 assert( pList!=0 ); /* pList!=0 if pF->pFunc->needCollSeq is true */
2833 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
2834 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
2835 }
2836 if( !pColl ){
2837 pColl = pParse->db->pDfltColl;
2838 }
2839 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
2840 }
2841 sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF);
2842 if( addrNext ){
2843 sqlite3VdbeResolveLabel(v, addrNext);
2844 }
2845 }
2846 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
2847 sqlite3ExprCode(pParse, pC->pExpr);
2848 sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1);
2849 }
2850 pAggInfo->directMode = 0;
2851}
2852
2853
2854/*
2855** Generate code for the given SELECT statement.
2856**
2857** The results are distributed in various ways depending on the
2858** value of eDest and iParm.
2859**
2860** eDest Value Result
2861** ------------ -------------------------------------------
2862** SRT_Callback Invoke the callback for each row of the result.
2863**
2864** SRT_Mem Store first result in memory cell iParm
2865**
2866** SRT_Set Store results as keys of table iParm.
2867**
2868** SRT_Union Store results as a key in a temporary table iParm
2869**
2870** SRT_Except Remove results from the temporary table iParm.
2871**
2872** SRT_Table Store results in temporary table iParm
2873**
2874** The table above is incomplete. Additional eDist value have be added
2875** since this comment was written. See the selectInnerLoop() function for
2876** a complete listing of the allowed values of eDest and their meanings.
2877**
2878** This routine returns the number of errors. If any errors are
2879** encountered, then an appropriate error message is left in
2880** pParse->zErrMsg.
2881**
2882** This routine does NOT free the Select structure passed in. The
2883** calling function needs to do that.
2884**
2885** The pParent, parentTab, and *pParentAgg fields are filled in if this
2886** SELECT is a subquery. This routine may try to combine this SELECT
2887** with its parent to form a single flat query. In so doing, it might
2888** change the parent query from a non-aggregate to an aggregate query.
2889** For that reason, the pParentAgg flag is passed as a pointer, so it
2890** can be changed.
2891**
2892** Example 1: The meaning of the pParent parameter.
2893**
2894** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
2895** \ \_______ subquery _______/ /
2896** \ /
2897** \____________________ outer query ___________________/
2898**
2899** This routine is called for the outer query first. For that call,
2900** pParent will be NULL. During the processing of the outer query, this
2901** routine is called recursively to handle the subquery. For the recursive
2902** call, pParent will point to the outer query. Because the subquery is
2903** the second element in a three-way join, the parentTab parameter will
2904** be 1 (the 2nd value of a 0-indexed array.)
2905*/
2906int sqlite3Select(
2907 Parse *pParse, /* The parser context */
2908 Select *p, /* The SELECT statement being coded. */
2909 int eDest, /* How to dispose of the results */
2910 int iParm, /* A parameter used by the eDest disposal method */
2911 Select *pParent, /* Another SELECT for which this is a sub-query */
2912 int parentTab, /* Index in pParent->pSrc of this query */
2913 int *pParentAgg, /* True if pParent uses aggregate functions */
2914 char *aff /* If eDest is SRT_Union, the affinity string */
2915){
2916 int i, j; /* Loop counters */
2917 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
2918 Vdbe *v; /* The virtual machine under construction */
2919 int isAgg; /* True for select lists like "count(*)" */
2920 ExprList *pEList; /* List of columns to extract. */
2921 SrcList *pTabList; /* List of tables to select from */
2922 Expr *pWhere; /* The WHERE clause. May be NULL */
2923 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
2924 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
2925 Expr *pHaving; /* The HAVING clause. May be NULL */
2926 int isDistinct; /* True if the DISTINCT keyword is present */
2927 int distinct; /* Table to use for the distinct set */
2928 int rc = 1; /* Value to return from this function */
2929 int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
2930 AggInfo sAggInfo; /* Information used by aggregate queries */
2931 int iEnd; /* Address of the end of the query */
2932 sqlite3 *db; /* The database connection */
2933
2934 db = pParse->db;
2935 if( p==0 || db->mallocFailed || pParse->nErr ){
2936 return 1;
2937 }
2938 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
2939 memset(&sAggInfo, 0, sizeof(sAggInfo));
2940
2941#ifndef SQLITE_OMIT_COMPOUND_SELECT
2942 /* If there is are a sequence of queries, do the earlier ones first.
2943 */
2944 if( p->pPrior ){
2945 if( p->pRightmost==0 ){
2946 Select *pLoop;
2947 int cnt = 0;
2948 for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
2949 pLoop->pRightmost = p;
2950 }
2951 if( SQLITE_MAX_COMPOUND_SELECT>0 && cnt>SQLITE_MAX_COMPOUND_SELECT ){
2952 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
2953 return 1;
2954 }
2955 }
2956 return multiSelect(pParse, p, eDest, iParm, aff);
2957 }
2958#endif
2959
2960 pOrderBy = p->pOrderBy;
2961 if( IgnorableOrderby(eDest) ){
2962 p->pOrderBy = 0;
2963 }
2964 if( sqlite3SelectResolve(pParse, p, 0) ){
2965 goto select_end;
2966 }
2967 p->pOrderBy = pOrderBy;
2968
2969 /* Make local copies of the parameters for this query.
2970 */
2971 pTabList = p->pSrc;
2972 pWhere = p->pWhere;
2973 pGroupBy = p->pGroupBy;
2974 pHaving = p->pHaving;
2975 isAgg = p->isAgg;
2976 isDistinct = p->isDistinct;
2977 pEList = p->pEList;
2978 if( pEList==0 ) goto select_end;
2979
2980 /*
2981 ** Do not even attempt to generate any code if we have already seen
2982 ** errors before this routine starts.
2983 */
2984 if( pParse->nErr>0 ) goto select_end;
2985
2986 /* If writing to memory or generating a set
2987 ** only a single column may be output.
2988 */
2989#ifndef SQLITE_OMIT_SUBQUERY
2990 if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
2991 goto select_end;
2992 }
2993#endif
2994
2995 /* ORDER BY is ignored for some destinations.
2996 */
2997 if( IgnorableOrderby(eDest) ){
2998 pOrderBy = 0;
2999 }
3000
3001 /* Begin generating code.
3002 */
3003 v = sqlite3GetVdbe(pParse);
3004 if( v==0 ) goto select_end;
3005
3006 /* Generate code for all sub-queries in the FROM clause
3007 */
3008#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
3009 for(i=0; i<pTabList->nSrc; i++){
3010 const char *zSavedAuthContext = 0;
3011 int needRestoreContext;
3012 struct SrcList_item *pItem = &pTabList->a[i];
3013
3014 if( pItem->pSelect==0 || pItem->isPopulated ) continue;
3015 if( pItem->zName!=0 ){
3016 zSavedAuthContext = pParse->zAuthContext;
3017 pParse->zAuthContext = pItem->zName;
3018 needRestoreContext = 1;
3019 }else{
3020 needRestoreContext = 0;
3021 }
3022#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
3023 /* Increment Parse.nHeight by the height of the largest expression
3024 ** tree refered to by this, the parent select. The child select
3025 ** may contain expression trees of at most
3026 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
3027 ** more conservative than necessary, but much easier than enforcing
3028 ** an exact limit.
3029 */
3030 pParse->nHeight += sqlite3SelectExprHeight(p);
3031#endif
3032 sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab,
3033 pItem->iCursor, p, i, &isAgg, 0);
3034#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
3035 pParse->nHeight -= sqlite3SelectExprHeight(p);
3036#endif
3037 if( needRestoreContext ){
3038 pParse->zAuthContext = zSavedAuthContext;
3039 }
3040 pTabList = p->pSrc;
3041 pWhere = p->pWhere;
3042 if( !IgnorableOrderby(eDest) ){
3043 pOrderBy = p->pOrderBy;
3044 }
3045 pGroupBy = p->pGroupBy;
3046 pHaving = p->pHaving;
3047 isDistinct = p->isDistinct;
3048 }
3049#endif
3050
3051 /* Check for the special case of a min() or max() function by itself
3052 ** in the result set.
3053 */
3054 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
3055 rc = 0;
3056 goto select_end;
3057 }
3058
3059 /* Check to see if this is a subquery that can be "flattened" into its parent.
3060 ** If flattening is a possiblity, do so and return immediately.
3061 */
3062#ifndef SQLITE_OMIT_VIEW
3063 if( pParent && pParentAgg &&
3064 flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){
3065 if( isAgg ) *pParentAgg = 1;
3066 goto select_end;
3067 }
3068#endif
3069
3070 /* If there is an ORDER BY clause, then this sorting
3071 ** index might end up being unused if the data can be
3072 ** extracted in pre-sorted order. If that is the case, then the
3073 ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
3074 ** we figure out that the sorting index is not needed. The addrSortIndex
3075 ** variable is used to facilitate that change.
3076 */
3077 if( pOrderBy ){
3078 KeyInfo *pKeyInfo;
3079 if( pParse->nErr ){
3080 goto select_end;
3081 }
3082 pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
3083 pOrderBy->iECursor = pParse->nTab++;
3084 p->addrOpenEphm[2] = addrSortIndex =
3085 sqlite3VdbeOp3(v, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+2, (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3086 }else{
3087 addrSortIndex = -1;
3088 }
3089
3090 /* If the output is destined for a temporary table, open that table.
3091 */
3092 if( eDest==SRT_EphemTab ){
3093 sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, pEList->nExpr);
3094 }
3095
3096 /* Set the limiter.
3097 */
3098 iEnd = sqlite3VdbeMakeLabel(v);
3099 computeLimitRegisters(pParse, p, iEnd);
3100
3101 /* Open a virtual index to use for the distinct set.
3102 */
3103 if( isDistinct ){
3104 KeyInfo *pKeyInfo;
3105 distinct = pParse->nTab++;
3106 pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
3107 sqlite3VdbeOp3(v, OP_OpenEphemeral, distinct, 0,
3108 (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3109 }else{
3110 distinct = -1;
3111 }
3112
3113 /* Aggregate and non-aggregate queries are handled differently */
3114 if( !isAgg && pGroupBy==0 ){
3115 /* This case is for non-aggregate queries
3116 ** Begin the database scan
3117 */
3118 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy);
3119 if( pWInfo==0 ) goto select_end;
3120
3121 /* If sorting index that was created by a prior OP_OpenEphemeral
3122 ** instruction ended up not being needed, then change the OP_OpenEphemeral
3123 ** into an OP_Noop.
3124 */
3125 if( addrSortIndex>=0 && pOrderBy==0 ){
3126 sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
3127 p->addrOpenEphm[2] = -1;
3128 }
3129
3130 /* Use the standard inner loop
3131 */
3132 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
3133 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){
3134 goto select_end;
3135 }
3136
3137 /* End the database scan loop.
3138 */
3139 sqlite3WhereEnd(pWInfo);
3140 }else{
3141 /* This is the processing for aggregate queries */
3142 NameContext sNC; /* Name context for processing aggregate information */
3143 int iAMem; /* First Mem address for storing current GROUP BY */
3144 int iBMem; /* First Mem address for previous GROUP BY */
3145 int iUseFlag; /* Mem address holding flag indicating that at least
3146 ** one row of the input to the aggregator has been
3147 ** processed */
3148 int iAbortFlag; /* Mem address which causes query abort if positive */
3149 int groupBySort; /* Rows come from source in GROUP BY order */
3150
3151
3152 /* The following variables hold addresses or labels for parts of the
3153 ** virtual machine program we are putting together */
3154 int addrOutputRow; /* Start of subroutine that outputs a result row */
3155 int addrSetAbort; /* Set the abort flag and return */
3156 int addrInitializeLoop; /* Start of code that initializes the input loop */
3157 int addrTopOfLoop; /* Top of the input loop */
3158 int addrGroupByChange; /* Code that runs when any GROUP BY term changes */
3159 int addrProcessRow; /* Code to process a single input row */
3160 int addrEnd; /* End of all processing */
3161 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
3162 int addrReset; /* Subroutine for resetting the accumulator */
3163
3164 addrEnd = sqlite3VdbeMakeLabel(v);
3165
3166 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
3167 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
3168 ** SELECT statement.
3169 */
3170 memset(&sNC, 0, sizeof(sNC));
3171 sNC.pParse = pParse;
3172 sNC.pSrcList = pTabList;
3173 sNC.pAggInfo = &sAggInfo;
3174 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
3175 sAggInfo.pGroupBy = pGroupBy;
3176 if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){
3177 goto select_end;
3178 }
3179 if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){
3180 goto select_end;
3181 }
3182 if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){
3183 goto select_end;
3184 }
3185 sAggInfo.nAccumulator = sAggInfo.nColumn;
3186 for(i=0; i<sAggInfo.nFunc; i++){
3187 if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
3188 goto select_end;
3189 }
3190 }
3191 if( db->mallocFailed ) goto select_end;
3192
3193 /* Processing for aggregates with GROUP BY is very different and
3194 ** much more complex tha aggregates without a GROUP BY.
3195 */
3196 if( pGroupBy ){
3197 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
3198
3199 /* Create labels that we will be needing
3200 */
3201
3202 addrInitializeLoop = sqlite3VdbeMakeLabel(v);
3203 addrGroupByChange = sqlite3VdbeMakeLabel(v);
3204 addrProcessRow = sqlite3VdbeMakeLabel(v);
3205
3206 /* If there is a GROUP BY clause we might need a sorting index to
3207 ** implement it. Allocate that sorting index now. If it turns out
3208 ** that we do not need it after all, the OpenEphemeral instruction
3209 ** will be converted into a Noop.
3210 */
3211 sAggInfo.sortingIdx = pParse->nTab++;
3212 pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
3213 addrSortingIdx =
3214 sqlite3VdbeOp3(v, OP_OpenEphemeral, sAggInfo.sortingIdx,
3215 sAggInfo.nSortingColumn,
3216 (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
3217
3218 /* Initialize memory locations used by GROUP BY aggregate processing
3219 */
3220 iUseFlag = pParse->nMem++;
3221 iAbortFlag = pParse->nMem++;
3222 iAMem = pParse->nMem;
3223 pParse->nMem += pGroupBy->nExpr;
3224 iBMem = pParse->nMem;
3225 pParse->nMem += pGroupBy->nExpr;
3226 sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag);
3227 VdbeComment((v, "# clear abort flag"));
3228 sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag);
3229 VdbeComment((v, "# indicate accumulator empty"));
3230 sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop);
3231
3232 /* Generate a subroutine that outputs a single row of the result
3233 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
3234 ** is less than or equal to zero, the subroutine is a no-op. If
3235 ** the processing calls for the query to abort, this subroutine
3236 ** increments the iAbortFlag memory location before returning in
3237 ** order to signal the caller to abort.
3238 */
3239 addrSetAbort = sqlite3VdbeCurrentAddr(v);
3240 sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag);
3241 VdbeComment((v, "# set abort flag"));
3242 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3243 addrOutputRow = sqlite3VdbeCurrentAddr(v);
3244 sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2);
3245 VdbeComment((v, "# Groupby result generator entry point"));
3246 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3247 finalizeAggFunctions(pParse, &sAggInfo);
3248 if( pHaving ){
3249 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1);
3250 }
3251 rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
3252 distinct, eDest, iParm,
3253 addrOutputRow+1, addrSetAbort, aff);
3254 if( rc ){
3255 goto select_end;
3256 }
3257 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3258 VdbeComment((v, "# end groupby result generator"));
3259
3260 /* Generate a subroutine that will reset the group-by accumulator
3261 */
3262 addrReset = sqlite3VdbeCurrentAddr(v);
3263 resetAccumulator(pParse, &sAggInfo);
3264 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
3265
3266 /* Begin a loop that will extract all source rows in GROUP BY order.
3267 ** This might involve two separate loops with an OP_Sort in between, or
3268 ** it might be a single loop that uses an index to extract information
3269 ** in the right order to begin with.
3270 */
3271 sqlite3VdbeResolveLabel(v, addrInitializeLoop);
3272 sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
3273 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy);
3274 if( pWInfo==0 ) goto select_end;
3275 if( pGroupBy==0 ){
3276 /* The optimizer is able to deliver rows in group by order so
3277 ** we do not have to sort. The OP_OpenEphemeral table will be
3278 ** cancelled later because we still need to use the pKeyInfo
3279 */
3280 pGroupBy = p->pGroupBy;
3281 groupBySort = 0;
3282 }else{
3283 /* Rows are coming out in undetermined order. We have to push
3284 ** each row into a sorting index, terminate the first loop,
3285 ** then loop over the sorting index in order to get the output
3286 ** in sorted order
3287 */
3288 groupBySort = 1;
3289 sqlite3ExprCodeExprList(pParse, pGroupBy);
3290 sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0);
3291 j = pGroupBy->nExpr+1;
3292 for(i=0; i<sAggInfo.nColumn; i++){
3293 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
3294 if( pCol->iSorterColumn<j ) continue;
3295 sqlite3ExprCodeGetColumn(v, pCol->pTab, pCol->iColumn, pCol->iTable);
3296 j++;
3297 }
3298 sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0);
3299 sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0);
3300 sqlite3WhereEnd(pWInfo);
3301 sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
3302 VdbeComment((v, "# GROUP BY sort"));
3303 sAggInfo.useSortingIdx = 1;
3304 }
3305
3306 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
3307 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
3308 ** Then compare the current GROUP BY terms against the GROUP BY terms
3309 ** from the previous row currently stored in a0, a1, a2...
3310 */
3311 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
3312 for(j=0; j<pGroupBy->nExpr; j++){
3313 if( groupBySort ){
3314 sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j);
3315 }else{
3316 sAggInfo.directMode = 1;
3317 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr);
3318 }
3319 sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, j<pGroupBy->nExpr-1);
3320 }
3321 for(j=pGroupBy->nExpr-1; j>=0; j--){
3322 if( j<pGroupBy->nExpr-1 ){
3323 sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0);
3324 }
3325 sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0);
3326 if( j==0 ){
3327 sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow);
3328 }else{
3329 sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange);
3330 }
3331 sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ);
3332 }
3333
3334 /* Generate code that runs whenever the GROUP BY changes.
3335 ** Change in the GROUP BY are detected by the previous code
3336 ** block. If there were no changes, this block is skipped.
3337 **
3338 ** This code copies current group by terms in b0,b1,b2,...
3339 ** over to a0,a1,a2. It then calls the output subroutine
3340 ** and resets the aggregate accumulator registers in preparation
3341 ** for the next GROUP BY batch.
3342 */
3343 sqlite3VdbeResolveLabel(v, addrGroupByChange);
3344 for(j=0; j<pGroupBy->nExpr; j++){
3345 sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j);
3346 }
3347 sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
3348 VdbeComment((v, "# output one row"));
3349 sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd);
3350 VdbeComment((v, "# check abort flag"));
3351 sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
3352 VdbeComment((v, "# reset accumulator"));
3353
3354 /* Update the aggregate accumulators based on the content of
3355 ** the current row
3356 */
3357 sqlite3VdbeResolveLabel(v, addrProcessRow);
3358 updateAccumulator(pParse, &sAggInfo);
3359 sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag);
3360 VdbeComment((v, "# indicate data in accumulator"));
3361
3362 /* End of the loop
3363 */
3364 if( groupBySort ){
3365 sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
3366 }else{
3367 sqlite3WhereEnd(pWInfo);
3368 sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
3369 }
3370
3371 /* Output the final row of result
3372 */
3373 sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
3374 VdbeComment((v, "# output final row"));
3375
3376 } /* endif pGroupBy */
3377 else {
3378 /* This case runs if the aggregate has no GROUP BY clause. The
3379 ** processing is much simpler since there is only a single row
3380 ** of output.
3381 */
3382 resetAccumulator(pParse, &sAggInfo);
3383 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
3384 if( pWInfo==0 ) goto select_end;
3385 updateAccumulator(pParse, &sAggInfo);
3386 sqlite3WhereEnd(pWInfo);
3387 finalizeAggFunctions(pParse, &sAggInfo);
3388 pOrderBy = 0;
3389 if( pHaving ){
3390 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1);
3391 }
3392 selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
3393 eDest, iParm, addrEnd, addrEnd, aff);
3394 }
3395 sqlite3VdbeResolveLabel(v, addrEnd);
3396
3397 } /* endif aggregate query */
3398
3399 /* If there is an ORDER BY clause, then we need to sort the results
3400 ** and send them to the callback one by one.
3401 */
3402 if( pOrderBy ){
3403 generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
3404 }
3405
3406#ifndef SQLITE_OMIT_SUBQUERY
3407 /* If this was a subquery, we have now converted the subquery into a
3408 ** temporary table. So set the SrcList_item.isPopulated flag to prevent
3409 ** this subquery from being evaluated again and to force the use of
3410 ** the temporary table.
3411 */
3412 if( pParent ){
3413 assert( pParent->pSrc->nSrc>parentTab );
3414 assert( pParent->pSrc->a[parentTab].pSelect==p );
3415 pParent->pSrc->a[parentTab].isPopulated = 1;
3416 }
3417#endif
3418
3419 /* Jump here to skip this query
3420 */
3421 sqlite3VdbeResolveLabel(v, iEnd);
3422
3423 /* The SELECT was successfully coded. Set the return code to 0
3424 ** to indicate no errors.
3425 */
3426 rc = 0;
3427
3428 /* Control jumps to here if an error is encountered above, or upon
3429 ** successful coding of the SELECT.
3430 */
3431select_end:
3432
3433 /* Identify column names if we will be using them in a callback. This
3434 ** step is skipped if the output is going to some other destination.
3435 */
3436 if( rc==SQLITE_OK && eDest==SRT_Callback ){
3437 generateColumnNames(pParse, pTabList, pEList);
3438 }
3439
3440 sqlite3_free(sAggInfo.aCol);
3441 sqlite3_free(sAggInfo.aFunc);
3442 return rc;
3443}
3444
3445#if defined(SQLITE_DEBUG)
3446/*
3447*******************************************************************************
3448** The following code is used for testing and debugging only. The code
3449** that follows does not appear in normal builds.
3450**
3451** These routines are used to print out the content of all or part of a
3452** parse structures such as Select or Expr. Such printouts are useful
3453** for helping to understand what is happening inside the code generator
3454** during the execution of complex SELECT statements.
3455**
3456** These routine are not called anywhere from within the normal
3457** code base. Then are intended to be called from within the debugger
3458** or from temporary "printf" statements inserted for debugging.
3459*/
3460void sqlite3PrintExpr(Expr *p){
3461 if( p->token.z && p->token.n>0 ){
3462 sqlite3DebugPrintf("(%.*s", p->token.n, p->token.z);
3463 }else{
3464 sqlite3DebugPrintf("(%d", p->op);
3465 }
3466 if( p->pLeft ){
3467 sqlite3DebugPrintf(" ");
3468 sqlite3PrintExpr(p->pLeft);
3469 }
3470 if( p->pRight ){
3471 sqlite3DebugPrintf(" ");
3472 sqlite3PrintExpr(p->pRight);
3473 }
3474 sqlite3DebugPrintf(")");
3475}
3476void sqlite3PrintExprList(ExprList *pList){
3477 int i;
3478 for(i=0; i<pList->nExpr; i++){
3479 sqlite3PrintExpr(pList->a[i].pExpr);
3480 if( i<pList->nExpr-1 ){
3481 sqlite3DebugPrintf(", ");
3482 }
3483 }
3484}
3485void sqlite3PrintSelect(Select *p, int indent){
3486 sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
3487 sqlite3PrintExprList(p->pEList);
3488 sqlite3DebugPrintf("\n");
3489 if( p->pSrc ){
3490 char *zPrefix;
3491 int i;
3492 zPrefix = "FROM";
3493 for(i=0; i<p->pSrc->nSrc; i++){
3494 struct SrcList_item *pItem = &p->pSrc->a[i];
3495 sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
3496 zPrefix = "";
3497 if( pItem->pSelect ){
3498 sqlite3DebugPrintf("(\n");
3499 sqlite3PrintSelect(pItem->pSelect, indent+10);
3500 sqlite3DebugPrintf("%*s)", indent+8, "");
3501 }else if( pItem->zName ){
3502 sqlite3DebugPrintf("%s", pItem->zName);
3503 }
3504 if( pItem->pTab ){
3505 sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
3506 }
3507 if( pItem->zAlias ){
3508 sqlite3DebugPrintf(" AS %s", pItem->zAlias);
3509 }
3510 if( i<p->pSrc->nSrc-1 ){
3511 sqlite3DebugPrintf(",");
3512 }
3513 sqlite3DebugPrintf("\n");
3514 }
3515 }
3516 if( p->pWhere ){
3517 sqlite3DebugPrintf("%*s WHERE ", indent, "");
3518 sqlite3PrintExpr(p->pWhere);
3519 sqlite3DebugPrintf("\n");
3520 }
3521 if( p->pGroupBy ){
3522 sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
3523 sqlite3PrintExprList(p->pGroupBy);
3524 sqlite3DebugPrintf("\n");
3525 }
3526 if( p->pHaving ){
3527 sqlite3DebugPrintf("%*s HAVING ", indent, "");
3528 sqlite3PrintExpr(p->pHaving);
3529 sqlite3DebugPrintf("\n");
3530 }
3531 if( p->pOrderBy ){
3532 sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
3533 sqlite3PrintExprList(p->pOrderBy);
3534 sqlite3DebugPrintf("\n");
3535 }
3536}
3537/* End of the structure debug printing code
3538*****************************************************************************/
3539#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
diff --git a/libraries/sqlite/win32/shell.c b/libraries/sqlite/win32/shell.c
new file mode 100755
index 0000000..bd5105e
--- /dev/null
+++ b/libraries/sqlite/win32/shell.c
@@ -0,0 +1,2019 @@
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.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14**
15** $Id: shell.c,v 1.167 2007/09/07 01:12:32 drh Exp $
16*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20#include <assert.h>
21#include "sqlite3.h"
22#include <ctype.h>
23#include <stdarg.h>
24
25#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) && !defined(__OS2__)
26# include <signal.h>
27# include <pwd.h>
28# include <unistd.h>
29# include <sys/types.h>
30#endif
31
32#ifdef __MACOS__
33# include <console.h>
34# include <signal.h>
35# include <unistd.h>
36# include <extras.h>
37# include <Files.h>
38# include <Folders.h>
39#endif
40
41#ifdef __OS2__
42# include <unistd.h>
43#endif
44
45#if defined(HAVE_READLINE) && HAVE_READLINE==1
46# include <readline/readline.h>
47# include <readline/history.h>
48#else
49# define readline(p) local_getline(p,stdin)
50# define add_history(X)
51# define read_history(X)
52# define write_history(X)
53# define stifle_history(X)
54#endif
55
56#if defined(_WIN32) || defined(WIN32)
57# include <io.h>
58#else
59/* Make sure isatty() has a prototype.
60*/
61extern int isatty();
62#endif
63
64/*
65** If the following flag is set, then command execution stops
66** at an error if we are not interactive.
67*/
68static int bail_on_error = 0;
69
70/*
71** Threat stdin as an interactive input if the following variable
72** is true. Otherwise, assume stdin is connected to a file or pipe.
73*/
74static int stdin_is_interactive = 1;
75
76/*
77** The following is the open SQLite database. We make a pointer
78** to this database a static variable so that it can be accessed
79** by the SIGINT handler to interrupt database processing.
80*/
81static sqlite3 *db = 0;
82
83/*
84** True if an interrupt (Control-C) has been received.
85*/
86static volatile int seenInterrupt = 0;
87
88/*
89** This is the name of our program. It is set in main(), used
90** in a number of other places, mostly for error messages.
91*/
92static char *Argv0;
93
94/*
95** Prompt strings. Initialized in main. Settable with
96** .prompt main continue
97*/
98static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
99static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
100
101/*
102** Write I/O traces to the following stream.
103*/
104#ifdef SQLITE_ENABLE_IOTRACE
105static FILE *iotrace = 0;
106#endif
107
108/*
109** This routine works like printf in that its first argument is a
110** format string and subsequent arguments are values to be substituted
111** in place of % fields. The result of formatting this string
112** is written to iotrace.
113*/
114#ifdef SQLITE_ENABLE_IOTRACE
115static void iotracePrintf(const char *zFormat, ...){
116 va_list ap;
117 char *z;
118 if( iotrace==0 ) return;
119 va_start(ap, zFormat);
120 z = sqlite3_vmprintf(zFormat, ap);
121 va_end(ap);
122 fprintf(iotrace, "%s", z);
123 sqlite3_free(z);
124}
125#endif
126
127
128/*
129** Determines if a string is a number of not.
130*/
131static int isNumber(const char *z, int *realnum){
132 if( *z=='-' || *z=='+' ) z++;
133 if( !isdigit(*z) ){
134 return 0;
135 }
136 z++;
137 if( realnum ) *realnum = 0;
138 while( isdigit(*z) ){ z++; }
139 if( *z=='.' ){
140 z++;
141 if( !isdigit(*z) ) return 0;
142 while( isdigit(*z) ){ z++; }
143 if( realnum ) *realnum = 1;
144 }
145 if( *z=='e' || *z=='E' ){
146 z++;
147 if( *z=='+' || *z=='-' ) z++;
148 if( !isdigit(*z) ) return 0;
149 while( isdigit(*z) ){ z++; }
150 if( realnum ) *realnum = 1;
151 }
152 return *z==0;
153}
154
155/*
156** A global char* and an SQL function to access its current value
157** from within an SQL statement. This program used to use the
158** sqlite_exec_printf() API to substitue a string into an SQL statement.
159** The correct way to do this with sqlite3 is to use the bind API, but
160** since the shell is built around the callback paradigm it would be a lot
161** of work. Instead just use this hack, which is quite harmless.
162*/
163static const char *zShellStatic = 0;
164static void shellstaticFunc(
165 sqlite3_context *context,
166 int argc,
167 sqlite3_value **argv
168){
169 assert( 0==argc );
170 assert( zShellStatic );
171 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
172}
173
174
175/*
176** This routine reads a line of text from FILE in, stores
177** the text in memory obtained from malloc() and returns a pointer
178** to the text. NULL is returned at end of file, or if malloc()
179** fails.
180**
181** The interface is like "readline" but no command-line editing
182** is done.
183*/
184static char *local_getline(char *zPrompt, FILE *in){
185 char *zLine;
186 int nLine;
187 int n;
188 int eol;
189
190 if( zPrompt && *zPrompt ){
191 printf("%s",zPrompt);
192 fflush(stdout);
193 }
194 nLine = 100;
195 zLine = malloc( nLine );
196 if( zLine==0 ) return 0;
197 n = 0;
198 eol = 0;
199 while( !eol ){
200 if( n+100>nLine ){
201 nLine = nLine*2 + 100;
202 zLine = realloc(zLine, nLine);
203 if( zLine==0 ) return 0;
204 }
205 if( fgets(&zLine[n], nLine - n, in)==0 ){
206 if( n==0 ){
207 free(zLine);
208 return 0;
209 }
210 zLine[n] = 0;
211 eol = 1;
212 break;
213 }
214 while( zLine[n] ){ n++; }
215 if( n>0 && zLine[n-1]=='\n' ){
216 n--;
217 zLine[n] = 0;
218 eol = 1;
219 }
220 }
221 zLine = realloc( zLine, n+1 );
222 return zLine;
223}
224
225/*
226** Retrieve a single line of input text.
227**
228** zPrior is a string of prior text retrieved. If not the empty
229** string, then issue a continuation prompt.
230*/
231static char *one_input_line(const char *zPrior, FILE *in){
232 char *zPrompt;
233 char *zResult;
234 if( in!=0 ){
235 return local_getline(0, in);
236 }
237 if( zPrior && zPrior[0] ){
238 zPrompt = continuePrompt;
239 }else{
240 zPrompt = mainPrompt;
241 }
242 zResult = readline(zPrompt);
243#if defined(HAVE_READLINE) && HAVE_READLINE==1
244 if( zResult && *zResult ) add_history(zResult);
245#endif
246 return zResult;
247}
248
249struct previous_mode_data {
250 int valid; /* Is there legit data in here? */
251 int mode;
252 int showHeader;
253 int colWidth[100];
254};
255
256/*
257** An pointer to an instance of this structure is passed from
258** the main program to the callback. This is used to communicate
259** state and mode information.
260*/
261struct callback_data {
262 sqlite3 *db; /* The database */
263 int echoOn; /* True to echo input commands */
264 int cnt; /* Number of records displayed so far */
265 FILE *out; /* Write results here */
266 int mode; /* An output mode setting */
267 int writableSchema; /* True if PRAGMA writable_schema=ON */
268 int showHeader; /* True to show column names in List or Column mode */
269 char *zDestTable; /* Name of destination table when MODE_Insert */
270 char separator[20]; /* Separator character for MODE_List */
271 int colWidth[100]; /* Requested width of each column when in column mode*/
272 int actualWidth[100]; /* Actual width of each column */
273 char nullvalue[20]; /* The text to print when a NULL comes back from
274 ** the database */
275 struct previous_mode_data explainPrev;
276 /* Holds the mode information just before
277 ** .explain ON */
278 char outfile[FILENAME_MAX]; /* Filename for *out */
279 const char *zDbFilename; /* name of the database file */
280};
281
282/*
283** These are the allowed modes.
284*/
285#define MODE_Line 0 /* One column per line. Blank line between records */
286#define MODE_Column 1 /* One record per line in neat columns */
287#define MODE_List 2 /* One record per line with a separator */
288#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
289#define MODE_Html 4 /* Generate an XHTML table */
290#define MODE_Insert 5 /* Generate SQL "insert" statements */
291#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
292#define MODE_Csv 7 /* Quote strings, numbers are plain */
293#define MODE_NUM_OF 8 /* The number of modes (not a mode itself) */
294
295static const char *modeDescr[MODE_NUM_OF] = {
296 "line",
297 "column",
298 "list",
299 "semi",
300 "html",
301 "insert",
302 "tcl",
303 "csv",
304};
305
306/*
307** Number of elements in an array
308*/
309#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
310
311/*
312** Output the given string as a quoted string using SQL quoting conventions.
313*/
314static void output_quoted_string(FILE *out, const char *z){
315 int i;
316 int nSingle = 0;
317 for(i=0; z[i]; i++){
318 if( z[i]=='\'' ) nSingle++;
319 }
320 if( nSingle==0 ){
321 fprintf(out,"'%s'",z);
322 }else{
323 fprintf(out,"'");
324 while( *z ){
325 for(i=0; z[i] && z[i]!='\''; i++){}
326 if( i==0 ){
327 fprintf(out,"''");
328 z++;
329 }else if( z[i]=='\'' ){
330 fprintf(out,"%.*s''",i,z);
331 z += i+1;
332 }else{
333 fprintf(out,"%s",z);
334 break;
335 }
336 }
337 fprintf(out,"'");
338 }
339}
340
341/*
342** Output the given string as a quoted according to C or TCL quoting rules.
343*/
344static void output_c_string(FILE *out, const char *z){
345 unsigned int c;
346 fputc('"', out);
347 while( (c = *(z++))!=0 ){
348 if( c=='\\' ){
349 fputc(c, out);
350 fputc(c, out);
351 }else if( c=='\t' ){
352 fputc('\\', out);
353 fputc('t', out);
354 }else if( c=='\n' ){
355 fputc('\\', out);
356 fputc('n', out);
357 }else if( c=='\r' ){
358 fputc('\\', out);
359 fputc('r', out);
360 }else if( !isprint(c) ){
361 fprintf(out, "\\%03o", c&0xff);
362 }else{
363 fputc(c, out);
364 }
365 }
366 fputc('"', out);
367}
368
369/*
370** Output the given string with characters that are special to
371** HTML escaped.
372*/
373static void output_html_string(FILE *out, const char *z){
374 int i;
375 while( *z ){
376 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
377 if( i>0 ){
378 fprintf(out,"%.*s",i,z);
379 }
380 if( z[i]=='<' ){
381 fprintf(out,"&lt;");
382 }else if( z[i]=='&' ){
383 fprintf(out,"&amp;");
384 }else{
385 break;
386 }
387 z += i + 1;
388 }
389}
390
391/*
392** If a field contains any character identified by a 1 in the following
393** array, then the string must be quoted for CSV.
394*/
395static const char needCsvQuote[] = {
396 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
397 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
398 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
400 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
401 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
402 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
403 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
404 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
405 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
406 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
407 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
408 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
409 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
410 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
411 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
412};
413
414/*
415** Output a single term of CSV. Actually, p->separator is used for
416** the separator, which may or may not be a comma. p->nullvalue is
417** the null value. Strings are quoted using ANSI-C rules. Numbers
418** appear outside of quotes.
419*/
420static void output_csv(struct callback_data *p, const char *z, int bSep){
421 FILE *out = p->out;
422 if( z==0 ){
423 fprintf(out,"%s",p->nullvalue);
424 }else{
425 int i;
426 for(i=0; z[i]; i++){
427 if( needCsvQuote[((unsigned char*)z)[i]] ){
428 i = 0;
429 break;
430 }
431 }
432 if( i==0 ){
433 putc('"', out);
434 for(i=0; z[i]; i++){
435 if( z[i]=='"' ) putc('"', out);
436 putc(z[i], out);
437 }
438 putc('"', out);
439 }else{
440 fprintf(out, "%s", z);
441 }
442 }
443 if( bSep ){
444 fprintf(p->out, p->separator);
445 }
446}
447
448#ifdef SIGINT
449/*
450** This routine runs when the user presses Ctrl-C
451*/
452static void interrupt_handler(int NotUsed){
453 seenInterrupt = 1;
454 if( db ) sqlite3_interrupt(db);
455}
456#endif
457
458/*
459** This is the callback routine that the SQLite library
460** invokes for each row of a query result.
461*/
462static int callback(void *pArg, int nArg, char **azArg, char **azCol){
463 int i;
464 struct callback_data *p = (struct callback_data*)pArg;
465 switch( p->mode ){
466 case MODE_Line: {
467 int w = 5;
468 if( azArg==0 ) break;
469 for(i=0; i<nArg; i++){
470 int len = strlen(azCol[i] ? azCol[i] : "");
471 if( len>w ) w = len;
472 }
473 if( p->cnt++>0 ) fprintf(p->out,"\n");
474 for(i=0; i<nArg; i++){
475 fprintf(p->out,"%*s = %s\n", w, azCol[i],
476 azArg[i] ? azArg[i] : p->nullvalue);
477 }
478 break;
479 }
480 case MODE_Column: {
481 if( p->cnt++==0 ){
482 for(i=0; i<nArg; i++){
483 int w, n;
484 if( i<ArraySize(p->colWidth) ){
485 w = p->colWidth[i];
486 }else{
487 w = 0;
488 }
489 if( w<=0 ){
490 w = strlen(azCol[i] ? azCol[i] : "");
491 if( w<10 ) w = 10;
492 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
493 if( w<n ) w = n;
494 }
495 if( i<ArraySize(p->actualWidth) ){
496 p->actualWidth[i] = w;
497 }
498 if( p->showHeader ){
499 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
500 }
501 }
502 if( p->showHeader ){
503 for(i=0; i<nArg; i++){
504 int w;
505 if( i<ArraySize(p->actualWidth) ){
506 w = p->actualWidth[i];
507 }else{
508 w = 10;
509 }
510 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
511 "----------------------------------------------------------",
512 i==nArg-1 ? "\n": " ");
513 }
514 }
515 }
516 if( azArg==0 ) break;
517 for(i=0; i<nArg; i++){
518 int w;
519 if( i<ArraySize(p->actualWidth) ){
520 w = p->actualWidth[i];
521 }else{
522 w = 10;
523 }
524 fprintf(p->out,"%-*.*s%s",w,w,
525 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
526 }
527 break;
528 }
529 case MODE_Semi:
530 case MODE_List: {
531 if( p->cnt++==0 && p->showHeader ){
532 for(i=0; i<nArg; i++){
533 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
534 }
535 }
536 if( azArg==0 ) break;
537 for(i=0; i<nArg; i++){
538 char *z = azArg[i];
539 if( z==0 ) z = p->nullvalue;
540 fprintf(p->out, "%s", z);
541 if( i<nArg-1 ){
542 fprintf(p->out, "%s", p->separator);
543 }else if( p->mode==MODE_Semi ){
544 fprintf(p->out, ";\n");
545 }else{
546 fprintf(p->out, "\n");
547 }
548 }
549 break;
550 }
551 case MODE_Html: {
552 if( p->cnt++==0 && p->showHeader ){
553 fprintf(p->out,"<TR>");
554 for(i=0; i<nArg; i++){
555 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
556 }
557 fprintf(p->out,"</TR>\n");
558 }
559 if( azArg==0 ) break;
560 fprintf(p->out,"<TR>");
561 for(i=0; i<nArg; i++){
562 fprintf(p->out,"<TD>");
563 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
564 fprintf(p->out,"</TD>\n");
565 }
566 fprintf(p->out,"</TR>\n");
567 break;
568 }
569 case MODE_Tcl: {
570 if( p->cnt++==0 && p->showHeader ){
571 for(i=0; i<nArg; i++){
572 output_c_string(p->out,azCol[i] ? azCol[i] : "");
573 fprintf(p->out, "%s", p->separator);
574 }
575 fprintf(p->out,"\n");
576 }
577 if( azArg==0 ) break;
578 for(i=0; i<nArg; i++){
579 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
580 fprintf(p->out, "%s", p->separator);
581 }
582 fprintf(p->out,"\n");
583 break;
584 }
585 case MODE_Csv: {
586 if( p->cnt++==0 && p->showHeader ){
587 for(i=0; i<nArg; i++){
588 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
589 }
590 fprintf(p->out,"\n");
591 }
592 if( azArg==0 ) break;
593 for(i=0; i<nArg; i++){
594 output_csv(p, azArg[i], i<nArg-1);
595 }
596 fprintf(p->out,"\n");
597 break;
598 }
599 case MODE_Insert: {
600 if( azArg==0 ) break;
601 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
602 for(i=0; i<nArg; i++){
603 char *zSep = i>0 ? ",": "";
604 if( azArg[i]==0 ){
605 fprintf(p->out,"%sNULL",zSep);
606 }else if( isNumber(azArg[i], 0) ){
607 fprintf(p->out,"%s%s",zSep, azArg[i]);
608 }else{
609 if( zSep[0] ) fprintf(p->out,"%s",zSep);
610 output_quoted_string(p->out, azArg[i]);
611 }
612 }
613 fprintf(p->out,");\n");
614 break;
615 }
616 }
617 return 0;
618}
619
620/*
621** Set the destination table field of the callback_data structure to
622** the name of the table given. Escape any quote characters in the
623** table name.
624*/
625static void set_table_name(struct callback_data *p, const char *zName){
626 int i, n;
627 int needQuote;
628 char *z;
629
630 if( p->zDestTable ){
631 free(p->zDestTable);
632 p->zDestTable = 0;
633 }
634 if( zName==0 ) return;
635 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
636 for(i=n=0; zName[i]; i++, n++){
637 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
638 needQuote = 1;
639 if( zName[i]=='\'' ) n++;
640 }
641 }
642 if( needQuote ) n += 2;
643 z = p->zDestTable = malloc( n+1 );
644 if( z==0 ){
645 fprintf(stderr,"Out of memory!\n");
646 exit(1);
647 }
648 n = 0;
649 if( needQuote ) z[n++] = '\'';
650 for(i=0; zName[i]; i++){
651 z[n++] = zName[i];
652 if( zName[i]=='\'' ) z[n++] = '\'';
653 }
654 if( needQuote ) z[n++] = '\'';
655 z[n] = 0;
656}
657
658/* zIn is either a pointer to a NULL-terminated string in memory obtained
659** from malloc(), or a NULL pointer. The string pointed to by zAppend is
660** added to zIn, and the result returned in memory obtained from malloc().
661** zIn, if it was not NULL, is freed.
662**
663** If the third argument, quote, is not '\0', then it is used as a
664** quote character for zAppend.
665*/
666static char *appendText(char *zIn, char const *zAppend, char quote){
667 int len;
668 int i;
669 int nAppend = strlen(zAppend);
670 int nIn = (zIn?strlen(zIn):0);
671
672 len = nAppend+nIn+1;
673 if( quote ){
674 len += 2;
675 for(i=0; i<nAppend; i++){
676 if( zAppend[i]==quote ) len++;
677 }
678 }
679
680 zIn = (char *)realloc(zIn, len);
681 if( !zIn ){
682 return 0;
683 }
684
685 if( quote ){
686 char *zCsr = &zIn[nIn];
687 *zCsr++ = quote;
688 for(i=0; i<nAppend; i++){
689 *zCsr++ = zAppend[i];
690 if( zAppend[i]==quote ) *zCsr++ = quote;
691 }
692 *zCsr++ = quote;
693 *zCsr++ = '\0';
694 assert( (zCsr-zIn)==len );
695 }else{
696 memcpy(&zIn[nIn], zAppend, nAppend);
697 zIn[len-1] = '\0';
698 }
699
700 return zIn;
701}
702
703
704/*
705** Execute a query statement that has a single result column. Print
706** that result column on a line by itself with a semicolon terminator.
707**
708** This is used, for example, to show the schema of the database by
709** querying the SQLITE_MASTER table.
710*/
711static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){
712 sqlite3_stmt *pSelect;
713 int rc;
714 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
715 if( rc!=SQLITE_OK || !pSelect ){
716 return rc;
717 }
718 rc = sqlite3_step(pSelect);
719 while( rc==SQLITE_ROW ){
720 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
721 rc = sqlite3_step(pSelect);
722 }
723 return sqlite3_finalize(pSelect);
724}
725
726
727/*
728** This is a different callback routine used for dumping the database.
729** Each row received by this callback consists of a table name,
730** the table type ("index" or "table") and SQL to create the table.
731** This routine should print text sufficient to recreate the table.
732*/
733static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
734 int rc;
735 const char *zTable;
736 const char *zType;
737 const char *zSql;
738 struct callback_data *p = (struct callback_data *)pArg;
739
740 if( nArg!=3 ) return 1;
741 zTable = azArg[0];
742 zType = azArg[1];
743 zSql = azArg[2];
744
745 if( strcmp(zTable, "sqlite_sequence")==0 ){
746 fprintf(p->out, "DELETE FROM sqlite_sequence;\n");
747 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
748 fprintf(p->out, "ANALYZE sqlite_master;\n");
749 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
750 return 0;
751 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
752 char *zIns;
753 if( !p->writableSchema ){
754 fprintf(p->out, "PRAGMA writable_schema=ON;\n");
755 p->writableSchema = 1;
756 }
757 zIns = sqlite3_mprintf(
758 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
759 "VALUES('table','%q','%q',0,'%q');",
760 zTable, zTable, zSql);
761 fprintf(p->out, "%s\n", zIns);
762 sqlite3_free(zIns);
763 return 0;
764 }else{
765 fprintf(p->out, "%s;\n", zSql);
766 }
767
768 if( strcmp(zType, "table")==0 ){
769 sqlite3_stmt *pTableInfo = 0;
770 char *zSelect = 0;
771 char *zTableInfo = 0;
772 char *zTmp = 0;
773
774 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
775 zTableInfo = appendText(zTableInfo, zTable, '"');
776 zTableInfo = appendText(zTableInfo, ");", 0);
777
778 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
779 if( zTableInfo ) free(zTableInfo);
780 if( rc!=SQLITE_OK || !pTableInfo ){
781 return 1;
782 }
783
784 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
785 zTmp = appendText(zTmp, zTable, '"');
786 if( zTmp ){
787 zSelect = appendText(zSelect, zTmp, '\'');
788 }
789 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
790 rc = sqlite3_step(pTableInfo);
791 while( rc==SQLITE_ROW ){
792 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
793 zSelect = appendText(zSelect, "quote(", 0);
794 zSelect = appendText(zSelect, zText, '"');
795 rc = sqlite3_step(pTableInfo);
796 if( rc==SQLITE_ROW ){
797 zSelect = appendText(zSelect, ") || ',' || ", 0);
798 }else{
799 zSelect = appendText(zSelect, ") ", 0);
800 }
801 }
802 rc = sqlite3_finalize(pTableInfo);
803 if( rc!=SQLITE_OK ){
804 if( zSelect ) free(zSelect);
805 return 1;
806 }
807 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
808 zSelect = appendText(zSelect, zTable, '"');
809
810 rc = run_table_dump_query(p->out, p->db, zSelect);
811 if( rc==SQLITE_CORRUPT ){
812 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
813 rc = run_table_dump_query(p->out, p->db, zSelect);
814 }
815 if( zSelect ) free(zSelect);
816 }
817 return 0;
818}
819
820/*
821** Run zQuery. Use dump_callback() as the callback routine so that
822** the contents of the query are output as SQL statements.
823**
824** If we get a SQLITE_CORRUPT error, rerun the query after appending
825** "ORDER BY rowid DESC" to the end.
826*/
827static int run_schema_dump_query(
828 struct callback_data *p,
829 const char *zQuery,
830 char **pzErrMsg
831){
832 int rc;
833 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
834 if( rc==SQLITE_CORRUPT ){
835 char *zQ2;
836 int len = strlen(zQuery);
837 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
838 zQ2 = malloc( len+100 );
839 if( zQ2==0 ) return rc;
840 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
841 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
842 free(zQ2);
843 }
844 return rc;
845}
846
847/*
848** Text of a help message
849*/
850static char zHelp[] =
851 ".bail ON|OFF Stop after hitting an error. Default OFF\n"
852 ".databases List names and files of attached databases\n"
853 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
854 ".echo ON|OFF Turn command echo on or off\n"
855 ".exit Exit this program\n"
856 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
857 ".header(s) ON|OFF Turn display of headers on or off\n"
858 ".help Show this message\n"
859 ".import FILE TABLE Import data from FILE into TABLE\n"
860 ".indices TABLE Show names of all indices on TABLE\n"
861#ifdef SQLITE_ENABLE_IOTRACE
862 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
863#endif
864#ifndef SQLITE_OMIT_LOAD_EXTENSION
865 ".load FILE ?ENTRY? Load an extension library\n"
866#endif
867 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
868 " csv Comma-separated values\n"
869 " column Left-aligned columns. (See .width)\n"
870 " html HTML <table> code\n"
871 " insert SQL insert statements for TABLE\n"
872 " line One value per line\n"
873 " list Values delimited by .separator string\n"
874 " tabs Tab-separated values\n"
875 " tcl TCL list elements\n"
876 ".nullvalue STRING Print STRING in place of NULL values\n"
877 ".output FILENAME Send output to FILENAME\n"
878 ".output stdout Send output to the screen\n"
879 ".prompt MAIN CONTINUE Replace the standard prompts\n"
880 ".quit Exit this program\n"
881 ".read FILENAME Execute SQL in FILENAME\n"
882 ".schema ?TABLE? Show the CREATE statements\n"
883 ".separator STRING Change separator used by output mode and .import\n"
884 ".show Show the current values for various settings\n"
885 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
886 ".timeout MS Try opening locked tables for MS milliseconds\n"
887 ".width NUM NUM ... Set column widths for \"column\" mode\n"
888;
889
890/* Forward reference */
891static int process_input(struct callback_data *p, FILE *in);
892
893/*
894** Make sure the database is open. If it is not, then open it. If
895** the database fails to open, print an error message and exit.
896*/
897static void open_db(struct callback_data *p){
898 if( p->db==0 ){
899 sqlite3_open(p->zDbFilename, &p->db);
900 db = p->db;
901 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
902 shellstaticFunc, 0, 0);
903 if( SQLITE_OK!=sqlite3_errcode(db) ){
904 fprintf(stderr,"Unable to open database \"%s\": %s\n",
905 p->zDbFilename, sqlite3_errmsg(db));
906 exit(1);
907 }
908#ifndef SQLITE_OMIT_LOAD_EXTENSION
909 sqlite3_enable_load_extension(p->db, 1);
910#endif
911 }
912}
913
914/*
915** Do C-language style dequoting.
916**
917** \t -> tab
918** \n -> newline
919** \r -> carriage return
920** \NNN -> ascii character NNN in octal
921** \\ -> backslash
922*/
923static void resolve_backslashes(char *z){
924 int i, j, c;
925 for(i=j=0; (c = z[i])!=0; i++, j++){
926 if( c=='\\' ){
927 c = z[++i];
928 if( c=='n' ){
929 c = '\n';
930 }else if( c=='t' ){
931 c = '\t';
932 }else if( c=='r' ){
933 c = '\r';
934 }else if( c>='0' && c<='7' ){
935 c -= '0';
936 if( z[i+1]>='0' && z[i+1]<='7' ){
937 i++;
938 c = (c<<3) + z[i] - '0';
939 if( z[i+1]>='0' && z[i+1]<='7' ){
940 i++;
941 c = (c<<3) + z[i] - '0';
942 }
943 }
944 }
945 }
946 z[j] = c;
947 }
948 z[j] = 0;
949}
950
951/*
952** Interpret zArg as a boolean value. Return either 0 or 1.
953*/
954static int booleanValue(char *zArg){
955 int val = atoi(zArg);
956 int j;
957 for(j=0; zArg[j]; j++){
958 zArg[j] = tolower(zArg[j]);
959 }
960 if( strcmp(zArg,"on")==0 ){
961 val = 1;
962 }else if( strcmp(zArg,"yes")==0 ){
963 val = 1;
964 }
965 return val;
966}
967
968/*
969** If an input line begins with "." then invoke this routine to
970** process that line.
971**
972** Return 1 on error, 2 to exit, and 0 otherwise.
973*/
974static int do_meta_command(char *zLine, struct callback_data *p){
975 int i = 1;
976 int nArg = 0;
977 int n, c;
978 int rc = 0;
979 char *azArg[50];
980
981 /* Parse the input line into tokens.
982 */
983 while( zLine[i] && nArg<ArraySize(azArg) ){
984 while( isspace((unsigned char)zLine[i]) ){ i++; }
985 if( zLine[i]==0 ) break;
986 if( zLine[i]=='\'' || zLine[i]=='"' ){
987 int delim = zLine[i++];
988 azArg[nArg++] = &zLine[i];
989 while( zLine[i] && zLine[i]!=delim ){ i++; }
990 if( zLine[i]==delim ){
991 zLine[i++] = 0;
992 }
993 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
994 }else{
995 azArg[nArg++] = &zLine[i];
996 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
997 if( zLine[i] ) zLine[i++] = 0;
998 resolve_backslashes(azArg[nArg-1]);
999 }
1000 }
1001
1002 /* Process the input line.
1003 */
1004 if( nArg==0 ) return rc;
1005 n = strlen(azArg[0]);
1006 c = azArg[0][0];
1007 if( c=='b' && n>1 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){
1008 bail_on_error = booleanValue(azArg[1]);
1009 }else
1010
1011 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
1012 struct callback_data data;
1013 char *zErrMsg = 0;
1014 open_db(p);
1015 memcpy(&data, p, sizeof(data));
1016 data.showHeader = 1;
1017 data.mode = MODE_Column;
1018 data.colWidth[0] = 3;
1019 data.colWidth[1] = 15;
1020 data.colWidth[2] = 58;
1021 data.cnt = 0;
1022 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
1023 if( zErrMsg ){
1024 fprintf(stderr,"Error: %s\n", zErrMsg);
1025 sqlite3_free(zErrMsg);
1026 }
1027 }else
1028
1029 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
1030 char *zErrMsg = 0;
1031 open_db(p);
1032 fprintf(p->out, "BEGIN TRANSACTION;\n");
1033 p->writableSchema = 0;
1034 if( nArg==1 ){
1035 run_schema_dump_query(p,
1036 "SELECT name, type, sql FROM sqlite_master "
1037 "WHERE sql NOT NULL AND type=='table'", 0
1038 );
1039 run_table_dump_query(p->out, p->db,
1040 "SELECT sql FROM sqlite_master "
1041 "WHERE sql NOT NULL AND type IN ('index','trigger','view')"
1042 );
1043 }else{
1044 int i;
1045 for(i=1; i<nArg; i++){
1046 zShellStatic = azArg[i];
1047 run_schema_dump_query(p,
1048 "SELECT name, type, sql FROM sqlite_master "
1049 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
1050 " AND sql NOT NULL", 0);
1051 run_table_dump_query(p->out, p->db,
1052 "SELECT sql FROM sqlite_master "
1053 "WHERE sql NOT NULL"
1054 " AND type IN ('index','trigger','view')"
1055 " AND tbl_name LIKE shellstatic()"
1056 );
1057 zShellStatic = 0;
1058 }
1059 }
1060 if( p->writableSchema ){
1061 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
1062 p->writableSchema = 0;
1063 }
1064 if( zErrMsg ){
1065 fprintf(stderr,"Error: %s\n", zErrMsg);
1066 sqlite3_free(zErrMsg);
1067 }else{
1068 fprintf(p->out, "COMMIT;\n");
1069 }
1070 }else
1071
1072 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
1073 p->echoOn = booleanValue(azArg[1]);
1074 }else
1075
1076 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
1077 rc = 2;
1078 }else
1079
1080 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
1081 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
1082 if(val == 1) {
1083 if(!p->explainPrev.valid) {
1084 p->explainPrev.valid = 1;
1085 p->explainPrev.mode = p->mode;
1086 p->explainPrev.showHeader = p->showHeader;
1087 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
1088 }
1089 /* We could put this code under the !p->explainValid
1090 ** condition so that it does not execute if we are already in
1091 ** explain mode. However, always executing it allows us an easy
1092 ** was to reset to explain mode in case the user previously
1093 ** did an .explain followed by a .width, .mode or .header
1094 ** command.
1095 */
1096 p->mode = MODE_Column;
1097 p->showHeader = 1;
1098 memset(p->colWidth,0,ArraySize(p->colWidth));
1099 p->colWidth[0] = 4;
1100 p->colWidth[1] = 14;
1101 p->colWidth[2] = 10;
1102 p->colWidth[3] = 10;
1103 p->colWidth[4] = 33;
1104 }else if (p->explainPrev.valid) {
1105 p->explainPrev.valid = 0;
1106 p->mode = p->explainPrev.mode;
1107 p->showHeader = p->explainPrev.showHeader;
1108 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
1109 }
1110 }else
1111
1112 if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
1113 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
1114 p->showHeader = booleanValue(azArg[1]);
1115 }else
1116
1117 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
1118 fprintf(stderr,zHelp);
1119 }else
1120
1121 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
1122 char *zTable = azArg[2]; /* Insert data into this table */
1123 char *zFile = azArg[1]; /* The file from which to extract data */
1124 sqlite3_stmt *pStmt; /* A statement */
1125 int rc; /* Result code */
1126 int nCol; /* Number of columns in the table */
1127 int nByte; /* Number of bytes in an SQL string */
1128 int i, j; /* Loop counters */
1129 int nSep; /* Number of bytes in p->separator[] */
1130 char *zSql; /* An SQL statement */
1131 char *zLine; /* A single line of input from the file */
1132 char **azCol; /* zLine[] broken up into columns */
1133 char *zCommit; /* How to commit changes */
1134 FILE *in; /* The input file */
1135 int lineno = 0; /* Line number of input file */
1136
1137 open_db(p);
1138 nSep = strlen(p->separator);
1139 if( nSep==0 ){
1140 fprintf(stderr, "non-null separator required for import\n");
1141 return 0;
1142 }
1143 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1144 if( zSql==0 ) return 0;
1145 nByte = strlen(zSql);
1146 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
1147 sqlite3_free(zSql);
1148 if( rc ){
1149 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1150 nCol = 0;
1151 rc = 1;
1152 }else{
1153 nCol = sqlite3_column_count(pStmt);
1154 }
1155 sqlite3_finalize(pStmt);
1156 if( nCol==0 ) return 0;
1157 zSql = malloc( nByte + 20 + nCol*2 );
1158 if( zSql==0 ) return 0;
1159 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1160 j = strlen(zSql);
1161 for(i=1; i<nCol; i++){
1162 zSql[j++] = ',';
1163 zSql[j++] = '?';
1164 }
1165 zSql[j++] = ')';
1166 zSql[j] = 0;
1167 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
1168 free(zSql);
1169 if( rc ){
1170 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
1171 sqlite3_finalize(pStmt);
1172 return 1;
1173 }
1174 in = fopen(zFile, "rb");
1175 if( in==0 ){
1176 fprintf(stderr, "cannot open file: %s\n", zFile);
1177 sqlite3_finalize(pStmt);
1178 return 0;
1179 }
1180 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1181 if( azCol==0 ){
1182 fclose(in);
1183 return 0;
1184 }
1185 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1186 zCommit = "COMMIT";
1187 while( (zLine = local_getline(0, in))!=0 ){
1188 char *z;
1189 i = 0;
1190 lineno++;
1191 azCol[0] = zLine;
1192 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
1193 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1194 *z = 0;
1195 i++;
1196 if( i<nCol ){
1197 azCol[i] = &z[nSep];
1198 z += nSep-1;
1199 }
1200 }
1201 }
1202 *z = 0;
1203 if( i+1!=nCol ){
1204 fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
1205 zFile, lineno, nCol, i+1);
1206 zCommit = "ROLLBACK";
1207 break;
1208 }
1209 for(i=0; i<nCol; i++){
1210 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1211 }
1212 sqlite3_step(pStmt);
1213 rc = sqlite3_reset(pStmt);
1214 free(zLine);
1215 if( rc!=SQLITE_OK ){
1216 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1217 zCommit = "ROLLBACK";
1218 rc = 1;
1219 break;
1220 }
1221 }
1222 free(azCol);
1223 fclose(in);
1224 sqlite3_finalize(pStmt);
1225 sqlite3_exec(p->db, zCommit, 0, 0, 0);
1226 }else
1227
1228 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
1229 struct callback_data data;
1230 char *zErrMsg = 0;
1231 open_db(p);
1232 memcpy(&data, p, sizeof(data));
1233 data.showHeader = 0;
1234 data.mode = MODE_List;
1235 zShellStatic = azArg[1];
1236 sqlite3_exec(p->db,
1237 "SELECT name FROM sqlite_master "
1238 "WHERE type='index' AND tbl_name LIKE shellstatic() "
1239 "UNION ALL "
1240 "SELECT name FROM sqlite_temp_master "
1241 "WHERE type='index' AND tbl_name LIKE shellstatic() "
1242 "ORDER BY 1",
1243 callback, &data, &zErrMsg
1244 );
1245 zShellStatic = 0;
1246 if( zErrMsg ){
1247 fprintf(stderr,"Error: %s\n", zErrMsg);
1248 sqlite3_free(zErrMsg);
1249 }
1250 }else
1251
1252#ifdef SQLITE_ENABLE_IOTRACE
1253 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
1254 extern void (*sqlite3_io_trace)(const char*, ...);
1255 if( iotrace && iotrace!=stdout ) fclose(iotrace);
1256 iotrace = 0;
1257 if( nArg<2 ){
1258 sqlite3_io_trace = 0;
1259 }else if( strcmp(azArg[1], "-")==0 ){
1260 sqlite3_io_trace = iotracePrintf;
1261 iotrace = stdout;
1262 }else{
1263 iotrace = fopen(azArg[1], "w");
1264 if( iotrace==0 ){
1265 fprintf(stderr, "cannot open \"%s\"\n", azArg[1]);
1266 sqlite3_io_trace = 0;
1267 }else{
1268 sqlite3_io_trace = iotracePrintf;
1269 }
1270 }
1271 }else
1272#endif
1273
1274#ifndef SQLITE_OMIT_LOAD_EXTENSION
1275 if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
1276 const char *zFile, *zProc;
1277 char *zErrMsg = 0;
1278 int rc;
1279 zFile = azArg[1];
1280 zProc = nArg>=3 ? azArg[2] : 0;
1281 open_db(p);
1282 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
1283 if( rc!=SQLITE_OK ){
1284 fprintf(stderr, "%s\n", zErrMsg);
1285 sqlite3_free(zErrMsg);
1286 rc = 1;
1287 }
1288 }else
1289#endif
1290
1291 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
1292 int n2 = strlen(azArg[1]);
1293 if( strncmp(azArg[1],"line",n2)==0
1294 ||
1295 strncmp(azArg[1],"lines",n2)==0 ){
1296 p->mode = MODE_Line;
1297 }else if( strncmp(azArg[1],"column",n2)==0
1298 ||
1299 strncmp(azArg[1],"columns",n2)==0 ){
1300 p->mode = MODE_Column;
1301 }else if( strncmp(azArg[1],"list",n2)==0 ){
1302 p->mode = MODE_List;
1303 }else if( strncmp(azArg[1],"html",n2)==0 ){
1304 p->mode = MODE_Html;
1305 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
1306 p->mode = MODE_Tcl;
1307 }else if( strncmp(azArg[1],"csv",n2)==0 ){
1308 p->mode = MODE_Csv;
1309 sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
1310 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
1311 p->mode = MODE_List;
1312 sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
1313 }else if( strncmp(azArg[1],"insert",n2)==0 ){
1314 p->mode = MODE_Insert;
1315 if( nArg>=3 ){
1316 set_table_name(p, azArg[2]);
1317 }else{
1318 set_table_name(p, "table");
1319 }
1320 }else {
1321 fprintf(stderr,"mode should be one of: "
1322 "column csv html insert line list tabs tcl\n");
1323 }
1324 }else
1325
1326 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1327 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
1328 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1329 }else
1330
1331 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1332 if( p->out!=stdout ){
1333 fclose(p->out);
1334 }
1335 if( strcmp(azArg[1],"stdout")==0 ){
1336 p->out = stdout;
1337 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
1338 }else{
1339 p->out = fopen(azArg[1], "wb");
1340 if( p->out==0 ){
1341 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
1342 p->out = stdout;
1343 } else {
1344 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
1345 }
1346 }
1347 }else
1348
1349 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
1350 if( nArg >= 2) {
1351 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1352 }
1353 if( nArg >= 3) {
1354 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1355 }
1356 }else
1357
1358 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
1359 rc = 2;
1360 }else
1361
1362 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
1363 FILE *alt = fopen(azArg[1], "rb");
1364 if( alt==0 ){
1365 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
1366 }else{
1367 process_input(p, alt);
1368 fclose(alt);
1369 }
1370 }else
1371
1372 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
1373 struct callback_data data;
1374 char *zErrMsg = 0;
1375 open_db(p);
1376 memcpy(&data, p, sizeof(data));
1377 data.showHeader = 0;
1378 data.mode = MODE_Semi;
1379 if( nArg>1 ){
1380 int i;
1381 for(i=0; azArg[1][i]; i++) azArg[1][i] = tolower(azArg[1][i]);
1382 if( strcmp(azArg[1],"sqlite_master")==0 ){
1383 char *new_argv[2], *new_colv[2];
1384 new_argv[0] = "CREATE TABLE sqlite_master (\n"
1385 " type text,\n"
1386 " name text,\n"
1387 " tbl_name text,\n"
1388 " rootpage integer,\n"
1389 " sql text\n"
1390 ")";
1391 new_argv[1] = 0;
1392 new_colv[0] = "sql";
1393 new_colv[1] = 0;
1394 callback(&data, 1, new_argv, new_colv);
1395 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
1396 char *new_argv[2], *new_colv[2];
1397 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1398 " type text,\n"
1399 " name text,\n"
1400 " tbl_name text,\n"
1401 " rootpage integer,\n"
1402 " sql text\n"
1403 ")";
1404 new_argv[1] = 0;
1405 new_colv[0] = "sql";
1406 new_colv[1] = 0;
1407 callback(&data, 1, new_argv, new_colv);
1408 }else{
1409 zShellStatic = azArg[1];
1410 sqlite3_exec(p->db,
1411 "SELECT sql FROM "
1412 " (SELECT * FROM sqlite_master UNION ALL"
1413 " SELECT * FROM sqlite_temp_master) "
1414 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
1415 "ORDER BY substr(type,2,1), name",
1416 callback, &data, &zErrMsg);
1417 zShellStatic = 0;
1418 }
1419 }else{
1420 sqlite3_exec(p->db,
1421 "SELECT sql FROM "
1422 " (SELECT * FROM sqlite_master UNION ALL"
1423 " SELECT * FROM sqlite_temp_master) "
1424 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
1425 "ORDER BY substr(type,2,1), name",
1426 callback, &data, &zErrMsg
1427 );
1428 }
1429 if( zErrMsg ){
1430 fprintf(stderr,"Error: %s\n", zErrMsg);
1431 sqlite3_free(zErrMsg);
1432 }
1433 }else
1434
1435 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1436 sqlite3_snprintf(sizeof(p->separator), p->separator,
1437 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
1438 }else
1439
1440 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1441 int i;
1442 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
1443 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
1444 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
1445 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
1446 fprintf(p->out,"%9.9s: ", "nullvalue");
1447 output_c_string(p->out, p->nullvalue);
1448 fprintf(p->out, "\n");
1449 fprintf(p->out,"%9.9s: %s\n","output",
1450 strlen(p->outfile) ? p->outfile : "stdout");
1451 fprintf(p->out,"%9.9s: ", "separator");
1452 output_c_string(p->out, p->separator);
1453 fprintf(p->out, "\n");
1454 fprintf(p->out,"%9.9s: ","width");
1455 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
1456 fprintf(p->out,"%d ",p->colWidth[i]);
1457 }
1458 fprintf(p->out,"\n");
1459 }else
1460
1461 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
1462 char **azResult;
1463 int nRow, rc;
1464 char *zErrMsg;
1465 open_db(p);
1466 if( nArg==1 ){
1467 rc = sqlite3_get_table(p->db,
1468 "SELECT name FROM sqlite_master "
1469 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'"
1470 "UNION ALL "
1471 "SELECT name FROM sqlite_temp_master "
1472 "WHERE type IN ('table','view') "
1473 "ORDER BY 1",
1474 &azResult, &nRow, 0, &zErrMsg
1475 );
1476 }else{
1477 zShellStatic = azArg[1];
1478 rc = sqlite3_get_table(p->db,
1479 "SELECT name FROM sqlite_master "
1480 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
1481 "UNION ALL "
1482 "SELECT name FROM sqlite_temp_master "
1483 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
1484 "ORDER BY 1",
1485 &azResult, &nRow, 0, &zErrMsg
1486 );
1487 zShellStatic = 0;
1488 }
1489 if( zErrMsg ){
1490 fprintf(stderr,"Error: %s\n", zErrMsg);
1491 sqlite3_free(zErrMsg);
1492 }
1493 if( rc==SQLITE_OK ){
1494 int len, maxlen = 0;
1495 int i, j;
1496 int nPrintCol, nPrintRow;
1497 for(i=1; i<=nRow; i++){
1498 if( azResult[i]==0 ) continue;
1499 len = strlen(azResult[i]);
1500 if( len>maxlen ) maxlen = len;
1501 }
1502 nPrintCol = 80/(maxlen+2);
1503 if( nPrintCol<1 ) nPrintCol = 1;
1504 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1505 for(i=0; i<nPrintRow; i++){
1506 for(j=i+1; j<=nRow; j+=nPrintRow){
1507 char *zSp = j<=nPrintRow ? "" : " ";
1508 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1509 }
1510 printf("\n");
1511 }
1512 }else{
1513 rc = 1;
1514 }
1515 sqlite3_free_table(azResult);
1516 }else
1517
1518 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
1519 open_db(p);
1520 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
1521 }else
1522
1523 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1524 int j;
1525 assert( nArg<=ArraySize(azArg) );
1526 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1527 p->colWidth[j-1] = atoi(azArg[j]);
1528 }
1529 }else
1530
1531 {
1532 fprintf(stderr, "unknown command or invalid arguments: "
1533 " \"%s\". Enter \".help\" for help\n", azArg[0]);
1534 }
1535
1536 return rc;
1537}
1538
1539/*
1540** Return TRUE if a semicolon occurs anywhere in the first N characters
1541** of string z[].
1542*/
1543static int _contains_semicolon(const char *z, int N){
1544 int i;
1545 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
1546 return 0;
1547}
1548
1549/*
1550** Test to see if a line consists entirely of whitespace.
1551*/
1552static int _all_whitespace(const char *z){
1553 for(; *z; z++){
1554 if( isspace(*(unsigned char*)z) ) continue;
1555 if( *z=='/' && z[1]=='*' ){
1556 z += 2;
1557 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1558 if( *z==0 ) return 0;
1559 z++;
1560 continue;
1561 }
1562 if( *z=='-' && z[1]=='-' ){
1563 z += 2;
1564 while( *z && *z!='\n' ){ z++; }
1565 if( *z==0 ) return 1;
1566 continue;
1567 }
1568 return 0;
1569 }
1570 return 1;
1571}
1572
1573/*
1574** Return TRUE if the line typed in is an SQL command terminator other
1575** than a semi-colon. The SQL Server style "go" command is understood
1576** as is the Oracle "/".
1577*/
1578static int _is_command_terminator(const char *zLine){
1579 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
1580 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
1581 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
1582 && _all_whitespace(&zLine[2]) ){
1583 return 1; /* SQL Server */
1584 }
1585 return 0;
1586}
1587
1588/*
1589** Read input from *in and process it. If *in==0 then input
1590** is interactive - the user is typing it it. Otherwise, input
1591** is coming from a file or device. A prompt is issued and history
1592** is saved only if input is interactive. An interrupt signal will
1593** cause this routine to exit immediately, unless input is interactive.
1594**
1595** Return the number of errors.
1596*/
1597static int process_input(struct callback_data *p, FILE *in){
1598 char *zLine = 0;
1599 char *zSql = 0;
1600 int nSql = 0;
1601 int nSqlPrior = 0;
1602 char *zErrMsg;
1603 int rc;
1604 int errCnt = 0;
1605 int lineno = 0;
1606 int startline = 0;
1607
1608 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
1609 fflush(p->out);
1610 free(zLine);
1611 zLine = one_input_line(zSql, in);
1612 if( zLine==0 ){
1613 break; /* We have reached EOF */
1614 }
1615 if( seenInterrupt ){
1616 if( in!=0 ) break;
1617 seenInterrupt = 0;
1618 }
1619 lineno++;
1620 if( p->echoOn ) printf("%s\n", zLine);
1621 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
1622 if( zLine && zLine[0]=='.' && nSql==0 ){
1623 rc = do_meta_command(zLine, p);
1624 if( rc==2 ){
1625 break;
1626 }else if( rc ){
1627 errCnt++;
1628 }
1629 continue;
1630 }
1631 if( _is_command_terminator(zLine) ){
1632 memcpy(zLine,";",2);
1633 }
1634 nSqlPrior = nSql;
1635 if( zSql==0 ){
1636 int i;
1637 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
1638 if( zLine[i]!=0 ){
1639 nSql = strlen(zLine);
1640 zSql = malloc( nSql+1 );
1641 if( zSql==0 ){
1642 fprintf(stderr, "out of memory\n");
1643 exit(1);
1644 }
1645 memcpy(zSql, zLine, nSql+1);
1646 startline = lineno;
1647 }
1648 }else{
1649 int len = strlen(zLine);
1650 zSql = realloc( zSql, nSql + len + 2 );
1651 if( zSql==0 ){
1652 fprintf(stderr,"%s: out of memory!\n", Argv0);
1653 exit(1);
1654 }
1655 zSql[nSql++] = '\n';
1656 memcpy(&zSql[nSql], zLine, len+1);
1657 nSql += len;
1658 }
1659 if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
1660 && sqlite3_complete(zSql) ){
1661 p->cnt = 0;
1662 open_db(p);
1663 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
1664 if( rc || zErrMsg ){
1665 char zPrefix[100];
1666 if( in!=0 || !stdin_is_interactive ){
1667 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
1668 "SQL error near line %d:", startline);
1669 }else{
1670 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "SQL error:");
1671 }
1672 if( zErrMsg!=0 ){
1673 printf("%s %s\n", zPrefix, zErrMsg);
1674 sqlite3_free(zErrMsg);
1675 zErrMsg = 0;
1676 }else{
1677 printf("%s %s\n", zPrefix, sqlite3_errmsg(p->db));
1678 }
1679 errCnt++;
1680 }
1681 free(zSql);
1682 zSql = 0;
1683 nSql = 0;
1684 }
1685 }
1686 if( zSql ){
1687 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
1688 free(zSql);
1689 }
1690 free(zLine);
1691 return errCnt;
1692}
1693
1694/*
1695** Return a pathname which is the user's home directory. A
1696** 0 return indicates an error of some kind. Space to hold the
1697** resulting string is obtained from malloc(). The calling
1698** function should free the result.
1699*/
1700static char *find_home_dir(void){
1701 char *home_dir = NULL;
1702
1703#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) && !defined(__OS2__)
1704 struct passwd *pwent;
1705 uid_t uid = getuid();
1706 if( (pwent=getpwuid(uid)) != NULL) {
1707 home_dir = pwent->pw_dir;
1708 }
1709#endif
1710
1711#ifdef __MACOS__
1712 char home_path[_MAX_PATH+1];
1713 home_dir = getcwd(home_path, _MAX_PATH);
1714#endif
1715
1716#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
1717 if (!home_dir) {
1718 home_dir = getenv("USERPROFILE");
1719 }
1720#endif
1721
1722 if (!home_dir) {
1723 home_dir = getenv("HOME");
1724 }
1725
1726#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
1727 if (!home_dir) {
1728 char *zDrive, *zPath;
1729 int n;
1730 zDrive = getenv("HOMEDRIVE");
1731 zPath = getenv("HOMEPATH");
1732 if( zDrive && zPath ){
1733 n = strlen(zDrive) + strlen(zPath) + 1;
1734 home_dir = malloc( n );
1735 if( home_dir==0 ) return 0;
1736 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
1737 return home_dir;
1738 }
1739 home_dir = "c:\\";
1740 }
1741#endif
1742
1743 if( home_dir ){
1744 int n = strlen(home_dir) + 1;
1745 char *z = malloc( n );
1746 if( z ) memcpy(z, home_dir, n);
1747 home_dir = z;
1748 }
1749
1750 return home_dir;
1751}
1752
1753/*
1754** Read input from the file given by sqliterc_override. Or if that
1755** parameter is NULL, take input from ~/.sqliterc
1756*/
1757static void process_sqliterc(
1758 struct callback_data *p, /* Configuration data */
1759 const char *sqliterc_override /* Name of config file. NULL to use default */
1760){
1761 char *home_dir = NULL;
1762 const char *sqliterc = sqliterc_override;
1763 char *zBuf = 0;
1764 FILE *in = NULL;
1765 int nBuf;
1766
1767 if (sqliterc == NULL) {
1768 home_dir = find_home_dir();
1769 if( home_dir==0 ){
1770 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1771 return;
1772 }
1773 nBuf = strlen(home_dir) + 16;
1774 zBuf = malloc( nBuf );
1775 if( zBuf==0 ){
1776 fprintf(stderr,"%s: out of memory!\n", Argv0);
1777 exit(1);
1778 }
1779 sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
1780 free(home_dir);
1781 sqliterc = (const char*)zBuf;
1782 }
1783 in = fopen(sqliterc,"rb");
1784 if( in ){
1785 if( stdin_is_interactive ){
1786 printf("-- Loading resources from %s\n",sqliterc);
1787 }
1788 process_input(p,in);
1789 fclose(in);
1790 }
1791 free(zBuf);
1792 return;
1793}
1794
1795/*
1796** Show available command line options
1797*/
1798static const char zOptions[] =
1799 " -init filename read/process named file\n"
1800 " -echo print commands before execution\n"
1801 " -[no]header turn headers on or off\n"
1802 " -bail stop after hitting an error\n"
1803 " -interactive force interactive I/O\n"
1804 " -batch force batch I/O\n"
1805 " -column set output mode to 'column'\n"
1806 " -csv set output mode to 'csv'\n"
1807 " -html set output mode to HTML\n"
1808 " -line set output mode to 'line'\n"
1809 " -list set output mode to 'list'\n"
1810 " -separator 'x' set output field separator (|)\n"
1811 " -nullvalue 'text' set text string for NULL values\n"
1812 " -version show SQLite version\n"
1813;
1814static void usage(int showDetail){
1815 fprintf(stderr,
1816 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
1817 "FILENAME is the name of an SQLite database. A new database is created\n"
1818 "if the file does not previously exist.\n", Argv0);
1819 if( showDetail ){
1820 fprintf(stderr, "OPTIONS include:\n%s", zOptions);
1821 }else{
1822 fprintf(stderr, "Use the -help option for additional information\n");
1823 }
1824 exit(1);
1825}
1826
1827/*
1828** Initialize the state information in data
1829*/
1830static void main_init(struct callback_data *data) {
1831 memset(data, 0, sizeof(*data));
1832 data->mode = MODE_List;
1833 memcpy(data->separator,"|", 2);
1834 data->showHeader = 0;
1835 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
1836 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
1837}
1838
1839int main(int argc, char **argv){
1840 char *zErrMsg = 0;
1841 struct callback_data data;
1842 const char *zInitFile = 0;
1843 char *zFirstCmd = 0;
1844 int i;
1845 int rc = 0;
1846
1847#ifdef __MACOS__
1848 argc = ccommand(&argv);
1849#endif
1850
1851 Argv0 = argv[0];
1852 main_init(&data);
1853 stdin_is_interactive = isatty(0);
1854
1855 /* Make sure we have a valid signal handler early, before anything
1856 ** else is done.
1857 */
1858#ifdef SIGINT
1859 signal(SIGINT, interrupt_handler);
1860#endif
1861
1862 /* Do an initial pass through the command-line argument to locate
1863 ** the name of the database file, the name of the initialization file,
1864 ** and the first command to execute.
1865 */
1866 for(i=1; i<argc-1; i++){
1867 char *z;
1868 if( argv[i][0]!='-' ) break;
1869 z = argv[i];
1870 if( z[0]=='-' && z[1]=='-' ) z++;
1871 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1872 i++;
1873 }else if( strcmp(argv[i],"-init")==0 ){
1874 i++;
1875 zInitFile = argv[i];
1876 }
1877 }
1878 if( i<argc ){
1879 data.zDbFilename = argv[i++];
1880 }else{
1881#ifndef SQLITE_OMIT_MEMORYDB
1882 data.zDbFilename = ":memory:";
1883#else
1884 data.zDbFilename = 0;
1885#endif
1886 }
1887 if( i<argc ){
1888 zFirstCmd = argv[i++];
1889 }
1890 data.out = stdout;
1891
1892#ifdef SQLITE_OMIT_MEMORYDB
1893 if( data.zDbFilename==0 ){
1894 fprintf(stderr,"%s: no database filename specified\n", argv[0]);
1895 exit(1);
1896 }
1897#endif
1898
1899 /* Go ahead and open the database file if it already exists. If the
1900 ** file does not exist, delay opening it. This prevents empty database
1901 ** files from being created if a user mistypes the database name argument
1902 ** to the sqlite command-line tool.
1903 */
1904 if( access(data.zDbFilename, 0)==0 ){
1905 open_db(&data);
1906 }
1907
1908 /* Process the initialization file if there is one. If no -init option
1909 ** is given on the command line, look for a file named ~/.sqliterc and
1910 ** try to process it.
1911 */
1912 process_sqliterc(&data,zInitFile);
1913
1914 /* Make a second pass through the command-line argument and set
1915 ** options. This second pass is delayed until after the initialization
1916 ** file is processed so that the command-line arguments will override
1917 ** settings in the initialization file.
1918 */
1919 for(i=1; i<argc && argv[i][0]=='-'; i++){
1920 char *z = argv[i];
1921 if( z[1]=='-' ){ z++; }
1922 if( strcmp(z,"-init")==0 ){
1923 i++;
1924 }else if( strcmp(z,"-html")==0 ){
1925 data.mode = MODE_Html;
1926 }else if( strcmp(z,"-list")==0 ){
1927 data.mode = MODE_List;
1928 }else if( strcmp(z,"-line")==0 ){
1929 data.mode = MODE_Line;
1930 }else if( strcmp(z,"-column")==0 ){
1931 data.mode = MODE_Column;
1932 }else if( strcmp(z,"-csv")==0 ){
1933 data.mode = MODE_Csv;
1934 memcpy(data.separator,",",2);
1935 }else if( strcmp(z,"-separator")==0 ){
1936 i++;
1937 sqlite3_snprintf(sizeof(data.separator), data.separator,
1938 "%.*s",(int)sizeof(data.separator)-1,argv[i]);
1939 }else if( strcmp(z,"-nullvalue")==0 ){
1940 i++;
1941 sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
1942 "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1943 }else if( strcmp(z,"-header")==0 ){
1944 data.showHeader = 1;
1945 }else if( strcmp(z,"-noheader")==0 ){
1946 data.showHeader = 0;
1947 }else if( strcmp(z,"-echo")==0 ){
1948 data.echoOn = 1;
1949 }else if( strcmp(z,"-bail")==0 ){
1950 bail_on_error = 1;
1951 }else if( strcmp(z,"-version")==0 ){
1952 printf("%s\n", sqlite3_libversion());
1953 return 0;
1954 }else if( strcmp(z,"-interactive")==0 ){
1955 stdin_is_interactive = 1;
1956 }else if( strcmp(z,"-batch")==0 ){
1957 stdin_is_interactive = 0;
1958 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
1959 usage(1);
1960 }else{
1961 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
1962 fprintf(stderr,"Use -help for a list of options.\n");
1963 return 1;
1964 }
1965 }
1966
1967 if( zFirstCmd ){
1968 /* Run just the command that follows the database name
1969 */
1970 if( zFirstCmd[0]=='.' ){
1971 do_meta_command(zFirstCmd, &data);
1972 exit(0);
1973 }else{
1974 int rc;
1975 open_db(&data);
1976 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
1977 if( rc!=0 && zErrMsg!=0 ){
1978 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1979 exit(1);
1980 }
1981 }
1982 }else{
1983 /* Run commands received from standard input
1984 */
1985 if( stdin_is_interactive ){
1986 char *zHome;
1987 char *zHistory = 0;
1988 int nHistory;
1989 printf(
1990 "SQLite version %s\n"
1991 "Enter \".help\" for instructions\n",
1992 sqlite3_libversion()
1993 );
1994 zHome = find_home_dir();
1995 if( zHome && (zHistory = malloc(nHistory = strlen(zHome)+20))!=0 ){
1996 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
1997 }
1998#if defined(HAVE_READLINE) && HAVE_READLINE==1
1999 if( zHistory ) read_history(zHistory);
2000#endif
2001 rc = process_input(&data, 0);
2002 if( zHistory ){
2003 stifle_history(100);
2004 write_history(zHistory);
2005 free(zHistory);
2006 }
2007 free(zHome);
2008 }else{
2009 rc = process_input(&data, stdin);
2010 }
2011 }
2012 set_table_name(&data, 0);
2013 if( db ){
2014 if( sqlite3_close(db)!=SQLITE_OK ){
2015 fprintf(stderr,"error closing database: %s\n", sqlite3_errmsg(db));
2016 }
2017 }
2018 return rc;
2019}
diff --git a/libraries/sqlite/win32/sqlite3.h b/libraries/sqlite/win32/sqlite3.h
new file mode 100755
index 0000000..562ae80
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite3.h
@@ -0,0 +1,3552 @@
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.
10**
11*************************************************************************
12** This header file defines the interface that the SQLite library
13** presents to client programs. If a C-function, structure, datatype,
14** or constant definition does not appear in this file, then it is
15** not a published API of SQLite, is subject to change without
16** notice, and should not be referenced by programs that use SQLite.
17**
18** Some of the definitions that are in this file are marked as
19** "experimental". Experimental interfaces are normally new
20** features recently added to SQLite. We do not anticipate changes
21** to experimental interfaces but reserve to make minor changes if
22** experience from use "in the wild" suggest such changes are prudent.
23**
24** The official C-language API documentation for SQLite is derived
25** from comments in this file. This file is the authoritative source
26** on how SQLite interfaces are suppose to operate.
27**
28** The name of this file under configuration management is "sqlite.h.in".
29** The makefile makes some minor changes to this file (such as inserting
30** the version number) and changes its name to "sqlite3.h" as
31** part of the build process.
32**
33** @(#) $Id: sqlite.h.in,v 1.266 2007/10/03 20:15:28 drh Exp $
34*/
35#ifndef _SQLITE3_H_
36#define _SQLITE3_H_
37#include <stdarg.h> /* Needed for the definition of va_list */
38
39/*
40** Make sure we can call this stuff from C++.
41*/
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46
47/*
48** Add the ability to override 'extern'
49*/
50#ifndef SQLITE_EXTERN
51# define SQLITE_EXTERN extern
52#endif
53
54/*
55** Make sure these symbols where not defined by some previous header
56** file.
57*/
58#ifdef SQLITE_VERSION
59# undef SQLITE_VERSION
60#endif
61#ifdef SQLITE_VERSION_NUMBER
62# undef SQLITE_VERSION_NUMBER
63#endif
64
65/*
66** CAPI3REF: Compile-Time Library Version Numbers
67**
68** The version of the SQLite library is contained in the sqlite3.h
69** header file in a #define named SQLITE_VERSION. The SQLITE_VERSION
70** macro resolves to a string constant.
71**
72** The format of the version string is "X.Y.Z", where
73** X is the major version number, Y is the minor version number and Z
74** is the release number. The X.Y.Z might be followed by "alpha" or "beta".
75** For example "3.1.1beta".
76**
77** The X value is always 3 in SQLite. The X value only changes when
78** backwards compatibility is broken and we intend to never break
79** backwards compatibility. The Y value only changes when
80** there are major feature enhancements that are forwards compatible
81** but not backwards compatible. The Z value is incremented with
82** each release but resets back to 0 when Y is incremented.
83**
84** The SQLITE_VERSION_NUMBER is an integer with the value
85** (X*1000000 + Y*1000 + Z). For example, for version "3.1.1beta",
86** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using
87** version 3.1.1 or greater at compile time, programs may use the test
88** (SQLITE_VERSION_NUMBER>=3001001).
89**
90** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
91*/
92#define SQLITE_VERSION "3.5.1"
93#define SQLITE_VERSION_NUMBER 3005001
94
95/*
96** CAPI3REF: Run-Time Library Version Numbers
97**
98** These routines return values equivalent to the header constants
99** [SQLITE_VERSION] and [SQLITE_VERSION_NUMBER]. The values returned
100** by this routines should only be different from the header values
101** if you compile your program using an sqlite3.h header from a
102** different version of SQLite that the version of the library you
103** link against.
104**
105** The sqlite3_version[] string constant contains the text of the
106** [SQLITE_VERSION] string. The sqlite3_libversion() function returns
107** a poiner to the sqlite3_version[] string constant. The function
108** is provided for DLL users who can only access functions and not
109** constants within the DLL.
110*/
111SQLITE_EXTERN const char sqlite3_version[];
112const char *sqlite3_libversion(void);
113int sqlite3_libversion_number(void);
114
115/*
116** CAPI3REF: Test To See If The Library Is Threadsafe
117**
118** This routine returns TRUE (nonzero) if SQLite was compiled with
119** all of its mutexes enabled and is thus threadsafe. It returns
120** zero if the particular build is for single-threaded operation
121** only.
122**
123** Really all this routine does is return true if SQLite was compiled
124** with the -DSQLITE_THREADSAFE=1 option and false if
125** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an
126** application-defined mutex subsystem, malloc subsystem, collating
127** sequence, VFS, SQL function, progress callback, commit hook,
128** extension, or other accessories and these add-ons are not
129** threadsafe, then clearly the combination will not be threadsafe
130** either. Hence, this routine never reports that the library
131** is guaranteed to be threadsafe, only when it is guaranteed not
132** to be.
133**
134** This is an experimental API and may go away or change in future
135** releases.
136*/
137int sqlite3_threadsafe(void);
138
139/*
140** CAPI3REF: Database Connection Handle
141**
142** Each open SQLite database is represented by pointer to an instance of the
143** opaque structure named "sqlite3". It is useful to think of an sqlite3
144** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
145** [sqlite3_open_v2()] interfaces are its constructors
146** and [sqlite3_close()] is its destructor. There are many other interfaces
147** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and
148** [sqlite3_busy_timeout()] to name but three) that are methods on this
149** object.
150*/
151typedef struct sqlite3 sqlite3;
152
153
154/*
155** CAPI3REF: 64-Bit Integer Types
156**
157** Some compilers do not support the "long long" datatype. So we have
158** to do compiler-specific typedefs for 64-bit signed and unsigned integers.
159**
160** Many SQLite interface functions require a 64-bit integer arguments.
161** Those interfaces are declared using this typedef.
162*/
163#ifdef SQLITE_INT64_TYPE
164 typedef SQLITE_INT64_TYPE sqlite_int64;
165 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
166#elif defined(_MSC_VER) || defined(__BORLANDC__)
167 typedef __int64 sqlite_int64;
168 typedef unsigned __int64 sqlite_uint64;
169#else
170 typedef long long int sqlite_int64;
171 typedef unsigned long long int sqlite_uint64;
172#endif
173typedef sqlite_int64 sqlite3_int64;
174typedef sqlite_uint64 sqlite3_uint64;
175
176/*
177** If compiling for a processor that lacks floating point support,
178** substitute integer for floating-point
179*/
180#ifdef SQLITE_OMIT_FLOATING_POINT
181# define double sqlite3_int64
182#endif
183
184/*
185** CAPI3REF: Closing A Database Connection
186**
187** Call this function with a pointer to a structure that was previously
188** returned from [sqlite3_open()], [sqlite3_open16()], or
189** [sqlite3_open_v2()] and the corresponding database will by
190** closed.
191**
192** All SQL statements prepared using [sqlite3_prepare_v2()] or
193** [sqlite3_prepare16_v2()] must be destroyed using [sqlite3_finalize()]
194** before this routine is called. Otherwise, SQLITE_BUSY is returned and the
195** database connection remains open.
196**
197** Passing this routine a database connection that has already been
198** closed results in undefined behavior. If other interfaces that
199** reference the same database connection are pending (either in the
200** same thread or in different threads) when this routine is called,
201** then the behavior is undefined and is almost certainly undesirable.
202*/
203int sqlite3_close(sqlite3 *);
204
205/*
206** The type for a callback function.
207** This is legacy and deprecated. It is included for historical
208** compatibility and is not documented.
209*/
210typedef int (*sqlite3_callback)(void*,int,char**, char**);
211
212/*
213** CAPI3REF: One-Step Query Execution Interface
214**
215** This interface is used to do a one-time evaluatation of zero
216** or more SQL statements. UTF-8 text of the SQL statements to
217** be evaluted is passed in as the second parameter. The statements
218** are prepared one by one using [sqlite3_prepare()], evaluated
219** using [sqlite3_step()], then destroyed using [sqlite3_finalize()].
220**
221** If one or more of the SQL statements are queries, then
222** the callback function specified by the 3rd parameter is
223** invoked once for each row of the query result. This callback
224** should normally return 0. If the callback returns a non-zero
225** value then the query is aborted, all subsequent SQL statements
226** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT].
227**
228** The 4th parameter to this interface is an arbitrary pointer that is
229** passed through to the callback function as its first parameter.
230**
231** The 2nd parameter to the callback function is the number of
232** columns in the query result. The 3rd parameter to the callback
233** is an array of strings holding the values for each column
234** as extracted using [sqlite3_column_text()].
235** The 4th parameter to the callback is an array of strings
236** obtained using [sqlite3_column_name()] and holding
237** the names of each column.
238**
239** The callback function may be NULL, even for queries. A NULL
240** callback is not an error. It just means that no callback
241** will be invoked.
242**
243** If an error occurs while parsing or evaluating the SQL (but
244** not while executing the callback) then an appropriate error
245** message is written into memory obtained from [sqlite3_malloc()] and
246** *errmsg is made to point to that message. The calling function
247** is responsible for freeing the memory using [sqlite3_free()].
248** If errmsg==NULL, then no error message is ever written.
249**
250** The return value is is SQLITE_OK if there are no errors and
251** some other [SQLITE_OK | return code] if there is an error.
252** The particular return value depends on the type of error.
253**
254*/
255int sqlite3_exec(
256 sqlite3*, /* An open database */
257 const char *sql, /* SQL to be evaluted */
258 int (*callback)(void*,int,char**,char**), /* Callback function */
259 void *, /* 1st argument to callback */
260 char **errmsg /* Error msg written here */
261);
262
263/*
264** CAPI3REF: Result Codes
265** KEYWORDS: SQLITE_OK
266**
267** Many SQLite functions return an integer result code from the set shown
268** above in order to indicates success or failure.
269**
270** The result codes above are the only ones returned by SQLite in its
271** default configuration. However, the [sqlite3_extended_result_codes()]
272** API can be used to set a database connectoin to return more detailed
273** result codes.
274**
275** See also: [SQLITE_IOERR_READ | extended result codes]
276**
277*/
278#define SQLITE_OK 0 /* Successful result */
279/* beginning-of-error-codes */
280#define SQLITE_ERROR 1 /* SQL error or missing database */
281#define SQLITE_INTERNAL 2 /* NOT USED. Internal logic error in SQLite */
282#define SQLITE_PERM 3 /* Access permission denied */
283#define SQLITE_ABORT 4 /* Callback routine requested an abort */
284#define SQLITE_BUSY 5 /* The database file is locked */
285#define SQLITE_LOCKED 6 /* A table in the database is locked */
286#define SQLITE_NOMEM 7 /* A malloc() failed */
287#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
288#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
289#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
290#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
291#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
292#define SQLITE_FULL 13 /* Insertion failed because database is full */
293#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
294#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
295#define SQLITE_EMPTY 16 /* Database is empty */
296#define SQLITE_SCHEMA 17 /* The database schema changed */
297#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
298#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
299#define SQLITE_MISMATCH 20 /* Data type mismatch */
300#define SQLITE_MISUSE 21 /* Library used incorrectly */
301#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
302#define SQLITE_AUTH 23 /* Authorization denied */
303#define SQLITE_FORMAT 24 /* Auxiliary database format error */
304#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
305#define SQLITE_NOTADB 26 /* File opened that is not a database file */
306#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
307#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
308/* end-of-error-codes */
309
310/*
311** CAPI3REF: Extended Result Codes
312**
313** In its default configuration, SQLite API routines return one of 26 integer
314** result codes described at result-codes. However, experience has shown that
315** many of these result codes are too course-grained. They do not provide as
316** much information about problems as users might like. In an effort to
317** address this, newer versions of SQLite (version 3.3.8 and later) include
318** support for additional result codes that provide more detailed information
319** about errors. The extended result codes are enabled (or disabled) for
320** each database
321** connection using the [sqlite3_extended_result_codes()] API.
322**
323** Some of the available extended result codes are listed above.
324** We expect the number of extended result codes will be expand
325** over time. Software that uses extended result codes should expect
326** to see new result codes in future releases of SQLite.
327**
328** The symbolic name for an extended result code always contains a related
329** primary result code as a prefix. Primary result codes contain a single
330** "_" character. Extended result codes contain two or more "_" characters.
331** The numeric value of an extended result code can be converted to its
332** corresponding primary result code by masking off the lower 8 bytes.
333**
334** The SQLITE_OK result code will never be extended. It will always
335** be exactly zero.
336*/
337#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
338#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
339#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
340#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
341#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
342#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
343#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
344#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
345#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
346#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
347#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
348#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
349
350/*
351** CAPI3REF: Flags For File Open Operations
352**
353** Combination of the following bit values are used as the
354** third argument to the [sqlite3_open_v2()] interface and
355** as fourth argument to the xOpen method of the
356** [sqlite3_vfs] object.
357**
358*/
359#define SQLITE_OPEN_READONLY 0x00000001
360#define SQLITE_OPEN_READWRITE 0x00000002
361#define SQLITE_OPEN_CREATE 0x00000004
362#define SQLITE_OPEN_DELETEONCLOSE 0x00000008
363#define SQLITE_OPEN_EXCLUSIVE 0x00000010
364#define SQLITE_OPEN_MAIN_DB 0x00000100
365#define SQLITE_OPEN_TEMP_DB 0x00000200
366#define SQLITE_OPEN_TRANSIENT_DB 0x00000400
367#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800
368#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000
369#define SQLITE_OPEN_SUBJOURNAL 0x00002000
370#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000
371
372/*
373** CAPI3REF: Device Characteristics
374**
375** The xDeviceCapabilities method of the [sqlite3_io_methods]
376** object returns an integer which is a vector of the following
377** bit values expressing I/O characteristics of the mass storage
378** device that holds the file that the [sqlite3_io_methods]
379** refers to.
380**
381** The SQLITE_IOCAP_ATOMIC property means that all writes of
382** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
383** mean that writes of blocks that are nnn bytes in size and
384** are aligned to an address which is an integer multiple of
385** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
386** that when data is appended to a file, the data is appended
387** first then the size of the file is extended, never the other
388** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
389** information is written to disk in the same order as calls
390** to xWrite().
391*/
392#define SQLITE_IOCAP_ATOMIC 0x00000001
393#define SQLITE_IOCAP_ATOMIC512 0x00000002
394#define SQLITE_IOCAP_ATOMIC1K 0x00000004
395#define SQLITE_IOCAP_ATOMIC2K 0x00000008
396#define SQLITE_IOCAP_ATOMIC4K 0x00000010
397#define SQLITE_IOCAP_ATOMIC8K 0x00000020
398#define SQLITE_IOCAP_ATOMIC16K 0x00000040
399#define SQLITE_IOCAP_ATOMIC32K 0x00000080
400#define SQLITE_IOCAP_ATOMIC64K 0x00000100
401#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
402#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
403
404/*
405** CAPI3REF: File Locking Levels
406**
407** SQLite uses one of the following integer values as the second
408** argument to calls it makes to the xLock() and xUnlock() methods
409** of an [sqlite3_io_methods] object.
410*/
411#define SQLITE_LOCK_NONE 0
412#define SQLITE_LOCK_SHARED 1
413#define SQLITE_LOCK_RESERVED 2
414#define SQLITE_LOCK_PENDING 3
415#define SQLITE_LOCK_EXCLUSIVE 4
416
417/*
418** CAPI3REF: Synchronization Type Flags
419**
420** When SQLite invokes the xSync() method of an [sqlite3_io_methods]
421** object it uses a combination of the following integer values as
422** the second argument.
423**
424** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
425** sync operation only needs to flush data to mass storage. Inode
426** information need not be flushed. The SQLITE_SYNC_NORMAL means
427** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
428** to use Mac OS-X style fullsync instead of fsync().
429*/
430#define SQLITE_SYNC_NORMAL 0x00002
431#define SQLITE_SYNC_FULL 0x00003
432#define SQLITE_SYNC_DATAONLY 0x00010
433
434
435/*
436** CAPI3REF: OS Interface Open File Handle
437**
438** An [sqlite3_file] object represents an open file in the OS
439** interface layer. Individual OS interface implementations will
440** want to subclass this object by appending additional fields
441** for their own use. The pMethods entry is a pointer to an
442** [sqlite3_io_methods] object that defines methods for performing
443** I/O operations on the open file.
444*/
445typedef struct sqlite3_file sqlite3_file;
446struct sqlite3_file {
447 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
448};
449
450/*
451** CAPI3REF: OS Interface File Virtual Methods Object
452**
453** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to
454** an instance of the this object. This object defines the
455** methods used to perform various operations against the open file.
456**
457** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
458** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
459* The second choice is an
460** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to
461** indicate that only the data of the file and not its inode needs to be
462** synced.
463**
464** The integer values to xLock() and xUnlock() are one of
465** <ul>
466** <li> [SQLITE_LOCK_NONE],
467** <li> [SQLITE_LOCK_SHARED],
468** <li> [SQLITE_LOCK_RESERVED],
469** <li> [SQLITE_LOCK_PENDING], or
470** <li> [SQLITE_LOCK_EXCLUSIVE].
471** </ul>
472** xLock() increases the lock. xUnlock() decreases the lock.
473** The xCheckReservedLock() method looks
474** to see if any database connection, either in this
475** process or in some other process, is holding an RESERVED,
476** PENDING, or EXCLUSIVE lock on the file. It returns true
477** if such a lock exists and false if not.
478**
479** The xFileControl() method is a generic interface that allows custom
480** VFS implementations to directly control an open file using the
481** [sqlite3_file_control()] interface. The second "op" argument
482** is an integer opcode. The third
483** argument is a generic pointer which is intended to be a pointer
484** to a structure that may contain arguments or space in which to
485** write return values. Potential uses for xFileControl() might be
486** functions to enable blocking locks with timeouts, to change the
487** locking strategy (for example to use dot-file locks), to inquire
488** about the status of a lock, or to break stale locks. The SQLite
489** core reserves opcodes less than 100 for its own use.
490** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
491** Applications that define a custom xFileControl method should use opcodes
492** greater than 100 to avoid conflicts.
493**
494** The xSectorSize() method returns the sector size of the
495** device that underlies the file. The sector size is the
496** minimum write that can be performed without disturbing
497** other bytes in the file. The xDeviceCharacteristics()
498** method returns a bit vector describing behaviors of the
499** underlying device:
500**
501** <ul>
502** <li> [SQLITE_IOCAP_ATOMIC]
503** <li> [SQLITE_IOCAP_ATOMIC512]
504** <li> [SQLITE_IOCAP_ATOMIC1K]
505** <li> [SQLITE_IOCAP_ATOMIC2K]
506** <li> [SQLITE_IOCAP_ATOMIC4K]
507** <li> [SQLITE_IOCAP_ATOMIC8K]
508** <li> [SQLITE_IOCAP_ATOMIC16K]
509** <li> [SQLITE_IOCAP_ATOMIC32K]
510** <li> [SQLITE_IOCAP_ATOMIC64K]
511** <li> [SQLITE_IOCAP_SAFE_APPEND]
512** <li> [SQLITE_IOCAP_SEQUENTIAL]
513** </ul>
514**
515** The SQLITE_IOCAP_ATOMIC property means that all writes of
516** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
517** mean that writes of blocks that are nnn bytes in size and
518** are aligned to an address which is an integer multiple of
519** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
520** that when data is appended to a file, the data is appended
521** first then the size of the file is extended, never the other
522** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
523** information is written to disk in the same order as calls
524** to xWrite().
525*/
526typedef struct sqlite3_io_methods sqlite3_io_methods;
527struct sqlite3_io_methods {
528 int iVersion;
529 int (*xClose)(sqlite3_file*);
530 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
531 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
532 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
533 int (*xSync)(sqlite3_file*, int flags);
534 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
535 int (*xLock)(sqlite3_file*, int);
536 int (*xUnlock)(sqlite3_file*, int);
537 int (*xCheckReservedLock)(sqlite3_file*);
538 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
539 int (*xSectorSize)(sqlite3_file*);
540 int (*xDeviceCharacteristics)(sqlite3_file*);
541 /* Additional methods may be added in future releases */
542};
543
544/*
545** CAPI3REF: Standard File Control Opcodes
546**
547** These integer constants are opcodes for the xFileControl method
548** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()]
549** interface.
550**
551** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
552** opcode cases the xFileControl method to write the current state of
553** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
554** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
555** into an integer that the pArg argument points to. This capability
556** is used during testing and only needs to be supported when SQLITE_TEST
557** is defined.
558*/
559#define SQLITE_FCNTL_LOCKSTATE 1
560
561/*
562** CAPI3REF: Mutex Handle
563**
564** The mutex module within SQLite defines [sqlite3_mutex] to be an
565** abstract type for a mutex object. The SQLite core never looks
566** at the internal representation of an [sqlite3_mutex]. It only
567** deals with pointers to the [sqlite3_mutex] object.
568**
569** Mutexes are created using [sqlite3_mutex_alloc()].
570*/
571typedef struct sqlite3_mutex sqlite3_mutex;
572
573/*
574** CAPI3REF: OS Interface Object
575**
576** An instance of this object defines the interface between the
577** SQLite core and the underlying operating system. The "vfs"
578** in the name of the object stands for "virtual file system".
579**
580** The iVersion field is initially 1 but may be larger for future
581** versions of SQLite. Additional fields may be appended to this
582** object when the iVersion value is increased.
583**
584** The szOsFile field is the size of the subclassed [sqlite3_file]
585** structure used by this VFS. mxPathname is the maximum length of
586** a pathname in this VFS.
587**
588** Registered vfs modules are kept on a linked list formed by
589** the pNext pointer. The [sqlite3_vfs_register()]
590** and [sqlite3_vfs_unregister()] interfaces manage this list
591** in a thread-safe way. The [sqlite3_vfs_find()] interface
592** searches the list.
593**
594** The pNext field is the only fields in the sqlite3_vfs
595** structure that SQLite will ever modify. SQLite will only access
596** or modify this field while holding a particular static mutex.
597** The application should never modify anything within the sqlite3_vfs
598** object once the object has been registered.
599**
600** The zName field holds the name of the VFS module. The name must
601** be unique across all VFS modules.
602**
603** SQLite will guarantee that the zFilename string passed to
604** xOpen() is a full pathname as generated by xFullPathname() and
605** that the string will be valid and unchanged until xClose() is
606** called. So the [sqlite3_file] can store a pointer to the
607** filename if it needs to remember the filename for some reason.
608**
609** The flags argument to xOpen() is a copy of the flags argument
610** to [sqlite3_open_v2()]. If [sqlite3_open()] or [sqlite3_open16()]
611** is used, then flags is [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
612** If xOpen() opens a file read-only then it sets *pOutFlags to
613** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be
614** set.
615**
616** SQLite will also add one of the following flags to the xOpen()
617** call, depending on the object being opened:
618**
619** <ul>
620** <li> [SQLITE_OPEN_MAIN_DB]
621** <li> [SQLITE_OPEN_MAIN_JOURNAL]
622** <li> [SQLITE_OPEN_TEMP_DB]
623** <li> [SQLITE_OPEN_TEMP_JOURNAL]
624** <li> [SQLITE_OPEN_TRANSIENT_DB]
625** <li> [SQLITE_OPEN_SUBJOURNAL]
626** <li> [SQLITE_OPEN_MASTER_JOURNAL]
627** </ul>
628**
629** The file I/O implementation can use the object type flags to
630** changes the way it deals with files. For example, an application
631** that does not care about crash recovery or rollback, might make
632** the open of a journal file a no-op. Writes to this journal are
633** also a no-op. Any attempt to read the journal return SQLITE_IOERR.
634** Or the implementation might recognize the a database file will
635** be doing page-aligned sector reads and writes in a random order
636** and set up its I/O subsystem accordingly.
637**
638** SQLite might also add one of the following flags to the xOpen
639** method:
640**
641** <ul>
642** <li> [SQLITE_OPEN_DELETEONCLOSE]
643** <li> [SQLITE_OPEN_EXCLUSIVE]
644** </ul>
645**
646** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
647** deleted when it is closed. This will always be set for TEMP
648** databases and journals and for subjournals. The
649** [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened
650** for exclusive access. This flag is set for all files except
651** for the main database file.
652**
653** Space to hold the [sqlite3_file] structure passed as the third
654** argument to xOpen is allocated by caller (the SQLite core).
655** szOsFile bytes are allocated for this object. The xOpen method
656** fills in the allocated space.
657**
658** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
659** to test for the existance of a file,
660** or [SQLITE_ACCESS_READWRITE] to test to see
661** if a file is readable and writable, or [SQLITE_ACCESS_READ]
662** to test to see if a file is at least readable. The file can be a
663** directory.
664**
665** SQLite will always allocate at least mxPathname+1 byte for
666** the output buffers for xGetTempname and xFullPathname. The exact
667** size of the output buffer is also passed as a parameter to both
668** methods. If the output buffer is not large enough, SQLITE_CANTOPEN
669** should be returned. As this is handled as a fatal error by SQLite,
670** vfs implementations should endevour to prevent this by setting
671** mxPathname to a sufficiently large value.
672**
673** The xRandomness(), xSleep(), and xCurrentTime() interfaces
674** are not strictly a part of the filesystem, but they are
675** included in the VFS structure for completeness.
676** The xRandomness() function attempts to return nBytes bytes
677** of good-quality randomness into zOut. The return value is
678** the actual number of bytes of randomness obtained. The
679** xSleep() method cause the calling thread to sleep for at
680** least the number of microseconds given. The xCurrentTime()
681** method returns a Julian Day Number for the current date and
682** time.
683*/
684typedef struct sqlite3_vfs sqlite3_vfs;
685struct sqlite3_vfs {
686 int iVersion; /* Structure version number */
687 int szOsFile; /* Size of subclassed sqlite3_file */
688 int mxPathname; /* Maximum file pathname length */
689 sqlite3_vfs *pNext; /* Next registered VFS */
690 const char *zName; /* Name of this virtual file system */
691 void *pAppData; /* Pointer to application-specific data */
692 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
693 int flags, int *pOutFlags);
694 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
695 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags);
696 int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut);
697 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
698 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
699 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
700 void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol);
701 void (*xDlClose)(sqlite3_vfs*, void*);
702 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
703 int (*xSleep)(sqlite3_vfs*, int microseconds);
704 int (*xCurrentTime)(sqlite3_vfs*, double*);
705 /* New fields may be appended in figure versions. The iVersion
706 ** value will increment whenever this happens. */
707};
708
709/*
710** CAPI3REF: Flags for the xAccess VFS method
711**
712** These integer constants can be used as the third parameter to
713** the xAccess method of an [sqlite3_vfs] object. They determine
714** the kind of what kind of permissions the xAccess method is
715** looking for. With SQLITE_ACCESS_EXISTS, the xAccess method
716** simply checks to see if the file exists. With SQLITE_ACCESS_READWRITE,
717** the xAccess method checks to see if the file is both readable
718** and writable. With SQLITE_ACCESS_READ the xAccess method
719** checks to see if the file is readable.
720*/
721#define SQLITE_ACCESS_EXISTS 0
722#define SQLITE_ACCESS_READWRITE 1
723#define SQLITE_ACCESS_READ 2
724
725/*
726** CAPI3REF: Enable Or Disable Extended Result Codes
727**
728** This routine enables or disables the
729** [SQLITE_IOERR_READ | extended result codes] feature.
730** By default, SQLite API routines return one of only 26 integer
731** [SQLITE_OK | result codes]. When extended result codes
732** are enabled by this routine, the repetoire of result codes can be
733** much larger and can (hopefully) provide more detailed information
734** about the cause of an error.
735**
736** The second argument is a boolean value that turns extended result
737** codes on and off. Extended result codes are off by default for
738** backwards compatibility with older versions of SQLite.
739*/
740int sqlite3_extended_result_codes(sqlite3*, int onoff);
741
742/*
743** CAPI3REF: Last Insert Rowid
744**
745** Each entry in an SQLite table has a unique 64-bit signed integer key
746** called the "rowid". The rowid is always available as an undeclared
747** column named ROWID, OID, or _ROWID_. If the table has a column of
748** type INTEGER PRIMARY KEY then that column is another an alias for the
749** rowid.
750**
751** This routine returns the rowid of the most recent INSERT into
752** the database from the database connection given in the first
753** argument. If no inserts have ever occurred on this database
754** connection, zero is returned.
755**
756** If an INSERT occurs within a trigger, then the rowid of the
757** inserted row is returned by this routine as long as the trigger
758** is running. But once the trigger terminates, the value returned
759** by this routine reverts to the last value inserted before the
760** trigger fired.
761**
762** If another thread does a new insert on the same database connection
763** while this routine is running and thus changes the last insert rowid,
764** then the return value of this routine is undefined.
765*/
766sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
767
768/*
769** CAPI3REF: Count The Number Of Rows Modified
770**
771** This function returns the number of database rows that were changed
772** (or inserted or deleted) by the most recent SQL statement. Only
773** changes that are directly specified by the INSERT, UPDATE, or
774** DELETE statement are counted. Auxiliary changes caused by
775** triggers are not counted. Use the [sqlite3_total_changes()] function
776** to find the total number of changes including changes caused by triggers.
777**
778** Within the body of a trigger, the sqlite3_changes() interface can be
779** called to find the number of
780** changes in the most recently completed INSERT, UPDATE, or DELETE
781** statement within the body of the trigger.
782**
783** All changes are counted, even if they were later undone by a
784** ROLLBACK or ABORT. Except, changes associated with creating and
785** dropping tables are not counted.
786**
787** If a callback invokes [sqlite3_exec()] or [sqlite3_step()] recursively,
788** then the changes in the inner, recursive call are counted together
789** with the changes in the outer call.
790**
791** SQLite implements the command "DELETE FROM table" without a WHERE clause
792** by dropping and recreating the table. (This is much faster than going
793** through and deleting individual elements from the table.) Because of
794** this optimization, the change count for "DELETE FROM table" will be
795** zero regardless of the number of elements that were originally in the
796** table. To get an accurate count of the number of rows deleted, use
797** "DELETE FROM table WHERE 1" instead.
798**
799** If another thread makes changes on the same database connection
800** while this routine is running then the return value of this routine
801** is undefined.
802*/
803int sqlite3_changes(sqlite3*);
804
805/*
806** CAPI3REF: Total Number Of Rows Modified
807***
808** This function returns the number of database rows that have been
809** modified by INSERT, UPDATE or DELETE statements since the database handle
810** was opened. This includes UPDATE, INSERT and DELETE statements executed
811** as part of trigger programs. All changes are counted as soon as the
812** statement that makes them is completed (when the statement handle is
813** passed to [sqlite3_reset()] or [sqlite3_finalize()]).
814**
815** See also the [sqlite3_change()] interface.
816**
817** SQLite implements the command "DELETE FROM table" without a WHERE clause
818** by dropping and recreating the table. (This is much faster than going
819** through and deleting individual elements form the table.) Because of
820** this optimization, the change count for "DELETE FROM table" will be
821** zero regardless of the number of elements that were originally in the
822** table. To get an accurate count of the number of rows deleted, use
823** "DELETE FROM table WHERE 1" instead.
824**
825** If another thread makes changes on the same database connection
826** while this routine is running then the return value of this routine
827** is undefined.
828*/
829int sqlite3_total_changes(sqlite3*);
830
831/*
832** CAPI3REF: Interrupt A Long-Running Query
833**
834** This function causes any pending database operation to abort and
835** return at its earliest opportunity. This routine is typically
836** called in response to a user action such as pressing "Cancel"
837** or Ctrl-C where the user wants a long query operation to halt
838** immediately.
839**
840** It is safe to call this routine from a thread different from the
841** thread that is currently running the database operation. But it
842** is not safe to call this routine with a database connection that
843** is closed or might close before sqlite3_interrupt() returns.
844**
845** The SQL operation that is interrupted will return [SQLITE_INTERRUPT].
846** If an interrupted operation was an update that is inside an
847** explicit transaction, then the entire transaction will be rolled
848** back automatically.
849*/
850void sqlite3_interrupt(sqlite3*);
851
852/*
853** CAPI3REF: Determine If An SQL Statement Is Complete
854**
855** These functions return true if the given input string comprises
856** one or more complete SQL statements. For the sqlite3_complete() call,
857** the parameter must be a nul-terminated UTF-8 string. For
858** sqlite3_complete16(), a nul-terminated machine byte order UTF-16 string
859** is required.
860**
861** These routines are useful for command-line input to determine if the
862** currently entered text forms one or more complete SQL statements or
863** if additional input is needed before sending the statements into
864** SQLite for parsing. The algorithm is simple. If the
865** last token other than spaces and comments is a semicolon, then return
866** true. Actually, the algorithm is a little more complicated than that
867** in order to deal with triggers, but the basic idea is the same: the
868** statement is not complete unless it ends in a semicolon.
869*/
870int sqlite3_complete(const char *sql);
871int sqlite3_complete16(const void *sql);
872
873/*
874** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
875**
876** This routine identifies a callback function that might be invoked
877** whenever an attempt is made to open a database table
878** that another thread or process has locked.
879** If the busy callback is NULL, then [SQLITE_BUSY]
880** (or sometimes [SQLITE_IOERR_BLOCKED])
881** is returned immediately upon encountering the lock.
882** If the busy callback is not NULL, then the
883** callback will be invoked with two arguments. The
884** first argument to the handler is a copy of the void* pointer which
885** is the third argument to this routine. The second argument to
886** the handler is the number of times that the busy handler has
887** been invoked for this locking event. If the
888** busy callback returns 0, then no additional attempts are made to
889** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
890** If the callback returns non-zero, then another attempt is made to open the
891** database for reading and the cycle repeats.
892**
893** The presence of a busy handler does not guarantee that
894** it will be invoked when there is lock contention.
895** If SQLite determines that invoking the busy handler could result in
896** a deadlock, it will return [SQLITE_BUSY] instead.
897** Consider a scenario where one process is holding a read lock that
898** it is trying to promote to a reserved lock and
899** a second process is holding a reserved lock that it is trying
900** to promote to an exclusive lock. The first process cannot proceed
901** because it is blocked by the second and the second process cannot
902** proceed because it is blocked by the first. If both processes
903** invoke the busy handlers, neither will make any progress. Therefore,
904** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
905** will induce the first process to release its read lock and allow
906** the second process to proceed.
907**
908** The default busy callback is NULL.
909**
910** The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] when
911** SQLite is in the middle of a large transaction where all the
912** changes will not fit into the in-memory cache. SQLite will
913** already hold a RESERVED lock on the database file, but it needs
914** to promote this lock to EXCLUSIVE so that it can spill cache
915** pages into the database file without harm to concurrent
916** readers. If it is unable to promote the lock, then the in-memory
917** cache will be left in an inconsistent state and so the error
918** code is promoted from the relatively benign [SQLITE_BUSY] to
919** the more severe [SQLITE_IOERR_BLOCKED]. This error code promotion
920** forces an automatic rollback of the changes. See the
921** <a href="http://www.sqlite.org/cvstrac/wiki?p=CorruptionFollowingBusyError">
922** CorruptionFollowingBusyError</a> wiki page for a discussion of why
923** this is important.
924**
925** Sqlite is re-entrant, so the busy handler may start a new query.
926** (It is not clear why anyone would every want to do this, but it
927** is allowed, in theory.) But the busy handler may not close the
928** database. Closing the database from a busy handler will delete
929** data structures out from under the executing query and will
930** probably result in a segmentation fault or other runtime error.
931**
932** There can only be a single busy handler defined for each database
933** connection. Setting a new busy handler clears any previous one.
934** Note that calling [sqlite3_busy_timeout()] will also set or clear
935** the busy handler.
936**
937** When operating in [sqlite3_enable_shared_cache | shared cache mode],
938** only a single busy handler can be defined for each database file.
939** So if two database connections share a single cache, then changing
940** the busy handler on one connection will also change the busy
941** handler in the other connection. The busy handler is invoked
942** in the thread that was running when the SQLITE_BUSY was hit.
943*/
944int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
945
946/*
947** CAPI3REF: Set A Busy Timeout
948**
949** This routine sets a busy handler that sleeps for a while when a
950** table is locked. The handler will sleep multiple times until
951** at least "ms" milliseconds of sleeping have been done. After
952** "ms" milliseconds of sleeping, the handler returns 0 which
953** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
954**
955** Calling this routine with an argument less than or equal to zero
956** turns off all busy handlers.
957**
958** There can only be a single busy handler for a particular database
959** connection. If another busy handler was defined
960** (using [sqlite3_busy_handler()]) prior to calling
961** this routine, that other busy handler is cleared.
962*/
963int sqlite3_busy_timeout(sqlite3*, int ms);
964
965/*
966** CAPI3REF: Convenience Routines For Running Queries
967**
968** This next routine is a convenience wrapper around [sqlite3_exec()].
969** Instead of invoking a user-supplied callback for each row of the
970** result, this routine remembers each row of the result in memory
971** obtained from [sqlite3_malloc()], then returns all of the result after the
972** query has finished.
973**
974** As an example, suppose the query result where this table:
975**
976** <blockquote><pre>
977** Name | Age
978** -----------------------
979** Alice | 43
980** Bob | 28
981** Cindy | 21
982** </pre></blockquote>
983**
984** If the 3rd argument were &azResult then after the function returns
985** azResult will contain the following data:
986**
987** <blockquote><pre>
988** azResult&#91;0] = "Name";
989** azResult&#91;1] = "Age";
990** azResult&#91;2] = "Alice";
991** azResult&#91;3] = "43";
992** azResult&#91;4] = "Bob";
993** azResult&#91;5] = "28";
994** azResult&#91;6] = "Cindy";
995** azResult&#91;7] = "21";
996** </pre></blockquote>
997**
998** Notice that there is an extra row of data containing the column
999** headers. But the *nrow return value is still 3. *ncolumn is
1000** set to 2. In general, the number of values inserted into azResult
1001** will be ((*nrow) + 1)*(*ncolumn).
1002**
1003** After the calling function has finished using the result, it should
1004** pass the result data pointer to sqlite3_free_table() in order to
1005** release the memory that was malloc-ed. Because of the way the
1006** [sqlite3_malloc()] happens, the calling function must not try to call
1007** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release
1008** the memory properly and safely.
1009**
1010** The return value of this routine is the same as from [sqlite3_exec()].
1011*/
1012int sqlite3_get_table(
1013 sqlite3*, /* An open database */
1014 const char *sql, /* SQL to be executed */
1015 char ***resultp, /* Result written to a char *[] that this points to */
1016 int *nrow, /* Number of result rows written here */
1017 int *ncolumn, /* Number of result columns written here */
1018 char **errmsg /* Error msg written here */
1019);
1020void sqlite3_free_table(char **result);
1021
1022/*
1023** CAPI3REF: Formatted String Printing Functions
1024**
1025** These routines are workalikes of the "printf()" family of functions
1026** from the standard C library.
1027**
1028** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
1029** results into memory obtained from [sqlite3_malloc()].
1030** The strings returned by these two routines should be
1031** released by [sqlite3_free()]. Both routines return a
1032** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
1033** memory to hold the resulting string.
1034**
1035** In sqlite3_snprintf() routine is similar to "snprintf()" from
1036** the standard C library. The result is written into the
1037** buffer supplied as the second parameter whose size is given by
1038** the first parameter. Note that the order of the
1039** first two parameters is reversed from snprintf(). This is an
1040** historical accident that cannot be fixed without breaking
1041** backwards compatibility. Note also that sqlite3_snprintf()
1042** returns a pointer to its buffer instead of the number of
1043** characters actually written into the buffer. We admit that
1044** the number of characters written would be a more useful return
1045** value but we cannot change the implementation of sqlite3_snprintf()
1046** now without breaking compatibility.
1047**
1048** As long as the buffer size is greater than zero, sqlite3_snprintf()
1049** guarantees that the buffer is always zero-terminated. The first
1050** parameter "n" is the total size of the buffer, including space for
1051** the zero terminator. So the longest string that can be completely
1052** written will be n-1 characters.
1053**
1054** These routines all implement some additional formatting
1055** options that are useful for constructing SQL statements.
1056** All of the usual printf formatting options apply. In addition, there
1057** is are "%q", "%Q", and "%z" options.
1058**
1059** The %q option works like %s in that it substitutes a null-terminated
1060** string from the argument list. But %q also doubles every '\'' character.
1061** %q is designed for use inside a string literal. By doubling each '\''
1062** character it escapes that character and allows it to be inserted into
1063** the string.
1064**
1065** For example, so some string variable contains text as follows:
1066**
1067** <blockquote><pre>
1068** char *zText = "It's a happy day!";
1069** </pre></blockquote>
1070**
1071** One can use this text in an SQL statement as follows:
1072**
1073** <blockquote><pre>
1074** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
1075** sqlite3_exec(db, zSQL, 0, 0, 0);
1076** sqlite3_free(zSQL);
1077** </pre></blockquote>
1078**
1079** Because the %q format string is used, the '\'' character in zText
1080** is escaped and the SQL generated is as follows:
1081**
1082** <blockquote><pre>
1083** INSERT INTO table1 VALUES('It''s a happy day!')
1084** </pre></blockquote>
1085**
1086** This is correct. Had we used %s instead of %q, the generated SQL
1087** would have looked like this:
1088**
1089** <blockquote><pre>
1090** INSERT INTO table1 VALUES('It's a happy day!');
1091** </pre></blockquote>
1092**
1093** This second example is an SQL syntax error. As a general rule you
1094** should always use %q instead of %s when inserting text into a string
1095** literal.
1096**
1097** The %Q option works like %q except it also adds single quotes around
1098** the outside of the total string. Or if the parameter in the argument
1099** list is a NULL pointer, %Q substitutes the text "NULL" (without single
1100** quotes) in place of the %Q option. So, for example, one could say:
1101**
1102** <blockquote><pre>
1103** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
1104** sqlite3_exec(db, zSQL, 0, 0, 0);
1105** sqlite3_free(zSQL);
1106** </pre></blockquote>
1107**
1108** The code above will render a correct SQL statement in the zSQL
1109** variable even if the zText variable is a NULL pointer.
1110**
1111** The "%z" formatting option works exactly like "%s" with the
1112** addition that after the string has been read and copied into
1113** the result, [sqlite3_free()] is called on the input string.
1114*/
1115char *sqlite3_mprintf(const char*,...);
1116char *sqlite3_vmprintf(const char*, va_list);
1117char *sqlite3_snprintf(int,char*,const char*, ...);
1118
1119/*
1120** CAPI3REF: Memory Allocation Subsystem
1121**
1122** The SQLite core uses these three routines for all of its own
1123** internal memory allocation needs. (See the exception below.)
1124** The default implementation
1125** of the memory allocation subsystem uses the malloc(), realloc()
1126** and free() provided by the standard C library. However, if
1127** SQLite is compiled with the following C preprocessor macro
1128**
1129** <blockquote> SQLITE_OMIT_MEMORY_ALLOCATION </blockquote>
1130**
1131** then no implementation is provided for these routines by
1132** SQLite. The application that links against SQLite is
1133** expected to provide its own implementation. If the application
1134** does provide its own implementation for these routines, then
1135** it must also provide an implementations for
1136** [sqlite3_memory_alarm()], [sqlite3_memory_used()], and
1137** [sqlite3_memory_highwater()]. The alternative implementations
1138** for these last three routines need not actually work, but
1139** stub functions at least are needed to statisfy the linker.
1140** SQLite never calls [sqlite3_memory_highwater()] itself, but
1141** the symbol is included in a table as part of the
1142** [sqlite3_load_extension()] interface. The
1143** [sqlite3_memory_alarm()] and [sqlite3_memory_used()] interfaces
1144** are called by [sqlite3_soft_heap_limit()] and working implementations
1145** of both routines must be provided if [sqlite3_soft_heap_limit()]
1146** is to operate correctly.
1147**
1148** <b>Exception:</b> The windows OS interface layer calls
1149** the system malloc() and free() directly when converting
1150** filenames between the UTF-8 encoding used by SQLite
1151** and whatever filename encoding is used by the particular windows
1152** installation. Memory allocation errors are detected, but
1153** they are reported back as [SQLITE_CANTOPEN] or
1154** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
1155*/
1156void *sqlite3_malloc(int);
1157void *sqlite3_realloc(void*, int);
1158void sqlite3_free(void*);
1159
1160/*
1161** CAPI3REF: Memory Allocator Statistics
1162**
1163** In addition to the basic three allocation routines
1164** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()],
1165** the memory allocation subsystem included with the SQLite
1166** sources provides the interfaces shown below.
1167**
1168** The first of these two routines returns the amount of memory
1169** currently outstanding (malloced but not freed). The second
1170** returns the largest instantaneous amount of outstanding
1171** memory. The highwater mark is reset if the argument is
1172** true.
1173**
1174** The implementation of these routines in the SQLite core
1175** is omitted if the application is compiled with the
1176** SQLITE_OMIT_MEMORY_ALLOCATION macro defined. In that case,
1177** the application that links SQLite must provide its own
1178** alternative implementation. See the documentation on
1179** [sqlite3_malloc()] for additional information.
1180*/
1181sqlite3_int64 sqlite3_memory_used(void);
1182sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
1183
1184/*
1185** CAPI3REF: Memory Allocation Alarms
1186**
1187** The [sqlite3_memory_alarm] routine is used to register
1188** a callback on memory allocation events.
1189**
1190** This routine registers or clears a callbacks that fires when
1191** the amount of memory allocated exceeds iThreshold. Only
1192** a single callback can be registered at a time. Each call
1193** to [sqlite3_memory_alarm()] overwrites the previous callback.
1194** The callback is disabled by setting xCallback to a NULL
1195** pointer.
1196**
1197** The parameters to the callback are the pArg value, the
1198** amount of memory currently in use, and the size of the
1199** allocation that provoked the callback. The callback will
1200** presumably invoke [sqlite3_free()] to free up memory space.
1201** The callback may invoke [sqlite3_malloc()] or [sqlite3_realloc()]
1202** but if it does, no additional callbacks will be invoked by
1203** the recursive calls.
1204**
1205** The [sqlite3_soft_heap_limit()] interface works by registering
1206** a memory alarm at the soft heap limit and invoking
1207** [sqlite3_release_memory()] in the alarm callback. Application
1208** programs should not attempt to use the [sqlite3_memory_alarm()]
1209** interface because doing so will interfere with the
1210** [sqlite3_soft_heap_limit()] module. This interface is exposed
1211** only so that applications can provide their own
1212** alternative implementation when the SQLite core is
1213** compiled with SQLITE_OMIT_MEMORY_ALLOCATION.
1214*/
1215int sqlite3_memory_alarm(
1216 void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
1217 void *pArg,
1218 sqlite3_int64 iThreshold
1219);
1220
1221
1222/*
1223** CAPI3REF: Compile-Time Authorization Callbacks
1224***
1225** This routine registers a authorizer callback with the SQLite library.
1226** The authorizer callback is invoked as SQL statements are being compiled
1227** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
1228** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. At various
1229** points during the compilation process, as logic is being created
1230** to perform various actions, the authorizer callback is invoked to
1231** see if those actions are allowed. The authorizer callback should
1232** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the
1233** specific action but allow the SQL statement to continue to be
1234** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
1235** rejected with an error.
1236**
1237** Depending on the action, the [SQLITE_IGNORE] and [SQLITE_DENY] return
1238** codes might mean something different or they might mean the same
1239** thing. If the action is, for example, to perform a delete opertion,
1240** then [SQLITE_IGNORE] and [SQLITE_DENY] both cause the statement compilation
1241** to fail with an error. But if the action is to read a specific column
1242** from a specific table, then [SQLITE_DENY] will cause the entire
1243** statement to fail but [SQLITE_IGNORE] will cause a NULL value to be
1244** read instead of the actual column value.
1245**
1246** The first parameter to the authorizer callback is a copy of
1247** the third parameter to the sqlite3_set_authorizer() interface.
1248** The second parameter to the callback is an integer
1249** [SQLITE_COPY | action code] that specifies the particular action
1250** to be authorized. The available action codes are
1251** [SQLITE_COPY | documented separately]. The third through sixth
1252** parameters to the callback are strings that contain additional
1253** details about the action to be authorized.
1254**
1255** An authorizer is used when preparing SQL statements from an untrusted
1256** source, to ensure that the SQL statements do not try to access data
1257** that they are not allowed to see, or that they do not try to
1258** execute malicious statements that damage the database. For
1259** example, an application may allow a user to enter arbitrary
1260** SQL queries for evaluation by a database. But the application does
1261** not want the user to be able to make arbitrary changes to the
1262** database. An authorizer could then be put in place while the
1263** user-entered SQL is being prepared that disallows everything
1264** except SELECT statements.
1265**
1266** Only a single authorizer can be in place on a database connection
1267** at a time. Each call to sqlite3_set_authorizer overrides the
1268** previous call. A NULL authorizer means that no authorization
1269** callback is invoked. The default authorizer is NULL.
1270**
1271** Note that the authorizer callback is invoked only during
1272** [sqlite3_prepare()] or its variants. Authorization is not
1273** performed during statement evaluation in [sqlite3_step()].
1274*/
1275int sqlite3_set_authorizer(
1276 sqlite3*,
1277 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
1278 void *pUserData
1279);
1280
1281/*
1282** CAPI3REF: Authorizer Return Codes
1283**
1284** The [sqlite3_set_authorizer | authorizer callback function] must
1285** return either [SQLITE_OK] or one of these two constants in order
1286** to signal SQLite whether or not the action is permitted. See the
1287** [sqlite3_set_authorizer | authorizer documentation] for additional
1288** information.
1289*/
1290#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
1291#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
1292
1293/*
1294** CAPI3REF: Authorizer Action Codes
1295**
1296** The [sqlite3_set_authorizer()] interface registers a callback function
1297** that is invoked to authorizer certain SQL statement actions. The
1298** second parameter to the callback is an integer code that specifies
1299** what action is being authorized. These are the integer action codes that
1300** the authorizer callback may be passed.
1301**
1302** These action code values signify what kind of operation is to be
1303** authorized. The 3rd and 4th parameters to the authorization callback
1304** function will be parameters or NULL depending on which of these
1305** codes is used as the second parameter. The 5th parameter to the
1306** authorizer callback is the name of the database ("main", "temp",
1307** etc.) if applicable. The 6th parameter to the authorizer callback
1308** is the name of the inner-most trigger or view that is responsible for
1309** the access attempt or NULL if this access attempt is directly from
1310** top-level SQL code.
1311*/
1312/******************************************* 3rd ************ 4th ***********/
1313#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
1314#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
1315#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
1316#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
1317#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
1318#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
1319#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
1320#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
1321#define SQLITE_DELETE 9 /* Table Name NULL */
1322#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
1323#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
1324#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
1325#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
1326#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
1327#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
1328#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
1329#define SQLITE_DROP_VIEW 17 /* View Name NULL */
1330#define SQLITE_INSERT 18 /* Table Name NULL */
1331#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
1332#define SQLITE_READ 20 /* Table Name Column Name */
1333#define SQLITE_SELECT 21 /* NULL NULL */
1334#define SQLITE_TRANSACTION 22 /* NULL NULL */
1335#define SQLITE_UPDATE 23 /* Table Name Column Name */
1336#define SQLITE_ATTACH 24 /* Filename NULL */
1337#define SQLITE_DETACH 25 /* Database Name NULL */
1338#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
1339#define SQLITE_REINDEX 27 /* Index Name NULL */
1340#define SQLITE_ANALYZE 28 /* Table Name NULL */
1341#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
1342#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
1343#define SQLITE_FUNCTION 31 /* Function Name NULL */
1344#define SQLITE_COPY 0 /* No longer used */
1345
1346/*
1347** CAPI3REF: Tracing And Profiling Functions
1348**
1349** These routines register callback functions that can be used for
1350** tracing and profiling the execution of SQL statements.
1351** The callback function registered by sqlite3_trace() is invoked
1352** at the first [sqlite3_step()] for the evaluation of an SQL statement.
1353** The callback function registered by sqlite3_profile() is invoked
1354** as each SQL statement finishes and includes
1355** information on how long that statement ran.
1356**
1357** The sqlite3_profile() API is currently considered experimental and
1358** is subject to change.
1359*/
1360void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
1361void *sqlite3_profile(sqlite3*,
1362 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
1363
1364/*
1365** CAPI3REF: Query Progress Callbacks
1366**
1367** This routine configures a callback function - the progress callback - that
1368** is invoked periodically during long running calls to [sqlite3_exec()],
1369** [sqlite3_step()] and [sqlite3_get_table()]. An example use for this
1370** interface is to keep a GUI updated during a large query.
1371**
1372** The progress callback is invoked once for every N virtual machine opcodes,
1373** where N is the second argument to this function. The progress callback
1374** itself is identified by the third argument to this function. The fourth
1375** argument to this function is a void pointer passed to the progress callback
1376** function each time it is invoked.
1377**
1378** If a call to [sqlite3_exec()], [sqlite3_step()], or [sqlite3_get_table()]
1379** results in fewer than N opcodes being executed, then the progress
1380** callback is never invoked.
1381**
1382** Only a single progress callback function may be registered for each
1383** open database connection. Every call to sqlite3_progress_handler()
1384** overwrites the results of the previous call.
1385** To remove the progress callback altogether, pass NULL as the third
1386** argument to this function.
1387**
1388** If the progress callback returns a result other than 0, then the current
1389** query is immediately terminated and any database changes rolled back.
1390** The containing [sqlite3_exec()], [sqlite3_step()], or
1391** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. This feature
1392** can be used, for example, to implement the "Cancel" button on a
1393** progress dialog box in a GUI.
1394*/
1395void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
1396
1397/*
1398** CAPI3REF: Opening A New Database Connection
1399**
1400** Open the sqlite database file "filename". The "filename" is UTF-8
1401** encoded for [sqlite3_open()] and [sqlite3_open_v2()] and UTF-16 encoded
1402** in the native byte order for [sqlite3_open16()].
1403** An [sqlite3*] handle is returned in *ppDb, even
1404** if an error occurs. If the database is opened (or created) successfully,
1405** then [SQLITE_OK] is returned. Otherwise an error code is returned. The
1406** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
1407** an English language description of the error.
1408**
1409** The default encoding for the database will be UTF-8 if
1410** [sqlite3_open()] or [sqlite3_open_v2()] is called and
1411** UTF-16 if [sqlite3_open16()] is used.
1412**
1413** Whether or not an error occurs when it is opened, resources associated
1414** with the [sqlite3*] handle should be released by passing it to
1415** [sqlite3_close()] when it is no longer required.
1416**
1417** The [sqlite3_open_v2()] interface works like [sqlite3_open()] except that
1418** provides two additional parameters for additional control over the
1419** new database connection. The flags parameter can be one of:
1420**
1421** <ol>
1422** <li> [SQLITE_OPEN_READONLY]
1423** <li> [SQLITE_OPEN_READWRITE]
1424** <li> [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]
1425** </ol>
1426**
1427** The first value opens the database read-only. If the database does
1428** not previously exist, an error is returned. The second option opens
1429** the database for reading and writing if possible, or reading only if
1430** if the file is write protected. In either case the database must already
1431** exist or an error is returned. The third option opens the database
1432** for reading and writing and creates it if it does not already exist.
1433** The third options is behavior that is always used for [sqlite3_open()]
1434** and [sqlite3_open16()].
1435**
1436** If the filename is ":memory:", then an private
1437** in-memory database is created for the connection. This in-memory
1438** database will vanish when the database connection is closed. Future
1439** version of SQLite might make use of additional special filenames
1440** that begin with the ":" character. It is recommended that
1441** when a database filename really does begin with
1442** ":" that you prefix the filename with a pathname like "./" to
1443** avoid ambiguity.
1444**
1445** If the filename is an empty string, then a private temporary
1446** on-disk database will be created. This private database will be
1447** automatically deleted as soon as the database connection is closed.
1448**
1449** The fourth parameter to sqlite3_open_v2() is the name of the
1450** [sqlite3_vfs] object that defines the operating system
1451** interface that the new database connection should use. If the
1452** fourth parameter is a NULL pointer then the default [sqlite3_vfs]
1453** object is used.
1454**
1455** <b>Note to windows users:</b> The encoding used for the filename argument
1456** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever
1457** codepage is currently defined. Filenames containing international
1458** characters must be converted to UTF-8 prior to passing them into
1459** [sqlite3_open()] or [sqlite3_open_v2()].
1460*/
1461int sqlite3_open(
1462 const char *filename, /* Database filename (UTF-8) */
1463 sqlite3 **ppDb /* OUT: SQLite db handle */
1464);
1465int sqlite3_open16(
1466 const void *filename, /* Database filename (UTF-16) */
1467 sqlite3 **ppDb /* OUT: SQLite db handle */
1468);
1469int sqlite3_open_v2(
1470 const char *filename, /* Database filename (UTF-8) */
1471 sqlite3 **ppDb, /* OUT: SQLite db handle */
1472 int flags, /* Flags */
1473 const char *zVfs /* Name of VFS module to use */
1474);
1475
1476/*
1477** CAPI3REF: Error Codes And Messages
1478**
1479** The sqlite3_errcode() interface returns the numeric
1480** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code]
1481** for the most recent failed sqlite3_* API call associated
1482** with [sqlite3] handle 'db'. If a prior API call failed but the
1483** most recent API call succeeded, the return value from sqlite3_errcode()
1484** is undefined.
1485**
1486** The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
1487** text that describes the error, as either UTF8 or UTF16 respectively.
1488** Memory to hold the error message string is managed internally. The
1489** string may be overwritten or deallocated by subsequent calls to SQLite
1490** interface functions.
1491**
1492** Calls to many sqlite3_* functions set the error code and string returned
1493** by [sqlite3_errcode()], [sqlite3_errmsg()], and [sqlite3_errmsg16()]
1494** (overwriting the previous values). Note that calls to [sqlite3_errcode()],
1495** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the
1496** results of future invocations. Calls to API routines that do not return
1497** an error code (example: [sqlite3_data_count()]) do not
1498** change the error code returned by this routine. Interfaces that are
1499** not associated with a specific database connection (examples:
1500** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change
1501** the return code.
1502**
1503** Assuming no other intervening sqlite3_* API calls are made, the error
1504** code returned by this function is associated with the same error as
1505** the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()].
1506*/
1507int sqlite3_errcode(sqlite3 *db);
1508const char *sqlite3_errmsg(sqlite3*);
1509const void *sqlite3_errmsg16(sqlite3*);
1510
1511/*
1512** CAPI3REF: SQL Statement Object
1513**
1514** Instance of this object represent single SQL statements. This
1515** is variously known as a "prepared statement" or a
1516** "compiled SQL statement" or simply as a "statement".
1517**
1518** The life of a statement object goes something like this:
1519**
1520** <ol>
1521** <li> Create the object using [sqlite3_prepare_v2()] or a related
1522** function.
1523** <li> Bind values to host parameters using
1524** [sqlite3_bind_blob | sqlite3_bind_* interfaces].
1525** <li> Run the SQL by calling [sqlite3_step()] one or more times.
1526** <li> Reset the statement using [sqlite3_reset()] then go back
1527** to step 2. Do this zero or more times.
1528** <li> Destroy the object using [sqlite3_finalize()].
1529** </ol>
1530**
1531** Refer to documentation on individual methods above for additional
1532** information.
1533*/
1534typedef struct sqlite3_stmt sqlite3_stmt;
1535
1536/*
1537** CAPI3REF: Compiling An SQL Statement
1538**
1539** To execute an SQL query, it must first be compiled into a byte-code
1540** program using one of these routines.
1541**
1542** The first argument "db" is an [sqlite3 | SQLite database handle]
1543** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()]
1544** or [sqlite3_open16()].
1545** The second argument "zSql" is the statement to be compiled, encoded
1546** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2()
1547** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2()
1548** use UTF-16.
1549**
1550** If the nByte argument is less
1551** than zero, then zSql is read up to the first zero terminator. If
1552** nByte is non-negative, then it is the maximum number of
1553** bytes read from zSql. When nByte is non-negative, the
1554** zSql string ends at either the first '\000' character or
1555** until the nByte-th byte, whichever comes first.
1556**
1557** *pzTail is made to point to the first byte past the end of the first
1558** SQL statement in zSql. This routine only compiles the first statement
1559** in zSql, so *pzTail is left pointing to what remains uncompiled.
1560**
1561** *ppStmt is left pointing to a compiled
1562** [sqlite3_stmt | SQL statement structure] that can be
1563** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be
1564** set to NULL. If the input text contained no SQL (if the input is and
1565** empty string or a comment) then *ppStmt is set to NULL. The calling
1566** procedure is responsible for deleting the compiled SQL statement
1567** using [sqlite3_finalize()] after it has finished with it.
1568**
1569** On success, [SQLITE_OK] is returned. Otherwise an
1570** [SQLITE_ERROR | error code] is returned.
1571**
1572** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
1573** recommended for all new programs. The two older interfaces are retained
1574** for backwards compatibility, but their use is discouraged.
1575** In the "v2" interfaces, the prepared statement
1576** that is returned (the [sqlite3_stmt] object) contains a copy of the
1577** original SQL text. This causes the [sqlite3_step()] interface to
1578** behave a differently in two ways:
1579**
1580** <ol>
1581** <li>
1582** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
1583** always used to do, [sqlite3_step()] will automatically recompile the SQL
1584** statement and try to run it again. If the schema has changed in a way
1585** that makes the statement no longer valid, [sqlite3_step()] will still
1586** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is
1587** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the
1588** error go away. Note: use [sqlite3_errmsg()] to find the text of the parsing
1589** error that results in an [SQLITE_SCHEMA] return.
1590** </li>
1591**
1592** <li>
1593** When an error occurs,
1594** [sqlite3_step()] will return one of the detailed
1595** [SQLITE_ERROR | result codes] or
1596** [SQLITE_IOERR_READ | extended result codes] such as directly.
1597** The legacy behavior was that [sqlite3_step()] would only return a generic
1598** [SQLITE_ERROR] result code and you would have to make a second call to
1599** [sqlite3_reset()] in order to find the underlying cause of the problem.
1600** With the "v2" prepare interfaces, the underlying reason for the error is
1601** returned immediately.
1602** </li>
1603** </ol>
1604*/
1605int sqlite3_prepare(
1606 sqlite3 *db, /* Database handle */
1607 const char *zSql, /* SQL statement, UTF-8 encoded */
1608 int nByte, /* Maximum length of zSql in bytes. */
1609 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
1610 const char **pzTail /* OUT: Pointer to unused portion of zSql */
1611);
1612int sqlite3_prepare_v2(
1613 sqlite3 *db, /* Database handle */
1614 const char *zSql, /* SQL statement, UTF-8 encoded */
1615 int nByte, /* Maximum length of zSql in bytes. */
1616 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
1617 const char **pzTail /* OUT: Pointer to unused portion of zSql */
1618);
1619int sqlite3_prepare16(
1620 sqlite3 *db, /* Database handle */
1621 const void *zSql, /* SQL statement, UTF-16 encoded */
1622 int nByte, /* Maximum length of zSql in bytes. */
1623 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
1624 const void **pzTail /* OUT: Pointer to unused portion of zSql */
1625);
1626int sqlite3_prepare16_v2(
1627 sqlite3 *db, /* Database handle */
1628 const void *zSql, /* SQL statement, UTF-16 encoded */
1629 int nByte, /* Maximum length of zSql in bytes. */
1630 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
1631 const void **pzTail /* OUT: Pointer to unused portion of zSql */
1632);
1633
1634/*
1635** CAPI3REF: Dynamically Typed Value Object
1636**
1637** SQLite uses dynamic typing for the values it stores. Values can
1638** be integers, floating point values, strings, BLOBs, or NULL. When
1639** passing around values internally, each value is represented as
1640** an instance of the sqlite3_value object.
1641*/
1642typedef struct Mem sqlite3_value;
1643
1644/*
1645** CAPI3REF: SQL Function Context Object
1646**
1647** The context in which an SQL function executes is stored in an
1648** sqlite3_context object. A pointer to such an object is the
1649** first parameter to user-defined SQL functions.
1650*/
1651typedef struct sqlite3_context sqlite3_context;
1652
1653/*
1654** CAPI3REF: Binding Values To Prepared Statements
1655**
1656** In the SQL strings input to [sqlite3_prepare_v2()] and its variants,
1657** one or more literals can be replace by a parameter in one of these
1658** forms:
1659**
1660** <ul>
1661** <li> ?
1662** <li> ?NNN
1663** <li> :AAA
1664** <li> @AAA
1665** <li> $VVV
1666** </ul>
1667**
1668** In the parameter forms shown above NNN is an integer literal,
1669** AAA is an alphanumeric identifier and VVV is a variable name according
1670** to the syntax rules of the TCL programming language.
1671** The values of these parameters (also called "host parameter names")
1672** can be set using the sqlite3_bind_*() routines defined here.
1673**
1674** The first argument to the sqlite3_bind_*() routines always is a pointer
1675** to the [sqlite3_stmt] object returned from [sqlite3_prepare_v2()] or
1676** its variants. The second
1677** argument is the index of the parameter to be set. The first parameter has
1678** an index of 1. When the same named parameter is used more than once, second
1679** and subsequent
1680** occurrences have the same index as the first occurrence. The index for
1681** named parameters can be looked up using the
1682** [sqlite3_bind_parameter_name()] API if desired. The index for "?NNN"
1683** parametes is the value of NNN.
1684** The NNN value must be between 1 and the compile-time
1685** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999).
1686** See <a href="limits.html">limits.html</a> for additional information.
1687**
1688** The third argument is the value to bind to the parameter.
1689**
1690** In those
1691** routines that have a fourth argument, its value is the number of bytes
1692** in the parameter. To be clear: the value is the number of bytes in the
1693** string, not the number of characters. The number
1694** of bytes does not include the zero-terminator at the end of strings.
1695** If the fourth parameter is negative, the length of the string is
1696** number of bytes up to the first zero terminator.
1697**
1698** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
1699** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
1700** text after SQLite has finished with it. If the fifth argument is the
1701** special value [SQLITE_STATIC], then the library assumes that the information
1702** is in static, unmanaged space and does not need to be freed. If the
1703** fifth argument has the value [SQLITE_TRANSIENT], then SQLite makes its
1704** own private copy of the data immediately, before the sqlite3_bind_*()
1705** routine returns.
1706**
1707** The sqlite3_bind_zeroblob() routine binds a BLOB of length n that
1708** is filled with zeros. A zeroblob uses a fixed amount of memory
1709** (just an integer to hold it size) while it is being processed.
1710** Zeroblobs are intended to serve as place-holders for BLOBs whose
1711** content is later written using
1712** [sqlite3_blob_open | increment BLOB I/O] routines. A negative
1713** value for the zeroblob results in a zero-length BLOB.
1714**
1715** The sqlite3_bind_*() routines must be called after
1716** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and
1717** before [sqlite3_step()].
1718** Bindings are not cleared by the [sqlite3_reset()] routine.
1719** Unbound parameters are interpreted as NULL.
1720**
1721** These routines return [SQLITE_OK] on success or an error code if
1722** anything goes wrong. [SQLITE_RANGE] is returned if the parameter
1723** index is out of range. [SQLITE_NOMEM] is returned if malloc fails.
1724** [SQLITE_MISUSE] is returned if these routines are called on a virtual
1725** machine that is the wrong state or which has already been finalized.
1726*/
1727int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
1728int sqlite3_bind_double(sqlite3_stmt*, int, double);
1729int sqlite3_bind_int(sqlite3_stmt*, int, int);
1730int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
1731int sqlite3_bind_null(sqlite3_stmt*, int);
1732int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
1733int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
1734int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
1735int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
1736
1737/*
1738** CAPI3REF: Number Of Host Parameters
1739**
1740** Return the largest host parameter index in the precompiled statement given
1741** as the argument. When the host parameters are of the forms like ":AAA"
1742** or "?", then they are assigned sequential increasing numbers beginning
1743** with one, so the value returned is the number of parameters. However
1744** if the same host parameter name is used multiple times, each occurrance
1745** is given the same number, so the value returned in that case is the number
1746** of unique host parameter names. If host parameters of the form "?NNN"
1747** are used (where NNN is an integer) then there might be gaps in the
1748** numbering and the value returned by this interface is the index of the
1749** host parameter with the largest index value.
1750**
1751** The prepared statement must not be [sqlite3_finalize | finalized]
1752** prior to this routine returnning. Otherwise the results are undefined
1753** and probably undesirable.
1754*/
1755int sqlite3_bind_parameter_count(sqlite3_stmt*);
1756
1757/*
1758** CAPI3REF: Name Of A Host Parameter
1759**
1760** This routine returns a pointer to the name of the n-th parameter in a
1761** [sqlite3_stmt | prepared statement].
1762** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name
1763** which is the string ":AAA" or "@AAA" or "$VVV".
1764** In other words, the initial ":" or "$" or "@"
1765** is included as part of the name.
1766** Parameters of the form "?" or "?NNN" have no name.
1767**
1768** The first bound parameter has an index of 1, not 0.
1769**
1770** If the value n is out of range or if the n-th parameter is nameless,
1771** then NULL is returned. The returned string is always in the
1772** UTF-8 encoding even if the named parameter was originally specified
1773** as UTF-16 in [sqlite3_prepare16()] or [sqlite3_prepare16_v2()].
1774*/
1775const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
1776
1777/*
1778** CAPI3REF: Index Of A Parameter With A Given Name
1779**
1780** This routine returns the index of a host parameter with the given name.
1781** The name must match exactly. If no parameter with the given name is
1782** found, return 0. Parameter names must be UTF8.
1783*/
1784int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
1785
1786/*
1787** CAPI3REF: Reset All Bindings On A Prepared Statement
1788**
1789** Contrary to the intuition of many, [sqlite3_reset()] does not
1790** reset the [sqlite3_bind_blob | bindings] on a
1791** [sqlite3_stmt | prepared statement]. Use this routine to
1792** reset all host parameters to NULL.
1793*/
1794int sqlite3_clear_bindings(sqlite3_stmt*);
1795
1796/*
1797** CAPI3REF: Number Of Columns In A Result Set
1798**
1799** Return the number of columns in the result set returned by the
1800** [sqlite3_stmt | compiled SQL statement]. This routine returns 0
1801** if pStmt is an SQL statement that does not return data (for
1802** example an UPDATE).
1803*/
1804int sqlite3_column_count(sqlite3_stmt *pStmt);
1805
1806/*
1807** CAPI3REF: Column Names In A Result Set
1808**
1809** These routines return the name assigned to a particular column
1810** in the result set of a SELECT statement. The sqlite3_column_name()
1811** interface returns a pointer to a UTF8 string and sqlite3_column_name16()
1812** returns a pointer to a UTF16 string. The first parameter is the
1813** [sqlite3_stmt | prepared statement] that implements the SELECT statement.
1814** The second parameter is the column number. The left-most column is
1815** number 0.
1816**
1817** The returned string pointer is valid until either the
1818** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()]
1819** or until the next call sqlite3_column_name() or sqlite3_column_name16()
1820** on the same column.
1821**
1822** If sqlite3_malloc() fails during the processing of either routine
1823** (for example during a conversion from UTF-8 to UTF-16) then a
1824** NULL pointer is returned.
1825*/
1826const char *sqlite3_column_name(sqlite3_stmt*, int N);
1827const void *sqlite3_column_name16(sqlite3_stmt*, int N);
1828
1829/*
1830** CAPI3REF: Source Of Data In A Query Result
1831**
1832** These routines provide a means to determine what column of what
1833** table in which database a result of a SELECT statement comes from.
1834** The name of the database or table or column can be returned as
1835** either a UTF8 or UTF16 string. The _database_ routines return
1836** the database name, the _table_ routines return the table name, and
1837** the origin_ routines return the column name.
1838** The returned string is valid until
1839** the [sqlite3_stmt | prepared statement] is destroyed using
1840** [sqlite3_finalize()] or until the same information is requested
1841** again in a different encoding.
1842**
1843** The names returned are the original un-aliased names of the
1844** database, table, and column.
1845**
1846** The first argument to the following calls is a
1847** [sqlite3_stmt | compiled SQL statement].
1848** These functions return information about the Nth column returned by
1849** the statement, where N is the second function argument.
1850**
1851** If the Nth column returned by the statement is an expression
1852** or subquery and is not a column value, then all of these functions
1853** return NULL. Otherwise, they return the
1854** name of the attached database, table and column that query result
1855** column was extracted from.
1856**
1857** As with all other SQLite APIs, those postfixed with "16" return UTF-16
1858** encoded strings, the other functions return UTF-8.
1859**
1860** These APIs are only available if the library was compiled with the
1861** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined.
1862**
1863** If two or more threads call one or more of these routines against the same
1864** prepared statement and column at the same time then the results are
1865** undefined.
1866*/
1867const char *sqlite3_column_database_name(sqlite3_stmt*,int);
1868const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
1869const char *sqlite3_column_table_name(sqlite3_stmt*,int);
1870const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
1871const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
1872const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
1873
1874/*
1875** CAPI3REF: Declared Datatype Of A Query Result
1876**
1877** The first parameter is a [sqlite3_stmt | compiled SQL statement].
1878** If this statement is a SELECT statement and the Nth column of the
1879** returned result set of that SELECT is a table column (not an
1880** expression or subquery) then the declared type of the table
1881** column is returned. If the Nth column of the result set is an
1882** expression or subquery, then a NULL pointer is returned.
1883** The returned string is always UTF-8 encoded. For example, in
1884** the database schema:
1885**
1886** CREATE TABLE t1(c1 VARIANT);
1887**
1888** And the following statement compiled:
1889**
1890** SELECT c1 + 1, c1 FROM t1;
1891**
1892** Then this routine would return the string "VARIANT" for the second
1893** result column (i==1), and a NULL pointer for the first result column
1894** (i==0).
1895**
1896** SQLite uses dynamic run-time typing. So just because a column
1897** is declared to contain a particular type does not mean that the
1898** data stored in that column is of the declared type. SQLite is
1899** strongly typed, but the typing is dynamic not static. Type
1900** is associated with individual values, not with the containers
1901** used to hold those values.
1902*/
1903const char *sqlite3_column_decltype(sqlite3_stmt *, int i);
1904const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
1905
1906/*
1907** CAPI3REF: Evaluate An SQL Statement
1908**
1909** After an [sqlite3_stmt | SQL statement] has been prepared with a call
1910** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of
1911** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()],
1912** then this function must be called one or more times to evaluate the
1913** statement.
1914**
1915** The details of the behavior of this sqlite3_step() interface depend
1916** on whether the statement was prepared using the newer "v2" interface
1917** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
1918** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
1919** new "v2" interface is recommended for new applications but the legacy
1920** interface will continue to be supported.
1921**
1922** In the lagacy interface, the return value will be either [SQLITE_BUSY],
1923** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
1924** With the "v2" interface, any of the other [SQLITE_OK | result code]
1925** or [SQLITE_IOERR_READ | extended result code] might be returned as
1926** well.
1927**
1928** [SQLITE_BUSY] means that the database engine was unable to acquire the
1929** database locks it needs to do its job. If the statement is a COMMIT
1930** or occurs outside of an explicit transaction, then you can retry the
1931** statement. If the statement is not a COMMIT and occurs within a
1932** explicit transaction then you should rollback the transaction before
1933** continuing.
1934**
1935** [SQLITE_DONE] means that the statement has finished executing
1936** successfully. sqlite3_step() should not be called again on this virtual
1937** machine without first calling [sqlite3_reset()] to reset the virtual
1938** machine back to its initial state.
1939**
1940** If the SQL statement being executed returns any data, then
1941** [SQLITE_ROW] is returned each time a new row of data is ready
1942** for processing by the caller. The values may be accessed using
1943** the [sqlite3_column_int | column access functions].
1944** sqlite3_step() is called again to retrieve the next row of data.
1945**
1946** [SQLITE_ERROR] means that a run-time error (such as a constraint
1947** violation) has occurred. sqlite3_step() should not be called again on
1948** the VM. More information may be found by calling [sqlite3_errmsg()].
1949** With the legacy interface, a more specific error code (example:
1950** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
1951** can be obtained by calling [sqlite3_reset()] on the
1952** [sqlite3_stmt | prepared statement]. In the "v2" interface,
1953** the more specific error code is returned directly by sqlite3_step().
1954**
1955** [SQLITE_MISUSE] means that the this routine was called inappropriately.
1956** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has
1957** already been [sqlite3_finalize | finalized] or on one that had
1958** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
1959** be the case that the same database connection is being used by two or
1960** more threads at the same moment in time.
1961**
1962** <b>Goofy Interface Alert:</b>
1963** In the legacy interface,
1964** the sqlite3_step() API always returns a generic error code,
1965** [SQLITE_ERROR], following any error other than [SQLITE_BUSY]
1966** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or
1967** [sqlite3_finalize()] in order to find one of the specific
1968** [SQLITE_ERROR | result codes] that better describes the error.
1969** We admit that this is a goofy design. The problem has been fixed
1970** with the "v2" interface. If you prepare all of your SQL statements
1971** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
1972** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the
1973** more specific [SQLITE_ERROR | result codes] are returned directly
1974** by sqlite3_step(). The use of the "v2" interface is recommended.
1975*/
1976int sqlite3_step(sqlite3_stmt*);
1977
1978/*
1979** CAPI3REF:
1980**
1981** Return the number of values in the current row of the result set.
1982**
1983** After a call to [sqlite3_step()] that returns [SQLITE_ROW], this routine
1984** will return the same value as the [sqlite3_column_count()] function.
1985** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or
1986** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been
1987** called on the [sqlite3_stmt | prepared statement] for the first time,
1988** this routine returns zero.
1989*/
1990int sqlite3_data_count(sqlite3_stmt *pStmt);
1991
1992/*
1993** CAPI3REF: Fundamental Datatypes
1994**
1995** Every value in SQLite has one of five fundamental datatypes:
1996**
1997** <ul>
1998** <li> 64-bit signed integer
1999** <li> 64-bit IEEE floating point number
2000** <li> string
2001** <li> BLOB
2002** <li> NULL
2003** </ul>
2004**
2005** These constants are codes for each of those types.
2006**
2007** Note that the SQLITE_TEXT constant was also used in SQLite version 2
2008** for a completely different meaning. Software that links against both
2009** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not
2010** SQLITE_TEXT.
2011*/
2012#define SQLITE_INTEGER 1
2013#define SQLITE_FLOAT 2
2014#define SQLITE_BLOB 4
2015#define SQLITE_NULL 5
2016#ifdef SQLITE_TEXT
2017# undef SQLITE_TEXT
2018#else
2019# define SQLITE_TEXT 3
2020#endif
2021#define SQLITE3_TEXT 3
2022
2023/*
2024** CAPI3REF: Results Values From A Query
2025**
2026** These routines return information about
2027** a single column of the current result row of a query. In every
2028** case the first argument is a pointer to the
2029** [sqlite3_stmt | SQL statement] that is being
2030** evaluated (the [sqlite3_stmt*] that was returned from
2031** [sqlite3_prepare_v2()] or one of its variants) and
2032** the second argument is the index of the column for which information
2033** should be returned. The left-most column of the result set
2034** has an index of 0.
2035**
2036** If the SQL statement is not currently point to a valid row, or if the
2037** the column index is out of range, the result is undefined.
2038** These routines may only be called when the most recent call to
2039** [sqlite3_step()] has returned [SQLITE_ROW] and neither
2040** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently.
2041** If any of these routines are called after [sqlite3_reset()] or
2042** [sqlite3_finalize()] or after [sqlite3_step()] has returned
2043** something other than [SQLITE_ROW], the results are undefined.
2044** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
2045** are called from a different thread while any of these routines
2046** are pending, then the results are undefined.
2047**
2048** The sqlite3_column_type() routine returns
2049** [SQLITE_INTEGER | datatype code] for the initial data type
2050** of the result column. The returned value is one of [SQLITE_INTEGER],
2051** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value
2052** returned by sqlite3_column_type() is only meaningful if no type
2053** conversions have occurred as described below. After a type conversion,
2054** the value returned by sqlite3_column_type() is undefined. Future
2055** versions of SQLite may change the behavior of sqlite3_column_type()
2056** following a type conversion.
2057**
2058** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
2059** routine returns the number of bytes in that BLOB or string.
2060** If the result is a UTF-16 string, then sqlite3_column_bytes() converts
2061** the string to UTF-8 and then returns the number of bytes.
2062** If the result is a numeric value then sqlite3_column_bytes() uses
2063** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
2064** the number of bytes in that string.
2065** The value returned does not include the zero terminator at the end
2066** of the string. For clarity: the value returned is the number of
2067** bytes in the string, not the number of characters.
2068**
2069** Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
2070** even zero-length strings, are always zero terminated. The return
2071** value from sqlite3_column_blob() for a zero-length blob is an arbitrary
2072** pointer, possibly even a NULL pointer.
2073**
2074** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes()
2075** but leaves the result in UTF-16 instead of UTF-8.
2076** The zero terminator is not included in this count.
2077**
2078** These routines attempt to convert the value where appropriate. For
2079** example, if the internal representation is FLOAT and a text result
2080** is requested, [sqlite3_snprintf()] is used internally to do the conversion
2081** automatically. The following table details the conversions that
2082** are applied:
2083**
2084** <blockquote>
2085** <table border="1">
2086** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
2087**
2088** <tr><td> NULL <td> INTEGER <td> Result is 0
2089** <tr><td> NULL <td> FLOAT <td> Result is 0.0
2090** <tr><td> NULL <td> TEXT <td> Result is NULL pointer
2091** <tr><td> NULL <td> BLOB <td> Result is NULL pointer
2092** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
2093** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
2094** <tr><td> INTEGER <td> BLOB <td> Same as for INTEGER->TEXT
2095** <tr><td> FLOAT <td> INTEGER <td> Convert from float to integer
2096** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
2097** <tr><td> FLOAT <td> BLOB <td> Same as FLOAT->TEXT
2098** <tr><td> TEXT <td> INTEGER <td> Use atoi()
2099** <tr><td> TEXT <td> FLOAT <td> Use atof()
2100** <tr><td> TEXT <td> BLOB <td> No change
2101** <tr><td> BLOB <td> INTEGER <td> Convert to TEXT then use atoi()
2102** <tr><td> BLOB <td> FLOAT <td> Convert to TEXT then use atof()
2103** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
2104** </table>
2105** </blockquote>
2106**
2107** The table above makes reference to standard C library functions atoi()
2108** and atof(). SQLite does not really use these functions. It has its
2109** on equavalent internal routines. The atoi() and atof() names are
2110** used in the table for brevity and because they are familiar to most
2111** C programmers.
2112**
2113** Note that when type conversions occur, pointers returned by prior
2114** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
2115** sqlite3_column_text16() may be invalidated.
2116** Type conversions and pointer invalidations might occur
2117** in the following cases:
2118**
2119** <ul>
2120** <li><p> The initial content is a BLOB and sqlite3_column_text()
2121** or sqlite3_column_text16() is called. A zero-terminator might
2122** need to be added to the string.</p></li>
2123**
2124** <li><p> The initial content is UTF-8 text and sqlite3_column_bytes16() or
2125** sqlite3_column_text16() is called. The content must be converted
2126** to UTF-16.</p></li>
2127**
2128** <li><p> The initial content is UTF-16 text and sqlite3_column_bytes() or
2129** sqlite3_column_text() is called. The content must be converted
2130** to UTF-8.</p></li>
2131** </ul>
2132**
2133** Conversions between UTF-16be and UTF-16le are always done in place and do
2134** not invalidate a prior pointer, though of course the content of the buffer
2135** that the prior pointer points to will have been modified. Other kinds
2136** of conversion are done in place when it is possible, but sometime it is
2137** not possible and in those cases prior pointers are invalidated.
2138**
2139** The safest and easiest to remember policy is to invoke these routines
2140** in one of the following ways:
2141**
2142** <ul>
2143** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
2144** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
2145** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
2146** </ul>
2147**
2148** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(),
2149** or sqlite3_column_text16() first to force the result into the desired
2150** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to
2151** find the size of the result. Do not mix call to sqlite3_column_text() or
2152** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not
2153** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes().
2154**
2155** The pointers returned are valid until a type conversion occurs as
2156** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
2157** [sqlite3_finalize()] is called. The memory space used to hold strings
2158** and blobs is freed automatically. Do <b>not</b> pass the pointers returned
2159** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
2160** [sqlite3_free()].
2161**
2162** If a memory allocation error occurs during the evaluation of any
2163** of these routines, a default value is returned. The default value
2164** is either the integer 0, the floating point number 0.0, or a NULL
2165** pointer. Subsequent calls to [sqlite3_errcode()] will return
2166** [SQLITE_NOMEM].
2167*/
2168const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
2169int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
2170int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
2171double sqlite3_column_double(sqlite3_stmt*, int iCol);
2172int sqlite3_column_int(sqlite3_stmt*, int iCol);
2173sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
2174const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
2175const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
2176int sqlite3_column_type(sqlite3_stmt*, int iCol);
2177sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
2178
2179/*
2180** CAPI3REF: Destroy A Prepared Statement Object
2181**
2182** The sqlite3_finalize() function is called to delete a
2183** [sqlite3_stmt | compiled SQL statement]. If the statement was
2184** executed successfully, or not executed at all, then SQLITE_OK is returned.
2185** If execution of the statement failed then an
2186** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code]
2187** is returned.
2188**
2189** This routine can be called at any point during the execution of the
2190** [sqlite3_stmt | virtual machine]. If the virtual machine has not
2191** completed execution when this routine is called, that is like
2192** encountering an error or an interrupt. (See [sqlite3_interrupt()].)
2193** Incomplete updates may be rolled back and transactions cancelled,
2194** depending on the circumstances, and the
2195** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT].
2196*/
2197int sqlite3_finalize(sqlite3_stmt *pStmt);
2198
2199/*
2200** CAPI3REF: Reset A Prepared Statement Object
2201**
2202** The sqlite3_reset() function is called to reset a
2203** [sqlite3_stmt | compiled SQL statement] object.
2204** back to it's initial state, ready to be re-executed.
2205** Any SQL statement variables that had values bound to them using
2206** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
2207** Use [sqlite3_clear_bindings()] to reset the bindings.
2208*/
2209int sqlite3_reset(sqlite3_stmt *pStmt);
2210
2211/*
2212** CAPI3REF: Create Or Redefine SQL Functions
2213**
2214** The following two functions are used to add SQL functions or aggregates
2215** or to redefine the behavior of existing SQL functions or aggregates. The
2216** difference only between the two is that the second parameter, the
2217** name of the (scalar) function or aggregate, is encoded in UTF-8 for
2218** sqlite3_create_function() and UTF-16 for sqlite3_create_function16().
2219**
2220** The first argument is the [sqlite3 | database handle] that holds the
2221** SQL function or aggregate is to be added or redefined. If a single
2222** program uses more than one database handle internally, then SQL
2223** functions or aggregates must be added individually to each database
2224** handle with which they will be used.
2225**
2226** The second parameter is the name of the SQL function to be created
2227** or redefined.
2228** The length of the name is limited to 255 bytes, exclusive of the
2229** zero-terminator. Note that the name length limit is in bytes, not
2230** characters. Any attempt to create a function with a longer name
2231** will result in an SQLITE_ERROR error.
2232**
2233** The third parameter is the number of arguments that the SQL function or
2234** aggregate takes. If this parameter is negative, then the SQL function or
2235** aggregate may take any number of arguments.
2236**
2237** The fourth parameter, eTextRep, specifies what
2238** [SQLITE_UTF8 | text encoding] this SQL function prefers for
2239** its parameters. Any SQL function implementation should be able to work
2240** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be
2241** more efficient with one encoding than another. It is allowed to
2242** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
2243** times with the same function but with different values of eTextRep.
2244** When multiple implementations of the same function are available, SQLite
2245** will pick the one that involves the least amount of data conversion.
2246** If there is only a single implementation which does not care what
2247** text encoding is used, then the fourth argument should be
2248** [SQLITE_ANY].
2249**
2250** The fifth parameter is an arbitrary pointer. The implementation
2251** of the function can gain access to this pointer using
2252** [sqlite3_user_data()].
2253**
2254** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
2255** pointers to C-language functions that implement the SQL
2256** function or aggregate. A scalar SQL function requires an implementation of
2257** the xFunc callback only, NULL pointers should be passed as the xStep
2258** and xFinal parameters. An aggregate SQL function requires an implementation
2259** of xStep and xFinal and NULL should be passed for xFunc. To delete an
2260** existing SQL function or aggregate, pass NULL for all three function
2261** callback.
2262**
2263** It is permitted to register multiple implementations of the same
2264** functions with the same name but with either differing numbers of
2265** arguments or differing perferred text encodings. SQLite will use
2266** the implementation most closely matches the way in which the
2267** SQL function is used.
2268*/
2269int sqlite3_create_function(
2270 sqlite3 *,
2271 const char *zFunctionName,
2272 int nArg,
2273 int eTextRep,
2274 void*,
2275 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
2276 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
2277 void (*xFinal)(sqlite3_context*)
2278);
2279int sqlite3_create_function16(
2280 sqlite3*,
2281 const void *zFunctionName,
2282 int nArg,
2283 int eTextRep,
2284 void*,
2285 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
2286 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
2287 void (*xFinal)(sqlite3_context*)
2288);
2289
2290/*
2291** CAPI3REF: Text Encodings
2292**
2293** These constant define integer codes that represent the various
2294** text encodings supported by SQLite.
2295*/
2296#define SQLITE_UTF8 1
2297#define SQLITE_UTF16LE 2
2298#define SQLITE_UTF16BE 3
2299#define SQLITE_UTF16 4 /* Use native byte order */
2300#define SQLITE_ANY 5 /* sqlite3_create_function only */
2301#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
2302
2303/*
2304** CAPI3REF: Obsolete Functions
2305**
2306** These functions are all now obsolete. In order to maintain
2307** backwards compatibility with older code, we continue to support
2308** these functions. However, new development projects should avoid
2309** the use of these functions. To help encourage people to avoid
2310** using these functions, we are not going to tell you want they do.
2311*/
2312int sqlite3_aggregate_count(sqlite3_context*);
2313int sqlite3_expired(sqlite3_stmt*);
2314int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
2315int sqlite3_global_recover(void);
2316void sqlite3_thread_cleanup(void);
2317
2318/*
2319** CAPI3REF: Obtaining SQL Function Parameter Values
2320**
2321** The C-language implementation of SQL functions and aggregates uses
2322** this set of interface routines to access the parameter values on
2323** the function or aggregate.
2324**
2325** The xFunc (for scalar functions) or xStep (for aggregates) parameters
2326** to [sqlite3_create_function()] and [sqlite3_create_function16()]
2327** define callbacks that implement the SQL functions and aggregates.
2328** The 4th parameter to these callbacks is an array of pointers to
2329** [sqlite3_value] objects. There is one [sqlite3_value] object for
2330** each parameter to the SQL function. These routines are used to
2331** extract values from the [sqlite3_value] objects.
2332**
2333** These routines work just like the corresponding
2334** [sqlite3_column_blob | sqlite3_column_* routines] except that
2335** these routines take a single [sqlite3_value*] pointer instead
2336** of an [sqlite3_stmt*] pointer and an integer column number.
2337**
2338** The sqlite3_value_text16() interface extracts a UTF16 string
2339** in the native byte-order of the host machine. The
2340** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
2341** extract UTF16 strings as big-endian and little-endian respectively.
2342**
2343** The sqlite3_value_numeric_type() interface attempts to apply
2344** numeric affinity to the value. This means that an attempt is
2345** made to convert the value to an integer or floating point. If
2346** such a conversion is possible without loss of information (in order
2347** words if the value is original a string that looks like a number)
2348** then it is done. Otherwise no conversion occurs. The
2349** [SQLITE_INTEGER | datatype] after conversion is returned.
2350**
2351** Please pay particular attention to the fact that the pointer that
2352** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or
2353** [sqlite3_value_text16()] can be invalidated by a subsequent call to
2354** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
2355** or [sqlite3_value_text16()].
2356**
2357** These routines must be called from the same thread as
2358** the SQL function that supplied the sqlite3_value* parameters.
2359** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()]
2360** interface, then these routines should be called from the same thread
2361** that ran [sqlite3_column_value()].
2362*/
2363const void *sqlite3_value_blob(sqlite3_value*);
2364int sqlite3_value_bytes(sqlite3_value*);
2365int sqlite3_value_bytes16(sqlite3_value*);
2366double sqlite3_value_double(sqlite3_value*);
2367int sqlite3_value_int(sqlite3_value*);
2368sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
2369const unsigned char *sqlite3_value_text(sqlite3_value*);
2370const void *sqlite3_value_text16(sqlite3_value*);
2371const void *sqlite3_value_text16le(sqlite3_value*);
2372const void *sqlite3_value_text16be(sqlite3_value*);
2373int sqlite3_value_type(sqlite3_value*);
2374int sqlite3_value_numeric_type(sqlite3_value*);
2375
2376/*
2377** CAPI3REF: Obtain Aggregate Function Context
2378**
2379** The implementation of aggregate SQL functions use this routine to allocate
2380** a structure for storing their state. The first time this routine
2381** is called for a particular aggregate, a new structure of size nBytes
2382** is allocated, zeroed, and returned. On subsequent calls (for the
2383** same aggregate instance) the same buffer is returned. The implementation
2384** of the aggregate can use the returned buffer to accumulate data.
2385**
2386** The buffer allocated is freed automatically by SQLite whan the aggregate
2387** query concludes.
2388**
2389** The first parameter should be a copy of the
2390** [sqlite3_context | SQL function context] that is the first
2391** parameter to the callback routine that implements the aggregate
2392** function.
2393**
2394** This routine must be called from the same thread in which
2395** the aggregate SQL function is running.
2396*/
2397void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
2398
2399/*
2400** CAPI3REF: User Data For Functions
2401**
2402** The pUserData parameter to the [sqlite3_create_function()]
2403** and [sqlite3_create_function16()] routines
2404** used to register user functions is available to
2405** the implementation of the function using this call.
2406**
2407** This routine must be called from the same thread in which
2408** the SQL function is running.
2409*/
2410void *sqlite3_user_data(sqlite3_context*);
2411
2412/*
2413** CAPI3REF: Function Auxiliary Data
2414**
2415** The following two functions may be used by scalar SQL functions to
2416** associate meta-data with argument values. If the same value is passed to
2417** multiple invocations of the same SQL function during query execution, under
2418** some circumstances the associated meta-data may be preserved. This may
2419** be used, for example, to add a regular-expression matching scalar
2420** function. The compiled version of the regular expression is stored as
2421** meta-data associated with the SQL value passed as the regular expression
2422** pattern. The compiled regular expression can be reused on multiple
2423** invocations of the same function so that the original pattern string
2424** does not need to be recompiled on each invocation.
2425**
2426** The sqlite3_get_auxdata() interface returns a pointer to the meta-data
2427** associated with the Nth argument value to the current SQL function
2428** call, where N is the second parameter. If no meta-data has been set for
2429** that value, then a NULL pointer is returned.
2430**
2431** The sqlite3_set_auxdata() is used to associate meta-data with an SQL
2432** function argument. The third parameter is a pointer to the meta-data
2433** to be associated with the Nth user function argument value. The fourth
2434** parameter specifies a destructor that will be called on the meta-
2435** data pointer to release it when it is no longer required. If the
2436** destructor is NULL, it is not invoked.
2437**
2438** In practice, meta-data is preserved between function calls for
2439** expressions that are constant at compile time. This includes literal
2440** values and SQL variables.
2441**
2442** These routines must be called from the same thread in which
2443** the SQL function is running.
2444*/
2445void *sqlite3_get_auxdata(sqlite3_context*, int);
2446void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
2447
2448
2449/*
2450** CAPI3REF: Constants Defining Special Destructor Behavior
2451**
2452** These are special value for the destructor that is passed in as the
2453** final argument to routines like [sqlite3_result_blob()]. If the destructor
2454** argument is SQLITE_STATIC, it means that the content pointer is constant
2455** and will never change. It does not need to be destroyed. The
2456** SQLITE_TRANSIENT value means that the content will likely change in
2457** the near future and that SQLite should make its own private copy of
2458** the content before returning.
2459**
2460** The typedef is necessary to work around problems in certain
2461** C++ compilers. See ticket #2191.
2462*/
2463typedef void (*sqlite3_destructor_type)(void*);
2464#define SQLITE_STATIC ((sqlite3_destructor_type)0)
2465#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
2466
2467/*
2468** CAPI3REF: Setting The Result Of An SQL Function
2469**
2470** These routines are used by the xFunc or xFinal callbacks that
2471** implement SQL functions and aggregates. See
2472** [sqlite3_create_function()] and [sqlite3_create_function16()]
2473** for additional information.
2474**
2475** These functions work very much like the
2476** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used
2477** to bind values to host parameters in prepared statements.
2478** Refer to the
2479** [sqlite3_bind_blob | sqlite3_bind_* documentation] for
2480** additional information.
2481**
2482** The sqlite3_result_error() and sqlite3_result_error16() functions
2483** cause the implemented SQL function to throw an exception. The
2484** parameter to sqlite3_result_error() or sqlite3_result_error16()
2485** is the text of an error message.
2486**
2487** The sqlite3_result_toobig() cause the function implementation
2488** to throw and error indicating that a string or BLOB is to long
2489** to represent.
2490**
2491** These routines must be called from within the same thread as
2492** the SQL function associated with the [sqlite3_context] pointer.
2493*/
2494void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
2495void sqlite3_result_double(sqlite3_context*, double);
2496void sqlite3_result_error(sqlite3_context*, const char*, int);
2497void sqlite3_result_error16(sqlite3_context*, const void*, int);
2498void sqlite3_result_error_toobig(sqlite3_context*);
2499void sqlite3_result_error_nomem(sqlite3_context*);
2500void sqlite3_result_int(sqlite3_context*, int);
2501void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
2502void sqlite3_result_null(sqlite3_context*);
2503void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
2504void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
2505void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
2506void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
2507void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
2508void sqlite3_result_zeroblob(sqlite3_context*, int n);
2509
2510/*
2511** CAPI3REF: Define New Collating Sequences
2512**
2513** These functions are used to add new collation sequences to the
2514** [sqlite3*] handle specified as the first argument.
2515**
2516** The name of the new collation sequence is specified as a UTF-8 string
2517** for sqlite3_create_collation() and sqlite3_create_collation_v2()
2518** and a UTF-16 string for sqlite3_create_collation16(). In all cases
2519** the name is passed as the second function argument.
2520**
2521** The third argument must be one of the constants [SQLITE_UTF8],
2522** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied
2523** routine expects to be passed pointers to strings encoded using UTF-8,
2524** UTF-16 little-endian or UTF-16 big-endian respectively.
2525**
2526** A pointer to the user supplied routine must be passed as the fifth
2527** argument. If it is NULL, this is the same as deleting the collation
2528** sequence (so that SQLite cannot call it anymore). Each time the user
2529** supplied function is invoked, it is passed a copy of the void* passed as
2530** the fourth argument to sqlite3_create_collation() or
2531** sqlite3_create_collation16() as its first parameter.
2532**
2533** The remaining arguments to the user-supplied routine are two strings,
2534** each represented by a [length, data] pair and encoded in the encoding
2535** that was passed as the third argument when the collation sequence was
2536** registered. The user routine should return negative, zero or positive if
2537** the first string is less than, equal to, or greater than the second
2538** string. i.e. (STRING1 - STRING2).
2539**
2540** The sqlite3_create_collation_v2() works like sqlite3_create_collation()
2541** excapt that it takes an extra argument which is a destructor for
2542** the collation. The destructor is called when the collation is
2543** destroyed and is passed a copy of the fourth parameter void* pointer
2544** of the sqlite3_create_collation_v2(). Collations are destroyed when
2545** they are overridden by later calls to the collation creation functions
2546** or when the [sqlite3*] database handle is closed using [sqlite3_close()].
2547**
2548** The sqlite3_create_collation_v2() interface is experimental and
2549** subject to change in future releases. The other collation creation
2550** functions are stable.
2551*/
2552int sqlite3_create_collation(
2553 sqlite3*,
2554 const char *zName,
2555 int eTextRep,
2556 void*,
2557 int(*xCompare)(void*,int,const void*,int,const void*)
2558);
2559int sqlite3_create_collation_v2(
2560 sqlite3*,
2561 const char *zName,
2562 int eTextRep,
2563 void*,
2564 int(*xCompare)(void*,int,const void*,int,const void*),
2565 void(*xDestroy)(void*)
2566);
2567int sqlite3_create_collation16(
2568 sqlite3*,
2569 const char *zName,
2570 int eTextRep,
2571 void*,
2572 int(*xCompare)(void*,int,const void*,int,const void*)
2573);
2574
2575/*
2576** CAPI3REF: Collation Needed Callbacks
2577**
2578** To avoid having to register all collation sequences before a database
2579** can be used, a single callback function may be registered with the
2580** database handle to be called whenever an undefined collation sequence is
2581** required.
2582**
2583** If the function is registered using the sqlite3_collation_needed() API,
2584** then it is passed the names of undefined collation sequences as strings
2585** encoded in UTF-8. If sqlite3_collation_needed16() is used, the names
2586** are passed as UTF-16 in machine native byte order. A call to either
2587** function replaces any existing callback.
2588**
2589** When the callback is invoked, the first argument passed is a copy
2590** of the second argument to sqlite3_collation_needed() or
2591** sqlite3_collation_needed16(). The second argument is the database
2592** handle. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], or
2593** [SQLITE_UTF16LE], indicating the most desirable form of the collation
2594** sequence function required. The fourth parameter is the name of the
2595** required collation sequence.
2596**
2597** The callback function should register the desired collation using
2598** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
2599** [sqlite3_create_collation_v2()].
2600*/
2601int sqlite3_collation_needed(
2602 sqlite3*,
2603 void*,
2604 void(*)(void*,sqlite3*,int eTextRep,const char*)
2605);
2606int sqlite3_collation_needed16(
2607 sqlite3*,
2608 void*,
2609 void(*)(void*,sqlite3*,int eTextRep,const void*)
2610);
2611
2612/*
2613** Specify the key for an encrypted database. This routine should be
2614** called right after sqlite3_open().
2615**
2616** The code to implement this API is not available in the public release
2617** of SQLite.
2618*/
2619int sqlite3_key(
2620 sqlite3 *db, /* Database to be rekeyed */
2621 const void *pKey, int nKey /* The key */
2622);
2623
2624/*
2625** Change the key on an open database. If the current database is not
2626** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
2627** database is decrypted.
2628**
2629** The code to implement this API is not available in the public release
2630** of SQLite.
2631*/
2632int sqlite3_rekey(
2633 sqlite3 *db, /* Database to be rekeyed */
2634 const void *pKey, int nKey /* The new key */
2635);
2636
2637/*
2638** CAPI3REF: Suspend Execution For A Short Time
2639**
2640** This function causes the current thread to suspend execution
2641** a number of milliseconds specified in its parameter.
2642**
2643** If the operating system does not support sleep requests with
2644** millisecond time resolution, then the time will be rounded up to
2645** the nearest second. The number of milliseconds of sleep actually
2646** requested from the operating system is returned.
2647**
2648** SQLite implements this interface by calling the xSleep()
2649** method of the default [sqlite3_vfs] object.
2650*/
2651int sqlite3_sleep(int);
2652
2653/*
2654** CAPI3REF: Name Of The Folder Holding Temporary Files
2655**
2656** If this global variable is made to point to a string which is
2657** the name of a folder (a.ka. directory), then all temporary files
2658** created by SQLite will be placed in that directory. If this variable
2659** is NULL pointer, then SQLite does a search for an appropriate temporary
2660** file directory.
2661**
2662** It is not safe to modify this variable once a database connection
2663** has been opened. It is intended that this variable be set once
2664** as part of process initialization and before any SQLite interface
2665** routines have been call and remain unchanged thereafter.
2666*/
2667SQLITE_EXTERN char *sqlite3_temp_directory;
2668
2669/*
2670** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode
2671**
2672** Test to see whether or not the database connection is in autocommit
2673** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
2674** by default. Autocommit is disabled by a BEGIN statement and reenabled
2675** by the next COMMIT or ROLLBACK.
2676**
2677** If certain kinds of errors occur on a statement within a multi-statement
2678** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR],
2679** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
2680** transaction might be rolled back automatically. The only way to
2681** find out if SQLite automatically rolled back the transaction after
2682** an error is to use this function.
2683**
2684** If another thread changes the autocommit status of the database
2685** connection while this routine is running, then the return value
2686** is undefined.
2687*/
2688int sqlite3_get_autocommit(sqlite3*);
2689
2690/*
2691** CAPI3REF: Find The Database Handle Associated With A Prepared Statement
2692**
2693** Return the [sqlite3*] database handle to which a
2694** [sqlite3_stmt | prepared statement] belongs.
2695** This is the same database handle that was
2696** the first argument to the [sqlite3_prepare_v2()] or its variants
2697** that was used to create the statement in the first place.
2698*/
2699sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
2700
2701
2702/*
2703** CAPI3REF: Commit And Rollback Notification Callbacks
2704**
2705** These routines
2706** register callback functions to be invoked whenever a transaction
2707** is committed or rolled back. The pArg argument is passed through
2708** to the callback. If the callback on a commit hook function
2709** returns non-zero, then the commit is converted into a rollback.
2710**
2711** If another function was previously registered, its pArg value is returned.
2712** Otherwise NULL is returned.
2713**
2714** Registering a NULL function disables the callback.
2715**
2716** For the purposes of this API, a transaction is said to have been
2717** rolled back if an explicit "ROLLBACK" statement is executed, or
2718** an error or constraint causes an implicit rollback to occur. The
2719** callback is not invoked if a transaction is automatically rolled
2720** back because the database connection is closed.
2721**
2722** These are experimental interfaces and are subject to change.
2723*/
2724void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
2725void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
2726
2727/*
2728** CAPI3REF: Data Change Notification Callbacks
2729**
2730** Register a callback function with the database connection identified by the
2731** first argument to be invoked whenever a row is updated, inserted or deleted.
2732** Any callback set by a previous call to this function for the same
2733** database connection is overridden.
2734**
2735** The second argument is a pointer to the function to invoke when a
2736** row is updated, inserted or deleted. The first argument to the callback is
2737** a copy of the third argument to sqlite3_update_hook(). The second callback
2738** argument is one of SQLITE_INSERT, SQLITE_DELETE or SQLITE_UPDATE, depending
2739** on the operation that caused the callback to be invoked. The third and
2740** fourth arguments to the callback contain pointers to the database and
2741** table name containing the affected row. The final callback parameter is
2742** the rowid of the row. In the case of an update, this is the rowid after
2743** the update takes place.
2744**
2745** The update hook is not invoked when internal system tables are
2746** modified (i.e. sqlite_master and sqlite_sequence).
2747**
2748** If another function was previously registered, its pArg value is returned.
2749** Otherwise NULL is returned.
2750*/
2751void *sqlite3_update_hook(
2752 sqlite3*,
2753 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
2754 void*
2755);
2756
2757/*
2758** CAPI3REF: Enable Or Disable Shared Pager Cache
2759**
2760** This routine enables or disables the sharing of the database cache
2761** and schema data structures between connections to the same database.
2762** Sharing is enabled if the argument is true and disabled if the argument
2763** is false.
2764**
2765** Beginning in SQLite version 3.5.0, cache sharing is enabled and disabled
2766** for an entire process. In prior versions of SQLite, sharing was
2767** enabled or disabled for each thread separately.
2768**
2769** The cache sharing mode set by this interface effects all subsequent
2770** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
2771** Existing database connections continue use the sharing mode that was
2772** in effect at the time they were opened.
2773**
2774** Virtual tables cannot be used with a shared cache. When shared
2775** cache is enabled, the [sqlite3_create_module()] API used to register
2776** virtual tables will always return an error.
2777**
2778** This routine returns [SQLITE_OK] if shared cache was
2779** enabled or disabled successfully. An [SQLITE_ERROR | error code]
2780** is returned otherwise.
2781**
2782** Shared cache is disabled by default. But this might change in
2783** future releases of SQLite. Applications that care about shared
2784** cache setting should set it explicitly.
2785*/
2786int sqlite3_enable_shared_cache(int);
2787
2788/*
2789** CAPI3REF: Attempt To Free Heap Memory
2790**
2791** Attempt to free N bytes of heap memory by deallocating non-essential
2792** memory allocations held by the database library (example: memory
2793** used to cache database pages to improve performance).
2794*/
2795int sqlite3_release_memory(int);
2796
2797/*
2798** CAPI3REF: Impose A Limit On Heap Size
2799**
2800** Place a "soft" limit on the amount of heap memory that may be allocated
2801** by SQLite. If an internal allocation is requested
2802** that would exceed the specified limit, [sqlite3_release_memory()] is
2803** invoked one or more times to free up some space before the allocation
2804** is made.
2805**
2806** The limit is called "soft", because if [sqlite3_release_memory()] cannot
2807** free sufficient memory to prevent the limit from being exceeded,
2808** the memory is allocated anyway and the current operation proceeds.
2809**
2810** A negative or zero value for N means that there is no soft heap limit and
2811** [sqlite3_release_memory()] will only be called when memory is exhausted.
2812** The default value for the soft heap limit is zero.
2813**
2814** SQLite makes a best effort to honor the soft heap limit. But if it
2815** is unable to reduce memory usage below the soft limit, execution will
2816** continue without error or notification. This is why the limit is
2817** called a "soft" limit. It is advisory only.
2818**
2819** The soft heap limit is implemented using the [sqlite3_memory_alarm()]
2820** interface. Only a single memory alarm is available in the default
2821** implementation. This means that if the application also uses the
2822** memory alarm interface it will interfere with the operation of the
2823** soft heap limit and undefined behavior will result.
2824**
2825** Prior to SQLite version 3.5.0, this routine only constrained the memory
2826** allocated by a single thread - the same thread in which this routine
2827** runs. Beginning with SQLite version 3.5.0, the soft heap limit is
2828** applied to all threads. The value specified for the soft heap limit
2829** is an upper bound on the total memory allocation for all threads. In
2830** version 3.5.0 there is no mechanism for limiting the heap usage for
2831** individual threads.
2832*/
2833void sqlite3_soft_heap_limit(int);
2834
2835/*
2836** CAPI3REF: Extract Metadata About A Column Of A Table
2837**
2838** This routine
2839** returns meta-data about a specific column of a specific database
2840** table accessible using the connection handle passed as the first function
2841** argument.
2842**
2843** The column is identified by the second, third and fourth parameters to
2844** this function. The second parameter is either the name of the database
2845** (i.e. "main", "temp" or an attached database) containing the specified
2846** table or NULL. If it is NULL, then all attached databases are searched
2847** for the table using the same algorithm as the database engine uses to
2848** resolve unqualified table references.
2849**
2850** The third and fourth parameters to this function are the table and column
2851** name of the desired column, respectively. Neither of these parameters
2852** may be NULL.
2853**
2854** Meta information is returned by writing to the memory locations passed as
2855** the 5th and subsequent parameters to this function. Any of these
2856** arguments may be NULL, in which case the corresponding element of meta
2857** information is ommitted.
2858**
2859** <pre>
2860** Parameter Output Type Description
2861** -----------------------------------
2862**
2863** 5th const char* Data type
2864** 6th const char* Name of the default collation sequence
2865** 7th int True if the column has a NOT NULL constraint
2866** 8th int True if the column is part of the PRIMARY KEY
2867** 9th int True if the column is AUTOINCREMENT
2868** </pre>
2869**
2870**
2871** The memory pointed to by the character pointers returned for the
2872** declaration type and collation sequence is valid only until the next
2873** call to any sqlite API function.
2874**
2875** If the specified table is actually a view, then an error is returned.
2876**
2877** If the specified column is "rowid", "oid" or "_rowid_" and an
2878** INTEGER PRIMARY KEY column has been explicitly declared, then the output
2879** parameters are set for the explicitly declared column. If there is no
2880** explicitly declared IPK column, then the output parameters are set as
2881** follows:
2882**
2883** <pre>
2884** data type: "INTEGER"
2885** collation sequence: "BINARY"
2886** not null: 0
2887** primary key: 1
2888** auto increment: 0
2889** </pre>
2890**
2891** This function may load one or more schemas from database files. If an
2892** error occurs during this process, or if the requested table or column
2893** cannot be found, an SQLITE error code is returned and an error message
2894** left in the database handle (to be retrieved using sqlite3_errmsg()).
2895**
2896** This API is only available if the library was compiled with the
2897** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined.
2898*/
2899int sqlite3_table_column_metadata(
2900 sqlite3 *db, /* Connection handle */
2901 const char *zDbName, /* Database name or NULL */
2902 const char *zTableName, /* Table name */
2903 const char *zColumnName, /* Column name */
2904 char const **pzDataType, /* OUTPUT: Declared data type */
2905 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
2906 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
2907 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
2908 int *pAutoinc /* OUTPUT: True if column is auto-increment */
2909);
2910
2911/*
2912** CAPI3REF: Load An Extension
2913**
2914** Attempt to load an SQLite extension library contained in the file
2915** zFile. The entry point is zProc. zProc may be 0 in which case the
2916** name of the entry point defaults to "sqlite3_extension_init".
2917**
2918** Return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
2919**
2920** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
2921** error message text. The calling function should free this memory
2922** by calling [sqlite3_free()].
2923**
2924** Extension loading must be enabled using [sqlite3_enable_load_extension()]
2925** prior to calling this API or an error will be returned.
2926*/
2927int sqlite3_load_extension(
2928 sqlite3 *db, /* Load the extension into this database connection */
2929 const char *zFile, /* Name of the shared library containing extension */
2930 const char *zProc, /* Entry point. Derived from zFile if 0 */
2931 char **pzErrMsg /* Put error message here if not 0 */
2932);
2933
2934/*
2935** CAPI3REF: Enable Or Disable Extension Loading
2936**
2937** So as not to open security holes in older applications that are
2938** unprepared to deal with extension loading, and as a means of disabling
2939** extension loading while evaluating user-entered SQL, the following
2940** API is provided to turn the [sqlite3_load_extension()] mechanism on and
2941** off. It is off by default. See ticket #1863.
2942**
2943** Call this routine with onoff==1 to turn extension loading on
2944** and call it with onoff==0 to turn it back off again.
2945*/
2946int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
2947
2948/*
2949** CAPI3REF: Make Arrangements To Automatically Load An Extension
2950**
2951** Register an extension entry point that is automatically invoked
2952** whenever a new database connection is opened using
2953** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()].
2954**
2955** This API can be invoked at program startup in order to register
2956** one or more statically linked extensions that will be available
2957** to all new database connections.
2958**
2959** Duplicate extensions are detected so calling this routine multiple
2960** times with the same extension is harmless.
2961**
2962** This routine stores a pointer to the extension in an array
2963** that is obtained from malloc(). If you run a memory leak
2964** checker on your program and it reports a leak because of this
2965** array, then invoke [sqlite3_automatic_extension_reset()] prior
2966** to shutdown to free the memory.
2967**
2968** Automatic extensions apply across all threads.
2969**
2970** This interface is experimental and is subject to change or
2971** removal in future releases of SQLite.
2972*/
2973int sqlite3_auto_extension(void *xEntryPoint);
2974
2975
2976/*
2977** CAPI3REF: Reset Automatic Extension Loading
2978**
2979** Disable all previously registered automatic extensions. This
2980** routine undoes the effect of all prior [sqlite3_automatic_extension()]
2981** calls.
2982**
2983** This call disabled automatic extensions in all threads.
2984**
2985** This interface is experimental and is subject to change or
2986** removal in future releases of SQLite.
2987*/
2988void sqlite3_reset_auto_extension(void);
2989
2990
2991/*
2992****** EXPERIMENTAL - subject to change without notice **************
2993**
2994** The interface to the virtual-table mechanism is currently considered
2995** to be experimental. The interface might change in incompatible ways.
2996** If this is a problem for you, do not use the interface at this time.
2997**
2998** When the virtual-table mechanism stablizes, we will declare the
2999** interface fixed, support it indefinitely, and remove this comment.
3000*/
3001
3002/*
3003** Structures used by the virtual table interface
3004*/
3005typedef struct sqlite3_vtab sqlite3_vtab;
3006typedef struct sqlite3_index_info sqlite3_index_info;
3007typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
3008typedef struct sqlite3_module sqlite3_module;
3009
3010/*
3011** A module is a class of virtual tables. Each module is defined
3012** by an instance of the following structure. This structure consists
3013** mostly of methods for the module.
3014*/
3015struct sqlite3_module {
3016 int iVersion;
3017 int (*xCreate)(sqlite3*, void *pAux,
3018 int argc, const char *const*argv,
3019 sqlite3_vtab **ppVTab, char**);
3020 int (*xConnect)(sqlite3*, void *pAux,
3021 int argc, const char *const*argv,
3022 sqlite3_vtab **ppVTab, char**);
3023 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
3024 int (*xDisconnect)(sqlite3_vtab *pVTab);
3025 int (*xDestroy)(sqlite3_vtab *pVTab);
3026 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
3027 int (*xClose)(sqlite3_vtab_cursor*);
3028 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
3029 int argc, sqlite3_value **argv);
3030 int (*xNext)(sqlite3_vtab_cursor*);
3031 int (*xEof)(sqlite3_vtab_cursor*);
3032 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
3033 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
3034 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
3035 int (*xBegin)(sqlite3_vtab *pVTab);
3036 int (*xSync)(sqlite3_vtab *pVTab);
3037 int (*xCommit)(sqlite3_vtab *pVTab);
3038 int (*xRollback)(sqlite3_vtab *pVTab);
3039 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
3040 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
3041 void **ppArg);
3042
3043 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
3044};
3045
3046/*
3047** The sqlite3_index_info structure and its substructures is used to
3048** pass information into and receive the reply from the xBestIndex
3049** method of an sqlite3_module. The fields under **Inputs** are the
3050** inputs to xBestIndex and are read-only. xBestIndex inserts its
3051** results into the **Outputs** fields.
3052**
3053** The aConstraint[] array records WHERE clause constraints of the
3054** form:
3055**
3056** column OP expr
3057**
3058** Where OP is =, <, <=, >, or >=. The particular operator is stored
3059** in aConstraint[].op. The index of the column is stored in
3060** aConstraint[].iColumn. aConstraint[].usable is TRUE if the
3061** expr on the right-hand side can be evaluated (and thus the constraint
3062** is usable) and false if it cannot.
3063**
3064** The optimizer automatically inverts terms of the form "expr OP column"
3065** and makes other simplifications to the WHERE clause in an attempt to
3066** get as many WHERE clause terms into the form shown above as possible.
3067** The aConstraint[] array only reports WHERE clause terms in the correct
3068** form that refer to the particular virtual table being queried.
3069**
3070** Information about the ORDER BY clause is stored in aOrderBy[].
3071** Each term of aOrderBy records a column of the ORDER BY clause.
3072**
3073** The xBestIndex method must fill aConstraintUsage[] with information
3074** about what parameters to pass to xFilter. If argvIndex>0 then
3075** the right-hand side of the corresponding aConstraint[] is evaluated
3076** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit
3077** is true, then the constraint is assumed to be fully handled by the
3078** virtual table and is not checked again by SQLite.
3079**
3080** The idxNum and idxPtr values are recorded and passed into xFilter.
3081** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true.
3082**
3083** The orderByConsumed means that output from xFilter will occur in
3084** the correct order to satisfy the ORDER BY clause so that no separate
3085** sorting step is required.
3086**
3087** The estimatedCost value is an estimate of the cost of doing the
3088** particular lookup. A full scan of a table with N entries should have
3089** a cost of N. A binary search of a table of N entries should have a
3090** cost of approximately log(N).
3091*/
3092struct sqlite3_index_info {
3093 /* Inputs */
3094 int nConstraint; /* Number of entries in aConstraint */
3095 struct sqlite3_index_constraint {
3096 int iColumn; /* Column on left-hand side of constraint */
3097 unsigned char op; /* Constraint operator */
3098 unsigned char usable; /* True if this constraint is usable */
3099 int iTermOffset; /* Used internally - xBestIndex should ignore */
3100 } *aConstraint; /* Table of WHERE clause constraints */
3101 int nOrderBy; /* Number of terms in the ORDER BY clause */
3102 struct sqlite3_index_orderby {
3103 int iColumn; /* Column number */
3104 unsigned char desc; /* True for DESC. False for ASC. */
3105 } *aOrderBy; /* The ORDER BY clause */
3106
3107 /* Outputs */
3108 struct sqlite3_index_constraint_usage {
3109 int argvIndex; /* if >0, constraint is part of argv to xFilter */
3110 unsigned char omit; /* Do not code a test for this constraint */
3111 } *aConstraintUsage;
3112 int idxNum; /* Number used to identify the index */
3113 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
3114 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
3115 int orderByConsumed; /* True if output is already ordered */
3116 double estimatedCost; /* Estimated cost of using this index */
3117};
3118#define SQLITE_INDEX_CONSTRAINT_EQ 2
3119#define SQLITE_INDEX_CONSTRAINT_GT 4
3120#define SQLITE_INDEX_CONSTRAINT_LE 8
3121#define SQLITE_INDEX_CONSTRAINT_LT 16
3122#define SQLITE_INDEX_CONSTRAINT_GE 32
3123#define SQLITE_INDEX_CONSTRAINT_MATCH 64
3124
3125/*
3126** This routine is used to register a new module name with an SQLite
3127** connection. Module names must be registered before creating new
3128** virtual tables on the module, or before using preexisting virtual
3129** tables of the module.
3130*/
3131int sqlite3_create_module(
3132 sqlite3 *db, /* SQLite connection to register module with */
3133 const char *zName, /* Name of the module */
3134 const sqlite3_module *, /* Methods for the module */
3135 void * /* Client data for xCreate/xConnect */
3136);
3137
3138/*
3139** This routine is identical to the sqlite3_create_module() method above,
3140** except that it allows a destructor function to be specified. It is
3141** even more experimental than the rest of the virtual tables API.
3142*/
3143int sqlite3_create_module_v2(
3144 sqlite3 *db, /* SQLite connection to register module with */
3145 const char *zName, /* Name of the module */
3146 const sqlite3_module *, /* Methods for the module */
3147 void *, /* Client data for xCreate/xConnect */
3148 void(*xDestroy)(void*) /* Module destructor function */
3149);
3150
3151/*
3152** Every module implementation uses a subclass of the following structure
3153** to describe a particular instance of the module. Each subclass will
3154** be tailored to the specific needs of the module implementation. The
3155** purpose of this superclass is to define certain fields that are common
3156** to all module implementations.
3157**
3158** Virtual tables methods can set an error message by assigning a
3159** string obtained from sqlite3_mprintf() to zErrMsg. The method should
3160** take care that any prior string is freed by a call to sqlite3_free()
3161** prior to assigning a new string to zErrMsg. After the error message
3162** is delivered up to the client application, the string will be automatically
3163** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note
3164** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field
3165** since virtual tables are commonly implemented in loadable extensions which
3166** do not have access to sqlite3MPrintf() or sqlite3Free().
3167*/
3168struct sqlite3_vtab {
3169 const sqlite3_module *pModule; /* The module for this virtual table */
3170 int nRef; /* Used internally */
3171 char *zErrMsg; /* Error message from sqlite3_mprintf() */
3172 /* Virtual table implementations will typically add additional fields */
3173};
3174
3175/* Every module implementation uses a subclass of the following structure
3176** to describe cursors that point into the virtual table and are used
3177** to loop through the virtual table. Cursors are created using the
3178** xOpen method of the module. Each module implementation will define
3179** the content of a cursor structure to suit its own needs.
3180**
3181** This superclass exists in order to define fields of the cursor that
3182** are common to all implementations.
3183*/
3184struct sqlite3_vtab_cursor {
3185 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
3186 /* Virtual table implementations will typically add additional fields */
3187};
3188
3189/*
3190** The xCreate and xConnect methods of a module use the following API
3191** to declare the format (the names and datatypes of the columns) of
3192** the virtual tables they implement.
3193*/
3194int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable);
3195
3196/*
3197** Virtual tables can provide alternative implementations of functions
3198** using the xFindFunction method. But global versions of those functions
3199** must exist in order to be overloaded.
3200**
3201** This API makes sure a global version of a function with a particular
3202** name and number of parameters exists. If no such function exists
3203** before this API is called, a new function is created. The implementation
3204** of the new function always causes an exception to be thrown. So
3205** the new function is not good for anything by itself. Its only
3206** purpose is to be a place-holder function that can be overloaded
3207** by virtual tables.
3208**
3209** This API should be considered part of the virtual table interface,
3210** which is experimental and subject to change.
3211*/
3212int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
3213
3214/*
3215** The interface to the virtual-table mechanism defined above (back up
3216** to a comment remarkably similar to this one) is currently considered
3217** to be experimental. The interface might change in incompatible ways.
3218** If this is a problem for you, do not use the interface at this time.
3219**
3220** When the virtual-table mechanism stabilizes, we will declare the
3221** interface fixed, support it indefinitely, and remove this comment.
3222**
3223****** EXPERIMENTAL - subject to change without notice **************
3224*/
3225
3226/*
3227** CAPI3REF: A Handle To An Open BLOB
3228**
3229** An instance of the following opaque structure is used to
3230** represent an blob-handle. A blob-handle is created by
3231** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()].
3232** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
3233** can be used to read or write small subsections of the blob.
3234** The [sqlite3_blob_bytes()] interface returns the size of the
3235** blob in bytes.
3236*/
3237typedef struct sqlite3_blob sqlite3_blob;
3238
3239/*
3240** CAPI3REF: Open A BLOB For Incremental I/O
3241**
3242** Open a handle to the blob located in row iRow,, column zColumn,
3243** table zTable in database zDb. i.e. the same blob that would
3244** be selected by:
3245**
3246** <pre>
3247** SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
3248** </pre>
3249**
3250** If the flags parameter is non-zero, the blob is opened for
3251** read and write access. If it is zero, the blob is opened for read
3252** access.
3253**
3254** On success, [SQLITE_OK] is returned and the new
3255** [sqlite3_blob | blob handle] is written to *ppBlob.
3256** Otherwise an error code is returned and
3257** any value written to *ppBlob should not be used by the caller.
3258** This function sets the database-handle error code and message
3259** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()].
3260*/
3261int sqlite3_blob_open(
3262 sqlite3*,
3263 const char *zDb,
3264 const char *zTable,
3265 const char *zColumn,
3266 sqlite3_int64 iRow,
3267 int flags,
3268 sqlite3_blob **ppBlob
3269);
3270
3271/*
3272** CAPI3REF: Close A BLOB Handle
3273**
3274** Close an open [sqlite3_blob | blob handle].
3275*/
3276int sqlite3_blob_close(sqlite3_blob *);
3277
3278/*
3279** CAPI3REF: Return The Size Of An Open BLOB
3280**
3281** Return the size in bytes of the blob accessible via the open
3282** [sqlite3_blob | blob-handle] passed as an argument.
3283*/
3284int sqlite3_blob_bytes(sqlite3_blob *);
3285
3286/*
3287** CAPI3REF: Read Data From A BLOB Incrementally
3288**
3289** This function is used to read data from an open
3290** [sqlite3_blob | blob-handle] into a caller supplied buffer.
3291** n bytes of data are copied into buffer
3292** z from the open blob, starting at offset iOffset.
3293**
3294** On success, SQLITE_OK is returned. Otherwise, an
3295** [SQLITE_ERROR | SQLite error code] or an
3296** [SQLITE_IOERR_READ | extended error code] is returned.
3297*/
3298int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset);
3299
3300/*
3301** CAPI3REF: Write Data Into A BLOB Incrementally
3302**
3303** This function is used to write data into an open
3304** [sqlite3_blob | blob-handle] from a user supplied buffer.
3305** n bytes of data are copied from the buffer
3306** pointed to by z into the open blob, starting at offset iOffset.
3307**
3308** If the [sqlite3_blob | blob-handle] passed as the first argument
3309** was not opened for writing (the flags parameter to [sqlite3_blob_open()]
3310*** was zero), this function returns [SQLITE_READONLY].
3311**
3312** This function may only modify the contents of the blob, it is
3313** not possible to increase the size of a blob using this API. If
3314** offset iOffset is less than n bytes from the end of the blob,
3315** [SQLITE_ERROR] is returned and no data is written.
3316**
3317** On success, SQLITE_OK is returned. Otherwise, an
3318** [SQLITE_ERROR | SQLite error code] or an
3319** [SQLITE_IOERR_READ | extended error code] is returned.
3320*/
3321int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
3322
3323/*
3324** CAPI3REF: Virtual File System Objects
3325**
3326** A virtual filesystem (VFS) is an [sqlite3_vfs] object
3327** that SQLite uses to interact
3328** with the underlying operating system. Most builds come with a
3329** single default VFS that is appropriate for the host computer.
3330** New VFSes can be registered and existing VFSes can be unregistered.
3331** The following interfaces are provided.
3332**
3333** The sqlite3_vfs_find() interface returns a pointer to a VFS given its
3334** name. Names are case sensitive. If there is no match, a NULL
3335** pointer is returned. If zVfsName is NULL then the default
3336** VFS is returned.
3337**
3338** New VFSes are registered with sqlite3_vfs_register(). Each
3339** new VFS becomes the default VFS if the makeDflt flag is set.
3340** The same VFS can be registered multiple times without injury.
3341** To make an existing VFS into the default VFS, register it again
3342** with the makeDflt flag set. If two different VFSes with the
3343** same name are registered, the behavior is undefined. If a
3344** VFS is registered with a name that is NULL or an empty string,
3345** then the behavior is undefined.
3346**
3347** Unregister a VFS with the sqlite3_vfs_unregister() interface.
3348** If the default VFS is unregistered, another VFS is chosen as
3349** the default. The choice for the new VFS is arbitrary.
3350*/
3351sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
3352int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
3353int sqlite3_vfs_unregister(sqlite3_vfs*);
3354
3355/*
3356** CAPI3REF: Mutexes
3357**
3358** The SQLite core uses these routines for thread
3359** synchronization. Though they are intended for internal
3360** use by SQLite, code that links against SQLite is
3361** permitted to use any of these routines.
3362**
3363** The SQLite source code contains multiple implementations
3364** of these mutex routines. An appropriate implementation
3365** is selected automatically at compile-time. The following
3366** implementations are available in the SQLite core:
3367**
3368** <ul>
3369** <li> SQLITE_MUTEX_OS2
3370** <li> SQLITE_MUTEX_PTHREAD
3371** <li> SQLITE_MUTEX_W32
3372** <li> SQLITE_MUTEX_NOOP
3373** </ul>
3374**
3375** The SQLITE_MUTEX_NOOP implementation is a set of routines
3376** that does no real locking and is appropriate for use in
3377** a single-threaded application. The SQLITE_MUTEX_OS2,
3378** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
3379** are appropriate for use on os/2, unix, and windows.
3380**
3381** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
3382** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
3383** implementation is included with the library. The
3384** mutex interface routines defined here become external
3385** references in the SQLite library for which implementations
3386** must be provided by the application. This facility allows an
3387** application that links against SQLite to provide its own mutex
3388** implementation without having to modify the SQLite core.
3389**
3390** The sqlite3_mutex_alloc() routine allocates a new
3391** mutex and returns a pointer to it. If it returns NULL
3392** that means that a mutex could not be allocated. SQLite
3393** will unwind its stack and return an error. The argument
3394** to sqlite3_mutex_alloc() is one of these integer constants:
3395**
3396** <ul>
3397** <li> SQLITE_MUTEX_FAST
3398** <li> SQLITE_MUTEX_RECURSIVE
3399** <li> SQLITE_MUTEX_STATIC_MASTER
3400** <li> SQLITE_MUTEX_STATIC_MEM
3401** <li> SQLITE_MUTEX_STATIC_MEM2
3402** <li> SQLITE_MUTEX_STATIC_PRNG
3403** <li> SQLITE_MUTEX_STATIC_LRU
3404** </ul>
3405**
3406** The first two constants cause sqlite3_mutex_alloc() to create
3407** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
3408** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
3409** The mutex implementation does not need to make a distinction
3410** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
3411** not want to. But SQLite will only request a recursive mutex in
3412** cases where it really needs one. If a faster non-recursive mutex
3413** implementation is available on the host platform, the mutex subsystem
3414** might return such a mutex in response to SQLITE_MUTEX_FAST.
3415**
3416** The other allowed parameters to sqlite3_mutex_alloc() each return
3417** a pointer to a static preexisting mutex. Four static mutexes are
3418** used by the current version of SQLite. Future versions of SQLite
3419** may add additional static mutexes. Static mutexes are for internal
3420** use by SQLite only. Applications that use SQLite mutexes should
3421** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
3422** SQLITE_MUTEX_RECURSIVE.
3423**
3424** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
3425** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
3426** returns a different mutex on every call. But for the static
3427** mutex types, the same mutex is returned on every call that has
3428** the same type number.
3429**
3430** The sqlite3_mutex_free() routine deallocates a previously
3431** allocated dynamic mutex. SQLite is careful to deallocate every
3432** dynamic mutex that it allocates. The dynamic mutexes must not be in
3433** use when they are deallocated. Attempting to deallocate a static
3434** mutex results in undefined behavior. SQLite never deallocates
3435** a static mutex.
3436**
3437** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
3438** to enter a mutex. If another thread is already within the mutex,
3439** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
3440** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
3441** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
3442** be entered multiple times by the same thread. In such cases the,
3443** mutex must be exited an equal number of times before another thread
3444** can enter. If the same thread tries to enter any other kind of mutex
3445** more than once, the behavior is undefined. SQLite will never exhibit
3446** such behavior in its own use of mutexes.
3447**
3448** Some systems (ex: windows95) do not the operation implemented by
3449** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will
3450** always return SQLITE_BUSY. The SQLite core only ever uses
3451** sqlite3_mutex_try() as an optimization so this is acceptable behavior.
3452**
3453** The sqlite3_mutex_leave() routine exits a mutex that was
3454** previously entered by the same thread. The behavior
3455** is undefined if the mutex is not currently entered by the
3456** calling thread or is not currently allocated. SQLite will
3457** never do either.
3458**
3459** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
3460*/
3461sqlite3_mutex *sqlite3_mutex_alloc(int);
3462void sqlite3_mutex_free(sqlite3_mutex*);
3463void sqlite3_mutex_enter(sqlite3_mutex*);
3464int sqlite3_mutex_try(sqlite3_mutex*);
3465void sqlite3_mutex_leave(sqlite3_mutex*);
3466
3467/*
3468** CAPI3REF: Mutex Verifcation Routines
3469**
3470** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
3471** are intended for use inside assert() statements. The SQLite core
3472** never uses these routines except inside an assert() and applications
3473** are advised to follow the lead of the core. The core only
3474** provides implementations for these routines when it is compiled
3475** with the SQLITE_DEBUG flag. External mutex implementations
3476** are only required to provide these routines if SQLITE_DEBUG is
3477** defined and if NDEBUG is not defined.
3478**
3479** These routines should return true if the mutex in their argument
3480** is held or not held, respectively, by the calling thread.
3481**
3482** The implementation is not required to provided versions of these
3483** routines that actually work.
3484** If the implementation does not provide working
3485** versions of these routines, it should at least provide stubs
3486** that always return true so that one does not get spurious
3487** assertion failures.
3488**
3489** If the argument to sqlite3_mutex_held() is a NULL pointer then
3490** the routine should return 1. This seems counter-intuitive since
3491** clearly the mutex cannot be held if it does not exist. But the
3492** the reason the mutex does not exist is because the build is not
3493** using mutexes. And we do not want the assert() containing the
3494** call to sqlite3_mutex_held() to fail, so a non-zero return is
3495** the appropriate thing to do. The sqlite3_mutex_notheld()
3496** interface should also return 1 when given a NULL pointer.
3497*/
3498int sqlite3_mutex_held(sqlite3_mutex*);
3499int sqlite3_mutex_notheld(sqlite3_mutex*);
3500
3501/*
3502** CAPI3REF: Mutex Types
3503**
3504** The [sqlite3_mutex_alloc()] interface takes a single argument
3505** which is one of these integer constants.
3506*/
3507#define SQLITE_MUTEX_FAST 0
3508#define SQLITE_MUTEX_RECURSIVE 1
3509#define SQLITE_MUTEX_STATIC_MASTER 2
3510#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
3511#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */
3512#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
3513#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
3514
3515/*
3516** CAPI3REF: Low-Level Control Of Database Files
3517**
3518** The [sqlite3_file_control()] interface makes a direct call to the
3519** xFileControl method for the [sqlite3_io_methods] object associated
3520** with a particular database identified by the second argument. The
3521** name of the database is the name assigned to the database by the
3522** <a href="lang_attach.html">ATTACH</a> SQL command that opened the
3523** database. To control the main database file, use the name "main"
3524** or a NULL pointer. The third and fourth parameters to this routine
3525** are passed directly through to the second and third parameters of
3526** the xFileControl method. The return value of the xFileControl
3527** method becomes the return value of this routine.
3528**
3529** If the second parameter (zDbName) does not match the name of any
3530** open database file, then SQLITE_ERROR is returned. This error
3531** code is not remembered and will not be recalled by [sqlite3_errcode()]
3532** or [sqlite3_errmsg()]. The underlying xFileControl method might
3533** also return SQLITE_ERROR. There is no way to distinguish between
3534** an incorrect zDbName and an SQLITE_ERROR return from the underlying
3535** xFileControl method.
3536**
3537** See also: [SQLITE_FCNTL_LOCKSTATE]
3538*/
3539int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
3540
3541/*
3542** Undo the hack that converts floating point types to integer for
3543** builds on processors without floating point support.
3544*/
3545#ifdef SQLITE_OMIT_FLOATING_POINT
3546# undef double
3547#endif
3548
3549#ifdef __cplusplus
3550} /* End of the 'extern "C"' block */
3551#endif
3552#endif
diff --git a/libraries/sqlite/win32/sqlite3ext.h b/libraries/sqlite/win32/sqlite3ext.h
new file mode 100755
index 0000000..5d4c2de
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite3ext.h
@@ -0,0 +1,350 @@
1/*
2** 2006 June 7
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.
10**
11*************************************************************************
12** This header file defines the SQLite interface for use by
13** shared libraries that want to be imported as extensions into
14** an SQLite instance. Shared libraries that intend to be loaded
15** as extensions by SQLite should #include this file instead of
16** sqlite3.h.
17**
18** @(#) $Id: sqlite3ext.h,v 1.17 2007/08/31 16:11:36 drh Exp $
19*/
20#ifndef _SQLITE3EXT_H_
21#define _SQLITE3EXT_H_
22#include "sqlite3.h"
23
24typedef struct sqlite3_api_routines sqlite3_api_routines;
25
26/*
27** The following structure hold pointers to all of the SQLite API
28** routines.
29**
30** WARNING: In order to maintain backwards compatibility, add new
31** interfaces to the end of this structure only. If you insert new
32** interfaces in the middle of this structure, then older different
33** versions of SQLite will not be able to load each others shared
34** libraries!
35*/
36struct sqlite3_api_routines {
37 void * (*aggregate_context)(sqlite3_context*,int nBytes);
38 int (*aggregate_count)(sqlite3_context*);
39 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
40 int (*bind_double)(sqlite3_stmt*,int,double);
41 int (*bind_int)(sqlite3_stmt*,int,int);
42 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
43 int (*bind_null)(sqlite3_stmt*,int);
44 int (*bind_parameter_count)(sqlite3_stmt*);
45 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
46 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
47 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
48 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
49 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
50 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
51 int (*busy_timeout)(sqlite3*,int ms);
52 int (*changes)(sqlite3*);
53 int (*close)(sqlite3*);
54 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
55 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
56 const void * (*column_blob)(sqlite3_stmt*,int iCol);
57 int (*column_bytes)(sqlite3_stmt*,int iCol);
58 int (*column_bytes16)(sqlite3_stmt*,int iCol);
59 int (*column_count)(sqlite3_stmt*pStmt);
60 const char * (*column_database_name)(sqlite3_stmt*,int);
61 const void * (*column_database_name16)(sqlite3_stmt*,int);
62 const char * (*column_decltype)(sqlite3_stmt*,int i);
63 const void * (*column_decltype16)(sqlite3_stmt*,int);
64 double (*column_double)(sqlite3_stmt*,int iCol);
65 int (*column_int)(sqlite3_stmt*,int iCol);
66 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
67 const char * (*column_name)(sqlite3_stmt*,int);
68 const void * (*column_name16)(sqlite3_stmt*,int);
69 const char * (*column_origin_name)(sqlite3_stmt*,int);
70 const void * (*column_origin_name16)(sqlite3_stmt*,int);
71 const char * (*column_table_name)(sqlite3_stmt*,int);
72 const void * (*column_table_name16)(sqlite3_stmt*,int);
73 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
74 const void * (*column_text16)(sqlite3_stmt*,int iCol);
75 int (*column_type)(sqlite3_stmt*,int iCol);
76 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
77 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
78 int (*complete)(const char*sql);
79 int (*complete16)(const void*sql);
80 int (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
81 int (*create_collation16)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
82 int (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
83 int (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
84 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
85 int (*data_count)(sqlite3_stmt*pStmt);
86 sqlite3 * (*db_handle)(sqlite3_stmt*);
87 int (*declare_vtab)(sqlite3*,const char*);
88 int (*enable_shared_cache)(int);
89 int (*errcode)(sqlite3*db);
90 const char * (*errmsg)(sqlite3*);
91 const void * (*errmsg16)(sqlite3*);
92 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
93 int (*expired)(sqlite3_stmt*);
94 int (*finalize)(sqlite3_stmt*pStmt);
95 void (*free)(void*);
96 void (*free_table)(char**result);
97 int (*get_autocommit)(sqlite3*);
98 void * (*get_auxdata)(sqlite3_context*,int);
99 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
100 int (*global_recover)(void);
101 void (*interruptx)(sqlite3*);
102 sqlite_int64 (*last_insert_rowid)(sqlite3*);
103 const char * (*libversion)(void);
104 int (*libversion_number)(void);
105 void *(*malloc)(int);
106 char * (*mprintf)(const char*,...);
107 int (*open)(const char*,sqlite3**);
108 int (*open16)(const void*,sqlite3**);
109 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
110 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
111 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
112 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
113 void *(*realloc)(void*,int);
114 int (*reset)(sqlite3_stmt*pStmt);
115 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
116 void (*result_double)(sqlite3_context*,double);
117 void (*result_error)(sqlite3_context*,const char*,int);
118 void (*result_error16)(sqlite3_context*,const void*,int);
119 void (*result_int)(sqlite3_context*,int);
120 void (*result_int64)(sqlite3_context*,sqlite_int64);
121 void (*result_null)(sqlite3_context*);
122 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
123 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
124 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
125 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
126 void (*result_value)(sqlite3_context*,sqlite3_value*);
127 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
128 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
129 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
130 char * (*snprintf)(int,char*,const char*,...);
131 int (*step)(sqlite3_stmt*);
132 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
133 void (*thread_cleanup)(void);
134 int (*total_changes)(sqlite3*);
135 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
136 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
137 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
138 void * (*user_data)(sqlite3_context*);
139 const void * (*value_blob)(sqlite3_value*);
140 int (*value_bytes)(sqlite3_value*);
141 int (*value_bytes16)(sqlite3_value*);
142 double (*value_double)(sqlite3_value*);
143 int (*value_int)(sqlite3_value*);
144 sqlite_int64 (*value_int64)(sqlite3_value*);
145 int (*value_numeric_type)(sqlite3_value*);
146 const unsigned char * (*value_text)(sqlite3_value*);
147 const void * (*value_text16)(sqlite3_value*);
148 const void * (*value_text16be)(sqlite3_value*);
149 const void * (*value_text16le)(sqlite3_value*);
150 int (*value_type)(sqlite3_value*);
151 char *(*vmprintf)(const char*,va_list);
152 /* Added ??? */
153 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
154 /* Added by 3.3.13 */
155 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
156 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
157 int (*clear_bindings)(sqlite3_stmt*);
158 /* Added by 3.4.1 */
159 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
160 /* Added by 3.5.0 */
161 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
162 int (*blob_bytes)(sqlite3_blob*);
163 int (*blob_close)(sqlite3_blob*);
164 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
165 int (*blob_read)(sqlite3_blob*,void*,int,int);
166 int (*blob_write)(sqlite3_blob*,const void*,int,int);
167 int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
168 int (*file_control)(sqlite3*,const char*,int,void*);
169 sqlite3_int64 (*memory_highwater)(int);
170 sqlite3_int64 (*memory_used)(void);
171 sqlite3_mutex *(*mutex_alloc)(int);
172 void (*mutex_enter)(sqlite3_mutex*);
173 void (*mutex_free)(sqlite3_mutex*);
174 void (*mutex_leave)(sqlite3_mutex*);
175 int (*mutex_try)(sqlite3_mutex*);
176 int (*open_v2)(const char*,sqlite3**,int,const char*);
177 int (*release_memory)(int);
178 void (*result_error_nomem)(sqlite3_context*);
179 void (*result_error_toobig)(sqlite3_context*);
180 int (*sleep)(int);
181 void (*soft_heap_limit)(int);
182 sqlite3_vfs *(*vfs_find)(const char*);
183 int (*vfs_register)(sqlite3_vfs*,int);
184 int (*vfs_unregister)(sqlite3_vfs*);
185};
186
187/*
188** The following macros redefine the API routines so that they are
189** redirected throught the global sqlite3_api structure.
190**
191** This header file is also used by the loadext.c source file
192** (part of the main SQLite library - not an extension) so that
193** it can get access to the sqlite3_api_routines structure
194** definition. But the main library does not want to redefine
195** the API. So the redefinition macros are only valid if the
196** SQLITE_CORE macros is undefined.
197*/
198#ifndef SQLITE_CORE
199#define sqlite3_aggregate_context sqlite3_api->aggregate_context
200#define sqlite3_aggregate_count sqlite3_api->aggregate_count
201#define sqlite3_bind_blob sqlite3_api->bind_blob
202#define sqlite3_bind_double sqlite3_api->bind_double
203#define sqlite3_bind_int sqlite3_api->bind_int
204#define sqlite3_bind_int64 sqlite3_api->bind_int64
205#define sqlite3_bind_null sqlite3_api->bind_null
206#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
207#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
208#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
209#define sqlite3_bind_text sqlite3_api->bind_text
210#define sqlite3_bind_text16 sqlite3_api->bind_text16
211#define sqlite3_bind_value sqlite3_api->bind_value
212#define sqlite3_busy_handler sqlite3_api->busy_handler
213#define sqlite3_busy_timeout sqlite3_api->busy_timeout
214#define sqlite3_changes sqlite3_api->changes
215#define sqlite3_close sqlite3_api->close
216#define sqlite3_collation_needed sqlite3_api->collation_needed
217#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
218#define sqlite3_column_blob sqlite3_api->column_blob
219#define sqlite3_column_bytes sqlite3_api->column_bytes
220#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
221#define sqlite3_column_count sqlite3_api->column_count
222#define sqlite3_column_database_name sqlite3_api->column_database_name
223#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
224#define sqlite3_column_decltype sqlite3_api->column_decltype
225#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
226#define sqlite3_column_double sqlite3_api->column_double
227#define sqlite3_column_int sqlite3_api->column_int
228#define sqlite3_column_int64 sqlite3_api->column_int64
229#define sqlite3_column_name sqlite3_api->column_name
230#define sqlite3_column_name16 sqlite3_api->column_name16
231#define sqlite3_column_origin_name sqlite3_api->column_origin_name
232#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
233#define sqlite3_column_table_name sqlite3_api->column_table_name
234#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
235#define sqlite3_column_text sqlite3_api->column_text
236#define sqlite3_column_text16 sqlite3_api->column_text16
237#define sqlite3_column_type sqlite3_api->column_type
238#define sqlite3_column_value sqlite3_api->column_value
239#define sqlite3_commit_hook sqlite3_api->commit_hook
240#define sqlite3_complete sqlite3_api->complete
241#define sqlite3_complete16 sqlite3_api->complete16
242#define sqlite3_create_collation sqlite3_api->create_collation
243#define sqlite3_create_collation16 sqlite3_api->create_collation16
244#define sqlite3_create_function sqlite3_api->create_function
245#define sqlite3_create_function16 sqlite3_api->create_function16
246#define sqlite3_create_module sqlite3_api->create_module
247#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
248#define sqlite3_data_count sqlite3_api->data_count
249#define sqlite3_db_handle sqlite3_api->db_handle
250#define sqlite3_declare_vtab sqlite3_api->declare_vtab
251#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
252#define sqlite3_errcode sqlite3_api->errcode
253#define sqlite3_errmsg sqlite3_api->errmsg
254#define sqlite3_errmsg16 sqlite3_api->errmsg16
255#define sqlite3_exec sqlite3_api->exec
256#define sqlite3_expired sqlite3_api->expired
257#define sqlite3_finalize sqlite3_api->finalize
258#define sqlite3_free sqlite3_api->free
259#define sqlite3_free_table sqlite3_api->free_table
260#define sqlite3_get_autocommit sqlite3_api->get_autocommit
261#define sqlite3_get_auxdata sqlite3_api->get_auxdata
262#define sqlite3_get_table sqlite3_api->get_table
263#define sqlite3_global_recover sqlite3_api->global_recover
264#define sqlite3_interrupt sqlite3_api->interruptx
265#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
266#define sqlite3_libversion sqlite3_api->libversion
267#define sqlite3_libversion_number sqlite3_api->libversion_number
268#define sqlite3_malloc sqlite3_api->malloc
269#define sqlite3_mprintf sqlite3_api->mprintf
270#define sqlite3_open sqlite3_api->open
271#define sqlite3_open16 sqlite3_api->open16
272#define sqlite3_prepare sqlite3_api->prepare
273#define sqlite3_prepare16 sqlite3_api->prepare16
274#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
275#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
276#define sqlite3_profile sqlite3_api->profile
277#define sqlite3_progress_handler sqlite3_api->progress_handler
278#define sqlite3_realloc sqlite3_api->realloc
279#define sqlite3_reset sqlite3_api->reset
280#define sqlite3_result_blob sqlite3_api->result_blob
281#define sqlite3_result_double sqlite3_api->result_double
282#define sqlite3_result_error sqlite3_api->result_error
283#define sqlite3_result_error16 sqlite3_api->result_error16
284#define sqlite3_result_int sqlite3_api->result_int
285#define sqlite3_result_int64 sqlite3_api->result_int64
286#define sqlite3_result_null sqlite3_api->result_null
287#define sqlite3_result_text sqlite3_api->result_text
288#define sqlite3_result_text16 sqlite3_api->result_text16
289#define sqlite3_result_text16be sqlite3_api->result_text16be
290#define sqlite3_result_text16le sqlite3_api->result_text16le
291#define sqlite3_result_value sqlite3_api->result_value
292#define sqlite3_rollback_hook sqlite3_api->rollback_hook
293#define sqlite3_set_authorizer sqlite3_api->set_authorizer
294#define sqlite3_set_auxdata sqlite3_api->set_auxdata
295#define sqlite3_snprintf sqlite3_api->snprintf
296#define sqlite3_step sqlite3_api->step
297#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
298#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
299#define sqlite3_total_changes sqlite3_api->total_changes
300#define sqlite3_trace sqlite3_api->trace
301#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
302#define sqlite3_update_hook sqlite3_api->update_hook
303#define sqlite3_user_data sqlite3_api->user_data
304#define sqlite3_value_blob sqlite3_api->value_blob
305#define sqlite3_value_bytes sqlite3_api->value_bytes
306#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
307#define sqlite3_value_double sqlite3_api->value_double
308#define sqlite3_value_int sqlite3_api->value_int
309#define sqlite3_value_int64 sqlite3_api->value_int64
310#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
311#define sqlite3_value_text sqlite3_api->value_text
312#define sqlite3_value_text16 sqlite3_api->value_text16
313#define sqlite3_value_text16be sqlite3_api->value_text16be
314#define sqlite3_value_text16le sqlite3_api->value_text16le
315#define sqlite3_value_type sqlite3_api->value_type
316#define sqlite3_vmprintf sqlite3_api->vmprintf
317#define sqlite3_overload_function sqlite3_api->overload_function
318#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
319#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
320#define sqlite3_clear_bindings sqlite3_api->clear_bindings
321#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
322#define sqlite3_blob_bytes sqlite3_api->blob_bytes
323#define sqlite3_blob_close sqlite3_api->blob_close
324#define sqlite3_blob_open sqlite3_api->blob_open
325#define sqlite3_blob_read sqlite3_api->blob_read
326#define sqlite3_blob_write sqlite3_api->blob_write
327#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
328#define sqlite3_file_control sqlite3_api->file_control
329#define sqlite3_memory_highwater sqlite3_api->memory_highwater
330#define sqlite3_memory_used sqlite3_api->memory_used
331#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
332#define sqlite3_mutex_enter sqlite3_api->mutex_enter
333#define sqlite3_mutex_free sqlite3_api->mutex_free
334#define sqlite3_mutex_leave sqlite3_api->mutex_leave
335#define sqlite3_mutex_try sqlite3_api->mutex_try
336#define sqlite3_open_v2 sqlite3_api->open_v2
337#define sqlite3_release_memory sqlite3_api->release_memory
338#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
339#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
340#define sqlite3_sleep sqlite3_api->sleep
341#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
342#define sqlite3_vfs_find sqlite3_api->vfs_find
343#define sqlite3_vfs_register sqlite3_api->vfs_register
344#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
345#endif /* SQLITE_CORE */
346
347#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api;
348#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
349
350#endif /* _SQLITE3EXT_H_ */
diff --git a/libraries/sqlite/win32/sqliteInt.h b/libraries/sqlite/win32/sqliteInt.h
new file mode 100755
index 0000000..2170e53
--- /dev/null
+++ b/libraries/sqlite/win32/sqliteInt.h
@@ -0,0 +1,1972 @@
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.
10**
11*************************************************************************
12** Internal interface definitions for SQLite.
13**
14** @(#) $Id: sqliteInt.h,v 1.613 2007/10/03 08:46:45 danielk1977 Exp $
15*/
16#ifndef _SQLITEINT_H_
17#define _SQLITEINT_H_
18#include "sqliteLimit.h"
19
20/*
21** For testing purposes, the various size limit constants are really
22** variables that we can modify in the testfixture.
23*/
24#ifdef SQLITE_TEST
25 #undef SQLITE_MAX_LENGTH
26 #undef SQLITE_MAX_COLUMN
27 #undef SQLITE_MAX_SQL_LENGTH
28 #undef SQLITE_MAX_EXPR_DEPTH
29 #undef SQLITE_MAX_COMPOUND_SELECT
30 #undef SQLITE_MAX_VDBE_OP
31 #undef SQLITE_MAX_FUNCTION_ARG
32 #undef SQLITE_MAX_VARIABLE_NUMBER
33 #undef SQLITE_MAX_PAGE_SIZE
34 #undef SQLITE_MAX_PAGE_COUNT
35 #undef SQLITE_MAX_LIKE_PATTERN_LENGTH
36
37 #define SQLITE_MAX_LENGTH sqlite3MAX_LENGTH
38 #define SQLITE_MAX_COLUMN sqlite3MAX_COLUMN
39 #define SQLITE_MAX_SQL_LENGTH sqlite3MAX_SQL_LENGTH
40 #define SQLITE_MAX_EXPR_DEPTH sqlite3MAX_EXPR_DEPTH
41 #define SQLITE_MAX_COMPOUND_SELECT sqlite3MAX_COMPOUND_SELECT
42 #define SQLITE_MAX_VDBE_OP sqlite3MAX_VDBE_OP
43 #define SQLITE_MAX_FUNCTION_ARG sqlite3MAX_FUNCTION_ARG
44 #define SQLITE_MAX_VARIABLE_NUMBER sqlite3MAX_VARIABLE_NUMBER
45 #define SQLITE_MAX_PAGE_SIZE sqlite3MAX_PAGE_SIZE
46 #define SQLITE_MAX_PAGE_COUNT sqlite3MAX_PAGE_COUNT
47 #define SQLITE_MAX_LIKE_PATTERN_LENGTH sqlite3MAX_LIKE_PATTERN_LENGTH
48
49 extern int sqlite3MAX_LENGTH;
50 extern int sqlite3MAX_COLUMN;
51 extern int sqlite3MAX_SQL_LENGTH;
52 extern int sqlite3MAX_EXPR_DEPTH;
53 extern int sqlite3MAX_COMPOUND_SELECT;
54 extern int sqlite3MAX_VDBE_OP;
55 extern int sqlite3MAX_FUNCTION_ARG;
56 extern int sqlite3MAX_VARIABLE_NUMBER;
57 extern int sqlite3MAX_PAGE_SIZE;
58 extern int sqlite3MAX_PAGE_COUNT;
59 extern int sqlite3MAX_LIKE_PATTERN_LENGTH;
60#endif
61
62
63/*
64** The SQLITE_THREADSAFE macro must be defined as either 0 or 1.
65** Older versions of SQLite used an optional THREADSAFE macro.
66** We support that for legacy
67*/
68#if !defined(SQLITE_THREADSAFE)
69#if defined(THREADSAFE)
70# define SQLITE_THREADSAFE THREADSAFE
71#else
72# define SQLITE_THREADSAFE 1
73#endif
74#endif
75
76/*
77** We need to define _XOPEN_SOURCE as follows in order to enable
78** recursive mutexes on most unix systems. But Mac OS X is different.
79** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
80** so it is omitted there. See ticket #2673.
81**
82** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
83** implemented on some systems. So we avoid defining it at all
84** if it is already defined or if it is unneeded because we are
85** not doing a threadsafe build. Ticket #2681.
86*/
87#if !defined(_XOPEN_SOURCE) && !defined(__MACOS__) && SQLITE_THREADSAFE
88# define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */
89#endif
90
91#if defined(SQLITE_TCL) || defined(TCLSH)
92# include <tcl.h>
93#endif
94
95/*
96** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
97** Setting NDEBUG makes the code smaller and run faster. So the following
98** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
99** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out
100** feature.
101*/
102#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
103# define NDEBUG 1
104#endif
105
106/*
107** These #defines should enable >2GB file support on Posix if the
108** underlying operating system supports it. If the OS lacks
109** large file support, or if the OS is windows, these should be no-ops.
110**
111** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
112** on the compiler command line. This is necessary if you are compiling
113** on a recent machine (ex: RedHat 7.2) but you want your code to work
114** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
115** without this option, LFS is enable. But LFS does not exist in the kernel
116** in RedHat 6.0, so the code won't work. Hence, for maximum binary
117** portability you should omit LFS.
118**
119** Similar is true for MacOS. LFS is only supported on MacOS 9 and later.
120*/
121#ifndef SQLITE_DISABLE_LFS
122# define _LARGE_FILE 1
123# ifndef _FILE_OFFSET_BITS
124# define _FILE_OFFSET_BITS 64
125# endif
126# define _LARGEFILE_SOURCE 1
127#endif
128
129#include "sqlite3.h"
130#include "hash.h"
131#include "parse.h"
132#include <stdio.h>
133#include <stdlib.h>
134#include <string.h>
135#include <assert.h>
136#include <stddef.h>
137
138#define sqlite3_isnan(X) ((X)!=(X))
139
140/*
141** If compiling for a processor that lacks floating point support,
142** substitute integer for floating-point
143*/
144#ifdef SQLITE_OMIT_FLOATING_POINT
145# define double sqlite_int64
146# define LONGDOUBLE_TYPE sqlite_int64
147# ifndef SQLITE_BIG_DBL
148# define SQLITE_BIG_DBL (0x7fffffffffffffff)
149# endif
150# define SQLITE_OMIT_DATETIME_FUNCS 1
151# define SQLITE_OMIT_TRACE 1
152# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
153#endif
154#ifndef SQLITE_BIG_DBL
155# define SQLITE_BIG_DBL (1e99)
156#endif
157
158/*
159** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
160** afterward. Having this macro allows us to cause the C compiler
161** to omit code used by TEMP tables without messy #ifndef statements.
162*/
163#ifdef SQLITE_OMIT_TEMPDB
164#define OMIT_TEMPDB 1
165#else
166#define OMIT_TEMPDB 0
167#endif
168
169/*
170** If the following macro is set to 1, then NULL values are considered
171** distinct when determining whether or not two entries are the same
172** in a UNIQUE index. This is the way PostgreSQL, Oracle, DB2, MySQL,
173** OCELOT, and Firebird all work. The SQL92 spec explicitly says this
174** is the way things are suppose to work.
175**
176** If the following macro is set to 0, the NULLs are indistinct for
177** a UNIQUE index. In this mode, you can only have a single NULL entry
178** for a column declared UNIQUE. This is the way Informix and SQL Server
179** work.
180*/
181#define NULL_DISTINCT_FOR_UNIQUE 1
182
183/*
184** The "file format" number is an integer that is incremented whenever
185** the VDBE-level file format changes. The following macros define the
186** the default file format for new databases and the maximum file format
187** that the library can read.
188*/
189#define SQLITE_MAX_FILE_FORMAT 4
190#ifndef SQLITE_DEFAULT_FILE_FORMAT
191# define SQLITE_DEFAULT_FILE_FORMAT 1
192#endif
193
194/*
195** Provide a default value for TEMP_STORE in case it is not specified
196** on the command-line
197*/
198#ifndef TEMP_STORE
199# define TEMP_STORE 1
200#endif
201
202/*
203** GCC does not define the offsetof() macro so we'll have to do it
204** ourselves.
205*/
206#ifndef offsetof
207#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
208#endif
209
210/*
211** Check to see if this machine uses EBCDIC. (Yes, believe it or
212** not, there are still machines out there that use EBCDIC.)
213*/
214#if 'A' == '\301'
215# define SQLITE_EBCDIC 1
216#else
217# define SQLITE_ASCII 1
218#endif
219
220/*
221** Integers of known sizes. These typedefs might change for architectures
222** where the sizes very. Preprocessor macros are available so that the
223** types can be conveniently redefined at compile-type. Like this:
224**
225** cc '-DUINTPTR_TYPE=long long int' ...
226*/
227#ifndef UINT32_TYPE
228# define UINT32_TYPE unsigned int
229#endif
230#ifndef UINT16_TYPE
231# define UINT16_TYPE unsigned short int
232#endif
233#ifndef INT16_TYPE
234# define INT16_TYPE short int
235#endif
236#ifndef UINT8_TYPE
237# define UINT8_TYPE unsigned char
238#endif
239#ifndef INT8_TYPE
240# define INT8_TYPE signed char
241#endif
242#ifndef LONGDOUBLE_TYPE
243# define LONGDOUBLE_TYPE long double
244#endif
245typedef sqlite_int64 i64; /* 8-byte signed integer */
246typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
247typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
248typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
249typedef INT16_TYPE i16; /* 2-byte signed integer */
250typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
251typedef UINT8_TYPE i8; /* 1-byte signed integer */
252
253/*
254** Macros to determine whether the machine is big or little endian,
255** evaluated at runtime.
256*/
257#ifdef SQLITE_AMALGAMATION
258const int sqlite3One;
259#else
260extern const int sqlite3one;
261#endif
262#if defined(i386) || defined(__i386__) || defined(_M_IX86)
263# define SQLITE_BIGENDIAN 0
264# define SQLITE_LITTLEENDIAN 1
265# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
266#else
267# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
268# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
269# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
270#endif
271
272/*
273** An instance of the following structure is used to store the busy-handler
274** callback for a given sqlite handle.
275**
276** The sqlite.busyHandler member of the sqlite struct contains the busy
277** callback for the database handle. Each pager opened via the sqlite
278** handle is passed a pointer to sqlite.busyHandler. The busy-handler
279** callback is currently invoked only from within pager.c.
280*/
281typedef struct BusyHandler BusyHandler;
282struct BusyHandler {
283 int (*xFunc)(void *,int); /* The busy callback */
284 void *pArg; /* First arg to busy callback */
285 int nBusy; /* Incremented with each busy call */
286};
287
288/*
289** Defer sourcing vdbe.h and btree.h until after the "u8" and
290** "BusyHandler typedefs.
291*/
292#include "btree.h"
293#include "vdbe.h"
294#include "pager.h"
295
296
297/*
298** Name of the master database table. The master database table
299** is a special table that holds the names and attributes of all
300** user tables and indices.
301*/
302#define MASTER_NAME "sqlite_master"
303#define TEMP_MASTER_NAME "sqlite_temp_master"
304
305/*
306** The root-page of the master database table.
307*/
308#define MASTER_ROOT 1
309
310/*
311** The name of the schema table.
312*/
313#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
314
315/*
316** A convenience macro that returns the number of elements in
317** an array.
318*/
319#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
320
321/*
322** Forward references to structures
323*/
324typedef struct AggInfo AggInfo;
325typedef struct AuthContext AuthContext;
326typedef struct CollSeq CollSeq;
327typedef struct Column Column;
328typedef struct Db Db;
329typedef struct Schema Schema;
330typedef struct Expr Expr;
331typedef struct ExprList ExprList;
332typedef struct FKey FKey;
333typedef struct FuncDef FuncDef;
334typedef struct IdList IdList;
335typedef struct Index Index;
336typedef struct KeyClass KeyClass;
337typedef struct KeyInfo KeyInfo;
338typedef struct Module Module;
339typedef struct NameContext NameContext;
340typedef struct Parse Parse;
341typedef struct Select Select;
342typedef struct SrcList SrcList;
343typedef struct Table Table;
344typedef struct TableLock TableLock;
345typedef struct Token Token;
346typedef struct TriggerStack TriggerStack;
347typedef struct TriggerStep TriggerStep;
348typedef struct Trigger Trigger;
349typedef struct WhereInfo WhereInfo;
350typedef struct WhereLevel WhereLevel;
351
352#include "os.h"
353#include "mutex.h"
354
355/*
356** Each database file to be accessed by the system is an instance
357** of the following structure. There are normally two of these structures
358** in the sqlite.aDb[] array. aDb[0] is the main database file and
359** aDb[1] is the database file used to hold temporary tables. Additional
360** databases may be attached.
361*/
362struct Db {
363 char *zName; /* Name of this database */
364 Btree *pBt; /* The B*Tree structure for this database file */
365 u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */
366 u8 safety_level; /* How aggressive at synching data to disk */
367 void *pAux; /* Auxiliary data. Usually NULL */
368 void (*xFreeAux)(void*); /* Routine to free pAux */
369 Schema *pSchema; /* Pointer to database schema (possibly shared) */
370};
371
372/*
373** An instance of the following structure stores a database schema.
374**
375** If there are no virtual tables configured in this schema, the
376** Schema.db variable is set to NULL. After the first virtual table
377** has been added, it is set to point to the database connection
378** used to create the connection. Once a virtual table has been
379** added to the Schema structure and the Schema.db variable populated,
380** only that database connection may use the Schema to prepare
381** statements.
382*/
383struct Schema {
384 int schema_cookie; /* Database schema version number for this file */
385 Hash tblHash; /* All tables indexed by name */
386 Hash idxHash; /* All (named) indices indexed by name */
387 Hash trigHash; /* All triggers indexed by name */
388 Hash aFKey; /* Foreign keys indexed by to-table */
389 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
390 u8 file_format; /* Schema format version for this file */
391 u8 enc; /* Text encoding used by this database */
392 u16 flags; /* Flags associated with this schema */
393 int cache_size; /* Number of pages to use in the cache */
394#ifndef SQLITE_OMIT_VIRTUALTABLE
395 sqlite3 *db; /* "Owner" connection. See comment above */
396#endif
397};
398
399/*
400** These macros can be used to test, set, or clear bits in the
401** Db.flags field.
402*/
403#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
404#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
405#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
406#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
407
408/*
409** Allowed values for the DB.flags field.
410**
411** The DB_SchemaLoaded flag is set after the database schema has been
412** read into internal hash tables.
413**
414** DB_UnresetViews means that one or more views have column names that
415** have been filled out. If the schema changes, these column names might
416** changes and so the view will need to be reset.
417*/
418#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
419#define DB_UnresetViews 0x0002 /* Some views have defined column names */
420#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
421
422
423/*
424** Each database is an instance of the following structure.
425**
426** The sqlite.lastRowid records the last insert rowid generated by an
427** insert statement. Inserts on views do not affect its value. Each
428** trigger has its own context, so that lastRowid can be updated inside
429** triggers as usual. The previous value will be restored once the trigger
430** exits. Upon entering a before or instead of trigger, lastRowid is no
431** longer (since after version 2.8.12) reset to -1.
432**
433** The sqlite.nChange does not count changes within triggers and keeps no
434** context. It is reset at start of sqlite3_exec.
435** The sqlite.lsChange represents the number of changes made by the last
436** insert, update, or delete statement. It remains constant throughout the
437** length of a statement and is then updated by OP_SetCounts. It keeps a
438** context stack just like lastRowid so that the count of changes
439** within a trigger is not seen outside the trigger. Changes to views do not
440** affect the value of lsChange.
441** The sqlite.csChange keeps track of the number of current changes (since
442** the last statement) and is used to update sqlite_lsChange.
443**
444** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
445** store the most recent error code and, if applicable, string. The
446** internal function sqlite3Error() is used to set these variables
447** consistently.
448*/
449struct sqlite3 {
450 sqlite3_vfs *pVfs; /* OS Interface */
451 int nDb; /* Number of backends currently in use */
452 Db *aDb; /* All backends */
453 int flags; /* Miscellanous flags. See below */
454 int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
455 int errCode; /* Most recent error code (SQLITE_*) */
456 int errMask; /* & result codes with this before returning */
457 u8 autoCommit; /* The auto-commit flag. */
458 u8 temp_store; /* 1: file 2: memory 0: default */
459 u8 mallocFailed; /* True if we have seen a malloc failure */
460 int nTable; /* Number of tables in the database */
461 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
462 i64 lastRowid; /* ROWID of most recent insert (see above) */
463 i64 priorNewRowid; /* Last randomly generated ROWID */
464 int magic; /* Magic number for detect library misuse */
465 int nChange; /* Value returned by sqlite3_changes() */
466 int nTotalChange; /* Value returned by sqlite3_total_changes() */
467 sqlite3_mutex *mutex; /* Connection mutex */
468 struct sqlite3InitInfo { /* Information used during initialization */
469 int iDb; /* When back is being initialized */
470 int newTnum; /* Rootpage of table being initialized */
471 u8 busy; /* TRUE if currently initializing */
472 } init;
473 int nExtension; /* Number of loaded extensions */
474 void **aExtension; /* Array of shared libraray handles */
475 struct Vdbe *pVdbe; /* List of active virtual machines */
476 int activeVdbeCnt; /* Number of vdbes currently executing */
477 void (*xTrace)(void*,const char*); /* Trace function */
478 void *pTraceArg; /* Argument to the trace function */
479 void (*xProfile)(void*,const char*,u64); /* Profiling function */
480 void *pProfileArg; /* Argument to profile function */
481 void *pCommitArg; /* Argument to xCommitCallback() */
482 int (*xCommitCallback)(void*); /* Invoked at every commit. */
483 void *pRollbackArg; /* Argument to xRollbackCallback() */
484 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
485 void *pUpdateArg;
486 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
487 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
488 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
489 void *pCollNeededArg;
490 sqlite3_value *pErr; /* Most recent error message */
491 char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
492 char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */
493 union {
494 int isInterrupted; /* True if sqlite3_interrupt has been called */
495 double notUsed1; /* Spacer */
496 } u1;
497#ifndef SQLITE_OMIT_AUTHORIZATION
498 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
499 /* Access authorization function */
500 void *pAuthArg; /* 1st argument to the access auth function */
501#endif
502#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
503 int (*xProgress)(void *); /* The progress callback */
504 void *pProgressArg; /* Argument to the progress callback */
505 int nProgressOps; /* Number of opcodes for progress callback */
506#endif
507#ifndef SQLITE_OMIT_VIRTUALTABLE
508 Hash aModule; /* populated by sqlite3_create_module() */
509 Table *pVTab; /* vtab with active Connect/Create method */
510 sqlite3_vtab **aVTrans; /* Virtual tables with open transactions */
511 int nVTrans; /* Allocated size of aVTrans */
512#endif
513 Hash aFunc; /* All functions that can be in SQL exprs */
514 Hash aCollSeq; /* All collating sequences */
515 BusyHandler busyHandler; /* Busy callback */
516 int busyTimeout; /* Busy handler timeout, in msec */
517 Db aDbStatic[2]; /* Static space for the 2 default backends */
518#ifdef SQLITE_SSE
519 sqlite3_stmt *pFetch; /* Used by SSE to fetch stored statements */
520#endif
521 u8 dfltLockMode; /* Default locking-mode for attached dbs */
522};
523
524/*
525** A macro to discover the encoding of a database.
526*/
527#define ENC(db) ((db)->aDb[0].pSchema->enc)
528
529/*
530** Possible values for the sqlite.flags and or Db.flags fields.
531**
532** On sqlite.flags, the SQLITE_InTrans value means that we have
533** executed a BEGIN. On Db.flags, SQLITE_InTrans means a statement
534** transaction is active on that particular database file.
535*/
536#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
537#define SQLITE_InTrans 0x00000008 /* True if in a transaction */
538#define SQLITE_InternChanges 0x00000010 /* Uncommitted Hash table changes */
539#define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
540#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
541#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
542 /* DELETE, or UPDATE and return */
543 /* the count using a callback. */
544#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
545 /* result set is empty */
546#define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */
547#define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */
548#define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */
549#define SQLITE_NoReadlock 0x00001000 /* Readlocks are omitted when
550 ** accessing read-only databases */
551#define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */
552#define SQLITE_ReadUncommitted 0x00004000 /* For shared-cache mode */
553#define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */
554#define SQLITE_FullFSync 0x00010000 /* Use full fsync on the backend */
555#define SQLITE_LoadExtension 0x00020000 /* Enable load_extension */
556
557#define SQLITE_RecoveryMode 0x00040000 /* Ignore schema errors */
558#define SQLITE_SharedCache 0x00080000 /* Cache sharing is enabled */
559#define SQLITE_Vtab 0x00100000 /* There exists a virtual table */
560
561/*
562** Possible values for the sqlite.magic field.
563** The numbers are obtained at random and have no special meaning, other
564** than being distinct from one another.
565*/
566#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
567#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
568#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
569#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
570
571/*
572** Each SQL function is defined by an instance of the following
573** structure. A pointer to this structure is stored in the sqlite.aFunc
574** hash table. When multiple functions have the same name, the hash table
575** points to a linked list of these structures.
576*/
577struct FuncDef {
578 i16 nArg; /* Number of arguments. -1 means unlimited */
579 u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
580 u8 needCollSeq; /* True if sqlite3GetFuncCollSeq() might be called */
581 u8 flags; /* Some combination of SQLITE_FUNC_* */
582 void *pUserData; /* User data parameter */
583 FuncDef *pNext; /* Next function with same name */
584 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
585 void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
586 void (*xFinalize)(sqlite3_context*); /* Aggregate finializer */
587 char zName[1]; /* SQL name of the function. MUST BE LAST */
588};
589
590/*
591** Each SQLite module (virtual table definition) is defined by an
592** instance of the following structure, stored in the sqlite3.aModule
593** hash table.
594*/
595struct Module {
596 const sqlite3_module *pModule; /* Callback pointers */
597 const char *zName; /* Name passed to create_module() */
598 void *pAux; /* pAux passed to create_module() */
599 void (*xDestroy)(void *); /* Module destructor function */
600};
601
602/*
603** Possible values for FuncDef.flags
604*/
605#define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */
606#define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */
607#define SQLITE_FUNC_EPHEM 0x04 /* Ephermeral. Delete with VDBE */
608
609/*
610** information about each column of an SQL table is held in an instance
611** of this structure.
612*/
613struct Column {
614 char *zName; /* Name of this column */
615 Expr *pDflt; /* Default value of this column */
616 char *zType; /* Data type for this column */
617 char *zColl; /* Collating sequence. If NULL, use the default */
618 u8 notNull; /* True if there is a NOT NULL constraint */
619 u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */
620 char affinity; /* One of the SQLITE_AFF_... values */
621#ifndef SQLITE_OMIT_VIRTUALTABLE
622 u8 isHidden; /* True if this column is 'hidden' */
623#endif
624};
625
626/*
627** A "Collating Sequence" is defined by an instance of the following
628** structure. Conceptually, a collating sequence consists of a name and
629** a comparison routine that defines the order of that sequence.
630**
631** There may two seperate implementations of the collation function, one
632** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
633** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
634** native byte order. When a collation sequence is invoked, SQLite selects
635** the version that will require the least expensive encoding
636** translations, if any.
637**
638** The CollSeq.pUser member variable is an extra parameter that passed in
639** as the first argument to the UTF-8 comparison function, xCmp.
640** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
641** xCmp16.
642**
643** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
644** collating sequence is undefined. Indices built on an undefined
645** collating sequence may not be read or written.
646*/
647struct CollSeq {
648 char *zName; /* Name of the collating sequence, UTF-8 encoded */
649 u8 enc; /* Text encoding handled by xCmp() */
650 u8 type; /* One of the SQLITE_COLL_... values below */
651 void *pUser; /* First argument to xCmp() */
652 int (*xCmp)(void*,int, const void*, int, const void*);
653 void (*xDel)(void*); /* Destructor for pUser */
654};
655
656/*
657** Allowed values of CollSeq flags:
658*/
659#define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */
660#define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */
661#define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */
662#define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */
663
664/*
665** A sort order can be either ASC or DESC.
666*/
667#define SQLITE_SO_ASC 0 /* Sort in ascending order */
668#define SQLITE_SO_DESC 1 /* Sort in ascending order */
669
670/*
671** Column affinity types.
672**
673** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
674** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
675** the speed a little by number the values consecutively.
676**
677** But rather than start with 0 or 1, we begin with 'a'. That way,
678** when multiple affinity types are concatenated into a string and
679** used as the P3 operand, they will be more readable.
680**
681** Note also that the numeric types are grouped together so that testing
682** for a numeric type is a single comparison.
683*/
684#define SQLITE_AFF_TEXT 'a'
685#define SQLITE_AFF_NONE 'b'
686#define SQLITE_AFF_NUMERIC 'c'
687#define SQLITE_AFF_INTEGER 'd'
688#define SQLITE_AFF_REAL 'e'
689
690#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
691
692/*
693** Each SQL table is represented in memory by an instance of the
694** following structure.
695**
696** Table.zName is the name of the table. The case of the original
697** CREATE TABLE statement is stored, but case is not significant for
698** comparisons.
699**
700** Table.nCol is the number of columns in this table. Table.aCol is a
701** pointer to an array of Column structures, one for each column.
702**
703** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
704** the column that is that key. Otherwise Table.iPKey is negative. Note
705** that the datatype of the PRIMARY KEY must be INTEGER for this field to
706** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of
707** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid
708** is generated for each row of the table. Table.hasPrimKey is true if
709** the table has any PRIMARY KEY, INTEGER or otherwise.
710**
711** Table.tnum is the page number for the root BTree page of the table in the
712** database file. If Table.iDb is the index of the database table backend
713** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that
714** holds temporary tables and indices. If Table.isEphem
715** is true, then the table is stored in a file that is automatically deleted
716** when the VDBE cursor to the table is closed. In this case Table.tnum
717** refers VDBE cursor number that holds the table open, not to the root
718** page number. Transient tables are used to hold the results of a
719** sub-query that appears instead of a real table name in the FROM clause
720** of a SELECT statement.
721*/
722struct Table {
723 char *zName; /* Name of the table */
724 int nCol; /* Number of columns in this table */
725 Column *aCol; /* Information about each column */
726 int iPKey; /* If not less then 0, use aCol[iPKey] as the primary key */
727 Index *pIndex; /* List of SQL indexes on this table. */
728 int tnum; /* Root BTree node for this table (see note above) */
729 Select *pSelect; /* NULL for tables. Points to definition if a view. */
730 int nRef; /* Number of pointers to this Table */
731 Trigger *pTrigger; /* List of SQL triggers on this table */
732 FKey *pFKey; /* Linked list of all foreign keys in this table */
733 char *zColAff; /* String defining the affinity of each column */
734#ifndef SQLITE_OMIT_CHECK
735 Expr *pCheck; /* The AND of all CHECK constraints */
736#endif
737#ifndef SQLITE_OMIT_ALTERTABLE
738 int addColOffset; /* Offset in CREATE TABLE statement to add a new column */
739#endif
740 u8 readOnly; /* True if this table should not be written by the user */
741 u8 isEphem; /* True if created using OP_OpenEphermeral */
742 u8 hasPrimKey; /* True if there exists a primary key */
743 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
744 u8 autoInc; /* True if the integer primary key is autoincrement */
745#ifndef SQLITE_OMIT_VIRTUALTABLE
746 u8 isVirtual; /* True if this is a virtual table */
747 u8 isCommit; /* True once the CREATE TABLE has been committed */
748 Module *pMod; /* Pointer to the implementation of the module */
749 sqlite3_vtab *pVtab; /* Pointer to the module instance */
750 int nModuleArg; /* Number of arguments to the module */
751 char **azModuleArg; /* Text of all module args. [0] is module name */
752#endif
753 Schema *pSchema; /* Schema that contains this table */
754};
755
756/*
757** Test to see whether or not a table is a virtual table. This is
758** done as a macro so that it will be optimized out when virtual
759** table support is omitted from the build.
760*/
761#ifndef SQLITE_OMIT_VIRTUALTABLE
762# define IsVirtual(X) ((X)->isVirtual)
763# define IsHiddenColumn(X) ((X)->isHidden)
764#else
765# define IsVirtual(X) 0
766# define IsHiddenColumn(X) 0
767#endif
768
769/*
770** Each foreign key constraint is an instance of the following structure.
771**
772** A foreign key is associated with two tables. The "from" table is
773** the table that contains the REFERENCES clause that creates the foreign
774** key. The "to" table is the table that is named in the REFERENCES clause.
775** Consider this example:
776**
777** CREATE TABLE ex1(
778** a INTEGER PRIMARY KEY,
779** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
780** );
781**
782** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
783**
784** Each REFERENCES clause generates an instance of the following structure
785** which is attached to the from-table. The to-table need not exist when
786** the from-table is created. The existance of the to-table is not checked
787** until an attempt is made to insert data into the from-table.
788**
789** The sqlite.aFKey hash table stores pointers to this structure
790** given the name of a to-table. For each to-table, all foreign keys
791** associated with that table are on a linked list using the FKey.pNextTo
792** field.
793*/
794struct FKey {
795 Table *pFrom; /* The table that constains the REFERENCES clause */
796 FKey *pNextFrom; /* Next foreign key in pFrom */
797 char *zTo; /* Name of table that the key points to */
798 FKey *pNextTo; /* Next foreign key that points to zTo */
799 int nCol; /* Number of columns in this key */
800 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
801 int iFrom; /* Index of column in pFrom */
802 char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */
803 } *aCol; /* One entry for each of nCol column s */
804 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
805 u8 updateConf; /* How to resolve conflicts that occur on UPDATE */
806 u8 deleteConf; /* How to resolve conflicts that occur on DELETE */
807 u8 insertConf; /* How to resolve conflicts that occur on INSERT */
808};
809
810/*
811** SQLite supports many different ways to resolve a constraint
812** error. ROLLBACK processing means that a constraint violation
813** causes the operation in process to fail and for the current transaction
814** to be rolled back. ABORT processing means the operation in process
815** fails and any prior changes from that one operation are backed out,
816** but the transaction is not rolled back. FAIL processing means that
817** the operation in progress stops and returns an error code. But prior
818** changes due to the same operation are not backed out and no rollback
819** occurs. IGNORE means that the particular row that caused the constraint
820** error is not inserted or updated. Processing continues and no error
821** is returned. REPLACE means that preexisting database rows that caused
822** a UNIQUE constraint violation are removed so that the new insert or
823** update can proceed. Processing continues and no error is reported.
824**
825** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
826** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
827** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
828** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
829** referenced table row is propagated into the row that holds the
830** foreign key.
831**
832** The following symbolic values are used to record which type
833** of action to take.
834*/
835#define OE_None 0 /* There is no constraint to check */
836#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
837#define OE_Abort 2 /* Back out changes but do no rollback transaction */
838#define OE_Fail 3 /* Stop the operation but leave all prior changes */
839#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
840#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
841
842#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
843#define OE_SetNull 7 /* Set the foreign key value to NULL */
844#define OE_SetDflt 8 /* Set the foreign key value to its default */
845#define OE_Cascade 9 /* Cascade the changes */
846
847#define OE_Default 99 /* Do whatever the default action is */
848
849
850/*
851** An instance of the following structure is passed as the first
852** argument to sqlite3VdbeKeyCompare and is used to control the
853** comparison of the two index keys.
854**
855** If the KeyInfo.incrKey value is true and the comparison would
856** otherwise be equal, then return a result as if the second key
857** were larger.
858*/
859struct KeyInfo {
860 sqlite3 *db; /* The database connection */
861 u8 enc; /* Text encoding - one of the TEXT_Utf* values */
862 u8 incrKey; /* Increase 2nd key by epsilon before comparison */
863 int nField; /* Number of entries in aColl[] */
864 u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */
865 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
866};
867
868/*
869** Each SQL index is represented in memory by an
870** instance of the following structure.
871**
872** The columns of the table that are to be indexed are described
873** by the aiColumn[] field of this structure. For example, suppose
874** we have the following table and index:
875**
876** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
877** CREATE INDEX Ex2 ON Ex1(c3,c1);
878**
879** In the Table structure describing Ex1, nCol==3 because there are
880** three columns in the table. In the Index structure describing
881** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
882** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
883** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
884** The second column to be indexed (c1) has an index of 0 in
885** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
886**
887** The Index.onError field determines whether or not the indexed columns
888** must be unique and what to do if they are not. When Index.onError=OE_None,
889** it means this is not a unique index. Otherwise it is a unique index
890** and the value of Index.onError indicate the which conflict resolution
891** algorithm to employ whenever an attempt is made to insert a non-unique
892** element.
893*/
894struct Index {
895 char *zName; /* Name of this index */
896 int nColumn; /* Number of columns in the table used by this index */
897 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
898 unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
899 Table *pTable; /* The SQL table being indexed */
900 int tnum; /* Page containing root of this index in database file */
901 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
902 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
903 char *zColAff; /* String defining the affinity of each column */
904 Index *pNext; /* The next index associated with the same table */
905 Schema *pSchema; /* Schema containing this index */
906 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
907 char **azColl; /* Array of collation sequence names for index */
908};
909
910/*
911** Each token coming out of the lexer is an instance of
912** this structure. Tokens are also used as part of an expression.
913**
914** Note if Token.z==0 then Token.dyn and Token.n are undefined and
915** may contain random values. Do not make any assuptions about Token.dyn
916** and Token.n when Token.z==0.
917*/
918struct Token {
919 const unsigned char *z; /* Text of the token. Not NULL-terminated! */
920 unsigned dyn : 1; /* True for malloced memory, false for static */
921 unsigned n : 31; /* Number of characters in this token */
922};
923
924/*
925** An instance of this structure contains information needed to generate
926** code for a SELECT that contains aggregate functions.
927**
928** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
929** pointer to this structure. The Expr.iColumn field is the index in
930** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
931** code for that node.
932**
933** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
934** original Select structure that describes the SELECT statement. These
935** fields do not need to be freed when deallocating the AggInfo structure.
936*/
937struct AggInfo {
938 u8 directMode; /* Direct rendering mode means take data directly
939 ** from source tables rather than from accumulators */
940 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
941 ** than the source table */
942 int sortingIdx; /* Cursor number of the sorting index */
943 ExprList *pGroupBy; /* The group by clause */
944 int nSortingColumn; /* Number of columns in the sorting index */
945 struct AggInfo_col { /* For each column used in source tables */
946 Table *pTab; /* Source table */
947 int iTable; /* Cursor number of the source table */
948 int iColumn; /* Column number within the source table */
949 int iSorterColumn; /* Column number in the sorting index */
950 int iMem; /* Memory location that acts as accumulator */
951 Expr *pExpr; /* The original expression */
952 } *aCol;
953 int nColumn; /* Number of used entries in aCol[] */
954 int nColumnAlloc; /* Number of slots allocated for aCol[] */
955 int nAccumulator; /* Number of columns that show through to the output.
956 ** Additional columns are used only as parameters to
957 ** aggregate functions */
958 struct AggInfo_func { /* For each aggregate function */
959 Expr *pExpr; /* Expression encoding the function */
960 FuncDef *pFunc; /* The aggregate function implementation */
961 int iMem; /* Memory location that acts as accumulator */
962 int iDistinct; /* Ephermeral table used to enforce DISTINCT */
963 } *aFunc;
964 int nFunc; /* Number of entries in aFunc[] */
965 int nFuncAlloc; /* Number of slots allocated for aFunc[] */
966};
967
968/*
969** Each node of an expression in the parse tree is an instance
970** of this structure.
971**
972** Expr.op is the opcode. The integer parser token codes are reused
973** as opcodes here. For example, the parser defines TK_GE to be an integer
974** code representing the ">=" operator. This same integer code is reused
975** to represent the greater-than-or-equal-to operator in the expression
976** tree.
977**
978** Expr.pRight and Expr.pLeft are subexpressions. Expr.pList is a list
979** of argument if the expression is a function.
980**
981** Expr.token is the operator token for this node. For some expressions
982** that have subexpressions, Expr.token can be the complete text that gave
983** rise to the Expr. In the latter case, the token is marked as being
984** a compound token.
985**
986** An expression of the form ID or ID.ID refers to a column in a table.
987** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
988** the integer cursor number of a VDBE cursor pointing to that table and
989** Expr.iColumn is the column number for the specific column. If the
990** expression is used as a result in an aggregate SELECT, then the
991** value is also stored in the Expr.iAgg column in the aggregate so that
992** it can be accessed after all aggregates are computed.
993**
994** If the expression is a function, the Expr.iTable is an integer code
995** representing which function. If the expression is an unbound variable
996** marker (a question mark character '?' in the original SQL) then the
997** Expr.iTable holds the index number for that variable.
998**
999** If the expression is a subquery then Expr.iColumn holds an integer
1000** register number containing the result of the subquery. If the
1001** subquery gives a constant result, then iTable is -1. If the subquery
1002** gives a different answer at different times during statement processing
1003** then iTable is the address of a subroutine that computes the subquery.
1004**
1005** The Expr.pSelect field points to a SELECT statement. The SELECT might
1006** be the right operand of an IN operator. Or, if a scalar SELECT appears
1007** in an expression the opcode is TK_SELECT and Expr.pSelect is the only
1008** operand.
1009**
1010** If the Expr is of type OP_Column, and the table it is selecting from
1011** is a disk table or the "old.*" pseudo-table, then pTab points to the
1012** corresponding table definition.
1013*/
1014struct Expr {
1015 u8 op; /* Operation performed by this node */
1016 char affinity; /* The affinity of the column or 0 if not a column */
1017 u16 flags; /* Various flags. See below */
1018 CollSeq *pColl; /* The collation type of the column or 0 */
1019 Expr *pLeft, *pRight; /* Left and right subnodes */
1020 ExprList *pList; /* A list of expressions used as function arguments
1021 ** or in "<expr> IN (<expr-list)" */
1022 Token token; /* An operand token */
1023 Token span; /* Complete text of the expression */
1024 int iTable, iColumn; /* When op==TK_COLUMN, then this expr node means the
1025 ** iColumn-th field of the iTable-th table. */
1026 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
1027 int iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
1028 int iRightJoinTable; /* If EP_FromJoin, the right table of the join */
1029 Select *pSelect; /* When the expression is a sub-select. Also the
1030 ** right side of "<expr> IN (<select>)" */
1031 Table *pTab; /* Table for OP_Column expressions. */
1032/* Schema *pSchema; */
1033#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1034 int nHeight; /* Height of the tree headed by this node */
1035#endif
1036};
1037
1038/*
1039** The following are the meanings of bits in the Expr.flags field.
1040*/
1041#define EP_FromJoin 0x01 /* Originated in ON or USING clause of a join */
1042#define EP_Agg 0x02 /* Contains one or more aggregate functions */
1043#define EP_Resolved 0x04 /* IDs have been resolved to COLUMNs */
1044#define EP_Error 0x08 /* Expression contains one or more errors */
1045#define EP_Distinct 0x10 /* Aggregate function with DISTINCT keyword */
1046#define EP_VarSelect 0x20 /* pSelect is correlated, not constant */
1047#define EP_Dequoted 0x40 /* True if the string has been dequoted */
1048#define EP_InfixFunc 0x80 /* True for an infix function: LIKE, GLOB, etc */
1049#define EP_ExpCollate 0x100 /* Collating sequence specified explicitly */
1050
1051/*
1052** These macros can be used to test, set, or clear bits in the
1053** Expr.flags field.
1054*/
1055#define ExprHasProperty(E,P) (((E)->flags&(P))==(P))
1056#define ExprHasAnyProperty(E,P) (((E)->flags&(P))!=0)
1057#define ExprSetProperty(E,P) (E)->flags|=(P)
1058#define ExprClearProperty(E,P) (E)->flags&=~(P)
1059
1060/*
1061** A list of expressions. Each expression may optionally have a
1062** name. An expr/name combination can be used in several ways, such
1063** as the list of "expr AS ID" fields following a "SELECT" or in the
1064** list of "ID = expr" items in an UPDATE. A list of expressions can
1065** also be used as the argument to a function, in which case the a.zName
1066** field is not used.
1067*/
1068struct ExprList {
1069 int nExpr; /* Number of expressions on the list */
1070 int nAlloc; /* Number of entries allocated below */
1071 int iECursor; /* VDBE Cursor associated with this ExprList */
1072 struct ExprList_item {
1073 Expr *pExpr; /* The list of expressions */
1074 char *zName; /* Token associated with this expression */
1075 u8 sortOrder; /* 1 for DESC or 0 for ASC */
1076 u8 isAgg; /* True if this is an aggregate like count(*) */
1077 u8 done; /* A flag to indicate when processing is finished */
1078 } *a; /* One entry for each expression */
1079};
1080
1081/*
1082** An instance of this structure can hold a simple list of identifiers,
1083** such as the list "a,b,c" in the following statements:
1084**
1085** INSERT INTO t(a,b,c) VALUES ...;
1086** CREATE INDEX idx ON t(a,b,c);
1087** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
1088**
1089** The IdList.a.idx field is used when the IdList represents the list of
1090** column names after a table name in an INSERT statement. In the statement
1091**
1092** INSERT INTO t(a,b,c) ...
1093**
1094** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
1095*/
1096struct IdList {
1097 struct IdList_item {
1098 char *zName; /* Name of the identifier */
1099 int idx; /* Index in some Table.aCol[] of a column named zName */
1100 } *a;
1101 int nId; /* Number of identifiers on the list */
1102 int nAlloc; /* Number of entries allocated for a[] below */
1103};
1104
1105/*
1106** The bitmask datatype defined below is used for various optimizations.
1107**
1108** Changing this from a 64-bit to a 32-bit type limits the number of
1109** tables in a join to 32 instead of 64. But it also reduces the size
1110** of the library by 738 bytes on ix86.
1111*/
1112typedef u64 Bitmask;
1113
1114/*
1115** The following structure describes the FROM clause of a SELECT statement.
1116** Each table or subquery in the FROM clause is a separate element of
1117** the SrcList.a[] array.
1118**
1119** With the addition of multiple database support, the following structure
1120** can also be used to describe a particular table such as the table that
1121** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
1122** such a table must be a simple name: ID. But in SQLite, the table can
1123** now be identified by a database name, a dot, then the table name: ID.ID.
1124**
1125** The jointype starts out showing the join type between the current table
1126** and the next table on the list. The parser builds the list this way.
1127** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
1128** jointype expresses the join between the table and the previous table.
1129*/
1130struct SrcList {
1131 i16 nSrc; /* Number of tables or subqueries in the FROM clause */
1132 i16 nAlloc; /* Number of entries allocated in a[] below */
1133 struct SrcList_item {
1134 char *zDatabase; /* Name of database holding this table */
1135 char *zName; /* Name of the table */
1136 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
1137 Table *pTab; /* An SQL table corresponding to zName */
1138 Select *pSelect; /* A SELECT statement used in place of a table name */
1139 u8 isPopulated; /* Temporary table associated with SELECT is populated */
1140 u8 jointype; /* Type of join between this able and the previous */
1141 int iCursor; /* The VDBE cursor number used to access this table */
1142 Expr *pOn; /* The ON clause of a join */
1143 IdList *pUsing; /* The USING clause of a join */
1144 Bitmask colUsed; /* Bit N (1<<N) set if column N or pTab is used */
1145 } a[1]; /* One entry for each identifier on the list */
1146};
1147
1148/*
1149** Permitted values of the SrcList.a.jointype field
1150*/
1151#define JT_INNER 0x0001 /* Any kind of inner or cross join */
1152#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
1153#define JT_NATURAL 0x0004 /* True for a "natural" join */
1154#define JT_LEFT 0x0008 /* Left outer join */
1155#define JT_RIGHT 0x0010 /* Right outer join */
1156#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
1157#define JT_ERROR 0x0040 /* unknown or unsupported join type */
1158
1159/*
1160** For each nested loop in a WHERE clause implementation, the WhereInfo
1161** structure contains a single instance of this structure. This structure
1162** is intended to be private the the where.c module and should not be
1163** access or modified by other modules.
1164**
1165** The pIdxInfo and pBestIdx fields are used to help pick the best
1166** index on a virtual table. The pIdxInfo pointer contains indexing
1167** information for the i-th table in the FROM clause before reordering.
1168** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
1169** The pBestIdx pointer is a copy of pIdxInfo for the i-th table after
1170** FROM clause ordering. This is a little confusing so I will repeat
1171** it in different words. WhereInfo.a[i].pIdxInfo is index information
1172** for WhereInfo.pTabList.a[i]. WhereInfo.a[i].pBestInfo is the
1173** index information for the i-th loop of the join. pBestInfo is always
1174** either NULL or a copy of some pIdxInfo. So for cleanup it is
1175** sufficient to free all of the pIdxInfo pointers.
1176**
1177*/
1178struct WhereLevel {
1179 int iFrom; /* Which entry in the FROM clause */
1180 int flags; /* Flags associated with this level */
1181 int iMem; /* First memory cell used by this level */
1182 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
1183 Index *pIdx; /* Index used. NULL if no index */
1184 int iTabCur; /* The VDBE cursor used to access the table */
1185 int iIdxCur; /* The VDBE cursor used to acesss pIdx */
1186 int brk; /* Jump here to break out of the loop */
1187 int nxt; /* Jump here to start the next IN combination */
1188 int cont; /* Jump here to continue with the next loop cycle */
1189 int top; /* First instruction of interior of the loop */
1190 int op, p1, p2; /* Opcode used to terminate the loop */
1191 int nEq; /* Number of == or IN constraints on this loop */
1192 int nIn; /* Number of IN operators constraining this loop */
1193 struct InLoop {
1194 int iCur; /* The VDBE cursor used by this IN operator */
1195 int topAddr; /* Top of the IN loop */
1196 } *aInLoop; /* Information about each nested IN operator */
1197 sqlite3_index_info *pBestIdx; /* Index information for this level */
1198
1199 /* The following field is really not part of the current level. But
1200 ** we need a place to cache index information for each table in the
1201 ** FROM clause and the WhereLevel structure is a convenient place.
1202 */
1203 sqlite3_index_info *pIdxInfo; /* Index info for n-th source table */
1204};
1205
1206/*
1207** The WHERE clause processing routine has two halves. The
1208** first part does the start of the WHERE loop and the second
1209** half does the tail of the WHERE loop. An instance of
1210** this structure is returned by the first half and passed
1211** into the second half to give some continuity.
1212*/
1213struct WhereInfo {
1214 Parse *pParse;
1215 SrcList *pTabList; /* List of tables in the join */
1216 int iTop; /* The very beginning of the WHERE loop */
1217 int iContinue; /* Jump here to continue with next record */
1218 int iBreak; /* Jump here to break out of the loop */
1219 int nLevel; /* Number of nested loop */
1220 sqlite3_index_info **apInfo; /* Array of pointers to index info structures */
1221 WhereLevel a[1]; /* Information about each nest loop in the WHERE */
1222};
1223
1224/*
1225** A NameContext defines a context in which to resolve table and column
1226** names. The context consists of a list of tables (the pSrcList) field and
1227** a list of named expression (pEList). The named expression list may
1228** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
1229** to the table being operated on by INSERT, UPDATE, or DELETE. The
1230** pEList corresponds to the result set of a SELECT and is NULL for
1231** other statements.
1232**
1233** NameContexts can be nested. When resolving names, the inner-most
1234** context is searched first. If no match is found, the next outer
1235** context is checked. If there is still no match, the next context
1236** is checked. This process continues until either a match is found
1237** or all contexts are check. When a match is found, the nRef member of
1238** the context containing the match is incremented.
1239**
1240** Each subquery gets a new NameContext. The pNext field points to the
1241** NameContext in the parent query. Thus the process of scanning the
1242** NameContext list corresponds to searching through successively outer
1243** subqueries looking for a match.
1244*/
1245struct NameContext {
1246 Parse *pParse; /* The parser */
1247 SrcList *pSrcList; /* One or more tables used to resolve names */
1248 ExprList *pEList; /* Optional list of named expressions */
1249 int nRef; /* Number of names resolved by this context */
1250 int nErr; /* Number of errors encountered while resolving names */
1251 u8 allowAgg; /* Aggregate functions allowed here */
1252 u8 hasAgg; /* True if aggregates are seen */
1253 u8 isCheck; /* True if resolving names in a CHECK constraint */
1254 int nDepth; /* Depth of subquery recursion. 1 for no recursion */
1255 AggInfo *pAggInfo; /* Information about aggregates at this level */
1256 NameContext *pNext; /* Next outer name context. NULL for outermost */
1257};
1258
1259/*
1260** An instance of the following structure contains all information
1261** needed to generate code for a single SELECT statement.
1262**
1263** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
1264** If there is a LIMIT clause, the parser sets nLimit to the value of the
1265** limit and nOffset to the value of the offset (or 0 if there is not
1266** offset). But later on, nLimit and nOffset become the memory locations
1267** in the VDBE that record the limit and offset counters.
1268**
1269** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
1270** These addresses must be stored so that we can go back and fill in
1271** the P3_KEYINFO and P2 parameters later. Neither the KeyInfo nor
1272** the number of columns in P2 can be computed at the same time
1273** as the OP_OpenEphm instruction is coded because not
1274** enough information about the compound query is known at that point.
1275** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
1276** for the result set. The KeyInfo for addrOpenTran[2] contains collating
1277** sequences for the ORDER BY clause.
1278*/
1279struct Select {
1280 ExprList *pEList; /* The fields of the result */
1281 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
1282 u8 isDistinct; /* True if the DISTINCT keyword is present */
1283 u8 isResolved; /* True once sqlite3SelectResolve() has run. */
1284 u8 isAgg; /* True if this is an aggregate query */
1285 u8 usesEphm; /* True if uses an OpenEphemeral opcode */
1286 u8 disallowOrderBy; /* Do not allow an ORDER BY to be attached if TRUE */
1287 char affinity; /* MakeRecord with this affinity for SRT_Set */
1288 SrcList *pSrc; /* The FROM clause */
1289 Expr *pWhere; /* The WHERE clause */
1290 ExprList *pGroupBy; /* The GROUP BY clause */
1291 Expr *pHaving; /* The HAVING clause */
1292 ExprList *pOrderBy; /* The ORDER BY clause */
1293 Select *pPrior; /* Prior select in a compound select statement */
1294 Select *pRightmost; /* Right-most select in a compound select statement */
1295 Expr *pLimit; /* LIMIT expression. NULL means not used. */
1296 Expr *pOffset; /* OFFSET expression. NULL means not used. */
1297 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
1298 int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
1299};
1300
1301/*
1302** The results of a select can be distributed in several ways.
1303*/
1304#define SRT_Union 1 /* Store result as keys in an index */
1305#define SRT_Except 2 /* Remove result from a UNION index */
1306#define SRT_Discard 3 /* Do not save the results anywhere */
1307
1308/* The ORDER BY clause is ignored for all of the above */
1309#define IgnorableOrderby(X) (X<=SRT_Discard)
1310
1311#define SRT_Callback 4 /* Invoke a callback with each row of result */
1312#define SRT_Mem 5 /* Store result in a memory cell */
1313#define SRT_Set 6 /* Store non-null results as keys in an index */
1314#define SRT_Table 7 /* Store result as data with an automatic rowid */
1315#define SRT_EphemTab 8 /* Create transient tab and store like SRT_Table */
1316#define SRT_Subroutine 9 /* Call a subroutine to handle results */
1317#define SRT_Exists 10 /* Store 1 if the result is not empty */
1318
1319/*
1320** An SQL parser context. A copy of this structure is passed through
1321** the parser and down into all the parser action routine in order to
1322** carry around information that is global to the entire parse.
1323**
1324** The structure is divided into two parts. When the parser and code
1325** generate call themselves recursively, the first part of the structure
1326** is constant but the second part is reset at the beginning and end of
1327** each recursion.
1328**
1329** The nTableLock and aTableLock variables are only used if the shared-cache
1330** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
1331** used to store the set of table-locks required by the statement being
1332** compiled. Function sqlite3TableLock() is used to add entries to the
1333** list.
1334*/
1335struct Parse {
1336 sqlite3 *db; /* The main database structure */
1337 int rc; /* Return code from execution */
1338 char *zErrMsg; /* An error message */
1339 Vdbe *pVdbe; /* An engine for executing database bytecode */
1340 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
1341 u8 nameClash; /* A permanent table name clashes with temp table name */
1342 u8 checkSchema; /* Causes schema cookie check after an error */
1343 u8 nested; /* Number of nested calls to the parser/code generator */
1344 u8 parseError; /* True after a parsing error. Ticket #1794 */
1345 int nErr; /* Number of errors seen */
1346 int nTab; /* Number of previously allocated VDBE cursors */
1347 int nMem; /* Number of memory cells used so far */
1348 int nSet; /* Number of sets used so far */
1349 int ckOffset; /* Stack offset to data used by CHECK constraints */
1350 u32 writeMask; /* Start a write transaction on these databases */
1351 u32 cookieMask; /* Bitmask of schema verified databases */
1352 int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
1353 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
1354#ifndef SQLITE_OMIT_SHARED_CACHE
1355 int nTableLock; /* Number of locks in aTableLock */
1356 TableLock *aTableLock; /* Required table locks for shared-cache mode */
1357#endif
1358
1359 /* Above is constant between recursions. Below is reset before and after
1360 ** each recursion */
1361
1362 int nVar; /* Number of '?' variables seen in the SQL so far */
1363 int nVarExpr; /* Number of used slots in apVarExpr[] */
1364 int nVarExprAlloc; /* Number of allocated slots in apVarExpr[] */
1365 Expr **apVarExpr; /* Pointers to :aaa and $aaaa wildcard expressions */
1366 u8 explain; /* True if the EXPLAIN flag is found on the query */
1367 Token sErrToken; /* The token at which the error occurred */
1368 Token sNameToken; /* Token with unqualified schema object name */
1369 Token sLastToken; /* The last token parsed */
1370 const char *zSql; /* All SQL text */
1371 const char *zTail; /* All SQL text past the last semicolon parsed */
1372 Table *pNewTable; /* A table being constructed by CREATE TABLE */
1373 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
1374 TriggerStack *trigStack; /* Trigger actions being coded */
1375 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
1376#ifndef SQLITE_OMIT_VIRTUALTABLE
1377 Token sArg; /* Complete text of a module argument */
1378 u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
1379 Table *pVirtualLock; /* Require virtual table lock on this table */
1380#endif
1381#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1382 int nHeight; /* Expression tree height of current sub-select */
1383#endif
1384};
1385
1386#ifdef SQLITE_OMIT_VIRTUALTABLE
1387 #define IN_DECLARE_VTAB 0
1388#else
1389 #define IN_DECLARE_VTAB (pParse->declareVtab)
1390#endif
1391
1392/*
1393** An instance of the following structure can be declared on a stack and used
1394** to save the Parse.zAuthContext value so that it can be restored later.
1395*/
1396struct AuthContext {
1397 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
1398 Parse *pParse; /* The Parse structure */
1399};
1400
1401/*
1402** Bitfield flags for P2 value in OP_Insert and OP_Delete
1403*/
1404#define OPFLAG_NCHANGE 1 /* Set to update db->nChange */
1405#define OPFLAG_LASTROWID 2 /* Set to update db->lastRowid */
1406#define OPFLAG_ISUPDATE 4 /* This OP_Insert is an sql UPDATE */
1407#define OPFLAG_APPEND 8 /* This is likely to be an append */
1408
1409/*
1410 * Each trigger present in the database schema is stored as an instance of
1411 * struct Trigger.
1412 *
1413 * Pointers to instances of struct Trigger are stored in two ways.
1414 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
1415 * database). This allows Trigger structures to be retrieved by name.
1416 * 2. All triggers associated with a single table form a linked list, using the
1417 * pNext member of struct Trigger. A pointer to the first element of the
1418 * linked list is stored as the "pTrigger" member of the associated
1419 * struct Table.
1420 *
1421 * The "step_list" member points to the first element of a linked list
1422 * containing the SQL statements specified as the trigger program.
1423 */
1424struct Trigger {
1425 char *name; /* The name of the trigger */
1426 char *table; /* The table or view to which the trigger applies */
1427 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
1428 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
1429 Expr *pWhen; /* The WHEN clause of the expresion (may be NULL) */
1430 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
1431 the <column-list> is stored here */
1432 Token nameToken; /* Token containing zName. Use during parsing only */
1433 Schema *pSchema; /* Schema containing the trigger */
1434 Schema *pTabSchema; /* Schema containing the table */
1435 TriggerStep *step_list; /* Link list of trigger program steps */
1436 Trigger *pNext; /* Next trigger associated with the table */
1437};
1438
1439/*
1440** A trigger is either a BEFORE or an AFTER trigger. The following constants
1441** determine which.
1442**
1443** If there are multiple triggers, you might of some BEFORE and some AFTER.
1444** In that cases, the constants below can be ORed together.
1445*/
1446#define TRIGGER_BEFORE 1
1447#define TRIGGER_AFTER 2
1448
1449/*
1450 * An instance of struct TriggerStep is used to store a single SQL statement
1451 * that is a part of a trigger-program.
1452 *
1453 * Instances of struct TriggerStep are stored in a singly linked list (linked
1454 * using the "pNext" member) referenced by the "step_list" member of the
1455 * associated struct Trigger instance. The first element of the linked list is
1456 * the first step of the trigger-program.
1457 *
1458 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
1459 * "SELECT" statement. The meanings of the other members is determined by the
1460 * value of "op" as follows:
1461 *
1462 * (op == TK_INSERT)
1463 * orconf -> stores the ON CONFLICT algorithm
1464 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
1465 * this stores a pointer to the SELECT statement. Otherwise NULL.
1466 * target -> A token holding the name of the table to insert into.
1467 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
1468 * this stores values to be inserted. Otherwise NULL.
1469 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
1470 * statement, then this stores the column-names to be
1471 * inserted into.
1472 *
1473 * (op == TK_DELETE)
1474 * target -> A token holding the name of the table to delete from.
1475 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
1476 * Otherwise NULL.
1477 *
1478 * (op == TK_UPDATE)
1479 * target -> A token holding the name of the table to update rows of.
1480 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
1481 * Otherwise NULL.
1482 * pExprList -> A list of the columns to update and the expressions to update
1483 * them to. See sqlite3Update() documentation of "pChanges"
1484 * argument.
1485 *
1486 */
1487struct TriggerStep {
1488 int op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
1489 int orconf; /* OE_Rollback etc. */
1490 Trigger *pTrig; /* The trigger that this step is a part of */
1491
1492 Select *pSelect; /* Valid for SELECT and sometimes
1493 INSERT steps (when pExprList == 0) */
1494 Token target; /* Valid for DELETE, UPDATE, INSERT steps */
1495 Expr *pWhere; /* Valid for DELETE, UPDATE steps */
1496 ExprList *pExprList; /* Valid for UPDATE statements and sometimes
1497 INSERT steps (when pSelect == 0) */
1498 IdList *pIdList; /* Valid for INSERT statements only */
1499 TriggerStep *pNext; /* Next in the link-list */
1500 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
1501};
1502
1503/*
1504 * An instance of struct TriggerStack stores information required during code
1505 * generation of a single trigger program. While the trigger program is being
1506 * coded, its associated TriggerStack instance is pointed to by the
1507 * "pTriggerStack" member of the Parse structure.
1508 *
1509 * The pTab member points to the table that triggers are being coded on. The
1510 * newIdx member contains the index of the vdbe cursor that points at the temp
1511 * table that stores the new.* references. If new.* references are not valid
1512 * for the trigger being coded (for example an ON DELETE trigger), then newIdx
1513 * is set to -1. The oldIdx member is analogous to newIdx, for old.* references.
1514 *
1515 * The ON CONFLICT policy to be used for the trigger program steps is stored
1516 * as the orconf member. If this is OE_Default, then the ON CONFLICT clause
1517 * specified for individual triggers steps is used.
1518 *
1519 * struct TriggerStack has a "pNext" member, to allow linked lists to be
1520 * constructed. When coding nested triggers (triggers fired by other triggers)
1521 * each nested trigger stores its parent trigger's TriggerStack as the "pNext"
1522 * pointer. Once the nested trigger has been coded, the pNext value is restored
1523 * to the pTriggerStack member of the Parse stucture and coding of the parent
1524 * trigger continues.
1525 *
1526 * Before a nested trigger is coded, the linked list pointed to by the
1527 * pTriggerStack is scanned to ensure that the trigger is not about to be coded
1528 * recursively. If this condition is detected, the nested trigger is not coded.
1529 */
1530struct TriggerStack {
1531 Table *pTab; /* Table that triggers are currently being coded on */
1532 int newIdx; /* Index of vdbe cursor to "new" temp table */
1533 int oldIdx; /* Index of vdbe cursor to "old" temp table */
1534 int orconf; /* Current orconf policy */
1535 int ignoreJump; /* where to jump to for a RAISE(IGNORE) */
1536 Trigger *pTrigger; /* The trigger currently being coded */
1537 TriggerStack *pNext; /* Next trigger down on the trigger stack */
1538};
1539
1540/*
1541** The following structure contains information used by the sqliteFix...
1542** routines as they walk the parse tree to make database references
1543** explicit.
1544*/
1545typedef struct DbFixer DbFixer;
1546struct DbFixer {
1547 Parse *pParse; /* The parsing context. Error messages written here */
1548 const char *zDb; /* Make sure all objects are contained in this database */
1549 const char *zType; /* Type of the container - used for error messages */
1550 const Token *pName; /* Name of the container - used for error messages */
1551};
1552
1553/*
1554** A pointer to this structure is used to communicate information
1555** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
1556*/
1557typedef struct {
1558 sqlite3 *db; /* The database being initialized */
1559 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
1560 char **pzErrMsg; /* Error message stored here */
1561 int rc; /* Result code stored here */
1562} InitData;
1563
1564/*
1565** Assuming zIn points to the first byte of a UTF-8 character,
1566** advance zIn to point to the first byte of the next UTF-8 character.
1567*/
1568#define SQLITE_SKIP_UTF8(zIn) { \
1569 if( (*(zIn++))>=0xc0 ){ \
1570 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
1571 } \
1572}
1573
1574/*
1575** The SQLITE_CORRUPT_BKPT macro can be either a constant (for production
1576** builds) or a function call (for debugging). If it is a function call,
1577** it allows the operator to set a breakpoint at the spot where database
1578** corruption is first detected.
1579*/
1580#ifdef SQLITE_DEBUG
1581 int sqlite3Corrupt(void);
1582# define SQLITE_CORRUPT_BKPT sqlite3Corrupt()
1583#else
1584# define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT
1585#endif
1586
1587/*
1588** Internal function prototypes
1589*/
1590int sqlite3StrICmp(const char *, const char *);
1591int sqlite3StrNICmp(const char *, const char *, int);
1592int sqlite3IsNumber(const char*, int*, u8);
1593
1594void *sqlite3MallocZero(unsigned);
1595void *sqlite3DbMallocZero(sqlite3*, unsigned);
1596void *sqlite3DbMallocRaw(sqlite3*, unsigned);
1597char *sqlite3StrDup(const char*);
1598char *sqlite3StrNDup(const char*, int);
1599char *sqlite3DbStrDup(sqlite3*,const char*);
1600char *sqlite3DbStrNDup(sqlite3*,const char*, int);
1601void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
1602void *sqlite3DbRealloc(sqlite3 *, void *, int);
1603
1604char *sqlite3MPrintf(sqlite3*,const char*, ...);
1605char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
1606#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
1607 void sqlite3DebugPrintf(const char*, ...);
1608 void *sqlite3TextToPtr(const char*);
1609#endif
1610void sqlite3SetString(char **, ...);
1611void sqlite3ErrorMsg(Parse*, const char*, ...);
1612void sqlite3ErrorClear(Parse*);
1613void sqlite3Dequote(char*);
1614void sqlite3DequoteExpr(sqlite3*, Expr*);
1615int sqlite3KeywordCode(const unsigned char*, int);
1616int sqlite3RunParser(Parse*, const char*, char **);
1617void sqlite3FinishCoding(Parse*);
1618Expr *sqlite3Expr(sqlite3*, int, Expr*, Expr*, const Token*);
1619Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
1620Expr *sqlite3RegisterExpr(Parse*,Token*);
1621Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
1622void sqlite3ExprSpan(Expr*,Token*,Token*);
1623Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
1624void sqlite3ExprAssignVarNumber(Parse*, Expr*);
1625void sqlite3ExprDelete(Expr*);
1626ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*,Token*);
1627void sqlite3ExprListDelete(ExprList*);
1628int sqlite3Init(sqlite3*, char**);
1629int sqlite3InitCallback(void*, int, char**, char**);
1630void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
1631void sqlite3ResetInternalSchema(sqlite3*, int);
1632void sqlite3BeginParse(Parse*,int);
1633void sqlite3CommitInternalChanges(sqlite3*);
1634Table *sqlite3ResultSetOfSelect(Parse*,char*,Select*);
1635void sqlite3OpenMasterTable(Parse *, int);
1636void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
1637void sqlite3AddColumn(Parse*,Token*);
1638void sqlite3AddNotNull(Parse*, int);
1639void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
1640void sqlite3AddCheckConstraint(Parse*, Expr*);
1641void sqlite3AddColumnType(Parse*,Token*);
1642void sqlite3AddDefaultValue(Parse*,Expr*);
1643void sqlite3AddCollateType(Parse*, const char*, int);
1644void sqlite3EndTable(Parse*,Token*,Token*,Select*);
1645
1646void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
1647
1648#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1649 int sqlite3ViewGetColumnNames(Parse*,Table*);
1650#else
1651# define sqlite3ViewGetColumnNames(A,B) 0
1652#endif
1653
1654void sqlite3DropTable(Parse*, SrcList*, int, int);
1655void sqlite3DeleteTable(Table*);
1656void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
1657void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
1658IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
1659int sqlite3IdListIndex(IdList*,const char*);
1660SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
1661SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, Token*,
1662 Select*, Expr*, IdList*);
1663void sqlite3SrcListShiftJoinType(SrcList*);
1664void sqlite3SrcListAssignCursors(Parse*, SrcList*);
1665void sqlite3IdListDelete(IdList*);
1666void sqlite3SrcListDelete(SrcList*);
1667void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
1668 Token*, int, int);
1669void sqlite3DropIndex(Parse*, SrcList*, int);
1670int sqlite3Select(Parse*, Select*, int, int, Select*, int, int*, char *aff);
1671Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
1672 Expr*,ExprList*,int,Expr*,Expr*);
1673void sqlite3SelectDelete(Select*);
1674Table *sqlite3SrcListLookup(Parse*, SrcList*);
1675int sqlite3IsReadOnly(Parse*, Table*, int);
1676void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
1677void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
1678void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
1679WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**);
1680void sqlite3WhereEnd(WhereInfo*);
1681void sqlite3ExprCodeGetColumn(Vdbe*, Table*, int, int);
1682void sqlite3ExprCode(Parse*, Expr*);
1683void sqlite3ExprCodeAndCache(Parse*, Expr*);
1684int sqlite3ExprCodeExprList(Parse*, ExprList*);
1685void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
1686void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
1687Table *sqlite3FindTable(sqlite3*,const char*, const char*);
1688Table *sqlite3LocateTable(Parse*,const char*, const char*);
1689Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
1690void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
1691void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
1692void sqlite3Vacuum(Parse*);
1693int sqlite3RunVacuum(char**, sqlite3*);
1694char *sqlite3NameFromToken(sqlite3*, Token*);
1695int sqlite3ExprCompare(Expr*, Expr*);
1696int sqlite3ExprResolveNames(NameContext *, Expr *);
1697int sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
1698int sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
1699Vdbe *sqlite3GetVdbe(Parse*);
1700Expr *sqlite3CreateIdExpr(Parse *, const char*);
1701void sqlite3Randomness(int, void*);
1702void sqlite3RollbackAll(sqlite3*);
1703void sqlite3CodeVerifySchema(Parse*, int);
1704void sqlite3BeginTransaction(Parse*, int);
1705void sqlite3CommitTransaction(Parse*);
1706void sqlite3RollbackTransaction(Parse*);
1707int sqlite3ExprIsConstant(Expr*);
1708int sqlite3ExprIsConstantNotJoin(Expr*);
1709int sqlite3ExprIsConstantOrFunction(Expr*);
1710int sqlite3ExprIsInteger(Expr*, int*);
1711int sqlite3IsRowid(const char*);
1712void sqlite3GenerateRowDelete(sqlite3*, Vdbe*, Table*, int, int);
1713void sqlite3GenerateRowIndexDelete(Vdbe*, Table*, int, char*);
1714void sqlite3GenerateIndexKey(Vdbe*, Index*, int);
1715void sqlite3GenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int);
1716void sqlite3CompleteInsertion(Parse*, Table*, int, char*, int, int, int, int);
1717void sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
1718void sqlite3BeginWriteOperation(Parse*, int, int);
1719Expr *sqlite3ExprDup(sqlite3*,Expr*);
1720void sqlite3TokenCopy(sqlite3*,Token*, Token*);
1721ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
1722SrcList *sqlite3SrcListDup(sqlite3*,SrcList*);
1723IdList *sqlite3IdListDup(sqlite3*,IdList*);
1724Select *sqlite3SelectDup(sqlite3*,Select*);
1725FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
1726void sqlite3RegisterBuiltinFunctions(sqlite3*);
1727void sqlite3RegisterDateTimeFunctions(sqlite3*);
1728int sqlite3SafetyOn(sqlite3*);
1729int sqlite3SafetyOff(sqlite3*);
1730int sqlite3SafetyCheck(sqlite3*);
1731void sqlite3ChangeCookie(sqlite3*, Vdbe*, int);
1732
1733#ifndef SQLITE_OMIT_TRIGGER
1734 void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
1735 Expr*,int, int);
1736 void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
1737 void sqlite3DropTrigger(Parse*, SrcList*, int);
1738 void sqlite3DropTriggerPtr(Parse*, Trigger*);
1739 int sqlite3TriggersExist(Parse*, Table*, int, ExprList*);
1740 int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
1741 int, int);
1742 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
1743 void sqlite3DeleteTriggerStep(TriggerStep*);
1744 TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
1745 TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
1746 ExprList*,Select*,int);
1747 TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int);
1748 TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
1749 void sqlite3DeleteTrigger(Trigger*);
1750 void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
1751#else
1752# define sqlite3TriggersExist(A,B,C,D,E,F) 0
1753# define sqlite3DeleteTrigger(A)
1754# define sqlite3DropTriggerPtr(A,B)
1755# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
1756# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I) 0
1757#endif
1758
1759int sqlite3JoinType(Parse*, Token*, Token*, Token*);
1760void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
1761void sqlite3DeferForeignKey(Parse*, int);
1762#ifndef SQLITE_OMIT_AUTHORIZATION
1763 void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
1764 int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
1765 void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
1766 void sqlite3AuthContextPop(AuthContext*);
1767#else
1768# define sqlite3AuthRead(a,b,c)
1769# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
1770# define sqlite3AuthContextPush(a,b,c)
1771# define sqlite3AuthContextPop(a) ((void)(a))
1772#endif
1773void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
1774void sqlite3Detach(Parse*, Expr*);
1775int sqlite3BtreeFactory(const sqlite3 *db, const char *zFilename,
1776 int omitJournal, int nCache, int flags, Btree **ppBtree);
1777int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
1778int sqlite3FixSrcList(DbFixer*, SrcList*);
1779int sqlite3FixSelect(DbFixer*, Select*);
1780int sqlite3FixExpr(DbFixer*, Expr*);
1781int sqlite3FixExprList(DbFixer*, ExprList*);
1782int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
1783int sqlite3AtoF(const char *z, double*);
1784char *sqlite3_snprintf(int,char*,const char*,...);
1785int sqlite3GetInt32(const char *, int*);
1786int sqlite3FitsIn64Bits(const char *);
1787int sqlite3Utf16ByteLen(const void *pData, int nChar);
1788int sqlite3Utf8CharLen(const char *pData, int nByte);
1789int sqlite3Utf8Read(const u8*, const u8*, const u8**);
1790int sqlite3PutVarint(unsigned char *, u64);
1791int sqlite3GetVarint(const unsigned char *, u64 *);
1792int sqlite3GetVarint32(const unsigned char *, u32 *);
1793int sqlite3VarintLen(u64 v);
1794void sqlite3IndexAffinityStr(Vdbe *, Index *);
1795void sqlite3TableAffinityStr(Vdbe *, Table *);
1796char sqlite3CompareAffinity(Expr *pExpr, char aff2);
1797int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
1798char sqlite3ExprAffinity(Expr *pExpr);
1799int sqlite3Atoi64(const char*, i64*);
1800void sqlite3Error(sqlite3*, int, const char*,...);
1801void *sqlite3HexToBlob(sqlite3*, const char *z);
1802int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
1803const char *sqlite3ErrStr(int);
1804int sqlite3ReadSchema(Parse *pParse);
1805CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char *,int,int);
1806CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName);
1807CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
1808Expr *sqlite3ExprSetColl(Parse *pParse, Expr *, Token *);
1809int sqlite3CheckCollSeq(Parse *, CollSeq *);
1810int sqlite3CheckObjectName(Parse *, const char *);
1811void sqlite3VdbeSetChanges(sqlite3 *, int);
1812
1813const void *sqlite3ValueText(sqlite3_value*, u8);
1814int sqlite3ValueBytes(sqlite3_value*, u8);
1815void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1816 void(*)(void*));
1817void sqlite3ValueFree(sqlite3_value*);
1818sqlite3_value *sqlite3ValueNew(sqlite3 *);
1819char *sqlite3Utf16to8(sqlite3 *, const void*, int);
1820int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1821void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1822#ifndef SQLITE_AMALGAMATION
1823extern const unsigned char sqlite3UpperToLower[];
1824#endif
1825void sqlite3RootPageMoved(Db*, int, int);
1826void sqlite3Reindex(Parse*, Token*, Token*);
1827void sqlite3AlterFunctions(sqlite3*);
1828void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
1829int sqlite3GetToken(const unsigned char *, int *);
1830void sqlite3NestedParse(Parse*, const char*, ...);
1831void sqlite3ExpirePreparedStatements(sqlite3*);
1832void sqlite3CodeSubselect(Parse *, Expr *);
1833int sqlite3SelectResolve(Parse *, Select *, NameContext *);
1834void sqlite3ColumnDefault(Vdbe *, Table *, int);
1835void sqlite3AlterFinishAddColumn(Parse *, Token *);
1836void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
1837CollSeq *sqlite3GetCollSeq(sqlite3*, CollSeq *, const char *, int);
1838char sqlite3AffinityType(const Token*);
1839void sqlite3Analyze(Parse*, Token*, Token*);
1840int sqlite3InvokeBusyHandler(BusyHandler*);
1841int sqlite3FindDb(sqlite3*, Token*);
1842int sqlite3AnalysisLoad(sqlite3*,int iDB);
1843void sqlite3DefaultRowEst(Index*);
1844void sqlite3RegisterLikeFunctions(sqlite3*, int);
1845int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
1846void sqlite3AttachFunctions(sqlite3 *);
1847void sqlite3MinimumFileFormat(Parse*, int, int);
1848void sqlite3SchemaFree(void *);
1849Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
1850int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
1851KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
1852int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
1853 void (*)(sqlite3_context*,int,sqlite3_value **),
1854 void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*));
1855int sqlite3ApiExit(sqlite3 *db, int);
1856void sqlite3AbortOtherActiveVdbes(sqlite3 *, Vdbe *);
1857int sqlite3OpenTempDatabase(Parse *);
1858
1859
1860/*
1861** The interface to the LEMON-generated parser
1862*/
1863void *sqlite3ParserAlloc(void*(*)(size_t));
1864void sqlite3ParserFree(void*, void(*)(void*));
1865void sqlite3Parser(void*, int, Token, Parse*);
1866
1867#ifndef SQLITE_OMIT_LOAD_EXTENSION
1868 void sqlite3CloseExtensions(sqlite3*);
1869 int sqlite3AutoLoadExtensions(sqlite3*);
1870#else
1871# define sqlite3CloseExtensions(X)
1872# define sqlite3AutoLoadExtensions(X) SQLITE_OK
1873#endif
1874
1875#ifndef SQLITE_OMIT_SHARED_CACHE
1876 void sqlite3TableLock(Parse *, int, int, u8, const char *);
1877#else
1878 #define sqlite3TableLock(v,w,x,y,z)
1879#endif
1880
1881#ifdef SQLITE_TEST
1882 int sqlite3Utf8To8(unsigned char*);
1883#endif
1884
1885/*
1886** The MallocDisallow() and MallocAllow() routines are like asserts.
1887** Call them around a section of code that you do not expect to do
1888** any memory allocation.
1889*/
1890#ifdef SQLITE_MEMDEBUG
1891 void sqlite3MallocDisallow(void);
1892 void sqlite3MallocAllow(void);
1893 void sqlite3MallocBenignFailure(int);
1894 void sqlite3MallocEnterBenignBlock(int isBenign);
1895 void sqlite3MallocLeaveBenignBlock();
1896#else
1897# define sqlite3MallocDisallow()
1898# define sqlite3MallocAllow()
1899# define sqlite3MallocBenignFailure(x)
1900# define sqlite3MallocEnterBenignBlock(x);
1901# define sqlite3MallocLeaveBenignBlock();
1902#endif
1903
1904
1905#ifdef SQLITE_OMIT_VIRTUALTABLE
1906# define sqlite3VtabClear(X)
1907# define sqlite3VtabSync(X,Y) (Y)
1908# define sqlite3VtabRollback(X)
1909# define sqlite3VtabCommit(X)
1910#else
1911 void sqlite3VtabClear(Table*);
1912 int sqlite3VtabSync(sqlite3 *db, int rc);
1913 int sqlite3VtabRollback(sqlite3 *db);
1914 int sqlite3VtabCommit(sqlite3 *db);
1915#endif
1916void sqlite3VtabLock(sqlite3_vtab*);
1917void sqlite3VtabUnlock(sqlite3*, sqlite3_vtab*);
1918void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
1919void sqlite3VtabFinishParse(Parse*, Token*);
1920void sqlite3VtabArgInit(Parse*);
1921void sqlite3VtabArgExtend(Parse*, Token*);
1922int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
1923int sqlite3VtabCallConnect(Parse*, Table*);
1924int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
1925int sqlite3VtabBegin(sqlite3 *, sqlite3_vtab *);
1926FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
1927void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
1928int sqlite3Reprepare(Vdbe*);
1929void sqlite3ExprListCheckLength(Parse*, ExprList*, int, const char*);
1930CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
1931
1932#ifdef SQLITE_ENABLE_ATOMIC_WRITE
1933 int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
1934 int sqlite3JournalSize(sqlite3_vfs *);
1935 int sqlite3JournalCreate(sqlite3_file *);
1936#else
1937 #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
1938#endif
1939
1940#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1941 void sqlite3ExprSetHeight(Expr *);
1942 int sqlite3SelectExprHeight(Select *);
1943#else
1944 #define sqlite3ExprSetHeight(x)
1945#endif
1946
1947u32 sqlite3Get4byte(const u8*);
1948void sqlite3Put4byte(u8*, u32);
1949
1950#ifdef SQLITE_SSE
1951#include "sseInt.h"
1952#endif
1953
1954#ifdef SQLITE_DEBUG
1955 void sqlite3ParserTrace(FILE*, char *);
1956#endif
1957
1958/*
1959** If the SQLITE_ENABLE IOTRACE exists then the global variable
1960** sqlite3_io_trace is a pointer to a printf-like routine used to
1961** print I/O tracing messages.
1962*/
1963#ifdef SQLITE_ENABLE_IOTRACE
1964# define IOTRACE(A) if( sqlite3_io_trace ){ sqlite3_io_trace A; }
1965 void sqlite3VdbeIOTraceSql(Vdbe*);
1966#else
1967# define IOTRACE(A)
1968# define sqlite3VdbeIOTraceSql(X)
1969#endif
1970SQLITE_EXTERN void (*sqlite3_io_trace)(const char*,...);
1971
1972#endif
diff --git a/libraries/sqlite/win32/sqliteLimit.h b/libraries/sqlite/win32/sqliteLimit.h
new file mode 100755
index 0000000..cec417d
--- /dev/null
+++ b/libraries/sqlite/win32/sqliteLimit.h
@@ -0,0 +1,169 @@
1/*
2** 2007 May 7
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.
10**
11*************************************************************************
12**
13** This file defines various limits of what SQLite can process.
14**
15** @(#) $Id: sqliteLimit.h,v 1.2 2007/08/24 11:52:29 danielk1977 Exp $
16*/
17
18/*
19** The maximum length of a TEXT or BLOB in bytes. This also
20** limits the size of a row in a table or index.
21**
22** The hard limit is the ability of a 32-bit signed integer
23** to count the size: 2^31-1 or 2147483647.
24*/
25#ifndef SQLITE_MAX_LENGTH
26# define SQLITE_MAX_LENGTH 1000000000
27#endif
28
29/*
30** This is the maximum number of
31**
32** * Columns in a table
33** * Columns in an index
34** * Columns in a view
35** * Terms in the SET clause of an UPDATE statement
36** * Terms in the result set of a SELECT statement
37** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
38** * Terms in the VALUES clause of an INSERT statement
39**
40** The hard upper limit here is 32676. Most database people will
41** tell you that in a well-normalized database, you usually should
42** not have more than a dozen or so columns in any table. And if
43** that is the case, there is no point in having more than a few
44** dozen values in any of the other situations described above.
45*/
46#ifndef SQLITE_MAX_COLUMN
47# define SQLITE_MAX_COLUMN 2000
48#endif
49
50/*
51** The maximum length of a single SQL statement in bytes.
52** The hard limit here is the same as SQLITE_MAX_LENGTH.
53*/
54#ifndef SQLITE_MAX_SQL_LENGTH
55# define SQLITE_MAX_SQL_LENGTH 1000000
56#endif
57
58/*
59** The maximum depth of an expression tree. This is limited to
60** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
61** want to place more severe limits on the complexity of an
62** expression. A value of 0 (the default) means do not enforce
63** any limitation on expression tree depth.
64*/
65#ifndef SQLITE_MAX_EXPR_DEPTH
66# define SQLITE_MAX_EXPR_DEPTH 1000
67#endif
68
69/*
70** The maximum number of terms in a compound SELECT statement.
71** The code generator for compound SELECT statements does one
72** level of recursion for each term. A stack overflow can result
73** if the number of terms is too large. In practice, most SQL
74** never has more than 3 or 4 terms. Use a value of 0 to disable
75** any limit on the number of terms in a compount SELECT.
76*/
77#ifndef SQLITE_MAX_COMPOUND_SELECT
78# define SQLITE_MAX_COMPOUND_SELECT 500
79#endif
80
81/*
82** The maximum number of opcodes in a VDBE program.
83** Not currently enforced.
84*/
85#ifndef SQLITE_MAX_VDBE_OP
86# define SQLITE_MAX_VDBE_OP 25000
87#endif
88
89/*
90** The maximum number of arguments to an SQL function.
91*/
92#ifndef SQLITE_MAX_FUNCTION_ARG
93# define SQLITE_MAX_FUNCTION_ARG 100
94#endif
95
96/*
97** The maximum number of in-memory pages to use for the main database
98** table and for temporary tables. The SQLITE_DEFAULT_CACHE_SIZE
99*/
100#ifndef SQLITE_DEFAULT_CACHE_SIZE
101# define SQLITE_DEFAULT_CACHE_SIZE 2000
102#endif
103#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
104# define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500
105#endif
106
107/*
108** The maximum number of attached databases. This must be at least 2
109** in order to support the main database file (0) and the file used to
110** hold temporary tables (1). And it must be less than 32 because
111** we use a bitmask of databases with a u32 in places (for example
112** the Parse.cookieMask field).
113*/
114#ifndef SQLITE_MAX_ATTACHED
115# define SQLITE_MAX_ATTACHED 10
116#endif
117
118
119/*
120** The maximum value of a ?nnn wildcard that the parser will accept.
121*/
122#ifndef SQLITE_MAX_VARIABLE_NUMBER
123# define SQLITE_MAX_VARIABLE_NUMBER 999
124#endif
125
126/*
127** The default size of a database page.
128*/
129#ifndef SQLITE_DEFAULT_PAGE_SIZE
130# define SQLITE_DEFAULT_PAGE_SIZE 1024
131#endif
132
133/*
134** Ordinarily, if no value is explicitly provided, SQLite creates databases
135** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
136** device characteristics (sector-size and atomic write() support),
137** SQLite may choose a larger value. This constant is the maximum value
138** SQLite will choose on it's own.
139*/
140#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
141# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
142#endif
143
144/* Maximum page size. The upper bound on this value is 32768. This a limit
145** imposed by the necessity of storing the value in a 2-byte unsigned integer
146** and the fact that the page size must be a power of 2.
147*/
148#ifndef SQLITE_MAX_PAGE_SIZE
149# define SQLITE_MAX_PAGE_SIZE 32768
150#endif
151
152/*
153** Maximum number of pages in one database file.
154**
155** This is really just the default value for the max_page_count pragma.
156** This value can be lowered (or raised) at run-time using that the
157** max_page_count macro.
158*/
159#ifndef SQLITE_MAX_PAGE_COUNT
160# define SQLITE_MAX_PAGE_COUNT 1073741823
161#endif
162
163/*
164** Maximum length (in bytes) of the pattern in a LIKE or GLOB
165** operator.
166*/
167#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
168# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
169#endif
diff --git a/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005.sln b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005.sln
new file mode 100755
index 0000000..a1f2084
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005.sln
@@ -0,0 +1,20 @@
1
2Microsoft Visual Studio Solution File, Format Version 9.00
3# Visual C++ Express 2005
4Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sqlite_vs2005", "sqlite_vs2005\sqlite_vs2005.vcproj", "{5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}"
5EndProject
6Global
7 GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 Debug|Win32 = Debug|Win32
9 Release|Win32 = Release|Win32
10 EndGlobalSection
11 GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 {5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}.Debug|Win32.ActiveCfg = Debug|Win32
13 {5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}.Debug|Win32.Build.0 = Debug|Win32
14 {5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}.Release|Win32.ActiveCfg = Release|Win32
15 {5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}.Release|Win32.Build.0 = Release|Win32
16 EndGlobalSection
17 GlobalSection(SolutionProperties) = preSolution
18 HideSolutionNode = FALSE
19 EndGlobalSection
20EndGlobal
diff --git a/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite3.def b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite3.def
new file mode 100755
index 0000000..9eb6a4e
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite3.def
@@ -0,0 +1,131 @@
1EXPORTS
2sqlite3_aggregate_context
3sqlite3_aggregate_count
4sqlite3_apis
5sqlite3_auto_extension
6sqlite3_bind_blob
7sqlite3_bind_double
8sqlite3_bind_int
9sqlite3_bind_int64
10sqlite3_bind_null
11sqlite3_bind_parameter_count
12sqlite3_bind_parameter_index
13sqlite3_bind_parameter_name
14sqlite3_bind_text
15sqlite3_bind_text16
16sqlite3_bind_value
17sqlite3_bind_zeroblob
18sqlite3_blob_bytes
19sqlite3_blob_close
20sqlite3_blob_open
21sqlite3_blob_read
22sqlite3_blob_write
23sqlite3_busy_handler
24sqlite3_busy_timeout
25sqlite3_changes
26sqlite3_clear_bindings
27sqlite3_close
28sqlite3_collation_needed
29sqlite3_collation_needed16
30sqlite3_column_blob
31sqlite3_column_bytes
32sqlite3_column_bytes16
33sqlite3_column_count
34sqlite3_column_decltype
35sqlite3_column_decltype16
36sqlite3_column_double
37sqlite3_column_int
38sqlite3_column_int64
39sqlite3_column_name
40sqlite3_column_name16
41sqlite3_column_text
42sqlite3_column_text16
43sqlite3_column_type
44sqlite3_column_value
45sqlite3_commit_hook
46sqlite3_complete
47sqlite3_complete16
48sqlite3_create_collation
49sqlite3_create_collation16
50sqlite3_create_collation_v2
51sqlite3_create_function
52sqlite3_create_function16
53sqlite3_create_module
54sqlite3_create_module_v2
55sqlite3_data_count
56sqlite3_db_handle
57sqlite3_declare_vtab
58sqlite3_enable_load_extension
59sqlite3_enable_shared_cache
60sqlite3_errcode
61sqlite3_errmsg
62sqlite3_errmsg16
63sqlite3_exec
64sqlite3_expired
65sqlite3_extended_result_codes
66sqlite3_finalize
67sqlite3_free
68sqlite3_free_table
69sqlite3_get_autocommit
70sqlite3_get_auxdata
71sqlite3_get_table
72sqlite3_global_recover
73sqlite3_interrupt
74sqlite3_last_insert_rowid
75sqlite3_libversion
76sqlite3_libversion_number
77sqlite3_load_extension
78sqlite3_malloc
79sqlite3_mprintf
80sqlite3_open
81sqlite3_open16
82sqlite3_overload_function
83sqlite3_prepare
84sqlite3_prepare16
85sqlite3_prepare16_v2
86sqlite3_prepare_v2
87sqlite3_profile
88sqlite3_progress_handler
89sqlite3_realloc
90sqlite3_reset
91sqlite3_reset_auto_extension
92sqlite3_result_blob
93sqlite3_result_double
94sqlite3_result_error
95sqlite3_result_error16
96sqlite3_result_error_toobig
97sqlite3_result_int
98sqlite3_result_int64
99sqlite3_result_null
100sqlite3_result_text
101sqlite3_result_text16
102sqlite3_result_text16be
103sqlite3_result_text16le
104sqlite3_result_value
105sqlite3_result_zeroblob
106sqlite3_rollback_hook
107sqlite3_set_authorizer
108sqlite3_set_auxdata
109sqlite3_sleep
110sqlite3_snprintf
111sqlite3_step
112sqlite3_thread_cleanup
113sqlite3_total_changes
114sqlite3_trace
115sqlite3_transfer_bindings
116sqlite3_update_hook
117sqlite3_user_data
118sqlite3_value_blob
119sqlite3_value_bytes
120sqlite3_value_bytes16
121sqlite3_value_double
122sqlite3_value_int
123sqlite3_value_int64
124sqlite3_value_numeric_type
125sqlite3_value_text
126sqlite3_value_text16
127sqlite3_value_text16be
128sqlite3_value_text16le
129sqlite3_value_type
130sqlite3_version
131sqlite3_vmprintf
diff --git a/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj
new file mode 100755
index 0000000..0e76661
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj
@@ -0,0 +1,497 @@
1<?xml version="1.0" encoding="Windows-1252"?>
2<VisualStudioProject
3 ProjectType="Visual C++"
4 Version="8.00"
5 Name="sqlite_vs2005"
6 ProjectGUID="{5E4E7BB0-CF29-493E-A983-C0E5FCF66F44}"
7 RootNamespace="sqlite_vs2005"
8 Keyword="Win32Proj"
9 >
10 <Platforms>
11 <Platform
12 Name="Win32"
13 />
14 </Platforms>
15 <ToolFiles>
16 </ToolFiles>
17 <Configurations>
18 <Configuration
19 Name="Debug|Win32"
20 OutputDirectory="$(SolutionDir)$(ConfigurationName)"
21 IntermediateDirectory="$(ConfigurationName)"
22 ConfigurationType="1"
23 CharacterSet="1"
24 >
25 <Tool
26 Name="VCPreBuildEventTool"
27 />
28 <Tool
29 Name="VCCustomBuildTool"
30 />
31 <Tool
32 Name="VCXMLDataGeneratorTool"
33 />
34 <Tool
35 Name="VCWebServiceProxyGeneratorTool"
36 />
37 <Tool
38 Name="VCMIDLTool"
39 />
40 <Tool
41 Name="VCCLCompilerTool"
42 Optimization="0"
43 PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
44 MinimalRebuild="true"
45 BasicRuntimeChecks="3"
46 RuntimeLibrary="3"
47 UsePrecompiledHeader="0"
48 WarningLevel="3"
49 Detect64BitPortabilityProblems="true"
50 DebugInformationFormat="4"
51 />
52 <Tool
53 Name="VCManagedResourceCompilerTool"
54 />
55 <Tool
56 Name="VCResourceCompilerTool"
57 />
58 <Tool
59 Name="VCPreLinkEventTool"
60 />
61 <Tool
62 Name="VCLinkerTool"
63 AdditionalDependencies="kernel32.lib $(NoInherit)"
64 LinkIncremental="2"
65 GenerateDebugInformation="true"
66 SubSystem="1"
67 TargetMachine="1"
68 />
69 <Tool
70 Name="VCALinkTool"
71 />
72 <Tool
73 Name="VCManifestTool"
74 />
75 <Tool
76 Name="VCXDCMakeTool"
77 />
78 <Tool
79 Name="VCBscMakeTool"
80 />
81 <Tool
82 Name="VCFxCopTool"
83 />
84 <Tool
85 Name="VCAppVerifierTool"
86 />
87 <Tool
88 Name="VCWebDeploymentTool"
89 />
90 <Tool
91 Name="VCPostBuildEventTool"
92 />
93 </Configuration>
94 <Configuration
95 Name="Release|Win32"
96 OutputDirectory="$(SolutionDir)$(ConfigurationName)"
97 IntermediateDirectory="$(ConfigurationName)"
98 ConfigurationType="2"
99 CharacterSet="1"
100 WholeProgramOptimization="1"
101 >
102 <Tool
103 Name="VCPreBuildEventTool"
104 />
105 <Tool
106 Name="VCCustomBuildTool"
107 />
108 <Tool
109 Name="VCXMLDataGeneratorTool"
110 />
111 <Tool
112 Name="VCWebServiceProxyGeneratorTool"
113 />
114 <Tool
115 Name="VCMIDLTool"
116 />
117 <Tool
118 Name="VCCLCompilerTool"
119 PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
120 RuntimeLibrary="2"
121 UsePrecompiledHeader="0"
122 WarningLevel="3"
123 Detect64BitPortabilityProblems="true"
124 DebugInformationFormat="3"
125 />
126 <Tool
127 Name="VCManagedResourceCompilerTool"
128 />
129 <Tool
130 Name="VCResourceCompilerTool"
131 />
132 <Tool
133 Name="VCPreLinkEventTool"
134 />
135 <Tool
136 Name="VCLinkerTool"
137 AdditionalDependencies="kernel32.lib $(NoInherit)"
138 LinkIncremental="1"
139 AdditionalLibraryDirectories="Kernel32.lib"
140 ModuleDefinitionFile="sqlite3.def"
141 GenerateDebugInformation="true"
142 SubSystem="2"
143 OptimizeReferences="2"
144 EnableCOMDATFolding="2"
145 TargetMachine="1"
146 />
147 <Tool
148 Name="VCALinkTool"
149 />
150 <Tool
151 Name="VCManifestTool"
152 />
153 <Tool
154 Name="VCXDCMakeTool"
155 />
156 <Tool
157 Name="VCBscMakeTool"
158 />
159 <Tool
160 Name="VCFxCopTool"
161 />
162 <Tool
163 Name="VCAppVerifierTool"
164 />
165 <Tool
166 Name="VCWebDeploymentTool"
167 />
168 <Tool
169 Name="VCPostBuildEventTool"
170 />
171 </Configuration>
172 </Configurations>
173 <References>
174 </References>
175 <Files>
176 <Filter
177 Name="Source Files"
178 Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
179 UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
180 >
181 <File
182 RelativePath="..\..\alter.c"
183 >
184 </File>
185 <File
186 RelativePath="..\..\analyze.c"
187 >
188 </File>
189 <File
190 RelativePath="..\..\attach.c"
191 >
192 </File>
193 <File
194 RelativePath="..\..\auth.c"
195 >
196 </File>
197 <File
198 RelativePath="..\..\btmutex.c"
199 >
200 </File>
201 <File
202 RelativePath="..\..\btree.c"
203 >
204 </File>
205 <File
206 RelativePath="..\..\btree.h"
207 >
208 </File>
209 <File
210 RelativePath="..\..\btreeInt.h"
211 >
212 </File>
213 <File
214 RelativePath="..\..\build.c"
215 >
216 </File>
217 <File
218 RelativePath="..\..\callback.c"
219 >
220 </File>
221 <File
222 RelativePath="..\..\complete.c"
223 >
224 </File>
225 <File
226 RelativePath="..\..\date.c"
227 >
228 </File>
229 <File
230 RelativePath="..\..\delete.c"
231 >
232 </File>
233 <File
234 RelativePath="..\..\expr.c"
235 >
236 </File>
237 <File
238 RelativePath="..\..\fts1.h"
239 >
240 </File>
241 <File
242 RelativePath="..\..\fts1_hash.c"
243 >
244 </File>
245 <File
246 RelativePath="..\..\fts1_hash.h"
247 >
248 </File>
249 <File
250 RelativePath="..\..\fts1_porter.c"
251 >
252 </File>
253 <File
254 RelativePath="..\..\fts1_tokenizer.h"
255 >
256 </File>
257 <File
258 RelativePath="..\..\fts1_tokenizer1.c"
259 >
260 </File>
261 <File
262 RelativePath="..\..\func.c"
263 >
264 </File>
265 <File
266 RelativePath="..\..\hash.c"
267 >
268 </File>
269 <File
270 RelativePath="..\..\hash.h"
271 >
272 </File>
273 <File
274 RelativePath="..\..\insert.c"
275 >
276 </File>
277 <File
278 RelativePath="..\..\journal.c"
279 >
280 </File>
281 <File
282 RelativePath="..\..\keywordhash.h"
283 >
284 </File>
285 <File
286 RelativePath="..\..\legacy.c"
287 >
288 </File>
289 <File
290 RelativePath="..\..\loadext.c"
291 >
292 </File>
293 <File
294 RelativePath="..\..\main.c"
295 >
296 </File>
297 <File
298 RelativePath="..\..\malloc.c"
299 >
300 </File>
301 <File
302 RelativePath="..\..\mem1.c"
303 >
304 </File>
305 <File
306 RelativePath="..\..\mem2.c"
307 >
308 </File>
309 <File
310 RelativePath="..\..\mutex.c"
311 >
312 </File>
313 <File
314 RelativePath="..\..\mutex.h"
315 >
316 </File>
317 <File
318 RelativePath="..\..\mutex_w32.c"
319 >
320 </File>
321 <File
322 RelativePath="..\..\opcodes.c"
323 >
324 </File>
325 <File
326 RelativePath="..\..\opcodes.h"
327 >
328 </File>
329 <File
330 RelativePath="..\..\os.c"
331 >
332 </File>
333 <File
334 RelativePath="..\..\os.h"
335 >
336 </File>
337 <File
338 RelativePath="..\..\os_common.h"
339 >
340 </File>
341 <File
342 RelativePath="..\..\os_os2.c"
343 >
344 </File>
345 <File
346 RelativePath="..\..\os_unix.c"
347 >
348 </File>
349 <File
350 RelativePath="..\..\os_win.c"
351 >
352 </File>
353 <File
354 RelativePath="..\..\pager.c"
355 >
356 </File>
357 <File
358 RelativePath="..\..\pager.h"
359 >
360 </File>
361 <File
362 RelativePath="..\..\parse.c"
363 >
364 </File>
365 <File
366 RelativePath="..\..\parse.h"
367 >
368 </File>
369 <File
370 RelativePath="..\..\pragma.c"
371 >
372 </File>
373 <File
374 RelativePath="..\..\prepare.c"
375 >
376 </File>
377 <File
378 RelativePath="..\..\printf.c"
379 >
380 </File>
381 <File
382 RelativePath="..\..\random.c"
383 >
384 </File>
385 <File
386 RelativePath="..\..\select.c"
387 >
388 </File>
389 <File
390 RelativePath="..\..\shell.c"
391 >
392 </File>
393 <File
394 RelativePath="..\..\sqlite3.h"
395 >
396 </File>
397 <File
398 RelativePath="..\..\sqlite3ext.h"
399 >
400 </File>
401 <File
402 RelativePath="..\..\sqliteInt.h"
403 >
404 </File>
405 <File
406 RelativePath="..\..\sqliteLimit.h"
407 >
408 </File>
409 <File
410 RelativePath="..\..\table.c"
411 >
412 </File>
413 <File
414 RelativePath="..\..\tokenize.c"
415 >
416 </File>
417 <File
418 RelativePath="..\..\trigger.c"
419 >
420 </File>
421 <File
422 RelativePath="..\..\update.c"
423 >
424 </File>
425 <File
426 RelativePath="..\..\utf.c"
427 >
428 </File>
429 <File
430 RelativePath="..\..\util.c"
431 >
432 </File>
433 <File
434 RelativePath="..\..\vacuum.c"
435 >
436 </File>
437 <File
438 RelativePath="..\..\vdbe.c"
439 >
440 </File>
441 <File
442 RelativePath="..\..\vdbe.h"
443 >
444 </File>
445 <File
446 RelativePath="..\..\vdbeapi.c"
447 >
448 </File>
449 <File
450 RelativePath="..\..\vdbeaux.c"
451 >
452 </File>
453 <File
454 RelativePath="..\..\vdbeblob.c"
455 >
456 </File>
457 <File
458 RelativePath="..\..\vdbefifo.c"
459 >
460 </File>
461 <File
462 RelativePath="..\..\vdbeInt.h"
463 >
464 </File>
465 <File
466 RelativePath="..\..\vdbemem.c"
467 >
468 </File>
469 <File
470 RelativePath="..\..\vtab.c"
471 >
472 </File>
473 <File
474 RelativePath="..\..\where.c"
475 >
476 </File>
477 </Filter>
478 <Filter
479 Name="Header Files"
480 Filter="h;hpp;hxx;hm;inl;inc;xsd"
481 UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
482 >
483 </Filter>
484 <Filter
485 Name="Resource Files"
486 Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
487 UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
488 >
489 </Filter>
490 <File
491 RelativePath=".\ReadMe.txt"
492 >
493 </File>
494 </Files>
495 <Globals>
496 </Globals>
497</VisualStudioProject>
diff --git a/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj.dan-PC.dan.user b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj.dan-PC.dan.user
new file mode 100755
index 0000000..ebf4b0f
--- /dev/null
+++ b/libraries/sqlite/win32/sqlite_vs2005/sqlite_vs2005/sqlite_vs2005.vcproj.dan-PC.dan.user
@@ -0,0 +1,65 @@
1<?xml version="1.0" encoding="Windows-1252"?>
2<VisualStudioUserFile
3 ProjectType="Visual C++"
4 Version="8.00"
5 ShowAllFiles="false"
6 >
7 <Configurations>
8 <Configuration
9 Name="Debug|Win32"
10 >
11 <DebugSettings
12 Command="$(TargetPath)"
13 WorkingDirectory=""
14 CommandArguments=""
15 Attach="false"
16 DebuggerType="3"
17 Remote="1"
18 RemoteMachine="DAN-PC"
19 RemoteCommand=""
20 HttpUrl=""
21 PDBPath=""
22 SQLDebugging=""
23 Environment=""
24 EnvironmentMerge="true"
25 DebuggerFlavor=""
26 MPIRunCommand=""
27 MPIRunArguments=""
28 MPIRunWorkingDirectory=""
29 ApplicationCommand=""
30 ApplicationArguments=""
31 ShimCommand=""
32 MPIAcceptMode=""
33 MPIAcceptFilter=""
34 />
35 </Configuration>
36 <Configuration
37 Name="Release|Win32"
38 >
39 <DebugSettings
40 Command=""
41 WorkingDirectory=""
42 CommandArguments=""
43 Attach="false"
44 DebuggerType="3"
45 Remote="1"
46 RemoteMachine="DAN-PC"
47 RemoteCommand=""
48 HttpUrl=""
49 PDBPath=""
50 SQLDebugging=""
51 Environment=""
52 EnvironmentMerge="true"
53 DebuggerFlavor=""
54 MPIRunCommand=""
55 MPIRunArguments=""
56 MPIRunWorkingDirectory=""
57 ApplicationCommand=""
58 ApplicationArguments=""
59 ShimCommand=""
60 MPIAcceptMode=""
61 MPIAcceptFilter=""
62 />
63 </Configuration>
64 </Configurations>
65</VisualStudioUserFile>
diff --git a/libraries/sqlite/win32/table.c b/libraries/sqlite/win32/table.c
new file mode 100755
index 0000000..a79a6ac
--- /dev/null
+++ b/libraries/sqlite/win32/table.c
@@ -0,0 +1,206 @@
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.
10**
11*************************************************************************
12** This file contains the sqlite3_get_table() and sqlite3_free_table()
13** interface routines. These are just wrappers around the main
14** interface routine of sqlite3_exec().
15**
16** These routines are in a separate files so that they will not be linked
17** if they are not used.
18*/
19#include "sqliteInt.h"
20#include <stdlib.h>
21#include <string.h>
22
23#ifndef SQLITE_OMIT_GET_TABLE
24
25/*
26** This structure is used to pass data from sqlite3_get_table() through
27** to the callback function is uses to build the result.
28*/
29typedef struct TabResult {
30 char **azResult;
31 char *zErrMsg;
32 int nResult;
33 int nAlloc;
34 int nRow;
35 int nColumn;
36 int nData;
37 int rc;
38} TabResult;
39
40/*
41** This routine is called once for each row in the result table. Its job
42** is to fill in the TabResult structure appropriately, allocating new
43** memory as necessary.
44*/
45static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
46 TabResult *p = (TabResult*)pArg;
47 int need;
48 int i;
49 char *z;
50
51 /* Make sure there is enough space in p->azResult to hold everything
52 ** we need to remember from this invocation of the callback.
53 */
54 if( p->nRow==0 && argv!=0 ){
55 need = nCol*2;
56 }else{
57 need = nCol;
58 }
59 if( p->nData + need >= p->nAlloc ){
60 char **azNew;
61 p->nAlloc = p->nAlloc*2 + need + 1;
62 azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
63 if( azNew==0 ) goto malloc_failed;
64 p->azResult = azNew;
65 }
66
67 /* If this is the first row, then generate an extra row containing
68 ** the names of all columns.
69 */
70 if( p->nRow==0 ){
71 p->nColumn = nCol;
72 for(i=0; i<nCol; i++){
73 if( colv[i]==0 ){
74 z = sqlite3_mprintf("");
75 }else{
76 z = sqlite3_mprintf("%s", colv[i]);
77 }
78 p->azResult[p->nData++] = z;
79 }
80 }else if( p->nColumn!=nCol ){
81 sqlite3SetString(&p->zErrMsg,
82 "sqlite3_get_table() called with two or more incompatible queries",
83 (char*)0);
84 p->rc = SQLITE_ERROR;
85 return 1;
86 }
87
88 /* Copy over the row data
89 */
90 if( argv!=0 ){
91 for(i=0; i<nCol; i++){
92 if( argv[i]==0 ){
93 z = 0;
94 }else{
95 int n = strlen(argv[i])+1;
96 z = sqlite3_malloc( n );
97 if( z==0 ) goto malloc_failed;
98 memcpy(z, argv[i], n);
99 }
100 p->azResult[p->nData++] = z;
101 }
102 p->nRow++;
103 }
104 return 0;
105
106malloc_failed:
107 p->rc = SQLITE_NOMEM;
108 return 1;
109}
110
111/*
112** Query the database. But instead of invoking a callback for each row,
113** malloc() for space to hold the result and return the entire results
114** at the conclusion of the call.
115**
116** The result that is written to ***pazResult is held in memory obtained
117** from malloc(). But the caller cannot free this memory directly.
118** Instead, the entire table should be passed to sqlite3_free_table() when
119** the calling procedure is finished using it.
120*/
121int sqlite3_get_table(
122 sqlite3 *db, /* The database on which the SQL executes */
123 const char *zSql, /* The SQL to be executed */
124 char ***pazResult, /* Write the result table here */
125 int *pnRow, /* Write the number of rows in the result here */
126 int *pnColumn, /* Write the number of columns of result here */
127 char **pzErrMsg /* Write error messages here */
128){
129 int rc;
130 TabResult res;
131 if( pazResult==0 ){ return SQLITE_ERROR; }
132 *pazResult = 0;
133 if( pnColumn ) *pnColumn = 0;
134 if( pnRow ) *pnRow = 0;
135 res.zErrMsg = 0;
136 res.nResult = 0;
137 res.nRow = 0;
138 res.nColumn = 0;
139 res.nData = 1;
140 res.nAlloc = 20;
141 res.rc = SQLITE_OK;
142 res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc );
143 if( res.azResult==0 ) return SQLITE_NOMEM;
144 res.azResult[0] = 0;
145 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
146#ifndef NDEBUG
147 sqlite3_mutex_enter(db->mutex);
148 assert((rc&db->errMask)==rc && (res.rc&db->errMask)==res.rc);
149 sqlite3_mutex_leave(db->mutex);
150#endif
151 if( res.azResult ){
152 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
153 res.azResult[0] = (char*)res.nData;
154 }
155 if( (rc&0xff)==SQLITE_ABORT ){
156 sqlite3_free_table(&res.azResult[1]);
157 if( res.zErrMsg ){
158 if( pzErrMsg ){
159 sqlite3_free(*pzErrMsg);
160 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
161 }
162 sqlite3_free(res.zErrMsg);
163 }
164 sqlite3_mutex_enter(db->mutex);
165 db->errCode = res.rc;
166 sqlite3_mutex_leave(db->mutex);
167 return res.rc;
168 }
169 sqlite3_free(res.zErrMsg);
170 if( rc!=SQLITE_OK ){
171 sqlite3_free_table(&res.azResult[1]);
172 return rc;
173 }
174 if( res.nAlloc>res.nData ){
175 char **azNew;
176 azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
177 if( azNew==0 ){
178 sqlite3_free_table(&res.azResult[1]);
179 return SQLITE_NOMEM;
180 }
181 res.nAlloc = res.nData+1;
182 res.azResult = azNew;
183 }
184 *pazResult = &res.azResult[1];
185 if( pnColumn ) *pnColumn = res.nColumn;
186 if( pnRow ) *pnRow = res.nRow;
187 return rc;
188}
189
190/*
191** This routine frees the space the sqlite3_get_table() malloced.
192*/
193void sqlite3_free_table(
194 char **azResult /* Result returned from from sqlite3_get_table() */
195){
196 if( azResult ){
197 int i, n;
198 azResult--;
199 if( azResult==0 ) return;
200 n = (int)azResult[0];
201 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
202 sqlite3_free(azResult);
203 }
204}
205
206#endif /* SQLITE_OMIT_GET_TABLE */
diff --git a/libraries/sqlite/win32/tclsqlite.c b/libraries/sqlite/win32/tclsqlite.c
new file mode 100755
index 0000000..e81846e
--- /dev/null
+++ b/libraries/sqlite/win32/tclsqlite.c
@@ -0,0 +1,2551 @@
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.
10**
11*************************************************************************
12** A TCL Interface to SQLite. Append this file to sqlite3.c and
13** compile the whole thing to build a TCL-enabled version of SQLite.
14**
15** $Id: tclsqlite.c,v 1.203 2007/09/14 16:20:01 danielk1977 Exp $
16*/
17#include "tcl.h"
18#include <errno.h>
19
20/*
21** Some additional include files are needed if this file is not
22** appended to the amalgamation.
23*/
24#ifndef SQLITE_AMALGAMATION
25# include "sqliteInt.h"
26# include <stdlib.h>
27# include <string.h>
28# include <assert.h>
29# include <ctype.h>
30#endif
31
32/*
33 * Windows needs to know which symbols to export. Unix does not.
34 * BUILD_sqlite should be undefined for Unix.
35 */
36#ifdef BUILD_sqlite
37#undef TCL_STORAGE_CLASS
38#define TCL_STORAGE_CLASS DLLEXPORT
39#endif /* BUILD_sqlite */
40
41#define NUM_PREPARED_STMTS 10
42#define MAX_PREPARED_STMTS 100
43
44/*
45** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
46** have to do a translation when going between the two. Set the
47** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
48** this translation.
49*/
50#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
51# define UTF_TRANSLATION_NEEDED 1
52#endif
53
54/*
55** New SQL functions can be created as TCL scripts. Each such function
56** is described by an instance of the following structure.
57*/
58typedef struct SqlFunc SqlFunc;
59struct SqlFunc {
60 Tcl_Interp *interp; /* The TCL interpret to execute the function */
61 Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */
62 int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */
63 char *zName; /* Name of this function */
64 SqlFunc *pNext; /* Next function on the list of them all */
65};
66
67/*
68** New collation sequences function can be created as TCL scripts. Each such
69** function is described by an instance of the following structure.
70*/
71typedef struct SqlCollate SqlCollate;
72struct SqlCollate {
73 Tcl_Interp *interp; /* The TCL interpret to execute the function */
74 char *zScript; /* The script to be run */
75 SqlCollate *pNext; /* Next function on the list of them all */
76};
77
78/*
79** Prepared statements are cached for faster execution. Each prepared
80** statement is described by an instance of the following structure.
81*/
82typedef struct SqlPreparedStmt SqlPreparedStmt;
83struct SqlPreparedStmt {
84 SqlPreparedStmt *pNext; /* Next in linked list */
85 SqlPreparedStmt *pPrev; /* Previous on the list */
86 sqlite3_stmt *pStmt; /* The prepared statement */
87 int nSql; /* chars in zSql[] */
88 char zSql[1]; /* Text of the SQL statement */
89};
90
91typedef struct IncrblobChannel IncrblobChannel;
92
93/*
94** There is one instance of this structure for each SQLite database
95** that has been opened by the SQLite TCL interface.
96*/
97typedef struct SqliteDb SqliteDb;
98struct SqliteDb {
99 sqlite3 *db; /* The "real" database structure. MUST BE FIRST */
100 Tcl_Interp *interp; /* The interpreter used for this database */
101 char *zBusy; /* The busy callback routine */
102 char *zCommit; /* The commit hook callback routine */
103 char *zTrace; /* The trace callback routine */
104 char *zProfile; /* The profile callback routine */
105 char *zProgress; /* The progress callback routine */
106 char *zAuth; /* The authorization callback routine */
107 char *zNull; /* Text to substitute for an SQL NULL value */
108 SqlFunc *pFunc; /* List of SQL functions */
109 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */
110 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */
111 SqlCollate *pCollate; /* List of SQL collation functions */
112 int rc; /* Return code of most recent sqlite3_exec() */
113 Tcl_Obj *pCollateNeeded; /* Collation needed script */
114 SqlPreparedStmt *stmtList; /* List of prepared statements*/
115 SqlPreparedStmt *stmtLast; /* Last statement in the list */
116 int maxStmt; /* The next maximum number of stmtList */
117 int nStmt; /* Number of statements in stmtList */
118 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */
119};
120
121struct IncrblobChannel {
122 sqlite3_blob *pBlob; /* sqlite3 blob handle */
123 SqliteDb *pDb; /* Associated database connection */
124 int iSeek; /* Current seek offset */
125 Tcl_Channel channel; /* Channel identifier */
126 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */
127 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */
128};
129
130#ifndef SQLITE_OMIT_INCRBLOB
131/*
132** Close all incrblob channels opened using database connection pDb.
133** This is called when shutting down the database connection.
134*/
135static void closeIncrblobChannels(SqliteDb *pDb){
136 IncrblobChannel *p;
137 IncrblobChannel *pNext;
138
139 for(p=pDb->pIncrblob; p; p=pNext){
140 pNext = p->pNext;
141
142 /* Note: Calling unregister here call Tcl_Close on the incrblob channel,
143 ** which deletes the IncrblobChannel structure at *p. So do not
144 ** call Tcl_Free() here.
145 */
146 Tcl_UnregisterChannel(pDb->interp, p->channel);
147 }
148}
149
150/*
151** Close an incremental blob channel.
152*/
153static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){
154 IncrblobChannel *p = (IncrblobChannel *)instanceData;
155 int rc = sqlite3_blob_close(p->pBlob);
156 sqlite3 *db = p->pDb->db;
157
158 /* Remove the channel from the SqliteDb.pIncrblob list. */
159 if( p->pNext ){
160 p->pNext->pPrev = p->pPrev;
161 }
162 if( p->pPrev ){
163 p->pPrev->pNext = p->pNext;
164 }
165 if( p->pDb->pIncrblob==p ){
166 p->pDb->pIncrblob = p->pNext;
167 }
168
169 /* Free the IncrblobChannel structure */
170 Tcl_Free((char *)p);
171
172 if( rc!=SQLITE_OK ){
173 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
174 return TCL_ERROR;
175 }
176 return TCL_OK;
177}
178
179/*
180** Read data from an incremental blob channel.
181*/
182static int incrblobInput(
183 ClientData instanceData,
184 char *buf,
185 int bufSize,
186 int *errorCodePtr
187){
188 IncrblobChannel *p = (IncrblobChannel *)instanceData;
189 int nRead = bufSize; /* Number of bytes to read */
190 int nBlob; /* Total size of the blob */
191 int rc; /* sqlite error code */
192
193 nBlob = sqlite3_blob_bytes(p->pBlob);
194 if( (p->iSeek+nRead)>nBlob ){
195 nRead = nBlob-p->iSeek;
196 }
197 if( nRead<=0 ){
198 return 0;
199 }
200
201 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek);
202 if( rc!=SQLITE_OK ){
203 *errorCodePtr = rc;
204 return -1;
205 }
206
207 p->iSeek += nRead;
208 return nRead;
209}
210
211/*
212** Write data to an incremental blob channel.
213*/
214static int incrblobOutput(
215 ClientData instanceData,
216 CONST char *buf,
217 int toWrite,
218 int *errorCodePtr
219){
220 IncrblobChannel *p = (IncrblobChannel *)instanceData;
221 int nWrite = toWrite; /* Number of bytes to write */
222 int nBlob; /* Total size of the blob */
223 int rc; /* sqlite error code */
224
225 nBlob = sqlite3_blob_bytes(p->pBlob);
226 if( (p->iSeek+nWrite)>nBlob ){
227 *errorCodePtr = EINVAL;
228 return -1;
229 }
230 if( nWrite<=0 ){
231 return 0;
232 }
233
234 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek);
235 if( rc!=SQLITE_OK ){
236 *errorCodePtr = EIO;
237 return -1;
238 }
239
240 p->iSeek += nWrite;
241 return nWrite;
242}
243
244/*
245** Seek an incremental blob channel.
246*/
247static int incrblobSeek(
248 ClientData instanceData,
249 long offset,
250 int seekMode,
251 int *errorCodePtr
252){
253 IncrblobChannel *p = (IncrblobChannel *)instanceData;
254
255 switch( seekMode ){
256 case SEEK_SET:
257 p->iSeek = offset;
258 break;
259 case SEEK_CUR:
260 p->iSeek += offset;
261 break;
262 case SEEK_END:
263 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset;
264 break;
265
266 default: assert(!"Bad seekMode");
267 }
268
269 return p->iSeek;
270}
271
272
273static void incrblobWatch(ClientData instanceData, int mode){
274 /* NO-OP */
275}
276static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){
277 return TCL_ERROR;
278}
279
280static Tcl_ChannelType IncrblobChannelType = {
281 "incrblob", /* typeName */
282 TCL_CHANNEL_VERSION_2, /* version */
283 incrblobClose, /* closeProc */
284 incrblobInput, /* inputProc */
285 incrblobOutput, /* outputProc */
286 incrblobSeek, /* seekProc */
287 0, /* setOptionProc */
288 0, /* getOptionProc */
289 incrblobWatch, /* watchProc (this is a no-op) */
290 incrblobHandle, /* getHandleProc (always returns error) */
291 0, /* close2Proc */
292 0, /* blockModeProc */
293 0, /* flushProc */
294 0, /* handlerProc */
295 0, /* wideSeekProc */
296};
297
298/*
299** Create a new incrblob channel.
300*/
301static int createIncrblobChannel(
302 Tcl_Interp *interp,
303 SqliteDb *pDb,
304 const char *zDb,
305 const char *zTable,
306 const char *zColumn,
307 sqlite_int64 iRow,
308 int isReadonly
309){
310 IncrblobChannel *p;
311 sqlite3 *db = pDb->db;
312 sqlite3_blob *pBlob;
313 int rc;
314 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE);
315
316 /* This variable is used to name the channels: "incrblob_[incr count]" */
317 static int count = 0;
318 char zChannel[64];
319
320 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob);
321 if( rc!=SQLITE_OK ){
322 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
323 return TCL_ERROR;
324 }
325
326 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel));
327 p->iSeek = 0;
328 p->pBlob = pBlob;
329
330 sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count);
331 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags);
332 Tcl_RegisterChannel(interp, p->channel);
333
334 /* Link the new channel into the SqliteDb.pIncrblob list. */
335 p->pNext = pDb->pIncrblob;
336 p->pPrev = 0;
337 if( p->pNext ){
338 p->pNext->pPrev = p;
339 }
340 pDb->pIncrblob = p;
341 p->pDb = pDb;
342
343 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE);
344 return TCL_OK;
345}
346#else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
347 #define closeIncrblobChannels(pDb)
348#endif
349
350/*
351** Look at the script prefix in pCmd. We will be executing this script
352** after first appending one or more arguments. This routine analyzes
353** the script to see if it is safe to use Tcl_EvalObjv() on the script
354** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
355** faster.
356**
357** Scripts that are safe to use with Tcl_EvalObjv() consists of a
358** command name followed by zero or more arguments with no [...] or $
359** or {...} or ; to be seen anywhere. Most callback scripts consist
360** of just a single procedure name and they meet this requirement.
361*/
362static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){
363 /* We could try to do something with Tcl_Parse(). But we will instead
364 ** just do a search for forbidden characters. If any of the forbidden
365 ** characters appear in pCmd, we will report the string as unsafe.
366 */
367 const char *z;
368 int n;
369 z = Tcl_GetStringFromObj(pCmd, &n);
370 while( n-- > 0 ){
371 int c = *(z++);
372 if( c=='$' || c=='[' || c==';' ) return 0;
373 }
374 return 1;
375}
376
377/*
378** Find an SqlFunc structure with the given name. Or create a new
379** one if an existing one cannot be found. Return a pointer to the
380** structure.
381*/
382static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
383 SqlFunc *p, *pNew;
384 int i;
385 pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen(zName) + 1 );
386 pNew->zName = (char*)&pNew[1];
387 for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); }
388 pNew->zName[i] = 0;
389 for(p=pDb->pFunc; p; p=p->pNext){
390 if( strcmp(p->zName, pNew->zName)==0 ){
391 Tcl_Free((char*)pNew);
392 return p;
393 }
394 }
395 pNew->interp = pDb->interp;
396 pNew->pScript = 0;
397 pNew->pNext = pDb->pFunc;
398 pDb->pFunc = pNew;
399 return pNew;
400}
401
402/*
403** Finalize and free a list of prepared statements
404*/
405static void flushStmtCache( SqliteDb *pDb ){
406 SqlPreparedStmt *pPreStmt;
407
408 while( pDb->stmtList ){
409 sqlite3_finalize( pDb->stmtList->pStmt );
410 pPreStmt = pDb->stmtList;
411 pDb->stmtList = pDb->stmtList->pNext;
412 Tcl_Free( (char*)pPreStmt );
413 }
414 pDb->nStmt = 0;
415 pDb->stmtLast = 0;
416}
417
418/*
419** TCL calls this procedure when an sqlite3 database command is
420** deleted.
421*/
422static void DbDeleteCmd(void *db){
423 SqliteDb *pDb = (SqliteDb*)db;
424 flushStmtCache(pDb);
425 closeIncrblobChannels(pDb);
426 sqlite3_close(pDb->db);
427 while( pDb->pFunc ){
428 SqlFunc *pFunc = pDb->pFunc;
429 pDb->pFunc = pFunc->pNext;
430 Tcl_DecrRefCount(pFunc->pScript);
431 Tcl_Free((char*)pFunc);
432 }
433 while( pDb->pCollate ){
434 SqlCollate *pCollate = pDb->pCollate;
435 pDb->pCollate = pCollate->pNext;
436 Tcl_Free((char*)pCollate);
437 }
438 if( pDb->zBusy ){
439 Tcl_Free(pDb->zBusy);
440 }
441 if( pDb->zTrace ){
442 Tcl_Free(pDb->zTrace);
443 }
444 if( pDb->zProfile ){
445 Tcl_Free(pDb->zProfile);
446 }
447 if( pDb->zAuth ){
448 Tcl_Free(pDb->zAuth);
449 }
450 if( pDb->zNull ){
451 Tcl_Free(pDb->zNull);
452 }
453 if( pDb->pUpdateHook ){
454 Tcl_DecrRefCount(pDb->pUpdateHook);
455 }
456 if( pDb->pRollbackHook ){
457 Tcl_DecrRefCount(pDb->pRollbackHook);
458 }
459 if( pDb->pCollateNeeded ){
460 Tcl_DecrRefCount(pDb->pCollateNeeded);
461 }
462 Tcl_Free((char*)pDb);
463}
464
465/*
466** This routine is called when a database file is locked while trying
467** to execute SQL.
468*/
469static int DbBusyHandler(void *cd, int nTries){
470 SqliteDb *pDb = (SqliteDb*)cd;
471 int rc;
472 char zVal[30];
473
474 sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries);
475 rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0);
476 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
477 return 0;
478 }
479 return 1;
480}
481
482/*
483** This routine is invoked as the 'progress callback' for the database.
484*/
485static int DbProgressHandler(void *cd){
486 SqliteDb *pDb = (SqliteDb*)cd;
487 int rc;
488
489 assert( pDb->zProgress );
490 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
491 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
492 return 1;
493 }
494 return 0;
495}
496
497#ifndef SQLITE_OMIT_TRACE
498/*
499** This routine is called by the SQLite trace handler whenever a new
500** block of SQL is executed. The TCL script in pDb->zTrace is executed.
501*/
502static void DbTraceHandler(void *cd, const char *zSql){
503 SqliteDb *pDb = (SqliteDb*)cd;
504 Tcl_DString str;
505
506 Tcl_DStringInit(&str);
507 Tcl_DStringAppend(&str, pDb->zTrace, -1);
508 Tcl_DStringAppendElement(&str, zSql);
509 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
510 Tcl_DStringFree(&str);
511 Tcl_ResetResult(pDb->interp);
512}
513#endif
514
515#ifndef SQLITE_OMIT_TRACE
516/*
517** This routine is called by the SQLite profile handler after a statement
518** SQL has executed. The TCL script in pDb->zProfile is evaluated.
519*/
520static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
521 SqliteDb *pDb = (SqliteDb*)cd;
522 Tcl_DString str;
523 char zTm[100];
524
525 sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm);
526 Tcl_DStringInit(&str);
527 Tcl_DStringAppend(&str, pDb->zProfile, -1);
528 Tcl_DStringAppendElement(&str, zSql);
529 Tcl_DStringAppendElement(&str, zTm);
530 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
531 Tcl_DStringFree(&str);
532 Tcl_ResetResult(pDb->interp);
533}
534#endif
535
536/*
537** This routine is called when a transaction is committed. The
538** TCL script in pDb->zCommit is executed. If it returns non-zero or
539** if it throws an exception, the transaction is rolled back instead
540** of being committed.
541*/
542static int DbCommitHandler(void *cd){
543 SqliteDb *pDb = (SqliteDb*)cd;
544 int rc;
545
546 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
547 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
548 return 1;
549 }
550 return 0;
551}
552
553static void DbRollbackHandler(void *clientData){
554 SqliteDb *pDb = (SqliteDb*)clientData;
555 assert(pDb->pRollbackHook);
556 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){
557 Tcl_BackgroundError(pDb->interp);
558 }
559}
560
561static void DbUpdateHandler(
562 void *p,
563 int op,
564 const char *zDb,
565 const char *zTbl,
566 sqlite_int64 rowid
567){
568 SqliteDb *pDb = (SqliteDb *)p;
569 Tcl_Obj *pCmd;
570
571 assert( pDb->pUpdateHook );
572 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
573
574 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook);
575 Tcl_IncrRefCount(pCmd);
576 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(
577 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1));
578 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
579 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
580 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid));
581 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
582}
583
584static void tclCollateNeeded(
585 void *pCtx,
586 sqlite3 *db,
587 int enc,
588 const char *zName
589){
590 SqliteDb *pDb = (SqliteDb *)pCtx;
591 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
592 Tcl_IncrRefCount(pScript);
593 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
594 Tcl_EvalObjEx(pDb->interp, pScript, 0);
595 Tcl_DecrRefCount(pScript);
596}
597
598/*
599** This routine is called to evaluate an SQL collation function implemented
600** using TCL script.
601*/
602static int tclSqlCollate(
603 void *pCtx,
604 int nA,
605 const void *zA,
606 int nB,
607 const void *zB
608){
609 SqlCollate *p = (SqlCollate *)pCtx;
610 Tcl_Obj *pCmd;
611
612 pCmd = Tcl_NewStringObj(p->zScript, -1);
613 Tcl_IncrRefCount(pCmd);
614 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
615 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
616 Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
617 Tcl_DecrRefCount(pCmd);
618 return (atoi(Tcl_GetStringResult(p->interp)));
619}
620
621/*
622** This routine is called to evaluate an SQL function implemented
623** using TCL script.
624*/
625static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
626 SqlFunc *p = sqlite3_user_data(context);
627 Tcl_Obj *pCmd;
628 int i;
629 int rc;
630
631 if( argc==0 ){
632 /* If there are no arguments to the function, call Tcl_EvalObjEx on the
633 ** script object directly. This allows the TCL compiler to generate
634 ** bytecode for the command on the first invocation and thus make
635 ** subsequent invocations much faster. */
636 pCmd = p->pScript;
637 Tcl_IncrRefCount(pCmd);
638 rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
639 Tcl_DecrRefCount(pCmd);
640 }else{
641 /* If there are arguments to the function, make a shallow copy of the
642 ** script object, lappend the arguments, then evaluate the copy.
643 **
644 ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated.
645 ** The new Tcl_Obj contains pointers to the original list elements.
646 ** That way, when Tcl_EvalObjv() is run and shimmers the first element
647 ** of the list to tclCmdNameType, that alternate representation will
648 ** be preserved and reused on the next invocation.
649 */
650 Tcl_Obj **aArg;
651 int nArg;
652 if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
653 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
654 return;
655 }
656 pCmd = Tcl_NewListObj(nArg, aArg);
657 Tcl_IncrRefCount(pCmd);
658 for(i=0; i<argc; i++){
659 sqlite3_value *pIn = argv[i];
660 Tcl_Obj *pVal;
661
662 /* Set pVal to contain the i'th column of this row. */
663 switch( sqlite3_value_type(pIn) ){
664 case SQLITE_BLOB: {
665 int bytes = sqlite3_value_bytes(pIn);
666 pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes);
667 break;
668 }
669 case SQLITE_INTEGER: {
670 sqlite_int64 v = sqlite3_value_int64(pIn);
671 if( v>=-2147483647 && v<=2147483647 ){
672 pVal = Tcl_NewIntObj(v);
673 }else{
674 pVal = Tcl_NewWideIntObj(v);
675 }
676 break;
677 }
678 case SQLITE_FLOAT: {
679 double r = sqlite3_value_double(pIn);
680 pVal = Tcl_NewDoubleObj(r);
681 break;
682 }
683 case SQLITE_NULL: {
684 pVal = Tcl_NewStringObj("", 0);
685 break;
686 }
687 default: {
688 int bytes = sqlite3_value_bytes(pIn);
689 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
690 break;
691 }
692 }
693 rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
694 if( rc ){
695 Tcl_DecrRefCount(pCmd);
696 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
697 return;
698 }
699 }
700 if( !p->useEvalObjv ){
701 /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
702 ** is a list without a string representation. To prevent this from
703 ** happening, make sure pCmd has a valid string representation */
704 Tcl_GetString(pCmd);
705 }
706 rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
707 Tcl_DecrRefCount(pCmd);
708 }
709
710 if( rc && rc!=TCL_RETURN ){
711 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
712 }else{
713 Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
714 int n;
715 u8 *data;
716 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
717 char c = zType[0];
718 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
719 /* Only return a BLOB type if the Tcl variable is a bytearray and
720 ** has no string representation. */
721 data = Tcl_GetByteArrayFromObj(pVar, &n);
722 sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT);
723 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
724 Tcl_GetIntFromObj(0, pVar, &n);
725 sqlite3_result_int(context, n);
726 }else if( c=='d' && strcmp(zType,"double")==0 ){
727 double r;
728 Tcl_GetDoubleFromObj(0, pVar, &r);
729 sqlite3_result_double(context, r);
730 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
731 (c=='i' && strcmp(zType,"int")==0) ){
732 Tcl_WideInt v;
733 Tcl_GetWideIntFromObj(0, pVar, &v);
734 sqlite3_result_int64(context, v);
735 }else{
736 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
737 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT);
738 }
739 }
740}
741
742#ifndef SQLITE_OMIT_AUTHORIZATION
743/*
744** This is the authentication function. It appends the authentication
745** type code and the two arguments to zCmd[] then invokes the result
746** on the interpreter. The reply is examined to determine if the
747** authentication fails or succeeds.
748*/
749static int auth_callback(
750 void *pArg,
751 int code,
752 const char *zArg1,
753 const char *zArg2,
754 const char *zArg3,
755 const char *zArg4
756){
757 char *zCode;
758 Tcl_DString str;
759 int rc;
760 const char *zReply;
761 SqliteDb *pDb = (SqliteDb*)pArg;
762
763 switch( code ){
764 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
765 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
766 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
767 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
768 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
769 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
770 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
771 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
772 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
773 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
774 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
775 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
776 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
777 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
778 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
779 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
780 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
781 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
782 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
783 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
784 case SQLITE_READ : zCode="SQLITE_READ"; break;
785 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
786 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
787 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
788 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
789 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
790 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
791 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
792 case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break;
793 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break;
794 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break;
795 case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break;
796 default : zCode="????"; break;
797 }
798 Tcl_DStringInit(&str);
799 Tcl_DStringAppend(&str, pDb->zAuth, -1);
800 Tcl_DStringAppendElement(&str, zCode);
801 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
802 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
803 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
804 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
805 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
806 Tcl_DStringFree(&str);
807 zReply = Tcl_GetStringResult(pDb->interp);
808 if( strcmp(zReply,"SQLITE_OK")==0 ){
809 rc = SQLITE_OK;
810 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
811 rc = SQLITE_DENY;
812 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
813 rc = SQLITE_IGNORE;
814 }else{
815 rc = 999;
816 }
817 return rc;
818}
819#endif /* SQLITE_OMIT_AUTHORIZATION */
820
821/*
822** zText is a pointer to text obtained via an sqlite3_result_text()
823** or similar interface. This routine returns a Tcl string object,
824** reference count set to 0, containing the text. If a translation
825** between iso8859 and UTF-8 is required, it is preformed.
826*/
827static Tcl_Obj *dbTextToObj(char const *zText){
828 Tcl_Obj *pVal;
829#ifdef UTF_TRANSLATION_NEEDED
830 Tcl_DString dCol;
831 Tcl_DStringInit(&dCol);
832 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
833 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
834 Tcl_DStringFree(&dCol);
835#else
836 pVal = Tcl_NewStringObj(zText, -1);
837#endif
838 return pVal;
839}
840
841/*
842** This routine reads a line of text from FILE in, stores
843** the text in memory obtained from malloc() and returns a pointer
844** to the text. NULL is returned at end of file, or if malloc()
845** fails.
846**
847** The interface is like "readline" but no command-line editing
848** is done.
849**
850** copied from shell.c from '.import' command
851*/
852static char *local_getline(char *zPrompt, FILE *in){
853 char *zLine;
854 int nLine;
855 int n;
856 int eol;
857
858 nLine = 100;
859 zLine = malloc( nLine );
860 if( zLine==0 ) return 0;
861 n = 0;
862 eol = 0;
863 while( !eol ){
864 if( n+100>nLine ){
865 nLine = nLine*2 + 100;
866 zLine = realloc(zLine, nLine);
867 if( zLine==0 ) return 0;
868 }
869 if( fgets(&zLine[n], nLine - n, in)==0 ){
870 if( n==0 ){
871 free(zLine);
872 return 0;
873 }
874 zLine[n] = 0;
875 eol = 1;
876 break;
877 }
878 while( zLine[n] ){ n++; }
879 if( n>0 && zLine[n-1]=='\n' ){
880 n--;
881 zLine[n] = 0;
882 eol = 1;
883 }
884 }
885 zLine = realloc( zLine, n+1 );
886 return zLine;
887}
888
889/*
890** The "sqlite" command below creates a new Tcl command for each
891** connection it opens to an SQLite database. This routine is invoked
892** whenever one of those connection-specific commands is executed
893** in Tcl. For example, if you run Tcl code like this:
894**
895** sqlite3 db1 "my_database"
896** db1 close
897**
898** The first command opens a connection to the "my_database" database
899** and calls that connection "db1". The second command causes this
900** subroutine to be invoked.
901*/
902static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
903 SqliteDb *pDb = (SqliteDb*)cd;
904 int choice;
905 int rc = TCL_OK;
906 static const char *DB_strs[] = {
907 "authorizer", "busy", "cache",
908 "changes", "close", "collate",
909 "collation_needed", "commit_hook", "complete",
910 "copy", "enable_load_extension","errorcode",
911 "eval", "exists", "function",
912 "incrblob", "interrupt", "last_insert_rowid",
913 "nullvalue", "onecolumn", "profile",
914 "progress", "rekey", "rollback_hook",
915 "timeout", "total_changes", "trace",
916 "transaction", "update_hook", "version",
917 0
918 };
919 enum DB_enum {
920 DB_AUTHORIZER, DB_BUSY, DB_CACHE,
921 DB_CHANGES, DB_CLOSE, DB_COLLATE,
922 DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE,
923 DB_COPY, DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE,
924 DB_EVAL, DB_EXISTS, DB_FUNCTION,
925 DB_INCRBLOB, DB_INTERRUPT, DB_LAST_INSERT_ROWID,
926 DB_NULLVALUE, DB_ONECOLUMN, DB_PROFILE,
927 DB_PROGRESS, DB_REKEY, DB_ROLLBACK_HOOK,
928 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
929 DB_TRANSACTION, DB_UPDATE_HOOK, DB_VERSION
930 };
931 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
932
933 if( objc<2 ){
934 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
935 return TCL_ERROR;
936 }
937 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
938 return TCL_ERROR;
939 }
940
941 switch( (enum DB_enum)choice ){
942
943 /* $db authorizer ?CALLBACK?
944 **
945 ** Invoke the given callback to authorize each SQL operation as it is
946 ** compiled. 5 arguments are appended to the callback before it is
947 ** invoked:
948 **
949 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
950 ** (2) First descriptive name (depends on authorization type)
951 ** (3) Second descriptive name
952 ** (4) Name of the database (ex: "main", "temp")
953 ** (5) Name of trigger that is doing the access
954 **
955 ** The callback should return on of the following strings: SQLITE_OK,
956 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
957 **
958 ** If this method is invoked with no arguments, the current authorization
959 ** callback string is returned.
960 */
961 case DB_AUTHORIZER: {
962#ifdef SQLITE_OMIT_AUTHORIZATION
963 Tcl_AppendResult(interp, "authorization not available in this build", 0);
964 return TCL_ERROR;
965#else
966 if( objc>3 ){
967 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
968 return TCL_ERROR;
969 }else if( objc==2 ){
970 if( pDb->zAuth ){
971 Tcl_AppendResult(interp, pDb->zAuth, 0);
972 }
973 }else{
974 char *zAuth;
975 int len;
976 if( pDb->zAuth ){
977 Tcl_Free(pDb->zAuth);
978 }
979 zAuth = Tcl_GetStringFromObj(objv[2], &len);
980 if( zAuth && len>0 ){
981 pDb->zAuth = Tcl_Alloc( len + 1 );
982 memcpy(pDb->zAuth, zAuth, len+1);
983 }else{
984 pDb->zAuth = 0;
985 }
986 if( pDb->zAuth ){
987 pDb->interp = interp;
988 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
989 }else{
990 sqlite3_set_authorizer(pDb->db, 0, 0);
991 }
992 }
993#endif
994 break;
995 }
996
997 /* $db busy ?CALLBACK?
998 **
999 ** Invoke the given callback if an SQL statement attempts to open
1000 ** a locked database file.
1001 */
1002 case DB_BUSY: {
1003 if( objc>3 ){
1004 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
1005 return TCL_ERROR;
1006 }else if( objc==2 ){
1007 if( pDb->zBusy ){
1008 Tcl_AppendResult(interp, pDb->zBusy, 0);
1009 }
1010 }else{
1011 char *zBusy;
1012 int len;
1013 if( pDb->zBusy ){
1014 Tcl_Free(pDb->zBusy);
1015 }
1016 zBusy = Tcl_GetStringFromObj(objv[2], &len);
1017 if( zBusy && len>0 ){
1018 pDb->zBusy = Tcl_Alloc( len + 1 );
1019 memcpy(pDb->zBusy, zBusy, len+1);
1020 }else{
1021 pDb->zBusy = 0;
1022 }
1023 if( pDb->zBusy ){
1024 pDb->interp = interp;
1025 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
1026 }else{
1027 sqlite3_busy_handler(pDb->db, 0, 0);
1028 }
1029 }
1030 break;
1031 }
1032
1033 /* $db cache flush
1034 ** $db cache size n
1035 **
1036 ** Flush the prepared statement cache, or set the maximum number of
1037 ** cached statements.
1038 */
1039 case DB_CACHE: {
1040 char *subCmd;
1041 int n;
1042
1043 if( objc<=2 ){
1044 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
1045 return TCL_ERROR;
1046 }
1047 subCmd = Tcl_GetStringFromObj( objv[2], 0 );
1048 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
1049 if( objc!=3 ){
1050 Tcl_WrongNumArgs(interp, 2, objv, "flush");
1051 return TCL_ERROR;
1052 }else{
1053 flushStmtCache( pDb );
1054 }
1055 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
1056 if( objc!=4 ){
1057 Tcl_WrongNumArgs(interp, 2, objv, "size n");
1058 return TCL_ERROR;
1059 }else{
1060 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
1061 Tcl_AppendResult( interp, "cannot convert \"",
1062 Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0);
1063 return TCL_ERROR;
1064 }else{
1065 if( n<0 ){
1066 flushStmtCache( pDb );
1067 n = 0;
1068 }else if( n>MAX_PREPARED_STMTS ){
1069 n = MAX_PREPARED_STMTS;
1070 }
1071 pDb->maxStmt = n;
1072 }
1073 }
1074 }else{
1075 Tcl_AppendResult( interp, "bad option \"",
1076 Tcl_GetStringFromObj(objv[0],0), "\": must be flush or size", 0);
1077 return TCL_ERROR;
1078 }
1079 break;
1080 }
1081
1082 /* $db changes
1083 **
1084 ** Return the number of rows that were modified, inserted, or deleted by
1085 ** the most recent INSERT, UPDATE or DELETE statement, not including
1086 ** any changes made by trigger programs.
1087 */
1088 case DB_CHANGES: {
1089 Tcl_Obj *pResult;
1090 if( objc!=2 ){
1091 Tcl_WrongNumArgs(interp, 2, objv, "");
1092 return TCL_ERROR;
1093 }
1094 pResult = Tcl_GetObjResult(interp);
1095 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
1096 break;
1097 }
1098
1099 /* $db close
1100 **
1101 ** Shutdown the database
1102 */
1103 case DB_CLOSE: {
1104 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
1105 break;
1106 }
1107
1108 /*
1109 ** $db collate NAME SCRIPT
1110 **
1111 ** Create a new SQL collation function called NAME. Whenever
1112 ** that function is called, invoke SCRIPT to evaluate the function.
1113 */
1114 case DB_COLLATE: {
1115 SqlCollate *pCollate;
1116 char *zName;
1117 char *zScript;
1118 int nScript;
1119 if( objc!=4 ){
1120 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
1121 return TCL_ERROR;
1122 }
1123 zName = Tcl_GetStringFromObj(objv[2], 0);
1124 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
1125 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
1126 if( pCollate==0 ) return TCL_ERROR;
1127 pCollate->interp = interp;
1128 pCollate->pNext = pDb->pCollate;
1129 pCollate->zScript = (char*)&pCollate[1];
1130 pDb->pCollate = pCollate;
1131 memcpy(pCollate->zScript, zScript, nScript+1);
1132 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
1133 pCollate, tclSqlCollate) ){
1134 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
1135 return TCL_ERROR;
1136 }
1137 break;
1138 }
1139
1140 /*
1141 ** $db collation_needed SCRIPT
1142 **
1143 ** Create a new SQL collation function called NAME. Whenever
1144 ** that function is called, invoke SCRIPT to evaluate the function.
1145 */
1146 case DB_COLLATION_NEEDED: {
1147 if( objc!=3 ){
1148 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
1149 return TCL_ERROR;
1150 }
1151 if( pDb->pCollateNeeded ){
1152 Tcl_DecrRefCount(pDb->pCollateNeeded);
1153 }
1154 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
1155 Tcl_IncrRefCount(pDb->pCollateNeeded);
1156 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
1157 break;
1158 }
1159
1160 /* $db commit_hook ?CALLBACK?
1161 **
1162 ** Invoke the given callback just before committing every SQL transaction.
1163 ** If the callback throws an exception or returns non-zero, then the
1164 ** transaction is aborted. If CALLBACK is an empty string, the callback
1165 ** is disabled.
1166 */
1167 case DB_COMMIT_HOOK: {
1168 if( objc>3 ){
1169 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
1170 return TCL_ERROR;
1171 }else if( objc==2 ){
1172 if( pDb->zCommit ){
1173 Tcl_AppendResult(interp, pDb->zCommit, 0);
1174 }
1175 }else{
1176 char *zCommit;
1177 int len;
1178 if( pDb->zCommit ){
1179 Tcl_Free(pDb->zCommit);
1180 }
1181 zCommit = Tcl_GetStringFromObj(objv[2], &len);
1182 if( zCommit && len>0 ){
1183 pDb->zCommit = Tcl_Alloc( len + 1 );
1184 memcpy(pDb->zCommit, zCommit, len+1);
1185 }else{
1186 pDb->zCommit = 0;
1187 }
1188 if( pDb->zCommit ){
1189 pDb->interp = interp;
1190 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
1191 }else{
1192 sqlite3_commit_hook(pDb->db, 0, 0);
1193 }
1194 }
1195 break;
1196 }
1197
1198 /* $db complete SQL
1199 **
1200 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
1201 ** additional lines of input are needed. This is similar to the
1202 ** built-in "info complete" command of Tcl.
1203 */
1204 case DB_COMPLETE: {
1205#ifndef SQLITE_OMIT_COMPLETE
1206 Tcl_Obj *pResult;
1207 int isComplete;
1208 if( objc!=3 ){
1209 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
1210 return TCL_ERROR;
1211 }
1212 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
1213 pResult = Tcl_GetObjResult(interp);
1214 Tcl_SetBooleanObj(pResult, isComplete);
1215#endif
1216 break;
1217 }
1218
1219 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
1220 **
1221 ** Copy data into table from filename, optionally using SEPARATOR
1222 ** as column separators. If a column contains a null string, or the
1223 ** value of NULLINDICATOR, a NULL is inserted for the column.
1224 ** conflict-algorithm is one of the sqlite conflict algorithms:
1225 ** rollback, abort, fail, ignore, replace
1226 ** On success, return the number of lines processed, not necessarily same
1227 ** as 'db changes' due to conflict-algorithm selected.
1228 **
1229 ** This code is basically an implementation/enhancement of
1230 ** the sqlite3 shell.c ".import" command.
1231 **
1232 ** This command usage is equivalent to the sqlite2.x COPY statement,
1233 ** which imports file data into a table using the PostgreSQL COPY file format:
1234 ** $db copy $conflit_algo $table_name $filename \t \\N
1235 */
1236 case DB_COPY: {
1237 char *zTable; /* Insert data into this table */
1238 char *zFile; /* The file from which to extract data */
1239 char *zConflict; /* The conflict algorithm to use */
1240 sqlite3_stmt *pStmt; /* A statement */
1241 int rc; /* Result code */
1242 int nCol; /* Number of columns in the table */
1243 int nByte; /* Number of bytes in an SQL string */
1244 int i, j; /* Loop counters */
1245 int nSep; /* Number of bytes in zSep[] */
1246 int nNull; /* Number of bytes in zNull[] */
1247 char *zSql; /* An SQL statement */
1248 char *zLine; /* A single line of input from the file */
1249 char **azCol; /* zLine[] broken up into columns */
1250 char *zCommit; /* How to commit changes */
1251 FILE *in; /* The input file */
1252 int lineno = 0; /* Line number of input file */
1253 char zLineNum[80]; /* Line number print buffer */
1254 Tcl_Obj *pResult; /* interp result */
1255
1256 char *zSep;
1257 char *zNull;
1258 if( objc<5 || objc>7 ){
1259 Tcl_WrongNumArgs(interp, 2, objv,
1260 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
1261 return TCL_ERROR;
1262 }
1263 if( objc>=6 ){
1264 zSep = Tcl_GetStringFromObj(objv[5], 0);
1265 }else{
1266 zSep = "\t";
1267 }
1268 if( objc>=7 ){
1269 zNull = Tcl_GetStringFromObj(objv[6], 0);
1270 }else{
1271 zNull = "";
1272 }
1273 zConflict = Tcl_GetStringFromObj(objv[2], 0);
1274 zTable = Tcl_GetStringFromObj(objv[3], 0);
1275 zFile = Tcl_GetStringFromObj(objv[4], 0);
1276 nSep = strlen(zSep);
1277 nNull = strlen(zNull);
1278 if( nSep==0 ){
1279 Tcl_AppendResult(interp,"Error: non-null separator required for copy",0);
1280 return TCL_ERROR;
1281 }
1282 if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
1283 sqlite3StrICmp(zConflict, "abort" ) != 0 &&
1284 sqlite3StrICmp(zConflict, "fail" ) != 0 &&
1285 sqlite3StrICmp(zConflict, "ignore" ) != 0 &&
1286 sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
1287 Tcl_AppendResult(interp, "Error: \"", zConflict,
1288 "\", conflict-algorithm must be one of: rollback, "
1289 "abort, fail, ignore, or replace", 0);
1290 return TCL_ERROR;
1291 }
1292 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1293 if( zSql==0 ){
1294 Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
1295 return TCL_ERROR;
1296 }
1297 nByte = strlen(zSql);
1298 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
1299 sqlite3_free(zSql);
1300 if( rc ){
1301 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1302 nCol = 0;
1303 }else{
1304 nCol = sqlite3_column_count(pStmt);
1305 }
1306 sqlite3_finalize(pStmt);
1307 if( nCol==0 ) {
1308 return TCL_ERROR;
1309 }
1310 zSql = malloc( nByte + 50 + nCol*2 );
1311 if( zSql==0 ) {
1312 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1313 return TCL_ERROR;
1314 }
1315 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
1316 zConflict, zTable);
1317 j = strlen(zSql);
1318 for(i=1; i<nCol; i++){
1319 zSql[j++] = ',';
1320 zSql[j++] = '?';
1321 }
1322 zSql[j++] = ')';
1323 zSql[j] = 0;
1324 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
1325 free(zSql);
1326 if( rc ){
1327 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1328 sqlite3_finalize(pStmt);
1329 return TCL_ERROR;
1330 }
1331 in = fopen(zFile, "rb");
1332 if( in==0 ){
1333 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
1334 sqlite3_finalize(pStmt);
1335 return TCL_ERROR;
1336 }
1337 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1338 if( azCol==0 ) {
1339 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1340 fclose(in);
1341 return TCL_ERROR;
1342 }
1343 (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
1344 zCommit = "COMMIT";
1345 while( (zLine = local_getline(0, in))!=0 ){
1346 char *z;
1347 i = 0;
1348 lineno++;
1349 azCol[0] = zLine;
1350 for(i=0, z=zLine; *z; z++){
1351 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
1352 *z = 0;
1353 i++;
1354 if( i<nCol ){
1355 azCol[i] = &z[nSep];
1356 z += nSep-1;
1357 }
1358 }
1359 }
1360 if( i+1!=nCol ){
1361 char *zErr;
1362 int nErr = strlen(zFile) + 200;
1363 zErr = malloc(nErr);
1364 if( zErr ){
1365 sqlite3_snprintf(nErr, zErr,
1366 "Error: %s line %d: expected %d columns of data but found %d",
1367 zFile, lineno, nCol, i+1);
1368 Tcl_AppendResult(interp, zErr, 0);
1369 free(zErr);
1370 }
1371 zCommit = "ROLLBACK";
1372 break;
1373 }
1374 for(i=0; i<nCol; i++){
1375 /* check for null data, if so, bind as null */
1376 if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) {
1377 sqlite3_bind_null(pStmt, i+1);
1378 }else{
1379 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1380 }
1381 }
1382 sqlite3_step(pStmt);
1383 rc = sqlite3_reset(pStmt);
1384 free(zLine);
1385 if( rc!=SQLITE_OK ){
1386 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
1387 zCommit = "ROLLBACK";
1388 break;
1389 }
1390 }
1391 free(azCol);
1392 fclose(in);
1393 sqlite3_finalize(pStmt);
1394 (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
1395
1396 if( zCommit[0] == 'C' ){
1397 /* success, set result as number of lines processed */
1398 pResult = Tcl_GetObjResult(interp);
1399 Tcl_SetIntObj(pResult, lineno);
1400 rc = TCL_OK;
1401 }else{
1402 /* failure, append lineno where failed */
1403 sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno);
1404 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
1405 rc = TCL_ERROR;
1406 }
1407 break;
1408 }
1409
1410 /*
1411 ** $db enable_load_extension BOOLEAN
1412 **
1413 ** Turn the extension loading feature on or off. It if off by
1414 ** default.
1415 */
1416 case DB_ENABLE_LOAD_EXTENSION: {
1417#ifndef SQLITE_OMIT_LOAD_EXTENSION
1418 int onoff;
1419 if( objc!=3 ){
1420 Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN");
1421 return TCL_ERROR;
1422 }
1423 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1424 return TCL_ERROR;
1425 }
1426 sqlite3_enable_load_extension(pDb->db, onoff);
1427 break;
1428#else
1429 Tcl_AppendResult(interp, "extension loading is turned off at compile-time",
1430 0);
1431 return TCL_ERROR;
1432#endif
1433 }
1434
1435 /*
1436 ** $db errorcode
1437 **
1438 ** Return the numeric error code that was returned by the most recent
1439 ** call to sqlite3_exec().
1440 */
1441 case DB_ERRORCODE: {
1442 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
1443 break;
1444 }
1445
1446 /*
1447 ** $db eval $sql ?array? ?{ ...code... }?
1448 ** $db onecolumn $sql
1449 **
1450 ** The SQL statement in $sql is evaluated. For each row, the values are
1451 ** placed in elements of the array named "array" and ...code... is executed.
1452 ** If "array" and "code" are omitted, then no callback is every invoked.
1453 ** If "array" is an empty string, then the values are placed in variables
1454 ** that have the same name as the fields extracted by the query.
1455 **
1456 ** The onecolumn method is the equivalent of:
1457 ** lindex [$db eval $sql] 0
1458 */
1459 case DB_ONECOLUMN:
1460 case DB_EVAL:
1461 case DB_EXISTS: {
1462 char const *zSql; /* Next SQL statement to execute */
1463 char const *zLeft; /* What is left after first stmt in zSql */
1464 sqlite3_stmt *pStmt; /* Compiled SQL statment */
1465 Tcl_Obj *pArray; /* Name of array into which results are written */
1466 Tcl_Obj *pScript; /* Script to run for each result set */
1467 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
1468 int nParm; /* Number of entries used in apParm[] */
1469 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
1470 Tcl_Obj *pRet; /* Value to be returned */
1471 SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
1472 int rc2;
1473
1474 if( choice==DB_EVAL ){
1475 if( objc<3 || objc>5 ){
1476 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
1477 return TCL_ERROR;
1478 }
1479 pRet = Tcl_NewObj();
1480 Tcl_IncrRefCount(pRet);
1481 }else{
1482 if( objc!=3 ){
1483 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
1484 return TCL_ERROR;
1485 }
1486 if( choice==DB_EXISTS ){
1487 pRet = Tcl_NewBooleanObj(0);
1488 Tcl_IncrRefCount(pRet);
1489 }else{
1490 pRet = 0;
1491 }
1492 }
1493 if( objc==3 ){
1494 pArray = pScript = 0;
1495 }else if( objc==4 ){
1496 pArray = 0;
1497 pScript = objv[3];
1498 }else{
1499 pArray = objv[3];
1500 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
1501 pScript = objv[4];
1502 }
1503
1504 Tcl_IncrRefCount(objv[2]);
1505 zSql = Tcl_GetStringFromObj(objv[2], 0);
1506 while( rc==TCL_OK && zSql[0] ){
1507 int i; /* Loop counter */
1508 int nVar; /* Number of bind parameters in the pStmt */
1509 int nCol; /* Number of columns in the result set */
1510 Tcl_Obj **apColName = 0; /* Array of column names */
1511 int len; /* String length of zSql */
1512
1513 /* Try to find a SQL statement that has already been compiled and
1514 ** which matches the next sequence of SQL.
1515 */
1516 pStmt = 0;
1517 pPreStmt = pDb->stmtList;
1518 len = strlen(zSql);
1519 if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){
1520 flushStmtCache(pDb);
1521 pPreStmt = 0;
1522 }
1523 for(; pPreStmt; pPreStmt=pPreStmt->pNext){
1524 int n = pPreStmt->nSql;
1525 if( len>=n
1526 && memcmp(pPreStmt->zSql, zSql, n)==0
1527 && (zSql[n]==0 || zSql[n-1]==';')
1528 ){
1529 pStmt = pPreStmt->pStmt;
1530 zLeft = &zSql[pPreStmt->nSql];
1531
1532 /* When a prepared statement is found, unlink it from the
1533 ** cache list. It will later be added back to the beginning
1534 ** of the cache list in order to implement LRU replacement.
1535 */
1536 if( pPreStmt->pPrev ){
1537 pPreStmt->pPrev->pNext = pPreStmt->pNext;
1538 }else{
1539 pDb->stmtList = pPreStmt->pNext;
1540 }
1541 if( pPreStmt->pNext ){
1542 pPreStmt->pNext->pPrev = pPreStmt->pPrev;
1543 }else{
1544 pDb->stmtLast = pPreStmt->pPrev;
1545 }
1546 pDb->nStmt--;
1547 break;
1548 }
1549 }
1550
1551 /* If no prepared statement was found. Compile the SQL text
1552 */
1553 if( pStmt==0 ){
1554 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
1555 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1556 rc = TCL_ERROR;
1557 break;
1558 }
1559 if( pStmt==0 ){
1560 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
1561 /* A compile-time error in the statement
1562 */
1563 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1564 rc = TCL_ERROR;
1565 break;
1566 }else{
1567 /* The statement was a no-op. Continue to the next statement
1568 ** in the SQL string.
1569 */
1570 zSql = zLeft;
1571 continue;
1572 }
1573 }
1574 assert( pPreStmt==0 );
1575 }
1576
1577 /* Bind values to parameters that begin with $ or :
1578 */
1579 nVar = sqlite3_bind_parameter_count(pStmt);
1580 nParm = 0;
1581 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
1582 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
1583 }else{
1584 apParm = aParm;
1585 }
1586 for(i=1; i<=nVar; i++){
1587 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
1588 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){
1589 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
1590 if( pVar ){
1591 int n;
1592 u8 *data;
1593 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
1594 char c = zType[0];
1595 if( zVar[0]=='@' ||
1596 (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){
1597 /* Load a BLOB type if the Tcl variable is a bytearray and
1598 ** it has no string representation or the host
1599 ** parameter name begins with "@". */
1600 data = Tcl_GetByteArrayFromObj(pVar, &n);
1601 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
1602 Tcl_IncrRefCount(pVar);
1603 apParm[nParm++] = pVar;
1604 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
1605 Tcl_GetIntFromObj(interp, pVar, &n);
1606 sqlite3_bind_int(pStmt, i, n);
1607 }else if( c=='d' && strcmp(zType,"double")==0 ){
1608 double r;
1609 Tcl_GetDoubleFromObj(interp, pVar, &r);
1610 sqlite3_bind_double(pStmt, i, r);
1611 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
1612 (c=='i' && strcmp(zType,"int")==0) ){
1613 Tcl_WideInt v;
1614 Tcl_GetWideIntFromObj(interp, pVar, &v);
1615 sqlite3_bind_int64(pStmt, i, v);
1616 }else{
1617 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
1618 sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC);
1619 Tcl_IncrRefCount(pVar);
1620 apParm[nParm++] = pVar;
1621 }
1622 }else{
1623 sqlite3_bind_null( pStmt, i );
1624 }
1625 }
1626 }
1627
1628 /* Compute column names */
1629 nCol = sqlite3_column_count(pStmt);
1630 if( pScript ){
1631 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
1632 if( apColName==0 ) break;
1633 for(i=0; i<nCol; i++){
1634 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
1635 Tcl_IncrRefCount(apColName[i]);
1636 }
1637 }
1638
1639 /* If results are being stored in an array variable, then create
1640 ** the array(*) entry for that array
1641 */
1642 if( pArray ){
1643 Tcl_Obj *pColList = Tcl_NewObj();
1644 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
1645 Tcl_IncrRefCount(pColList);
1646 for(i=0; i<nCol; i++){
1647 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
1648 }
1649 Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0);
1650 Tcl_DecrRefCount(pColList);
1651 }
1652
1653 /* Execute the SQL
1654 */
1655 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
1656 for(i=0; i<nCol; i++){
1657 Tcl_Obj *pVal;
1658
1659 /* Set pVal to contain the i'th column of this row. */
1660 switch( sqlite3_column_type(pStmt, i) ){
1661 case SQLITE_BLOB: {
1662 int bytes = sqlite3_column_bytes(pStmt, i);
1663 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
1664 break;
1665 }
1666 case SQLITE_INTEGER: {
1667 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
1668 if( v>=-2147483647 && v<=2147483647 ){
1669 pVal = Tcl_NewIntObj(v);
1670 }else{
1671 pVal = Tcl_NewWideIntObj(v);
1672 }
1673 break;
1674 }
1675 case SQLITE_FLOAT: {
1676 double r = sqlite3_column_double(pStmt, i);
1677 pVal = Tcl_NewDoubleObj(r);
1678 break;
1679 }
1680 case SQLITE_NULL: {
1681 pVal = dbTextToObj(pDb->zNull);
1682 break;
1683 }
1684 default: {
1685 pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i));
1686 break;
1687 }
1688 }
1689
1690 if( pScript ){
1691 if( pArray==0 ){
1692 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
1693 }else{
1694 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
1695 }
1696 }else if( choice==DB_ONECOLUMN ){
1697 assert( pRet==0 );
1698 if( pRet==0 ){
1699 pRet = pVal;
1700 Tcl_IncrRefCount(pRet);
1701 }
1702 rc = TCL_BREAK;
1703 i = nCol;
1704 }else if( choice==DB_EXISTS ){
1705 Tcl_DecrRefCount(pRet);
1706 pRet = Tcl_NewBooleanObj(1);
1707 Tcl_IncrRefCount(pRet);
1708 rc = TCL_BREAK;
1709 i = nCol;
1710 }else{
1711 Tcl_ListObjAppendElement(interp, pRet, pVal);
1712 }
1713 }
1714
1715 if( pScript ){
1716 rc = Tcl_EvalObjEx(interp, pScript, 0);
1717 if( rc==TCL_CONTINUE ){
1718 rc = TCL_OK;
1719 }
1720 }
1721 }
1722 if( rc==TCL_BREAK ){
1723 rc = TCL_OK;
1724 }
1725
1726 /* Free the column name objects */
1727 if( pScript ){
1728 for(i=0; i<nCol; i++){
1729 Tcl_DecrRefCount(apColName[i]);
1730 }
1731 Tcl_Free((char*)apColName);
1732 }
1733
1734 /* Free the bound string and blob parameters */
1735 for(i=0; i<nParm; i++){
1736 Tcl_DecrRefCount(apParm[i]);
1737 }
1738 if( apParm!=aParm ){
1739 Tcl_Free((char*)apParm);
1740 }
1741
1742 /* Reset the statement. If the result code is SQLITE_SCHEMA, then
1743 ** flush the statement cache and try the statement again.
1744 */
1745 rc2 = sqlite3_reset(pStmt);
1746 if( SQLITE_SCHEMA==rc2 ){
1747 /* After a schema change, flush the cache and try to run the
1748 ** statement again
1749 */
1750 flushStmtCache( pDb );
1751 sqlite3_finalize(pStmt);
1752 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1753 continue;
1754 }else if( SQLITE_OK!=rc2 ){
1755 /* If a run-time error occurs, report the error and stop reading
1756 ** the SQL
1757 */
1758 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1759 sqlite3_finalize(pStmt);
1760 rc = TCL_ERROR;
1761 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1762 break;
1763 }else if( pDb->maxStmt<=0 ){
1764 /* If the cache is turned off, deallocated the statement */
1765 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1766 sqlite3_finalize(pStmt);
1767 }else{
1768 /* Everything worked and the cache is operational.
1769 ** Create a new SqlPreparedStmt structure if we need one.
1770 ** (If we already have one we can just reuse it.)
1771 */
1772 if( pPreStmt==0 ){
1773 len = zLeft - zSql;
1774 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) + len );
1775 if( pPreStmt==0 ) return TCL_ERROR;
1776 pPreStmt->pStmt = pStmt;
1777 pPreStmt->nSql = len;
1778 memcpy(pPreStmt->zSql, zSql, len);
1779 pPreStmt->zSql[len] = 0;
1780 }
1781
1782 /* Add the prepared statement to the beginning of the cache list
1783 */
1784 pPreStmt->pNext = pDb->stmtList;
1785 pPreStmt->pPrev = 0;
1786 if( pDb->stmtList ){
1787 pDb->stmtList->pPrev = pPreStmt;
1788 }
1789 pDb->stmtList = pPreStmt;
1790 if( pDb->stmtLast==0 ){
1791 assert( pDb->nStmt==0 );
1792 pDb->stmtLast = pPreStmt;
1793 }else{
1794 assert( pDb->nStmt>0 );
1795 }
1796 pDb->nStmt++;
1797
1798 /* If we have too many statement in cache, remove the surplus from the
1799 ** end of the cache list.
1800 */
1801 while( pDb->nStmt>pDb->maxStmt ){
1802 sqlite3_finalize(pDb->stmtLast->pStmt);
1803 pDb->stmtLast = pDb->stmtLast->pPrev;
1804 Tcl_Free((char*)pDb->stmtLast->pNext);
1805 pDb->stmtLast->pNext = 0;
1806 pDb->nStmt--;
1807 }
1808 }
1809
1810 /* Proceed to the next statement */
1811 zSql = zLeft;
1812 }
1813 Tcl_DecrRefCount(objv[2]);
1814
1815 if( pRet ){
1816 if( rc==TCL_OK ){
1817 Tcl_SetObjResult(interp, pRet);
1818 }
1819 Tcl_DecrRefCount(pRet);
1820 }else if( rc==TCL_OK ){
1821 Tcl_ResetResult(interp);
1822 }
1823 break;
1824 }
1825
1826 /*
1827 ** $db function NAME SCRIPT
1828 **
1829 ** Create a new SQL function called NAME. Whenever that function is
1830 ** called, invoke SCRIPT to evaluate the function.
1831 */
1832 case DB_FUNCTION: {
1833 SqlFunc *pFunc;
1834 Tcl_Obj *pScript;
1835 char *zName;
1836 if( objc!=4 ){
1837 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
1838 return TCL_ERROR;
1839 }
1840 zName = Tcl_GetStringFromObj(objv[2], 0);
1841 pScript = objv[3];
1842 pFunc = findSqlFunc(pDb, zName);
1843 if( pFunc==0 ) return TCL_ERROR;
1844 if( pFunc->pScript ){
1845 Tcl_DecrRefCount(pFunc->pScript);
1846 }
1847 pFunc->pScript = pScript;
1848 Tcl_IncrRefCount(pScript);
1849 pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
1850 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
1851 pFunc, tclSqlFunc, 0, 0);
1852 if( rc!=SQLITE_OK ){
1853 rc = TCL_ERROR;
1854 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
1855 }else{
1856 /* Must flush any cached statements */
1857 flushStmtCache( pDb );
1858 }
1859 break;
1860 }
1861
1862 /*
1863 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
1864 */
1865 case DB_INCRBLOB: {
1866#ifdef SQLITE_OMIT_INCRBLOB
1867 Tcl_AppendResult(interp, "incrblob not available in this build", 0);
1868 return TCL_ERROR;
1869#else
1870 int isReadonly = 0;
1871 const char *zDb = "main";
1872 const char *zTable;
1873 const char *zColumn;
1874 sqlite_int64 iRow;
1875
1876 /* Check for the -readonly option */
1877 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){
1878 isReadonly = 1;
1879 }
1880
1881 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){
1882 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID");
1883 return TCL_ERROR;
1884 }
1885
1886 if( objc==(6+isReadonly) ){
1887 zDb = Tcl_GetString(objv[2]);
1888 }
1889 zTable = Tcl_GetString(objv[objc-3]);
1890 zColumn = Tcl_GetString(objv[objc-2]);
1891 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);
1892
1893 if( rc==TCL_OK ){
1894 rc = createIncrblobChannel(
1895 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly
1896 );
1897 }
1898#endif
1899 break;
1900 }
1901
1902 /*
1903 ** $db interrupt
1904 **
1905 ** Interrupt the execution of the inner-most SQL interpreter. This
1906 ** causes the SQL statement to return an error of SQLITE_INTERRUPT.
1907 */
1908 case DB_INTERRUPT: {
1909 sqlite3_interrupt(pDb->db);
1910 break;
1911 }
1912
1913 /*
1914 ** $db nullvalue ?STRING?
1915 **
1916 ** Change text used when a NULL comes back from the database. If ?STRING?
1917 ** is not present, then the current string used for NULL is returned.
1918 ** If STRING is present, then STRING is returned.
1919 **
1920 */
1921 case DB_NULLVALUE: {
1922 if( objc!=2 && objc!=3 ){
1923 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
1924 return TCL_ERROR;
1925 }
1926 if( objc==3 ){
1927 int len;
1928 char *zNull = Tcl_GetStringFromObj(objv[2], &len);
1929 if( pDb->zNull ){
1930 Tcl_Free(pDb->zNull);
1931 }
1932 if( zNull && len>0 ){
1933 pDb->zNull = Tcl_Alloc( len + 1 );
1934 strncpy(pDb->zNull, zNull, len);
1935 pDb->zNull[len] = '\0';
1936 }else{
1937 pDb->zNull = 0;
1938 }
1939 }
1940 Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
1941 break;
1942 }
1943
1944 /*
1945 ** $db last_insert_rowid
1946 **
1947 ** Return an integer which is the ROWID for the most recent insert.
1948 */
1949 case DB_LAST_INSERT_ROWID: {
1950 Tcl_Obj *pResult;
1951 Tcl_WideInt rowid;
1952 if( objc!=2 ){
1953 Tcl_WrongNumArgs(interp, 2, objv, "");
1954 return TCL_ERROR;
1955 }
1956 rowid = sqlite3_last_insert_rowid(pDb->db);
1957 pResult = Tcl_GetObjResult(interp);
1958 Tcl_SetWideIntObj(pResult, rowid);
1959 break;
1960 }
1961
1962 /*
1963 ** The DB_ONECOLUMN method is implemented together with DB_EVAL.
1964 */
1965
1966 /* $db progress ?N CALLBACK?
1967 **
1968 ** Invoke the given callback every N virtual machine opcodes while executing
1969 ** queries.
1970 */
1971 case DB_PROGRESS: {
1972 if( objc==2 ){
1973 if( pDb->zProgress ){
1974 Tcl_AppendResult(interp, pDb->zProgress, 0);
1975 }
1976 }else if( objc==4 ){
1977 char *zProgress;
1978 int len;
1979 int N;
1980 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
1981 return TCL_ERROR;
1982 };
1983 if( pDb->zProgress ){
1984 Tcl_Free(pDb->zProgress);
1985 }
1986 zProgress = Tcl_GetStringFromObj(objv[3], &len);
1987 if( zProgress && len>0 ){
1988 pDb->zProgress = Tcl_Alloc( len + 1 );
1989 memcpy(pDb->zProgress, zProgress, len+1);
1990 }else{
1991 pDb->zProgress = 0;
1992 }
1993#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1994 if( pDb->zProgress ){
1995 pDb->interp = interp;
1996 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
1997 }else{
1998 sqlite3_progress_handler(pDb->db, 0, 0, 0);
1999 }
2000#endif
2001 }else{
2002 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
2003 return TCL_ERROR;
2004 }
2005 break;
2006 }
2007
2008 /* $db profile ?CALLBACK?
2009 **
2010 ** Make arrangements to invoke the CALLBACK routine after each SQL statement
2011 ** that has run. The text of the SQL and the amount of elapse time are
2012 ** appended to CALLBACK before the script is run.
2013 */
2014 case DB_PROFILE: {
2015 if( objc>3 ){
2016 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2017 return TCL_ERROR;
2018 }else if( objc==2 ){
2019 if( pDb->zProfile ){
2020 Tcl_AppendResult(interp, pDb->zProfile, 0);
2021 }
2022 }else{
2023 char *zProfile;
2024 int len;
2025 if( pDb->zProfile ){
2026 Tcl_Free(pDb->zProfile);
2027 }
2028 zProfile = Tcl_GetStringFromObj(objv[2], &len);
2029 if( zProfile && len>0 ){
2030 pDb->zProfile = Tcl_Alloc( len + 1 );
2031 memcpy(pDb->zProfile, zProfile, len+1);
2032 }else{
2033 pDb->zProfile = 0;
2034 }
2035#ifndef SQLITE_OMIT_TRACE
2036 if( pDb->zProfile ){
2037 pDb->interp = interp;
2038 sqlite3_profile(pDb->db, DbProfileHandler, pDb);
2039 }else{
2040 sqlite3_profile(pDb->db, 0, 0);
2041 }
2042#endif
2043 }
2044 break;
2045 }
2046
2047 /*
2048 ** $db rekey KEY
2049 **
2050 ** Change the encryption key on the currently open database.
2051 */
2052 case DB_REKEY: {
2053 int nKey;
2054 void *pKey;
2055 if( objc!=3 ){
2056 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
2057 return TCL_ERROR;
2058 }
2059 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
2060#ifdef SQLITE_HAS_CODEC
2061 rc = sqlite3_rekey(pDb->db, pKey, nKey);
2062 if( rc ){
2063 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2064 rc = TCL_ERROR;
2065 }
2066#endif
2067 break;
2068 }
2069
2070 /*
2071 ** $db timeout MILLESECONDS
2072 **
2073 ** Delay for the number of milliseconds specified when a file is locked.
2074 */
2075 case DB_TIMEOUT: {
2076 int ms;
2077 if( objc!=3 ){
2078 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
2079 return TCL_ERROR;
2080 }
2081 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
2082 sqlite3_busy_timeout(pDb->db, ms);
2083 break;
2084 }
2085
2086 /*
2087 ** $db total_changes
2088 **
2089 ** Return the number of rows that were modified, inserted, or deleted
2090 ** since the database handle was created.
2091 */
2092 case DB_TOTAL_CHANGES: {
2093 Tcl_Obj *pResult;
2094 if( objc!=2 ){
2095 Tcl_WrongNumArgs(interp, 2, objv, "");
2096 return TCL_ERROR;
2097 }
2098 pResult = Tcl_GetObjResult(interp);
2099 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
2100 break;
2101 }
2102
2103 /* $db trace ?CALLBACK?
2104 **
2105 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
2106 ** that is executed. The text of the SQL is appended to CALLBACK before
2107 ** it is executed.
2108 */
2109 case DB_TRACE: {
2110 if( objc>3 ){
2111 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2112 return TCL_ERROR;
2113 }else if( objc==2 ){
2114 if( pDb->zTrace ){
2115 Tcl_AppendResult(interp, pDb->zTrace, 0);
2116 }
2117 }else{
2118 char *zTrace;
2119 int len;
2120 if( pDb->zTrace ){
2121 Tcl_Free(pDb->zTrace);
2122 }
2123 zTrace = Tcl_GetStringFromObj(objv[2], &len);
2124 if( zTrace && len>0 ){
2125 pDb->zTrace = Tcl_Alloc( len + 1 );
2126 memcpy(pDb->zTrace, zTrace, len+1);
2127 }else{
2128 pDb->zTrace = 0;
2129 }
2130#ifndef SQLITE_OMIT_TRACE
2131 if( pDb->zTrace ){
2132 pDb->interp = interp;
2133 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
2134 }else{
2135 sqlite3_trace(pDb->db, 0, 0);
2136 }
2137#endif
2138 }
2139 break;
2140 }
2141
2142 /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT
2143 **
2144 ** Start a new transaction (if we are not already in the midst of a
2145 ** transaction) and execute the TCL script SCRIPT. After SCRIPT
2146 ** completes, either commit the transaction or roll it back if SCRIPT
2147 ** throws an exception. Or if no new transation was started, do nothing.
2148 ** pass the exception on up the stack.
2149 **
2150 ** This command was inspired by Dave Thomas's talk on Ruby at the
2151 ** 2005 O'Reilly Open Source Convention (OSCON).
2152 */
2153 case DB_TRANSACTION: {
2154 int inTrans;
2155 Tcl_Obj *pScript;
2156 const char *zBegin = "BEGIN";
2157 if( objc!=3 && objc!=4 ){
2158 Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT");
2159 return TCL_ERROR;
2160 }
2161 if( objc==3 ){
2162 pScript = objv[2];
2163 } else {
2164 static const char *TTYPE_strs[] = {
2165 "deferred", "exclusive", "immediate", 0
2166 };
2167 enum TTYPE_enum {
2168 TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE
2169 };
2170 int ttype;
2171 if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type",
2172 0, &ttype) ){
2173 return TCL_ERROR;
2174 }
2175 switch( (enum TTYPE_enum)ttype ){
2176 case TTYPE_DEFERRED: /* no-op */; break;
2177 case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break;
2178 case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break;
2179 }
2180 pScript = objv[3];
2181 }
2182 inTrans = !sqlite3_get_autocommit(pDb->db);
2183 if( !inTrans ){
2184 (void)sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
2185 }
2186 rc = Tcl_EvalObjEx(interp, pScript, 0);
2187 if( !inTrans ){
2188 const char *zEnd;
2189 if( rc==TCL_ERROR ){
2190 zEnd = "ROLLBACK";
2191 } else {
2192 zEnd = "COMMIT";
2193 }
2194 if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){
2195 sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
2196 }
2197 }
2198 break;
2199 }
2200
2201 /*
2202 ** $db update_hook ?script?
2203 ** $db rollback_hook ?script?
2204 */
2205 case DB_UPDATE_HOOK:
2206 case DB_ROLLBACK_HOOK: {
2207
2208 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on
2209 ** whether [$db update_hook] or [$db rollback_hook] was invoked.
2210 */
2211 Tcl_Obj **ppHook;
2212 if( choice==DB_UPDATE_HOOK ){
2213 ppHook = &pDb->pUpdateHook;
2214 }else{
2215 ppHook = &pDb->pRollbackHook;
2216 }
2217
2218 if( objc!=2 && objc!=3 ){
2219 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
2220 return TCL_ERROR;
2221 }
2222 if( *ppHook ){
2223 Tcl_SetObjResult(interp, *ppHook);
2224 if( objc==3 ){
2225 Tcl_DecrRefCount(*ppHook);
2226 *ppHook = 0;
2227 }
2228 }
2229 if( objc==3 ){
2230 assert( !(*ppHook) );
2231 if( Tcl_GetCharLength(objv[2])>0 ){
2232 *ppHook = objv[2];
2233 Tcl_IncrRefCount(*ppHook);
2234 }
2235 }
2236
2237 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb);
2238 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb);
2239
2240 break;
2241 }
2242
2243 /* $db version
2244 **
2245 ** Return the version string for this database.
2246 */
2247 case DB_VERSION: {
2248 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
2249 break;
2250 }
2251
2252
2253 } /* End of the SWITCH statement */
2254 return rc;
2255}
2256
2257/*
2258** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN?
2259** ?-create BOOLEAN?
2260**
2261** This is the main Tcl command. When the "sqlite" Tcl command is
2262** invoked, this routine runs to process that command.
2263**
2264** The first argument, DBNAME, is an arbitrary name for a new
2265** database connection. This command creates a new command named
2266** DBNAME that is used to control that connection. The database
2267** connection is deleted when the DBNAME command is deleted.
2268**
2269** The second argument is the name of the database file.
2270**
2271*/
2272static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
2273 SqliteDb *p;
2274 void *pKey = 0;
2275 int nKey = 0;
2276 const char *zArg;
2277 char *zErrMsg;
2278 int i;
2279 const char *zFile;
2280 const char *zVfs = 0;
2281 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
2282 Tcl_DString translatedFilename;
2283 if( objc==2 ){
2284 zArg = Tcl_GetStringFromObj(objv[1], 0);
2285 if( strcmp(zArg,"-version")==0 ){
2286 Tcl_AppendResult(interp,sqlite3_version,0);
2287 return TCL_OK;
2288 }
2289 if( strcmp(zArg,"-has-codec")==0 ){
2290#ifdef SQLITE_HAS_CODEC
2291 Tcl_AppendResult(interp,"1",0);
2292#else
2293 Tcl_AppendResult(interp,"0",0);
2294#endif
2295 return TCL_OK;
2296 }
2297 }
2298 for(i=3; i+1<objc; i+=2){
2299 zArg = Tcl_GetString(objv[i]);
2300 if( strcmp(zArg,"-key")==0 ){
2301 pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey);
2302 }else if( strcmp(zArg, "-vfs")==0 ){
2303 i++;
2304 zVfs = Tcl_GetString(objv[i]);
2305 }else if( strcmp(zArg, "-readonly")==0 ){
2306 int b;
2307 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
2308 if( b ){
2309 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2310 flags |= SQLITE_OPEN_READONLY;
2311 }else{
2312 flags &= ~SQLITE_OPEN_READONLY;
2313 flags |= SQLITE_OPEN_READWRITE;
2314 }
2315 }else if( strcmp(zArg, "-create")==0 ){
2316 int b;
2317 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
2318 if( b && (flags & SQLITE_OPEN_READONLY)==0 ){
2319 flags |= SQLITE_OPEN_CREATE;
2320 }else{
2321 flags &= ~SQLITE_OPEN_CREATE;
2322 }
2323 }else{
2324 Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
2325 return TCL_ERROR;
2326 }
2327 }
2328 if( objc<3 || (objc&1)!=1 ){
2329 Tcl_WrongNumArgs(interp, 1, objv,
2330 "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
2331#ifdef SQLITE_HAS_CODEC
2332 " ?-key CODECKEY?"
2333#endif
2334 );
2335 return TCL_ERROR;
2336 }
2337 zErrMsg = 0;
2338 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
2339 if( p==0 ){
2340 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
2341 return TCL_ERROR;
2342 }
2343 memset(p, 0, sizeof(*p));
2344 zFile = Tcl_GetStringFromObj(objv[2], 0);
2345 zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
2346 sqlite3_open_v2(zFile, &p->db, flags, zVfs);
2347 Tcl_DStringFree(&translatedFilename);
2348 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
2349 zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
2350 sqlite3_close(p->db);
2351 p->db = 0;
2352 }
2353#ifdef SQLITE_TEST
2354 if( p->db ){
2355 extern int Md5_Register(sqlite3*);
2356 if( Md5_Register(p->db)==SQLITE_NOMEM ){
2357 zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
2358 sqlite3_close(p->db);
2359 p->db = 0;
2360 }
2361 }
2362#endif
2363#ifdef SQLITE_HAS_CODEC
2364 if( p->db ){
2365 sqlite3_key(p->db, pKey, nKey);
2366 }
2367#endif
2368 if( p->db==0 ){
2369 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
2370 Tcl_Free((char*)p);
2371 sqlite3_free(zErrMsg);
2372 return TCL_ERROR;
2373 }
2374 p->maxStmt = NUM_PREPARED_STMTS;
2375 p->interp = interp;
2376 zArg = Tcl_GetStringFromObj(objv[1], 0);
2377 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
2378 return TCL_OK;
2379}
2380
2381/*
2382** Provide a dummy Tcl_InitStubs if we are using this as a static
2383** library.
2384*/
2385#ifndef USE_TCL_STUBS
2386# undef Tcl_InitStubs
2387# define Tcl_InitStubs(a,b,c)
2388#endif
2389
2390/*
2391** Make sure we have a PACKAGE_VERSION macro defined. This will be
2392** defined automatically by the TEA makefile. But other makefiles
2393** do not define it.
2394*/
2395#ifndef PACKAGE_VERSION
2396# define PACKAGE_VERSION SQLITE_VERSION
2397#endif
2398
2399/*
2400** Initialize this module.
2401**
2402** This Tcl module contains only a single new Tcl command named "sqlite".
2403** (Hence there is no namespace. There is no point in using a namespace
2404** if the extension only supplies one new name!) The "sqlite" command is
2405** used to open a new SQLite database. See the DbMain() routine above
2406** for additional information.
2407*/
2408EXTERN int Sqlite3_Init(Tcl_Interp *interp){
2409 Tcl_InitStubs(interp, "8.4", 0);
2410 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
2411 Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION);
2412 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
2413 Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION);
2414 return TCL_OK;
2415}
2416EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2417EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2418EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2419
2420#ifndef SQLITE_3_SUFFIX_ONLY
2421EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2422EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2423EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2424EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2425#endif
2426
2427#ifdef TCLSH
2428/*****************************************************************************
2429** The code that follows is used to build standalone TCL interpreters
2430** that are statically linked with SQLite.
2431*/
2432
2433/*
2434** If the macro TCLSH is one, then put in code this for the
2435** "main" routine that will initialize Tcl and take input from
2436** standard input, or if a file is named on the command line
2437** the TCL interpreter reads and evaluates that file.
2438*/
2439#if TCLSH==1
2440static char zMainloop[] =
2441 "set line {}\n"
2442 "while {![eof stdin]} {\n"
2443 "if {$line!=\"\"} {\n"
2444 "puts -nonewline \"> \"\n"
2445 "} else {\n"
2446 "puts -nonewline \"% \"\n"
2447 "}\n"
2448 "flush stdout\n"
2449 "append line [gets stdin]\n"
2450 "if {[info complete $line]} {\n"
2451 "if {[catch {uplevel #0 $line} result]} {\n"
2452 "puts stderr \"Error: $result\"\n"
2453 "} elseif {$result!=\"\"} {\n"
2454 "puts $result\n"
2455 "}\n"
2456 "set line {}\n"
2457 "} else {\n"
2458 "append line \\n\n"
2459 "}\n"
2460 "}\n"
2461;
2462#endif
2463
2464/*
2465** If the macro TCLSH is two, then get the main loop code out of
2466** the separate file "spaceanal_tcl.h".
2467*/
2468#if TCLSH==2
2469static char zMainloop[] =
2470#include "spaceanal_tcl.h"
2471;
2472#endif
2473
2474#define TCLSH_MAIN main /* Needed to fake out mktclapp */
2475int TCLSH_MAIN(int argc, char **argv){
2476 Tcl_Interp *interp;
2477 Tcl_FindExecutable(argv[0]);
2478 interp = Tcl_CreateInterp();
2479 Sqlite3_Init(interp);
2480#ifdef SQLITE_TEST
2481 {
2482 extern int Md5_Init(Tcl_Interp*);
2483 extern int Sqliteconfig_Init(Tcl_Interp*);
2484 extern int Sqlitetest1_Init(Tcl_Interp*);
2485 extern int Sqlitetest2_Init(Tcl_Interp*);
2486 extern int Sqlitetest3_Init(Tcl_Interp*);
2487 extern int Sqlitetest4_Init(Tcl_Interp*);
2488 extern int Sqlitetest5_Init(Tcl_Interp*);
2489 extern int Sqlitetest6_Init(Tcl_Interp*);
2490 extern int Sqlitetest7_Init(Tcl_Interp*);
2491 extern int Sqlitetest8_Init(Tcl_Interp*);
2492 extern int Sqlitetest9_Init(Tcl_Interp*);
2493 extern int Sqlitetestasync_Init(Tcl_Interp*);
2494 extern int Sqlitetest_autoext_Init(Tcl_Interp*);
2495 extern int Sqlitetest_hexio_Init(Tcl_Interp*);
2496 extern int Sqlitetest_malloc_Init(Tcl_Interp*);
2497 extern int Sqlitetestschema_Init(Tcl_Interp*);
2498 extern int Sqlitetestsse_Init(Tcl_Interp*);
2499 extern int Sqlitetesttclvar_Init(Tcl_Interp*);
2500 extern int SqlitetestThread_Init(Tcl_Interp*);
2501 extern int SqlitetestOnefile_Init();
2502
2503 Md5_Init(interp);
2504 Sqliteconfig_Init(interp);
2505 Sqlitetest1_Init(interp);
2506 Sqlitetest2_Init(interp);
2507 Sqlitetest3_Init(interp);
2508 Sqlitetest4_Init(interp);
2509 Sqlitetest5_Init(interp);
2510 Sqlitetest6_Init(interp);
2511 Sqlitetest7_Init(interp);
2512 Sqlitetest8_Init(interp);
2513 Sqlitetest9_Init(interp);
2514 Sqlitetestasync_Init(interp);
2515 Sqlitetest_autoext_Init(interp);
2516 Sqlitetest_hexio_Init(interp);
2517 Sqlitetest_malloc_Init(interp);
2518 Sqlitetestschema_Init(interp);
2519 Sqlitetesttclvar_Init(interp);
2520 SqlitetestThread_Init(interp);
2521 SqlitetestOnefile_Init(interp);
2522
2523#ifdef SQLITE_SSE
2524 Sqlitetestsse_Init(interp);
2525#endif
2526 }
2527#endif
2528 if( argc>=2 || TCLSH==2 ){
2529 int i;
2530 char zArgc[32];
2531 sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH));
2532 Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY);
2533 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
2534 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
2535 for(i=3-TCLSH; i<argc; i++){
2536 Tcl_SetVar(interp, "argv", argv[i],
2537 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
2538 }
2539 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
2540 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
2541 if( zInfo==0 ) zInfo = interp->result;
2542 fprintf(stderr,"%s: %s\n", *argv, zInfo);
2543 return 1;
2544 }
2545 }
2546 if( argc<=1 || TCLSH==2 ){
2547 Tcl_GlobalEval(interp, zMainloop);
2548 }
2549 return 0;
2550}
2551#endif /* TCLSH */
diff --git a/libraries/sqlite/win32/tokenize.c b/libraries/sqlite/win32/tokenize.c
new file mode 100755
index 0000000..b4a9b9b
--- /dev/null
+++ b/libraries/sqlite/win32/tokenize.c
@@ -0,0 +1,508 @@
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.
10**
11*************************************************************************
12** An tokenizer for SQL
13**
14** This file contains C code that splits an SQL input string up into
15** individual tokens and sends those tokens one-by-one over to the
16** parser for analysis.
17**
18** $Id: tokenize.c,v 1.136 2007/08/27 23:26:59 drh Exp $
19*/
20#include "sqliteInt.h"
21#include <ctype.h>
22#include <stdlib.h>
23
24/*
25** The charMap() macro maps alphabetic characters into their
26** lower-case ASCII equivalent. On ASCII machines, this is just
27** an upper-to-lower case map. On EBCDIC machines we also need
28** to adjust the encoding. Only alphabetic characters and underscores
29** need to be translated.
30*/
31#ifdef SQLITE_ASCII
32# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
33#endif
34#ifdef SQLITE_EBCDIC
35# define charMap(X) ebcdicToAscii[(unsigned char)X]
36const unsigned char ebcdicToAscii[] = {
37/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
46 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
47 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
48 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
50 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
51 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
52 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
54};
55#endif
56
57/*
58** The sqlite3KeywordCode function looks up an identifier to determine if
59** it is a keyword. If it is a keyword, the token code of that keyword is
60** returned. If the input is not a keyword, TK_ID is returned.
61**
62** The implementation of this routine was generated by a program,
63** mkkeywordhash.h, located in the tool subdirectory of the distribution.
64** The output of the mkkeywordhash.c program is written into a file
65** named keywordhash.h and then included into this source file by
66** the #include below.
67*/
68#include "keywordhash.h"
69
70
71/*
72** If X is a character that can be used in an identifier then
73** IdChar(X) will be true. Otherwise it is false.
74**
75** For ASCII, any character with the high-order bit set is
76** allowed in an identifier. For 7-bit characters,
77** sqlite3IsIdChar[X] must be 1.
78**
79** For EBCDIC, the rules are more complex but have the same
80** end result.
81**
82** Ticket #1066. the SQL standard does not allow '$' in the
83** middle of identfiers. But many SQL implementations do.
84** SQLite will allow '$' in identifiers for compatibility.
85** But the feature is undocumented.
86*/
87#ifdef SQLITE_ASCII
88const char sqlite3IsAsciiIdChar[] = {
89/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
90 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
92 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
94 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
96};
97#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
98#endif
99#ifdef SQLITE_EBCDIC
100const char sqlite3IsEbcdicIdChar[] = {
101/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
102 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
103 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
104 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
105 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
106 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
107 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
108 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
110 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
111 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
112 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
114};
115#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
116#endif
117
118
119/*
120** Return the length of the token that begins at z[0].
121** Store the token type in *tokenType before returning.
122*/
123static int getToken(const unsigned char *z, int *tokenType){
124 int i, c;
125 switch( *z ){
126 case ' ': case '\t': case '\n': case '\f': case '\r': {
127 for(i=1; isspace(z[i]); i++){}
128 *tokenType = TK_SPACE;
129 return i;
130 }
131 case '-': {
132 if( z[1]=='-' ){
133 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
134 *tokenType = TK_COMMENT;
135 return i;
136 }
137 *tokenType = TK_MINUS;
138 return 1;
139 }
140 case '(': {
141 *tokenType = TK_LP;
142 return 1;
143 }
144 case ')': {
145 *tokenType = TK_RP;
146 return 1;
147 }
148 case ';': {
149 *tokenType = TK_SEMI;
150 return 1;
151 }
152 case '+': {
153 *tokenType = TK_PLUS;
154 return 1;
155 }
156 case '*': {
157 *tokenType = TK_STAR;
158 return 1;
159 }
160 case '/': {
161 if( z[1]!='*' || z[2]==0 ){
162 *tokenType = TK_SLASH;
163 return 1;
164 }
165 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
166 if( c ) i++;
167 *tokenType = TK_COMMENT;
168 return i;
169 }
170 case '%': {
171 *tokenType = TK_REM;
172 return 1;
173 }
174 case '=': {
175 *tokenType = TK_EQ;
176 return 1 + (z[1]=='=');
177 }
178 case '<': {
179 if( (c=z[1])=='=' ){
180 *tokenType = TK_LE;
181 return 2;
182 }else if( c=='>' ){
183 *tokenType = TK_NE;
184 return 2;
185 }else if( c=='<' ){
186 *tokenType = TK_LSHIFT;
187 return 2;
188 }else{
189 *tokenType = TK_LT;
190 return 1;
191 }
192 }
193 case '>': {
194 if( (c=z[1])=='=' ){
195 *tokenType = TK_GE;
196 return 2;
197 }else if( c=='>' ){
198 *tokenType = TK_RSHIFT;
199 return 2;
200 }else{
201 *tokenType = TK_GT;
202 return 1;
203 }
204 }
205 case '!': {
206 if( z[1]!='=' ){
207 *tokenType = TK_ILLEGAL;
208 return 2;
209 }else{
210 *tokenType = TK_NE;
211 return 2;
212 }
213 }
214 case '|': {
215 if( z[1]!='|' ){
216 *tokenType = TK_BITOR;
217 return 1;
218 }else{
219 *tokenType = TK_CONCAT;
220 return 2;
221 }
222 }
223 case ',': {
224 *tokenType = TK_COMMA;
225 return 1;
226 }
227 case '&': {
228 *tokenType = TK_BITAND;
229 return 1;
230 }
231 case '~': {
232 *tokenType = TK_BITNOT;
233 return 1;
234 }
235 case '`':
236 case '\'':
237 case '"': {
238 int delim = z[0];
239 for(i=1; (c=z[i])!=0; i++){
240 if( c==delim ){
241 if( z[i+1]==delim ){
242 i++;
243 }else{
244 break;
245 }
246 }
247 }
248 if( c ){
249 *tokenType = TK_STRING;
250 return i+1;
251 }else{
252 *tokenType = TK_ILLEGAL;
253 return i;
254 }
255 }
256 case '.': {
257#ifndef SQLITE_OMIT_FLOATING_POINT
258 if( !isdigit(z[1]) )
259#endif
260 {
261 *tokenType = TK_DOT;
262 return 1;
263 }
264 /* If the next character is a digit, this is a floating point
265 ** number that begins with ".". Fall thru into the next case */
266 }
267 case '0': case '1': case '2': case '3': case '4':
268 case '5': case '6': case '7': case '8': case '9': {
269 *tokenType = TK_INTEGER;
270 for(i=0; isdigit(z[i]); i++){}
271#ifndef SQLITE_OMIT_FLOATING_POINT
272 if( z[i]=='.' ){
273 i++;
274 while( isdigit(z[i]) ){ i++; }
275 *tokenType = TK_FLOAT;
276 }
277 if( (z[i]=='e' || z[i]=='E') &&
278 ( isdigit(z[i+1])
279 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
280 )
281 ){
282 i += 2;
283 while( isdigit(z[i]) ){ i++; }
284 *tokenType = TK_FLOAT;
285 }
286#endif
287 while( IdChar(z[i]) ){
288 *tokenType = TK_ILLEGAL;
289 i++;
290 }
291 return i;
292 }
293 case '[': {
294 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
295 *tokenType = TK_ID;
296 return i;
297 }
298 case '?': {
299 *tokenType = TK_VARIABLE;
300 for(i=1; isdigit(z[i]); i++){}
301 return i;
302 }
303 case '#': {
304 for(i=1; isdigit(z[i]); i++){}
305 if( i>1 ){
306 /* Parameters of the form #NNN (where NNN is a number) are used
307 ** internally by sqlite3NestedParse. */
308 *tokenType = TK_REGISTER;
309 return i;
310 }
311 /* Fall through into the next case if the '#' is not followed by
312 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
313 }
314#ifndef SQLITE_OMIT_TCL_VARIABLE
315 case '$':
316#endif
317 case '@': /* For compatibility with MS SQL Server */
318 case ':': {
319 int n = 0;
320 *tokenType = TK_VARIABLE;
321 for(i=1; (c=z[i])!=0; i++){
322 if( IdChar(c) ){
323 n++;
324#ifndef SQLITE_OMIT_TCL_VARIABLE
325 }else if( c=='(' && n>0 ){
326 do{
327 i++;
328 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
329 if( c==')' ){
330 i++;
331 }else{
332 *tokenType = TK_ILLEGAL;
333 }
334 break;
335 }else if( c==':' && z[i+1]==':' ){
336 i++;
337#endif
338 }else{
339 break;
340 }
341 }
342 if( n==0 ) *tokenType = TK_ILLEGAL;
343 return i;
344 }
345#ifndef SQLITE_OMIT_BLOB_LITERAL
346 case 'x': case 'X': {
347 if( (c=z[1])=='\'' || c=='"' ){
348 int delim = c;
349 *tokenType = TK_BLOB;
350 for(i=2; (c=z[i])!=0; i++){
351 if( c==delim ){
352 if( i%2 ) *tokenType = TK_ILLEGAL;
353 break;
354 }
355 if( !isxdigit(c) ){
356 *tokenType = TK_ILLEGAL;
357 return i;
358 }
359 }
360 if( c ) i++;
361 return i;
362 }
363 /* Otherwise fall through to the next case */
364 }
365#endif
366 default: {
367 if( !IdChar(*z) ){
368 break;
369 }
370 for(i=1; IdChar(z[i]); i++){}
371 *tokenType = keywordCode((char*)z, i);
372 return i;
373 }
374 }
375 *tokenType = TK_ILLEGAL;
376 return 1;
377}
378int sqlite3GetToken(const unsigned char *z, int *tokenType){
379 return getToken(z, tokenType);
380}
381
382/*
383** Run the parser on the given SQL string. The parser structure is
384** passed in. An SQLITE_ status code is returned. If an error occurs
385** and pzErrMsg!=NULL then an error message might be written into
386** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that
387** error message. Or maybe not.
388*/
389int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
390 int nErr = 0;
391 int i;
392 void *pEngine;
393 int tokenType;
394 int lastTokenParsed = -1;
395 sqlite3 *db = pParse->db;
396
397 if( db->activeVdbeCnt==0 ){
398 db->u1.isInterrupted = 0;
399 }
400 pParse->rc = SQLITE_OK;
401 i = 0;
402 pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc);
403 if( pEngine==0 ){
404 db->mallocFailed = 1;
405 return SQLITE_NOMEM;
406 }
407 assert( pParse->sLastToken.dyn==0 );
408 assert( pParse->pNewTable==0 );
409 assert( pParse->pNewTrigger==0 );
410 assert( pParse->nVar==0 );
411 assert( pParse->nVarExpr==0 );
412 assert( pParse->nVarExprAlloc==0 );
413 assert( pParse->apVarExpr==0 );
414 pParse->zTail = pParse->zSql = zSql;
415 while( !db->mallocFailed && zSql[i]!=0 ){
416 assert( i>=0 );
417 pParse->sLastToken.z = (u8*)&zSql[i];
418 assert( pParse->sLastToken.dyn==0 );
419 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
420 i += pParse->sLastToken.n;
421 if( i>SQLITE_MAX_SQL_LENGTH ){
422 pParse->rc = SQLITE_TOOBIG;
423 break;
424 }
425 switch( tokenType ){
426 case TK_SPACE:
427 case TK_COMMENT: {
428 if( db->u1.isInterrupted ){
429 pParse->rc = SQLITE_INTERRUPT;
430 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
431 goto abort_parse;
432 }
433 break;
434 }
435 case TK_ILLEGAL: {
436 if( pzErrMsg ){
437 sqlite3_free(*pzErrMsg);
438 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
439 &pParse->sLastToken);
440 }
441 nErr++;
442 goto abort_parse;
443 }
444 case TK_SEMI: {
445 pParse->zTail = &zSql[i];
446 /* Fall thru into the default case */
447 }
448 default: {
449 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
450 lastTokenParsed = tokenType;
451 if( pParse->rc!=SQLITE_OK ){
452 goto abort_parse;
453 }
454 break;
455 }
456 }
457 }
458abort_parse:
459 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
460 if( lastTokenParsed!=TK_SEMI ){
461 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
462 pParse->zTail = &zSql[i];
463 }
464 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
465 }
466 sqlite3ParserFree(pEngine, sqlite3_free);
467 if( db->mallocFailed ){
468 pParse->rc = SQLITE_NOMEM;
469 }
470 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
471 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
472 }
473 if( pParse->zErrMsg ){
474 if( pzErrMsg && *pzErrMsg==0 ){
475 *pzErrMsg = pParse->zErrMsg;
476 }else{
477 sqlite3_free(pParse->zErrMsg);
478 }
479 pParse->zErrMsg = 0;
480 if( !nErr ) nErr++;
481 }
482 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
483 sqlite3VdbeDelete(pParse->pVdbe);
484 pParse->pVdbe = 0;
485 }
486#ifndef SQLITE_OMIT_SHARED_CACHE
487 if( pParse->nested==0 ){
488 sqlite3_free(pParse->aTableLock);
489 pParse->aTableLock = 0;
490 pParse->nTableLock = 0;
491 }
492#endif
493
494 if( !IN_DECLARE_VTAB ){
495 /* If the pParse->declareVtab flag is set, do not delete any table
496 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
497 ** will take responsibility for freeing the Table structure.
498 */
499 sqlite3DeleteTable(pParse->pNewTable);
500 }
501
502 sqlite3DeleteTrigger(pParse->pNewTrigger);
503 sqlite3_free(pParse->apVarExpr);
504 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
505 pParse->rc = SQLITE_ERROR;
506 }
507 return nErr;
508}
diff --git a/libraries/sqlite/win32/trigger.c b/libraries/sqlite/win32/trigger.c
new file mode 100755
index 0000000..252dd7b
--- /dev/null
+++ b/libraries/sqlite/win32/trigger.c
@@ -0,0 +1,839 @@
1/*
2**
3** The author disclaims copyright to this source code. In place of
4** a legal notice, here is a blessing:
5**
6** May you do good and not evil.
7** May you find forgiveness for yourself and forgive others.
8** May you share freely, never taking more than you give.
9**
10*************************************************************************
11*
12*/
13#include "sqliteInt.h"
14
15#ifndef SQLITE_OMIT_TRIGGER
16/*
17** Delete a linked list of TriggerStep structures.
18*/
19void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){
20 while( pTriggerStep ){
21 TriggerStep * pTmp = pTriggerStep;
22 pTriggerStep = pTriggerStep->pNext;
23
24 if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z);
25 sqlite3ExprDelete(pTmp->pWhere);
26 sqlite3ExprListDelete(pTmp->pExprList);
27 sqlite3SelectDelete(pTmp->pSelect);
28 sqlite3IdListDelete(pTmp->pIdList);
29
30 sqlite3_free(pTmp);
31 }
32}
33
34/*
35** This is called by the parser when it sees a CREATE TRIGGER statement
36** up to the point of the BEGIN before the trigger actions. A Trigger
37** structure is generated based on the information available and stored
38** in pParse->pNewTrigger. After the trigger actions have been parsed, the
39** sqlite3FinishTrigger() function is called to complete the trigger
40** construction process.
41*/
42void sqlite3BeginTrigger(
43 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
44 Token *pName1, /* The name of the trigger */
45 Token *pName2, /* The name of the trigger */
46 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
47 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
48 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
49 SrcList *pTableName,/* The name of the table/view the trigger applies to */
50 Expr *pWhen, /* WHEN clause */
51 int isTemp, /* True if the TEMPORARY keyword is present */
52 int noErr /* Suppress errors if the trigger already exists */
53){
54 Trigger *pTrigger = 0;
55 Table *pTab;
56 char *zName = 0; /* Name of the trigger */
57 sqlite3 *db = pParse->db;
58 int iDb; /* The database to store the trigger in */
59 Token *pName; /* The unqualified db name */
60 DbFixer sFix;
61 int iTabDb;
62
63 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
64 assert( pName2!=0 );
65 if( isTemp ){
66 /* If TEMP was specified, then the trigger name may not be qualified. */
67 if( pName2->n>0 ){
68 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
69 goto trigger_cleanup;
70 }
71 iDb = 1;
72 pName = pName1;
73 }else{
74 /* Figure out the db that the the trigger will be created in */
75 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
76 if( iDb<0 ){
77 goto trigger_cleanup;
78 }
79 }
80
81 /* If the trigger name was unqualified, and the table is a temp table,
82 ** then set iDb to 1 to create the trigger in the temporary database.
83 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
84 ** exist, the error is caught by the block below.
85 */
86 if( !pTableName || db->mallocFailed ){
87 goto trigger_cleanup;
88 }
89 pTab = sqlite3SrcListLookup(pParse, pTableName);
90 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
91 iDb = 1;
92 }
93
94 /* Ensure the table name matches database name and that the table exists */
95 if( db->mallocFailed ) goto trigger_cleanup;
96 assert( pTableName->nSrc==1 );
97 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
98 sqlite3FixSrcList(&sFix, pTableName) ){
99 goto trigger_cleanup;
100 }
101 pTab = sqlite3SrcListLookup(pParse, pTableName);
102 if( !pTab ){
103 /* The table does not exist. */
104 goto trigger_cleanup;
105 }
106 if( IsVirtual(pTab) ){
107 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
108 goto trigger_cleanup;
109 }
110
111 /* Check that the trigger name is not reserved and that no trigger of the
112 ** specified name exists */
113 zName = sqlite3NameFromToken(db, pName);
114 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
115 goto trigger_cleanup;
116 }
117 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
118 if( !noErr ){
119 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
120 }
121 goto trigger_cleanup;
122 }
123
124 /* Do not create a trigger on a system table */
125 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
126 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
127 pParse->nErr++;
128 goto trigger_cleanup;
129 }
130
131 /* INSTEAD of triggers are only for views and views only support INSTEAD
132 ** of triggers.
133 */
134 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
135 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
136 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
137 goto trigger_cleanup;
138 }
139 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
140 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
141 " trigger on table: %S", pTableName, 0);
142 goto trigger_cleanup;
143 }
144 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
145
146#ifndef SQLITE_OMIT_AUTHORIZATION
147 {
148 int code = SQLITE_CREATE_TRIGGER;
149 const char *zDb = db->aDb[iTabDb].zName;
150 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
151 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
152 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
153 goto trigger_cleanup;
154 }
155 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
156 goto trigger_cleanup;
157 }
158 }
159#endif
160
161 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
162 ** cannot appear on views. So we might as well translate every
163 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
164 ** elsewhere.
165 */
166 if (tr_tm == TK_INSTEAD){
167 tr_tm = TK_BEFORE;
168 }
169
170 /* Build the Trigger object */
171 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
172 if( pTrigger==0 ) goto trigger_cleanup;
173 pTrigger->name = zName;
174 zName = 0;
175 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
176 pTrigger->pSchema = db->aDb[iDb].pSchema;
177 pTrigger->pTabSchema = pTab->pSchema;
178 pTrigger->op = op;
179 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
180 pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
181 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
182 sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
183 assert( pParse->pNewTrigger==0 );
184 pParse->pNewTrigger = pTrigger;
185
186trigger_cleanup:
187 sqlite3_free(zName);
188 sqlite3SrcListDelete(pTableName);
189 sqlite3IdListDelete(pColumns);
190 sqlite3ExprDelete(pWhen);
191 if( !pParse->pNewTrigger ){
192 sqlite3DeleteTrigger(pTrigger);
193 }else{
194 assert( pParse->pNewTrigger==pTrigger );
195 }
196}
197
198/*
199** This routine is called after all of the trigger actions have been parsed
200** in order to complete the process of building the trigger.
201*/
202void sqlite3FinishTrigger(
203 Parse *pParse, /* Parser context */
204 TriggerStep *pStepList, /* The triggered program */
205 Token *pAll /* Token that describes the complete CREATE TRIGGER */
206){
207 Trigger *pTrig = 0; /* The trigger whose construction is finishing up */
208 sqlite3 *db = pParse->db; /* The database */
209 DbFixer sFix;
210 int iDb; /* Database containing the trigger */
211
212 pTrig = pParse->pNewTrigger;
213 pParse->pNewTrigger = 0;
214 if( pParse->nErr || !pTrig ) goto triggerfinish_cleanup;
215 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
216 pTrig->step_list = pStepList;
217 while( pStepList ){
218 pStepList->pTrig = pTrig;
219 pStepList = pStepList->pNext;
220 }
221 if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken)
222 && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
223 goto triggerfinish_cleanup;
224 }
225
226 /* if we are not initializing, and this trigger is not on a TEMP table,
227 ** build the sqlite_master entry
228 */
229 if( !db->init.busy ){
230 static const VdbeOpList insertTrig[] = {
231 { OP_NewRowid, 0, 0, 0 },
232 { OP_String8, 0, 0, "trigger" },
233 { OP_String8, 0, 0, 0 }, /* 2: trigger name */
234 { OP_String8, 0, 0, 0 }, /* 3: table name */
235 { OP_Integer, 0, 0, 0 },
236 { OP_String8, 0, 0, "CREATE TRIGGER "},
237 { OP_String8, 0, 0, 0 }, /* 6: SQL */
238 { OP_Concat, 0, 0, 0 },
239 { OP_MakeRecord, 5, 0, "aaada" },
240 { OP_Insert, 0, 0, 0 },
241 };
242 int addr;
243 Vdbe *v;
244
245 /* Make an entry in the sqlite_master table */
246 v = sqlite3GetVdbe(pParse);
247 if( v==0 ) goto triggerfinish_cleanup;
248 sqlite3BeginWriteOperation(pParse, 0, iDb);
249 sqlite3OpenMasterTable(pParse, iDb);
250 addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
251 sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0);
252 sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0);
253 sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n);
254 sqlite3ChangeCookie(db, v, iDb);
255 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
256 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf(
257 db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC
258 );
259 }
260
261 if( db->init.busy ){
262 int n;
263 Table *pTab;
264 Trigger *pDel;
265 pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
266 pTrig->name, strlen(pTrig->name), pTrig);
267 if( pDel ){
268 assert( pDel==pTrig );
269 db->mallocFailed = 1;
270 goto triggerfinish_cleanup;
271 }
272 n = strlen(pTrig->table) + 1;
273 pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
274 assert( pTab!=0 );
275 pTrig->pNext = pTab->pTrigger;
276 pTab->pTrigger = pTrig;
277 pTrig = 0;
278 }
279
280triggerfinish_cleanup:
281 sqlite3DeleteTrigger(pTrig);
282 assert( !pParse->pNewTrigger );
283 sqlite3DeleteTriggerStep(pStepList);
284}
285
286/*
287** Make a copy of all components of the given trigger step. This has
288** the effect of copying all Expr.token.z values into memory obtained
289** from sqlite3_malloc(). As initially created, the Expr.token.z values
290** all point to the input string that was fed to the parser. But that
291** string is ephemeral - it will go away as soon as the sqlite3_exec()
292** call that started the parser exits. This routine makes a persistent
293** copy of all the Expr.token.z strings so that the TriggerStep structure
294** will be valid even after the sqlite3_exec() call returns.
295*/
296static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
297 if( p->target.z ){
298 p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
299 p->target.dyn = 1;
300 }
301 if( p->pSelect ){
302 Select *pNew = sqlite3SelectDup(db, p->pSelect);
303 sqlite3SelectDelete(p->pSelect);
304 p->pSelect = pNew;
305 }
306 if( p->pWhere ){
307 Expr *pNew = sqlite3ExprDup(db, p->pWhere);
308 sqlite3ExprDelete(p->pWhere);
309 p->pWhere = pNew;
310 }
311 if( p->pExprList ){
312 ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
313 sqlite3ExprListDelete(p->pExprList);
314 p->pExprList = pNew;
315 }
316 if( p->pIdList ){
317 IdList *pNew = sqlite3IdListDup(db, p->pIdList);
318 sqlite3IdListDelete(p->pIdList);
319 p->pIdList = pNew;
320 }
321}
322
323/*
324** Turn a SELECT statement (that the pSelect parameter points to) into
325** a trigger step. Return a pointer to a TriggerStep structure.
326**
327** The parser calls this routine when it finds a SELECT statement in
328** body of a TRIGGER.
329*/
330TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
331 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
332 if( pTriggerStep==0 ) {
333 sqlite3SelectDelete(pSelect);
334 return 0;
335 }
336
337 pTriggerStep->op = TK_SELECT;
338 pTriggerStep->pSelect = pSelect;
339 pTriggerStep->orconf = OE_Default;
340 sqlitePersistTriggerStep(db, pTriggerStep);
341
342 return pTriggerStep;
343}
344
345/*
346** Build a trigger step out of an INSERT statement. Return a pointer
347** to the new trigger step.
348**
349** The parser calls this routine when it sees an INSERT inside the
350** body of a trigger.
351*/
352TriggerStep *sqlite3TriggerInsertStep(
353 sqlite3 *db, /* The database connection */
354 Token *pTableName, /* Name of the table into which we insert */
355 IdList *pColumn, /* List of columns in pTableName to insert into */
356 ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
357 Select *pSelect, /* A SELECT statement that supplies values */
358 int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
359){
360 TriggerStep *pTriggerStep;
361
362 assert(pEList == 0 || pSelect == 0);
363 assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
364
365 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
366 if( pTriggerStep ){
367 pTriggerStep->op = TK_INSERT;
368 pTriggerStep->pSelect = pSelect;
369 pTriggerStep->target = *pTableName;
370 pTriggerStep->pIdList = pColumn;
371 pTriggerStep->pExprList = pEList;
372 pTriggerStep->orconf = orconf;
373 sqlitePersistTriggerStep(db, pTriggerStep);
374 }else{
375 sqlite3IdListDelete(pColumn);
376 sqlite3ExprListDelete(pEList);
377 sqlite3SelectDelete(pSelect);
378 }
379
380 return pTriggerStep;
381}
382
383/*
384** Construct a trigger step that implements an UPDATE statement and return
385** a pointer to that trigger step. The parser calls this routine when it
386** sees an UPDATE statement inside the body of a CREATE TRIGGER.
387*/
388TriggerStep *sqlite3TriggerUpdateStep(
389 sqlite3 *db, /* The database connection */
390 Token *pTableName, /* Name of the table to be updated */
391 ExprList *pEList, /* The SET clause: list of column and new values */
392 Expr *pWhere, /* The WHERE clause */
393 int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
394){
395 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
396 if( pTriggerStep==0 ){
397 sqlite3ExprListDelete(pEList);
398 sqlite3ExprDelete(pWhere);
399 return 0;
400 }
401
402 pTriggerStep->op = TK_UPDATE;
403 pTriggerStep->target = *pTableName;
404 pTriggerStep->pExprList = pEList;
405 pTriggerStep->pWhere = pWhere;
406 pTriggerStep->orconf = orconf;
407 sqlitePersistTriggerStep(db, pTriggerStep);
408
409 return pTriggerStep;
410}
411
412/*
413** Construct a trigger step that implements a DELETE statement and return
414** a pointer to that trigger step. The parser calls this routine when it
415** sees a DELETE statement inside the body of a CREATE TRIGGER.
416*/
417TriggerStep *sqlite3TriggerDeleteStep(
418 sqlite3 *db, /* Database connection */
419 Token *pTableName, /* The table from which rows are deleted */
420 Expr *pWhere /* The WHERE clause */
421){
422 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
423 if( pTriggerStep==0 ){
424 sqlite3ExprDelete(pWhere);
425 return 0;
426 }
427
428 pTriggerStep->op = TK_DELETE;
429 pTriggerStep->target = *pTableName;
430 pTriggerStep->pWhere = pWhere;
431 pTriggerStep->orconf = OE_Default;
432 sqlitePersistTriggerStep(db, pTriggerStep);
433
434 return pTriggerStep;
435}
436
437/*
438** Recursively delete a Trigger structure
439*/
440void sqlite3DeleteTrigger(Trigger *pTrigger){
441 if( pTrigger==0 ) return;
442 sqlite3DeleteTriggerStep(pTrigger->step_list);
443 sqlite3_free(pTrigger->name);
444 sqlite3_free(pTrigger->table);
445 sqlite3ExprDelete(pTrigger->pWhen);
446 sqlite3IdListDelete(pTrigger->pColumns);
447 if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
448 sqlite3_free(pTrigger);
449}
450
451/*
452** This function is called to drop a trigger from the database schema.
453**
454** This may be called directly from the parser and therefore identifies
455** the trigger by name. The sqlite3DropTriggerPtr() routine does the
456** same job as this routine except it takes a pointer to the trigger
457** instead of the trigger name.
458**/
459void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
460 Trigger *pTrigger = 0;
461 int i;
462 const char *zDb;
463 const char *zName;
464 int nName;
465 sqlite3 *db = pParse->db;
466
467 if( db->mallocFailed ) goto drop_trigger_cleanup;
468 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
469 goto drop_trigger_cleanup;
470 }
471
472 assert( pName->nSrc==1 );
473 zDb = pName->a[0].zDatabase;
474 zName = pName->a[0].zName;
475 nName = strlen(zName);
476 for(i=OMIT_TEMPDB; i<db->nDb; i++){
477 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
478 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
479 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
480 if( pTrigger ) break;
481 }
482 if( !pTrigger ){
483 if( !noErr ){
484 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
485 }
486 goto drop_trigger_cleanup;
487 }
488 sqlite3DropTriggerPtr(pParse, pTrigger);
489
490drop_trigger_cleanup:
491 sqlite3SrcListDelete(pName);
492}
493
494/*
495** Return a pointer to the Table structure for the table that a trigger
496** is set on.
497*/
498static Table *tableOfTrigger(Trigger *pTrigger){
499 int n = strlen(pTrigger->table) + 1;
500 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
501}
502
503
504/*
505** Drop a trigger given a pointer to that trigger.
506*/
507void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
508 Table *pTable;
509 Vdbe *v;
510 sqlite3 *db = pParse->db;
511 int iDb;
512
513 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
514 assert( iDb>=0 && iDb<db->nDb );
515 pTable = tableOfTrigger(pTrigger);
516 assert( pTable );
517 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
518#ifndef SQLITE_OMIT_AUTHORIZATION
519 {
520 int code = SQLITE_DROP_TRIGGER;
521 const char *zDb = db->aDb[iDb].zName;
522 const char *zTab = SCHEMA_TABLE(iDb);
523 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
524 if( sqlite3AuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
525 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
526 return;
527 }
528 }
529#endif
530
531 /* Generate code to destroy the database record of the trigger.
532 */
533 assert( pTable!=0 );
534 if( (v = sqlite3GetVdbe(pParse))!=0 ){
535 int base;
536 static const VdbeOpList dropTrigger[] = {
537 { OP_Rewind, 0, ADDR(9), 0},
538 { OP_String8, 0, 0, 0}, /* 1 */
539 { OP_Column, 0, 1, 0},
540 { OP_Ne, 0, ADDR(8), 0},
541 { OP_String8, 0, 0, "trigger"},
542 { OP_Column, 0, 0, 0},
543 { OP_Ne, 0, ADDR(8), 0},
544 { OP_Delete, 0, 0, 0},
545 { OP_Next, 0, ADDR(1), 0}, /* 8 */
546 };
547
548 sqlite3BeginWriteOperation(pParse, 0, iDb);
549 sqlite3OpenMasterTable(pParse, iDb);
550 base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
551 sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
552 sqlite3ChangeCookie(db, v, iDb);
553 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
554 sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0);
555 }
556}
557
558/*
559** Remove a trigger from the hash tables of the sqlite* pointer.
560*/
561void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
562 Trigger *pTrigger;
563 int nName = strlen(zName);
564 pTrigger = sqlite3HashInsert(&(db->aDb[iDb].pSchema->trigHash),
565 zName, nName, 0);
566 if( pTrigger ){
567 Table *pTable = tableOfTrigger(pTrigger);
568 assert( pTable!=0 );
569 if( pTable->pTrigger == pTrigger ){
570 pTable->pTrigger = pTrigger->pNext;
571 }else{
572 Trigger *cc = pTable->pTrigger;
573 while( cc ){
574 if( cc->pNext == pTrigger ){
575 cc->pNext = cc->pNext->pNext;
576 break;
577 }
578 cc = cc->pNext;
579 }
580 assert(cc);
581 }
582 sqlite3DeleteTrigger(pTrigger);
583 db->flags |= SQLITE_InternChanges;
584 }
585}
586
587/*
588** pEList is the SET clause of an UPDATE statement. Each entry
589** in pEList is of the format <id>=<expr>. If any of the entries
590** in pEList have an <id> which matches an identifier in pIdList,
591** then return TRUE. If pIdList==NULL, then it is considered a
592** wildcard that matches anything. Likewise if pEList==NULL then
593** it matches anything so always return true. Return false only
594** if there is no match.
595*/
596static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
597 int e;
598 if( !pIdList || !pEList ) return 1;
599 for(e=0; e<pEList->nExpr; e++){
600 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
601 }
602 return 0;
603}
604
605/*
606** Return a bit vector to indicate what kind of triggers exist for operation
607** "op" on table pTab. If pChanges is not NULL then it is a list of columns
608** that are being updated. Triggers only match if the ON clause of the
609** trigger definition overlaps the set of columns being updated.
610**
611** The returned bit vector is some combination of TRIGGER_BEFORE and
612** TRIGGER_AFTER.
613*/
614int sqlite3TriggersExist(
615 Parse *pParse, /* Used to check for recursive triggers */
616 Table *pTab, /* The table the contains the triggers */
617 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
618 ExprList *pChanges /* Columns that change in an UPDATE statement */
619){
620 Trigger *pTrigger;
621 int mask = 0;
622
623 pTrigger = IsVirtual(pTab) ? 0 : pTab->pTrigger;
624 while( pTrigger ){
625 if( pTrigger->op==op && checkColumnOverLap(pTrigger->pColumns, pChanges) ){
626 mask |= pTrigger->tr_tm;
627 }
628 pTrigger = pTrigger->pNext;
629 }
630 return mask;
631}
632
633/*
634** Convert the pStep->target token into a SrcList and return a pointer
635** to that SrcList.
636**
637** This routine adds a specific database name, if needed, to the target when
638** forming the SrcList. This prevents a trigger in one database from
639** referring to a target in another database. An exception is when the
640** trigger is in TEMP in which case it can refer to any other database it
641** wants.
642*/
643static SrcList *targetSrcList(
644 Parse *pParse, /* The parsing context */
645 TriggerStep *pStep /* The trigger containing the target token */
646){
647 Token sDb; /* Dummy database name token */
648 int iDb; /* Index of the database to use */
649 SrcList *pSrc; /* SrcList to be returned */
650
651 iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
652 if( iDb==0 || iDb>=2 ){
653 assert( iDb<pParse->db->nDb );
654 sDb.z = (u8*)pParse->db->aDb[iDb].zName;
655 sDb.n = strlen((char*)sDb.z);
656 pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
657 } else {
658 pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
659 }
660 return pSrc;
661}
662
663/*
664** Generate VDBE code for zero or more statements inside the body of a
665** trigger.
666*/
667static int codeTriggerProgram(
668 Parse *pParse, /* The parser context */
669 TriggerStep *pStepList, /* List of statements inside the trigger body */
670 int orconfin /* Conflict algorithm. (OE_Abort, etc) */
671){
672 TriggerStep * pTriggerStep = pStepList;
673 int orconf;
674 Vdbe *v = pParse->pVdbe;
675 sqlite3 *db = pParse->db;
676
677 assert( pTriggerStep!=0 );
678 assert( v!=0 );
679 sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
680 VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name));
681 while( pTriggerStep ){
682 orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
683 pParse->trigStack->orconf = orconf;
684 switch( pTriggerStep->op ){
685 case TK_SELECT: {
686 Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
687 if( ss ){
688 sqlite3SelectResolve(pParse, ss, 0);
689 sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
690 sqlite3SelectDelete(ss);
691 }
692 break;
693 }
694 case TK_UPDATE: {
695 SrcList *pSrc;
696 pSrc = targetSrcList(pParse, pTriggerStep);
697 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
698 sqlite3Update(pParse, pSrc,
699 sqlite3ExprListDup(db, pTriggerStep->pExprList),
700 sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
701 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
702 break;
703 }
704 case TK_INSERT: {
705 SrcList *pSrc;
706 pSrc = targetSrcList(pParse, pTriggerStep);
707 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
708 sqlite3Insert(pParse, pSrc,
709 sqlite3ExprListDup(db, pTriggerStep->pExprList),
710 sqlite3SelectDup(db, pTriggerStep->pSelect),
711 sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
712 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
713 break;
714 }
715 case TK_DELETE: {
716 SrcList *pSrc;
717 sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
718 pSrc = targetSrcList(pParse, pTriggerStep);
719 sqlite3DeleteFrom(pParse, pSrc,
720 sqlite3ExprDup(db, pTriggerStep->pWhere));
721 sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
722 break;
723 }
724 default:
725 assert(0);
726 }
727 pTriggerStep = pTriggerStep->pNext;
728 }
729 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
730 VdbeComment((v, "# end trigger %s", pStepList->pTrig->name));
731
732 return 0;
733}
734
735/*
736** This is called to code FOR EACH ROW triggers.
737**
738** When the code that this function generates is executed, the following
739** must be true:
740**
741** 1. No cursors may be open in the main database. (But newIdx and oldIdx
742** can be indices of cursors in temporary tables. See below.)
743**
744** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
745** a temporary vdbe cursor (index newIdx) must be open and pointing at
746** a row containing values to be substituted for new.* expressions in the
747** trigger program(s).
748**
749** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
750** a temporary vdbe cursor (index oldIdx) must be open and pointing at
751** a row containing values to be substituted for old.* expressions in the
752** trigger program(s).
753**
754*/
755int sqlite3CodeRowTrigger(
756 Parse *pParse, /* Parse context */
757 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
758 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
759 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
760 Table *pTab, /* The table to code triggers from */
761 int newIdx, /* The indice of the "new" row to access */
762 int oldIdx, /* The indice of the "old" row to access */
763 int orconf, /* ON CONFLICT policy */
764 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
765){
766 Trigger *p;
767 TriggerStack trigStackEntry;
768
769 assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
770 assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
771
772 assert(newIdx != -1 || oldIdx != -1);
773
774 for(p=pTab->pTrigger; p; p=p->pNext){
775 int fire_this = 0;
776
777 /* Determine whether we should code this trigger */
778 if(
779 p->op==op &&
780 p->tr_tm==tr_tm &&
781 (p->pSchema==p->pTabSchema || p->pSchema==pParse->db->aDb[1].pSchema) &&
782 (op!=TK_UPDATE||!p->pColumns||checkColumnOverLap(p->pColumns,pChanges))
783 ){
784 TriggerStack *pS; /* Pointer to trigger-stack entry */
785 for(pS=pParse->trigStack; pS && p!=pS->pTrigger; pS=pS->pNext){}
786 if( !pS ){
787 fire_this = 1;
788 }
789#if 0 /* Give no warning for recursive triggers. Just do not do them */
790 else{
791 sqlite3ErrorMsg(pParse, "recursive triggers not supported (%s)",
792 p->name);
793 return SQLITE_ERROR;
794 }
795#endif
796 }
797
798 if( fire_this ){
799 int endTrigger;
800 Expr * whenExpr;
801 AuthContext sContext;
802 NameContext sNC;
803
804 memset(&sNC, 0, sizeof(sNC));
805 sNC.pParse = pParse;
806
807 /* Push an entry on to the trigger stack */
808 trigStackEntry.pTrigger = p;
809 trigStackEntry.newIdx = newIdx;
810 trigStackEntry.oldIdx = oldIdx;
811 trigStackEntry.pTab = pTab;
812 trigStackEntry.pNext = pParse->trigStack;
813 trigStackEntry.ignoreJump = ignoreJump;
814 pParse->trigStack = &trigStackEntry;
815 sqlite3AuthContextPush(pParse, &sContext, p->name);
816
817 /* code the WHEN clause */
818 endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
819 whenExpr = sqlite3ExprDup(pParse->db, p->pWhen);
820 if( sqlite3ExprResolveNames(&sNC, whenExpr) ){
821 pParse->trigStack = trigStackEntry.pNext;
822 sqlite3ExprDelete(whenExpr);
823 return 1;
824 }
825 sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1);
826 sqlite3ExprDelete(whenExpr);
827
828 codeTriggerProgram(pParse, p->step_list, orconf);
829
830 /* Pop the entry off the trigger stack */
831 pParse->trigStack = trigStackEntry.pNext;
832 sqlite3AuthContextPop(&sContext);
833
834 sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger);
835 }
836 }
837 return 0;
838}
839#endif /* !defined(SQLITE_OMIT_TRIGGER) */
diff --git a/libraries/sqlite/win32/update.c b/libraries/sqlite/win32/update.c
new file mode 100755
index 0000000..6cf7f73
--- /dev/null
+++ b/libraries/sqlite/win32/update.c
@@ -0,0 +1,630 @@
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.
10**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle UPDATE statements.
14**
15** $Id: update.c,v 1.140 2007/08/16 10:09:03 danielk1977 Exp $
16*/
17#include "sqliteInt.h"
18
19#ifndef SQLITE_OMIT_VIRTUALTABLE
20/* Forward declaration */
21static void updateVirtualTable(
22 Parse *pParse, /* The parsing context */
23 SrcList *pSrc, /* The virtual table to be modified */
24 Table *pTab, /* The virtual table */
25 ExprList *pChanges, /* The columns to change in the UPDATE statement */
26 Expr *pRowidExpr, /* Expression used to recompute the rowid */
27 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
28 Expr *pWhere /* WHERE clause of the UPDATE statement */
29);
30#endif /* SQLITE_OMIT_VIRTUALTABLE */
31
32/*
33** The most recently coded instruction was an OP_Column to retrieve the
34** i-th column of table pTab. This routine sets the P3 parameter of the
35** OP_Column to the default value, if any.
36**
37** The default value of a column is specified by a DEFAULT clause in the
38** column definition. This was either supplied by the user when the table
39** was created, or added later to the table definition by an ALTER TABLE
40** command. If the latter, then the row-records in the table btree on disk
41** may not contain a value for the column and the default value, taken
42** from the P3 parameter of the OP_Column instruction, is returned instead.
43** If the former, then all row-records are guaranteed to include a value
44** for the column and the P3 value is not required.
45**
46** Column definitions created by an ALTER TABLE command may only have
47** literal default values specified: a number, null or a string. (If a more
48** complicated default expression value was provided, it is evaluated
49** when the ALTER TABLE is executed and one of the literal values written
50** into the sqlite_master table.)
51**
52** Therefore, the P3 parameter is only required if the default value for
53** the column is a literal number, string or null. The sqlite3ValueFromExpr()
54** function is capable of transforming these types of expressions into
55** sqlite3_value objects.
56*/
57void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
58 if( pTab && !pTab->pSelect ){
59 sqlite3_value *pValue;
60 u8 enc = ENC(sqlite3VdbeDb(v));
61 Column *pCol = &pTab->aCol[i];
62 assert( i<pTab->nCol );
63 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue);
64 if( pValue ){
65 sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
66 }else{
67 VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName));
68 }
69 }
70}
71
72/*
73** Process an UPDATE statement.
74**
75** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
76** \_______/ \________/ \______/ \________________/
77* onError pTabList pChanges pWhere
78*/
79void sqlite3Update(
80 Parse *pParse, /* The parser context */
81 SrcList *pTabList, /* The table in which we should change things */
82 ExprList *pChanges, /* Things to be changed */
83 Expr *pWhere, /* The WHERE clause. May be null */
84 int onError /* How to handle constraint errors */
85){
86 int i, j; /* Loop counters */
87 Table *pTab; /* The table to be updated */
88 int addr = 0; /* VDBE instruction address of the start of the loop */
89 WhereInfo *pWInfo; /* Information about the WHERE clause */
90 Vdbe *v; /* The virtual database engine */
91 Index *pIdx; /* For looping over indices */
92 int nIdx; /* Number of indices that need updating */
93 int nIdxTotal; /* Total number of indices */
94 int iCur; /* VDBE Cursor number of pTab */
95 sqlite3 *db; /* The database structure */
96 Index **apIdx = 0; /* An array of indices that need updating too */
97 char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
98 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
99 ** an expression for the i-th column of the table.
100 ** aXRef[i]==-1 if the i-th column is not changed. */
101 int chngRowid; /* True if the record number is being changed */
102 Expr *pRowidExpr = 0; /* Expression defining the new record number */
103 int openAll = 0; /* True if all indices need to be opened */
104 AuthContext sContext; /* The authorization context */
105 NameContext sNC; /* The name-context to resolve expressions in */
106 int iDb; /* Database containing the table being updated */
107 int memCnt = 0; /* Memory cell used for counting rows changed */
108
109#ifndef SQLITE_OMIT_TRIGGER
110 int isView; /* Trying to update a view */
111 int triggers_exist = 0; /* True if any row triggers exist */
112#endif
113
114 int newIdx = -1; /* index of trigger "new" temp table */
115 int oldIdx = -1; /* index of trigger "old" temp table */
116
117 sContext.pParse = 0;
118 db = pParse->db;
119 if( pParse->nErr || db->mallocFailed ){
120 goto update_cleanup;
121 }
122 assert( pTabList->nSrc==1 );
123
124 /* Locate the table which we want to update.
125 */
126 pTab = sqlite3SrcListLookup(pParse, pTabList);
127 if( pTab==0 ) goto update_cleanup;
128 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
129
130 /* Figure out if we have any triggers and if the table being
131 ** updated is a view
132 */
133#ifndef SQLITE_OMIT_TRIGGER
134 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
135 isView = pTab->pSelect!=0;
136#else
137# define triggers_exist 0
138# define isView 0
139#endif
140#ifdef SQLITE_OMIT_VIEW
141# undef isView
142# define isView 0
143#endif
144
145 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
146 goto update_cleanup;
147 }
148 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
149 goto update_cleanup;
150 }
151 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
152 if( aXRef==0 ) goto update_cleanup;
153 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
154
155 /* If there are FOR EACH ROW triggers, allocate cursors for the
156 ** special OLD and NEW tables
157 */
158 if( triggers_exist ){
159 newIdx = pParse->nTab++;
160 oldIdx = pParse->nTab++;
161 }
162
163 /* Allocate a cursors for the main database table and for all indices.
164 ** The index cursors might not be used, but if they are used they
165 ** need to occur right after the database cursor. So go ahead and
166 ** allocate enough space, just in case.
167 */
168 pTabList->a[0].iCursor = iCur = pParse->nTab++;
169 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
170 pParse->nTab++;
171 }
172
173 /* Initialize the name-context */
174 memset(&sNC, 0, sizeof(sNC));
175 sNC.pParse = pParse;
176 sNC.pSrcList = pTabList;
177
178 /* Resolve the column names in all the expressions of the
179 ** of the UPDATE statement. Also find the column index
180 ** for each column to be updated in the pChanges array. For each
181 ** column to be updated, make sure we have authorization to change
182 ** that column.
183 */
184 chngRowid = 0;
185 for(i=0; i<pChanges->nExpr; i++){
186 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
187 goto update_cleanup;
188 }
189 for(j=0; j<pTab->nCol; j++){
190 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
191 if( j==pTab->iPKey ){
192 chngRowid = 1;
193 pRowidExpr = pChanges->a[i].pExpr;
194 }
195 aXRef[j] = i;
196 break;
197 }
198 }
199 if( j>=pTab->nCol ){
200 if( sqlite3IsRowid(pChanges->a[i].zName) ){
201 chngRowid = 1;
202 pRowidExpr = pChanges->a[i].pExpr;
203 }else{
204 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
205 goto update_cleanup;
206 }
207 }
208#ifndef SQLITE_OMIT_AUTHORIZATION
209 {
210 int rc;
211 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
212 pTab->aCol[j].zName, db->aDb[iDb].zName);
213 if( rc==SQLITE_DENY ){
214 goto update_cleanup;
215 }else if( rc==SQLITE_IGNORE ){
216 aXRef[j] = -1;
217 }
218 }
219#endif
220 }
221
222 /* Allocate memory for the array apIdx[] and fill it with pointers to every
223 ** index that needs to be updated. Indices only need updating if their
224 ** key includes one of the columns named in pChanges or if the record
225 ** number of the original table entry is changing.
226 */
227 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
228 if( chngRowid ){
229 i = 0;
230 }else {
231 for(i=0; i<pIdx->nColumn; i++){
232 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
233 }
234 }
235 if( i<pIdx->nColumn ) nIdx++;
236 }
237 if( nIdxTotal>0 ){
238 apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal );
239 if( apIdx==0 ) goto update_cleanup;
240 aIdxUsed = (char*)&apIdx[nIdx];
241 }
242 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
243 if( chngRowid ){
244 i = 0;
245 }else{
246 for(i=0; i<pIdx->nColumn; i++){
247 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
248 }
249 }
250 if( i<pIdx->nColumn ){
251 apIdx[nIdx++] = pIdx;
252 aIdxUsed[j] = 1;
253 }else{
254 aIdxUsed[j] = 0;
255 }
256 }
257
258 /* Begin generating code.
259 */
260 v = sqlite3GetVdbe(pParse);
261 if( v==0 ) goto update_cleanup;
262 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
263 sqlite3BeginWriteOperation(pParse, 1, iDb);
264
265#ifndef SQLITE_OMIT_VIRTUALTABLE
266 /* Virtual tables must be handled separately */
267 if( IsVirtual(pTab) ){
268 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
269 pWhere);
270 pWhere = 0;
271 pTabList = 0;
272 goto update_cleanup;
273 }
274#endif
275
276 /* Resolve the column names in all the expressions in the
277 ** WHERE clause.
278 */
279 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
280 goto update_cleanup;
281 }
282
283 /* Start the view context
284 */
285 if( isView ){
286 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
287 }
288
289 /* If we are trying to update a view, realize that view into
290 ** a ephemeral table.
291 */
292 if( isView ){
293 Select *pView;
294 pView = sqlite3SelectDup(db, pTab->pSelect);
295 sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
296 sqlite3SelectDelete(pView);
297 }
298
299 /* Begin the database scan
300 */
301 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
302 if( pWInfo==0 ) goto update_cleanup;
303
304 /* Remember the rowid of every item to be updated.
305 */
306 sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
307 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
308
309 /* End the database scan loop.
310 */
311 sqlite3WhereEnd(pWInfo);
312
313 /* Initialize the count of updated rows
314 */
315 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
316 memCnt = pParse->nMem++;
317 sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt);
318 }
319
320 if( triggers_exist ){
321 /* Create pseudo-tables for NEW and OLD
322 */
323 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
324 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
325 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
326 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
327
328 /* The top of the update loop for when there are triggers.
329 */
330 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
331
332 if( !isView ){
333 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
334 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
335 /* Open a cursor and make it point to the record that is
336 ** being updated.
337 */
338 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
339 }
340 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
341
342 /* Generate the OLD table
343 */
344 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
345 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
346 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
347
348 /* Generate the NEW table
349 */
350 if( chngRowid ){
351 sqlite3ExprCodeAndCache(pParse, pRowidExpr);
352 }else{
353 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
354 }
355 for(i=0; i<pTab->nCol; i++){
356 if( i==pTab->iPKey ){
357 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
358 continue;
359 }
360 j = aXRef[i];
361 if( j<0 ){
362 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
363 sqlite3ColumnDefault(v, pTab, i);
364 }else{
365 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
366 }
367 }
368 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
369 if( !isView ){
370 sqlite3TableAffinityStr(v, pTab);
371 }
372 if( pParse->nErr ) goto update_cleanup;
373 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
374 if( !isView ){
375 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
376 }
377
378 /* Fire the BEFORE and INSTEAD OF triggers
379 */
380 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
381 newIdx, oldIdx, onError, addr) ){
382 goto update_cleanup;
383 }
384 }
385
386 if( !isView && !IsVirtual(pTab) ){
387 /*
388 ** Open every index that needs updating. Note that if any
389 ** index could potentially invoke a REPLACE conflict resolution
390 ** action, then we need to open all indices because we might need
391 ** to be deleting some records.
392 */
393 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
394 if( onError==OE_Replace ){
395 openAll = 1;
396 }else{
397 openAll = 0;
398 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
399 if( pIdx->onError==OE_Replace ){
400 openAll = 1;
401 break;
402 }
403 }
404 }
405 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
406 if( openAll || aIdxUsed[i] ){
407 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
408 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
409 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
410 (char*)pKey, P3_KEYINFO_HANDOFF);
411 assert( pParse->nTab>iCur+i+1 );
412 }
413 }
414
415 /* Loop over every record that needs updating. We have to load
416 ** the old data for each record to be updated because some columns
417 ** might not change and we will need to copy the old value.
418 ** Also, the old data is needed to delete the old index entries.
419 ** So make the cursor point at the old record.
420 */
421 if( !triggers_exist ){
422 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
423 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
424 }
425 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
426
427 /* If the record number will change, push the record number as it
428 ** will be after the update. (The old record number is currently
429 ** on top of the stack.)
430 */
431 if( chngRowid ){
432 sqlite3ExprCode(pParse, pRowidExpr);
433 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
434 }
435
436 /* Compute new data for this record.
437 */
438 for(i=0; i<pTab->nCol; i++){
439 if( i==pTab->iPKey ){
440 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
441 continue;
442 }
443 j = aXRef[i];
444 if( j<0 ){
445 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
446 sqlite3ColumnDefault(v, pTab, i);
447 }else{
448 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
449 }
450 }
451
452 /* Do constraint checks
453 */
454 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1,
455 onError, addr);
456
457 /* Delete the old indices for the current record.
458 */
459 sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed);
460
461 /* If changing the record number, delete the old record.
462 */
463 if( chngRowid ){
464 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
465 }
466
467 /* Create the new index entries and the new record.
468 */
469 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0);
470 }
471
472 /* Increment the row counter
473 */
474 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
475 sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
476 }
477
478 /* If there are triggers, close all the cursors after each iteration
479 ** through the loop. The fire the after triggers.
480 */
481 if( triggers_exist ){
482 if( !isView ){
483 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
484 if( openAll || aIdxUsed[i] )
485 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
486 }
487 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
488 }
489 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
490 newIdx, oldIdx, onError, addr) ){
491 goto update_cleanup;
492 }
493 }
494
495 /* Repeat the above with the next record to be updated, until
496 ** all record selected by the WHERE clause have been updated.
497 */
498 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
499 sqlite3VdbeJumpHere(v, addr);
500
501 /* Close all tables if there were no FOR EACH ROW triggers */
502 if( !triggers_exist ){
503 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
504 if( openAll || aIdxUsed[i] ){
505 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
506 }
507 }
508 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
509 }else{
510 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
511 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
512 }
513
514 /*
515 ** Return the number of rows that were changed. If this routine is
516 ** generating code because of a call to sqlite3NestedParse(), do not
517 ** invoke the callback function.
518 */
519 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
520 sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0);
521 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
522 sqlite3VdbeSetNumCols(v, 1);
523 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC);
524 }
525
526update_cleanup:
527 sqlite3AuthContextPop(&sContext);
528 sqlite3_free(apIdx);
529 sqlite3_free(aXRef);
530 sqlite3SrcListDelete(pTabList);
531 sqlite3ExprListDelete(pChanges);
532 sqlite3ExprDelete(pWhere);
533 return;
534}
535
536#ifndef SQLITE_OMIT_VIRTUALTABLE
537/*
538** Generate code for an UPDATE of a virtual table.
539**
540** The strategy is that we create an ephemerial table that contains
541** for each row to be changed:
542**
543** (A) The original rowid of that row.
544** (B) The revised rowid for the row. (note1)
545** (C) The content of every column in the row.
546**
547** Then we loop over this ephemeral table and for each row in
548** the ephermeral table call VUpdate.
549**
550** When finished, drop the ephemeral table.
551**
552** (note1) Actually, if we know in advance that (A) is always the same
553** as (B) we only store (A), then duplicate (A) when pulling
554** it out of the ephemeral table before calling VUpdate.
555*/
556static void updateVirtualTable(
557 Parse *pParse, /* The parsing context */
558 SrcList *pSrc, /* The virtual table to be modified */
559 Table *pTab, /* The virtual table */
560 ExprList *pChanges, /* The columns to change in the UPDATE statement */
561 Expr *pRowid, /* Expression used to recompute the rowid */
562 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
563 Expr *pWhere /* WHERE clause of the UPDATE statement */
564){
565 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
566 ExprList *pEList = 0; /* The result set of the SELECT statement */
567 Select *pSelect = 0; /* The SELECT statement */
568 Expr *pExpr; /* Temporary expression */
569 int ephemTab; /* Table holding the result of the SELECT */
570 int i; /* Loop counter */
571 int addr; /* Address of top of loop */
572 sqlite3 *db = pParse->db; /* Database connection */
573
574 /* Construct the SELECT statement that will find the new values for
575 ** all updated rows.
576 */
577 pEList = sqlite3ExprListAppend(pParse, 0,
578 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
579 if( pRowid ){
580 pEList = sqlite3ExprListAppend(pParse, pEList,
581 sqlite3ExprDup(db, pRowid), 0);
582 }
583 assert( pTab->iPKey<0 );
584 for(i=0; i<pTab->nCol; i++){
585 if( aXRef[i]>=0 ){
586 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
587 }else{
588 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
589 }
590 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
591 }
592 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
593
594 /* Create the ephemeral table into which the update results will
595 ** be stored.
596 */
597 assert( v );
598 ephemTab = pParse->nTab++;
599 sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
600
601 /* fill the ephemeral table
602 */
603 sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0);
604
605 /*
606 ** Generate code to scan the ephemeral table and call VDelete and
607 ** VInsert
608 */
609 sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0);
610 addr = sqlite3VdbeCurrentAddr(v);
611 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0);
612 if( pRowid ){
613 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1);
614 }else{
615 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
616 }
617 for(i=0; i<pTab->nCol; i++){
618 sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0));
619 }
620 pParse->pVirtualLock = pTab;
621 sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2,
622 (const char*)pTab->pVtab, P3_VTAB);
623 sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr);
624 sqlite3VdbeJumpHere(v, addr-1);
625 sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0);
626
627 /* Cleanup */
628 sqlite3SelectDelete(pSelect);
629}
630#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/libraries/sqlite/win32/utf.c b/libraries/sqlite/win32/utf.c
new file mode 100755
index 0000000..b6b3133
--- /dev/null
+++ b/libraries/sqlite/win32/utf.c
@@ -0,0 +1,545 @@
1/*
2** 2004 April 13
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.
10**
11*************************************************************************
12** This file contains routines used to translate between UTF-8,
13** UTF-16, UTF-16BE, and UTF-16LE.
14**
15** $Id: utf.c,v 1.59 2007/10/03 08:46:45 danielk1977 Exp $
16**
17** Notes on UTF-8:
18**
19** Byte-0 Byte-1 Byte-2 Byte-3 Value
20** 0xxxxxxx 00000000 00000000 0xxxxxxx
21** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
22** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
23** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
24**
25**
26** Notes on UTF-16: (with wwww+1==uuuuu)
27**
28** Word-0 Word-1 Value
29** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
30** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
31**
32**
33** BOM or Byte Order Mark:
34** 0xff 0xfe little-endian utf-16 follows
35** 0xfe 0xff big-endian utf-16 follows
36**
37*/
38#include "sqliteInt.h"
39#include <assert.h>
40#include "vdbeInt.h"
41
42/*
43** The following constant value is used by the SQLITE_BIGENDIAN and
44** SQLITE_LITTLEENDIAN macros.
45*/
46const int sqlite3one = 1;
47
48/*
49** This lookup table is used to help decode the first byte of
50** a multi-byte UTF8 character.
51*/
52static const unsigned char sqlite3UtfTrans1[] = {
53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
54 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
55 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
56 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
57 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
58 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
59 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
60 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
61};
62
63
64#define WRITE_UTF8(zOut, c) { \
65 if( c<0x00080 ){ \
66 *zOut++ = (c&0xFF); \
67 } \
68 else if( c<0x00800 ){ \
69 *zOut++ = 0xC0 + ((c>>6)&0x1F); \
70 *zOut++ = 0x80 + (c & 0x3F); \
71 } \
72 else if( c<0x10000 ){ \
73 *zOut++ = 0xE0 + ((c>>12)&0x0F); \
74 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
75 *zOut++ = 0x80 + (c & 0x3F); \
76 }else{ \
77 *zOut++ = 0xF0 + ((c>>18) & 0x07); \
78 *zOut++ = 0x80 + ((c>>12) & 0x3F); \
79 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
80 *zOut++ = 0x80 + (c & 0x3F); \
81 } \
82}
83
84#define WRITE_UTF16LE(zOut, c) { \
85 if( c<=0xFFFF ){ \
86 *zOut++ = (c&0x00FF); \
87 *zOut++ = ((c>>8)&0x00FF); \
88 }else{ \
89 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
90 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \
91 *zOut++ = (c&0x00FF); \
92 *zOut++ = (0x00DC + ((c>>8)&0x03)); \
93 } \
94}
95
96#define WRITE_UTF16BE(zOut, c) { \
97 if( c<=0xFFFF ){ \
98 *zOut++ = ((c>>8)&0x00FF); \
99 *zOut++ = (c&0x00FF); \
100 }else{ \
101 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \
102 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
103 *zOut++ = (0x00DC + ((c>>8)&0x03)); \
104 *zOut++ = (c&0x00FF); \
105 } \
106}
107
108#define READ_UTF16LE(zIn, c){ \
109 c = (*zIn++); \
110 c += ((*zIn++)<<8); \
111 if( c>=0xD800 && c<0xE000 ){ \
112 int c2 = (*zIn++); \
113 c2 += ((*zIn++)<<8); \
114 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
115 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
116 } \
117}
118
119#define READ_UTF16BE(zIn, c){ \
120 c = ((*zIn++)<<8); \
121 c += (*zIn++); \
122 if( c>=0xD800 && c<0xE000 ){ \
123 int c2 = ((*zIn++)<<8); \
124 c2 += (*zIn++); \
125 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
126 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
127 } \
128}
129
130/*
131** Translate a single UTF-8 character. Return the unicode value.
132**
133** During translation, assume that the byte that zTerm points
134** is a 0x00.
135**
136** Write a pointer to the next unread byte back into *pzNext.
137**
138** Notes On Invalid UTF-8:
139**
140** * This routine never allows a 7-bit character (0x00 through 0x7f) to
141** be encoded as a multi-byte character. Any multi-byte character that
142** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
143**
144** * This routine never allows a UTF16 surrogate value to be encoded.
145** If a multi-byte character attempts to encode a value between
146** 0xd800 and 0xe000 then it is rendered as 0xfffd.
147**
148** * Bytes in the range of 0x80 through 0xbf which occur as the first
149** byte of a character are interpreted as single-byte characters
150** and rendered as themselves even though they are technically
151** invalid characters.
152**
153** * This routine accepts an infinite number of different UTF8 encodings
154** for unicode values 0x80 and greater. It do not change over-length
155** encodings to 0xfffd as some systems recommend.
156*/
157int sqlite3Utf8Read(
158 const unsigned char *z, /* First byte of UTF-8 character */
159 const unsigned char *zTerm, /* Pretend this byte is 0x00 */
160 const unsigned char **pzNext /* Write first byte past UTF-8 char here */
161){
162 int c = *(z++);
163 if( c>=0xc0 ){
164 c = sqlite3UtfTrans1[c-0xc0];
165 while( z!=zTerm && (*z & 0xc0)==0x80 ){
166 c = (c<<6) + (0x3f & *(z++));
167 }
168 if( c<0x80
169 || (c&0xFFFFF800)==0xD800
170 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
171 }
172 *pzNext = z;
173 return c;
174}
175
176
177
178/*
179** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
180** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
181*/
182/* #define TRANSLATE_TRACE 1 */
183
184#ifndef SQLITE_OMIT_UTF16
185/*
186** This routine transforms the internal text encoding used by pMem to
187** desiredEnc. It is an error if the string is already of the desired
188** encoding, or if *pMem does not contain a string value.
189*/
190int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
191 unsigned char zShort[NBFS]; /* Temporary short output buffer */
192 int len; /* Maximum length of output string in bytes */
193 unsigned char *zOut; /* Output buffer */
194 unsigned char *zIn; /* Input iterator */
195 unsigned char *zTerm; /* End of input */
196 unsigned char *z; /* Output iterator */
197 unsigned int c;
198
199 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
200 assert( pMem->flags&MEM_Str );
201 assert( pMem->enc!=desiredEnc );
202 assert( pMem->enc!=0 );
203 assert( pMem->n>=0 );
204
205#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
206 {
207 char zBuf[100];
208 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
209 fprintf(stderr, "INPUT: %s\n", zBuf);
210 }
211#endif
212
213 /* If the translation is between UTF-16 little and big endian, then
214 ** all that is required is to swap the byte order. This case is handled
215 ** differently from the others.
216 */
217 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
218 u8 temp;
219 int rc;
220 rc = sqlite3VdbeMemMakeWriteable(pMem);
221 if( rc!=SQLITE_OK ){
222 assert( rc==SQLITE_NOMEM );
223 return SQLITE_NOMEM;
224 }
225 zIn = (u8*)pMem->z;
226 zTerm = &zIn[pMem->n];
227 while( zIn<zTerm ){
228 temp = *zIn;
229 *zIn = *(zIn+1);
230 zIn++;
231 *zIn++ = temp;
232 }
233 pMem->enc = desiredEnc;
234 goto translate_out;
235 }
236
237 /* Set len to the maximum number of bytes required in the output buffer. */
238 if( desiredEnc==SQLITE_UTF8 ){
239 /* When converting from UTF-16, the maximum growth results from
240 ** translating a 2-byte character to a 4-byte UTF-8 character.
241 ** A single byte is required for the output string
242 ** nul-terminator.
243 */
244 len = pMem->n * 2 + 1;
245 }else{
246 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
247 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
248 ** character. Two bytes are required in the output buffer for the
249 ** nul-terminator.
250 */
251 len = pMem->n * 2 + 2;
252 }
253
254 /* Set zIn to point at the start of the input buffer and zTerm to point 1
255 ** byte past the end.
256 **
257 ** Variable zOut is set to point at the output buffer. This may be space
258 ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and
259 ** not in use, or the zShort array on the stack (see above).
260 */
261 zIn = (u8*)pMem->z;
262 zTerm = &zIn[pMem->n];
263 if( len>NBFS ){
264 zOut = sqlite3DbMallocRaw(pMem->db, len);
265 if( !zOut ){
266 return SQLITE_NOMEM;
267 }
268 }else{
269 zOut = zShort;
270 }
271 z = zOut;
272
273 if( pMem->enc==SQLITE_UTF8 ){
274 if( desiredEnc==SQLITE_UTF16LE ){
275 /* UTF-8 -> UTF-16 Little-endian */
276 while( zIn<zTerm ){
277 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
278 WRITE_UTF16LE(z, c);
279 }
280 }else{
281 assert( desiredEnc==SQLITE_UTF16BE );
282 /* UTF-8 -> UTF-16 Big-endian */
283 while( zIn<zTerm ){
284 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
285 WRITE_UTF16BE(z, c);
286 }
287 }
288 pMem->n = z - zOut;
289 *z++ = 0;
290 }else{
291 assert( desiredEnc==SQLITE_UTF8 );
292 if( pMem->enc==SQLITE_UTF16LE ){
293 /* UTF-16 Little-endian -> UTF-8 */
294 while( zIn<zTerm ){
295 READ_UTF16LE(zIn, c);
296 WRITE_UTF8(z, c);
297 }
298 }else{
299 /* UTF-16 Little-endian -> UTF-8 */
300 while( zIn<zTerm ){
301 READ_UTF16BE(zIn, c);
302 WRITE_UTF8(z, c);
303 }
304 }
305 pMem->n = z - zOut;
306 }
307 *z = 0;
308 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
309
310 sqlite3VdbeMemRelease(pMem);
311 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
312 pMem->enc = desiredEnc;
313 if( zOut==zShort ){
314 memcpy(pMem->zShort, zOut, len);
315 zOut = (u8*)pMem->zShort;
316 pMem->flags |= (MEM_Term|MEM_Short);
317 }else{
318 pMem->flags |= (MEM_Term|MEM_Dyn);
319 }
320 pMem->z = (char*)zOut;
321
322translate_out:
323#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
324 {
325 char zBuf[100];
326 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
327 fprintf(stderr, "OUTPUT: %s\n", zBuf);
328 }
329#endif
330 return SQLITE_OK;
331}
332
333/*
334** This routine checks for a byte-order mark at the beginning of the
335** UTF-16 string stored in *pMem. If one is present, it is removed and
336** the encoding of the Mem adjusted. This routine does not do any
337** byte-swapping, it just sets Mem.enc appropriately.
338**
339** The allocation (static, dynamic etc.) and encoding of the Mem may be
340** changed by this function.
341*/
342int sqlite3VdbeMemHandleBom(Mem *pMem){
343 int rc = SQLITE_OK;
344 u8 bom = 0;
345
346 if( pMem->n<0 || pMem->n>1 ){
347 u8 b1 = *(u8 *)pMem->z;
348 u8 b2 = *(((u8 *)pMem->z) + 1);
349 if( b1==0xFE && b2==0xFF ){
350 bom = SQLITE_UTF16BE;
351 }
352 if( b1==0xFF && b2==0xFE ){
353 bom = SQLITE_UTF16LE;
354 }
355 }
356
357 if( bom ){
358 /* This function is called as soon as a string is stored in a Mem*,
359 ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
360 ** for the string to be stored in Mem.zShort, or for it to be stored
361 ** in dynamic memory with no destructor.
362 */
363 assert( !(pMem->flags&MEM_Short) );
364 assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
365 if( pMem->flags & MEM_Dyn ){
366 void (*xDel)(void*) = pMem->xDel;
367 char *z = pMem->z;
368 pMem->z = 0;
369 pMem->xDel = 0;
370 rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom,
371 SQLITE_TRANSIENT);
372 xDel(z);
373 }else{
374 rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom,
375 SQLITE_TRANSIENT);
376 }
377 }
378 return rc;
379}
380#endif /* SQLITE_OMIT_UTF16 */
381
382/*
383** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
384** return the number of unicode characters in pZ up to (but not including)
385** the first 0x00 byte. If nByte is not less than zero, return the
386** number of unicode characters in the first nByte of pZ (or up to
387** the first 0x00, whichever comes first).
388*/
389int sqlite3Utf8CharLen(const char *zIn, int nByte){
390 int r = 0;
391 const u8 *z = (const u8*)zIn;
392 const u8 *zTerm;
393 if( nByte>=0 ){
394 zTerm = &z[nByte];
395 }else{
396 zTerm = (const u8*)(-1);
397 }
398 assert( z<=zTerm );
399 while( *z!=0 && z<zTerm ){
400 SQLITE_SKIP_UTF8(z);
401 r++;
402 }
403 return r;
404}
405
406/* This test function is not currently used by the automated test-suite.
407** Hence it is only available in debug builds.
408*/
409#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
410/*
411** Translate UTF-8 to UTF-8.
412**
413** This has the effect of making sure that the string is well-formed
414** UTF-8. Miscoded characters are removed.
415**
416** The translation is done in-place (since it is impossible for the
417** correct UTF-8 encoding to be longer than a malformed encoding).
418*/
419int sqlite3Utf8To8(unsigned char *zIn){
420 unsigned char *zOut = zIn;
421 unsigned char *zStart = zIn;
422 unsigned char *zTerm;
423 u32 c;
424
425 while( zIn[0] ){
426 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
427 if( c!=0xfffd ){
428 WRITE_UTF8(zOut, c);
429 }
430 }
431 *zOut = 0;
432 return zOut - zStart;
433}
434#endif
435
436#ifndef SQLITE_OMIT_UTF16
437/*
438** Convert a UTF-16 string in the native encoding into a UTF-8 string.
439** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
440** be freed by the calling function.
441**
442** NULL is returned if there is an allocation error.
443*/
444char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
445 Mem m;
446 memset(&m, 0, sizeof(m));
447 m.db = db;
448 sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
449 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
450 if( db->mallocFailed ){
451 sqlite3VdbeMemRelease(&m);
452 m.z = 0;
453 }
454 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
455 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
456 return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
457}
458
459/*
460** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
461** return the number of bytes up to (but not including), the first pair
462** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
463** then return the number of bytes in the first nChar unicode characters
464** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
465*/
466int sqlite3Utf16ByteLen(const void *zIn, int nChar){
467 unsigned int c = 1;
468 char const *z = zIn;
469 int n = 0;
470 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
471 /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
472 ** and in other parts of this file means that at one branch will
473 ** not be covered by coverage testing on any single host. But coverage
474 ** will be complete if the tests are run on both a little-endian and
475 ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
476 ** macros are constant at compile time the compiler can determine
477 ** which branch will be followed. It is therefore assumed that no runtime
478 ** penalty is paid for this "if" statement.
479 */
480 while( c && ((nChar<0) || n<nChar) ){
481 READ_UTF16BE(z, c);
482 n++;
483 }
484 }else{
485 while( c && ((nChar<0) || n<nChar) ){
486 READ_UTF16LE(z, c);
487 n++;
488 }
489 }
490 return (z-(char const *)zIn)-((c==0)?2:0);
491}
492
493#if defined(SQLITE_TEST)
494/*
495** This routine is called from the TCL test function "translate_selftest".
496** It checks that the primitives for serializing and deserializing
497** characters in each encoding are inverses of each other.
498*/
499void sqlite3UtfSelfTest(){
500 unsigned int i, t;
501 unsigned char zBuf[20];
502 unsigned char *z;
503 unsigned char *zTerm;
504 int n;
505 unsigned int c;
506
507 for(i=0; i<0x00110000; i++){
508 z = zBuf;
509 WRITE_UTF8(z, i);
510 n = z-zBuf;
511 z[0] = 0;
512 zTerm = z;
513 z = zBuf;
514 c = sqlite3Utf8Read(z, zTerm, (const u8**)&z);
515 t = i;
516 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
517 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
518 assert( c==t );
519 assert( (z-zBuf)==n );
520 }
521 for(i=0; i<0x00110000; i++){
522 if( i>=0xD800 && i<0xE000 ) continue;
523 z = zBuf;
524 WRITE_UTF16LE(z, i);
525 n = z-zBuf;
526 z[0] = 0;
527 z = zBuf;
528 READ_UTF16LE(z, c);
529 assert( c==i );
530 assert( (z-zBuf)==n );
531 }
532 for(i=0; i<0x00110000; i++){
533 if( i>=0xD800 && i<0xE000 ) continue;
534 z = zBuf;
535 WRITE_UTF16BE(z, i);
536 n = z-zBuf;
537 z[0] = 0;
538 z = zBuf;
539 READ_UTF16BE(z, c);
540 assert( c==i );
541 assert( (z-zBuf)==n );
542 }
543}
544#endif /* SQLITE_TEST */
545#endif /* SQLITE_OMIT_UTF16 */
diff --git a/libraries/sqlite/win32/util.c b/libraries/sqlite/win32/util.c
new file mode 100755
index 0000000..722df63
--- /dev/null
+++ b/libraries/sqlite/win32/util.c
@@ -0,0 +1,706 @@
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.
10**
11*************************************************************************
12** Utility functions used throughout sqlite.
13**
14** This file contains functions for allocating memory, comparing
15** strings, and stuff like that.
16**
17** $Id: util.c,v 1.212 2007/09/01 10:01:13 danielk1977 Exp $
18*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
23
24/*
25** Set the most recent error code and error string for the sqlite
26** handle "db". The error code is set to "err_code".
27**
28** If it is not NULL, string zFormat specifies the format of the
29** error string in the style of the printf functions: The following
30** format characters are allowed:
31**
32** %s Insert a string
33** %z A string that should be freed after use
34** %d Insert an integer
35** %T Insert a token
36** %S Insert the first element of a SrcList
37**
38** zFormat and any string tokens that follow it are assumed to be
39** encoded in UTF-8.
40**
41** To clear the most recent error for sqlite handle "db", sqlite3Error
42** should be called with err_code set to SQLITE_OK and zFormat set
43** to NULL.
44*/
45void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
46 if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
47 db->errCode = err_code;
48 if( zFormat ){
49 char *z;
50 va_list ap;
51 va_start(ap, zFormat);
52 z = sqlite3VMPrintf(db, zFormat, ap);
53 va_end(ap);
54 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free);
55 }else{
56 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
57 }
58 }
59}
60
61/*
62** Add an error message to pParse->zErrMsg and increment pParse->nErr.
63** The following formatting characters are allowed:
64**
65** %s Insert a string
66** %z A string that should be freed after use
67** %d Insert an integer
68** %T Insert a token
69** %S Insert the first element of a SrcList
70**
71** This function should be used to report any error that occurs whilst
72** compiling an SQL statement (i.e. within sqlite3_prepare()). The
73** last thing the sqlite3_prepare() function does is copy the error
74** stored by this function into the database handle using sqlite3Error().
75** Function sqlite3Error() should be used during statement execution
76** (sqlite3_step() etc.).
77*/
78void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
79 va_list ap;
80 pParse->nErr++;
81 sqlite3_free(pParse->zErrMsg);
82 va_start(ap, zFormat);
83 pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap);
84 va_end(ap);
85 if( pParse->rc==SQLITE_OK ){
86 pParse->rc = SQLITE_ERROR;
87 }
88}
89
90/*
91** Clear the error message in pParse, if any
92*/
93void sqlite3ErrorClear(Parse *pParse){
94 sqlite3_free(pParse->zErrMsg);
95 pParse->zErrMsg = 0;
96 pParse->nErr = 0;
97}
98
99/*
100** Convert an SQL-style quoted string into a normal string by removing
101** the quote characters. The conversion is done in-place. If the
102** input does not begin with a quote character, then this routine
103** is a no-op.
104**
105** 2002-Feb-14: This routine is extended to remove MS-Access style
106** brackets from around identifers. For example: "[a-b-c]" becomes
107** "a-b-c".
108*/
109void sqlite3Dequote(char *z){
110 int quote;
111 int i, j;
112 if( z==0 ) return;
113 quote = z[0];
114 switch( quote ){
115 case '\'': break;
116 case '"': break;
117 case '`': break; /* For MySQL compatibility */
118 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
119 default: return;
120 }
121 for(i=1, j=0; z[i]; i++){
122 if( z[i]==quote ){
123 if( z[i+1]==quote ){
124 z[j++] = quote;
125 i++;
126 }else{
127 z[j++] = 0;
128 break;
129 }
130 }else{
131 z[j++] = z[i];
132 }
133 }
134}
135
136/* An array to map all upper-case characters into their corresponding
137** lower-case character.
138*/
139const unsigned char sqlite3UpperToLower[] = {
140#ifdef SQLITE_ASCII
141 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
142 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
143 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
144 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
145 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
146 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
147 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
148 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
149 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
150 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
151 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
152 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
153 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
154 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
155 252,253,254,255
156#endif
157#ifdef SQLITE_EBCDIC
158 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
159 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
160 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
161 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
162 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
163 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
164 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
165 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
166 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
167 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
168 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
169 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
170 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
171 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
172 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
173 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
174#endif
175};
176#define UpperToLower sqlite3UpperToLower
177
178/*
179** Some systems have stricmp(). Others have strcasecmp(). Because
180** there is no consistency, we will define our own.
181*/
182int sqlite3StrICmp(const char *zLeft, const char *zRight){
183 register unsigned char *a, *b;
184 a = (unsigned char *)zLeft;
185 b = (unsigned char *)zRight;
186 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
187 return UpperToLower[*a] - UpperToLower[*b];
188}
189int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
190 register unsigned char *a, *b;
191 a = (unsigned char *)zLeft;
192 b = (unsigned char *)zRight;
193 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
194 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
195}
196
197/*
198** Return TRUE if z is a pure numeric string. Return FALSE if the
199** string contains any character which is not part of a number. If
200** the string is numeric and contains the '.' character, set *realnum
201** to TRUE (otherwise FALSE).
202**
203** An empty string is considered non-numeric.
204*/
205int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
206 int incr = (enc==SQLITE_UTF8?1:2);
207 if( enc==SQLITE_UTF16BE ) z++;
208 if( *z=='-' || *z=='+' ) z += incr;
209 if( !isdigit(*(u8*)z) ){
210 return 0;
211 }
212 z += incr;
213 if( realnum ) *realnum = 0;
214 while( isdigit(*(u8*)z) ){ z += incr; }
215 if( *z=='.' ){
216 z += incr;
217 if( !isdigit(*(u8*)z) ) return 0;
218 while( isdigit(*(u8*)z) ){ z += incr; }
219 if( realnum ) *realnum = 1;
220 }
221 if( *z=='e' || *z=='E' ){
222 z += incr;
223 if( *z=='+' || *z=='-' ) z += incr;
224 if( !isdigit(*(u8*)z) ) return 0;
225 while( isdigit(*(u8*)z) ){ z += incr; }
226 if( realnum ) *realnum = 1;
227 }
228 return *z==0;
229}
230
231/*
232** The string z[] is an ascii representation of a real number.
233** Convert this string to a double.
234**
235** This routine assumes that z[] really is a valid number. If it
236** is not, the result is undefined.
237**
238** This routine is used instead of the library atof() function because
239** the library atof() might want to use "," as the decimal point instead
240** of "." depending on how locale is set. But that would cause problems
241** for SQL. So this routine always uses "." regardless of locale.
242*/
243int sqlite3AtoF(const char *z, double *pResult){
244#ifndef SQLITE_OMIT_FLOATING_POINT
245 int sign = 1;
246 const char *zBegin = z;
247 LONGDOUBLE_TYPE v1 = 0.0;
248 while( isspace(*(u8*)z) ) z++;
249 if( *z=='-' ){
250 sign = -1;
251 z++;
252 }else if( *z=='+' ){
253 z++;
254 }
255 while( isdigit(*(u8*)z) ){
256 v1 = v1*10.0 + (*z - '0');
257 z++;
258 }
259 if( *z=='.' ){
260 LONGDOUBLE_TYPE divisor = 1.0;
261 z++;
262 while( isdigit(*(u8*)z) ){
263 v1 = v1*10.0 + (*z - '0');
264 divisor *= 10.0;
265 z++;
266 }
267 v1 /= divisor;
268 }
269 if( *z=='e' || *z=='E' ){
270 int esign = 1;
271 int eval = 0;
272 LONGDOUBLE_TYPE scale = 1.0;
273 z++;
274 if( *z=='-' ){
275 esign = -1;
276 z++;
277 }else if( *z=='+' ){
278 z++;
279 }
280 while( isdigit(*(u8*)z) ){
281 eval = eval*10 + *z - '0';
282 z++;
283 }
284 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
285 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
286 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
287 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
288 if( esign<0 ){
289 v1 /= scale;
290 }else{
291 v1 *= scale;
292 }
293 }
294 *pResult = sign<0 ? -v1 : v1;
295 return z - zBegin;
296#else
297 return sqlite3Atoi64(z, pResult);
298#endif /* SQLITE_OMIT_FLOATING_POINT */
299}
300
301/*
302** Compare the 19-character string zNum against the text representation
303** value 2^63: 9223372036854775808. Return negative, zero, or positive
304** if zNum is less than, equal to, or greater than the string.
305**
306** Unlike memcmp() this routine is guaranteed to return the difference
307** in the values of the last digit if the only difference is in the
308** last digit. So, for example,
309**
310** compare2pow63("9223372036854775800")
311**
312** will return -8.
313*/
314static int compare2pow63(const char *zNum){
315 int c;
316 c = memcmp(zNum,"922337203685477580",18);
317 if( c==0 ){
318 c = zNum[18] - '8';
319 }
320 return c;
321}
322
323
324/*
325** Return TRUE if zNum is a 64-bit signed integer and write
326** the value of the integer into *pNum. If zNum is not an integer
327** or is an integer that is too large to be expressed with 64 bits,
328** then return false.
329**
330** When this routine was originally written it dealt with only
331** 32-bit numbers. At that time, it was much faster than the
332** atoi() library routine in RedHat 7.2.
333*/
334int sqlite3Atoi64(const char *zNum, i64 *pNum){
335 i64 v = 0;
336 int neg;
337 int i, c;
338 while( isspace(*(u8*)zNum) ) zNum++;
339 if( *zNum=='-' ){
340 neg = 1;
341 zNum++;
342 }else if( *zNum=='+' ){
343 neg = 0;
344 zNum++;
345 }else{
346 neg = 0;
347 }
348 while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
349 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
350 v = v*10 + c - '0';
351 }
352 *pNum = neg ? -v : v;
353 if( c!=0 || i==0 || i>19 ){
354 /* zNum is empty or contains non-numeric text or is longer
355 ** than 19 digits (thus guaranting that it is too large) */
356 return 0;
357 }else if( i<19 ){
358 /* Less than 19 digits, so we know that it fits in 64 bits */
359 return 1;
360 }else{
361 /* 19-digit numbers must be no larger than 9223372036854775807 if positive
362 ** or 9223372036854775808 if negative. Note that 9223372036854665808
363 ** is 2^63. */
364 return compare2pow63(zNum)<neg;
365 }
366}
367
368/*
369** The string zNum represents an integer. There might be some other
370** information following the integer too, but that part is ignored.
371** If the integer that the prefix of zNum represents will fit in a
372** 64-bit signed integer, return TRUE. Otherwise return FALSE.
373**
374** This routine returns FALSE for the string -9223372036854775808 even that
375** that number will, in theory fit in a 64-bit integer. Positive
376** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
377** false.
378*/
379int sqlite3FitsIn64Bits(const char *zNum){
380 int i, c;
381 int neg = 0;
382 if( *zNum=='-' ){
383 neg = 1;
384 zNum++;
385 }else if( *zNum=='+' ){
386 zNum++;
387 }
388 while( *zNum=='0' ){
389 zNum++; /* Skip leading zeros. Ticket #2454 */
390 }
391 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
392 if( i<19 ){
393 /* Guaranteed to fit if less than 19 digits */
394 return 1;
395 }else if( i>19 ){
396 /* Guaranteed to be too big if greater than 19 digits */
397 return 0;
398 }else{
399 /* Compare against 2^63. */
400 return compare2pow63(zNum)<neg;
401 }
402}
403
404/*
405** If zNum represents an integer that will fit in 32-bits, then set
406** *pValue to that integer and return true. Otherwise return false.
407**
408** Any non-numeric characters that following zNum are ignored.
409** This is different from sqlite3Atoi64() which requires the
410** input number to be zero-terminated.
411*/
412int sqlite3GetInt32(const char *zNum, int *pValue){
413 sqlite_int64 v = 0;
414 int i, c;
415 int neg = 0;
416 if( zNum[0]=='-' ){
417 neg = 1;
418 zNum++;
419 }else if( zNum[0]=='+' ){
420 zNum++;
421 }
422 while( zNum[0]=='0' ) zNum++;
423 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
424 v = v*10 + c;
425 }
426
427 /* The longest decimal representation of a 32 bit integer is 10 digits:
428 **
429 ** 1234567890
430 ** 2^31 -> 2147483648
431 */
432 if( i>10 ){
433 return 0;
434 }
435 if( v-neg>2147483647 ){
436 return 0;
437 }
438 if( neg ){
439 v = -v;
440 }
441 *pValue = (int)v;
442 return 1;
443}
444
445/*
446** Check to make sure we have a valid db pointer. This test is not
447** foolproof but it does provide some measure of protection against
448** misuse of the interface such as passing in db pointers that are
449** NULL or which have been previously closed. If this routine returns
450** TRUE it means that the db pointer is invalid and should not be
451** dereferenced for any reason. The calling function should invoke
452** SQLITE_MISUSE immediately.
453*/
454int sqlite3SafetyCheck(sqlite3 *db){
455 int magic;
456 if( db==0 ) return 1;
457 magic = db->magic;
458 if( magic!=SQLITE_MAGIC_CLOSED &&
459 magic!=SQLITE_MAGIC_OPEN &&
460 magic!=SQLITE_MAGIC_BUSY ) return 1;
461 return 0;
462}
463
464/*
465** The variable-length integer encoding is as follows:
466**
467** KEY:
468** A = 0xxxxxxx 7 bits of data and one flag bit
469** B = 1xxxxxxx 7 bits of data and one flag bit
470** C = xxxxxxxx 8 bits of data
471**
472** 7 bits - A
473** 14 bits - BA
474** 21 bits - BBA
475** 28 bits - BBBA
476** 35 bits - BBBBA
477** 42 bits - BBBBBA
478** 49 bits - BBBBBBA
479** 56 bits - BBBBBBBA
480** 64 bits - BBBBBBBBC
481*/
482
483/*
484** Write a 64-bit variable-length integer to memory starting at p[0].
485** The length of data write will be between 1 and 9 bytes. The number
486** of bytes written is returned.
487**
488** A variable-length integer consists of the lower 7 bits of each byte
489** for all bytes that have the 8th bit set and one byte with the 8th
490** bit clear. Except, if we get to the 9th byte, it stores the full
491** 8 bits and is the last byte.
492*/
493int sqlite3PutVarint(unsigned char *p, u64 v){
494 int i, j, n;
495 u8 buf[10];
496 if( v & (((u64)0xff000000)<<32) ){
497 p[8] = v;
498 v >>= 8;
499 for(i=7; i>=0; i--){
500 p[i] = (v & 0x7f) | 0x80;
501 v >>= 7;
502 }
503 return 9;
504 }
505 n = 0;
506 do{
507 buf[n++] = (v & 0x7f) | 0x80;
508 v >>= 7;
509 }while( v!=0 );
510 buf[0] &= 0x7f;
511 assert( n<=9 );
512 for(i=0, j=n-1; j>=0; j--, i++){
513 p[i] = buf[j];
514 }
515 return n;
516}
517
518/*
519** Read a 64-bit variable-length integer from memory starting at p[0].
520** Return the number of bytes read. The value is stored in *v.
521*/
522int sqlite3GetVarint(const unsigned char *p, u64 *v){
523 u32 x;
524 u64 x64;
525 int n;
526 unsigned char c;
527 if( ((c = p[0]) & 0x80)==0 ){
528 *v = c;
529 return 1;
530 }
531 x = c & 0x7f;
532 if( ((c = p[1]) & 0x80)==0 ){
533 *v = (x<<7) | c;
534 return 2;
535 }
536 x = (x<<7) | (c&0x7f);
537 if( ((c = p[2]) & 0x80)==0 ){
538 *v = (x<<7) | c;
539 return 3;
540 }
541 x = (x<<7) | (c&0x7f);
542 if( ((c = p[3]) & 0x80)==0 ){
543 *v = (x<<7) | c;
544 return 4;
545 }
546 x64 = (x<<7) | (c&0x7f);
547 n = 4;
548 do{
549 c = p[n++];
550 if( n==9 ){
551 x64 = (x64<<8) | c;
552 break;
553 }
554 x64 = (x64<<7) | (c&0x7f);
555 }while( (c & 0x80)!=0 );
556 *v = x64;
557 return n;
558}
559
560/*
561** Read a 32-bit variable-length integer from memory starting at p[0].
562** Return the number of bytes read. The value is stored in *v.
563*/
564int sqlite3GetVarint32(const unsigned char *p, u32 *v){
565 u32 x;
566 int n;
567 unsigned char c;
568 if( ((signed char*)p)[0]>=0 ){
569 *v = p[0];
570 return 1;
571 }
572 x = p[0] & 0x7f;
573 if( ((signed char*)p)[1]>=0 ){
574 *v = (x<<7) | p[1];
575 return 2;
576 }
577 x = (x<<7) | (p[1] & 0x7f);
578 n = 2;
579 do{
580 x = (x<<7) | ((c = p[n++])&0x7f);
581 }while( (c & 0x80)!=0 && n<9 );
582 *v = x;
583 return n;
584}
585
586/*
587** Return the number of bytes that will be needed to store the given
588** 64-bit integer.
589*/
590int sqlite3VarintLen(u64 v){
591 int i = 0;
592 do{
593 i++;
594 v >>= 7;
595 }while( v!=0 && i<9 );
596 return i;
597}
598
599
600/*
601** Read or write a four-byte big-endian integer value.
602*/
603u32 sqlite3Get4byte(const u8 *p){
604 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
605}
606void sqlite3Put4byte(unsigned char *p, u32 v){
607 p[0] = v>>24;
608 p[1] = v>>16;
609 p[2] = v>>8;
610 p[3] = v;
611}
612
613
614
615#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
616 || defined(SQLITE_TEST)
617/*
618** Translate a single byte of Hex into an integer.
619*/
620static int hexToInt(int h){
621 if( h>='0' && h<='9' ){
622 return h - '0';
623 }else if( h>='a' && h<='f' ){
624 return h - 'a' + 10;
625 }else{
626 assert( h>='A' && h<='F' );
627 return h - 'A' + 10;
628 }
629}
630#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
631
632#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
633/*
634** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
635** value. Return a pointer to its binary value. Space to hold the
636** binary value has been obtained from malloc and must be freed by
637** the calling routine.
638*/
639void *sqlite3HexToBlob(sqlite3 *db, const char *z){
640 char *zBlob;
641 int i;
642 int n = strlen(z);
643 if( n%2 ) return 0;
644
645 zBlob = (char *)sqlite3DbMallocRaw(db, n/2);
646 if( zBlob ){
647 for(i=0; i<n; i+=2){
648 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
649 }
650 }
651 return zBlob;
652}
653#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
654
655
656/*
657** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
658** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
659** when this routine is called.
660**
661** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
662** value indicates that the database connection passed into the API is
663** open and is not being used by another thread. By changing the value
664** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
665** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
666** when the API exits.
667**
668** This routine is a attempt to detect if two threads use the
669** same sqlite* pointer at the same time. There is a race
670** condition so it is possible that the error is not detected.
671** But usually the problem will be seen. The result will be an
672** error which can be used to debug the application that is
673** using SQLite incorrectly.
674**
675** Ticket #202: If db->magic is not a valid open value, take care not
676** to modify the db structure at all. It could be that db is a stale
677** pointer. In other words, it could be that there has been a prior
678** call to sqlite3_close(db) and db has been deallocated. And we do
679** not want to write into deallocated memory.
680*/
681int sqlite3SafetyOn(sqlite3 *db){
682 if( db->magic==SQLITE_MAGIC_OPEN ){
683 db->magic = SQLITE_MAGIC_BUSY;
684 return 0;
685 }else if( db->magic==SQLITE_MAGIC_BUSY ){
686 db->magic = SQLITE_MAGIC_ERROR;
687 db->u1.isInterrupted = 1;
688 }
689 return 1;
690}
691
692/*
693** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
694** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
695** when this routine is called.
696*/
697int sqlite3SafetyOff(sqlite3 *db){
698 if( db->magic==SQLITE_MAGIC_BUSY ){
699 db->magic = SQLITE_MAGIC_OPEN;
700 return 0;
701 }else {
702 db->magic = SQLITE_MAGIC_ERROR;
703 db->u1.isInterrupted = 1;
704 return 1;
705 }
706}
diff --git a/libraries/sqlite/win32/vacuum.c b/libraries/sqlite/win32/vacuum.c
new file mode 100755
index 0000000..73cccba
--- /dev/null
+++ b/libraries/sqlite/win32/vacuum.c
@@ -0,0 +1,262 @@
1/*
2** 2003 April 6
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.
10**
11*************************************************************************
12** This file contains code used to implement the VACUUM command.
13**
14** Most of the code in this file may be omitted by defining the
15** SQLITE_OMIT_VACUUM macro.
16**
17** $Id: vacuum.c,v 1.73 2007/08/29 12:31:28 danielk1977 Exp $
18*/
19#include "sqliteInt.h"
20#include "vdbeInt.h"
21
22#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
23/*
24** Execute zSql on database db. Return an error code.
25*/
26static int execSql(sqlite3 *db, const char *zSql){
27 sqlite3_stmt *pStmt;
28 if( !zSql ){
29 return SQLITE_NOMEM;
30 }
31 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
32 return sqlite3_errcode(db);
33 }
34 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
35 return sqlite3_finalize(pStmt);
36}
37
38/*
39** Execute zSql on database db. The statement returns exactly
40** one column. Execute this as SQL on the same database.
41*/
42static int execExecSql(sqlite3 *db, const char *zSql){
43 sqlite3_stmt *pStmt;
44 int rc;
45
46 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
47 if( rc!=SQLITE_OK ) return rc;
48
49 while( SQLITE_ROW==sqlite3_step(pStmt) ){
50 rc = execSql(db, (char*)sqlite3_column_text(pStmt, 0));
51 if( rc!=SQLITE_OK ){
52 sqlite3_finalize(pStmt);
53 return rc;
54 }
55 }
56
57 return sqlite3_finalize(pStmt);
58}
59
60/*
61** The non-standard VACUUM command is used to clean up the database,
62** collapse free space, etc. It is modelled after the VACUUM command
63** in PostgreSQL.
64**
65** In version 1.0.x of SQLite, the VACUUM command would call
66** gdbm_reorganize() on all the database tables. But beginning
67** with 2.0.0, SQLite no longer uses GDBM so this command has
68** become a no-op.
69*/
70void sqlite3Vacuum(Parse *pParse){
71 Vdbe *v = sqlite3GetVdbe(pParse);
72 if( v ){
73 sqlite3VdbeAddOp(v, OP_Vacuum, 0, 0);
74 }
75 return;
76}
77
78/*
79** This routine implements the OP_Vacuum opcode of the VDBE.
80*/
81int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
82 int rc = SQLITE_OK; /* Return code from service routines */
83 Btree *pMain; /* The database being vacuumed */
84 Btree *pTemp; /* The temporary database we vacuum into */
85 char *zSql = 0; /* SQL statements */
86 int saved_flags; /* Saved value of the db->flags */
87 Db *pDb = 0; /* Database to detach at end of vacuum */
88
89 /* Save the current value of the write-schema flag before setting it. */
90 saved_flags = db->flags;
91 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
92
93 if( !db->autoCommit ){
94 sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction",
95 (char*)0);
96 rc = SQLITE_ERROR;
97 goto end_of_vacuum;
98 }
99 pMain = db->aDb[0].pBt;
100
101 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
102 ** can be set to 'off' for this file, as it is not recovered if a crash
103 ** occurs anyway. The integrity of the database is maintained by a
104 ** (possibly synchronous) transaction opened on the main database before
105 ** sqlite3BtreeCopyFile() is called.
106 **
107 ** An optimisation would be to use a non-journaled pager.
108 */
109 zSql = "ATTACH '' AS vacuum_db;";
110 rc = execSql(db, zSql);
111 if( rc!=SQLITE_OK ) goto end_of_vacuum;
112 pDb = &db->aDb[db->nDb-1];
113 assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
114 pTemp = db->aDb[db->nDb-1].pBt;
115 sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain),
116 sqlite3BtreeGetReserve(pMain));
117 if( db->mallocFailed ){
118 rc = SQLITE_NOMEM;
119 goto end_of_vacuum;
120 }
121 assert( sqlite3BtreeGetPageSize(pTemp)==sqlite3BtreeGetPageSize(pMain) );
122 rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
123 if( rc!=SQLITE_OK ){
124 goto end_of_vacuum;
125 }
126
127#ifndef SQLITE_OMIT_AUTOVACUUM
128 sqlite3BtreeSetAutoVacuum(pTemp, sqlite3BtreeGetAutoVacuum(pMain));
129#endif
130
131 /* Begin a transaction */
132 rc = execSql(db, "BEGIN EXCLUSIVE;");
133 if( rc!=SQLITE_OK ) goto end_of_vacuum;
134
135 /* Query the schema of the main database. Create a mirror schema
136 ** in the temporary database.
137 */
138 rc = execExecSql(db,
139 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14,100000000) "
140 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
141 " AND rootpage>0"
142 );
143 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144 rc = execExecSql(db,
145 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14,100000000)"
146 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
147 if( rc!=SQLITE_OK ) goto end_of_vacuum;
148 rc = execExecSql(db,
149 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21,100000000) "
150 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
151 if( rc!=SQLITE_OK ) goto end_of_vacuum;
152
153 /* Loop through the tables in the main database. For each, do
154 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM xxx;" to copy
155 ** the contents to the temporary database.
156 */
157 rc = execExecSql(db,
158 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
159 "|| ' SELECT * FROM ' || quote(name) || ';'"
160 "FROM sqlite_master "
161 "WHERE type = 'table' AND name!='sqlite_sequence' "
162 " AND rootpage>0"
163
164 );
165 if( rc!=SQLITE_OK ) goto end_of_vacuum;
166
167 /* Copy over the sequence table
168 */
169 rc = execExecSql(db,
170 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
171 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
172 );
173 if( rc!=SQLITE_OK ) goto end_of_vacuum;
174 rc = execExecSql(db,
175 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
176 "|| ' SELECT * FROM ' || quote(name) || ';' "
177 "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
178 );
179 if( rc!=SQLITE_OK ) goto end_of_vacuum;
180
181
182 /* Copy the triggers, views, and virtual tables from the main database
183 ** over to the temporary database. None of these objects has any
184 ** associated storage, so all we have to do is copy their entries
185 ** from the SQLITE_MASTER table.
186 */
187 rc = execSql(db,
188 "INSERT INTO vacuum_db.sqlite_master "
189 " SELECT type, name, tbl_name, rootpage, sql"
190 " FROM sqlite_master"
191 " WHERE type='view' OR type='trigger'"
192 " OR (type='table' AND rootpage=0)"
193 );
194 if( rc ) goto end_of_vacuum;
195
196 /* At this point, unless the main db was completely empty, there is now a
197 ** transaction open on the vacuum database, but not on the main database.
198 ** Open a btree level transaction on the main database. This allows a
199 ** call to sqlite3BtreeCopyFile(). The main database btree level
200 ** transaction is then committed, so the SQL level never knows it was
201 ** opened for writing. This way, the SQL transaction used to create the
202 ** temporary database never needs to be committed.
203 */
204 if( rc==SQLITE_OK ){
205 u32 meta;
206 int i;
207
208 /* This array determines which meta meta values are preserved in the
209 ** vacuum. Even entries are the meta value number and odd entries
210 ** are an increment to apply to the meta value after the vacuum.
211 ** The increment is used to increase the schema cookie so that other
212 ** connections to the same database will know to reread the schema.
213 */
214 static const unsigned char aCopy[] = {
215 1, 1, /* Add one to the old schema cookie */
216 3, 0, /* Preserve the default page cache size */
217 5, 0, /* Preserve the default text encoding */
218 6, 0, /* Preserve the user version */
219 };
220
221 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
222 assert( 1==sqlite3BtreeIsInTrans(pMain) );
223
224 /* Copy Btree meta values */
225 for(i=0; i<sizeof(aCopy)/sizeof(aCopy[0]); i+=2){
226 rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
227 if( rc!=SQLITE_OK ) goto end_of_vacuum;
228 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
229 if( rc!=SQLITE_OK ) goto end_of_vacuum;
230 }
231
232 rc = sqlite3BtreeCopyFile(pMain, pTemp);
233 if( rc!=SQLITE_OK ) goto end_of_vacuum;
234 rc = sqlite3BtreeCommit(pTemp);
235 if( rc!=SQLITE_OK ) goto end_of_vacuum;
236 rc = sqlite3BtreeCommit(pMain);
237 }
238
239end_of_vacuum:
240 /* Restore the original value of db->flags */
241 db->flags = saved_flags;
242
243 /* Currently there is an SQL level transaction open on the vacuum
244 ** database. No locks are held on any other files (since the main file
245 ** was committed at the btree level). So it safe to end the transaction
246 ** by manually setting the autoCommit flag to true and detaching the
247 ** vacuum database. The vacuum_db journal file is deleted when the pager
248 ** is closed by the DETACH.
249 */
250 db->autoCommit = 1;
251
252 if( pDb ){
253 sqlite3BtreeClose(pDb->pBt);
254 pDb->pBt = 0;
255 pDb->pSchema = 0;
256 }
257
258 sqlite3ResetInternalSchema(db, 0);
259
260 return rc;
261}
262#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
diff --git a/libraries/sqlite/win32/vdbe.c b/libraries/sqlite/win32/vdbe.c
new file mode 100755
index 0000000..654c17f
--- /dev/null
+++ b/libraries/sqlite/win32/vdbe.c
@@ -0,0 +1,5279 @@
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.
10**
11*************************************************************************
12** The code in this file implements execution method of the
13** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
14** handles housekeeping details such as creating and deleting
15** VDBE instances. This file is solely interested in executing
16** the VDBE program.
17**
18** In the external interface, an "sqlite3_stmt*" is an opaque pointer
19** to a VDBE.
20**
21** The SQL parser generates a program which is then executed by
22** the VDBE to do the work of the SQL statement. VDBE programs are
23** similar in form to assembly language. The program consists of
24** a linear sequence of operations. Each operation has an opcode
25** and 3 operands. Operands P1 and P2 are integers. Operand P3
26** is a null-terminated string. The P2 operand must be non-negative.
27** Opcodes will typically ignore one or more operands. Many opcodes
28** ignore all three operands.
29**
30** Computation results are stored on a stack. Each entry on the
31** stack is either an integer, a null-terminated string, a floating point
32** number, or the SQL "NULL" value. An inplicit conversion from one
33** type to the other occurs as necessary.
34**
35** Most of the code in this file is taken up by the sqlite3VdbeExec()
36** function which does the work of interpreting a VDBE program.
37** But other routines are also provided to help in building up
38** a program instruction by instruction.
39**
40** Various scripts scan this source file in order to generate HTML
41** documentation, headers files, or other derived files. The formatting
42** of the code in this file is, therefore, important. See other comments
43** in this file for details. If in doubt, do not deviate from existing
44** commenting and indentation practices when changing or adding code.
45**
46** $Id: vdbe.c,v 1.650 2007/09/03 15:19:36 drh Exp $
47*/
48#include "sqliteInt.h"
49#include <ctype.h>
50#include <math.h>
51#include "vdbeInt.h"
52
53/*
54** The following global variable is incremented every time a cursor
55** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
56** procedures use this information to make sure that indices are
57** working correctly. This variable has no function other than to
58** help verify the correct operation of the library.
59*/
60#ifdef SQLITE_TEST
61int sqlite3_search_count = 0;
62#endif
63
64/*
65** When this global variable is positive, it gets decremented once before
66** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
67** field of the sqlite3 structure is set in order to simulate and interrupt.
68**
69** This facility is used for testing purposes only. It does not function
70** in an ordinary build.
71*/
72#ifdef SQLITE_TEST
73int sqlite3_interrupt_count = 0;
74#endif
75
76/*
77** The next global variable is incremented each type the OP_Sort opcode
78** is executed. The test procedures use this information to make sure that
79** sorting is occurring or not occuring at appropriate times. This variable
80** has no function other than to help verify the correct operation of the
81** library.
82*/
83#ifdef SQLITE_TEST
84int sqlite3_sort_count = 0;
85#endif
86
87/*
88** The next global variable records the size of the largest MEM_Blob
89** or MEM_Str that has appeared on the VDBE stack. The test procedures
90** use this information to make sure that the zero-blob functionality
91** is working correctly. This variable has no function other than to
92** help verify the correct operation of the library.
93*/
94#ifdef SQLITE_TEST
95int sqlite3_max_blobsize = 0;
96#endif
97
98/*
99** Release the memory associated with the given stack level. This
100** leaves the Mem.flags field in an inconsistent state.
101*/
102#define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); }
103
104/*
105** Convert the given stack entity into a string if it isn't one
106** already. Return non-zero if a malloc() fails.
107*/
108#define Stringify(P, enc) \
109 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
110 { goto no_mem; }
111
112/*
113** The header of a record consists of a sequence variable-length integers.
114** These integers are almost always small and are encoded as a single byte.
115** The following macro takes advantage this fact to provide a fast decode
116** of the integers in a record header. It is faster for the common case
117** where the integer is a single byte. It is a little slower when the
118** integer is two or more bytes. But overall it is faster.
119**
120** The following expressions are equivalent:
121**
122** x = sqlite3GetVarint32( A, &B );
123**
124** x = GetVarint( A, B );
125**
126*/
127#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
128
129/*
130** An ephemeral string value (signified by the MEM_Ephem flag) contains
131** a pointer to a dynamically allocated string where some other entity
132** is responsible for deallocating that string. Because the stack entry
133** does not control the string, it might be deleted without the stack
134** entry knowing it.
135**
136** This routine converts an ephemeral string into a dynamically allocated
137** string that the stack entry itself controls. In other words, it
138** converts an MEM_Ephem string into an MEM_Dyn string.
139*/
140#define Deephemeralize(P) \
141 if( ((P)->flags&MEM_Ephem)!=0 \
142 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
143
144/*
145** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
146** P if required.
147*/
148#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
149
150/*
151** Argument pMem points at a memory cell that will be passed to a
152** user-defined function or returned to the user as the result of a query.
153** The second argument, 'db_enc' is the text encoding used by the vdbe for
154** stack variables. This routine sets the pMem->enc and pMem->type
155** variables used by the sqlite3_value_*() routines.
156*/
157#define storeTypeInfo(A,B) _storeTypeInfo(A)
158static void _storeTypeInfo(Mem *pMem){
159 int flags = pMem->flags;
160 if( flags & MEM_Null ){
161 pMem->type = SQLITE_NULL;
162 }
163 else if( flags & MEM_Int ){
164 pMem->type = SQLITE_INTEGER;
165 }
166 else if( flags & MEM_Real ){
167 pMem->type = SQLITE_FLOAT;
168 }
169 else if( flags & MEM_Str ){
170 pMem->type = SQLITE_TEXT;
171 }else{
172 pMem->type = SQLITE_BLOB;
173 }
174}
175
176/*
177** Pop the stack N times.
178*/
179static void popStack(Mem **ppTos, int N){
180 Mem *pTos = *ppTos;
181 while( N>0 ){
182 N--;
183 Release(pTos);
184 pTos--;
185 }
186 *ppTos = pTos;
187}
188
189/*
190** Allocate cursor number iCur. Return a pointer to it. Return NULL
191** if we run out of memory.
192*/
193static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){
194 Cursor *pCx;
195 assert( iCur<p->nCursor );
196 if( p->apCsr[iCur] ){
197 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
198 }
199 p->apCsr[iCur] = pCx = sqlite3MallocZero( sizeof(Cursor) );
200 if( pCx ){
201 pCx->iDb = iDb;
202 }
203 return pCx;
204}
205
206/*
207** Try to convert a value into a numeric representation if we can
208** do so without loss of information. In other words, if the string
209** looks like a number, convert it into a number. If it does not
210** look like a number, leave it alone.
211*/
212static void applyNumericAffinity(Mem *pRec){
213 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
214 int realnum;
215 sqlite3VdbeMemNulTerminate(pRec);
216 if( (pRec->flags&MEM_Str)
217 && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
218 i64 value;
219 sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
220 if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
221 sqlite3VdbeMemRelease(pRec);
222 pRec->u.i = value;
223 pRec->flags = MEM_Int;
224 }else{
225 sqlite3VdbeMemRealify(pRec);
226 }
227 }
228 }
229}
230
231/*
232** Processing is determine by the affinity parameter:
233**
234** SQLITE_AFF_INTEGER:
235** SQLITE_AFF_REAL:
236** SQLITE_AFF_NUMERIC:
237** Try to convert pRec to an integer representation or a
238** floating-point representation if an integer representation
239** is not possible. Note that the integer representation is
240** always preferred, even if the affinity is REAL, because
241** an integer representation is more space efficient on disk.
242**
243** SQLITE_AFF_TEXT:
244** Convert pRec to a text representation.
245**
246** SQLITE_AFF_NONE:
247** No-op. pRec is unchanged.
248*/
249static void applyAffinity(
250 Mem *pRec, /* The value to apply affinity to */
251 char affinity, /* The affinity to be applied */
252 u8 enc /* Use this text encoding */
253){
254 if( affinity==SQLITE_AFF_TEXT ){
255 /* Only attempt the conversion to TEXT if there is an integer or real
256 ** representation (blob and NULL do not get converted) but no string
257 ** representation.
258 */
259 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
260 sqlite3VdbeMemStringify(pRec, enc);
261 }
262 pRec->flags &= ~(MEM_Real|MEM_Int);
263 }else if( affinity!=SQLITE_AFF_NONE ){
264 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
265 || affinity==SQLITE_AFF_NUMERIC );
266 applyNumericAffinity(pRec);
267 if( pRec->flags & MEM_Real ){
268 sqlite3VdbeIntegerAffinity(pRec);
269 }
270 }
271}
272
273/*
274** Try to convert the type of a function argument or a result column
275** into a numeric representation. Use either INTEGER or REAL whichever
276** is appropriate. But only do the conversion if it is possible without
277** loss of information and return the revised type of the argument.
278**
279** This is an EXPERIMENTAL api and is subject to change or removal.
280*/
281int sqlite3_value_numeric_type(sqlite3_value *pVal){
282 Mem *pMem = (Mem*)pVal;
283 applyNumericAffinity(pMem);
284 storeTypeInfo(pMem, 0);
285 return pMem->type;
286}
287
288/*
289** Exported version of applyAffinity(). This one works on sqlite3_value*,
290** not the internal Mem* type.
291*/
292void sqlite3ValueApplyAffinity(
293 sqlite3_value *pVal,
294 u8 affinity,
295 u8 enc
296){
297 applyAffinity((Mem *)pVal, affinity, enc);
298}
299
300#ifdef SQLITE_DEBUG
301/*
302** Write a nice string representation of the contents of cell pMem
303** into buffer zBuf, length nBuf.
304*/
305void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
306 char *zCsr = zBuf;
307 int f = pMem->flags;
308
309 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
310
311 if( f&MEM_Blob ){
312 int i;
313 char c;
314 if( f & MEM_Dyn ){
315 c = 'z';
316 assert( (f & (MEM_Static|MEM_Ephem))==0 );
317 }else if( f & MEM_Static ){
318 c = 't';
319 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
320 }else if( f & MEM_Ephem ){
321 c = 'e';
322 assert( (f & (MEM_Static|MEM_Dyn))==0 );
323 }else{
324 c = 's';
325 }
326
327 sqlite3_snprintf(100, zCsr, "%c", c);
328 zCsr += strlen(zCsr);
329 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
330 zCsr += strlen(zCsr);
331 for(i=0; i<16 && i<pMem->n; i++){
332 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
333 zCsr += strlen(zCsr);
334 }
335 for(i=0; i<16 && i<pMem->n; i++){
336 char z = pMem->z[i];
337 if( z<32 || z>126 ) *zCsr++ = '.';
338 else *zCsr++ = z;
339 }
340
341 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
342 zCsr += strlen(zCsr);
343 if( f & MEM_Zero ){
344 sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i);
345 zCsr += strlen(zCsr);
346 }
347 *zCsr = '\0';
348 }else if( f & MEM_Str ){
349 int j, k;
350 zBuf[0] = ' ';
351 if( f & MEM_Dyn ){
352 zBuf[1] = 'z';
353 assert( (f & (MEM_Static|MEM_Ephem))==0 );
354 }else if( f & MEM_Static ){
355 zBuf[1] = 't';
356 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
357 }else if( f & MEM_Ephem ){
358 zBuf[1] = 'e';
359 assert( (f & (MEM_Static|MEM_Dyn))==0 );
360 }else{
361 zBuf[1] = 's';
362 }
363 k = 2;
364 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
365 k += strlen(&zBuf[k]);
366 zBuf[k++] = '[';
367 for(j=0; j<15 && j<pMem->n; j++){
368 u8 c = pMem->z[j];
369 if( c>=0x20 && c<0x7f ){
370 zBuf[k++] = c;
371 }else{
372 zBuf[k++] = '.';
373 }
374 }
375 zBuf[k++] = ']';
376 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
377 k += strlen(&zBuf[k]);
378 zBuf[k++] = 0;
379 }
380}
381#endif
382
383
384#ifdef VDBE_PROFILE
385/*
386** The following routine only works on pentium-class processors.
387** It uses the RDTSC opcode to read the cycle count value out of the
388** processor and returns that value. This can be used for high-res
389** profiling.
390*/
391__inline__ unsigned long long int hwtime(void){
392 unsigned long long int x;
393 __asm__("rdtsc\n\t"
394 "mov %%edx, %%ecx\n\t"
395 :"=A" (x));
396 return x;
397}
398#endif
399
400/*
401** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
402** sqlite3_interrupt() routine has been called. If it has been, then
403** processing of the VDBE program is interrupted.
404**
405** This macro added to every instruction that does a jump in order to
406** implement a loop. This test used to be on every single instruction,
407** but that meant we more testing that we needed. By only testing the
408** flag on jump instructions, we get a (small) speed improvement.
409*/
410#define CHECK_FOR_INTERRUPT \
411 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
412
413
414/*
415** Execute as much of a VDBE program as we can then return.
416**
417** sqlite3VdbeMakeReady() must be called before this routine in order to
418** close the program with a final OP_Halt and to set up the callbacks
419** and the error message pointer.
420**
421** Whenever a row or result data is available, this routine will either
422** invoke the result callback (if there is one) or return with
423** SQLITE_ROW.
424**
425** If an attempt is made to open a locked database, then this routine
426** will either invoke the busy callback (if there is one) or it will
427** return SQLITE_BUSY.
428**
429** If an error occurs, an error message is written to memory obtained
430** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
431** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
432**
433** If the callback ever returns non-zero, then the program exits
434** immediately. There will be no error message but the p->rc field is
435** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
436**
437** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
438** routine to return SQLITE_ERROR.
439**
440** Other fatal errors return SQLITE_ERROR.
441**
442** After this routine has finished, sqlite3VdbeFinalize() should be
443** used to clean up the mess that was left behind.
444*/
445int sqlite3VdbeExec(
446 Vdbe *p /* The VDBE */
447){
448 int pc; /* The program counter */
449 Op *pOp; /* Current operation */
450 int rc = SQLITE_OK; /* Value to return */
451 sqlite3 *db = p->db; /* The database */
452 u8 encoding = ENC(db); /* The database encoding */
453 Mem *pTos; /* Top entry in the operand stack */
454#ifdef VDBE_PROFILE
455 unsigned long long start; /* CPU clock count at start of opcode */
456 int origPc; /* Program counter at start of opcode */
457#endif
458#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
459 int nProgressOps = 0; /* Opcodes executed since progress callback. */
460#endif
461#ifndef NDEBUG
462 Mem *pStackLimit;
463#endif
464
465 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
466 assert( db->magic==SQLITE_MAGIC_BUSY );
467 pTos = p->pTos;
468 sqlite3BtreeMutexArrayEnter(&p->aMutex);
469 if( p->rc==SQLITE_NOMEM ){
470 /* This happens if a malloc() inside a call to sqlite3_column_text() or
471 ** sqlite3_column_text16() failed. */
472 goto no_mem;
473 }
474 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
475 p->rc = SQLITE_OK;
476 assert( p->explain==0 );
477 if( p->popStack ){
478 popStack(&pTos, p->popStack);
479 p->popStack = 0;
480 }
481 p->resOnStack = 0;
482 db->busyHandler.nBusy = 0;
483 CHECK_FOR_INTERRUPT;
484 sqlite3VdbeIOTraceSql(p);
485#ifdef SQLITE_DEBUG
486 if( (p->db->flags & SQLITE_VdbeListing)!=0
487 || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)
488 ){
489 int i;
490 printf("VDBE Program Listing:\n");
491 sqlite3VdbePrintSql(p);
492 for(i=0; i<p->nOp; i++){
493 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
494 }
495 }
496 if( sqlite3OsAccess(db->pVfs, "vdbe_trace", SQLITE_ACCESS_EXISTS) ){
497 p->trace = stdout;
498 }
499#endif
500 for(pc=p->pc; rc==SQLITE_OK; pc++){
501 assert( pc>=0 && pc<p->nOp );
502 assert( pTos<=&p->aStack[pc] );
503 if( db->mallocFailed ) goto no_mem;
504#ifdef VDBE_PROFILE
505 origPc = pc;
506 start = hwtime();
507#endif
508 pOp = &p->aOp[pc];
509
510 /* Only allow tracing if SQLITE_DEBUG is defined.
511 */
512#ifdef SQLITE_DEBUG
513 if( p->trace ){
514 if( pc==0 ){
515 printf("VDBE Execution Trace:\n");
516 sqlite3VdbePrintSql(p);
517 }
518 sqlite3VdbePrintOp(p->trace, pc, pOp);
519 }
520 if( p->trace==0 && pc==0
521 && sqlite3OsAccess(db->pVfs, "vdbe_sqltrace", SQLITE_ACCESS_EXISTS) ){
522 sqlite3VdbePrintSql(p);
523 }
524#endif
525
526
527 /* Check to see if we need to simulate an interrupt. This only happens
528 ** if we have a special test build.
529 */
530#ifdef SQLITE_TEST
531 if( sqlite3_interrupt_count>0 ){
532 sqlite3_interrupt_count--;
533 if( sqlite3_interrupt_count==0 ){
534 sqlite3_interrupt(db);
535 }
536 }
537#endif
538
539#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
540 /* Call the progress callback if it is configured and the required number
541 ** of VDBE ops have been executed (either since this invocation of
542 ** sqlite3VdbeExec() or since last time the progress callback was called).
543 ** If the progress callback returns non-zero, exit the virtual machine with
544 ** a return code SQLITE_ABORT.
545 */
546 if( db->xProgress ){
547 if( db->nProgressOps==nProgressOps ){
548 int prc;
549 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
550 prc =db->xProgress(db->pProgressArg);
551 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
552 if( prc!=0 ){
553 rc = SQLITE_INTERRUPT;
554 goto vdbe_halt;
555 }
556 nProgressOps = 0;
557 }
558 nProgressOps++;
559 }
560#endif
561
562#ifndef NDEBUG
563 /* This is to check that the return value of static function
564 ** opcodeNoPush() (see vdbeaux.c) returns values that match the
565 ** implementation of the virtual machine in this file. If
566 ** opcodeNoPush() returns non-zero, then the stack is guarenteed
567 ** not to grow when the opcode is executed. If it returns zero, then
568 ** the stack may grow by at most 1.
569 **
570 ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not
571 ** available if NDEBUG is defined at build time.
572 */
573 pStackLimit = pTos;
574 if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){
575 pStackLimit++;
576 }
577#endif
578
579 switch( pOp->opcode ){
580
581/*****************************************************************************
582** What follows is a massive switch statement where each case implements a
583** separate instruction in the virtual machine. If we follow the usual
584** indentation conventions, each case should be indented by 6 spaces. But
585** that is a lot of wasted space on the left margin. So the code within
586** the switch statement will break with convention and be flush-left. Another
587** big comment (similar to this one) will mark the point in the code where
588** we transition back to normal indentation.
589**
590** The formatting of each case is important. The makefile for SQLite
591** generates two C files "opcodes.h" and "opcodes.c" by scanning this
592** file looking for lines that begin with "case OP_". The opcodes.h files
593** will be filled with #defines that give unique integer values to each
594** opcode and the opcodes.c file is filled with an array of strings where
595** each string is the symbolic name for the corresponding opcode. If the
596** case statement is followed by a comment of the form "/# same as ... #/"
597** that comment is used to determine the particular value of the opcode.
598**
599** If a comment on the same line as the "case OP_" construction contains
600** the word "no-push", then the opcode is guarenteed not to grow the
601** vdbe stack when it is executed. See function opcode() in
602** vdbeaux.c for details.
603**
604** Documentation about VDBE opcodes is generated by scanning this file
605** for lines of that contain "Opcode:". That line and all subsequent
606** comment lines are used in the generation of the opcode.html documentation
607** file.
608**
609** SUMMARY:
610**
611** Formatting is important to scripts that scan this file.
612** Do not deviate from the formatting style currently in use.
613**
614*****************************************************************************/
615
616/* Opcode: Goto * P2 *
617**
618** An unconditional jump to address P2.
619** The next instruction executed will be
620** the one at index P2 from the beginning of
621** the program.
622*/
623case OP_Goto: { /* no-push */
624 CHECK_FOR_INTERRUPT;
625 pc = pOp->p2 - 1;
626 break;
627}
628
629/* Opcode: Gosub * P2 *
630**
631** Push the current address plus 1 onto the return address stack
632** and then jump to address P2.
633**
634** The return address stack is of limited depth. If too many
635** OP_Gosub operations occur without intervening OP_Returns, then
636** the return address stack will fill up and processing will abort
637** with a fatal error.
638*/
639case OP_Gosub: { /* no-push */
640 assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) );
641 p->returnStack[p->returnDepth++] = pc+1;
642 pc = pOp->p2 - 1;
643 break;
644}
645
646/* Opcode: Return * * *
647**
648** Jump immediately to the next instruction after the last unreturned
649** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
650** processing aborts with a fatal error.
651*/
652case OP_Return: { /* no-push */
653 assert( p->returnDepth>0 );
654 p->returnDepth--;
655 pc = p->returnStack[p->returnDepth] - 1;
656 break;
657}
658
659/* Opcode: Halt P1 P2 P3
660**
661** Exit immediately. All open cursors, Fifos, etc are closed
662** automatically.
663**
664** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
665** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
666** For errors, it can be some other value. If P1!=0 then P2 will determine
667** whether or not to rollback the current transaction. Do not rollback
668** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
669** then back out all changes that have occurred during this execution of the
670** VDBE, but do not rollback the transaction.
671**
672** If P3 is not null then it is an error message string.
673**
674** There is an implied "Halt 0 0 0" instruction inserted at the very end of
675** every program. So a jump past the last instruction of the program
676** is the same as executing Halt.
677*/
678case OP_Halt: { /* no-push */
679 p->pTos = pTos;
680 p->rc = pOp->p1;
681 p->pc = pc;
682 p->errorAction = pOp->p2;
683 if( pOp->p3 ){
684 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
685 }
686 rc = sqlite3VdbeHalt(p);
687 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
688 if( rc==SQLITE_BUSY ){
689 p->rc = rc = SQLITE_BUSY;
690 }else{
691 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
692 }
693 goto vdbe_return;
694}
695
696/* Opcode: Integer P1 * *
697**
698** The 32-bit integer value P1 is pushed onto the stack.
699*/
700case OP_Integer: {
701 pTos++;
702 pTos->flags = MEM_Int;
703 pTos->u.i = pOp->p1;
704 break;
705}
706
707/* Opcode: Int64 * * P3
708**
709** P3 is a string representation of an integer. Convert that integer
710** to a 64-bit value and push it onto the stack.
711*/
712case OP_Int64: {
713 pTos++;
714 assert( pOp->p3!=0 );
715 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
716 pTos->z = pOp->p3;
717 pTos->n = strlen(pTos->z);
718 pTos->enc = SQLITE_UTF8;
719 pTos->u.i = sqlite3VdbeIntValue(pTos);
720 pTos->flags |= MEM_Int;
721 break;
722}
723
724/* Opcode: Real * * P3
725**
726** The string value P3 is converted to a real and pushed on to the stack.
727*/
728case OP_Real: { /* same as TK_FLOAT, */
729 pTos++;
730 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
731 pTos->z = pOp->p3;
732 pTos->n = strlen(pTos->z);
733 pTos->enc = SQLITE_UTF8;
734 pTos->r = sqlite3VdbeRealValue(pTos);
735 pTos->flags |= MEM_Real;
736 sqlite3VdbeChangeEncoding(pTos, encoding);
737 break;
738}
739
740/* Opcode: String8 * * P3
741**
742** P3 points to a nul terminated UTF-8 string. This opcode is transformed
743** into an OP_String before it is executed for the first time.
744*/
745case OP_String8: { /* same as TK_STRING */
746 assert( pOp->p3!=0 );
747 pOp->opcode = OP_String;
748 pOp->p1 = strlen(pOp->p3);
749 assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH );
750 assert( pOp->p1 < SQLITE_MAX_LENGTH );
751
752#ifndef SQLITE_OMIT_UTF16
753 if( encoding!=SQLITE_UTF8 ){
754 pTos++;
755 sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC);
756 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem;
757 if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
758 pTos->flags &= ~(MEM_Dyn);
759 pTos->flags |= MEM_Static;
760 if( pOp->p3type==P3_DYNAMIC ){
761 sqlite3_free(pOp->p3);
762 }
763 pOp->p3type = P3_DYNAMIC;
764 pOp->p3 = pTos->z;
765 pOp->p1 = pTos->n;
766 assert( pOp->p1 < SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
767 break;
768 }
769#endif
770 /* Otherwise fall through to the next case, OP_String */
771}
772
773/* Opcode: String P1 * P3
774**
775** The string value P3 of length P1 (bytes) is pushed onto the stack.
776*/
777case OP_String: {
778 assert( pOp->p1 < SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
779 pTos++;
780 assert( pOp->p3!=0 );
781 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
782 pTos->z = pOp->p3;
783 pTos->n = pOp->p1;
784 pTos->enc = encoding;
785 break;
786}
787
788/* Opcode: Null * * *
789**
790** Push a NULL onto the stack.
791*/
792case OP_Null: {
793 pTos++;
794 pTos->flags = MEM_Null;
795 pTos->n = 0;
796 break;
797}
798
799
800#ifndef SQLITE_OMIT_BLOB_LITERAL
801/* Opcode: HexBlob * * P3
802**
803** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the
804** vdbe stack.
805**
806** The first time this instruction executes, in transforms itself into a
807** 'Blob' opcode with a binary blob as P3.
808*/
809case OP_HexBlob: { /* same as TK_BLOB */
810 pOp->opcode = OP_Blob;
811 pOp->p1 = strlen(pOp->p3)/2;
812 assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH );
813 assert( pOp->p1 < SQLITE_MAX_LENGTH );
814 if( pOp->p1 ){
815 char *zBlob = sqlite3HexToBlob(db, pOp->p3);
816 if( !zBlob ) goto no_mem;
817 if( pOp->p3type==P3_DYNAMIC ){
818 sqlite3_free(pOp->p3);
819 }
820 pOp->p3 = zBlob;
821 pOp->p3type = P3_DYNAMIC;
822 }else{
823 if( pOp->p3type==P3_DYNAMIC ){
824 sqlite3_free(pOp->p3);
825 }
826 pOp->p3type = P3_STATIC;
827 pOp->p3 = "";
828 }
829
830 /* Fall through to the next case, OP_Blob. */
831}
832
833/* Opcode: Blob P1 * P3
834**
835** P3 points to a blob of data P1 bytes long. Push this
836** value onto the stack. This instruction is not coded directly
837** by the compiler. Instead, the compiler layer specifies
838** an OP_HexBlob opcode, with the hex string representation of
839** the blob as P3. This opcode is transformed to an OP_Blob
840** the first time it is executed.
841*/
842case OP_Blob: {
843 pTos++;
844 assert( pOp->p1 < SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */
845 sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
846 pTos->enc = encoding;
847 break;
848}
849#endif /* SQLITE_OMIT_BLOB_LITERAL */
850
851/* Opcode: Variable P1 * *
852**
853** Push the value of variable P1 onto the stack. A variable is
854** an unknown in the original SQL string as handed to sqlite3_compile().
855** Any occurance of the '?' character in the original SQL is considered
856** a variable. Variables in the SQL string are number from left to
857** right beginning with 1. The values of variables are set using the
858** sqlite3_bind() API.
859*/
860case OP_Variable: {
861 int j = pOp->p1 - 1;
862 Mem *pVar;
863 assert( j>=0 && j<p->nVar );
864
865 pVar = &p->aVar[j];
866 if( sqlite3VdbeMemTooBig(pVar) ){
867 goto too_big;
868 }
869 pTos++;
870 sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static);
871 break;
872}
873
874/* Opcode: Pop P1 * *
875**
876** P1 elements are popped off of the top of stack and discarded.
877*/
878case OP_Pop: { /* no-push */
879 assert( pOp->p1>=0 );
880 popStack(&pTos, pOp->p1);
881 assert( pTos>=&p->aStack[-1] );
882 break;
883}
884
885/* Opcode: Dup P1 P2 *
886**
887** A copy of the P1-th element of the stack
888** is made and pushed onto the top of the stack.
889** The top of the stack is element 0. So the
890** instruction "Dup 0 0 0" will make a copy of the
891** top of the stack.
892**
893** If the content of the P1-th element is a dynamically
894** allocated string, then a new copy of that string
895** is made if P2==0. If P2!=0, then just a pointer
896** to the string is copied.
897**
898** Also see the Pull instruction.
899*/
900case OP_Dup: {
901 Mem *pFrom = &pTos[-pOp->p1];
902 assert( pFrom<=pTos && pFrom>=p->aStack );
903 pTos++;
904 sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem);
905 if( pOp->p2 ){
906 Deephemeralize(pTos);
907 }
908 break;
909}
910
911/* Opcode: Pull P1 * *
912**
913** The P1-th element is removed from its current location on
914** the stack and pushed back on top of the stack. The
915** top of the stack is element 0, so "Pull 0 0 0" is
916** a no-op. "Pull 1 0 0" swaps the top two elements of
917** the stack.
918**
919** See also the Dup instruction.
920*/
921case OP_Pull: { /* no-push */
922 Mem *pFrom = &pTos[-pOp->p1];
923 int i;
924 Mem ts;
925
926 ts = *pFrom;
927 Deephemeralize(pTos);
928 for(i=0; i<pOp->p1; i++, pFrom++){
929 Deephemeralize(&pFrom[1]);
930 assert( (pFrom[1].flags & MEM_Ephem)==0 );
931 *pFrom = pFrom[1];
932 if( pFrom->flags & MEM_Short ){
933 assert( pFrom->flags & (MEM_Str|MEM_Blob) );
934 assert( pFrom->z==pFrom[1].zShort );
935 pFrom->z = pFrom->zShort;
936 }
937 }
938 *pTos = ts;
939 if( pTos->flags & MEM_Short ){
940 assert( pTos->flags & (MEM_Str|MEM_Blob) );
941 assert( pTos->z==pTos[-pOp->p1].zShort );
942 pTos->z = pTos->zShort;
943 }
944 break;
945}
946
947/* Opcode: Push P1 * *
948**
949** Overwrite the value of the P1-th element down on the
950** stack (P1==0 is the top of the stack) with the value
951** of the top of the stack. Then pop the top of the stack.
952*/
953case OP_Push: { /* no-push */
954 Mem *pTo = &pTos[-pOp->p1];
955
956 assert( pTo>=p->aStack );
957 sqlite3VdbeMemMove(pTo, pTos);
958 pTos--;
959 break;
960}
961
962/* Opcode: Callback P1 * *
963**
964** The top P1 values on the stack represent a single result row from
965** a query. This opcode causes the sqlite3_step() call to terminate
966** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
967** structure to provide access to the top P1 values as the result
968** row. When the sqlite3_step() function is run again, the top P1
969** values will be automatically popped from the stack before the next
970** instruction executes.
971*/
972case OP_Callback: { /* no-push */
973 Mem *pMem;
974 Mem *pFirstColumn;
975 assert( p->nResColumn==pOp->p1 );
976
977 /* Data in the pager might be moved or changed out from under us
978 ** in between the return from this sqlite3_step() call and the
979 ** next call to sqlite3_step(). So deephermeralize everything on
980 ** the stack. Note that ephemeral data is never stored in memory
981 ** cells so we do not have to worry about them.
982 */
983 pFirstColumn = &pTos[0-pOp->p1];
984 for(pMem = p->aStack; pMem<pFirstColumn; pMem++){
985 Deephemeralize(pMem);
986 }
987
988 /* Invalidate all ephemeral cursor row caches */
989 p->cacheCtr = (p->cacheCtr + 2)|1;
990
991 /* Make sure the results of the current row are \000 terminated
992 ** and have an assigned type. The results are deephemeralized as
993 ** as side effect.
994 */
995 for(; pMem<=pTos; pMem++ ){
996 sqlite3VdbeMemNulTerminate(pMem);
997 storeTypeInfo(pMem, encoding);
998 }
999
1000 /* Set up the statement structure so that it will pop the current
1001 ** results from the stack when the statement returns.
1002 */
1003 p->resOnStack = 1;
1004 p->nCallback++;
1005 p->popStack = pOp->p1;
1006 p->pc = pc + 1;
1007 p->pTos = pTos;
1008 rc = SQLITE_ROW;
1009 goto vdbe_return;
1010}
1011
1012/* Opcode: Concat P1 P2 *
1013**
1014** Look at the first P1+2 elements of the stack. Append them all
1015** together with the lowest element first. The original P1+2 elements
1016** are popped from the stack if P2==0 and retained if P2==1. If
1017** any element of the stack is NULL, then the result is NULL.
1018**
1019** When P1==1, this routine makes a copy of the top stack element
1020** into memory obtained from sqlite3_malloc().
1021*/
1022case OP_Concat: { /* same as TK_CONCAT */
1023 char *zNew;
1024 i64 nByte;
1025 int nField;
1026 int i, j;
1027 Mem *pTerm;
1028
1029 /* Loop through the stack elements to see how long the result will be. */
1030 nField = pOp->p1 + 2;
1031 pTerm = &pTos[1-nField];
1032 nByte = 0;
1033 for(i=0; i<nField; i++, pTerm++){
1034 assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
1035 if( pTerm->flags&MEM_Null ){
1036 nByte = -1;
1037 break;
1038 }
1039 ExpandBlob(pTerm);
1040 Stringify(pTerm, encoding);
1041 nByte += pTerm->n;
1042 }
1043
1044 if( nByte<0 ){
1045 /* If nByte is less than zero, then there is a NULL value on the stack.
1046 ** In this case just pop the values off the stack (if required) and
1047 ** push on a NULL.
1048 */
1049 if( pOp->p2==0 ){
1050 popStack(&pTos, nField);
1051 }
1052 pTos++;
1053 pTos->flags = MEM_Null;
1054 }else{
1055 /* Otherwise malloc() space for the result and concatenate all the
1056 ** stack values.
1057 */
1058 if( nByte+2>SQLITE_MAX_LENGTH ){
1059 goto too_big;
1060 }
1061 zNew = sqlite3DbMallocRaw(db, nByte+2 );
1062 if( zNew==0 ) goto no_mem;
1063 j = 0;
1064 pTerm = &pTos[1-nField];
1065 for(i=j=0; i<nField; i++, pTerm++){
1066 int n = pTerm->n;
1067 assert( pTerm->flags & (MEM_Str|MEM_Blob) );
1068 memcpy(&zNew[j], pTerm->z, n);
1069 j += n;
1070 }
1071 zNew[j] = 0;
1072 zNew[j+1] = 0;
1073 assert( j==nByte );
1074
1075 if( pOp->p2==0 ){
1076 popStack(&pTos, nField);
1077 }
1078 pTos++;
1079 pTos->n = j;
1080 pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
1081 pTos->xDel = 0;
1082 pTos->enc = encoding;
1083 pTos->z = zNew;
1084 }
1085 break;
1086}
1087
1088/* Opcode: Add * * *
1089**
1090** Pop the top two elements from the stack, add them together,
1091** and push the result back onto the stack. If either element
1092** is a string then it is converted to a double using the atof()
1093** function before the addition.
1094** If either operand is NULL, the result is NULL.
1095*/
1096/* Opcode: Multiply * * *
1097**
1098** Pop the top two elements from the stack, multiply them together,
1099** and push the result back onto the stack. If either element
1100** is a string then it is converted to a double using the atof()
1101** function before the multiplication.
1102** If either operand is NULL, the result is NULL.
1103*/
1104/* Opcode: Subtract * * *
1105**
1106** Pop the top two elements from the stack, subtract the
1107** first (what was on top of the stack) from the second (the
1108** next on stack)
1109** and push the result back onto the stack. If either element
1110** is a string then it is converted to a double using the atof()
1111** function before the subtraction.
1112** If either operand is NULL, the result is NULL.
1113*/
1114/* Opcode: Divide * * *
1115**
1116** Pop the top two elements from the stack, divide the
1117** first (what was on top of the stack) from the second (the
1118** next on stack)
1119** and push the result back onto the stack. If either element
1120** is a string then it is converted to a double using the atof()
1121** function before the division. Division by zero returns NULL.
1122** If either operand is NULL, the result is NULL.
1123*/
1124/* Opcode: Remainder * * *
1125**
1126** Pop the top two elements from the stack, divide the
1127** first (what was on top of the stack) from the second (the
1128** next on stack)
1129** and push the remainder after division onto the stack. If either element
1130** is a string then it is converted to a double using the atof()
1131** function before the division. Division by zero returns NULL.
1132** If either operand is NULL, the result is NULL.
1133*/
1134case OP_Add: /* same as TK_PLUS, no-push */
1135case OP_Subtract: /* same as TK_MINUS, no-push */
1136case OP_Multiply: /* same as TK_STAR, no-push */
1137case OP_Divide: /* same as TK_SLASH, no-push */
1138case OP_Remainder: { /* same as TK_REM, no-push */
1139 Mem *pNos = &pTos[-1];
1140 int flags;
1141 assert( pNos>=p->aStack );
1142 flags = pTos->flags | pNos->flags;
1143 if( (flags & MEM_Null)!=0 ){
1144 Release(pTos);
1145 pTos--;
1146 Release(pTos);
1147 pTos->flags = MEM_Null;
1148 }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
1149 i64 a, b;
1150 a = pTos->u.i;
1151 b = pNos->u.i;
1152 switch( pOp->opcode ){
1153 case OP_Add: b += a; break;
1154 case OP_Subtract: b -= a; break;
1155 case OP_Multiply: b *= a; break;
1156 case OP_Divide: {
1157 if( a==0 ) goto divide_by_zero;
1158 /* Dividing the largest possible negative 64-bit integer (1<<63) by
1159 ** -1 returns an integer to large to store in a 64-bit data-type. On
1160 ** some architectures, the value overflows to (1<<63). On others,
1161 ** a SIGFPE is issued. The following statement normalizes this
1162 ** behaviour so that all architectures behave as if integer
1163 ** overflow occured.
1164 */
1165 if( a==-1 && b==(((i64)1)<<63) ) a = 1;
1166 b /= a;
1167 break;
1168 }
1169 default: {
1170 if( a==0 ) goto divide_by_zero;
1171 if( a==-1 ) a = 1;
1172 b %= a;
1173 break;
1174 }
1175 }
1176 Release(pTos);
1177 pTos--;
1178 Release(pTos);
1179 pTos->u.i = b;
1180 pTos->flags = MEM_Int;
1181 }else{
1182 double a, b;
1183 a = sqlite3VdbeRealValue(pTos);
1184 b = sqlite3VdbeRealValue(pNos);
1185 switch( pOp->opcode ){
1186 case OP_Add: b += a; break;
1187 case OP_Subtract: b -= a; break;
1188 case OP_Multiply: b *= a; break;
1189 case OP_Divide: {
1190 if( a==0.0 ) goto divide_by_zero;
1191 b /= a;
1192 break;
1193 }
1194 default: {
1195 i64 ia = (i64)a;
1196 i64 ib = (i64)b;
1197 if( ia==0 ) goto divide_by_zero;
1198 if( ia==-1 ) ia = 1;
1199 b = ib % ia;
1200 break;
1201 }
1202 }
1203 if( sqlite3_isnan(b) ){
1204 goto divide_by_zero;
1205 }
1206 Release(pTos);
1207 pTos--;
1208 Release(pTos);
1209 pTos->r = b;
1210 pTos->flags = MEM_Real;
1211 if( (flags & MEM_Real)==0 ){
1212 sqlite3VdbeIntegerAffinity(pTos);
1213 }
1214 }
1215 break;
1216
1217divide_by_zero:
1218 Release(pTos);
1219 pTos--;
1220 Release(pTos);
1221 pTos->flags = MEM_Null;
1222 break;
1223}
1224
1225/* Opcode: CollSeq * * P3
1226**
1227** P3 is a pointer to a CollSeq struct. If the next call to a user function
1228** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1229** be returned. This is used by the built-in min(), max() and nullif()
1230** functions.
1231**
1232** The interface used by the implementation of the aforementioned functions
1233** to retrieve the collation sequence set by this opcode is not available
1234** publicly, only to user functions defined in func.c.
1235*/
1236case OP_CollSeq: { /* no-push */
1237 assert( pOp->p3type==P3_COLLSEQ );
1238 break;
1239}
1240
1241/* Opcode: Function P1 P2 P3
1242**
1243** Invoke a user function (P3 is a pointer to a Function structure that
1244** defines the function) with P2 arguments taken from the stack. Pop all
1245** arguments from the stack and push back the result.
1246**
1247** P1 is a 32-bit bitmask indicating whether or not each argument to the
1248** function was determined to be constant at compile time. If the first
1249** argument was constant then bit 0 of P1 is set. This is used to determine
1250** whether meta data associated with a user function argument using the
1251** sqlite3_set_auxdata() API may be safely retained until the next
1252** invocation of this opcode.
1253**
1254** See also: AggStep and AggFinal
1255*/
1256case OP_Function: {
1257 int i;
1258 Mem *pArg;
1259 sqlite3_context ctx;
1260 sqlite3_value **apVal;
1261 int n = pOp->p2;
1262
1263 apVal = p->apArg;
1264 assert( apVal || n==0 );
1265
1266 pArg = &pTos[1-n];
1267 for(i=0; i<n; i++, pArg++){
1268 apVal[i] = pArg;
1269 storeTypeInfo(pArg, encoding);
1270 }
1271
1272 assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
1273 if( pOp->p3type==P3_FUNCDEF ){
1274 ctx.pFunc = (FuncDef*)pOp->p3;
1275 ctx.pVdbeFunc = 0;
1276 }else{
1277 ctx.pVdbeFunc = (VdbeFunc*)pOp->p3;
1278 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1279 }
1280
1281 ctx.s.flags = MEM_Null;
1282 ctx.s.z = 0;
1283 ctx.s.xDel = 0;
1284 ctx.s.db = db;
1285 ctx.isError = 0;
1286 if( ctx.pFunc->needCollSeq ){
1287 assert( pOp>p->aOp );
1288 assert( pOp[-1].p3type==P3_COLLSEQ );
1289 assert( pOp[-1].opcode==OP_CollSeq );
1290 ctx.pColl = (CollSeq *)pOp[-1].p3;
1291 }
1292 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1293 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
1294 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1295 if( db->mallocFailed ){
1296 /* Even though a malloc() has failed, the implementation of the
1297 ** user function may have called an sqlite3_result_XXX() function
1298 ** to return a value. The following call releases any resources
1299 ** associated with such a value.
1300 **
1301 ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1302 ** fails also (the if(...) statement above). But if people are
1303 ** misusing sqlite, they have bigger problems than a leaked value.
1304 */
1305 sqlite3VdbeMemRelease(&ctx.s);
1306 goto no_mem;
1307 }
1308 popStack(&pTos, n);
1309
1310 /* If any auxilary data functions have been called by this user function,
1311 ** immediately call the destructor for any non-static values.
1312 */
1313 if( ctx.pVdbeFunc ){
1314 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
1315 pOp->p3 = (char *)ctx.pVdbeFunc;
1316 pOp->p3type = P3_VDBEFUNC;
1317 }
1318
1319 /* If the function returned an error, throw an exception */
1320 if( ctx.isError ){
1321 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
1322 rc = SQLITE_ERROR;
1323 }
1324
1325 /* Copy the result of the function to the top of the stack */
1326 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
1327 pTos++;
1328 pTos->flags = 0;
1329 sqlite3VdbeMemMove(pTos, &ctx.s);
1330 if( sqlite3VdbeMemTooBig(pTos) ){
1331 goto too_big;
1332 }
1333 break;
1334}
1335
1336/* Opcode: BitAnd * * *
1337**
1338** Pop the top two elements from the stack. Convert both elements
1339** to integers. Push back onto the stack the bit-wise AND of the
1340** two elements.
1341** If either operand is NULL, the result is NULL.
1342*/
1343/* Opcode: BitOr * * *
1344**
1345** Pop the top two elements from the stack. Convert both elements
1346** to integers. Push back onto the stack the bit-wise OR of the
1347** two elements.
1348** If either operand is NULL, the result is NULL.
1349*/
1350/* Opcode: ShiftLeft * * *
1351**
1352** Pop the top two elements from the stack. Convert both elements
1353** to integers. Push back onto the stack the second element shifted
1354** left by N bits where N is the top element on the stack.
1355** If either operand is NULL, the result is NULL.
1356*/
1357/* Opcode: ShiftRight * * *
1358**
1359** Pop the top two elements from the stack. Convert both elements
1360** to integers. Push back onto the stack the second element shifted
1361** right by N bits where N is the top element on the stack.
1362** If either operand is NULL, the result is NULL.
1363*/
1364case OP_BitAnd: /* same as TK_BITAND, no-push */
1365case OP_BitOr: /* same as TK_BITOR, no-push */
1366case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */
1367case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */
1368 Mem *pNos = &pTos[-1];
1369 i64 a, b;
1370
1371 assert( pNos>=p->aStack );
1372 if( (pTos->flags | pNos->flags) & MEM_Null ){
1373 popStack(&pTos, 2);
1374 pTos++;
1375 pTos->flags = MEM_Null;
1376 break;
1377 }
1378 a = sqlite3VdbeIntValue(pNos);
1379 b = sqlite3VdbeIntValue(pTos);
1380 switch( pOp->opcode ){
1381 case OP_BitAnd: a &= b; break;
1382 case OP_BitOr: a |= b; break;
1383 case OP_ShiftLeft: a <<= b; break;
1384 case OP_ShiftRight: a >>= b; break;
1385 default: /* CANT HAPPEN */ break;
1386 }
1387 Release(pTos);
1388 pTos--;
1389 Release(pTos);
1390 pTos->u.i = a;
1391 pTos->flags = MEM_Int;
1392 break;
1393}
1394
1395/* Opcode: AddImm P1 * *
1396**
1397** Add the value P1 to whatever is on top of the stack. The result
1398** is always an integer.
1399**
1400** To force the top of the stack to be an integer, just add 0.
1401*/
1402case OP_AddImm: { /* no-push */
1403 assert( pTos>=p->aStack );
1404 sqlite3VdbeMemIntegerify(pTos);
1405 pTos->u.i += pOp->p1;
1406 break;
1407}
1408
1409/* Opcode: ForceInt P1 P2 *
1410**
1411** Convert the top of the stack into an integer. If the current top of
1412** the stack is not numeric (meaning that is is a NULL or a string that
1413** does not look like an integer or floating point number) then pop the
1414** stack and jump to P2. If the top of the stack is numeric then
1415** convert it into the least integer that is greater than or equal to its
1416** current value if P1==0, or to the least integer that is strictly
1417** greater than its current value if P1==1.
1418*/
1419case OP_ForceInt: { /* no-push */
1420 i64 v;
1421 assert( pTos>=p->aStack );
1422 applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
1423 if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){
1424 Release(pTos);
1425 pTos--;
1426 pc = pOp->p2 - 1;
1427 break;
1428 }
1429 if( pTos->flags & MEM_Int ){
1430 v = pTos->u.i + (pOp->p1!=0);
1431 }else{
1432 /* FIX ME: should this not be assert( pTos->flags & MEM_Real ) ??? */
1433 sqlite3VdbeMemRealify(pTos);
1434 v = (int)pTos->r;
1435 if( pTos->r>(double)v ) v++;
1436 if( pOp->p1 && pTos->r==(double)v ) v++;
1437 }
1438 Release(pTos);
1439 pTos->u.i = v;
1440 pTos->flags = MEM_Int;
1441 break;
1442}
1443
1444/* Opcode: MustBeInt P1 P2 *
1445**
1446** Force the top of the stack to be an integer. If the top of the
1447** stack is not an integer and cannot be converted into an integer
1448** with out data loss, then jump immediately to P2, or if P2==0
1449** raise an SQLITE_MISMATCH exception.
1450**
1451** If the top of the stack is not an integer and P2 is not zero and
1452** P1 is 1, then the stack is popped. In all other cases, the depth
1453** of the stack is unchanged.
1454*/
1455case OP_MustBeInt: { /* no-push */
1456 assert( pTos>=p->aStack );
1457 applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
1458 if( (pTos->flags & MEM_Int)==0 ){
1459 if( pOp->p2==0 ){
1460 rc = SQLITE_MISMATCH;
1461 goto abort_due_to_error;
1462 }else{
1463 if( pOp->p1 ) popStack(&pTos, 1);
1464 pc = pOp->p2 - 1;
1465 }
1466 }else{
1467 Release(pTos);
1468 pTos->flags = MEM_Int;
1469 }
1470 break;
1471}
1472
1473/* Opcode: RealAffinity * * *
1474**
1475** If the top of the stack is an integer, convert it to a real value.
1476**
1477** This opcode is used when extracting information from a column that
1478** has REAL affinity. Such column values may still be stored as
1479** integers, for space efficiency, but after extraction we want them
1480** to have only a real value.
1481*/
1482case OP_RealAffinity: { /* no-push */
1483 assert( pTos>=p->aStack );
1484 if( pTos->flags & MEM_Int ){
1485 sqlite3VdbeMemRealify(pTos);
1486 }
1487 break;
1488}
1489
1490#ifndef SQLITE_OMIT_CAST
1491/* Opcode: ToText * * *
1492**
1493** Force the value on the top of the stack to be text.
1494** If the value is numeric, convert it to a string using the
1495** equivalent of printf(). Blob values are unchanged and
1496** are afterwards simply interpreted as text.
1497**
1498** A NULL value is not changed by this routine. It remains NULL.
1499*/
1500case OP_ToText: { /* same as TK_TO_TEXT, no-push */
1501 assert( pTos>=p->aStack );
1502 if( pTos->flags & MEM_Null ) break;
1503 assert( MEM_Str==(MEM_Blob>>3) );
1504 pTos->flags |= (pTos->flags&MEM_Blob)>>3;
1505 applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
1506 rc = ExpandBlob(pTos);
1507 assert( pTos->flags & MEM_Str );
1508 pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
1509 break;
1510}
1511
1512/* Opcode: ToBlob * * *
1513**
1514** Force the value on the top of the stack to be a BLOB.
1515** If the value is numeric, convert it to a string first.
1516** Strings are simply reinterpreted as blobs with no change
1517** to the underlying data.
1518**
1519** A NULL value is not changed by this routine. It remains NULL.
1520*/
1521case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */
1522 assert( pTos>=p->aStack );
1523 if( pTos->flags & MEM_Null ) break;
1524 if( (pTos->flags & MEM_Blob)==0 ){
1525 applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
1526 assert( pTos->flags & MEM_Str );
1527 pTos->flags |= MEM_Blob;
1528 }
1529 pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str);
1530 break;
1531}
1532
1533/* Opcode: ToNumeric * * *
1534**
1535** Force the value on the top of the stack to be numeric (either an
1536** integer or a floating-point number.)
1537** If the value is text or blob, try to convert it to an using the
1538** equivalent of atoi() or atof() and store 0 if no such conversion
1539** is possible.
1540**
1541** A NULL value is not changed by this routine. It remains NULL.
1542*/
1543case OP_ToNumeric: { /* same as TK_TO_NUMERIC, no-push */
1544 assert( pTos>=p->aStack );
1545 if( (pTos->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1546 sqlite3VdbeMemNumerify(pTos);
1547 }
1548 break;
1549}
1550#endif /* SQLITE_OMIT_CAST */
1551
1552/* Opcode: ToInt * * *
1553**
1554** Force the value on the top of the stack to be an integer. If
1555** The value is currently a real number, drop its fractional part.
1556** If the value is text or blob, try to convert it to an integer using the
1557** equivalent of atoi() and store 0 if no such conversion is possible.
1558**
1559** A NULL value is not changed by this routine. It remains NULL.
1560*/
1561case OP_ToInt: { /* same as TK_TO_INT, no-push */
1562 assert( pTos>=p->aStack );
1563 if( (pTos->flags & MEM_Null)==0 ){
1564 sqlite3VdbeMemIntegerify(pTos);
1565 }
1566 break;
1567}
1568
1569#ifndef SQLITE_OMIT_CAST
1570/* Opcode: ToReal * * *
1571**
1572** Force the value on the top of the stack to be a floating point number.
1573** If The value is currently an integer, convert it.
1574** If the value is text or blob, try to convert it to an integer using the
1575** equivalent of atoi() and store 0 if no such conversion is possible.
1576**
1577** A NULL value is not changed by this routine. It remains NULL.
1578*/
1579case OP_ToReal: { /* same as TK_TO_REAL, no-push */
1580 assert( pTos>=p->aStack );
1581 if( (pTos->flags & MEM_Null)==0 ){
1582 sqlite3VdbeMemRealify(pTos);
1583 }
1584 break;
1585}
1586#endif /* SQLITE_OMIT_CAST */
1587
1588/* Opcode: Eq P1 P2 P3
1589**
1590** Pop the top two elements from the stack. If they are equal, then
1591** jump to instruction P2. Otherwise, continue to the next instruction.
1592**
1593** If the 0x100 bit of P1 is true and either operand is NULL then take the
1594** jump. If the 0x100 bit of P1 is clear then fall thru if either operand
1595** is NULL.
1596**
1597** If the 0x200 bit of P1 is set and either operand is NULL then
1598** both operands are converted to integers prior to comparison.
1599** NULL operands are converted to zero and non-NULL operands are
1600** converted to 1. Thus, for example, with 0x200 set, NULL==NULL is true
1601** whereas it would normally be NULL. Similarly, NULL==123 is false when
1602** 0x200 is set but is NULL when the 0x200 bit of P1 is clear.
1603**
1604** The least significant byte of P1 (mask 0xff) must be an affinity character -
1605** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1606** to coerce both values
1607** according to the affinity before the comparison is made. If the byte is
1608** 0x00, then numeric affinity is used.
1609**
1610** Once any conversions have taken place, and neither value is NULL,
1611** the values are compared. If both values are blobs, or both are text,
1612** then memcmp() is used to determine the results of the comparison. If
1613** both values are numeric, then a numeric comparison is used. If the
1614** two values are of different types, then they are inequal.
1615**
1616** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1617** stack if the jump would have been taken, or a 0 if not. Push a
1618** NULL if either operand was NULL.
1619**
1620** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
1621** structure) that defines how to compare text.
1622*/
1623/* Opcode: Ne P1 P2 P3
1624**
1625** This works just like the Eq opcode except that the jump is taken if
1626** the operands from the stack are not equal. See the Eq opcode for
1627** additional information.
1628*/
1629/* Opcode: Lt P1 P2 P3
1630**
1631** This works just like the Eq opcode except that the jump is taken if
1632** the 2nd element down on the stack is less than the top of the stack.
1633** See the Eq opcode for additional information.
1634*/
1635/* Opcode: Le P1 P2 P3
1636**
1637** This works just like the Eq opcode except that the jump is taken if
1638** the 2nd element down on the stack is less than or equal to the
1639** top of the stack. See the Eq opcode for additional information.
1640*/
1641/* Opcode: Gt P1 P2 P3
1642**
1643** This works just like the Eq opcode except that the jump is taken if
1644** the 2nd element down on the stack is greater than the top of the stack.
1645** See the Eq opcode for additional information.
1646*/
1647/* Opcode: Ge P1 P2 P3
1648**
1649** This works just like the Eq opcode except that the jump is taken if
1650** the 2nd element down on the stack is greater than or equal to the
1651** top of the stack. See the Eq opcode for additional information.
1652*/
1653case OP_Eq: /* same as TK_EQ, no-push */
1654case OP_Ne: /* same as TK_NE, no-push */
1655case OP_Lt: /* same as TK_LT, no-push */
1656case OP_Le: /* same as TK_LE, no-push */
1657case OP_Gt: /* same as TK_GT, no-push */
1658case OP_Ge: { /* same as TK_GE, no-push */
1659 Mem *pNos;
1660 int flags;
1661 int res;
1662 char affinity;
1663
1664 pNos = &pTos[-1];
1665 flags = pTos->flags|pNos->flags;
1666
1667 /* If either value is a NULL P2 is not zero, take the jump if the least
1668 ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
1669 ** the stack.
1670 */
1671 if( flags&MEM_Null ){
1672 if( (pOp->p1 & 0x200)!=0 ){
1673 /* The 0x200 bit of P1 means, roughly "do not treat NULL as the
1674 ** magic SQL value it normally is - treat it as if it were another
1675 ** integer".
1676 **
1677 ** With 0x200 set, if either operand is NULL then both operands
1678 ** are converted to integers prior to being passed down into the
1679 ** normal comparison logic below. NULL operands are converted to
1680 ** zero and non-NULL operands are converted to 1. Thus, for example,
1681 ** with 0x200 set, NULL==NULL is true whereas it would normally
1682 ** be NULL. Similarly, NULL!=123 is true.
1683 */
1684 sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0);
1685 sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0);
1686 }else{
1687 /* If the 0x200 bit of P1 is clear and either operand is NULL then
1688 ** the result is always NULL. The jump is taken if the 0x100 bit
1689 ** of P1 is set.
1690 */
1691 popStack(&pTos, 2);
1692 if( pOp->p2 ){
1693 if( pOp->p1 & 0x100 ){
1694 pc = pOp->p2-1;
1695 }
1696 }else{
1697 pTos++;
1698 pTos->flags = MEM_Null;
1699 }
1700 break;
1701 }
1702 }
1703
1704 affinity = pOp->p1 & 0xFF;
1705 if( affinity ){
1706 applyAffinity(pNos, affinity, encoding);
1707 applyAffinity(pTos, affinity, encoding);
1708 }
1709
1710 assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
1711 ExpandBlob(pNos);
1712 ExpandBlob(pTos);
1713 res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
1714 switch( pOp->opcode ){
1715 case OP_Eq: res = res==0; break;
1716 case OP_Ne: res = res!=0; break;
1717 case OP_Lt: res = res<0; break;
1718 case OP_Le: res = res<=0; break;
1719 case OP_Gt: res = res>0; break;
1720 default: res = res>=0; break;
1721 }
1722
1723 popStack(&pTos, 2);
1724 if( pOp->p2 ){
1725 if( res ){
1726 pc = pOp->p2-1;
1727 }
1728 }else{
1729 pTos++;
1730 pTos->flags = MEM_Int;
1731 pTos->u.i = res;
1732 }
1733 break;
1734}
1735
1736/* Opcode: And * * *
1737**
1738** Pop two values off the stack. Take the logical AND of the
1739** two values and push the resulting boolean value back onto the
1740** stack.
1741*/
1742/* Opcode: Or * * *
1743**
1744** Pop two values off the stack. Take the logical OR of the
1745** two values and push the resulting boolean value back onto the
1746** stack.
1747*/
1748case OP_And: /* same as TK_AND, no-push */
1749case OP_Or: { /* same as TK_OR, no-push */
1750 Mem *pNos = &pTos[-1];
1751 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1752
1753 assert( pNos>=p->aStack );
1754 if( pTos->flags & MEM_Null ){
1755 v1 = 2;
1756 }else{
1757 sqlite3VdbeMemIntegerify(pTos);
1758 v1 = pTos->u.i==0;
1759 }
1760 if( pNos->flags & MEM_Null ){
1761 v2 = 2;
1762 }else{
1763 sqlite3VdbeMemIntegerify(pNos);
1764 v2 = pNos->u.i==0;
1765 }
1766 if( pOp->opcode==OP_And ){
1767 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1768 v1 = and_logic[v1*3+v2];
1769 }else{
1770 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1771 v1 = or_logic[v1*3+v2];
1772 }
1773 popStack(&pTos, 2);
1774 pTos++;
1775 if( v1==2 ){
1776 pTos->flags = MEM_Null;
1777 }else{
1778 pTos->u.i = v1==0;
1779 pTos->flags = MEM_Int;
1780 }
1781 break;
1782}
1783
1784/* Opcode: Negative * * *
1785**
1786** Treat the top of the stack as a numeric quantity. Replace it
1787** with its additive inverse. If the top of the stack is NULL
1788** its value is unchanged.
1789*/
1790/* Opcode: AbsValue * * *
1791**
1792** Treat the top of the stack as a numeric quantity. Replace it
1793** with its absolute value. If the top of the stack is NULL
1794** its value is unchanged.
1795*/
1796case OP_Negative: /* same as TK_UMINUS, no-push */
1797case OP_AbsValue: {
1798 assert( pTos>=p->aStack );
1799 if( (pTos->flags & (MEM_Real|MEM_Int|MEM_Null))==0 ){
1800 sqlite3VdbeMemNumerify(pTos);
1801 }
1802 if( pTos->flags & MEM_Real ){
1803 Release(pTos);
1804 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1805 pTos->r = -pTos->r;
1806 }
1807 pTos->flags = MEM_Real;
1808 }else if( pTos->flags & MEM_Int ){
1809 Release(pTos);
1810 if( pOp->opcode==OP_Negative || pTos->u.i<0 ){
1811 pTos->u.i = -pTos->u.i;
1812 }
1813 pTos->flags = MEM_Int;
1814 }
1815 break;
1816}
1817
1818/* Opcode: Not * * *
1819**
1820** Interpret the top of the stack as a boolean value. Replace it
1821** with its complement. If the top of the stack is NULL its value
1822** is unchanged.
1823*/
1824case OP_Not: { /* same as TK_NOT, no-push */
1825 assert( pTos>=p->aStack );
1826 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1827 sqlite3VdbeMemIntegerify(pTos);
1828 assert( (pTos->flags & MEM_Dyn)==0 );
1829 pTos->u.i = !pTos->u.i;
1830 pTos->flags = MEM_Int;
1831 break;
1832}
1833
1834/* Opcode: BitNot * * *
1835**
1836** Interpret the top of the stack as an value. Replace it
1837** with its ones-complement. If the top of the stack is NULL its
1838** value is unchanged.
1839*/
1840case OP_BitNot: { /* same as TK_BITNOT, no-push */
1841 assert( pTos>=p->aStack );
1842 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1843 sqlite3VdbeMemIntegerify(pTos);
1844 assert( (pTos->flags & MEM_Dyn)==0 );
1845 pTos->u.i = ~pTos->u.i;
1846 pTos->flags = MEM_Int;
1847 break;
1848}
1849
1850/* Opcode: Noop * * *
1851**
1852** Do nothing. This instruction is often useful as a jump
1853** destination.
1854*/
1855/*
1856** The magic Explain opcode are only inserted when explain==2 (which
1857** is to say when the EXPLAIN QUERY PLAN syntax is used.)
1858** This opcode records information from the optimizer. It is the
1859** the same as a no-op. This opcodesnever appears in a real VM program.
1860*/
1861case OP_Explain:
1862case OP_Noop: { /* no-push */
1863 break;
1864}
1865
1866/* Opcode: If P1 P2 *
1867**
1868** Pop a single boolean from the stack. If the boolean popped is
1869** true, then jump to p2. Otherwise continue to the next instruction.
1870** An integer is false if zero and true otherwise. A string is
1871** false if it has zero length and true otherwise.
1872**
1873** If the value popped of the stack is NULL, then take the jump if P1
1874** is true and fall through if P1 is false.
1875*/
1876/* Opcode: IfNot P1 P2 *
1877**
1878** Pop a single boolean from the stack. If the boolean popped is
1879** false, then jump to p2. Otherwise continue to the next instruction.
1880** An integer is false if zero and true otherwise. A string is
1881** false if it has zero length and true otherwise.
1882**
1883** If the value popped of the stack is NULL, then take the jump if P1
1884** is true and fall through if P1 is false.
1885*/
1886case OP_If: /* no-push */
1887case OP_IfNot: { /* no-push */
1888 int c;
1889 assert( pTos>=p->aStack );
1890 if( pTos->flags & MEM_Null ){
1891 c = pOp->p1;
1892 }else{
1893#ifdef SQLITE_OMIT_FLOATING_POINT
1894 c = sqlite3VdbeIntValue(pTos);
1895#else
1896 c = sqlite3VdbeRealValue(pTos)!=0.0;
1897#endif
1898 if( pOp->opcode==OP_IfNot ) c = !c;
1899 }
1900 Release(pTos);
1901 pTos--;
1902 if( c ) pc = pOp->p2-1;
1903 break;
1904}
1905
1906/* Opcode: IsNull P1 P2 *
1907**
1908** Check the top of the stack and jump to P2 if the top of the stack
1909** is NULL. If P1 is positive, then pop P1 elements from the stack
1910** regardless of whether or not the jump is taken. If P1 is negative,
1911** pop -P1 elements from the stack only if the jump is taken and leave
1912** the stack unchanged if the jump is not taken.
1913*/
1914case OP_IsNull: { /* same as TK_ISNULL, no-push */
1915 if( pTos->flags & MEM_Null ){
1916 pc = pOp->p2-1;
1917 if( pOp->p1<0 ){
1918 popStack(&pTos, -pOp->p1);
1919 }
1920 }
1921 if( pOp->p1>0 ){
1922 popStack(&pTos, pOp->p1);
1923 }
1924 break;
1925}
1926
1927/* Opcode: NotNull P1 P2 *
1928**
1929** Jump to P2 if the top abs(P1) values on the stack are all not NULL.
1930** Regardless of whether or not the jump is taken, pop the stack
1931** P1 times if P1 is greater than zero. But if P1 is negative,
1932** leave the stack unchanged.
1933*/
1934case OP_NotNull: { /* same as TK_NOTNULL, no-push */
1935 int i, cnt;
1936 cnt = pOp->p1;
1937 if( cnt<0 ) cnt = -cnt;
1938 assert( &pTos[1-cnt] >= p->aStack );
1939 for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
1940 if( i>=cnt ) pc = pOp->p2-1;
1941 if( pOp->p1>0 ) popStack(&pTos, cnt);
1942 break;
1943}
1944
1945/* Opcode: SetNumColumns P1 P2 *
1946**
1947** Before the OP_Column opcode can be executed on a cursor, this
1948** opcode must be called to set the number of fields in the table.
1949**
1950** This opcode sets the number of columns for cursor P1 to P2.
1951**
1952** If OP_KeyAsData is to be applied to cursor P1, it must be executed
1953** before this op-code.
1954*/
1955case OP_SetNumColumns: { /* no-push */
1956 Cursor *pC;
1957 assert( (pOp->p1)<p->nCursor );
1958 assert( p->apCsr[pOp->p1]!=0 );
1959 pC = p->apCsr[pOp->p1];
1960 pC->nField = pOp->p2;
1961 break;
1962}
1963
1964/* Opcode: Column P1 P2 P3
1965**
1966** Interpret the data that cursor P1 points to as a structure built using
1967** the MakeRecord instruction. (See the MakeRecord opcode for additional
1968** information about the format of the data.) Push onto the stack the value
1969** of the P2-th column contained in the data. If there are less that (P2+1)
1970** values in the record, push a NULL onto the stack.
1971**
1972** If the KeyAsData opcode has previously executed on this cursor, then the
1973** field might be extracted from the key rather than the data.
1974**
1975** If the column contains fewer than P2 fields, then push a NULL. Or
1976** if P3 is of type P3_MEM, then push the P3 value. The P3 value will
1977** be default value for a column that has been added using the ALTER TABLE
1978** ADD COLUMN command. If P3 is an ordinary string, just push a NULL.
1979** When P3 is a string it is really just a comment describing the value
1980** to be pushed, not a default value.
1981*/
1982case OP_Column: {
1983 u32 payloadSize; /* Number of bytes in the record */
1984 int p1 = pOp->p1; /* P1 value of the opcode */
1985 int p2 = pOp->p2; /* column number to retrieve */
1986 Cursor *pC = 0; /* The VDBE cursor */
1987 char *zRec; /* Pointer to complete record-data */
1988 BtCursor *pCrsr; /* The BTree cursor */
1989 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1990 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
1991 u32 nField; /* number of fields in the record */
1992 int len; /* The length of the serialized data for the column */
1993 int i; /* Loop counter */
1994 char *zData; /* Part of the record being decoded */
1995 Mem sMem; /* For storing the record being decoded */
1996
1997 sMem.flags = 0;
1998 assert( p1<p->nCursor );
1999 pTos++;
2000 pTos->flags = MEM_Null;
2001
2002 /* This block sets the variable payloadSize to be the total number of
2003 ** bytes in the record.
2004 **
2005 ** zRec is set to be the complete text of the record if it is available.
2006 ** The complete record text is always available for pseudo-tables
2007 ** If the record is stored in a cursor, the complete record text
2008 ** might be available in the pC->aRow cache. Or it might not be.
2009 ** If the data is unavailable, zRec is set to NULL.
2010 **
2011 ** We also compute the number of columns in the record. For cursors,
2012 ** the number of columns is stored in the Cursor.nField element. For
2013 ** records on the stack, the next entry down on the stack is an integer
2014 ** which is the number of records.
2015 */
2016 pC = p->apCsr[p1];
2017#ifndef SQLITE_OMIT_VIRTUALTABLE
2018 assert( pC->pVtabCursor==0 );
2019#endif
2020 assert( pC!=0 );
2021 if( pC->pCursor!=0 ){
2022 /* The record is stored in a B-Tree */
2023 rc = sqlite3VdbeCursorMoveto(pC);
2024 if( rc ) goto abort_due_to_error;
2025 zRec = 0;
2026 pCrsr = pC->pCursor;
2027 if( pC->nullRow ){
2028 payloadSize = 0;
2029 }else if( pC->cacheStatus==p->cacheCtr ){
2030 payloadSize = pC->payloadSize;
2031 zRec = (char*)pC->aRow;
2032 }else if( pC->isIndex ){
2033 i64 payloadSize64;
2034 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2035 payloadSize = payloadSize64;
2036 }else{
2037 sqlite3BtreeDataSize(pCrsr, &payloadSize);
2038 }
2039 nField = pC->nField;
2040 }else if( pC->pseudoTable ){
2041 /* The record is the sole entry of a pseudo-table */
2042 payloadSize = pC->nData;
2043 zRec = pC->pData;
2044 pC->cacheStatus = CACHE_STALE;
2045 assert( payloadSize==0 || zRec!=0 );
2046 nField = pC->nField;
2047 pCrsr = 0;
2048 }else{
2049 zRec = 0;
2050 payloadSize = 0;
2051 pCrsr = 0;
2052 nField = 0;
2053 }
2054
2055 /* If payloadSize is 0, then just push a NULL onto the stack. */
2056 if( payloadSize==0 ){
2057 assert( pTos->flags==MEM_Null );
2058 break;
2059 }
2060 if( payloadSize>SQLITE_MAX_LENGTH ){
2061 goto too_big;
2062 }
2063
2064 assert( p2<nField );
2065
2066 /* Read and parse the table header. Store the results of the parse
2067 ** into the record header cache fields of the cursor.
2068 */
2069 if( pC && pC->cacheStatus==p->cacheCtr ){
2070 aType = pC->aType;
2071 aOffset = pC->aOffset;
2072 }else{
2073 u8 *zIdx; /* Index into header */
2074 u8 *zEndHdr; /* Pointer to first byte after the header */
2075 u32 offset; /* Offset into the data */
2076 int szHdrSz; /* Size of the header size field at start of record */
2077 int avail; /* Number of bytes of available data */
2078
2079 aType = pC->aType;
2080 if( aType==0 ){
2081 pC->aType = aType = sqlite3DbMallocRaw(db, 2*nField*sizeof(aType) );
2082 }
2083 if( aType==0 ){
2084 goto no_mem;
2085 }
2086 pC->aOffset = aOffset = &aType[nField];
2087 pC->payloadSize = payloadSize;
2088 pC->cacheStatus = p->cacheCtr;
2089
2090 /* Figure out how many bytes are in the header */
2091 if( zRec ){
2092 zData = zRec;
2093 }else{
2094 if( pC->isIndex ){
2095 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
2096 }else{
2097 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
2098 }
2099 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2100 ** save the payload in the pC->aRow cache. That will save us from
2101 ** having to make additional calls to fetch the content portion of
2102 ** the record.
2103 */
2104 if( avail>=payloadSize ){
2105 zRec = zData;
2106 pC->aRow = (u8*)zData;
2107 }else{
2108 pC->aRow = 0;
2109 }
2110 }
2111 /* The following assert is true in all cases accept when
2112 ** the database file has been corrupted externally.
2113 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
2114 szHdrSz = GetVarint((u8*)zData, offset);
2115
2116 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2117 ** record header in most cases. But they will fail to get the complete
2118 ** record header if the record header does not fit on a single page
2119 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2120 ** acquire the complete header text.
2121 */
2122 if( !zRec && avail<offset ){
2123 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
2124 if( rc!=SQLITE_OK ){
2125 goto op_column_out;
2126 }
2127 zData = sMem.z;
2128 }
2129 zEndHdr = (u8 *)&zData[offset];
2130 zIdx = (u8 *)&zData[szHdrSz];
2131
2132 /* Scan the header and use it to fill in the aType[] and aOffset[]
2133 ** arrays. aType[i] will contain the type integer for the i-th
2134 ** column and aOffset[i] will contain the offset from the beginning
2135 ** of the record to the start of the data for the i-th column
2136 */
2137 for(i=0; i<nField; i++){
2138 if( zIdx<zEndHdr ){
2139 aOffset[i] = offset;
2140 zIdx += GetVarint(zIdx, aType[i]);
2141 offset += sqlite3VdbeSerialTypeLen(aType[i]);
2142 }else{
2143 /* If i is less that nField, then there are less fields in this
2144 ** record than SetNumColumns indicated there are columns in the
2145 ** table. Set the offset for any extra columns not present in
2146 ** the record to 0. This tells code below to push a NULL onto the
2147 ** stack instead of deserializing a value from the record.
2148 */
2149 aOffset[i] = 0;
2150 }
2151 }
2152 Release(&sMem);
2153 sMem.flags = MEM_Null;
2154
2155 /* If we have read more header data than was contained in the header,
2156 ** or if the end of the last field appears to be past the end of the
2157 ** record, then we must be dealing with a corrupt database.
2158 */
2159 if( zIdx>zEndHdr || offset>payloadSize ){
2160 rc = SQLITE_CORRUPT_BKPT;
2161 goto op_column_out;
2162 }
2163 }
2164
2165 /* Get the column information. If aOffset[p2] is non-zero, then
2166 ** deserialize the value from the record. If aOffset[p2] is zero,
2167 ** then there are not enough fields in the record to satisfy the
2168 ** request. In this case, set the value NULL or to P3 if P3 is
2169 ** a pointer to a Mem object.
2170 */
2171 if( aOffset[p2] ){
2172 assert( rc==SQLITE_OK );
2173 if( zRec ){
2174 zData = &zRec[aOffset[p2]];
2175 }else{
2176 len = sqlite3VdbeSerialTypeLen(aType[p2]);
2177 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
2178 if( rc!=SQLITE_OK ){
2179 goto op_column_out;
2180 }
2181 zData = sMem.z;
2182 }
2183 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos);
2184 pTos->enc = encoding;
2185 }else{
2186 if( pOp->p3type==P3_MEM ){
2187 sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
2188 }else{
2189 pTos->flags = MEM_Null;
2190 }
2191 }
2192
2193 /* If we dynamically allocated space to hold the data (in the
2194 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
2195 ** dynamically allocated space over to the pTos structure.
2196 ** This prevents a memory copy.
2197 */
2198 if( (sMem.flags & MEM_Dyn)!=0 ){
2199 assert( pTos->flags & MEM_Ephem );
2200 assert( pTos->flags & (MEM_Str|MEM_Blob) );
2201 assert( pTos->z==sMem.z );
2202 assert( sMem.flags & MEM_Term );
2203 pTos->flags &= ~MEM_Ephem;
2204 pTos->flags |= MEM_Dyn|MEM_Term;
2205 }
2206
2207 /* pTos->z might be pointing to sMem.zShort[]. Fix that so that we
2208 ** can abandon sMem */
2209 rc = sqlite3VdbeMemMakeWriteable(pTos);
2210
2211op_column_out:
2212 break;
2213}
2214
2215/* Opcode: MakeRecord P1 P2 P3
2216**
2217** Convert the top abs(P1) entries of the stack into a single entry
2218** suitable for use as a data record in a database table or as a key
2219** in an index. The details of the format are irrelavant as long as
2220** the OP_Column opcode can decode the record later and as long as the
2221** sqlite3VdbeRecordCompare function will correctly compare two encoded
2222** records. Refer to source code comments for the details of the record
2223** format.
2224**
2225** The original stack entries are popped from the stack if P1>0 but
2226** remain on the stack if P1<0.
2227**
2228** If P2 is not zero and one or more of the entries are NULL, then jump
2229** to the address given by P2. This feature can be used to skip a
2230** uniqueness test on indices.
2231**
2232** P3 may be a string that is P1 characters long. The nth character of the
2233** string indicates the column affinity that should be used for the nth
2234** field of the index key (i.e. the first character of P3 corresponds to the
2235** lowest element on the stack).
2236**
2237** The mapping from character to affinity is given by the SQLITE_AFF_
2238** macros defined in sqliteInt.h.
2239**
2240** If P3 is NULL then all index fields have the affinity NONE.
2241**
2242** See also OP_MakeIdxRec
2243*/
2244/* Opcode: MakeIdxRec P1 P2 P3
2245**
2246** This opcode works just OP_MakeRecord except that it reads an extra
2247** integer from the stack (thus reading a total of abs(P1+1) entries)
2248** and appends that extra integer to the end of the record as a varint.
2249** This results in an index key.
2250*/
2251case OP_MakeIdxRec:
2252case OP_MakeRecord: {
2253 /* Assuming the record contains N fields, the record format looks
2254 ** like this:
2255 **
2256 ** ------------------------------------------------------------------------
2257 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2258 ** ------------------------------------------------------------------------
2259 **
2260 ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2261 ** the top of the stack.
2262 **
2263 ** Each type field is a varint representing the serial type of the
2264 ** corresponding data element (see sqlite3VdbeSerialType()). The
2265 ** hdr-size field is also a varint which is the offset from the beginning
2266 ** of the record to data0.
2267 */
2268 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2269 Mem *pRec; /* The new record */
2270 Mem *pRowid = 0; /* Rowid appended to the new record */
2271 u64 nData = 0; /* Number of bytes of data space */
2272 int nHdr = 0; /* Number of bytes of header space */
2273 u64 nByte = 0; /* Data space required for this record */
2274 int nZero = 0; /* Number of zero bytes at the end of the record */
2275 int nVarint; /* Number of bytes in a varint */
2276 u32 serial_type; /* Type field */
2277 int containsNull = 0; /* True if any of the data fields are NULL */
2278 Mem *pData0; /* Bottom of the stack */
2279 int leaveOnStack; /* If true, leave the entries on the stack */
2280 int nField; /* Number of fields in the record */
2281 int jumpIfNull; /* Jump here if non-zero and any entries are NULL. */
2282 int addRowid; /* True to append a rowid column at the end */
2283 char *zAffinity; /* The affinity string for the record */
2284 int file_format; /* File format to use for encoding */
2285 int i; /* Space used in zNewRecord[] */
2286 char zTemp[NBFS]; /* Space to hold small records */
2287
2288 leaveOnStack = ((pOp->p1<0)?1:0);
2289 nField = pOp->p1 * (leaveOnStack?-1:1);
2290 jumpIfNull = pOp->p2;
2291 addRowid = pOp->opcode==OP_MakeIdxRec;
2292 zAffinity = pOp->p3;
2293
2294 pData0 = &pTos[1-nField];
2295 assert( pData0>=p->aStack );
2296 containsNull = 0;
2297 file_format = p->minWriteFileFormat;
2298
2299 /* Loop through the elements that will make up the record to figure
2300 ** out how much space is required for the new record.
2301 */
2302 for(pRec=pData0; pRec<=pTos; pRec++){
2303 int len;
2304 if( zAffinity ){
2305 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2306 }
2307 if( pRec->flags&MEM_Null ){
2308 containsNull = 1;
2309 }
2310 if( pRec->flags&MEM_Zero && pRec->n>0 ){
2311 ExpandBlob(pRec);
2312 }
2313 serial_type = sqlite3VdbeSerialType(pRec, file_format);
2314 len = sqlite3VdbeSerialTypeLen(serial_type);
2315 nData += len;
2316 nHdr += sqlite3VarintLen(serial_type);
2317 if( pRec->flags & MEM_Zero ){
2318 /* Only pure zero-filled BLOBs can be input to this Opcode.
2319 ** We do not allow blobs with a prefix and a zero-filled tail. */
2320 nZero += pRec->u.i;
2321 }else if( len ){
2322 nZero = 0;
2323 }
2324 }
2325
2326 /* If we have to append a varint rowid to this record, set pRowid
2327 ** to the value of the rowid and increase nByte by the amount of space
2328 ** required to store it.
2329 */
2330 if( addRowid ){
2331 pRowid = &pTos[0-nField];
2332 assert( pRowid>=p->aStack );
2333 sqlite3VdbeMemIntegerify(pRowid);
2334 serial_type = sqlite3VdbeSerialType(pRowid, 0);
2335 nData += sqlite3VdbeSerialTypeLen(serial_type);
2336 nHdr += sqlite3VarintLen(serial_type);
2337 nZero = 0;
2338 }
2339
2340 /* Add the initial header varint and total the size */
2341 nHdr += nVarint = sqlite3VarintLen(nHdr);
2342 if( nVarint<sqlite3VarintLen(nHdr) ){
2343 nHdr++;
2344 }
2345 nByte = nHdr+nData-nZero;
2346 if( nByte>SQLITE_MAX_LENGTH ){
2347 goto too_big;
2348 }
2349
2350 /* Allocate space for the new record. */
2351 if( nByte>sizeof(zTemp) ){
2352 zNewRecord = sqlite3DbMallocRaw(db, nByte);
2353 if( !zNewRecord ){
2354 goto no_mem;
2355 }
2356 }else{
2357 zNewRecord = (u8*)zTemp;
2358 }
2359
2360 /* Write the record */
2361 i = sqlite3PutVarint(zNewRecord, nHdr);
2362 for(pRec=pData0; pRec<=pTos; pRec++){
2363 serial_type = sqlite3VdbeSerialType(pRec, file_format);
2364 i += sqlite3PutVarint(&zNewRecord[i], serial_type); /* serial type */
2365 }
2366 if( addRowid ){
2367 i += sqlite3PutVarint(&zNewRecord[i], sqlite3VdbeSerialType(pRowid, 0));
2368 }
2369 for(pRec=pData0; pRec<=pTos; pRec++){ /* serial data */
2370 i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format);
2371 }
2372 if( addRowid ){
2373 i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRowid, 0);
2374 }
2375 assert( i==nByte );
2376
2377 /* Pop entries off the stack if required. Push the new record on. */
2378 if( !leaveOnStack ){
2379 popStack(&pTos, nField+addRowid);
2380 }
2381 pTos++;
2382 pTos->n = nByte;
2383 if( nByte<=sizeof(zTemp) ){
2384 assert( zNewRecord==(unsigned char *)zTemp );
2385 pTos->z = pTos->zShort;
2386 memcpy(pTos->zShort, zTemp, nByte);
2387 pTos->flags = MEM_Blob | MEM_Short;
2388 }else{
2389 assert( zNewRecord!=(unsigned char *)zTemp );
2390 pTos->z = (char*)zNewRecord;
2391 pTos->flags = MEM_Blob | MEM_Dyn;
2392 pTos->xDel = 0;
2393 }
2394 if( nZero ){
2395 pTos->u.i = nZero;
2396 pTos->flags |= MEM_Zero;
2397 }
2398 pTos->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
2399
2400 /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */
2401 if( jumpIfNull && containsNull ){
2402 pc = jumpIfNull - 1;
2403 }
2404 break;
2405}
2406
2407/* Opcode: Statement P1 * *
2408**
2409** Begin an individual statement transaction which is part of a larger
2410** BEGIN..COMMIT transaction. This is needed so that the statement
2411** can be rolled back after an error without having to roll back the
2412** entire transaction. The statement transaction will automatically
2413** commit when the VDBE halts.
2414**
2415** The statement is begun on the database file with index P1. The main
2416** database file has an index of 0 and the file used for temporary tables
2417** has an index of 1.
2418*/
2419case OP_Statement: { /* no-push */
2420 int i = pOp->p1;
2421 Btree *pBt;
2422 if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){
2423 assert( sqlite3BtreeIsInTrans(pBt) );
2424 assert( (p->btreeMask & (1<<i))!=0 );
2425 if( !sqlite3BtreeIsInStmt(pBt) ){
2426 rc = sqlite3BtreeBeginStmt(pBt);
2427 p->openedStatement = 1;
2428 }
2429 }
2430 break;
2431}
2432
2433/* Opcode: AutoCommit P1 P2 *
2434**
2435** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
2436** back any currently active btree transactions. If there are any active
2437** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
2438**
2439** This instruction causes the VM to halt.
2440*/
2441case OP_AutoCommit: { /* no-push */
2442 u8 i = pOp->p1;
2443 u8 rollback = pOp->p2;
2444
2445 assert( i==1 || i==0 );
2446 assert( i==1 || rollback==0 );
2447
2448 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
2449
2450 if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
2451 /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
2452 ** still running, and a transaction is active, return an error indicating
2453 ** that the other VMs must complete first.
2454 */
2455 sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit",
2456 " transaction - SQL statements in progress", (char*)0);
2457 rc = SQLITE_ERROR;
2458 }else if( i!=db->autoCommit ){
2459 if( pOp->p2 ){
2460 assert( i==1 );
2461 sqlite3RollbackAll(db);
2462 db->autoCommit = 1;
2463 }else{
2464 db->autoCommit = i;
2465 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2466 p->pTos = pTos;
2467 p->pc = pc;
2468 db->autoCommit = 1-i;
2469 p->rc = rc = SQLITE_BUSY;
2470 goto vdbe_return;
2471 }
2472 }
2473 if( p->rc==SQLITE_OK ){
2474 rc = SQLITE_DONE;
2475 }else{
2476 rc = SQLITE_ERROR;
2477 }
2478 goto vdbe_return;
2479 }else{
2480 sqlite3SetString(&p->zErrMsg,
2481 (!i)?"cannot start a transaction within a transaction":(
2482 (rollback)?"cannot rollback - no transaction is active":
2483 "cannot commit - no transaction is active"), (char*)0);
2484
2485 rc = SQLITE_ERROR;
2486 }
2487 break;
2488}
2489
2490/* Opcode: Transaction P1 P2 *
2491**
2492** Begin a transaction. The transaction ends when a Commit or Rollback
2493** opcode is encountered. Depending on the ON CONFLICT setting, the
2494** transaction might also be rolled back if an error is encountered.
2495**
2496** P1 is the index of the database file on which the transaction is
2497** started. Index 0 is the main database file and index 1 is the
2498** file used for temporary tables.
2499**
2500** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
2501** obtained on the database file when a write-transaction is started. No
2502** other process can start another write transaction while this transaction is
2503** underway. Starting a write transaction also creates a rollback journal. A
2504** write transaction must be started before any changes can be made to the
2505** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2506** on the file.
2507**
2508** If P2 is zero, then a read-lock is obtained on the database file.
2509*/
2510case OP_Transaction: { /* no-push */
2511 int i = pOp->p1;
2512 Btree *pBt;
2513
2514 assert( i>=0 && i<db->nDb );
2515 assert( (p->btreeMask & (1<<i))!=0 );
2516 pBt = db->aDb[i].pBt;
2517
2518 if( pBt ){
2519 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
2520 if( rc==SQLITE_BUSY ){
2521 p->pc = pc;
2522 p->rc = rc = SQLITE_BUSY;
2523 p->pTos = pTos;
2524 goto vdbe_return;
2525 }
2526 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
2527 goto abort_due_to_error;
2528 }
2529 }
2530 break;
2531}
2532
2533/* Opcode: ReadCookie P1 P2 *
2534**
2535** Read cookie number P2 from database P1 and push it onto the stack.
2536** P2==0 is the schema version. P2==1 is the database format.
2537** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2538** the main database file and P1==1 is the database file used to store
2539** temporary tables.
2540**
2541** If P1 is negative, then this is a request to read the size of a
2542** databases free-list. P2 must be set to 1 in this case. The actual
2543** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
2544** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
2545**
2546** There must be a read-lock on the database (either a transaction
2547** must be started or there must be an open cursor) before
2548** executing this instruction.
2549*/
2550case OP_ReadCookie: {
2551 int iMeta;
2552 int iDb = pOp->p1;
2553 int iCookie = pOp->p2;
2554
2555 assert( pOp->p2<SQLITE_N_BTREE_META );
2556 if( iDb<0 ){
2557 iDb = (-1*(iDb+1));
2558 iCookie *= -1;
2559 }
2560 assert( iDb>=0 && iDb<db->nDb );
2561 assert( db->aDb[iDb].pBt!=0 );
2562 assert( (p->btreeMask & (1<<iDb))!=0 );
2563 /* The indexing of meta values at the schema layer is off by one from
2564 ** the indexing in the btree layer. The btree considers meta[0] to
2565 ** be the number of free pages in the database (a read-only value)
2566 ** and meta[1] to be the schema cookie. The schema layer considers
2567 ** meta[1] to be the schema cookie. So we have to shift the index
2568 ** by one in the following statement.
2569 */
2570 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
2571 pTos++;
2572 pTos->u.i = iMeta;
2573 pTos->flags = MEM_Int;
2574 break;
2575}
2576
2577/* Opcode: SetCookie P1 P2 *
2578**
2579** Write the top of the stack into cookie number P2 of database P1.
2580** P2==0 is the schema version. P2==1 is the database format.
2581** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2582** the main database file and P1==1 is the database file used to store
2583** temporary tables.
2584**
2585** A transaction must be started before executing this opcode.
2586*/
2587case OP_SetCookie: { /* no-push */
2588 Db *pDb;
2589 assert( pOp->p2<SQLITE_N_BTREE_META );
2590 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2591 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
2592 pDb = &db->aDb[pOp->p1];
2593 assert( pDb->pBt!=0 );
2594 assert( pTos>=p->aStack );
2595 sqlite3VdbeMemIntegerify(pTos);
2596 /* See note about index shifting on OP_ReadCookie */
2597 rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->u.i);
2598 if( pOp->p2==0 ){
2599 /* When the schema cookie changes, record the new cookie internally */
2600 pDb->pSchema->schema_cookie = pTos->u.i;
2601 db->flags |= SQLITE_InternChanges;
2602 }else if( pOp->p2==1 ){
2603 /* Record changes in the file format */
2604 pDb->pSchema->file_format = pTos->u.i;
2605 }
2606 assert( (pTos->flags & MEM_Dyn)==0 );
2607 pTos--;
2608 if( pOp->p1==1 ){
2609 /* Invalidate all prepared statements whenever the TEMP database
2610 ** schema is changed. Ticket #1644 */
2611 sqlite3ExpirePreparedStatements(db);
2612 }
2613 break;
2614}
2615
2616/* Opcode: VerifyCookie P1 P2 *
2617**
2618** Check the value of global database parameter number 0 (the
2619** schema version) and make sure it is equal to P2.
2620** P1 is the database number which is 0 for the main database file
2621** and 1 for the file holding temporary tables and some higher number
2622** for auxiliary databases.
2623**
2624** The cookie changes its value whenever the database schema changes.
2625** This operation is used to detect when that the cookie has changed
2626** and that the current process needs to reread the schema.
2627**
2628** Either a transaction needs to have been started or an OP_Open needs
2629** to be executed (to establish a read lock) before this opcode is
2630** invoked.
2631*/
2632case OP_VerifyCookie: { /* no-push */
2633 int iMeta;
2634 Btree *pBt;
2635 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2636 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
2637 pBt = db->aDb[pOp->p1].pBt;
2638 if( pBt ){
2639 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2640 }else{
2641 rc = SQLITE_OK;
2642 iMeta = 0;
2643 }
2644 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
2645 sqlite3_free(p->zErrMsg);
2646 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
2647 /* If the schema-cookie from the database file matches the cookie
2648 ** stored with the in-memory representation of the schema, do
2649 ** not reload the schema from the database file.
2650 **
2651 ** If virtual-tables are in use, this is not just an optimisation.
2652 ** Often, v-tables store their data in other SQLite tables, which
2653 ** are queried from within xNext() and other v-table methods using
2654 ** prepared queries. If such a query is out-of-date, we do not want to
2655 ** discard the database schema, as the user code implementing the
2656 ** v-table would have to be ready for the sqlite3_vtab structure itself
2657 ** to be invalidated whenever sqlite3_step() is called from within
2658 ** a v-table method.
2659 */
2660 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2661 sqlite3ResetInternalSchema(db, pOp->p1);
2662 }
2663
2664 sqlite3ExpirePreparedStatements(db);
2665 rc = SQLITE_SCHEMA;
2666 }
2667 break;
2668}
2669
2670/* Opcode: OpenRead P1 P2 P3
2671**
2672** Open a read-only cursor for the database table whose root page is
2673** P2 in a database file. The database file is determined by an
2674** integer from the top of the stack. 0 means the main database and
2675** 1 means the database used for temporary tables. Give the new
2676** cursor an identifier of P1. The P1 values need not be contiguous
2677** but all P1 values should be small integers. It is an error for
2678** P1 to be negative.
2679**
2680** If P2==0 then take the root page number from the next of the stack.
2681**
2682** There will be a read lock on the database whenever there is an
2683** open cursor. If the database was unlocked prior to this instruction
2684** then a read lock is acquired as part of this instruction. A read
2685** lock allows other processes to read the database but prohibits
2686** any other process from modifying the database. The read lock is
2687** released when all cursors are closed. If this instruction attempts
2688** to get a read lock but fails, the script terminates with an
2689** SQLITE_BUSY error code.
2690**
2691** The P3 value is a pointer to a KeyInfo structure that defines the
2692** content and collating sequence of indices. P3 is NULL for cursors
2693** that are not pointing to indices.
2694**
2695** See also OpenWrite.
2696*/
2697/* Opcode: OpenWrite P1 P2 P3
2698**
2699** Open a read/write cursor named P1 on the table or index whose root
2700** page is P2. If P2==0 then take the root page number from the stack.
2701**
2702** The P3 value is a pointer to a KeyInfo structure that defines the
2703** content and collating sequence of indices. P3 is NULL for cursors
2704** that are not pointing to indices.
2705**
2706** This instruction works just like OpenRead except that it opens the cursor
2707** in read/write mode. For a given table, there can be one or more read-only
2708** cursors or a single read/write cursor but not both.
2709**
2710** See also OpenRead.
2711*/
2712case OP_OpenRead: /* no-push */
2713case OP_OpenWrite: { /* no-push */
2714 int i = pOp->p1;
2715 int p2 = pOp->p2;
2716 int wrFlag;
2717 Btree *pX;
2718 int iDb;
2719 Cursor *pCur;
2720 Db *pDb;
2721
2722 assert( pTos>=p->aStack );
2723 sqlite3VdbeMemIntegerify(pTos);
2724 iDb = pTos->u.i;
2725 assert( (pTos->flags & MEM_Dyn)==0 );
2726 pTos--;
2727 assert( iDb>=0 && iDb<db->nDb );
2728 assert( (p->btreeMask & (1<<iDb))!=0 );
2729 pDb = &db->aDb[iDb];
2730 pX = pDb->pBt;
2731 assert( pX!=0 );
2732 if( pOp->opcode==OP_OpenWrite ){
2733 wrFlag = 1;
2734 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2735 p->minWriteFileFormat = pDb->pSchema->file_format;
2736 }
2737 }else{
2738 wrFlag = 0;
2739 }
2740 if( p2<=0 ){
2741 assert( pTos>=p->aStack );
2742 sqlite3VdbeMemIntegerify(pTos);
2743 p2 = pTos->u.i;
2744 assert( (pTos->flags & MEM_Dyn)==0 );
2745 pTos--;
2746 assert( p2>=2 );
2747 }
2748 assert( i>=0 );
2749 pCur = allocateCursor(p, i, iDb);
2750 if( pCur==0 ) goto no_mem;
2751 pCur->nullRow = 1;
2752 if( pX==0 ) break;
2753 /* We always provide a key comparison function. If the table being
2754 ** opened is of type INTKEY, the comparision function will be ignored. */
2755 rc = sqlite3BtreeCursor(pX, p2, wrFlag,
2756 sqlite3VdbeRecordCompare, pOp->p3,
2757 &pCur->pCursor);
2758 if( pOp->p3type==P3_KEYINFO ){
2759 pCur->pKeyInfo = (KeyInfo*)pOp->p3;
2760 pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
2761 pCur->pKeyInfo->enc = ENC(p->db);
2762 }else{
2763 pCur->pKeyInfo = 0;
2764 pCur->pIncrKey = &pCur->bogusIncrKey;
2765 }
2766 switch( rc ){
2767 case SQLITE_BUSY: {
2768 p->pc = pc;
2769 p->rc = rc = SQLITE_BUSY;
2770 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
2771 goto vdbe_return;
2772 }
2773 case SQLITE_OK: {
2774 int flags = sqlite3BtreeFlags(pCur->pCursor);
2775 /* Sanity checking. Only the lower four bits of the flags byte should
2776 ** be used. Bit 3 (mask 0x08) is unpreditable. The lower 3 bits
2777 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2778 ** 2 (zerodata for indices). If these conditions are not met it can
2779 ** only mean that we are dealing with a corrupt database file
2780 */
2781 if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
2782 rc = SQLITE_CORRUPT_BKPT;
2783 goto abort_due_to_error;
2784 }
2785 pCur->isTable = (flags & BTREE_INTKEY)!=0;
2786 pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
2787 /* If P3==0 it means we are expected to open a table. If P3!=0 then
2788 ** we expect to be opening an index. If this is not what happened,
2789 ** then the database is corrupt
2790 */
2791 if( (pCur->isTable && pOp->p3type==P3_KEYINFO)
2792 || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){
2793 rc = SQLITE_CORRUPT_BKPT;
2794 goto abort_due_to_error;
2795 }
2796 break;
2797 }
2798 case SQLITE_EMPTY: {
2799 pCur->isTable = pOp->p3type!=P3_KEYINFO;
2800 pCur->isIndex = !pCur->isTable;
2801 rc = SQLITE_OK;
2802 break;
2803 }
2804 default: {
2805 goto abort_due_to_error;
2806 }
2807 }
2808 break;
2809}
2810
2811/* Opcode: OpenEphemeral P1 P2 P3
2812**
2813** Open a new cursor P1 to a transient table.
2814** The cursor is always opened read/write even if
2815** the main database is read-only. The transient or virtual
2816** table is deleted automatically when the cursor is closed.
2817**
2818** P2 is the number of columns in the virtual table.
2819** The cursor points to a BTree table if P3==0 and to a BTree index
2820** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure
2821** that defines the format of keys in the index.
2822**
2823** This opcode was once called OpenTemp. But that created
2824** confusion because the term "temp table", might refer either
2825** to a TEMP table at the SQL level, or to a table opened by
2826** this opcode. Then this opcode was call OpenVirtual. But
2827** that created confusion with the whole virtual-table idea.
2828*/
2829case OP_OpenEphemeral: { /* no-push */
2830 int i = pOp->p1;
2831 Cursor *pCx;
2832 static const int openFlags =
2833 SQLITE_OPEN_READWRITE |
2834 SQLITE_OPEN_CREATE |
2835 SQLITE_OPEN_EXCLUSIVE |
2836 SQLITE_OPEN_DELETEONCLOSE |
2837 SQLITE_OPEN_TRANSIENT_DB;
2838
2839 assert( i>=0 );
2840 pCx = allocateCursor(p, i, -1);
2841 if( pCx==0 ) goto no_mem;
2842 pCx->nullRow = 1;
2843 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
2844 &pCx->pBt);
2845 if( rc==SQLITE_OK ){
2846 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
2847 }
2848 if( rc==SQLITE_OK ){
2849 /* If a transient index is required, create it by calling
2850 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2851 ** opening it. If a transient table is required, just use the
2852 ** automatically created table with root-page 1 (an INTKEY table).
2853 */
2854 if( pOp->p3 ){
2855 int pgno;
2856 assert( pOp->p3type==P3_KEYINFO );
2857 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
2858 if( rc==SQLITE_OK ){
2859 assert( pgno==MASTER_ROOT+1 );
2860 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare,
2861 pOp->p3, &pCx->pCursor);
2862 pCx->pKeyInfo = (KeyInfo*)pOp->p3;
2863 pCx->pKeyInfo->enc = ENC(p->db);
2864 pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
2865 }
2866 pCx->isTable = 0;
2867 }else{
2868 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
2869 pCx->isTable = 1;
2870 pCx->pIncrKey = &pCx->bogusIncrKey;
2871 }
2872 }
2873 pCx->nField = pOp->p2;
2874 pCx->isIndex = !pCx->isTable;
2875 break;
2876}
2877
2878/* Opcode: OpenPseudo P1 * *
2879**
2880** Open a new cursor that points to a fake table that contains a single
2881** row of data. Any attempt to write a second row of data causes the
2882** first row to be deleted. All data is deleted when the cursor is
2883** closed.
2884**
2885** A pseudo-table created by this opcode is useful for holding the
2886** NEW or OLD tables in a trigger. Also used to hold the a single
2887** row output from the sorter so that the row can be decomposed into
2888** individual columns using the OP_Column opcode.
2889*/
2890case OP_OpenPseudo: { /* no-push */
2891 int i = pOp->p1;
2892 Cursor *pCx;
2893 assert( i>=0 );
2894 pCx = allocateCursor(p, i, -1);
2895 if( pCx==0 ) goto no_mem;
2896 pCx->nullRow = 1;
2897 pCx->pseudoTable = 1;
2898 pCx->pIncrKey = &pCx->bogusIncrKey;
2899 pCx->isTable = 1;
2900 pCx->isIndex = 0;
2901 break;
2902}
2903
2904/* Opcode: Close P1 * *
2905**
2906** Close a cursor previously opened as P1. If P1 is not
2907** currently open, this instruction is a no-op.
2908*/
2909case OP_Close: { /* no-push */
2910 int i = pOp->p1;
2911 if( i>=0 && i<p->nCursor ){
2912 sqlite3VdbeFreeCursor(p, p->apCsr[i]);
2913 p->apCsr[i] = 0;
2914 }
2915 break;
2916}
2917
2918/* Opcode: MoveGe P1 P2 *
2919**
2920** Pop the top of the stack and use its value as a key. Reposition
2921** cursor P1 so that it points to the smallest entry that is greater
2922** than or equal to the key that was popped ffrom the stack.
2923** If there are no records greater than or equal to the key and P2
2924** is not zero, then jump to P2.
2925**
2926** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2927*/
2928/* Opcode: MoveGt P1 P2 *
2929**
2930** Pop the top of the stack and use its value as a key. Reposition
2931** cursor P1 so that it points to the smallest entry that is greater
2932** than the key from the stack.
2933** If there are no records greater than the key and P2 is not zero,
2934** then jump to P2.
2935**
2936** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
2937*/
2938/* Opcode: MoveLt P1 P2 *
2939**
2940** Pop the top of the stack and use its value as a key. Reposition
2941** cursor P1 so that it points to the largest entry that is less
2942** than the key from the stack.
2943** If there are no records less than the key and P2 is not zero,
2944** then jump to P2.
2945**
2946** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2947*/
2948/* Opcode: MoveLe P1 P2 *
2949**
2950** Pop the top of the stack and use its value as a key. Reposition
2951** cursor P1 so that it points to the largest entry that is less than
2952** or equal to the key that was popped from the stack.
2953** If there are no records less than or eqal to the key and P2 is not zero,
2954** then jump to P2.
2955**
2956** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
2957*/
2958case OP_MoveLt: /* no-push */
2959case OP_MoveLe: /* no-push */
2960case OP_MoveGe: /* no-push */
2961case OP_MoveGt: { /* no-push */
2962 int i = pOp->p1;
2963 Cursor *pC;
2964
2965 assert( pTos>=p->aStack );
2966 assert( i>=0 && i<p->nCursor );
2967 pC = p->apCsr[i];
2968 assert( pC!=0 );
2969 if( pC->pCursor!=0 ){
2970 int res, oc;
2971 oc = pOp->opcode;
2972 pC->nullRow = 0;
2973 *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
2974 if( pC->isTable ){
2975 i64 iKey;
2976 sqlite3VdbeMemIntegerify(pTos);
2977 iKey = intToKey(pTos->u.i);
2978 if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
2979 pC->movetoTarget = iKey;
2980 pC->deferredMoveto = 1;
2981 assert( (pTos->flags & MEM_Dyn)==0 );
2982 pTos--;
2983 break;
2984 }
2985 rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, 0, &res);
2986 if( rc!=SQLITE_OK ){
2987 goto abort_due_to_error;
2988 }
2989 pC->lastRowid = pTos->u.i;
2990 pC->rowidIsValid = res==0;
2991 }else{
2992 assert( pTos->flags & MEM_Blob );
2993 ExpandBlob(pTos);
2994 rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
2995 if( rc!=SQLITE_OK ){
2996 goto abort_due_to_error;
2997 }
2998 pC->rowidIsValid = 0;
2999 }
3000 pC->deferredMoveto = 0;
3001 pC->cacheStatus = CACHE_STALE;
3002 *pC->pIncrKey = 0;
3003#ifdef SQLITE_TEST
3004 sqlite3_search_count++;
3005#endif
3006 if( oc==OP_MoveGe || oc==OP_MoveGt ){
3007 if( res<0 ){
3008 rc = sqlite3BtreeNext(pC->pCursor, &res);
3009 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3010 pC->rowidIsValid = 0;
3011 }else{
3012 res = 0;
3013 }
3014 }else{
3015 assert( oc==OP_MoveLt || oc==OP_MoveLe );
3016 if( res>=0 ){
3017 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3018 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3019 pC->rowidIsValid = 0;
3020 }else{
3021 /* res might be negative because the table is empty. Check to
3022 ** see if this is the case.
3023 */
3024 res = sqlite3BtreeEof(pC->pCursor);
3025 }
3026 }
3027 if( res ){
3028 if( pOp->p2>0 ){
3029 pc = pOp->p2 - 1;
3030 }else{
3031 pC->nullRow = 1;
3032 }
3033 }
3034 }
3035 Release(pTos);
3036 pTos--;
3037 break;
3038}
3039
3040/* Opcode: Distinct P1 P2 *
3041**
3042** Use the top of the stack as a record created using MakeRecord. P1 is a
3043** cursor on a table that declared as an index. If that table contains an
3044** entry that matches the top of the stack fall thru. If the top of the stack
3045** matches no entry in P1 then jump to P2.
3046**
3047** The cursor is left pointing at the matching entry if it exists. The
3048** record on the top of the stack is not popped.
3049**
3050** This instruction is similar to NotFound except that this operation
3051** does not pop the key from the stack.
3052**
3053** The instruction is used to implement the DISTINCT operator on SELECT
3054** statements. The P1 table is not a true index but rather a record of
3055** all results that have produced so far.
3056**
3057** See also: Found, NotFound, MoveTo, IsUnique, NotExists
3058*/
3059/* Opcode: Found P1 P2 *
3060**
3061** Top of the stack holds a blob constructed by MakeRecord. P1 is an index.
3062** If an entry that matches the top of the stack exists in P1 then
3063** jump to P2. If the top of the stack does not match any entry in P1
3064** then fall thru. The P1 cursor is left pointing at the matching entry
3065** if it exists. The blob is popped off the top of the stack.
3066**
3067** This instruction is used to implement the IN operator where the
3068** left-hand side is a SELECT statement. P1 is not a true index but
3069** is instead a temporary index that holds the results of the SELECT
3070** statement. This instruction just checks to see if the left-hand side
3071** of the IN operator (stored on the top of the stack) exists in the
3072** result of the SELECT statement.
3073**
3074** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
3075*/
3076/* Opcode: NotFound P1 P2 *
3077**
3078** The top of the stack holds a blob constructed by MakeRecord. P1 is
3079** an index. If no entry exists in P1 that matches the blob then jump
3080** to P2. If an entry does existing, fall through. The cursor is left
3081** pointing to the entry that matches. The blob is popped from the stack.
3082**
3083** The difference between this operation and Distinct is that
3084** Distinct does not pop the key from the stack.
3085**
3086** See also: Distinct, Found, MoveTo, NotExists, IsUnique
3087*/
3088case OP_Distinct: /* no-push */
3089case OP_NotFound: /* no-push */
3090case OP_Found: { /* no-push */
3091 int i = pOp->p1;
3092 int alreadyExists = 0;
3093 Cursor *pC;
3094 assert( pTos>=p->aStack );
3095 assert( i>=0 && i<p->nCursor );
3096 assert( p->apCsr[i]!=0 );
3097 if( (pC = p->apCsr[i])->pCursor!=0 ){
3098 int res;
3099 assert( pC->isTable==0 );
3100 assert( pTos->flags & MEM_Blob );
3101 Stringify(pTos, encoding);
3102 rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
3103 if( rc!=SQLITE_OK ){
3104 break;
3105 }
3106 alreadyExists = (res==0);
3107 pC->deferredMoveto = 0;
3108 pC->cacheStatus = CACHE_STALE;
3109 }
3110 if( pOp->opcode==OP_Found ){
3111 if( alreadyExists ) pc = pOp->p2 - 1;
3112 }else{
3113 if( !alreadyExists ) pc = pOp->p2 - 1;
3114 }
3115 if( pOp->opcode!=OP_Distinct ){
3116 Release(pTos);
3117 pTos--;
3118 }
3119 break;
3120}
3121
3122/* Opcode: IsUnique P1 P2 *
3123**
3124** The top of the stack is an integer record number. Call this
3125** record number R. The next on the stack is an index key created
3126** using MakeIdxRec. Call it K. This instruction pops R from the
3127** stack but it leaves K unchanged.
3128**
3129** P1 is an index. So it has no data and its key consists of a
3130** record generated by OP_MakeRecord where the last field is the
3131** rowid of the entry that the index refers to.
3132**
3133** This instruction asks if there is an entry in P1 where the
3134** fields matches K but the rowid is different from R.
3135** If there is no such entry, then there is an immediate
3136** jump to P2. If any entry does exist where the index string
3137** matches K but the record number is not R, then the record
3138** number for that entry is pushed onto the stack and control
3139** falls through to the next instruction.
3140**
3141** See also: Distinct, NotFound, NotExists, Found
3142*/
3143case OP_IsUnique: { /* no-push */
3144 int i = pOp->p1;
3145 Mem *pNos = &pTos[-1];
3146 Cursor *pCx;
3147 BtCursor *pCrsr;
3148 i64 R;
3149
3150 /* Pop the value R off the top of the stack
3151 */
3152 assert( pNos>=p->aStack );
3153 sqlite3VdbeMemIntegerify(pTos);
3154 R = pTos->u.i;
3155 assert( (pTos->flags & MEM_Dyn)==0 );
3156 pTos--;
3157 assert( i>=0 && i<p->nCursor );
3158 pCx = p->apCsr[i];
3159 assert( pCx!=0 );
3160 pCrsr = pCx->pCursor;
3161 if( pCrsr!=0 ){
3162 int res;
3163 i64 v; /* The record number on the P1 entry that matches K */
3164 char *zKey; /* The value of K */
3165 int nKey; /* Number of bytes in K */
3166 int len; /* Number of bytes in K without the rowid at the end */
3167 int szRowid; /* Size of the rowid column at the end of zKey */
3168
3169 /* Make sure K is a string and make zKey point to K
3170 */
3171 assert( pNos->flags & MEM_Blob );
3172 Stringify(pNos, encoding);
3173 zKey = pNos->z;
3174 nKey = pNos->n;
3175
3176 szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey);
3177 len = nKey-szRowid;
3178
3179 /* Search for an entry in P1 where all but the last four bytes match K.
3180 ** If there is no such entry, jump immediately to P2.
3181 */
3182 assert( pCx->deferredMoveto==0 );
3183 pCx->cacheStatus = CACHE_STALE;
3184 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, 0, &res);
3185 if( rc!=SQLITE_OK ){
3186 goto abort_due_to_error;
3187 }
3188 if( res<0 ){
3189 rc = sqlite3BtreeNext(pCrsr, &res);
3190 if( res ){
3191 pc = pOp->p2 - 1;
3192 break;
3193 }
3194 }
3195 rc = sqlite3VdbeIdxKeyCompare(pCx, len, (u8*)zKey, &res);
3196 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3197 if( res>0 ){
3198 pc = pOp->p2 - 1;
3199 break;
3200 }
3201
3202 /* At this point, pCrsr is pointing to an entry in P1 where all but
3203 ** the final entry (the rowid) matches K. Check to see if the
3204 ** final rowid column is different from R. If it equals R then jump
3205 ** immediately to P2.
3206 */
3207 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
3208 if( rc!=SQLITE_OK ){
3209 goto abort_due_to_error;
3210 }
3211 if( v==R ){
3212 pc = pOp->p2 - 1;
3213 break;
3214 }
3215
3216 /* The final varint of the key is different from R. Push it onto
3217 ** the stack. (The record number of an entry that violates a UNIQUE
3218 ** constraint.)
3219 */
3220 pTos++;
3221 pTos->u.i = v;
3222 pTos->flags = MEM_Int;
3223 }
3224 break;
3225}
3226
3227/* Opcode: NotExists P1 P2 *
3228**
3229** Use the top of the stack as a integer key. If a record with that key
3230** does not exist in table of P1, then jump to P2. If the record
3231** does exist, then fall thru. The cursor is left pointing to the
3232** record if it exists. The integer key is popped from the stack.
3233**
3234** The difference between this operation and NotFound is that this
3235** operation assumes the key is an integer and that P1 is a table whereas
3236** NotFound assumes key is a blob constructed from MakeRecord and
3237** P1 is an index.
3238**
3239** See also: Distinct, Found, MoveTo, NotFound, IsUnique
3240*/
3241case OP_NotExists: { /* no-push */
3242 int i = pOp->p1;
3243 Cursor *pC;
3244 BtCursor *pCrsr;
3245 assert( pTos>=p->aStack );
3246 assert( i>=0 && i<p->nCursor );
3247 assert( p->apCsr[i]!=0 );
3248 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3249 int res;
3250 u64 iKey;
3251 assert( pTos->flags & MEM_Int );
3252 assert( p->apCsr[i]->isTable );
3253 iKey = intToKey(pTos->u.i);
3254 rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, 0,&res);
3255 pC->lastRowid = pTos->u.i;
3256 pC->rowidIsValid = res==0;
3257 pC->nullRow = 0;
3258 pC->cacheStatus = CACHE_STALE;
3259 /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK
3260 ** processing is about to abort so we really do not care whether or not
3261 ** the following jump is taken. (In other words, do not stress over
3262 ** the error that valgrind sometimes shows on the next statement when
3263 ** running ioerr.test and similar failure-recovery test scripts.) */
3264 if( res!=0 ){
3265 pc = pOp->p2 - 1;
3266 pC->rowidIsValid = 0;
3267 }
3268 }
3269 Release(pTos);
3270 pTos--;
3271 break;
3272}
3273
3274/* Opcode: Sequence P1 * *
3275**
3276** Push an integer onto the stack which is the next available
3277** sequence number for cursor P1. The sequence number on the
3278** cursor is incremented after the push.
3279*/
3280case OP_Sequence: {
3281 int i = pOp->p1;
3282 assert( pTos>=p->aStack );
3283 assert( i>=0 && i<p->nCursor );
3284 assert( p->apCsr[i]!=0 );
3285 pTos++;
3286 pTos->u.i = p->apCsr[i]->seqCount++;
3287 pTos->flags = MEM_Int;
3288 break;
3289}
3290
3291
3292/* Opcode: NewRowid P1 P2 *
3293**
3294** Get a new integer record number (a.k.a "rowid") used as the key to a table.
3295** The record number is not previously used as a key in the database
3296** table that cursor P1 points to. The new record number is pushed
3297** onto the stack.
3298**
3299** If P2>0 then P2 is a memory cell that holds the largest previously
3300** generated record number. No new record numbers are allowed to be less
3301** than this value. When this value reaches its maximum, a SQLITE_FULL
3302** error is generated. The P2 memory cell is updated with the generated
3303** record number. This P2 mechanism is used to help implement the
3304** AUTOINCREMENT feature.
3305*/
3306case OP_NewRowid: {
3307 int i = pOp->p1;
3308 i64 v = 0;
3309 Cursor *pC;
3310 assert( i>=0 && i<p->nCursor );
3311 assert( p->apCsr[i]!=0 );
3312 if( (pC = p->apCsr[i])->pCursor==0 ){
3313 /* The zero initialization above is all that is needed */
3314 }else{
3315 /* The next rowid or record number (different terms for the same
3316 ** thing) is obtained in a two-step algorithm.
3317 **
3318 ** First we attempt to find the largest existing rowid and add one
3319 ** to that. But if the largest existing rowid is already the maximum
3320 ** positive integer, we have to fall through to the second
3321 ** probabilistic algorithm
3322 **
3323 ** The second algorithm is to select a rowid at random and see if
3324 ** it already exists in the table. If it does not exist, we have
3325 ** succeeded. If the random rowid does exist, we select a new one
3326 ** and try again, up to 1000 times.
3327 **
3328 ** For a table with less than 2 billion entries, the probability
3329 ** of not finding a unused rowid is about 1.0e-300. This is a
3330 ** non-zero probability, but it is still vanishingly small and should
3331 ** never cause a problem. You are much, much more likely to have a
3332 ** hardware failure than for this algorithm to fail.
3333 **
3334 ** The analysis in the previous paragraph assumes that you have a good
3335 ** source of random numbers. Is a library function like lrand48()
3336 ** good enough? Maybe. Maybe not. It's hard to know whether there
3337 ** might be subtle bugs is some implementations of lrand48() that
3338 ** could cause problems. To avoid uncertainty, SQLite uses its own
3339 ** random number generator based on the RC4 algorithm.
3340 **
3341 ** To promote locality of reference for repetitive inserts, the
3342 ** first few attempts at chosing a random rowid pick values just a little
3343 ** larger than the previous rowid. This has been shown experimentally
3344 ** to double the speed of the COPY operation.
3345 */
3346 int res, rx=SQLITE_OK, cnt;
3347 i64 x;
3348 cnt = 0;
3349 if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3350 BTREE_INTKEY ){
3351 rc = SQLITE_CORRUPT_BKPT;
3352 goto abort_due_to_error;
3353 }
3354 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3355 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
3356
3357#ifdef SQLITE_32BIT_ROWID
3358# define MAX_ROWID 0x7fffffff
3359#else
3360 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3361 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3362 ** to provide the constant while making all compilers happy.
3363 */
3364# define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
3365#endif
3366
3367 if( !pC->useRandomRowid ){
3368 if( pC->nextRowidValid ){
3369 v = pC->nextRowid;
3370 }else{
3371 rc = sqlite3BtreeLast(pC->pCursor, &res);
3372 if( rc!=SQLITE_OK ){
3373 goto abort_due_to_error;
3374 }
3375 if( res ){
3376 v = 1;
3377 }else{
3378 sqlite3BtreeKeySize(pC->pCursor, &v);
3379 v = keyToInt(v);
3380 if( v==MAX_ROWID ){
3381 pC->useRandomRowid = 1;
3382 }else{
3383 v++;
3384 }
3385 }
3386 }
3387
3388#ifndef SQLITE_OMIT_AUTOINCREMENT
3389 if( pOp->p2 ){
3390 Mem *pMem;
3391 assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */
3392 pMem = &p->aMem[pOp->p2];
3393 sqlite3VdbeMemIntegerify(pMem);
3394 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */
3395 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
3396 rc = SQLITE_FULL;
3397 goto abort_due_to_error;
3398 }
3399 if( v<pMem->u.i+1 ){
3400 v = pMem->u.i + 1;
3401 }
3402 pMem->u.i = v;
3403 }
3404#endif
3405
3406 if( v<MAX_ROWID ){
3407 pC->nextRowidValid = 1;
3408 pC->nextRowid = v+1;
3409 }else{
3410 pC->nextRowidValid = 0;
3411 }
3412 }
3413 if( pC->useRandomRowid ){
3414 assert( pOp->p2==0 ); /* SQLITE_FULL must have occurred prior to this */
3415 v = db->priorNewRowid;
3416 cnt = 0;
3417 do{
3418 if( v==0 || cnt>2 ){
3419 sqlite3Randomness(sizeof(v), &v);
3420 if( cnt<5 ) v &= 0xffffff;
3421 }else{
3422 unsigned char r;
3423 sqlite3Randomness(1, &r);
3424 v += r + 1;
3425 }
3426 if( v==0 ) continue;
3427 x = intToKey(v);
3428 rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, 0, &res);
3429 cnt++;
3430 }while( cnt<1000 && rx==SQLITE_OK && res==0 );
3431 db->priorNewRowid = v;
3432 if( rx==SQLITE_OK && res==0 ){
3433 rc = SQLITE_FULL;
3434 goto abort_due_to_error;
3435 }
3436 }
3437 pC->rowidIsValid = 0;
3438 pC->deferredMoveto = 0;
3439 pC->cacheStatus = CACHE_STALE;
3440 }
3441 pTos++;
3442 pTos->u.i = v;
3443 pTos->flags = MEM_Int;
3444 break;
3445}
3446
3447/* Opcode: Insert P1 P2 P3
3448**
3449** Write an entry into the table of cursor P1. A new entry is
3450** created if it doesn't already exist or the data for an existing
3451** entry is overwritten. The data is the value on the top of the
3452** stack. The key is the next value down on the stack. The key must
3453** be an integer. The stack is popped twice by this instruction.
3454**
3455** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3456** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P2 is set,
3457** then rowid is stored for subsequent return by the
3458** sqlite3_last_insert_rowid() function (otherwise it's unmodified).
3459**
3460** Parameter P3 may point to a string containing the table-name, or
3461** may be NULL. If it is not NULL, then the update-hook
3462** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3463**
3464** This instruction only works on tables. The equivalent instruction
3465** for indices is OP_IdxInsert.
3466*/
3467case OP_Insert: { /* no-push */
3468 Mem *pNos = &pTos[-1];
3469 int i = pOp->p1;
3470 Cursor *pC;
3471 assert( pNos>=p->aStack );
3472 assert( i>=0 && i<p->nCursor );
3473 assert( p->apCsr[i]!=0 );
3474 if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
3475 i64 iKey; /* The integer ROWID or key for the record to be inserted */
3476
3477 assert( pNos->flags & MEM_Int );
3478 assert( pC->isTable );
3479 iKey = intToKey(pNos->u.i);
3480
3481 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
3482 if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->u.i;
3483 if( pC->nextRowidValid && pNos->u.i>=pC->nextRowid ){
3484 pC->nextRowidValid = 0;
3485 }
3486 if( pTos->flags & MEM_Null ){
3487 pTos->z = 0;
3488 pTos->n = 0;
3489 }else{
3490 assert( pTos->flags & (MEM_Blob|MEM_Str) );
3491 }
3492 if( pC->pseudoTable ){
3493 sqlite3_free(pC->pData);
3494 pC->iKey = iKey;
3495 pC->nData = pTos->n;
3496 if( pTos->flags & MEM_Dyn ){
3497 pC->pData = pTos->z;
3498 pTos->flags = MEM_Null;
3499 }else{
3500 pC->pData = sqlite3_malloc( pC->nData+2 );
3501 if( !pC->pData ) goto no_mem;
3502 memcpy(pC->pData, pTos->z, pC->nData);
3503 pC->pData[pC->nData] = 0;
3504 pC->pData[pC->nData+1] = 0;
3505 }
3506 pC->nullRow = 0;
3507 }else{
3508 int nZero;
3509 if( pTos->flags & MEM_Zero ){
3510 nZero = pTos->u.i;
3511 }else{
3512 nZero = 0;
3513 }
3514 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3515 pTos->z, pTos->n, nZero,
3516 pOp->p2 & OPFLAG_APPEND);
3517 }
3518
3519 pC->rowidIsValid = 0;
3520 pC->deferredMoveto = 0;
3521 pC->cacheStatus = CACHE_STALE;
3522
3523 /* Invoke the update-hook if required. */
3524 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3525 const char *zDb = db->aDb[pC->iDb].zName;
3526 const char *zTbl = pOp->p3;
3527 int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3528 assert( pC->isTable );
3529 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3530 assert( pC->iDb>=0 );
3531 }
3532 }
3533 popStack(&pTos, 2);
3534
3535 break;
3536}
3537
3538/* Opcode: Delete P1 P2 P3
3539**
3540** Delete the record at which the P1 cursor is currently pointing.
3541**
3542** The cursor will be left pointing at either the next or the previous
3543** record in the table. If it is left pointing at the next record, then
3544** the next Next instruction will be a no-op. Hence it is OK to delete
3545** a record from within an Next loop.
3546**
3547** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3548** incremented (otherwise not).
3549**
3550** If P1 is a pseudo-table, then this instruction is a no-op.
3551*/
3552case OP_Delete: { /* no-push */
3553 int i = pOp->p1;
3554 Cursor *pC;
3555 assert( i>=0 && i<p->nCursor );
3556 pC = p->apCsr[i];
3557 assert( pC!=0 );
3558 if( pC->pCursor!=0 ){
3559 i64 iKey;
3560
3561 /* If the update-hook will be invoked, set iKey to the rowid of the
3562 ** row being deleted.
3563 */
3564 if( db->xUpdateCallback && pOp->p3 ){
3565 assert( pC->isTable );
3566 if( pC->rowidIsValid ){
3567 iKey = pC->lastRowid;
3568 }else{
3569 rc = sqlite3BtreeKeySize(pC->pCursor, &iKey);
3570 if( rc ){
3571 goto abort_due_to_error;
3572 }
3573 iKey = keyToInt(iKey);
3574 }
3575 }
3576
3577 rc = sqlite3VdbeCursorMoveto(pC);
3578 if( rc ) goto abort_due_to_error;
3579 rc = sqlite3BtreeDelete(pC->pCursor);
3580 pC->nextRowidValid = 0;
3581 pC->cacheStatus = CACHE_STALE;
3582
3583 /* Invoke the update-hook if required. */
3584 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3585 const char *zDb = db->aDb[pC->iDb].zName;
3586 const char *zTbl = pOp->p3;
3587 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3588 assert( pC->iDb>=0 );
3589 }
3590 }
3591 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
3592 break;
3593}
3594
3595/* Opcode: ResetCount P1 * *
3596**
3597** This opcode resets the VMs internal change counter to 0. If P1 is true,
3598** then the value of the change counter is copied to the database handle
3599** change counter (returned by subsequent calls to sqlite3_changes())
3600** before it is reset. This is used by trigger programs.
3601*/
3602case OP_ResetCount: { /* no-push */
3603 if( pOp->p1 ){
3604 sqlite3VdbeSetChanges(db, p->nChange);
3605 }
3606 p->nChange = 0;
3607 break;
3608}
3609
3610/* Opcode: RowData P1 * *
3611**
3612** Push onto the stack the complete row data for cursor P1.
3613** There is no interpretation of the data. It is just copied
3614** onto the stack exactly as it is found in the database file.
3615**
3616** If the cursor is not pointing to a valid row, a NULL is pushed
3617** onto the stack.
3618*/
3619/* Opcode: RowKey P1 * *
3620**
3621** Push onto the stack the complete row key for cursor P1.
3622** There is no interpretation of the key. It is just copied
3623** onto the stack exactly as it is found in the database file.
3624**
3625** If the cursor is not pointing to a valid row, a NULL is pushed
3626** onto the stack.
3627*/
3628case OP_RowKey:
3629case OP_RowData: {
3630 int i = pOp->p1;
3631 Cursor *pC;
3632 u32 n;
3633
3634 /* Note that RowKey and RowData are really exactly the same instruction */
3635 pTos++;
3636 assert( i>=0 && i<p->nCursor );
3637 pC = p->apCsr[i];
3638 assert( pC->isTable || pOp->opcode==OP_RowKey );
3639 assert( pC->isIndex || pOp->opcode==OP_RowData );
3640 assert( pC!=0 );
3641 if( pC->nullRow ){
3642 pTos->flags = MEM_Null;
3643 }else if( pC->pCursor!=0 ){
3644 BtCursor *pCrsr = pC->pCursor;
3645 rc = sqlite3VdbeCursorMoveto(pC);
3646 if( rc ) goto abort_due_to_error;
3647 if( pC->nullRow ){
3648 pTos->flags = MEM_Null;
3649 break;
3650 }else if( pC->isIndex ){
3651 i64 n64;
3652 assert( !pC->isTable );
3653 sqlite3BtreeKeySize(pCrsr, &n64);
3654 if( n64>SQLITE_MAX_LENGTH ){
3655 goto too_big;
3656 }
3657 n = n64;
3658 }else{
3659 sqlite3BtreeDataSize(pCrsr, &n);
3660 }
3661 if( n>SQLITE_MAX_LENGTH ){
3662 goto too_big;
3663 }
3664 pTos->n = n;
3665 if( n<=NBFS ){
3666 pTos->flags = MEM_Blob | MEM_Short;
3667 pTos->z = pTos->zShort;
3668 }else{
3669 char *z = sqlite3_malloc( n );
3670 if( z==0 ) goto no_mem;
3671 pTos->flags = MEM_Blob | MEM_Dyn;
3672 pTos->xDel = 0;
3673 pTos->z = z;
3674 }
3675 if( pC->isIndex ){
3676 rc = sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
3677 }else{
3678 rc = sqlite3BtreeData(pCrsr, 0, n, pTos->z);
3679 }
3680 }else if( pC->pseudoTable ){
3681 pTos->n = pC->nData;
3682 assert( pC->nData<=SQLITE_MAX_LENGTH );
3683 pTos->z = pC->pData;
3684 pTos->flags = MEM_Blob|MEM_Ephem;
3685 }else{
3686 pTos->flags = MEM_Null;
3687 }
3688 pTos->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
3689 break;
3690}
3691
3692/* Opcode: Rowid P1 * *
3693**
3694** Push onto the stack an integer which is the key of the table entry that
3695** P1 is currently point to.
3696*/
3697case OP_Rowid: {
3698 int i = pOp->p1;
3699 Cursor *pC;
3700 i64 v;
3701
3702 assert( i>=0 && i<p->nCursor );
3703 pC = p->apCsr[i];
3704 assert( pC!=0 );
3705 rc = sqlite3VdbeCursorMoveto(pC);
3706 if( rc ) goto abort_due_to_error;
3707 pTos++;
3708 if( pC->rowidIsValid ){
3709 v = pC->lastRowid;
3710 }else if( pC->pseudoTable ){
3711 v = keyToInt(pC->iKey);
3712 }else if( pC->nullRow || pC->pCursor==0 ){
3713 pTos->flags = MEM_Null;
3714 break;
3715 }else{
3716 assert( pC->pCursor!=0 );
3717 sqlite3BtreeKeySize(pC->pCursor, &v);
3718 v = keyToInt(v);
3719 }
3720 pTos->u.i = v;
3721 pTos->flags = MEM_Int;
3722 break;
3723}
3724
3725/* Opcode: NullRow P1 * *
3726**
3727** Move the cursor P1 to a null row. Any OP_Column operations
3728** that occur while the cursor is on the null row will always push
3729** a NULL onto the stack.
3730*/
3731case OP_NullRow: { /* no-push */
3732 int i = pOp->p1;
3733 Cursor *pC;
3734
3735 assert( i>=0 && i<p->nCursor );
3736 pC = p->apCsr[i];
3737 assert( pC!=0 );
3738 pC->nullRow = 1;
3739 pC->rowidIsValid = 0;
3740 break;
3741}
3742
3743/* Opcode: Last P1 P2 *
3744**
3745** The next use of the Rowid or Column or Next instruction for P1
3746** will refer to the last entry in the database table or index.
3747** If the table or index is empty and P2>0, then jump immediately to P2.
3748** If P2 is 0 or if the table or index is not empty, fall through
3749** to the following instruction.
3750*/
3751case OP_Last: { /* no-push */
3752 int i = pOp->p1;
3753 Cursor *pC;
3754 BtCursor *pCrsr;
3755
3756 assert( i>=0 && i<p->nCursor );
3757 pC = p->apCsr[i];
3758 assert( pC!=0 );
3759 if( (pCrsr = pC->pCursor)!=0 ){
3760 int res;
3761 rc = sqlite3BtreeLast(pCrsr, &res);
3762 pC->nullRow = res;
3763 pC->deferredMoveto = 0;
3764 pC->cacheStatus = CACHE_STALE;
3765 if( res && pOp->p2>0 ){
3766 pc = pOp->p2 - 1;
3767 }
3768 }else{
3769 pC->nullRow = 0;
3770 }
3771 break;
3772}
3773
3774
3775/* Opcode: Sort P1 P2 *
3776**
3777** This opcode does exactly the same thing as OP_Rewind except that
3778** it increments an undocumented global variable used for testing.
3779**
3780** Sorting is accomplished by writing records into a sorting index,
3781** then rewinding that index and playing it back from beginning to
3782** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3783** rewinding so that the global variable will be incremented and
3784** regression tests can determine whether or not the optimizer is
3785** correctly optimizing out sorts.
3786*/
3787case OP_Sort: { /* no-push */
3788#ifdef SQLITE_TEST
3789 sqlite3_sort_count++;
3790 sqlite3_search_count--;
3791#endif
3792 /* Fall through into OP_Rewind */
3793}
3794/* Opcode: Rewind P1 P2 *
3795**
3796** The next use of the Rowid or Column or Next instruction for P1
3797** will refer to the first entry in the database table or index.
3798** If the table or index is empty and P2>0, then jump immediately to P2.
3799** If P2 is 0 or if the table or index is not empty, fall through
3800** to the following instruction.
3801*/
3802case OP_Rewind: { /* no-push */
3803 int i = pOp->p1;
3804 Cursor *pC;
3805 BtCursor *pCrsr;
3806 int res;
3807
3808 assert( i>=0 && i<p->nCursor );
3809 pC = p->apCsr[i];
3810 assert( pC!=0 );
3811 if( (pCrsr = pC->pCursor)!=0 ){
3812 rc = sqlite3BtreeFirst(pCrsr, &res);
3813 pC->atFirst = res==0;
3814 pC->deferredMoveto = 0;
3815 pC->cacheStatus = CACHE_STALE;
3816 }else{
3817 res = 1;
3818 }
3819 pC->nullRow = res;
3820 if( res && pOp->p2>0 ){
3821 pc = pOp->p2 - 1;
3822 }
3823 break;
3824}
3825
3826/* Opcode: Next P1 P2 *
3827**
3828** Advance cursor P1 so that it points to the next key/data pair in its
3829** table or index. If there are no more key/value pairs then fall through
3830** to the following instruction. But if the cursor advance was successful,
3831** jump immediately to P2.
3832**
3833** See also: Prev
3834*/
3835/* Opcode: Prev P1 P2 *
3836**
3837** Back up cursor P1 so that it points to the previous key/data pair in its
3838** table or index. If there is no previous key/value pairs then fall through
3839** to the following instruction. But if the cursor backup was successful,
3840** jump immediately to P2.
3841*/
3842case OP_Prev: /* no-push */
3843case OP_Next: { /* no-push */
3844 Cursor *pC;
3845 BtCursor *pCrsr;
3846
3847 CHECK_FOR_INTERRUPT;
3848 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3849 pC = p->apCsr[pOp->p1];
3850 if( pC==0 ){
3851 break; /* See ticket #2273 */
3852 }
3853 if( (pCrsr = pC->pCursor)!=0 ){
3854 int res;
3855 if( pC->nullRow ){
3856 res = 1;
3857 }else{
3858 assert( pC->deferredMoveto==0 );
3859 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3860 sqlite3BtreePrevious(pCrsr, &res);
3861 pC->nullRow = res;
3862 pC->cacheStatus = CACHE_STALE;
3863 }
3864 if( res==0 ){
3865 pc = pOp->p2 - 1;
3866#ifdef SQLITE_TEST
3867 sqlite3_search_count++;
3868#endif
3869 }
3870 }else{
3871 pC->nullRow = 1;
3872 }
3873 pC->rowidIsValid = 0;
3874 break;
3875}
3876
3877/* Opcode: IdxInsert P1 P2 *
3878**
3879** The top of the stack holds a SQL index key made using either the
3880** MakeIdxRec or MakeRecord instructions. This opcode writes that key
3881** into the index P1. Data for the entry is nil.
3882**
3883** P2 is a flag that provides a hint to the b-tree layer that this
3884** insert is likely to be an append.
3885**
3886** This instruction only works for indices. The equivalent instruction
3887** for tables is OP_Insert.
3888*/
3889case OP_IdxInsert: { /* no-push */
3890 int i = pOp->p1;
3891 Cursor *pC;
3892 BtCursor *pCrsr;
3893 assert( pTos>=p->aStack );
3894 assert( i>=0 && i<p->nCursor );
3895 assert( p->apCsr[i]!=0 );
3896 assert( pTos->flags & MEM_Blob );
3897 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3898 assert( pC->isTable==0 );
3899 rc = ExpandBlob(pTos);
3900 if( rc==SQLITE_OK ){
3901 int nKey = pTos->n;
3902 const char *zKey = pTos->z;
3903 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p2);
3904 assert( pC->deferredMoveto==0 );
3905 pC->cacheStatus = CACHE_STALE;
3906 }
3907 }
3908 Release(pTos);
3909 pTos--;
3910 break;
3911}
3912
3913/* Opcode: IdxDelete P1 * *
3914**
3915** The top of the stack is an index key built using the either the
3916** MakeIdxRec or MakeRecord opcodes.
3917** This opcode removes that entry from the index.
3918*/
3919case OP_IdxDelete: { /* no-push */
3920 int i = pOp->p1;
3921 Cursor *pC;
3922 BtCursor *pCrsr;
3923 assert( pTos>=p->aStack );
3924 assert( pTos->flags & MEM_Blob );
3925 assert( i>=0 && i<p->nCursor );
3926 assert( p->apCsr[i]!=0 );
3927 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3928 int res;
3929 rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, 0, &res);
3930 if( rc==SQLITE_OK && res==0 ){
3931 rc = sqlite3BtreeDelete(pCrsr);
3932 }
3933 assert( pC->deferredMoveto==0 );
3934 pC->cacheStatus = CACHE_STALE;
3935 }
3936 Release(pTos);
3937 pTos--;
3938 break;
3939}
3940
3941/* Opcode: IdxRowid P1 * *
3942**
3943** Push onto the stack an integer which is the last entry in the record at
3944** the end of the index key pointed to by cursor P1. This integer should be
3945** the rowid of the table entry to which this index entry points.
3946**
3947** See also: Rowid, MakeIdxRec.
3948*/
3949case OP_IdxRowid: {
3950 int i = pOp->p1;
3951 BtCursor *pCrsr;
3952 Cursor *pC;
3953
3954 assert( i>=0 && i<p->nCursor );
3955 assert( p->apCsr[i]!=0 );
3956 pTos++;
3957 pTos->flags = MEM_Null;
3958 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3959 i64 rowid;
3960
3961 assert( pC->deferredMoveto==0 );
3962 assert( pC->isTable==0 );
3963 if( pC->nullRow ){
3964 pTos->flags = MEM_Null;
3965 }else{
3966 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3967 if( rc!=SQLITE_OK ){
3968 goto abort_due_to_error;
3969 }
3970 pTos->flags = MEM_Int;
3971 pTos->u.i = rowid;
3972 }
3973 }
3974 break;
3975}
3976
3977/* Opcode: IdxGT P1 P2 *
3978**
3979** The top of the stack is an index entry that omits the ROWID. Compare
3980** the top of stack against the index that P1 is currently pointing to.
3981** Ignore the ROWID on the P1 index.
3982**
3983** The top of the stack might have fewer columns that P1.
3984**
3985** If the P1 index entry is greater than the top of the stack
3986** then jump to P2. Otherwise fall through to the next instruction.
3987** In either case, the stack is popped once.
3988*/
3989/* Opcode: IdxGE P1 P2 P3
3990**
3991** The top of the stack is an index entry that omits the ROWID. Compare
3992** the top of stack against the index that P1 is currently pointing to.
3993** Ignore the ROWID on the P1 index.
3994**
3995** If the P1 index entry is greater than or equal to the top of the stack
3996** then jump to P2. Otherwise fall through to the next instruction.
3997** In either case, the stack is popped once.
3998**
3999** If P3 is the "+" string (or any other non-NULL string) then the
4000** index taken from the top of the stack is temporarily increased by
4001** an epsilon prior to the comparison. This make the opcode work
4002** like IdxGT except that if the key from the stack is a prefix of
4003** the key in the cursor, the result is false whereas it would be
4004** true with IdxGT.
4005*/
4006/* Opcode: IdxLT P1 P2 P3
4007**
4008** The top of the stack is an index entry that omits the ROWID. Compare
4009** the top of stack against the index that P1 is currently pointing to.
4010** Ignore the ROWID on the P1 index.
4011**
4012** If the P1 index entry is less than the top of the stack
4013** then jump to P2. Otherwise fall through to the next instruction.
4014** In either case, the stack is popped once.
4015**
4016** If P3 is the "+" string (or any other non-NULL string) then the
4017** index taken from the top of the stack is temporarily increased by
4018** an epsilon prior to the comparison. This makes the opcode work
4019** like IdxLE.
4020*/
4021case OP_IdxLT: /* no-push */
4022case OP_IdxGT: /* no-push */
4023case OP_IdxGE: { /* no-push */
4024 int i= pOp->p1;
4025 Cursor *pC;
4026
4027 assert( i>=0 && i<p->nCursor );
4028 assert( p->apCsr[i]!=0 );
4029 assert( pTos>=p->aStack );
4030 if( (pC = p->apCsr[i])->pCursor!=0 ){
4031 int res;
4032
4033 assert( pTos->flags & MEM_Blob ); /* Created using OP_MakeRecord */
4034 assert( pC->deferredMoveto==0 );
4035 ExpandBlob(pTos);
4036 *pC->pIncrKey = pOp->p3!=0;
4037 assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
4038 rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res);
4039 *pC->pIncrKey = 0;
4040 if( rc!=SQLITE_OK ){
4041 break;
4042 }
4043 if( pOp->opcode==OP_IdxLT ){
4044 res = -res;
4045 }else if( pOp->opcode==OP_IdxGE ){
4046 res++;
4047 }
4048 if( res>0 ){
4049 pc = pOp->p2 - 1 ;
4050 }
4051 }
4052 Release(pTos);
4053 pTos--;
4054 break;
4055}
4056
4057/* Opcode: Destroy P1 P2 *
4058**
4059** Delete an entire database table or index whose root page in the database
4060** file is given by P1.
4061**
4062** The table being destroyed is in the main database file if P2==0. If
4063** P2==1 then the table to be clear is in the auxiliary database file
4064** that is used to store tables create using CREATE TEMPORARY TABLE.
4065**
4066** If AUTOVACUUM is enabled then it is possible that another root page
4067** might be moved into the newly deleted root page in order to keep all
4068** root pages contiguous at the beginning of the database. The former
4069** value of the root page that moved - its value before the move occurred -
4070** is pushed onto the stack. If no page movement was required (because
4071** the table being dropped was already the last one in the database) then
4072** a zero is pushed onto the stack. If AUTOVACUUM is disabled
4073** then a zero is pushed onto the stack.
4074**
4075** See also: Clear
4076*/
4077case OP_Destroy: {
4078 int iMoved;
4079 int iCnt;
4080#ifndef SQLITE_OMIT_VIRTUALTABLE
4081 Vdbe *pVdbe;
4082 iCnt = 0;
4083 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
4084 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4085 iCnt++;
4086 }
4087 }
4088#else
4089 iCnt = db->activeVdbeCnt;
4090#endif
4091 if( iCnt>1 ){
4092 rc = SQLITE_LOCKED;
4093 }else{
4094 assert( iCnt==1 );
4095 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
4096 rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
4097 pTos++;
4098 pTos->flags = MEM_Int;
4099 pTos->u.i = iMoved;
4100#ifndef SQLITE_OMIT_AUTOVACUUM
4101 if( rc==SQLITE_OK && iMoved!=0 ){
4102 sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1);
4103 }
4104#endif
4105 }
4106 break;
4107}
4108
4109/* Opcode: Clear P1 P2 *
4110**
4111** Delete all contents of the database table or index whose root page
4112** in the database file is given by P1. But, unlike Destroy, do not
4113** remove the table or index from the database file.
4114**
4115** The table being clear is in the main database file if P2==0. If
4116** P2==1 then the table to be clear is in the auxiliary database file
4117** that is used to store tables create using CREATE TEMPORARY TABLE.
4118**
4119** See also: Destroy
4120*/
4121case OP_Clear: { /* no-push */
4122
4123 /* For consistency with the way other features of SQLite operate
4124 ** with a truncate, we will also skip the update callback.
4125 */
4126#if 0
4127 Btree *pBt = db->aDb[pOp->p2].pBt;
4128 if( db->xUpdateCallback && pOp->p3 ){
4129 const char *zDb = db->aDb[pOp->p2].zName;
4130 const char *zTbl = pOp->p3;
4131 BtCursor *pCur = 0;
4132 int fin = 0;
4133
4134 rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur);
4135 if( rc!=SQLITE_OK ){
4136 goto abort_due_to_error;
4137 }
4138 for(
4139 rc=sqlite3BtreeFirst(pCur, &fin);
4140 rc==SQLITE_OK && !fin;
4141 rc=sqlite3BtreeNext(pCur, &fin)
4142 ){
4143 i64 iKey;
4144 rc = sqlite3BtreeKeySize(pCur, &iKey);
4145 if( rc ){
4146 break;
4147 }
4148 iKey = keyToInt(iKey);
4149 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4150 }
4151 sqlite3BtreeCloseCursor(pCur);
4152 if( rc!=SQLITE_OK ){
4153 goto abort_due_to_error;
4154 }
4155 }
4156#endif
4157 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
4158 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
4159 break;
4160}
4161
4162/* Opcode: CreateTable P1 * *
4163**
4164** Allocate a new table in the main database file if P2==0 or in the
4165** auxiliary database file if P2==1. Push the page number
4166** for the root page of the new table onto the stack.
4167**
4168** The difference between a table and an index is this: A table must
4169** have a 4-byte integer key and can have arbitrary data. An index
4170** has an arbitrary key but no data.
4171**
4172** See also: CreateIndex
4173*/
4174/* Opcode: CreateIndex P1 * *
4175**
4176** Allocate a new index in the main database file if P2==0 or in the
4177** auxiliary database file if P2==1. Push the page number of the
4178** root page of the new index onto the stack.
4179**
4180** See documentation on OP_CreateTable for additional information.
4181*/
4182case OP_CreateIndex:
4183case OP_CreateTable: {
4184 int pgno;
4185 int flags;
4186 Db *pDb;
4187 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4188 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
4189 pDb = &db->aDb[pOp->p1];
4190 assert( pDb->pBt!=0 );
4191 if( pOp->opcode==OP_CreateTable ){
4192 /* flags = BTREE_INTKEY; */
4193 flags = BTREE_LEAFDATA|BTREE_INTKEY;
4194 }else{
4195 flags = BTREE_ZERODATA;
4196 }
4197 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
4198 pTos++;
4199 if( rc==SQLITE_OK ){
4200 pTos->u.i = pgno;
4201 pTos->flags = MEM_Int;
4202 }else{
4203 pTos->flags = MEM_Null;
4204 }
4205 break;
4206}
4207
4208/* Opcode: ParseSchema P1 P2 P3
4209**
4210** Read and parse all entries from the SQLITE_MASTER table of database P1
4211** that match the WHERE clause P3. P2 is the "force" flag. Always do
4212** the parsing if P2 is true. If P2 is false, then this routine is a
4213** no-op if the schema is not currently loaded. In other words, if P2
4214** is false, the SQLITE_MASTER table is only parsed if the rest of the
4215** schema is already loaded into the symbol table.
4216**
4217** This opcode invokes the parser to create a new virtual machine,
4218** then runs the new virtual machine. It is thus a reentrant opcode.
4219*/
4220case OP_ParseSchema: { /* no-push */
4221 char *zSql;
4222 int iDb = pOp->p1;
4223 const char *zMaster;
4224 InitData initData;
4225
4226 assert( iDb>=0 && iDb<db->nDb );
4227 if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
4228 break;
4229 }
4230 zMaster = SCHEMA_TABLE(iDb);
4231 initData.db = db;
4232 initData.iDb = pOp->p1;
4233 initData.pzErrMsg = &p->zErrMsg;
4234 zSql = sqlite3MPrintf(db,
4235 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
4236 db->aDb[iDb].zName, zMaster, pOp->p3);
4237 if( zSql==0 ) goto no_mem;
4238 sqlite3SafetyOff(db);
4239 assert( db->init.busy==0 );
4240 db->init.busy = 1;
4241 assert( !db->mallocFailed );
4242 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4243 if( rc==SQLITE_ABORT ) rc = initData.rc;
4244 sqlite3_free(zSql);
4245 db->init.busy = 0;
4246 sqlite3SafetyOn(db);
4247 if( rc==SQLITE_NOMEM ){
4248 goto no_mem;
4249 }
4250 break;
4251}
4252
4253#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
4254/* Opcode: LoadAnalysis P1 * *
4255**
4256** Read the sqlite_stat1 table for database P1 and load the content
4257** of that table into the internal index hash table. This will cause
4258** the analysis to be used when preparing all subsequent queries.
4259*/
4260case OP_LoadAnalysis: { /* no-push */
4261 int iDb = pOp->p1;
4262 assert( iDb>=0 && iDb<db->nDb );
4263 rc = sqlite3AnalysisLoad(db, iDb);
4264 break;
4265}
4266#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
4267
4268/* Opcode: DropTable P1 * P3
4269**
4270** Remove the internal (in-memory) data structures that describe
4271** the table named P3 in database P1. This is called after a table
4272** is dropped in order to keep the internal representation of the
4273** schema consistent with what is on disk.
4274*/
4275case OP_DropTable: { /* no-push */
4276 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3);
4277 break;
4278}
4279
4280/* Opcode: DropIndex P1 * P3
4281**
4282** Remove the internal (in-memory) data structures that describe
4283** the index named P3 in database P1. This is called after an index
4284** is dropped in order to keep the internal representation of the
4285** schema consistent with what is on disk.
4286*/
4287case OP_DropIndex: { /* no-push */
4288 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3);
4289 break;
4290}
4291
4292/* Opcode: DropTrigger P1 * P3
4293**
4294** Remove the internal (in-memory) data structures that describe
4295** the trigger named P3 in database P1. This is called after a trigger
4296** is dropped in order to keep the internal representation of the
4297** schema consistent with what is on disk.
4298*/
4299case OP_DropTrigger: { /* no-push */
4300 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3);
4301 break;
4302}
4303
4304
4305#ifndef SQLITE_OMIT_INTEGRITY_CHECK
4306/* Opcode: IntegrityCk P1 P2 *
4307**
4308** Do an analysis of the currently open database. Push onto the
4309** stack the text of an error message describing any problems.
4310** If no problems are found, push a NULL onto the stack.
4311**
4312** P1 is the address of a memory cell that contains the maximum
4313** number of allowed errors. At most mem[P1] errors will be reported.
4314** In other words, the analysis stops as soon as mem[P1] errors are
4315** seen. Mem[P1] is updated with the number of errors remaining.
4316**
4317** The root page numbers of all tables in the database are integer
4318** values on the stack. This opcode pulls as many integers as it
4319** can off of the stack and uses those numbers as the root pages.
4320**
4321** If P2 is not zero, the check is done on the auxiliary database
4322** file, not the main database file.
4323**
4324** This opcode is used to implement the integrity_check pragma.
4325*/
4326case OP_IntegrityCk: {
4327 int nRoot;
4328 int *aRoot;
4329 int j;
4330 int nErr;
4331 char *z;
4332 Mem *pnErr;
4333
4334 for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
4335 if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
4336 }
4337 assert( nRoot>0 );
4338 aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) );
4339 if( aRoot==0 ) goto no_mem;
4340 j = pOp->p1;
4341 assert( j>=0 && j<p->nMem );
4342 pnErr = &p->aMem[j];
4343 assert( (pnErr->flags & MEM_Int)!=0 );
4344 for(j=0; j<nRoot; j++){
4345 aRoot[j] = (pTos-j)->u.i;
4346 }
4347 aRoot[j] = 0;
4348 popStack(&pTos, nRoot);
4349 pTos++;
4350 assert( pOp->p2>=0 && pOp->p2<db->nDb );
4351 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
4352 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot,
4353 pnErr->u.i, &nErr);
4354 pnErr->u.i -= nErr;
4355 if( nErr==0 ){
4356 assert( z==0 );
4357 pTos->flags = MEM_Null;
4358 }else{
4359 pTos->z = z;
4360 pTos->n = strlen(z);
4361 pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
4362 pTos->xDel = 0;
4363 }
4364 pTos->enc = SQLITE_UTF8;
4365 sqlite3VdbeChangeEncoding(pTos, encoding);
4366 sqlite3_free(aRoot);
4367 break;
4368}
4369#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
4370
4371/* Opcode: FifoWrite * * *
4372**
4373** Write the integer on the top of the stack
4374** into the Fifo.
4375*/
4376case OP_FifoWrite: { /* no-push */
4377 assert( pTos>=p->aStack );
4378 sqlite3VdbeMemIntegerify(pTos);
4379 if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){
4380 goto no_mem;
4381 }
4382 assert( (pTos->flags & MEM_Dyn)==0 );
4383 pTos--;
4384 break;
4385}
4386
4387/* Opcode: FifoRead * P2 *
4388**
4389** Attempt to read a single integer from the Fifo
4390** and push it onto the stack. If the Fifo is empty
4391** push nothing but instead jump to P2.
4392*/
4393case OP_FifoRead: {
4394 i64 v;
4395 CHECK_FOR_INTERRUPT;
4396 if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){
4397 pc = pOp->p2 - 1;
4398 }else{
4399 pTos++;
4400 pTos->u.i = v;
4401 pTos->flags = MEM_Int;
4402 }
4403 break;
4404}
4405
4406#ifndef SQLITE_OMIT_TRIGGER
4407/* Opcode: ContextPush * * *
4408**
4409** Save the current Vdbe context such that it can be restored by a ContextPop
4410** opcode. The context stores the last insert row id, the last statement change
4411** count, and the current statement change count.
4412*/
4413case OP_ContextPush: { /* no-push */
4414 int i = p->contextStackTop++;
4415 Context *pContext;
4416
4417 assert( i>=0 );
4418 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
4419 if( i>=p->contextStackDepth ){
4420 p->contextStackDepth = i+1;
4421 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
4422 sizeof(Context)*(i+1));
4423 if( p->contextStack==0 ) goto no_mem;
4424 }
4425 pContext = &p->contextStack[i];
4426 pContext->lastRowid = db->lastRowid;
4427 pContext->nChange = p->nChange;
4428 pContext->sFifo = p->sFifo;
4429 sqlite3VdbeFifoInit(&p->sFifo);
4430 break;
4431}
4432
4433/* Opcode: ContextPop * * *
4434**
4435** Restore the Vdbe context to the state it was in when contextPush was last
4436** executed. The context stores the last insert row id, the last statement
4437** change count, and the current statement change count.
4438*/
4439case OP_ContextPop: { /* no-push */
4440 Context *pContext = &p->contextStack[--p->contextStackTop];
4441 assert( p->contextStackTop>=0 );
4442 db->lastRowid = pContext->lastRowid;
4443 p->nChange = pContext->nChange;
4444 sqlite3VdbeFifoClear(&p->sFifo);
4445 p->sFifo = pContext->sFifo;
4446 break;
4447}
4448#endif /* #ifndef SQLITE_OMIT_TRIGGER */
4449
4450/* Opcode: MemStore P1 P2 *
4451**
4452** Write the top of the stack into memory location P1.
4453** P1 should be a small integer since space is allocated
4454** for all memory locations between 0 and P1 inclusive.
4455**
4456** After the data is stored in the memory location, the
4457** stack is popped once if P2 is 1. If P2 is zero, then
4458** the original data remains on the stack.
4459*/
4460case OP_MemStore: { /* no-push */
4461 assert( pTos>=p->aStack );
4462 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4463 rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos);
4464 pTos--;
4465
4466 /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will
4467 ** restore the top of the stack to its original value.
4468 */
4469 if( pOp->p2 ){
4470 break;
4471 }
4472}
4473/* Opcode: MemLoad P1 * *
4474**
4475** Push a copy of the value in memory location P1 onto the stack.
4476**
4477** If the value is a string, then the value pushed is a pointer to
4478** the string that is stored in the memory location. If the memory
4479** location is subsequently changed (using OP_MemStore) then the
4480** value pushed onto the stack will change too.
4481*/
4482case OP_MemLoad: {
4483 int i = pOp->p1;
4484 assert( i>=0 && i<p->nMem );
4485 pTos++;
4486 sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem);
4487 break;
4488}
4489
4490#ifndef SQLITE_OMIT_AUTOINCREMENT
4491/* Opcode: MemMax P1 * *
4492**
4493** Set the value of memory cell P1 to the maximum of its current value
4494** and the value on the top of the stack. The stack is unchanged.
4495**
4496** This instruction throws an error if the memory cell is not initially
4497** an integer.
4498*/
4499case OP_MemMax: { /* no-push */
4500 int i = pOp->p1;
4501 Mem *pMem;
4502 assert( pTos>=p->aStack );
4503 assert( i>=0 && i<p->nMem );
4504 pMem = &p->aMem[i];
4505 sqlite3VdbeMemIntegerify(pMem);
4506 sqlite3VdbeMemIntegerify(pTos);
4507 if( pMem->u.i<pTos->u.i){
4508 pMem->u.i = pTos->u.i;
4509 }
4510 break;
4511}
4512#endif /* SQLITE_OMIT_AUTOINCREMENT */
4513
4514/* Opcode: MemIncr P1 P2 *
4515**
4516** Increment the integer valued memory cell P2 by the value in P1.
4517**
4518** It is illegal to use this instruction on a memory cell that does
4519** not contain an integer. An assertion fault will result if you try.
4520*/
4521case OP_MemIncr: { /* no-push */
4522 int i = pOp->p2;
4523 Mem *pMem;
4524 assert( i>=0 && i<p->nMem );
4525 pMem = &p->aMem[i];
4526 assert( pMem->flags==MEM_Int );
4527 pMem->u.i += pOp->p1;
4528 break;
4529}
4530
4531/* Opcode: IfMemPos P1 P2 *
4532**
4533** If the value of memory cell P1 is 1 or greater, jump to P2.
4534**
4535** It is illegal to use this instruction on a memory cell that does
4536** not contain an integer. An assertion fault will result if you try.
4537*/
4538case OP_IfMemPos: { /* no-push */
4539 int i = pOp->p1;
4540 Mem *pMem;
4541 assert( i>=0 && i<p->nMem );
4542 pMem = &p->aMem[i];
4543 assert( pMem->flags==MEM_Int );
4544 if( pMem->u.i>0 ){
4545 pc = pOp->p2 - 1;
4546 }
4547 break;
4548}
4549
4550/* Opcode: IfMemNeg P1 P2 *
4551**
4552** If the value of memory cell P1 is less than zero, jump to P2.
4553**
4554** It is illegal to use this instruction on a memory cell that does
4555** not contain an integer. An assertion fault will result if you try.
4556*/
4557case OP_IfMemNeg: { /* no-push */
4558 int i = pOp->p1;
4559 Mem *pMem;
4560 assert( i>=0 && i<p->nMem );
4561 pMem = &p->aMem[i];
4562 assert( pMem->flags==MEM_Int );
4563 if( pMem->u.i<0 ){
4564 pc = pOp->p2 - 1;
4565 }
4566 break;
4567}
4568
4569/* Opcode: IfMemZero P1 P2 *
4570**
4571** If the value of memory cell P1 is exactly 0, jump to P2.
4572**
4573** It is illegal to use this instruction on a memory cell that does
4574** not contain an integer. An assertion fault will result if you try.
4575*/
4576case OP_IfMemZero: { /* no-push */
4577 int i = pOp->p1;
4578 Mem *pMem;
4579 assert( i>=0 && i<p->nMem );
4580 pMem = &p->aMem[i];
4581 assert( pMem->flags==MEM_Int );
4582 if( pMem->u.i==0 ){
4583 pc = pOp->p2 - 1;
4584 }
4585 break;
4586}
4587
4588/* Opcode: MemNull P1 * *
4589**
4590** Store a NULL in memory cell P1
4591*/
4592case OP_MemNull: {
4593 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4594 sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]);
4595 break;
4596}
4597
4598/* Opcode: MemInt P1 P2 *
4599**
4600** Store the integer value P1 in memory cell P2.
4601*/
4602case OP_MemInt: {
4603 assert( pOp->p2>=0 && pOp->p2<p->nMem );
4604 sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1);
4605 break;
4606}
4607
4608/* Opcode: MemMove P1 P2 *
4609**
4610** Move the content of memory cell P2 over to memory cell P1.
4611** Any prior content of P1 is erased. Memory cell P2 is left
4612** containing a NULL.
4613*/
4614case OP_MemMove: {
4615 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4616 assert( pOp->p2>=0 && pOp->p2<p->nMem );
4617 rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]);
4618 break;
4619}
4620
4621/* Opcode: AggStep P1 P2 P3
4622**
4623** Execute the step function for an aggregate. The
4624** function has P2 arguments. P3 is a pointer to the FuncDef
4625** structure that specifies the function. Use memory location
4626** P1 as the accumulator.
4627**
4628** The P2 arguments are popped from the stack.
4629*/
4630case OP_AggStep: { /* no-push */
4631 int n = pOp->p2;
4632 int i;
4633 Mem *pMem, *pRec;
4634 sqlite3_context ctx;
4635 sqlite3_value **apVal;
4636
4637 assert( n>=0 );
4638 pRec = &pTos[1-n];
4639 assert( pRec>=p->aStack );
4640 apVal = p->apArg;
4641 assert( apVal || n==0 );
4642 for(i=0; i<n; i++, pRec++){
4643 apVal[i] = pRec;
4644 storeTypeInfo(pRec, encoding);
4645 }
4646 ctx.pFunc = (FuncDef*)pOp->p3;
4647 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4648 ctx.pMem = pMem = &p->aMem[pOp->p1];
4649 pMem->n++;
4650 ctx.s.flags = MEM_Null;
4651 ctx.s.z = 0;
4652 ctx.s.xDel = 0;
4653 ctx.s.db = db;
4654 ctx.isError = 0;
4655 ctx.pColl = 0;
4656 if( ctx.pFunc->needCollSeq ){
4657 assert( pOp>p->aOp );
4658 assert( pOp[-1].p3type==P3_COLLSEQ );
4659 assert( pOp[-1].opcode==OP_CollSeq );
4660 ctx.pColl = (CollSeq *)pOp[-1].p3;
4661 }
4662 (ctx.pFunc->xStep)(&ctx, n, apVal);
4663 popStack(&pTos, n);
4664 if( ctx.isError ){
4665 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
4666 rc = SQLITE_ERROR;
4667 }
4668 sqlite3VdbeMemRelease(&ctx.s);
4669 break;
4670}
4671
4672/* Opcode: AggFinal P1 P2 P3
4673**
4674** Execute the finalizer function for an aggregate. P1 is
4675** the memory location that is the accumulator for the aggregate.
4676**
4677** P2 is the number of arguments that the step function takes and
4678** P3 is a pointer to the FuncDef for this function. The P2
4679** argument is not used by this opcode. It is only there to disambiguate
4680** functions that can take varying numbers of arguments. The
4681** P3 argument is only needed for the degenerate case where
4682** the step function was not previously called.
4683*/
4684case OP_AggFinal: { /* no-push */
4685 Mem *pMem;
4686 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4687 pMem = &p->aMem[pOp->p1];
4688 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
4689 rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
4690 if( rc==SQLITE_ERROR ){
4691 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
4692 }
4693 if( sqlite3VdbeMemTooBig(pMem) ){
4694 goto too_big;
4695 }
4696 break;
4697}
4698
4699
4700#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
4701/* Opcode: Vacuum * * *
4702**
4703** Vacuum the entire database. This opcode will cause other virtual
4704** machines to be created and run. It may not be called from within
4705** a transaction.
4706*/
4707case OP_Vacuum: { /* no-push */
4708 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4709 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4710 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4711 break;
4712}
4713#endif
4714
4715#if !defined(SQLITE_OMIT_AUTOVACUUM)
4716/* Opcode: IncrVacuum P1 P2 *
4717**
4718** Perform a single step of the incremental vacuum procedure on
4719** the P1 database. If the vacuum has finished, jump to instruction
4720** P2. Otherwise, fall through to the next instruction.
4721*/
4722case OP_IncrVacuum: { /* no-push */
4723 Btree *pBt;
4724
4725 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4726 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
4727 pBt = db->aDb[pOp->p1].pBt;
4728 rc = sqlite3BtreeIncrVacuum(pBt);
4729 if( rc==SQLITE_DONE ){
4730 pc = pOp->p2 - 1;
4731 rc = SQLITE_OK;
4732 }
4733 break;
4734}
4735#endif
4736
4737/* Opcode: Expire P1 * *
4738**
4739** Cause precompiled statements to become expired. An expired statement
4740** fails with an error code of SQLITE_SCHEMA if it is ever executed
4741** (via sqlite3_step()).
4742**
4743** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4744** then only the currently executing statement is affected.
4745*/
4746case OP_Expire: { /* no-push */
4747 if( !pOp->p1 ){
4748 sqlite3ExpirePreparedStatements(db);
4749 }else{
4750 p->expired = 1;
4751 }
4752 break;
4753}
4754
4755#ifndef SQLITE_OMIT_SHARED_CACHE
4756/* Opcode: TableLock P1 P2 P3
4757**
4758** Obtain a lock on a particular table. This instruction is only used when
4759** the shared-cache feature is enabled.
4760**
4761** If P1 is not negative, then it is the index of the database
4762** in sqlite3.aDb[] and a read-lock is required. If P1 is negative, a
4763** write-lock is required. In this case the index of the database is the
4764** absolute value of P1 minus one (iDb = abs(P1) - 1;) and a write-lock is
4765** required.
4766**
4767** P2 contains the root-page of the table to lock.
4768**
4769** P3 contains a pointer to the name of the table being locked. This is only
4770** used to generate an error message if the lock cannot be obtained.
4771*/
4772case OP_TableLock: { /* no-push */
4773 int p1 = pOp->p1;
4774 u8 isWriteLock = (p1<0);
4775 if( isWriteLock ){
4776 p1 = (-1*p1)-1;
4777 }
4778 assert( p1>=0 && p1<db->nDb );
4779 assert( (p->btreeMask & (1<<p1))!=0 );
4780 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4781 if( rc==SQLITE_LOCKED ){
4782 const char *z = (const char *)pOp->p3;
4783 sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
4784 }
4785 break;
4786}
4787#endif /* SQLITE_OMIT_SHARED_CACHE */
4788
4789#ifndef SQLITE_OMIT_VIRTUALTABLE
4790/* Opcode: VBegin * * P3
4791**
4792** P3 a pointer to an sqlite3_vtab structure. Call the xBegin method
4793** for that table.
4794*/
4795case OP_VBegin: { /* no-push */
4796 rc = sqlite3VtabBegin(db, (sqlite3_vtab *)pOp->p3);
4797 break;
4798}
4799#endif /* SQLITE_OMIT_VIRTUALTABLE */
4800
4801#ifndef SQLITE_OMIT_VIRTUALTABLE
4802/* Opcode: VCreate P1 * P3
4803**
4804** P3 is the name of a virtual table in database P1. Call the xCreate method
4805** for that table.
4806*/
4807case OP_VCreate: { /* no-push */
4808 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg);
4809 break;
4810}
4811#endif /* SQLITE_OMIT_VIRTUALTABLE */
4812
4813#ifndef SQLITE_OMIT_VIRTUALTABLE
4814/* Opcode: VDestroy P1 * P3
4815**
4816** P3 is the name of a virtual table in database P1. Call the xDestroy method
4817** of that table.
4818*/
4819case OP_VDestroy: { /* no-push */
4820 p->inVtabMethod = 2;
4821 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3);
4822 p->inVtabMethod = 0;
4823 break;
4824}
4825#endif /* SQLITE_OMIT_VIRTUALTABLE */
4826
4827#ifndef SQLITE_OMIT_VIRTUALTABLE
4828/* Opcode: VOpen P1 * P3
4829**
4830** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
4831** P1 is a cursor number. This opcode opens a cursor to the virtual
4832** table and stores that cursor in P1.
4833*/
4834case OP_VOpen: { /* no-push */
4835 Cursor *pCur = 0;
4836 sqlite3_vtab_cursor *pVtabCursor = 0;
4837
4838 sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
4839 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4840
4841 assert(pVtab && pModule);
4842 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4843 rc = pModule->xOpen(pVtab, &pVtabCursor);
4844 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4845 if( SQLITE_OK==rc ){
4846 /* Initialise sqlite3_vtab_cursor base class */
4847 pVtabCursor->pVtab = pVtab;
4848
4849 /* Initialise vdbe cursor object */
4850 pCur = allocateCursor(p, pOp->p1, -1);
4851 if( pCur ){
4852 pCur->pVtabCursor = pVtabCursor;
4853 pCur->pModule = pVtabCursor->pVtab->pModule;
4854 }else{
4855 db->mallocFailed = 1;
4856 pModule->xClose(pVtabCursor);
4857 }
4858 }
4859 break;
4860}
4861#endif /* SQLITE_OMIT_VIRTUALTABLE */
4862
4863#ifndef SQLITE_OMIT_VIRTUALTABLE
4864/* Opcode: VFilter P1 P2 P3
4865**
4866** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4867** the filtered result set is empty.
4868**
4869** P3 is either NULL or a string that was generated by the xBestIndex
4870** method of the module. The interpretation of the P3 string is left
4871** to the module implementation.
4872**
4873** This opcode invokes the xFilter method on the virtual table specified
4874** by P1. The integer query plan parameter to xFilter is the top of the
4875** stack. Next down on the stack is the argc parameter. Beneath the
4876** next of stack are argc additional parameters which are passed to
4877** xFilter as argv. The topmost parameter (i.e. 3rd element popped from
4878** the stack) becomes argv[argc-1] when passed to xFilter.
4879**
4880** The integer query plan parameter, argc, and all argv stack values
4881** are popped from the stack before this instruction completes.
4882**
4883** A jump is made to P2 if the result set after filtering would be
4884** empty.
4885*/
4886case OP_VFilter: { /* no-push */
4887 int nArg;
4888
4889 const sqlite3_module *pModule;
4890
4891 Cursor *pCur = p->apCsr[pOp->p1];
4892 assert( pCur->pVtabCursor );
4893 pModule = pCur->pVtabCursor->pVtab->pModule;
4894
4895 /* Grab the index number and argc parameters off the top of the stack. */
4896 assert( (&pTos[-1])>=p->aStack );
4897 assert( (pTos[0].flags&MEM_Int)!=0 && pTos[-1].flags==MEM_Int );
4898 nArg = pTos[-1].u.i;
4899
4900 /* Invoke the xFilter method */
4901 {
4902 int res = 0;
4903 int i;
4904 Mem **apArg = p->apArg;
4905 for(i = 0; i<nArg; i++){
4906 apArg[i] = &pTos[i+1-2-nArg];
4907 storeTypeInfo(apArg[i], 0);
4908 }
4909
4910 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4911 p->inVtabMethod = 1;
4912 rc = pModule->xFilter(pCur->pVtabCursor, pTos->u.i, pOp->p3, nArg, apArg);
4913 p->inVtabMethod = 0;
4914 if( rc==SQLITE_OK ){
4915 res = pModule->xEof(pCur->pVtabCursor);
4916 }
4917 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4918
4919 if( res ){
4920 pc = pOp->p2 - 1;
4921 }
4922 }
4923
4924 /* Pop the index number, argc value and parameters off the stack */
4925 popStack(&pTos, 2+nArg);
4926 break;
4927}
4928#endif /* SQLITE_OMIT_VIRTUALTABLE */
4929
4930#ifndef SQLITE_OMIT_VIRTUALTABLE
4931/* Opcode: VRowid P1 * *
4932**
4933** Push an integer onto the stack which is the rowid of
4934** the virtual-table that the P1 cursor is pointing to.
4935*/
4936case OP_VRowid: {
4937 const sqlite3_module *pModule;
4938
4939 Cursor *pCur = p->apCsr[pOp->p1];
4940 assert( pCur->pVtabCursor );
4941 pModule = pCur->pVtabCursor->pVtab->pModule;
4942 if( pModule->xRowid==0 ){
4943 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0);
4944 rc = SQLITE_ERROR;
4945 } else {
4946 sqlite_int64 iRow;
4947
4948 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4949 rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
4950 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4951
4952 pTos++;
4953 pTos->flags = MEM_Int;
4954 pTos->u.i = iRow;
4955 }
4956
4957 break;
4958}
4959#endif /* SQLITE_OMIT_VIRTUALTABLE */
4960
4961#ifndef SQLITE_OMIT_VIRTUALTABLE
4962/* Opcode: VColumn P1 P2 *
4963**
4964** Push onto the stack the value of the P2-th column of
4965** the row of the virtual-table that the P1 cursor is pointing to.
4966*/
4967case OP_VColumn: {
4968 const sqlite3_module *pModule;
4969
4970 Cursor *pCur = p->apCsr[pOp->p1];
4971 assert( pCur->pVtabCursor );
4972 pModule = pCur->pVtabCursor->pVtab->pModule;
4973 if( pModule->xColumn==0 ){
4974 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0);
4975 rc = SQLITE_ERROR;
4976 } else {
4977 sqlite3_context sContext;
4978 memset(&sContext, 0, sizeof(sContext));
4979 sContext.s.flags = MEM_Null;
4980 sContext.s.db = db;
4981 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4982 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
4983
4984 /* Copy the result of the function to the top of the stack. We
4985 ** do this regardless of whether or not an error occured to ensure any
4986 ** dynamic allocation in sContext.s (a Mem struct) is released.
4987 */
4988 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
4989 pTos++;
4990 pTos->flags = 0;
4991 sqlite3VdbeMemMove(pTos, &sContext.s);
4992
4993 if( sqlite3SafetyOn(db) ){
4994 goto abort_due_to_misuse;
4995 }
4996 if( sqlite3VdbeMemTooBig(pTos) ){
4997 goto too_big;
4998 }
4999 }
5000
5001 break;
5002}
5003#endif /* SQLITE_OMIT_VIRTUALTABLE */
5004
5005#ifndef SQLITE_OMIT_VIRTUALTABLE
5006/* Opcode: VNext P1 P2 *
5007**
5008** Advance virtual table P1 to the next row in its result set and
5009** jump to instruction P2. Or, if the virtual table has reached
5010** the end of its result set, then fall through to the next instruction.
5011*/
5012case OP_VNext: { /* no-push */
5013 const sqlite3_module *pModule;
5014 int res = 0;
5015
5016 Cursor *pCur = p->apCsr[pOp->p1];
5017 assert( pCur->pVtabCursor );
5018 pModule = pCur->pVtabCursor->pVtab->pModule;
5019 if( pModule->xNext==0 ){
5020 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0);
5021 rc = SQLITE_ERROR;
5022 } else {
5023 /* Invoke the xNext() method of the module. There is no way for the
5024 ** underlying implementation to return an error if one occurs during
5025 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5026 ** data is available) and the error code returned when xColumn or
5027 ** some other method is next invoked on the save virtual table cursor.
5028 */
5029 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5030 p->inVtabMethod = 1;
5031 rc = pModule->xNext(pCur->pVtabCursor);
5032 p->inVtabMethod = 0;
5033 if( rc==SQLITE_OK ){
5034 res = pModule->xEof(pCur->pVtabCursor);
5035 }
5036 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5037
5038 if( !res ){
5039 /* If there is data, jump to P2 */
5040 pc = pOp->p2 - 1;
5041 }
5042 }
5043
5044 break;
5045}
5046#endif /* SQLITE_OMIT_VIRTUALTABLE */
5047
5048#ifndef SQLITE_OMIT_VIRTUALTABLE
5049/* Opcode: VRename * * P3
5050**
5051** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
5052** This opcode invokes the corresponding xRename method. The value
5053** on the top of the stack is popped and passed as the zName argument
5054** to the xRename method.
5055*/
5056case OP_VRename: { /* no-push */
5057 sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
5058 assert( pVtab->pModule->xRename );
5059
5060 Stringify(pTos, encoding);
5061
5062 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5063 sqlite3VtabLock(pVtab);
5064 rc = pVtab->pModule->xRename(pVtab, pTos->z);
5065 sqlite3VtabUnlock(db, pVtab);
5066 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5067
5068 popStack(&pTos, 1);
5069 break;
5070}
5071#endif
5072
5073#ifndef SQLITE_OMIT_VIRTUALTABLE
5074/* Opcode: VUpdate P1 P2 P3
5075**
5076** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
5077** This opcode invokes the corresponding xUpdate method. P2 values
5078** are taken from the stack to pass to the xUpdate invocation. The
5079** value on the top of the stack corresponds to the p2th element
5080** of the argv array passed to xUpdate.
5081**
5082** The xUpdate method will do a DELETE or an INSERT or both.
5083** The argv[0] element (which corresponds to the P2-th element down
5084** on the stack) is the rowid of a row to delete. If argv[0] is
5085** NULL then no deletion occurs. The argv[1] element is the rowid
5086** of the new row. This can be NULL to have the virtual table
5087** select the new rowid for itself. The higher elements in the
5088** stack are the values of columns in the new row.
5089**
5090** If P2==1 then no insert is performed. argv[0] is the rowid of
5091** a row to delete.
5092**
5093** P1 is a boolean flag. If it is set to true and the xUpdate call
5094** is successful, then the value returned by sqlite3_last_insert_rowid()
5095** is set to the value of the rowid for the row just inserted.
5096*/
5097case OP_VUpdate: { /* no-push */
5098 sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
5099 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
5100 int nArg = pOp->p2;
5101 assert( pOp->p3type==P3_VTAB );
5102 if( pModule->xUpdate==0 ){
5103 sqlite3SetString(&p->zErrMsg, "read-only table", 0);
5104 rc = SQLITE_ERROR;
5105 }else{
5106 int i;
5107 sqlite_int64 rowid;
5108 Mem **apArg = p->apArg;
5109 Mem *pX = &pTos[1-nArg];
5110 for(i = 0; i<nArg; i++, pX++){
5111 storeTypeInfo(pX, 0);
5112 apArg[i] = pX;
5113 }
5114 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5115 sqlite3VtabLock(pVtab);
5116 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
5117 sqlite3VtabUnlock(db, pVtab);
5118 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5119 if( pOp->p1 && rc==SQLITE_OK ){
5120 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5121 db->lastRowid = rowid;
5122 }
5123 }
5124 popStack(&pTos, nArg);
5125 break;
5126}
5127#endif /* SQLITE_OMIT_VIRTUALTABLE */
5128
5129/* An other opcode is illegal...
5130*/
5131default: {
5132 assert( 0 );
5133 break;
5134}
5135
5136/*****************************************************************************
5137** The cases of the switch statement above this line should all be indented
5138** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5139** readability. From this point on down, the normal indentation rules are
5140** restored.
5141*****************************************************************************/
5142 }
5143
5144 /* Make sure the stack limit was not exceeded */
5145 assert( pTos<=pStackLimit );
5146
5147#ifdef VDBE_PROFILE
5148 {
5149 long long elapse = hwtime() - start;
5150 pOp->cycles += elapse;
5151 pOp->cnt++;
5152#if 0
5153 fprintf(stdout, "%10lld ", elapse);
5154 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
5155#endif
5156 }
5157#endif
5158
5159#ifdef SQLITE_TEST
5160 /* Keep track of the size of the largest BLOB or STR that has appeared
5161 ** on the top of the VDBE stack.
5162 */
5163 if( pTos>=p->aStack && (pTos->flags & (MEM_Blob|MEM_Str))!=0
5164 && pTos->n>sqlite3_max_blobsize ){
5165 sqlite3_max_blobsize = pTos->n;
5166 }
5167#endif
5168
5169 /* The following code adds nothing to the actual functionality
5170 ** of the program. It is only here for testing and debugging.
5171 ** On the other hand, it does burn CPU cycles every time through
5172 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5173 */
5174#ifndef NDEBUG
5175 /* Sanity checking on the top element of the stack. If the previous
5176 ** instruction was VNoChange, then the flags field of the top
5177 ** of the stack is set to 0. This is technically invalid for a memory
5178 ** cell, so avoid calling MemSanity() in this case.
5179 */
5180 if( pTos>=p->aStack && pTos->flags ){
5181 assert( pTos->db==db );
5182 sqlite3VdbeMemSanity(pTos);
5183 assert( !sqlite3VdbeMemTooBig(pTos) );
5184 }
5185 assert( pc>=-1 && pc<p->nOp );
5186
5187#ifdef SQLITE_DEBUG
5188 /* Code for tracing the vdbe stack. */
5189 if( p->trace && pTos>=p->aStack ){
5190 int i;
5191 fprintf(p->trace, "Stack:");
5192 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
5193 if( pTos[i].flags & MEM_Null ){
5194 fprintf(p->trace, " NULL");
5195 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
5196 fprintf(p->trace, " si:%lld", pTos[i].u.i);
5197 }else if( pTos[i].flags & MEM_Int ){
5198 fprintf(p->trace, " i:%lld", pTos[i].u.i);
5199 }else if( pTos[i].flags & MEM_Real ){
5200 fprintf(p->trace, " r:%g", pTos[i].r);
5201 }else{
5202 char zBuf[200];
5203 sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf);
5204 fprintf(p->trace, " ");
5205 fprintf(p->trace, "%s", zBuf);
5206 }
5207 }
5208 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
5209 fprintf(p->trace,"\n");
5210 }
5211#endif /* SQLITE_DEBUG */
5212#endif /* NDEBUG */
5213 } /* The end of the for(;;) loop the loops through opcodes */
5214
5215 /* If we reach this point, it means that execution is finished.
5216 */
5217vdbe_halt:
5218 if( rc ){
5219 p->rc = rc;
5220 rc = SQLITE_ERROR;
5221 }else{
5222 rc = SQLITE_DONE;
5223 }
5224 sqlite3VdbeHalt(p);
5225 p->pTos = pTos;
5226
5227 /* This is the only way out of this procedure. We have to
5228 ** release the mutexes on btrees that were acquired at the
5229 ** top. */
5230vdbe_return:
5231 sqlite3BtreeMutexArrayLeave(&p->aMutex);
5232 return rc;
5233
5234 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5235 ** is encountered.
5236 */
5237too_big:
5238 sqlite3SetString(&p->zErrMsg, "string or blob too big", (char*)0);
5239 rc = SQLITE_TOOBIG;
5240 goto vdbe_halt;
5241
5242 /* Jump to here if a malloc() fails.
5243 */
5244no_mem:
5245 db->mallocFailed = 1;
5246 sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
5247 rc = SQLITE_NOMEM;
5248 goto vdbe_halt;
5249
5250 /* Jump to here for an SQLITE_MISUSE error.
5251 */
5252abort_due_to_misuse:
5253 rc = SQLITE_MISUSE;
5254 /* Fall thru into abort_due_to_error */
5255
5256 /* Jump to here for any other kind of fatal error. The "rc" variable
5257 ** should hold the error number.
5258 */
5259abort_due_to_error:
5260 if( p->zErrMsg==0 ){
5261 if( db->mallocFailed ) rc = SQLITE_NOMEM;
5262 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
5263 }
5264 goto vdbe_halt;
5265
5266 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
5267 ** flag.
5268 */
5269abort_due_to_interrupt:
5270 assert( db->u1.isInterrupted );
5271 if( db->magic!=SQLITE_MAGIC_BUSY ){
5272 rc = SQLITE_MISUSE;
5273 }else{
5274 rc = SQLITE_INTERRUPT;
5275 }
5276 p->rc = rc;
5277 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
5278 goto vdbe_halt;
5279}
diff --git a/libraries/sqlite/win32/vdbe.h b/libraries/sqlite/win32/vdbe.h
new file mode 100755
index 0000000..75f186c
--- /dev/null
+++ b/libraries/sqlite/win32/vdbe.h
@@ -0,0 +1,151 @@
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.
10**
11*************************************************************************
12** Header file for the Virtual DataBase Engine (VDBE)
13**
14** This header defines the interface to the virtual database engine
15** or VDBE. The VDBE implements an abstract machine that runs a
16** simple program to access and modify the underlying database.
17**
18** $Id: vdbe.h,v 1.113 2007/08/30 01:19:59 drh Exp $
19*/
20#ifndef _SQLITE_VDBE_H_
21#define _SQLITE_VDBE_H_
22#include <stdio.h>
23
24/*
25** A single VDBE is an opaque structure named "Vdbe". Only routines
26** in the source file sqliteVdbe.c are allowed to see the insides
27** of this structure.
28*/
29typedef struct Vdbe Vdbe;
30
31/*
32** A single instruction of the virtual machine has an opcode
33** and as many as three operands. The instruction is recorded
34** as an instance of the following structure:
35*/
36struct VdbeOp {
37 u8 opcode; /* What operation to perform */
38 int p1; /* First operand */
39 int p2; /* Second parameter (often the jump destination) */
40 char *p3; /* Third parameter */
41 int p3type; /* One of the P3_xxx constants defined below */
42#ifdef VDBE_PROFILE
43 int cnt; /* Number of times this instruction was executed */
44 long long cycles; /* Total time spend executing this instruction */
45#endif
46};
47typedef struct VdbeOp VdbeOp;
48
49/*
50** A smaller version of VdbeOp used for the VdbeAddOpList() function because
51** it takes up less space.
52*/
53struct VdbeOpList {
54 u8 opcode; /* What operation to perform */
55 signed char p1; /* First operand */
56 short int p2; /* Second parameter (often the jump destination) */
57 char *p3; /* Third parameter */
58};
59typedef struct VdbeOpList VdbeOpList;
60
61/*
62** Allowed values of VdbeOp.p3type
63*/
64#define P3_NOTUSED 0 /* The P3 parameter is not used */
65#define P3_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
66#define P3_STATIC (-2) /* Pointer to a static string */
67#define P3_COLLSEQ (-4) /* P3 is a pointer to a CollSeq structure */
68#define P3_FUNCDEF (-5) /* P3 is a pointer to a FuncDef structure */
69#define P3_KEYINFO (-6) /* P3 is a pointer to a KeyInfo structure */
70#define P3_VDBEFUNC (-7) /* P3 is a pointer to a VdbeFunc structure */
71#define P3_MEM (-8) /* P3 is a pointer to a Mem* structure */
72#define P3_TRANSIENT (-9) /* P3 is a pointer to a transient string */
73#define P3_VTAB (-10) /* P3 is a pointer to an sqlite3_vtab structure */
74#define P3_MPRINTF (-11) /* P3 is a string obtained from sqlite3_mprintf() */
75
76/* When adding a P3 argument using P3_KEYINFO, a copy of the KeyInfo structure
77** is made. That copy is freed when the Vdbe is finalized. But if the
78** argument is P3_KEYINFO_HANDOFF, the passed in pointer is used. It still
79** gets freed when the Vdbe is finalized so it still should be obtained
80** from a single sqliteMalloc(). But no copy is made and the calling
81** function should *not* try to free the KeyInfo.
82*/
83#define P3_KEYINFO_HANDOFF (-9)
84
85/*
86** The Vdbe.aColName array contains 5n Mem structures, where n is the
87** number of columns of data returned by the statement.
88*/
89#define COLNAME_NAME 0
90#define COLNAME_DECLTYPE 1
91#define COLNAME_DATABASE 2
92#define COLNAME_TABLE 3
93#define COLNAME_COLUMN 4
94#define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
95
96/*
97** The following macro converts a relative address in the p2 field
98** of a VdbeOp structure into a negative number so that
99** sqlite3VdbeAddOpList() knows that the address is relative. Calling
100** the macro again restores the address.
101*/
102#define ADDR(X) (-1-(X))
103
104/*
105** The makefile scans the vdbe.c source file and creates the "opcodes.h"
106** header file that defines a number for each opcode used by the VDBE.
107*/
108#include "opcodes.h"
109
110/*
111** Prototypes for the VDBE interface. See comments on the implementation
112** for a description of what each of these routines does.
113*/
114Vdbe *sqlite3VdbeCreate(sqlite3*);
115int sqlite3VdbeAddOp(Vdbe*,int,int,int);
116int sqlite3VdbeOp3(Vdbe*,int,int,int,const char *zP3,int);
117int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
118void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
119void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
120void sqlite3VdbeJumpHere(Vdbe*, int addr);
121void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
122void sqlite3VdbeChangeP3(Vdbe*, int addr, const char *zP1, int N);
123void sqlite3VdbeUsesBtree(Vdbe*, int);
124VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
125int sqlite3VdbeMakeLabel(Vdbe*);
126void sqlite3VdbeDelete(Vdbe*);
127void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int);
128int sqlite3VdbeFinalize(Vdbe*);
129void sqlite3VdbeResolveLabel(Vdbe*, int);
130int sqlite3VdbeCurrentAddr(Vdbe*);
131#ifdef SQLITE_DEBUG
132 void sqlite3VdbeTrace(Vdbe*,FILE*);
133#endif
134void sqlite3VdbeResetStepResult(Vdbe*);
135int sqlite3VdbeReset(Vdbe*);
136void sqlite3VdbeSetNumCols(Vdbe*,int);
137int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, int);
138void sqlite3VdbeCountChanges(Vdbe*);
139sqlite3 *sqlite3VdbeDb(Vdbe*);
140void sqlite3VdbeSetSql(Vdbe*, const char *z, int n);
141const char *sqlite3VdbeGetSql(Vdbe*);
142void sqlite3VdbeSwap(Vdbe*,Vdbe*);
143
144#ifndef NDEBUG
145 void sqlite3VdbeComment(Vdbe*, const char*, ...);
146# define VdbeComment(X) sqlite3VdbeComment X
147#else
148# define VdbeComment(X)
149#endif
150
151#endif
diff --git a/libraries/sqlite/win32/vdbeInt.h b/libraries/sqlite/win32/vdbeInt.h
new file mode 100755
index 0000000..a807bdd
--- /dev/null
+++ b/libraries/sqlite/win32/vdbeInt.h
@@ -0,0 +1,427 @@
1/*
2** 2003 September 6
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.
10**
11*************************************************************************
12** This is the header file for information that is private to the
13** VDBE. This information used to all be at the top of the single
14** source code file "vdbe.c". When that file became too big (over
15** 6000 lines long) it was split up into several smaller files and
16** this header information was factored out.
17*/
18#ifndef _VDBEINT_H_
19#define _VDBEINT_H_
20
21/*
22** intToKey() and keyToInt() used to transform the rowid. But with
23** the latest versions of the design they are no-ops.
24*/
25#define keyToInt(X) (X)
26#define intToKey(X) (X)
27
28
29/*
30** SQL is translated into a sequence of instructions to be
31** executed by a virtual machine. Each instruction is an instance
32** of the following structure.
33*/
34typedef struct VdbeOp Op;
35
36/*
37** Boolean values
38*/
39typedef unsigned char Bool;
40
41/*
42** A cursor is a pointer into a single BTree within a database file.
43** The cursor can seek to a BTree entry with a particular key, or
44** loop over all entries of the Btree. You can also insert new BTree
45** entries or retrieve the key or data from the entry that the cursor
46** is currently pointing to.
47**
48** Every cursor that the virtual machine has open is represented by an
49** instance of the following structure.
50**
51** If the Cursor.isTriggerRow flag is set it means that this cursor is
52** really a single row that represents the NEW or OLD pseudo-table of
53** a row trigger. The data for the row is stored in Cursor.pData and
54** the rowid is in Cursor.iKey.
55*/
56struct Cursor {
57 BtCursor *pCursor; /* The cursor structure of the backend */
58 int iDb; /* Index of cursor database in db->aDb[] (or -1) */
59 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
60 i64 nextRowid; /* Next rowid returned by OP_NewRowid */
61 Bool zeroed; /* True if zeroed out and ready for reuse */
62 Bool rowidIsValid; /* True if lastRowid is valid */
63 Bool atFirst; /* True if pointing to first entry */
64 Bool useRandomRowid; /* Generate new record numbers semi-randomly */
65 Bool nullRow; /* True if pointing to a row with no data */
66 Bool nextRowidValid; /* True if the nextRowid field is valid */
67 Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */
68 Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
69 Bool isTable; /* True if a table requiring integer keys */
70 Bool isIndex; /* True if an index containing keys only - no data */
71 u8 bogusIncrKey; /* Something for pIncrKey to point to if pKeyInfo==0 */
72 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
73 Btree *pBt; /* Separate file holding temporary table */
74 int nData; /* Number of bytes in pData */
75 char *pData; /* Data for a NEW or OLD pseudo-table */
76 i64 iKey; /* Key for the NEW or OLD pseudo-table row */
77 u8 *pIncrKey; /* Pointer to pKeyInfo->incrKey */
78 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
79 int nField; /* Number of fields in the header */
80 i64 seqCount; /* Sequence counter */
81 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
82 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
83
84 /* Cached information about the header for the data record that the
85 ** cursor is currently pointing to. Only valid if cacheValid is true.
86 ** aRow might point to (ephemeral) data for the current row, or it might
87 ** be NULL.
88 */
89 int cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
90 int payloadSize; /* Total number of bytes in the record */
91 u32 *aType; /* Type values for all entries in the record */
92 u32 *aOffset; /* Cached offsets to the start of each columns data */
93 u8 *aRow; /* Data for the current row, if all on one page */
94};
95typedef struct Cursor Cursor;
96
97/*
98** Number of bytes of string storage space available to each stack
99** layer without having to malloc. NBFS is short for Number of Bytes
100** For Strings.
101*/
102#define NBFS 32
103
104/*
105** A value for Cursor.cacheValid that means the cache is always invalid.
106*/
107#define CACHE_STALE 0
108
109/*
110** Internally, the vdbe manipulates nearly all SQL values as Mem
111** structures. Each Mem struct may cache multiple representations (string,
112** integer etc.) of the same value. A value (and therefore Mem structure)
113** has the following properties:
114**
115** Each value has a manifest type. The manifest type of the value stored
116** in a Mem struct is returned by the MemType(Mem*) macro. The type is
117** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
118** SQLITE_BLOB.
119*/
120struct Mem {
121 union {
122 i64 i; /* Integer value. Or FuncDef* when flags==MEM_Agg */
123 FuncDef *pDef; /* Used only when flags==MEM_Agg */
124 } u;
125 double r; /* Real value */
126 sqlite3 *db; /* The associated database connection */
127 char *z; /* String or BLOB value */
128 int n; /* Number of characters in string value, including '\0' */
129 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
130 u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
131 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
132 void (*xDel)(void *); /* If not null, call this function to delete Mem.z */
133 char zShort[NBFS]; /* Space for short strings */
134};
135typedef struct Mem Mem;
136
137/* One or more of the following flags are set to indicate the validOK
138** representations of the value stored in the Mem struct.
139**
140** If the MEM_Null flag is set, then the value is an SQL NULL value.
141** No other flags may be set in this case.
142**
143** If the MEM_Str flag is set then Mem.z points at a string representation.
144** Usually this is encoded in the same unicode encoding as the main
145** database (see below for exceptions). If the MEM_Term flag is also
146** set, then the string is nul terminated. The MEM_Int and MEM_Real
147** flags may coexist with the MEM_Str flag.
148**
149** Multiple of these values can appear in Mem.flags. But only one
150** at a time can appear in Mem.type.
151*/
152#define MEM_Null 0x0001 /* Value is NULL */
153#define MEM_Str 0x0002 /* Value is a string */
154#define MEM_Int 0x0004 /* Value is an integer */
155#define MEM_Real 0x0008 /* Value is a real number */
156#define MEM_Blob 0x0010 /* Value is a BLOB */
157
158/* Whenever Mem contains a valid string or blob representation, one of
159** the following flags must be set to determine the memory management
160** policy for Mem.z. The MEM_Term flag tells us whether or not the
161** string is \000 or \u0000 terminated
162*/
163#define MEM_Term 0x0020 /* String rep is nul terminated */
164#define MEM_Dyn 0x0040 /* Need to call sqliteFree() on Mem.z */
165#define MEM_Static 0x0080 /* Mem.z points to a static string */
166#define MEM_Ephem 0x0100 /* Mem.z points to an ephemeral string */
167#define MEM_Short 0x0200 /* Mem.z points to Mem.zShort */
168#define MEM_Agg 0x0400 /* Mem.z points to an agg function context */
169#define MEM_Zero 0x0800 /* Mem.i contains count of 0s appended to blob */
170
171#ifdef SQLITE_OMIT_INCRBLOB
172 #undef MEM_Zero
173 #define MEM_Zero 0x0000
174#endif
175
176
177/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
178** additional information about auxiliary information bound to arguments
179** of the function. This is used to implement the sqlite3_get_auxdata()
180** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data
181** that can be associated with a constant argument to a function. This
182** allows functions such as "regexp" to compile their constant regular
183** expression argument once and reused the compiled code for multiple
184** invocations.
185*/
186struct VdbeFunc {
187 FuncDef *pFunc; /* The definition of the function */
188 int nAux; /* Number of entries allocated for apAux[] */
189 struct AuxData {
190 void *pAux; /* Aux data for the i-th argument */
191 void (*xDelete)(void *); /* Destructor for the aux data */
192 } apAux[1]; /* One slot for each function argument */
193};
194typedef struct VdbeFunc VdbeFunc;
195
196/*
197** The "context" argument for a installable function. A pointer to an
198** instance of this structure is the first argument to the routines used
199** implement the SQL functions.
200**
201** There is a typedef for this structure in sqlite.h. So all routines,
202** even the public interface to SQLite, can use a pointer to this structure.
203** But this file is the only place where the internal details of this
204** structure are known.
205**
206** This structure is defined inside of vdbeInt.h because it uses substructures
207** (Mem) which are only defined there.
208*/
209struct sqlite3_context {
210 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
211 VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */
212 Mem s; /* The return value is stored here */
213 Mem *pMem; /* Memory cell used to store aggregate context */
214 u8 isError; /* Set to true for an error */
215 CollSeq *pColl; /* Collating sequence */
216};
217
218/*
219** A Set structure is used for quick testing to see if a value
220** is part of a small set. Sets are used to implement code like
221** this:
222** x.y IN ('hi','hoo','hum')
223*/
224typedef struct Set Set;
225struct Set {
226 Hash hash; /* A set is just a hash table */
227 HashElem *prev; /* Previously accessed hash elemen */
228};
229
230/*
231** A FifoPage structure holds a single page of valves. Pages are arranged
232** in a list.
233*/
234typedef struct FifoPage FifoPage;
235struct FifoPage {
236 int nSlot; /* Number of entries aSlot[] */
237 int iWrite; /* Push the next value into this entry in aSlot[] */
238 int iRead; /* Read the next value from this entry in aSlot[] */
239 FifoPage *pNext; /* Next page in the fifo */
240 i64 aSlot[1]; /* One or more slots for rowid values */
241};
242
243/*
244** The Fifo structure is typedef-ed in vdbeInt.h. But the implementation
245** of that structure is private to this file.
246**
247** The Fifo structure describes the entire fifo.
248*/
249typedef struct Fifo Fifo;
250struct Fifo {
251 int nEntry; /* Total number of entries */
252 FifoPage *pFirst; /* First page on the list */
253 FifoPage *pLast; /* Last page on the list */
254};
255
256/*
257** A Context stores the last insert rowid, the last statement change count,
258** and the current statement change count (i.e. changes since last statement).
259** The current keylist is also stored in the context.
260** Elements of Context structure type make up the ContextStack, which is
261** updated by the ContextPush and ContextPop opcodes (used by triggers).
262** The context is pushed before executing a trigger a popped when the
263** trigger finishes.
264*/
265typedef struct Context Context;
266struct Context {
267 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
268 int nChange; /* Statement changes (Vdbe.nChanges) */
269 Fifo sFifo; /* Records that will participate in a DELETE or UPDATE */
270};
271
272/*
273** An instance of the virtual machine. This structure contains the complete
274** state of the virtual machine.
275**
276** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile()
277** is really a pointer to an instance of this structure.
278**
279** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
280** any virtual table method invocations made by the vdbe program. It is
281** set to 2 for xDestroy method calls and 1 for all other methods. This
282** variable is used for two purposes: to allow xDestroy methods to execute
283** "DROP TABLE" statements and to prevent some nasty side effects of
284** malloc failure when SQLite is invoked recursively by a virtual table
285** method function.
286*/
287struct Vdbe {
288 sqlite3 *db; /* The whole database */
289 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
290 int nOp; /* Number of instructions in the program */
291 int nOpAlloc; /* Number of slots allocated for aOp[] */
292 Op *aOp; /* Space to hold the virtual machine's program */
293 int nLabel; /* Number of labels used */
294 int nLabelAlloc; /* Number of slots allocated in aLabel[] */
295 int *aLabel; /* Space to hold the labels */
296 Mem *aStack; /* The operand stack, except string values */
297 Mem *pTos; /* Top entry in the operand stack */
298 Mem **apArg; /* Arguments to currently executing user function */
299 Mem *aColName; /* Column names to return */
300 int nCursor; /* Number of slots in apCsr[] */
301 Cursor **apCsr; /* One element of this array for each open cursor */
302 int nVar; /* Number of entries in aVar[] */
303 Mem *aVar; /* Values for the OP_Variable opcode. */
304 char **azVar; /* Name of variables */
305 int okVar; /* True if azVar[] has been initialized */
306 int magic; /* Magic number for sanity checking */
307 int nMem; /* Number of memory locations currently allocated */
308 Mem *aMem; /* The memory locations */
309 int nCallback; /* Number of callbacks invoked so far */
310 int cacheCtr; /* Cursor row cache generation counter */
311 Fifo sFifo; /* A list of ROWIDs */
312 int contextStackTop; /* Index of top element in the context stack */
313 int contextStackDepth; /* The size of the "context" stack */
314 Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
315 int pc; /* The program counter */
316 int rc; /* Value to return */
317 unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */
318 int errorAction; /* Recovery action to do in case of an error */
319 int inTempTrans; /* True if temp database is transactioned */
320 int returnStack[25]; /* Return address stack for OP_Gosub & OP_Return */
321 int returnDepth; /* Next unused element in returnStack[] */
322 int nResColumn; /* Number of columns in one row of the result set */
323 char **azResColumn; /* Values for one row of result */
324 int popStack; /* Pop the stack this much on entry to VdbeExec() */
325 char *zErrMsg; /* Error message written here */
326 u8 resOnStack; /* True if there are result values on the stack */
327 u8 explain; /* True if EXPLAIN present on SQL command */
328 u8 changeCntOn; /* True to update the change-counter */
329 u8 aborted; /* True if ROLLBACK in another VM causes an abort */
330 u8 expired; /* True if the VM needs to be recompiled */
331 u8 minWriteFileFormat; /* Minimum file format for writable database files */
332 u8 inVtabMethod; /* See comments above */
333 int nChange; /* Number of db changes made since last reset */
334 i64 startTime; /* Time when query started - used for profiling */
335 int btreeMask; /* Bitmask of db->aDb[] entries referenced */
336 BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
337 int nSql; /* Number of bytes in zSql */
338 char *zSql; /* Text of the SQL statement that generated this */
339#ifdef SQLITE_DEBUG
340 FILE *trace; /* Write an execution trace here, if not NULL */
341#endif
342 int openedStatement; /* True if this VM has opened a statement journal */
343#ifdef SQLITE_SSE
344 int fetchId; /* Statement number used by sqlite3_fetch_statement */
345 int lru; /* Counter used for LRU cache replacement */
346#endif
347};
348
349/*
350** The following are allowed values for Vdbe.magic
351*/
352#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
353#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
354#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
355#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
356
357/*
358** Function prototypes
359*/
360void sqlite3VdbeFreeCursor(Vdbe *, Cursor*);
361void sqliteVdbePopStack(Vdbe*,int);
362int sqlite3VdbeCursorMoveto(Cursor*);
363#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
364void sqlite3VdbePrintOp(FILE*, int, Op*);
365#endif
366int sqlite3VdbeSerialTypeLen(u32);
367u32 sqlite3VdbeSerialType(Mem*, int);
368int sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
369int sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
370void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
371
372int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
373int sqlite3VdbeIdxKeyCompare(Cursor*,int,const unsigned char*,int*);
374int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
375int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
376int sqlite3VdbeRecordCompare(void*,int,const void*,int, const void*);
377int sqlite3VdbeIdxRowidLen(const u8*);
378int sqlite3VdbeExec(Vdbe*);
379int sqlite3VdbeList(Vdbe*);
380int sqlite3VdbeHalt(Vdbe*);
381int sqlite3VdbeChangeEncoding(Mem *, int);
382int sqlite3VdbeMemTooBig(Mem*);
383int sqlite3VdbeMemCopy(Mem*, const Mem*);
384void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
385int sqlite3VdbeMemMove(Mem*, Mem*);
386int sqlite3VdbeMemNulTerminate(Mem*);
387int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
388void sqlite3VdbeMemSetInt64(Mem*, i64);
389void sqlite3VdbeMemSetDouble(Mem*, double);
390void sqlite3VdbeMemSetNull(Mem*);
391void sqlite3VdbeMemSetZeroBlob(Mem*,int);
392int sqlite3VdbeMemMakeWriteable(Mem*);
393int sqlite3VdbeMemDynamicify(Mem*);
394int sqlite3VdbeMemStringify(Mem*, int);
395i64 sqlite3VdbeIntValue(Mem*);
396int sqlite3VdbeMemIntegerify(Mem*);
397double sqlite3VdbeRealValue(Mem*);
398void sqlite3VdbeIntegerAffinity(Mem*);
399int sqlite3VdbeMemRealify(Mem*);
400int sqlite3VdbeMemNumerify(Mem*);
401int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
402void sqlite3VdbeMemRelease(Mem *p);
403int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
404const char *sqlite3OpcodeName(int);
405
406#ifndef NDEBUG
407 void sqlite3VdbeMemSanity(Mem*);
408 int sqlite3VdbeOpcodeNoPush(u8);
409#endif
410int sqlite3VdbeMemTranslate(Mem*, u8);
411#ifdef SQLITE_DEBUG
412 void sqlite3VdbePrintSql(Vdbe*);
413 void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
414#endif
415int sqlite3VdbeMemHandleBom(Mem *pMem);
416void sqlite3VdbeFifoInit(Fifo*);
417int sqlite3VdbeFifoPush(Fifo*, i64);
418int sqlite3VdbeFifoPop(Fifo*, i64*);
419void sqlite3VdbeFifoClear(Fifo*);
420
421#ifndef SQLITE_OMIT_INCRBLOB
422 int sqlite3VdbeMemExpandBlob(Mem *);
423#else
424 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
425#endif
426
427#endif /* !defined(_VDBEINT_H_) */
diff --git a/libraries/sqlite/win32/vdbeapi.c b/libraries/sqlite/win32/vdbeapi.c
new file mode 100755
index 0000000..9445c76
--- /dev/null
+++ b/libraries/sqlite/win32/vdbeapi.c
@@ -0,0 +1,1066 @@
1/*
2** 2004 May 26
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.
10**
11*************************************************************************
12**
13** This file contains code use to implement APIs that are part of the
14** VDBE.
15*/
16#include "sqliteInt.h"
17#include "vdbeInt.h"
18
19/*
20** Return TRUE (non-zero) of the statement supplied as an argument needs
21** to be recompiled. A statement needs to be recompiled whenever the
22** execution environment changes in a way that would alter the program
23** that sqlite3_prepare() generates. For example, if new functions or
24** collating sequences are registered or if an authorizer function is
25** added or changed.
26*/
27int sqlite3_expired(sqlite3_stmt *pStmt){
28 Vdbe *p = (Vdbe*)pStmt;
29 return p==0 || p->expired;
30}
31
32/*
33** The following routine destroys a virtual machine that is created by
34** the sqlite3_compile() routine. The integer returned is an SQLITE_
35** success/failure code that describes the result of executing the virtual
36** machine.
37**
38** This routine sets the error code and string returned by
39** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
40*/
41int sqlite3_finalize(sqlite3_stmt *pStmt){
42 int rc;
43 if( pStmt==0 ){
44 rc = SQLITE_OK;
45 }else{
46 Vdbe *v = (Vdbe*)pStmt;
47 sqlite3_mutex *mutex = v->db->mutex;
48 sqlite3_mutex_enter(mutex);
49 rc = sqlite3VdbeFinalize(v);
50 sqlite3_mutex_leave(mutex);
51 }
52 return rc;
53}
54
55/*
56** Terminate the current execution of an SQL statement and reset it
57** back to its starting state so that it can be reused. A success code from
58** the prior execution is returned.
59**
60** This routine sets the error code and string returned by
61** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
62*/
63int sqlite3_reset(sqlite3_stmt *pStmt){
64 int rc;
65 if( pStmt==0 ){
66 rc = SQLITE_OK;
67 }else{
68 Vdbe *v = (Vdbe*)pStmt;
69 sqlite3_mutex_enter(v->db->mutex);
70 rc = sqlite3VdbeReset(v);
71 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
72 assert( (rc & (v->db->errMask))==rc );
73 sqlite3_mutex_leave(v->db->mutex);
74 }
75 return rc;
76}
77
78/*
79** Set all the parameters in the compiled SQL statement to NULL.
80*/
81int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
82 int i;
83 int rc = SQLITE_OK;
84 Vdbe *v = (Vdbe*)pStmt;
85 sqlite3_mutex_enter(v->db->mutex);
86 for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
87 rc = sqlite3_bind_null(pStmt, i);
88 }
89 sqlite3_mutex_leave(v->db->mutex);
90 return rc;
91}
92
93
94/**************************** sqlite3_value_ *******************************
95** The following routines extract information from a Mem or sqlite3_value
96** structure.
97*/
98const void *sqlite3_value_blob(sqlite3_value *pVal){
99 Mem *p = (Mem*)pVal;
100 if( p->flags & (MEM_Blob|MEM_Str) ){
101 sqlite3VdbeMemExpandBlob(p);
102 p->flags &= ~MEM_Str;
103 p->flags |= MEM_Blob;
104 return p->z;
105 }else{
106 return sqlite3_value_text(pVal);
107 }
108}
109int sqlite3_value_bytes(sqlite3_value *pVal){
110 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
111}
112int sqlite3_value_bytes16(sqlite3_value *pVal){
113 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
114}
115double sqlite3_value_double(sqlite3_value *pVal){
116 return sqlite3VdbeRealValue((Mem*)pVal);
117}
118int sqlite3_value_int(sqlite3_value *pVal){
119 return sqlite3VdbeIntValue((Mem*)pVal);
120}
121sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
122 return sqlite3VdbeIntValue((Mem*)pVal);
123}
124const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
125 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
126}
127#ifndef SQLITE_OMIT_UTF16
128const void *sqlite3_value_text16(sqlite3_value* pVal){
129 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
130}
131const void *sqlite3_value_text16be(sqlite3_value *pVal){
132 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
133}
134const void *sqlite3_value_text16le(sqlite3_value *pVal){
135 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
136}
137#endif /* SQLITE_OMIT_UTF16 */
138int sqlite3_value_type(sqlite3_value* pVal){
139 return pVal->type;
140}
141
142/**************************** sqlite3_result_ *******************************
143** The following routines are used by user-defined functions to specify
144** the function result.
145*/
146void sqlite3_result_blob(
147 sqlite3_context *pCtx,
148 const void *z,
149 int n,
150 void (*xDel)(void *)
151){
152 assert( n>=0 );
153 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
154 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
155}
156void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
157 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
158 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
159}
160void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
161 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
162 pCtx->isError = 1;
163 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
164}
165#ifndef SQLITE_OMIT_UTF16
166void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
167 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
168 pCtx->isError = 1;
169 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
170}
171#endif
172void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
173 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
174 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
175}
176void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
177 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
178 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
179}
180void sqlite3_result_null(sqlite3_context *pCtx){
181 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
182 sqlite3VdbeMemSetNull(&pCtx->s);
183}
184void sqlite3_result_text(
185 sqlite3_context *pCtx,
186 const char *z,
187 int n,
188 void (*xDel)(void *)
189){
190 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
191 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
192}
193#ifndef SQLITE_OMIT_UTF16
194void sqlite3_result_text16(
195 sqlite3_context *pCtx,
196 const void *z,
197 int n,
198 void (*xDel)(void *)
199){
200 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
201 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
202}
203void sqlite3_result_text16be(
204 sqlite3_context *pCtx,
205 const void *z,
206 int n,
207 void (*xDel)(void *)
208){
209 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
210 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
211}
212void sqlite3_result_text16le(
213 sqlite3_context *pCtx,
214 const void *z,
215 int n,
216 void (*xDel)(void *)
217){
218 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
219 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
220}
221#endif /* SQLITE_OMIT_UTF16 */
222void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
223 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
224 sqlite3VdbeMemCopy(&pCtx->s, pValue);
225}
226void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
227 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
228 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
229}
230
231/* Force an SQLITE_TOOBIG error. */
232void sqlite3_result_error_toobig(sqlite3_context *pCtx){
233 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
234 sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
235}
236
237/* An SQLITE_NOMEM error. */
238void sqlite3_result_error_nomem(sqlite3_context *pCtx){
239 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
240 sqlite3VdbeMemSetNull(&pCtx->s);
241 pCtx->isError = 1;
242 pCtx->s.db->mallocFailed = 1;
243}
244
245/*
246** Execute the statement pStmt, either until a row of data is ready, the
247** statement is completely executed or an error occurs.
248**
249** This routine implements the bulk of the logic behind the sqlite_step()
250** API. The only thing omitted is the automatic recompile if a
251** schema change has occurred. That detail is handled by the
252** outer sqlite3_step() wrapper procedure.
253*/
254static int sqlite3Step(Vdbe *p){
255 sqlite3 *db;
256 int rc;
257
258 /* Assert that malloc() has not failed */
259 db = p->db;
260 assert( !db->mallocFailed );
261
262 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
263 return SQLITE_MISUSE;
264 }
265 if( p->aborted ){
266 return SQLITE_ABORT;
267 }
268 if( p->pc<=0 && p->expired ){
269 if( p->rc==SQLITE_OK ){
270 p->rc = SQLITE_SCHEMA;
271 }
272 rc = SQLITE_ERROR;
273 goto end_of_step;
274 }
275 if( sqlite3SafetyOn(db) ){
276 p->rc = SQLITE_MISUSE;
277 return SQLITE_MISUSE;
278 }
279 if( p->pc<0 ){
280 /* If there are no other statements currently running, then
281 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
282 ** from interrupting a statement that has not yet started.
283 */
284 if( db->activeVdbeCnt==0 ){
285 db->u1.isInterrupted = 0;
286 }
287
288#ifndef SQLITE_OMIT_TRACE
289 /* Invoke the trace callback if there is one
290 */
291 if( db->xTrace && !db->init.busy ){
292 assert( p->nOp>0 );
293 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
294 assert( p->aOp[p->nOp-1].p3!=0 );
295 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
296 sqlite3SafetyOff(db);
297 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
298 if( sqlite3SafetyOn(db) ){
299 p->rc = SQLITE_MISUSE;
300 return SQLITE_MISUSE;
301 }
302 }
303 if( db->xProfile && !db->init.busy ){
304 double rNow;
305 sqlite3OsCurrentTime(db->pVfs, &rNow);
306 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
307 }
308#endif
309
310 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
311 ** on in debugging mode.
312 */
313#ifdef SQLITE_DEBUG
314 if( (db->flags & SQLITE_SqlTrace)!=0 ){
315 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
316 }
317#endif /* SQLITE_DEBUG */
318
319 db->activeVdbeCnt++;
320 p->pc = 0;
321 }
322#ifndef SQLITE_OMIT_EXPLAIN
323 if( p->explain ){
324 rc = sqlite3VdbeList(p);
325 }else
326#endif /* SQLITE_OMIT_EXPLAIN */
327 {
328 rc = sqlite3VdbeExec(p);
329 }
330
331 if( sqlite3SafetyOff(db) ){
332 rc = SQLITE_MISUSE;
333 }
334
335#ifndef SQLITE_OMIT_TRACE
336 /* Invoke the profile callback if there is one
337 */
338 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
339 double rNow;
340 u64 elapseTime;
341
342 sqlite3OsCurrentTime(db->pVfs, &rNow);
343 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
344 assert( p->nOp>0 );
345 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
346 assert( p->aOp[p->nOp-1].p3!=0 );
347 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
348 db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
349 }
350#endif
351
352 sqlite3Error(p->db, rc, 0);
353 p->rc = sqlite3ApiExit(p->db, p->rc);
354end_of_step:
355 assert( (rc&0xff)==rc );
356 if( p->zSql && (rc&0xff)<SQLITE_ROW ){
357 /* This behavior occurs if sqlite3_prepare_v2() was used to build
358 ** the prepared statement. Return error codes directly */
359 sqlite3Error(p->db, p->rc, 0);
360 return p->rc;
361 }else{
362 /* This is for legacy sqlite3_prepare() builds and when the code
363 ** is SQLITE_ROW or SQLITE_DONE */
364 return rc;
365 }
366}
367
368/*
369** This is the top-level implementation of sqlite3_step(). Call
370** sqlite3Step() to do most of the work. If a schema error occurs,
371** call sqlite3Reprepare() and try again.
372*/
373#ifdef SQLITE_OMIT_PARSER
374int sqlite3_step(sqlite3_stmt *pStmt){
375 int rc;
376 Vdbe *v;
377 v = (Vdbe*)pStmt;
378 sqlite3_mutex_enter(v->db->mutex);
379 rc = sqlite3Step(v);
380 sqlite3_mutex_leave(v->db->mutex);
381 return rc;
382}
383#else
384int sqlite3_step(sqlite3_stmt *pStmt){
385 int cnt = 0;
386 int rc;
387 Vdbe *v = (Vdbe*)pStmt;
388 sqlite3_mutex_enter(v->db->mutex);
389 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
390 && cnt++ < 5
391 && sqlite3Reprepare(v) ){
392 sqlite3_reset(pStmt);
393 v->expired = 0;
394 }
395 sqlite3_mutex_leave(v->db->mutex);
396 return rc;
397}
398#endif
399
400/*
401** Extract the user data from a sqlite3_context structure and return a
402** pointer to it.
403*/
404void *sqlite3_user_data(sqlite3_context *p){
405 assert( p && p->pFunc );
406 return p->pFunc->pUserData;
407}
408
409/*
410** The following is the implementation of an SQL function that always
411** fails with an error message stating that the function is used in the
412** wrong context. The sqlite3_overload_function() API might construct
413** SQL function that use this routine so that the functions will exist
414** for name resolution but are actually overloaded by the xFindFunction
415** method of virtual tables.
416*/
417void sqlite3InvalidFunction(
418 sqlite3_context *context, /* The function calling context */
419 int argc, /* Number of arguments to the function */
420 sqlite3_value **argv /* Value of each argument */
421){
422 const char *zName = context->pFunc->zName;
423 char *zErr;
424 zErr = sqlite3MPrintf(0,
425 "unable to use function %s in the requested context", zName);
426 sqlite3_result_error(context, zErr, -1);
427 sqlite3_free(zErr);
428}
429
430/*
431** Allocate or return the aggregate context for a user function. A new
432** context is allocated on the first call. Subsequent calls return the
433** same context that was returned on prior calls.
434*/
435void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
436 Mem *pMem;
437 assert( p && p->pFunc && p->pFunc->xStep );
438 assert( sqlite3_mutex_held(p->s.db->mutex) );
439 pMem = p->pMem;
440 if( (pMem->flags & MEM_Agg)==0 ){
441 if( nByte==0 ){
442 assert( pMem->flags==MEM_Null );
443 pMem->z = 0;
444 }else{
445 pMem->flags = MEM_Agg;
446 pMem->xDel = sqlite3_free;
447 pMem->u.pDef = p->pFunc;
448 if( nByte<=NBFS ){
449 pMem->z = pMem->zShort;
450 memset(pMem->z, 0, nByte);
451 }else{
452 pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
453 }
454 }
455 }
456 return (void*)pMem->z;
457}
458
459/*
460** Return the auxilary data pointer, if any, for the iArg'th argument to
461** the user-function defined by pCtx.
462*/
463void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
464 VdbeFunc *pVdbeFunc;
465
466 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
467 pVdbeFunc = pCtx->pVdbeFunc;
468 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
469 return 0;
470 }
471 return pVdbeFunc->apAux[iArg].pAux;
472}
473
474/*
475** Set the auxilary data pointer and delete function, for the iArg'th
476** argument to the user-function defined by pCtx. Any previous value is
477** deleted by calling the delete function specified when it was set.
478*/
479void sqlite3_set_auxdata(
480 sqlite3_context *pCtx,
481 int iArg,
482 void *pAux,
483 void (*xDelete)(void*)
484){
485 struct AuxData *pAuxData;
486 VdbeFunc *pVdbeFunc;
487 if( iArg<0 ) goto failed;
488
489 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
490 pVdbeFunc = pCtx->pVdbeFunc;
491 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
492 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
493 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
494 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
495 if( !pVdbeFunc ){
496 goto failed;
497 }
498 pCtx->pVdbeFunc = pVdbeFunc;
499 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
500 pVdbeFunc->nAux = iArg+1;
501 pVdbeFunc->pFunc = pCtx->pFunc;
502 }
503
504 pAuxData = &pVdbeFunc->apAux[iArg];
505 if( pAuxData->pAux && pAuxData->xDelete ){
506 pAuxData->xDelete(pAuxData->pAux);
507 }
508 pAuxData->pAux = pAux;
509 pAuxData->xDelete = xDelete;
510 return;
511
512failed:
513 if( xDelete ){
514 xDelete(pAux);
515 }
516}
517
518/*
519** Return the number of times the Step function of a aggregate has been
520** called.
521**
522** This function is deprecated. Do not use it for new code. It is
523** provide only to avoid breaking legacy code. New aggregate function
524** implementations should keep their own counts within their aggregate
525** context.
526*/
527int sqlite3_aggregate_count(sqlite3_context *p){
528 assert( p && p->pFunc && p->pFunc->xStep );
529 return p->pMem->n;
530}
531
532/*
533** Return the number of columns in the result set for the statement pStmt.
534*/
535int sqlite3_column_count(sqlite3_stmt *pStmt){
536 Vdbe *pVm = (Vdbe *)pStmt;
537 return pVm ? pVm->nResColumn : 0;
538}
539
540/*
541** Return the number of values available from the current row of the
542** currently executing statement pStmt.
543*/
544int sqlite3_data_count(sqlite3_stmt *pStmt){
545 Vdbe *pVm = (Vdbe *)pStmt;
546 if( pVm==0 || !pVm->resOnStack ) return 0;
547 return pVm->nResColumn;
548}
549
550
551/*
552** Check to see if column iCol of the given statement is valid. If
553** it is, return a pointer to the Mem for the value of that column.
554** If iCol is not valid, return a pointer to a Mem which has a value
555** of NULL.
556*/
557static Mem *columnMem(sqlite3_stmt *pStmt, int i){
558 Vdbe *pVm;
559 int vals;
560 Mem *pOut;
561
562 pVm = (Vdbe *)pStmt;
563 if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
564 sqlite3_mutex_enter(pVm->db->mutex);
565 vals = sqlite3_data_count(pStmt);
566 pOut = &pVm->pTos[(1-vals)+i];
567 }else{
568 static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
569 if( pVm->db ){
570 sqlite3_mutex_enter(pVm->db->mutex);
571 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
572 }
573 pOut = (Mem*)&nullMem;
574 }
575 return pOut;
576}
577
578/*
579** This function is called after invoking an sqlite3_value_XXX function on a
580** column value (i.e. a value returned by evaluating an SQL expression in the
581** select list of a SELECT statement) that may cause a malloc() failure. If
582** malloc() has failed, the threads mallocFailed flag is cleared and the result
583** code of statement pStmt set to SQLITE_NOMEM.
584**
585** Specifically, this is called from within:
586**
587** sqlite3_column_int()
588** sqlite3_column_int64()
589** sqlite3_column_text()
590** sqlite3_column_text16()
591** sqlite3_column_real()
592** sqlite3_column_bytes()
593** sqlite3_column_bytes16()
594**
595** But not for sqlite3_column_blob(), which never calls malloc().
596*/
597static void columnMallocFailure(sqlite3_stmt *pStmt)
598{
599 /* If malloc() failed during an encoding conversion within an
600 ** sqlite3_column_XXX API, then set the return code of the statement to
601 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
602 ** and _finalize() will return NOMEM.
603 */
604 Vdbe *p = (Vdbe *)pStmt;
605 if( p ){
606 p->rc = sqlite3ApiExit(p->db, p->rc);
607 sqlite3_mutex_leave(p->db->mutex);
608 }
609}
610
611/**************************** sqlite3_column_ *******************************
612** The following routines are used to access elements of the current row
613** in the result set.
614*/
615const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
616 const void *val;
617 val = sqlite3_value_blob( columnMem(pStmt,i) );
618 /* Even though there is no encoding conversion, value_blob() might
619 ** need to call malloc() to expand the result of a zeroblob()
620 ** expression.
621 */
622 columnMallocFailure(pStmt);
623 return val;
624}
625int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
626 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
627 columnMallocFailure(pStmt);
628 return val;
629}
630int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
631 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
632 columnMallocFailure(pStmt);
633 return val;
634}
635double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
636 double val = sqlite3_value_double( columnMem(pStmt,i) );
637 columnMallocFailure(pStmt);
638 return val;
639}
640int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
641 int val = sqlite3_value_int( columnMem(pStmt,i) );
642 columnMallocFailure(pStmt);
643 return val;
644}
645sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
646 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
647 columnMallocFailure(pStmt);
648 return val;
649}
650const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
651 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
652 columnMallocFailure(pStmt);
653 return val;
654}
655sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
656 sqlite3_value *pOut = columnMem(pStmt, i);
657 columnMallocFailure(pStmt);
658 return pOut;
659}
660#ifndef SQLITE_OMIT_UTF16
661const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
662 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
663 columnMallocFailure(pStmt);
664 return val;
665}
666#endif /* SQLITE_OMIT_UTF16 */
667int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
668 int iType = sqlite3_value_type( columnMem(pStmt,i) );
669 columnMallocFailure(pStmt);
670 return iType;
671}
672
673/* The following function is experimental and subject to change or
674** removal */
675/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
676** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
677**}
678*/
679
680/*
681** Convert the N-th element of pStmt->pColName[] into a string using
682** xFunc() then return that string. If N is out of range, return 0.
683**
684** There are up to 5 names for each column. useType determines which
685** name is returned. Here are the names:
686**
687** 0 The column name as it should be displayed for output
688** 1 The datatype name for the column
689** 2 The name of the database that the column derives from
690** 3 The name of the table that the column derives from
691** 4 The name of the table column that the result column derives from
692**
693** If the result is not a simple column reference (if it is an expression
694** or a constant) then useTypes 2, 3, and 4 return NULL.
695*/
696static const void *columnName(
697 sqlite3_stmt *pStmt,
698 int N,
699 const void *(*xFunc)(Mem*),
700 int useType
701){
702 const void *ret = 0;
703 Vdbe *p = (Vdbe *)pStmt;
704 int n;
705
706
707 if( p!=0 ){
708 n = sqlite3_column_count(pStmt);
709 if( N<n && N>=0 ){
710 N += useType*n;
711 sqlite3_mutex_enter(p->db->mutex);
712 ret = xFunc(&p->aColName[N]);
713
714 /* A malloc may have failed inside of the xFunc() call. If this
715 ** is the case, clear the mallocFailed flag and return NULL.
716 */
717 if( p->db && p->db->mallocFailed ){
718 p->db->mallocFailed = 0;
719 ret = 0;
720 }
721 sqlite3_mutex_leave(p->db->mutex);
722 }
723 }
724 return ret;
725}
726
727/*
728** Return the name of the Nth column of the result set returned by SQL
729** statement pStmt.
730*/
731const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
732 return columnName(
733 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
734}
735#ifndef SQLITE_OMIT_UTF16
736const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
737 return columnName(
738 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
739}
740#endif
741
742/*
743** Return the column declaration type (if applicable) of the 'i'th column
744** of the result set of SQL statement pStmt.
745*/
746const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
747 return columnName(
748 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
749}
750#ifndef SQLITE_OMIT_UTF16
751const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
752 return columnName(
753 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
754}
755#endif /* SQLITE_OMIT_UTF16 */
756
757#ifdef SQLITE_ENABLE_COLUMN_METADATA
758/*
759** Return the name of the database from which a result column derives.
760** NULL is returned if the result column is an expression or constant or
761** anything else which is not an unabiguous reference to a database column.
762*/
763const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
764 return columnName(
765 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
766}
767#ifndef SQLITE_OMIT_UTF16
768const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
769 return columnName(
770 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
771}
772#endif /* SQLITE_OMIT_UTF16 */
773
774/*
775** Return the name of the table from which a result column derives.
776** NULL is returned if the result column is an expression or constant or
777** anything else which is not an unabiguous reference to a database column.
778*/
779const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
780 return columnName(
781 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
782}
783#ifndef SQLITE_OMIT_UTF16
784const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
785 return columnName(
786 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
787}
788#endif /* SQLITE_OMIT_UTF16 */
789
790/*
791** Return the name of the table column from which a result column derives.
792** NULL is returned if the result column is an expression or constant or
793** anything else which is not an unabiguous reference to a database column.
794*/
795const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
796 return columnName(
797 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
798}
799#ifndef SQLITE_OMIT_UTF16
800const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
801 return columnName(
802 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
803}
804#endif /* SQLITE_OMIT_UTF16 */
805#endif /* SQLITE_ENABLE_COLUMN_METADATA */
806
807
808/******************************* sqlite3_bind_ ***************************
809**
810** Routines used to attach values to wildcards in a compiled SQL statement.
811*/
812/*
813** Unbind the value bound to variable i in virtual machine p. This is the
814** the same as binding a NULL value to the column. If the "i" parameter is
815** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
816**
817** The error code stored in database p->db is overwritten with the return
818** value in any case.
819*/
820static int vdbeUnbind(Vdbe *p, int i){
821 Mem *pVar;
822 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
823 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
824 return SQLITE_MISUSE;
825 }
826 if( i<1 || i>p->nVar ){
827 sqlite3Error(p->db, SQLITE_RANGE, 0);
828 return SQLITE_RANGE;
829 }
830 i--;
831 pVar = &p->aVar[i];
832 sqlite3VdbeMemRelease(pVar);
833 pVar->flags = MEM_Null;
834 sqlite3Error(p->db, SQLITE_OK, 0);
835 return SQLITE_OK;
836}
837
838/*
839** Bind a text or BLOB value.
840*/
841static int bindText(
842 sqlite3_stmt *pStmt, /* The statement to bind against */
843 int i, /* Index of the parameter to bind */
844 const void *zData, /* Pointer to the data to be bound */
845 int nData, /* Number of bytes of data to be bound */
846 void (*xDel)(void*), /* Destructor for the data */
847 int encoding /* Encoding for the data */
848){
849 Vdbe *p = (Vdbe *)pStmt;
850 Mem *pVar;
851 int rc;
852
853 if( p==0 ){
854 return SQLITE_MISUSE;
855 }
856 sqlite3_mutex_enter(p->db->mutex);
857 rc = vdbeUnbind(p, i);
858 if( rc==SQLITE_OK && zData!=0 ){
859 pVar = &p->aVar[i-1];
860 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
861 if( rc==SQLITE_OK && encoding!=0 ){
862 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
863 }
864 sqlite3Error(p->db, rc, 0);
865 rc = sqlite3ApiExit(p->db, rc);
866 }
867 sqlite3_mutex_leave(p->db->mutex);
868 return rc;
869}
870
871
872/*
873** Bind a blob value to an SQL statement variable.
874*/
875int sqlite3_bind_blob(
876 sqlite3_stmt *pStmt,
877 int i,
878 const void *zData,
879 int nData,
880 void (*xDel)(void*)
881){
882 return bindText(pStmt, i, zData, nData, xDel, 0);
883}
884int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
885 int rc;
886 Vdbe *p = (Vdbe *)pStmt;
887 sqlite3_mutex_enter(p->db->mutex);
888 rc = vdbeUnbind(p, i);
889 if( rc==SQLITE_OK ){
890 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
891 }
892 sqlite3_mutex_leave(p->db->mutex);
893 return rc;
894}
895int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
896 return sqlite3_bind_int64(p, i, (i64)iValue);
897}
898int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
899 int rc;
900 Vdbe *p = (Vdbe *)pStmt;
901 sqlite3_mutex_enter(p->db->mutex);
902 rc = vdbeUnbind(p, i);
903 if( rc==SQLITE_OK ){
904 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
905 }
906 sqlite3_mutex_leave(p->db->mutex);
907 return rc;
908}
909int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
910 int rc;
911 Vdbe *p = (Vdbe*)pStmt;
912 sqlite3_mutex_enter(p->db->mutex);
913 rc = vdbeUnbind(p, i);
914 sqlite3_mutex_leave(p->db->mutex);
915 return rc;
916}
917int sqlite3_bind_text(
918 sqlite3_stmt *pStmt,
919 int i,
920 const char *zData,
921 int nData,
922 void (*xDel)(void*)
923){
924 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
925}
926#ifndef SQLITE_OMIT_UTF16
927int sqlite3_bind_text16(
928 sqlite3_stmt *pStmt,
929 int i,
930 const void *zData,
931 int nData,
932 void (*xDel)(void*)
933){
934 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
935}
936#endif /* SQLITE_OMIT_UTF16 */
937int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
938 int rc;
939 Vdbe *p = (Vdbe *)pStmt;
940 sqlite3_mutex_enter(p->db->mutex);
941 rc = vdbeUnbind(p, i);
942 if( rc==SQLITE_OK ){
943 rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
944 }
945 sqlite3_mutex_leave(p->db->mutex);
946 return rc;
947}
948int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
949 int rc;
950 Vdbe *p = (Vdbe *)pStmt;
951 sqlite3_mutex_enter(p->db->mutex);
952 rc = vdbeUnbind(p, i);
953 if( rc==SQLITE_OK ){
954 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
955 }
956 sqlite3_mutex_leave(p->db->mutex);
957 return rc;
958}
959
960/*
961** Return the number of wildcards that can be potentially bound to.
962** This routine is added to support DBD::SQLite.
963*/
964int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
965 Vdbe *p = (Vdbe*)pStmt;
966 return p ? p->nVar : 0;
967}
968
969/*
970** Create a mapping from variable numbers to variable names
971** in the Vdbe.azVar[] array, if such a mapping does not already
972** exist.
973*/
974static void createVarMap(Vdbe *p){
975 if( !p->okVar ){
976 sqlite3_mutex_enter(p->db->mutex);
977 if( !p->okVar ){
978 int j;
979 Op *pOp;
980 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
981 if( pOp->opcode==OP_Variable ){
982 assert( pOp->p1>0 && pOp->p1<=p->nVar );
983 p->azVar[pOp->p1-1] = pOp->p3;
984 }
985 }
986 p->okVar = 1;
987 }
988 sqlite3_mutex_leave(p->db->mutex);
989 }
990}
991
992/*
993** Return the name of a wildcard parameter. Return NULL if the index
994** is out of range or if the wildcard is unnamed.
995**
996** The result is always UTF-8.
997*/
998const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
999 Vdbe *p = (Vdbe*)pStmt;
1000 if( p==0 || i<1 || i>p->nVar ){
1001 return 0;
1002 }
1003 createVarMap(p);
1004 return p->azVar[i-1];
1005}
1006
1007/*
1008** Given a wildcard parameter name, return the index of the variable
1009** with that name. If there is no variable with the given name,
1010** return 0.
1011*/
1012int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1013 Vdbe *p = (Vdbe*)pStmt;
1014 int i;
1015 if( p==0 ){
1016 return 0;
1017 }
1018 createVarMap(p);
1019 if( zName ){
1020 for(i=0; i<p->nVar; i++){
1021 const char *z = p->azVar[i];
1022 if( z && strcmp(z,zName)==0 ){
1023 return i+1;
1024 }
1025 }
1026 }
1027 return 0;
1028}
1029
1030/*
1031** Transfer all bindings from the first statement over to the second.
1032** If the two statements contain a different number of bindings, then
1033** an SQLITE_ERROR is returned.
1034*/
1035int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1036 Vdbe *pFrom = (Vdbe*)pFromStmt;
1037 Vdbe *pTo = (Vdbe*)pToStmt;
1038 int i, rc = SQLITE_OK;
1039 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
1040 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
1041 || pTo->db!=pFrom->db ){
1042 return SQLITE_MISUSE;
1043 }
1044 if( pFrom->nVar!=pTo->nVar ){
1045 return SQLITE_ERROR;
1046 }
1047 sqlite3_mutex_enter(pTo->db->mutex);
1048 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
1049 sqlite3MallocDisallow();
1050 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
1051 sqlite3MallocAllow();
1052 }
1053 sqlite3_mutex_leave(pTo->db->mutex);
1054 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
1055 return rc;
1056}
1057
1058/*
1059** Return the sqlite3* database handle to which the prepared statement given
1060** in the argument belongs. This is the same database handle that was
1061** the first argument to the sqlite3_prepare() that was used to create
1062** the statement in the first place.
1063*/
1064sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
1065 return pStmt ? ((Vdbe*)pStmt)->db : 0;
1066}
diff --git a/libraries/sqlite/win32/vdbeaux.c b/libraries/sqlite/win32/vdbeaux.c
new file mode 100755
index 0000000..8a61872
--- /dev/null
+++ b/libraries/sqlite/win32/vdbeaux.c
@@ -0,0 +1,2243 @@
1/*
2** 2003 September 6
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.
10**
11*************************************************************************
12** This file contains code used for creating, destroying, and populating
13** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
14** to version 2.8.7, all this code was combined into the vdbe.c source file.
15** But that file was getting too big so this subroutines were split out.
16*/
17#include "sqliteInt.h"
18#include <ctype.h>
19#include "vdbeInt.h"
20
21
22
23/*
24** When debugging the code generator in a symbolic debugger, one can
25** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
26** as they are added to the instruction stream.
27*/
28#ifdef SQLITE_DEBUG
29int sqlite3_vdbe_addop_trace = 0;
30#endif
31
32
33/*
34** Create a new virtual database engine.
35*/
36Vdbe *sqlite3VdbeCreate(sqlite3 *db){
37 Vdbe *p;
38 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
39 if( p==0 ) return 0;
40 p->db = db;
41 if( db->pVdbe ){
42 db->pVdbe->pPrev = p;
43 }
44 p->pNext = db->pVdbe;
45 p->pPrev = 0;
46 db->pVdbe = p;
47 p->magic = VDBE_MAGIC_INIT;
48 return p;
49}
50
51/*
52** Remember the SQL string for a prepared statement.
53*/
54void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
55 if( p==0 ) return;
56 assert( p->zSql==0 );
57 p->zSql = sqlite3DbStrNDup(p->db, z, n);
58}
59
60/*
61** Return the SQL associated with a prepared statement
62*/
63const char *sqlite3VdbeGetSql(Vdbe *p){
64 return p->zSql;
65}
66
67/*
68** Swap all content between two VDBE structures.
69*/
70void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
71 Vdbe tmp, *pTmp;
72 char *zTmp;
73 int nTmp;
74 tmp = *pA;
75 *pA = *pB;
76 *pB = tmp;
77 pTmp = pA->pNext;
78 pA->pNext = pB->pNext;
79 pB->pNext = pTmp;
80 pTmp = pA->pPrev;
81 pA->pPrev = pB->pPrev;
82 pB->pPrev = pTmp;
83 zTmp = pA->zSql;
84 pA->zSql = pB->zSql;
85 pB->zSql = zTmp;
86 nTmp = pA->nSql;
87 pA->nSql = pB->nSql;
88 pB->nSql = nTmp;
89}
90
91#ifdef SQLITE_DEBUG
92/*
93** Turn tracing on or off
94*/
95void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
96 p->trace = trace;
97}
98#endif
99
100/*
101** Resize the Vdbe.aOp array so that it contains at least N
102** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
103** the Vdbe.aOp array will be sized to contain exactly N
104** elements. Vdbe.nOpAlloc is set to reflect the new size of
105** the array.
106**
107** If an out-of-memory error occurs while resizing the array,
108** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
109** any opcodes already allocated can be correctly deallocated
110** along with the rest of the Vdbe).
111*/
112static void resizeOpArray(Vdbe *p, int N){
113 int runMode = p->magic==VDBE_MAGIC_RUN;
114 if( runMode || p->nOpAlloc<N ){
115 VdbeOp *pNew;
116 int nNew = N + 100*(!runMode);
117 int oldSize = p->nOpAlloc;
118 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
119 if( pNew ){
120 p->nOpAlloc = nNew;
121 p->aOp = pNew;
122 if( nNew>oldSize ){
123 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
124 }
125 }
126 }
127}
128
129/*
130** Add a new instruction to the list of instructions current in the
131** VDBE. Return the address of the new instruction.
132**
133** Parameters:
134**
135** p Pointer to the VDBE
136**
137** op The opcode for this instruction
138**
139** p1, p2 First two of the three possible operands.
140**
141** Use the sqlite3VdbeResolveLabel() function to fix an address and
142** the sqlite3VdbeChangeP3() function to change the value of the P3
143** operand.
144*/
145int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
146 int i;
147 VdbeOp *pOp;
148
149 i = p->nOp;
150 assert( p->magic==VDBE_MAGIC_INIT );
151 if( p->nOpAlloc<=i ){
152 resizeOpArray(p, i+1);
153 if( p->db->mallocFailed ){
154 return 0;
155 }
156 }
157 p->nOp++;
158 pOp = &p->aOp[i];
159 pOp->opcode = op;
160 pOp->p1 = p1;
161 pOp->p2 = p2;
162 pOp->p3 = 0;
163 pOp->p3type = P3_NOTUSED;
164 p->expired = 0;
165#ifdef SQLITE_DEBUG
166 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
167#endif
168 return i;
169}
170
171/*
172** Add an opcode that includes the p3 value.
173*/
174int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
175 int addr = sqlite3VdbeAddOp(p, op, p1, p2);
176 sqlite3VdbeChangeP3(p, addr, zP3, p3type);
177 return addr;
178}
179
180/*
181** Create a new symbolic label for an instruction that has yet to be
182** coded. The symbolic label is really just a negative number. The
183** label can be used as the P2 value of an operation. Later, when
184** the label is resolved to a specific address, the VDBE will scan
185** through its operation list and change all values of P2 which match
186** the label into the resolved address.
187**
188** The VDBE knows that a P2 value is a label because labels are
189** always negative and P2 values are suppose to be non-negative.
190** Hence, a negative P2 value is a label that has yet to be resolved.
191**
192** Zero is returned if a malloc() fails.
193*/
194int sqlite3VdbeMakeLabel(Vdbe *p){
195 int i;
196 i = p->nLabel++;
197 assert( p->magic==VDBE_MAGIC_INIT );
198 if( i>=p->nLabelAlloc ){
199 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
200 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
201 p->nLabelAlloc*sizeof(p->aLabel[0]));
202 }
203 if( p->aLabel ){
204 p->aLabel[i] = -1;
205 }
206 return -1-i;
207}
208
209/*
210** Resolve label "x" to be the address of the next instruction to
211** be inserted. The parameter "x" must have been obtained from
212** a prior call to sqlite3VdbeMakeLabel().
213*/
214void sqlite3VdbeResolveLabel(Vdbe *p, int x){
215 int j = -1-x;
216 assert( p->magic==VDBE_MAGIC_INIT );
217 assert( j>=0 && j<p->nLabel );
218 if( p->aLabel ){
219 p->aLabel[j] = p->nOp;
220 }
221}
222
223/*
224** Return non-zero if opcode 'op' is guarenteed not to push more values
225** onto the VDBE stack than it pops off.
226*/
227static int opcodeNoPush(u8 op){
228 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
229 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
230 ** bit corresponding to each opcode implemented by the virtual
231 ** machine in vdbe.c. The bit is true if the word "no-push" appears
232 ** in a comment on the same line as the "case OP_XXX:" in
233 ** sqlite3VdbeExec() in vdbe.c.
234 **
235 ** If the bit is true, then the corresponding opcode is guarenteed not
236 ** to grow the stack when it is executed. Otherwise, it may grow the
237 ** stack by at most one entry.
238 **
239 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
240 ** one bit for opcodes 16 to 31, and so on.
241 **
242 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
243 ** because the file is generated by an awk program. Awk manipulates
244 ** all numbers as floating-point and we don't want to risk a rounding
245 ** error if someone builds with an awk that uses (for example) 32-bit
246 ** IEEE floats.
247 */
248 static const u32 masks[5] = {
249 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
250 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
251 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
252 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
253 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
254 };
255 assert( op<32*5 );
256 return (masks[op>>5] & (1<<(op&0x1F)));
257}
258
259#ifndef NDEBUG
260int sqlite3VdbeOpcodeNoPush(u8 op){
261 return opcodeNoPush(op);
262}
263#endif
264
265/*
266** Loop through the program looking for P2 values that are negative.
267** Each such value is a label. Resolve the label by setting the P2
268** value to its correct non-zero value.
269**
270** This routine is called once after all opcodes have been inserted.
271**
272** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
273** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
274** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
275**
276** The integer *pMaxStack is set to the maximum number of vdbe stack
277** entries that static analysis reveals this program might need.
278**
279** This routine also does the following optimization: It scans for
280** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
281** IdxInsert instructions where P2!=0. If no such instruction is
282** found, then every Statement instruction is changed to a Noop. In
283** this way, we avoid creating the statement journal file unnecessarily.
284*/
285static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
286 int i;
287 int nMaxArgs = 0;
288 int nMaxStack = p->nOp;
289 Op *pOp;
290 int *aLabel = p->aLabel;
291 int doesStatementRollback = 0;
292 int hasStatementBegin = 0;
293 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
294 u8 opcode = pOp->opcode;
295
296 if( opcode==OP_Function || opcode==OP_AggStep
297#ifndef SQLITE_OMIT_VIRTUALTABLE
298 || opcode==OP_VUpdate
299#endif
300 ){
301 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
302 }
303 if( opcode==OP_Halt ){
304 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
305 doesStatementRollback = 1;
306 }
307 }else if( opcode==OP_Statement ){
308 hasStatementBegin = 1;
309#ifndef SQLITE_OMIT_VIRTUALTABLE
310 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
311 doesStatementRollback = 1;
312 }else if( opcode==OP_VFilter ){
313 int n;
314 assert( p->nOp - i >= 3 );
315 assert( pOp[-2].opcode==OP_Integer );
316 n = pOp[-2].p1;
317 if( n>nMaxArgs ) nMaxArgs = n;
318#endif
319 }
320 if( opcodeNoPush(opcode) ){
321 nMaxStack--;
322 }
323
324 if( pOp->p2>=0 ) continue;
325 assert( -1-pOp->p2<p->nLabel );
326 pOp->p2 = aLabel[-1-pOp->p2];
327 }
328 sqlite3_free(p->aLabel);
329 p->aLabel = 0;
330
331 *pMaxFuncArgs = nMaxArgs;
332 *pMaxStack = nMaxStack;
333
334 /* If we never rollback a statement transaction, then statement
335 ** transactions are not needed. So change every OP_Statement
336 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
337 ** which can be expensive on some platforms.
338 */
339 if( hasStatementBegin && !doesStatementRollback ){
340 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
341 if( pOp->opcode==OP_Statement ){
342 pOp->opcode = OP_Noop;
343 }
344 }
345 }
346}
347
348/*
349** Return the address of the next instruction to be inserted.
350*/
351int sqlite3VdbeCurrentAddr(Vdbe *p){
352 assert( p->magic==VDBE_MAGIC_INIT );
353 return p->nOp;
354}
355
356/*
357** Add a whole list of operations to the operation stack. Return the
358** address of the first operation added.
359*/
360int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
361 int addr;
362 assert( p->magic==VDBE_MAGIC_INIT );
363 resizeOpArray(p, p->nOp + nOp);
364 if( p->db->mallocFailed ){
365 return 0;
366 }
367 addr = p->nOp;
368 if( nOp>0 ){
369 int i;
370 VdbeOpList const *pIn = aOp;
371 for(i=0; i<nOp; i++, pIn++){
372 int p2 = pIn->p2;
373 VdbeOp *pOut = &p->aOp[i+addr];
374 pOut->opcode = pIn->opcode;
375 pOut->p1 = pIn->p1;
376 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
377 pOut->p3 = pIn->p3;
378 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
379#ifdef SQLITE_DEBUG
380 if( sqlite3_vdbe_addop_trace ){
381 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
382 }
383#endif
384 }
385 p->nOp += nOp;
386 }
387 return addr;
388}
389
390/*
391** Change the value of the P1 operand for a specific instruction.
392** This routine is useful when a large program is loaded from a
393** static array using sqlite3VdbeAddOpList but we want to make a
394** few minor changes to the program.
395*/
396void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
397 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
398 if( p && addr>=0 && p->nOp>addr && p->aOp ){
399 p->aOp[addr].p1 = val;
400 }
401}
402
403/*
404** Change the value of the P2 operand for a specific instruction.
405** This routine is useful for setting a jump destination.
406*/
407void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
408 assert( val>=0 );
409 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
410 if( p && addr>=0 && p->nOp>addr && p->aOp ){
411 p->aOp[addr].p2 = val;
412 }
413}
414
415/*
416** Change the P2 operand of instruction addr so that it points to
417** the address of the next instruction to be coded.
418*/
419void sqlite3VdbeJumpHere(Vdbe *p, int addr){
420 sqlite3VdbeChangeP2(p, addr, p->nOp);
421}
422
423
424/*
425** If the input FuncDef structure is ephemeral, then free it. If
426** the FuncDef is not ephermal, then do nothing.
427*/
428static void freeEphemeralFunction(FuncDef *pDef){
429 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
430 sqlite3_free(pDef);
431 }
432}
433
434/*
435** Delete a P3 value if necessary.
436*/
437static void freeP3(int p3type, void *p3){
438 if( p3 ){
439 switch( p3type ){
440 case P3_DYNAMIC:
441 case P3_KEYINFO:
442 case P3_KEYINFO_HANDOFF: {
443 sqlite3_free(p3);
444 break;
445 }
446 case P3_MPRINTF: {
447 sqlite3_free(p3);
448 break;
449 }
450 case P3_VDBEFUNC: {
451 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
452 freeEphemeralFunction(pVdbeFunc->pFunc);
453 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
454 sqlite3_free(pVdbeFunc);
455 break;
456 }
457 case P3_FUNCDEF: {
458 freeEphemeralFunction((FuncDef*)p3);
459 break;
460 }
461 case P3_MEM: {
462 sqlite3ValueFree((sqlite3_value*)p3);
463 break;
464 }
465 }
466 }
467}
468
469
470/*
471** Change N opcodes starting at addr to No-ops.
472*/
473void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
474 if( p && p->aOp ){
475 VdbeOp *pOp = &p->aOp[addr];
476 while( N-- ){
477 freeP3(pOp->p3type, pOp->p3);
478 memset(pOp, 0, sizeof(pOp[0]));
479 pOp->opcode = OP_Noop;
480 pOp++;
481 }
482 }
483}
484
485/*
486** Change the value of the P3 operand for a specific instruction.
487** This routine is useful when a large program is loaded from a
488** static array using sqlite3VdbeAddOpList but we want to make a
489** few minor changes to the program.
490**
491** If n>=0 then the P3 operand is dynamic, meaning that a copy of
492** the string is made into memory obtained from sqlite3_malloc().
493** A value of n==0 means copy bytes of zP3 up to and including the
494** first null byte. If n>0 then copy n+1 bytes of zP3.
495**
496** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
497** A copy is made of the KeyInfo structure into memory obtained from
498** sqlite3_malloc, to be freed when the Vdbe is finalized.
499** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
500** stored in memory that the caller has obtained from sqlite3_malloc. The
501** caller should not free the allocation, it will be freed when the Vdbe is
502** finalized.
503**
504** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
505** to a string or structure that is guaranteed to exist for the lifetime of
506** the Vdbe. In these cases we can just copy the pointer.
507**
508** If addr<0 then change P3 on the most recently inserted instruction.
509*/
510void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
511 Op *pOp;
512 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
513 if( p==0 || p->aOp==0 || p->db->mallocFailed ){
514 if (n != P3_KEYINFO) {
515 freeP3(n, (void*)*(char**)&zP3);
516 }
517 return;
518 }
519 if( addr<0 || addr>=p->nOp ){
520 addr = p->nOp - 1;
521 if( addr<0 ) return;
522 }
523 pOp = &p->aOp[addr];
524 freeP3(pOp->p3type, pOp->p3);
525 pOp->p3 = 0;
526 if( zP3==0 ){
527 pOp->p3 = 0;
528 pOp->p3type = P3_NOTUSED;
529 }else if( n==P3_KEYINFO ){
530 KeyInfo *pKeyInfo;
531 int nField, nByte;
532
533 nField = ((KeyInfo*)zP3)->nField;
534 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
535 pKeyInfo = sqlite3_malloc( nByte );
536 pOp->p3 = (char*)pKeyInfo;
537 if( pKeyInfo ){
538 unsigned char *aSortOrder;
539 memcpy(pKeyInfo, zP3, nByte);
540 aSortOrder = pKeyInfo->aSortOrder;
541 if( aSortOrder ){
542 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
543 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
544 }
545 pOp->p3type = P3_KEYINFO;
546 }else{
547 p->db->mallocFailed = 1;
548 pOp->p3type = P3_NOTUSED;
549 }
550 }else if( n==P3_KEYINFO_HANDOFF ){
551 pOp->p3 = (char*)zP3;
552 pOp->p3type = P3_KEYINFO;
553 }else if( n<0 ){
554 pOp->p3 = (char*)zP3;
555 pOp->p3type = n;
556 }else{
557 if( n==0 ) n = strlen(zP3);
558 pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
559 pOp->p3type = P3_DYNAMIC;
560 }
561}
562
563#ifndef NDEBUG
564/*
565** Replace the P3 field of the most recently coded instruction with
566** comment text.
567*/
568void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
569 va_list ap;
570 assert( p->nOp>0 || p->aOp==0 );
571 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
572 va_start(ap, zFormat);
573 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
574 va_end(ap);
575}
576#endif
577
578/*
579** Return the opcode for a given address.
580*/
581VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
582 assert( p->magic==VDBE_MAGIC_INIT );
583 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
584 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
585}
586
587#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
588 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
589/*
590** Compute a string that describes the P3 parameter for an opcode.
591** Use zTemp for any required temporary buffer space.
592*/
593static char *displayP3(Op *pOp, char *zTemp, int nTemp){
594 char *zP3;
595 assert( nTemp>=20 );
596 switch( pOp->p3type ){
597 case P3_KEYINFO: {
598 int i, j;
599 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
600 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
601 i = strlen(zTemp);
602 for(j=0; j<pKeyInfo->nField; j++){
603 CollSeq *pColl = pKeyInfo->aColl[j];
604 if( pColl ){
605 int n = strlen(pColl->zName);
606 if( i+n>nTemp-6 ){
607 memcpy(&zTemp[i],",...",4);
608 break;
609 }
610 zTemp[i++] = ',';
611 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
612 zTemp[i++] = '-';
613 }
614 memcpy(&zTemp[i], pColl->zName,n+1);
615 i += n;
616 }else if( i+4<nTemp-6 ){
617 memcpy(&zTemp[i],",nil",4);
618 i += 4;
619 }
620 }
621 zTemp[i++] = ')';
622 zTemp[i] = 0;
623 assert( i<nTemp );
624 zP3 = zTemp;
625 break;
626 }
627 case P3_COLLSEQ: {
628 CollSeq *pColl = (CollSeq*)pOp->p3;
629 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
630 zP3 = zTemp;
631 break;
632 }
633 case P3_FUNCDEF: {
634 FuncDef *pDef = (FuncDef*)pOp->p3;
635 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
636 zP3 = zTemp;
637 break;
638 }
639#ifndef SQLITE_OMIT_VIRTUALTABLE
640 case P3_VTAB: {
641 sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
642 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
643 zP3 = zTemp;
644 break;
645 }
646#endif
647 default: {
648 zP3 = pOp->p3;
649 if( zP3==0 || pOp->opcode==OP_Noop ){
650 zP3 = "";
651 }
652 }
653 }
654 assert( zP3!=0 );
655 return zP3;
656}
657#endif
658
659/*
660** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
661**
662*/
663void sqlite3VdbeUsesBtree(Vdbe *p, int i){
664 int mask;
665 assert( i>=0 && i<p->db->nDb );
666 assert( i<sizeof(p->btreeMask)*8 );
667 mask = 1<<i;
668 if( (p->btreeMask & mask)==0 ){
669 p->btreeMask |= mask;
670 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
671 }
672}
673
674
675#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
676/*
677** Print a single opcode. This routine is used for debugging only.
678*/
679void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
680 char *zP3;
681 char zPtr[50];
682 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
683 if( pOut==0 ) pOut = stdout;
684 zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
685 fprintf(pOut, zFormat1,
686 pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
687 fflush(pOut);
688}
689#endif
690
691/*
692** Release an array of N Mem elements
693*/
694static void releaseMemArray(Mem *p, int N){
695 if( p ){
696 while( N-->0 ){
697 assert( N<2 || p[0].db==p[1].db );
698 sqlite3VdbeMemRelease(p++);
699 }
700 }
701}
702
703#ifndef SQLITE_OMIT_EXPLAIN
704/*
705** Give a listing of the program in the virtual machine.
706**
707** The interface is the same as sqlite3VdbeExec(). But instead of
708** running the code, it invokes the callback once for each instruction.
709** This feature is used to implement "EXPLAIN".
710*/
711int sqlite3VdbeList(
712 Vdbe *p /* The VDBE */
713){
714 sqlite3 *db = p->db;
715 int i;
716 int rc = SQLITE_OK;
717
718 assert( p->explain );
719 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
720 assert( db->magic==SQLITE_MAGIC_BUSY );
721 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
722
723 /* Even though this opcode does not put dynamic strings onto the
724 ** the stack, they may become dynamic if the user calls
725 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
726 */
727 if( p->pTos==&p->aStack[4] ){
728 releaseMemArray(p->aStack, 5);
729 }
730 p->resOnStack = 0;
731
732 do{
733 i = p->pc++;
734 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
735 if( i>=p->nOp ){
736 p->rc = SQLITE_OK;
737 rc = SQLITE_DONE;
738 }else if( db->u1.isInterrupted ){
739 p->rc = SQLITE_INTERRUPT;
740 rc = SQLITE_ERROR;
741 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
742 }else{
743 Op *pOp = &p->aOp[i];
744 Mem *pMem = p->aStack;
745 pMem->flags = MEM_Int;
746 pMem->type = SQLITE_INTEGER;
747 pMem->u.i = i; /* Program counter */
748 pMem++;
749
750 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
751 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
752 assert( pMem->z!=0 );
753 pMem->n = strlen(pMem->z);
754 pMem->type = SQLITE_TEXT;
755 pMem->enc = SQLITE_UTF8;
756 pMem++;
757
758 pMem->flags = MEM_Int;
759 pMem->u.i = pOp->p1; /* P1 */
760 pMem->type = SQLITE_INTEGER;
761 pMem++;
762
763 pMem->flags = MEM_Int;
764 pMem->u.i = pOp->p2; /* P2 */
765 pMem->type = SQLITE_INTEGER;
766 pMem++;
767
768 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
769 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
770 assert( pMem->z!=0 );
771 pMem->n = strlen(pMem->z);
772 pMem->type = SQLITE_TEXT;
773 pMem->enc = SQLITE_UTF8;
774
775 p->nResColumn = 5 - 2*(p->explain-1);
776 p->pTos = pMem;
777 p->rc = SQLITE_OK;
778 p->resOnStack = 1;
779 rc = SQLITE_ROW;
780 }
781 return rc;
782}
783#endif /* SQLITE_OMIT_EXPLAIN */
784
785#ifdef SQLITE_DEBUG
786/*
787** Print the SQL that was used to generate a VDBE program.
788*/
789void sqlite3VdbePrintSql(Vdbe *p){
790 int nOp = p->nOp;
791 VdbeOp *pOp;
792 if( nOp<1 ) return;
793 pOp = &p->aOp[nOp-1];
794 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
795 const char *z = pOp->p3;
796 while( isspace(*(u8*)z) ) z++;
797 printf("SQL: [%s]\n", z);
798 }
799}
800#endif
801
802#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
803/*
804** Print an IOTRACE message showing SQL content.
805*/
806void sqlite3VdbeIOTraceSql(Vdbe *p){
807 int nOp = p->nOp;
808 VdbeOp *pOp;
809 if( sqlite3_io_trace==0 ) return;
810 if( nOp<1 ) return;
811 pOp = &p->aOp[nOp-1];
812 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
813 int i, j;
814 char z[1000];
815 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
816 for(i=0; isspace((unsigned char)z[i]); i++){}
817 for(j=0; z[i]; i++){
818 if( isspace((unsigned char)z[i]) ){
819 if( z[i-1]!=' ' ){
820 z[j++] = ' ';
821 }
822 }else{
823 z[j++] = z[i];
824 }
825 }
826 z[j] = 0;
827 sqlite3_io_trace("SQL %s\n", z);
828 }
829}
830#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
831
832
833/*
834** Prepare a virtual machine for execution. This involves things such
835** as allocating stack space and initializing the program counter.
836** After the VDBE has be prepped, it can be executed by one or more
837** calls to sqlite3VdbeExec().
838**
839** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
840** VDBE_MAGIC_RUN.
841*/
842void sqlite3VdbeMakeReady(
843 Vdbe *p, /* The VDBE */
844 int nVar, /* Number of '?' see in the SQL statement */
845 int nMem, /* Number of memory cells to allocate */
846 int nCursor, /* Number of cursors to allocate */
847 int isExplain /* True if the EXPLAIN keywords is present */
848){
849 int n;
850 sqlite3 *db = p->db;
851
852 assert( p!=0 );
853 assert( p->magic==VDBE_MAGIC_INIT );
854
855 /* There should be at least one opcode.
856 */
857 assert( p->nOp>0 );
858
859 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
860 * is because the call to resizeOpArray() below may shrink the
861 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
862 * state.
863 */
864 p->magic = VDBE_MAGIC_RUN;
865
866 /* No instruction ever pushes more than a single element onto the
867 ** stack. And the stack never grows on successive executions of the
868 ** same loop. So the total number of instructions is an upper bound
869 ** on the maximum stack depth required. (Added later:) The
870 ** resolveP2Values() call computes a tighter upper bound on the
871 ** stack size.
872 **
873 ** Allocation all the stack space we will ever need.
874 */
875 if( p->aStack==0 ){
876 int nArg; /* Maximum number of args passed to a user function. */
877 int nStack; /* Maximum number of stack entries required */
878 resolveP2Values(p, &nArg, &nStack);
879 resizeOpArray(p, p->nOp);
880 assert( nVar>=0 );
881 assert( nStack<p->nOp );
882 if( isExplain ){
883 nStack = 10;
884 }
885 p->aStack = sqlite3DbMallocZero(db,
886 nStack*sizeof(p->aStack[0]) /* aStack */
887 + nArg*sizeof(Mem*) /* apArg */
888 + nVar*sizeof(Mem) /* aVar */
889 + nVar*sizeof(char*) /* azVar */
890 + nMem*sizeof(Mem) /* aMem */
891 + nCursor*sizeof(Cursor*) /* apCsr */
892 );
893 if( !db->mallocFailed ){
894 p->aMem = &p->aStack[nStack];
895 p->nMem = nMem;
896 p->aVar = &p->aMem[nMem];
897 p->nVar = nVar;
898 p->okVar = 0;
899 p->apArg = (Mem**)&p->aVar[nVar];
900 p->azVar = (char**)&p->apArg[nArg];
901 p->apCsr = (Cursor**)&p->azVar[nVar];
902 p->nCursor = nCursor;
903 for(n=0; n<nVar; n++){
904 p->aVar[n].flags = MEM_Null;
905 p->aVar[n].db = db;
906 }
907 for(n=0; n<nStack; n++){
908 p->aStack[n].db = db;
909 }
910 }
911 }
912 for(n=0; n<p->nMem; n++){
913 p->aMem[n].flags = MEM_Null;
914 p->aMem[n].db = db;
915 }
916
917 p->pTos = &p->aStack[-1];
918 p->pc = -1;
919 p->rc = SQLITE_OK;
920 p->uniqueCnt = 0;
921 p->returnDepth = 0;
922 p->errorAction = OE_Abort;
923 p->popStack = 0;
924 p->explain |= isExplain;
925 p->magic = VDBE_MAGIC_RUN;
926 p->nChange = 0;
927 p->cacheCtr = 1;
928 p->minWriteFileFormat = 255;
929 p->openedStatement = 0;
930#ifdef VDBE_PROFILE
931 {
932 int i;
933 for(i=0; i<p->nOp; i++){
934 p->aOp[i].cnt = 0;
935 p->aOp[i].cycles = 0;
936 }
937 }
938#endif
939}
940
941/*
942** Close a VDBE cursor and release all the resources that cursor happens
943** to hold.
944*/
945void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
946 if( pCx==0 ){
947 return;
948 }
949 if( pCx->pCursor ){
950 sqlite3BtreeCloseCursor(pCx->pCursor);
951 }
952 if( pCx->pBt ){
953 sqlite3BtreeClose(pCx->pBt);
954 }
955#ifndef SQLITE_OMIT_VIRTUALTABLE
956 if( pCx->pVtabCursor ){
957 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
958 const sqlite3_module *pModule = pCx->pModule;
959 p->inVtabMethod = 1;
960 sqlite3SafetyOff(p->db);
961 pModule->xClose(pVtabCursor);
962 sqlite3SafetyOn(p->db);
963 p->inVtabMethod = 0;
964 }
965#endif
966 sqlite3_free(pCx->pData);
967 sqlite3_free(pCx->aType);
968 sqlite3_free(pCx);
969}
970
971/*
972** Close all cursors except for VTab cursors that are currently
973** in use.
974*/
975static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
976 int i;
977 if( p->apCsr==0 ) return;
978 for(i=0; i<p->nCursor; i++){
979 Cursor *pC = p->apCsr[i];
980 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
981 sqlite3VdbeFreeCursor(p, pC);
982 p->apCsr[i] = 0;
983 }
984 }
985}
986
987/*
988** Clean up the VM after execution.
989**
990** This routine will automatically close any cursors, lists, and/or
991** sorters that were left open. It also deletes the values of
992** variables in the aVar[] array.
993*/
994static void Cleanup(Vdbe *p){
995 int i;
996 if( p->aStack ){
997 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
998 p->pTos = &p->aStack[-1];
999 }
1000 closeAllCursorsExceptActiveVtabs(p);
1001 releaseMemArray(p->aMem, p->nMem);
1002 sqlite3VdbeFifoClear(&p->sFifo);
1003 if( p->contextStack ){
1004 for(i=0; i<p->contextStackTop; i++){
1005 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
1006 }
1007 sqlite3_free(p->contextStack);
1008 }
1009 p->contextStack = 0;
1010 p->contextStackDepth = 0;
1011 p->contextStackTop = 0;
1012 sqlite3_free(p->zErrMsg);
1013 p->zErrMsg = 0;
1014 p->resOnStack = 0;
1015}
1016
1017/*
1018** Set the number of result columns that will be returned by this SQL
1019** statement. This is now set at compile time, rather than during
1020** execution of the vdbe program so that sqlite3_column_count() can
1021** be called on an SQL statement before sqlite3_step().
1022*/
1023void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
1024 Mem *pColName;
1025 int n;
1026
1027 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1028 sqlite3_free(p->aColName);
1029 n = nResColumn*COLNAME_N;
1030 p->nResColumn = nResColumn;
1031 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
1032 if( p->aColName==0 ) return;
1033 while( n-- > 0 ){
1034 pColName->flags = MEM_Null;
1035 pColName->db = p->db;
1036 pColName++;
1037 }
1038}
1039
1040/*
1041** Set the name of the idx'th column to be returned by the SQL statement.
1042** zName must be a pointer to a nul terminated string.
1043**
1044** This call must be made after a call to sqlite3VdbeSetNumCols().
1045**
1046** If N==P3_STATIC it means that zName is a pointer to a constant static
1047** string and we can just copy the pointer. If it is P3_DYNAMIC, then
1048** the string is freed using sqlite3_free() when the vdbe is finished with
1049** it. Otherwise, N bytes of zName are copied.
1050*/
1051int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
1052 int rc;
1053 Mem *pColName;
1054 assert( idx<p->nResColumn );
1055 assert( var<COLNAME_N );
1056 if( p->db->mallocFailed ) return SQLITE_NOMEM;
1057 assert( p->aColName!=0 );
1058 pColName = &(p->aColName[idx+var*p->nResColumn]);
1059 if( N==P3_DYNAMIC || N==P3_STATIC ){
1060 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
1061 }else{
1062 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
1063 }
1064 if( rc==SQLITE_OK && N==P3_DYNAMIC ){
1065 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
1066 pColName->xDel = 0;
1067 }
1068 return rc;
1069}
1070
1071/*
1072** A read or write transaction may or may not be active on database handle
1073** db. If a transaction is active, commit it. If there is a
1074** write-transaction spanning more than one database file, this routine
1075** takes care of the master journal trickery.
1076*/
1077static int vdbeCommit(sqlite3 *db){
1078 int i;
1079 int nTrans = 0; /* Number of databases with an active write-transaction */
1080 int rc = SQLITE_OK;
1081 int needXcommit = 0;
1082
1083 /* Before doing anything else, call the xSync() callback for any
1084 ** virtual module tables written in this transaction. This has to
1085 ** be done before determining whether a master journal file is
1086 ** required, as an xSync() callback may add an attached database
1087 ** to the transaction.
1088 */
1089 rc = sqlite3VtabSync(db, rc);
1090 if( rc!=SQLITE_OK ){
1091 return rc;
1092 }
1093
1094 /* This loop determines (a) if the commit hook should be invoked and
1095 ** (b) how many database files have open write transactions, not
1096 ** including the temp database. (b) is important because if more than
1097 ** one database file has an open write transaction, a master journal
1098 ** file is required for an atomic commit.
1099 */
1100 for(i=0; i<db->nDb; i++){
1101 Btree *pBt = db->aDb[i].pBt;
1102 if( sqlite3BtreeIsInTrans(pBt) ){
1103 needXcommit = 1;
1104 if( i!=1 ) nTrans++;
1105 }
1106 }
1107
1108 /* If there are any write-transactions at all, invoke the commit hook */
1109 if( needXcommit && db->xCommitCallback ){
1110 sqlite3SafetyOff(db);
1111 rc = db->xCommitCallback(db->pCommitArg);
1112 sqlite3SafetyOn(db);
1113 if( rc ){
1114 return SQLITE_CONSTRAINT;
1115 }
1116 }
1117
1118 /* The simple case - no more than one database file (not counting the
1119 ** TEMP database) has a transaction active. There is no need for the
1120 ** master-journal.
1121 **
1122 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1123 ** string, it means the main database is :memory:. In that case we do
1124 ** not support atomic multi-file commits, so use the simple case then
1125 ** too.
1126 */
1127 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
1128 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1129 Btree *pBt = db->aDb[i].pBt;
1130 if( pBt ){
1131 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
1132 }
1133 }
1134
1135 /* Do the commit only if all databases successfully complete phase 1.
1136 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1137 ** IO error while deleting or truncating a journal file. It is unlikely,
1138 ** but could happen. In this case abandon processing and return the error.
1139 */
1140 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1141 Btree *pBt = db->aDb[i].pBt;
1142 if( pBt ){
1143 rc = sqlite3BtreeCommitPhaseTwo(pBt);
1144 }
1145 }
1146 if( rc==SQLITE_OK ){
1147 sqlite3VtabCommit(db);
1148 }
1149 }
1150
1151 /* The complex case - There is a multi-file write-transaction active.
1152 ** This requires a master journal file to ensure the transaction is
1153 ** committed atomicly.
1154 */
1155#ifndef SQLITE_OMIT_DISKIO
1156 else{
1157 sqlite3_vfs *pVfs = db->pVfs;
1158 int needSync = 0;
1159 char *zMaster = 0; /* File-name for the master journal */
1160 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1161 sqlite3_file *pMaster = 0;
1162 i64 offset = 0;
1163
1164 /* Select a master journal file name */
1165 do {
1166 u32 random;
1167 sqlite3_free(zMaster);
1168 sqlite3Randomness(sizeof(random), &random);
1169 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
1170 if( !zMaster ){
1171 return SQLITE_NOMEM;
1172 }
1173 }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
1174
1175 /* Open the master journal. */
1176 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1177 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1178 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1179 );
1180 if( rc!=SQLITE_OK ){
1181 sqlite3_free(zMaster);
1182 return rc;
1183 }
1184
1185 /* Write the name of each database file in the transaction into the new
1186 ** master journal file. If an error occurs at this point close
1187 ** and delete the master journal file. All the individual journal files
1188 ** still have 'null' as the master journal pointer, so they will roll
1189 ** back independently if a failure occurs.
1190 */
1191 for(i=0; i<db->nDb; i++){
1192 Btree *pBt = db->aDb[i].pBt;
1193 if( i==1 ) continue; /* Ignore the TEMP database */
1194 if( sqlite3BtreeIsInTrans(pBt) ){
1195 char const *zFile = sqlite3BtreeGetJournalname(pBt);
1196 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1197 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1198 needSync = 1;
1199 }
1200 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
1201 offset += strlen(zFile)+1;
1202 if( rc!=SQLITE_OK ){
1203 sqlite3OsCloseFree(pMaster);
1204 sqlite3OsDelete(pVfs, zMaster, 0);
1205 sqlite3_free(zMaster);
1206 return rc;
1207 }
1208 }
1209 }
1210
1211 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1212 ** flag is set this is not required.
1213 */
1214 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1215 if( (needSync
1216 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
1217 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
1218 sqlite3OsCloseFree(pMaster);
1219 sqlite3OsDelete(pVfs, zMaster, 0);
1220 sqlite3_free(zMaster);
1221 return rc;
1222 }
1223
1224 /* Sync all the db files involved in the transaction. The same call
1225 ** sets the master journal pointer in each individual journal. If
1226 ** an error occurs here, do not delete the master journal file.
1227 **
1228 ** If the error occurs during the first call to
1229 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1230 ** master journal file will be orphaned. But we cannot delete it,
1231 ** in case the master journal file name was written into the journal
1232 ** file before the failure occured.
1233 */
1234 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1235 Btree *pBt = db->aDb[i].pBt;
1236 if( pBt ){
1237 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
1238 }
1239 }
1240 sqlite3OsCloseFree(pMaster);
1241 if( rc!=SQLITE_OK ){
1242 sqlite3_free(zMaster);
1243 return rc;
1244 }
1245
1246 /* Delete the master journal file. This commits the transaction. After
1247 ** doing this the directory is synced again before any individual
1248 ** transaction files are deleted.
1249 */
1250 rc = sqlite3OsDelete(pVfs, zMaster, 1);
1251 sqlite3_free(zMaster);
1252 zMaster = 0;
1253 if( rc ){
1254 return rc;
1255 }
1256
1257 /* All files and directories have already been synced, so the following
1258 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1259 ** deleting or truncating journals. If something goes wrong while
1260 ** this is happening we don't really care. The integrity of the
1261 ** transaction is already guaranteed, but some stray 'cold' journals
1262 ** may be lying around. Returning an error code won't help matters.
1263 */
1264 disable_simulated_io_errors();
1265 for(i=0; i<db->nDb; i++){
1266 Btree *pBt = db->aDb[i].pBt;
1267 if( pBt ){
1268 sqlite3BtreeCommitPhaseTwo(pBt);
1269 }
1270 }
1271 enable_simulated_io_errors();
1272
1273 sqlite3VtabCommit(db);
1274 }
1275#endif
1276
1277 return rc;
1278}
1279
1280/*
1281** This routine checks that the sqlite3.activeVdbeCnt count variable
1282** matches the number of vdbe's in the list sqlite3.pVdbe that are
1283** currently active. An assertion fails if the two counts do not match.
1284** This is an internal self-check only - it is not an essential processing
1285** step.
1286**
1287** This is a no-op if NDEBUG is defined.
1288*/
1289#ifndef NDEBUG
1290static void checkActiveVdbeCnt(sqlite3 *db){
1291 Vdbe *p;
1292 int cnt = 0;
1293 p = db->pVdbe;
1294 while( p ){
1295 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
1296 cnt++;
1297 }
1298 p = p->pNext;
1299 }
1300 assert( cnt==db->activeVdbeCnt );
1301}
1302#else
1303#define checkActiveVdbeCnt(x)
1304#endif
1305
1306/*
1307** For every Btree that in database connection db which
1308** has been modified, "trip" or invalidate each cursor in
1309** that Btree might have been modified so that the cursor
1310** can never be used again. This happens when a rollback
1311*** occurs. We have to trip all the other cursors, even
1312** cursor from other VMs in different database connections,
1313** so that none of them try to use the data at which they
1314** were pointing and which now may have been changed due
1315** to the rollback.
1316**
1317** Remember that a rollback can delete tables complete and
1318** reorder rootpages. So it is not sufficient just to save
1319** the state of the cursor. We have to invalidate the cursor
1320** so that it is never used again.
1321*/
1322void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
1323 int i;
1324 for(i=0; i<db->nDb; i++){
1325 Btree *p = db->aDb[i].pBt;
1326 if( p && sqlite3BtreeIsInTrans(p) ){
1327 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1328 }
1329 }
1330}
1331
1332/*
1333** This routine is called the when a VDBE tries to halt. If the VDBE
1334** has made changes and is in autocommit mode, then commit those
1335** changes. If a rollback is needed, then do the rollback.
1336**
1337** This routine is the only way to move the state of a VM from
1338** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1339** call this on a VM that is in the SQLITE_MAGIC_HALT state.
1340**
1341** Return an error code. If the commit could not complete because of
1342** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1343** means the close did not happen and needs to be repeated.
1344*/
1345int sqlite3VdbeHalt(Vdbe *p){
1346 sqlite3 *db = p->db;
1347 int i;
1348 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
1349 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1350
1351 /* This function contains the logic that determines if a statement or
1352 ** transaction will be committed or rolled back as a result of the
1353 ** execution of this virtual machine.
1354 **
1355 ** If any of the following errors occur:
1356 **
1357 ** SQLITE_NOMEM
1358 ** SQLITE_IOERR
1359 ** SQLITE_FULL
1360 ** SQLITE_INTERRUPT
1361 **
1362 ** Then the internal cache might have been left in an inconsistent
1363 ** state. We need to rollback the statement transaction, if there is
1364 ** one, or the complete transaction if there is no statement transaction.
1365 */
1366
1367 if( p->db->mallocFailed ){
1368 p->rc = SQLITE_NOMEM;
1369 }
1370 closeAllCursorsExceptActiveVtabs(p);
1371 if( p->magic!=VDBE_MAGIC_RUN ){
1372 return SQLITE_OK;
1373 }
1374 checkActiveVdbeCnt(db);
1375
1376 /* No commit or rollback needed if the program never started */
1377 if( p->pc>=0 ){
1378 int mrc; /* Primary error code from p->rc */
1379
1380 /* Lock all btrees used by the statement */
1381 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1382
1383 /* Check for one of the special errors */
1384 mrc = p->rc & 0xff;
1385 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
1386 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL ;
1387 if( isSpecialError ){
1388 /* This loop does static analysis of the query to see which of the
1389 ** following three categories it falls into:
1390 **
1391 ** Read-only
1392 ** Query with statement journal
1393 ** Query without statement journal
1394 **
1395 ** We could do something more elegant than this static analysis (i.e.
1396 ** store the type of query as part of the compliation phase), but
1397 ** handling malloc() or IO failure is a fairly obscure edge case so
1398 ** this is probably easier. Todo: Might be an opportunity to reduce
1399 ** code size a very small amount though...
1400 */
1401 int notReadOnly = 0;
1402 int isStatement = 0;
1403 assert(p->aOp || p->nOp==0);
1404 for(i=0; i<p->nOp; i++){
1405 switch( p->aOp[i].opcode ){
1406 case OP_Transaction:
1407 notReadOnly |= p->aOp[i].p2;
1408 break;
1409 case OP_Statement:
1410 isStatement = 1;
1411 break;
1412 }
1413 }
1414
1415
1416 /* If the query was read-only, we need do no rollback at all. Otherwise,
1417 ** proceed with the special handling.
1418 */
1419 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
1420 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1421 xFunc = sqlite3BtreeRollbackStmt;
1422 p->rc = SQLITE_BUSY;
1423 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
1424 xFunc = sqlite3BtreeRollbackStmt;
1425 }else{
1426 /* We are forced to roll back the active transaction. Before doing
1427 ** so, abort any other statements this handle currently has active.
1428 */
1429 invalidateCursorsOnModifiedBtrees(db);
1430 sqlite3RollbackAll(db);
1431 db->autoCommit = 1;
1432 }
1433 }
1434 }
1435
1436 /* If the auto-commit flag is set and this is the only active vdbe, then
1437 ** we do either a commit or rollback of the current transaction.
1438 **
1439 ** Note: This block also runs if one of the special errors handled
1440 ** above has occured.
1441 */
1442 if( db->autoCommit && db->activeVdbeCnt==1 ){
1443 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1444 /* The auto-commit flag is true, and the vdbe program was
1445 ** successful or hit an 'OR FAIL' constraint. This means a commit
1446 ** is required.
1447 */
1448 int rc = vdbeCommit(db);
1449 if( rc==SQLITE_BUSY ){
1450 sqlite3BtreeMutexArrayLeave(&p->aMutex);
1451 return SQLITE_BUSY;
1452 }else if( rc!=SQLITE_OK ){
1453 p->rc = rc;
1454 sqlite3RollbackAll(db);
1455 }else{
1456 sqlite3CommitInternalChanges(db);
1457 }
1458 }else{
1459 sqlite3RollbackAll(db);
1460 }
1461 }else if( !xFunc ){
1462 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1463 if( p->openedStatement ){
1464 xFunc = sqlite3BtreeCommitStmt;
1465 }
1466 }else if( p->errorAction==OE_Abort ){
1467 xFunc = sqlite3BtreeRollbackStmt;
1468 }else{
1469 invalidateCursorsOnModifiedBtrees(db);
1470 sqlite3RollbackAll(db);
1471 db->autoCommit = 1;
1472 }
1473 }
1474
1475 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1476 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1477 ** and the return code is still SQLITE_OK, set the return code to the new
1478 ** error value.
1479 */
1480 assert(!xFunc ||
1481 xFunc==sqlite3BtreeCommitStmt ||
1482 xFunc==sqlite3BtreeRollbackStmt
1483 );
1484 for(i=0; xFunc && i<db->nDb; i++){
1485 int rc;
1486 Btree *pBt = db->aDb[i].pBt;
1487 if( pBt ){
1488 rc = xFunc(pBt);
1489 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1490 p->rc = rc;
1491 sqlite3SetString(&p->zErrMsg, 0);
1492 }
1493 }
1494 }
1495
1496 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1497 ** set the change counter.
1498 */
1499 if( p->changeCntOn && p->pc>=0 ){
1500 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1501 sqlite3VdbeSetChanges(db, p->nChange);
1502 }else{
1503 sqlite3VdbeSetChanges(db, 0);
1504 }
1505 p->nChange = 0;
1506 }
1507
1508 /* Rollback or commit any schema changes that occurred. */
1509 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1510 sqlite3ResetInternalSchema(db, 0);
1511 db->flags = (db->flags | SQLITE_InternChanges);
1512 }
1513
1514 /* Release the locks */
1515 sqlite3BtreeMutexArrayLeave(&p->aMutex);
1516 }
1517
1518 /* We have successfully halted and closed the VM. Record this fact. */
1519 if( p->pc>=0 ){
1520 db->activeVdbeCnt--;
1521 }
1522 p->magic = VDBE_MAGIC_HALT;
1523 checkActiveVdbeCnt(db);
1524 if( p->db->mallocFailed ){
1525 p->rc = SQLITE_NOMEM;
1526 }
1527 checkActiveVdbeCnt(db);
1528
1529 return SQLITE_OK;
1530}
1531
1532
1533/*
1534** Each VDBE holds the result of the most recent sqlite3_step() call
1535** in p->rc. This routine sets that result back to SQLITE_OK.
1536*/
1537void sqlite3VdbeResetStepResult(Vdbe *p){
1538 p->rc = SQLITE_OK;
1539}
1540
1541/*
1542** Clean up a VDBE after execution but do not delete the VDBE just yet.
1543** Write any error messages into *pzErrMsg. Return the result code.
1544**
1545** After this routine is run, the VDBE should be ready to be executed
1546** again.
1547**
1548** To look at it another way, this routine resets the state of the
1549** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1550** VDBE_MAGIC_INIT.
1551*/
1552int sqlite3VdbeReset(Vdbe *p){
1553 sqlite3 *db;
1554 db = p->db;
1555
1556 /* If the VM did not run to completion or if it encountered an
1557 ** error, then it might not have been halted properly. So halt
1558 ** it now.
1559 */
1560 sqlite3SafetyOn(db);
1561 sqlite3VdbeHalt(p);
1562 sqlite3SafetyOff(db);
1563
1564 /* If the VDBE has be run even partially, then transfer the error code
1565 ** and error message from the VDBE into the main database structure. But
1566 ** if the VDBE has just been set to run but has not actually executed any
1567 ** instructions yet, leave the main database error information unchanged.
1568 */
1569 if( p->pc>=0 ){
1570 if( p->zErrMsg ){
1571 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
1572 db->errCode = p->rc;
1573 p->zErrMsg = 0;
1574 }else if( p->rc ){
1575 sqlite3Error(db, p->rc, 0);
1576 }else{
1577 sqlite3Error(db, SQLITE_OK, 0);
1578 }
1579 }else if( p->rc && p->expired ){
1580 /* The expired flag was set on the VDBE before the first call
1581 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1582 ** called), set the database error in this case as well.
1583 */
1584 sqlite3Error(db, p->rc, 0);
1585 }
1586
1587 /* Reclaim all memory used by the VDBE
1588 */
1589 Cleanup(p);
1590
1591 /* Save profiling information from this VDBE run.
1592 */
1593 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
1594#ifdef VDBE_PROFILE
1595 {
1596 FILE *out = fopen("vdbe_profile.out", "a");
1597 if( out ){
1598 int i;
1599 fprintf(out, "---- ");
1600 for(i=0; i<p->nOp; i++){
1601 fprintf(out, "%02x", p->aOp[i].opcode);
1602 }
1603 fprintf(out, "\n");
1604 for(i=0; i<p->nOp; i++){
1605 fprintf(out, "%6d %10lld %8lld ",
1606 p->aOp[i].cnt,
1607 p->aOp[i].cycles,
1608 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1609 );
1610 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
1611 }
1612 fclose(out);
1613 }
1614 }
1615#endif
1616 p->magic = VDBE_MAGIC_INIT;
1617 p->aborted = 0;
1618 return p->rc & db->errMask;
1619}
1620
1621/*
1622** Clean up and delete a VDBE after execution. Return an integer which is
1623** the result code. Write any error message text into *pzErrMsg.
1624*/
1625int sqlite3VdbeFinalize(Vdbe *p){
1626 int rc = SQLITE_OK;
1627 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1628 rc = sqlite3VdbeReset(p);
1629 assert( (rc & p->db->errMask)==rc );
1630 }else if( p->magic!=VDBE_MAGIC_INIT ){
1631 return SQLITE_MISUSE;
1632 }
1633 sqlite3VdbeDelete(p);
1634 return rc;
1635}
1636
1637/*
1638** Call the destructor for each auxdata entry in pVdbeFunc for which
1639** the corresponding bit in mask is clear. Auxdata entries beyond 31
1640** are always destroyed. To destroy all auxdata entries, call this
1641** routine with mask==0.
1642*/
1643void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1644 int i;
1645 for(i=0; i<pVdbeFunc->nAux; i++){
1646 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1647 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1648 if( pAux->xDelete ){
1649 pAux->xDelete(pAux->pAux);
1650 }
1651 pAux->pAux = 0;
1652 }
1653 }
1654}
1655
1656/*
1657** Delete an entire VDBE.
1658*/
1659void sqlite3VdbeDelete(Vdbe *p){
1660 int i;
1661 if( p==0 ) return;
1662 Cleanup(p);
1663 if( p->pPrev ){
1664 p->pPrev->pNext = p->pNext;
1665 }else{
1666 assert( p->db->pVdbe==p );
1667 p->db->pVdbe = p->pNext;
1668 }
1669 if( p->pNext ){
1670 p->pNext->pPrev = p->pPrev;
1671 }
1672 if( p->aOp ){
1673 for(i=0; i<p->nOp; i++){
1674 Op *pOp = &p->aOp[i];
1675 freeP3(pOp->p3type, pOp->p3);
1676 }
1677 sqlite3_free(p->aOp);
1678 }
1679 releaseMemArray(p->aVar, p->nVar);
1680 sqlite3_free(p->aLabel);
1681 sqlite3_free(p->aStack);
1682 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1683 sqlite3_free(p->aColName);
1684 sqlite3_free(p->zSql);
1685 p->magic = VDBE_MAGIC_DEAD;
1686 sqlite3_free(p);
1687}
1688
1689/*
1690** If a MoveTo operation is pending on the given cursor, then do that
1691** MoveTo now. Return an error code. If no MoveTo is pending, this
1692** routine does nothing and returns SQLITE_OK.
1693*/
1694int sqlite3VdbeCursorMoveto(Cursor *p){
1695 if( p->deferredMoveto ){
1696 int res, rc;
1697#ifdef SQLITE_TEST
1698 extern int sqlite3_search_count;
1699#endif
1700 assert( p->isTable );
1701 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
1702 if( rc ) return rc;
1703 *p->pIncrKey = 0;
1704 p->lastRowid = keyToInt(p->movetoTarget);
1705 p->rowidIsValid = res==0;
1706 if( res<0 ){
1707 rc = sqlite3BtreeNext(p->pCursor, &res);
1708 if( rc ) return rc;
1709 }
1710#ifdef SQLITE_TEST
1711 sqlite3_search_count++;
1712#endif
1713 p->deferredMoveto = 0;
1714 p->cacheStatus = CACHE_STALE;
1715 }
1716 return SQLITE_OK;
1717}
1718
1719/*
1720** The following functions:
1721**
1722** sqlite3VdbeSerialType()
1723** sqlite3VdbeSerialTypeLen()
1724** sqlite3VdbeSerialRead()
1725** sqlite3VdbeSerialLen()
1726** sqlite3VdbeSerialWrite()
1727**
1728** encapsulate the code that serializes values for storage in SQLite
1729** data and index records. Each serialized value consists of a
1730** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1731** integer, stored as a varint.
1732**
1733** In an SQLite index record, the serial type is stored directly before
1734** the blob of data that it corresponds to. In a table record, all serial
1735** types are stored at the start of the record, and the blobs of data at
1736** the end. Hence these functions allow the caller to handle the
1737** serial-type and data blob seperately.
1738**
1739** The following table describes the various storage classes for data:
1740**
1741** serial type bytes of data type
1742** -------------- --------------- ---------------
1743** 0 0 NULL
1744** 1 1 signed integer
1745** 2 2 signed integer
1746** 3 3 signed integer
1747** 4 4 signed integer
1748** 5 6 signed integer
1749** 6 8 signed integer
1750** 7 8 IEEE float
1751** 8 0 Integer constant 0
1752** 9 0 Integer constant 1
1753** 10,11 reserved for expansion
1754** N>=12 and even (N-12)/2 BLOB
1755** N>=13 and odd (N-13)/2 text
1756**
1757** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1758** of SQLite will not understand those serial types.
1759*/
1760
1761/*
1762** Return the serial-type for the value stored in pMem.
1763*/
1764u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
1765 int flags = pMem->flags;
1766 int n;
1767
1768 if( flags&MEM_Null ){
1769 return 0;
1770 }
1771 if( flags&MEM_Int ){
1772 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
1773# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
1774 i64 i = pMem->u.i;
1775 u64 u;
1776 if( file_format>=4 && (i&1)==i ){
1777 return 8+i;
1778 }
1779 u = i<0 ? -i : i;
1780 if( u<=127 ) return 1;
1781 if( u<=32767 ) return 2;
1782 if( u<=8388607 ) return 3;
1783 if( u<=2147483647 ) return 4;
1784 if( u<=MAX_6BYTE ) return 5;
1785 return 6;
1786 }
1787 if( flags&MEM_Real ){
1788 return 7;
1789 }
1790 assert( flags&(MEM_Str|MEM_Blob) );
1791 n = pMem->n;
1792 if( flags & MEM_Zero ){
1793 n += pMem->u.i;
1794 }
1795 assert( n>=0 );
1796 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
1797}
1798
1799/*
1800** Return the length of the data corresponding to the supplied serial-type.
1801*/
1802int sqlite3VdbeSerialTypeLen(u32 serial_type){
1803 if( serial_type>=12 ){
1804 return (serial_type-12)/2;
1805 }else{
1806 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
1807 return aSize[serial_type];
1808 }
1809}
1810
1811/*
1812** If we are on an architecture with mixed-endian floating
1813** points (ex: ARM7) then swap the lower 4 bytes with the
1814** upper 4 bytes. Return the result.
1815**
1816** For most architectures, this is a no-op.
1817**
1818** (later): It is reported to me that the mixed-endian problem
1819** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1820** that early versions of GCC stored the two words of a 64-bit
1821** float in the wrong order. And that error has been propagated
1822** ever since. The blame is not necessarily with GCC, though.
1823** GCC might have just copying the problem from a prior compiler.
1824** I am also told that newer versions of GCC that follow a different
1825** ABI get the byte order right.
1826**
1827** Developers using SQLite on an ARM7 should compile and run their
1828** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1829** enabled, some asserts below will ensure that the byte order of
1830** floating point values is correct.
1831**
1832** (2007-08-30) Frank van Vugt has studied this problem closely
1833** and has send his findings to the SQLite developers. Frank
1834** writes that some Linux kernels offer floating point hardware
1835** emulation that uses only 32-bit mantissas instead of a full
1836** 48-bits as required by the IEEE standard. (This is the
1837** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1838** byte swapping becomes very complicated. To avoid problems,
1839** the necessary byte swapping is carried out using a 64-bit integer
1840** rather than a 64-bit float. Frank assures us that the code here
1841** works for him. We, the developers, have no way to independently
1842** verify this, but Frank seems to know what he is talking about
1843** so we trust him.
1844*/
1845#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
1846static u64 floatSwap(u64 in){
1847 union {
1848 u64 r;
1849 u32 i[2];
1850 } u;
1851 u32 t;
1852
1853 u.r = in;
1854 t = u.i[0];
1855 u.i[0] = u.i[1];
1856 u.i[1] = t;
1857 return u.r;
1858}
1859# define swapMixedEndianFloat(X) X = floatSwap(X)
1860#else
1861# define swapMixedEndianFloat(X)
1862#endif
1863
1864/*
1865** Write the serialized data blob for the value stored in pMem into
1866** buf. It is assumed that the caller has allocated sufficient space.
1867** Return the number of bytes written.
1868**
1869** nBuf is the amount of space left in buf[]. nBuf must always be
1870** large enough to hold the entire field. Except, if the field is
1871** a blob with a zero-filled tail, then buf[] might be just the right
1872** size to hold everything except for the zero-filled tail. If buf[]
1873** is only big enough to hold the non-zero prefix, then only write that
1874** prefix into buf[]. But if buf[] is large enough to hold both the
1875** prefix and the tail then write the prefix and set the tail to all
1876** zeros.
1877**
1878** Return the number of bytes actually written into buf[]. The number
1879** of bytes in the zero-filled tail is included in the return value only
1880** if those bytes were zeroed in buf[].
1881*/
1882int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
1883 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
1884 int len;
1885
1886 /* Integer and Real */
1887 if( serial_type<=7 && serial_type>0 ){
1888 u64 v;
1889 int i;
1890 if( serial_type==7 ){
1891 assert( sizeof(v)==sizeof(pMem->r) );
1892 memcpy(&v, &pMem->r, sizeof(v));
1893 swapMixedEndianFloat(v);
1894 }else{
1895 v = pMem->u.i;
1896 }
1897 len = i = sqlite3VdbeSerialTypeLen(serial_type);
1898 assert( len<=nBuf );
1899 while( i-- ){
1900 buf[i] = (v&0xFF);
1901 v >>= 8;
1902 }
1903 return len;
1904 }
1905
1906 /* String or blob */
1907 if( serial_type>=12 ){
1908 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1909 == sqlite3VdbeSerialTypeLen(serial_type) );
1910 assert( pMem->n<=nBuf );
1911 len = pMem->n;
1912 memcpy(buf, pMem->z, len);
1913 if( pMem->flags & MEM_Zero ){
1914 len += pMem->u.i;
1915 if( len>nBuf ){
1916 len = nBuf;
1917 }
1918 memset(&buf[pMem->n], 0, len-pMem->n);
1919 }
1920 return len;
1921 }
1922
1923 /* NULL or constants 0 or 1 */
1924 return 0;
1925}
1926
1927/*
1928** Deserialize the data blob pointed to by buf as serial type serial_type
1929** and store the result in pMem. Return the number of bytes read.
1930*/
1931int sqlite3VdbeSerialGet(
1932 const unsigned char *buf, /* Buffer to deserialize from */
1933 u32 serial_type, /* Serial type to deserialize */
1934 Mem *pMem /* Memory cell to write value into */
1935){
1936 switch( serial_type ){
1937 case 10: /* Reserved for future use */
1938 case 11: /* Reserved for future use */
1939 case 0: { /* NULL */
1940 pMem->flags = MEM_Null;
1941 break;
1942 }
1943 case 1: { /* 1-byte signed integer */
1944 pMem->u.i = (signed char)buf[0];
1945 pMem->flags = MEM_Int;
1946 return 1;
1947 }
1948 case 2: { /* 2-byte signed integer */
1949 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
1950 pMem->flags = MEM_Int;
1951 return 2;
1952 }
1953 case 3: { /* 3-byte signed integer */
1954 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1955 pMem->flags = MEM_Int;
1956 return 3;
1957 }
1958 case 4: { /* 4-byte signed integer */
1959 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1960 pMem->flags = MEM_Int;
1961 return 4;
1962 }
1963 case 5: { /* 6-byte signed integer */
1964 u64 x = (((signed char)buf[0])<<8) | buf[1];
1965 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1966 x = (x<<32) | y;
1967 pMem->u.i = *(i64*)&x;
1968 pMem->flags = MEM_Int;
1969 return 6;
1970 }
1971 case 6: /* 8-byte signed integer */
1972 case 7: { /* IEEE floating point */
1973 u64 x;
1974 u32 y;
1975#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
1976 /* Verify that integers and floating point values use the same
1977 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
1978 ** defined that 64-bit floating point values really are mixed
1979 ** endian.
1980 */
1981 static const u64 t1 = ((u64)0x3ff00000)<<32;
1982 static const double r1 = 1.0;
1983 u64 t2 = t1;
1984 swapMixedEndianFloat(t2);
1985 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
1986#endif
1987
1988 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1989 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
1990 x = (x<<32) | y;
1991 if( serial_type==6 ){
1992 pMem->u.i = *(i64*)&x;
1993 pMem->flags = MEM_Int;
1994 }else{
1995 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
1996 swapMixedEndianFloat(x);
1997 memcpy(&pMem->r, &x, sizeof(x));
1998 pMem->flags = MEM_Real;
1999 }
2000 return 8;
2001 }
2002 case 8: /* Integer 0 */
2003 case 9: { /* Integer 1 */
2004 pMem->u.i = serial_type-8;
2005 pMem->flags = MEM_Int;
2006 return 0;
2007 }
2008 default: {
2009 int len = (serial_type-12)/2;
2010 pMem->z = (char *)buf;
2011 pMem->n = len;
2012 pMem->xDel = 0;
2013 if( serial_type&0x01 ){
2014 pMem->flags = MEM_Str | MEM_Ephem;
2015 }else{
2016 pMem->flags = MEM_Blob | MEM_Ephem;
2017 }
2018 return len;
2019 }
2020 }
2021 return 0;
2022}
2023
2024/*
2025** The header of a record consists of a sequence variable-length integers.
2026** These integers are almost always small and are encoded as a single byte.
2027** The following macro takes advantage this fact to provide a fast decode
2028** of the integers in a record header. It is faster for the common case
2029** where the integer is a single byte. It is a little slower when the
2030** integer is two or more bytes. But overall it is faster.
2031**
2032** The following expressions are equivalent:
2033**
2034** x = sqlite3GetVarint32( A, &B );
2035**
2036** x = GetVarint( A, B );
2037**
2038*/
2039#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
2040
2041/*
2042** This function compares the two table rows or index records specified by
2043** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
2044** or positive integer if {nKey1, pKey1} is less than, equal to or
2045** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
2046** composed by the OP_MakeRecord opcode of the VDBE.
2047*/
2048int sqlite3VdbeRecordCompare(
2049 void *userData,
2050 int nKey1, const void *pKey1,
2051 int nKey2, const void *pKey2
2052){
2053 KeyInfo *pKeyInfo = (KeyInfo*)userData;
2054 u32 d1, d2; /* Offset into aKey[] of next data element */
2055 u32 idx1, idx2; /* Offset into aKey[] of next header element */
2056 u32 szHdr1, szHdr2; /* Number of bytes in header */
2057 int i = 0;
2058 int nField;
2059 int rc = 0;
2060 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2061 const unsigned char *aKey2 = (const unsigned char *)pKey2;
2062
2063 Mem mem1;
2064 Mem mem2;
2065 mem1.enc = pKeyInfo->enc;
2066 mem1.db = pKeyInfo->db;
2067 mem2.enc = pKeyInfo->enc;
2068 mem2.db = pKeyInfo->db;
2069
2070 idx1 = GetVarint(aKey1, szHdr1);
2071 d1 = szHdr1;
2072 idx2 = GetVarint(aKey2, szHdr2);
2073 d2 = szHdr2;
2074 nField = pKeyInfo->nField;
2075 while( idx1<szHdr1 && idx2<szHdr2 ){
2076 u32 serial_type1;
2077 u32 serial_type2;
2078
2079 /* Read the serial types for the next element in each key. */
2080 idx1 += GetVarint( aKey1+idx1, serial_type1 );
2081 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2082 idx2 += GetVarint( aKey2+idx2, serial_type2 );
2083 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
2084
2085 /* Extract the values to be compared.
2086 */
2087 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2088 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
2089
2090 /* Do the comparison
2091 */
2092 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
2093 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
2094 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
2095 if( rc!=0 ){
2096 break;
2097 }
2098 i++;
2099 }
2100
2101 /* One of the keys ran out of fields, but all the fields up to that point
2102 ** were equal. If the incrKey flag is true, then the second key is
2103 ** treated as larger.
2104 */
2105 if( rc==0 ){
2106 if( pKeyInfo->incrKey ){
2107 rc = -1;
2108 }else if( d1<nKey1 ){
2109 rc = 1;
2110 }else if( d2<nKey2 ){
2111 rc = -1;
2112 }
2113 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2114 && pKeyInfo->aSortOrder[i] ){
2115 rc = -rc;
2116 }
2117
2118 return rc;
2119}
2120
2121/*
2122** The argument is an index entry composed using the OP_MakeRecord opcode.
2123** The last entry in this record should be an integer (specifically
2124** an integer rowid). This routine returns the number of bytes in
2125** that integer.
2126*/
2127int sqlite3VdbeIdxRowidLen(const u8 *aKey){
2128 u32 szHdr; /* Size of the header */
2129 u32 typeRowid; /* Serial type of the rowid */
2130
2131 sqlite3GetVarint32(aKey, &szHdr);
2132 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2133 return sqlite3VdbeSerialTypeLen(typeRowid);
2134}
2135
2136
2137/*
2138** pCur points at an index entry created using the OP_MakeRecord opcode.
2139** Read the rowid (the last field in the record) and store it in *rowid.
2140** Return SQLITE_OK if everything works, or an error code otherwise.
2141*/
2142int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
2143 i64 nCellKey = 0;
2144 int rc;
2145 u32 szHdr; /* Size of the header */
2146 u32 typeRowid; /* Serial type of the rowid */
2147 u32 lenRowid; /* Size of the rowid */
2148 Mem m, v;
2149
2150 sqlite3BtreeKeySize(pCur, &nCellKey);
2151 if( nCellKey<=0 ){
2152 return SQLITE_CORRUPT_BKPT;
2153 }
2154 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
2155 if( rc ){
2156 return rc;
2157 }
2158 sqlite3GetVarint32((u8*)m.z, &szHdr);
2159 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
2160 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
2161 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
2162 *rowid = v.u.i;
2163 sqlite3VdbeMemRelease(&m);
2164 return SQLITE_OK;
2165}
2166
2167/*
2168** Compare the key of the index entry that cursor pC is point to against
2169** the key string in pKey (of length nKey). Write into *pRes a number
2170** that is negative, zero, or positive if pC is less than, equal to,
2171** or greater than pKey. Return SQLITE_OK on success.
2172**
2173** pKey is either created without a rowid or is truncated so that it
2174** omits the rowid at the end. The rowid at the end of the index entry
2175** is ignored as well.
2176*/
2177int sqlite3VdbeIdxKeyCompare(
2178 Cursor *pC, /* The cursor to compare against */
2179 int nKey, const u8 *pKey, /* The key to compare */
2180 int *res /* Write the comparison result here */
2181){
2182 i64 nCellKey = 0;
2183 int rc;
2184 BtCursor *pCur = pC->pCursor;
2185 int lenRowid;
2186 Mem m;
2187
2188 sqlite3BtreeKeySize(pCur, &nCellKey);
2189 if( nCellKey<=0 ){
2190 *res = 0;
2191 return SQLITE_OK;
2192 }
2193 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2194 if( rc ){
2195 return rc;
2196 }
2197 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
2198 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
2199 sqlite3VdbeMemRelease(&m);
2200 return SQLITE_OK;
2201}
2202
2203/*
2204** This routine sets the value to be returned by subsequent calls to
2205** sqlite3_changes() on the database handle 'db'.
2206*/
2207void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
2208 assert( sqlite3_mutex_held(db->mutex) );
2209 db->nChange = nChange;
2210 db->nTotalChange += nChange;
2211}
2212
2213/*
2214** Set a flag in the vdbe to update the change counter when it is finalised
2215** or reset.
2216*/
2217void sqlite3VdbeCountChanges(Vdbe *v){
2218 v->changeCntOn = 1;
2219}
2220
2221/*
2222** Mark every prepared statement associated with a database connection
2223** as expired.
2224**
2225** An expired statement means that recompilation of the statement is
2226** recommend. Statements expire when things happen that make their
2227** programs obsolete. Removing user-defined functions or collating
2228** sequences, or changing an authorization function are the types of
2229** things that make prepared statements obsolete.
2230*/
2231void sqlite3ExpirePreparedStatements(sqlite3 *db){
2232 Vdbe *p;
2233 for(p = db->pVdbe; p; p=p->pNext){
2234 p->expired = 1;
2235 }
2236}
2237
2238/*
2239** Return the database associated with the Vdbe.
2240*/
2241sqlite3 *sqlite3VdbeDb(Vdbe *v){
2242 return v->db;
2243}
diff --git a/libraries/sqlite/win32/vdbeblob.c b/libraries/sqlite/win32/vdbeblob.c
new file mode 100755
index 0000000..d56fbd1
--- /dev/null
+++ b/libraries/sqlite/win32/vdbeblob.c
@@ -0,0 +1,340 @@
1/*
2** 2007 May 1
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.
10**
11*************************************************************************
12**
13** This file contains code used to implement incremental BLOB I/O.
14**
15** $Id: vdbeblob.c,v 1.16 2007/08/30 01:19:59 drh Exp $
16*/
17
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
21#ifndef SQLITE_OMIT_INCRBLOB
22
23/*
24** Valid sqlite3_blob* handles point to Incrblob structures.
25*/
26typedef struct Incrblob Incrblob;
27struct Incrblob {
28 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
29 int nByte; /* Size of open blob, in bytes */
30 int iOffset; /* Byte offset of blob in cursor data */
31 BtCursor *pCsr; /* Cursor pointing at blob row */
32 sqlite3_stmt *pStmt; /* Statement holding cursor open */
33 sqlite3 *db; /* The associated database */
34};
35
36/*
37** Open a blob handle.
38*/
39int sqlite3_blob_open(
40 sqlite3* db, /* The database connection */
41 const char *zDb, /* The attached database containing the blob */
42 const char *zTable, /* The table containing the blob */
43 const char *zColumn, /* The column containing the blob */
44 sqlite_int64 iRow, /* The row containing the glob */
45 int flags, /* True -> read/write access, false -> read-only */
46 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
47){
48 int nAttempt = 0;
49 int iCol; /* Index of zColumn in row-record */
50
51 /* This VDBE program seeks a btree cursor to the identified
52 ** db/table/row entry. The reason for using a vdbe program instead
53 ** of writing code to use the b-tree layer directly is that the
54 ** vdbe program will take advantage of the various transaction,
55 ** locking and error handling infrastructure built into the vdbe.
56 **
57 ** After seeking the cursor, the vdbe executes an OP_Callback.
58 ** Code external to the Vdbe then "borrows" the b-tree cursor and
59 ** uses it to implement the blob_read(), blob_write() and
60 ** blob_bytes() functions.
61 **
62 ** The sqlite3_blob_close() function finalizes the vdbe program,
63 ** which closes the b-tree cursor and (possibly) commits the
64 ** transaction.
65 */
66 static const VdbeOpList openBlob[] = {
67 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
68 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
69 {OP_Integer, 0, 0, 0}, /* 2: Database number */
70
71 /* One of the following two instructions is replaced by an
72 ** OP_Noop before exection.
73 */
74 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
75 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
76 {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */
77
78 {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */
79 {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */
80 {OP_Column, 0, 0, 0}, /* 8 */
81 {OP_Callback, 0, 0, 0}, /* 9 */
82 {OP_Close, 0, 0, 0}, /* 10 */
83 {OP_Halt, 0, 0, 0}, /* 11 */
84 };
85
86 Vdbe *v = 0;
87 int rc = SQLITE_OK;
88 char zErr[128];
89
90 zErr[0] = 0;
91 sqlite3_mutex_enter(db->mutex);
92 do {
93 Parse sParse;
94 Table *pTab;
95
96 memset(&sParse, 0, sizeof(Parse));
97 sParse.db = db;
98
99 rc = sqlite3SafetyOn(db);
100 if( rc!=SQLITE_OK ){
101 sqlite3_mutex_leave(db->mutex);
102 return rc;
103 }
104
105 sqlite3BtreeEnterAll(db);
106 pTab = sqlite3LocateTable(&sParse, zTable, zDb);
107 if( !pTab ){
108 if( sParse.zErrMsg ){
109 sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg);
110 }
111 sqlite3_free(sParse.zErrMsg);
112 rc = SQLITE_ERROR;
113 sqlite3SafetyOff(db);
114 sqlite3BtreeLeaveAll(db);
115 goto blob_open_out;
116 }
117
118 /* Now search pTab for the exact column. */
119 for(iCol=0; iCol < pTab->nCol; iCol++) {
120 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
121 break;
122 }
123 }
124 if( iCol==pTab->nCol ){
125 sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn);
126 rc = SQLITE_ERROR;
127 sqlite3SafetyOff(db);
128 sqlite3BtreeLeaveAll(db);
129 goto blob_open_out;
130 }
131
132 /* If the value is being opened for writing, check that the
133 ** column is not indexed. It is against the rules to open an
134 ** indexed column for writing.
135 */
136 if( flags ){
137 Index *pIdx;
138 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139 int j;
140 for(j=0; j<pIdx->nColumn; j++){
141 if( pIdx->aiColumn[j]==iCol ){
142 sqlite3_snprintf(sizeof(zErr), zErr,
143 "cannot open indexed column for writing");
144 rc = SQLITE_ERROR;
145 sqlite3SafetyOff(db);
146 sqlite3BtreeLeaveAll(db);
147 goto blob_open_out;
148 }
149 }
150 }
151 }
152
153 v = sqlite3VdbeCreate(db);
154 if( v ){
155 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
156 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
157
158 /* Configure the OP_Transaction */
159 sqlite3VdbeChangeP1(v, 0, iDb);
160 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0));
161
162 /* Configure the OP_VerifyCookie */
163 sqlite3VdbeChangeP1(v, 1, iDb);
164 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
165
166 /* Make sure a mutex is held on the table to be accessed */
167 sqlite3VdbeUsesBtree(v, iDb);
168
169 /* Configure the db number pushed onto the stack */
170 sqlite3VdbeChangeP1(v, 2, iDb);
171
172 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
173 ** parameter of the other to pTab->tnum.
174 */
175 sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1);
176 sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum);
177
178 /* Configure the OP_SetNumColumns. Configure the cursor to
179 ** think that the table has one more column than it really
180 ** does. An OP_Column to retrieve this imaginary column will
181 ** always return an SQL NULL. This is useful because it means
182 ** we can invoke OP_Column to fill in the vdbe cursors type
183 ** and offset cache without causing any IO.
184 */
185 sqlite3VdbeChangeP2(v, 5, pTab->nCol+1);
186 if( !db->mallocFailed ){
187 sqlite3VdbeMakeReady(v, 1, 0, 1, 0);
188 }
189 }
190
191 sqlite3BtreeLeaveAll(db);
192 rc = sqlite3SafetyOff(db);
193 if( rc!=SQLITE_OK || db->mallocFailed ){
194 goto blob_open_out;
195 }
196
197 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
198 rc = sqlite3_step((sqlite3_stmt *)v);
199 if( rc!=SQLITE_ROW ){
200 nAttempt++;
201 rc = sqlite3_finalize((sqlite3_stmt *)v);
202 sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db));
203 v = 0;
204 }
205 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
206
207 if( rc==SQLITE_ROW ){
208 /* The row-record has been opened successfully. Check that the
209 ** column in question contains text or a blob. If it contains
210 ** text, it is up to the caller to get the encoding right.
211 */
212 Incrblob *pBlob;
213 u32 type = v->apCsr[0]->aType[iCol];
214
215 if( type<12 ){
216 sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s",
217 type==0?"null": type==7?"real": "integer"
218 );
219 rc = SQLITE_ERROR;
220 goto blob_open_out;
221 }
222 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
223 if( db->mallocFailed ){
224 sqlite3_free(pBlob);
225 goto blob_open_out;
226 }
227 pBlob->flags = flags;
228 pBlob->pCsr = v->apCsr[0]->pCursor;
229 sqlite3BtreeEnterCursor(pBlob->pCsr);
230 sqlite3BtreeCacheOverflow(pBlob->pCsr);
231 sqlite3BtreeLeaveCursor(pBlob->pCsr);
232 pBlob->pStmt = (sqlite3_stmt *)v;
233 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
234 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
235 pBlob->db = db;
236 *ppBlob = (sqlite3_blob *)pBlob;
237 rc = SQLITE_OK;
238 }else if( rc==SQLITE_OK ){
239 sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow);
240 rc = SQLITE_ERROR;
241 }
242
243blob_open_out:
244 zErr[sizeof(zErr)-1] = '\0';
245 if( rc!=SQLITE_OK || db->mallocFailed ){
246 sqlite3_finalize((sqlite3_stmt *)v);
247 }
248 sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr));
249 rc = sqlite3ApiExit(db, rc);
250 sqlite3_mutex_leave(db->mutex);
251 return rc;
252}
253
254/*
255** Close a blob handle that was previously created using
256** sqlite3_blob_open().
257*/
258int sqlite3_blob_close(sqlite3_blob *pBlob){
259 Incrblob *p = (Incrblob *)pBlob;
260 int rc;
261
262 rc = sqlite3_finalize(p->pStmt);
263 sqlite3_free(p);
264 return rc;
265}
266
267/*
268** Perform a read or write operation on a blob
269*/
270static int blobReadWrite(
271 sqlite3_blob *pBlob,
272 void *z,
273 int n,
274 int iOffset,
275 int (*xCall)(BtCursor*, u32, u32, void*)
276){
277 int rc;
278 Incrblob *p = (Incrblob *)pBlob;
279 Vdbe *v;
280 sqlite3 *db = p->db;
281
282 /* Request is out of range. Return a transient error. */
283 if( (iOffset+n)>p->nByte ){
284 return SQLITE_ERROR;
285 }
286 sqlite3_mutex_enter(db->mutex);
287
288 /* If there is no statement handle, then the blob-handle has
289 ** already been invalidated. Return SQLITE_ABORT in this case.
290 */
291 v = (Vdbe*)p->pStmt;
292 if( v==0 ){
293 rc = SQLITE_ABORT;
294 }else{
295 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
296 ** returned, clean-up the statement handle.
297 */
298 assert( db == v->db );
299 sqlite3BtreeEnterCursor(p->pCsr);
300 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
301 sqlite3BtreeLeaveCursor(p->pCsr);
302 if( rc==SQLITE_ABORT ){
303 sqlite3VdbeFinalize(v);
304 p->pStmt = 0;
305 }else{
306 db->errCode = rc;
307 v->rc = rc;
308 }
309 }
310 rc = sqlite3ApiExit(db, rc);
311 sqlite3_mutex_leave(db->mutex);
312 return rc;
313}
314
315/*
316** Read data from a blob handle.
317*/
318int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
319 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
320}
321
322/*
323** Write data to a blob handle.
324*/
325int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
326 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
327}
328
329/*
330** Query a blob handle for the size of the data.
331**
332** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
333** so no mutex is required for access.
334*/
335int sqlite3_blob_bytes(sqlite3_blob *pBlob){
336 Incrblob *p = (Incrblob *)pBlob;
337 return p->nByte;
338}
339
340#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
diff --git a/libraries/sqlite/win32/vdbefifo.c b/libraries/sqlite/win32/vdbefifo.c
new file mode 100755
index 0000000..f03f5c6
--- /dev/null
+++ b/libraries/sqlite/win32/vdbefifo.c
@@ -0,0 +1,114 @@
1/*
2** 2005 June 16
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.
10**
11*************************************************************************
12** This file implements a FIFO queue of rowids used for processing
13** UPDATE and DELETE statements.
14*/
15#include "sqliteInt.h"
16#include "vdbeInt.h"
17
18/*
19** Allocate a new FifoPage and return a pointer to it. Return NULL if
20** we run out of memory. Leave space on the page for nEntry entries.
21*/
22static FifoPage *allocateFifoPage(int nEntry){
23 FifoPage *pPage;
24 if( nEntry>32767 ){
25 nEntry = 32767;
26 }
27 pPage = sqlite3_malloc( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
28 if( pPage ){
29 pPage->nSlot = nEntry;
30 pPage->iWrite = 0;
31 pPage->iRead = 0;
32 pPage->pNext = 0;
33 }
34 return pPage;
35}
36
37/*
38** Initialize a Fifo structure.
39*/
40void sqlite3VdbeFifoInit(Fifo *pFifo){
41 memset(pFifo, 0, sizeof(*pFifo));
42}
43
44/*
45** Push a single 64-bit integer value into the Fifo. Return SQLITE_OK
46** normally. SQLITE_NOMEM is returned if we are unable to allocate
47** memory.
48*/
49int sqlite3VdbeFifoPush(Fifo *pFifo, i64 val){
50 FifoPage *pPage;
51 pPage = pFifo->pLast;
52 if( pPage==0 ){
53 pPage = pFifo->pLast = pFifo->pFirst = allocateFifoPage(20);
54 if( pPage==0 ){
55 return SQLITE_NOMEM;
56 }
57 }else if( pPage->iWrite>=pPage->nSlot ){
58 pPage->pNext = allocateFifoPage(pFifo->nEntry);
59 if( pPage->pNext==0 ){
60 return SQLITE_NOMEM;
61 }
62 pPage = pFifo->pLast = pPage->pNext;
63 }
64 pPage->aSlot[pPage->iWrite++] = val;
65 pFifo->nEntry++;
66 return SQLITE_OK;
67}
68
69/*
70** Extract a single 64-bit integer value from the Fifo. The integer
71** extracted is the one least recently inserted. If the Fifo is empty
72** return SQLITE_DONE.
73*/
74int sqlite3VdbeFifoPop(Fifo *pFifo, i64 *pVal){
75 FifoPage *pPage;
76 if( pFifo->nEntry==0 ){
77 return SQLITE_DONE;
78 }
79 assert( pFifo->nEntry>0 );
80 pPage = pFifo->pFirst;
81 assert( pPage!=0 );
82 assert( pPage->iWrite>pPage->iRead );
83 assert( pPage->iWrite<=pPage->nSlot );
84 assert( pPage->iRead<pPage->nSlot );
85 assert( pPage->iRead>=0 );
86 *pVal = pPage->aSlot[pPage->iRead++];
87 pFifo->nEntry--;
88 if( pPage->iRead>=pPage->iWrite ){
89 pFifo->pFirst = pPage->pNext;
90 sqlite3_free(pPage);
91 if( pFifo->nEntry==0 ){
92 assert( pFifo->pLast==pPage );
93 pFifo->pLast = 0;
94 }else{
95 assert( pFifo->pFirst!=0 );
96 }
97 }else{
98 assert( pFifo->nEntry>0 );
99 }
100 return SQLITE_OK;
101}
102
103/*
104** Delete all information from a Fifo object. Free all memory held
105** by the Fifo.
106*/
107void sqlite3VdbeFifoClear(Fifo *pFifo){
108 FifoPage *pPage, *pNextPage;
109 for(pPage=pFifo->pFirst; pPage; pPage=pNextPage){
110 pNextPage = pPage->pNext;
111 sqlite3_free(pPage);
112 }
113 sqlite3VdbeFifoInit(pFifo);
114}
diff --git a/libraries/sqlite/win32/vdbemem.c b/libraries/sqlite/win32/vdbemem.c
new file mode 100755
index 0000000..58e2946
--- /dev/null
+++ b/libraries/sqlite/win32/vdbemem.c
@@ -0,0 +1,1017 @@
1/*
2** 2004 May 26
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.
10**
11*************************************************************************
12**
13** This file contains code use to manipulate "Mem" structure. A "Mem"
14** stores a single value in the VDBE. Mem is an opaque structure visible
15** only within the VDBE. Interface routines refer to a Mem using the
16** name sqlite_value
17*/
18#include "sqliteInt.h"
19#include <math.h>
20#include <ctype.h>
21#include "vdbeInt.h"
22
23/*
24** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
25** P if required.
26*/
27#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
28
29/*
30** If pMem is an object with a valid string representation, this routine
31** ensures the internal encoding for the string representation is
32** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
33**
34** If pMem is not a string object, or the encoding of the string
35** representation is already stored using the requested encoding, then this
36** routine is a no-op.
37**
38** SQLITE_OK is returned if the conversion is successful (or not required).
39** SQLITE_NOMEM may be returned if a malloc() fails during conversion
40** between formats.
41*/
42int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
43 int rc;
44 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
45 return SQLITE_OK;
46 }
47 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
48#ifdef SQLITE_OMIT_UTF16
49 return SQLITE_ERROR;
50#else
51
52 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
53 ** then the encoding of the value may not have changed.
54 */
55 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
56 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
57 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
58 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
59 return rc;
60#endif
61}
62
63/*
64** Make the given Mem object MEM_Dyn.
65**
66** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
67*/
68int sqlite3VdbeMemDynamicify(Mem *pMem){
69 int n;
70 u8 *z;
71 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
72 expandBlob(pMem);
73 if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
74 return SQLITE_OK;
75 }
76 assert( (pMem->flags & MEM_Dyn)==0 );
77 n = pMem->n;
78 assert( pMem->flags & (MEM_Str|MEM_Blob) );
79 z = sqlite3DbMallocRaw(pMem->db, n+2 );
80 if( z==0 ){
81 return SQLITE_NOMEM;
82 }
83 pMem->flags |= MEM_Dyn|MEM_Term;
84 pMem->xDel = 0;
85 memcpy(z, pMem->z, n );
86 z[n] = 0;
87 z[n+1] = 0;
88 pMem->z = (char*)z;
89 pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
90 return SQLITE_OK;
91}
92
93/*
94** If the given Mem* has a zero-filled tail, turn it into an ordinary
95** blob stored in dynamically allocated space.
96*/
97#ifndef SQLITE_OMIT_INCRBLOB
98int sqlite3VdbeMemExpandBlob(Mem *pMem){
99 if( pMem->flags & MEM_Zero ){
100 char *pNew;
101 int nByte;
102 assert( (pMem->flags & MEM_Blob)!=0 );
103 nByte = pMem->n + pMem->u.i;
104 if( nByte<=0 ) nByte = 1;
105 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
106 pNew = sqlite3DbMallocRaw(pMem->db, nByte);
107 if( pNew==0 ){
108 return SQLITE_NOMEM;
109 }
110 memcpy(pNew, pMem->z, pMem->n);
111 memset(&pNew[pMem->n], 0, pMem->u.i);
112 sqlite3VdbeMemRelease(pMem);
113 pMem->z = pNew;
114 pMem->n += pMem->u.i;
115 pMem->u.i = 0;
116 pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);
117 pMem->flags |= MEM_Dyn;
118 }
119 return SQLITE_OK;
120}
121#endif
122
123
124/*
125** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
126** of the Mem.z[] array can be modified.
127**
128** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
129*/
130int sqlite3VdbeMemMakeWriteable(Mem *pMem){
131 int n;
132 u8 *z;
133 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
134 expandBlob(pMem);
135 if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
136 return SQLITE_OK;
137 }
138 assert( (pMem->flags & MEM_Dyn)==0 );
139 assert( pMem->flags & (MEM_Str|MEM_Blob) );
140 if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
141 z = (u8*)pMem->zShort;
142 pMem->flags |= MEM_Short|MEM_Term;
143 }else{
144 z = sqlite3DbMallocRaw(pMem->db, n+2 );
145 if( z==0 ){
146 return SQLITE_NOMEM;
147 }
148 pMem->flags |= MEM_Dyn|MEM_Term;
149 pMem->xDel = 0;
150 }
151 memcpy(z, pMem->z, n );
152 z[n] = 0;
153 z[n+1] = 0;
154 pMem->z = (char*)z;
155 pMem->flags &= ~(MEM_Ephem|MEM_Static);
156 assert(0==(1&(int)pMem->z));
157 return SQLITE_OK;
158}
159
160/*
161** Make sure the given Mem is \u0000 terminated.
162*/
163int sqlite3VdbeMemNulTerminate(Mem *pMem){
164 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
165 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
166 return SQLITE_OK; /* Nothing to do */
167 }
168 if( pMem->flags & (MEM_Static|MEM_Ephem) ){
169 return sqlite3VdbeMemMakeWriteable(pMem);
170 }else{
171 char *z;
172 sqlite3VdbeMemExpandBlob(pMem);
173 z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);
174 if( !z ){
175 return SQLITE_NOMEM;
176 }
177 memcpy(z, pMem->z, pMem->n);
178 z[pMem->n] = 0;
179 z[pMem->n+1] = 0;
180 if( pMem->xDel ){
181 pMem->xDel(pMem->z);
182 }else{
183 sqlite3_free(pMem->z);
184 }
185 pMem->xDel = 0;
186 pMem->z = z;
187 pMem->flags |= MEM_Term;
188 }
189 return SQLITE_OK;
190}
191
192/*
193** Add MEM_Str to the set of representations for the given Mem. Numbers
194** are converted using sqlite3_snprintf(). Converting a BLOB to a string
195** is a no-op.
196**
197** Existing representations MEM_Int and MEM_Real are *not* invalidated.
198**
199** A MEM_Null value will never be passed to this function. This function is
200** used for converting values to text for returning to the user (i.e. via
201** sqlite3_value_text()), or for ensuring that values to be used as btree
202** keys are strings. In the former case a NULL pointer is returned the
203** user and the later is an internal programming error.
204*/
205int sqlite3VdbeMemStringify(Mem *pMem, int enc){
206 int rc = SQLITE_OK;
207 int fg = pMem->flags;
208 char *z = pMem->zShort;
209
210 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
211 assert( !(fg&MEM_Zero) );
212 assert( !(fg&(MEM_Str|MEM_Blob)) );
213 assert( fg&(MEM_Int|MEM_Real) );
214
215 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
216 ** string representation of the value. Then, if the required encoding
217 ** is UTF-16le or UTF-16be do a translation.
218 **
219 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
220 */
221 if( fg & MEM_Int ){
222 sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i);
223 }else{
224 assert( fg & MEM_Real );
225 sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
226 }
227 pMem->n = strlen(z);
228 pMem->z = z;
229 pMem->enc = SQLITE_UTF8;
230 pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
231 sqlite3VdbeChangeEncoding(pMem, enc);
232 return rc;
233}
234
235/*
236** Memory cell pMem contains the context of an aggregate function.
237** This routine calls the finalize method for that function. The
238** result of the aggregate is stored back into pMem.
239**
240** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
241** otherwise.
242*/
243int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
244 int rc = SQLITE_OK;
245 if( pFunc && pFunc->xFinalize ){
246 sqlite3_context ctx;
247 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
248 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
249 ctx.s.flags = MEM_Null;
250 ctx.s.z = pMem->zShort;
251 ctx.s.db = pMem->db;
252 ctx.pMem = pMem;
253 ctx.pFunc = pFunc;
254 ctx.isError = 0;
255 pFunc->xFinalize(&ctx);
256 if( pMem->z && pMem->z!=pMem->zShort ){
257 sqlite3_free( pMem->z );
258 }
259 *pMem = ctx.s;
260 if( pMem->flags & MEM_Short ){
261 pMem->z = pMem->zShort;
262 }
263 rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
264 }
265 return rc;
266}
267
268/*
269** Release any memory held by the Mem. This may leave the Mem in an
270** inconsistent state, for example with (Mem.z==0) and
271** (Mem.type==SQLITE_TEXT).
272*/
273void sqlite3VdbeMemRelease(Mem *p){
274 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
275 if( p->flags & (MEM_Dyn|MEM_Agg) ){
276 if( p->xDel ){
277 if( p->flags & MEM_Agg ){
278 sqlite3VdbeMemFinalize(p, p->u.pDef);
279 assert( (p->flags & MEM_Agg)==0 );
280 sqlite3VdbeMemRelease(p);
281 }else{
282 p->xDel((void *)p->z);
283 }
284 }else{
285 sqlite3_free(p->z);
286 }
287 p->z = 0;
288 p->xDel = 0;
289 }
290}
291
292/*
293** Return some kind of integer value which is the best we can do
294** at representing the value that *pMem describes as an integer.
295** If pMem is an integer, then the value is exact. If pMem is
296** a floating-point then the value returned is the integer part.
297** If pMem is a string or blob, then we make an attempt to convert
298** it into a integer and return that. If pMem is NULL, return 0.
299**
300** If pMem is a string, its encoding might be changed.
301*/
302i64 sqlite3VdbeIntValue(Mem *pMem){
303 int flags;
304 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
305 flags = pMem->flags;
306 if( flags & MEM_Int ){
307 return pMem->u.i;
308 }else if( flags & MEM_Real ){
309 return (i64)pMem->r;
310 }else if( flags & (MEM_Str|MEM_Blob) ){
311 i64 value;
312 pMem->flags |= MEM_Str;
313 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
314 || sqlite3VdbeMemNulTerminate(pMem) ){
315 return 0;
316 }
317 assert( pMem->z );
318 sqlite3Atoi64(pMem->z, &value);
319 return value;
320 }else{
321 return 0;
322 }
323}
324
325/*
326** Return the best representation of pMem that we can get into a
327** double. If pMem is already a double or an integer, return its
328** value. If it is a string or blob, try to convert it to a double.
329** If it is a NULL, return 0.0.
330*/
331double sqlite3VdbeRealValue(Mem *pMem){
332 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
333 if( pMem->flags & MEM_Real ){
334 return pMem->r;
335 }else if( pMem->flags & MEM_Int ){
336 return (double)pMem->u.i;
337 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
338 double val = 0.0;
339 pMem->flags |= MEM_Str;
340 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
341 || sqlite3VdbeMemNulTerminate(pMem) ){
342 return 0.0;
343 }
344 assert( pMem->z );
345 sqlite3AtoF(pMem->z, &val);
346 return val;
347 }else{
348 return 0.0;
349 }
350}
351
352/*
353** The MEM structure is already a MEM_Real. Try to also make it a
354** MEM_Int if we can.
355*/
356void sqlite3VdbeIntegerAffinity(Mem *pMem){
357 assert( pMem->flags & MEM_Real );
358 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
359 pMem->u.i = pMem->r;
360 if( ((double)pMem->u.i)==pMem->r ){
361 pMem->flags |= MEM_Int;
362 }
363}
364
365/*
366** Convert pMem to type integer. Invalidate any prior representations.
367*/
368int sqlite3VdbeMemIntegerify(Mem *pMem){
369 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
370 pMem->u.i = sqlite3VdbeIntValue(pMem);
371 sqlite3VdbeMemRelease(pMem);
372 pMem->flags = MEM_Int;
373 return SQLITE_OK;
374}
375
376/*
377** Convert pMem so that it is of type MEM_Real.
378** Invalidate any prior representations.
379*/
380int sqlite3VdbeMemRealify(Mem *pMem){
381 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
382 pMem->r = sqlite3VdbeRealValue(pMem);
383 sqlite3VdbeMemRelease(pMem);
384 pMem->flags = MEM_Real;
385 return SQLITE_OK;
386}
387
388/*
389** Convert pMem so that it has types MEM_Real or MEM_Int or both.
390** Invalidate any prior representations.
391*/
392int sqlite3VdbeMemNumerify(Mem *pMem){
393 double r1, r2;
394 i64 i;
395 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
396 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
397 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
398 r1 = sqlite3VdbeRealValue(pMem);
399 i = (i64)r1;
400 r2 = (double)i;
401 if( r1==r2 ){
402 sqlite3VdbeMemIntegerify(pMem);
403 }else{
404 pMem->r = r1;
405 pMem->flags = MEM_Real;
406 sqlite3VdbeMemRelease(pMem);
407 }
408 return SQLITE_OK;
409}
410
411/*
412** Delete any previous value and set the value stored in *pMem to NULL.
413*/
414void sqlite3VdbeMemSetNull(Mem *pMem){
415 sqlite3VdbeMemRelease(pMem);
416 pMem->flags = MEM_Null;
417 pMem->type = SQLITE_NULL;
418 pMem->n = 0;
419}
420
421/*
422** Delete any previous value and set the value to be a BLOB of length
423** n containing all zeros.
424*/
425void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
426 sqlite3VdbeMemRelease(pMem);
427 pMem->flags = MEM_Blob|MEM_Zero|MEM_Short;
428 pMem->type = SQLITE_BLOB;
429 pMem->n = 0;
430 if( n<0 ) n = 0;
431 pMem->u.i = n;
432 pMem->z = pMem->zShort;
433 pMem->enc = SQLITE_UTF8;
434}
435
436/*
437** Delete any previous value and set the value stored in *pMem to val,
438** manifest type INTEGER.
439*/
440void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
441 sqlite3VdbeMemRelease(pMem);
442 pMem->u.i = val;
443 pMem->flags = MEM_Int;
444 pMem->type = SQLITE_INTEGER;
445}
446
447/*
448** Delete any previous value and set the value stored in *pMem to val,
449** manifest type REAL.
450*/
451void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
452 if( sqlite3_isnan(val) ){
453 sqlite3VdbeMemSetNull(pMem);
454 }else{
455 sqlite3VdbeMemRelease(pMem);
456 pMem->r = val;
457 pMem->flags = MEM_Real;
458 pMem->type = SQLITE_FLOAT;
459 }
460}
461
462/*
463** Return true if the Mem object contains a TEXT or BLOB that is
464** too large - whose size exceeds SQLITE_MAX_LENGTH.
465*/
466int sqlite3VdbeMemTooBig(Mem *p){
467 if( p->flags & (MEM_Str|MEM_Blob) ){
468 int n = p->n;
469 if( p->flags & MEM_Zero ){
470 n += p->u.i;
471 }
472 return n>SQLITE_MAX_LENGTH;
473 }
474 return 0;
475}
476
477/*
478** Make an shallow copy of pFrom into pTo. Prior contents of
479** pTo are overwritten. The pFrom->z field is not duplicated. If
480** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
481** and flags gets srcType (either MEM_Ephem or MEM_Static).
482*/
483void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
484 memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
485 pTo->xDel = 0;
486 if( pTo->flags & (MEM_Str|MEM_Blob) ){
487 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
488 assert( srcType==MEM_Ephem || srcType==MEM_Static );
489 pTo->flags |= srcType;
490 }
491}
492
493/*
494** Make a full copy of pFrom into pTo. Prior contents of pTo are
495** freed before the copy is made.
496*/
497int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
498 int rc;
499 if( pTo->flags & MEM_Dyn ){
500 sqlite3VdbeMemRelease(pTo);
501 }
502 sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
503 if( pTo->flags & MEM_Ephem ){
504 rc = sqlite3VdbeMemMakeWriteable(pTo);
505 }else{
506 rc = SQLITE_OK;
507 }
508 return rc;
509}
510
511/*
512** Transfer the contents of pFrom to pTo. Any existing value in pTo is
513** freed. If pFrom contains ephemeral data, a copy is made.
514**
515** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM
516** might be returned if pFrom held ephemeral data and we were unable
517** to allocate enough space to make a copy.
518*/
519int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
520 int rc;
521 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
522 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
523 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
524 if( pTo->flags & MEM_Dyn ){
525 sqlite3VdbeMemRelease(pTo);
526 }
527 memcpy(pTo, pFrom, sizeof(Mem));
528 if( pFrom->flags & MEM_Short ){
529 pTo->z = pTo->zShort;
530 }
531 pFrom->flags = MEM_Null;
532 pFrom->xDel = 0;
533 if( pTo->flags & MEM_Ephem ){
534 rc = sqlite3VdbeMemMakeWriteable(pTo);
535 }else{
536 rc = SQLITE_OK;
537 }
538 return rc;
539}
540
541/*
542** Change the value of a Mem to be a string or a BLOB.
543*/
544int sqlite3VdbeMemSetStr(
545 Mem *pMem, /* Memory cell to set to string value */
546 const char *z, /* String pointer */
547 int n, /* Bytes in string, or negative */
548 u8 enc, /* Encoding of z. 0 for BLOBs */
549 void (*xDel)(void*) /* Destructor function */
550){
551 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
552 sqlite3VdbeMemRelease(pMem);
553 if( !z ){
554 pMem->flags = MEM_Null;
555 pMem->type = SQLITE_NULL;
556 return SQLITE_OK;
557 }
558 pMem->z = (char *)z;
559 if( xDel==SQLITE_STATIC ){
560 pMem->flags = MEM_Static;
561 }else if( xDel==SQLITE_TRANSIENT ){
562 pMem->flags = MEM_Ephem;
563 }else{
564 pMem->flags = MEM_Dyn;
565 pMem->xDel = xDel;
566 }
567
568 pMem->enc = enc;
569 pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
570 pMem->n = n;
571
572 assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
573 || enc==SQLITE_UTF16BE );
574 switch( enc ){
575 case 0:
576 pMem->flags |= MEM_Blob;
577 pMem->enc = SQLITE_UTF8;
578 break;
579
580 case SQLITE_UTF8:
581 pMem->flags |= MEM_Str;
582 if( n<0 ){
583 pMem->n = strlen(z);
584 pMem->flags |= MEM_Term;
585 }
586 break;
587
588#ifndef SQLITE_OMIT_UTF16
589 case SQLITE_UTF16LE:
590 case SQLITE_UTF16BE:
591 pMem->flags |= MEM_Str;
592 if( pMem->n<0 ){
593 pMem->n = sqlite3Utf16ByteLen(pMem->z,-1);
594 pMem->flags |= MEM_Term;
595 }
596 if( sqlite3VdbeMemHandleBom(pMem) ){
597 return SQLITE_NOMEM;
598 }
599#endif /* SQLITE_OMIT_UTF16 */
600 }
601 if( pMem->flags&MEM_Ephem ){
602 return sqlite3VdbeMemMakeWriteable(pMem);
603 }
604 return SQLITE_OK;
605}
606
607/*
608** Compare the values contained by the two memory cells, returning
609** negative, zero or positive if pMem1 is less than, equal to, or greater
610** than pMem2. Sorting order is NULL's first, followed by numbers (integers
611** and reals) sorted numerically, followed by text ordered by the collating
612** sequence pColl and finally blob's ordered by memcmp().
613**
614** Two NULL values are considered equal by this function.
615*/
616int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
617 int rc;
618 int f1, f2;
619 int combined_flags;
620
621 /* Interchange pMem1 and pMem2 if the collating sequence specifies
622 ** DESC order.
623 */
624 f1 = pMem1->flags;
625 f2 = pMem2->flags;
626 combined_flags = f1|f2;
627
628 /* If one value is NULL, it is less than the other. If both values
629 ** are NULL, return 0.
630 */
631 if( combined_flags&MEM_Null ){
632 return (f2&MEM_Null) - (f1&MEM_Null);
633 }
634
635 /* If one value is a number and the other is not, the number is less.
636 ** If both are numbers, compare as reals if one is a real, or as integers
637 ** if both values are integers.
638 */
639 if( combined_flags&(MEM_Int|MEM_Real) ){
640 if( !(f1&(MEM_Int|MEM_Real)) ){
641 return 1;
642 }
643 if( !(f2&(MEM_Int|MEM_Real)) ){
644 return -1;
645 }
646 if( (f1 & f2 & MEM_Int)==0 ){
647 double r1, r2;
648 if( (f1&MEM_Real)==0 ){
649 r1 = pMem1->u.i;
650 }else{
651 r1 = pMem1->r;
652 }
653 if( (f2&MEM_Real)==0 ){
654 r2 = pMem2->u.i;
655 }else{
656 r2 = pMem2->r;
657 }
658 if( r1<r2 ) return -1;
659 if( r1>r2 ) return 1;
660 return 0;
661 }else{
662 assert( f1&MEM_Int );
663 assert( f2&MEM_Int );
664 if( pMem1->u.i < pMem2->u.i ) return -1;
665 if( pMem1->u.i > pMem2->u.i ) return 1;
666 return 0;
667 }
668 }
669
670 /* If one value is a string and the other is a blob, the string is less.
671 ** If both are strings, compare using the collating functions.
672 */
673 if( combined_flags&MEM_Str ){
674 if( (f1 & MEM_Str)==0 ){
675 return 1;
676 }
677 if( (f2 & MEM_Str)==0 ){
678 return -1;
679 }
680
681 assert( pMem1->enc==pMem2->enc );
682 assert( pMem1->enc==SQLITE_UTF8 ||
683 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
684
685 /* The collation sequence must be defined at this point, even if
686 ** the user deletes the collation sequence after the vdbe program is
687 ** compiled (this was not always the case).
688 */
689 assert( !pColl || pColl->xCmp );
690
691 if( pColl ){
692 if( pMem1->enc==pColl->enc ){
693 /* The strings are already in the correct encoding. Call the
694 ** comparison function directly */
695 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
696 }else{
697 u8 origEnc = pMem1->enc;
698 const void *v1, *v2;
699 int n1, n2;
700 /* Convert the strings into the encoding that the comparison
701 ** function expects */
702 v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
703 n1 = v1==0 ? 0 : pMem1->n;
704 assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
705 v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
706 n2 = v2==0 ? 0 : pMem2->n;
707 assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
708 /* Do the comparison */
709 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
710 /* Convert the strings back into the database encoding */
711 sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
712 sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
713 return rc;
714 }
715 }
716 /* If a NULL pointer was passed as the collate function, fall through
717 ** to the blob case and use memcmp(). */
718 }
719
720 /* Both values must be blobs. Compare using memcmp(). */
721 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
722 if( rc==0 ){
723 rc = pMem1->n - pMem2->n;
724 }
725 return rc;
726}
727
728/*
729** Move data out of a btree key or data field and into a Mem structure.
730** The data or key is taken from the entry that pCur is currently pointing
731** to. offset and amt determine what portion of the data or key to retrieve.
732** key is true to get the key or false to get data. The result is written
733** into the pMem element.
734**
735** The pMem structure is assumed to be uninitialized. Any prior content
736** is overwritten without being freed.
737**
738** If this routine fails for any reason (malloc returns NULL or unable
739** to read from the disk) then the pMem is left in an inconsistent state.
740*/
741int sqlite3VdbeMemFromBtree(
742 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
743 int offset, /* Offset from the start of data to return bytes from. */
744 int amt, /* Number of bytes to return. */
745 int key, /* If true, retrieve from the btree key, not data. */
746 Mem *pMem /* OUT: Return data in this Mem structure. */
747){
748 char *zData; /* Data from the btree layer */
749 int available = 0; /* Number of bytes available on the local btree page */
750 sqlite3 *db; /* Database connection */
751
752 db = sqlite3BtreeCursorDb(pCur);
753 assert( sqlite3_mutex_held(db->mutex) );
754 if( key ){
755 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
756 }else{
757 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
758 }
759 assert( zData!=0 );
760
761 pMem->db = db;
762 pMem->n = amt;
763 if( offset+amt<=available ){
764 pMem->z = &zData[offset];
765 pMem->flags = MEM_Blob|MEM_Ephem;
766 }else{
767 int rc;
768 if( amt>NBFS-2 ){
769 zData = (char *)sqlite3DbMallocRaw(db, amt+2);
770 if( !zData ){
771 return SQLITE_NOMEM;
772 }
773 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
774 pMem->xDel = 0;
775 }else{
776 zData = &(pMem->zShort[0]);
777 pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
778 }
779 pMem->z = zData;
780 pMem->enc = 0;
781 pMem->type = SQLITE_BLOB;
782
783 if( key ){
784 rc = sqlite3BtreeKey(pCur, offset, amt, zData);
785 }else{
786 rc = sqlite3BtreeData(pCur, offset, amt, zData);
787 }
788 zData[amt] = 0;
789 zData[amt+1] = 0;
790 if( rc!=SQLITE_OK ){
791 if( amt>NBFS-2 ){
792 assert( zData!=pMem->zShort );
793 assert( pMem->flags & MEM_Dyn );
794 sqlite3_free(zData);
795 } else {
796 assert( zData==pMem->zShort );
797 assert( pMem->flags & MEM_Short );
798 }
799 return rc;
800 }
801 }
802
803 return SQLITE_OK;
804}
805
806#ifndef NDEBUG
807/*
808** Perform various checks on the memory cell pMem. An assert() will
809** fail if pMem is internally inconsistent.
810*/
811void sqlite3VdbeMemSanity(Mem *pMem){
812 int flags = pMem->flags;
813 assert( flags!=0 ); /* Must define some type */
814 if( flags & (MEM_Str|MEM_Blob) ){
815 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
816 assert( x!=0 ); /* Strings must define a string subtype */
817 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
818 assert( pMem->z!=0 ); /* Strings must have a value */
819 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
820 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
821 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
822 /* No destructor unless there is MEM_Dyn */
823 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
824
825 if( (flags & MEM_Str) ){
826 assert( pMem->enc==SQLITE_UTF8 ||
827 pMem->enc==SQLITE_UTF16BE ||
828 pMem->enc==SQLITE_UTF16LE
829 );
830 /* If the string is UTF-8 encoded and nul terminated, then pMem->n
831 ** must be the length of the string. (Later:) If the database file
832 ** has been corrupted, '\000' characters might have been inserted
833 ** into the middle of the string. In that case, the strlen() might
834 ** be less.
835 */
836 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
837 assert( strlen(pMem->z)<=pMem->n );
838 assert( pMem->z[pMem->n]==0 );
839 }
840 }
841 }else{
842 /* Cannot define a string subtype for non-string objects */
843 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
844 assert( pMem->xDel==0 );
845 }
846 /* MEM_Null excludes all other types */
847 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
848 || (pMem->flags&MEM_Null)==0 );
849 /* If the MEM is both real and integer, the values are equal */
850 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
851 || pMem->r==pMem->u.i );
852}
853#endif
854
855/* This function is only available internally, it is not part of the
856** external API. It works in a similar way to sqlite3_value_text(),
857** except the data returned is in the encoding specified by the second
858** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
859** SQLITE_UTF8.
860**
861** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
862** If that is the case, then the result must be aligned on an even byte
863** boundary.
864*/
865const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
866 if( !pVal ) return 0;
867
868 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
869 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
870
871 if( pVal->flags&MEM_Null ){
872 return 0;
873 }
874 assert( (MEM_Blob>>3) == MEM_Str );
875 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
876 expandBlob(pVal);
877 if( pVal->flags&MEM_Str ){
878 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
879 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
880 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
881 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
882 return 0;
883 }
884 }
885 sqlite3VdbeMemNulTerminate(pVal);
886 }else{
887 assert( (pVal->flags&MEM_Blob)==0 );
888 sqlite3VdbeMemStringify(pVal, enc);
889 assert( 0==(1&(int)pVal->z) );
890 }
891 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
892 || pVal->db->mallocFailed );
893 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
894 return pVal->z;
895 }else{
896 return 0;
897 }
898}
899
900/*
901** Create a new sqlite3_value object.
902*/
903sqlite3_value *sqlite3ValueNew(sqlite3 *db){
904 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
905 if( p ){
906 p->flags = MEM_Null;
907 p->type = SQLITE_NULL;
908 p->db = db;
909 }
910 return p;
911}
912
913/*
914** Create a new sqlite3_value object, containing the value of pExpr.
915**
916** This only works for very simple expressions that consist of one constant
917** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
918** be converted directly into a value, then the value is allocated and
919** a pointer written to *ppVal. The caller is responsible for deallocating
920** the value by passing it to sqlite3ValueFree() later on. If the expression
921** cannot be converted to a value, then *ppVal is set to NULL.
922*/
923int sqlite3ValueFromExpr(
924 sqlite3 *db, /* The database connection */
925 Expr *pExpr, /* The expression to evaluate */
926 u8 enc, /* Encoding to use */
927 u8 affinity, /* Affinity to use */
928 sqlite3_value **ppVal /* Write the new value here */
929){
930 int op;
931 char *zVal = 0;
932 sqlite3_value *pVal = 0;
933
934 if( !pExpr ){
935 *ppVal = 0;
936 return SQLITE_OK;
937 }
938 op = pExpr->op;
939
940 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
941 zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
942 pVal = sqlite3ValueNew(db);
943 if( !zVal || !pVal ) goto no_mem;
944 sqlite3Dequote(zVal);
945 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
946 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
947 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
948 }else{
949 sqlite3ValueApplyAffinity(pVal, affinity, enc);
950 }
951 }else if( op==TK_UMINUS ) {
952 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
953 pVal->u.i = -1 * pVal->u.i;
954 pVal->r = -1.0 * pVal->r;
955 }
956 }
957#ifndef SQLITE_OMIT_BLOB_LITERAL
958 else if( op==TK_BLOB ){
959 int nVal;
960 pVal = sqlite3ValueNew(db);
961 zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
962 if( !zVal || !pVal ) goto no_mem;
963 sqlite3Dequote(zVal);
964 nVal = strlen(zVal)/2;
965 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free);
966 sqlite3_free(zVal);
967 }
968#endif
969
970 *ppVal = pVal;
971 return SQLITE_OK;
972
973no_mem:
974 db->mallocFailed = 1;
975 sqlite3_free(zVal);
976 sqlite3ValueFree(pVal);
977 *ppVal = 0;
978 return SQLITE_NOMEM;
979}
980
981/*
982** Change the string value of an sqlite3_value object
983*/
984void sqlite3ValueSetStr(
985 sqlite3_value *v, /* Value to be set */
986 int n, /* Length of string z */
987 const void *z, /* Text of the new string */
988 u8 enc, /* Encoding to use */
989 void (*xDel)(void*) /* Destructor for the string */
990){
991 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
992}
993
994/*
995** Free an sqlite3_value object
996*/
997void sqlite3ValueFree(sqlite3_value *v){
998 if( !v ) return;
999 sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
1000 sqlite3_free(v);
1001}
1002
1003/*
1004** Return the number of bytes in the sqlite3_value object assuming
1005** that it uses the encoding "enc"
1006*/
1007int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1008 Mem *p = (Mem*)pVal;
1009 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1010 if( p->flags & MEM_Zero ){
1011 return p->n+p->u.i;
1012 }else{
1013 return p->n;
1014 }
1015 }
1016 return 0;
1017}
diff --git a/libraries/sqlite/win32/vtab.c b/libraries/sqlite/win32/vtab.c
new file mode 100755
index 0000000..9ca317b
--- /dev/null
+++ b/libraries/sqlite/win32/vtab.c
@@ -0,0 +1,797 @@
1/*
2** 2006 June 10
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.
10**
11*************************************************************************
12** This file contains code used to help implement virtual tables.
13**
14** $Id: vtab.c,v 1.59 2007/09/20 11:32:18 rse Exp $
15*/
16#ifndef SQLITE_OMIT_VIRTUALTABLE
17#include "sqliteInt.h"
18
19static int createModule(
20 sqlite3 *db, /* Database in which module is registered */
21 const char *zName, /* Name assigned to this module */
22 const sqlite3_module *pModule, /* The definition of the module */
23 void *pAux, /* Context pointer for xCreate/xConnect */
24 void (*xDestroy)(void *) /* Module destructor function */
25) {
26 int rc, nName;
27 Module *pMod;
28
29 sqlite3_mutex_enter(db->mutex);
30 nName = strlen(zName);
31 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
32 if( pMod ){
33 char *zCopy = (char *)(&pMod[1]);
34 memcpy(zCopy, zName, nName+1);
35 pMod->zName = zCopy;
36 pMod->pModule = pModule;
37 pMod->pAux = pAux;
38 pMod->xDestroy = xDestroy;
39 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
40 if( pMod && pMod->xDestroy ){
41 pMod->xDestroy(pMod->pAux);
42 }
43 sqlite3_free(pMod);
44 sqlite3ResetInternalSchema(db, 0);
45 }
46 rc = sqlite3ApiExit(db, SQLITE_OK);
47 sqlite3_mutex_leave(db->mutex);
48 return rc;
49}
50
51
52/*
53** External API function used to create a new virtual-table module.
54*/
55int sqlite3_create_module(
56 sqlite3 *db, /* Database in which module is registered */
57 const char *zName, /* Name assigned to this module */
58 const sqlite3_module *pModule, /* The definition of the module */
59 void *pAux /* Context pointer for xCreate/xConnect */
60){
61 return createModule(db, zName, pModule, pAux, 0);
62}
63
64/*
65** External API function used to create a new virtual-table module.
66*/
67int sqlite3_create_module_v2(
68 sqlite3 *db, /* Database in which module is registered */
69 const char *zName, /* Name assigned to this module */
70 const sqlite3_module *pModule, /* The definition of the module */
71 void *pAux, /* Context pointer for xCreate/xConnect */
72 void (*xDestroy)(void *) /* Module destructor function */
73){
74 return createModule(db, zName, pModule, pAux, xDestroy);
75}
76
77/*
78** Lock the virtual table so that it cannot be disconnected.
79** Locks nest. Every lock should have a corresponding unlock.
80** If an unlock is omitted, resources leaks will occur.
81**
82** If a disconnect is attempted while a virtual table is locked,
83** the disconnect is deferred until all locks have been removed.
84*/
85void sqlite3VtabLock(sqlite3_vtab *pVtab){
86 pVtab->nRef++;
87}
88
89/*
90** Unlock a virtual table. When the last lock is removed,
91** disconnect the virtual table.
92*/
93void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
94 pVtab->nRef--;
95 assert(db);
96 assert(!sqlite3SafetyCheck(db));
97 if( pVtab->nRef==0 ){
98 if( db->magic==SQLITE_MAGIC_BUSY ){
99 sqlite3SafetyOff(db);
100 pVtab->pModule->xDisconnect(pVtab);
101 sqlite3SafetyOn(db);
102 } else {
103 pVtab->pModule->xDisconnect(pVtab);
104 }
105 }
106}
107
108/*
109** Clear any and all virtual-table information from the Table record.
110** This routine is called, for example, just before deleting the Table
111** record.
112*/
113void sqlite3VtabClear(Table *p){
114 sqlite3_vtab *pVtab = p->pVtab;
115 if( pVtab ){
116 assert( p->pMod && p->pMod->pModule );
117 sqlite3VtabUnlock(p->pSchema->db, pVtab);
118 p->pVtab = 0;
119 }
120 if( p->azModuleArg ){
121 int i;
122 for(i=0; i<p->nModuleArg; i++){
123 sqlite3_free(p->azModuleArg[i]);
124 }
125 sqlite3_free(p->azModuleArg);
126 }
127}
128
129/*
130** Add a new module argument to pTable->azModuleArg[].
131** The string is not copied - the pointer is stored. The
132** string will be freed automatically when the table is
133** deleted.
134*/
135static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
136 int i = pTable->nModuleArg++;
137 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
138 char **azModuleArg;
139 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
140 if( azModuleArg==0 ){
141 int j;
142 for(j=0; j<i; j++){
143 sqlite3_free(pTable->azModuleArg[j]);
144 }
145 sqlite3_free(zArg);
146 sqlite3_free(pTable->azModuleArg);
147 pTable->nModuleArg = 0;
148 }else{
149 azModuleArg[i] = zArg;
150 azModuleArg[i+1] = 0;
151 }
152 pTable->azModuleArg = azModuleArg;
153}
154
155/*
156** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
157** statement. The module name has been parsed, but the optional list
158** of parameters that follow the module name are still pending.
159*/
160void sqlite3VtabBeginParse(
161 Parse *pParse, /* Parsing context */
162 Token *pName1, /* Name of new table, or database name */
163 Token *pName2, /* Name of new table or NULL */
164 Token *pModuleName /* Name of the module for the virtual table */
165){
166 int iDb; /* The database the table is being created in */
167 Table *pTable; /* The new virtual table */
168 sqlite3 *db; /* Database connection */
169
170 if( pParse->db->flags & SQLITE_SharedCache ){
171 sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
172 return;
173 }
174
175 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
176 pTable = pParse->pNewTable;
177 if( pTable==0 || pParse->nErr ) return;
178 assert( 0==pTable->pIndex );
179
180 db = pParse->db;
181 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
182 assert( iDb>=0 );
183
184 pTable->isVirtual = 1;
185 pTable->nModuleArg = 0;
186 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
187 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
188 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
189 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
190
191#ifndef SQLITE_OMIT_AUTHORIZATION
192 /* Creating a virtual table invokes the authorization callback twice.
193 ** The first invocation, to obtain permission to INSERT a row into the
194 ** sqlite_master table, has already been made by sqlite3StartTable().
195 ** The second call, to obtain permission to create the table, is made now.
196 */
197 if( pTable->azModuleArg ){
198 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
199 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
200 }
201#endif
202}
203
204/*
205** This routine takes the module argument that has been accumulating
206** in pParse->zArg[] and appends it to the list of arguments on the
207** virtual table currently under construction in pParse->pTable.
208*/
209static void addArgumentToVtab(Parse *pParse){
210 if( pParse->sArg.z && pParse->pNewTable ){
211 const char *z = (const char*)pParse->sArg.z;
212 int n = pParse->sArg.n;
213 sqlite3 *db = pParse->db;
214 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
215 }
216}
217
218/*
219** The parser calls this routine after the CREATE VIRTUAL TABLE statement
220** has been completely parsed.
221*/
222void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
223 Table *pTab; /* The table being constructed */
224 sqlite3 *db; /* The database connection */
225 char *zModule; /* The module name of the table: USING modulename */
226 Module *pMod = 0;
227
228 addArgumentToVtab(pParse);
229 pParse->sArg.z = 0;
230
231 /* Lookup the module name. */
232 pTab = pParse->pNewTable;
233 if( pTab==0 ) return;
234 db = pParse->db;
235 if( pTab->nModuleArg<1 ) return;
236 zModule = pTab->azModuleArg[0];
237 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
238 pTab->pMod = pMod;
239
240 /* If the CREATE VIRTUAL TABLE statement is being entered for the
241 ** first time (in other words if the virtual table is actually being
242 ** created now instead of just being read out of sqlite_master) then
243 ** do additional initialization work and store the statement text
244 ** in the sqlite_master table.
245 */
246 if( !db->init.busy ){
247 char *zStmt;
248 char *zWhere;
249 int iDb;
250 Vdbe *v;
251
252 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
253 if( pEnd ){
254 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
255 }
256 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
257
258 /* A slot for the record has already been allocated in the
259 ** SQLITE_MASTER table. We just need to update that slot with all
260 ** the information we've collected.
261 **
262 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
263 ** This value is always 0 and is ignored, a virtual table does not have a
264 ** rootpage. The next entry on the stack is the rowid of the record
265 ** in the sqlite_master table.
266 */
267 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
268 sqlite3NestedParse(pParse,
269 "UPDATE %Q.%s "
270 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
271 "WHERE rowid=#1",
272 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
273 pTab->zName,
274 pTab->zName,
275 zStmt
276 );
277 sqlite3_free(zStmt);
278 v = sqlite3GetVdbe(pParse);
279 sqlite3ChangeCookie(db, v, iDb);
280
281 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
282 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
283 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
284 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
285 }
286
287 /* If we are rereading the sqlite_master table create the in-memory
288 ** record of the table. If the module has already been registered,
289 ** also call the xConnect method here.
290 */
291 else {
292 Table *pOld;
293 Schema *pSchema = pTab->pSchema;
294 const char *zName = pTab->zName;
295 int nName = strlen(zName) + 1;
296 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
297 if( pOld ){
298 db->mallocFailed = 1;
299 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
300 return;
301 }
302 pSchema->db = pParse->db;
303 pParse->pNewTable = 0;
304 }
305}
306
307/*
308** The parser calls this routine when it sees the first token
309** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
310*/
311void sqlite3VtabArgInit(Parse *pParse){
312 addArgumentToVtab(pParse);
313 pParse->sArg.z = 0;
314 pParse->sArg.n = 0;
315}
316
317/*
318** The parser calls this routine for each token after the first token
319** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
320*/
321void sqlite3VtabArgExtend(Parse *pParse, Token *p){
322 Token *pArg = &pParse->sArg;
323 if( pArg->z==0 ){
324 pArg->z = p->z;
325 pArg->n = p->n;
326 }else{
327 assert(pArg->z < p->z);
328 pArg->n = (p->z + p->n - pArg->z);
329 }
330}
331
332/*
333** Invoke a virtual table constructor (either xCreate or xConnect). The
334** pointer to the function to invoke is passed as the fourth parameter
335** to this procedure.
336*/
337static int vtabCallConstructor(
338 sqlite3 *db,
339 Table *pTab,
340 Module *pMod,
341 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
342 char **pzErr
343){
344 int rc;
345 int rc2;
346 sqlite3_vtab *pVtab = 0;
347 const char *const*azArg = (const char *const*)pTab->azModuleArg;
348 int nArg = pTab->nModuleArg;
349 char *zErr = 0;
350 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
351
352 if( !zModuleName ){
353 return SQLITE_NOMEM;
354 }
355
356 assert( !db->pVTab );
357 assert( xConstruct );
358
359 db->pVTab = pTab;
360 rc = sqlite3SafetyOff(db);
361 assert( rc==SQLITE_OK );
362 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
363 rc2 = sqlite3SafetyOn(db);
364 if( rc==SQLITE_OK && pVtab ){
365 pVtab->pModule = pMod->pModule;
366 pVtab->nRef = 1;
367 pTab->pVtab = pVtab;
368 }
369
370 if( SQLITE_OK!=rc ){
371 if( zErr==0 ){
372 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
373 }else {
374 *pzErr = sqlite3MPrintf(db, "%s", zErr);
375 sqlite3_free(zErr);
376 }
377 }else if( db->pVTab ){
378 const char *zFormat = "vtable constructor did not declare schema: %s";
379 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
380 rc = SQLITE_ERROR;
381 }
382 if( rc==SQLITE_OK ){
383 rc = rc2;
384 }
385 db->pVTab = 0;
386 sqlite3_free(zModuleName);
387
388 /* If everything went according to plan, loop through the columns
389 ** of the table to see if any of them contain the token "hidden".
390 ** If so, set the Column.isHidden flag and remove the token from
391 ** the type string.
392 */
393 if( rc==SQLITE_OK ){
394 int iCol;
395 for(iCol=0; iCol<pTab->nCol; iCol++){
396 char *zType = pTab->aCol[iCol].zType;
397 int nType;
398 int i = 0;
399 if( !zType ) continue;
400 nType = strlen(zType);
401 if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
402 for(i=0; i<nType; i++){
403 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
404 && (zType[i+7]=='\0' || zType[i+7]==' ')
405 ){
406 i++;
407 break;
408 }
409 }
410 }
411 if( i<nType ){
412 int j;
413 int nDel = 6 + (zType[i+6] ? 1 : 0);
414 for(j=i; (j+nDel)<=nType; j++){
415 zType[j] = zType[j+nDel];
416 }
417 if( zType[i]=='\0' && i>0 ){
418 assert(zType[i-1]==' ');
419 zType[i-1] = '\0';
420 }
421 pTab->aCol[iCol].isHidden = 1;
422 }
423 }
424 }
425 return rc;
426}
427
428/*
429** This function is invoked by the parser to call the xConnect() method
430** of the virtual table pTab. If an error occurs, an error code is returned
431** and an error left in pParse.
432**
433** This call is a no-op if table pTab is not a virtual table.
434*/
435int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
436 Module *pMod;
437 int rc = SQLITE_OK;
438
439 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
440 return SQLITE_OK;
441 }
442
443 pMod = pTab->pMod;
444 if( !pMod ){
445 const char *zModule = pTab->azModuleArg[0];
446 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
447 rc = SQLITE_ERROR;
448 } else {
449 char *zErr = 0;
450 sqlite3 *db = pParse->db;
451 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
452 if( rc!=SQLITE_OK ){
453 sqlite3ErrorMsg(pParse, "%s", zErr);
454 }
455 sqlite3_free(zErr);
456 }
457
458 return rc;
459}
460
461/*
462** Add the virtual table pVtab to the array sqlite3.aVTrans[].
463*/
464static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
465 const int ARRAY_INCR = 5;
466
467 /* Grow the sqlite3.aVTrans array if required */
468 if( (db->nVTrans%ARRAY_INCR)==0 ){
469 sqlite3_vtab **aVTrans;
470 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
471 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
472 if( !aVTrans ){
473 return SQLITE_NOMEM;
474 }
475 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
476 db->aVTrans = aVTrans;
477 }
478
479 /* Add pVtab to the end of sqlite3.aVTrans */
480 db->aVTrans[db->nVTrans++] = pVtab;
481 sqlite3VtabLock(pVtab);
482 return SQLITE_OK;
483}
484
485/*
486** This function is invoked by the vdbe to call the xCreate method
487** of the virtual table named zTab in database iDb.
488**
489** If an error occurs, *pzErr is set to point an an English language
490** description of the error and an SQLITE_XXX error code is returned.
491** In this case the caller must call sqlite3_free() on *pzErr.
492*/
493int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
494 int rc = SQLITE_OK;
495 Table *pTab;
496 Module *pMod;
497 const char *zModule;
498
499 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
500 assert(pTab && pTab->isVirtual && !pTab->pVtab);
501 pMod = pTab->pMod;
502 zModule = pTab->azModuleArg[0];
503
504 /* If the module has been registered and includes a Create method,
505 ** invoke it now. If the module has not been registered, return an
506 ** error. Otherwise, do nothing.
507 */
508 if( !pMod ){
509 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
510 rc = SQLITE_ERROR;
511 }else{
512 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
513 }
514
515 if( rc==SQLITE_OK && pTab->pVtab ){
516 rc = addToVTrans(db, pTab->pVtab);
517 }
518
519 return rc;
520}
521
522/*
523** This function is used to set the schema of a virtual table. It is only
524** valid to call this function from within the xCreate() or xConnect() of a
525** virtual table module.
526*/
527int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
528 Parse sParse;
529
530 int rc = SQLITE_OK;
531 Table *pTab;
532 char *zErr = 0;
533
534 sqlite3_mutex_enter(db->mutex);
535 pTab = db->pVTab;
536 if( !pTab ){
537 sqlite3Error(db, SQLITE_MISUSE, 0);
538 sqlite3_mutex_leave(db->mutex);
539 return SQLITE_MISUSE;
540 }
541 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
542
543 memset(&sParse, 0, sizeof(Parse));
544 sParse.declareVtab = 1;
545 sParse.db = db;
546
547 if(
548 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
549 sParse.pNewTable &&
550 !sParse.pNewTable->pSelect &&
551 !sParse.pNewTable->isVirtual
552 ){
553 pTab->aCol = sParse.pNewTable->aCol;
554 pTab->nCol = sParse.pNewTable->nCol;
555 sParse.pNewTable->nCol = 0;
556 sParse.pNewTable->aCol = 0;
557 db->pVTab = 0;
558 } else {
559 sqlite3Error(db, SQLITE_ERROR, zErr);
560 sqlite3_free(zErr);
561 rc = SQLITE_ERROR;
562 }
563 sParse.declareVtab = 0;
564
565 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
566 sqlite3DeleteTable(sParse.pNewTable);
567 sParse.pNewTable = 0;
568
569 assert( (rc&0xff)==rc );
570 rc = sqlite3ApiExit(db, rc);
571 sqlite3_mutex_leave(db->mutex);
572 return rc;
573}
574
575/*
576** This function is invoked by the vdbe to call the xDestroy method
577** of the virtual table named zTab in database iDb. This occurs
578** when a DROP TABLE is mentioned.
579**
580** This call is a no-op if zTab is not a virtual table.
581*/
582int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
583{
584 int rc = SQLITE_OK;
585 Table *pTab;
586
587 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
588 assert(pTab);
589 if( pTab->pVtab ){
590 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
591 rc = sqlite3SafetyOff(db);
592 assert( rc==SQLITE_OK );
593 if( xDestroy ){
594 rc = xDestroy(pTab->pVtab);
595 }
596 sqlite3SafetyOn(db);
597 if( rc==SQLITE_OK ){
598 pTab->pVtab = 0;
599 }
600 }
601
602 return rc;
603}
604
605/*
606** This function invokes either the xRollback or xCommit method
607** of each of the virtual tables in the sqlite3.aVTrans array. The method
608** called is identified by the second argument, "offset", which is
609** the offset of the method to call in the sqlite3_module structure.
610**
611** The array is cleared after invoking the callbacks.
612*/
613static void callFinaliser(sqlite3 *db, int offset){
614 int i;
615 if( db->aVTrans ){
616 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
617 sqlite3_vtab *pVtab = db->aVTrans[i];
618 int (*x)(sqlite3_vtab *);
619 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
620 if( x ) x(pVtab);
621 sqlite3VtabUnlock(db, pVtab);
622 }
623 sqlite3_free(db->aVTrans);
624 db->nVTrans = 0;
625 db->aVTrans = 0;
626 }
627}
628
629/*
630** If argument rc2 is not SQLITE_OK, then return it and do nothing.
631** Otherwise, invoke the xSync method of all virtual tables in the
632** sqlite3.aVTrans array. Return the error code for the first error
633** that occurs, or SQLITE_OK if all xSync operations are successful.
634*/
635int sqlite3VtabSync(sqlite3 *db, int rc2){
636 int i;
637 int rc = SQLITE_OK;
638 int rcsafety;
639 sqlite3_vtab **aVTrans = db->aVTrans;
640 if( rc2!=SQLITE_OK ) return rc2;
641
642 rc = sqlite3SafetyOff(db);
643 db->aVTrans = 0;
644 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
645 sqlite3_vtab *pVtab = aVTrans[i];
646 int (*x)(sqlite3_vtab *);
647 x = pVtab->pModule->xSync;
648 if( x ){
649 rc = x(pVtab);
650 }
651 }
652 db->aVTrans = aVTrans;
653 rcsafety = sqlite3SafetyOn(db);
654
655 if( rc==SQLITE_OK ){
656 rc = rcsafety;
657 }
658 return rc;
659}
660
661/*
662** Invoke the xRollback method of all virtual tables in the
663** sqlite3.aVTrans array. Then clear the array itself.
664*/
665int sqlite3VtabRollback(sqlite3 *db){
666 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
667 return SQLITE_OK;
668}
669
670/*
671** Invoke the xCommit method of all virtual tables in the
672** sqlite3.aVTrans array. Then clear the array itself.
673*/
674int sqlite3VtabCommit(sqlite3 *db){
675 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
676 return SQLITE_OK;
677}
678
679/*
680** If the virtual table pVtab supports the transaction interface
681** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
682** not currently open, invoke the xBegin method now.
683**
684** If the xBegin call is successful, place the sqlite3_vtab pointer
685** in the sqlite3.aVTrans array.
686*/
687int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
688 int rc = SQLITE_OK;
689 const sqlite3_module *pModule;
690
691 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
692 ** than zero, then this function is being called from within a
693 ** virtual module xSync() callback. It is illegal to write to
694 ** virtual module tables in this case, so return SQLITE_LOCKED.
695 */
696 if( 0==db->aVTrans && db->nVTrans>0 ){
697 return SQLITE_LOCKED;
698 }
699 if( !pVtab ){
700 return SQLITE_OK;
701 }
702 pModule = pVtab->pModule;
703
704 if( pModule->xBegin ){
705 int i;
706
707
708 /* If pVtab is already in the aVTrans array, return early */
709 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
710 if( db->aVTrans[i]==pVtab ){
711 return SQLITE_OK;
712 }
713 }
714
715 /* Invoke the xBegin method */
716 rc = pModule->xBegin(pVtab);
717 if( rc!=SQLITE_OK ){
718 return rc;
719 }
720
721 rc = addToVTrans(db, pVtab);
722 }
723 return rc;
724}
725
726/*
727** The first parameter (pDef) is a function implementation. The
728** second parameter (pExpr) is the first argument to this function.
729** If pExpr is a column in a virtual table, then let the virtual
730** table implementation have an opportunity to overload the function.
731**
732** This routine is used to allow virtual table implementations to
733** overload MATCH, LIKE, GLOB, and REGEXP operators.
734**
735** Return either the pDef argument (indicating no change) or a
736** new FuncDef structure that is marked as ephemeral using the
737** SQLITE_FUNC_EPHEM flag.
738*/
739FuncDef *sqlite3VtabOverloadFunction(
740 sqlite3 *db, /* Database connection for reporting malloc problems */
741 FuncDef *pDef, /* Function to possibly overload */
742 int nArg, /* Number of arguments to the function */
743 Expr *pExpr /* First argument to the function */
744){
745 Table *pTab;
746 sqlite3_vtab *pVtab;
747 sqlite3_module *pMod;
748 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
749 void *pArg;
750 FuncDef *pNew;
751 int rc = 0;
752 char *zLowerName;
753 unsigned char *z;
754
755
756 /* Check to see the left operand is a column in a virtual table */
757 if( pExpr==0 ) return pDef;
758 if( pExpr->op!=TK_COLUMN ) return pDef;
759 pTab = pExpr->pTab;
760 if( pTab==0 ) return pDef;
761 if( !pTab->isVirtual ) return pDef;
762 pVtab = pTab->pVtab;
763 assert( pVtab!=0 );
764 assert( pVtab->pModule!=0 );
765 pMod = (sqlite3_module *)pVtab->pModule;
766 if( pMod->xFindFunction==0 ) return pDef;
767
768 /* Call the xFindFunction method on the virtual table implementation
769 ** to see if the implementation wants to overload this function
770 */
771 zLowerName = sqlite3DbStrDup(db, pDef->zName);
772 if( zLowerName ){
773 for(z=(unsigned char*)zLowerName; *z; z++){
774 *z = sqlite3UpperToLower[*z];
775 }
776 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
777 sqlite3_free(zLowerName);
778 }
779 if( rc==0 ){
780 return pDef;
781 }
782
783 /* Create a new ephemeral function definition for the overloaded
784 ** function */
785 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
786 if( pNew==0 ){
787 return pDef;
788 }
789 *pNew = *pDef;
790 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
791 pNew->xFunc = xFunc;
792 pNew->pUserData = pArg;
793 pNew->flags |= SQLITE_FUNC_EPHEM;
794 return pNew;
795}
796
797#endif /* SQLITE_OMIT_VIRTUALTABLE */
diff --git a/libraries/sqlite/win32/where.c b/libraries/sqlite/win32/where.c
new file mode 100755
index 0000000..0deea14
--- /dev/null
+++ b/libraries/sqlite/win32/where.c
@@ -0,0 +1,2773 @@
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.
10**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
13** the WHERE clause of SQL statements. This module is reponsible for
14** generating the code that loops through a table looking for applicable
15** rows. Indices are selected and used to speed the search when doing
16** so is applicable. Because this module is responsible for selecting
17** indices, you might also think of this module as the "query optimizer".
18**
19** $Id: where.c,v 1.261 2007/09/13 17:54:40 drh Exp $
20*/
21#include "sqliteInt.h"
22
23/*
24** The number of bits in a Bitmask. "BMS" means "BitMask Size".
25*/
26#define BMS (sizeof(Bitmask)*8)
27
28/*
29** Trace output macros
30*/
31#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
32int sqlite3_where_trace = 0;
33# define WHERETRACE(X) if(sqlite3_where_trace) sqlite3DebugPrintf X
34#else
35# define WHERETRACE(X)
36#endif
37
38/* Forward reference
39*/
40typedef struct WhereClause WhereClause;
41typedef struct ExprMaskSet ExprMaskSet;
42
43/*
44** The query generator uses an array of instances of this structure to
45** help it analyze the subexpressions of the WHERE clause. Each WHERE
46** clause subexpression is separated from the others by an AND operator.
47**
48** All WhereTerms are collected into a single WhereClause structure.
49** The following identity holds:
50**
51** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
52**
53** When a term is of the form:
54**
55** X <op> <expr>
56**
57** where X is a column name and <op> is one of certain operators,
58** then WhereTerm.leftCursor and WhereTerm.leftColumn record the
59** cursor number and column number for X. WhereTerm.operator records
60** the <op> using a bitmask encoding defined by WO_xxx below. The
61** use of a bitmask encoding for the operator allows us to search
62** quickly for terms that match any of several different operators.
63**
64** prereqRight and prereqAll record sets of cursor numbers,
65** but they do so indirectly. A single ExprMaskSet structure translates
66** cursor number into bits and the translated bit is stored in the prereq
67** fields. The translation is used in order to maximize the number of
68** bits that will fit in a Bitmask. The VDBE cursor numbers might be
69** spread out over the non-negative integers. For example, the cursor
70** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The ExprMaskSet
71** translates these sparse cursor numbers into consecutive integers
72** beginning with 0 in order to make the best possible use of the available
73** bits in the Bitmask. So, in the example above, the cursor numbers
74** would be mapped into integers 0 through 7.
75*/
76typedef struct WhereTerm WhereTerm;
77struct WhereTerm {
78 Expr *pExpr; /* Pointer to the subexpression */
79 i16 iParent; /* Disable pWC->a[iParent] when this term disabled */
80 i16 leftCursor; /* Cursor number of X in "X <op> <expr>" */
81 i16 leftColumn; /* Column number of X in "X <op> <expr>" */
82 u16 eOperator; /* A WO_xx value describing <op> */
83 u8 flags; /* Bit flags. See below */
84 u8 nChild; /* Number of children that must disable us */
85 WhereClause *pWC; /* The clause this term is part of */
86 Bitmask prereqRight; /* Bitmask of tables used by pRight */
87 Bitmask prereqAll; /* Bitmask of tables referenced by p */
88};
89
90/*
91** Allowed values of WhereTerm.flags
92*/
93#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(pExpr) */
94#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
95#define TERM_CODED 0x04 /* This term is already coded */
96#define TERM_COPIED 0x08 /* Has a child */
97#define TERM_OR_OK 0x10 /* Used during OR-clause processing */
98
99/*
100** An instance of the following structure holds all information about a
101** WHERE clause. Mostly this is a container for one or more WhereTerms.
102*/
103struct WhereClause {
104 Parse *pParse; /* The parser context */
105 ExprMaskSet *pMaskSet; /* Mapping of table indices to bitmasks */
106 int nTerm; /* Number of terms */
107 int nSlot; /* Number of entries in a[] */
108 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
109 WhereTerm aStatic[10]; /* Initial static space for a[] */
110};
111
112/*
113** An instance of the following structure keeps track of a mapping
114** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
115**
116** The VDBE cursor numbers are small integers contained in
117** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
118** clause, the cursor numbers might not begin with 0 and they might
119** contain gaps in the numbering sequence. But we want to make maximum
120** use of the bits in our bitmasks. This structure provides a mapping
121** from the sparse cursor numbers into consecutive integers beginning
122** with 0.
123**
124** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
125** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
126**
127** For example, if the WHERE clause expression used these VDBE
128** cursors: 4, 5, 8, 29, 57, 73. Then the ExprMaskSet structure
129** would map those cursor numbers into bits 0 through 5.
130**
131** Note that the mapping is not necessarily ordered. In the example
132** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
133** 57->5, 73->4. Or one of 719 other combinations might be used. It
134** does not really matter. What is important is that sparse cursor
135** numbers all get mapped into bit numbers that begin with 0 and contain
136** no gaps.
137*/
138struct ExprMaskSet {
139 int n; /* Number of assigned cursor values */
140 int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */
141};
142
143
144/*
145** Bitmasks for the operators that indices are able to exploit. An
146** OR-ed combination of these values can be used when searching for
147** terms in the where clause.
148*/
149#define WO_IN 1
150#define WO_EQ 2
151#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
152#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
153#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
154#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
155#define WO_MATCH 64
156#define WO_ISNULL 128
157
158/*
159** Value for flags returned by bestIndex().
160**
161** The least significant byte is reserved as a mask for WO_ values above.
162** The WhereLevel.flags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
163** But if the table is the right table of a left join, WhereLevel.flags
164** is set to WO_IN|WO_EQ. The WhereLevel.flags field can then be used as
165** the "op" parameter to findTerm when we are resolving equality constraints.
166** ISNULL constraints will then not be used on the right table of a left
167** join. Tickets #2177 and #2189.
168*/
169#define WHERE_ROWID_EQ 0x000100 /* rowid=EXPR or rowid IN (...) */
170#define WHERE_ROWID_RANGE 0x000200 /* rowid<EXPR and/or rowid>EXPR */
171#define WHERE_COLUMN_EQ 0x001000 /* x=EXPR or x IN (...) */
172#define WHERE_COLUMN_RANGE 0x002000 /* x<EXPR and/or x>EXPR */
173#define WHERE_COLUMN_IN 0x004000 /* x IN (...) */
174#define WHERE_TOP_LIMIT 0x010000 /* x<EXPR or x<=EXPR constraint */
175#define WHERE_BTM_LIMIT 0x020000 /* x>EXPR or x>=EXPR constraint */
176#define WHERE_IDX_ONLY 0x080000 /* Use index only - omit table */
177#define WHERE_ORDERBY 0x100000 /* Output will appear in correct order */
178#define WHERE_REVERSE 0x200000 /* Scan in reverse order */
179#define WHERE_UNIQUE 0x400000 /* Selects no more than one row */
180#define WHERE_VIRTUALTABLE 0x800000 /* Use virtual-table processing */
181
182/*
183** Initialize a preallocated WhereClause structure.
184*/
185static void whereClauseInit(
186 WhereClause *pWC, /* The WhereClause to be initialized */
187 Parse *pParse, /* The parsing context */
188 ExprMaskSet *pMaskSet /* Mapping from table indices to bitmasks */
189){
190 pWC->pParse = pParse;
191 pWC->pMaskSet = pMaskSet;
192 pWC->nTerm = 0;
193 pWC->nSlot = ArraySize(pWC->aStatic);
194 pWC->a = pWC->aStatic;
195}
196
197/*
198** Deallocate a WhereClause structure. The WhereClause structure
199** itself is not freed. This routine is the inverse of whereClauseInit().
200*/
201static void whereClauseClear(WhereClause *pWC){
202 int i;
203 WhereTerm *a;
204 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
205 if( a->flags & TERM_DYNAMIC ){
206 sqlite3ExprDelete(a->pExpr);
207 }
208 }
209 if( pWC->a!=pWC->aStatic ){
210 sqlite3_free(pWC->a);
211 }
212}
213
214/*
215** Add a new entries to the WhereClause structure. Increase the allocated
216** space as necessary.
217**
218** If the flags argument includes TERM_DYNAMIC, then responsibility
219** for freeing the expression p is assumed by the WhereClause object.
220**
221** WARNING: This routine might reallocate the space used to store
222** WhereTerms. All pointers to WhereTerms should be invalided after
223** calling this routine. Such pointers may be reinitialized by referencing
224** the pWC->a[] array.
225*/
226static int whereClauseInsert(WhereClause *pWC, Expr *p, int flags){
227 WhereTerm *pTerm;
228 int idx;
229 if( pWC->nTerm>=pWC->nSlot ){
230 WhereTerm *pOld = pWC->a;
231 pWC->a = sqlite3_malloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
232 if( pWC->a==0 ){
233 pWC->pParse->db->mallocFailed = 1;
234 if( flags & TERM_DYNAMIC ){
235 sqlite3ExprDelete(p);
236 }
237 return 0;
238 }
239 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
240 if( pOld!=pWC->aStatic ){
241 sqlite3_free(pOld);
242 }
243 pWC->nSlot *= 2;
244 }
245 pTerm = &pWC->a[idx = pWC->nTerm];
246 pWC->nTerm++;
247 pTerm->pExpr = p;
248 pTerm->flags = flags;
249 pTerm->pWC = pWC;
250 pTerm->iParent = -1;
251 return idx;
252}
253
254/*
255** This routine identifies subexpressions in the WHERE clause where
256** each subexpression is separated by the AND operator or some other
257** operator specified in the op parameter. The WhereClause structure
258** is filled with pointers to subexpressions. For example:
259**
260** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
261** \________/ \_______________/ \________________/
262** slot[0] slot[1] slot[2]
263**
264** The original WHERE clause in pExpr is unaltered. All this routine
265** does is make slot[] entries point to substructure within pExpr.
266**
267** In the previous sentence and in the diagram, "slot[]" refers to
268** the WhereClause.a[] array. This array grows as needed to contain
269** all terms of the WHERE clause.
270*/
271static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
272 if( pExpr==0 ) return;
273 if( pExpr->op!=op ){
274 whereClauseInsert(pWC, pExpr, 0);
275 }else{
276 whereSplit(pWC, pExpr->pLeft, op);
277 whereSplit(pWC, pExpr->pRight, op);
278 }
279}
280
281/*
282** Initialize an expression mask set
283*/
284#define initMaskSet(P) memset(P, 0, sizeof(*P))
285
286/*
287** Return the bitmask for the given cursor number. Return 0 if
288** iCursor is not in the set.
289*/
290static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){
291 int i;
292 for(i=0; i<pMaskSet->n; i++){
293 if( pMaskSet->ix[i]==iCursor ){
294 return ((Bitmask)1)<<i;
295 }
296 }
297 return 0;
298}
299
300/*
301** Create a new mask for cursor iCursor.
302**
303** There is one cursor per table in the FROM clause. The number of
304** tables in the FROM clause is limited by a test early in the
305** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
306** array will never overflow.
307*/
308static void createMask(ExprMaskSet *pMaskSet, int iCursor){
309 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
310 pMaskSet->ix[pMaskSet->n++] = iCursor;
311}
312
313/*
314** This routine walks (recursively) an expression tree and generates
315** a bitmask indicating which tables are used in that expression
316** tree.
317**
318** In order for this routine to work, the calling function must have
319** previously invoked sqlite3ExprResolveNames() on the expression. See
320** the header comment on that routine for additional information.
321** The sqlite3ExprResolveNames() routines looks for column names and
322** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
323** the VDBE cursor number of the table. This routine just has to
324** translate the cursor numbers into bitmask values and OR all
325** the bitmasks together.
326*/
327static Bitmask exprListTableUsage(ExprMaskSet*, ExprList*);
328static Bitmask exprSelectTableUsage(ExprMaskSet*, Select*);
329static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
330 Bitmask mask = 0;
331 if( p==0 ) return 0;
332 if( p->op==TK_COLUMN ){
333 mask = getMask(pMaskSet, p->iTable);
334 return mask;
335 }
336 mask = exprTableUsage(pMaskSet, p->pRight);
337 mask |= exprTableUsage(pMaskSet, p->pLeft);
338 mask |= exprListTableUsage(pMaskSet, p->pList);
339 mask |= exprSelectTableUsage(pMaskSet, p->pSelect);
340 return mask;
341}
342static Bitmask exprListTableUsage(ExprMaskSet *pMaskSet, ExprList *pList){
343 int i;
344 Bitmask mask = 0;
345 if( pList ){
346 for(i=0; i<pList->nExpr; i++){
347 mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
348 }
349 }
350 return mask;
351}
352static Bitmask exprSelectTableUsage(ExprMaskSet *pMaskSet, Select *pS){
353 Bitmask mask = 0;
354 while( pS ){
355 mask |= exprListTableUsage(pMaskSet, pS->pEList);
356 mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
357 mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
358 mask |= exprTableUsage(pMaskSet, pS->pWhere);
359 mask |= exprTableUsage(pMaskSet, pS->pHaving);
360 pS = pS->pPrior;
361 }
362 return mask;
363}
364
365/*
366** Return TRUE if the given operator is one of the operators that is
367** allowed for an indexable WHERE clause term. The allowed operators are
368** "=", "<", ">", "<=", ">=", and "IN".
369*/
370static int allowedOp(int op){
371 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
372 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
373 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
374 assert( TK_GE==TK_EQ+4 );
375 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
376}
377
378/*
379** Swap two objects of type T.
380*/
381#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
382
383/*
384** Commute a comparision operator. Expressions of the form "X op Y"
385** are converted into "Y op X".
386**
387** If a collation sequence is associated with either the left or right
388** side of the comparison, it remains associated with the same side after
389** the commutation. So "Y collate NOCASE op X" becomes
390** "X collate NOCASE op Y". This is because any collation sequence on
391** the left hand side of a comparison overrides any collation sequence
392** attached to the right. For the same reason the EP_ExpCollate flag
393** is not commuted.
394*/
395static void exprCommute(Expr *pExpr){
396 u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
397 u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
398 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
399 SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
400 pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
401 pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
402 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
403 if( pExpr->op>=TK_GT ){
404 assert( TK_LT==TK_GT+2 );
405 assert( TK_GE==TK_LE+2 );
406 assert( TK_GT>TK_EQ );
407 assert( TK_GT<TK_LE );
408 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
409 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
410 }
411}
412
413/*
414** Translate from TK_xx operator to WO_xx bitmask.
415*/
416static int operatorMask(int op){
417 int c;
418 assert( allowedOp(op) );
419 if( op==TK_IN ){
420 c = WO_IN;
421 }else if( op==TK_ISNULL ){
422 c = WO_ISNULL;
423 }else{
424 c = WO_EQ<<(op-TK_EQ);
425 }
426 assert( op!=TK_ISNULL || c==WO_ISNULL );
427 assert( op!=TK_IN || c==WO_IN );
428 assert( op!=TK_EQ || c==WO_EQ );
429 assert( op!=TK_LT || c==WO_LT );
430 assert( op!=TK_LE || c==WO_LE );
431 assert( op!=TK_GT || c==WO_GT );
432 assert( op!=TK_GE || c==WO_GE );
433 return c;
434}
435
436/*
437** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
438** where X is a reference to the iColumn of table iCur and <op> is one of
439** the WO_xx operator codes specified by the op parameter.
440** Return a pointer to the term. Return 0 if not found.
441*/
442static WhereTerm *findTerm(
443 WhereClause *pWC, /* The WHERE clause to be searched */
444 int iCur, /* Cursor number of LHS */
445 int iColumn, /* Column number of LHS */
446 Bitmask notReady, /* RHS must not overlap with this mask */
447 u16 op, /* Mask of WO_xx values describing operator */
448 Index *pIdx /* Must be compatible with this index, if not NULL */
449){
450 WhereTerm *pTerm;
451 int k;
452 for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
453 if( pTerm->leftCursor==iCur
454 && (pTerm->prereqRight & notReady)==0
455 && pTerm->leftColumn==iColumn
456 && (pTerm->eOperator & op)!=0
457 ){
458 if( iCur>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
459 Expr *pX = pTerm->pExpr;
460 CollSeq *pColl;
461 char idxaff;
462 int j;
463 Parse *pParse = pWC->pParse;
464
465 idxaff = pIdx->pTable->aCol[iColumn].affinity;
466 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
467
468 /* Figure out the collation sequence required from an index for
469 ** it to be useful for optimising expression pX. Store this
470 ** value in variable pColl.
471 */
472 assert(pX->pLeft);
473 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
474 if( !pColl ){
475 pColl = pParse->db->pDfltColl;
476 }
477
478 for(j=0; j<pIdx->nColumn && pIdx->aiColumn[j]!=iColumn; j++){}
479 assert( j<pIdx->nColumn );
480 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
481 }
482 return pTerm;
483 }
484 }
485 return 0;
486}
487
488/* Forward reference */
489static void exprAnalyze(SrcList*, WhereClause*, int);
490
491/*
492** Call exprAnalyze on all terms in a WHERE clause.
493**
494**
495*/
496static void exprAnalyzeAll(
497 SrcList *pTabList, /* the FROM clause */
498 WhereClause *pWC /* the WHERE clause to be analyzed */
499){
500 int i;
501 for(i=pWC->nTerm-1; i>=0; i--){
502 exprAnalyze(pTabList, pWC, i);
503 }
504}
505
506#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
507/*
508** Check to see if the given expression is a LIKE or GLOB operator that
509** can be optimized using inequality constraints. Return TRUE if it is
510** so and false if not.
511**
512** In order for the operator to be optimizible, the RHS must be a string
513** literal that does not begin with a wildcard.
514*/
515static int isLikeOrGlob(
516 sqlite3 *db, /* The database */
517 Expr *pExpr, /* Test this expression */
518 int *pnPattern, /* Number of non-wildcard prefix characters */
519 int *pisComplete /* True if the only wildcard is % in the last character */
520){
521 const char *z;
522 Expr *pRight, *pLeft;
523 ExprList *pList;
524 int c, cnt;
525 int noCase;
526 char wc[3];
527 CollSeq *pColl;
528
529 if( !sqlite3IsLikeFunction(db, pExpr, &noCase, wc) ){
530 return 0;
531 }
532 pList = pExpr->pList;
533 pRight = pList->a[0].pExpr;
534 if( pRight->op!=TK_STRING ){
535 return 0;
536 }
537 pLeft = pList->a[1].pExpr;
538 if( pLeft->op!=TK_COLUMN ){
539 return 0;
540 }
541 pColl = pLeft->pColl;
542 if( pColl==0 ){
543 /* TODO: Coverage testing doesn't get this case. Is it actually possible
544 ** for an expression of type TK_COLUMN to not have an assigned collation
545 ** sequence at this point?
546 */
547 pColl = db->pDfltColl;
548 }
549 if( (pColl->type!=SQLITE_COLL_BINARY || noCase) &&
550 (pColl->type!=SQLITE_COLL_NOCASE || !noCase) ){
551 return 0;
552 }
553 sqlite3DequoteExpr(db, pRight);
554 z = (char *)pRight->token.z;
555 for(cnt=0; (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2]; cnt++){}
556 if( cnt==0 || 255==(u8)z[cnt] ){
557 return 0;
558 }
559 *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
560 *pnPattern = cnt;
561 return 1;
562}
563#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
564
565
566#ifndef SQLITE_OMIT_VIRTUALTABLE
567/*
568** Check to see if the given expression is of the form
569**
570** column MATCH expr
571**
572** If it is then return TRUE. If not, return FALSE.
573*/
574static int isMatchOfColumn(
575 Expr *pExpr /* Test this expression */
576){
577 ExprList *pList;
578
579 if( pExpr->op!=TK_FUNCTION ){
580 return 0;
581 }
582 if( pExpr->token.n!=5 ||
583 sqlite3StrNICmp((const char*)pExpr->token.z,"match",5)!=0 ){
584 return 0;
585 }
586 pList = pExpr->pList;
587 if( pList->nExpr!=2 ){
588 return 0;
589 }
590 if( pList->a[1].pExpr->op != TK_COLUMN ){
591 return 0;
592 }
593 return 1;
594}
595#endif /* SQLITE_OMIT_VIRTUALTABLE */
596
597/*
598** If the pBase expression originated in the ON or USING clause of
599** a join, then transfer the appropriate markings over to derived.
600*/
601static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
602 pDerived->flags |= pBase->flags & EP_FromJoin;
603 pDerived->iRightJoinTable = pBase->iRightJoinTable;
604}
605
606#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
607/*
608** Return TRUE if the given term of an OR clause can be converted
609** into an IN clause. The iCursor and iColumn define the left-hand
610** side of the IN clause.
611**
612** The context is that we have multiple OR-connected equality terms
613** like this:
614**
615** a=<expr1> OR a=<expr2> OR b=<expr3> OR ...
616**
617** The pOrTerm input to this routine corresponds to a single term of
618** this OR clause. In order for the term to be a condidate for
619** conversion to an IN operator, the following must be true:
620**
621** * The left-hand side of the term must be the column which
622** is identified by iCursor and iColumn.
623**
624** * If the right-hand side is also a column, then the affinities
625** of both right and left sides must be such that no type
626** conversions are required on the right. (Ticket #2249)
627**
628** If both of these conditions are true, then return true. Otherwise
629** return false.
630*/
631static int orTermIsOptCandidate(WhereTerm *pOrTerm, int iCursor, int iColumn){
632 int affLeft, affRight;
633 assert( pOrTerm->eOperator==WO_EQ );
634 if( pOrTerm->leftCursor!=iCursor ){
635 return 0;
636 }
637 if( pOrTerm->leftColumn!=iColumn ){
638 return 0;
639 }
640 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
641 if( affRight==0 ){
642 return 1;
643 }
644 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
645 if( affRight!=affLeft ){
646 return 0;
647 }
648 return 1;
649}
650
651/*
652** Return true if the given term of an OR clause can be ignored during
653** a check to make sure all OR terms are candidates for optimization.
654** In other words, return true if a call to the orTermIsOptCandidate()
655** above returned false but it is not necessary to disqualify the
656** optimization.
657**
658** Suppose the original OR phrase was this:
659**
660** a=4 OR a=11 OR a=b
661**
662** During analysis, the third term gets flipped around and duplicate
663** so that we are left with this:
664**
665** a=4 OR a=11 OR a=b OR b=a
666**
667** Since the last two terms are duplicates, only one of them
668** has to qualify in order for the whole phrase to qualify. When
669** this routine is called, we know that pOrTerm did not qualify.
670** This routine merely checks to see if pOrTerm has a duplicate that
671** might qualify. If there is a duplicate that has not yet been
672** disqualified, then return true. If there are no duplicates, or
673** the duplicate has also been disqualifed, return false.
674*/
675static int orTermHasOkDuplicate(WhereClause *pOr, WhereTerm *pOrTerm){
676 if( pOrTerm->flags & TERM_COPIED ){
677 /* This is the original term. The duplicate is to the left had
678 ** has not yet been analyzed and thus has not yet been disqualified. */
679 return 1;
680 }
681 if( (pOrTerm->flags & TERM_VIRTUAL)!=0
682 && (pOr->a[pOrTerm->iParent].flags & TERM_OR_OK)!=0 ){
683 /* This is a duplicate term. The original qualified so this one
684 ** does not have to. */
685 return 1;
686 }
687 /* This is either a singleton term or else it is a duplicate for
688 ** which the original did not qualify. Either way we are done for. */
689 return 0;
690}
691#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
692
693/*
694** The input to this routine is an WhereTerm structure with only the
695** "pExpr" field filled in. The job of this routine is to analyze the
696** subexpression and populate all the other fields of the WhereTerm
697** structure.
698**
699** If the expression is of the form "<expr> <op> X" it gets commuted
700** to the standard form of "X <op> <expr>". If the expression is of
701** the form "X <op> Y" where both X and Y are columns, then the original
702** expression is unchanged and a new virtual expression of the form
703** "Y <op> X" is added to the WHERE clause and analyzed separately.
704*/
705static void exprAnalyze(
706 SrcList *pSrc, /* the FROM clause */
707 WhereClause *pWC, /* the WHERE clause */
708 int idxTerm /* Index of the term to be analyzed */
709){
710 WhereTerm *pTerm = &pWC->a[idxTerm];
711 ExprMaskSet *pMaskSet = pWC->pMaskSet;
712 Expr *pExpr = pTerm->pExpr;
713 Bitmask prereqLeft;
714 Bitmask prereqAll;
715 int nPattern;
716 int isComplete;
717 int op;
718 Parse *pParse = pWC->pParse;
719 sqlite3 *db = pParse->db;
720
721 if( db->mallocFailed ) return;
722 prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
723 op = pExpr->op;
724 if( op==TK_IN ){
725 assert( pExpr->pRight==0 );
726 pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->pList)
727 | exprSelectTableUsage(pMaskSet, pExpr->pSelect);
728 }else if( op==TK_ISNULL ){
729 pTerm->prereqRight = 0;
730 }else{
731 pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
732 }
733 prereqAll = exprTableUsage(pMaskSet, pExpr);
734 if( ExprHasProperty(pExpr, EP_FromJoin) ){
735 prereqAll |= getMask(pMaskSet, pExpr->iRightJoinTable);
736 }
737 pTerm->prereqAll = prereqAll;
738 pTerm->leftCursor = -1;
739 pTerm->iParent = -1;
740 pTerm->eOperator = 0;
741 if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
742 Expr *pLeft = pExpr->pLeft;
743 Expr *pRight = pExpr->pRight;
744 if( pLeft->op==TK_COLUMN ){
745 pTerm->leftCursor = pLeft->iTable;
746 pTerm->leftColumn = pLeft->iColumn;
747 pTerm->eOperator = operatorMask(op);
748 }
749 if( pRight && pRight->op==TK_COLUMN ){
750 WhereTerm *pNew;
751 Expr *pDup;
752 if( pTerm->leftCursor>=0 ){
753 int idxNew;
754 pDup = sqlite3ExprDup(db, pExpr);
755 if( db->mallocFailed ){
756 sqlite3ExprDelete(pDup);
757 return;
758 }
759 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
760 if( idxNew==0 ) return;
761 pNew = &pWC->a[idxNew];
762 pNew->iParent = idxTerm;
763 pTerm = &pWC->a[idxTerm];
764 pTerm->nChild = 1;
765 pTerm->flags |= TERM_COPIED;
766 }else{
767 pDup = pExpr;
768 pNew = pTerm;
769 }
770 exprCommute(pDup);
771 pLeft = pDup->pLeft;
772 pNew->leftCursor = pLeft->iTable;
773 pNew->leftColumn = pLeft->iColumn;
774 pNew->prereqRight = prereqLeft;
775 pNew->prereqAll = prereqAll;
776 pNew->eOperator = operatorMask(pDup->op);
777 }
778 }
779
780#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
781 /* If a term is the BETWEEN operator, create two new virtual terms
782 ** that define the range that the BETWEEN implements.
783 */
784 else if( pExpr->op==TK_BETWEEN ){
785 ExprList *pList = pExpr->pList;
786 int i;
787 static const u8 ops[] = {TK_GE, TK_LE};
788 assert( pList!=0 );
789 assert( pList->nExpr==2 );
790 for(i=0; i<2; i++){
791 Expr *pNewExpr;
792 int idxNew;
793 pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft),
794 sqlite3ExprDup(db, pList->a[i].pExpr), 0);
795 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
796 exprAnalyze(pSrc, pWC, idxNew);
797 pTerm = &pWC->a[idxTerm];
798 pWC->a[idxNew].iParent = idxTerm;
799 }
800 pTerm->nChild = 2;
801 }
802#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
803
804#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
805 /* Attempt to convert OR-connected terms into an IN operator so that
806 ** they can make use of indices. Example:
807 **
808 ** x = expr1 OR expr2 = x OR x = expr3
809 **
810 ** is converted into
811 **
812 ** x IN (expr1,expr2,expr3)
813 **
814 ** This optimization must be omitted if OMIT_SUBQUERY is defined because
815 ** the compiler for the the IN operator is part of sub-queries.
816 */
817 else if( pExpr->op==TK_OR ){
818 int ok;
819 int i, j;
820 int iColumn, iCursor;
821 WhereClause sOr;
822 WhereTerm *pOrTerm;
823
824 assert( (pTerm->flags & TERM_DYNAMIC)==0 );
825 whereClauseInit(&sOr, pWC->pParse, pMaskSet);
826 whereSplit(&sOr, pExpr, TK_OR);
827 exprAnalyzeAll(pSrc, &sOr);
828 assert( sOr.nTerm>=2 );
829 j = 0;
830 do{
831 assert( j<sOr.nTerm );
832 iColumn = sOr.a[j].leftColumn;
833 iCursor = sOr.a[j].leftCursor;
834 ok = iCursor>=0;
835 for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
836 if( pOrTerm->eOperator!=WO_EQ ){
837 goto or_not_possible;
838 }
839 if( orTermIsOptCandidate(pOrTerm, iCursor, iColumn) ){
840 pOrTerm->flags |= TERM_OR_OK;
841 }else if( orTermHasOkDuplicate(&sOr, pOrTerm) ){
842 pOrTerm->flags &= ~TERM_OR_OK;
843 }else{
844 ok = 0;
845 }
846 }
847 }while( !ok && (sOr.a[j++].flags & TERM_COPIED)!=0 && j<2 );
848 if( ok ){
849 ExprList *pList = 0;
850 Expr *pNew, *pDup;
851 Expr *pLeft = 0;
852 for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
853 if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue;
854 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight);
855 pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
856 pLeft = pOrTerm->pExpr->pLeft;
857 }
858 assert( pLeft!=0 );
859 pDup = sqlite3ExprDup(db, pLeft);
860 pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
861 if( pNew ){
862 int idxNew;
863 transferJoinMarkings(pNew, pExpr);
864 pNew->pList = pList;
865 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
866 exprAnalyze(pSrc, pWC, idxNew);
867 pTerm = &pWC->a[idxTerm];
868 pWC->a[idxNew].iParent = idxTerm;
869 pTerm->nChild = 1;
870 }else{
871 sqlite3ExprListDelete(pList);
872 }
873 }
874or_not_possible:
875 whereClauseClear(&sOr);
876 }
877#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
878
879#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
880 /* Add constraints to reduce the search space on a LIKE or GLOB
881 ** operator.
882 */
883 if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete) ){
884 Expr *pLeft, *pRight;
885 Expr *pStr1, *pStr2;
886 Expr *pNewExpr1, *pNewExpr2;
887 int idxNew1, idxNew2;
888
889 pLeft = pExpr->pList->a[1].pExpr;
890 pRight = pExpr->pList->a[0].pExpr;
891 pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
892 if( pStr1 ){
893 sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
894 pStr1->token.n = nPattern;
895 pStr1->flags = EP_Dequoted;
896 }
897 pStr2 = sqlite3ExprDup(db, pStr1);
898 if( pStr2 ){
899 assert( pStr2->token.dyn );
900 ++*(u8*)&pStr2->token.z[nPattern-1];
901 }
902 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0);
903 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
904 exprAnalyze(pSrc, pWC, idxNew1);
905 pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0);
906 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
907 exprAnalyze(pSrc, pWC, idxNew2);
908 pTerm = &pWC->a[idxTerm];
909 if( isComplete ){
910 pWC->a[idxNew1].iParent = idxTerm;
911 pWC->a[idxNew2].iParent = idxTerm;
912 pTerm->nChild = 2;
913 }
914 }
915#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
916
917#ifndef SQLITE_OMIT_VIRTUALTABLE
918 /* Add a WO_MATCH auxiliary term to the constraint set if the
919 ** current expression is of the form: column MATCH expr.
920 ** This information is used by the xBestIndex methods of
921 ** virtual tables. The native query optimizer does not attempt
922 ** to do anything with MATCH functions.
923 */
924 if( isMatchOfColumn(pExpr) ){
925 int idxNew;
926 Expr *pRight, *pLeft;
927 WhereTerm *pNewTerm;
928 Bitmask prereqColumn, prereqExpr;
929
930 pRight = pExpr->pList->a[0].pExpr;
931 pLeft = pExpr->pList->a[1].pExpr;
932 prereqExpr = exprTableUsage(pMaskSet, pRight);
933 prereqColumn = exprTableUsage(pMaskSet, pLeft);
934 if( (prereqExpr & prereqColumn)==0 ){
935 Expr *pNewExpr;
936 pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0);
937 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
938 pNewTerm = &pWC->a[idxNew];
939 pNewTerm->prereqRight = prereqExpr;
940 pNewTerm->leftCursor = pLeft->iTable;
941 pNewTerm->leftColumn = pLeft->iColumn;
942 pNewTerm->eOperator = WO_MATCH;
943 pNewTerm->iParent = idxTerm;
944 pTerm = &pWC->a[idxTerm];
945 pTerm->nChild = 1;
946 pTerm->flags |= TERM_COPIED;
947 pNewTerm->prereqAll = pTerm->prereqAll;
948 }
949 }
950#endif /* SQLITE_OMIT_VIRTUALTABLE */
951}
952
953/*
954** Return TRUE if any of the expressions in pList->a[iFirst...] contain
955** a reference to any table other than the iBase table.
956*/
957static int referencesOtherTables(
958 ExprList *pList, /* Search expressions in ths list */
959 ExprMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
960 int iFirst, /* Be searching with the iFirst-th expression */
961 int iBase /* Ignore references to this table */
962){
963 Bitmask allowed = ~getMask(pMaskSet, iBase);
964 while( iFirst<pList->nExpr ){
965 if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
966 return 1;
967 }
968 }
969 return 0;
970}
971
972
973/*
974** This routine decides if pIdx can be used to satisfy the ORDER BY
975** clause. If it can, it returns 1. If pIdx cannot satisfy the
976** ORDER BY clause, this routine returns 0.
977**
978** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
979** left-most table in the FROM clause of that same SELECT statement and
980** the table has a cursor number of "base". pIdx is an index on pTab.
981**
982** nEqCol is the number of columns of pIdx that are used as equality
983** constraints. Any of these columns may be missing from the ORDER BY
984** clause and the match can still be a success.
985**
986** All terms of the ORDER BY that match against the index must be either
987** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
988** index do not need to satisfy this constraint.) The *pbRev value is
989** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
990** the ORDER BY clause is all ASC.
991*/
992static int isSortingIndex(
993 Parse *pParse, /* Parsing context */
994 ExprMaskSet *pMaskSet, /* Mapping from table indices to bitmaps */
995 Index *pIdx, /* The index we are testing */
996 int base, /* Cursor number for the table to be sorted */
997 ExprList *pOrderBy, /* The ORDER BY clause */
998 int nEqCol, /* Number of index columns with == constraints */
999 int *pbRev /* Set to 1 if ORDER BY is DESC */
1000){
1001 int i, j; /* Loop counters */
1002 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
1003 int nTerm; /* Number of ORDER BY terms */
1004 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
1005 sqlite3 *db = pParse->db;
1006
1007 assert( pOrderBy!=0 );
1008 nTerm = pOrderBy->nExpr;
1009 assert( nTerm>0 );
1010
1011 /* Match terms of the ORDER BY clause against columns of
1012 ** the index.
1013 **
1014 ** Note that indices have pIdx->nColumn regular columns plus
1015 ** one additional column containing the rowid. The rowid column
1016 ** of the index is also allowed to match against the ORDER BY
1017 ** clause.
1018 */
1019 for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
1020 Expr *pExpr; /* The expression of the ORDER BY pTerm */
1021 CollSeq *pColl; /* The collating sequence of pExpr */
1022 int termSortOrder; /* Sort order for this term */
1023 int iColumn; /* The i-th column of the index. -1 for rowid */
1024 int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
1025 const char *zColl; /* Name of the collating sequence for i-th index term */
1026
1027 pExpr = pTerm->pExpr;
1028 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
1029 /* Can not use an index sort on anything that is not a column in the
1030 ** left-most table of the FROM clause */
1031 break;
1032 }
1033 pColl = sqlite3ExprCollSeq(pParse, pExpr);
1034 if( !pColl ){
1035 pColl = db->pDfltColl;
1036 }
1037 if( i<pIdx->nColumn ){
1038 iColumn = pIdx->aiColumn[i];
1039 if( iColumn==pIdx->pTable->iPKey ){
1040 iColumn = -1;
1041 }
1042 iSortOrder = pIdx->aSortOrder[i];
1043 zColl = pIdx->azColl[i];
1044 }else{
1045 iColumn = -1;
1046 iSortOrder = 0;
1047 zColl = pColl->zName;
1048 }
1049 if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
1050 /* Term j of the ORDER BY clause does not match column i of the index */
1051 if( i<nEqCol ){
1052 /* If an index column that is constrained by == fails to match an
1053 ** ORDER BY term, that is OK. Just ignore that column of the index
1054 */
1055 continue;
1056 }else{
1057 /* If an index column fails to match and is not constrained by ==
1058 ** then the index cannot satisfy the ORDER BY constraint.
1059 */
1060 return 0;
1061 }
1062 }
1063 assert( pIdx->aSortOrder!=0 );
1064 assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
1065 assert( iSortOrder==0 || iSortOrder==1 );
1066 termSortOrder = iSortOrder ^ pTerm->sortOrder;
1067 if( i>nEqCol ){
1068 if( termSortOrder!=sortOrder ){
1069 /* Indices can only be used if all ORDER BY terms past the
1070 ** equality constraints are all either DESC or ASC. */
1071 return 0;
1072 }
1073 }else{
1074 sortOrder = termSortOrder;
1075 }
1076 j++;
1077 pTerm++;
1078 if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
1079 /* If the indexed column is the primary key and everything matches
1080 ** so far and none of the ORDER BY terms to the right reference other
1081 ** tables in the join, then we are assured that the index can be used
1082 ** to sort because the primary key is unique and so none of the other
1083 ** columns will make any difference
1084 */
1085 j = nTerm;
1086 }
1087 }
1088
1089 *pbRev = sortOrder!=0;
1090 if( j>=nTerm ){
1091 /* All terms of the ORDER BY clause are covered by this index so
1092 ** this index can be used for sorting. */
1093 return 1;
1094 }
1095 if( pIdx->onError!=OE_None && i==pIdx->nColumn
1096 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
1097 /* All terms of this index match some prefix of the ORDER BY clause
1098 ** and the index is UNIQUE and no terms on the tail of the ORDER BY
1099 ** clause reference other tables in a join. If this is all true then
1100 ** the order by clause is superfluous. */
1101 return 1;
1102 }
1103 return 0;
1104}
1105
1106/*
1107** Check table to see if the ORDER BY clause in pOrderBy can be satisfied
1108** by sorting in order of ROWID. Return true if so and set *pbRev to be
1109** true for reverse ROWID and false for forward ROWID order.
1110*/
1111static int sortableByRowid(
1112 int base, /* Cursor number for table to be sorted */
1113 ExprList *pOrderBy, /* The ORDER BY clause */
1114 ExprMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
1115 int *pbRev /* Set to 1 if ORDER BY is DESC */
1116){
1117 Expr *p;
1118
1119 assert( pOrderBy!=0 );
1120 assert( pOrderBy->nExpr>0 );
1121 p = pOrderBy->a[0].pExpr;
1122 if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1
1123 && !referencesOtherTables(pOrderBy, pMaskSet, 1, base) ){
1124 *pbRev = pOrderBy->a[0].sortOrder;
1125 return 1;
1126 }
1127 return 0;
1128}
1129
1130/*
1131** Prepare a crude estimate of the logarithm of the input value.
1132** The results need not be exact. This is only used for estimating
1133** the total cost of performing operatings with O(logN) or O(NlogN)
1134** complexity. Because N is just a guess, it is no great tragedy if
1135** logN is a little off.
1136*/
1137static double estLog(double N){
1138 double logN = 1;
1139 double x = 10;
1140 while( N>x ){
1141 logN += 1;
1142 x *= 10;
1143 }
1144 return logN;
1145}
1146
1147/*
1148** Two routines for printing the content of an sqlite3_index_info
1149** structure. Used for testing and debugging only. If neither
1150** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
1151** are no-ops.
1152*/
1153#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
1154static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
1155 int i;
1156 if( !sqlite3_where_trace ) return;
1157 for(i=0; i<p->nConstraint; i++){
1158 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
1159 i,
1160 p->aConstraint[i].iColumn,
1161 p->aConstraint[i].iTermOffset,
1162 p->aConstraint[i].op,
1163 p->aConstraint[i].usable);
1164 }
1165 for(i=0; i<p->nOrderBy; i++){
1166 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
1167 i,
1168 p->aOrderBy[i].iColumn,
1169 p->aOrderBy[i].desc);
1170 }
1171}
1172static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
1173 int i;
1174 if( !sqlite3_where_trace ) return;
1175 for(i=0; i<p->nConstraint; i++){
1176 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
1177 i,
1178 p->aConstraintUsage[i].argvIndex,
1179 p->aConstraintUsage[i].omit);
1180 }
1181 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
1182 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
1183 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
1184 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
1185}
1186#else
1187#define TRACE_IDX_INPUTS(A)
1188#define TRACE_IDX_OUTPUTS(A)
1189#endif
1190
1191#ifndef SQLITE_OMIT_VIRTUALTABLE
1192/*
1193** Compute the best index for a virtual table.
1194**
1195** The best index is computed by the xBestIndex method of the virtual
1196** table module. This routine is really just a wrapper that sets up
1197** the sqlite3_index_info structure that is used to communicate with
1198** xBestIndex.
1199**
1200** In a join, this routine might be called multiple times for the
1201** same virtual table. The sqlite3_index_info structure is created
1202** and initialized on the first invocation and reused on all subsequent
1203** invocations. The sqlite3_index_info structure is also used when
1204** code is generated to access the virtual table. The whereInfoDelete()
1205** routine takes care of freeing the sqlite3_index_info structure after
1206** everybody has finished with it.
1207*/
1208static double bestVirtualIndex(
1209 Parse *pParse, /* The parsing context */
1210 WhereClause *pWC, /* The WHERE clause */
1211 struct SrcList_item *pSrc, /* The FROM clause term to search */
1212 Bitmask notReady, /* Mask of cursors that are not available */
1213 ExprList *pOrderBy, /* The order by clause */
1214 int orderByUsable, /* True if we can potential sort */
1215 sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
1216){
1217 Table *pTab = pSrc->pTab;
1218 sqlite3_index_info *pIdxInfo;
1219 struct sqlite3_index_constraint *pIdxCons;
1220 struct sqlite3_index_orderby *pIdxOrderBy;
1221 struct sqlite3_index_constraint_usage *pUsage;
1222 WhereTerm *pTerm;
1223 int i, j;
1224 int nOrderBy;
1225 int rc;
1226
1227 /* If the sqlite3_index_info structure has not been previously
1228 ** allocated and initialized for this virtual table, then allocate
1229 ** and initialize it now
1230 */
1231 pIdxInfo = *ppIdxInfo;
1232 if( pIdxInfo==0 ){
1233 WhereTerm *pTerm;
1234 int nTerm;
1235 WHERETRACE(("Recomputing index info for %s...\n", pTab->zName));
1236
1237 /* Count the number of possible WHERE clause constraints referring
1238 ** to this virtual table */
1239 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1240 if( pTerm->leftCursor != pSrc->iCursor ) continue;
1241 if( pTerm->eOperator==WO_IN ) continue;
1242 nTerm++;
1243 }
1244
1245 /* If the ORDER BY clause contains only columns in the current
1246 ** virtual table then allocate space for the aOrderBy part of
1247 ** the sqlite3_index_info structure.
1248 */
1249 nOrderBy = 0;
1250 if( pOrderBy ){
1251 for(i=0; i<pOrderBy->nExpr; i++){
1252 Expr *pExpr = pOrderBy->a[i].pExpr;
1253 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
1254 }
1255 if( i==pOrderBy->nExpr ){
1256 nOrderBy = pOrderBy->nExpr;
1257 }
1258 }
1259
1260 /* Allocate the sqlite3_index_info structure
1261 */
1262 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
1263 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
1264 + sizeof(*pIdxOrderBy)*nOrderBy );
1265 if( pIdxInfo==0 ){
1266 sqlite3ErrorMsg(pParse, "out of memory");
1267 return 0.0;
1268 }
1269 *ppIdxInfo = pIdxInfo;
1270
1271 /* Initialize the structure. The sqlite3_index_info structure contains
1272 ** many fields that are declared "const" to prevent xBestIndex from
1273 ** changing them. We have to do some funky casting in order to
1274 ** initialize those fields.
1275 */
1276 pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
1277 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
1278 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
1279 *(int*)&pIdxInfo->nConstraint = nTerm;
1280 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
1281 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
1282 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
1283 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
1284 pUsage;
1285
1286 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1287 if( pTerm->leftCursor != pSrc->iCursor ) continue;
1288 if( pTerm->eOperator==WO_IN ) continue;
1289 pIdxCons[j].iColumn = pTerm->leftColumn;
1290 pIdxCons[j].iTermOffset = i;
1291 pIdxCons[j].op = pTerm->eOperator;
1292 /* The direct assignment in the previous line is possible only because
1293 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
1294 ** following asserts verify this fact. */
1295 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
1296 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
1297 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
1298 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
1299 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
1300 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
1301 assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
1302 j++;
1303 }
1304 for(i=0; i<nOrderBy; i++){
1305 Expr *pExpr = pOrderBy->a[i].pExpr;
1306 pIdxOrderBy[i].iColumn = pExpr->iColumn;
1307 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
1308 }
1309 }
1310
1311 /* At this point, the sqlite3_index_info structure that pIdxInfo points
1312 ** to will have been initialized, either during the current invocation or
1313 ** during some prior invocation. Now we just have to customize the
1314 ** details of pIdxInfo for the current invocation and pass it to
1315 ** xBestIndex.
1316 */
1317
1318 /* The module name must be defined. Also, by this point there must
1319 ** be a pointer to an sqlite3_vtab structure. Otherwise
1320 ** sqlite3ViewGetColumnNames() would have picked up the error.
1321 */
1322 assert( pTab->azModuleArg && pTab->azModuleArg[0] );
1323 assert( pTab->pVtab );
1324#if 0
1325 if( pTab->pVtab==0 ){
1326 sqlite3ErrorMsg(pParse, "undefined module %s for table %s",
1327 pTab->azModuleArg[0], pTab->zName);
1328 return 0.0;
1329 }
1330#endif
1331
1332 /* Set the aConstraint[].usable fields and initialize all
1333 ** output variables to zero.
1334 **
1335 ** aConstraint[].usable is true for constraints where the right-hand
1336 ** side contains only references to tables to the left of the current
1337 ** table. In other words, if the constraint is of the form:
1338 **
1339 ** column = expr
1340 **
1341 ** and we are evaluating a join, then the constraint on column is
1342 ** only valid if all tables referenced in expr occur to the left
1343 ** of the table containing column.
1344 **
1345 ** The aConstraints[] array contains entries for all constraints
1346 ** on the current table. That way we only have to compute it once
1347 ** even though we might try to pick the best index multiple times.
1348 ** For each attempt at picking an index, the order of tables in the
1349 ** join might be different so we have to recompute the usable flag
1350 ** each time.
1351 */
1352 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
1353 pUsage = pIdxInfo->aConstraintUsage;
1354 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
1355 j = pIdxCons->iTermOffset;
1356 pTerm = &pWC->a[j];
1357 pIdxCons->usable = (pTerm->prereqRight & notReady)==0;
1358 }
1359 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
1360 if( pIdxInfo->needToFreeIdxStr ){
1361 sqlite3_free(pIdxInfo->idxStr);
1362 }
1363 pIdxInfo->idxStr = 0;
1364 pIdxInfo->idxNum = 0;
1365 pIdxInfo->needToFreeIdxStr = 0;
1366 pIdxInfo->orderByConsumed = 0;
1367 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / 2.0;
1368 nOrderBy = pIdxInfo->nOrderBy;
1369 if( pIdxInfo->nOrderBy && !orderByUsable ){
1370 *(int*)&pIdxInfo->nOrderBy = 0;
1371 }
1372
1373 sqlite3SafetyOff(pParse->db);
1374 WHERETRACE(("xBestIndex for %s\n", pTab->zName));
1375 TRACE_IDX_INPUTS(pIdxInfo);
1376 rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
1377 TRACE_IDX_OUTPUTS(pIdxInfo);
1378 if( rc!=SQLITE_OK ){
1379 if( rc==SQLITE_NOMEM ){
1380 pParse->db->mallocFailed = 1;
1381 }else {
1382 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
1383 }
1384 sqlite3SafetyOn(pParse->db);
1385 }else{
1386 rc = sqlite3SafetyOn(pParse->db);
1387 }
1388 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
1389
1390 return pIdxInfo->estimatedCost;
1391}
1392#endif /* SQLITE_OMIT_VIRTUALTABLE */
1393
1394/*
1395** Find the best index for accessing a particular table. Return a pointer
1396** to the index, flags that describe how the index should be used, the
1397** number of equality constraints, and the "cost" for this index.
1398**
1399** The lowest cost index wins. The cost is an estimate of the amount of
1400** CPU and disk I/O need to process the request using the selected index.
1401** Factors that influence cost include:
1402**
1403** * The estimated number of rows that will be retrieved. (The
1404** fewer the better.)
1405**
1406** * Whether or not sorting must occur.
1407**
1408** * Whether or not there must be separate lookups in the
1409** index and in the main table.
1410**
1411*/
1412static double bestIndex(
1413 Parse *pParse, /* The parsing context */
1414 WhereClause *pWC, /* The WHERE clause */
1415 struct SrcList_item *pSrc, /* The FROM clause term to search */
1416 Bitmask notReady, /* Mask of cursors that are not available */
1417 ExprList *pOrderBy, /* The order by clause */
1418 Index **ppIndex, /* Make *ppIndex point to the best index */
1419 int *pFlags, /* Put flags describing this choice in *pFlags */
1420 int *pnEq /* Put the number of == or IN constraints here */
1421){
1422 WhereTerm *pTerm;
1423 Index *bestIdx = 0; /* Index that gives the lowest cost */
1424 double lowestCost; /* The cost of using bestIdx */
1425 int bestFlags = 0; /* Flags associated with bestIdx */
1426 int bestNEq = 0; /* Best value for nEq */
1427 int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
1428 Index *pProbe; /* An index we are evaluating */
1429 int rev; /* True to scan in reverse order */
1430 int flags; /* Flags associated with pProbe */
1431 int nEq; /* Number of == or IN constraints */
1432 int eqTermMask; /* Mask of valid equality operators */
1433 double cost; /* Cost of using pProbe */
1434
1435 WHERETRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady));
1436 lowestCost = SQLITE_BIG_DBL;
1437 pProbe = pSrc->pTab->pIndex;
1438
1439 /* If the table has no indices and there are no terms in the where
1440 ** clause that refer to the ROWID, then we will never be able to do
1441 ** anything other than a full table scan on this table. We might as
1442 ** well put it first in the join order. That way, perhaps it can be
1443 ** referenced by other tables in the join.
1444 */
1445 if( pProbe==0 &&
1446 findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IN|WO_LT|WO_LE|WO_GT|WO_GE,0)==0 &&
1447 (pOrderBy==0 || !sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev)) ){
1448 *pFlags = 0;
1449 *ppIndex = 0;
1450 *pnEq = 0;
1451 return 0.0;
1452 }
1453
1454 /* Check for a rowid=EXPR or rowid IN (...) constraints
1455 */
1456 pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
1457 if( pTerm ){
1458 Expr *pExpr;
1459 *ppIndex = 0;
1460 bestFlags = WHERE_ROWID_EQ;
1461 if( pTerm->eOperator & WO_EQ ){
1462 /* Rowid== is always the best pick. Look no further. Because only
1463 ** a single row is generated, output is always in sorted order */
1464 *pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
1465 *pnEq = 1;
1466 WHERETRACE(("... best is rowid\n"));
1467 return 0.0;
1468 }else if( (pExpr = pTerm->pExpr)->pList!=0 ){
1469 /* Rowid IN (LIST): cost is NlogN where N is the number of list
1470 ** elements. */
1471 lowestCost = pExpr->pList->nExpr;
1472 lowestCost *= estLog(lowestCost);
1473 }else{
1474 /* Rowid IN (SELECT): cost is NlogN where N is the number of rows
1475 ** in the result of the inner select. We have no way to estimate
1476 ** that value so make a wild guess. */
1477 lowestCost = 200;
1478 }
1479 WHERETRACE(("... rowid IN cost: %.9g\n", lowestCost));
1480 }
1481
1482 /* Estimate the cost of a table scan. If we do not know how many
1483 ** entries are in the table, use 1 million as a guess.
1484 */
1485 cost = pProbe ? pProbe->aiRowEst[0] : 1000000;
1486 WHERETRACE(("... table scan base cost: %.9g\n", cost));
1487 flags = WHERE_ROWID_RANGE;
1488
1489 /* Check for constraints on a range of rowids in a table scan.
1490 */
1491 pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0);
1492 if( pTerm ){
1493 if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){
1494 flags |= WHERE_TOP_LIMIT;
1495 cost /= 3; /* Guess that rowid<EXPR eliminates two-thirds or rows */
1496 }
1497 if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){
1498 flags |= WHERE_BTM_LIMIT;
1499 cost /= 3; /* Guess that rowid>EXPR eliminates two-thirds of rows */
1500 }
1501 WHERETRACE(("... rowid range reduces cost to %.9g\n", cost));
1502 }else{
1503 flags = 0;
1504 }
1505
1506 /* If the table scan does not satisfy the ORDER BY clause, increase
1507 ** the cost by NlogN to cover the expense of sorting. */
1508 if( pOrderBy ){
1509 if( sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev) ){
1510 flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE;
1511 if( rev ){
1512 flags |= WHERE_REVERSE;
1513 }
1514 }else{
1515 cost += cost*estLog(cost);
1516 WHERETRACE(("... sorting increases cost to %.9g\n", cost));
1517 }
1518 }
1519 if( cost<lowestCost ){
1520 lowestCost = cost;
1521 bestFlags = flags;
1522 }
1523
1524 /* If the pSrc table is the right table of a LEFT JOIN then we may not
1525 ** use an index to satisfy IS NULL constraints on that table. This is
1526 ** because columns might end up being NULL if the table does not match -
1527 ** a circumstance which the index cannot help us discover. Ticket #2177.
1528 */
1529 if( (pSrc->jointype & JT_LEFT)!=0 ){
1530 eqTermMask = WO_EQ|WO_IN;
1531 }else{
1532 eqTermMask = WO_EQ|WO_IN|WO_ISNULL;
1533 }
1534
1535 /* Look at each index.
1536 */
1537 for(; pProbe; pProbe=pProbe->pNext){
1538 int i; /* Loop counter */
1539 double inMultiplier = 1;
1540
1541 WHERETRACE(("... index %s:\n", pProbe->zName));
1542
1543 /* Count the number of columns in the index that are satisfied
1544 ** by x=EXPR constraints or x IN (...) constraints.
1545 */
1546 flags = 0;
1547 for(i=0; i<pProbe->nColumn; i++){
1548 int j = pProbe->aiColumn[i];
1549 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pProbe);
1550 if( pTerm==0 ) break;
1551 flags |= WHERE_COLUMN_EQ;
1552 if( pTerm->eOperator & WO_IN ){
1553 Expr *pExpr = pTerm->pExpr;
1554 flags |= WHERE_COLUMN_IN;
1555 if( pExpr->pSelect!=0 ){
1556 inMultiplier *= 25;
1557 }else if( pExpr->pList!=0 ){
1558 inMultiplier *= pExpr->pList->nExpr + 1;
1559 }
1560 }
1561 }
1562 cost = pProbe->aiRowEst[i] * inMultiplier * estLog(inMultiplier);
1563 nEq = i;
1564 if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
1565 && nEq==pProbe->nColumn ){
1566 flags |= WHERE_UNIQUE;
1567 }
1568 WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n",nEq,inMultiplier,cost));
1569
1570 /* Look for range constraints
1571 */
1572 if( nEq<pProbe->nColumn ){
1573 int j = pProbe->aiColumn[nEq];
1574 pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe);
1575 if( pTerm ){
1576 flags |= WHERE_COLUMN_RANGE;
1577 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pProbe) ){
1578 flags |= WHERE_TOP_LIMIT;
1579 cost /= 3;
1580 }
1581 if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){
1582 flags |= WHERE_BTM_LIMIT;
1583 cost /= 3;
1584 }
1585 WHERETRACE(("...... range reduces cost to %.9g\n", cost));
1586 }
1587 }
1588
1589 /* Add the additional cost of sorting if that is a factor.
1590 */
1591 if( pOrderBy ){
1592 if( (flags & WHERE_COLUMN_IN)==0 &&
1593 isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev) ){
1594 if( flags==0 ){
1595 flags = WHERE_COLUMN_RANGE;
1596 }
1597 flags |= WHERE_ORDERBY;
1598 if( rev ){
1599 flags |= WHERE_REVERSE;
1600 }
1601 }else{
1602 cost += cost*estLog(cost);
1603 WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
1604 }
1605 }
1606
1607 /* Check to see if we can get away with using just the index without
1608 ** ever reading the table. If that is the case, then halve the
1609 ** cost of this index.
1610 */
1611 if( flags && pSrc->colUsed < (((Bitmask)1)<<(BMS-1)) ){
1612 Bitmask m = pSrc->colUsed;
1613 int j;
1614 for(j=0; j<pProbe->nColumn; j++){
1615 int x = pProbe->aiColumn[j];
1616 if( x<BMS-1 ){
1617 m &= ~(((Bitmask)1)<<x);
1618 }
1619 }
1620 if( m==0 ){
1621 flags |= WHERE_IDX_ONLY;
1622 cost /= 2;
1623 WHERETRACE(("...... idx-only reduces cost to %.9g\n", cost));
1624 }
1625 }
1626
1627 /* If this index has achieved the lowest cost so far, then use it.
1628 */
1629 if( flags && cost < lowestCost ){
1630 bestIdx = pProbe;
1631 lowestCost = cost;
1632 bestFlags = flags;
1633 bestNEq = nEq;
1634 }
1635 }
1636
1637 /* Report the best result
1638 */
1639 *ppIndex = bestIdx;
1640 WHERETRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
1641 bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq));
1642 *pFlags = bestFlags | eqTermMask;
1643 *pnEq = bestNEq;
1644 return lowestCost;
1645}
1646
1647
1648/*
1649** Disable a term in the WHERE clause. Except, do not disable the term
1650** if it controls a LEFT OUTER JOIN and it did not originate in the ON
1651** or USING clause of that join.
1652**
1653** Consider the term t2.z='ok' in the following queries:
1654**
1655** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
1656** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
1657** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
1658**
1659** The t2.z='ok' is disabled in the in (2) because it originates
1660** in the ON clause. The term is disabled in (3) because it is not part
1661** of a LEFT OUTER JOIN. In (1), the term is not disabled.
1662**
1663** Disabling a term causes that term to not be tested in the inner loop
1664** of the join. Disabling is an optimization. When terms are satisfied
1665** by indices, we disable them to prevent redundant tests in the inner
1666** loop. We would get the correct results if nothing were ever disabled,
1667** but joins might run a little slower. The trick is to disable as much
1668** as we can without disabling too much. If we disabled in (1), we'd get
1669** the wrong answer. See ticket #813.
1670*/
1671static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
1672 if( pTerm
1673 && (pTerm->flags & TERM_CODED)==0
1674 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
1675 ){
1676 pTerm->flags |= TERM_CODED;
1677 if( pTerm->iParent>=0 ){
1678 WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
1679 if( (--pOther->nChild)==0 ){
1680 disableTerm(pLevel, pOther);
1681 }
1682 }
1683 }
1684}
1685
1686/*
1687** Generate code that builds a probe for an index.
1688**
1689** There should be nColumn values on the stack. The index
1690** to be probed is pIdx. Pop the values from the stack and
1691** replace them all with a single record that is the index
1692** problem.
1693*/
1694static void buildIndexProbe(
1695 Vdbe *v, /* Generate code into this VM */
1696 int nColumn, /* The number of columns to check for NULL */
1697 Index *pIdx /* Index that we will be searching */
1698){
1699 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
1700 sqlite3IndexAffinityStr(v, pIdx);
1701}
1702
1703
1704/*
1705** Generate code for a single equality term of the WHERE clause. An equality
1706** term can be either X=expr or X IN (...). pTerm is the term to be
1707** coded.
1708**
1709** The current value for the constraint is left on the top of the stack.
1710**
1711** For a constraint of the form X=expr, the expression is evaluated and its
1712** result is left on the stack. For constraints of the form X IN (...)
1713** this routine sets up a loop that will iterate over all values of X.
1714*/
1715static void codeEqualityTerm(
1716 Parse *pParse, /* The parsing context */
1717 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
1718 WhereLevel *pLevel /* When level of the FROM clause we are working on */
1719){
1720 Expr *pX = pTerm->pExpr;
1721 Vdbe *v = pParse->pVdbe;
1722 if( pX->op==TK_EQ ){
1723 sqlite3ExprCode(pParse, pX->pRight);
1724 }else if( pX->op==TK_ISNULL ){
1725 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1726#ifndef SQLITE_OMIT_SUBQUERY
1727 }else{
1728 int iTab;
1729 struct InLoop *pIn;
1730
1731 assert( pX->op==TK_IN );
1732 sqlite3CodeSubselect(pParse, pX);
1733 iTab = pX->iTable;
1734 sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
1735 VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
1736 if( pLevel->nIn==0 ){
1737 pLevel->nxt = sqlite3VdbeMakeLabel(v);
1738 }
1739 pLevel->nIn++;
1740 pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop,
1741 sizeof(pLevel->aInLoop[0])*pLevel->nIn);
1742 pIn = pLevel->aInLoop;
1743 if( pIn ){
1744 pIn += pLevel->nIn - 1;
1745 pIn->iCur = iTab;
1746 pIn->topAddr = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
1747 sqlite3VdbeAddOp(v, OP_IsNull, -1, 0);
1748 }else{
1749 pLevel->nIn = 0;
1750 }
1751#endif
1752 }
1753 disableTerm(pLevel, pTerm);
1754}
1755
1756/*
1757** Generate code that will evaluate all == and IN constraints for an
1758** index. The values for all constraints are left on the stack.
1759**
1760** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
1761** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
1762** The index has as many as three equality constraints, but in this
1763** example, the third "c" value is an inequality. So only two
1764** constraints are coded. This routine will generate code to evaluate
1765** a==5 and b IN (1,2,3). The current values for a and b will be left
1766** on the stack - a is the deepest and b the shallowest.
1767**
1768** In the example above nEq==2. But this subroutine works for any value
1769** of nEq including 0. If nEq==0, this routine is nearly a no-op.
1770** The only thing it does is allocate the pLevel->iMem memory cell.
1771**
1772** This routine always allocates at least one memory cell and puts
1773** the address of that memory cell in pLevel->iMem. The code that
1774** calls this routine will use pLevel->iMem to store the termination
1775** key value of the loop. If one or more IN operators appear, then
1776** this routine allocates an additional nEq memory cells for internal
1777** use.
1778*/
1779static void codeAllEqualityTerms(
1780 Parse *pParse, /* Parsing context */
1781 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
1782 WhereClause *pWC, /* The WHERE clause */
1783 Bitmask notReady /* Which parts of FROM have not yet been coded */
1784){
1785 int nEq = pLevel->nEq; /* The number of == or IN constraints to code */
1786 int termsInMem = 0; /* If true, store value in mem[] cells */
1787 Vdbe *v = pParse->pVdbe; /* The virtual machine under construction */
1788 Index *pIdx = pLevel->pIdx; /* The index being used for this loop */
1789 int iCur = pLevel->iTabCur; /* The cursor of the table */
1790 WhereTerm *pTerm; /* A single constraint term */
1791 int j; /* Loop counter */
1792
1793 /* Figure out how many memory cells we will need then allocate them.
1794 ** We always need at least one used to store the loop terminator
1795 ** value. If there are IN operators we'll need one for each == or
1796 ** IN constraint.
1797 */
1798 pLevel->iMem = pParse->nMem++;
1799 if( pLevel->flags & WHERE_COLUMN_IN ){
1800 pParse->nMem += pLevel->nEq;
1801 termsInMem = 1;
1802 }
1803
1804 /* Evaluate the equality constraints
1805 */
1806 assert( pIdx->nColumn>=nEq );
1807 for(j=0; j<nEq; j++){
1808 int k = pIdx->aiColumn[j];
1809 pTerm = findTerm(pWC, iCur, k, notReady, pLevel->flags, pIdx);
1810 if( pTerm==0 ) break;
1811 assert( (pTerm->flags & TERM_CODED)==0 );
1812 codeEqualityTerm(pParse, pTerm, pLevel);
1813 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
1814 sqlite3VdbeAddOp(v, OP_IsNull, termsInMem ? -1 : -(j+1), pLevel->brk);
1815 }
1816 if( termsInMem ){
1817 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem+j+1, 1);
1818 }
1819 }
1820
1821 /* Make sure all the constraint values are on the top of the stack
1822 */
1823 if( termsInMem ){
1824 for(j=0; j<nEq; j++){
1825 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem+j+1, 0);
1826 }
1827 }
1828}
1829
1830#if defined(SQLITE_TEST)
1831/*
1832** The following variable holds a text description of query plan generated
1833** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
1834** overwrites the previous. This information is used for testing and
1835** analysis only.
1836*/
1837char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
1838static int nQPlan = 0; /* Next free slow in _query_plan[] */
1839
1840#endif /* SQLITE_TEST */
1841
1842
1843/*
1844** Free a WhereInfo structure
1845*/
1846static void whereInfoFree(WhereInfo *pWInfo){
1847 if( pWInfo ){
1848 int i;
1849 for(i=0; i<pWInfo->nLevel; i++){
1850 sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
1851 if( pInfo ){
1852 if( pInfo->needToFreeIdxStr ){
1853 /* Coverage: Don't think this can be reached. By the time this
1854 ** function is called, the index-strings have been passed
1855 ** to the vdbe layer for deletion.
1856 */
1857 sqlite3_free(pInfo->idxStr);
1858 }
1859 sqlite3_free(pInfo);
1860 }
1861 }
1862 sqlite3_free(pWInfo);
1863 }
1864}
1865
1866
1867/*
1868** Generate the beginning of the loop used for WHERE clause processing.
1869** The return value is a pointer to an opaque structure that contains
1870** information needed to terminate the loop. Later, the calling routine
1871** should invoke sqlite3WhereEnd() with the return value of this function
1872** in order to complete the WHERE clause processing.
1873**
1874** If an error occurs, this routine returns NULL.
1875**
1876** The basic idea is to do a nested loop, one loop for each table in
1877** the FROM clause of a select. (INSERT and UPDATE statements are the
1878** same as a SELECT with only a single table in the FROM clause.) For
1879** example, if the SQL is this:
1880**
1881** SELECT * FROM t1, t2, t3 WHERE ...;
1882**
1883** Then the code generated is conceptually like the following:
1884**
1885** foreach row1 in t1 do \ Code generated
1886** foreach row2 in t2 do |-- by sqlite3WhereBegin()
1887** foreach row3 in t3 do /
1888** ...
1889** end \ Code generated
1890** end |-- by sqlite3WhereEnd()
1891** end /
1892**
1893** Note that the loops might not be nested in the order in which they
1894** appear in the FROM clause if a different order is better able to make
1895** use of indices. Note also that when the IN operator appears in
1896** the WHERE clause, it might result in additional nested loops for
1897** scanning through all values on the right-hand side of the IN.
1898**
1899** There are Btree cursors associated with each table. t1 uses cursor
1900** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
1901** And so forth. This routine generates code to open those VDBE cursors
1902** and sqlite3WhereEnd() generates the code to close them.
1903**
1904** The code that sqlite3WhereBegin() generates leaves the cursors named
1905** in pTabList pointing at their appropriate entries. The [...] code
1906** can use OP_Column and OP_Rowid opcodes on these cursors to extract
1907** data from the various tables of the loop.
1908**
1909** If the WHERE clause is empty, the foreach loops must each scan their
1910** entire tables. Thus a three-way join is an O(N^3) operation. But if
1911** the tables have indices and there are terms in the WHERE clause that
1912** refer to those indices, a complete table scan can be avoided and the
1913** code will run much faster. Most of the work of this routine is checking
1914** to see if there are indices that can be used to speed up the loop.
1915**
1916** Terms of the WHERE clause are also used to limit which rows actually
1917** make it to the "..." in the middle of the loop. After each "foreach",
1918** terms of the WHERE clause that use only terms in that loop and outer
1919** loops are evaluated and if false a jump is made around all subsequent
1920** inner loops (or around the "..." if the test occurs within the inner-
1921** most loop)
1922**
1923** OUTER JOINS
1924**
1925** An outer join of tables t1 and t2 is conceptally coded as follows:
1926**
1927** foreach row1 in t1 do
1928** flag = 0
1929** foreach row2 in t2 do
1930** start:
1931** ...
1932** flag = 1
1933** end
1934** if flag==0 then
1935** move the row2 cursor to a null row
1936** goto start
1937** fi
1938** end
1939**
1940** ORDER BY CLAUSE PROCESSING
1941**
1942** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
1943** if there is one. If there is no ORDER BY clause or if this routine
1944** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
1945**
1946** If an index can be used so that the natural output order of the table
1947** scan is correct for the ORDER BY clause, then that index is used and
1948** *ppOrderBy is set to NULL. This is an optimization that prevents an
1949** unnecessary sort of the result set if an index appropriate for the
1950** ORDER BY clause already exists.
1951**
1952** If the where clause loops cannot be arranged to provide the correct
1953** output order, then the *ppOrderBy is unchanged.
1954*/
1955WhereInfo *sqlite3WhereBegin(
1956 Parse *pParse, /* The parser context */
1957 SrcList *pTabList, /* A list of all tables to be scanned */
1958 Expr *pWhere, /* The WHERE clause */
1959 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
1960){
1961 int i; /* Loop counter */
1962 WhereInfo *pWInfo; /* Will become the return value of this function */
1963 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
1964 int brk, cont = 0; /* Addresses used during code generation */
1965 Bitmask notReady; /* Cursors that are not yet positioned */
1966 WhereTerm *pTerm; /* A single term in the WHERE clause */
1967 ExprMaskSet maskSet; /* The expression mask set */
1968 WhereClause wc; /* The WHERE clause is divided into these terms */
1969 struct SrcList_item *pTabItem; /* A single entry from pTabList */
1970 WhereLevel *pLevel; /* A single level in the pWInfo list */
1971 int iFrom; /* First unused FROM clause element */
1972 int andFlags; /* AND-ed combination of all wc.a[].flags */
1973 sqlite3 *db; /* Database connection */
1974
1975 /* The number of tables in the FROM clause is limited by the number of
1976 ** bits in a Bitmask
1977 */
1978 if( pTabList->nSrc>BMS ){
1979 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
1980 return 0;
1981 }
1982
1983 /* Split the WHERE clause into separate subexpressions where each
1984 ** subexpression is separated by an AND operator.
1985 */
1986 initMaskSet(&maskSet);
1987 whereClauseInit(&wc, pParse, &maskSet);
1988 whereSplit(&wc, pWhere, TK_AND);
1989
1990 /* Allocate and initialize the WhereInfo structure that will become the
1991 ** return value.
1992 */
1993 db = pParse->db;
1994 pWInfo = sqlite3DbMallocZero(db,
1995 sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
1996 if( db->mallocFailed ){
1997 goto whereBeginNoMem;
1998 }
1999 pWInfo->nLevel = pTabList->nSrc;
2000 pWInfo->pParse = pParse;
2001 pWInfo->pTabList = pTabList;
2002 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
2003
2004 /* Special case: a WHERE clause that is constant. Evaluate the
2005 ** expression and either jump over all of the code or fall thru.
2006 */
2007 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
2008 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
2009 pWhere = 0;
2010 }
2011
2012 /* Analyze all of the subexpressions. Note that exprAnalyze() might
2013 ** add new virtual terms onto the end of the WHERE clause. We do not
2014 ** want to analyze these virtual terms, so start analyzing at the end
2015 ** and work forward so that the added virtual terms are never processed.
2016 */
2017 for(i=0; i<pTabList->nSrc; i++){
2018 createMask(&maskSet, pTabList->a[i].iCursor);
2019 }
2020 exprAnalyzeAll(pTabList, &wc);
2021 if( db->mallocFailed ){
2022 goto whereBeginNoMem;
2023 }
2024
2025 /* Chose the best index to use for each table in the FROM clause.
2026 **
2027 ** This loop fills in the following fields:
2028 **
2029 ** pWInfo->a[].pIdx The index to use for this level of the loop.
2030 ** pWInfo->a[].flags WHERE_xxx flags associated with pIdx
2031 ** pWInfo->a[].nEq The number of == and IN constraints
2032 ** pWInfo->a[].iFrom When term of the FROM clause is being coded
2033 ** pWInfo->a[].iTabCur The VDBE cursor for the database table
2034 ** pWInfo->a[].iIdxCur The VDBE cursor for the index
2035 **
2036 ** This loop also figures out the nesting order of tables in the FROM
2037 ** clause.
2038 */
2039 notReady = ~(Bitmask)0;
2040 pTabItem = pTabList->a;
2041 pLevel = pWInfo->a;
2042 andFlags = ~0;
2043 WHERETRACE(("*** Optimizer Start ***\n"));
2044 for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
2045 Index *pIdx; /* Index for FROM table at pTabItem */
2046 int flags; /* Flags asssociated with pIdx */
2047 int nEq; /* Number of == or IN constraints */
2048 double cost; /* The cost for pIdx */
2049 int j; /* For looping over FROM tables */
2050 Index *pBest = 0; /* The best index seen so far */
2051 int bestFlags = 0; /* Flags associated with pBest */
2052 int bestNEq = 0; /* nEq associated with pBest */
2053 double lowestCost; /* Cost of the pBest */
2054 int bestJ = 0; /* The value of j */
2055 Bitmask m; /* Bitmask value for j or bestJ */
2056 int once = 0; /* True when first table is seen */
2057 sqlite3_index_info *pIndex; /* Current virtual index */
2058
2059 lowestCost = SQLITE_BIG_DBL;
2060 for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
2061 int doNotReorder; /* True if this table should not be reordered */
2062
2063 doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
2064 if( once && doNotReorder ) break;
2065 m = getMask(&maskSet, pTabItem->iCursor);
2066 if( (m & notReady)==0 ){
2067 if( j==iFrom ) iFrom++;
2068 continue;
2069 }
2070 assert( pTabItem->pTab );
2071#ifndef SQLITE_OMIT_VIRTUALTABLE
2072 if( IsVirtual(pTabItem->pTab) ){
2073 sqlite3_index_info **ppIdxInfo = &pWInfo->a[j].pIdxInfo;
2074 cost = bestVirtualIndex(pParse, &wc, pTabItem, notReady,
2075 ppOrderBy ? *ppOrderBy : 0, i==0,
2076 ppIdxInfo);
2077 flags = WHERE_VIRTUALTABLE;
2078 pIndex = *ppIdxInfo;
2079 if( pIndex && pIndex->orderByConsumed ){
2080 flags = WHERE_VIRTUALTABLE | WHERE_ORDERBY;
2081 }
2082 pIdx = 0;
2083 nEq = 0;
2084 if( (SQLITE_BIG_DBL/2.0)<cost ){
2085 /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
2086 ** inital value of lowestCost in this loop. If it is, then
2087 ** the (cost<lowestCost) test below will never be true and
2088 ** pLevel->pBestIdx never set.
2089 */
2090 cost = (SQLITE_BIG_DBL/2.0);
2091 }
2092 }else
2093#endif
2094 {
2095 cost = bestIndex(pParse, &wc, pTabItem, notReady,
2096 (i==0 && ppOrderBy) ? *ppOrderBy : 0,
2097 &pIdx, &flags, &nEq);
2098 pIndex = 0;
2099 }
2100 if( cost<lowestCost ){
2101 once = 1;
2102 lowestCost = cost;
2103 pBest = pIdx;
2104 bestFlags = flags;
2105 bestNEq = nEq;
2106 bestJ = j;
2107 pLevel->pBestIdx = pIndex;
2108 }
2109 if( doNotReorder ) break;
2110 }
2111 WHERETRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
2112 pLevel-pWInfo->a));
2113 if( (bestFlags & WHERE_ORDERBY)!=0 ){
2114 *ppOrderBy = 0;
2115 }
2116 andFlags &= bestFlags;
2117 pLevel->flags = bestFlags;
2118 pLevel->pIdx = pBest;
2119 pLevel->nEq = bestNEq;
2120 pLevel->aInLoop = 0;
2121 pLevel->nIn = 0;
2122 if( pBest ){
2123 pLevel->iIdxCur = pParse->nTab++;
2124 }else{
2125 pLevel->iIdxCur = -1;
2126 }
2127 notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor);
2128 pLevel->iFrom = bestJ;
2129 }
2130 WHERETRACE(("*** Optimizer Finished ***\n"));
2131
2132 /* If the total query only selects a single row, then the ORDER BY
2133 ** clause is irrelevant.
2134 */
2135 if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
2136 *ppOrderBy = 0;
2137 }
2138
2139 /* Open all tables in the pTabList and any indices selected for
2140 ** searching those tables.
2141 */
2142 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
2143 for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
2144 Table *pTab; /* Table to open */
2145 Index *pIx; /* Index used to access pTab (if any) */
2146 int iDb; /* Index of database containing table/index */
2147 int iIdxCur = pLevel->iIdxCur;
2148
2149#ifndef SQLITE_OMIT_EXPLAIN
2150 if( pParse->explain==2 ){
2151 char *zMsg;
2152 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
2153 zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
2154 if( pItem->zAlias ){
2155 zMsg = sqlite3MPrintf(db, "%z AS %s", zMsg, pItem->zAlias);
2156 }
2157 if( (pIx = pLevel->pIdx)!=0 ){
2158 zMsg = sqlite3MPrintf(db, "%z WITH INDEX %s", zMsg, pIx->zName);
2159 }else if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
2160 zMsg = sqlite3MPrintf(db, "%z USING PRIMARY KEY", zMsg);
2161 }
2162#ifndef SQLITE_OMIT_VIRTUALTABLE
2163 else if( pLevel->pBestIdx ){
2164 sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
2165 zMsg = sqlite3MPrintf(db, "%z VIRTUAL TABLE INDEX %d:%s", zMsg,
2166 pBestIdx->idxNum, pBestIdx->idxStr);
2167 }
2168#endif
2169 if( pLevel->flags & WHERE_ORDERBY ){
2170 zMsg = sqlite3MPrintf(db, "%z ORDER BY", zMsg);
2171 }
2172 sqlite3VdbeOp3(v, OP_Explain, i, pLevel->iFrom, zMsg, P3_DYNAMIC);
2173 }
2174#endif /* SQLITE_OMIT_EXPLAIN */
2175 pTabItem = &pTabList->a[pLevel->iFrom];
2176 pTab = pTabItem->pTab;
2177 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2178 if( pTab->isEphem || pTab->pSelect ) continue;
2179#ifndef SQLITE_OMIT_VIRTUALTABLE
2180 if( pLevel->pBestIdx ){
2181 int iCur = pTabItem->iCursor;
2182 sqlite3VdbeOp3(v, OP_VOpen, iCur, 0, (const char*)pTab->pVtab, P3_VTAB);
2183 }else
2184#endif
2185 if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
2186 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, OP_OpenRead);
2187 if( pTab->nCol<(sizeof(Bitmask)*8) ){
2188 Bitmask b = pTabItem->colUsed;
2189 int n = 0;
2190 for(; b; b=b>>1, n++){}
2191 sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-1, n);
2192 assert( n<=pTab->nCol );
2193 }
2194 }else{
2195 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
2196 }
2197 pLevel->iTabCur = pTabItem->iCursor;
2198 if( (pIx = pLevel->pIdx)!=0 ){
2199 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
2200 assert( pIx->pSchema==pTab->pSchema );
2201 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
2202 VdbeComment((v, "# %s", pIx->zName));
2203 sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum,
2204 (char*)pKey, P3_KEYINFO_HANDOFF);
2205 }
2206 if( (pLevel->flags & (WHERE_IDX_ONLY|WHERE_COLUMN_RANGE))!=0 ){
2207 /* Only call OP_SetNumColumns on the index if we might later use
2208 ** OP_Column on the index. */
2209 sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1);
2210 }
2211 sqlite3CodeVerifySchema(pParse, iDb);
2212 }
2213 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
2214
2215 /* Generate the code to do the search. Each iteration of the for
2216 ** loop below generates code for a single nested loop of the VM
2217 ** program.
2218 */
2219 notReady = ~(Bitmask)0;
2220 for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
2221 int j;
2222 int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */
2223 Index *pIdx; /* The index we will be using */
2224 int nxt; /* Where to jump to continue with the next IN case */
2225 int iIdxCur; /* The VDBE cursor for the index */
2226 int omitTable; /* True if we use the index only */
2227 int bRev; /* True if we need to scan in reverse order */
2228
2229 pTabItem = &pTabList->a[pLevel->iFrom];
2230 iCur = pTabItem->iCursor;
2231 pIdx = pLevel->pIdx;
2232 iIdxCur = pLevel->iIdxCur;
2233 bRev = (pLevel->flags & WHERE_REVERSE)!=0;
2234 omitTable = (pLevel->flags & WHERE_IDX_ONLY)!=0;
2235
2236 /* Create labels for the "break" and "continue" instructions
2237 ** for the current loop. Jump to brk to break out of a loop.
2238 ** Jump to cont to go immediately to the next iteration of the
2239 ** loop.
2240 **
2241 ** When there is an IN operator, we also have a "nxt" label that
2242 ** means to continue with the next IN value combination. When
2243 ** there are no IN operators in the constraints, the "nxt" label
2244 ** is the same as "brk".
2245 */
2246 brk = pLevel->brk = pLevel->nxt = sqlite3VdbeMakeLabel(v);
2247 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
2248
2249 /* If this is the right table of a LEFT OUTER JOIN, allocate and
2250 ** initialize a memory cell that records if this table matches any
2251 ** row of the left table of the join.
2252 */
2253 if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
2254 if( !pParse->nMem ) pParse->nMem++;
2255 pLevel->iLeftJoin = pParse->nMem++;
2256 sqlite3VdbeAddOp(v, OP_MemInt, 0, pLevel->iLeftJoin);
2257 VdbeComment((v, "# init LEFT JOIN no-match flag"));
2258 }
2259
2260#ifndef SQLITE_OMIT_VIRTUALTABLE
2261 if( pLevel->pBestIdx ){
2262 /* Case 0: The table is a virtual-table. Use the VFilter and VNext
2263 ** to access the data.
2264 */
2265 int j;
2266 sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
2267 int nConstraint = pBestIdx->nConstraint;
2268 struct sqlite3_index_constraint_usage *aUsage =
2269 pBestIdx->aConstraintUsage;
2270 const struct sqlite3_index_constraint *aConstraint =
2271 pBestIdx->aConstraint;
2272
2273 for(j=1; j<=nConstraint; j++){
2274 int k;
2275 for(k=0; k<nConstraint; k++){
2276 if( aUsage[k].argvIndex==j ){
2277 int iTerm = aConstraint[k].iTermOffset;
2278 sqlite3ExprCode(pParse, wc.a[iTerm].pExpr->pRight);
2279 break;
2280 }
2281 }
2282 if( k==nConstraint ) break;
2283 }
2284 sqlite3VdbeAddOp(v, OP_Integer, j-1, 0);
2285 sqlite3VdbeAddOp(v, OP_Integer, pBestIdx->idxNum, 0);
2286 sqlite3VdbeOp3(v, OP_VFilter, iCur, brk, pBestIdx->idxStr,
2287 pBestIdx->needToFreeIdxStr ? P3_MPRINTF : P3_STATIC);
2288 pBestIdx->needToFreeIdxStr = 0;
2289 for(j=0; j<pBestIdx->nConstraint; j++){
2290 if( aUsage[j].omit ){
2291 int iTerm = aConstraint[j].iTermOffset;
2292 disableTerm(pLevel, &wc.a[iTerm]);
2293 }
2294 }
2295 pLevel->op = OP_VNext;
2296 pLevel->p1 = iCur;
2297 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
2298 }else
2299#endif /* SQLITE_OMIT_VIRTUALTABLE */
2300
2301 if( pLevel->flags & WHERE_ROWID_EQ ){
2302 /* Case 1: We can directly reference a single row using an
2303 ** equality comparison against the ROWID field. Or
2304 ** we reference multiple rows using a "rowid IN (...)"
2305 ** construct.
2306 */
2307 pTerm = findTerm(&wc, iCur, -1, notReady, WO_EQ|WO_IN, 0);
2308 assert( pTerm!=0 );
2309 assert( pTerm->pExpr!=0 );
2310 assert( pTerm->leftCursor==iCur );
2311 assert( omitTable==0 );
2312 codeEqualityTerm(pParse, pTerm, pLevel);
2313 nxt = pLevel->nxt;
2314 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, nxt);
2315 sqlite3VdbeAddOp(v, OP_NotExists, iCur, nxt);
2316 VdbeComment((v, "pk"));
2317 pLevel->op = OP_Noop;
2318 }else if( pLevel->flags & WHERE_ROWID_RANGE ){
2319 /* Case 2: We have an inequality comparison against the ROWID field.
2320 */
2321 int testOp = OP_Noop;
2322 int start;
2323 WhereTerm *pStart, *pEnd;
2324
2325 assert( omitTable==0 );
2326 pStart = findTerm(&wc, iCur, -1, notReady, WO_GT|WO_GE, 0);
2327 pEnd = findTerm(&wc, iCur, -1, notReady, WO_LT|WO_LE, 0);
2328 if( bRev ){
2329 pTerm = pStart;
2330 pStart = pEnd;
2331 pEnd = pTerm;
2332 }
2333 if( pStart ){
2334 Expr *pX;
2335 pX = pStart->pExpr;
2336 assert( pX!=0 );
2337 assert( pStart->leftCursor==iCur );
2338 sqlite3ExprCode(pParse, pX->pRight);
2339 sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LE || pX->op==TK_GT, brk);
2340 sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk);
2341 VdbeComment((v, "pk"));
2342 disableTerm(pLevel, pStart);
2343 }else{
2344 sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk);
2345 }
2346 if( pEnd ){
2347 Expr *pX;
2348 pX = pEnd->pExpr;
2349 assert( pX!=0 );
2350 assert( pEnd->leftCursor==iCur );
2351 sqlite3ExprCode(pParse, pX->pRight);
2352 pLevel->iMem = pParse->nMem++;
2353 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2354 if( pX->op==TK_LT || pX->op==TK_GT ){
2355 testOp = bRev ? OP_Le : OP_Ge;
2356 }else{
2357 testOp = bRev ? OP_Lt : OP_Gt;
2358 }
2359 disableTerm(pLevel, pEnd);
2360 }
2361 start = sqlite3VdbeCurrentAddr(v);
2362 pLevel->op = bRev ? OP_Prev : OP_Next;
2363 pLevel->p1 = iCur;
2364 pLevel->p2 = start;
2365 if( testOp!=OP_Noop ){
2366 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
2367 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2368 sqlite3VdbeAddOp(v, testOp, SQLITE_AFF_NUMERIC|0x100, brk);
2369 }
2370 }else if( pLevel->flags & WHERE_COLUMN_RANGE ){
2371 /* Case 3: The WHERE clause term that refers to the right-most
2372 ** column of the index is an inequality. For example, if
2373 ** the index is on (x,y,z) and the WHERE clause is of the
2374 ** form "x=5 AND y<10" then this case is used. Only the
2375 ** right-most column can be an inequality - the rest must
2376 ** use the "==" and "IN" operators.
2377 **
2378 ** This case is also used when there are no WHERE clause
2379 ** constraints but an index is selected anyway, in order
2380 ** to force the output order to conform to an ORDER BY.
2381 */
2382 int start;
2383 int nEq = pLevel->nEq;
2384 int topEq=0; /* True if top limit uses ==. False is strictly < */
2385 int btmEq=0; /* True if btm limit uses ==. False if strictly > */
2386 int topOp, btmOp; /* Operators for the top and bottom search bounds */
2387 int testOp;
2388 int topLimit = (pLevel->flags & WHERE_TOP_LIMIT)!=0;
2389 int btmLimit = (pLevel->flags & WHERE_BTM_LIMIT)!=0;
2390
2391 /* Generate code to evaluate all constraint terms using == or IN
2392 ** and level the values of those terms on the stack.
2393 */
2394 codeAllEqualityTerms(pParse, pLevel, &wc, notReady);
2395
2396 /* Duplicate the equality term values because they will all be
2397 ** used twice: once to make the termination key and once to make the
2398 ** start key.
2399 */
2400 for(j=0; j<nEq; j++){
2401 sqlite3VdbeAddOp(v, OP_Dup, nEq-1, 0);
2402 }
2403
2404 /* Figure out what comparison operators to use for top and bottom
2405 ** search bounds. For an ascending index, the bottom bound is a > or >=
2406 ** operator and the top bound is a < or <= operator. For a descending
2407 ** index the operators are reversed.
2408 */
2409 if( pIdx->aSortOrder[nEq]==SQLITE_SO_ASC ){
2410 topOp = WO_LT|WO_LE;
2411 btmOp = WO_GT|WO_GE;
2412 }else{
2413 topOp = WO_GT|WO_GE;
2414 btmOp = WO_LT|WO_LE;
2415 SWAP(int, topLimit, btmLimit);
2416 }
2417
2418 /* Generate the termination key. This is the key value that
2419 ** will end the search. There is no termination key if there
2420 ** are no equality terms and no "X<..." term.
2421 **
2422 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
2423 ** key computed here really ends up being the start key.
2424 */
2425 nxt = pLevel->nxt;
2426 if( topLimit ){
2427 Expr *pX;
2428 int k = pIdx->aiColumn[j];
2429 pTerm = findTerm(&wc, iCur, k, notReady, topOp, pIdx);
2430 assert( pTerm!=0 );
2431 pX = pTerm->pExpr;
2432 assert( (pTerm->flags & TERM_CODED)==0 );
2433 sqlite3ExprCode(pParse, pX->pRight);
2434 sqlite3VdbeAddOp(v, OP_IsNull, -(nEq*2+1), nxt);
2435 topEq = pTerm->eOperator & (WO_LE|WO_GE);
2436 disableTerm(pLevel, pTerm);
2437 testOp = OP_IdxGE;
2438 }else{
2439 testOp = nEq>0 ? OP_IdxGE : OP_Noop;
2440 topEq = 1;
2441 }
2442 if( testOp!=OP_Noop ){
2443 int nCol = nEq + topLimit;
2444 pLevel->iMem = pParse->nMem++;
2445 buildIndexProbe(v, nCol, pIdx);
2446 if( bRev ){
2447 int op = topEq ? OP_MoveLe : OP_MoveLt;
2448 sqlite3VdbeAddOp(v, op, iIdxCur, nxt);
2449 }else{
2450 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2451 }
2452 }else if( bRev ){
2453 sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk);
2454 }
2455
2456 /* Generate the start key. This is the key that defines the lower
2457 ** bound on the search. There is no start key if there are no
2458 ** equality terms and if there is no "X>..." term. In
2459 ** that case, generate a "Rewind" instruction in place of the
2460 ** start key search.
2461 **
2462 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
2463 ** "start" key really ends up being used as the termination key.
2464 */
2465 if( btmLimit ){
2466 Expr *pX;
2467 int k = pIdx->aiColumn[j];
2468 pTerm = findTerm(&wc, iCur, k, notReady, btmOp, pIdx);
2469 assert( pTerm!=0 );
2470 pX = pTerm->pExpr;
2471 assert( (pTerm->flags & TERM_CODED)==0 );
2472 sqlite3ExprCode(pParse, pX->pRight);
2473 sqlite3VdbeAddOp(v, OP_IsNull, -(nEq+1), nxt);
2474 btmEq = pTerm->eOperator & (WO_LE|WO_GE);
2475 disableTerm(pLevel, pTerm);
2476 }else{
2477 btmEq = 1;
2478 }
2479 if( nEq>0 || btmLimit ){
2480 int nCol = nEq + btmLimit;
2481 buildIndexProbe(v, nCol, pIdx);
2482 if( bRev ){
2483 pLevel->iMem = pParse->nMem++;
2484 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2485 testOp = OP_IdxLT;
2486 }else{
2487 int op = btmEq ? OP_MoveGe : OP_MoveGt;
2488 sqlite3VdbeAddOp(v, op, iIdxCur, nxt);
2489 }
2490 }else if( bRev ){
2491 testOp = OP_Noop;
2492 }else{
2493 sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk);
2494 }
2495
2496 /* Generate the the top of the loop. If there is a termination
2497 ** key we have to test for that key and abort at the top of the
2498 ** loop.
2499 */
2500 start = sqlite3VdbeCurrentAddr(v);
2501 if( testOp!=OP_Noop ){
2502 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2503 sqlite3VdbeAddOp(v, testOp, iIdxCur, nxt);
2504 if( (topEq && !bRev) || (!btmEq && bRev) ){
2505 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
2506 }
2507 }
2508 if( topLimit | btmLimit ){
2509 sqlite3VdbeAddOp(v, OP_Column, iIdxCur, nEq);
2510 sqlite3VdbeAddOp(v, OP_IsNull, 1, cont);
2511 }
2512 if( !omitTable ){
2513 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
2514 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
2515 }
2516
2517 /* Record the instruction used to terminate the loop.
2518 */
2519 pLevel->op = bRev ? OP_Prev : OP_Next;
2520 pLevel->p1 = iIdxCur;
2521 pLevel->p2 = start;
2522 }else if( pLevel->flags & WHERE_COLUMN_EQ ){
2523 /* Case 4: There is an index and all terms of the WHERE clause that
2524 ** refer to the index using the "==" or "IN" operators.
2525 */
2526 int start;
2527 int nEq = pLevel->nEq;
2528
2529 /* Generate code to evaluate all constraint terms using == or IN
2530 ** and leave the values of those terms on the stack.
2531 */
2532 codeAllEqualityTerms(pParse, pLevel, &wc, notReady);
2533 nxt = pLevel->nxt;
2534
2535 /* Generate a single key that will be used to both start and terminate
2536 ** the search
2537 */
2538 buildIndexProbe(v, nEq, pIdx);
2539 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
2540
2541 /* Generate code (1) to move to the first matching element of the table.
2542 ** Then generate code (2) that jumps to "nxt" after the cursor is past
2543 ** the last matching element of the table. The code (1) is executed
2544 ** once to initialize the search, the code (2) is executed before each
2545 ** iteration of the scan to see if the scan has finished. */
2546 if( bRev ){
2547 /* Scan in reverse order */
2548 sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, nxt);
2549 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2550 sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, nxt);
2551 pLevel->op = OP_Prev;
2552 }else{
2553 /* Scan in the forward order */
2554 sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, nxt);
2555 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2556 sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, nxt, "+", P3_STATIC);
2557 pLevel->op = OP_Next;
2558 }
2559 if( !omitTable ){
2560 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
2561 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
2562 }
2563 pLevel->p1 = iIdxCur;
2564 pLevel->p2 = start;
2565 }else{
2566 /* Case 5: There is no usable index. We must do a complete
2567 ** scan of the entire table.
2568 */
2569 assert( omitTable==0 );
2570 assert( bRev==0 );
2571 pLevel->op = OP_Next;
2572 pLevel->p1 = iCur;
2573 pLevel->p2 = 1 + sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
2574 }
2575 notReady &= ~getMask(&maskSet, iCur);
2576
2577 /* Insert code to test every subexpression that can be completely
2578 ** computed using the current set of tables.
2579 */
2580 for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){
2581 Expr *pE;
2582 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
2583 if( (pTerm->prereqAll & notReady)!=0 ) continue;
2584 pE = pTerm->pExpr;
2585 assert( pE!=0 );
2586 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
2587 continue;
2588 }
2589 sqlite3ExprIfFalse(pParse, pE, cont, 1);
2590 pTerm->flags |= TERM_CODED;
2591 }
2592
2593 /* For a LEFT OUTER JOIN, generate code that will record the fact that
2594 ** at least one row of the right table has matched the left table.
2595 */
2596 if( pLevel->iLeftJoin ){
2597 pLevel->top = sqlite3VdbeCurrentAddr(v);
2598 sqlite3VdbeAddOp(v, OP_MemInt, 1, pLevel->iLeftJoin);
2599 VdbeComment((v, "# record LEFT JOIN hit"));
2600 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
2601 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
2602 if( (pTerm->prereqAll & notReady)!=0 ) continue;
2603 assert( pTerm->pExpr );
2604 sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, 1);
2605 pTerm->flags |= TERM_CODED;
2606 }
2607 }
2608 }
2609
2610#ifdef SQLITE_TEST /* For testing and debugging use only */
2611 /* Record in the query plan information about the current table
2612 ** and the index used to access it (if any). If the table itself
2613 ** is not used, its name is just '{}'. If no index is used
2614 ** the index is listed as "{}". If the primary key is used the
2615 ** index name is '*'.
2616 */
2617 for(i=0; i<pTabList->nSrc; i++){
2618 char *z;
2619 int n;
2620 pLevel = &pWInfo->a[i];
2621 pTabItem = &pTabList->a[pLevel->iFrom];
2622 z = pTabItem->zAlias;
2623 if( z==0 ) z = pTabItem->pTab->zName;
2624 n = strlen(z);
2625 if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
2626 if( pLevel->flags & WHERE_IDX_ONLY ){
2627 memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
2628 nQPlan += 2;
2629 }else{
2630 memcpy(&sqlite3_query_plan[nQPlan], z, n);
2631 nQPlan += n;
2632 }
2633 sqlite3_query_plan[nQPlan++] = ' ';
2634 }
2635 if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
2636 memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
2637 nQPlan += 2;
2638 }else if( pLevel->pIdx==0 ){
2639 memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
2640 nQPlan += 3;
2641 }else{
2642 n = strlen(pLevel->pIdx->zName);
2643 if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
2644 memcpy(&sqlite3_query_plan[nQPlan], pLevel->pIdx->zName, n);
2645 nQPlan += n;
2646 sqlite3_query_plan[nQPlan++] = ' ';
2647 }
2648 }
2649 }
2650 while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
2651 sqlite3_query_plan[--nQPlan] = 0;
2652 }
2653 sqlite3_query_plan[nQPlan] = 0;
2654 nQPlan = 0;
2655#endif /* SQLITE_TEST // Testing and debugging use only */
2656
2657 /* Record the continuation address in the WhereInfo structure. Then
2658 ** clean up and return.
2659 */
2660 pWInfo->iContinue = cont;
2661 whereClauseClear(&wc);
2662 return pWInfo;
2663
2664 /* Jump here if malloc fails */
2665whereBeginNoMem:
2666 whereClauseClear(&wc);
2667 whereInfoFree(pWInfo);
2668 return 0;
2669}
2670
2671/*
2672** Generate the end of the WHERE loop. See comments on
2673** sqlite3WhereBegin() for additional information.
2674*/
2675void sqlite3WhereEnd(WhereInfo *pWInfo){
2676 Vdbe *v = pWInfo->pParse->pVdbe;
2677 int i;
2678 WhereLevel *pLevel;
2679 SrcList *pTabList = pWInfo->pTabList;
2680
2681 /* Generate loop termination code.
2682 */
2683 for(i=pTabList->nSrc-1; i>=0; i--){
2684 pLevel = &pWInfo->a[i];
2685 sqlite3VdbeResolveLabel(v, pLevel->cont);
2686 if( pLevel->op!=OP_Noop ){
2687 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
2688 }
2689 if( pLevel->nIn ){
2690 struct InLoop *pIn;
2691 int j;
2692 sqlite3VdbeResolveLabel(v, pLevel->nxt);
2693 for(j=pLevel->nIn, pIn=&pLevel->aInLoop[j-1]; j>0; j--, pIn--){
2694 sqlite3VdbeJumpHere(v, pIn->topAddr+1);
2695 sqlite3VdbeAddOp(v, OP_Next, pIn->iCur, pIn->topAddr);
2696 sqlite3VdbeJumpHere(v, pIn->topAddr-1);
2697 }
2698 sqlite3_free(pLevel->aInLoop);
2699 }
2700 sqlite3VdbeResolveLabel(v, pLevel->brk);
2701 if( pLevel->iLeftJoin ){
2702 int addr;
2703 addr = sqlite3VdbeAddOp(v, OP_IfMemPos, pLevel->iLeftJoin, 0);
2704 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
2705 if( pLevel->iIdxCur>=0 ){
2706 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0);
2707 }
2708 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
2709 sqlite3VdbeJumpHere(v, addr);
2710 }
2711 }
2712
2713 /* The "break" point is here, just past the end of the outer loop.
2714 ** Set it.
2715 */
2716 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
2717
2718 /* Close all of the cursors that were opened by sqlite3WhereBegin.
2719 */
2720 for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
2721 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
2722 Table *pTab = pTabItem->pTab;
2723 assert( pTab!=0 );
2724 if( pTab->isEphem || pTab->pSelect ) continue;
2725 if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
2726 sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0);
2727 }
2728 if( pLevel->pIdx!=0 ){
2729 sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0);
2730 }
2731
2732 /* Make cursor substitutions for cases where we want to use
2733 ** just the index and never reference the table.
2734 **
2735 ** Calls to the code generator in between sqlite3WhereBegin and
2736 ** sqlite3WhereEnd will have created code that references the table
2737 ** directly. This loop scans all that code looking for opcodes
2738 ** that reference the table and converts them into opcodes that
2739 ** reference the index.
2740 */
2741 if( pLevel->flags & WHERE_IDX_ONLY ){
2742 int k, j, last;
2743 VdbeOp *pOp;
2744 Index *pIdx = pLevel->pIdx;
2745
2746 assert( pIdx!=0 );
2747 pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
2748 last = sqlite3VdbeCurrentAddr(v);
2749 for(k=pWInfo->iTop; k<last; k++, pOp++){
2750 if( pOp->p1!=pLevel->iTabCur ) continue;
2751 if( pOp->opcode==OP_Column ){
2752 pOp->p1 = pLevel->iIdxCur;
2753 for(j=0; j<pIdx->nColumn; j++){
2754 if( pOp->p2==pIdx->aiColumn[j] ){
2755 pOp->p2 = j;
2756 break;
2757 }
2758 }
2759 }else if( pOp->opcode==OP_Rowid ){
2760 pOp->p1 = pLevel->iIdxCur;
2761 pOp->opcode = OP_IdxRowid;
2762 }else if( pOp->opcode==OP_NullRow ){
2763 pOp->opcode = OP_Noop;
2764 }
2765 }
2766 }
2767 }
2768
2769 /* Final cleanup
2770 */
2771 whereInfoFree(pWInfo);
2772 return;
2773}