blob: 7c55a635b9e2cccbf6f89ae37b854983fb6007f8 [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**
danielk197720713f32007-05-03 11:43:33 +000013** $Id: vdbeblob.c,v 1.3 2007/05/03 11:43:33 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){
46 int rc = SQLITE_OK;
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;
86
87 do {
88 Parse sParse;
89 Table *pTab;
90
91 memset(&sParse, 0, sizeof(Parse));
92 sParse.db = db;
93
danielk197720713f32007-05-03 11:43:33 +000094 rc = sqlite3SafetyOn(db);
95 if( rc!=SQLITE_OK ){
96 return rc;
97 }
98
danielk1977b4e9af92007-05-01 17:49:49 +000099 pTab = sqlite3LocateTable(&sParse, zTable, zDb);
100 if( !pTab ){
101 sqlite3Error(db, sParse.rc, "%s", sParse.zErrMsg);
102 sqliteFree(sParse.zErrMsg);
103 rc = sParse.rc;
danielk197720713f32007-05-03 11:43:33 +0000104 sqlite3SafetyOff(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000105 goto blob_open_out;
106 }
107
108 /* Now search pTab for the exact column. */
109 for(iCol=0; iCol < pTab->nCol; iCol++) {
110 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
111 break;
112 }
113 }
114 if( iCol==pTab->nCol ){
115 sqlite3Error(db, SQLITE_ERROR, "no such column: %s", zColumn);
116 sqliteFree(sParse.zErrMsg);
117 rc = SQLITE_ERROR;
danielk197720713f32007-05-03 11:43:33 +0000118 sqlite3SafetyOff(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000119 goto blob_open_out;
120 }
121
122 v = sqlite3VdbeCreate(db);
123 if( v ){
124 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
126
127 /* Configure the OP_Transaction */
128 sqlite3VdbeChangeP1(v, 0, iDb);
129 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0));
130
131 /* Configure the OP_VerifyCookie */
132 sqlite3VdbeChangeP1(v, 1, iDb);
133 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
134
135 /* Configure the db number pushed onto the stack */
136 sqlite3VdbeChangeP1(v, 2, iDb);
137
138 /* Remove either the OP_OpenWrite or OpenRead. Set the P2 parameter
139 ** of the other to pTab->tnum.
140 */
141 sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1);
142 sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum);
143
144 /* Configure the OP_SetNumColumns. Configure the cursor to
145 ** think that the table has one more column than it really
146 ** does. An OP_Column to retrieve this imaginary column will
147 ** always return an SQL NULL. This is useful because it means
148 ** we can invoke OP_Column to fill in the vdbe cursors type
149 ** and offset cache without causing any IO.
150 */
151 sqlite3VdbeChangeP2(v, 5, pTab->nCol+1);
152 sqlite3VdbeMakeReady(v, 1, 0, 1, 0);
153 }
154
danielk197720713f32007-05-03 11:43:33 +0000155 rc = sqlite3SafetyOff(db);
156 if( rc!=SQLITE_OK ){
157 return rc;
158 }
159
danielk1977b4e9af92007-05-01 17:49:49 +0000160 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
161 rc = sqlite3_step((sqlite3_stmt *)v);
162 if( rc!=SQLITE_ROW ){
163 nAttempt++;
164 rc = sqlite3_finalize((sqlite3_stmt *)v);
165 v = 0;
166 }
167 } while( nAttempt<5 && rc==SQLITE_SCHEMA );
168
169 if( rc==SQLITE_ROW ){
170 /* The row-record has been opened successfully. Check that the
171 ** column in question contains text or a blob. If it contains
172 ** text, it is up to the caller to get the encoding right.
173 */
174 Incrblob *pBlob;
175 u32 type = v->apCsr[0]->aType[iCol];
176
177 if( type<12 ){
178 rc = SQLITE_ERROR;
179 goto blob_open_out;
180 }
181 pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob));
182 if( sqlite3MallocFailed() ){
183 sqliteFree(pBlob);
184 goto blob_open_out;
185 }
186 pBlob->flags = flags;
187 pBlob->pCsr = v->apCsr[0]->pCursor;
danielk19772dec9702007-05-02 16:48:37 +0000188 sqlite3BtreeCacheOverflow(pBlob->pCsr);
danielk1977b4e9af92007-05-01 17:49:49 +0000189 pBlob->pStmt = (sqlite3_stmt *)v;
190 pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
191 pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
192 *ppBlob = (sqlite3_blob *)pBlob;
193 rc = SQLITE_OK;
194 }else{
195 if( rc==SQLITE_DONE ){
196 rc = SQLITE_ERROR;
197 }
198 }
199
200blob_open_out:
201 if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
202 sqlite3_finalize((sqlite3_stmt *)v);
203 }
204 sqlite3Error(db, rc, "");
205 return sqlite3ApiExit(db, rc);
206}
207
208/*
209** Close a blob handle.
210*/
211int sqlite3_blob_close(sqlite3_blob *pBlob){
212 Incrblob *p = (Incrblob *)pBlob;
213 sqlite3_finalize(p->pStmt);
214 sqliteFree(p);
215 return SQLITE_OK;
216}
217
218/*
219** Read data from a blob handle.
220*/
221int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
222 Incrblob *p = (Incrblob *)pBlob;
223 if( (iOffset+n)>p->nByte ){
224 return SQLITE_ERROR;
225 }
226 return sqlite3BtreeData(p->pCsr, iOffset+p->iOffset, n, z);
227}
228
229/*
230** Write data to a blob handle.
231*/
232int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
233 Incrblob *p = (Incrblob *)pBlob;
234 if( (iOffset+n)>p->nByte ){
235 return SQLITE_ERROR;
236 }
237 return sqlite3BtreePutData(p->pCsr, iOffset+p->iOffset, n, z);
238}
239
240/*
241** Query a blob handle for the size of the data.
242*/
243int sqlite3_blob_bytes(sqlite3_blob *pBlob){
244 Incrblob *p = (Incrblob *)pBlob;
245 return p->nByte;
246}
247
248#endif /* #ifndef SQLITE_OMIT_INCRBLOB */