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