blob: c81ac214103f4f8e58ade0689fb33e55034798c6 [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**
danielk1977f1819242007-05-03 18:14:10 +000013** $Id: vdbeblob.c,v 1.5 2007/05/03 18:14:10 danielk1977 Exp $
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;
25
26struct Incrblob {
27 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
28 int nByte; /* Size of open blob, in bytes */
29 int iOffset; /* Byte offset of blob in cursor data */
30 BtCursor *pCsr; /* Cursor pointing at blob row */
31 sqlite3_stmt *pStmt; /* Statement holding cursor open */
32};
33
34/*
35** Open a blob handle.
36*/
37int sqlite3_blob_open(
38 sqlite3* db,
39 const char *zDb,
40 const char *zTable,
41 const char *zColumn,
42 sqlite_int64 iRow,
43 int flags, /* True -> read/write access, false -> read-only */
44 sqlite3_blob **ppBlob
45){
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 **
55 ** After seeking the cursor, the vdbe executes an OP_Callback.
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 */
67 {OP_Integer, 0, 0, 0}, /* 2: Database number */
68
69 /* One of the following two instructions is replaced by an
70 ** OP_Noop before exection.
71 */
72 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
73 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
74 {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */
75
76 {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */
77 {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */
78 {OP_Column, 0, 0, 0}, /* 8 */
79 {OP_Callback, 0, 0, 0}, /* 9 */
80 {OP_Close, 0, 0, 0}, /* 10 */
81 {OP_Halt, 0, 0, 0}, /* 11 */
82 };
83
84 Vdbe *v = 0;
danielk19778cbadb02007-05-03 16:31:26 +000085 int rc = SQLITE_OK;
86 char zErr[128] = {0};
danielk1977b4e9af92007-05-01 17:49:49 +000087
88 do {
89 Parse sParse;
90 Table *pTab;
91
92 memset(&sParse, 0, sizeof(Parse));
93 sParse.db = db;
94
danielk197720713f32007-05-03 11:43:33 +000095 rc = sqlite3SafetyOn(db);
96 if( rc!=SQLITE_OK ){
97 return rc;
98 }
99
danielk1977b4e9af92007-05-01 17:49:49 +0000100 pTab = sqlite3LocateTable(&sParse, zTable, zDb);
101 if( !pTab ){
danielk19778cbadb02007-05-03 16:31:26 +0000102 if( sParse.zErrMsg ){
103 sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg);
danielk19778cbadb02007-05-03 16:31:26 +0000104 }
danielk1977b4e9af92007-05-01 17:49:49 +0000105 sqliteFree(sParse.zErrMsg);
danielk19778cbadb02007-05-03 16:31:26 +0000106 rc = SQLITE_ERROR;
danielk197720713f32007-05-03 11:43:33 +0000107 sqlite3SafetyOff(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000108 goto blob_open_out;
109 }
110
111 /* Now search pTab for the exact column. */
112 for(iCol=0; iCol < pTab->nCol; iCol++) {
113 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
114 break;
115 }
116 }
117 if( iCol==pTab->nCol ){
danielk1977f1819242007-05-03 18:14:10 +0000118 sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000119 rc = SQLITE_ERROR;
danielk197720713f32007-05-03 11:43:33 +0000120 sqlite3SafetyOff(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000121 goto blob_open_out;
122 }
123
danielk1977f1819242007-05-03 18:14:10 +0000124 /* If the value is being opened for writing, check that the
125 ** column is not indexed. It is against the rules to open an
126 ** indexed column for writing.
127 */
128 if( flags ){
129 Index *pIdx;
130 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131 int j;
132 for(j=0; j<pIdx->nColumn; j++){
133 if( pIdx->aiColumn[j]==iCol ){
134 strcpy(zErr, "cannot open indexed column for writing");
135 rc = SQLITE_ERROR;
136 sqlite3SafetyOff(db);
137 goto blob_open_out;
138 }
139 }
140 }
141 }
142
danielk1977b4e9af92007-05-01 17:49:49 +0000143 v = sqlite3VdbeCreate(db);
144 if( v ){
145 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
146 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
147
148 /* Configure the OP_Transaction */
149 sqlite3VdbeChangeP1(v, 0, iDb);
150 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0));
151
152 /* Configure the OP_VerifyCookie */
153 sqlite3VdbeChangeP1(v, 1, iDb);
154 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
155
156 /* Configure the db number pushed onto the stack */
157 sqlite3VdbeChangeP1(v, 2, iDb);
158
danielk1977f1819242007-05-03 18:14:10 +0000159 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
160 ** parameter of the other to pTab->tnum.
danielk1977b4e9af92007-05-01 17:49:49 +0000161 */
162 sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1);
163 sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum);
164
165 /* Configure the OP_SetNumColumns. Configure the cursor to
166 ** think that the table has one more column than it really
167 ** does. An OP_Column to retrieve this imaginary column will
168 ** always return an SQL NULL. This is useful because it means
169 ** we can invoke OP_Column to fill in the vdbe cursors type
170 ** and offset cache without causing any IO.
171 */
172 sqlite3VdbeChangeP2(v, 5, pTab->nCol+1);
173 sqlite3VdbeMakeReady(v, 1, 0, 1, 0);
174 }
175
danielk197720713f32007-05-03 11:43:33 +0000176 rc = sqlite3SafetyOff(db);
177 if( rc!=SQLITE_OK ){
178 return rc;
179 }
180
danielk1977b4e9af92007-05-01 17:49:49 +0000181 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
182 rc = sqlite3_step((sqlite3_stmt *)v);
183 if( rc!=SQLITE_ROW ){
184 nAttempt++;
185 rc = sqlite3_finalize((sqlite3_stmt *)v);
danielk1977f1819242007-05-03 18:14:10 +0000186 sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db));
danielk1977b4e9af92007-05-01 17:49:49 +0000187 v = 0;
188 }
189 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
190
191 if( rc==SQLITE_ROW ){
192 /* The row-record has been opened successfully. Check that the
193 ** column in question contains text or a blob. If it contains
194 ** text, it is up to the caller to get the encoding right.
195 */
196 Incrblob *pBlob;
197 u32 type = v->apCsr[0]->aType[iCol];
198
199 if( type<12 ){
danielk1977f1819242007-05-03 18:14:10 +0000200 sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s",
201 type==0?"null": type==7?"real": "integer"
202 );
danielk1977b4e9af92007-05-01 17:49:49 +0000203 rc = SQLITE_ERROR;
204 goto blob_open_out;
205 }
206 pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob));
207 if( sqlite3MallocFailed() ){
208 sqliteFree(pBlob);
209 goto blob_open_out;
210 }
211 pBlob->flags = flags;
212 pBlob->pCsr = v->apCsr[0]->pCursor;
danielk19772dec9702007-05-02 16:48:37 +0000213 sqlite3BtreeCacheOverflow(pBlob->pCsr);
danielk1977b4e9af92007-05-01 17:49:49 +0000214 pBlob->pStmt = (sqlite3_stmt *)v;
215 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
216 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
217 *ppBlob = (sqlite3_blob *)pBlob;
218 rc = SQLITE_OK;
danielk19778cbadb02007-05-03 16:31:26 +0000219 }else if( rc==SQLITE_OK ){
danielk1977f1819242007-05-03 18:14:10 +0000220 sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow);
danielk19778cbadb02007-05-03 16:31:26 +0000221 rc = SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +0000222 }
223
224blob_open_out:
225 if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
226 sqlite3_finalize((sqlite3_stmt *)v);
227 }
danielk1977f1819242007-05-03 18:14:10 +0000228 zErr[sizeof(zErr)-1] = '\0';
danielk19778cbadb02007-05-03 16:31:26 +0000229 sqlite3Error(db, rc, zErr);
danielk1977b4e9af92007-05-01 17:49:49 +0000230 return sqlite3ApiExit(db, rc);
231}
232
233/*
234** Close a blob handle.
235*/
236int sqlite3_blob_close(sqlite3_blob *pBlob){
237 Incrblob *p = (Incrblob *)pBlob;
238 sqlite3_finalize(p->pStmt);
239 sqliteFree(p);
240 return SQLITE_OK;
241}
242
243/*
244** Read data from a blob handle.
245*/
246int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
247 Incrblob *p = (Incrblob *)pBlob;
248 if( (iOffset+n)>p->nByte ){
249 return SQLITE_ERROR;
250 }
251 return sqlite3BtreeData(p->pCsr, iOffset+p->iOffset, n, z);
252}
253
254/*
255** Write data to a blob handle.
256*/
257int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
258 Incrblob *p = (Incrblob *)pBlob;
259 if( (iOffset+n)>p->nByte ){
260 return SQLITE_ERROR;
261 }
262 return sqlite3BtreePutData(p->pCsr, iOffset+p->iOffset, n, z);
263}
264
265/*
266** Query a blob handle for the size of the data.
267*/
268int sqlite3_blob_bytes(sqlite3_blob *pBlob){
269 Incrblob *p = (Incrblob *)pBlob;
270 return p->nByte;
271}
272
273#endif /* #ifndef SQLITE_OMIT_INCRBLOB */