blob: d60a0d98c32bcb579da54f411789dd849bec434e [file] [log] [blame]
danielk1977b4e9af92007-05-01 17:49:49 +00001/*
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**
drh16a9b832007-05-05 18:39:25 +000013** This file contains code used to implement incremental BLOB I/O.
14**
drh6ee700c2009-06-01 19:53:30 +000015** $Id: vdbeblob.c,v 1.33 2009/06/01 19:53:31 drh Exp $
danielk1977b4e9af92007-05-01 17:49:49 +000016*/
17
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
21#ifndef SQLITE_OMIT_INCRBLOB
22
23/*
24** Valid sqlite3_blob* handles point to Incrblob structures.
25*/
26typedef struct Incrblob Incrblob;
danielk1977b4e9af92007-05-01 17:49:49 +000027struct Incrblob {
28 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
29 int nByte; /* Size of open blob, in bytes */
30 int iOffset; /* Byte offset of blob in cursor data */
31 BtCursor *pCsr; /* Cursor pointing at blob row */
32 sqlite3_stmt *pStmt; /* Statement holding cursor open */
drh605264d2007-08-21 15:13:19 +000033 sqlite3 *db; /* The associated database */
danielk1977b4e9af92007-05-01 17:49:49 +000034};
35
36/*
37** Open a blob handle.
38*/
39int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +000040 sqlite3* db, /* The database connection */
41 const char *zDb, /* The attached database containing the blob */
42 const char *zTable, /* The table containing the blob */
43 const char *zColumn, /* The column containing the blob */
44 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +000045 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +000046 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +000047){
danielk1977b4e9af92007-05-01 17:49:49 +000048 int nAttempt = 0;
49 int iCol; /* Index of zColumn in row-record */
50
51 /* This VDBE program seeks a btree cursor to the identified
52 ** db/table/row entry. The reason for using a vdbe program instead
53 ** of writing code to use the b-tree layer directly is that the
54 ** vdbe program will take advantage of the various transaction,
55 ** locking and error handling infrastructure built into the vdbe.
56 **
drh2d401ab2008-01-10 23:50:11 +000057 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +000058 ** Code external to the Vdbe then "borrows" the b-tree cursor and
59 ** uses it to implement the blob_read(), blob_write() and
60 ** blob_bytes() functions.
61 **
62 ** The sqlite3_blob_close() function finalizes the vdbe program,
63 ** which closes the b-tree cursor and (possibly) commits the
64 ** transaction.
65 */
66 static const VdbeOpList openBlob[] = {
67 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
68 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
danielk1977b4e9af92007-05-01 17:49:49 +000069
70 /* One of the following two instructions is replaced by an
71 ** OP_Noop before exection.
72 */
danielk1977d336e222009-02-20 10:58:41 +000073 {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */
74 {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +000075
danielk1977d336e222009-02-20 10:58:41 +000076 {OP_Variable, 1, 1, 1}, /* 4: Push the rowid to the stack */
77 {OP_NotExists, 0, 8, 1}, /* 5: Seek the cursor */
78 {OP_Column, 0, 0, 1}, /* 6 */
79 {OP_ResultRow, 1, 0, 0}, /* 7 */
80 {OP_Close, 0, 0, 0}, /* 8 */
81 {OP_Halt, 0, 0, 0}, /* 9 */
danielk1977b4e9af92007-05-01 17:49:49 +000082 };
83
84 Vdbe *v = 0;
danielk19778cbadb02007-05-03 16:31:26 +000085 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +000086 char *zErr = 0;
87 Table *pTab;
88 Parse *pParse;
danielk1977b4e9af92007-05-01 17:49:49 +000089
drh28414ee2009-05-09 15:17:47 +000090 *ppBlob = 0;
drh605264d2007-08-21 15:13:19 +000091 sqlite3_mutex_enter(db->mutex);
drh6ee700c2009-06-01 19:53:30 +000092 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
93 if( pParse==0 ){
94 rc = SQLITE_NOMEM;
95 goto blob_open_out;
96 }
danielk1977b4e9af92007-05-01 17:49:49 +000097 do {
drh6ee700c2009-06-01 19:53:30 +000098 memset(pParse, 0, sizeof(Parse));
99 pParse->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +0000100
drh93a960a2008-07-10 00:32:42 +0000101 if( sqlite3SafetyOn(db) ){
drh6ee700c2009-06-01 19:53:30 +0000102 sqlite3DbFree(db, zErr);
103 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000104 sqlite3_mutex_leave(db->mutex);
drh93a960a2008-07-10 00:32:42 +0000105 return SQLITE_MISUSE;
danielk197720713f32007-05-03 11:43:33 +0000106 }
107
drhff0587c2007-08-29 17:43:19 +0000108 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000109 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000110 if( pTab && IsVirtual(pTab) ){
111 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000112 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000113 }
114#ifndef SQLITE_OMIT_VIEW
115 if( pTab && pTab->pSelect ){
116 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000117 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000118 }
119#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000120 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000121 if( pParse->zErrMsg ){
122 sqlite3DbFree(db, zErr);
123 zErr = pParse->zErrMsg;
124 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000125 }
danielk19778cbadb02007-05-03 16:31:26 +0000126 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000127 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000128 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000129 goto blob_open_out;
130 }
131
132 /* Now search pTab for the exact column. */
133 for(iCol=0; iCol < pTab->nCol; iCol++) {
134 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
135 break;
136 }
137 }
138 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000139 sqlite3DbFree(db, zErr);
140 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000141 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000142 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000143 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000144 goto blob_open_out;
145 }
146
danielk1977f1819242007-05-03 18:14:10 +0000147 /* If the value is being opened for writing, check that the
148 ** column is not indexed. It is against the rules to open an
149 ** indexed column for writing.
150 */
151 if( flags ){
152 Index *pIdx;
153 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154 int j;
155 for(j=0; j<pIdx->nColumn; j++){
156 if( pIdx->aiColumn[j]==iCol ){
drh6ee700c2009-06-01 19:53:30 +0000157 sqlite3DbFree(db, zErr);
158 zErr = sqlite3MPrintf(db,
drh5bb3eb92007-05-04 13:15:55 +0000159 "cannot open indexed column for writing");
danielk1977f1819242007-05-03 18:14:10 +0000160 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000161 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000162 sqlite3BtreeLeaveAll(db);
danielk1977f1819242007-05-03 18:14:10 +0000163 goto blob_open_out;
164 }
165 }
166 }
167 }
168
danielk1977b4e9af92007-05-01 17:49:49 +0000169 v = sqlite3VdbeCreate(db);
170 if( v ){
171 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
172 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
173
174 /* Configure the OP_Transaction */
175 sqlite3VdbeChangeP1(v, 0, iDb);
176 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0));
177
178 /* Configure the OP_VerifyCookie */
179 sqlite3VdbeChangeP1(v, 1, iDb);
180 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
181
drhff0587c2007-08-29 17:43:19 +0000182 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000183 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000184
danielk1977f1819242007-05-03 18:14:10 +0000185 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
186 ** parameter of the other to pTab->tnum.
danielk1977b4e9af92007-05-01 17:49:49 +0000187 */
drh28414ee2009-05-09 15:17:47 +0000188 flags = !!flags;
189 sqlite3VdbeChangeToNoop(v, 3 - flags, 1);
190 sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
191 sqlite3VdbeChangeP3(v, 2 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000192
danielk1977d336e222009-02-20 10:58:41 +0000193 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000194 ** think that the table has one more column than it really
195 ** does. An OP_Column to retrieve this imaginary column will
196 ** always return an SQL NULL. This is useful because it means
197 ** we can invoke OP_Column to fill in the vdbe cursors type
198 ** and offset cache without causing any IO.
199 */
drh28414ee2009-05-09 15:17:47 +0000200 sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
danielk1977d336e222009-02-20 10:58:41 +0000201 sqlite3VdbeChangeP2(v, 6, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000202 if( !db->mallocFailed ){
drh2d401ab2008-01-10 23:50:11 +0000203 sqlite3VdbeMakeReady(v, 1, 1, 1, 0);
danielk197792d4d7a2007-05-04 12:05:56 +0000204 }
danielk1977b4e9af92007-05-01 17:49:49 +0000205 }
drhff0587c2007-08-29 17:43:19 +0000206
207 sqlite3BtreeLeaveAll(db);
danielk197720713f32007-05-03 11:43:33 +0000208 rc = sqlite3SafetyOff(db);
drh28414ee2009-05-09 15:17:47 +0000209 if( NEVER(rc!=SQLITE_OK) || db->mallocFailed ){
danielk197792d4d7a2007-05-04 12:05:56 +0000210 goto blob_open_out;
danielk197720713f32007-05-03 11:43:33 +0000211 }
212
danielk1977b4e9af92007-05-01 17:49:49 +0000213 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
214 rc = sqlite3_step((sqlite3_stmt *)v);
215 if( rc!=SQLITE_ROW ){
216 nAttempt++;
217 rc = sqlite3_finalize((sqlite3_stmt *)v);
drh6ee700c2009-06-01 19:53:30 +0000218 sqlite3DbFree(db, zErr);
219 zErr = sqlite3MPrintf(db, sqlite3_errmsg(db));
danielk1977b4e9af92007-05-01 17:49:49 +0000220 v = 0;
221 }
222 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
223
224 if( rc==SQLITE_ROW ){
225 /* The row-record has been opened successfully. Check that the
226 ** column in question contains text or a blob. If it contains
227 ** text, it is up to the caller to get the encoding right.
228 */
229 Incrblob *pBlob;
230 u32 type = v->apCsr[0]->aType[iCol];
231
232 if( type<12 ){
drh6ee700c2009-06-01 19:53:30 +0000233 sqlite3DbFree(db, zErr);
234 zErr = sqlite3MPrintf(db, "cannot open value of type %s",
danielk1977f1819242007-05-03 18:14:10 +0000235 type==0?"null": type==7?"real": "integer"
236 );
danielk1977b4e9af92007-05-01 17:49:49 +0000237 rc = SQLITE_ERROR;
238 goto blob_open_out;
239 }
drh17435752007-08-16 04:30:38 +0000240 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
241 if( db->mallocFailed ){
drh633e6d52008-07-28 19:34:53 +0000242 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000243 goto blob_open_out;
244 }
245 pBlob->flags = flags;
246 pBlob->pCsr = v->apCsr[0]->pCursor;
drhff0587c2007-08-29 17:43:19 +0000247 sqlite3BtreeEnterCursor(pBlob->pCsr);
danielk19772dec9702007-05-02 16:48:37 +0000248 sqlite3BtreeCacheOverflow(pBlob->pCsr);
drhff0587c2007-08-29 17:43:19 +0000249 sqlite3BtreeLeaveCursor(pBlob->pCsr);
danielk1977b4e9af92007-05-01 17:49:49 +0000250 pBlob->pStmt = (sqlite3_stmt *)v;
251 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
252 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
drh605264d2007-08-21 15:13:19 +0000253 pBlob->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +0000254 *ppBlob = (sqlite3_blob *)pBlob;
255 rc = SQLITE_OK;
danielk19778cbadb02007-05-03 16:31:26 +0000256 }else if( rc==SQLITE_OK ){
drh6ee700c2009-06-01 19:53:30 +0000257 sqlite3DbFree(db, zErr);
258 zErr = sqlite3MPrintf(db, "no such rowid: %lld", iRow);
danielk19778cbadb02007-05-03 16:31:26 +0000259 rc = SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +0000260 }
261
262blob_open_out:
danielk1977238746a2009-03-19 18:51:06 +0000263 if( v && (rc!=SQLITE_OK || db->mallocFailed) ){
264 sqlite3VdbeFinalize(v);
danielk1977b4e9af92007-05-01 17:49:49 +0000265 }
drh6ee700c2009-06-01 19:53:30 +0000266 sqlite3Error(db, rc, zErr);
267 sqlite3DbFree(db, zErr);
268 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000269 rc = sqlite3ApiExit(db, rc);
270 sqlite3_mutex_leave(db->mutex);
271 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000272}
273
274/*
drh16a9b832007-05-05 18:39:25 +0000275** Close a blob handle that was previously created using
276** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000277*/
278int sqlite3_blob_close(sqlite3_blob *pBlob){
279 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000280 int rc;
drhd9da78a2009-03-24 15:08:09 +0000281 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000282
drh28414ee2009-05-09 15:17:47 +0000283 if( p ){
284 db = p->db;
285 sqlite3_mutex_enter(db->mutex);
286 rc = sqlite3_finalize(p->pStmt);
287 sqlite3DbFree(db, p);
288 sqlite3_mutex_leave(db->mutex);
289 }else{
290 rc = SQLITE_OK;
291 }
drh605264d2007-08-21 15:13:19 +0000292 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000293}
294
drh605264d2007-08-21 15:13:19 +0000295/*
296** Perform a read or write operation on a blob
297*/
drhee858132007-05-08 20:37:38 +0000298static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000299 sqlite3_blob *pBlob,
300 void *z,
301 int n,
302 int iOffset,
303 int (*xCall)(BtCursor*, u32, u32, void*)
304){
305 int rc;
306 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000307 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000308 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000309
drh28414ee2009-05-09 15:17:47 +0000310 if( p==0 ) return SQLITE_MISUSE;
311 db = p->db;
drh605264d2007-08-21 15:13:19 +0000312 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000313 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000314
315 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
316 /* Request is out of range. Return a transient error. */
317 rc = SQLITE_ERROR;
318 sqlite3Error(db, SQLITE_ERROR, 0);
319 } else if( v==0 ){
320 /* If there is no statement handle, then the blob-handle has
321 ** already been invalidated. Return SQLITE_ABORT in this case.
322 */
drh605264d2007-08-21 15:13:19 +0000323 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000324 }else{
drh605264d2007-08-21 15:13:19 +0000325 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
326 ** returned, clean-up the statement handle.
327 */
328 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000329 sqlite3BtreeEnterCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000330 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000331 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000332 if( rc==SQLITE_ABORT ){
333 sqlite3VdbeFinalize(v);
334 p->pStmt = 0;
335 }else{
336 db->errCode = rc;
337 v->rc = rc;
338 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000339 }
drh605264d2007-08-21 15:13:19 +0000340 rc = sqlite3ApiExit(db, rc);
341 sqlite3_mutex_leave(db->mutex);
342 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000343}
344
danielk1977b4e9af92007-05-01 17:49:49 +0000345/*
346** Read data from a blob handle.
347*/
348int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000349 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000350}
351
352/*
353** Write data to a blob handle.
354*/
355int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000356 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000357}
358
359/*
360** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000361**
362** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
363** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000364*/
365int sqlite3_blob_bytes(sqlite3_blob *pBlob){
366 Incrblob *p = (Incrblob *)pBlob;
drh28414ee2009-05-09 15:17:47 +0000367 return p ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000368}
369
370#endif /* #ifndef SQLITE_OMIT_INCRBLOB */