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 | ** |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 15 | ** $Id: vdbeblob.c,v 1.29 2009/03/05 03:48:07 shane 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 */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 33 | sqlite3 *db; /* The associated database */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* |
| 37 | ** Open a blob handle. |
| 38 | */ |
| 39 | int sqlite3_blob_open( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 40 | 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 */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 45 | int flags, /* True -> read/write access, false -> read-only */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 46 | sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 47 | ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 48 | 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 | ** |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 57 | ** After seeking the cursor, the vdbe executes an OP_ResultRow. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 58 | ** 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 */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 69 | |
| 70 | /* One of the following two instructions is replaced by an |
| 71 | ** OP_Noop before exection. |
| 72 | */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 73 | {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */ |
| 74 | {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 75 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 76 | {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 */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | Vdbe *v = 0; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 85 | int rc = SQLITE_OK; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 86 | char zErr[128]; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 87 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 88 | zErr[0] = 0; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 89 | sqlite3_mutex_enter(db->mutex); |
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 | |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 97 | if( sqlite3SafetyOn(db) ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 98 | sqlite3_mutex_leave(db->mutex); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 99 | return SQLITE_MISUSE; |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 100 | } |
| 101 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 102 | sqlite3BtreeEnterAll(db); |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 103 | pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 +0000 | [diff] [blame] | 104 | if( pTab && IsVirtual(pTab) ){ |
| 105 | pTab = 0; |
| 106 | sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable); |
| 107 | } |
| 108 | #ifndef SQLITE_OMIT_VIEW |
| 109 | if( pTab && pTab->pSelect ){ |
| 110 | pTab = 0; |
| 111 | sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable); |
| 112 | } |
| 113 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 114 | if( !pTab ){ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 115 | if( sParse.zErrMsg ){ |
| 116 | sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 117 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 118 | sqlite3DbFree(db, sParse.zErrMsg); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 119 | rc = SQLITE_ERROR; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 120 | (void)sqlite3SafetyOff(db); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 121 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 122 | goto blob_open_out; |
| 123 | } |
| 124 | |
| 125 | /* Now search pTab for the exact column. */ |
| 126 | for(iCol=0; iCol < pTab->nCol; iCol++) { |
| 127 | if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | if( iCol==pTab->nCol ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 132 | sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 133 | rc = SQLITE_ERROR; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 134 | (void)sqlite3SafetyOff(db); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 135 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 136 | goto blob_open_out; |
| 137 | } |
| 138 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 139 | /* If the value is being opened for writing, check that the |
| 140 | ** column is not indexed. It is against the rules to open an |
| 141 | ** indexed column for writing. |
| 142 | */ |
| 143 | if( flags ){ |
| 144 | Index *pIdx; |
| 145 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 146 | int j; |
| 147 | for(j=0; j<pIdx->nColumn; j++){ |
| 148 | if( pIdx->aiColumn[j]==iCol ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 149 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 150 | "cannot open indexed column for writing"); |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 151 | rc = SQLITE_ERROR; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 152 | (void)sqlite3SafetyOff(db); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 153 | sqlite3BtreeLeaveAll(db); |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 154 | goto blob_open_out; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 160 | v = sqlite3VdbeCreate(db); |
| 161 | if( v ){ |
| 162 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 163 | sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob); |
| 164 | |
| 165 | /* Configure the OP_Transaction */ |
| 166 | sqlite3VdbeChangeP1(v, 0, iDb); |
| 167 | sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0)); |
| 168 | |
| 169 | /* Configure the OP_VerifyCookie */ |
| 170 | sqlite3VdbeChangeP1(v, 1, iDb); |
| 171 | sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie); |
| 172 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 173 | /* Make sure a mutex is held on the table to be accessed */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 174 | sqlite3VdbeUsesBtree(v, iDb); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 175 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 176 | /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
| 177 | ** parameter of the other to pTab->tnum. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 178 | */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 179 | sqlite3VdbeChangeToNoop(v, (flags ? 2 : 3), 1); |
| 180 | sqlite3VdbeChangeP2(v, (flags ? 3 : 2), pTab->tnum); |
| 181 | sqlite3VdbeChangeP3(v, (flags ? 3 : 2), iDb); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 182 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 183 | /* Configure the number of columns. Configure the cursor to |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 184 | ** think that the table has one more column than it really |
| 185 | ** does. An OP_Column to retrieve this imaginary column will |
| 186 | ** always return an SQL NULL. This is useful because it means |
| 187 | ** we can invoke OP_Column to fill in the vdbe cursors type |
| 188 | ** and offset cache without causing any IO. |
| 189 | */ |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 190 | sqlite3VdbeChangeP4(v, flags ? 3 : 2, SQLITE_INT_TO_PTR(pTab->nCol+1), P4_INT32); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 191 | sqlite3VdbeChangeP2(v, 6, pTab->nCol); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 192 | if( !db->mallocFailed ){ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 193 | sqlite3VdbeMakeReady(v, 1, 1, 1, 0); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 194 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 195 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 196 | |
| 197 | sqlite3BtreeLeaveAll(db); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 198 | rc = sqlite3SafetyOff(db); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 199 | if( rc!=SQLITE_OK || db->mallocFailed ){ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 200 | goto blob_open_out; |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 201 | } |
| 202 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 203 | sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow); |
| 204 | rc = sqlite3_step((sqlite3_stmt *)v); |
| 205 | if( rc!=SQLITE_ROW ){ |
| 206 | nAttempt++; |
| 207 | rc = sqlite3_finalize((sqlite3_stmt *)v); |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 208 | sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db)); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 209 | v = 0; |
| 210 | } |
| 211 | } while( nAttempt<5 && rc==SQLITE_SCHEMA ); |
| 212 | |
| 213 | if( rc==SQLITE_ROW ){ |
| 214 | /* The row-record has been opened successfully. Check that the |
| 215 | ** column in question contains text or a blob. If it contains |
| 216 | ** text, it is up to the caller to get the encoding right. |
| 217 | */ |
| 218 | Incrblob *pBlob; |
| 219 | u32 type = v->apCsr[0]->aType[iCol]; |
| 220 | |
| 221 | if( type<12 ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 222 | sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s", |
| 223 | type==0?"null": type==7?"real": "integer" |
| 224 | ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 225 | rc = SQLITE_ERROR; |
| 226 | goto blob_open_out; |
| 227 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 228 | pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
| 229 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 230 | sqlite3DbFree(db, pBlob); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 231 | goto blob_open_out; |
| 232 | } |
| 233 | pBlob->flags = flags; |
| 234 | pBlob->pCsr = v->apCsr[0]->pCursor; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 235 | sqlite3BtreeEnterCursor(pBlob->pCsr); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 236 | sqlite3BtreeCacheOverflow(pBlob->pCsr); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 237 | sqlite3BtreeLeaveCursor(pBlob->pCsr); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 238 | pBlob->pStmt = (sqlite3_stmt *)v; |
| 239 | pBlob->iOffset = v->apCsr[0]->aOffset[iCol]; |
| 240 | pBlob->nByte = sqlite3VdbeSerialTypeLen(type); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 241 | pBlob->db = db; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 242 | *ppBlob = (sqlite3_blob *)pBlob; |
| 243 | rc = SQLITE_OK; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 244 | }else if( rc==SQLITE_OK ){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 245 | sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow); |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 246 | rc = SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | blob_open_out: |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 250 | zErr[sizeof(zErr)-1] = '\0'; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 251 | if( rc!=SQLITE_OK || db->mallocFailed ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 252 | sqlite3_finalize((sqlite3_stmt *)v); |
| 253 | } |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 254 | sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 255 | rc = sqlite3ApiExit(db, rc); |
| 256 | sqlite3_mutex_leave(db->mutex); |
| 257 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 261 | ** Close a blob handle that was previously created using |
| 262 | ** sqlite3_blob_open(). |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 263 | */ |
| 264 | int sqlite3_blob_close(sqlite3_blob *pBlob){ |
| 265 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 266 | int rc; |
| 267 | |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 268 | rc = sqlite3_finalize(p->pStmt); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 269 | sqlite3DbFree(p->db, p); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 270 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 271 | } |
| 272 | |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 273 | /* |
| 274 | ** Perform a read or write operation on a blob |
| 275 | */ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 276 | static int blobReadWrite( |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 277 | sqlite3_blob *pBlob, |
| 278 | void *z, |
| 279 | int n, |
| 280 | int iOffset, |
| 281 | int (*xCall)(BtCursor*, u32, u32, void*) |
| 282 | ){ |
| 283 | int rc; |
| 284 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 285 | Vdbe *v; |
| 286 | sqlite3 *db = p->db; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 287 | |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 288 | sqlite3_mutex_enter(db->mutex); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 289 | v = (Vdbe*)p->pStmt; |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 290 | |
| 291 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 292 | /* Request is out of range. Return a transient error. */ |
| 293 | rc = SQLITE_ERROR; |
| 294 | sqlite3Error(db, SQLITE_ERROR, 0); |
| 295 | } else if( v==0 ){ |
| 296 | /* If there is no statement handle, then the blob-handle has |
| 297 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 298 | */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 299 | rc = SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 300 | }else{ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 301 | /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
| 302 | ** returned, clean-up the statement handle. |
| 303 | */ |
| 304 | assert( db == v->db ); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 305 | sqlite3BtreeEnterCursor(p->pCsr); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 306 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 307 | sqlite3BtreeLeaveCursor(p->pCsr); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 308 | if( rc==SQLITE_ABORT ){ |
| 309 | sqlite3VdbeFinalize(v); |
| 310 | p->pStmt = 0; |
| 311 | }else{ |
| 312 | db->errCode = rc; |
| 313 | v->rc = rc; |
| 314 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 315 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 316 | rc = sqlite3ApiExit(db, rc); |
| 317 | sqlite3_mutex_leave(db->mutex); |
| 318 | return rc; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 319 | } |
| 320 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 321 | /* |
| 322 | ** Read data from a blob handle. |
| 323 | */ |
| 324 | int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 325 | return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
| 329 | ** Write data to a blob handle. |
| 330 | */ |
| 331 | 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] | 332 | return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | ** Query a blob handle for the size of the data. |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 337 | ** |
| 338 | ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob |
| 339 | ** so no mutex is required for access. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 340 | */ |
| 341 | int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
| 342 | Incrblob *p = (Incrblob *)pBlob; |
| 343 | return p->nByte; |
| 344 | } |
| 345 | |
| 346 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |