Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: ae5a734179ed51396ce4bfd728df4d9df48615d5 [file] [log] [blame]
danb391b942014-11-07 14:41:111/*
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"
drh064b6812024-07-30 15:49:0215#include "tclsqlite.h"
danb391b942014-11-07 14:41:1116#include <stdlib.h>
17#include <string.h>
18#include <assert.h>
drhbfa582a2015-05-05 11:08:0219#ifndef SQLITE_OMIT_INCRBLOB
danb391b942014-11-07 14:41:1120
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** Return a pointer to a buffer containing a text representation of the
30** pointer passed as the only argument. The original pointer may be extracted
31** from the text using sqlite3TestTextToPtr().
32*/
33static char *ptrToText(void *p){
34 static char buf[100];
35 sqlite3_snprintf(sizeof(buf)-1, buf, "%p", p);
36 return buf;
37}
38
39/*
40** Attempt to extract a blob handle (type sqlite3_blob*) from the Tcl
41** object passed as the second argument. If successful, set *ppBlob to
42** point to the blob handle and return TCL_OK. Otherwise, store an error
43** message in the tcl interpreter and return TCL_ERROR. The final value
44** of *ppBlob is undefined in this case.
45**
46** If the object contains a string that begins with "incrblob_", then it
47** is assumed to be the name of a Tcl channel opened using the [db incrblob]
48** command (see tclsqlite.c). Otherwise, it is assumed to be a pointer
49** encoded using the ptrToText() routine or similar.
50*/
51static int blobHandleFromObj(
52 Tcl_Interp *interp,
53 Tcl_Obj *pObj,
54 sqlite3_blob **ppBlob
55){
56 char *z;
drh064b6812024-07-30 15:49:0257 Tcl_Size n;
danb391b942014-11-07 14:41:1158
59 z = Tcl_GetStringFromObj(pObj, &n);
60 if( n==0 ){
61 *ppBlob = 0;
62 }else if( n>9 && 0==memcmp("incrblob_", z, 9) ){
63 int notUsed;
64 Tcl_Channel channel;
65 ClientData instanceData;
66
67 channel = Tcl_GetChannel(interp, z, &notUsed);
68 if( !channel ) return TCL_ERROR;
69
70 Tcl_Flush(channel);
71 Tcl_Seek(channel, 0, SEEK_SET);
72
73 instanceData = Tcl_GetChannelInstanceData(channel);
74 *ppBlob = *((sqlite3_blob **)instanceData);
75 }else{
76 *ppBlob = (sqlite3_blob*)sqlite3TestTextToPtr(z);
77 }
78
79 return TCL_OK;
80}
81
82/*
83** Like Tcl_GetString(), except that if the string is 0 bytes in size, a
84** NULL Pointer is returned.
85*/
86static char *blobStringFromObj(Tcl_Obj *pObj){
drh064b6812024-07-30 15:49:0287 Tcl_Size n;
danb391b942014-11-07 14:41:1188 char *z;
89 z = Tcl_GetStringFromObj(pObj, &n);
90 return (n ? z : 0);
91}
92
93/*
94** sqlite3_blob_open DB DATABASE TABLE COLUMN ROWID FLAGS VARNAME
95**
96** Tcl test harness for the sqlite3_blob_open() function.
97*/
mistachkin7617e4a2016-07-28 17:11:2098static int SQLITE_TCLAPI test_blob_open(
danb391b942014-11-07 14:41:1199 ClientData clientData, /* Not used */
100 Tcl_Interp *interp, /* Calling TCL interpreter */
101 int objc, /* Number of arguments */
102 Tcl_Obj *CONST objv[] /* Command arguments */
103){
104 sqlite3 *db;
105 const char *zDb;
106 const char *zTable;
107 const char *zColumn;
drh9a1e85e2016-02-19 13:29:52108 Tcl_WideInt iRowid;
danb391b942014-11-07 14:41:11109 int flags;
110 const char *zVarname;
drh064b6812024-07-30 15:49:02111 Tcl_Size nVarname;
danb391b942014-11-07 14:41:11112
drh48c286f2016-11-11 20:37:27113 sqlite3_blob *pBlob = (sqlite3_blob*)&flags; /* Non-zero initialization */
danb391b942014-11-07 14:41:11114 int rc;
115
116 if( objc!=8 ){
117 const char *zUsage = "DB DATABASE TABLE COLUMN ROWID FLAGS VARNAME";
118 Tcl_WrongNumArgs(interp, 1, objv, zUsage);
119 return TCL_ERROR;
120 }
121 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
122 zDb = Tcl_GetString(objv[2]);
123 zTable = blobStringFromObj(objv[3]);
124 zColumn = Tcl_GetString(objv[4]);
125 if( Tcl_GetWideIntFromObj(interp, objv[5], &iRowid) ) return TCL_ERROR;
126 if( Tcl_GetIntFromObj(interp, objv[6], &flags) ) return TCL_ERROR;
127 zVarname = Tcl_GetStringFromObj(objv[7], &nVarname);
128
129 if( nVarname>0 ){
130 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRowid, flags, &pBlob);
131 Tcl_SetVar(interp, zVarname, ptrToText(pBlob), 0);
132 }else{
133 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRowid, flags, 0);
134 }
135
136 if( rc==SQLITE_OK ){
137 Tcl_ResetResult(interp);
138 }else{
139 Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE);
140 return TCL_ERROR;
141 }
142 return TCL_OK;
143}
144
145
146/*
147** sqlite3_blob_close HANDLE
148*/
mistachkin7617e4a2016-07-28 17:11:20149static int SQLITE_TCLAPI test_blob_close(
danb391b942014-11-07 14:41:11150 ClientData clientData, /* Not used */
151 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
152 int objc, /* Number of arguments */
153 Tcl_Obj *CONST objv[] /* Command arguments */
154){
155 sqlite3_blob *pBlob;
156 int rc;
157
158 if( objc!=2 ){
159 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
160 return TCL_ERROR;
161 }
162
163 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
164 rc = sqlite3_blob_close(pBlob);
165
166 if( rc ){
167 Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE);
168 }else{
169 Tcl_ResetResult(interp);
170 }
171 return TCL_OK;
172}
173
174/*
175** sqlite3_blob_bytes HANDLE
176*/
mistachkin7617e4a2016-07-28 17:11:20177static int SQLITE_TCLAPI test_blob_bytes(
danb391b942014-11-07 14:41:11178 ClientData clientData, /* Not used */
179 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
180 int objc, /* Number of arguments */
181 Tcl_Obj *CONST objv[] /* Command arguments */
182){
183 sqlite3_blob *pBlob;
184 int nByte;
185
186 if( objc!=2 ){
187 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
188 return TCL_ERROR;
189 }
190
191 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
192 nByte = sqlite3_blob_bytes(pBlob);
193 Tcl_SetObjResult(interp, Tcl_NewIntObj(nByte));
194
195 return TCL_OK;
196}
197
198/*
199** sqlite3_blob_read CHANNEL OFFSET N
200**
201** This command is used to test the sqlite3_blob_read() in ways that
202** the Tcl channel interface does not. The first argument should
203** be the name of a valid channel created by the [incrblob] method
204** of a database handle. This function calls sqlite3_blob_read()
205** to read N bytes from offset OFFSET from the underlying SQLite
206** blob handle.
207**
208** On success, a byte-array object containing the read data is
209** returned. On failure, the interpreter result is set to the
210** text representation of the returned error code (i.e. "SQLITE_NOMEM")
211** and a Tcl exception is thrown.
212*/
mistachkin7617e4a2016-07-28 17:11:20213static int SQLITE_TCLAPI test_blob_read(
danb391b942014-11-07 14:41:11214 ClientData clientData, /* Not used */
215 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
216 int objc, /* Number of arguments */
217 Tcl_Obj *CONST objv[] /* Command arguments */
218){
219 sqlite3_blob *pBlob;
220 int nByte;
221 int iOffset;
222 unsigned char *zBuf = 0;
223 int rc;
224
225 if( objc!=4 ){
226 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
227 return TCL_ERROR;
228 }
229
230 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
231 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
232 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
233 ){
234 return TCL_ERROR;
235 }
236
237 if( nByte>0 ){
mistachkindd22c092017-02-15 01:39:28238 zBuf = (unsigned char *)Tcl_AttemptAlloc(nByte);
239 if( zBuf==0 ){
stephanb6503f12025-03-06 13:38:07240 Tcl_AppendResult(interp, "out of memory in " __FILE__, NULL);
mistachkindd22c092017-02-15 01:39:28241 return TCL_ERROR;
242 }
danb391b942014-11-07 14:41:11243 }
244 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
245 if( rc==SQLITE_OK ){
246 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
247 }else{
248 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
249 }
250 Tcl_Free((char *)zBuf);
251
252 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
253}
254
255/*
256** sqlite3_blob_write HANDLE OFFSET DATA ?NDATA?
257**
258** This command is used to test the sqlite3_blob_write() in ways that
259** the Tcl channel interface does not. The first argument should
260** be the name of a valid channel created by the [incrblob] method
261** of a database handle. This function calls sqlite3_blob_write()
262** to write the DATA byte-array to the underlying SQLite blob handle.
263** at offset OFFSET.
264**
265** On success, an empty string is returned. On failure, the interpreter
266** result is set to the text representation of the returned error code
267** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
268*/
mistachkin7617e4a2016-07-28 17:11:20269static int SQLITE_TCLAPI test_blob_write(
danb391b942014-11-07 14:41:11270 ClientData clientData, /* Not used */
271 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
272 int objc, /* Number of arguments */
273 Tcl_Obj *CONST objv[] /* Command arguments */
274){
275 sqlite3_blob *pBlob;
276 int iOffset;
277 int rc;
278
279 unsigned char *zBuf;
drh064b6812024-07-30 15:49:02280 Tcl_Size nBuf;
281 int n;
danb391b942014-11-07 14:41:11282
283 if( objc!=4 && objc!=5 ){
284 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET DATA ?NDATA?");
285 return TCL_ERROR;
286 }
287
288 if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
289 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
290 return TCL_ERROR;
291 }
292
293 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
drh064b6812024-07-30 15:49:02294 n = (int)(nBuf & 0x7fffffff);
295 if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &n) ){
danb391b942014-11-07 14:41:11296 return TCL_ERROR;
297 }
drh064b6812024-07-30 15:49:02298 rc = sqlite3_blob_write(pBlob, zBuf, n, iOffset);
danb391b942014-11-07 14:41:11299 if( rc!=SQLITE_OK ){
300 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
301 }
302
303 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
304}
drhbfa582a2015-05-05 11:08:02305#endif /* SQLITE_OMIT_INCRBLOB */
danb391b942014-11-07 14:41:11306
307/*
308** Register commands with the TCL interpreter.
309*/
310int Sqlitetest_blob_Init(Tcl_Interp *interp){
drhbfa582a2015-05-05 11:08:02311#ifndef SQLITE_OMIT_INCRBLOB
danb391b942014-11-07 14:41:11312 static struct {
313 char *zName;
314 Tcl_ObjCmdProc *xProc;
315 } aObjCmd[] = {
316 { "sqlite3_blob_open", test_blob_open },
317 { "sqlite3_blob_close", test_blob_close },
318 { "sqlite3_blob_bytes", test_blob_bytes },
319 { "sqlite3_blob_read", test_blob_read },
320 { "sqlite3_blob_write", test_blob_write },
321 };
322 int i;
323 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
324 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
325 }
drhbfa582a2015-05-05 11:08:02326#endif /* SQLITE_OMIT_INCRBLOB */
danb391b942014-11-07 14:41:11327 return TCL_OK;
328}