interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / test_blob.c

317 lines · 9408 bytes Raw Blame History
1/*
2** 2014 October 30
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**
13*/
14#include "sqliteInt.h"
15#include "tclsqlite.h"
16#include <stdlib.h>
17#include <string.h>
18#include <assert.h>
19#ifndef SQLITE_OMIT_INCRBLOB
20
21/* These functions are implemented in main.c. */
22extern const char *sqlite3ErrName(int);
23
24/* From test1.c: */
25extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
26extern void *sqlite3TestTextToPtr(const char *z);
27
28/*
29** Attempt to extract a blob handle (type sqlite3_blob*) from the Tcl
30** object passed as the second argument. If successful, set *ppBlob to
31** point to the blob handle and return TCL_OK. Otherwise, store an error
32** message in the tcl interpreter and return TCL_ERROR. The final value
33** of *ppBlob is undefined in this case.
34**
35** If the object contains a string that begins with "incrblob_", then it
36** is assumed to be the name of a Tcl channel opened using the [db incrblob]
37** command (see tclsqlite.c). Otherwise, it is assumed to be a pointer
38** encoded using the ptrToText() routine or similar.
39*/
40static int blobHandleFromObj(
41 Tcl_Interp *interp,
42 Tcl_Obj *pObj,
43 sqlite3_blob **ppBlob
44){
45 char *z;
46 Tcl_Size n;
47
48 z = Tcl_GetStringFromObj(pObj, &n);
49 if( n==0 ){
50 *ppBlob = 0;
51 }else if( n>9 && 0==memcmp("incrblob_", z, 9) ){
52 int notUsed;
53 Tcl_Channel channel;
54 ClientData instanceData;
55
56 channel = Tcl_GetChannel(interp, z, &notUsed);
57 if( !channel ) return TCL_ERROR;
58
59 Tcl_Flush(channel);
60 Tcl_Seek(channel, 0, SEEK_SET);
61
62 instanceData = Tcl_GetChannelInstanceData(channel);
63 *ppBlob = *((sqlite3_blob **)instanceData);
64 }else{
65 *ppBlob = (sqlite3_blob*)sqlite3TestTextToPtr(z);
66 }
67
68 return TCL_OK;
69}
70
71/*
72** Like Tcl_GetString(), except that if the string is 0 bytes in size, a
73** NULL Pointer is returned.
74*/
75static char *blobStringFromObj(Tcl_Obj *pObj){
76 Tcl_Size n;
77 char *z;
78 z = Tcl_GetStringFromObj(pObj, &n);
79 return (n ? z : 0);
80}
81
82/*
83** sqlite3_blob_open DB DATABASE TABLE COLUMN ROWID FLAGS VARNAME
84**
85** Tcl test harness for the sqlite3_blob_open() function.
86*/
87static int SQLITE_TCLAPI test_blob_open(
88 ClientData clientData, /* Not used */
89 Tcl_Interp *interp, /* Calling TCL interpreter */
90 int objc, /* Number of arguments */
91 Tcl_Obj *CONST objv[] /* Command arguments */
92){
93 sqlite3 *db;
94 const char *zDb;
95 const char *zTable;
96 const char *zColumn;
97 Tcl_WideInt iRowid;
98 int flags;
99 const char *zVarname;
100 Tcl_Size nVarname;
101
102 sqlite3_blob *pBlob = (sqlite3_blob*)&flags; /* Non-zero initialization */
103 int rc;
104
105 if( objc!=8 ){
106 const char *zUsage = "DB DATABASE TABLE COLUMN ROWID FLAGS VARNAME";
107 Tcl_WrongNumArgs(interp, 1, objv, zUsage);
108 return TCL_ERROR;
109 }
110 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
111 zDb = Tcl_GetString(objv[2]);
112 zTable = blobStringFromObj(objv[3]);
113 zColumn = Tcl_GetString(objv[4]);
114 if( Tcl_GetWideIntFromObj(interp, objv[5], &iRowid) ) return TCL_ERROR;
115 if( Tcl_GetIntFromObj(interp, objv[6], &flags) ) return TCL_ERROR;
116 zVarname = Tcl_GetStringFromObj(objv[7], &nVarname);
117
118 if( nVarname>0 ){
119 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRowid, flags, &pBlob);
120 Tcl_SetVar(interp, zVarname, sqlite3TestPtrToText(pBlob), 0);
121 }else{
122 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRowid, flags, 0);
123 }
124
125 if( rc==SQLITE_OK ){
126 Tcl_ResetResult(interp);
127 }else{
128 Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE);
129 return TCL_ERROR;
130 }
131 return TCL_OK;
132}
133
134
135/*
136** sqlite3_blob_close HANDLE
137*/
138static int SQLITE_TCLAPI test_blob_close(
139 ClientData clientData, /* Not used */
140 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
141 int objc, /* Number of arguments */
142 Tcl_Obj *CONST objv[] /* Command arguments */
143){
144 sqlite3_blob *pBlob;
145 int rc;
146
147 if( objc!=2 ){
148 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
149 return TCL_ERROR;
150 }
151
152 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
153 rc = sqlite3_blob_close(pBlob);
154
155 if( rc ){
156 Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE);
157 }else{
158 Tcl_ResetResult(interp);
159 }
160 return TCL_OK;
161}
162
163/*
164** sqlite3_blob_bytes HANDLE
165*/
166static int SQLITE_TCLAPI test_blob_bytes(
167 ClientData clientData, /* Not used */
168 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
169 int objc, /* Number of arguments */
170 Tcl_Obj *CONST objv[] /* Command arguments */
171){
172 sqlite3_blob *pBlob;
173 int nByte;
174
175 if( objc!=2 ){
176 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
177 return TCL_ERROR;
178 }
179
180 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
181 nByte = sqlite3_blob_bytes(pBlob);
182 Tcl_SetObjResult(interp, Tcl_NewIntObj(nByte));
183
184 return TCL_OK;
185}
186
187/*
188** sqlite3_blob_read CHANNEL OFFSET N
189**
190** This command is used to test the sqlite3_blob_read() in ways that
191** the Tcl channel interface does not. The first argument should
192** be the name of a valid channel created by the [incrblob] method
193** of a database handle. This function calls sqlite3_blob_read()
194** to read N bytes from offset OFFSET from the underlying SQLite
195** blob handle.
196**
197** On success, a byte-array object containing the read data is
198** returned. On failure, the interpreter result is set to the
199** text representation of the returned error code (i.e. "SQLITE_NOMEM")
200** and a Tcl exception is thrown.
201*/
202static int SQLITE_TCLAPI test_blob_read(
203 ClientData clientData, /* Not used */
204 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
205 int objc, /* Number of arguments */
206 Tcl_Obj *CONST objv[] /* Command arguments */
207){
208 sqlite3_blob *pBlob;
209 int nByte;
210 int iOffset;
211 unsigned char *zBuf = 0;
212 int rc;
213
214 if( objc!=4 ){
215 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
216 return TCL_ERROR;
217 }
218
219 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
220 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
221 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
222 ){
223 return TCL_ERROR;
224 }
225
226 if( nByte>0 ){
227 zBuf = (unsigned char *)Tcl_AttemptAlloc(nByte);
228 if( zBuf==0 ){
229 Tcl_AppendResult(interp, "out of memory in " __FILE__, NULL);
230 return TCL_ERROR;
231 }
232 }
233 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
234 if( rc==SQLITE_OK ){
235 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
236 }else{
237 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
238 }
239 Tcl_Free((char *)zBuf);
240
241 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
242}
243
244/*
245** sqlite3_blob_write HANDLE OFFSET DATA ?NDATA?
246**
247** This command is used to test the sqlite3_blob_write() in ways that
248** the Tcl channel interface does not. The first argument should
249** be the name of a valid channel created by the [incrblob] method
250** of a database handle. This function calls sqlite3_blob_write()
251** to write the DATA byte-array to the underlying SQLite blob handle.
252** at offset OFFSET.
253**
254** On success, an empty string is returned. On failure, the interpreter
255** result is set to the text representation of the returned error code
256** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
257*/
258static int SQLITE_TCLAPI test_blob_write(
259 ClientData clientData, /* Not used */
260 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
261 int objc, /* Number of arguments */
262 Tcl_Obj *CONST objv[] /* Command arguments */
263){
264 sqlite3_blob *pBlob;
265 int iOffset;
266 int rc;
267
268 unsigned char *zBuf;
269 Tcl_Size nBuf;
270 int n;
271
272 if( objc!=4 && objc!=5 ){
273 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET DATA ?NDATA?");
274 return TCL_ERROR;
275 }
276
277 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
278 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
279 return TCL_ERROR;
280 }
281
282 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
283 n = (int)(nBuf & 0x7fffffff);
284 if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &n) ){
285 return TCL_ERROR;
286 }
287 rc = sqlite3_blob_write(pBlob, zBuf, n, iOffset);
288 if( rc!=SQLITE_OK ){
289 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
290 }
291
292 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
293}
294#endif /* SQLITE_OMIT_INCRBLOB */
295
296/*
297** Register commands with the TCL interpreter.
298*/
299int Sqlitetest_blob_Init(Tcl_Interp *interp){
300#ifndef SQLITE_OMIT_INCRBLOB
301 static struct {
302 char *zName;
303 Tcl_ObjCmdProc *xProc;
304 } aObjCmd[] = {
305 { "sqlite3_blob_open", test_blob_open },
306 { "sqlite3_blob_close", test_blob_close },
307 { "sqlite3_blob_bytes", test_blob_bytes },
308 { "sqlite3_blob_read", test_blob_read },
309 { "sqlite3_blob_write", test_blob_write },
310 };
311 int i;
312 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
313 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
314 }
315#endif /* SQLITE_OMIT_INCRBLOB */
316 return TCL_OK;
317}