1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
** 2010 August 28
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing all sorts of SQLite interfaces. This code
** is not included in the SQLite library.
*/
#include "sqlite3.h"
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
/* Solely for the UNUSED_PARAMETER() macro. */
#include "sqliteInt.h"
#ifdef SQLITE_ENABLE_RTREE
typedef struct BoxGeomCtx BoxGeomCtx;
struct BoxGeomCtx {
Tcl_Interp *interp;
Tcl_Obj *pScript;
};
typedef struct BoxQueryCtx BoxQueryCtx;
struct BoxQueryCtx {
Tcl_Interp *interp;
Tcl_Obj *pScript;
};
static void testDelUser(void *pCtx){
BoxGeomCtx *p = (BoxGeomCtx*)pCtx;
Tcl_EvalObjEx(p->interp, p->pScript, 0);
Tcl_DecrRefCount(p->pScript);
sqlite3_free(p);
}
static int invokeTclGeomCb(
const char *zName,
sqlite3_rtree_geometry *p,
int nCoord,
sqlite3_rtree_dbl *aCoord
){
int rc = SQLITE_OK;
if( p->pContext ){
char aPtr[64];
BoxGeomCtx *pCtx = (BoxGeomCtx*)p->pContext;
Tcl_Interp *interp = pCtx->interp;
Tcl_Obj *pScript = 0;
Tcl_Obj *pParam = 0;
Tcl_Obj *pCoord = 0;
int ii;
Tcl_Obj *pRes;
pScript = Tcl_DuplicateObj(pCtx->pScript);
Tcl_IncrRefCount(pScript);
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zName,-1));
sqlite3_snprintf(sizeof(aPtr)-1, aPtr, "%p", (void*)p->pContext);
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(aPtr,-1));
pParam = Tcl_NewObj();
for(ii=0; ii<p->nParam; ii++){
Tcl_ListObjAppendElement(
interp, pParam, Tcl_NewDoubleObj(p->aParam[ii])
);
}
Tcl_ListObjAppendElement(interp, pScript, pParam);
pCoord = Tcl_NewObj();
for(ii=0; ii<nCoord; ii++){
Tcl_ListObjAppendElement(interp, pCoord, Tcl_NewDoubleObj(aCoord[ii]));
}
Tcl_ListObjAppendElement(interp, pScript, pCoord);
sqlite3_snprintf(sizeof(aPtr)-1, aPtr, "%p", (void*)p);
Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(aPtr,-1));
rc = Tcl_EvalObjEx(interp, pScript, 0);
if( rc!=TCL_OK ){
rc = SQLITE_ERROR;
}else{
int nObj = 0;
Tcl_Obj **aObj = 0;
pRes = Tcl_GetObjResult(interp);
if( Tcl_ListObjGetElements(interp, pRes, &nObj, &aObj) ) return TCL_ERROR;
if( nObj>0 ){
const char *zCmd = Tcl_GetString(aObj[0]);
if( 0==sqlite3_stricmp(zCmd, "zero") ){
p->aParam[0] = 0.0;
p->nParam = 1;
}
else if( 0==sqlite3_stricmp(zCmd, "user") ){
if( p->pUser || p->xDelUser ){
rc = SQLITE_ERROR;
}else{
BoxGeomCtx *pBGCtx = sqlite3_malloc(sizeof(BoxGeomCtx));
if( pBGCtx==0 ){
rc = SQLITE_NOMEM;
}else{
pBGCtx->interp = interp;
pBGCtx->pScript = Tcl_DuplicateObj(pRes);
Tcl_IncrRefCount(pBGCtx->pScript);
Tcl_ListObjReplace(interp, pBGCtx->pScript, 0, 1, 0, 0);
p->pUser = (void*)pBGCtx;
p->xDelUser = testDelUser;
}
}
}
else if( 0==sqlite3_stricmp(zCmd, "user_is_zero") ){
if( p->pUser || p->xDelUser ) rc = SQLITE_ERROR;
}
}
}
}
return rc;
}
/*
# EVIDENCE-OF: R-00693-36727 The legacy xGeom callback is invoked with
# four arguments.
# EVIDENCE-OF: R-50437-53270 The first argument is a pointer to an
# sqlite3_rtree_geometry structure which provides information about how
# the SQL function was invoked.
# EVIDENCE-OF: R-00090-24248 The third argument, aCoord[], is an array
# of nCoord coordinates that defines a bounding box to be tested.
# EVIDENCE-OF: R-28207-40885 The last argument is a pointer into which
# the callback result should be written.
*/
static int box_geom(
sqlite3_rtree_geometry *p, /* R-50437-53270 */
int nCoord, /* R-02424-24769 */
sqlite3_rtree_dbl *aCoord, /* R-00090-24248 */
int *pRes /* R-28207-40885 */
){
int ii;
if( p->nParam!=nCoord ){
invokeTclGeomCb("box", p, nCoord, aCoord);
return SQLITE_ERROR;
}
if( invokeTclGeomCb("box", p, nCoord, aCoord) ) return SQLITE_ERROR;
for(ii=0; ii<nCoord; ii+=2){
if( aCoord[ii]>p->aParam[ii+1] || aCoord[ii+1]<p->aParam[ii] ){
/* R-28207-40885 */
*pRes = 0;
return SQLITE_OK;
}
}
/* R-28207-40885 */
*pRes = 1;
return SQLITE_OK;
}
static int SQLITE_TCLAPI register_box_geom(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
extern const char *sqlite3ErrName(int);
sqlite3 *db;
BoxGeomCtx *pCtx;
char aPtr[64];
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB SCRIPT");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
pCtx = (BoxGeomCtx*)ckalloc(sizeof(BoxGeomCtx*));
pCtx->interp = interp;
pCtx->pScript = Tcl_DuplicateObj(objv[2]);
Tcl_IncrRefCount(pCtx->pScript);
sqlite3_rtree_geometry_callback(db, "box", box_geom, (void*)pCtx);
sqlite3_snprintf(64, aPtr, "%p", (void*)pCtx);
Tcl_SetObjResult(interp, Tcl_NewStringObj(aPtr, -1));
return TCL_OK;
}
static int box_query(sqlite3_rtree_query_info *pInfo){
const char *azParentWithin[] = {"not", "partly", "fully", 0};
BoxQueryCtx *pCtx = (BoxQueryCtx*)pInfo->pContext;
Tcl_Interp *interp = pCtx->interp;
Tcl_Obj *pEval;
Tcl_Obj *pArg;
Tcl_Obj *pTmp = 0;
int rc;
int ii;
pEval = Tcl_DuplicateObj(pCtx->pScript);
Tcl_IncrRefCount(pEval);
pArg = Tcl_NewObj();
Tcl_IncrRefCount(pArg);
/* aParam[] */
pTmp = Tcl_NewObj();
Tcl_IncrRefCount(pTmp);
for(ii=0; ii<pInfo->nParam; ii++){
Tcl_Obj *p = Tcl_NewDoubleObj(pInfo->aParam[ii]);
Tcl_ListObjAppendElement(interp, pTmp, p);
}
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("aParam", -1));
Tcl_ListObjAppendElement(interp, pArg, pTmp);
Tcl_DecrRefCount(pTmp);
/* aCoord[] */
pTmp = Tcl_NewObj();
Tcl_IncrRefCount(pTmp);
for(ii=0; ii<pInfo->nCoord; ii++){
Tcl_Obj *p = Tcl_NewDoubleObj(pInfo->aCoord[ii]);
Tcl_ListObjAppendElement(interp, pTmp, p);
}
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("aCoord", -1));
Tcl_ListObjAppendElement(interp, pArg, pTmp);
Tcl_DecrRefCount(pTmp);
/* anQueue[] */
pTmp = Tcl_NewObj();
Tcl_IncrRefCount(pTmp);
for(ii=0; ii<=pInfo->mxLevel; ii++){
Tcl_Obj *p = Tcl_NewIntObj((int)pInfo->anQueue[ii]);
Tcl_ListObjAppendElement(interp, pTmp, p);
}
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("anQueue", -1));
Tcl_ListObjAppendElement(interp, pArg, pTmp);
Tcl_DecrRefCount(pTmp);
/* iLevel */
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("iLevel", -1));
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewIntObj(pInfo->iLevel));
/* mxLevel */
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("mxLevel", -1));
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewIntObj(pInfo->mxLevel));
/* iRowid */
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("iRowid", -1));
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewWideIntObj(pInfo->iRowid));
/* rParentScore */
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("rParentScore", -1));
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewDoubleObj(pInfo->rParentScore));
/* eParentWithin */
assert( pInfo->eParentWithin==0
|| pInfo->eParentWithin==1
|| pInfo->eParentWithin==2
);
Tcl_ListObjAppendElement(interp, pArg, Tcl_NewStringObj("eParentWithin", -1));
Tcl_ListObjAppendElement(interp, pArg,
Tcl_NewStringObj(azParentWithin[pInfo->eParentWithin], -1)
);
Tcl_ListObjAppendElement(interp, pEval, pArg);
rc = Tcl_EvalObjEx(interp, pEval, 0) ? SQLITE_ERROR : SQLITE_OK;
if( rc==SQLITE_OK ){
double rScore = 0.0;
int nObj = 0;
int eP = 0;
Tcl_Obj **aObj = 0;
Tcl_Obj *pRes = Tcl_GetObjResult(interp);
if( Tcl_ListObjGetElements(interp, pRes, &nObj, &aObj)
|| nObj!=2
|| Tcl_GetDoubleFromObj(interp, aObj[1], &rScore)
|| Tcl_GetIndexFromObj(interp, aObj[0], azParentWithin, "value", 0, &eP)
){
rc = SQLITE_ERROR;
}else{
pInfo->rScore = rScore;
pInfo->eParentWithin = eP;
}
}
Tcl_DecrRefCount(pArg);
Tcl_DecrRefCount(pEval);
return rc;
}
static void box_query_destroy(void *p){
BoxQueryCtx *pCtx = (BoxQueryCtx*)p;
Tcl_DecrRefCount(pCtx->pScript);
ckfree((char*)pCtx);
}
static int SQLITE_TCLAPI register_box_query(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
extern const char *sqlite3ErrName(int);
sqlite3 *db;
BoxQueryCtx *pCtx;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB SCRIPT");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
pCtx = (BoxQueryCtx*)ckalloc(sizeof(BoxQueryCtx));
pCtx->interp = interp;
pCtx->pScript = Tcl_DuplicateObj(objv[2]);
Tcl_IncrRefCount(pCtx->pScript);
sqlite3_rtree_query_callback(
db, "qbox", box_query, (void*)pCtx, box_query_destroy
);
Tcl_ResetResult(interp);
return TCL_OK;
}
#endif /* SQLITE_ENABLE_RTREE */
int Sqlitetestrtreedoc_Init(Tcl_Interp *interp){
#ifdef SQLITE_ENABLE_RTREE
Tcl_CreateObjCommand(interp, "register_box_geom", register_box_geom, 0, 0);
Tcl_CreateObjCommand(interp, "register_box_query", register_box_query, 0, 0);
#endif /* SQLITE_ENABLE_RTREE */
return TCL_OK;
}
|