xref: /sqlite-3.40.0/src/vdbeblob.c (revision 87f500ce)
1 /*
2 ** 2007 May 1
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** This file contains code used to implement incremental BLOB I/O.
14 */
15 
16 #include "sqliteInt.h"
17 #include "vdbeInt.h"
18 
19 #ifndef SQLITE_OMIT_INCRBLOB
20 
21 /*
22 ** Valid sqlite3_blob* handles point to Incrblob structures.
23 */
24 typedef struct Incrblob Incrblob;
25 struct Incrblob {
26   int nByte;              /* Size of open blob, in bytes */
27   int iOffset;            /* Byte offset of blob in cursor data */
28   u16 iCol;               /* Table column this handle is open on */
29   BtCursor *pCsr;         /* Cursor pointing at blob row */
30   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
31   sqlite3 *db;            /* The associated database */
32   char *zDb;              /* Database name */
33   Table *pTab;            /* Table object */
34 };
35 
36 
37 /*
38 ** This function is used by both blob_open() and blob_reopen(). It seeks
39 ** the b-tree cursor associated with blob handle p to point to row iRow.
40 ** If successful, SQLITE_OK is returned and subsequent calls to
41 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
42 **
43 ** If an error occurs, or if the specified row does not exist or does not
44 ** contain a value of type TEXT or BLOB in the column nominated when the
45 ** blob handle was opened, then an error code is returned and *pzErr may
46 ** be set to point to a buffer containing an error message. It is the
47 ** responsibility of the caller to free the error message buffer using
48 ** sqlite3DbFree().
49 **
50 ** If an error does occur, then the b-tree cursor is closed. All subsequent
51 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
52 ** immediately return SQLITE_ABORT.
53 */
54 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
55   int rc;                         /* Error code */
56   char *zErr = 0;                 /* Error message */
57   Vdbe *v = (Vdbe *)p->pStmt;
58   sqlite3 *db = v->db;
59 
60   /* Set the value of register r[1] in the SQL statement to integer iRow.
61   ** This is done directly as a performance optimization
62   */
63   v->aMem[1].flags = MEM_Int;
64   v->aMem[1].u.i = iRow;
65 
66   /* If the statement has been run before (and is paused at the OP_ResultRow)
67   ** then back it up to the point where it does the OP_SeekRowid.  This could
68   ** have been down with an extra OP_Goto, but simply setting the program
69   ** counter is faster. */
70   if( v->pc>3 ){
71     v->pc = 3;
72     db->nVdbeExec++;
73     rc = sqlite3VdbeExec((Vdbe*)p->pStmt);
74     db->nVdbeExec--;
75   }else{
76     rc = sqlite3_step(p->pStmt);
77   }
78   if( rc==SQLITE_ROW ){
79     VdbeCursor *pC = v->apCsr[0];
80     u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
81     testcase( pC->nHdrParsed==p->iCol );
82     testcase( pC->nHdrParsed==p->iCol+1 );
83     if( type<12 ){
84       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
85           type==0?"null": type==7?"real": "integer"
86       );
87       rc = SQLITE_ERROR;
88       sqlite3_finalize(p->pStmt);
89       p->pStmt = 0;
90     }else{
91       p->iOffset = pC->aType[p->iCol + pC->nField];
92       p->nByte = sqlite3VdbeSerialTypeLen(type);
93       p->pCsr =  pC->uc.pCursor;
94       sqlite3BtreeIncrblobCursor(p->pCsr);
95     }
96   }
97 
98   if( rc==SQLITE_ROW ){
99     rc = SQLITE_OK;
100   }else if( p->pStmt ){
101     rc = sqlite3_finalize(p->pStmt);
102     p->pStmt = 0;
103     if( rc==SQLITE_OK ){
104       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
105       rc = SQLITE_ERROR;
106     }else{
107       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
108     }
109   }
110 
111   assert( rc!=SQLITE_OK || zErr==0 );
112   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
113 
114   *pzErr = zErr;
115   return rc;
116 }
117 
118 /*
119 ** Open a blob handle.
120 */
121 int sqlite3_blob_open(
122   sqlite3* db,            /* The database connection */
123   const char *zDb,        /* The attached database containing the blob */
124   const char *zTable,     /* The table containing the blob */
125   const char *zColumn,    /* The column containing the blob */
126   sqlite_int64 iRow,      /* The row containing the glob */
127   int wrFlag,             /* True -> read/write access, false -> read-only */
128   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
129 ){
130   int nAttempt = 0;
131   int iCol;               /* Index of zColumn in row-record */
132   int rc = SQLITE_OK;
133   char *zErr = 0;
134   Table *pTab;
135   Parse *pParse = 0;
136   Incrblob *pBlob = 0;
137 
138 #ifdef SQLITE_ENABLE_API_ARMOR
139   if( ppBlob==0 ){
140     return SQLITE_MISUSE_BKPT;
141   }
142 #endif
143   *ppBlob = 0;
144 #ifdef SQLITE_ENABLE_API_ARMOR
145   if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
146     return SQLITE_MISUSE_BKPT;
147   }
148 #endif
149   wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
150 
151   sqlite3_mutex_enter(db->mutex);
152 
153   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
154   if( !pBlob ) goto blob_open_out;
155   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
156   if( !pParse ) goto blob_open_out;
157 
158   do {
159     memset(pParse, 0, sizeof(Parse));
160     pParse->db = db;
161     sqlite3DbFree(db, zErr);
162     zErr = 0;
163 
164     sqlite3BtreeEnterAll(db);
165     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
166     if( pTab && IsVirtual(pTab) ){
167       pTab = 0;
168       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
169     }
170     if( pTab && !HasRowid(pTab) ){
171       pTab = 0;
172       sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
173     }
174 #ifndef SQLITE_OMIT_VIEW
175     if( pTab && pTab->pSelect ){
176       pTab = 0;
177       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
178     }
179 #endif
180     if( !pTab ){
181       if( pParse->zErrMsg ){
182         sqlite3DbFree(db, zErr);
183         zErr = pParse->zErrMsg;
184         pParse->zErrMsg = 0;
185       }
186       rc = SQLITE_ERROR;
187       sqlite3BtreeLeaveAll(db);
188       goto blob_open_out;
189     }
190     pBlob->pTab = pTab;
191     pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
192 
193     /* Now search pTab for the exact column. */
194     for(iCol=0; iCol<pTab->nCol; iCol++) {
195       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
196         break;
197       }
198     }
199     if( iCol==pTab->nCol ){
200       sqlite3DbFree(db, zErr);
201       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
202       rc = SQLITE_ERROR;
203       sqlite3BtreeLeaveAll(db);
204       goto blob_open_out;
205     }
206 
207     /* If the value is being opened for writing, check that the
208     ** column is not indexed, and that it is not part of a foreign key.
209     */
210     if( wrFlag ){
211       const char *zFault = 0;
212       Index *pIdx;
213 #ifndef SQLITE_OMIT_FOREIGN_KEY
214       if( db->flags&SQLITE_ForeignKeys ){
215         /* Check that the column is not part of an FK child key definition. It
216         ** is not necessary to check if it is part of a parent key, as parent
217         ** key columns must be indexed. The check below will pick up this
218         ** case.  */
219         FKey *pFKey;
220         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
221           int j;
222           for(j=0; j<pFKey->nCol; j++){
223             if( pFKey->aCol[j].iFrom==iCol ){
224               zFault = "foreign key";
225             }
226           }
227         }
228       }
229 #endif
230       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
231         int j;
232         for(j=0; j<pIdx->nKeyCol; j++){
233           /* FIXME: Be smarter about indexes that use expressions */
234           if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
235             zFault = "indexed";
236           }
237         }
238       }
239       if( zFault ){
240         sqlite3DbFree(db, zErr);
241         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
242         rc = SQLITE_ERROR;
243         sqlite3BtreeLeaveAll(db);
244         goto blob_open_out;
245       }
246     }
247 
248     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
249     assert( pBlob->pStmt || db->mallocFailed );
250     if( pBlob->pStmt ){
251 
252       /* This VDBE program seeks a btree cursor to the identified
253       ** db/table/row entry. The reason for using a vdbe program instead
254       ** of writing code to use the b-tree layer directly is that the
255       ** vdbe program will take advantage of the various transaction,
256       ** locking and error handling infrastructure built into the vdbe.
257       **
258       ** After seeking the cursor, the vdbe executes an OP_ResultRow.
259       ** Code external to the Vdbe then "borrows" the b-tree cursor and
260       ** uses it to implement the blob_read(), blob_write() and
261       ** blob_bytes() functions.
262       **
263       ** The sqlite3_blob_close() function finalizes the vdbe program,
264       ** which closes the b-tree cursor and (possibly) commits the
265       ** transaction.
266       */
267       static const int iLn = VDBE_OFFSET_LINENO(2);
268       static const VdbeOpList openBlob[] = {
269         {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
270         {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
271         /* blobSeekToRow() will initialize r[1] to the desired rowid */
272         {OP_NotExists,      0, 5, 1},  /* 2: Seek the cursor to rowid=r[1] */
273         {OP_Column,         0, 0, 1},  /* 3  */
274         {OP_ResultRow,      1, 0, 0},  /* 4  */
275         {OP_Halt,           0, 0, 0},  /* 5  */
276       };
277       Vdbe *v = (Vdbe *)pBlob->pStmt;
278       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
279       VdbeOp *aOp;
280 
281       sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
282                            pTab->pSchema->schema_cookie,
283                            pTab->pSchema->iGeneration);
284       sqlite3VdbeChangeP5(v, 1);
285       aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
286 
287       /* Make sure a mutex is held on the table to be accessed */
288       sqlite3VdbeUsesBtree(v, iDb);
289 
290       if( db->mallocFailed==0 ){
291         assert( aOp!=0 );
292         /* Configure the OP_TableLock instruction */
293 #ifdef SQLITE_OMIT_SHARED_CACHE
294         aOp[0].opcode = OP_Noop;
295 #else
296         aOp[0].p1 = iDb;
297         aOp[0].p2 = pTab->tnum;
298         aOp[0].p3 = wrFlag;
299         sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
300       }
301       if( db->mallocFailed==0 ){
302 #endif
303 
304         /* Remove either the OP_OpenWrite or OpenRead. Set the P2
305         ** parameter of the other to pTab->tnum.  */
306         if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
307         aOp[1].p2 = pTab->tnum;
308         aOp[1].p3 = iDb;
309 
310         /* Configure the number of columns. Configure the cursor to
311         ** think that the table has one more column than it really
312         ** does. An OP_Column to retrieve this imaginary column will
313         ** always return an SQL NULL. This is useful because it means
314         ** we can invoke OP_Column to fill in the vdbe cursors type
315         ** and offset cache without causing any IO.
316         */
317         aOp[1].p4type = P4_INT32;
318         aOp[1].p4.i = pTab->nCol+1;
319         aOp[3].p2 = pTab->nCol;
320 
321         pParse->nVar = 0;
322         pParse->nMem = 1;
323         pParse->nTab = 1;
324         sqlite3VdbeMakeReady(v, pParse);
325       }
326     }
327 
328     pBlob->iCol = iCol;
329     pBlob->db = db;
330     sqlite3BtreeLeaveAll(db);
331     if( db->mallocFailed ){
332       goto blob_open_out;
333     }
334     rc = blobSeekToRow(pBlob, iRow, &zErr);
335   } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
336 
337 blob_open_out:
338   if( rc==SQLITE_OK && db->mallocFailed==0 ){
339     *ppBlob = (sqlite3_blob *)pBlob;
340   }else{
341     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
342     sqlite3DbFree(db, pBlob);
343   }
344   sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
345   sqlite3DbFree(db, zErr);
346   sqlite3ParserReset(pParse);
347   sqlite3StackFree(db, pParse);
348   rc = sqlite3ApiExit(db, rc);
349   sqlite3_mutex_leave(db->mutex);
350   return rc;
351 }
352 
353 /*
354 ** Close a blob handle that was previously created using
355 ** sqlite3_blob_open().
356 */
357 int sqlite3_blob_close(sqlite3_blob *pBlob){
358   Incrblob *p = (Incrblob *)pBlob;
359   int rc;
360   sqlite3 *db;
361 
362   if( p ){
363     db = p->db;
364     sqlite3_mutex_enter(db->mutex);
365     rc = sqlite3_finalize(p->pStmt);
366     sqlite3DbFree(db, p);
367     sqlite3_mutex_leave(db->mutex);
368   }else{
369     rc = SQLITE_OK;
370   }
371   return rc;
372 }
373 
374 /*
375 ** Perform a read or write operation on a blob
376 */
377 static int blobReadWrite(
378   sqlite3_blob *pBlob,
379   void *z,
380   int n,
381   int iOffset,
382   int (*xCall)(BtCursor*, u32, u32, void*)
383 ){
384   int rc;
385   Incrblob *p = (Incrblob *)pBlob;
386   Vdbe *v;
387   sqlite3 *db;
388 
389   if( p==0 ) return SQLITE_MISUSE_BKPT;
390   db = p->db;
391   sqlite3_mutex_enter(db->mutex);
392   v = (Vdbe*)p->pStmt;
393 
394   if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
395     /* Request is out of range. Return a transient error. */
396     rc = SQLITE_ERROR;
397   }else if( v==0 ){
398     /* If there is no statement handle, then the blob-handle has
399     ** already been invalidated. Return SQLITE_ABORT in this case.
400     */
401     rc = SQLITE_ABORT;
402   }else{
403     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
404     ** returned, clean-up the statement handle.
405     */
406     assert( db == v->db );
407     sqlite3BtreeEnterCursor(p->pCsr);
408 
409 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
410     if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
411       /* If a pre-update hook is registered and this is a write cursor,
412       ** invoke it here.
413       **
414       ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
415       ** operation should really be an SQLITE_UPDATE. This is probably
416       ** incorrect, but is convenient because at this point the new.* values
417       ** are not easily obtainable. And for the sessions module, an
418       ** SQLITE_UPDATE where the PK columns do not change is handled in the
419       ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
420       ** slightly more efficient). Since you cannot write to a PK column
421       ** using the incremental-blob API, this works. For the sessions module
422       ** anyhow.
423       */
424       sqlite3_int64 iKey;
425       iKey = sqlite3BtreeIntegerKey(p->pCsr);
426       sqlite3VdbePreUpdateHook(
427           v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
428       );
429     }
430 #endif
431 
432     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
433     sqlite3BtreeLeaveCursor(p->pCsr);
434     if( rc==SQLITE_ABORT ){
435       sqlite3VdbeFinalize(v);
436       p->pStmt = 0;
437     }else{
438       v->rc = rc;
439     }
440   }
441   sqlite3Error(db, rc);
442   rc = sqlite3ApiExit(db, rc);
443   sqlite3_mutex_leave(db->mutex);
444   return rc;
445 }
446 
447 /*
448 ** Read data from a blob handle.
449 */
450 int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
451   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
452 }
453 
454 /*
455 ** Write data to a blob handle.
456 */
457 int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
458   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
459 }
460 
461 /*
462 ** Query a blob handle for the size of the data.
463 **
464 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
465 ** so no mutex is required for access.
466 */
467 int sqlite3_blob_bytes(sqlite3_blob *pBlob){
468   Incrblob *p = (Incrblob *)pBlob;
469   return (p && p->pStmt) ? p->nByte : 0;
470 }
471 
472 /*
473 ** Move an existing blob handle to point to a different row of the same
474 ** database table.
475 **
476 ** If an error occurs, or if the specified row does not exist or does not
477 ** contain a blob or text value, then an error code is returned and the
478 ** database handle error code and message set. If this happens, then all
479 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
480 ** immediately return SQLITE_ABORT.
481 */
482 int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
483   int rc;
484   Incrblob *p = (Incrblob *)pBlob;
485   sqlite3 *db;
486 
487   if( p==0 ) return SQLITE_MISUSE_BKPT;
488   db = p->db;
489   sqlite3_mutex_enter(db->mutex);
490 
491   if( p->pStmt==0 ){
492     /* If there is no statement handle, then the blob-handle has
493     ** already been invalidated. Return SQLITE_ABORT in this case.
494     */
495     rc = SQLITE_ABORT;
496   }else{
497     char *zErr;
498     rc = blobSeekToRow(p, iRow, &zErr);
499     if( rc!=SQLITE_OK ){
500       sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
501       sqlite3DbFree(db, zErr);
502     }
503     assert( rc!=SQLITE_SCHEMA );
504   }
505 
506   rc = sqlite3ApiExit(db, rc);
507   assert( rc==SQLITE_OK || p->pStmt==0 );
508   sqlite3_mutex_leave(db->mutex);
509   return rc;
510 }
511 
512 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
513