danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 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 | ** |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 13 | ** This file contains code used to implement incremental BLOB I/O. |
| 14 | ** |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 15 | ** $Id: vdbeblob.c,v 1.10 2007/05/08 20:37:40 drh Exp $ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 16 | */ |
| 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 | */ |
| 26 | typedef struct Incrblob Incrblob; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 27 | struct 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 */ |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | ** Open a blob handle. |
| 37 | */ |
| 38 | int sqlite3_blob_open( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 39 | sqlite3* db, /* The database connection */ |
| 40 | const char *zDb, /* The attached database containing the blob */ |
| 41 | const char *zTable, /* The table containing the blob */ |
| 42 | const char *zColumn, /* The column containing the blob */ |
| 43 | sqlite_int64 iRow, /* The row containing the glob */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 44 | int flags, /* True -> read/write access, false -> read-only */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 45 | sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 46 | ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 47 | int nAttempt = 0; |
| 48 | int iCol; /* Index of zColumn in row-record */ |
| 49 | |
| 50 | /* This VDBE program seeks a btree cursor to the identified |
| 51 | ** db/table/row entry. The reason for using a vdbe program instead |
| 52 | ** of writing code to use the b-tree layer directly is that the |
| 53 | ** vdbe program will take advantage of the various transaction, |
| 54 | ** locking and error handling infrastructure built into the vdbe. |
| 55 | ** |
| 56 | ** After seeking the cursor, the vdbe executes an OP_Callback. |
| 57 | ** Code external to the Vdbe then "borrows" the b-tree cursor and |
| 58 | ** uses it to implement the blob_read(), blob_write() and |
| 59 | ** blob_bytes() functions. |
| 60 | ** |
| 61 | ** The sqlite3_blob_close() function finalizes the vdbe program, |
| 62 | ** which closes the b-tree cursor and (possibly) commits the |
| 63 | ** transaction. |
| 64 | */ |
| 65 | static const VdbeOpList openBlob[] = { |
| 66 | {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */ |
| 67 | {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */ |
| 68 | {OP_Integer, 0, 0, 0}, /* 2: Database number */ |
| 69 | |
| 70 | /* One of the following two instructions is replaced by an |
| 71 | ** OP_Noop before exection. |
| 72 | */ |
| 73 | {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */ |
| 74 | {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */ |
| 75 | {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */ |
| 76 | |
| 77 | {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */ |
| 78 | {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */ |
| 79 | {OP_Column, 0, 0, 0}, /* 8 */ |
| 80 | {OP_Callback, 0, 0, 0}, /* 9 */ |
| 81 | {OP_Close, 0, 0, 0}, /* 10 */ |
| 82 | {OP_Halt, 0, 0, 0}, /* 11 */ |
| 83 | }; |
| 84 | |
| 85 | Vdbe *v = 0; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 86 | int rc = SQLITE_OK; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 87 | char zErr[128]; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 88 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 89 | zErr[0] = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 90 | do { |
| 91 | Parse sParse; |
| 92 | Table *pTab; |
| 93 | |
| 94 | memset(&sParse, 0, sizeof(Parse)); |
| 95 | sParse.db = db; |
| 96 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 97 | rc = sqlite3SafetyOn(db); |
| 98 | if( rc!=SQLITE_OK ){ |
| 99 | return rc; |
| 100 | } |
| 101 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 102 | pTab = sqlite3LocateTable(&sParse, zTable, zDb); |
| 103 | if( !pTab ){ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 104 | if( sParse.zErrMsg ){ |
| 105 | sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 106 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 107 | sqliteFree(sParse.zErrMsg); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 108 | rc = SQLITE_ERROR; |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 109 | sqlite3SafetyOff(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 110 | goto blob_open_out; |
| 111 | } |
| 112 | |
| 113 | /* Now search pTab for the exact column. */ |
| 114 | for(iCol=0; iCol < pTab->nCol; iCol++) { |
| 115 | if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | if( iCol==pTab->nCol ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 120 | sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 121 | rc = SQLITE_ERROR; |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 122 | sqlite3SafetyOff(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 123 | goto blob_open_out; |
| 124 | } |
| 125 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 126 | /* If the value is being opened for writing, check that the |
| 127 | ** column is not indexed. It is against the rules to open an |
| 128 | ** indexed column for writing. |
| 129 | */ |
| 130 | if( flags ){ |
| 131 | Index *pIdx; |
| 132 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 133 | int j; |
| 134 | for(j=0; j<pIdx->nColumn; j++){ |
| 135 | if( pIdx->aiColumn[j]==iCol ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 136 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 137 | "cannot open indexed column for writing"); |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 138 | rc = SQLITE_ERROR; |
| 139 | sqlite3SafetyOff(db); |
| 140 | goto blob_open_out; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 146 | v = sqlite3VdbeCreate(db); |
| 147 | if( v ){ |
| 148 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 149 | sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob); |
| 150 | |
| 151 | /* Configure the OP_Transaction */ |
| 152 | sqlite3VdbeChangeP1(v, 0, iDb); |
| 153 | sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0)); |
| 154 | |
| 155 | /* Configure the OP_VerifyCookie */ |
| 156 | sqlite3VdbeChangeP1(v, 1, iDb); |
| 157 | sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie); |
| 158 | |
| 159 | /* Configure the db number pushed onto the stack */ |
| 160 | sqlite3VdbeChangeP1(v, 2, iDb); |
| 161 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 162 | /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
| 163 | ** parameter of the other to pTab->tnum. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 164 | */ |
| 165 | sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1); |
| 166 | sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum); |
| 167 | |
| 168 | /* Configure the OP_SetNumColumns. Configure the cursor to |
| 169 | ** think that the table has one more column than it really |
| 170 | ** does. An OP_Column to retrieve this imaginary column will |
| 171 | ** always return an SQL NULL. This is useful because it means |
| 172 | ** we can invoke OP_Column to fill in the vdbe cursors type |
| 173 | ** and offset cache without causing any IO. |
| 174 | */ |
| 175 | sqlite3VdbeChangeP2(v, 5, pTab->nCol+1); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 176 | if( !sqlite3MallocFailed() ){ |
| 177 | sqlite3VdbeMakeReady(v, 1, 0, 1, 0); |
| 178 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 179 | } |
| 180 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 181 | rc = sqlite3SafetyOff(db); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 182 | if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ |
| 183 | goto blob_open_out; |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 186 | sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow); |
| 187 | rc = sqlite3_step((sqlite3_stmt *)v); |
| 188 | if( rc!=SQLITE_ROW ){ |
| 189 | nAttempt++; |
| 190 | rc = sqlite3_finalize((sqlite3_stmt *)v); |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 191 | sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db)); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 192 | v = 0; |
| 193 | } |
| 194 | } while( nAttempt<5 && rc==SQLITE_SCHEMA ); |
| 195 | |
| 196 | if( rc==SQLITE_ROW ){ |
| 197 | /* The row-record has been opened successfully. Check that the |
| 198 | ** column in question contains text or a blob. If it contains |
| 199 | ** text, it is up to the caller to get the encoding right. |
| 200 | */ |
| 201 | Incrblob *pBlob; |
| 202 | u32 type = v->apCsr[0]->aType[iCol]; |
| 203 | |
| 204 | if( type<12 ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 205 | sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s", |
| 206 | type==0?"null": type==7?"real": "integer" |
| 207 | ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 208 | rc = SQLITE_ERROR; |
| 209 | goto blob_open_out; |
| 210 | } |
| 211 | pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob)); |
| 212 | if( sqlite3MallocFailed() ){ |
| 213 | sqliteFree(pBlob); |
| 214 | goto blob_open_out; |
| 215 | } |
| 216 | pBlob->flags = flags; |
| 217 | pBlob->pCsr = v->apCsr[0]->pCursor; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 218 | sqlite3BtreeCacheOverflow(pBlob->pCsr); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 219 | pBlob->pStmt = (sqlite3_stmt *)v; |
| 220 | pBlob->iOffset = v->apCsr[0]->aOffset[iCol]; |
| 221 | pBlob->nByte = sqlite3VdbeSerialTypeLen(type); |
| 222 | *ppBlob = (sqlite3_blob *)pBlob; |
| 223 | rc = SQLITE_OK; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 224 | }else if( rc==SQLITE_OK ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 225 | sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 226 | rc = SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | blob_open_out: |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 230 | zErr[sizeof(zErr)-1] = '\0'; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 231 | if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ |
| 232 | sqlite3_finalize((sqlite3_stmt *)v); |
| 233 | } |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 234 | sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 235 | return sqlite3ApiExit(db, rc); |
| 236 | } |
| 237 | |
| 238 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 239 | ** Close a blob handle that was previously created using |
| 240 | ** sqlite3_blob_open(). |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 241 | */ |
| 242 | int sqlite3_blob_close(sqlite3_blob *pBlob){ |
| 243 | Incrblob *p = (Incrblob *)pBlob; |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 244 | sqlite3_stmt *pStmt = p->pStmt; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 245 | sqliteFree(p); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 246 | return sqlite3_finalize(pStmt); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 247 | } |
| 248 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 249 | |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 250 | static int blobReadWrite( |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 251 | sqlite3_blob *pBlob, |
| 252 | void *z, |
| 253 | int n, |
| 254 | int iOffset, |
| 255 | int (*xCall)(BtCursor*, u32, u32, void*) |
| 256 | ){ |
| 257 | int rc; |
| 258 | Incrblob *p = (Incrblob *)pBlob; |
| 259 | Vdbe *v = (Vdbe *)(p->pStmt); |
| 260 | sqlite3 *db; |
| 261 | |
| 262 | /* If there is no statement handle, then the blob-handle has |
| 263 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 264 | */ |
| 265 | if( !v ) return SQLITE_ABORT; |
| 266 | |
| 267 | /* Request is out of range. Return a transient error. */ |
| 268 | if( (iOffset+n)>p->nByte ){ |
| 269 | return SQLITE_ERROR; |
| 270 | } |
| 271 | |
| 272 | /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
| 273 | ** returned, clean-up the statement handle. |
| 274 | */ |
| 275 | db = v->db; |
| 276 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
| 277 | if( rc==SQLITE_ABORT ){ |
| 278 | sqlite3VdbeFinalize(v); |
| 279 | p->pStmt = 0; |
| 280 | }else{ |
| 281 | v->rc = rc; |
| 282 | } |
| 283 | |
| 284 | return sqlite3ApiExit(db, rc); |
| 285 | } |
| 286 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 287 | /* |
| 288 | ** Read data from a blob handle. |
| 289 | */ |
| 290 | int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 291 | return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | ** Write data to a blob handle. |
| 296 | */ |
| 297 | int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 298 | return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | ** Query a blob handle for the size of the data. |
| 303 | */ |
| 304 | int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
| 305 | Incrblob *p = (Incrblob *)pBlob; |
| 306 | return p->nByte; |
| 307 | } |
| 308 | |
| 309 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |