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