blob: 6a6c6a71fcbbf15c426215f44d022c286d7e5e60 [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.
danielk1977b4e9af92007-05-01 17:49:49 +000014*/
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*/
24typedef struct Incrblob Incrblob;
danielk1977b4e9af92007-05-01 17:49:49 +000025struct 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 BtCursor *pCsr; /* Cursor pointing at blob row */
30 sqlite3_stmt *pStmt; /* Statement holding cursor open */
drh605264d2007-08-21 15:13:19 +000031 sqlite3 *db; /* The associated database */
danielk1977b4e9af92007-05-01 17:49:49 +000032};
33
34/*
35** Open a blob handle.
36*/
37int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +000038 sqlite3* db, /* The database connection */
39 const char *zDb, /* The attached database containing the blob */
40 const char *zTable, /* The table containing the blob */
41 const char *zColumn, /* The column containing the blob */
42 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +000043 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +000044 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +000045){
danielk1977b4e9af92007-05-01 17:49:49 +000046 int nAttempt = 0;
47 int iCol; /* Index of zColumn in row-record */
48
49 /* This VDBE program seeks a btree cursor to the identified
50 ** db/table/row entry. The reason for using a vdbe program instead
51 ** of writing code to use the b-tree layer directly is that the
52 ** vdbe program will take advantage of the various transaction,
53 ** locking and error handling infrastructure built into the vdbe.
54 **
drh2d401ab2008-01-10 23:50:11 +000055 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +000056 ** Code external to the Vdbe then "borrows" the b-tree cursor and
57 ** uses it to implement the blob_read(), blob_write() and
58 ** blob_bytes() functions.
59 **
60 ** The sqlite3_blob_close() function finalizes the vdbe program,
61 ** which closes the b-tree cursor and (possibly) commits the
62 ** transaction.
63 */
64 static const VdbeOpList openBlob[] = {
65 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
66 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
danielk197796d48e92009-06-29 06:00:37 +000067 {OP_TableLock, 0, 0, 0}, /* 2: Acquire a read or write lock */
danielk1977b4e9af92007-05-01 17:49:49 +000068
danielk197796d48e92009-06-29 06:00:37 +000069 /* One of the following two instructions is replaced by an OP_Noop. */
70 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
71 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +000072
danielk197796d48e92009-06-29 06:00:37 +000073 {OP_Variable, 1, 1, 1}, /* 5: Push the rowid to the stack */
74 {OP_NotExists, 0, 9, 1}, /* 6: Seek the cursor */
75 {OP_Column, 0, 0, 1}, /* 7 */
76 {OP_ResultRow, 1, 0, 0}, /* 8 */
77 {OP_Close, 0, 0, 0}, /* 9 */
78 {OP_Halt, 0, 0, 0}, /* 10 */
danielk1977b4e9af92007-05-01 17:49:49 +000079 };
80
81 Vdbe *v = 0;
danielk19778cbadb02007-05-03 16:31:26 +000082 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +000083 char *zErr = 0;
84 Table *pTab;
85 Parse *pParse;
danielk1977b4e9af92007-05-01 17:49:49 +000086
drh28414ee2009-05-09 15:17:47 +000087 *ppBlob = 0;
drh605264d2007-08-21 15:13:19 +000088 sqlite3_mutex_enter(db->mutex);
drh6ee700c2009-06-01 19:53:30 +000089 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
90 if( pParse==0 ){
91 rc = SQLITE_NOMEM;
92 goto blob_open_out;
93 }
danielk1977b4e9af92007-05-01 17:49:49 +000094 do {
drh6ee700c2009-06-01 19:53:30 +000095 memset(pParse, 0, sizeof(Parse));
96 pParse->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +000097
drh93a960a2008-07-10 00:32:42 +000098 if( sqlite3SafetyOn(db) ){
drh6ee700c2009-06-01 19:53:30 +000099 sqlite3DbFree(db, zErr);
100 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000101 sqlite3_mutex_leave(db->mutex);
drh93a960a2008-07-10 00:32:42 +0000102 return SQLITE_MISUSE;
danielk197720713f32007-05-03 11:43:33 +0000103 }
104
drhff0587c2007-08-29 17:43:19 +0000105 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000106 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000107 if( pTab && IsVirtual(pTab) ){
108 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000109 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000110 }
111#ifndef SQLITE_OMIT_VIEW
112 if( pTab && pTab->pSelect ){
113 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000114 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000115 }
116#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000117 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000118 if( pParse->zErrMsg ){
119 sqlite3DbFree(db, zErr);
120 zErr = pParse->zErrMsg;
121 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000122 }
danielk19778cbadb02007-05-03 16:31:26 +0000123 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000124 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000125 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000126 goto blob_open_out;
127 }
128
129 /* Now search pTab for the exact column. */
130 for(iCol=0; iCol < pTab->nCol; iCol++) {
131 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
132 break;
133 }
134 }
135 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000136 sqlite3DbFree(db, zErr);
137 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000138 rc = SQLITE_ERROR;
drh7e8b8482008-01-23 03:03:05 +0000139 (void)sqlite3SafetyOff(db);
drhff0587c2007-08-29 17:43:19 +0000140 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000141 goto blob_open_out;
142 }
143
danielk1977f1819242007-05-03 18:14:10 +0000144 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000145 ** column is not indexed, and that it is not part of a foreign key.
146 ** It is against the rules to open a column to which either of these
147 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000148 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000149 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000150 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000151#ifndef SQLITE_OMIT_FOREIGN_KEY
152 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000153 /* Check that the column is not part of an FK child key definition. It
154 ** is not necessary to check if it is part of a parent key, as parent
155 ** key columns must be indexed. The check below will pick up this
156 ** case. */
dan1da40a32009-09-19 17:00:31 +0000157 FKey *pFKey;
158 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
159 int j;
160 for(j=0; j<pFKey->nCol; j++){
161 if( pFKey->aCol[j].iFrom==iCol ){
162 zFault = "foreign key";
163 }
164 }
165 }
166 }
167#endif
danielk1977f1819242007-05-03 18:14:10 +0000168 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
169 int j;
170 for(j=0; j<pIdx->nColumn; j++){
171 if( pIdx->aiColumn[j]==iCol ){
dan1da40a32009-09-19 17:00:31 +0000172 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000173 }
174 }
175 }
dan1da40a32009-09-19 17:00:31 +0000176 if( zFault ){
177 sqlite3DbFree(db, zErr);
178 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
179 rc = SQLITE_ERROR;
180 (void)sqlite3SafetyOff(db);
181 sqlite3BtreeLeaveAll(db);
182 goto blob_open_out;
183 }
danielk1977f1819242007-05-03 18:14:10 +0000184 }
185
danielk1977b4e9af92007-05-01 17:49:49 +0000186 v = sqlite3VdbeCreate(db);
187 if( v ){
188 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
189 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
danielk197796d48e92009-06-29 06:00:37 +0000190 flags = !!flags; /* flags = (flags ? 1 : 0); */
danielk1977b4e9af92007-05-01 17:49:49 +0000191
192 /* Configure the OP_Transaction */
193 sqlite3VdbeChangeP1(v, 0, iDb);
danielk197796d48e92009-06-29 06:00:37 +0000194 sqlite3VdbeChangeP2(v, 0, flags);
danielk1977b4e9af92007-05-01 17:49:49 +0000195
196 /* Configure the OP_VerifyCookie */
197 sqlite3VdbeChangeP1(v, 1, iDb);
198 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
199
drhff0587c2007-08-29 17:43:19 +0000200 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000201 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000202
danielk197796d48e92009-06-29 06:00:37 +0000203 /* Configure the OP_TableLock instruction */
204 sqlite3VdbeChangeP1(v, 2, iDb);
205 sqlite3VdbeChangeP2(v, 2, pTab->tnum);
206 sqlite3VdbeChangeP3(v, 2, flags);
207 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
208
danielk1977f1819242007-05-03 18:14:10 +0000209 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
danielk197796d48e92009-06-29 06:00:37 +0000210 ** parameter of the other to pTab->tnum. */
211 sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
212 sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
213 sqlite3VdbeChangeP3(v, 3 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000214
danielk1977d336e222009-02-20 10:58:41 +0000215 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000216 ** think that the table has one more column than it really
217 ** does. An OP_Column to retrieve this imaginary column will
218 ** always return an SQL NULL. This is useful because it means
219 ** we can invoke OP_Column to fill in the vdbe cursors type
220 ** and offset cache without causing any IO.
221 */
danielk197796d48e92009-06-29 06:00:37 +0000222 sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
223 sqlite3VdbeChangeP2(v, 7, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000224 if( !db->mallocFailed ){
dane0af83a2009-09-08 19:15:01 +0000225 sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
danielk197792d4d7a2007-05-04 12:05:56 +0000226 }
danielk1977b4e9af92007-05-01 17:49:49 +0000227 }
drhff0587c2007-08-29 17:43:19 +0000228
229 sqlite3BtreeLeaveAll(db);
danielk197720713f32007-05-03 11:43:33 +0000230 rc = sqlite3SafetyOff(db);
drh28414ee2009-05-09 15:17:47 +0000231 if( NEVER(rc!=SQLITE_OK) || db->mallocFailed ){
danielk197792d4d7a2007-05-04 12:05:56 +0000232 goto blob_open_out;
danielk197720713f32007-05-03 11:43:33 +0000233 }
234
danielk1977b4e9af92007-05-01 17:49:49 +0000235 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
236 rc = sqlite3_step((sqlite3_stmt *)v);
237 if( rc!=SQLITE_ROW ){
238 nAttempt++;
239 rc = sqlite3_finalize((sqlite3_stmt *)v);
drh6ee700c2009-06-01 19:53:30 +0000240 sqlite3DbFree(db, zErr);
241 zErr = sqlite3MPrintf(db, sqlite3_errmsg(db));
danielk1977b4e9af92007-05-01 17:49:49 +0000242 v = 0;
243 }
244 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
245
246 if( rc==SQLITE_ROW ){
247 /* The row-record has been opened successfully. Check that the
248 ** column in question contains text or a blob. If it contains
249 ** text, it is up to the caller to get the encoding right.
250 */
251 Incrblob *pBlob;
252 u32 type = v->apCsr[0]->aType[iCol];
253
254 if( type<12 ){
drh6ee700c2009-06-01 19:53:30 +0000255 sqlite3DbFree(db, zErr);
256 zErr = sqlite3MPrintf(db, "cannot open value of type %s",
danielk1977f1819242007-05-03 18:14:10 +0000257 type==0?"null": type==7?"real": "integer"
258 );
danielk1977b4e9af92007-05-01 17:49:49 +0000259 rc = SQLITE_ERROR;
260 goto blob_open_out;
261 }
drh17435752007-08-16 04:30:38 +0000262 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
263 if( db->mallocFailed ){
drh633e6d52008-07-28 19:34:53 +0000264 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000265 goto blob_open_out;
266 }
267 pBlob->flags = flags;
268 pBlob->pCsr = v->apCsr[0]->pCursor;
drhff0587c2007-08-29 17:43:19 +0000269 sqlite3BtreeEnterCursor(pBlob->pCsr);
danielk19772dec9702007-05-02 16:48:37 +0000270 sqlite3BtreeCacheOverflow(pBlob->pCsr);
drhff0587c2007-08-29 17:43:19 +0000271 sqlite3BtreeLeaveCursor(pBlob->pCsr);
danielk1977b4e9af92007-05-01 17:49:49 +0000272 pBlob->pStmt = (sqlite3_stmt *)v;
273 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
274 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
drh605264d2007-08-21 15:13:19 +0000275 pBlob->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +0000276 *ppBlob = (sqlite3_blob *)pBlob;
277 rc = SQLITE_OK;
danielk19778cbadb02007-05-03 16:31:26 +0000278 }else if( rc==SQLITE_OK ){
drh6ee700c2009-06-01 19:53:30 +0000279 sqlite3DbFree(db, zErr);
280 zErr = sqlite3MPrintf(db, "no such rowid: %lld", iRow);
danielk19778cbadb02007-05-03 16:31:26 +0000281 rc = SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +0000282 }
283
284blob_open_out:
danielk1977238746a2009-03-19 18:51:06 +0000285 if( v && (rc!=SQLITE_OK || db->mallocFailed) ){
286 sqlite3VdbeFinalize(v);
danielk1977b4e9af92007-05-01 17:49:49 +0000287 }
drh6ee700c2009-06-01 19:53:30 +0000288 sqlite3Error(db, rc, zErr);
289 sqlite3DbFree(db, zErr);
290 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000291 rc = sqlite3ApiExit(db, rc);
292 sqlite3_mutex_leave(db->mutex);
293 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000294}
295
296/*
drh16a9b832007-05-05 18:39:25 +0000297** Close a blob handle that was previously created using
298** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000299*/
300int sqlite3_blob_close(sqlite3_blob *pBlob){
301 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000302 int rc;
drhd9da78a2009-03-24 15:08:09 +0000303 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000304
drh28414ee2009-05-09 15:17:47 +0000305 if( p ){
306 db = p->db;
307 sqlite3_mutex_enter(db->mutex);
308 rc = sqlite3_finalize(p->pStmt);
309 sqlite3DbFree(db, p);
310 sqlite3_mutex_leave(db->mutex);
311 }else{
312 rc = SQLITE_OK;
313 }
drh605264d2007-08-21 15:13:19 +0000314 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000315}
316
drh605264d2007-08-21 15:13:19 +0000317/*
318** Perform a read or write operation on a blob
319*/
drhee858132007-05-08 20:37:38 +0000320static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000321 sqlite3_blob *pBlob,
322 void *z,
323 int n,
324 int iOffset,
325 int (*xCall)(BtCursor*, u32, u32, void*)
326){
327 int rc;
328 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000329 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000330 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000331
drh28414ee2009-05-09 15:17:47 +0000332 if( p==0 ) return SQLITE_MISUSE;
333 db = p->db;
drh605264d2007-08-21 15:13:19 +0000334 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000335 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000336
337 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
338 /* Request is out of range. Return a transient error. */
339 rc = SQLITE_ERROR;
340 sqlite3Error(db, SQLITE_ERROR, 0);
341 } else if( v==0 ){
342 /* If there is no statement handle, then the blob-handle has
343 ** already been invalidated. Return SQLITE_ABORT in this case.
344 */
drh605264d2007-08-21 15:13:19 +0000345 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000346 }else{
drh605264d2007-08-21 15:13:19 +0000347 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
348 ** returned, clean-up the statement handle.
349 */
350 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000351 sqlite3BtreeEnterCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000352 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000353 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000354 if( rc==SQLITE_ABORT ){
355 sqlite3VdbeFinalize(v);
356 p->pStmt = 0;
357 }else{
358 db->errCode = rc;
359 v->rc = rc;
360 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000361 }
drh605264d2007-08-21 15:13:19 +0000362 rc = sqlite3ApiExit(db, rc);
363 sqlite3_mutex_leave(db->mutex);
364 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000365}
366
danielk1977b4e9af92007-05-01 17:49:49 +0000367/*
368** Read data from a blob handle.
369*/
370int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000371 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000372}
373
374/*
375** Write data to a blob handle.
376*/
377int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000378 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000379}
380
381/*
382** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000383**
384** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
385** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000386*/
387int sqlite3_blob_bytes(sqlite3_blob *pBlob){
388 Incrblob *p = (Incrblob *)pBlob;
drh28414ee2009-05-09 15:17:47 +0000389 return p ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000390}
391
392#endif /* #ifndef SQLITE_OMIT_INCRBLOB */