blob: c0df14e06051b7a0c21cba481d48b3f70482ca49 [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**
danielk197799e925d2008-06-16 14:19:57 +000015** $Id: vdbeblob.c,v 1.23 2008/06/16 14:19:58 danielk1977 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 */
danielk1977cd3e8f72008-03-25 09:47:35 +000073 {OP_SetNumColumns, 0, 0, 0}, /* 2: Num cols for cursor */
74 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
danielk1977207872a2008-01-03 07:54:23 +000075 {OP_SetNumColumns, 0, 0, 0}, /* 4: Num cols for cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +000076 {OP_OpenWrite, 0, 0, 0}, /* 5: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +000077
danielk1977cd3e8f72008-03-25 09:47:35 +000078 {OP_Variable, 1, 1, 0}, /* 6: Push the rowid to the stack */
79 {OP_NotExists, 0, 10, 1}, /* 7: Seek the cursor */
80 {OP_Column, 0, 0, 1}, /* 8 */
81 {OP_ResultRow, 1, 0, 0}, /* 9 */
82 {OP_Close, 0, 0, 0}, /* 10 */
83 {OP_Halt, 0, 0, 0}, /* 11 */
danielk1977b4e9af92007-05-01 17:49:49 +000084 };
85
86 Vdbe *v = 0;
danielk19778cbadb02007-05-03 16:31:26 +000087 int rc = SQLITE_OK;
drh5bb3eb92007-05-04 13:15:55 +000088 char zErr[128];
danielk1977b4e9af92007-05-01 17:49:49 +000089
drh5bb3eb92007-05-04 13:15:55 +000090 zErr[0] = 0;
drh605264d2007-08-21 15:13:19 +000091 sqlite3_mutex_enter(db->mutex);
danielk1977b4e9af92007-05-01 17:49:49 +000092 do {
93 Parse sParse;
94 Table *pTab;
95
96 memset(&sParse, 0, sizeof(Parse));
97 sParse.db = db;
98
danielk197720713f32007-05-03 11:43:33 +000099 rc = sqlite3SafetyOn(db);
100 if( rc!=SQLITE_OK ){
drh605264d2007-08-21 15:13:19 +0000101 sqlite3_mutex_leave(db->mutex);
danielk197720713f32007-05-03 11:43:33 +0000102 return rc;
103 }
104
drhff0587c2007-08-29 17:43:19 +0000105 sqlite3BtreeEnterAll(db);
drhca424112008-01-25 15:04:48 +0000106 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000107 if( pTab && IsVirtual(pTab) ){
108 pTab = 0;
109 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
110 }
111#ifndef SQLITE_OMIT_VIEW
112 if( pTab && pTab->pSelect ){
113 pTab = 0;
114 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
115 }
116#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000117 if( !pTab ){
danielk19778cbadb02007-05-03 16:31:26 +0000118 if( sParse.zErrMsg ){
119 sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg);
danielk19778cbadb02007-05-03 16:31:26 +0000120 }
drh17435752007-08-16 04:30:38 +0000121 sqlite3_free(sParse.zErrMsg);
danielk19778cbadb02007-05-03 16:31:26 +0000122 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000123 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000124 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000125 goto blob_open_out;
126 }
127
128 /* Now search pTab for the exact column. */
129 for(iCol=0; iCol < pTab->nCol; iCol++) {
130 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
131 break;
132 }
133 }
134 if( iCol==pTab->nCol ){
danielk1977f1819242007-05-03 18:14:10 +0000135 sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000136 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000137 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000138 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000139 goto blob_open_out;
140 }
141
danielk1977f1819242007-05-03 18:14:10 +0000142 /* If the value is being opened for writing, check that the
143 ** column is not indexed. It is against the rules to open an
144 ** indexed column for writing.
145 */
146 if( flags ){
147 Index *pIdx;
148 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149 int j;
150 for(j=0; j<pIdx->nColumn; j++){
151 if( pIdx->aiColumn[j]==iCol ){
drh5bb3eb92007-05-04 13:15:55 +0000152 sqlite3_snprintf(sizeof(zErr), zErr,
153 "cannot open indexed column for writing");
danielk1977f1819242007-05-03 18:14:10 +0000154 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000155 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000156 sqlite3BtreeLeaveAll(db);
danielk1977f1819242007-05-03 18:14:10 +0000157 goto blob_open_out;
158 }
159 }
160 }
161 }
162
danielk1977b4e9af92007-05-01 17:49:49 +0000163 v = sqlite3VdbeCreate(db);
164 if( v ){
165 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
166 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
167
168 /* Configure the OP_Transaction */
169 sqlite3VdbeChangeP1(v, 0, iDb);
170 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0));
171
172 /* Configure the OP_VerifyCookie */
173 sqlite3VdbeChangeP1(v, 1, iDb);
174 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
175
drhff0587c2007-08-29 17:43:19 +0000176 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000177 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000178
danielk1977f1819242007-05-03 18:14:10 +0000179 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
180 ** parameter of the other to pTab->tnum.
danielk1977b4e9af92007-05-01 17:49:49 +0000181 */
danielk1977cd3e8f72008-03-25 09:47:35 +0000182 sqlite3VdbeChangeToNoop(v, (flags ? 3 : 5), 1);
183 sqlite3VdbeChangeP2(v, (flags ? 5 : 3), pTab->tnum);
184 sqlite3VdbeChangeP3(v, (flags ? 5 : 3), iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000185
186 /* Configure the OP_SetNumColumns. Configure the cursor to
187 ** think that the table has one more column than it really
188 ** does. An OP_Column to retrieve this imaginary column will
189 ** always return an SQL NULL. This is useful because it means
190 ** we can invoke OP_Column to fill in the vdbe cursors type
191 ** and offset cache without causing any IO.
192 */
danielk1977cd3e8f72008-03-25 09:47:35 +0000193 sqlite3VdbeChangeP2(v, flags ? 4 : 2, pTab->nCol+1);
danielk197799e925d2008-06-16 14:19:57 +0000194 sqlite3VdbeChangeP2(v, 8, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000195 if( !db->mallocFailed ){
drh2d401ab2008-01-10 23:50:11 +0000196 sqlite3VdbeMakeReady(v, 1, 1, 1, 0);
danielk197792d4d7a2007-05-04 12:05:56 +0000197 }
danielk1977b4e9af92007-05-01 17:49:49 +0000198 }
drhff0587c2007-08-29 17:43:19 +0000199
200 sqlite3BtreeLeaveAll(db);
danielk197720713f32007-05-03 11:43:33 +0000201 rc = sqlite3SafetyOff(db);
drh17435752007-08-16 04:30:38 +0000202 if( rc!=SQLITE_OK || db->mallocFailed ){
danielk197792d4d7a2007-05-04 12:05:56 +0000203 goto blob_open_out;
danielk197720713f32007-05-03 11:43:33 +0000204 }
205
danielk1977b4e9af92007-05-01 17:49:49 +0000206 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
207 rc = sqlite3_step((sqlite3_stmt *)v);
208 if( rc!=SQLITE_ROW ){
209 nAttempt++;
210 rc = sqlite3_finalize((sqlite3_stmt *)v);
danielk1977f1819242007-05-03 18:14:10 +0000211 sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db));
danielk1977b4e9af92007-05-01 17:49:49 +0000212 v = 0;
213 }
214 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
215
216 if( rc==SQLITE_ROW ){
217 /* The row-record has been opened successfully. Check that the
218 ** column in question contains text or a blob. If it contains
219 ** text, it is up to the caller to get the encoding right.
220 */
221 Incrblob *pBlob;
222 u32 type = v->apCsr[0]->aType[iCol];
223
224 if( type<12 ){
danielk1977f1819242007-05-03 18:14:10 +0000225 sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s",
226 type==0?"null": type==7?"real": "integer"
227 );
danielk1977b4e9af92007-05-01 17:49:49 +0000228 rc = SQLITE_ERROR;
229 goto blob_open_out;
230 }
drh17435752007-08-16 04:30:38 +0000231 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
232 if( db->mallocFailed ){
233 sqlite3_free(pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000234 goto blob_open_out;
235 }
236 pBlob->flags = flags;
237 pBlob->pCsr = v->apCsr[0]->pCursor;
drhff0587c2007-08-29 17:43:19 +0000238 sqlite3BtreeEnterCursor(pBlob->pCsr);
danielk19772dec9702007-05-02 16:48:37 +0000239 sqlite3BtreeCacheOverflow(pBlob->pCsr);
drhff0587c2007-08-29 17:43:19 +0000240 sqlite3BtreeLeaveCursor(pBlob->pCsr);
danielk1977b4e9af92007-05-01 17:49:49 +0000241 pBlob->pStmt = (sqlite3_stmt *)v;
242 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
243 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
drh605264d2007-08-21 15:13:19 +0000244 pBlob->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +0000245 *ppBlob = (sqlite3_blob *)pBlob;
246 rc = SQLITE_OK;
danielk19778cbadb02007-05-03 16:31:26 +0000247 }else if( rc==SQLITE_OK ){
danielk1977f1819242007-05-03 18:14:10 +0000248 sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow);
danielk19778cbadb02007-05-03 16:31:26 +0000249 rc = SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +0000250 }
251
252blob_open_out:
danielk197792d4d7a2007-05-04 12:05:56 +0000253 zErr[sizeof(zErr)-1] = '\0';
drh17435752007-08-16 04:30:38 +0000254 if( rc!=SQLITE_OK || db->mallocFailed ){
danielk1977b4e9af92007-05-01 17:49:49 +0000255 sqlite3_finalize((sqlite3_stmt *)v);
256 }
danielk197792d4d7a2007-05-04 12:05:56 +0000257 sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr));
drh605264d2007-08-21 15:13:19 +0000258 rc = sqlite3ApiExit(db, rc);
259 sqlite3_mutex_leave(db->mutex);
260 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000261}
262
263/*
drh16a9b832007-05-05 18:39:25 +0000264** Close a blob handle that was previously created using
265** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000266*/
267int sqlite3_blob_close(sqlite3_blob *pBlob){
268 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000269 int rc;
270
drh605264d2007-08-21 15:13:19 +0000271 rc = sqlite3_finalize(p->pStmt);
drh17435752007-08-16 04:30:38 +0000272 sqlite3_free(p);
drh605264d2007-08-21 15:13:19 +0000273 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000274}
275
drh605264d2007-08-21 15:13:19 +0000276/*
277** Perform a read or write operation on a blob
278*/
drhee858132007-05-08 20:37:38 +0000279static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000280 sqlite3_blob *pBlob,
281 void *z,
282 int n,
283 int iOffset,
284 int (*xCall)(BtCursor*, u32, u32, void*)
285){
286 int rc;
287 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000288 Vdbe *v;
289 sqlite3 *db = p->db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000290
291 /* Request is out of range. Return a transient error. */
292 if( (iOffset+n)>p->nByte ){
293 return SQLITE_ERROR;
294 }
drh605264d2007-08-21 15:13:19 +0000295 sqlite3_mutex_enter(db->mutex);
danielk1977dcbb5d32007-05-04 18:36:44 +0000296
drh605264d2007-08-21 15:13:19 +0000297 /* If there is no statement handle, then the blob-handle has
298 ** already been invalidated. Return SQLITE_ABORT in this case.
danielk1977dcbb5d32007-05-04 18:36:44 +0000299 */
drh605264d2007-08-21 15:13:19 +0000300 v = (Vdbe*)p->pStmt;
301 if( v==0 ){
302 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000303 }else{
drh605264d2007-08-21 15:13:19 +0000304 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
305 ** returned, clean-up the statement handle.
306 */
307 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000308 sqlite3BtreeEnterCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000309 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000310 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000311 if( rc==SQLITE_ABORT ){
312 sqlite3VdbeFinalize(v);
313 p->pStmt = 0;
314 }else{
315 db->errCode = rc;
316 v->rc = rc;
317 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000318 }
drh605264d2007-08-21 15:13:19 +0000319 rc = sqlite3ApiExit(db, rc);
320 sqlite3_mutex_leave(db->mutex);
321 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000322}
323
danielk1977b4e9af92007-05-01 17:49:49 +0000324/*
325** Read data from a blob handle.
326*/
327int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000328 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000329}
330
331/*
332** Write data to a blob handle.
333*/
334int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000335 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000336}
337
338/*
339** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000340**
341** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
342** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000343*/
344int sqlite3_blob_bytes(sqlite3_blob *pBlob){
345 Incrblob *p = (Incrblob *)pBlob;
346 return p->nByte;
347}
348
349#endif /* #ifndef SQLITE_OMIT_INCRBLOB */