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