wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | using System.IO; |
||
4 | using System.Text; |
||
5 | |||
6 | using i16 = System.Int16; |
||
7 | using u8 = System.Byte; |
||
8 | using u16 = System.UInt16; |
||
9 | using u32 = System.UInt32; |
||
10 | |||
11 | using Pgno = System.UInt32; |
||
12 | |||
13 | /* |
||
14 | ** The yDbMask datatype for the bitmask of all attached databases. |
||
15 | */ |
||
16 | #if SQLITE_MAX_ATTACHED//>30 |
||
17 | // typedef sqlite3_uint64 yDbMask; |
||
18 | using yDbMask = System.Int64; |
||
19 | #else |
||
20 | // typedef unsigned int yDbMask; |
||
21 | using yDbMask = System.Int32; |
||
22 | #endif |
||
23 | |||
24 | namespace Community.CsharpSqlite |
||
25 | { |
||
26 | public partial class Sqlite3 |
||
27 | { |
||
28 | /* |
||
29 | ** 2001 September 15 |
||
30 | ** |
||
31 | ** The author disclaims copyright to this source code. In place of |
||
32 | ** a legal notice, here is a blessing: |
||
33 | ** |
||
34 | ** May you do good and not evil. |
||
35 | ** May you find forgiveness for yourself and forgive others. |
||
36 | ** May you share freely, never taking more than you give. |
||
37 | ** |
||
38 | ************************************************************************* |
||
39 | ** This file contains C code routines that are called by the SQLite parser |
||
40 | ** when syntax rules are reduced. The routines in this file handle the |
||
41 | ** following kinds of SQL syntax: |
||
42 | ** |
||
43 | ** CREATE TABLE |
||
44 | ** DROP TABLE |
||
45 | ** CREATE INDEX |
||
46 | ** DROP INDEX |
||
47 | ** creating ID lists |
||
48 | ** BEGIN TRANSACTION |
||
49 | ** COMMIT |
||
50 | ** ROLLBACK |
||
51 | ************************************************************************* |
||
52 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
53 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
54 | ** |
||
55 | ** SQLITE_SOURCE_ID: 2011-06-23 19:49:22 4374b7e83ea0a3fbc3691f9c0c936272862f32f2 |
||
56 | ** |
||
57 | ************************************************************************* |
||
58 | */ |
||
59 | //#include "sqliteInt.h" |
||
60 | |||
61 | /* |
||
62 | ** This routine is called when a new SQL statement is beginning to |
||
63 | ** be parsed. Initialize the pParse structure as needed. |
||
64 | */ |
||
65 | static void sqlite3BeginParse( Parse pParse, int explainFlag ) |
||
66 | { |
||
67 | pParse.explain = (byte)explainFlag; |
||
68 | pParse.nVar = 0; |
||
69 | } |
||
70 | |||
71 | #if !SQLITE_OMIT_SHARED_CACHE |
||
72 | /* |
||
73 | ** The TableLock structure is only used by the sqlite3TableLock() and |
||
74 | ** codeTableLocks() functions. |
||
75 | */ |
||
76 | //struct TableLock { |
||
77 | // int iDb; /* The database containing the table to be locked */ |
||
78 | // int iTab; /* The root page of the table to be locked */ |
||
79 | // u8 isWriteLock; /* True for write lock. False for a read lock */ |
||
80 | // string zName; /* Name of the table */ |
||
81 | //}; |
||
82 | |||
83 | public class TableLock |
||
84 | { |
||
85 | public int iDb; /* The database containing the table to be locked */ |
||
86 | public int iTab; /* The root page of the table to be locked */ |
||
87 | public u8 isWriteLock; /* True for write lock. False for a read lock */ |
||
88 | public string zName; /* Name of the table */ |
||
89 | } |
||
90 | /* |
||
91 | ** Record the fact that we want to lock a table at run-time. |
||
92 | ** |
||
93 | ** The table to be locked has root page iTab and is found in database iDb. |
||
94 | ** A read or a write lock can be taken depending on isWritelock. |
||
95 | ** |
||
96 | ** This routine just records the fact that the lock is desired. The |
||
97 | ** code to make the lock occur is generated by a later call to |
||
98 | ** codeTableLocks() which occurs during sqlite3FinishCoding(). |
||
99 | */ |
||
100 | void sqlite3TableLock( |
||
101 | Parse *pParse, /* Parsing context */ |
||
102 | int iDb, /* Index of the database containing the table to lock */ |
||
103 | int iTab, /* Root page number of the table to be locked */ |
||
104 | u8 isWriteLock, /* True for a write lock */ |
||
105 | string zName /* Name of the table to be locked */ |
||
106 | ){ |
||
107 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
||
108 | int i; |
||
109 | int nBytes; |
||
110 | TableLock *p; |
||
111 | Debug.Assert( iDb>=0 ); |
||
112 | |||
113 | for(i=0; i<pToplevel->nTableLock; i++){ |
||
114 | p = pToplevel->aTableLock[i]; |
||
115 | if( p->iDb==iDb && p->iTab==iTab ){ |
||
116 | p->isWriteLock = (p->isWriteLock || isWriteLock); |
||
117 | return; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | nBytes = sizeof(vtableLock) * (pToplevel->nTableLock+1); |
||
122 | pToplevel->aTableLock = |
||
123 | sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); |
||
124 | if( pToplevel->aTableLock ){ |
||
125 | p = pToplevel->aTableLock[pToplevel->nTableLock++]; |
||
126 | p->iDb = iDb; |
||
127 | p->iTab = iTab; |
||
128 | p->isWriteLock = isWriteLock; |
||
129 | p->zName = zName; |
||
130 | }else{ |
||
131 | pToplevel->nTableLock = 0; |
||
132 | pToplevel->db->mallocFailed = 1; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /* |
||
137 | ** Code an OP_TableLock instruction for each table locked by the |
||
138 | ** statement (configured by calls to sqlite3TableLock()). |
||
139 | */ |
||
140 | static void codeTableLocks( Parse pParse ) |
||
141 | { |
||
142 | int i; |
||
143 | Vdbe pVdbe; |
||
144 | |||
145 | pVdbe = sqlite3GetVdbe( pParse ); |
||
146 | Debug.Assert( pVdbe != null ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ |
||
147 | |||
148 | for ( i = 0 ; i < pParse.nTableLock ; i++ ) |
||
149 | { |
||
150 | TableLock p = pParse.aTableLock[i]; |
||
151 | int p1 = p.iDb; |
||
152 | sqlite3VdbeAddOp4( pVdbe, OP_TableLock, p1, p.iTab, p.isWriteLock, |
||
153 | p.zName, P4_STATIC ); |
||
154 | } |
||
155 | } |
||
156 | #else |
||
157 | // #define codeTableLocks(x) |
||
158 | static void codeTableLocks( Parse pParse ) |
||
159 | { |
||
160 | } |
||
161 | #endif |
||
162 | |||
163 | /* |
||
164 | ** This routine is called after a single SQL statement has been |
||
165 | ** parsed and a VDBE program to execute that statement has been |
||
166 | ** prepared. This routine puts the finishing touches on the |
||
167 | ** VDBE program and resets the pParse structure for the next |
||
168 | ** parse. |
||
169 | ** |
||
170 | ** Note that if an error occurred, it might be the case that |
||
171 | ** no VDBE code was generated. |
||
172 | */ |
||
173 | static void sqlite3FinishCoding( Parse pParse ) |
||
174 | { |
||
175 | sqlite3 db; |
||
176 | Vdbe v; |
||
177 | |||
178 | db = pParse.db; |
||
179 | // if ( db.mallocFailed != 0 ) return; |
||
180 | if ( pParse.nested != 0 ) |
||
181 | return; |
||
182 | if ( pParse.nErr != 0 ) |
||
183 | return; |
||
184 | |||
185 | /* Begin by generating some termination code at the end of the |
||
186 | ** vdbe program |
||
187 | */ |
||
188 | v = sqlite3GetVdbe( pParse ); |
||
189 | Debug.Assert( 0 == pParse.isMultiWrite |
||
190 | #if SQLITE_DEBUG |
||
191 | || sqlite3VdbeAssertMayAbort( v, pParse.mayAbort ) != 0 |
||
192 | #endif |
||
193 | ); |
||
194 | if ( v != null ) |
||
195 | { |
||
196 | sqlite3VdbeAddOp0( v, OP_Halt ); |
||
197 | |||
198 | /* The cookie mask contains one bit for each database file open. |
||
199 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
||
200 | ** set for each database that is used. Generate code to start a |
||
201 | ** transaction on each used database and to verify the schema cookie |
||
202 | ** on each used database. |
||
203 | */ |
||
204 | if ( pParse.cookieGoto > 0 ) |
||
205 | { |
||
206 | u32 mask; |
||
207 | int iDb; |
||
208 | sqlite3VdbeJumpHere( v, pParse.cookieGoto - 1 ); |
||
209 | for ( iDb = 0, mask = 1; iDb < db.nDb; mask <<= 1, iDb++ ) |
||
210 | { |
||
211 | if ( ( mask & pParse.cookieMask ) == 0 ) |
||
212 | continue; |
||
213 | sqlite3VdbeUsesBtree( v, iDb ); |
||
214 | sqlite3VdbeAddOp2( v, OP_Transaction, iDb, ( mask & pParse.writeMask ) != 0 ); |
||
215 | if ( db.init.busy == 0 ) |
||
216 | { |
||
217 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
218 | sqlite3VdbeAddOp3( v, OP_VerifyCookie, |
||
219 | iDb, pParse.cookieValue[iDb], |
||
220 | (int)db.aDb[iDb].pSchema.iGeneration ); |
||
221 | } |
||
222 | } |
||
223 | #if !SQLITE_OMIT_VIRTUALTABLE |
||
224 | { |
||
225 | int i; |
||
226 | for ( i = 0; i < pParse.nVtabLock; i++ ) |
||
227 | { |
||
228 | VTable vtab = sqlite3GetVTable( db, pParse.apVtabLock[i] ); |
||
229 | sqlite3VdbeAddOp4( v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB ); |
||
230 | } |
||
231 | pParse.nVtabLock = 0; |
||
232 | } |
||
233 | #endif |
||
234 | |||
235 | /* Once all the cookies have been verified and transactions opened, |
||
236 | ** obtain the required table-locks. This is a no-op unless the |
||
237 | ** shared-cache feature is enabled. |
||
238 | */ |
||
239 | codeTableLocks( pParse ); |
||
240 | |||
241 | /* Initialize any AUTOINCREMENT data structures required. |
||
242 | */ |
||
243 | sqlite3AutoincrementBegin( pParse ); |
||
244 | |||
245 | /* Finally, jump back to the beginning of the executable code. */ |
||
246 | sqlite3VdbeAddOp2( v, OP_Goto, 0, pParse.cookieGoto ); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | |||
251 | /* Get the VDBE program ready for execution |
||
252 | */ |
||
253 | if ( v != null && ALWAYS( pParse.nErr == 0 ) /* && 0 == db.mallocFailed */ ) |
||
254 | { |
||
255 | #if SQLITE_DEBUG |
||
256 | TextWriter trace = ( db.flags & SQLITE_VdbeTrace ) != 0 ? Console.Out : null; |
||
257 | sqlite3VdbeTrace( v, trace ); |
||
258 | #endif |
||
259 | Debug.Assert( pParse.iCacheLevel == 0 ); /* Disables and re-enables match */ |
||
260 | /* A minimum of one cursor is required if autoincrement is used |
||
261 | * See ticket [a696379c1f08866] */ |
||
262 | if ( pParse.pAinc != null && pParse.nTab == 0 ) |
||
263 | pParse.nTab = 1; |
||
264 | sqlite3VdbeMakeReady( v, pParse); |
||
265 | pParse.rc = SQLITE_DONE; |
||
266 | pParse.colNamesSet = 0; |
||
267 | } |
||
268 | else |
||
269 | { |
||
270 | pParse.rc = SQLITE_ERROR; |
||
271 | } |
||
272 | pParse.nTab = 0; |
||
273 | pParse.nMem = 0; |
||
274 | pParse.nSet = 0; |
||
275 | pParse.nVar = 0; |
||
276 | pParse.cookieMask = 0; |
||
277 | pParse.cookieGoto = 0; |
||
278 | } |
||
279 | |||
280 | /* |
||
281 | ** Run the parser and code generator recursively in order to generate |
||
282 | ** code for the SQL statement given onto the end of the pParse context |
||
283 | ** currently under construction. When the parser is run recursively |
||
284 | ** this way, the final OP_Halt is not appended and other initialization |
||
285 | ** and finalization steps are omitted because those are handling by the |
||
286 | ** outermost parser. |
||
287 | ** |
||
288 | ** Not everything is nestable. This facility is designed to permit |
||
289 | ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use |
||
290 | ** care if you decide to try to use this routine for some other purposes. |
||
291 | */ |
||
292 | static void sqlite3NestedParse( Parse pParse, string zFormat, params object[] ap ) |
||
293 | { |
||
294 | string zSql; |
||
295 | string zErrMsg = string.Empty; |
||
296 | sqlite3 db = pParse.db; |
||
297 | |||
298 | //# define SAVE_SZ (Parse.Length - offsetof(Parse,nVar)) |
||
299 | // char saveBuf[SAVE_SZ]; |
||
300 | |||
301 | if ( pParse.nErr != 0 ) |
||
302 | return; |
||
303 | Debug.Assert( pParse.nested < 10 ); /* Nesting should only be of limited depth */ |
||
304 | // va_list ap; |
||
305 | lock ( lock_va_list ) |
||
306 | { |
||
307 | va_start( ap, zFormat ); |
||
308 | zSql = sqlite3VMPrintf( db, zFormat, ap ); |
||
309 | va_end( ref ap ); |
||
310 | } |
||
311 | lock ( nestingLock ) |
||
312 | { |
||
313 | pParse.nested++; |
||
314 | pParse.SaveMembers(); // memcpy(saveBuf, pParse.nVar, SAVE_SZ); |
||
315 | pParse.ResetMembers(); // memset(pParse.nVar, 0, SAVE_SZ); |
||
316 | sqlite3RunParser( pParse, zSql, ref zErrMsg ); |
||
317 | sqlite3DbFree( db, ref zErrMsg ); |
||
318 | sqlite3DbFree( db, ref zSql ); |
||
319 | pParse.RestoreMembers(); // memcpy(pParse.nVar, saveBuf, SAVE_SZ); |
||
320 | pParse.nested--; |
||
321 | } |
||
322 | } |
||
323 | static Object nestingLock = new Object(); |
||
324 | |||
325 | /* |
||
326 | ** Locate the in-memory structure that describes a particular database |
||
327 | ** table given the name of that table and (optionally) the name of the |
||
328 | ** database containing the table. Return NULL if not found. |
||
329 | ** |
||
330 | ** If zDatabase is 0, all databases are searched for the table and the |
||
331 | ** first matching table is returned. (No checking for duplicate table |
||
332 | ** names is done.) The search order is TEMP first, then MAIN, then any |
||
333 | ** auxiliary databases added using the ATTACH command. |
||
334 | ** |
||
335 | ** See also sqlite3LocateTable(). |
||
336 | */ |
||
337 | static Table sqlite3FindTable( sqlite3 db, string zName, string zDatabase ) |
||
338 | { |
||
339 | Table p = null; |
||
340 | int i; |
||
341 | int nName; |
||
342 | Debug.Assert( zName != null ); |
||
343 | nName = sqlite3Strlen30( zName ); |
||
344 | /* All mutexes are required for schema access. Make sure we hold them. */ |
||
345 | Debug.Assert( zDatabase != null || sqlite3BtreeHoldsAllMutexes( db ) ); |
||
346 | for ( i = OMIT_TEMPDB; i < db.nDb; i++ ) |
||
347 | { |
||
348 | int j = ( i < 2 ) ? i ^ 1 : i; /* Search TEMP before MAIN */ |
||
349 | if ( zDatabase != null && !zDatabase.Equals( db.aDb[j].zName, StringComparison.OrdinalIgnoreCase ) ) |
||
350 | continue; |
||
351 | Debug.Assert( sqlite3SchemaMutexHeld( db, j, null ) ); |
||
352 | p = sqlite3HashFind( db.aDb[j].pSchema.tblHash, zName, nName, (Table)null ); |
||
353 | if ( p != null ) |
||
354 | break; |
||
355 | } |
||
356 | return p; |
||
357 | } |
||
358 | |||
359 | /* |
||
360 | ** Locate the in-memory structure that describes a particular database |
||
361 | ** table given the name of that table and (optionally) the name of the |
||
362 | ** database containing the table. Return NULL if not found. Also leave an |
||
363 | ** error message in pParse.zErrMsg. |
||
364 | ** |
||
365 | ** The difference between this routine and sqlite3FindTable() is that this |
||
366 | ** routine leaves an error message in pParse.zErrMsg where |
||
367 | ** sqlite3FindTable() does not. |
||
368 | */ |
||
369 | static Table sqlite3LocateTable( |
||
370 | Parse pParse, /* context in which to report errors */ |
||
371 | int isView, /* True if looking for a VIEW rather than a TABLE */ |
||
372 | string zName, /* Name of the table we are looking for */ |
||
373 | string zDbase /* Name of the database. Might be NULL */ |
||
374 | ) |
||
375 | { |
||
376 | Table p; |
||
377 | |||
378 | /* Read the database schema. If an error occurs, leave an error message |
||
379 | ** and code in pParse and return NULL. */ |
||
380 | if ( SQLITE_OK != sqlite3ReadSchema( pParse ) ) |
||
381 | { |
||
382 | return null; |
||
383 | } |
||
384 | |||
385 | p = sqlite3FindTable( pParse.db, zName, zDbase ); |
||
386 | if ( p == null ) |
||
387 | { |
||
388 | string zMsg = isView != 0 ? "no such view" : "no such table"; |
||
389 | if ( zDbase != null ) |
||
390 | { |
||
391 | sqlite3ErrorMsg( pParse, "%s: %s.%s", zMsg, zDbase, zName ); |
||
392 | } |
||
393 | else |
||
394 | { |
||
395 | sqlite3ErrorMsg( pParse, "%s: %s", zMsg, zName ); |
||
396 | } |
||
397 | pParse.checkSchema = 1; |
||
398 | } |
||
399 | return p; |
||
400 | } |
||
401 | |||
402 | /* |
||
403 | ** Locate the in-memory structure that describes |
||
404 | ** a particular index given the name of that index |
||
405 | ** and the name of the database that contains the index. |
||
406 | ** Return NULL if not found. |
||
407 | ** |
||
408 | ** If zDatabase is 0, all databases are searched for the |
||
409 | ** table and the first matching index is returned. (No checking |
||
410 | ** for duplicate index names is done.) The search order is |
||
411 | ** TEMP first, then MAIN, then any auxiliary databases added |
||
412 | ** using the ATTACH command. |
||
413 | */ |
||
414 | static Index sqlite3FindIndex( sqlite3 db, string zName, string zDb ) |
||
415 | { |
||
416 | Index p = null; |
||
417 | int i; |
||
418 | int nName = sqlite3Strlen30( zName ); |
||
419 | /* All mutexes are required for schema access. Make sure we hold them. */ |
||
420 | Debug.Assert( zDb != null || sqlite3BtreeHoldsAllMutexes( db ) ); |
||
421 | for ( i = OMIT_TEMPDB; i < db.nDb; i++ ) |
||
422 | { |
||
423 | int j = ( i < 2 ) ? i ^ 1 : i; /* Search TEMP before MAIN */ |
||
424 | Schema pSchema = db.aDb[j].pSchema; |
||
425 | Debug.Assert( pSchema != null ); |
||
426 | if ( zDb != null && !zDb.Equals( db.aDb[j].zName, StringComparison.OrdinalIgnoreCase ) ) |
||
427 | continue; |
||
428 | Debug.Assert( sqlite3SchemaMutexHeld( db, j, null ) ); |
||
429 | p = sqlite3HashFind( pSchema.idxHash, zName, nName, (Index)null ); |
||
430 | if ( p != null ) |
||
431 | break; |
||
432 | } |
||
433 | return p; |
||
434 | } |
||
435 | |||
436 | /* |
||
437 | ** Reclaim the memory used by an index |
||
438 | */ |
||
439 | static void freeIndex( sqlite3 db, ref Index p ) |
||
440 | { |
||
441 | #if !SQLITE_OMIT_ANALYZE |
||
442 | sqlite3DeleteIndexSamples( db, p ); |
||
443 | #endif |
||
444 | sqlite3DbFree( db, ref p.zColAff ); |
||
445 | sqlite3DbFree( db, ref p ); |
||
446 | } |
||
447 | |||
448 | /* |
||
449 | ** For the index called zIdxName which is found in the database iDb, |
||
450 | ** unlike that index from its Table then remove the index from |
||
451 | ** the index hash table and free all memory structures associated |
||
452 | ** with the index. |
||
453 | */ |
||
454 | static void sqlite3UnlinkAndDeleteIndex( sqlite3 db, int iDb, string zIdxName ) |
||
455 | { |
||
456 | Index pIndex; |
||
457 | int len; |
||
458 | Hash pHash; |
||
459 | |||
460 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
461 | pHash = db.aDb[iDb].pSchema.idxHash; |
||
462 | |||
463 | len = sqlite3Strlen30( zIdxName ); |
||
464 | pIndex = sqlite3HashInsert( ref pHash, zIdxName, len, (Index)null ); |
||
465 | if ( ALWAYS(pIndex) ) |
||
466 | { |
||
467 | if ( pIndex.pTable.pIndex == pIndex ) |
||
468 | { |
||
469 | pIndex.pTable.pIndex = pIndex.pNext; |
||
470 | } |
||
471 | else |
||
472 | { |
||
473 | Index p; |
||
474 | /* Justification of ALWAYS(); The index must be on the list of |
||
475 | ** indices. */ |
||
476 | p = pIndex.pTable.pIndex; |
||
477 | while ( ALWAYS( p != null ) && p.pNext != pIndex ) |
||
478 | { |
||
479 | p = p.pNext; |
||
480 | } |
||
481 | if ( ALWAYS( p != null && p.pNext == pIndex ) ) |
||
482 | { |
||
483 | p.pNext = pIndex.pNext; |
||
484 | } |
||
485 | } |
||
486 | freeIndex( db, ref pIndex ); |
||
487 | } |
||
488 | db.flags |= SQLITE_InternChanges; |
||
489 | } |
||
490 | |||
491 | /* |
||
492 | ** Erase all schema information from the in-memory hash tables of |
||
493 | ** a single database. This routine is called to reclaim memory |
||
494 | ** before the database closes. It is also called during a rollback |
||
495 | ** if there were schema changes during the transaction or if a |
||
496 | ** schema-cookie mismatch occurs. |
||
497 | ** |
||
498 | ** If iDb<0 then reset the internal schema tables for all database |
||
499 | ** files. If iDb>=0 then reset the internal schema for only the |
||
500 | ** single file indicated. |
||
501 | */ |
||
502 | static void sqlite3ResetInternalSchema( sqlite3 db, int iDb ) |
||
503 | { |
||
504 | int i, j; |
||
505 | Debug.Assert( iDb < db.nDb ); |
||
506 | |||
507 | if ( iDb >= 0 ) |
||
508 | { |
||
509 | /* Case 1: Reset the single schema identified by iDb */ |
||
510 | Db pDb = db.aDb[iDb]; |
||
511 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
512 | Debug.Assert( pDb.pSchema != null); |
||
513 | sqlite3SchemaClear( pDb.pSchema ); |
||
514 | |||
515 | /* If any database other than TEMP is reset, then also reset TEMP |
||
516 | ** since TEMP might be holding triggers that reference tables in the |
||
517 | ** other database. |
||
518 | */ |
||
519 | if ( iDb != 1 ) |
||
520 | { |
||
521 | pDb = db.aDb[1]; |
||
522 | Debug.Assert( pDb.pSchema != null ); |
||
523 | sqlite3SchemaClear( pDb.pSchema ); |
||
524 | } |
||
525 | return; |
||
526 | } |
||
527 | /* Case 2 (from here to the end): Reset all schemas for all attached |
||
528 | ** databases. */ |
||
529 | Debug.Assert( iDb < 0 ); |
||
530 | sqlite3BtreeEnterAll( db ); |
||
531 | for ( i = 0; i < db.nDb; i++ ) |
||
532 | { |
||
533 | Db pDb = db.aDb[i]; |
||
534 | if ( pDb.pSchema != null ) |
||
535 | { |
||
536 | sqlite3SchemaClear( pDb.pSchema ); |
||
537 | } |
||
538 | } |
||
539 | db.flags &= ~SQLITE_InternChanges; |
||
540 | sqlite3VtabUnlockList( db ); |
||
541 | sqlite3BtreeLeaveAll( db ); |
||
542 | /* If one or more of the auxiliary database files has been closed, |
||
543 | ** then remove them from the auxiliary database list. We take the |
||
544 | ** opportunity to do this here since we have just deleted all of the |
||
545 | ** schema hash tables and therefore do not have to make any changes |
||
546 | ** to any of those tables. |
||
547 | */ |
||
548 | for ( i = j = 2; i < db.nDb; i++ ) |
||
549 | { |
||
550 | Db pDb = db.aDb[i]; |
||
551 | if ( pDb.pBt == null ) |
||
552 | { |
||
553 | sqlite3DbFree( db, ref pDb.zName ); |
||
554 | continue; |
||
555 | } |
||
556 | if ( j < i ) |
||
557 | { |
||
558 | db.aDb[j] = db.aDb[i]; |
||
559 | } |
||
560 | j++; |
||
561 | } |
||
562 | if ( db.nDb != j ) |
||
563 | db.aDb[j] = new Db();//memset(db.aDb[j], 0, (db.nDb-j)*sizeof(db.aDb[j])); |
||
564 | db.nDb = j; |
||
565 | if ( db.nDb <= 2 && db.aDb != db.aDbStatic ) |
||
566 | { |
||
567 | Array.Copy( db.aDb, db.aDbStatic, 2 );// memcpy(db.aDbStatic, db.aDb, 2*sizeof(db.aDb[0])); |
||
568 | //sqlite3DbFree( db, ref db.aDb ); |
||
569 | //db.aDb = db.aDbStatic; |
||
570 | } |
||
571 | } |
||
572 | |||
573 | /* |
||
574 | ** This routine is called when a commit occurs. |
||
575 | */ |
||
576 | static void sqlite3CommitInternalChanges( sqlite3 db ) |
||
577 | { |
||
578 | db.flags &= ~SQLITE_InternChanges; |
||
579 | } |
||
580 | |||
581 | /* |
||
582 | ** Delete memory allocated for the column names of a table or view (the |
||
583 | ** Table.aCol[] array). |
||
584 | */ |
||
585 | static void sqliteDeleteColumnNames( sqlite3 db, Table pTable ) |
||
586 | { |
||
587 | int i; |
||
588 | Column pCol; |
||
589 | Debug.Assert( pTable != null ); |
||
590 | for ( i = 0; i < pTable.nCol; i++ ) |
||
591 | { |
||
592 | pCol = pTable.aCol[i]; |
||
593 | if ( pCol != null ) |
||
594 | { |
||
595 | sqlite3DbFree( db, ref pCol.zName ); |
||
596 | sqlite3ExprDelete( db, ref pCol.pDflt ); |
||
597 | sqlite3DbFree( db, ref pCol.zDflt ); |
||
598 | sqlite3DbFree( db, ref pCol.zType ); |
||
599 | sqlite3DbFree( db, ref pCol.zColl ); |
||
600 | } |
||
601 | } |
||
602 | } |
||
603 | |||
604 | /* |
||
605 | ** Remove the memory data structures associated with the given |
||
606 | ** Table. No changes are made to disk by this routine. |
||
607 | ** |
||
608 | ** This routine just deletes the data structure. It does not unlink |
||
609 | ** the table data structure from the hash table. But it does destroy |
||
610 | ** memory structures of the indices and foreign keys associated with |
||
611 | ** the table. |
||
612 | */ |
||
613 | static void sqlite3DeleteTable( sqlite3 db, ref Table pTable ) |
||
614 | { |
||
615 | Index pIndex; |
||
616 | Index pNext; |
||
617 | |||
618 | Debug.Assert( null == pTable || pTable.nRef > 0 ); |
||
619 | |||
620 | /* Do not delete the table until the reference count reaches zero. */ |
||
621 | if ( null == pTable ) |
||
622 | return; |
||
623 | if ( (// ( !db || db->pnBytesFreed == 0 ) && |
||
624 | ( --pTable.nRef ) > 0 ) ) |
||
625 | return; |
||
626 | |||
627 | /* Delete all indices associated with this table. */ |
||
628 | for ( pIndex = pTable.pIndex; pIndex != null; pIndex = pNext ) |
||
629 | { |
||
630 | pNext = pIndex.pNext; |
||
631 | Debug.Assert( pIndex.pSchema == pTable.pSchema ); |
||
632 | //if( null==db || db.pnBytesFreed==0 ){ |
||
633 | string zName = pIndex.zName; |
||
634 | // |
||
635 | #if !NDEBUG || SQLITE_COVERAGE_TEST |
||
636 | // TESTONLY ( Index pOld = ) sqlite3HashInsert( |
||
637 | //ref pIndex.pSchema.idxHash, zName, sqlite3Strlen30(zName), 0 |
||
638 | // ); |
||
639 | Index pOld = sqlite3HashInsert( |
||
640 | ref pIndex.pSchema.idxHash, zName, sqlite3Strlen30( zName ), (Index)null |
||
641 | ); |
||
642 | Debug.Assert( db == null || sqlite3SchemaMutexHeld( db, 0, pIndex.pSchema ) ); |
||
643 | Debug.Assert( pOld == pIndex || pOld == null ); |
||
644 | #else |
||
645 | // TESTONLY ( Index pOld = ) sqlite3HashInsert( |
||
646 | //ref pIndex.pSchema.idxHash, zName, sqlite3Strlen30(zName), 0 |
||
647 | // ); |
||
648 | sqlite3HashInsert( |
||
649 | ref pIndex.pSchema.idxHash, zName, sqlite3Strlen30(zName),(Index)null |
||
650 | ); |
||
651 | #endif |
||
652 | //} |
||
653 | freeIndex( db, ref pIndex ); |
||
654 | } |
||
655 | |||
656 | /* Delete any foreign keys attached to this table. */ |
||
657 | sqlite3FkDelete( db, pTable ); |
||
658 | |||
659 | /* Delete the Table structure itself. |
||
660 | */ |
||
661 | sqliteDeleteColumnNames( db, pTable ); |
||
662 | sqlite3DbFree( db, ref pTable.zName ); |
||
663 | sqlite3DbFree( db, ref pTable.zColAff ); |
||
664 | sqlite3SelectDelete( db, ref pTable.pSelect ); |
||
665 | #if !SQLITE_OMIT_CHECK |
||
666 | sqlite3ExprDelete( db, ref pTable.pCheck ); |
||
667 | #endif |
||
668 | #if !SQLITE_OMIT_VIRTUALTABLE |
||
669 | sqlite3VtabClear(db, pTable); |
||
670 | #endif |
||
671 | pTable = null;// sqlite3DbFree( db, ref pTable ); |
||
672 | } |
||
673 | |||
674 | /* |
||
675 | ** Unlink the given table from the hash tables and the delete the |
||
676 | ** table structure with all its indices and foreign keys. |
||
677 | */ |
||
678 | static void sqlite3UnlinkAndDeleteTable( sqlite3 db, int iDb, string zTabName ) |
||
679 | { |
||
680 | Table p; |
||
681 | Db pDb; |
||
682 | |||
683 | Debug.Assert( db != null ); |
||
684 | Debug.Assert( iDb >= 0 && iDb < db.nDb ); |
||
685 | Debug.Assert( zTabName != null ); |
||
686 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
687 | testcase( zTabName.Length == 0 ); /* Zero-length table names are allowed */ |
||
688 | pDb = db.aDb[iDb]; |
||
689 | p = sqlite3HashInsert( ref pDb.pSchema.tblHash, zTabName, |
||
690 | sqlite3Strlen30( zTabName ), (Table)null ); |
||
691 | sqlite3DeleteTable( db, ref p ); |
||
692 | db.flags |= SQLITE_InternChanges; |
||
693 | } |
||
694 | |||
695 | /* |
||
696 | ** Given a token, return a string that consists of the text of that |
||
697 | ** token. Space to hold the returned string |
||
698 | ** is obtained from sqliteMalloc() and must be freed by the calling |
||
699 | ** function. |
||
700 | ** |
||
701 | ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
||
702 | ** surround the body of the token are removed. |
||
703 | ** |
||
704 | ** Tokens are often just pointers into the original SQL text and so |
||
705 | ** are not \000 terminated and are not persistent. The returned string |
||
706 | ** is \000 terminated and is persistent. |
||
707 | */ |
||
708 | static string sqlite3NameFromToken( sqlite3 db, Token pName ) |
||
709 | { |
||
710 | string zName; |
||
711 | if ( pName != null && pName.z != null ) |
||
712 | { |
||
713 | zName = pName.z.Substring( 0, pName.n );//sqlite3DbStrNDup(db, (char)pName.z, pName.n); |
||
714 | sqlite3Dequote( ref zName ); |
||
715 | } |
||
716 | else |
||
717 | { |
||
718 | return null; |
||
719 | } |
||
720 | return zName; |
||
721 | } |
||
722 | |||
723 | /* |
||
724 | ** Open the sqlite_master table stored in database number iDb for |
||
725 | ** writing. The table is opened using cursor 0. |
||
726 | */ |
||
727 | static void sqlite3OpenMasterTable( Parse p, int iDb ) |
||
728 | { |
||
729 | Vdbe v = sqlite3GetVdbe( p ); |
||
730 | sqlite3TableLock( p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE( iDb ) ); |
||
731 | sqlite3VdbeAddOp3( v, OP_OpenWrite, 0, MASTER_ROOT, iDb ); |
||
732 | sqlite3VdbeChangeP4( v, -1, (int)5, P4_INT32 ); /* 5 column table */ |
||
733 | if ( p.nTab == 0 ) |
||
734 | { |
||
735 | p.nTab = 1; |
||
736 | } |
||
737 | } |
||
738 | |||
739 | /* |
||
740 | ** Parameter zName points to a nul-terminated buffer containing the name |
||
741 | ** of a database ("main", "temp" or the name of an attached db). This |
||
742 | ** function returns the index of the named database in db->aDb[], or |
||
743 | ** -1 if the named db cannot be found. |
||
744 | */ |
||
745 | static int sqlite3FindDbName( sqlite3 db, string zName ) |
||
746 | { |
||
747 | int i = -1; /* Database number */ |
||
748 | if ( zName != null ) |
||
749 | { |
||
750 | Db pDb; |
||
751 | int n = sqlite3Strlen30( zName ); |
||
752 | for ( i = ( db.nDb - 1 ); i >= 0; i-- ) |
||
753 | { |
||
754 | pDb = db.aDb[i]; |
||
755 | if ( ( OMIT_TEMPDB == 0 || i != 1 ) && n == sqlite3Strlen30( pDb.zName ) && |
||
756 | pDb.zName.Equals( zName, StringComparison.OrdinalIgnoreCase ) ) |
||
757 | { |
||
758 | break; |
||
759 | } |
||
760 | } |
||
761 | } |
||
762 | return i; |
||
763 | } |
||
764 | |||
765 | /* |
||
766 | ** The token *pName contains the name of a database (either "main" or |
||
767 | ** "temp" or the name of an attached db). This routine returns the |
||
768 | ** index of the named database in db->aDb[], or -1 if the named db |
||
769 | ** does not exist. |
||
770 | */ |
||
771 | static int sqlite3FindDb( sqlite3 db, Token pName ) |
||
772 | { |
||
773 | int i; /* Database number */ |
||
774 | string zName; /* Name we are searching for */ |
||
775 | zName = sqlite3NameFromToken( db, pName ); |
||
776 | i = sqlite3FindDbName( db, zName ); |
||
777 | sqlite3DbFree( db, ref zName ); |
||
778 | return i; |
||
779 | } |
||
780 | |||
781 | /* The table or view or trigger name is passed to this routine via tokens |
||
782 | ** pName1 and pName2. If the table name was fully qualified, for example: |
||
783 | ** |
||
784 | ** CREATE TABLE xxx.yyy (...); |
||
785 | ** |
||
786 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
||
787 | ** the table name is not fully qualified, i.e.: |
||
788 | ** |
||
789 | ** CREATE TABLE yyy(...); |
||
790 | ** |
||
791 | ** Then pName1 is set to "yyy" and pName2 is "". |
||
792 | ** |
||
793 | ** This routine sets the ppUnqual pointer to point at the token (pName1 or |
||
794 | ** pName2) that stores the unqualified table name. The index of the |
||
795 | ** database "xxx" is returned. |
||
796 | */ |
||
797 | static int sqlite3TwoPartName( |
||
798 | Parse pParse, /* Parsing and code generating context */ |
||
799 | Token pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
||
800 | Token pName2, /* The "yyy" in the name "xxx.yyy" */ |
||
801 | ref Token pUnqual /* Write the unqualified object name here */ |
||
802 | ) |
||
803 | { |
||
804 | int iDb; /* Database holding the object */ |
||
805 | sqlite3 db = pParse.db; |
||
806 | |||
807 | if ( ALWAYS( pName2 != null ) && pName2.n > 0 ) |
||
808 | { |
||
809 | if ( db.init.busy != 0 ) |
||
810 | { |
||
811 | sqlite3ErrorMsg( pParse, "corrupt database" ); |
||
812 | pParse.nErr++; |
||
813 | return -1; |
||
814 | } |
||
815 | pUnqual = pName2; |
||
816 | iDb = sqlite3FindDb( db, pName1 ); |
||
817 | if ( iDb < 0 ) |
||
818 | { |
||
819 | sqlite3ErrorMsg( pParse, "unknown database %T", pName1 ); |
||
820 | pParse.nErr++; |
||
821 | return -1; |
||
822 | } |
||
823 | } |
||
824 | else |
||
825 | { |
||
826 | Debug.Assert( db.init.iDb == 0 || db.init.busy != 0 ); |
||
827 | iDb = db.init.iDb; |
||
828 | pUnqual = pName1; |
||
829 | } |
||
830 | return iDb; |
||
831 | } |
||
832 | |||
833 | /* |
||
834 | ** This routine is used to check if the UTF-8 string zName is a legal |
||
835 | ** unqualified name for a new schema object (vtable, index, view or |
||
836 | ** trigger). All names are legal except those that begin with the string |
||
837 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
||
838 | ** is reserved for internal use. |
||
839 | */ |
||
840 | static int sqlite3CheckObjectName( Parse pParse, string zName ) |
||
841 | { |
||
842 | if ( 0 == pParse.db.init.busy && pParse.nested == 0 |
||
843 | && ( pParse.db.flags & SQLITE_WriteSchema ) == 0 |
||
844 | && zName.StartsWith( "sqlite_", System.StringComparison.OrdinalIgnoreCase ) ) |
||
845 | { |
||
846 | sqlite3ErrorMsg( pParse, "object name reserved for internal use: %s", zName ); |
||
847 | return SQLITE_ERROR; |
||
848 | } |
||
849 | return SQLITE_OK; |
||
850 | } |
||
851 | |||
852 | /* |
||
853 | ** Begin constructing a new table representation in memory. This is |
||
854 | ** the first of several action routines that get called in response |
||
855 | ** to a CREATE TABLE statement. In particular, this routine is called |
||
856 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
||
857 | ** flag is true if the table should be stored in the auxiliary database |
||
858 | ** file instead of in the main database file. This is normally the case |
||
859 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
||
860 | ** CREATE and TABLE. |
||
861 | ** |
||
862 | ** The new table record is initialized and put in pParse.pNewTable. |
||
863 | ** As more of the CREATE TABLE statement is parsed, additional action |
||
864 | ** routines will be called to add more information to this record. |
||
865 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
||
866 | ** is called to complete the construction of the new table record. |
||
867 | */ |
||
868 | static void sqlite3StartTable( |
||
869 | Parse pParse, /* Parser context */ |
||
870 | Token pName1, /* First part of the name of the table or view */ |
||
871 | Token pName2, /* Second part of the name of the table or view */ |
||
872 | int isTemp, /* True if this is a TEMP table */ |
||
873 | int isView, /* True if this is a VIEW */ |
||
874 | int isVirtual, /* True if this is a VIRTUAL table */ |
||
875 | int noErr /* Do nothing if table already exists */ |
||
876 | ) |
||
877 | { |
||
878 | Table pTable; |
||
879 | string zName = null; /* The name of the new table */ |
||
880 | sqlite3 db = pParse.db; |
||
881 | Vdbe v; |
||
882 | int iDb; /* Database number to create the table in */ |
||
883 | Token pName = new Token(); /* Unqualified name of the table to create */ |
||
884 | |||
885 | /* The table or view name to create is passed to this routine via tokens |
||
886 | ** pName1 and pName2. If the table name was fully qualified, for example: |
||
887 | ** |
||
888 | ** CREATE TABLE xxx.yyy (...); |
||
889 | ** |
||
890 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
||
891 | ** the table name is not fully qualified, i.e.: |
||
892 | ** |
||
893 | ** CREATE TABLE yyy(...); |
||
894 | ** |
||
895 | ** Then pName1 is set to "yyy" and pName2 is "". |
||
896 | ** |
||
897 | ** The call below sets the pName pointer to point at the token (pName1 or |
||
898 | ** pName2) that stores the unqualified table name. The variable iDb is |
||
899 | ** set to the index of the database that the table or view is to be |
||
900 | ** created in. |
||
901 | */ |
||
902 | iDb = sqlite3TwoPartName( pParse, pName1, pName2, ref pName ); |
||
903 | if ( iDb < 0 ) |
||
904 | return; |
||
905 | if ( 0 == OMIT_TEMPDB && isTemp != 0 && pName2.n > 0 && iDb != 1 ) |
||
906 | { |
||
907 | /* If creating a temp table, the name may not be qualified. Unless |
||
908 | ** the database name is "temp" anyway. */ |
||
909 | |||
910 | sqlite3ErrorMsg( pParse, "temporary table name must be unqualified" ); |
||
911 | return; |
||
912 | } |
||
913 | if ( OMIT_TEMPDB == 0 && isTemp != 0 ) |
||
914 | iDb = 1; |
||
915 | |||
916 | pParse.sNameToken = pName; |
||
917 | zName = sqlite3NameFromToken( db, pName ); |
||
918 | if ( zName == null ) |
||
919 | return; |
||
920 | if ( SQLITE_OK != sqlite3CheckObjectName( pParse, zName ) ) |
||
921 | { |
||
922 | goto begin_table_error; |
||
923 | } |
||
924 | if ( db.init.iDb == 1 ) |
||
925 | isTemp = 1; |
||
926 | #if !SQLITE_OMIT_AUTHORIZATION |
||
927 | Debug.Assert( (isTemp & 1)==isTemp ); |
||
928 | { |
||
929 | int code; |
||
930 | string zDb = db.aDb[iDb].zName; |
||
931 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
||
932 | goto begin_table_error; |
||
933 | } |
||
934 | if( isView ){ |
||
935 | if( OMIT_TEMPDB ==0&& isTemp ){ |
||
936 | code = SQLITE_CREATE_TEMP_VIEW; |
||
937 | }else{ |
||
938 | code = SQLITE_CREATE_VIEW; |
||
939 | } |
||
940 | }else{ |
||
941 | if( OMIT_TEMPDB ==0&& isTemp ){ |
||
942 | code = SQLITE_CREATE_TEMP_TABLE; |
||
943 | }else{ |
||
944 | code = SQLITE_CREATE_TABLE; |
||
945 | } |
||
946 | } |
||
947 | if( null==isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ |
||
948 | goto begin_table_error; |
||
949 | } |
||
950 | } |
||
951 | #endif |
||
952 | |||
953 | /* Make sure the new table name does not collide with an existing |
||
954 | ** index or table name in the same database. Issue an error message if |
||
955 | ** it does. The exception is if the statement being parsed was passed |
||
956 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
||
957 | ** and types will be used, so there is no need to test for namespace |
||
958 | ** collisions. |
||
959 | */ |
||
960 | if ( !IN_DECLARE_VTAB( pParse ) ) |
||
961 | { |
||
962 | String zDb = db.aDb[iDb].zName; |
||
963 | if ( SQLITE_OK != sqlite3ReadSchema( pParse ) ) |
||
964 | { |
||
965 | goto begin_table_error; |
||
966 | } |
||
967 | pTable = sqlite3FindTable( db, zName, zDb ); |
||
968 | if ( pTable != null ) |
||
969 | { |
||
970 | if ( noErr == 0 ) |
||
971 | { |
||
972 | sqlite3ErrorMsg( pParse, "table %T already exists", pName ); |
||
973 | } |
||
974 | else |
||
975 | { |
||
976 | Debug.Assert( 0 == db.init.busy ); |
||
977 | sqlite3CodeVerifySchema( pParse, iDb ); |
||
978 | } |
||
979 | goto begin_table_error; |
||
980 | } |
||
981 | if ( sqlite3FindIndex( db, zName, zDb ) != null ) |
||
982 | { |
||
983 | sqlite3ErrorMsg( pParse, "there is already an index named %s", zName ); |
||
984 | goto begin_table_error; |
||
985 | } |
||
986 | } |
||
987 | |||
988 | pTable = new Table();// sqlite3DbMallocZero(db, Table).Length; |
||
989 | //if ( pTable == null ) |
||
990 | //{ |
||
991 | // db.mallocFailed = 1; |
||
992 | // pParse.rc = SQLITE_NOMEM; |
||
993 | // pParse.nErr++; |
||
994 | // goto begin_table_error; |
||
995 | //} |
||
996 | pTable.zName = zName; |
||
997 | pTable.iPKey = -1; |
||
998 | pTable.pSchema = db.aDb[iDb].pSchema; |
||
999 | pTable.nRef = 1; |
||
1000 | pTable.nRowEst = 1000000; |
||
1001 | Debug.Assert( pParse.pNewTable == null ); |
||
1002 | pParse.pNewTable = pTable; |
||
1003 | |||
1004 | /* If this is the magic sqlite_sequence table used by autoincrement, |
||
1005 | ** then record a pointer to this table in the main database structure |
||
1006 | ** so that INSERT can find the table easily. |
||
1007 | */ |
||
1008 | #if !SQLITE_OMIT_AUTOINCREMENT |
||
1009 | if ( pParse.nested == 0 && zName == "sqlite_sequence" ) |
||
1010 | { |
||
1011 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
1012 | pTable.pSchema.pSeqTab = pTable; |
||
1013 | } |
||
1014 | #endif |
||
1015 | |||
1016 | /* Begin generating the code that will insert the table record into |
||
1017 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
||
1018 | ** and allocate the record number for the table entry now. Before any |
||
1019 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
||
1020 | ** indices to be created and the table record must come before the |
||
1021 | ** indices. Hence, the record number for the table must be allocated |
||
1022 | ** now. |
||
1023 | */ |
||
1024 | if ( 0 == db.init.busy && ( v = sqlite3GetVdbe( pParse ) ) != null ) |
||
1025 | { |
||
1026 | int j1; |
||
1027 | int fileFormat; |
||
1028 | int reg1, reg2, reg3; |
||
1029 | sqlite3BeginWriteOperation( pParse, 0, iDb ); |
||
1030 | |||
1031 | if ( isVirtual != 0 ) |
||
1032 | { |
||
1033 | sqlite3VdbeAddOp0( v, OP_VBegin ); |
||
1034 | } |
||
1035 | |||
1036 | /* If the file format and encoding in the database have not been set, |
||
1037 | ** set them now. |
||
1038 | */ |
||
1039 | reg1 = pParse.regRowid = ++pParse.nMem; |
||
1040 | reg2 = pParse.regRoot = ++pParse.nMem; |
||
1041 | reg3 = ++pParse.nMem; |
||
1042 | sqlite3VdbeAddOp3( v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT ); |
||
1043 | sqlite3VdbeUsesBtree( v, iDb ); |
||
1044 | j1 = sqlite3VdbeAddOp1( v, OP_If, reg3 ); |
||
1045 | fileFormat = ( db.flags & SQLITE_LegacyFileFmt ) != 0 ? |
||
1046 | 1 : SQLITE_MAX_FILE_FORMAT; |
||
1047 | sqlite3VdbeAddOp2( v, OP_Integer, fileFormat, reg3 ); |
||
1048 | sqlite3VdbeAddOp3( v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3 ); |
||
1049 | sqlite3VdbeAddOp2( v, OP_Integer, ENC( db ), reg3 ); |
||
1050 | sqlite3VdbeAddOp3( v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3 ); |
||
1051 | sqlite3VdbeJumpHere( v, j1 ); |
||
1052 | |||
1053 | /* This just creates a place-holder record in the sqlite_master table. |
||
1054 | ** The record created does not contain anything yet. It will be replaced |
||
1055 | ** by the real entry in code generated at sqlite3EndTable(). |
||
1056 | ** |
||
1057 | ** The rowid for the new entry is left in register pParse->regRowid. |
||
1058 | ** The root page number of the new table is left in reg pParse->regRoot. |
||
1059 | ** The rowid and root page number values are needed by the code that |
||
1060 | ** sqlite3EndTable will generate. |
||
1061 | */ |
||
1062 | if ( isView != 0 || isVirtual != 0 ) |
||
1063 | { |
||
1064 | sqlite3VdbeAddOp2( v, OP_Integer, 0, reg2 ); |
||
1065 | } |
||
1066 | else |
||
1067 | { |
||
1068 | sqlite3VdbeAddOp2( v, OP_CreateTable, iDb, reg2 ); |
||
1069 | } |
||
1070 | sqlite3OpenMasterTable( pParse, iDb ); |
||
1071 | sqlite3VdbeAddOp2( v, OP_NewRowid, 0, reg1 ); |
||
1072 | sqlite3VdbeAddOp2( v, OP_Null, 0, reg3 ); |
||
1073 | sqlite3VdbeAddOp3( v, OP_Insert, 0, reg3, reg1 ); |
||
1074 | sqlite3VdbeChangeP5( v, OPFLAG_APPEND ); |
||
1075 | sqlite3VdbeAddOp0( v, OP_Close ); |
||
1076 | } |
||
1077 | |||
1078 | /* Normal (non-error) return. */ |
||
1079 | return; |
||
1080 | |||
1081 | /* If an error occurs, we jump here */ |
||
1082 | begin_table_error: |
||
1083 | sqlite3DbFree( db, ref zName ); |
||
1084 | return; |
||
1085 | } |
||
1086 | |||
1087 | /* |
||
1088 | ** This macro is used to compare two strings in a case-insensitive manner. |
||
1089 | ** It is slightly faster than calling sqlite3StrICmp() directly, but |
||
1090 | ** produces larger code. |
||
1091 | ** |
||
1092 | ** WARNING: This macro is not compatible with the strcmp() family. It |
||
1093 | ** returns true if the two strings are equal, otherwise false. |
||
1094 | */ |
||
1095 | //#define STRICMP(x, y) (\ |
||
1096 | //sqlite3UpperToLower[*(unsigned char )(x)]== \ |
||
1097 | //sqlite3UpperToLower[*(unsigned char )(y)] \ |
||
1098 | //&& sqlite3StrICmp((x)+1,(y)+1)==0 ) |
||
1099 | |||
1100 | /* |
||
1101 | ** Add a new column to the table currently being constructed. |
||
1102 | ** |
||
1103 | ** The parser calls this routine once for each column declaration |
||
1104 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
||
1105 | ** first to get things going. Then this routine is called for each |
||
1106 | ** column. |
||
1107 | */ |
||
1108 | static void sqlite3AddColumn( Parse pParse, Token pName ) |
||
1109 | { |
||
1110 | Table p; |
||
1111 | int i; |
||
1112 | string z; |
||
1113 | Column pCol; |
||
1114 | sqlite3 db = pParse.db; |
||
1115 | if ( ( p = pParse.pNewTable ) == null ) |
||
1116 | return; |
||
1117 | #if SQLITE_MAX_COLUMN || !SQLITE_MAX_COLUMN |
||
1118 | if ( p.nCol + 1 > db.aLimit[SQLITE_LIMIT_COLUMN] ) |
||
1119 | { |
||
1120 | sqlite3ErrorMsg( pParse, "too many columns on %s", p.zName ); |
||
1121 | return; |
||
1122 | } |
||
1123 | #endif |
||
1124 | z = sqlite3NameFromToken( db, pName ); |
||
1125 | if ( z == null ) |
||
1126 | return; |
||
1127 | for ( i = 0; i < p.nCol; i++ ) |
||
1128 | { |
||
1129 | if ( z.Equals( p.aCol[i].zName, StringComparison.OrdinalIgnoreCase ) ) |
||
1130 | {//STRICMP(z, p.aCol[i].zName) ){ |
||
1131 | sqlite3ErrorMsg( pParse, "duplicate column name: %s", z ); |
||
1132 | sqlite3DbFree( db, ref z ); |
||
1133 | return; |
||
1134 | } |
||
1135 | } |
||
1136 | if ( ( p.nCol & 0x7 ) == 0 ) |
||
1137 | { |
||
1138 | //aNew = sqlite3DbRealloc(db,p.aCol,(p.nCol+8)*sizeof(p.aCol[0])); |
||
1139 | //if( aNew==0 ){ |
||
1140 | // sqlite3DbFree(db,ref z); |
||
1141 | // return; |
||
1142 | //} |
||
1143 | Array.Resize( ref p.aCol, p.nCol + 8 ); |
||
1144 | } |
||
1145 | p.aCol[p.nCol] = new Column(); |
||
1146 | pCol = p.aCol[p.nCol]; |
||
1147 | //memset(pCol, 0, sizeof(p.aCol[0])); |
||
1148 | pCol.zName = z; |
||
1149 | |||
1150 | /* If there is no type specified, columns have the default affinity |
||
1151 | ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will |
||
1152 | ** be called next to set pCol.affinity correctly. |
||
1153 | */ |
||
1154 | pCol.affinity = SQLITE_AFF_NONE; |
||
1155 | p.nCol++; |
||
1156 | } |
||
1157 | |||
1158 | /* |
||
1159 | ** This routine is called by the parser while in the middle of |
||
1160 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
||
1161 | ** been seen on a column. This routine sets the notNull flag on |
||
1162 | ** the column currently under construction. |
||
1163 | */ |
||
1164 | static void sqlite3AddNotNull( Parse pParse, int onError ) |
||
1165 | { |
||
1166 | Table p; |
||
1167 | p = pParse.pNewTable; |
||
1168 | if ( p == null || NEVER( p.nCol < 1 ) ) |
||
1169 | return; |
||
1170 | p.aCol[p.nCol - 1].notNull = (u8)onError; |
||
1171 | } |
||
1172 | |||
1173 | /* |
||
1174 | ** Scan the column type name zType (length nType) and return the |
||
1175 | ** associated affinity type. |
||
1176 | ** |
||
1177 | ** This routine does a case-independent search of zType for the |
||
1178 | ** substrings in the following table. If one of the substrings is |
||
1179 | ** found, the corresponding affinity is returned. If zType contains |
||
1180 | ** more than one of the substrings, entries toward the top of |
||
1181 | ** the table take priority. For example, if zType is 'BLOBINT', |
||
1182 | ** SQLITE_AFF_INTEGER is returned. |
||
1183 | ** |
||
1184 | ** Substring | Affinity |
||
1185 | ** -------------------------------- |
||
1186 | ** 'INT' | SQLITE_AFF_INTEGER |
||
1187 | ** 'CHAR' | SQLITE_AFF_TEXT |
||
1188 | ** 'CLOB' | SQLITE_AFF_TEXT |
||
1189 | ** 'TEXT' | SQLITE_AFF_TEXT |
||
1190 | ** 'BLOB' | SQLITE_AFF_NONE |
||
1191 | ** 'REAL' | SQLITE_AFF_REAL |
||
1192 | ** 'FLOA' | SQLITE_AFF_REAL |
||
1193 | ** 'DOUB' | SQLITE_AFF_REAL |
||
1194 | ** |
||
1195 | ** If none of the substrings in the above table are found, |
||
1196 | ** SQLITE_AFF_NUMERIC is returned. |
||
1197 | */ |
||
1198 | static char sqlite3AffinityType( string zIn ) |
||
1199 | { |
||
1200 | //u32 h = 0; |
||
1201 | //char aff = SQLITE_AFF_NUMERIC; |
||
1202 | zIn = zIn.ToLower(); |
||
1203 | if ( zIn.Contains( "char" ) || zIn.Contains( "clob" ) || zIn.Contains( "text" ) ) |
||
1204 | return SQLITE_AFF_TEXT; |
||
1205 | if ( zIn.Contains( "blob" ) ) |
||
1206 | return SQLITE_AFF_NONE; |
||
1207 | if ( zIn.Contains( "doub" ) || zIn.Contains( "floa" ) || zIn.Contains( "real" ) ) |
||
1208 | return SQLITE_AFF_REAL; |
||
1209 | if ( zIn.Contains( "int" ) ) |
||
1210 | return SQLITE_AFF_INTEGER; |
||
1211 | return SQLITE_AFF_NUMERIC; |
||
1212 | // string zEnd = pType.z.Substring(pType.n); |
||
1213 | |||
1214 | // while( zIn!=zEnd ){ |
||
1215 | // h = (h<<8) + sqlite3UpperToLower[*zIn]; |
||
1216 | // zIn++; |
||
1217 | // if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
||
1218 | // aff = SQLITE_AFF_TEXT; |
||
1219 | // }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
||
1220 | // aff = SQLITE_AFF_TEXT; |
||
1221 | // }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
||
1222 | // aff = SQLITE_AFF_TEXT; |
||
1223 | // }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
||
1224 | // && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
||
1225 | // aff = SQLITE_AFF_NONE; |
||
1226 | //#if !SQLITE_OMIT_FLOATING_POINT |
||
1227 | // }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
||
1228 | // && aff==SQLITE_AFF_NUMERIC ){ |
||
1229 | // aff = SQLITE_AFF_REAL; |
||
1230 | // }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
||
1231 | // && aff==SQLITE_AFF_NUMERIC ){ |
||
1232 | // aff = SQLITE_AFF_REAL; |
||
1233 | // }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
||
1234 | // && aff==SQLITE_AFF_NUMERIC ){ |
||
1235 | // aff = SQLITE_AFF_REAL; |
||
1236 | //#endif |
||
1237 | // }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
||
1238 | // aff = SQLITE_AFF_INTEGER; |
||
1239 | // break; |
||
1240 | // } |
||
1241 | // } |
||
1242 | |||
1243 | // return aff; |
||
1244 | } |
||
1245 | |||
1246 | /* |
||
1247 | ** This routine is called by the parser while in the middle of |
||
1248 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
||
1249 | ** token in the sequence of tokens that describe the type of the |
||
1250 | ** column currently under construction. pLast is the last token |
||
1251 | ** in the sequence. Use this information to construct a string |
||
1252 | ** that contains the typename of the column and store that string |
||
1253 | ** in zType. |
||
1254 | */ |
||
1255 | static void sqlite3AddColumnType( Parse pParse, Token pType ) |
||
1256 | { |
||
1257 | Table p; |
||
1258 | Column pCol; |
||
1259 | |||
1260 | p = pParse.pNewTable; |
||
1261 | if ( p == null || NEVER( p.nCol < 1 ) ) |
||
1262 | return; |
||
1263 | pCol = p.aCol[p.nCol - 1]; |
||
1264 | Debug.Assert( pCol.zType == null ); |
||
1265 | pCol.zType = sqlite3NameFromToken( pParse.db, pType ); |
||
1266 | pCol.affinity = sqlite3AffinityType( pCol.zType ); |
||
1267 | } |
||
1268 | |||
1269 | |||
1270 | /* |
||
1271 | ** The expression is the default value for the most recently added column |
||
1272 | ** of the table currently under construction. |
||
1273 | ** |
||
1274 | ** Default value expressions must be constant. Raise an exception if this |
||
1275 | ** is not the case. |
||
1276 | ** |
||
1277 | ** This routine is called by the parser while in the middle of |
||
1278 | ** parsing a CREATE TABLE statement. |
||
1279 | */ |
||
1280 | static void sqlite3AddDefaultValue( Parse pParse, ExprSpan pSpan ) |
||
1281 | { |
||
1282 | Table p; |
||
1283 | Column pCol; |
||
1284 | sqlite3 db = pParse.db; |
||
1285 | p = pParse.pNewTable; |
||
1286 | if ( p != null ) |
||
1287 | { |
||
1288 | pCol = ( p.aCol[p.nCol - 1] ); |
||
1289 | if ( sqlite3ExprIsConstantOrFunction( pSpan.pExpr ) == 0 ) |
||
1290 | { |
||
1291 | sqlite3ErrorMsg( pParse, "default value of column [%s] is not constant", |
||
1292 | pCol.zName ); |
||
1293 | } |
||
1294 | else |
||
1295 | { |
||
1296 | /* A copy of pExpr is used instead of the original, as pExpr contains |
||
1297 | ** tokens that point to volatile memory. The 'span' of the expression |
||
1298 | ** is required by pragma table_info. |
||
1299 | */ |
||
1300 | sqlite3ExprDelete( db, ref pCol.pDflt ); |
||
1301 | pCol.pDflt = sqlite3ExprDup( db, pSpan.pExpr, EXPRDUP_REDUCE ); |
||
1302 | sqlite3DbFree( db, ref pCol.zDflt ); |
||
1303 | pCol.zDflt = pSpan.zStart.Substring( 0, pSpan.zStart.Length - pSpan.zEnd.Length ); |
||
1304 | //sqlite3DbStrNDup( db, pSpan.zStart, |
||
1305 | // (int)( pSpan.zEnd.Length - pSpan.zStart.Length ) ); |
||
1306 | } |
||
1307 | } |
||
1308 | sqlite3ExprDelete( db, ref pSpan.pExpr ); |
||
1309 | } |
||
1310 | |||
1311 | /* |
||
1312 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
||
1313 | ** of columns that form the primary key. If pList is NULL, then the |
||
1314 | ** most recently added column of the table is the primary key. |
||
1315 | ** |
||
1316 | ** A table can have at most one primary key. If the table already has |
||
1317 | ** a primary key (and this is the second primary key) then create an |
||
1318 | ** error. |
||
1319 | ** |
||
1320 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
||
1321 | ** then we will try to use that column as the rowid. Set the Table.iPKey |
||
1322 | ** field of the table under construction to be the index of the |
||
1323 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
||
1324 | ** no INTEGER PRIMARY KEY. |
||
1325 | ** |
||
1326 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
||
1327 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
||
1328 | */ |
||
1329 | // OVERLOADS, so I don't need to rewrite parse.c |
||
1330 | static void sqlite3AddPrimaryKey( Parse pParse, int null_2, int onError, int autoInc, int sortOrder ) |
||
1331 | { |
||
1332 | sqlite3AddPrimaryKey( pParse, null, onError, autoInc, sortOrder ); |
||
1333 | } |
||
1334 | static void sqlite3AddPrimaryKey( |
||
1335 | Parse pParse, /* Parsing context */ |
||
1336 | ExprList pList, /* List of field names to be indexed */ |
||
1337 | int onError, /* What to do with a uniqueness conflict */ |
||
1338 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
||
1339 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
||
1340 | ) |
||
1341 | { |
||
1342 | Table pTab = pParse.pNewTable; |
||
1343 | string zType = null; |
||
1344 | int iCol = -1, i; |
||
1345 | if ( pTab == null || IN_DECLARE_VTAB( pParse ) ) |
||
1346 | goto primary_key_exit; |
||
1347 | if ( ( pTab.tabFlags & TF_HasPrimaryKey ) != 0 ) |
||
1348 | { |
||
1349 | sqlite3ErrorMsg( pParse, |
||
1350 | "table \"%s\" has more than one primary key", pTab.zName ); |
||
1351 | goto primary_key_exit; |
||
1352 | } |
||
1353 | pTab.tabFlags |= TF_HasPrimaryKey; |
||
1354 | if ( pList == null ) |
||
1355 | { |
||
1356 | iCol = pTab.nCol - 1; |
||
1357 | pTab.aCol[iCol].isPrimKey = 1; |
||
1358 | } |
||
1359 | else |
||
1360 | { |
||
1361 | for ( i = 0; i < pList.nExpr; i++ ) |
||
1362 | { |
||
1363 | for ( iCol = 0; iCol < pTab.nCol; iCol++ ) |
||
1364 | { |
||
1365 | if ( pList.a[i].zName.Equals( pTab.aCol[iCol].zName, StringComparison.OrdinalIgnoreCase ) ) |
||
1366 | { |
||
1367 | break; |
||
1368 | } |
||
1369 | } |
||
1370 | if ( iCol < pTab.nCol ) |
||
1371 | { |
||
1372 | pTab.aCol[iCol].isPrimKey = 1; |
||
1373 | } |
||
1374 | } |
||
1375 | if ( pList.nExpr > 1 ) |
||
1376 | iCol = -1; |
||
1377 | } |
||
1378 | if ( iCol >= 0 && iCol < pTab.nCol ) |
||
1379 | { |
||
1380 | zType = pTab.aCol[iCol].zType; |
||
1381 | } |
||
1382 | if ( zType != null && zType.Equals( "INTEGER", StringComparison.OrdinalIgnoreCase ) |
||
1383 | && sortOrder == SQLITE_SO_ASC ) |
||
1384 | { |
||
1385 | pTab.iPKey = iCol; |
||
1386 | pTab.keyConf = (byte)onError; |
||
1387 | Debug.Assert( autoInc == 0 || autoInc == 1 ); |
||
1388 | pTab.tabFlags |= (u8)( autoInc * TF_Autoincrement ); |
||
1389 | } |
||
1390 | else if ( autoInc != 0 ) |
||
1391 | { |
||
1392 | #if !SQLITE_OMIT_AUTOINCREMENT |
||
1393 | sqlite3ErrorMsg( pParse, "AUTOINCREMENT is only allowed on an " + |
||
1394 | "INTEGER PRIMARY KEY" ); |
||
1395 | #endif |
||
1396 | } |
||
1397 | else |
||
1398 | { |
||
1399 | Index p; |
||
1400 | p = sqlite3CreateIndex( pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0 ); |
||
1401 | if ( p != null ) |
||
1402 | { |
||
1403 | p.autoIndex = 2; |
||
1404 | } |
||
1405 | pList = null; |
||
1406 | } |
||
1407 | |||
1408 | primary_key_exit: |
||
1409 | sqlite3ExprListDelete( pParse.db, ref pList ); |
||
1410 | return; |
||
1411 | } |
||
1412 | |||
1413 | /* |
||
1414 | ** Add a new CHECK constraint to the table currently under construction. |
||
1415 | */ |
||
1416 | static void sqlite3AddCheckConstraint( |
||
1417 | Parse pParse, /* Parsing context */ |
||
1418 | Expr pCheckExpr /* The check expression */ |
||
1419 | ) |
||
1420 | { |
||
1421 | sqlite3 db = pParse.db; |
||
1422 | #if !SQLITE_OMIT_CHECK |
||
1423 | Table pTab = pParse.pNewTable; |
||
1424 | if ( pTab != null && !IN_DECLARE_VTAB( pParse ) ) |
||
1425 | { |
||
1426 | pTab.pCheck = sqlite3ExprAnd( db, pTab.pCheck, pCheckExpr ); |
||
1427 | } |
||
1428 | else |
||
1429 | #endif |
||
1430 | { |
||
1431 | sqlite3ExprDelete( db, ref pCheckExpr ); |
||
1432 | } |
||
1433 | } |
||
1434 | /* |
||
1435 | ** Set the collation function of the most recently parsed table column |
||
1436 | ** to the CollSeq given. |
||
1437 | */ |
||
1438 | static void sqlite3AddCollateType( Parse pParse, Token pToken ) |
||
1439 | { |
||
1440 | Table p; |
||
1441 | int i; |
||
1442 | string zColl; /* Dequoted name of collation sequence */ |
||
1443 | sqlite3 db; |
||
1444 | |||
1445 | if ( ( p = pParse.pNewTable ) == null ) |
||
1446 | return; |
||
1447 | i = p.nCol - 1; |
||
1448 | db = pParse.db; |
||
1449 | zColl = sqlite3NameFromToken( db, pToken ); |
||
1450 | if ( zColl == null ) |
||
1451 | return; |
||
1452 | |||
1453 | if ( sqlite3LocateCollSeq( pParse, zColl ) != null ) |
||
1454 | { |
||
1455 | Index pIdx; |
||
1456 | p.aCol[i].zColl = zColl; |
||
1457 | |||
1458 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
||
1459 | ** then an index may have been created on this column before the |
||
1460 | ** collation type was added. Correct this if it is the case. |
||
1461 | */ |
||
1462 | for ( pIdx = p.pIndex; pIdx != null; pIdx = pIdx.pNext ) |
||
1463 | { |
||
1464 | Debug.Assert( pIdx.nColumn == 1 ); |
||
1465 | if ( pIdx.aiColumn[0] == i ) |
||
1466 | { |
||
1467 | pIdx.azColl[0] = p.aCol[i].zColl; |
||
1468 | } |
||
1469 | } |
||
1470 | } |
||
1471 | else |
||
1472 | { |
||
1473 | sqlite3DbFree( db, ref zColl ); |
||
1474 | } |
||
1475 | } |
||
1476 | |||
1477 | /* |
||
1478 | ** This function returns the collation sequence for database native text |
||
1479 | ** encoding identified by the string zName, length nName. |
||
1480 | ** |
||
1481 | ** If the requested collation sequence is not available, or not available |
||
1482 | ** in the database native encoding, the collation factory is invoked to |
||
1483 | ** request it. If the collation factory does not supply such a sequence, |
||
1484 | ** and the sequence is available in another text encoding, then that is |
||
1485 | ** returned instead. |
||
1486 | ** |
||
1487 | ** If no versions of the requested collations sequence are available, or |
||
1488 | ** another error occurs, NULL is returned and an error message written into |
||
1489 | ** pParse. |
||
1490 | ** |
||
1491 | ** This routine is a wrapper around sqlite3FindCollSeq(). This routine |
||
1492 | ** invokes the collation factory if the named collation cannot be found |
||
1493 | ** and generates an error message. |
||
1494 | ** |
||
1495 | ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() |
||
1496 | */ |
||
1497 | static CollSeq sqlite3LocateCollSeq( Parse pParse, string zName ) |
||
1498 | { |
||
1499 | sqlite3 db = pParse.db; |
||
1500 | u8 enc = db.aDb[0].pSchema.enc;// ENC(db); |
||
1501 | u8 initbusy = db.init.busy; |
||
1502 | CollSeq pColl; |
||
1503 | |||
1504 | pColl = sqlite3FindCollSeq( db, enc, zName, initbusy ); |
||
1505 | if ( 0 == initbusy && ( pColl == null || pColl.xCmp == null ) ) |
||
1506 | { |
||
1507 | pColl = sqlite3GetCollSeq( db, enc, pColl, zName ); |
||
1508 | if ( pColl == null ) |
||
1509 | { |
||
1510 | sqlite3ErrorMsg( pParse, "no such collation sequence: %s", zName ); |
||
1511 | } |
||
1512 | } |
||
1513 | |||
1514 | return pColl; |
||
1515 | } |
||
1516 | |||
1517 | |||
1518 | /* |
||
1519 | ** Generate code that will increment the schema cookie. |
||
1520 | ** |
||
1521 | ** The schema cookie is used to determine when the schema for the |
||
1522 | ** database changes. After each schema change, the cookie value |
||
1523 | ** changes. When a process first reads the schema it records the |
||
1524 | ** cookie. Thereafter, whenever it goes to access the database, |
||
1525 | ** it checks the cookie to make sure the schema has not changed |
||
1526 | ** since it was last read. |
||
1527 | ** |
||
1528 | ** This plan is not completely bullet-proof. It is possible for |
||
1529 | ** the schema to change multiple times and for the cookie to be |
||
1530 | ** set back to prior value. But schema changes are infrequent |
||
1531 | ** and the probability of hitting the same cookie value is only |
||
1532 | ** 1 chance in 2^32. So we're safe enough. |
||
1533 | */ |
||
1534 | static void sqlite3ChangeCookie( Parse pParse, int iDb ) |
||
1535 | { |
||
1536 | int r1 = sqlite3GetTempReg( pParse ); |
||
1537 | sqlite3 db = pParse.db; |
||
1538 | Vdbe v = pParse.pVdbe; |
||
1539 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
1540 | sqlite3VdbeAddOp2( v, OP_Integer, db.aDb[iDb].pSchema.schema_cookie + 1, r1 ); |
||
1541 | sqlite3VdbeAddOp3( v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1 ); |
||
1542 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
1543 | } |
||
1544 | |||
1545 | /* |
||
1546 | ** Measure the number of characters needed to output the given |
||
1547 | ** identifier. The number returned includes any quotes used |
||
1548 | ** but does not include the null terminator. |
||
1549 | ** |
||
1550 | ** The estimate is conservative. It might be larger that what is |
||
1551 | ** really needed. |
||
1552 | */ |
||
1553 | static int identLength( string z ) |
||
1554 | { |
||
1555 | int n; |
||
1556 | for ( n = 0; n < z.Length; n++ ) |
||
1557 | { |
||
1558 | if ( z[n] == (byte)'"' ) |
||
1559 | { |
||
1560 | n++; |
||
1561 | } |
||
1562 | } |
||
1563 | return n + 2; |
||
1564 | } |
||
1565 | |||
1566 | |||
1567 | /* |
||
1568 | ** The first parameter is a pointer to an output buffer. The second |
||
1569 | ** parameter is a pointer to an integer that contains the offset at |
||
1570 | ** which to write into the output buffer. This function copies the |
||
1571 | ** nul-terminated string pointed to by the third parameter, zSignedIdent, |
||
1572 | ** to the specified offset in the buffer and updates *pIdx to refer |
||
1573 | ** to the first byte after the last byte written before returning. |
||
1574 | ** |
||
1575 | ** If the string zSignedIdent consists entirely of alpha-numeric |
||
1576 | ** characters, does not begin with a digit and is not an SQL keyword, |
||
1577 | ** then it is copied to the output buffer exactly as it is. Otherwise, |
||
1578 | ** it is quoted using double-quotes. |
||
1579 | */ |
||
1580 | static void identPut( StringBuilder z, ref int pIdx, string zSignedIdent ) |
||
1581 | { |
||
1582 | string zIdent = zSignedIdent; |
||
1583 | int i; |
||
1584 | int j; |
||
1585 | bool needQuote; |
||
1586 | i = pIdx; |
||
1587 | for ( j = 0; j < zIdent.Length; j++ ) |
||
1588 | { |
||
1589 | if ( !sqlite3Isalnum( zIdent[j] ) && zIdent[j] != '_' ) |
||
1590 | break; |
||
1591 | } |
||
1592 | needQuote = sqlite3Isdigit( zIdent[0] ) || sqlite3KeywordCode( zIdent, j ) != TK_ID; |
||
1593 | if ( !needQuote ) |
||
1594 | { |
||
1595 | needQuote = ( j < zIdent.Length && zIdent[j] != 0 ); |
||
1596 | } |
||
1597 | if ( needQuote ) |
||
1598 | { |
||
1599 | if ( i == z.Length ) |
||
1600 | z.Append( '\0' ); |
||
1601 | z[i++] = '"'; |
||
1602 | } |
||
1603 | for ( j = 0; j < zIdent.Length; j++ ) |
||
1604 | { |
||
1605 | if ( i == z.Length ) |
||
1606 | z.Append( '\0' ); |
||
1607 | z[i++] = zIdent[j]; |
||
1608 | if ( zIdent[j] == '"' ) |
||
1609 | { |
||
1610 | if ( i == z.Length ) |
||
1611 | z.Append( '\0' ); |
||
1612 | z[i++] = '"'; |
||
1613 | } |
||
1614 | } |
||
1615 | if ( needQuote ) |
||
1616 | { |
||
1617 | if ( i == z.Length ) |
||
1618 | z.Append( '\0' ); |
||
1619 | z[i++] = '"'; |
||
1620 | } |
||
1621 | //z[i] = 0; |
||
1622 | pIdx = i; |
||
1623 | } |
||
1624 | |||
1625 | /* |
||
1626 | ** Generate a CREATE TABLE statement appropriate for the given |
||
1627 | ** table. Memory to hold the text of the statement is obtained |
||
1628 | ** from sqliteMalloc() and must be freed by the calling function. |
||
1629 | */ |
||
1630 | static string createTableStmt( sqlite3 db, Table p ) |
||
1631 | { |
||
1632 | int i, k, n; |
||
1633 | StringBuilder zStmt; |
||
1634 | string zSep; |
||
1635 | string zSep2; |
||
1636 | string zEnd; |
||
1637 | Column pCol; |
||
1638 | n = 0; |
||
1639 | for ( i = 0; i < p.nCol; i++ ) |
||
1640 | {//, pCol++){ |
||
1641 | pCol = p.aCol[i]; |
||
1642 | n += identLength( pCol.zName ) + 5; |
||
1643 | } |
||
1644 | n += identLength( p.zName ); |
||
1645 | if ( n < 50 ) |
||
1646 | { |
||
1647 | zSep = string.Empty; |
||
1648 | zSep2 = ","; |
||
1649 | zEnd = ")"; |
||
1650 | } |
||
1651 | else |
||
1652 | { |
||
1653 | zSep = "\n "; |
||
1654 | zSep2 = ",\n "; |
||
1655 | zEnd = "\n)"; |
||
1656 | } |
||
1657 | n += 35 + 6 * p.nCol; |
||
1658 | zStmt = new StringBuilder( n ); |
||
1659 | //zStmt = sqlite3DbMallocRaw(0, n); |
||
1660 | //if( zStmt==0 ){ |
||
1661 | // db.mallocFailed = 1; |
||
1662 | // return 0; |
||
1663 | //} |
||
1664 | //sqlite3_snprintf(n, zStmt,"CREATE TABLE "); |
||
1665 | zStmt.Append( "CREATE TABLE " ); |
||
1666 | k = sqlite3Strlen30( zStmt ); |
||
1667 | identPut( zStmt, ref k, p.zName ); |
||
1668 | zStmt.Append( '(' );//zStmt[k++] = '('; |
||
1669 | for ( i = 0; i < p.nCol; i++ ) |
||
1670 | {//, pCol++){ |
||
1671 | pCol = p.aCol[i]; |
||
1672 | string[] azType = new string[] { |
||
1673 | /* SQLITE_AFF_TEXT */ " TEXT", |
||
1674 | /* SQLITE_AFF_NONE */ "", |
||
1675 | /* SQLITE_AFF_NUMERIC */ " NUM", |
||
1676 | /* SQLITE_AFF_INTEGER */ " INT", |
||
1677 | /* SQLITE_AFF_REAL */ " REAL" |
||
1678 | }; |
||
1679 | int len; |
||
1680 | string zType; |
||
1681 | |||
1682 | zStmt.Append( zSep );// sqlite3_snprintf(n-k, zStmt[k], zSep); |
||
1683 | k = sqlite3Strlen30( zStmt );// k += strlen(zStmt[k]); |
||
1684 | zSep = zSep2; |
||
1685 | identPut( zStmt, ref k, pCol.zName ); |
||
1686 | Debug.Assert( pCol.affinity - SQLITE_AFF_TEXT >= 0 ); |
||
1687 | Debug.Assert( pCol.affinity - SQLITE_AFF_TEXT < ArraySize( azType ) ); |
||
1688 | testcase( pCol.affinity == SQLITE_AFF_TEXT ); |
||
1689 | testcase( pCol.affinity == SQLITE_AFF_NONE ); |
||
1690 | testcase( pCol.affinity == SQLITE_AFF_NUMERIC ); |
||
1691 | testcase( pCol.affinity == SQLITE_AFF_INTEGER ); |
||
1692 | testcase( pCol.affinity == SQLITE_AFF_REAL ); |
||
1693 | |||
1694 | zType = azType[pCol.affinity - SQLITE_AFF_TEXT]; |
||
1695 | len = sqlite3Strlen30( zType ); |
||
1696 | Debug.Assert( pCol.affinity == SQLITE_AFF_NONE |
||
1697 | || pCol.affinity == sqlite3AffinityType( zType ) ); |
||
1698 | zStmt.Append( zType );// memcpy( &zStmt[k], zType, len ); |
||
1699 | k += len; |
||
1700 | Debug.Assert( k <= n ); |
||
1701 | } |
||
1702 | zStmt.Append( zEnd );//sqlite3_snprintf(n-k, zStmt[k], "%s", zEnd); |
||
1703 | return zStmt.ToString(); |
||
1704 | } |
||
1705 | |||
1706 | /* |
||
1707 | ** This routine is called to report the final ")" that terminates |
||
1708 | ** a CREATE TABLE statement. |
||
1709 | ** |
||
1710 | ** The table structure that other action routines have been building |
||
1711 | ** is added to the internal hash tables, assuming no errors have |
||
1712 | ** occurred. |
||
1713 | ** |
||
1714 | ** An entry for the table is made in the master table on disk, unless |
||
1715 | ** this is a temporary table or db.init.busy==1. When db.init.busy==1 |
||
1716 | ** it means we are reading the sqlite_master table because we just |
||
1717 | ** connected to the database or because the sqlite_master table has |
||
1718 | ** recently changed, so the entry for this table already exists in |
||
1719 | ** the sqlite_master table. We do not want to create it again. |
||
1720 | ** |
||
1721 | ** If the pSelect argument is not NULL, it means that this routine |
||
1722 | ** was called to create a table generated from a |
||
1723 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
||
1724 | ** the new table will match the result set of the SELECT. |
||
1725 | */ |
||
1726 | // OVERLOADS, so I don't need to rewrite parse.c |
||
1727 | static void sqlite3EndTable( Parse pParse, Token pCons, Token pEnd, int null_4 ) |
||
1728 | { |
||
1729 | sqlite3EndTable( pParse, pCons, pEnd, null ); |
||
1730 | } |
||
1731 | static void sqlite3EndTable( Parse pParse, int null_2, int null_3, Select pSelect ) |
||
1732 | { |
||
1733 | sqlite3EndTable( pParse, null, null, pSelect ); |
||
1734 | } |
||
1735 | |||
1736 | static void sqlite3EndTable( |
||
1737 | Parse pParse, /* Parse context */ |
||
1738 | Token pCons, /* The ',' token after the last column defn. */ |
||
1739 | Token pEnd, /* The final ')' token in the CREATE TABLE */ |
||
1740 | Select pSelect /* Select from a "CREATE ... AS SELECT" */ |
||
1741 | ) |
||
1742 | { |
||
1743 | Table p; |
||
1744 | sqlite3 db = pParse.db; |
||
1745 | int iDb; |
||
1746 | |||
1747 | if ( ( pEnd == null && pSelect == null ) /*|| db.mallocFailed != 0 */ ) |
||
1748 | { |
||
1749 | return; |
||
1750 | } |
||
1751 | p = pParse.pNewTable; |
||
1752 | if ( p == null ) |
||
1753 | return; |
||
1754 | |||
1755 | Debug.Assert( 0 == db.init.busy || pSelect == null ); |
||
1756 | |||
1757 | iDb = sqlite3SchemaToIndex( db, p.pSchema ); |
||
1758 | |||
1759 | #if !SQLITE_OMIT_CHECK |
||
1760 | /* Resolve names in all CHECK constraint expressions. |
||
1761 | */ |
||
1762 | if ( p.pCheck != null ) |
||
1763 | { |
||
1764 | SrcList sSrc; /* Fake SrcList for pParse.pNewTable */ |
||
1765 | NameContext sNC; /* Name context for pParse.pNewTable */ |
||
1766 | |||
1767 | sNC = new NameContext();// memset(sNC, 0, sizeof(sNC)); |
||
1768 | sSrc = new SrcList();// memset(sSrc, 0, sizeof(sSrc)); |
||
1769 | sSrc.nSrc = 1; |
||
1770 | sSrc.a = new SrcList_item[1]; |
||
1771 | sSrc.a[0] = new SrcList_item(); |
||
1772 | sSrc.a[0].zName = p.zName; |
||
1773 | sSrc.a[0].pTab = p; |
||
1774 | sSrc.a[0].iCursor = -1; |
||
1775 | sNC.pParse = pParse; |
||
1776 | sNC.pSrcList = sSrc; |
||
1777 | sNC.isCheck = 1; |
||
1778 | if ( sqlite3ResolveExprNames( sNC, ref p.pCheck ) != 0 ) |
||
1779 | { |
||
1780 | return; |
||
1781 | } |
||
1782 | } |
||
1783 | #endif // * !SQLITE_OMIT_CHECK) */ |
||
1784 | |||
1785 | /* If the db.init.busy is 1 it means we are reading the SQL off the |
||
1786 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
||
1787 | ** So do not write to the disk again. Extract the root page number |
||
1788 | ** for the table from the db.init.newTnum field. (The page number |
||
1789 | ** should have been put there by the sqliteOpenCb routine.) |
||
1790 | */ |
||
1791 | if ( db.init.busy != 0 ) |
||
1792 | { |
||
1793 | p.tnum = db.init.newTnum; |
||
1794 | } |
||
1795 | |||
1796 | /* If not initializing, then create a record for the new table |
||
1797 | ** in the SQLITE_MASTER table of the database. |
||
1798 | ** |
||
1799 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
||
1800 | ** file instead of into the main database file. |
||
1801 | */ |
||
1802 | if ( 0 == db.init.busy ) |
||
1803 | { |
||
1804 | int n; |
||
1805 | Vdbe v; |
||
1806 | String zType = string.Empty; /* "view" or "table" */ |
||
1807 | String zType2 = string.Empty; /* "VIEW" or "TABLE" */ |
||
1808 | String zStmt = string.Empty; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
||
1809 | |||
1810 | v = sqlite3GetVdbe( pParse ); |
||
1811 | if ( NEVER( v == null ) ) |
||
1812 | return; |
||
1813 | |||
1814 | sqlite3VdbeAddOp1( v, OP_Close, 0 ); |
||
1815 | |||
1816 | /* |
||
1817 | ** Initialize zType for the new view or table. |
||
1818 | */ |
||
1819 | if ( p.pSelect == null ) |
||
1820 | { |
||
1821 | /* A regular table */ |
||
1822 | zType = "table"; |
||
1823 | zType2 = "TABLE"; |
||
1824 | #if !SQLITE_OMIT_VIEW |
||
1825 | } |
||
1826 | else |
||
1827 | { |
||
1828 | /* A view */ |
||
1829 | zType = "view"; |
||
1830 | zType2 = "VIEW"; |
||
1831 | #endif |
||
1832 | } |
||
1833 | |||
1834 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
||
1835 | ** statement to populate the new table. The root-page number for the |
||
1836 | ** new table is in register pParse->regRoot. |
||
1837 | ** |
||
1838 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
||
1839 | ** suitable state to query for the column names and types to be used |
||
1840 | ** by the new table. |
||
1841 | ** |
||
1842 | ** A shared-cache write-lock is not required to write to the new table, |
||
1843 | ** as a schema-lock must have already been obtained to create it. Since |
||
1844 | ** a schema-lock excludes all other database users, the write-lock would |
||
1845 | ** be redundant. |
||
1846 | */ |
||
1847 | if ( pSelect != null ) |
||
1848 | { |
||
1849 | SelectDest dest = new SelectDest(); |
||
1850 | Table pSelTab; |
||
1851 | |||
1852 | Debug.Assert( pParse.nTab == 1 ); |
||
1853 | sqlite3VdbeAddOp3( v, OP_OpenWrite, 1, pParse.regRoot, iDb ); |
||
1854 | sqlite3VdbeChangeP5( v, 1 ); |
||
1855 | pParse.nTab = 2; |
||
1856 | sqlite3SelectDestInit( dest, SRT_Table, 1 ); |
||
1857 | sqlite3Select( pParse, pSelect, ref dest ); |
||
1858 | sqlite3VdbeAddOp1( v, OP_Close, 1 ); |
||
1859 | if ( pParse.nErr == 0 ) |
||
1860 | { |
||
1861 | pSelTab = sqlite3ResultSetOfSelect( pParse, pSelect ); |
||
1862 | if ( pSelTab == null ) |
||
1863 | return; |
||
1864 | Debug.Assert( p.aCol == null ); |
||
1865 | p.nCol = pSelTab.nCol; |
||
1866 | p.aCol = pSelTab.aCol; |
||
1867 | pSelTab.nCol = 0; |
||
1868 | pSelTab.aCol = null; |
||
1869 | sqlite3DeleteTable( db, ref pSelTab ); |
||
1870 | } |
||
1871 | } |
||
1872 | |||
1873 | /* Compute the complete text of the CREATE statement */ |
||
1874 | if ( pSelect != null ) |
||
1875 | { |
||
1876 | zStmt = createTableStmt( db, p ); |
||
1877 | } |
||
1878 | else |
||
1879 | { |
||
1880 | n = (int)( pParse.sNameToken.z.Length - pEnd.z.Length ) + 1; |
||
1881 | zStmt = sqlite3MPrintf( db, |
||
1882 | "CREATE %s %.*s", zType2, n, pParse.sNameToken.z |
||
1883 | ); |
||
1884 | } |
||
1885 | |||
1886 | /* A slot for the record has already been allocated in the |
||
1887 | ** SQLITE_MASTER table. We just need to update that slot with all |
||
1888 | ** the information we've collected. |
||
1889 | */ |
||
1890 | sqlite3NestedParse( pParse, |
||
1891 | "UPDATE %Q.%s " + |
||
1892 | "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " + |
||
1893 | "WHERE rowid=#%d", |
||
1894 | db.aDb[iDb].zName, SCHEMA_TABLE( iDb ), |
||
1895 | zType, |
||
1896 | p.zName, |
||
1897 | p.zName, |
||
1898 | pParse.regRoot, |
||
1899 | zStmt, |
||
1900 | pParse.regRowid |
||
1901 | ); |
||
1902 | sqlite3DbFree( db, ref zStmt ); |
||
1903 | sqlite3ChangeCookie( pParse, iDb ); |
||
1904 | |||
1905 | #if !SQLITE_OMIT_AUTOINCREMENT |
||
1906 | /* Check to see if we need to create an sqlite_sequence table for |
||
1907 | ** keeping track of autoincrement keys. |
||
1908 | */ |
||
1909 | if ( ( p.tabFlags & TF_Autoincrement ) != 0 ) |
||
1910 | { |
||
1911 | Db pDb = db.aDb[iDb]; |
||
1912 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
1913 | if ( pDb.pSchema.pSeqTab == null ) |
||
1914 | { |
||
1915 | sqlite3NestedParse( pParse, |
||
1916 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
||
1917 | pDb.zName |
||
1918 | ); |
||
1919 | } |
||
1920 | } |
||
1921 | #endif |
||
1922 | |||
1923 | /* Reparse everything to update our internal data structures */ |
||
1924 | sqlite3VdbeAddParseSchemaOp( v, iDb, |
||
1925 | sqlite3MPrintf( db, "tbl_name='%q'", p.zName ) ); |
||
1926 | } |
||
1927 | |||
1928 | |||
1929 | /* Add the table to the in-memory representation of the database. |
||
1930 | */ |
||
1931 | if ( db.init.busy != 0 ) |
||
1932 | { |
||
1933 | Table pOld; |
||
1934 | Schema pSchema = p.pSchema; |
||
1935 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
1936 | pOld = sqlite3HashInsert( ref pSchema.tblHash, p.zName, |
||
1937 | sqlite3Strlen30( p.zName ), p ); |
||
1938 | if ( pOld != null ) |
||
1939 | { |
||
1940 | Debug.Assert( p == pOld ); /* Malloc must have failed inside HashInsert() */ |
||
1941 | // db.mallocFailed = 1; |
||
1942 | return; |
||
1943 | } |
||
1944 | pParse.pNewTable = null; |
||
1945 | db.nTable++; |
||
1946 | db.flags |= SQLITE_InternChanges; |
||
1947 | |||
1948 | #if !SQLITE_OMIT_ALTERTABLE |
||
1949 | if ( p.pSelect == null ) |
||
1950 | { |
||
1951 | string zName = pParse.sNameToken.z; |
||
1952 | int nName; |
||
1953 | Debug.Assert( pSelect == null && pCons != null && pEnd != null ); |
||
1954 | if ( pCons.z == null ) |
||
1955 | { |
||
1956 | pCons = pEnd; |
||
1957 | } |
||
1958 | nName = zName.Length - pCons.z.Length; |
||
1959 | p.addColOffset = 13 + nName; // sqlite3Utf8CharLen(zName, nName); |
||
1960 | } |
||
1961 | #endif |
||
1962 | } |
||
1963 | } |
||
1964 | |||
1965 | #if !SQLITE_OMIT_VIEW |
||
1966 | /* |
||
1967 | ** The parser calls this routine in order to create a new VIEW |
||
1968 | */ |
||
1969 | static void sqlite3CreateView( |
||
1970 | Parse pParse, /* The parsing context */ |
||
1971 | Token pBegin, /* The CREATE token that begins the statement */ |
||
1972 | Token pName1, /* The token that holds the name of the view */ |
||
1973 | Token pName2, /* The token that holds the name of the view */ |
||
1974 | Select pSelect, /* A SELECT statement that will become the new view */ |
||
1975 | int isTemp, /* TRUE for a TEMPORARY view */ |
||
1976 | int noErr /* Suppress error messages if VIEW already exists */ |
||
1977 | ) |
||
1978 | { |
||
1979 | Table p; |
||
1980 | int n; |
||
1981 | string z;//string z; |
||
1982 | Token sEnd; |
||
1983 | DbFixer sFix = new DbFixer(); |
||
1984 | Token pName = null; |
||
1985 | int iDb; |
||
1986 | sqlite3 db = pParse.db; |
||
1987 | |||
1988 | if ( pParse.nVar > 0 ) |
||
1989 | { |
||
1990 | sqlite3ErrorMsg( pParse, "parameters are not allowed in views" ); |
||
1991 | sqlite3SelectDelete( db, ref pSelect ); |
||
1992 | return; |
||
1993 | } |
||
1994 | sqlite3StartTable( pParse, pName1, pName2, isTemp, 1, 0, noErr ); |
||
1995 | p = pParse.pNewTable; |
||
1996 | if ( p == null || pParse.nErr != 0 ) |
||
1997 | { |
||
1998 | sqlite3SelectDelete( db, ref pSelect ); |
||
1999 | return; |
||
2000 | } |
||
2001 | sqlite3TwoPartName( pParse, pName1, pName2, ref pName ); |
||
2002 | iDb = sqlite3SchemaToIndex( db, p.pSchema ); |
||
2003 | if ( sqlite3FixInit( sFix, pParse, iDb, "view", pName ) != 0 |
||
2004 | && sqlite3FixSelect( sFix, pSelect ) != 0 |
||
2005 | ) |
||
2006 | { |
||
2007 | sqlite3SelectDelete( db, ref pSelect ); |
||
2008 | return; |
||
2009 | } |
||
2010 | |||
2011 | /* Make a copy of the entire SELECT statement that defines the view. |
||
2012 | ** This will force all the Expr.token.z values to be dynamically |
||
2013 | ** allocated rather than point to the input string - which means that |
||
2014 | ** they will persist after the current sqlite3_exec() call returns. |
||
2015 | */ |
||
2016 | p.pSelect = sqlite3SelectDup( db, pSelect, EXPRDUP_REDUCE ); |
||
2017 | sqlite3SelectDelete( db, ref pSelect ); |
||
2018 | //if ( db.mallocFailed != 0 ) |
||
2019 | //{ |
||
2020 | // return; |
||
2021 | //} |
||
2022 | if ( 0 == db.init.busy ) |
||
2023 | { |
||
2024 | sqlite3ViewGetColumnNames( pParse, p ); |
||
2025 | } |
||
2026 | |||
2027 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
||
2028 | ** the end. |
||
2029 | */ |
||
2030 | sEnd = pParse.sLastToken; |
||
2031 | if ( ALWAYS( sEnd.z[0] != 0 ) && sEnd.z[0] != ';' ) |
||
2032 | { |
||
2033 | sEnd.z = sEnd.z.Substring( sEnd.n ); |
||
2034 | } |
||
2035 | sEnd.n = 0; |
||
2036 | n = (int)( pBegin.z.Length - sEnd.z.Length );//sEnd.z - pBegin.z; |
||
2037 | z = pBegin.z; |
||
2038 | while ( ALWAYS( n > 0 ) && sqlite3Isspace( z[n - 1] ) ) |
||
2039 | { |
||
2040 | n--; |
||
2041 | } |
||
2042 | sEnd.z = z.Substring( n - 1 ); |
||
2043 | sEnd.n = 1; |
||
2044 | |||
2045 | /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
||
2046 | sqlite3EndTable( pParse, null, sEnd, null ); |
||
2047 | return; |
||
2048 | } |
||
2049 | #else |
||
2050 | static void sqlite3CreateView( |
||
2051 | Parse pParse, /* The parsing context */ |
||
2052 | Token pBegin, /* The CREATE token that begins the statement */ |
||
2053 | Token pName1, /* The token that holds the name of the view */ |
||
2054 | Token pName2, /* The token that holds the name of the view */ |
||
2055 | Select pSelect, /* A SELECT statement that will become the new view */ |
||
2056 | int isTemp, /* TRUE for a TEMPORARY view */ |
||
2057 | int noErr /* Suppress error messages if VIEW already exists */ |
||
2058 | ) |
||
2059 | { |
||
2060 | } |
||
2061 | #endif // * SQLITE_OMIT_VIEW */ |
||
2062 | |||
2063 | #if !SQLITE_OMIT_VIEW || !SQLITE_OMIT_VIRTUALTABLE |
||
2064 | /* |
||
2065 | ** The Table structure pTable is really a VIEW. Fill in the names of |
||
2066 | ** the columns of the view in the pTable structure. Return the number |
||
2067 | ** of errors. If an error is seen leave an error message in pParse.zErrMsg. |
||
2068 | */ |
||
2069 | static int sqlite3ViewGetColumnNames( Parse pParse, Table pTable ) |
||
2070 | { |
||
2071 | Table pSelTab; /* A fake table from which we get the result set */ |
||
2072 | Select pSel; /* Copy of the SELECT that implements the view */ |
||
2073 | int nErr = 0; /* Number of errors encountered */ |
||
2074 | int n; /* Temporarily holds the number of cursors assigned */ |
||
2075 | sqlite3 db = pParse.db; /* Database connection for malloc errors */ |
||
2076 | dxAuth xAuth; //)(void*,int,const char*,const char*,const char*,const char); |
||
2077 | |||
2078 | Debug.Assert( pTable != null ); |
||
2079 | |||
2080 | #if !SQLITE_OMIT_VIRTUALTABLE |
||
2081 | if ( sqlite3VtabCallConnect( pParse, pTable )!=0 ) |
||
2082 | { |
||
2083 | return SQLITE_ERROR; |
||
2084 | } |
||
2085 | #endif |
||
2086 | if ( IsVirtual( pTable ) ) |
||
2087 | return 0; |
||
2088 | |||
2089 | #if !SQLITE_OMIT_VIEW |
||
2090 | /* A positive nCol means the columns names for this view are |
||
2091 | ** already known. |
||
2092 | */ |
||
2093 | if ( pTable.nCol > 0 ) |
||
2094 | return 0; |
||
2095 | |||
2096 | /* A negative nCol is a special marker meaning that we are currently |
||
2097 | ** trying to compute the column names. If we enter this routine with |
||
2098 | ** a negative nCol, it means two or more views form a loop, like this: |
||
2099 | ** |
||
2100 | ** CREATE VIEW one AS SELECT * FROM two; |
||
2101 | ** CREATE VIEW two AS SELECT * FROM one; |
||
2102 | ** |
||
2103 | ** Actually, the error above is now caught prior to reaching this point. |
||
2104 | ** But the following test is still important as it does come up |
||
2105 | ** in the following: |
||
2106 | ** |
||
2107 | ** CREATE TABLE main.ex1(a); |
||
2108 | ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
||
2109 | ** SELECT * FROM temp.ex1; |
||
2110 | */ |
||
2111 | if ( pTable.nCol < 0 ) |
||
2112 | { |
||
2113 | sqlite3ErrorMsg( pParse, "view %s is circularly defined", pTable.zName ); |
||
2114 | return 1; |
||
2115 | } |
||
2116 | Debug.Assert( pTable.nCol >= 0 ); |
||
2117 | |||
2118 | /* If we get this far, it means we need to compute the table names. |
||
2119 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
||
2120 | ** "*" elements in the results set of the view and will assign cursors |
||
2121 | ** to the elements of the FROM clause. But we do not want these changes |
||
2122 | ** to be permanent. So the computation is done on a copy of the SELECT |
||
2123 | ** statement that defines the view. |
||
2124 | */ |
||
2125 | Debug.Assert( pTable.pSelect != null ); |
||
2126 | pSel = sqlite3SelectDup( db, pTable.pSelect, 0 ); |
||
2127 | if ( pSel != null ) |
||
2128 | { |
||
2129 | u8 enableLookaside = db.lookaside.bEnabled; |
||
2130 | n = pParse.nTab; |
||
2131 | sqlite3SrcListAssignCursors( pParse, pSel.pSrc ); |
||
2132 | pTable.nCol = -1; |
||
2133 | db.lookaside.bEnabled = 0; |
||
2134 | #if !SQLITE_OMIT_AUTHORIZATION |
||
2135 | xAuth = db.xAuth; |
||
2136 | db.xAuth = 0; |
||
2137 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
||
2138 | db.xAuth = xAuth; |
||
2139 | #else |
||
2140 | pSelTab = sqlite3ResultSetOfSelect( pParse, pSel ); |
||
2141 | #endif |
||
2142 | db.lookaside.bEnabled = enableLookaside; |
||
2143 | pParse.nTab = n; |
||
2144 | if ( pSelTab != null ) |
||
2145 | { |
||
2146 | Debug.Assert( pTable.aCol == null ); |
||
2147 | pTable.nCol = pSelTab.nCol; |
||
2148 | pTable.aCol = pSelTab.aCol; |
||
2149 | pSelTab.nCol = 0; |
||
2150 | pSelTab.aCol = null; |
||
2151 | sqlite3DeleteTable( db, ref pSelTab ); |
||
2152 | Debug.Assert( sqlite3SchemaMutexHeld( db, 0, pTable.pSchema ) ); |
||
2153 | pTable.pSchema.flags |= DB_UnresetViews; |
||
2154 | } |
||
2155 | else |
||
2156 | { |
||
2157 | pTable.nCol = 0; |
||
2158 | nErr++; |
||
2159 | } |
||
2160 | sqlite3SelectDelete( db, ref pSel ); |
||
2161 | } |
||
2162 | else |
||
2163 | { |
||
2164 | nErr++; |
||
2165 | } |
||
2166 | #endif // * SQLITE_OMIT_VIEW */ |
||
2167 | return nErr; |
||
2168 | } |
||
2169 | #endif // * !SQLITE_OMIT_VIEW) || !SQLITE_OMIT_VIRTUALTABLE) */ |
||
2170 | |||
2171 | #if !SQLITE_OMIT_VIEW |
||
2172 | /* |
||
2173 | ** Clear the column names from every VIEW in database idx. |
||
2174 | */ |
||
2175 | static void sqliteViewResetAll( sqlite3 db, int idx ) |
||
2176 | { |
||
2177 | HashElem i; |
||
2178 | Debug.Assert( sqlite3SchemaMutexHeld( db, idx, null ) ); |
||
2179 | if ( !DbHasProperty( db, idx, DB_UnresetViews ) ) |
||
2180 | return; |
||
2181 | //for(i=sqliteHashFirst(&db.aDb[idx].pSchema.tblHash); i;i=sqliteHashNext(i)){ |
||
2182 | for ( i = db.aDb[idx].pSchema.tblHash.first; i != null; i = i.next ) |
||
2183 | { |
||
2184 | Table pTab = (Table)i.data;// sqliteHashData( i ); |
||
2185 | if ( pTab.pSelect != null ) |
||
2186 | { |
||
2187 | sqliteDeleteColumnNames( db, pTab ); |
||
2188 | pTab.aCol = null; |
||
2189 | pTab.nCol = 0; |
||
2190 | |||
2191 | } |
||
2192 | } |
||
2193 | DbClearProperty( db, idx, DB_UnresetViews ); |
||
2194 | } |
||
2195 | #else |
||
2196 | //# define sqliteViewResetAll(A,B) |
||
2197 | static void sqliteViewResetAll( sqlite3 A, int B ) |
||
2198 | { |
||
2199 | } |
||
2200 | #endif // * SQLITE_OMIT_VIEW */ |
||
2201 | |||
2202 | /* |
||
2203 | ** This function is called by the VDBE to adjust the internal schema |
||
2204 | ** used by SQLite when the btree layer moves a table root page. The |
||
2205 | ** root-page of a table or index in database iDb has changed from iFrom |
||
2206 | ** to iTo. |
||
2207 | ** |
||
2208 | ** Ticket #1728: The symbol table might still contain information |
||
2209 | ** on tables and/or indices that are the process of being deleted. |
||
2210 | ** If you are unlucky, one of those deleted indices or tables might |
||
2211 | ** have the same rootpage number as the real table or index that is |
||
2212 | ** being moved. So we cannot stop searching after the first match |
||
2213 | ** because the first match might be for one of the deleted indices |
||
2214 | ** or tables and not the table/index that is actually being moved. |
||
2215 | ** We must continue looping until all tables and indices with |
||
2216 | ** rootpage==iFrom have been converted to have a rootpage of iTo |
||
2217 | ** in order to be certain that we got the right one. |
||
2218 | */ |
||
2219 | #if !SQLITE_OMIT_AUTOVACUUM |
||
2220 | static void sqlite3RootPageMoved( sqlite3 db, int iDb, int iFrom, int iTo ) |
||
2221 | { |
||
2222 | HashElem pElem; |
||
2223 | Hash pHash; |
||
2224 | Db pDb; |
||
2225 | |||
2226 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
2227 | pDb = db.aDb[iDb]; |
||
2228 | |||
2229 | pHash = pDb.pSchema.tblHash; |
||
2230 | for ( pElem = pHash.first; pElem != null; pElem = pElem.next )// ( pElem = sqliteHashFirst( pHash ) ; pElem ; pElem = sqliteHashNext( pElem ) ) |
||
2231 | { |
||
2232 | Table pTab = (Table)pElem.data;// sqliteHashData( pElem ); |
||
2233 | if ( pTab.tnum == iFrom ) |
||
2234 | { |
||
2235 | pTab.tnum = iTo; |
||
2236 | } |
||
2237 | } |
||
2238 | pHash = pDb.pSchema.idxHash; |
||
2239 | for ( pElem = pHash.first; pElem != null; pElem = pElem.next )// ( pElem = sqliteHashFirst( pHash ) ; pElem ; pElem = sqliteHashNext( pElem ) ) |
||
2240 | { |
||
2241 | Index pIdx = (Index)pElem.data;// sqliteHashData( pElem ); |
||
2242 | if ( pIdx.tnum == iFrom ) |
||
2243 | { |
||
2244 | pIdx.tnum = iTo; |
||
2245 | } |
||
2246 | } |
||
2247 | } |
||
2248 | #endif |
||
2249 | |||
2250 | /* |
||
2251 | ** Write code to erase the table with root-page iTable from database iDb. |
||
2252 | ** Also write code to modify the sqlite_master table and internal schema |
||
2253 | ** if a root-page of another table is moved by the btree-layer whilst |
||
2254 | ** erasing iTable (this can happen with an auto-vacuum database). |
||
2255 | */ |
||
2256 | static void destroyRootPage( Parse pParse, int iTable, int iDb ) |
||
2257 | { |
||
2258 | Vdbe v = sqlite3GetVdbe( pParse ); |
||
2259 | int r1 = sqlite3GetTempReg( pParse ); |
||
2260 | sqlite3VdbeAddOp3( v, OP_Destroy, iTable, r1, iDb ); |
||
2261 | sqlite3MayAbort( pParse ); |
||
2262 | #if !SQLITE_OMIT_AUTOVACUUM |
||
2263 | /* OP_Destroy stores an in integer r1. If this integer |
||
2264 | ** is non-zero, then it is the root page number of a table moved to |
||
2265 | ** location iTable. The following code modifies the sqlite_master table to |
||
2266 | ** reflect this. |
||
2267 | ** |
||
2268 | ** The "#NNN" in the SQL is a special constant that means whatever value |
||
2269 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
||
2270 | ** token for additional information. |
||
2271 | */ |
||
2272 | sqlite3NestedParse( pParse, |
||
2273 | "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", |
||
2274 | pParse.db.aDb[iDb].zName, SCHEMA_TABLE( iDb ), iTable, r1, r1 ); |
||
2275 | #endif |
||
2276 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
2277 | } |
||
2278 | |||
2279 | /* |
||
2280 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
||
2281 | ** Code to update the sqlite_master tables and internal schema definitions |
||
2282 | ** in case a root-page belonging to another table is moved by the btree layer |
||
2283 | ** is also added (this can happen with an auto-vacuum database). |
||
2284 | */ |
||
2285 | static void destroyTable( Parse pParse, Table pTab ) |
||
2286 | { |
||
2287 | #if SQLITE_OMIT_AUTOVACUUM |
||
2288 | Index pIdx; |
||
2289 | int iDb = sqlite3SchemaToIndex( pParse.db, pTab.pSchema ); |
||
2290 | destroyRootPage( pParse, pTab.tnum, iDb ); |
||
2291 | for ( pIdx = pTab.pIndex ; pIdx != null ; pIdx = pIdx.pNext ) |
||
2292 | { |
||
2293 | destroyRootPage( pParse, pIdx.tnum, iDb ); |
||
2294 | } |
||
2295 | #else |
||
2296 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
||
2297 | ** is not defined), then it is important to call OP_Destroy on the |
||
2298 | ** table and index root-pages in order, starting with the numerically |
||
2299 | ** largest root-page number. This guarantees that none of the root-pages |
||
2300 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
||
2301 | ** following were coded: |
||
2302 | ** |
||
2303 | ** OP_Destroy 4 0 |
||
2304 | ** ... |
||
2305 | ** OP_Destroy 5 0 |
||
2306 | ** |
||
2307 | ** and root page 5 happened to be the largest root-page number in the |
||
2308 | ** database, then root page 5 would be moved to page 4 by the |
||
2309 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
||
2310 | ** a free-list page. |
||
2311 | */ |
||
2312 | int iTab = pTab.tnum; |
||
2313 | int iDestroyed = 0; |
||
2314 | |||
2315 | while ( true ) |
||
2316 | { |
||
2317 | Index pIdx; |
||
2318 | int iLargest = 0; |
||
2319 | |||
2320 | if ( iDestroyed == 0 || iTab < iDestroyed ) |
||
2321 | { |
||
2322 | iLargest = iTab; |
||
2323 | } |
||
2324 | for ( pIdx = pTab.pIndex; pIdx != null; pIdx = pIdx.pNext ) |
||
2325 | { |
||
2326 | int iIdx = pIdx.tnum; |
||
2327 | Debug.Assert( pIdx.pSchema == pTab.pSchema ); |
||
2328 | if ( ( iDestroyed == 0 || ( iIdx < iDestroyed ) ) && iIdx > iLargest ) |
||
2329 | { |
||
2330 | iLargest = iIdx; |
||
2331 | } |
||
2332 | } |
||
2333 | if ( iLargest == 0 ) |
||
2334 | { |
||
2335 | return; |
||
2336 | } |
||
2337 | else |
||
2338 | { |
||
2339 | int iDb = sqlite3SchemaToIndex( pParse.db, pTab.pSchema ); |
||
2340 | destroyRootPage( pParse, iLargest, iDb ); |
||
2341 | iDestroyed = iLargest; |
||
2342 | } |
||
2343 | } |
||
2344 | #endif |
||
2345 | } |
||
2346 | |||
2347 | /* |
||
2348 | ** This routine is called to do the work of a DROP TABLE statement. |
||
2349 | ** pName is the name of the table to be dropped. |
||
2350 | */ |
||
2351 | static void sqlite3DropTable( Parse pParse, SrcList pName, int isView, int noErr ) |
||
2352 | { |
||
2353 | Table pTab; |
||
2354 | Vdbe v; |
||
2355 | sqlite3 db = pParse.db; |
||
2356 | int iDb; |
||
2357 | |||
2358 | //if ( db.mallocFailed != 0 ) |
||
2359 | //{ |
||
2360 | // goto exit_drop_table; |
||
2361 | //} |
||
2362 | Debug.Assert( pParse.nErr == 0 ); |
||
2363 | Debug.Assert( pName.nSrc == 1 ); |
||
2364 | if ( noErr != 0 ) |
||
2365 | db.suppressErr++; |
||
2366 | pTab = sqlite3LocateTable( pParse, isView, |
||
2367 | pName.a[0].zName, pName.a[0].zDatabase ); |
||
2368 | if ( noErr != 0 ) |
||
2369 | db.suppressErr--; |
||
2370 | |||
2371 | if ( pTab == null ) |
||
2372 | { |
||
2373 | if ( noErr != 0) |
||
2374 | sqlite3CodeVerifyNamedSchema( pParse, pName.a[0].zDatabase ); |
||
2375 | goto exit_drop_table; |
||
2376 | } |
||
2377 | iDb = sqlite3SchemaToIndex( db, pTab.pSchema ); |
||
2378 | Debug.Assert( iDb >= 0 && iDb < db.nDb ); |
||
2379 | |||
2380 | /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
||
2381 | ** it is initialized. |
||
2382 | */ |
||
2383 | if ( IsVirtual( pTab ) && sqlite3ViewGetColumnNames( pParse, pTab ) != 0 ) |
||
2384 | { |
||
2385 | goto exit_drop_table; |
||
2386 | } |
||
2387 | #if !SQLITE_OMIT_AUTHORIZATION |
||
2388 | { |
||
2389 | int code; |
||
2390 | string zTab = SCHEMA_TABLE(iDb); |
||
2391 | string zDb = db.aDb[iDb].zName; |
||
2392 | string zArg2 = 0; |
||
2393 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
||
2394 | goto exit_drop_table; |
||
2395 | } |
||
2396 | if( isView ){ |
||
2397 | if( OMIT_TEMPDB ==0&& iDb==1 ){ |
||
2398 | code = SQLITE_DROP_TEMP_VIEW; |
||
2399 | }else{ |
||
2400 | code = SQLITE_DROP_VIEW; |
||
2401 | } |
||
2402 | }else if( IsVirtual(pTab) ){ |
||
2403 | code = SQLITE_DROP_VTABLE; |
||
2404 | zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
||
2405 | }else{ |
||
2406 | if( OMIT_TEMPDB ==0&& iDb==1 ){ |
||
2407 | code = SQLITE_DROP_TEMP_TABLE; |
||
2408 | }else{ |
||
2409 | code = SQLITE_DROP_TABLE; |
||
2410 | } |
||
2411 | } |
||
2412 | if( sqlite3AuthCheck(pParse, code, pTab.zName, zArg2, zDb) ){ |
||
2413 | goto exit_drop_table; |
||
2414 | } |
||
2415 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab.zName, 0, zDb) ){ |
||
2416 | goto exit_drop_table; |
||
2417 | } |
||
2418 | } |
||
2419 | #endif |
||
2420 | if ( pTab.zName.StartsWith( "sqlite_", System.StringComparison.OrdinalIgnoreCase ) ) |
||
2421 | { |
||
2422 | sqlite3ErrorMsg( pParse, "table %s may not be dropped", pTab.zName ); |
||
2423 | goto exit_drop_table; |
||
2424 | } |
||
2425 | |||
2426 | #if !SQLITE_OMIT_VIEW |
||
2427 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
||
2428 | ** on a table. |
||
2429 | */ |
||
2430 | if ( isView != 0 && pTab.pSelect == null ) |
||
2431 | { |
||
2432 | sqlite3ErrorMsg( pParse, "use DROP TABLE to delete table %s", pTab.zName ); |
||
2433 | goto exit_drop_table; |
||
2434 | } |
||
2435 | if ( 0 == isView && pTab.pSelect != null ) |
||
2436 | { |
||
2437 | sqlite3ErrorMsg( pParse, "use DROP VIEW to delete view %s", pTab.zName ); |
||
2438 | goto exit_drop_table; |
||
2439 | } |
||
2440 | #endif |
||
2441 | |||
2442 | /* Generate code to remove the table from the master table |
||
2443 | ** on disk. |
||
2444 | */ |
||
2445 | v = sqlite3GetVdbe( pParse ); |
||
2446 | if ( v != null ) |
||
2447 | { |
||
2448 | Trigger pTrigger; |
||
2449 | Db pDb = db.aDb[iDb]; |
||
2450 | sqlite3BeginWriteOperation( pParse, 1, iDb ); |
||
2451 | |||
2452 | #if !SQLITE_OMIT_VIRTUALTABLE |
||
2453 | if ( IsVirtual( pTab ) ) |
||
2454 | { |
||
2455 | sqlite3VdbeAddOp0( v, OP_VBegin ); |
||
2456 | } |
||
2457 | #endif |
||
2458 | sqlite3FkDropTable( pParse, pName, pTab ); |
||
2459 | |||
2460 | /* Drop all triggers associated with the table being dropped. Code |
||
2461 | ** is generated to remove entries from sqlite_master and/or |
||
2462 | ** sqlite_temp_master if required. |
||
2463 | */ |
||
2464 | pTrigger = sqlite3TriggerList( pParse, pTab ); |
||
2465 | while ( pTrigger != null ) |
||
2466 | { |
||
2467 | Debug.Assert( pTrigger.pSchema == pTab.pSchema || |
||
2468 | pTrigger.pSchema == db.aDb[1].pSchema ); |
||
2469 | sqlite3DropTriggerPtr( pParse, pTrigger ); |
||
2470 | pTrigger = pTrigger.pNext; |
||
2471 | } |
||
2472 | |||
2473 | #if !SQLITE_OMIT_AUTOINCREMENT |
||
2474 | /* Remove any entries of the sqlite_sequence table associated with |
||
2475 | ** the table being dropped. This is done before the table is dropped |
||
2476 | ** at the btree level, in case the sqlite_sequence table needs to |
||
2477 | ** move as a result of the drop (can happen in auto-vacuum mode). |
||
2478 | */ |
||
2479 | if ( ( pTab.tabFlags & TF_Autoincrement ) != 0 ) |
||
2480 | { |
||
2481 | sqlite3NestedParse( pParse, |
||
2482 | "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
||
2483 | pDb.zName, pTab.zName |
||
2484 | ); |
||
2485 | } |
||
2486 | #endif |
||
2487 | |||
2488 | /* Drop all SQLITE_MASTER table and index entries that refer to the |
||
2489 | ** table. The program name loops through the master table and deletes |
||
2490 | ** every row that refers to a table of the same name as the one being |
||
2491 | ** dropped. Triggers are handled seperately because a trigger can be |
||
2492 | ** created in the temp database that refers to a table in another |
||
2493 | ** database. |
||
2494 | */ |
||
2495 | sqlite3NestedParse( pParse, |
||
2496 | "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
||
2497 | pDb.zName, SCHEMA_TABLE( iDb ), pTab.zName ); |
||
2498 | |||
2499 | /* Drop any statistics from the sqlite_stat1 table, if it exists */ |
||
2500 | if ( sqlite3FindTable( db, "sqlite_stat1", db.aDb[iDb].zName ) != null ) |
||
2501 | { |
||
2502 | sqlite3NestedParse( pParse, |
||
2503 | "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb.zName, pTab.zName |
||
2504 | ); |
||
2505 | } |
||
2506 | |||
2507 | if ( 0 == isView && !IsVirtual( pTab ) ) |
||
2508 | { |
||
2509 | destroyTable( pParse, pTab ); |
||
2510 | } |
||
2511 | |||
2512 | /* Remove the table entry from SQLite's internal schema and modify |
||
2513 | ** the schema cookie. |
||
2514 | */ |
||
2515 | if ( IsVirtual( pTab ) ) |
||
2516 | { |
||
2517 | sqlite3VdbeAddOp4( v, OP_VDestroy, iDb, 0, 0, pTab.zName, 0 ); |
||
2518 | } |
||
2519 | sqlite3VdbeAddOp4( v, OP_DropTable, iDb, 0, 0, pTab.zName, 0 ); |
||
2520 | sqlite3ChangeCookie( pParse, iDb ); |
||
2521 | } |
||
2522 | sqliteViewResetAll( db, iDb ); |
||
2523 | |||
2524 | exit_drop_table: |
||
2525 | sqlite3SrcListDelete( db, ref pName ); |
||
2526 | } |
||
2527 | |||
2528 | /* |
||
2529 | ** This routine is called to create a new foreign key on the table |
||
2530 | ** currently under construction. pFromCol determines which columns |
||
2531 | ** in the current table point to the foreign key. If pFromCol==0 then |
||
2532 | ** connect the key to the last column inserted. pTo is the name of |
||
2533 | ** the table referred to. pToCol is a list of tables in the other |
||
2534 | ** pTo table that the foreign key points to. flags contains all |
||
2535 | ** information about the conflict resolution algorithms specified |
||
2536 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
||
2537 | ** |
||
2538 | ** An FKey structure is created and added to the table currently |
||
2539 | ** under construction in the pParse.pNewTable field. |
||
2540 | ** |
||
2541 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
||
2542 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
||
2543 | */ |
||
2544 | // OVERLOADS, so I don't need to rewrite parse.c |
||
2545 | static void sqlite3CreateForeignKey( Parse pParse, int null_2, Token pTo, ExprList pToCol, int flags ) |
||
2546 | { |
||
2547 | sqlite3CreateForeignKey( pParse, null, pTo, pToCol, flags ); |
||
2548 | } |
||
2549 | static void sqlite3CreateForeignKey( |
||
2550 | Parse pParse, /* Parsing context */ |
||
2551 | ExprList pFromCol, /* Columns in this table that point to other table */ |
||
2552 | Token pTo, /* Name of the other table */ |
||
2553 | ExprList pToCol, /* Columns in the other table */ |
||
2554 | int flags /* Conflict resolution algorithms. */ |
||
2555 | ) |
||
2556 | { |
||
2557 | sqlite3 db = pParse.db; |
||
2558 | #if !SQLITE_OMIT_FOREIGN_KEY |
||
2559 | FKey pFKey = null; |
||
2560 | FKey pNextTo; |
||
2561 | Table p = pParse.pNewTable; |
||
2562 | int nByte; |
||
2563 | int i; |
||
2564 | int nCol; |
||
2565 | //string z; |
||
2566 | |||
2567 | Debug.Assert( pTo != null ); |
||
2568 | if ( p == null || IN_DECLARE_VTAB( pParse ) ) |
||
2569 | goto fk_end; |
||
2570 | if ( pFromCol == null ) |
||
2571 | { |
||
2572 | int iCol = p.nCol - 1; |
||
2573 | if ( NEVER( iCol < 0 ) ) |
||
2574 | goto fk_end; |
||
2575 | if ( pToCol != null && pToCol.nExpr != 1 ) |
||
2576 | { |
||
2577 | sqlite3ErrorMsg( pParse, "foreign key on %s" + |
||
2578 | " should reference only one column of table %T", |
||
2579 | p.aCol[iCol].zName, pTo ); |
||
2580 | goto fk_end; |
||
2581 | } |
||
2582 | nCol = 1; |
||
2583 | } |
||
2584 | else if ( pToCol != null && pToCol.nExpr != pFromCol.nExpr ) |
||
2585 | { |
||
2586 | sqlite3ErrorMsg( pParse, |
||
2587 | "number of columns in foreign key does not match the number of " + |
||
2588 | "columns in the referenced table" ); |
||
2589 | goto fk_end; |
||
2590 | } |
||
2591 | else |
||
2592 | { |
||
2593 | nCol = pFromCol.nExpr; |
||
2594 | } |
||
2595 | //nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey.aCol[0]) + pTo.n + 1; |
||
2596 | //if( pToCol ){ |
||
2597 | // for(i=0; i<pToCol.nExpr; i++){ |
||
2598 | // nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; |
||
2599 | // } |
||
2600 | //} |
||
2601 | pFKey = new FKey();//sqlite3DbMallocZero(db, nByte ); |
||
2602 | if ( pFKey == null ) |
||
2603 | { |
||
2604 | goto fk_end; |
||
2605 | } |
||
2606 | pFKey.pFrom = p; |
||
2607 | pFKey.pNextFrom = p.pFKey; |
||
2608 | //z = pFKey.aCol[nCol].zCol; |
||
2609 | pFKey.aCol = new FKey.sColMap[nCol];// z; |
||
2610 | pFKey.aCol[0] = new FKey.sColMap(); |
||
2611 | pFKey.zTo = pTo.z.Substring( 0, pTo.n ); //memcpy( z, pTo.z, pTo.n ); |
||
2612 | //z[pTo.n] = 0; |
||
2613 | sqlite3Dequote( ref pFKey.zTo ); |
||
2614 | //z += pTo.n + 1; |
||
2615 | pFKey.nCol = nCol; |
||
2616 | if ( pFromCol == null ) |
||
2617 | { |
||
2618 | pFKey.aCol[0].iFrom = p.nCol - 1; |
||
2619 | } |
||
2620 | else |
||
2621 | { |
||
2622 | for ( i = 0; i < nCol; i++ ) |
||
2623 | { |
||
2624 | if ( pFKey.aCol[i] == null ) |
||
2625 | pFKey.aCol[i] = new FKey.sColMap(); |
||
2626 | int j; |
||
2627 | for ( j = 0; j < p.nCol; j++ ) |
||
2628 | { |
||
2629 | if ( p.aCol[j].zName.Equals( pFromCol.a[i].zName, StringComparison.OrdinalIgnoreCase ) ) |
||
2630 | { |
||
2631 | pFKey.aCol[i].iFrom = j; |
||
2632 | break; |
||
2633 | } |
||
2634 | } |
||
2635 | if ( j >= p.nCol ) |
||
2636 | { |
||
2637 | sqlite3ErrorMsg( pParse, |
||
2638 | "unknown column \"%s\" in foreign key definition", |
||
2639 | pFromCol.a[i].zName ); |
||
2640 | goto fk_end; |
||
2641 | } |
||
2642 | } |
||
2643 | } |
||
2644 | if ( pToCol != null ) |
||
2645 | { |
||
2646 | for ( i = 0; i < nCol; i++ ) |
||
2647 | { |
||
2648 | ////int n = sqlite3Strlen30( pToCol.a[i].zName ); |
||
2649 | if ( pFKey.aCol[i] == null ) |
||
2650 | pFKey.aCol[i] = new FKey.sColMap(); |
||
2651 | pFKey.aCol[i].zCol = pToCol.a[i].zName; |
||
2652 | //memcpy( z, pToCol.a[i].zName, n ); |
||
2653 | //z[n] = 0; |
||
2654 | //z += n + 1; |
||
2655 | } |
||
2656 | } |
||
2657 | pFKey.isDeferred = 0; |
||
2658 | pFKey.aAction[0] = (u8)( flags & 0xff ); /* ON DELETE action */ |
||
2659 | pFKey.aAction[1] = (u8)( ( flags >> 8 ) & 0xff ); /* ON UPDATE action */ |
||
2660 | |||
2661 | Debug.Assert( sqlite3SchemaMutexHeld( db, 0, p.pSchema ) ); |
||
2662 | pNextTo = sqlite3HashInsert( ref p.pSchema.fkeyHash, |
||
2663 | pFKey.zTo, sqlite3Strlen30( pFKey.zTo ), pFKey |
||
2664 | ); |
||
2665 | //if( pNextTo==pFKey ){ |
||
2666 | // db.mallocFailed = 1; |
||
2667 | // goto fk_end; |
||
2668 | //} |
||
2669 | if ( pNextTo != null ) |
||
2670 | { |
||
2671 | Debug.Assert( pNextTo.pPrevTo == null ); |
||
2672 | pFKey.pNextTo = pNextTo; |
||
2673 | pNextTo.pPrevTo = pFKey; |
||
2674 | } |
||
2675 | /* Link the foreign key to the table as the last step. |
||
2676 | */ |
||
2677 | p.pFKey = pFKey; |
||
2678 | pFKey = null; |
||
2679 | |||
2680 | fk_end: |
||
2681 | sqlite3DbFree( db, ref pFKey ); |
||
2682 | #endif // * !SQLITE_OMIT_FOREIGN_KEY) */ |
||
2683 | sqlite3ExprListDelete( db, ref pFromCol ); |
||
2684 | sqlite3ExprListDelete( db, ref pToCol ); |
||
2685 | } |
||
2686 | |||
2687 | /* |
||
2688 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
||
2689 | ** clause is seen as part of a foreign key definition. The isDeferred |
||
2690 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
||
2691 | ** The behavior of the most recently created foreign key is adjusted |
||
2692 | ** accordingly. |
||
2693 | */ |
||
2694 | static void sqlite3DeferForeignKey( Parse pParse, int isDeferred ) |
||
2695 | { |
||
2696 | #if !SQLITE_OMIT_FOREIGN_KEY |
||
2697 | Table pTab; |
||
2698 | FKey pFKey; |
||
2699 | if ( ( pTab = pParse.pNewTable ) == null || ( pFKey = pTab.pFKey ) == null ) |
||
2700 | return; |
||
2701 | Debug.Assert( isDeferred == 0 || isDeferred == 1 ); /* EV: R-30323-21917 */ |
||
2702 | pFKey.isDeferred = (u8)isDeferred; |
||
2703 | #endif |
||
2704 | } |
||
2705 | |||
2706 | /* |
||
2707 | ** Generate code that will erase and refill index pIdx. This is |
||
2708 | ** used to initialize a newly created index or to recompute the |
||
2709 | ** content of an index in response to a REINDEX command. |
||
2710 | ** |
||
2711 | ** if memRootPage is not negative, it means that the index is newly |
||
2712 | ** created. The register specified by memRootPage contains the |
||
2713 | ** root page number of the index. If memRootPage is negative, then |
||
2714 | ** the index already exists and must be cleared before being refilled and |
||
2715 | ** the root page number of the index is taken from pIndex.tnum. |
||
2716 | */ |
||
2717 | static void sqlite3RefillIndex( Parse pParse, Index pIndex, int memRootPage ) |
||
2718 | { |
||
2719 | Table pTab = pIndex.pTable; /* The table that is indexed */ |
||
2720 | int iTab = pParse.nTab++; /* Btree cursor used for pTab */ |
||
2721 | int iIdx = pParse.nTab++; /* Btree cursor used for pIndex */ |
||
2722 | int addr1; /* Address of top of loop */ |
||
2723 | int tnum; /* Root page of index */ |
||
2724 | Vdbe v; /* Generate code into this virtual machine */ |
||
2725 | KeyInfo pKey; /* KeyInfo for index */ |
||
2726 | int regIdxKey; /* Registers containing the index key */ |
||
2727 | int regRecord; /* Register holding assemblied index record */ |
||
2728 | sqlite3 db = pParse.db; /* The database connection */ |
||
2729 | int iDb = sqlite3SchemaToIndex( db, pIndex.pSchema ); |
||
2730 | |||
2731 | #if !SQLITE_OMIT_AUTHORIZATION |
||
2732 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex.zName, 0, |
||
2733 | db.aDb[iDb].zName ) ){ |
||
2734 | return; |
||
2735 | } |
||
2736 | #endif |
||
2737 | |||
2738 | /* Require a write-lock on the table to perform this operation */ |
||
2739 | sqlite3TableLock( pParse, iDb, pTab.tnum, 1, pTab.zName ); |
||
2740 | v = sqlite3GetVdbe( pParse ); |
||
2741 | if ( v == null ) |
||
2742 | return; |
||
2743 | if ( memRootPage >= 0 ) |
||
2744 | { |
||
2745 | tnum = memRootPage; |
||
2746 | } |
||
2747 | else |
||
2748 | { |
||
2749 | tnum = pIndex.tnum; |
||
2750 | sqlite3VdbeAddOp2( v, OP_Clear, tnum, iDb ); |
||
2751 | } |
||
2752 | pKey = sqlite3IndexKeyinfo( pParse, pIndex ); |
||
2753 | sqlite3VdbeAddOp4( v, OP_OpenWrite, iIdx, tnum, iDb, |
||
2754 | pKey, P4_KEYINFO_HANDOFF ); |
||
2755 | if ( memRootPage >= 0 ) |
||
2756 | { |
||
2757 | sqlite3VdbeChangeP5( v, 1 ); |
||
2758 | } |
||
2759 | sqlite3OpenTable( pParse, iTab, iDb, pTab, OP_OpenRead ); |
||
2760 | addr1 = sqlite3VdbeAddOp2( v, OP_Rewind, iTab, 0 ); |
||
2761 | regRecord = sqlite3GetTempReg( pParse ); |
||
2762 | regIdxKey = sqlite3GenerateIndexKey( pParse, pIndex, iTab, regRecord, true ); |
||
2763 | if ( pIndex.onError != OE_None ) |
||
2764 | { |
||
2765 | int regRowid = regIdxKey + pIndex.nColumn; |
||
2766 | int j2 = sqlite3VdbeCurrentAddr( v ) + 2; |
||
2767 | int pRegKey = regIdxKey;// SQLITE_INT_TO_PTR( regIdxKey ); |
||
2768 | |||
2769 | /* The registers accessed by the OP_IsUnique opcode were allocated |
||
2770 | ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey() |
||
2771 | ** call above. Just before that function was freed they were released |
||
2772 | ** (made available to the compiler for reuse) using |
||
2773 | ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique |
||
2774 | ** opcode use the values stored within seems dangerous. However, since |
||
2775 | ** we can be sure that no other temp registers have been allocated |
||
2776 | ** since sqlite3ReleaseTempRange() was called, it is safe to do so. |
||
2777 | */ |
||
2778 | sqlite3VdbeAddOp4( v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32 ); |
||
2779 | sqlite3HaltConstraint( |
||
2780 | pParse, OE_Abort, "indexed columns are not unique", P4_STATIC ); |
||
2781 | } |
||
2782 | sqlite3VdbeAddOp2( v, OP_IdxInsert, iIdx, regRecord ); |
||
2783 | sqlite3VdbeChangeP5( v, OPFLAG_USESEEKRESULT ); |
||
2784 | sqlite3ReleaseTempReg( pParse, regRecord ); |
||
2785 | sqlite3VdbeAddOp2( v, OP_Next, iTab, addr1 + 1 ); |
||
2786 | sqlite3VdbeJumpHere( v, addr1 ); |
||
2787 | sqlite3VdbeAddOp1( v, OP_Close, iTab ); |
||
2788 | sqlite3VdbeAddOp1( v, OP_Close, iIdx ); |
||
2789 | } |
||
2790 | |||
2791 | /* |
||
2792 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
||
2793 | ** and pTblList is the name of the table that is to be indexed. Both will |
||
2794 | ** be NULL for a primary key or an index that is created to satisfy a |
||
2795 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse.pNewTable |
||
2796 | ** as the table to be indexed. pParse.pNewTable is a table that is |
||
2797 | ** currently being constructed by a CREATE TABLE statement. |
||
2798 | ** |
||
2799 | ** pList is a list of columns to be indexed. pList will be NULL if this |
||
2800 | ** is a primary key or unique-constraint on the most recent column added |
||
2801 | ** to the table currently under construction. |
||
2802 | ** |
||
2803 | ** If the index is created successfully, return a pointer to the new Index |
||
2804 | ** structure. This is used by sqlite3AddPrimaryKey() to mark the index |
||
2805 | ** as the tables primary key (Index.autoIndex==2). |
||
2806 | */ |
||
2807 | // OVERLOADS, so I don't need to rewrite parse.c |
||
2808 | static Index sqlite3CreateIndex( Parse pParse, int null_2, int null_3, int null_4, int null_5, int onError, int null_7, int null_8, int sortOrder, int ifNotExist ) |
||
2809 | { |
||
2810 | return sqlite3CreateIndex( pParse, null, null, null, null, onError, null, null, sortOrder, ifNotExist ); |
||
2811 | } |
||
2812 | static Index sqlite3CreateIndex( Parse pParse, int null_2, int null_3, int null_4, ExprList pList, int onError, int null_7, int null_8, int sortOrder, int ifNotExist ) |
||
2813 | { |
||
2814 | return sqlite3CreateIndex( pParse, null, null, null, pList, onError, null, null, sortOrder, ifNotExist ); |
||
2815 | } |
||
2816 | static Index sqlite3CreateIndex( |
||
2817 | Parse pParse, /* All information about this Parse */ |
||
2818 | Token pName1, /* First part of index name. May be NULL */ |
||
2819 | Token pName2, /* Second part of index name. May be NULL */ |
||
2820 | SrcList pTblName, /* Table to index. Use pParse.pNewTable if 0 */ |
||
2821 | ExprList pList, /* A list of columns to be indexed */ |
||
2822 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
||
2823 | Token pStart, /* The CREATE token that begins this statement */ |
||
2824 | Token pEnd, /* The ")" that closes the CREATE INDEX statement */ |
||
2825 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
||
2826 | int ifNotExist /* Omit error if index already exists */ |
||
2827 | ) |
||
2828 | { |
||
2829 | Index pRet = null; /* Pointer to return */ |
||
2830 | Table pTab = null; /* Table to be indexed */ |
||
2831 | Index pIndex = null; /* The index to be created */ |
||
2832 | string zName = null; /* Name of the index */ |
||
2833 | int nName; /* Number of characters in zName */ |
||
2834 | int i, j; |
||
2835 | Token nullId = new Token(); /* Fake token for an empty ID list */ |
||
2836 | DbFixer sFix = new DbFixer(); /* For assigning database names to pTable */ |
||
2837 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
||
2838 | sqlite3 db = pParse.db; |
||
2839 | Db pDb; /* The specific table containing the indexed database */ |
||
2840 | int iDb; /* Index of the database that is being written */ |
||
2841 | Token pName = null; /* Unqualified name of the index to create */ |
||
2842 | ExprList_item pListItem; /* For looping over pList */ |
||
2843 | int nCol; |
||
2844 | int nExtra = 0; |
||
2845 | StringBuilder zExtra = new StringBuilder(); |
||
2846 | |||
2847 | Debug.Assert( pStart == null || pEnd != null ); /* pEnd must be non-NULL if pStart is */ |
||
2848 | Debug.Assert( pParse.nErr == 0 ); /* Never called with prior errors */ |
||
2849 | if ( /* db.mallocFailed != 0 || */ |
||
2850 | IN_DECLARE_VTAB( pParse ) ) |
||
2851 | { |
||
2852 | goto exit_create_index; |
||
2853 | } |
||
2854 | if ( SQLITE_OK != sqlite3ReadSchema( pParse ) ) |
||
2855 | { |
||
2856 | goto exit_create_index; |
||
2857 | } |
||
2858 | |||
2859 | /* |
||
2860 | ** Find the table that is to be indexed. Return early if not found. |
||
2861 | */ |
||
2862 | if ( pTblName != null ) |
||
2863 | { |
||
2864 | |||
2865 | /* Use the two-part index name to determine the database |
||
2866 | ** to search for the table. 'Fix' the table name to this db |
||
2867 | ** before looking up the table. |
||
2868 | */ |
||
2869 | Debug.Assert( pName1 != null && pName2 != null ); |
||
2870 | iDb = sqlite3TwoPartName( pParse, pName1, pName2, ref pName ); |
||
2871 | if ( iDb < 0 ) |
||
2872 | goto exit_create_index; |
||
2873 | |||
2874 | #if !SQLITE_OMIT_TEMPDB |
||
2875 | /* If the index name was unqualified, check if the the table |
||
2876 | ** is a temp table. If so, set the database to 1. Do not do this |
||
2877 | ** if initialising a database schema. |
||
2878 | */ |
||
2879 | if ( 0 == db.init.busy ) |
||
2880 | { |
||
2881 | pTab = sqlite3SrcListLookup( pParse, pTblName ); |
||
2882 | if ( pName2.n == 0 && pTab != null && pTab.pSchema == db.aDb[1].pSchema ) |
||
2883 | { |
||
2884 | iDb = 1; |
||
2885 | } |
||
2886 | } |
||
2887 | #endif |
||
2888 | |||
2889 | if ( sqlite3FixInit( sFix, pParse, iDb, "index", pName ) != 0 && |
||
2890 | sqlite3FixSrcList( sFix, pTblName ) != 0 |
||
2891 | ) |
||
2892 | { |
||
2893 | /* Because the parser constructs pTblName from a single identifier, |
||
2894 | ** sqlite3FixSrcList can never fail. */ |
||
2895 | Debugger.Break(); |
||
2896 | } |
||
2897 | pTab = sqlite3LocateTable( pParse, 0, pTblName.a[0].zName, |
||
2898 | pTblName.a[0].zDatabase ); |
||
2899 | if ( pTab == null /*|| db.mallocFailed != 0 */ ) |
||
2900 | goto exit_create_index; |
||
2901 | Debug.Assert( db.aDb[iDb].pSchema == pTab.pSchema ); |
||
2902 | } |
||
2903 | else |
||
2904 | { |
||
2905 | Debug.Assert( pName == null ); |
||
2906 | pTab = pParse.pNewTable; |
||
2907 | if ( pTab == null ) |
||
2908 | goto exit_create_index; |
||
2909 | iDb = sqlite3SchemaToIndex( db, pTab.pSchema ); |
||
2910 | } |
||
2911 | pDb = db.aDb[iDb]; |
||
2912 | |||
2913 | Debug.Assert( pTab != null ); |
||
2914 | Debug.Assert( pParse.nErr == 0 ); |
||
2915 | |||
2916 | if ( pTab.zName.StartsWith( "sqlite_", System.StringComparison.OrdinalIgnoreCase ) |
||
2917 | && !pTab.zName.StartsWith( "sqlite_altertab_", System.StringComparison.OrdinalIgnoreCase ) ) |
||
2918 | { |
||
2919 | sqlite3ErrorMsg( pParse, "table %s may not be indexed", pTab.zName ); |
||
2920 | goto exit_create_index; |
||
2921 | } |
||
2922 | #if !SQLITE_OMIT_VIEW |
||
2923 | if ( pTab.pSelect != null ) |
||
2924 | { |
||
2925 | sqlite3ErrorMsg( pParse, "views may not be indexed" ); |
||
2926 | goto exit_create_index; |
||
2927 | } |
||
2928 | #endif |
||
2929 | if ( IsVirtual( pTab ) ) |
||
2930 | { |
||
2931 | sqlite3ErrorMsg( pParse, "virtual tables may not be indexed" ); |
||
2932 | goto exit_create_index; |
||
2933 | } |
||
2934 | |||
2935 | /* |
||
2936 | ** Find the name of the index. Make sure there is not already another |
||
2937 | ** index or table with the same name. |
||
2938 | ** |
||
2939 | ** Exception: If we are reading the names of permanent indices from the |
||
2940 | ** sqlite_master table (because some other process changed the schema) and |
||
2941 | ** one of the index names collides with the name of a temporary table or |
||
2942 | ** index, then we will continue to process this index. |
||
2943 | ** |
||
2944 | ** If pName==0 it means that we are |
||
2945 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
||
2946 | ** own name. |
||
2947 | */ |
||
2948 | if ( pName != null ) |
||
2949 | { |
||
2950 | zName = sqlite3NameFromToken( db, pName ); |
||
2951 | if ( zName == null ) |
||
2952 | goto exit_create_index; |
||
2953 | if ( SQLITE_OK != sqlite3CheckObjectName( pParse, zName ) ) |
||
2954 | { |
||
2955 | goto exit_create_index; |
||
2956 | } |
||
2957 | if ( 0 == db.init.busy ) |
||
2958 | { |
||
2959 | if ( sqlite3FindTable( db, zName, null ) != null ) |
||
2960 | { |
||
2961 | sqlite3ErrorMsg( pParse, "there is already a table named %s", zName ); |
||
2962 | goto exit_create_index; |
||
2963 | } |
||
2964 | } |
||
2965 | if ( sqlite3FindIndex( db, zName, pDb.zName ) != null ) |
||
2966 | { |
||
2967 | if ( ifNotExist == 0 ) |
||
2968 | { |
||
2969 | sqlite3ErrorMsg( pParse, "index %s already exists", zName ); |
||
2970 | } |
||
2971 | else |
||
2972 | { |
||
2973 | Debug.Assert( 0 == db.init.busy ); |
||
2974 | sqlite3CodeVerifySchema( pParse, iDb ); |
||
2975 | } |
||
2976 | goto exit_create_index; |
||
2977 | } |
||
2978 | } |
||
2979 | else |
||
2980 | { |
||
2981 | int n = 0; |
||
2982 | Index pLoop; |
||
2983 | for ( pLoop = pTab.pIndex, n = 1; pLoop != null; pLoop = pLoop.pNext, n++ ) |
||
2984 | { |
||
2985 | } |
||
2986 | zName = sqlite3MPrintf( db, "sqlite_autoindex_%s_%d", pTab.zName, n ); |
||
2987 | if ( zName == null ) |
||
2988 | { |
||
2989 | goto exit_create_index; |
||
2990 | } |
||
2991 | } |
||
2992 | |||
2993 | /* Check for authorization to create an index. |
||
2994 | */ |
||
2995 | #if !SQLITE_OMIT_AUTHORIZATION |
||
2996 | { |
||
2997 | string zDb = pDb.zName; |
||
2998 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
||
2999 | goto exit_create_index; |
||
3000 | } |
||
3001 | i = SQLITE_CREATE_INDEX; |
||
3002 | if( OMIT_TEMPDB ==0&& iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
||
3003 | if( sqlite3AuthCheck(pParse, i, zName, pTab.zName, zDb) ){ |
||
3004 | goto exit_create_index; |
||
3005 | } |
||
3006 | } |
||
3007 | #endif |
||
3008 | |||
3009 | /* If pList==0, it means this routine was called to make a primary |
||
3010 | ** key out of the last column added to the table under construction. |
||
3011 | ** So create a fake list to simulate this. |
||
3012 | */ |
||
3013 | if ( pList == null ) |
||
3014 | { |
||
3015 | nullId.z = pTab.aCol[pTab.nCol - 1].zName; |
||
3016 | nullId.n = sqlite3Strlen30( nullId.z ); |
||
3017 | pList = sqlite3ExprListAppend( pParse, null, null ); |
||
3018 | if ( pList == null ) |
||
3019 | goto exit_create_index; |
||
3020 | sqlite3ExprListSetName( pParse, pList, nullId, 0 ); |
||
3021 | pList.a[0].sortOrder = (u8)sortOrder; |
||
3022 | } |
||
3023 | |||
3024 | /* Figure out how many bytes of space are required to store explicitly |
||
3025 | ** specified collation sequence names. |
||
3026 | */ |
||
3027 | for ( i = 0; i < pList.nExpr; i++ ) |
||
3028 | { |
||
3029 | Expr pExpr = pList.a[i].pExpr; |
||
3030 | if ( pExpr != null ) |
||
3031 | { |
||
3032 | CollSeq pColl = pExpr.pColl; |
||
3033 | /* Either pColl!=0 or there was an OOM failure. But if an OOM |
||
3034 | ** failure we have quit before reaching this point. */ |
||
3035 | if ( ALWAYS( pColl != null ) ) |
||
3036 | { |
||
3037 | nExtra += ( 1 + sqlite3Strlen30( pColl.zName ) ); |
||
3038 | } |
||
3039 | } |
||
3040 | } |
||
3041 | |||
3042 | /* |
||
3043 | ** Allocate the index structure. |
||
3044 | */ |
||
3045 | nName = sqlite3Strlen30( zName ); |
||
3046 | nCol = pList.nExpr; |
||
3047 | pIndex = new Index(); |
||
3048 | // sqlite3DbMallocZero( db, |
||
3049 | // Index.Length + /* Index structure */ |
||
3050 | // sizeof( int ) * nCol + /* Index.aiColumn */ |
||
3051 | // sizeof( int ) * ( nCol + 1 ) + /* Index.aiRowEst */ |
||
3052 | // sizeof( char* ) * nCol + /* Index.azColl */ |
||
3053 | // u8.Length * nCol + /* Index.aSortOrder */ |
||
3054 | // nName + 1 + /* Index.zName */ |
||
3055 | // nExtra /* Collation sequence names */ |
||
3056 | //); |
||
3057 | //if ( db.mallocFailed != 0 ) |
||
3058 | //{ |
||
3059 | // goto exit_create_index; |
||
3060 | //} |
||
3061 | pIndex.azColl = new string[nCol + 1];//(char*)(pIndex[1]); |
||
3062 | pIndex.aiColumn = new int[nCol + 1];//(int )(pIndex->azColl[nCol]); |
||
3063 | pIndex.aiRowEst = new int[nCol + 1];//(unsigned )(pIndex->aiColumn[nCol]); |
||
3064 | pIndex.aSortOrder = new byte[nCol + 1];//(u8 )(pIndex->aiRowEst[nCol+1]); |
||
3065 | //pIndex.zName = null;// (char)( &pIndex->aSortOrder[nCol] ); |
||
3066 | zExtra = new StringBuilder( nName + 1 );// (char)( &pIndex.zName[nName + 1] ); |
||
3067 | if ( zName.Length == nName ) |
||
3068 | pIndex.zName = zName; |
||
3069 | else |
||
3070 | { |
||
3071 | pIndex.zName = zName.Substring( 0, nName ); |
||
3072 | }// memcpy( pIndex.zName, zName, nName + 1 ); |
||
3073 | pIndex.pTable = pTab; |
||
3074 | pIndex.nColumn = pList.nExpr; |
||
3075 | pIndex.onError = (u8)onError; |
||
3076 | pIndex.autoIndex = (u8)( pName == null ? 1 : 0 ); |
||
3077 | pIndex.pSchema = db.aDb[iDb].pSchema; |
||
3078 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
3079 | |||
3080 | /* Check to see if we should honor DESC requests on index columns |
||
3081 | */ |
||
3082 | if ( pDb.pSchema.file_format >= 4 ) |
||
3083 | { |
||
3084 | sortOrderMask = 1; /* Honor DESC */ |
||
3085 | } |
||
3086 | else |
||
3087 | { |
||
3088 | sortOrderMask = 0; /* Ignore DESC */ |
||
3089 | } |
||
3090 | |||
3091 | /* Scan the names of the columns of the table to be indexed and |
||
3092 | ** load the column indices into the Index structure. Report an error |
||
3093 | ** if any column is not found. |
||
3094 | ** |
||
3095 | ** TODO: Add a test to make sure that the same column is not named |
||
3096 | ** more than once within the same index. Only the first instance of |
||
3097 | ** the column will ever be used by the optimizer. Note that using the |
||
3098 | ** same column more than once cannot be an error because that would |
||
3099 | ** break backwards compatibility - it needs to be a warning. |
||
3100 | */ |
||
3101 | for ( i = 0; i < pList.nExpr; i++ ) |
||
3102 | {//, pListItem++){ |
||
3103 | pListItem = pList.a[i]; |
||
3104 | string zColName = pListItem.zName; |
||
3105 | Column pTabCol; |
||
3106 | byte requestedSortOrder; |
||
3107 | string zColl; /* Collation sequence name */ |
||
3108 | |||
3109 | for ( j = 0; j < pTab.nCol; j++ ) |
||
3110 | {//, pTabCol++){ |
||
3111 | pTabCol = pTab.aCol[j]; |
||
3112 | if ( zColName.Equals( pTabCol.zName, StringComparison.OrdinalIgnoreCase ) ) |
||
3113 | break; |
||
3114 | } |
||
3115 | if ( j >= pTab.nCol ) |
||
3116 | { |
||
3117 | sqlite3ErrorMsg( pParse, "table %s has no column named %s", |
||
3118 | pTab.zName, zColName ); |
||
3119 | pParse.checkSchema = 1; |
||
3120 | goto exit_create_index; |
||
3121 | } |
||
3122 | pIndex.aiColumn[i] = j; |
||
3123 | /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of |
||
3124 | ** the way the "idxlist" non-terminal is constructed by the parser, |
||
3125 | ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl |
||
3126 | ** must exist or else there must have been an OOM error. But if there |
||
3127 | ** was an OOM error, we would never reach this point. */ |
||
3128 | if ( pListItem.pExpr != null && ALWAYS( pListItem.pExpr.pColl ) ) |
||
3129 | { |
||
3130 | int nColl; |
||
3131 | zColl = pListItem.pExpr.pColl.zName; |
||
3132 | nColl = sqlite3Strlen30( zColl ); |
||
3133 | Debug.Assert( nExtra >= nColl ); |
||
3134 | zExtra = new StringBuilder( zColl.Substring( 0, nColl ) );// memcpy( zExtra, zColl, nColl ); |
||
3135 | zColl = zExtra.ToString(); |
||
3136 | //zExtra += nColl; |
||
3137 | nExtra -= nColl; |
||
3138 | } |
||
3139 | else |
||
3140 | { |
||
3141 | zColl = pTab.aCol[j].zColl; |
||
3142 | if ( zColl == null ) |
||
3143 | { |
||
3144 | zColl = db.pDfltColl.zName; |
||
3145 | } |
||
3146 | } |
||
3147 | if ( 0 == db.init.busy && sqlite3LocateCollSeq( pParse, zColl ) == null ) |
||
3148 | { |
||
3149 | goto exit_create_index; |
||
3150 | } |
||
3151 | pIndex.azColl[i] = zColl; |
||
3152 | requestedSortOrder = (u8)( ( pListItem.sortOrder & sortOrderMask ) != 0 ? 1 : 0 ); |
||
3153 | pIndex.aSortOrder[i] = (u8)requestedSortOrder; |
||
3154 | } |
||
3155 | sqlite3DefaultRowEst( pIndex ); |
||
3156 | |||
3157 | if ( pTab == pParse.pNewTable ) |
||
3158 | { |
||
3159 | /* This routine has been called to create an automatic index as a |
||
3160 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
||
3161 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
||
3162 | ** i.e. one of: |
||
3163 | ** |
||
3164 | ** CREATE TABLE t(x PRIMARY KEY, y); |
||
3165 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
||
3166 | ** |
||
3167 | ** Either way, check to see if the table already has such an index. If |
||
3168 | ** so, don't bother creating this one. This only applies to |
||
3169 | ** automatically created indices. Users can do as they wish with |
||
3170 | ** explicit indices. |
||
3171 | ** |
||
3172 | ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
||
3173 | ** (and thus suppressing the second one) even if they have different |
||
3174 | ** sort orders. |
||
3175 | ** |
||
3176 | ** If there are different collating sequences or if the columns of |
||
3177 | ** the constraint occur in different orders, then the constraints are |
||
3178 | ** considered distinct and both result in separate indices. |
||
3179 | */ |
||
3180 | Index pIdx; |
||
3181 | for ( pIdx = pTab.pIndex; pIdx != null; pIdx = pIdx.pNext ) |
||
3182 | { |
||
3183 | int k; |
||
3184 | Debug.Assert( pIdx.onError != OE_None ); |
||
3185 | Debug.Assert( pIdx.autoIndex != 0 ); |
||
3186 | Debug.Assert( pIndex.onError != OE_None ); |
||
3187 | |||
3188 | if ( pIdx.nColumn != pIndex.nColumn ) |
||
3189 | continue; |
||
3190 | for ( k = 0; k < pIdx.nColumn; k++ ) |
||
3191 | { |
||
3192 | string z1; |
||
3193 | string z2; |
||
3194 | if ( pIdx.aiColumn[k] != pIndex.aiColumn[k] ) |
||
3195 | break; |
||
3196 | z1 = pIdx.azColl[k]; |
||
3197 | z2 = pIndex.azColl[k]; |
||
3198 | if ( z1 != z2 && !z1.Equals( z2, StringComparison.OrdinalIgnoreCase ) ) |
||
3199 | break; |
||
3200 | } |
||
3201 | if ( k == pIdx.nColumn ) |
||
3202 | { |
||
3203 | if ( pIdx.onError != pIndex.onError ) |
||
3204 | { |
||
3205 | /* This constraint creates the same index as a previous |
||
3206 | ** constraint specified somewhere in the CREATE TABLE statement. |
||
3207 | ** However the ON CONFLICT clauses are different. If both this |
||
3208 | ** constraint and the previous equivalent constraint have explicit |
||
3209 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
||
3210 | ** explicitly specified behavior for the index. |
||
3211 | */ |
||
3212 | if ( !( pIdx.onError == OE_Default || pIndex.onError == OE_Default ) ) |
||
3213 | { |
||
3214 | sqlite3ErrorMsg( pParse, |
||
3215 | "conflicting ON CONFLICT clauses specified", 0 ); |
||
3216 | } |
||
3217 | if ( pIdx.onError == OE_Default ) |
||
3218 | { |
||
3219 | pIdx.onError = pIndex.onError; |
||
3220 | } |
||
3221 | } |
||
3222 | goto exit_create_index; |
||
3223 | } |
||
3224 | } |
||
3225 | } |
||
3226 | |||
3227 | /* Link the new Index structure to its table and to the other |
||
3228 | ** in-memory database structures. |
||
3229 | */ |
||
3230 | if ( db.init.busy != 0 ) |
||
3231 | { |
||
3232 | Index p; |
||
3233 | Debug.Assert( sqlite3SchemaMutexHeld( db, 0, pIndex.pSchema ) ); |
||
3234 | p = sqlite3HashInsert( ref pIndex.pSchema.idxHash, |
||
3235 | pIndex.zName, sqlite3Strlen30( pIndex.zName ), |
||
3236 | pIndex ); |
||
3237 | if ( p != null ) |
||
3238 | { |
||
3239 | Debug.Assert( p == pIndex ); /* Malloc must have failed */ |
||
3240 | // db.mallocFailed = 1; |
||
3241 | goto exit_create_index; |
||
3242 | } |
||
3243 | db.flags |= SQLITE_InternChanges; |
||
3244 | if ( pTblName != null ) |
||
3245 | { |
||
3246 | pIndex.tnum = db.init.newTnum; |
||
3247 | } |
||
3248 | } |
||
3249 | |||
3250 | /* If the db.init.busy is 0 then create the index on disk. This |
||
3251 | ** involves writing the index into the master table and filling in the |
||
3252 | ** index with the current table contents. |
||
3253 | ** |
||
3254 | ** The db.init.busy is 0 when the user first enters a CREATE INDEX |
||
3255 | ** command. db.init.busy is 1 when a database is opened and |
||
3256 | ** CREATE INDEX statements are read out of the master table. In |
||
3257 | ** the latter case the index already exists on disk, which is why |
||
3258 | ** we don't want to recreate it. |
||
3259 | ** |
||
3260 | ** If pTblName==0 it means this index is generated as a primary key |
||
3261 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
||
3262 | ** has just been created, it contains no data and the index initialization |
||
3263 | ** step can be skipped. |
||
3264 | */ |
||
3265 | else //if ( 0 == db.init.busy ) |
||
3266 | { |
||
3267 | Vdbe v; |
||
3268 | string zStmt; |
||
3269 | int iMem = ++pParse.nMem; |
||
3270 | |||
3271 | v = sqlite3GetVdbe( pParse ); |
||
3272 | if ( v == null ) |
||
3273 | goto exit_create_index; |
||
3274 | |||
3275 | |||
3276 | /* Create the rootpage for the index |
||
3277 | */ |
||
3278 | sqlite3BeginWriteOperation( pParse, 1, iDb ); |
||
3279 | sqlite3VdbeAddOp2( v, OP_CreateIndex, iDb, iMem ); |
||
3280 | |||
3281 | /* Gather the complete text of the CREATE INDEX statement into |
||
3282 | ** the zStmt variable |
||
3283 | */ |
||
3284 | if ( pStart != null ) |
||
3285 | { |
||
3286 | Debug.Assert( pEnd != null ); |
||
3287 | /* A named index with an explicit CREATE INDEX statement */ |
||
3288 | zStmt = sqlite3MPrintf( db, "CREATE%s INDEX %.*s", |
||
3289 | onError == OE_None ? string.Empty : " UNIQUE", |
||
3290 | (int)(pName.z.Length - pEnd.z.Length) + 1, |
||
3291 | pName.z ); |
||
3292 | } |
||
3293 | else |
||
3294 | { |
||
3295 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
||
3296 | /* zStmt = sqlite3MPrintf(string.Empty); */ |
||
3297 | zStmt = null; |
||
3298 | } |
||
3299 | |||
3300 | /* Add an entry in sqlite_master for this index |
||
3301 | */ |
||
3302 | sqlite3NestedParse( pParse, |
||
3303 | "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", |
||
3304 | db.aDb[iDb].zName, SCHEMA_TABLE( iDb ), |
||
3305 | pIndex.zName, |
||
3306 | pTab.zName, |
||
3307 | iMem, |
||
3308 | zStmt |
||
3309 | ); |
||
3310 | sqlite3DbFree( db, ref zStmt ); |
||
3311 | |||
3312 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
||
3313 | ** to invalidate all pre-compiled statements. |
||
3314 | */ |
||
3315 | if ( pTblName != null ) |
||
3316 | { |
||
3317 | sqlite3RefillIndex( pParse, pIndex, iMem ); |
||
3318 | sqlite3ChangeCookie( pParse, iDb ); |
||
3319 | sqlite3VdbeAddParseSchemaOp( v, iDb, |
||
3320 | sqlite3MPrintf( db, "name='%q' AND type='index'", pIndex.zName ) ); |
||
3321 | sqlite3VdbeAddOp1( v, OP_Expire, 0 ); |
||
3322 | } |
||
3323 | } |
||
3324 | |||
3325 | /* When adding an index to the list of indices for a table, make |
||
3326 | ** sure all indices labeled OE_Replace come after all those labeled |
||
3327 | ** OE_Ignore. This is necessary for the correct constraint check |
||
3328 | ** processing (in sqlite3GenerateConstraintChecks()) as part of |
||
3329 | ** UPDATE and INSERT statements. |
||
3330 | */ |
||
3331 | if ( db.init.busy != 0 || pTblName == null ) |
||
3332 | { |
||
3333 | if ( onError != OE_Replace || pTab.pIndex == null |
||
3334 | || pTab.pIndex.onError == OE_Replace ) |
||
3335 | { |
||
3336 | pIndex.pNext = pTab.pIndex; |
||
3337 | pTab.pIndex = pIndex; |
||
3338 | } |
||
3339 | else |
||
3340 | { |
||
3341 | Index pOther = pTab.pIndex; |
||
3342 | while ( pOther.pNext != null && pOther.pNext.onError != OE_Replace ) |
||
3343 | { |
||
3344 | pOther = pOther.pNext; |
||
3345 | } |
||
3346 | pIndex.pNext = pOther.pNext; |
||
3347 | pOther.pNext = pIndex; |
||
3348 | } |
||
3349 | pRet = pIndex; |
||
3350 | pIndex = null; |
||
3351 | } |
||
3352 | |||
3353 | /* Clean up before exiting */ |
||
3354 | exit_create_index: |
||
3355 | if ( pIndex != null ) |
||
3356 | { |
||
3357 | //sqlite3DbFree(db, ref pIndex.zColAff ); |
||
3358 | sqlite3DbFree( db, ref pIndex ); |
||
3359 | } |
||
3360 | sqlite3ExprListDelete( db, ref pList ); |
||
3361 | sqlite3SrcListDelete( db, ref pTblName ); |
||
3362 | sqlite3DbFree( db, ref zName ); |
||
3363 | return pRet; |
||
3364 | } |
||
3365 | |||
3366 | /* |
||
3367 | ** Fill the Index.aiRowEst[] array with default information - information |
||
3368 | ** to be used when we have not run the ANALYZE command. |
||
3369 | ** |
||
3370 | ** aiRowEst[0] is suppose to contain the number of elements in the index. |
||
3371 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
||
3372 | ** number of rows in the table that match any particular value of the |
||
3373 | ** first column of the index. aiRowEst[2] is an estimate of the number |
||
3374 | ** of rows that match any particular combiniation of the first 2 columns |
||
3375 | ** of the index. And so forth. It must always be the case that |
||
3376 | * |
||
3377 | ** aiRowEst[N]<=aiRowEst[N-1] |
||
3378 | ** aiRowEst[N]>=1 |
||
3379 | ** |
||
3380 | ** Apart from that, we have little to go on besides intuition as to |
||
3381 | ** how aiRowEst[] should be initialized. The numbers generated here |
||
3382 | ** are based on typical values found in actual indices. |
||
3383 | */ |
||
3384 | static void sqlite3DefaultRowEst( Index pIdx ) |
||
3385 | { |
||
3386 | int[] a = pIdx.aiRowEst; |
||
3387 | int i; |
||
3388 | int n; |
||
3389 | Debug.Assert( a != null ); |
||
3390 | a[0] = (int)pIdx.pTable.nRowEst; |
||
3391 | if ( a[0] < 10 ) |
||
3392 | a[0] = 10; |
||
3393 | n = 10; |
||
3394 | for ( i = 1; i <= pIdx.nColumn; i++ ) |
||
3395 | { |
||
3396 | a[i] = n; |
||
3397 | if ( n > 5 ) |
||
3398 | n--; |
||
3399 | } |
||
3400 | if ( pIdx.onError != OE_None ) |
||
3401 | { |
||
3402 | a[pIdx.nColumn] = 1; |
||
3403 | } |
||
3404 | } |
||
3405 | |||
3406 | /* |
||
3407 | ** This routine will drop an existing named index. This routine |
||
3408 | ** implements the DROP INDEX statement. |
||
3409 | */ |
||
3410 | static void sqlite3DropIndex( Parse pParse, SrcList pName, int ifExists ) |
||
3411 | { |
||
3412 | Index pIndex; |
||
3413 | Vdbe v; |
||
3414 | sqlite3 db = pParse.db; |
||
3415 | int iDb; |
||
3416 | |||
3417 | Debug.Assert( pParse.nErr == 0 ); /* Never called with prior errors */ |
||
3418 | //if ( db.mallocFailed != 0 ) |
||
3419 | //{ |
||
3420 | // goto exit_drop_index; |
||
3421 | //} |
||
3422 | Debug.Assert( pName.nSrc == 1 ); |
||
3423 | if ( SQLITE_OK != sqlite3ReadSchema( pParse ) ) |
||
3424 | { |
||
3425 | goto exit_drop_index; |
||
3426 | } |
||
3427 | pIndex = sqlite3FindIndex( db, pName.a[0].zName, pName.a[0].zDatabase ); |
||
3428 | if ( pIndex == null ) |
||
3429 | { |
||
3430 | if ( ifExists == 0 ) |
||
3431 | { |
||
3432 | sqlite3ErrorMsg( pParse, "no such index: %S", pName, 0 ); |
||
3433 | } |
||
3434 | else |
||
3435 | { |
||
3436 | sqlite3CodeVerifyNamedSchema( pParse, pName.a[0].zDatabase ); |
||
3437 | } |
||
3438 | pParse.checkSchema = 1; |
||
3439 | goto exit_drop_index; |
||
3440 | } |
||
3441 | if ( pIndex.autoIndex != 0 ) |
||
3442 | { |
||
3443 | sqlite3ErrorMsg( pParse, "index associated with UNIQUE " + |
||
3444 | "or PRIMARY KEY constraint cannot be dropped", 0 ); |
||
3445 | goto exit_drop_index; |
||
3446 | } |
||
3447 | iDb = sqlite3SchemaToIndex( db, pIndex.pSchema ); |
||
3448 | #if !SQLITE_OMIT_AUTHORIZATION |
||
3449 | { |
||
3450 | int code = SQLITE_DROP_INDEX; |
||
3451 | Table pTab = pIndex.pTable; |
||
3452 | string zDb = db.aDb[iDb].zName; |
||
3453 | string zTab = SCHEMA_TABLE(iDb); |
||
3454 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
||
3455 | goto exit_drop_index; |
||
3456 | } |
||
3457 | if( OMIT_TEMPDB ==0&& iDb ) code = SQLITE_DROP_TEMP_INDEX; |
||
3458 | if( sqlite3AuthCheck(pParse, code, pIndex.zName, pTab.zName, zDb) ){ |
||
3459 | goto exit_drop_index; |
||
3460 | } |
||
3461 | } |
||
3462 | #endif |
||
3463 | |||
3464 | /* Generate code to remove the index and from the master table */ |
||
3465 | v = sqlite3GetVdbe( pParse ); |
||
3466 | if ( v != null ) |
||
3467 | { |
||
3468 | sqlite3BeginWriteOperation( pParse, 1, iDb ); |
||
3469 | sqlite3NestedParse( pParse, |
||
3470 | "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", |
||
3471 | db.aDb[iDb].zName, SCHEMA_TABLE( iDb ), |
||
3472 | pIndex.zName |
||
3473 | ); |
||
3474 | if ( sqlite3FindTable( db, "sqlite_stat1", db.aDb[iDb].zName ) != null ) |
||
3475 | { |
||
3476 | sqlite3NestedParse( pParse, |
||
3477 | "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q", |
||
3478 | db.aDb[iDb].zName, pIndex.zName |
||
3479 | ); |
||
3480 | } |
||
3481 | sqlite3ChangeCookie( pParse, iDb ); |
||
3482 | destroyRootPage( pParse, pIndex.tnum, iDb ); |
||
3483 | sqlite3VdbeAddOp4( v, OP_DropIndex, iDb, 0, 0, pIndex.zName, 0 ); |
||
3484 | } |
||
3485 | |||
3486 | exit_drop_index: |
||
3487 | sqlite3SrcListDelete( db, ref pName ); |
||
3488 | } |
||
3489 | |||
3490 | /* |
||
3491 | ** pArray is a pointer to an array of objects. Each object in the |
||
3492 | ** array is szEntry bytes in size. This routine allocates a new |
||
3493 | ** object on the end of the array. |
||
3494 | ** |
||
3495 | ** pnEntry is the number of entries already in use. pnAlloc is |
||
3496 | ** the previously allocated size of the array. initSize is the |
||
3497 | ** suggested initial array size allocation. |
||
3498 | ** |
||
3499 | ** The index of the new entry is returned in pIdx. |
||
3500 | ** |
||
3501 | ** This routine returns a pointer to the array of objects. This |
||
3502 | ** might be the same as the pArray parameter or it might be a different |
||
3503 | ** pointer if the array was resized. |
||
3504 | */ |
||
3505 | static T[] sqlite3ArrayAllocate<T>( |
||
3506 | sqlite3 db, /* Connection to notify of malloc failures */ |
||
3507 | T[] pArray, /* Array of objects. Might be reallocated */ |
||
3508 | int szEntry, /* Size of each object in the array */ |
||
3509 | int initSize, /* Suggested initial allocation, in elements */ |
||
3510 | ref int pnEntry, /* Number of objects currently in use */ |
||
3511 | ref int pnAlloc, /* Current size of the allocation, in elements */ |
||
3512 | ref int pIdx /* Write the index of a new slot here */ |
||
3513 | ) where T : new() |
||
3514 | { |
||
3515 | //char* z; |
||
3516 | if ( pnEntry >= pnAlloc ) |
||
3517 | { |
||
3518 | //void* pNew; |
||
3519 | int newSize; |
||
3520 | newSize = ( pnAlloc ) * 2 + initSize; |
||
3521 | //pNew = sqlite3DbRealloc(db, pArray, newSize * szEntry); |
||
3522 | //if (pNew == 0) |
||
3523 | //{ |
||
3524 | // pIdx = -1; |
||
3525 | // return pArray; |
||
3526 | //} |
||
3527 | pnAlloc = newSize; //sqlite3DbMallocSize(db, pNew)/szEntry; |
||
3528 | //pArray = pNew; |
||
3529 | Array.Resize( ref pArray, newSize ); |
||
3530 | } |
||
3531 | pArray[pnEntry] = new T(); |
||
3532 | //z = (char)pArray; |
||
3533 | //memset(z[*pnEntry * szEntry], 0, szEntry); |
||
3534 | pIdx = pnEntry; |
||
3535 | ++pnEntry; |
||
3536 | return pArray; |
||
3537 | } |
||
3538 | |||
3539 | /* |
||
3540 | ** Append a new element to the given IdList. Create a new IdList if |
||
3541 | ** need be. |
||
3542 | ** |
||
3543 | ** A new IdList is returned, or NULL if malloc() fails. |
||
3544 | */ |
||
3545 | // OVERLOADS, so I don't need to rewrite parse.c |
||
3546 | static IdList sqlite3IdListAppend( sqlite3 db, int null_2, Token pToken ) |
||
3547 | { |
||
3548 | return sqlite3IdListAppend( db, null, pToken ); |
||
3549 | } |
||
3550 | static IdList sqlite3IdListAppend( sqlite3 db, IdList pList, Token pToken ) |
||
3551 | { |
||
3552 | int i = 0; |
||
3553 | if ( pList == null ) |
||
3554 | { |
||
3555 | pList = new IdList();//sqlite3DbMallocZero(db, sizeof(IdList)); |
||
3556 | if ( pList == null ) |
||
3557 | return null; |
||
3558 | pList.nAlloc = 0; |
||
3559 | } |
||
3560 | pList.a = (IdList_item[])sqlite3ArrayAllocate( |
||
3561 | db, |
||
3562 | pList.a, |
||
3563 | -1,//sizeof(pList.a[0]), |
||
3564 | 5, |
||
3565 | ref pList.nId, |
||
3566 | ref pList.nAlloc, |
||
3567 | ref i |
||
3568 | ); |
||
3569 | if ( i < 0 ) |
||
3570 | { |
||
3571 | sqlite3IdListDelete( db, ref pList ); |
||
3572 | return null; |
||
3573 | } |
||
3574 | pList.a[i].zName = sqlite3NameFromToken( db, pToken ); |
||
3575 | return pList; |
||
3576 | } |
||
3577 | |||
3578 | /* |
||
3579 | ** Delete an IdList. |
||
3580 | */ |
||
3581 | static void sqlite3IdListDelete( sqlite3 db, ref IdList pList ) |
||
3582 | { |
||
3583 | int i; |
||
3584 | if ( pList == null ) |
||
3585 | return; |
||
3586 | for ( i = 0; i < pList.nId; i++ ) |
||
3587 | { |
||
3588 | sqlite3DbFree( db, ref pList.a[i].zName ); |
||
3589 | } |
||
3590 | sqlite3DbFree( db, ref pList.a ); |
||
3591 | sqlite3DbFree( db, ref pList ); |
||
3592 | } |
||
3593 | |||
3594 | /* |
||
3595 | ** Return the index in pList of the identifier named zId. Return -1 |
||
3596 | ** if not found. |
||
3597 | */ |
||
3598 | static int sqlite3IdListIndex( IdList pList, string zName ) |
||
3599 | { |
||
3600 | int i; |
||
3601 | if ( pList == null ) |
||
3602 | return -1; |
||
3603 | for ( i = 0; i < pList.nId; i++ ) |
||
3604 | { |
||
3605 | if ( pList.a[i].zName.Equals( zName, StringComparison.OrdinalIgnoreCase ) ) |
||
3606 | return i; |
||
3607 | } |
||
3608 | return -1; |
||
3609 | } |
||
3610 | |||
3611 | /* |
||
3612 | ** Expand the space allocated for the given SrcList object by |
||
3613 | ** creating nExtra new slots beginning at iStart. iStart is zero based. |
||
3614 | ** New slots are zeroed. |
||
3615 | ** |
||
3616 | ** For example, suppose a SrcList initially contains two entries: A,B. |
||
3617 | ** To append 3 new entries onto the end, do this: |
||
3618 | ** |
||
3619 | ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
||
3620 | ** |
||
3621 | ** After the call above it would contain: A, B, nil, nil, nil. |
||
3622 | ** If the iStart argument had been 1 instead of 2, then the result |
||
3623 | ** would have been: A, nil, nil, nil, B. To prepend the new slots, |
||
3624 | ** the iStart value would be 0. The result then would |
||
3625 | ** be: nil, nil, nil, A, B. |
||
3626 | ** |
||
3627 | ** If a memory allocation fails the SrcList is unchanged. The |
||
3628 | ** db.mallocFailed flag will be set to true. |
||
3629 | */ |
||
3630 | static SrcList sqlite3SrcListEnlarge( |
||
3631 | sqlite3 db, /* Database connection to notify of OOM errors */ |
||
3632 | SrcList pSrc, /* The SrcList to be enlarged */ |
||
3633 | int nExtra, /* Number of new slots to add to pSrc.a[] */ |
||
3634 | int iStart /* Index in pSrc.a[] of first new slot */ |
||
3635 | ) |
||
3636 | { |
||
3637 | int i; |
||
3638 | |||
3639 | /* Sanity checking on calling parameters */ |
||
3640 | Debug.Assert( iStart >= 0 ); |
||
3641 | Debug.Assert( nExtra >= 1 ); |
||
3642 | Debug.Assert( pSrc != null ); |
||
3643 | Debug.Assert( iStart <= pSrc.nSrc ); |
||
3644 | |||
3645 | /* Allocate additional space if needed */ |
||
3646 | if ( pSrc.nSrc + nExtra > pSrc.nAlloc ) |
||
3647 | { |
||
3648 | int nAlloc = pSrc.nSrc + nExtra; |
||
3649 | int nGot; |
||
3650 | // sqlite3DbRealloc(db, pSrc, |
||
3651 | // sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc.a[0]) ); |
||
3652 | pSrc.nAlloc = (i16)nAlloc; |
||
3653 | Array.Resize( ref pSrc.a, nAlloc ); |
||
3654 | // nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
||
3655 | //pSrc->nAlloc = (u16)nGot; |
||
3656 | } |
||
3657 | |||
3658 | /* Move existing slots that come after the newly inserted slots |
||
3659 | ** out of the way */ |
||
3660 | for ( i = pSrc.nSrc - 1; i >= iStart; i-- ) |
||
3661 | { |
||
3662 | pSrc.a[i + nExtra] = pSrc.a[i]; |
||
3663 | } |
||
3664 | pSrc.nSrc += (i16)nExtra; |
||
3665 | |||
3666 | /* Zero the newly allocated slots */ |
||
3667 | //memset(&pSrc.a[iStart], 0, sizeof(pSrc.a[0])*nExtra); |
||
3668 | for ( i = iStart; i < iStart + nExtra; i++ ) |
||
3669 | { |
||
3670 | pSrc.a[i] = new SrcList_item(); |
||
3671 | pSrc.a[i].iCursor = -1; |
||
3672 | } |
||
3673 | |||
3674 | /* Return a pointer to the enlarged SrcList */ |
||
3675 | return pSrc; |
||
3676 | } |
||
3677 | |||
3678 | |||
3679 | /* |
||
3680 | ** Append a new table name to the given SrcList. Create a new SrcList if |
||
3681 | ** need be. A new entry is created in the SrcList even if pTable is NULL. |
||
3682 | ** |
||
3683 | ** A SrcList is returned, or NULL if there is an OOM error. The returned |
||
3684 | ** SrcList might be the same as the SrcList that was input or it might be |
||
3685 | ** a new one. If an OOM error does occurs, then the prior value of pList |
||
3686 | ** that is input to this routine is automatically freed. |
||
3687 | ** |
||
3688 | ** If pDatabase is not null, it means that the table has an optional |
||
3689 | ** database name prefix. Like this: "database.table". The pDatabase |
||
3690 | ** points to the table name and the pTable points to the database name. |
||
3691 | ** The SrcList.a[].zName field is filled with the table name which might |
||
3692 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
||
3693 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
||
3694 | ** or with NULL if no database is specified. |
||
3695 | ** |
||
3696 | ** In other words, if call like this: |
||
3697 | ** |
||
3698 | ** sqlite3SrcListAppend(D,A,B,0); |
||
3699 | ** |
||
3700 | ** Then B is a table name and the database name is unspecified. If called |
||
3701 | ** like this: |
||
3702 | ** |
||
3703 | ** sqlite3SrcListAppend(D,A,B,C); |
||
3704 | ** |
||
3705 | ** Then C is the table name and B is the database name. If C is defined |
||
3706 | ** then so is B. In other words, we never have a case where: |
||
3707 | ** |
||
3708 | ** sqlite3SrcListAppend(D,A,0,C); |
||
3709 | ** |
||
3710 | ** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
||
3711 | ** before being added to the SrcList. |
||
3712 | */ |
||
3713 | // OVERLOADS, so I don't need to rewrite parse.c |
||
3714 | static SrcList sqlite3SrcListAppend( sqlite3 db, int null_2, Token pTable, int null_4 ) |
||
3715 | { |
||
3716 | return sqlite3SrcListAppend( db, null, pTable, null ); |
||
3717 | } |
||
3718 | static SrcList sqlite3SrcListAppend( sqlite3 db, int null_2, Token pTable, Token pDatabase ) |
||
3719 | { |
||
3720 | return sqlite3SrcListAppend( db, null, pTable, pDatabase ); |
||
3721 | } |
||
3722 | static SrcList sqlite3SrcListAppend( |
||
3723 | sqlite3 db, /* Connection to notify of malloc failures */ |
||
3724 | SrcList pList, /* Append to this SrcList. NULL creates a new SrcList */ |
||
3725 | Token pTable, /* Table to append */ |
||
3726 | Token pDatabase /* Database of the table */ |
||
3727 | ) |
||
3728 | { |
||
3729 | SrcList_item pItem; |
||
3730 | Debug.Assert( pDatabase == null || pTable != null ); /* Cannot have C without B */ |
||
3731 | if ( pList == null ) |
||
3732 | { |
||
3733 | pList = new SrcList();//sqlite3DbMallocZero(db, SrcList.Length ); |
||
3734 | //if ( pList == null ) return null; |
||
3735 | pList.nAlloc = 1; |
||
3736 | pList.a = new SrcList_item[1]; |
||
3737 | } |
||
3738 | pList = sqlite3SrcListEnlarge( db, pList, 1, pList.nSrc ); |
||
3739 | //if ( db.mallocFailed != 0 ) |
||
3740 | //{ |
||
3741 | // sqlite3SrcListDelete( db, ref pList ); |
||
3742 | // return null; |
||
3743 | //} |
||
3744 | pItem = pList.a[pList.nSrc - 1]; |
||
3745 | if ( pDatabase != null && string.IsNullOrEmpty( pDatabase.z ) ) |
||
3746 | { |
||
3747 | pDatabase = null; |
||
3748 | } |
||
3749 | if ( pDatabase != null ) |
||
3750 | { |
||
3751 | Token pTemp = pDatabase; |
||
3752 | pDatabase = pTable; |
||
3753 | pTable = pTemp; |
||
3754 | } |
||
3755 | pItem.zName = sqlite3NameFromToken( db, pTable ); |
||
3756 | pItem.zDatabase = sqlite3NameFromToken( db, pDatabase ); |
||
3757 | return pList; |
||
3758 | } |
||
3759 | |||
3760 | /* |
||
3761 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
||
3762 | */ |
||
3763 | static void sqlite3SrcListAssignCursors( Parse pParse, SrcList pList ) |
||
3764 | { |
||
3765 | int i; |
||
3766 | SrcList_item pItem; |
||
3767 | Debug.Assert( pList != null /* || pParse.db.mallocFailed != 0 */ ); |
||
3768 | if ( pList != null ) |
||
3769 | { |
||
3770 | for ( i = 0; i < pList.nSrc; i++ ) |
||
3771 | { |
||
3772 | pItem = pList.a[i]; |
||
3773 | if ( pItem.iCursor >= 0 ) |
||
3774 | break; |
||
3775 | pItem.iCursor = pParse.nTab++; |
||
3776 | if ( pItem.pSelect != null ) |
||
3777 | { |
||
3778 | sqlite3SrcListAssignCursors( pParse, pItem.pSelect.pSrc ); |
||
3779 | } |
||
3780 | } |
||
3781 | } |
||
3782 | } |
||
3783 | |||
3784 | /* |
||
3785 | ** Delete an entire SrcList including all its substructure. |
||
3786 | */ |
||
3787 | static void sqlite3SrcListDelete( sqlite3 db, ref SrcList pList ) |
||
3788 | { |
||
3789 | int i; |
||
3790 | SrcList_item pItem; |
||
3791 | if ( pList == null ) |
||
3792 | return; |
||
3793 | for ( i = 0; i < pList.nSrc; i++ ) |
||
3794 | {//, pItem++){ |
||
3795 | pItem = pList.a[i]; |
||
3796 | sqlite3DbFree( db, ref pItem.zDatabase ); |
||
3797 | sqlite3DbFree( db, ref pItem.zName ); |
||
3798 | sqlite3DbFree( db, ref pItem.zAlias ); |
||
3799 | sqlite3DbFree( db, ref pItem.zIndex ); |
||
3800 | sqlite3DeleteTable( db, ref pItem.pTab ); |
||
3801 | sqlite3SelectDelete( db, ref pItem.pSelect ); |
||
3802 | sqlite3ExprDelete( db, ref pItem.pOn ); |
||
3803 | sqlite3IdListDelete( db, ref pItem.pUsing ); |
||
3804 | } |
||
3805 | sqlite3DbFree( db, ref pList ); |
||
3806 | } |
||
3807 | |||
3808 | /* |
||
3809 | ** This routine is called by the parser to add a new term to the |
||
3810 | ** end of a growing FROM clause. The "p" parameter is the part of |
||
3811 | ** the FROM clause that has already been constructed. "p" is NULL |
||
3812 | ** if this is the first term of the FROM clause. pTable and pDatabase |
||
3813 | ** are the name of the table and database named in the FROM clause term. |
||
3814 | ** pDatabase is NULL if the database name qualifier is missing - the |
||
3815 | ** usual case. If the term has a alias, then pAlias points to the |
||
3816 | ** alias token. If the term is a subquery, then pSubquery is the |
||
3817 | ** SELECT statement that the subquery encodes. The pTable and |
||
3818 | ** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
||
3819 | ** parameters are the content of the ON and USING clauses. |
||
3820 | ** |
||
3821 | ** Return a new SrcList which encodes is the FROM with the new |
||
3822 | ** term added. |
||
3823 | */ |
||
3824 | // OVERLOADS, so I don't need to rewrite parse.c |
||
3825 | static SrcList sqlite3SrcListAppendFromTerm( Parse pParse, SrcList p, int null_3, int null_4, Token pAlias, Select pSubquery, Expr pOn, IdList pUsing ) |
||
3826 | { |
||
3827 | return sqlite3SrcListAppendFromTerm( pParse, p, null, null, pAlias, pSubquery, pOn, pUsing ); |
||
3828 | } |
||
3829 | static SrcList sqlite3SrcListAppendFromTerm( Parse pParse, SrcList p, Token pTable, Token pDatabase, Token pAlias, int null_6, Expr pOn, IdList pUsing ) |
||
3830 | { |
||
3831 | return sqlite3SrcListAppendFromTerm( pParse, p, pTable, pDatabase, pAlias, null, pOn, pUsing ); |
||
3832 | } |
||
3833 | static SrcList sqlite3SrcListAppendFromTerm( |
||
3834 | Parse pParse, /* Parsing context */ |
||
3835 | SrcList p, /* The left part of the FROM clause already seen */ |
||
3836 | Token pTable, /* Name of the table to add to the FROM clause */ |
||
3837 | Token pDatabase, /* Name of the database containing pTable */ |
||
3838 | Token pAlias, /* The right-hand side of the AS subexpression */ |
||
3839 | Select pSubquery, /* A subquery used in place of a table name */ |
||
3840 | Expr pOn, /* The ON clause of a join */ |
||
3841 | IdList pUsing /* The USING clause of a join */ |
||
3842 | ) |
||
3843 | { |
||
3844 | SrcList_item pItem; |
||
3845 | sqlite3 db = pParse.db; |
||
3846 | if ( null == p && ( pOn != null || pUsing != null ) ) |
||
3847 | { |
||
3848 | sqlite3ErrorMsg( pParse, "a JOIN clause is required before %s", |
||
3849 | ( pOn != null ? "ON" : "USING" ) |
||
3850 | ); |
||
3851 | goto append_from_error; |
||
3852 | } |
||
3853 | p = sqlite3SrcListAppend( db, p, pTable, pDatabase ); |
||
3854 | //if ( p == null || NEVER( p.nSrc == 0 ) ) |
||
3855 | //{ |
||
3856 | // goto append_from_error; |
||
3857 | //} |
||
3858 | pItem = p.a[p.nSrc - 1]; |
||
3859 | Debug.Assert( pAlias != null ); |
||
3860 | if ( pAlias.n != 0 ) |
||
3861 | { |
||
3862 | pItem.zAlias = sqlite3NameFromToken( db, pAlias ); |
||
3863 | } |
||
3864 | pItem.pSelect = pSubquery; |
||
3865 | pItem.pOn = pOn; |
||
3866 | pItem.pUsing = pUsing; |
||
3867 | return p; |
||
3868 | append_from_error: |
||
3869 | Debug.Assert( p == null ); |
||
3870 | sqlite3ExprDelete( db, ref pOn ); |
||
3871 | sqlite3IdListDelete( db, ref pUsing ); |
||
3872 | sqlite3SelectDelete( db, ref pSubquery ); |
||
3873 | return null; |
||
3874 | } |
||
3875 | |||
3876 | /* |
||
3877 | ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
||
3878 | ** element of the source-list passed as the second argument. |
||
3879 | */ |
||
3880 | static void sqlite3SrcListIndexedBy( Parse pParse, SrcList p, Token pIndexedBy ) |
||
3881 | { |
||
3882 | Debug.Assert( pIndexedBy != null ); |
||
3883 | if ( p != null && ALWAYS( p.nSrc > 0 ) ) |
||
3884 | { |
||
3885 | SrcList_item pItem = p.a[p.nSrc - 1]; |
||
3886 | Debug.Assert( 0 == pItem.notIndexed && pItem.zIndex == null ); |
||
3887 | if ( pIndexedBy.n == 1 && null == pIndexedBy.z ) |
||
3888 | { |
||
3889 | /* A "NOT INDEXED" clause was supplied. See parse.y |
||
3890 | ** construct "indexed_opt" for details. */ |
||
3891 | pItem.notIndexed = 1; |
||
3892 | } |
||
3893 | else |
||
3894 | { |
||
3895 | pItem.zIndex = sqlite3NameFromToken( pParse.db, pIndexedBy ); |
||
3896 | } |
||
3897 | } |
||
3898 | } |
||
3899 | |||
3900 | /* |
||
3901 | ** When building up a FROM clause in the parser, the join operator |
||
3902 | ** is initially attached to the left operand. But the code generator |
||
3903 | ** expects the join operator to be on the right operand. This routine |
||
3904 | ** Shifts all join operators from left to right for an entire FROM |
||
3905 | ** clause. |
||
3906 | ** |
||
3907 | ** Example: Suppose the join is like this: |
||
3908 | ** |
||
3909 | ** A natural cross join B |
||
3910 | ** |
||
3911 | ** The operator is "natural cross join". The A and B operands are stored |
||
3912 | ** in p.a[0] and p.a[1], respectively. The parser initially stores the |
||
3913 | ** operator with A. This routine shifts that operator over to B. |
||
3914 | */ |
||
3915 | static void sqlite3SrcListShiftJoinType( SrcList p ) |
||
3916 | { |
||
3917 | if ( p != null && p.a != null ) |
||
3918 | { |
||
3919 | int i; |
||
3920 | for ( i = p.nSrc - 1; i > 0; i-- ) |
||
3921 | { |
||
3922 | p.a[i].jointype = p.a[i - 1].jointype; |
||
3923 | } |
||
3924 | p.a[0].jointype = 0; |
||
3925 | } |
||
3926 | } |
||
3927 | |||
3928 | /* |
||
3929 | ** Begin a transaction |
||
3930 | */ |
||
3931 | static void sqlite3BeginTransaction( Parse pParse, int type ) |
||
3932 | { |
||
3933 | sqlite3 db; |
||
3934 | Vdbe v; |
||
3935 | int i; |
||
3936 | |||
3937 | Debug.Assert( pParse != null ); |
||
3938 | db = pParse.db; |
||
3939 | Debug.Assert( db != null ); |
||
3940 | /* if( db.aDb[0].pBt==0 ) return; */ |
||
3941 | if ( sqlite3AuthCheck( pParse, SQLITE_TRANSACTION, "BEGIN", null, null ) != 0 ) |
||
3942 | { |
||
3943 | return; |
||
3944 | } |
||
3945 | v = sqlite3GetVdbe( pParse ); |
||
3946 | if ( v == null ) |
||
3947 | return; |
||
3948 | if ( type != TK_DEFERRED ) |
||
3949 | { |
||
3950 | for ( i = 0; i < db.nDb; i++ ) |
||
3951 | { |
||
3952 | sqlite3VdbeAddOp2( v, OP_Transaction, i, ( type == TK_EXCLUSIVE ) ? 2 : 1 ); |
||
3953 | sqlite3VdbeUsesBtree( v, i ); |
||
3954 | } |
||
3955 | } |
||
3956 | sqlite3VdbeAddOp2( v, OP_AutoCommit, 0, 0 ); |
||
3957 | } |
||
3958 | |||
3959 | /* |
||
3960 | ** Commit a transaction |
||
3961 | */ |
||
3962 | static void sqlite3CommitTransaction( Parse pParse ) |
||
3963 | { |
||
3964 | sqlite3 db; |
||
3965 | Vdbe v; |
||
3966 | |||
3967 | Debug.Assert( pParse != null ); |
||
3968 | db = pParse.db; |
||
3969 | Debug.Assert( db != null ); |
||
3970 | /* if( db.aDb[0].pBt==0 ) return; */ |
||
3971 | if ( sqlite3AuthCheck( pParse, SQLITE_TRANSACTION, "COMMIT", null, null ) != 0 ) |
||
3972 | { |
||
3973 | return; |
||
3974 | } |
||
3975 | v = sqlite3GetVdbe( pParse ); |
||
3976 | if ( v != null ) |
||
3977 | { |
||
3978 | sqlite3VdbeAddOp2( v, OP_AutoCommit, 1, 0 ); |
||
3979 | } |
||
3980 | } |
||
3981 | |||
3982 | /* |
||
3983 | ** Rollback a transaction |
||
3984 | */ |
||
3985 | static void sqlite3RollbackTransaction( Parse pParse ) |
||
3986 | { |
||
3987 | sqlite3 db; |
||
3988 | Vdbe v; |
||
3989 | |||
3990 | Debug.Assert( pParse != null ); |
||
3991 | db = pParse.db; |
||
3992 | Debug.Assert( db != null ); |
||
3993 | /* if( db.aDb[0].pBt==0 ) return; */ |
||
3994 | if ( sqlite3AuthCheck( pParse, SQLITE_TRANSACTION, "ROLLBACK", null, null ) != 0 ) |
||
3995 | { |
||
3996 | return; |
||
3997 | } |
||
3998 | v = sqlite3GetVdbe( pParse ); |
||
3999 | if ( v != null ) |
||
4000 | { |
||
4001 | sqlite3VdbeAddOp2( v, OP_AutoCommit, 1, 1 ); |
||
4002 | } |
||
4003 | } |
||
4004 | |||
4005 | /* |
||
4006 | ** This function is called by the parser when it parses a command to create, |
||
4007 | ** release or rollback an SQL savepoint. |
||
4008 | */ |
||
4009 | #if !SQLITE_OMIT_AUTHORIZATION |
||
4010 | const string[] az = { "BEGIN", "RELEASE", "ROLLBACK" }; |
||
4011 | #endif |
||
4012 | static void sqlite3Savepoint( Parse pParse, int op, Token pName ) |
||
4013 | { |
||
4014 | string zName = sqlite3NameFromToken( pParse.db, pName ); |
||
4015 | if ( zName != null ) |
||
4016 | { |
||
4017 | Vdbe v = sqlite3GetVdbe( pParse ); |
||
4018 | #if !SQLITE_OMIT_AUTHORIZATION |
||
4019 | Debug.Assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
||
4020 | #endif |
||
4021 | if ( null == v |
||
4022 | #if !SQLITE_OMIT_AUTHORIZATION |
||
4023 | || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) |
||
4024 | #endif |
||
4025 | ) |
||
4026 | { |
||
4027 | sqlite3DbFree( pParse.db, ref zName ); |
||
4028 | return; |
||
4029 | } |
||
4030 | sqlite3VdbeAddOp4( v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC ); |
||
4031 | } |
||
4032 | } |
||
4033 | |||
4034 | /* |
||
4035 | ** Make sure the TEMP database is open and available for use. Return |
||
4036 | ** the number of errors. Leave any error messages in the pParse structure. |
||
4037 | */ |
||
4038 | static int sqlite3OpenTempDatabase( Parse pParse ) |
||
4039 | { |
||
4040 | sqlite3 db = pParse.db; |
||
4041 | if ( db.aDb[1].pBt == null && pParse.explain == 0 ) |
||
4042 | { |
||
4043 | int rc; |
||
4044 | Btree pBt = null; |
||
4045 | const int flags = |
||
4046 | SQLITE_OPEN_READWRITE | |
||
4047 | SQLITE_OPEN_CREATE | |
||
4048 | SQLITE_OPEN_EXCLUSIVE | |
||
4049 | SQLITE_OPEN_DELETEONCLOSE | |
||
4050 | SQLITE_OPEN_TEMP_DB; |
||
4051 | |||
4052 | rc = sqlite3BtreeOpen(db.pVfs, null, db, ref pBt, 0, flags ); |
||
4053 | if ( rc != SQLITE_OK ) |
||
4054 | { |
||
4055 | sqlite3ErrorMsg( pParse, "unable to open a temporary database " + |
||
4056 | "file for storing temporary tables" ); |
||
4057 | pParse.rc = rc; |
||
4058 | return 1; |
||
4059 | } |
||
4060 | db.aDb[1].pBt = pBt; |
||
4061 | Debug.Assert( db.aDb[1].pSchema != null ); |
||
4062 | if ( SQLITE_NOMEM == sqlite3BtreeSetPageSize( pBt, db.nextPagesize, -1, 0 ) ) |
||
4063 | { |
||
4064 | // db.mallocFailed = 1; |
||
4065 | } |
||
4066 | } |
||
4067 | return 0; |
||
4068 | } |
||
4069 | |||
4070 | /* |
||
4071 | ** Generate VDBE code that will verify the schema cookie and start |
||
4072 | ** a read-transaction for all named database files. |
||
4073 | ** |
||
4074 | ** It is important that all schema cookies be verified and all |
||
4075 | ** read transactions be started before anything else happens in |
||
4076 | ** the VDBE program. But this routine can be called after much other |
||
4077 | ** code has been generated. So here is what we do: |
||
4078 | ** |
||
4079 | ** The first time this routine is called, we code an OP_Goto that |
||
4080 | ** will jump to a subroutine at the end of the program. Then we |
||
4081 | ** record every database that needs its schema verified in the |
||
4082 | ** pParse.cookieMask field. Later, after all other code has been |
||
4083 | ** generated, the subroutine that does the cookie verifications and |
||
4084 | ** starts the transactions will be coded and the OP_Goto P2 value |
||
4085 | ** will be made to point to that subroutine. The generation of the |
||
4086 | ** cookie verification subroutine code happens in sqlite3FinishCoding(). |
||
4087 | ** |
||
4088 | ** If iDb<0 then code the OP_Goto only - don't set flag to verify the |
||
4089 | ** schema on any databases. This can be used to position the OP_Goto |
||
4090 | ** early in the code, before we know if any database tables will be used. |
||
4091 | */ |
||
4092 | static void sqlite3CodeVerifySchema( Parse pParse, int iDb ) |
||
4093 | { |
||
4094 | Parse pToplevel = sqlite3ParseToplevel( pParse ); |
||
4095 | |||
4096 | if ( pToplevel.cookieGoto == 0 ) |
||
4097 | { |
||
4098 | Vdbe v = sqlite3GetVdbe( pToplevel ); |
||
4099 | if ( v == null ) |
||
4100 | return; /* This only happens if there was a prior error */ |
||
4101 | pToplevel.cookieGoto = sqlite3VdbeAddOp2( v, OP_Goto, 0, 0 ) + 1; |
||
4102 | } |
||
4103 | if ( iDb >= 0 ) |
||
4104 | { |
||
4105 | sqlite3 db = pToplevel.db; |
||
4106 | yDbMask mask; |
||
4107 | Debug.Assert( iDb < db.nDb ); |
||
4108 | Debug.Assert( db.aDb[iDb].pBt != null || iDb == 1 ); |
||
4109 | Debug.Assert( iDb < SQLITE_MAX_ATTACHED + 2 ); |
||
4110 | Debug.Assert( sqlite3SchemaMutexHeld( db, iDb, null ) ); |
||
4111 | mask = ( (yDbMask)1 ) << iDb; |
||
4112 | if ( ( pToplevel.cookieMask & mask ) == 0 ) |
||
4113 | { |
||
4114 | pToplevel.cookieMask |= mask; |
||
4115 | pToplevel.cookieValue[iDb] = db.aDb[iDb].pSchema.schema_cookie; |
||
4116 | if ( 0 == OMIT_TEMPDB && iDb == 1 ) |
||
4117 | { |
||
4118 | sqlite3OpenTempDatabase( pToplevel ); |
||
4119 | } |
||
4120 | } |
||
4121 | } |
||
4122 | } |
||
4123 | |||
4124 | /* |
||
4125 | ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
||
4126 | ** attached database. Otherwise, invoke it for the database named zDb only. |
||
4127 | */ |
||
4128 | static void sqlite3CodeVerifyNamedSchema( Parse pParse, string zDb ) |
||
4129 | { |
||
4130 | sqlite3 db = pParse.db; |
||
4131 | int i; |
||
4132 | for ( i = 0; i < db.nDb; i++ ) |
||
4133 | { |
||
4134 | Db pDb = db.aDb[i]; |
||
4135 | if ( pDb.pBt != null && ( null == zDb || 0 == zDb.CompareTo(pDb.zName ) ) ) |
||
4136 | { |
||
4137 | sqlite3CodeVerifySchema( pParse, i ); |
||
4138 | } |
||
4139 | } |
||
4140 | } |
||
4141 | /* |
||
4142 | ** Generate VDBE code that prepares for doing an operation that |
||
4143 | ** might change the database. |
||
4144 | ** |
||
4145 | ** This routine starts a new transaction if we are not already within |
||
4146 | ** a transaction. If we are already within a transaction, then a checkpoint |
||
4147 | ** is set if the setStatement parameter is true. A checkpoint should |
||
4148 | ** be set for operations that might fail (due to a constraint) part of |
||
4149 | ** the way through and which will need to undo some writes without having to |
||
4150 | ** rollback the whole transaction. For operations where all constraints |
||
4151 | ** can be checked before any changes are made to the database, it is never |
||
4152 | ** necessary to undo a write and the checkpoint should not be set. |
||
4153 | */ |
||
4154 | static void sqlite3BeginWriteOperation( Parse pParse, int setStatement, int iDb ) |
||
4155 | { |
||
4156 | Parse pToplevel = sqlite3ParseToplevel( pParse ); |
||
4157 | sqlite3CodeVerifySchema( pParse, iDb ); |
||
4158 | pToplevel.writeMask |= ( (yDbMask)1 ) << iDb; |
||
4159 | pToplevel.isMultiWrite |= (u8)setStatement; |
||
4160 | } |
||
4161 | |||
4162 | /* |
||
4163 | ** Indicate that the statement currently under construction might write |
||
4164 | ** more than one entry (example: deleting one row then inserting another, |
||
4165 | ** inserting multiple rows in a table, or inserting a row and index entries.) |
||
4166 | ** If an abort occurs after some of these writes have completed, then it will |
||
4167 | ** be necessary to undo the completed writes. |
||
4168 | */ |
||
4169 | static void sqlite3MultiWrite( Parse pParse ) |
||
4170 | { |
||
4171 | Parse pToplevel = sqlite3ParseToplevel( pParse ); |
||
4172 | pToplevel.isMultiWrite = 1; |
||
4173 | } |
||
4174 | |||
4175 | /* |
||
4176 | ** The code generator calls this routine if is discovers that it is |
||
4177 | ** possible to abort a statement prior to completion. In order to |
||
4178 | ** perform this abort without corrupting the database, we need to make |
||
4179 | ** sure that the statement is protected by a statement transaction. |
||
4180 | ** |
||
4181 | ** Technically, we only need to set the mayAbort flag if the |
||
4182 | ** isMultiWrite flag was previously set. There is a time dependency |
||
4183 | ** such that the abort must occur after the multiwrite. This makes |
||
4184 | ** some statements involving the REPLACE conflict resolution algorithm |
||
4185 | ** go a little faster. But taking advantage of this time dependency |
||
4186 | ** makes it more difficult to prove that the code is correct (in |
||
4187 | ** particular, it prevents us from writing an effective |
||
4188 | ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
||
4189 | ** to take the safe route and skip the optimization. |
||
4190 | */ |
||
4191 | static void sqlite3MayAbort( Parse pParse ) |
||
4192 | { |
||
4193 | Parse pToplevel = sqlite3ParseToplevel( pParse ); |
||
4194 | pToplevel.mayAbort = 1; |
||
4195 | } |
||
4196 | |||
4197 | /* |
||
4198 | ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
||
4199 | ** error. The onError parameter determines which (if any) of the statement |
||
4200 | ** and/or current transaction is rolled back. |
||
4201 | */ |
||
4202 | static void sqlite3HaltConstraint( Parse pParse, int onError, string p4, int p4type ) |
||
4203 | { |
||
4204 | Vdbe v = sqlite3GetVdbe( pParse ); |
||
4205 | if ( onError == OE_Abort ) |
||
4206 | { |
||
4207 | sqlite3MayAbort( pParse ); |
||
4208 | } |
||
4209 | sqlite3VdbeAddOp4( v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type ); |
||
4210 | } |
||
4211 | |||
4212 | static void sqlite3HaltConstraint( Parse pParse, int onError, byte[] p4, int p4type ) |
||
4213 | { |
||
4214 | Vdbe v = sqlite3GetVdbe( pParse ); |
||
4215 | if ( onError == OE_Abort ) |
||
4216 | { |
||
4217 | sqlite3MayAbort( pParse ); |
||
4218 | } |
||
4219 | sqlite3VdbeAddOp4( v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type ); |
||
4220 | } |
||
4221 | |||
4222 | /* |
||
4223 | ** Check to see if pIndex uses the collating sequence pColl. Return |
||
4224 | ** true if it does and false if it does not. |
||
4225 | */ |
||
4226 | #if !SQLITE_OMIT_REINDEX |
||
4227 | static bool collationMatch( string zColl, Index pIndex ) |
||
4228 | { |
||
4229 | int i; |
||
4230 | Debug.Assert( zColl != null ); |
||
4231 | for ( i = 0; i < pIndex.nColumn; i++ ) |
||
4232 | { |
||
4233 | string z = pIndex.azColl[i]; |
||
4234 | Debug.Assert( z != null ); |
||
4235 | if ( z.Equals( zColl, StringComparison.OrdinalIgnoreCase ) ) |
||
4236 | { |
||
4237 | return true; |
||
4238 | } |
||
4239 | } |
||
4240 | return false; |
||
4241 | } |
||
4242 | #endif |
||
4243 | |||
4244 | /* |
||
4245 | ** Recompute all indices of pTab that use the collating sequence pColl. |
||
4246 | ** If pColl == null then recompute all indices of pTab. |
||
4247 | */ |
||
4248 | #if !SQLITE_OMIT_REINDEX |
||
4249 | static void reindexTable( Parse pParse, Table pTab, string zColl ) |
||
4250 | { |
||
4251 | Index pIndex; /* An index associated with pTab */ |
||
4252 | |||
4253 | for ( pIndex = pTab.pIndex; pIndex != null; pIndex = pIndex.pNext ) |
||
4254 | { |
||
4255 | if ( zColl == null || collationMatch( zColl, pIndex ) ) |
||
4256 | { |
||
4257 | int iDb = sqlite3SchemaToIndex( pParse.db, pTab.pSchema ); |
||
4258 | sqlite3BeginWriteOperation( pParse, 0, iDb ); |
||
4259 | sqlite3RefillIndex( pParse, pIndex, -1 ); |
||
4260 | } |
||
4261 | } |
||
4262 | } |
||
4263 | #endif |
||
4264 | |||
4265 | /* |
||
4266 | ** Recompute all indices of all tables in all databases where the |
||
4267 | ** indices use the collating sequence pColl. If pColl == null then recompute |
||
4268 | ** all indices everywhere. |
||
4269 | */ |
||
4270 | #if !SQLITE_OMIT_REINDEX |
||
4271 | static void reindexDatabases( Parse pParse, string zColl ) |
||
4272 | { |
||
4273 | Db pDb; /* A single database */ |
||
4274 | int iDb; /* The database index number */ |
||
4275 | sqlite3 db = pParse.db; /* The database connection */ |
||
4276 | HashElem k; /* For looping over tables in pDb */ |
||
4277 | Table pTab; /* A table in the database */ |
||
4278 | |||
4279 | Debug.Assert( sqlite3BtreeHoldsAllMutexes( db ) ); /* Needed for schema access */ |
||
4280 | for ( iDb = 0; iDb < db.nDb; iDb++ )//, pDb++ ) |
||
4281 | { |
||
4282 | pDb = db.aDb[iDb]; |
||
4283 | Debug.Assert( pDb != null ); |
||
4284 | for ( k = pDb.pSchema.tblHash.first; k != null; k = k.next ) //for ( k = sqliteHashFirst( pDb.pSchema.tblHash ) ; k != null ; k = sqliteHashNext( k ) ) |
||
4285 | { |
||
4286 | pTab = (Table)k.data;// sqliteHashData( k ); |
||
4287 | reindexTable( pParse, pTab, zColl ); |
||
4288 | } |
||
4289 | } |
||
4290 | } |
||
4291 | #endif |
||
4292 | |||
4293 | /* |
||
4294 | ** Generate code for the REINDEX command. |
||
4295 | ** |
||
4296 | ** REINDEX -- 1 |
||
4297 | ** REINDEX <collation> -- 2 |
||
4298 | ** REINDEX ?<database>.?<tablename> -- 3 |
||
4299 | ** REINDEX ?<database>.?<indexname> -- 4 |
||
4300 | ** |
||
4301 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
||
4302 | ** Form 2 rebuilds all indices in all databases that use the named |
||
4303 | ** collating function. Forms 3 and 4 rebuild the named index or all |
||
4304 | ** indices associated with the named table. |
||
4305 | */ |
||
4306 | #if !SQLITE_OMIT_REINDEX |
||
4307 | // OVERLOADS, so I don't need to rewrite parse.c |
||
4308 | static void sqlite3Reindex( Parse pParse, int null_2, int null_3 ) |
||
4309 | { |
||
4310 | sqlite3Reindex( pParse, null, null ); |
||
4311 | } |
||
4312 | static void sqlite3Reindex( Parse pParse, Token pName1, Token pName2 ) |
||
4313 | { |
||
4314 | CollSeq pColl; /* Collating sequence to be reindexed, or NULL */ |
||
4315 | string z; /* Name of a table or index */ |
||
4316 | string zDb; /* Name of the database */ |
||
4317 | Table pTab; /* A table in the database */ |
||
4318 | Index pIndex; /* An index associated with pTab */ |
||
4319 | int iDb; /* The database index number */ |
||
4320 | sqlite3 db = pParse.db; /* The database connection */ |
||
4321 | Token pObjName = new Token(); /* Name of the table or index to be reindexed */ |
||
4322 | |||
4323 | /* Read the database schema. If an error occurs, leave an error message |
||
4324 | ** and code in pParse and return NULL. */ |
||
4325 | if ( SQLITE_OK != sqlite3ReadSchema( pParse ) ) |
||
4326 | { |
||
4327 | return; |
||
4328 | } |
||
4329 | |||
4330 | if ( pName1 == null ) |
||
4331 | { |
||
4332 | reindexDatabases( pParse, null ); |
||
4333 | return; |
||
4334 | } |
||
4335 | else if ( NEVER( pName2 == null ) || pName2.z == null || pName2.z.Length == 0 ) |
||
4336 | { |
||
4337 | string zColl; |
||
4338 | Debug.Assert( pName1.z != null ); |
||
4339 | zColl = sqlite3NameFromToken( pParse.db, pName1 ); |
||
4340 | if ( zColl == null ) |
||
4341 | return; |
||
4342 | pColl = sqlite3FindCollSeq( db, ENC( db ), zColl, 0 ); |
||
4343 | if ( pColl != null ) |
||
4344 | { |
||
4345 | reindexDatabases( pParse, zColl ); |
||
4346 | sqlite3DbFree( db, ref zColl ); |
||
4347 | return; |
||
4348 | } |
||
4349 | sqlite3DbFree( db, ref zColl ); |
||
4350 | } |
||
4351 | iDb = sqlite3TwoPartName( pParse, pName1, pName2, ref pObjName ); |
||
4352 | if ( iDb < 0 ) |
||
4353 | return; |
||
4354 | z = sqlite3NameFromToken( db, pObjName ); |
||
4355 | if ( z == null ) |
||
4356 | return; |
||
4357 | zDb = db.aDb[iDb].zName; |
||
4358 | pTab = sqlite3FindTable( db, z, zDb ); |
||
4359 | if ( pTab != null ) |
||
4360 | { |
||
4361 | reindexTable( pParse, pTab, null ); |
||
4362 | sqlite3DbFree( db, ref z ); |
||
4363 | return; |
||
4364 | } |
||
4365 | pIndex = sqlite3FindIndex( db, z, zDb ); |
||
4366 | sqlite3DbFree( db, ref z ); |
||
4367 | if ( pIndex != null ) |
||
4368 | { |
||
4369 | sqlite3BeginWriteOperation( pParse, 0, iDb ); |
||
4370 | sqlite3RefillIndex( pParse, pIndex, -1 ); |
||
4371 | return; |
||
4372 | } |
||
4373 | sqlite3ErrorMsg( pParse, "unable to identify the object to be reindexed" ); |
||
4374 | } |
||
4375 | #endif |
||
4376 | |||
4377 | /* |
||
4378 | ** Return a dynamicly allocated KeyInfo structure that can be used |
||
4379 | ** with OP_OpenRead or OP_OpenWrite to access database index pIdx. |
||
4380 | ** |
||
4381 | ** If successful, a pointer to the new structure is returned. In this case |
||
4382 | ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned |
||
4383 | ** pointer. If an error occurs (out of memory or missing collation |
||
4384 | ** sequence), NULL is returned and the state of pParse updated to reflect |
||
4385 | ** the error. |
||
4386 | */ |
||
4387 | static KeyInfo sqlite3IndexKeyinfo( Parse pParse, Index pIdx ) |
||
4388 | { |
||
4389 | int i; |
||
4390 | int nCol = pIdx.nColumn; |
||
4391 | //int nBytes = KeyInfo.Length + (nCol - 1) * CollSeq*.Length + nCol; |
||
4392 | sqlite3 db = pParse.db; |
||
4393 | KeyInfo pKey = new KeyInfo();// (KeyInfo)sqlite3DbMallocZero(db, nBytes); |
||
4394 | |||
4395 | if ( pKey != null ) |
||
4396 | { |
||
4397 | pKey.db = pParse.db; |
||
4398 | pKey.aSortOrder = new byte[nCol]; |
||
4399 | pKey.aColl = new CollSeq[nCol];// (u8)&(pKey.aColl[nCol]); |
||
4400 | // Debug.Assert(pKey.aSortOrder[nCol] == (((u8)pKey)[nBytes])); |
||
4401 | for ( i = 0; i < nCol; i++ ) |
||
4402 | { |
||
4403 | string zColl = pIdx.azColl[i]; |
||
4404 | Debug.Assert( zColl != null ); |
||
4405 | pKey.aColl[i] = sqlite3LocateCollSeq( pParse, zColl ); |
||
4406 | pKey.aSortOrder[i] = pIdx.aSortOrder[i]; |
||
4407 | } |
||
4408 | pKey.nField = (u16)nCol; |
||
4409 | } |
||
4410 | |||
4411 | if ( pParse.nErr != 0 ) |
||
4412 | { |
||
4413 | pKey = null; |
||
4414 | sqlite3DbFree( db, ref pKey ); |
||
4415 | } |
||
4416 | return pKey; |
||
4417 | } |
||
4418 | } |
||
4419 | } |