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