1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-01 06:27:03 +03:00

Add the sqlite3_value_dup() and sqlite3_value_free() interfaces. Use

these interfaces to enhance R-Tree to add the
sqlite3_rtree_query_info.apSqlParam field.

FossilOrigin-Name: a7ee40c4fc62843ac5b96ba47ca14a66e8cd6961
This commit is contained in:
drh
2015-05-20 21:28:32 +00:00
parent 7b12548906
commit 4f03f413bc
11 changed files with 155 additions and 46 deletions

View File

@ -351,6 +351,7 @@ struct RtreeMatchArg {
u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
RtreeGeomCallback cb; /* Info about the callback functions */
int nParam; /* Number of parameters to the SQL function */
sqlite3_value **apSqlParam; /* Original SQL parameter values */
RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
};
@ -1495,6 +1496,7 @@ static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
nExpected = (int)(sizeof(RtreeMatchArg) +
pBlob->nParam*sizeof(sqlite3_value*) +
(pBlob->nParam-1)*sizeof(RtreeDValue));
if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
sqlite3_free(pInfo);
@ -1503,6 +1505,7 @@ static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
pInfo->pContext = pBlob->cb.pContext;
pInfo->nParam = pBlob->nParam;
pInfo->aParam = pBlob->aParam;
pInfo->apSqlParam = pBlob->apSqlParam;
if( pBlob->cb.xGeom ){
pCons->u.xGeom = pBlob->cb.xGeom;
@ -3372,6 +3375,18 @@ static void rtreeFreeCallback(void *p){
sqlite3_free(p);
}
/*
** This routine frees the BLOB that is returned by geomCallback().
*/
static void rtreeMatchArgFree(void *pArg){
int i;
RtreeMatchArg *p = (RtreeMatchArg*)pArg;
for(i=0; i<p->nParam; i++){
sqlite3_value_free(p->apSqlParam[i]);
}
sqlite3_free(p);
}
/*
** Each call to sqlite3_rtree_geometry_callback() or
** sqlite3_rtree_query_callback() creates an ordinary SQLite
@ -3390,8 +3405,10 @@ static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
RtreeMatchArg *pBlob;
int nBlob;
int memErr = 0;
nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
+ nArg*sizeof(sqlite3_value*);
pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
if( !pBlob ){
sqlite3_result_error_nomem(ctx);
@ -3399,22 +3416,23 @@ static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
int i;
pBlob->magic = RTREE_GEOMETRY_MAGIC;
pBlob->cb = pGeomCtx[0];
pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
pBlob->nParam = nArg;
for(i=0; i<nArg; i++){
if( sqlite3_value_type(aArg[i])==SQLITE_BLOB
&& sqlite3_value_bytes(aArg[i])==sizeof(sqlite3_rtree_dbl)
){
memcpy(&pBlob->aParam[i], sqlite3_value_blob(aArg[i]),
sizeof(sqlite3_rtree_dbl));
}else{
pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
if( pBlob->apSqlParam[i]==0 ) memErr = 1;
#ifdef SQLITE_RTREE_INT_ONLY
pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
#else
pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
#endif
}
}
sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
if( memErr ){
sqlite3_result_error_nomem(ctx);
rtreeMatchArgFree(pBlob);
}else{
sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
}
}
}

View File

@ -52,6 +52,9 @@ do_execsql_test rtreeE-1.1 {
do_execsql_test rtreeE-1.1 {
SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 0.0, 50.0, 3) ORDER BY id;
} {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
do_execsql_test rtreeE-1.1x {
SELECT id FROM rt1 WHERE id MATCH Qcircle('x:0 y:0 r:50.0 e:3') ORDER BY id;
} {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
do_execsql_test rtreeE-1.2 {
SELECT id FROM rt1 WHERE id MATCH Qcircle(100.0, 0.0, 50.0, 3) ORDER BY id;
} {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}
@ -64,12 +67,12 @@ do_execsql_test rtreeE-1.3 {
# last.
#
do_execsql_test rtreeE-1.4 {
SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,3) AND id%100==0
SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:3') AND id%100==0
} {200 100 0}
# Exclude odd rowids on a depth-first search
do_execsql_test rtreeE-1.5 {
SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,4) ORDER BY +id
SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:4') ORDER BY +id
} {0 2 4 6 8 10 12 14 16 18 20 22 24 100 102 104 106 108 110 112 114 116 118 120 122 124 200 202 204 206 208 210 212 214 216 218 220 222 224}
# Exclude odd rowids on a breadth-first search.

View File

@ -98,6 +98,8 @@ struct sqlite3_rtree_query_info {
int eParentWithin; /* Visibility of parent node */
int eWithin; /* OUT: Visiblity */
sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
/* The following fields are only available in 3.8.11 and later */
sqlite3_value **apSqlParam; /* Original SQL values of parameters */
};
/*

View File

@ -1,5 +1,5 @@
C Fix\sthe\sinitialization\slogic\sin\sCREATE\sTABLE\sAS\sso\sthat\sthe\scorrect\saffinities\nare\sapplied\sto\sall\svalues\sbeing\sinserted\sinto\sthe\snew\stable,\seven\sif\sthe\nRHS\sis\sa\scompound\sSELECT.\s\sFix\sfor\sticket\s[f2ad7de056ab1dc9200].
D 2015-05-20T17:36:49.966
C Add\sthe\ssqlite3_value_dup()\sand\ssqlite3_value_free()\sinterfaces.\s\sUse\nthese\sinterfaces\sto\senhance\sR-Tree\sto\sadd\sthe\nsqlite3_rtree_query_info.apSqlParam\sfield.
D 2015-05-20T21:28:32.417
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 0a6ae26396ec696221021780dffbb894ff3cead7
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -143,7 +143,7 @@ F ext/ota/sqlite3ota.c 2246b779f46ab20d5e7876f5b96c378c601d20f4
F ext/ota/sqlite3ota.h 00028de37eede471ff1947d455cc3f33d3a911c6
F ext/ota/test_ota.c a876f88550d7d59a3ef62d4c1a5c04c4c2f1ebe1
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
F ext/rtree/rtree.c 60ec0a71d4d213665a706c795c887e7c4d148635
F ext/rtree/rtree.c 50d9df06d2b7ca946646531c20b3b627514ad832
F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e
F ext/rtree/rtree1.test 541bbcab74613907fea08b2ecdcdd5b7aa724cc9
F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba
@ -158,11 +158,11 @@ F ext/rtree/rtreeA.test ace05e729a36e342d40cf94e9efc7b4723d9dcdf
F ext/rtree/rtreeB.test c85f9ce78766c4e68b8b89fbf2979ee9cfa82b4e
F ext/rtree/rtreeC.test df158dcc81f1a43ce7eef361af03c48ec91f1e06
F ext/rtree/rtreeD.test 636630357638f5983701550b37f0f5867130d2ca
F ext/rtree/rtreeE.test 388c1c8602c3ce55c15f03b509e9cf545fb7c41f
F ext/rtree/rtreeE.test b268eed6a621613fbc0a44157a8d6fe10cff3436
F ext/rtree/rtreeF.test 66deb9fd1611c7ca2e374adba63debdc2dbb12b4
F ext/rtree/rtree_perf.tcl 6c18c1f23cd48e0f948930c98dfdd37dfccb5195
F ext/rtree/rtree_util.tcl 06aab2ed5b826545bf215fff90ecb9255a8647ea
F ext/rtree/sqlite3rtree.h 83349d519fe5f518b3ea025d18dd1fe51b1684bd
F ext/rtree/sqlite3rtree.h 9c5777af3d2921c7b4ae4954e8e5697502289d28
F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
F ext/userauth/sqlite3userauth.h 19cb6f0e31316d0ee4afdfb7a85ef9da3333a220
@ -252,9 +252,9 @@ F src/resolve.c 99eabf7eff0bfa65b75939b46caa82e2b2133f28
F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
F src/select.c 95e728e1bdbca2ee45016308e3eb6d19095155cc
F src/shell.c 07dda7cd692911d2f22269953418d049f2e2c0ee
F src/sqlite.h.in 0127e418883c2b41f7fbc056bc1033fa56fbd2a5
F src/sqlite.h.in f97f23de94767ab7e2c97745308920a749d4a4b5
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
F src/sqlite3ext.h 2ebeb634e751a61a6f0eebfa0f4669f46a42f6cd
F src/sqliteInt.h 88738d94a343000e7a5c0e295d111c4cfccb18b0
F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46
F src/status.c f266ad8a2892d659b74f0f50cb6a88b6e7c12179
@ -294,7 +294,7 @@ F src/test_osinst.c 5423dc1d355f594371f27dd292ca54bd320b8196
F src/test_pcache.c a5cd24730cb43c5b18629043314548c9169abb00
F src/test_quota.c 180813f43683be5725458fc1ff13ac455d8e722d
F src/test_quota.h 2a8ad1952d1d2ca9af0ce0465e56e6c023b5e15d
F src/test_rtree.c bfe6f4386517f70054311109f3528adffec34485
F src/test_rtree.c 43fff4c5a01576d6d213f27472598801a247890c
F src/test_schema.c 2bdba21b82f601da69793e1f1d11bf481a79b091
F src/test_server.c a2615049954cbb9cfb4a62e18e2f0616e4dc38fe
F src/test_sqllog.c b690c12933f50ff46491e0d56a251f84ae16e914
@ -314,11 +314,11 @@ F src/util.c a6431c92803b975b7322724a7b433e538d243539
F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701
F src/vdbe.c 6aee8a041742413ab3113e6682bc7ad1628a2bbe
F src/vdbe.h 7e538ecf47dccb307ea2d087c3ddc2dd8d70e79d
F src/vdbeInt.h de3291a6688dfef9f61d47705c1bd57008b1185d
F src/vdbeapi.c 583d56b129dd27f12bed518270de9ebe521e6a75
F src/vdbeInt.h f0ccddac48583d5f762dc554a9f79e85ea8807e0
F src/vdbeapi.c f9ad1be11b9e8f2f8580656191abeeefd24f71dd
F src/vdbeaux.c efe1667d31e8648dbe04a441e5aa9b62dbee2f03
F src/vdbeblob.c 4f2e8e075d238392df98c5e03a64342465b03f90
F src/vdbemem.c 7bfbeef0978a2e1a05d979641fdbf7c189b7ddf4
F src/vdbemem.c e87e076b863e4228d71d8c4d1c4df6836d810043
F src/vdbesort.c f5009e7a35e3065635d8918b9a31f498a499976b
F src/vdbetrace.c 8befe829faff6d9e6f6e4dee5a7d3f85cc85f1a0
F src/vtab.c c535e80259ebe616467181a83a4263555b97c694
@ -1278,8 +1278,10 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P c403502cdce8b82e570e6fc49ab7f5144800c189 0e45e8f1574ef19a43dbd118440ddbc5cec80ce7
R a7c7024ecc6e55965138342c00704c17
T +closed 0e45e8f1574ef19a43dbd118440ddbc5cec80ce7
P 6a0cf3ce9e68d0127f9653232e588ed59d34eca5
R bf385fc150043c4fd0764e8a32885eb2
T *branch * value-dup
T *sym-value-dup *
T -sym-trunk *
U drh
Z fd6f77c4d4a68df5c93e64187bb026e9
Z 7924e70131eeabe4b6a3d5fe0f7548fa

View File

@ -1 +1 @@
6a0cf3ce9e68d0127f9653232e588ed59d34eca5
a7ee40c4fc62843ac5b96ba47ca14a66e8cd6961

View File

@ -4294,7 +4294,7 @@ SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
#endif
/*
** CAPI3REF: Obtaining SQL Function Parameter Values
** CAPI3REF: Obtaining SQL Values
** METHOD: sqlite3_value
**
** The C-language implementation of SQL functions and aggregates uses
@ -4352,6 +4352,23 @@ const void *sqlite3_value_text16be(sqlite3_value*);
int sqlite3_value_type(sqlite3_value*);
int sqlite3_value_numeric_type(sqlite3_value*);
/*
** CAPI3REF: Copy And Free SQL Values
** METHOD: sqlite3_value
**
** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
** is a [protected sqlite3_value] object even if the input is not.
** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
** memory allocation fails.
**
** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
** previously obtained from [sqlite_value_dup()]. ^If V is a NULL pointer
** then sqlite3_value_free(V) is a harmless no-op.
*/
SQLITE_EXPERIMENTAL sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
SQLITE_EXPERIMENTAL void sqlite3_value_free(sqlite3_value*);
/*
** CAPI3REF: Obtain Aggregate Function Context
** METHOD: sqlite3_context
@ -5875,7 +5892,7 @@ int sqlite3_blob_open(
**
** ^This function sets the database handle error code and message.
*/
SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
/*
** CAPI3REF: Close A BLOB Handle
@ -7685,7 +7702,7 @@ int sqlite3_vtab_on_conflict(sqlite3 *);
**
** See also: [sqlite3_stmt_scanstatus_reset()]
*/
SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
int sqlite3_stmt_scanstatus(
sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
int idx, /* Index of loop to report on */
int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
@ -7701,7 +7718,7 @@ SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
** This API is only available if the library is built with pre-processor
** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
*/
SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
/*

View File

@ -267,6 +267,8 @@ struct sqlite3_api_routines {
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
void(*)(void*), unsigned char);
int (*strglob)(const char*,const char*);
sqlite3_value (*value_dup)(const sqlite3_value*);
void (*value_free)(sqlite3_value*);
};
/*
@ -497,6 +499,9 @@ struct sqlite3_api_routines {
#define sqlite3_result_blob64 sqlite3_api->result_blob64
#define sqlite3_result_text64 sqlite3_api->result_text64
#define sqlite3_strglob sqlite3_api->strglob
/* Version 3.8.11 and later */
#define sqlite3_value_dup sqlite3_api->value_dup
#define sqlite3_value_free sqlite3_api->value_free
#endif /* SQLITE_CORE */
#ifndef SQLITE_CORE

View File

@ -155,6 +155,11 @@ static int circle_geom(
/*
** Implementation of "circle" r-tree geometry callback using the
** 2nd-generation interface that allows scoring.
**
** Two calling forms:
**
** Qcircle(X,Y,Radius,eType) -- All values are doubles
** Qcircle('x:X y:Y r:R e:ETYPE') -- Single string parameter
*/
static int circle_query_func(sqlite3_rtree_query_info *p){
int i; /* Iterator variable */
@ -176,10 +181,9 @@ static int circle_query_func(sqlite3_rtree_query_info *p){
** Return an error if the table does not have exactly 2 dimensions. */
if( p->nCoord!=4 ) return SQLITE_ERROR;
/* Test that the correct number of parameters (4) have been supplied,
** and that the parameters are in range (that the radius of the circle
** radius is greater than zero). */
if( p->nParam!=4 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
/* Test that the correct number of parameters (1 or 4) have been supplied.
*/
if( p->nParam!=4 && p->nParam!=1 ) return SQLITE_ERROR;
/* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
** if the allocation fails. */
@ -191,10 +195,38 @@ static int circle_query_func(sqlite3_rtree_query_info *p){
** tested bounding boxes that intersect the circular region are detected
** is by testing if each corner of the bounding box lies within radius
** units of the center of the circle. */
pCircle->centerx = p->aParam[0];
pCircle->centery = p->aParam[1];
pCircle->radius = p->aParam[2];
pCircle->eScoreType = (int)p->aParam[3];
if( p->nParam==4 ){
pCircle->centerx = p->aParam[0];
pCircle->centery = p->aParam[1];
pCircle->radius = p->aParam[2];
pCircle->eScoreType = (int)p->aParam[3];
}else{
const char *z = (const char*)sqlite3_value_text(p->apSqlParam[0]);
pCircle->centerx = 0.0;
pCircle->centery = 0.0;
pCircle->radius = 0.0;
pCircle->eScoreType = 0;
while( z && z[0] ){
if( z[0]=='r' && z[1]==':' ){
pCircle->radius = atof(&z[2]);
}else if( z[0]=='x' && z[1]==':' ){
pCircle->centerx = atof(&z[2]);
}else if( z[0]=='y' && z[1]==':' ){
pCircle->centery = atof(&z[2]);
}else if( z[0]=='e' && z[1]==':' ){
pCircle->eScoreType = (int)atof(&z[2]);
}else if( z[0]==' ' ){
z++;
continue;
}
while( z[0]!=0 && z[0]!=' ' ) z++;
while( z[0]==' ' ) z++;
}
}
if( pCircle->radius<0.0 ){
sqlite3_free(pCircle);
return SQLITE_NOMEM;
}
/* Define two bounding box regions. The first, aBox[0], extends to
** infinity in the X dimension. It covers the same range of the Y dimension

View File

@ -186,6 +186,12 @@ struct Mem {
#endif
};
/*
** Size of struct Mem not including the Mem.zMalloc member or anything that
** follows.
*/
#define MEMCELLSIZE offsetof(Mem,zMalloc)
/* One or more of the following flags are set to indicate the validOK
** representations of the value stored in the Mem struct.
**

View File

@ -212,6 +212,34 @@ int sqlite3_value_type(sqlite3_value* pVal){
return aType[pVal->flags&MEM_AffMask];
}
/* Make a copy of an sqlite3_value object
*/
sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
sqlite3_value *pNew;
if( pOrig==0 ) return 0;
pNew = sqlite3_malloc( sizeof(*pNew) );
if( pNew==0 ) return 0;
memset(pNew, 0, sizeof(*pNew));
memcpy(pNew, pOrig, MEMCELLSIZE);
pNew->flags &= ~MEM_Dyn;
pNew->db = 0;
if( pNew->flags&(MEM_Str|MEM_Blob) ){
if( 0==(pOrig->flags&MEM_Static) ){
pNew->flags |= MEM_Ephem;
sqlite3VdbeMemMakeWriteable(pNew);
}
}
return pNew;
}
/* Destroy an sqlite3_value object previously obtained from
** sqlite3_value_dup().
*/
void sqlite3_value_free(sqlite3_value *pOld){
sqlite3ValueFree(pOld);
}
/**************************** sqlite3_result_ *******************************
** The following routines are used by user-defined functions to specify
** the function result.

View File

@ -770,10 +770,6 @@ void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
}
#endif /* SQLITE_DEBUG */
/*
** Size of struct Mem not including the Mem.zMalloc member.
*/
#define MEMCELLSIZE offsetof(Mem,zMalloc)
/*
** Make an shallow copy of pFrom into pTo. Prior contents of