ds@1952: /* ds@1952: ** 2007 May 1 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** ds@1952: ** This file contains code used to implement incremental BLOB I/O. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: ds@1952: #include "sqliteInt.h" ds@1952: #include "vdbeInt.h" ds@1952: ds@1952: #ifndef SQLITE_OMIT_INCRBLOB ds@1952: ds@1952: /* ds@1952: ** Valid sqlite3_blob* handles point to Incrblob structures. ds@1952: */ ds@1952: typedef struct Incrblob Incrblob; ds@1952: struct Incrblob { ds@1952: int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ ds@1952: int nByte; /* Size of open blob, in bytes */ ds@1952: int iOffset; /* Byte offset of blob in cursor data */ ds@1952: BtCursor *pCsr; /* Cursor pointing at blob row */ ds@1952: sqlite3_stmt *pStmt; /* Statement holding cursor open */ ds@1952: sqlite3 *db; /* The associated database */ ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** Open a blob handle. ds@1952: */ ds@1952: int sqlite3_blob_open( ds@1952: sqlite3* db, /* The database connection */ ds@1952: const char *zDb, /* The attached database containing the blob */ ds@1952: const char *zTable, /* The table containing the blob */ ds@1952: const char *zColumn, /* The column containing the blob */ ds@1952: sqlite_int64 iRow, /* The row containing the glob */ ds@1952: int flags, /* True -> read/write access, false -> read-only */ ds@1952: sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ ds@1952: ){ ds@1952: int nAttempt = 0; ds@1952: int iCol; /* Index of zColumn in row-record */ ds@1952: ds@1952: /* This VDBE program seeks a btree cursor to the identified ds@1952: ** db/table/row entry. The reason for using a vdbe program instead ds@1952: ** of writing code to use the b-tree layer directly is that the ds@1952: ** vdbe program will take advantage of the various transaction, ds@1952: ** locking and error handling infrastructure built into the vdbe. ds@1952: ** ds@1952: ** After seeking the cursor, the vdbe executes an OP_Callback. ds@1952: ** Code external to the Vdbe then "borrows" the b-tree cursor and ds@1952: ** uses it to implement the blob_read(), blob_write() and ds@1952: ** blob_bytes() functions. ds@1952: ** ds@1952: ** The sqlite3_blob_close() function finalizes the vdbe program, ds@1952: ** which closes the b-tree cursor and (possibly) commits the ds@1952: ** transaction. ds@1952: */ ds@1952: static const VdbeOpList openBlob[] = { ds@1952: {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */ ds@1952: {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */ ds@1952: {OP_Integer, 0, 0, 0}, /* 2: Database number */ ds@1952: ds@1952: /* One of the following two instructions is replaced by an ds@1952: ** OP_Noop before exection. ds@1952: */ ds@1952: {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */ ds@1952: {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */ ds@1952: {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */ ds@1952: ds@1952: {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */ ds@1952: {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */ ds@1952: {OP_Column, 0, 0, 0}, /* 8 */ ds@1952: {OP_Callback, 0, 0, 0}, /* 9 */ ds@1952: {OP_Close, 0, 0, 0}, /* 10 */ ds@1952: {OP_Halt, 0, 0, 0}, /* 11 */ ds@1952: }; ds@1952: ds@1952: Vdbe *v = 0; ds@1952: int rc = SQLITE_OK; ds@1952: char zErr[128]; ds@1952: ds@1952: zErr[0] = 0; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: do { ds@1952: Parse sParse; ds@1952: Table *pTab; ds@1952: ds@1952: memset(&sParse, 0, sizeof(Parse)); ds@1952: sParse.db = db; ds@1952: ds@1952: rc = sqlite3SafetyOn(db); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: sqlite3BtreeEnterAll(db); ds@1952: pTab = sqlite3LocateTable(&sParse, zTable, zDb); ds@1952: if( !pTab ){ ds@1952: if( sParse.zErrMsg ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); ds@1952: } ds@1952: sqlite3_free(sParse.zErrMsg); ds@1952: rc = SQLITE_ERROR; ds@1952: sqlite3SafetyOff(db); ds@1952: sqlite3BtreeLeaveAll(db); ds@1952: goto blob_open_out; ds@1952: } ds@1952: ds@1952: /* Now search pTab for the exact column. */ ds@1952: for(iCol=0; iCol < pTab->nCol; iCol++) { ds@1952: if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( iCol==pTab->nCol ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); ds@1952: rc = SQLITE_ERROR; ds@1952: sqlite3SafetyOff(db); ds@1952: sqlite3BtreeLeaveAll(db); ds@1952: goto blob_open_out; ds@1952: } ds@1952: ds@1952: /* If the value is being opened for writing, check that the ds@1952: ** column is not indexed. It is against the rules to open an ds@1952: ** indexed column for writing. ds@1952: */ ds@1952: if( flags ){ ds@1952: Index *pIdx; ds@1952: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ ds@1952: int j; ds@1952: for(j=0; jnColumn; j++){ ds@1952: if( pIdx->aiColumn[j]==iCol ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, ds@1952: "cannot open indexed column for writing"); ds@1952: rc = SQLITE_ERROR; ds@1952: sqlite3SafetyOff(db); ds@1952: sqlite3BtreeLeaveAll(db); ds@1952: goto blob_open_out; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: v = sqlite3VdbeCreate(db); ds@1952: if( v ){ ds@1952: int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); ds@1952: sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob); ds@1952: ds@1952: /* Configure the OP_Transaction */ ds@1952: sqlite3VdbeChangeP1(v, 0, iDb); ds@1952: sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0)); ds@1952: ds@1952: /* Configure the OP_VerifyCookie */ ds@1952: sqlite3VdbeChangeP1(v, 1, iDb); ds@1952: sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie); ds@1952: ds@1952: /* Make sure a mutex is held on the table to be accessed */ ds@1952: sqlite3VdbeUsesBtree(v, iDb); ds@1952: ds@1952: /* Configure the db number pushed onto the stack */ ds@1952: sqlite3VdbeChangeP1(v, 2, iDb); ds@1952: ds@1952: /* Remove either the OP_OpenWrite or OpenRead. Set the P2 ds@1952: ** parameter of the other to pTab->tnum. ds@1952: */ ds@1952: sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1); ds@1952: sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum); ds@1952: ds@1952: /* Configure the OP_SetNumColumns. Configure the cursor to ds@1952: ** think that the table has one more column than it really ds@1952: ** does. An OP_Column to retrieve this imaginary column will ds@1952: ** always return an SQL NULL. This is useful because it means ds@1952: ** we can invoke OP_Column to fill in the vdbe cursors type ds@1952: ** and offset cache without causing any IO. ds@1952: */ ds@1952: sqlite3VdbeChangeP2(v, 5, pTab->nCol+1); ds@1952: if( !db->mallocFailed ){ ds@1952: sqlite3VdbeMakeReady(v, 1, 0, 1, 0); ds@1952: } ds@1952: } ds@1952: ds@1952: sqlite3BtreeLeaveAll(db); ds@1952: rc = sqlite3SafetyOff(db); ds@1952: if( rc!=SQLITE_OK || db->mallocFailed ){ ds@1952: goto blob_open_out; ds@1952: } ds@1952: ds@1952: sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow); ds@1952: rc = sqlite3_step((sqlite3_stmt *)v); ds@1952: if( rc!=SQLITE_ROW ){ ds@1952: nAttempt++; ds@1952: rc = sqlite3_finalize((sqlite3_stmt *)v); ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db)); ds@1952: v = 0; ds@1952: } ds@1952: } while( nAttempt<5 && rc==SQLITE_SCHEMA ); ds@1952: ds@1952: if( rc==SQLITE_ROW ){ ds@1952: /* The row-record has been opened successfully. Check that the ds@1952: ** column in question contains text or a blob. If it contains ds@1952: ** text, it is up to the caller to get the encoding right. ds@1952: */ ds@1952: Incrblob *pBlob; ds@1952: u32 type = v->apCsr[0]->aType[iCol]; ds@1952: ds@1952: if( type<12 ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s", ds@1952: type==0?"null": type==7?"real": "integer" ds@1952: ); ds@1952: rc = SQLITE_ERROR; ds@1952: goto blob_open_out; ds@1952: } ds@1952: pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); ds@1952: if( db->mallocFailed ){ ds@1952: sqlite3_free(pBlob); ds@1952: goto blob_open_out; ds@1952: } ds@1952: pBlob->flags = flags; ds@1952: pBlob->pCsr = v->apCsr[0]->pCursor; ds@1952: sqlite3BtreeEnterCursor(pBlob->pCsr); ds@1952: sqlite3BtreeCacheOverflow(pBlob->pCsr); ds@1952: sqlite3BtreeLeaveCursor(pBlob->pCsr); ds@1952: pBlob->pStmt = (sqlite3_stmt *)v; ds@1952: pBlob->iOffset = v->apCsr[0]->aOffset[iCol]; ds@1952: pBlob->nByte = sqlite3VdbeSerialTypeLen(type); ds@1952: pBlob->db = db; ds@1952: *ppBlob = (sqlite3_blob *)pBlob; ds@1952: rc = SQLITE_OK; ds@1952: }else if( rc==SQLITE_OK ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow); ds@1952: rc = SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: blob_open_out: ds@1952: zErr[sizeof(zErr)-1] = '\0'; ds@1952: if( rc!=SQLITE_OK || db->mallocFailed ){ ds@1952: sqlite3_finalize((sqlite3_stmt *)v); ds@1952: } ds@1952: sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Close a blob handle that was previously created using ds@1952: ** sqlite3_blob_open(). ds@1952: */ ds@1952: int sqlite3_blob_close(sqlite3_blob *pBlob){ ds@1952: Incrblob *p = (Incrblob *)pBlob; ds@1952: int rc; ds@1952: ds@1952: rc = sqlite3_finalize(p->pStmt); ds@1952: sqlite3_free(p); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Perform a read or write operation on a blob ds@1952: */ ds@1952: static int blobReadWrite( ds@1952: sqlite3_blob *pBlob, ds@1952: void *z, ds@1952: int n, ds@1952: int iOffset, ds@1952: int (*xCall)(BtCursor*, u32, u32, void*) ds@1952: ){ ds@1952: int rc; ds@1952: Incrblob *p = (Incrblob *)pBlob; ds@1952: Vdbe *v; ds@1952: sqlite3 *db = p->db; ds@1952: ds@1952: /* Request is out of range. Return a transient error. */ ds@1952: if( (iOffset+n)>p->nByte ){ ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: ds@1952: /* If there is no statement handle, then the blob-handle has ds@1952: ** already been invalidated. Return SQLITE_ABORT in this case. ds@1952: */ ds@1952: v = (Vdbe*)p->pStmt; ds@1952: if( v==0 ){ ds@1952: rc = SQLITE_ABORT; ds@1952: }else{ ds@1952: /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is ds@1952: ** returned, clean-up the statement handle. ds@1952: */ ds@1952: assert( db == v->db ); ds@1952: sqlite3BtreeEnterCursor(p->pCsr); ds@1952: rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); ds@1952: sqlite3BtreeLeaveCursor(p->pCsr); ds@1952: if( rc==SQLITE_ABORT ){ ds@1952: sqlite3VdbeFinalize(v); ds@1952: p->pStmt = 0; ds@1952: }else{ ds@1952: db->errCode = rc; ds@1952: v->rc = rc; ds@1952: } ds@1952: } ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Read data from a blob handle. ds@1952: */ ds@1952: int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ ds@1952: return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Write data to a blob handle. ds@1952: */ ds@1952: int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ ds@1952: return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Query a blob handle for the size of the data. ds@1952: ** ds@1952: ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob ds@1952: ** so no mutex is required for access. ds@1952: */ ds@1952: int sqlite3_blob_bytes(sqlite3_blob *pBlob){ ds@1952: Incrblob *p = (Incrblob *)pBlob; ds@1952: return p->nByte; ds@1952: } ds@1952: ds@1952: #endif /* #ifndef SQLITE_OMIT_INCRBLOB */