line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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.1.1.1 2004/08/08 15:03:57 matt Exp $ |
18
|
|
|
|
|
|
|
*/ |
19
|
|
|
|
|
|
|
#include "sqliteInt.h" |
20
|
|
|
|
|
|
|
#include "os.h" |
21
|
|
|
|
|
|
|
#include |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
/* |
24
|
|
|
|
|
|
|
** A pointer to this structure is used to communicate information |
25
|
|
|
|
|
|
|
** from sqliteInit into the sqliteInitCallback. |
26
|
|
|
|
|
|
|
*/ |
27
|
|
|
|
|
|
|
typedef struct { |
28
|
|
|
|
|
|
|
sqlite *db; /* The database being initialized */ |
29
|
|
|
|
|
|
|
char **pzErrMsg; /* Error message stored here */ |
30
|
|
|
|
|
|
|
} InitData; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
/* |
33
|
|
|
|
|
|
|
** Fill the InitData structure with an error message that indicates |
34
|
|
|
|
|
|
|
** that the database is corrupt. |
35
|
|
|
|
|
|
|
*/ |
36
|
0
|
|
|
|
|
|
static void corruptSchema(InitData *pData, const char *zExtra){ |
37
|
0
|
0
|
|
|
|
|
sqliteSetString(pData->pzErrMsg, "malformed database schema", |
38
|
0
|
0
|
|
|
|
|
zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
39
|
0
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
/* |
42
|
|
|
|
|
|
|
** This is the callback routine for the code that initializes the |
43
|
|
|
|
|
|
|
** database. See sqliteInit() below for additional information. |
44
|
|
|
|
|
|
|
** |
45
|
|
|
|
|
|
|
** Each callback contains the following information: |
46
|
|
|
|
|
|
|
** |
47
|
|
|
|
|
|
|
** argv[0] = "file-format" or "schema-cookie" or "table" or "index" |
48
|
|
|
|
|
|
|
** argv[1] = table or index name or meta statement type. |
49
|
|
|
|
|
|
|
** argv[2] = root page number for table or index. NULL for meta. |
50
|
|
|
|
|
|
|
** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement. |
51
|
|
|
|
|
|
|
** argv[4] = "1" for temporary files, "0" for main database, "2" or more |
52
|
|
|
|
|
|
|
** for auxiliary database files. |
53
|
|
|
|
|
|
|
** |
54
|
|
|
|
|
|
|
*/ |
55
|
|
|
|
|
|
|
static |
56
|
75
|
|
|
|
|
|
int sqliteInitCallback(void *pInit, int argc, char **argv, char **azColName){ |
57
|
75
|
|
|
|
|
|
InitData *pData = (InitData*)pInit; |
58
|
75
|
|
|
|
|
|
int nErr = 0; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
assert( argc==5 ); |
61
|
75
|
100
|
|
|
|
|
if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
62
|
73
|
50
|
|
|
|
|
if( argv[0]==0 ){ |
63
|
0
|
|
|
|
|
|
corruptSchema(pData, 0); |
64
|
0
|
|
|
|
|
|
return 1; |
65
|
|
|
|
|
|
|
} |
66
|
73
|
50
|
|
|
|
|
switch( argv[0][0] ){ |
67
|
|
|
|
|
|
|
case 'v': |
68
|
|
|
|
|
|
|
case 'i': |
69
|
|
|
|
|
|
|
case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */ |
70
|
73
|
|
|
|
|
|
sqlite *db = pData->db; |
71
|
73
|
50
|
|
|
|
|
if( argv[2]==0 || argv[4]==0 ){ |
|
|
50
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
corruptSchema(pData, 0); |
73
|
0
|
|
|
|
|
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
146
|
50
|
|
|
|
|
if( argv[3] && argv[3][0] ){ |
|
|
50
|
|
|
|
|
|
76
|
|
|
|
|
|
|
/* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
77
|
|
|
|
|
|
|
** But because db->init.busy is set to 1, no VDBE code is generated |
78
|
|
|
|
|
|
|
** or executed. All the parser does is build the internal data |
79
|
|
|
|
|
|
|
** structures that describe the table, index, or view. |
80
|
|
|
|
|
|
|
*/ |
81
|
|
|
|
|
|
|
char *zErr; |
82
|
|
|
|
|
|
|
assert( db->init.busy ); |
83
|
73
|
|
|
|
|
|
db->init.iDb = atoi(argv[4]); |
84
|
|
|
|
|
|
|
assert( db->init.iDb>=0 && db->init.iDbnDb ); |
85
|
73
|
|
|
|
|
|
db->init.newTnum = atoi(argv[2]); |
86
|
73
|
50
|
|
|
|
|
if( sqlite_exec(db, argv[3], 0, 0, &zErr) ){ |
87
|
0
|
|
|
|
|
|
corruptSchema(pData, zErr); |
88
|
0
|
|
|
|
|
|
sqlite_freemem(zErr); |
89
|
|
|
|
|
|
|
} |
90
|
73
|
|
|
|
|
|
db->init.iDb = 0; |
91
|
|
|
|
|
|
|
}else{ |
92
|
|
|
|
|
|
|
/* If the SQL column is blank it means this is an index that |
93
|
|
|
|
|
|
|
** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
94
|
|
|
|
|
|
|
** constraint for a CREATE TABLE. The index should have already |
95
|
|
|
|
|
|
|
** been created when we processed the CREATE TABLE. All we have |
96
|
|
|
|
|
|
|
** to do here is record the root page number for that index. |
97
|
|
|
|
|
|
|
*/ |
98
|
|
|
|
|
|
|
int iDb; |
99
|
|
|
|
|
|
|
Index *pIndex; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
iDb = atoi(argv[4]); |
102
|
|
|
|
|
|
|
assert( iDb>=0 && iDbnDb ); |
103
|
0
|
|
|
|
|
|
pIndex = sqliteFindIndex(db, argv[1], db->aDb[iDb].zName); |
104
|
0
|
0
|
|
|
|
|
if( pIndex==0 || pIndex->tnum!=0 ){ |
|
|
0
|
|
|
|
|
|
105
|
|
|
|
|
|
|
/* This can occur if there exists an index on a TEMP table which |
106
|
|
|
|
|
|
|
** has the same name as another index on a permanent index. Since |
107
|
|
|
|
|
|
|
** the permanent table is hidden by the TEMP table, we can also |
108
|
|
|
|
|
|
|
** safely ignore the index on the permanent table. |
109
|
|
|
|
|
|
|
*/ |
110
|
|
|
|
|
|
|
/* Do Nothing */; |
111
|
|
|
|
|
|
|
}else{ |
112
|
0
|
|
|
|
|
|
pIndex->tnum = atoi(argv[2]); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
73
|
|
|
|
|
|
break; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
default: { |
118
|
|
|
|
|
|
|
/* This can not happen! */ |
119
|
0
|
|
|
|
|
|
nErr = 1; |
120
|
|
|
|
|
|
|
assert( nErr==0 ); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
73
|
|
|
|
|
|
return nErr; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
/* |
127
|
|
|
|
|
|
|
** This is a callback procedure used to reconstruct a table. The |
128
|
|
|
|
|
|
|
** name of the table to be reconstructed is passed in as argv[0]. |
129
|
|
|
|
|
|
|
** |
130
|
|
|
|
|
|
|
** This routine is used to automatically upgrade a database from |
131
|
|
|
|
|
|
|
** format version 1 or 2 to version 3. The correct operation of |
132
|
|
|
|
|
|
|
** this routine relys on the fact that no indices are used when |
133
|
|
|
|
|
|
|
** copying a table out to a temporary file. |
134
|
|
|
|
|
|
|
** |
135
|
|
|
|
|
|
|
** The change from version 2 to version 3 occurred between SQLite |
136
|
|
|
|
|
|
|
** version 2.5.6 and 2.6.0 on 2002-July-18. |
137
|
|
|
|
|
|
|
*/ |
138
|
|
|
|
|
|
|
static |
139
|
0
|
|
|
|
|
|
int upgrade_3_callback(void *pInit, int argc, char **argv, char **NotUsed){ |
140
|
0
|
|
|
|
|
|
InitData *pData = (InitData*)pInit; |
141
|
|
|
|
|
|
|
int rc; |
142
|
|
|
|
|
|
|
Table *pTab; |
143
|
|
|
|
|
|
|
Trigger *pTrig; |
144
|
0
|
|
|
|
|
|
char *zErr = 0; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
pTab = sqliteFindTable(pData->db, argv[0], 0); |
147
|
|
|
|
|
|
|
assert( pTab!=0 ); |
148
|
|
|
|
|
|
|
assert( sqliteStrICmp(pTab->zName, argv[0])==0 ); |
149
|
0
|
0
|
|
|
|
|
if( pTab ){ |
150
|
0
|
|
|
|
|
|
pTrig = pTab->pTrigger; |
151
|
0
|
|
|
|
|
|
pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */ |
152
|
|
|
|
|
|
|
} |
153
|
0
|
|
|
|
|
|
rc = sqlite_exec_printf(pData->db, |
154
|
|
|
|
|
|
|
"CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; " |
155
|
|
|
|
|
|
|
"DELETE FROM '%q'; " |
156
|
|
|
|
|
|
|
"INSERT INTO '%q' SELECT * FROM sqlite_x; " |
157
|
|
|
|
|
|
|
"DROP TABLE sqlite_x;", |
158
|
|
|
|
|
|
|
0, 0, &zErr, argv[0], argv[0], argv[0]); |
159
|
0
|
0
|
|
|
|
|
if( zErr ){ |
160
|
0
|
0
|
|
|
|
|
if( *pData->pzErrMsg ) sqlite_freemem(*pData->pzErrMsg); |
161
|
0
|
|
|
|
|
|
*pData->pzErrMsg = zErr; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
/* If an error occurred in the SQL above, then the transaction will |
165
|
|
|
|
|
|
|
** rollback which will delete the internal symbol tables. This will |
166
|
|
|
|
|
|
|
** cause the structure that pTab points to be deleted. In case that |
167
|
|
|
|
|
|
|
** happened, we need to refetch pTab. |
168
|
|
|
|
|
|
|
*/ |
169
|
0
|
|
|
|
|
|
pTab = sqliteFindTable(pData->db, argv[0], 0); |
170
|
0
|
0
|
|
|
|
|
if( pTab ){ |
171
|
|
|
|
|
|
|
assert( sqliteStrICmp(pTab->zName, argv[0])==0 ); |
172
|
0
|
|
|
|
|
|
pTab->pTrigger = pTrig; /* Re-enable triggers */ |
173
|
|
|
|
|
|
|
} |
174
|
0
|
|
|
|
|
|
return rc!=SQLITE_OK; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
/* |
180
|
|
|
|
|
|
|
** Attempt to read the database schema and initialize internal |
181
|
|
|
|
|
|
|
** data structures for a single database file. The index of the |
182
|
|
|
|
|
|
|
** database file is given by iDb. iDb==0 is used for the main |
183
|
|
|
|
|
|
|
** database. iDb==1 should never be used. iDb>=2 is used for |
184
|
|
|
|
|
|
|
** auxiliary databases. Return one of the SQLITE_ error codes to |
185
|
|
|
|
|
|
|
** indicate success or failure. |
186
|
|
|
|
|
|
|
*/ |
187
|
54
|
|
|
|
|
|
static int sqliteInitOne(sqlite *db, int iDb, char **pzErrMsg){ |
188
|
|
|
|
|
|
|
int rc; |
189
|
|
|
|
|
|
|
BtCursor *curMain; |
190
|
|
|
|
|
|
|
int size; |
191
|
|
|
|
|
|
|
Table *pTab; |
192
|
|
|
|
|
|
|
char const *azArg[6]; |
193
|
|
|
|
|
|
|
char zDbNum[30]; |
194
|
|
|
|
|
|
|
int meta[SQLITE_N_BTREE_META]; |
195
|
|
|
|
|
|
|
InitData initData; |
196
|
|
|
|
|
|
|
char const *zMasterSchema; |
197
|
|
|
|
|
|
|
char const *zMasterName; |
198
|
54
|
|
|
|
|
|
char *zSql = 0; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
/* |
201
|
|
|
|
|
|
|
** The master database table has a structure like this |
202
|
|
|
|
|
|
|
*/ |
203
|
|
|
|
|
|
|
static char master_schema[] = |
204
|
|
|
|
|
|
|
"CREATE TABLE sqlite_master(\n" |
205
|
|
|
|
|
|
|
" type text,\n" |
206
|
|
|
|
|
|
|
" name text,\n" |
207
|
|
|
|
|
|
|
" tbl_name text,\n" |
208
|
|
|
|
|
|
|
" rootpage integer,\n" |
209
|
|
|
|
|
|
|
" sql text\n" |
210
|
|
|
|
|
|
|
")" |
211
|
|
|
|
|
|
|
; |
212
|
|
|
|
|
|
|
static char temp_master_schema[] = |
213
|
|
|
|
|
|
|
"CREATE TEMP TABLE sqlite_temp_master(\n" |
214
|
|
|
|
|
|
|
" type text,\n" |
215
|
|
|
|
|
|
|
" name text,\n" |
216
|
|
|
|
|
|
|
" tbl_name text,\n" |
217
|
|
|
|
|
|
|
" rootpage integer,\n" |
218
|
|
|
|
|
|
|
" sql text\n" |
219
|
|
|
|
|
|
|
")" |
220
|
|
|
|
|
|
|
; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
assert( iDb>=0 && iDbnDb ); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
/* zMasterSchema and zInitScript are set to point at the master schema |
225
|
|
|
|
|
|
|
** and initialisation script appropriate for the database being |
226
|
|
|
|
|
|
|
** initialised. zMasterName is the name of the master table. |
227
|
|
|
|
|
|
|
*/ |
228
|
54
|
100
|
|
|
|
|
if( iDb==1 ){ |
229
|
27
|
|
|
|
|
|
zMasterSchema = temp_master_schema; |
230
|
27
|
|
|
|
|
|
zMasterName = TEMP_MASTER_NAME; |
231
|
|
|
|
|
|
|
}else{ |
232
|
27
|
|
|
|
|
|
zMasterSchema = master_schema; |
233
|
27
|
|
|
|
|
|
zMasterName = MASTER_NAME; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
/* Construct the schema table. |
237
|
|
|
|
|
|
|
*/ |
238
|
54
|
|
|
|
|
|
sqliteSafetyOff(db); |
239
|
54
|
|
|
|
|
|
azArg[0] = "table"; |
240
|
54
|
|
|
|
|
|
azArg[1] = zMasterName; |
241
|
54
|
|
|
|
|
|
azArg[2] = "2"; |
242
|
54
|
|
|
|
|
|
azArg[3] = zMasterSchema; |
243
|
54
|
|
|
|
|
|
sprintf(zDbNum, "%d", iDb); |
244
|
54
|
|
|
|
|
|
azArg[4] = zDbNum; |
245
|
54
|
|
|
|
|
|
azArg[5] = 0; |
246
|
54
|
|
|
|
|
|
initData.db = db; |
247
|
54
|
|
|
|
|
|
initData.pzErrMsg = pzErrMsg; |
248
|
54
|
|
|
|
|
|
sqliteInitCallback(&initData, 5, (char **)azArg, 0); |
249
|
54
|
|
|
|
|
|
pTab = sqliteFindTable(db, zMasterName, db->aDb[iDb].zName); |
250
|
54
|
50
|
|
|
|
|
if( pTab ){ |
251
|
54
|
|
|
|
|
|
pTab->readOnly = 1; |
252
|
|
|
|
|
|
|
}else{ |
253
|
0
|
|
|
|
|
|
return SQLITE_NOMEM; |
254
|
|
|
|
|
|
|
} |
255
|
54
|
|
|
|
|
|
sqliteSafetyOn(db); |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
/* Create a cursor to hold the database open |
258
|
|
|
|
|
|
|
*/ |
259
|
54
|
50
|
|
|
|
|
if( db->aDb[iDb].pBt==0 ) return SQLITE_OK; |
260
|
54
|
|
|
|
|
|
rc = sqliteBtreeCursor(db->aDb[iDb].pBt, 2, 0, &curMain); |
261
|
54
|
50
|
|
|
|
|
if( rc ){ |
262
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(rc), (char*)0); |
263
|
0
|
|
|
|
|
|
return rc; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
/* Get the database meta information |
267
|
|
|
|
|
|
|
*/ |
268
|
54
|
|
|
|
|
|
rc = sqliteBtreeGetMeta(db->aDb[iDb].pBt, meta); |
269
|
54
|
50
|
|
|
|
|
if( rc ){ |
270
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(rc), (char*)0); |
271
|
0
|
|
|
|
|
|
sqliteBtreeCloseCursor(curMain); |
272
|
0
|
|
|
|
|
|
return rc; |
273
|
|
|
|
|
|
|
} |
274
|
54
|
|
|
|
|
|
db->aDb[iDb].schema_cookie = meta[1]; |
275
|
54
|
100
|
|
|
|
|
if( iDb==0 ){ |
276
|
27
|
|
|
|
|
|
db->next_cookie = meta[1]; |
277
|
27
|
|
|
|
|
|
db->file_format = meta[2]; |
278
|
27
|
|
|
|
|
|
size = meta[3]; |
279
|
27
|
50
|
|
|
|
|
if( size==0 ){ size = MAX_PAGES; } |
280
|
27
|
|
|
|
|
|
db->cache_size = size; |
281
|
27
|
|
|
|
|
|
db->safety_level = meta[4]; |
282
|
27
|
50
|
|
|
|
|
if( meta[6]>0 && meta[6]<=2 && db->temp_store==0 ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
db->temp_store = meta[6]; |
284
|
|
|
|
|
|
|
} |
285
|
27
|
50
|
|
|
|
|
if( db->safety_level==0 ) db->safety_level = 2; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
/* |
288
|
|
|
|
|
|
|
** file_format==1 Version 2.1.0. |
289
|
|
|
|
|
|
|
** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY. |
290
|
|
|
|
|
|
|
** file_format==3 Version 2.6.0. Fix empty-string index bug. |
291
|
|
|
|
|
|
|
** file_format==4 Version 2.7.0. Add support for separate numeric and |
292
|
|
|
|
|
|
|
** text datatypes. |
293
|
|
|
|
|
|
|
*/ |
294
|
27
|
100
|
|
|
|
|
if( db->file_format==0 ){ |
295
|
|
|
|
|
|
|
/* This happens if the database was initially empty */ |
296
|
5
|
|
|
|
|
|
db->file_format = 4; |
297
|
22
|
50
|
|
|
|
|
}else if( db->file_format>4 ){ |
298
|
0
|
|
|
|
|
|
sqliteBtreeCloseCursor(curMain); |
299
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "unsupported file format", (char*)0); |
300
|
0
|
|
|
|
|
|
return SQLITE_ERROR; |
301
|
|
|
|
|
|
|
} |
302
|
27
|
50
|
|
|
|
|
}else if( iDb!=1 && (db->file_format!=meta[2] || db->file_format<4) ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
303
|
|
|
|
|
|
|
assert( db->file_format>=4 ); |
304
|
0
|
0
|
|
|
|
|
if( meta[2]==0 ){ |
305
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "cannot attach empty database: ", |
306
|
0
|
|
|
|
|
|
db->aDb[iDb].zName, (char*)0); |
307
|
|
|
|
|
|
|
}else{ |
308
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "incompatible file format in auxiliary " |
309
|
0
|
|
|
|
|
|
"database: ", db->aDb[iDb].zName, (char*)0); |
310
|
|
|
|
|
|
|
} |
311
|
0
|
|
|
|
|
|
sqliteBtreeClose(db->aDb[iDb].pBt); |
312
|
0
|
|
|
|
|
|
db->aDb[iDb].pBt = 0; |
313
|
0
|
|
|
|
|
|
return SQLITE_FORMAT; |
314
|
|
|
|
|
|
|
} |
315
|
54
|
|
|
|
|
|
sqliteBtreeSetCacheSize(db->aDb[iDb].pBt, db->cache_size); |
316
|
54
|
50
|
|
|
|
|
sqliteBtreeSetSafetyLevel(db->aDb[iDb].pBt, meta[4]==0 ? 2 : meta[4]); |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
/* Read the schema information out of the schema tables |
319
|
|
|
|
|
|
|
*/ |
320
|
|
|
|
|
|
|
assert( db->init.busy ); |
321
|
54
|
|
|
|
|
|
sqliteSafetyOff(db); |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
/* The following SQL will read the schema from the master tables. |
324
|
|
|
|
|
|
|
** The first version works with SQLite file formats 2 or greater. |
325
|
|
|
|
|
|
|
** The second version is for format 1 files. |
326
|
|
|
|
|
|
|
** |
327
|
|
|
|
|
|
|
** Beginning with file format 2, the rowid for new table entries |
328
|
|
|
|
|
|
|
** (including entries in sqlite_master) is an increasing integer. |
329
|
|
|
|
|
|
|
** So for file format 2 and later, we can play back sqlite_master |
330
|
|
|
|
|
|
|
** and all the CREATE statements will appear in the right order. |
331
|
|
|
|
|
|
|
** But with file format 1, table entries were random and so we |
332
|
|
|
|
|
|
|
** have to make sure the CREATE TABLEs occur before their corresponding |
333
|
|
|
|
|
|
|
** CREATE INDEXs. (We don't have to deal with CREATE VIEW or |
334
|
|
|
|
|
|
|
** CREATE TRIGGER in file format 1 because those constructs did |
335
|
|
|
|
|
|
|
** not exist then.) |
336
|
|
|
|
|
|
|
*/ |
337
|
54
|
50
|
|
|
|
|
if( db->file_format>=2 ){ |
338
|
54
|
|
|
|
|
|
sqliteSetString(&zSql, |
339
|
|
|
|
|
|
|
"SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"", |
340
|
54
|
|
|
|
|
|
db->aDb[iDb].zName, "\".", zMasterName, (char*)0); |
341
|
|
|
|
|
|
|
}else{ |
342
|
0
|
|
|
|
|
|
sqliteSetString(&zSql, |
343
|
|
|
|
|
|
|
"SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"", |
344
|
0
|
|
|
|
|
|
db->aDb[iDb].zName, "\".", zMasterName, |
345
|
|
|
|
|
|
|
" WHERE type IN ('table', 'index')" |
346
|
|
|
|
|
|
|
" ORDER BY CASE type WHEN 'table' THEN 0 ELSE 1 END", (char*)0); |
347
|
|
|
|
|
|
|
} |
348
|
54
|
|
|
|
|
|
rc = sqlite_exec(db, zSql, sqliteInitCallback, &initData, 0); |
349
|
|
|
|
|
|
|
|
350
|
54
|
|
|
|
|
|
sqliteFree(zSql); |
351
|
54
|
|
|
|
|
|
sqliteSafetyOn(db); |
352
|
54
|
|
|
|
|
|
sqliteBtreeCloseCursor(curMain); |
353
|
54
|
50
|
|
|
|
|
if( sqlite_malloc_failed ){ |
354
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "out of memory", (char*)0); |
355
|
0
|
|
|
|
|
|
rc = SQLITE_NOMEM; |
356
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
357
|
|
|
|
|
|
|
} |
358
|
54
|
50
|
|
|
|
|
if( rc==SQLITE_OK ){ |
359
|
54
|
|
|
|
|
|
DbSetProperty(db, iDb, DB_SchemaLoaded); |
360
|
|
|
|
|
|
|
}else{ |
361
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, iDb); |
362
|
|
|
|
|
|
|
} |
363
|
54
|
|
|
|
|
|
return rc; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
/* |
367
|
|
|
|
|
|
|
** Initialize all database files - the main database file, the file |
368
|
|
|
|
|
|
|
** used to store temporary tables, and any additional database files |
369
|
|
|
|
|
|
|
** created using ATTACH statements. Return a success code. If an |
370
|
|
|
|
|
|
|
** error occurs, write an error message into *pzErrMsg. |
371
|
|
|
|
|
|
|
** |
372
|
|
|
|
|
|
|
** After the database is initialized, the SQLITE_Initialized |
373
|
|
|
|
|
|
|
** bit is set in the flags field of the sqlite structure. An |
374
|
|
|
|
|
|
|
** attempt is made to initialize the database as soon as it |
375
|
|
|
|
|
|
|
** is opened. If that fails (perhaps because another process |
376
|
|
|
|
|
|
|
** has the sqlite_master table locked) than another attempt |
377
|
|
|
|
|
|
|
** is made the first time the database is accessed. |
378
|
|
|
|
|
|
|
*/ |
379
|
27
|
|
|
|
|
|
int sqliteInit(sqlite *db, char **pzErrMsg){ |
380
|
|
|
|
|
|
|
int i, rc; |
381
|
|
|
|
|
|
|
|
382
|
27
|
50
|
|
|
|
|
if( db->init.busy ) return SQLITE_OK; |
383
|
|
|
|
|
|
|
assert( (db->flags & SQLITE_Initialized)==0 ); |
384
|
27
|
|
|
|
|
|
rc = SQLITE_OK; |
385
|
27
|
|
|
|
|
|
db->init.busy = 1; |
386
|
81
|
50
|
|
|
|
|
for(i=0; rc==SQLITE_OK && inDb; i++){ |
|
|
100
|
|
|
|
|
|
387
|
54
|
50
|
|
|
|
|
if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
|
|
100
|
|
|
|
|
|
388
|
27
|
|
|
|
|
|
rc = sqliteInitOne(db, i, pzErrMsg); |
389
|
27
|
50
|
|
|
|
|
if( rc ){ |
390
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, i); |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
/* Once all the other databases have been initialised, load the schema |
395
|
|
|
|
|
|
|
** for the TEMP database. This is loaded last, as the TEMP database |
396
|
|
|
|
|
|
|
** schema may contain references to objects in other databases. |
397
|
|
|
|
|
|
|
*/ |
398
|
27
|
50
|
|
|
|
|
if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
399
|
27
|
|
|
|
|
|
rc = sqliteInitOne(db, 1, pzErrMsg); |
400
|
27
|
50
|
|
|
|
|
if( rc ){ |
401
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, 1); |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
27
|
|
|
|
|
|
db->init.busy = 0; |
406
|
27
|
50
|
|
|
|
|
if( rc==SQLITE_OK ){ |
407
|
27
|
|
|
|
|
|
db->flags |= SQLITE_Initialized; |
408
|
27
|
|
|
|
|
|
sqliteCommitInternalChanges(db); |
409
|
|
|
|
|
|
|
} |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
/* If the database is in formats 1 or 2, then upgrade it to |
412
|
|
|
|
|
|
|
** version 3. This will reconstruct all indices. If the |
413
|
|
|
|
|
|
|
** upgrade fails for any reason (ex: out of disk space, database |
414
|
|
|
|
|
|
|
** is read only, interrupt received, etc.) then fail the init. |
415
|
|
|
|
|
|
|
*/ |
416
|
27
|
50
|
|
|
|
|
if( rc==SQLITE_OK && db->file_format<3 ){ |
|
|
50
|
|
|
|
|
|
417
|
0
|
|
|
|
|
|
char *zErr = 0; |
418
|
|
|
|
|
|
|
InitData initData; |
419
|
|
|
|
|
|
|
int meta[SQLITE_N_BTREE_META]; |
420
|
|
|
|
|
|
|
|
421
|
0
|
|
|
|
|
|
db->magic = SQLITE_MAGIC_OPEN; |
422
|
0
|
|
|
|
|
|
initData.db = db; |
423
|
0
|
|
|
|
|
|
initData.pzErrMsg = &zErr; |
424
|
0
|
|
|
|
|
|
db->file_format = 3; |
425
|
0
|
|
|
|
|
|
rc = sqlite_exec(db, |
426
|
|
|
|
|
|
|
"BEGIN; SELECT name FROM sqlite_master WHERE type='table';", |
427
|
|
|
|
|
|
|
upgrade_3_callback, |
428
|
|
|
|
|
|
|
&initData, |
429
|
|
|
|
|
|
|
&zErr); |
430
|
0
|
0
|
|
|
|
|
if( rc==SQLITE_OK ){ |
431
|
0
|
|
|
|
|
|
sqliteBtreeGetMeta(db->aDb[0].pBt, meta); |
432
|
0
|
|
|
|
|
|
meta[2] = 4; |
433
|
0
|
|
|
|
|
|
sqliteBtreeUpdateMeta(db->aDb[0].pBt, meta); |
434
|
0
|
|
|
|
|
|
sqlite_exec(db, "COMMIT", 0, 0, 0); |
435
|
|
|
|
|
|
|
} |
436
|
0
|
0
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
437
|
0
|
0
|
|
|
|
|
sqliteSetString(pzErrMsg, |
438
|
|
|
|
|
|
|
"unable to upgrade database to the version 2.6 format", |
439
|
0
|
|
|
|
|
|
zErr ? ": " : 0, zErr, (char*)0); |
440
|
|
|
|
|
|
|
} |
441
|
0
|
|
|
|
|
|
sqlite_freemem(zErr); |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
27
|
50
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
445
|
0
|
|
|
|
|
|
db->flags &= ~SQLITE_Initialized; |
446
|
|
|
|
|
|
|
} |
447
|
27
|
|
|
|
|
|
return rc; |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
/* |
451
|
|
|
|
|
|
|
** The version of the library |
452
|
|
|
|
|
|
|
*/ |
453
|
|
|
|
|
|
|
const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $"; |
454
|
|
|
|
|
|
|
const char sqlite_version[] = SQLITE_VERSION; |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
/* |
457
|
|
|
|
|
|
|
** Does the library expect data to be encoded as UTF-8 or iso8859? The |
458
|
|
|
|
|
|
|
** following global constant always lets us know. |
459
|
|
|
|
|
|
|
*/ |
460
|
|
|
|
|
|
|
#ifdef SQLITE_UTF8 |
461
|
|
|
|
|
|
|
const char sqlite_encoding[] = "UTF-8"; |
462
|
|
|
|
|
|
|
#else |
463
|
|
|
|
|
|
|
const char sqlite_encoding[] = "iso8859"; |
464
|
|
|
|
|
|
|
#endif |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
/* |
467
|
|
|
|
|
|
|
** Open a new SQLite database. Construct an "sqlite" structure to define |
468
|
|
|
|
|
|
|
** the state of this database and return a pointer to that structure. |
469
|
|
|
|
|
|
|
** |
470
|
|
|
|
|
|
|
** An attempt is made to initialize the in-memory data structures that |
471
|
|
|
|
|
|
|
** hold the database schema. But if this fails (because the schema file |
472
|
|
|
|
|
|
|
** is locked) then that step is deferred until the first call to |
473
|
|
|
|
|
|
|
** sqlite_exec(). |
474
|
|
|
|
|
|
|
*/ |
475
|
25
|
|
|
|
|
|
sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){ |
476
|
|
|
|
|
|
|
sqlite *db; |
477
|
|
|
|
|
|
|
int rc, i; |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
/* Allocate the sqlite data structure */ |
480
|
25
|
|
|
|
|
|
db = sqliteMalloc( sizeof(sqlite) ); |
481
|
25
|
50
|
|
|
|
|
if( pzErrMsg ) *pzErrMsg = 0; |
482
|
25
|
50
|
|
|
|
|
if( db==0 ) goto no_mem_on_open; |
483
|
25
|
|
|
|
|
|
db->onError = OE_Default; |
484
|
25
|
|
|
|
|
|
db->priorNewRowid = 0; |
485
|
25
|
|
|
|
|
|
db->magic = SQLITE_MAGIC_BUSY; |
486
|
25
|
|
|
|
|
|
db->nDb = 2; |
487
|
25
|
|
|
|
|
|
db->aDb = db->aDbStatic; |
488
|
|
|
|
|
|
|
/* db->flags |= SQLITE_ShortColNames; */ |
489
|
25
|
|
|
|
|
|
sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1); |
490
|
75
|
100
|
|
|
|
|
for(i=0; inDb; i++){ |
491
|
50
|
|
|
|
|
|
sqliteHashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0); |
492
|
50
|
|
|
|
|
|
sqliteHashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0); |
493
|
50
|
|
|
|
|
|
sqliteHashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0); |
494
|
50
|
|
|
|
|
|
sqliteHashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1); |
495
|
|
|
|
|
|
|
} |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
/* Open the backend database driver */ |
498
|
25
|
50
|
|
|
|
|
if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){ |
|
|
0
|
|
|
|
|
|
499
|
0
|
|
|
|
|
|
db->temp_store = 2; |
500
|
|
|
|
|
|
|
} |
501
|
25
|
|
|
|
|
|
rc = sqliteBtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); |
502
|
25
|
50
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
503
|
|
|
|
|
|
|
switch( rc ){ |
504
|
|
|
|
|
|
|
default: { |
505
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "unable to open database: ", |
506
|
|
|
|
|
|
|
zFilename, (char*)0); |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
} |
509
|
0
|
|
|
|
|
|
sqliteFree(db); |
510
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
511
|
0
|
|
|
|
|
|
return 0; |
512
|
|
|
|
|
|
|
} |
513
|
25
|
|
|
|
|
|
db->aDb[0].zName = "main"; |
514
|
25
|
|
|
|
|
|
db->aDb[1].zName = "temp"; |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
/* Attempt to read the schema */ |
517
|
25
|
|
|
|
|
|
sqliteRegisterBuiltinFunctions(db); |
518
|
25
|
|
|
|
|
|
rc = sqliteInit(db, pzErrMsg); |
519
|
25
|
|
|
|
|
|
db->magic = SQLITE_MAGIC_OPEN; |
520
|
25
|
50
|
|
|
|
|
if( sqlite_malloc_failed ){ |
521
|
0
|
|
|
|
|
|
sqlite_close(db); |
522
|
0
|
|
|
|
|
|
goto no_mem_on_open; |
523
|
25
|
50
|
|
|
|
|
}else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
|
|
0
|
|
|
|
|
|
524
|
0
|
|
|
|
|
|
sqlite_close(db); |
525
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
526
|
0
|
|
|
|
|
|
return 0; |
527
|
25
|
50
|
|
|
|
|
}else if( pzErrMsg ){ |
528
|
25
|
|
|
|
|
|
sqliteFree(*pzErrMsg); |
529
|
25
|
|
|
|
|
|
*pzErrMsg = 0; |
530
|
|
|
|
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
/* Return a pointer to the newly opened database structure */ |
533
|
25
|
|
|
|
|
|
return db; |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
no_mem_on_open: |
536
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "out of memory", (char*)0); |
537
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
538
|
0
|
|
|
|
|
|
return 0; |
539
|
|
|
|
|
|
|
} |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
/* |
542
|
|
|
|
|
|
|
** Return the ROWID of the most recent insert |
543
|
|
|
|
|
|
|
*/ |
544
|
1
|
|
|
|
|
|
int sqlite_last_insert_rowid(sqlite *db){ |
545
|
1
|
|
|
|
|
|
return db->lastRowid; |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
/* |
549
|
|
|
|
|
|
|
** Return the number of changes in the most recent call to sqlite_exec(). |
550
|
|
|
|
|
|
|
*/ |
551
|
83
|
|
|
|
|
|
int sqlite_changes(sqlite *db){ |
552
|
83
|
|
|
|
|
|
return db->nChange; |
553
|
|
|
|
|
|
|
} |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
/* |
556
|
|
|
|
|
|
|
** Return the number of changes produced by the last INSERT, UPDATE, or |
557
|
|
|
|
|
|
|
** DELETE statement to complete execution. The count does not include |
558
|
|
|
|
|
|
|
** changes due to SQL statements executed in trigger programs that were |
559
|
|
|
|
|
|
|
** triggered by that statement |
560
|
|
|
|
|
|
|
*/ |
561
|
0
|
|
|
|
|
|
int sqlite_last_statement_changes(sqlite *db){ |
562
|
0
|
|
|
|
|
|
return db->lsChange; |
563
|
|
|
|
|
|
|
} |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
/* |
566
|
|
|
|
|
|
|
** Close an existing SQLite database |
567
|
|
|
|
|
|
|
*/ |
568
|
25
|
|
|
|
|
|
void sqlite_close(sqlite *db){ |
569
|
|
|
|
|
|
|
HashElem *i; |
570
|
|
|
|
|
|
|
int j; |
571
|
25
|
|
|
|
|
|
db->want_to_close = 1; |
572
|
25
|
50
|
|
|
|
|
if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ |
|
|
50
|
|
|
|
|
|
573
|
|
|
|
|
|
|
/* printf("DID NOT CLOSE\n"); fflush(stdout); */ |
574
|
0
|
|
|
|
|
|
return; |
575
|
|
|
|
|
|
|
} |
576
|
25
|
|
|
|
|
|
db->magic = SQLITE_MAGIC_CLOSED; |
577
|
75
|
100
|
|
|
|
|
for(j=0; jnDb; j++){ |
578
|
50
|
|
|
|
|
|
struct Db *pDb = &db->aDb[j]; |
579
|
50
|
50
|
|
|
|
|
if( pDb->pBt ){ |
580
|
50
|
|
|
|
|
|
sqliteBtreeClose(pDb->pBt); |
581
|
50
|
|
|
|
|
|
pDb->pBt = 0; |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
} |
584
|
25
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
585
|
|
|
|
|
|
|
assert( db->nDb<=2 ); |
586
|
|
|
|
|
|
|
assert( db->aDb==db->aDbStatic ); |
587
|
742
|
100
|
|
|
|
|
for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
588
|
|
|
|
|
|
|
FuncDef *pFunc, *pNext; |
589
|
1634
|
100
|
|
|
|
|
for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
590
|
917
|
|
|
|
|
|
pNext = pFunc->pNext; |
591
|
917
|
|
|
|
|
|
sqliteFree(pFunc); |
592
|
|
|
|
|
|
|
} |
593
|
|
|
|
|
|
|
} |
594
|
25
|
|
|
|
|
|
sqliteHashClear(&db->aFunc); |
595
|
25
|
|
|
|
|
|
sqliteFree(db); |
596
|
|
|
|
|
|
|
} |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
/* |
599
|
|
|
|
|
|
|
** Rollback all database files. |
600
|
|
|
|
|
|
|
*/ |
601
|
5
|
|
|
|
|
|
void sqliteRollbackAll(sqlite *db){ |
602
|
|
|
|
|
|
|
int i; |
603
|
15
|
100
|
|
|
|
|
for(i=0; inDb; i++){ |
604
|
10
|
50
|
|
|
|
|
if( db->aDb[i].pBt ){ |
605
|
10
|
|
|
|
|
|
sqliteBtreeRollback(db->aDb[i].pBt); |
606
|
10
|
|
|
|
|
|
db->aDb[i].inTrans = 0; |
607
|
|
|
|
|
|
|
} |
608
|
|
|
|
|
|
|
} |
609
|
5
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
610
|
|
|
|
|
|
|
/* sqliteRollbackInternalChanges(db); */ |
611
|
5
|
|
|
|
|
|
} |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
/* |
614
|
|
|
|
|
|
|
** Execute SQL code. Return one of the SQLITE_ success/failure |
615
|
|
|
|
|
|
|
** codes. Also write an error message into memory obtained from |
616
|
|
|
|
|
|
|
** malloc() and make *pzErrMsg point to that message. |
617
|
|
|
|
|
|
|
** |
618
|
|
|
|
|
|
|
** If the SQL is a query, then for each row in the query result |
619
|
|
|
|
|
|
|
** the xCallback() function is called. pArg becomes the first |
620
|
|
|
|
|
|
|
** argument to xCallback(). If xCallback=NULL then no callback |
621
|
|
|
|
|
|
|
** is invoked, even for queries. |
622
|
|
|
|
|
|
|
*/ |
623
|
189
|
|
|
|
|
|
int sqlite_exec( |
624
|
|
|
|
|
|
|
sqlite *db, /* The database on which the SQL executes */ |
625
|
|
|
|
|
|
|
const char *zSql, /* The SQL to be executed */ |
626
|
|
|
|
|
|
|
sqlite_callback xCallback, /* Invoke this callback routine */ |
627
|
|
|
|
|
|
|
void *pArg, /* First argument to xCallback() */ |
628
|
|
|
|
|
|
|
char **pzErrMsg /* Write error messages here */ |
629
|
|
|
|
|
|
|
){ |
630
|
189
|
|
|
|
|
|
int rc = SQLITE_OK; |
631
|
|
|
|
|
|
|
const char *zLeftover; |
632
|
|
|
|
|
|
|
sqlite_vm *pVm; |
633
|
189
|
|
|
|
|
|
int nRetry = 0; |
634
|
189
|
|
|
|
|
|
int nChange = 0; |
635
|
|
|
|
|
|
|
int nCallback; |
636
|
|
|
|
|
|
|
|
637
|
189
|
50
|
|
|
|
|
if( zSql==0 ) return SQLITE_OK; |
638
|
378
|
50
|
|
|
|
|
while( rc==SQLITE_OK && zSql[0] ){ |
|
|
100
|
|
|
|
|
|
639
|
189
|
|
|
|
|
|
pVm = 0; |
640
|
189
|
|
|
|
|
|
rc = sqlite_compile(db, zSql, &zLeftover, &pVm, pzErrMsg); |
641
|
189
|
50
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
642
|
|
|
|
|
|
|
assert( pVm==0 || sqlite_malloc_failed ); |
643
|
0
|
|
|
|
|
|
return rc; |
644
|
|
|
|
|
|
|
} |
645
|
189
|
50
|
|
|
|
|
if( pVm==0 ){ |
646
|
|
|
|
|
|
|
/* This happens if the zSql input contained only whitespace */ |
647
|
0
|
|
|
|
|
|
break; |
648
|
|
|
|
|
|
|
} |
649
|
189
|
|
|
|
|
|
db->nChange += nChange; |
650
|
189
|
|
|
|
|
|
nCallback = 0; |
651
|
|
|
|
|
|
|
while(1){ |
652
|
|
|
|
|
|
|
int nArg; |
653
|
|
|
|
|
|
|
char **azArg, **azCol; |
654
|
208
|
|
|
|
|
|
rc = sqlite_step(pVm, &nArg, (const char***)&azArg,(const char***)&azCol); |
655
|
208
|
100
|
|
|
|
|
if( rc==SQLITE_ROW ){ |
656
|
19
|
50
|
|
|
|
|
if( xCallback!=0 && xCallback(pArg, nArg, azArg, azCol) ){ |
|
|
50
|
|
|
|
|
|
657
|
0
|
|
|
|
|
|
sqlite_finalize(pVm, 0); |
658
|
0
|
|
|
|
|
|
return SQLITE_ABORT; |
659
|
|
|
|
|
|
|
} |
660
|
19
|
|
|
|
|
|
nCallback++; |
661
|
|
|
|
|
|
|
}else{ |
662
|
189
|
50
|
|
|
|
|
if( rc==SQLITE_DONE && nCallback==0 |
|
|
100
|
|
|
|
|
|
663
|
177
|
100
|
|
|
|
|
&& (db->flags & SQLITE_NullCallback)!=0 && xCallback!=0 ){ |
|
|
100
|
|
|
|
|
|
664
|
2
|
|
|
|
|
|
xCallback(pArg, nArg, azArg, azCol); |
665
|
|
|
|
|
|
|
} |
666
|
189
|
|
|
|
|
|
rc = sqlite_finalize(pVm, pzErrMsg); |
667
|
189
|
50
|
|
|
|
|
if( rc==SQLITE_SCHEMA && nRetry<2 ){ |
|
|
0
|
|
|
|
|
|
668
|
0
|
|
|
|
|
|
nRetry++; |
669
|
0
|
|
|
|
|
|
rc = SQLITE_OK; |
670
|
189
|
|
|
|
|
|
break; |
671
|
|
|
|
|
|
|
} |
672
|
189
|
100
|
|
|
|
|
if( db->pVdbe==0 ){ |
673
|
170
|
|
|
|
|
|
nChange = db->nChange; |
674
|
|
|
|
|
|
|
} |
675
|
189
|
|
|
|
|
|
nRetry = 0; |
676
|
189
|
|
|
|
|
|
zSql = zLeftover; |
677
|
189
|
50
|
|
|
|
|
while( isspace(zSql[0]) ) zSql++; |
678
|
189
|
|
|
|
|
|
break; |
679
|
|
|
|
|
|
|
} |
680
|
19
|
|
|
|
|
|
} |
681
|
|
|
|
|
|
|
} |
682
|
189
|
|
|
|
|
|
return rc; |
683
|
|
|
|
|
|
|
} |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
/* |
687
|
|
|
|
|
|
|
** Compile a single statement of SQL into a virtual machine. Return one |
688
|
|
|
|
|
|
|
** of the SQLITE_ success/failure codes. Also write an error message into |
689
|
|
|
|
|
|
|
** memory obtained from malloc() and make *pzErrMsg point to that message. |
690
|
|
|
|
|
|
|
*/ |
691
|
349
|
|
|
|
|
|
int sqlite_compile( |
692
|
|
|
|
|
|
|
sqlite *db, /* The database on which the SQL executes */ |
693
|
|
|
|
|
|
|
const char *zSql, /* The SQL to be executed */ |
694
|
|
|
|
|
|
|
const char **pzTail, /* OUT: Next statement after the first */ |
695
|
|
|
|
|
|
|
sqlite_vm **ppVm, /* OUT: The virtual machine */ |
696
|
|
|
|
|
|
|
char **pzErrMsg /* OUT: Write error messages here */ |
697
|
|
|
|
|
|
|
){ |
698
|
|
|
|
|
|
|
Parse sParse; |
699
|
|
|
|
|
|
|
|
700
|
349
|
100
|
|
|
|
|
if( pzErrMsg ) *pzErrMsg = 0; |
701
|
349
|
50
|
|
|
|
|
if( sqliteSafetyOn(db) ) goto exec_misuse; |
702
|
349
|
100
|
|
|
|
|
if( !db->init.busy ){ |
703
|
222
|
100
|
|
|
|
|
if( (db->flags & SQLITE_Initialized)==0 ){ |
704
|
2
|
|
|
|
|
|
int rc, cnt = 1; |
705
|
2
|
50
|
|
|
|
|
while( (rc = sqliteInit(db, pzErrMsg))==SQLITE_BUSY |
706
|
0
|
0
|
|
|
|
|
&& db->xBusyCallback |
707
|
0
|
0
|
|
|
|
|
&& db->xBusyCallback(db->pBusyArg, "", cnt++)!=0 ){} |
708
|
2
|
50
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
709
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
710
|
0
|
|
|
|
|
|
sqliteSafetyOff(db); |
711
|
0
|
|
|
|
|
|
return rc; |
712
|
|
|
|
|
|
|
} |
713
|
2
|
50
|
|
|
|
|
if( pzErrMsg ){ |
714
|
2
|
|
|
|
|
|
sqliteFree(*pzErrMsg); |
715
|
2
|
|
|
|
|
|
*pzErrMsg = 0; |
716
|
|
|
|
|
|
|
} |
717
|
|
|
|
|
|
|
} |
718
|
222
|
50
|
|
|
|
|
if( db->file_format<3 ){ |
719
|
0
|
|
|
|
|
|
sqliteSafetyOff(db); |
720
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "obsolete database file format", (char*)0); |
721
|
0
|
|
|
|
|
|
return SQLITE_ERROR; |
722
|
|
|
|
|
|
|
} |
723
|
|
|
|
|
|
|
} |
724
|
|
|
|
|
|
|
assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy ); |
725
|
349
|
100
|
|
|
|
|
if( db->pVdbe==0 ){ db->nChange = 0; } |
726
|
349
|
|
|
|
|
|
memset(&sParse, 0, sizeof(sParse)); |
727
|
349
|
|
|
|
|
|
sParse.db = db; |
728
|
349
|
|
|
|
|
|
sqliteRunParser(&sParse, zSql, pzErrMsg); |
729
|
349
|
50
|
|
|
|
|
if( db->xTrace && !db->init.busy ){ |
|
|
0
|
|
|
|
|
|
730
|
|
|
|
|
|
|
/* Trace only the statment that was compiled. |
731
|
|
|
|
|
|
|
** Make a copy of that part of the SQL string since zSQL is const |
732
|
|
|
|
|
|
|
** and we must pass a zero terminated string to the trace function |
733
|
|
|
|
|
|
|
** The copy is unnecessary if the tail pointer is pointing at the |
734
|
|
|
|
|
|
|
** beginnig or end of the SQL string. |
735
|
|
|
|
|
|
|
*/ |
736
|
0
|
0
|
|
|
|
|
if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
737
|
0
|
|
|
|
|
|
char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql); |
738
|
0
|
0
|
|
|
|
|
if( tmpSql ){ |
739
|
0
|
|
|
|
|
|
db->xTrace(db->pTraceArg, tmpSql); |
740
|
0
|
|
|
|
|
|
free(tmpSql); |
741
|
|
|
|
|
|
|
}else{ |
742
|
|
|
|
|
|
|
/* If a memory error occurred during the copy, |
743
|
|
|
|
|
|
|
** trace entire SQL string and fall through to the |
744
|
|
|
|
|
|
|
** sqlite_malloc_failed test to report the error. |
745
|
|
|
|
|
|
|
*/ |
746
|
0
|
|
|
|
|
|
db->xTrace(db->pTraceArg, zSql); |
747
|
|
|
|
|
|
|
} |
748
|
|
|
|
|
|
|
}else{ |
749
|
0
|
|
|
|
|
|
db->xTrace(db->pTraceArg, zSql); |
750
|
|
|
|
|
|
|
} |
751
|
|
|
|
|
|
|
} |
752
|
349
|
50
|
|
|
|
|
if( sqlite_malloc_failed ){ |
753
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, "out of memory", (char*)0); |
754
|
0
|
|
|
|
|
|
sParse.rc = SQLITE_NOMEM; |
755
|
0
|
|
|
|
|
|
sqliteRollbackAll(db); |
756
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
757
|
0
|
|
|
|
|
|
db->flags &= ~SQLITE_InTrans; |
758
|
|
|
|
|
|
|
} |
759
|
349
|
100
|
|
|
|
|
if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
760
|
349
|
100
|
|
|
|
|
if( sParse.rc!=SQLITE_OK && pzErrMsg && *pzErrMsg==0 ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
761
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(sParse.rc), (char*)0); |
762
|
|
|
|
|
|
|
} |
763
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
764
|
349
|
50
|
|
|
|
|
if( sParse.rc==SQLITE_SCHEMA ){ |
765
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
766
|
|
|
|
|
|
|
} |
767
|
|
|
|
|
|
|
assert( ppVm ); |
768
|
349
|
|
|
|
|
|
*ppVm = (sqlite_vm*)sParse.pVdbe; |
769
|
349
|
100
|
|
|
|
|
if( pzTail ) *pzTail = sParse.zTail; |
770
|
349
|
50
|
|
|
|
|
if( sqliteSafetyOff(db) ) goto exec_misuse; |
771
|
349
|
|
|
|
|
|
return sParse.rc; |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
exec_misuse: |
774
|
0
|
0
|
|
|
|
|
if( pzErrMsg ){ |
775
|
0
|
|
|
|
|
|
*pzErrMsg = 0; |
776
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), (char*)0); |
777
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
778
|
|
|
|
|
|
|
} |
779
|
349
|
|
|
|
|
|
return SQLITE_MISUSE; |
780
|
|
|
|
|
|
|
} |
781
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
/* |
784
|
|
|
|
|
|
|
** The following routine destroys a virtual machine that is created by |
785
|
|
|
|
|
|
|
** the sqlite_compile() routine. |
786
|
|
|
|
|
|
|
** |
787
|
|
|
|
|
|
|
** The integer returned is an SQLITE_ success/failure code that describes |
788
|
|
|
|
|
|
|
** the result of executing the virtual machine. An error message is |
789
|
|
|
|
|
|
|
** written into memory obtained from malloc and *pzErrMsg is made to |
790
|
|
|
|
|
|
|
** point to that error if pzErrMsg is not NULL. The calling routine |
791
|
|
|
|
|
|
|
** should use sqlite_freemem() to delete the message when it has finished |
792
|
|
|
|
|
|
|
** with it. |
793
|
|
|
|
|
|
|
*/ |
794
|
342
|
|
|
|
|
|
int sqlite_finalize( |
795
|
|
|
|
|
|
|
sqlite_vm *pVm, /* The virtual machine to be destroyed */ |
796
|
|
|
|
|
|
|
char **pzErrMsg /* OUT: Write error messages here */ |
797
|
|
|
|
|
|
|
){ |
798
|
342
|
|
|
|
|
|
int rc = sqliteVdbeFinalize((Vdbe*)pVm, pzErrMsg); |
799
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
800
|
342
|
|
|
|
|
|
return rc; |
801
|
|
|
|
|
|
|
} |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
/* |
804
|
|
|
|
|
|
|
** Terminate the current execution of a virtual machine then |
805
|
|
|
|
|
|
|
** reset the virtual machine back to its starting state so that it |
806
|
|
|
|
|
|
|
** can be reused. Any error message resulting from the prior execution |
807
|
|
|
|
|
|
|
** is written into *pzErrMsg. A success code from the prior execution |
808
|
|
|
|
|
|
|
** is returned. |
809
|
|
|
|
|
|
|
*/ |
810
|
0
|
|
|
|
|
|
int sqlite_reset( |
811
|
|
|
|
|
|
|
sqlite_vm *pVm, /* The virtual machine to be destroyed */ |
812
|
|
|
|
|
|
|
char **pzErrMsg /* OUT: Write error messages here */ |
813
|
|
|
|
|
|
|
){ |
814
|
0
|
|
|
|
|
|
int rc = sqliteVdbeReset((Vdbe*)pVm, pzErrMsg); |
815
|
0
|
|
|
|
|
|
sqliteVdbeMakeReady((Vdbe*)pVm, -1, 0); |
816
|
|
|
|
|
|
|
sqliteStrRealloc(pzErrMsg); |
817
|
0
|
|
|
|
|
|
return rc; |
818
|
|
|
|
|
|
|
} |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
/* |
821
|
|
|
|
|
|
|
** Return a static string that describes the kind of error specified in the |
822
|
|
|
|
|
|
|
** argument. |
823
|
|
|
|
|
|
|
*/ |
824
|
0
|
|
|
|
|
|
const char *sqlite_error_string(int rc){ |
825
|
|
|
|
|
|
|
const char *z; |
826
|
0
|
|
|
|
|
|
switch( rc ){ |
827
|
0
|
|
|
|
|
|
case SQLITE_OK: z = "not an error"; break; |
828
|
0
|
|
|
|
|
|
case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
829
|
0
|
|
|
|
|
|
case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
830
|
0
|
|
|
|
|
|
case SQLITE_PERM: z = "access permission denied"; break; |
831
|
0
|
|
|
|
|
|
case SQLITE_ABORT: z = "callback requested query abort"; break; |
832
|
0
|
|
|
|
|
|
case SQLITE_BUSY: z = "database is locked"; break; |
833
|
0
|
|
|
|
|
|
case SQLITE_LOCKED: z = "database table is locked"; break; |
834
|
0
|
|
|
|
|
|
case SQLITE_NOMEM: z = "out of memory"; break; |
835
|
0
|
|
|
|
|
|
case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
836
|
0
|
|
|
|
|
|
case SQLITE_INTERRUPT: z = "interrupted"; break; |
837
|
0
|
|
|
|
|
|
case SQLITE_IOERR: z = "disk I/O error"; break; |
838
|
0
|
|
|
|
|
|
case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
839
|
0
|
|
|
|
|
|
case SQLITE_NOTFOUND: z = "table or record not found"; break; |
840
|
0
|
|
|
|
|
|
case SQLITE_FULL: z = "database is full"; break; |
841
|
0
|
|
|
|
|
|
case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
842
|
0
|
|
|
|
|
|
case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
843
|
0
|
|
|
|
|
|
case SQLITE_EMPTY: z = "table contains no data"; break; |
844
|
0
|
|
|
|
|
|
case SQLITE_SCHEMA: z = "database schema has changed"; break; |
845
|
0
|
|
|
|
|
|
case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
846
|
0
|
|
|
|
|
|
case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
847
|
0
|
|
|
|
|
|
case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
848
|
0
|
|
|
|
|
|
case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
849
|
0
|
|
|
|
|
|
case SQLITE_NOLFS: z = "kernel lacks large file support"; break; |
850
|
0
|
|
|
|
|
|
case SQLITE_AUTH: z = "authorization denied"; break; |
851
|
0
|
|
|
|
|
|
case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
852
|
0
|
|
|
|
|
|
case SQLITE_RANGE: z = "bind index out of range"; break; |
853
|
0
|
|
|
|
|
|
case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
854
|
0
|
|
|
|
|
|
default: z = "unknown error"; break; |
855
|
|
|
|
|
|
|
} |
856
|
0
|
|
|
|
|
|
return z; |
857
|
|
|
|
|
|
|
} |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
/* |
860
|
|
|
|
|
|
|
** This routine implements a busy callback that sleeps and tries |
861
|
|
|
|
|
|
|
** again until a timeout value is reached. The timeout value is |
862
|
|
|
|
|
|
|
** an integer number of milliseconds passed in as the first |
863
|
|
|
|
|
|
|
** argument. |
864
|
|
|
|
|
|
|
*/ |
865
|
0
|
|
|
|
|
|
static int sqliteDefaultBusyCallback( |
866
|
|
|
|
|
|
|
void *Timeout, /* Maximum amount of time to wait */ |
867
|
|
|
|
|
|
|
const char *NotUsed, /* The name of the table that is busy */ |
868
|
|
|
|
|
|
|
int count /* Number of times table has been busy */ |
869
|
|
|
|
|
|
|
){ |
870
|
|
|
|
|
|
|
#if SQLITE_MIN_SLEEP_MS==1 |
871
|
|
|
|
|
|
|
static const char delays[] = |
872
|
|
|
|
|
|
|
{ 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100}; |
873
|
|
|
|
|
|
|
static const short int totals[] = |
874
|
|
|
|
|
|
|
{ 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287}; |
875
|
|
|
|
|
|
|
# define NDELAY (sizeof(delays)/sizeof(delays[0])) |
876
|
0
|
|
|
|
|
|
int timeout = (int)(long)Timeout; |
877
|
|
|
|
|
|
|
int delay, prior; |
878
|
|
|
|
|
|
|
|
879
|
0
|
0
|
|
|
|
|
if( count <= NDELAY ){ |
880
|
0
|
|
|
|
|
|
delay = delays[count-1]; |
881
|
0
|
|
|
|
|
|
prior = totals[count-1]; |
882
|
|
|
|
|
|
|
}else{ |
883
|
0
|
|
|
|
|
|
delay = delays[NDELAY-1]; |
884
|
0
|
|
|
|
|
|
prior = totals[NDELAY-1] + delay*(count-NDELAY-1); |
885
|
|
|
|
|
|
|
} |
886
|
0
|
0
|
|
|
|
|
if( prior + delay > timeout ){ |
887
|
0
|
|
|
|
|
|
delay = timeout - prior; |
888
|
0
|
0
|
|
|
|
|
if( delay<=0 ) return 0; |
889
|
|
|
|
|
|
|
} |
890
|
0
|
|
|
|
|
|
sqliteOsSleep(delay); |
891
|
0
|
|
|
|
|
|
return 1; |
892
|
|
|
|
|
|
|
#else |
893
|
|
|
|
|
|
|
int timeout = (int)(long)Timeout; |
894
|
|
|
|
|
|
|
if( (count+1)*1000 > timeout ){ |
895
|
|
|
|
|
|
|
return 0; |
896
|
|
|
|
|
|
|
} |
897
|
|
|
|
|
|
|
sqliteOsSleep(1000); |
898
|
|
|
|
|
|
|
return 1; |
899
|
|
|
|
|
|
|
#endif |
900
|
|
|
|
|
|
|
} |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
/* |
903
|
|
|
|
|
|
|
** This routine sets the busy callback for an Sqlite database to the |
904
|
|
|
|
|
|
|
** given callback function with the given argument. |
905
|
|
|
|
|
|
|
*/ |
906
|
25
|
|
|
|
|
|
void sqlite_busy_handler( |
907
|
|
|
|
|
|
|
sqlite *db, |
908
|
|
|
|
|
|
|
int (*xBusy)(void*,const char*,int), |
909
|
|
|
|
|
|
|
void *pArg |
910
|
|
|
|
|
|
|
){ |
911
|
25
|
|
|
|
|
|
db->xBusyCallback = xBusy; |
912
|
25
|
|
|
|
|
|
db->pBusyArg = pArg; |
913
|
25
|
|
|
|
|
|
} |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
916
|
|
|
|
|
|
|
/* |
917
|
|
|
|
|
|
|
** This routine sets the progress callback for an Sqlite database to the |
918
|
|
|
|
|
|
|
** given callback function with the given argument. The progress callback will |
919
|
|
|
|
|
|
|
** be invoked every nOps opcodes. |
920
|
|
|
|
|
|
|
*/ |
921
|
0
|
|
|
|
|
|
void sqlite_progress_handler( |
922
|
|
|
|
|
|
|
sqlite *db, |
923
|
|
|
|
|
|
|
int nOps, |
924
|
|
|
|
|
|
|
int (*xProgress)(void*), |
925
|
|
|
|
|
|
|
void *pArg |
926
|
|
|
|
|
|
|
){ |
927
|
0
|
0
|
|
|
|
|
if( nOps>0 ){ |
928
|
0
|
|
|
|
|
|
db->xProgress = xProgress; |
929
|
0
|
|
|
|
|
|
db->nProgressOps = nOps; |
930
|
0
|
|
|
|
|
|
db->pProgressArg = pArg; |
931
|
|
|
|
|
|
|
}else{ |
932
|
0
|
|
|
|
|
|
db->xProgress = 0; |
933
|
0
|
|
|
|
|
|
db->nProgressOps = 0; |
934
|
0
|
|
|
|
|
|
db->pProgressArg = 0; |
935
|
|
|
|
|
|
|
} |
936
|
0
|
|
|
|
|
|
} |
937
|
|
|
|
|
|
|
#endif |
938
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
/* |
941
|
|
|
|
|
|
|
** This routine installs a default busy handler that waits for the |
942
|
|
|
|
|
|
|
** specified number of milliseconds before returning 0. |
943
|
|
|
|
|
|
|
*/ |
944
|
25
|
|
|
|
|
|
void sqlite_busy_timeout(sqlite *db, int ms){ |
945
|
25
|
50
|
|
|
|
|
if( ms>0 ){ |
946
|
25
|
|
|
|
|
|
sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)(long)ms); |
947
|
|
|
|
|
|
|
}else{ |
948
|
0
|
|
|
|
|
|
sqlite_busy_handler(db, 0, 0); |
949
|
|
|
|
|
|
|
} |
950
|
25
|
|
|
|
|
|
} |
951
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
/* |
953
|
|
|
|
|
|
|
** Cause any pending operation to stop at its earliest opportunity. |
954
|
|
|
|
|
|
|
*/ |
955
|
0
|
|
|
|
|
|
void sqlite_interrupt(sqlite *db){ |
956
|
0
|
|
|
|
|
|
db->flags |= SQLITE_Interrupt; |
957
|
0
|
|
|
|
|
|
} |
958
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
/* |
960
|
|
|
|
|
|
|
** Windows systems should call this routine to free memory that |
961
|
|
|
|
|
|
|
** is returned in the in the errmsg parameter of sqlite_open() when |
962
|
|
|
|
|
|
|
** SQLite is a DLL. For some reason, it does not work to call free() |
963
|
|
|
|
|
|
|
** directly. |
964
|
|
|
|
|
|
|
** |
965
|
|
|
|
|
|
|
** Note that we need to call free() not sqliteFree() here, since every |
966
|
|
|
|
|
|
|
** string that is exported from SQLite should have already passed through |
967
|
|
|
|
|
|
|
** sqliteStrRealloc(). |
968
|
|
|
|
|
|
|
*/ |
969
|
9
|
|
|
|
|
|
void sqlite_freemem(void *p){ free(p); } |
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
/* |
972
|
|
|
|
|
|
|
** Windows systems need functions to call to return the sqlite_version |
973
|
|
|
|
|
|
|
** and sqlite_encoding strings since they are unable to access constants |
974
|
|
|
|
|
|
|
** within DLLs. |
975
|
|
|
|
|
|
|
*/ |
976
|
0
|
|
|
|
|
|
const char *sqlite_libversion(void){ return sqlite_version; } |
977
|
0
|
|
|
|
|
|
const char *sqlite_libencoding(void){ return sqlite_encoding; } |
978
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
/* |
980
|
|
|
|
|
|
|
** Create new user-defined functions. The sqlite_create_function() |
981
|
|
|
|
|
|
|
** routine creates a regular function and sqlite_create_aggregate() |
982
|
|
|
|
|
|
|
** creates an aggregate function. |
983
|
|
|
|
|
|
|
** |
984
|
|
|
|
|
|
|
** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments |
985
|
|
|
|
|
|
|
** disables the function. Calling sqlite_create_function() with the |
986
|
|
|
|
|
|
|
** same name and number of arguments as a prior call to |
987
|
|
|
|
|
|
|
** sqlite_create_aggregate() disables the prior call to |
988
|
|
|
|
|
|
|
** sqlite_create_aggregate(), and vice versa. |
989
|
|
|
|
|
|
|
** |
990
|
|
|
|
|
|
|
** If nArg is -1 it means that this function will accept any number |
991
|
|
|
|
|
|
|
** of arguments, including 0. The maximum allowed value of nArg is 127. |
992
|
|
|
|
|
|
|
*/ |
993
|
761
|
|
|
|
|
|
int sqlite_create_function( |
994
|
|
|
|
|
|
|
sqlite *db, /* Add the function to this database connection */ |
995
|
|
|
|
|
|
|
const char *zName, /* Name of the function to add */ |
996
|
|
|
|
|
|
|
int nArg, /* Number of arguments */ |
997
|
|
|
|
|
|
|
void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */ |
998
|
|
|
|
|
|
|
void *pUserData /* User data */ |
999
|
|
|
|
|
|
|
){ |
1000
|
|
|
|
|
|
|
FuncDef *p; |
1001
|
|
|
|
|
|
|
int nName; |
1002
|
761
|
50
|
|
|
|
|
if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1003
|
761
|
50
|
|
|
|
|
if( nArg<-1 || nArg>127 ) return 1; |
|
|
50
|
|
|
|
|
|
1004
|
761
|
|
|
|
|
|
nName = strlen(zName); |
1005
|
761
|
50
|
|
|
|
|
if( nName>255 ) return 1; |
1006
|
761
|
|
|
|
|
|
p = sqliteFindFunction(db, zName, nName, nArg, 1); |
1007
|
761
|
50
|
|
|
|
|
if( p==0 ) return 1; |
1008
|
761
|
|
|
|
|
|
p->xFunc = xFunc; |
1009
|
761
|
|
|
|
|
|
p->xStep = 0; |
1010
|
761
|
|
|
|
|
|
p->xFinalize = 0; |
1011
|
761
|
|
|
|
|
|
p->pUserData = pUserData; |
1012
|
761
|
|
|
|
|
|
return 0; |
1013
|
|
|
|
|
|
|
} |
1014
|
156
|
|
|
|
|
|
int sqlite_create_aggregate( |
1015
|
|
|
|
|
|
|
sqlite *db, /* Add the function to this database connection */ |
1016
|
|
|
|
|
|
|
const char *zName, /* Name of the function to add */ |
1017
|
|
|
|
|
|
|
int nArg, /* Number of arguments */ |
1018
|
|
|
|
|
|
|
void (*xStep)(sqlite_func*,int,const char**), /* The step function */ |
1019
|
|
|
|
|
|
|
void (*xFinalize)(sqlite_func*), /* The finalizer */ |
1020
|
|
|
|
|
|
|
void *pUserData /* User data */ |
1021
|
|
|
|
|
|
|
){ |
1022
|
|
|
|
|
|
|
FuncDef *p; |
1023
|
|
|
|
|
|
|
int nName; |
1024
|
156
|
50
|
|
|
|
|
if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1025
|
156
|
50
|
|
|
|
|
if( nArg<-1 || nArg>127 ) return 1; |
|
|
50
|
|
|
|
|
|
1026
|
156
|
|
|
|
|
|
nName = strlen(zName); |
1027
|
156
|
50
|
|
|
|
|
if( nName>255 ) return 1; |
1028
|
156
|
|
|
|
|
|
p = sqliteFindFunction(db, zName, nName, nArg, 1); |
1029
|
156
|
50
|
|
|
|
|
if( p==0 ) return 1; |
1030
|
156
|
|
|
|
|
|
p->xFunc = 0; |
1031
|
156
|
|
|
|
|
|
p->xStep = xStep; |
1032
|
156
|
|
|
|
|
|
p->xFinalize = xFinalize; |
1033
|
156
|
|
|
|
|
|
p->pUserData = pUserData; |
1034
|
156
|
|
|
|
|
|
return 0; |
1035
|
|
|
|
|
|
|
} |
1036
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
/* |
1038
|
|
|
|
|
|
|
** Change the datatype for all functions with a given name. See the |
1039
|
|
|
|
|
|
|
** header comment for the prototype of this function in sqlite.h for |
1040
|
|
|
|
|
|
|
** additional information. |
1041
|
|
|
|
|
|
|
*/ |
1042
|
800
|
|
|
|
|
|
int sqlite_function_type(sqlite *db, const char *zName, int dataType){ |
1043
|
800
|
|
|
|
|
|
FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName)); |
1044
|
1750
|
100
|
|
|
|
|
while( p ){ |
1045
|
950
|
|
|
|
|
|
p->dataType = dataType; |
1046
|
950
|
|
|
|
|
|
p = p->pNext; |
1047
|
|
|
|
|
|
|
} |
1048
|
800
|
|
|
|
|
|
return SQLITE_OK; |
1049
|
|
|
|
|
|
|
} |
1050
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
/* |
1052
|
|
|
|
|
|
|
** Register a trace function. The pArg from the previously registered trace |
1053
|
|
|
|
|
|
|
** is returned. |
1054
|
|
|
|
|
|
|
** |
1055
|
|
|
|
|
|
|
** A NULL trace function means that no tracing is executes. A non-NULL |
1056
|
|
|
|
|
|
|
** trace is a pointer to a function that is invoked at the start of each |
1057
|
|
|
|
|
|
|
** sqlite_exec(). |
1058
|
|
|
|
|
|
|
*/ |
1059
|
0
|
|
|
|
|
|
void *sqlite_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){ |
1060
|
0
|
|
|
|
|
|
void *pOld = db->pTraceArg; |
1061
|
0
|
|
|
|
|
|
db->xTrace = xTrace; |
1062
|
0
|
|
|
|
|
|
db->pTraceArg = pArg; |
1063
|
0
|
|
|
|
|
|
return pOld; |
1064
|
|
|
|
|
|
|
} |
1065
|
|
|
|
|
|
|
|
1066
|
|
|
|
|
|
|
/*** EXPERIMENTAL *** |
1067
|
|
|
|
|
|
|
** |
1068
|
|
|
|
|
|
|
** Register a function to be invoked when a transaction comments. |
1069
|
|
|
|
|
|
|
** If either function returns non-zero, then the commit becomes a |
1070
|
|
|
|
|
|
|
** rollback. |
1071
|
|
|
|
|
|
|
*/ |
1072
|
0
|
|
|
|
|
|
void *sqlite_commit_hook( |
1073
|
|
|
|
|
|
|
sqlite *db, /* Attach the hook to this database */ |
1074
|
|
|
|
|
|
|
int (*xCallback)(void*), /* Function to invoke on each commit */ |
1075
|
|
|
|
|
|
|
void *pArg /* Argument to the function */ |
1076
|
|
|
|
|
|
|
){ |
1077
|
0
|
|
|
|
|
|
void *pOld = db->pCommitArg; |
1078
|
0
|
|
|
|
|
|
db->xCommitCallback = xCallback; |
1079
|
0
|
|
|
|
|
|
db->pCommitArg = pArg; |
1080
|
0
|
|
|
|
|
|
return pOld; |
1081
|
|
|
|
|
|
|
} |
1082
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
/* |
1085
|
|
|
|
|
|
|
** This routine is called to create a connection to a database BTree |
1086
|
|
|
|
|
|
|
** driver. If zFilename is the name of a file, then that file is |
1087
|
|
|
|
|
|
|
** opened and used. If zFilename is the magic name ":memory:" then |
1088
|
|
|
|
|
|
|
** the database is stored in memory (and is thus forgotten as soon as |
1089
|
|
|
|
|
|
|
** the connection is closed.) If zFilename is NULL then the database |
1090
|
|
|
|
|
|
|
** is for temporary use only and is deleted as soon as the connection |
1091
|
|
|
|
|
|
|
** is closed. |
1092
|
|
|
|
|
|
|
** |
1093
|
|
|
|
|
|
|
** A temporary database can be either a disk file (that is automatically |
1094
|
|
|
|
|
|
|
** deleted when the file is closed) or a set of red-black trees held in memory, |
1095
|
|
|
|
|
|
|
** depending on the values of the TEMP_STORE compile-time macro and the |
1096
|
|
|
|
|
|
|
** db->temp_store variable, according to the following chart: |
1097
|
|
|
|
|
|
|
** |
1098
|
|
|
|
|
|
|
** TEMP_STORE db->temp_store Location of temporary database |
1099
|
|
|
|
|
|
|
** ---------- -------------- ------------------------------ |
1100
|
|
|
|
|
|
|
** 0 any file |
1101
|
|
|
|
|
|
|
** 1 1 file |
1102
|
|
|
|
|
|
|
** 1 2 memory |
1103
|
|
|
|
|
|
|
** 1 0 file |
1104
|
|
|
|
|
|
|
** 2 1 file |
1105
|
|
|
|
|
|
|
** 2 2 memory |
1106
|
|
|
|
|
|
|
** 2 0 memory |
1107
|
|
|
|
|
|
|
** 3 any memory |
1108
|
|
|
|
|
|
|
*/ |
1109
|
53
|
|
|
|
|
|
int sqliteBtreeFactory( |
1110
|
|
|
|
|
|
|
const sqlite *db, /* Main database when opening aux otherwise 0 */ |
1111
|
|
|
|
|
|
|
const char *zFilename, /* Name of the file containing the BTree database */ |
1112
|
|
|
|
|
|
|
int omitJournal, /* if TRUE then do not journal this file */ |
1113
|
|
|
|
|
|
|
int nCache, /* How many pages in the page cache */ |
1114
|
|
|
|
|
|
|
Btree **ppBtree){ /* Pointer to new Btree object written here */ |
1115
|
|
|
|
|
|
|
|
1116
|
|
|
|
|
|
|
assert( ppBtree != 0); |
1117
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_INMEMORYDB |
1119
|
53
|
100
|
|
|
|
|
if( zFilename==0 ){ |
1120
|
|
|
|
|
|
|
if (TEMP_STORE == 0) { |
1121
|
|
|
|
|
|
|
/* Always use file based temporary DB */ |
1122
|
|
|
|
|
|
|
return sqliteBtreeOpen(0, omitJournal, nCache, ppBtree); |
1123
|
|
|
|
|
|
|
} else if (TEMP_STORE == 1 || TEMP_STORE == 2) { |
1124
|
|
|
|
|
|
|
/* Switch depending on compile-time and/or runtime settings. */ |
1125
|
28
|
50
|
|
|
|
|
int location = db->temp_store==0 ? TEMP_STORE : db->temp_store; |
1126
|
|
|
|
|
|
|
|
1127
|
28
|
50
|
|
|
|
|
if (location == 1) { |
1128
|
28
|
|
|
|
|
|
return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree); |
1129
|
|
|
|
|
|
|
} else { |
1130
|
0
|
|
|
|
|
|
return sqliteRbtreeOpen(0, 0, 0, ppBtree); |
1131
|
|
|
|
|
|
|
} |
1132
|
|
|
|
|
|
|
} else { |
1133
|
|
|
|
|
|
|
/* Always use in-core DB */ |
1134
|
|
|
|
|
|
|
return sqliteRbtreeOpen(0, 0, 0, ppBtree); |
1135
|
|
|
|
|
|
|
} |
1136
|
25
|
50
|
|
|
|
|
}else if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){ |
|
|
0
|
|
|
|
|
|
1137
|
0
|
|
|
|
|
|
return sqliteRbtreeOpen(0, 0, 0, ppBtree); |
1138
|
|
|
|
|
|
|
}else |
1139
|
|
|
|
|
|
|
#endif |
1140
|
|
|
|
|
|
|
{ |
1141
|
25
|
|
|
|
|
|
return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree); |
1142
|
|
|
|
|
|
|
} |
1143
|
|
|
|
|
|
|
} |