1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-09 14:21:03 +03:00

Switch to using jsonParseCached() for json_patch().

FossilOrigin-Name: 2ed9c59e6a51037e63485d0a92dae25443116beddfca1ac73d2f5d5be38d4ad3
This commit is contained in:
drh
2023-07-27 23:51:36 +00:00
parent 6bc4baf313
commit caf7e26c4f
3 changed files with 41 additions and 48 deletions

View File

@@ -1751,26 +1751,15 @@ json_parse_restart:
** are any errors. If an error occurs, free all memory held by pParse,
** but not pParse itself.
**
** pParse is uninitialized when this routine is called.
**
** pParse->nJPRef set to 1. The caller becomes the owner of the
** the JsonParse object.
**
** pParse->bOwnsJson is set to bTakeJson. If bTakeJson is 1, the newly
** initialized JsonParse object will become the owner of the zJson input
** string. If bTakeJson is 0, then the caller is responsible for
** preserving zJson for the lifetime of the JsonParse object.
** pParse must be initialized to an empty parse object prior to calling
** this routine.
*/
static int jsonParse(
JsonParse *pParse, /* Initialize and fill this JsonParse object */
sqlite3_context *pCtx, /* Report errors here */
char *zJson /* Input JSON text to be parsed */
sqlite3_context *pCtx /* Report errors here */
){
int i;
memset(pParse, 0, sizeof(*pParse));
if( zJson==0 ) return 1;
pParse->zJson = zJson;
pParse->nJPRef = 1;
const char *zJson = pParse->zJson;
i = jsonParseValue(pParse, 0);
if( pParse->oom ) i = -1;
if( i>0 ){
@@ -1891,7 +1880,6 @@ static JsonParse *jsonParseCached(
u32 iMinHold = 0xffffffff;
u32 iMaxHold = 0;
int bJsonRCStr;
int rc;
if( zJson==0 ) return 0;
for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
@@ -1947,13 +1935,13 @@ static JsonParse *jsonParseCached(
memset(p, 0, sizeof(*p));
if( bJsonRCStr ){
p->zJson = sqlite3RCStrRef(zJson);
p->bJsonIsRCStr = 1;
}else{
p->zJson = (char*)&p[1];
memcpy(p->zJson, zJson, nJson+1);
}
rc = jsonParse(p, pErrCtx, p->zJson);
p->bJsonIsRCStr = bJsonRCStr;
if( rc ){
p->nJPRef = 1;
if( jsonParse(p, pErrCtx) ){
if( pErrCtx==0 ){
p->nErr = 1;
assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */
@@ -2677,29 +2665,27 @@ static void jsonPatchFunc(
int argc,
sqlite3_value **argv
){
JsonParse x; /* The JSON that is being patched */
JsonParse y; /* The patch */
JsonParse *pX; /* The JSON that is being patched */
JsonParse *pY; /* The patch */
JsonNode *pResult; /* The result of the merge */
UNUSED_PARAMETER(argc);
if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0])) ) return;
if( jsonParse(&y, ctx, (char*)sqlite3_value_text(argv[1])) ){
jsonParseReset(&x);
return;
}
x.useMod = 1;
y.useMod = 1;
pResult = jsonMergePatch(&x, 0, y.aNode);
assert( pResult!=0 || x.oom );
if( pResult && x.oom==0 ){
jsonDebugPrintParse(&x);
pX = jsonParseCached(ctx, argv[0], ctx, 1);
if( pX==0 ) return;
pY = jsonParseCached(ctx, argv[1], ctx, 1);
if( pY==0 ) return;
pX->useMod = 1;
pX->hasMod = 1;
pY->useMod = 1;
pResult = jsonMergePatch(pX, 0, pY->aNode);
assert( pResult!=0 || pX->oom );
if( pResult && pX->oom==0 ){
jsonDebugPrintParse(pX);
jsonDebugPrintNode(pResult);
jsonReturnJson(&x, pResult, ctx, 0);
jsonReturnJson(pX, pResult, ctx, 0);
}else{
sqlite3_result_error_nomem(ctx);
}
jsonParseReset(&x);
jsonParseReset(&y);
}
@@ -3326,7 +3312,6 @@ static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
/* Reset a JsonEachCursor back to its original state. Free any memory
** held. */
static void jsonEachCursorReset(JsonEachCursor *p){
sqlite3_free(p->zJson);
sqlite3_free(p->zRoot);
jsonParseReset(&p->sParse);
p->iRowid = 0;
@@ -3646,11 +3631,19 @@ static int jsonEachFilter(
if( idxNum==0 ) return SQLITE_OK;
z = (const char*)sqlite3_value_text(argv[0]);
if( z==0 ) return SQLITE_OK;
n = sqlite3_value_bytes(argv[0]);
p->zJson = sqlite3_malloc64( n+1 );
if( p->zJson==0 ) return SQLITE_NOMEM;
memcpy(p->zJson, z, (size_t)n+1);
if( jsonParse(&p->sParse, 0, p->zJson) ){
memset(&p->sParse, 0, sizeof(p->sParse));
p->sParse.nJPRef = 1;
if( sqlite3ValueIsOfClass(argv[0], (void(*)(void*))sqlite3RCStrUnref) ){
p->sParse.zJson = sqlite3RCStrRef((char*)z);
}else{
n = sqlite3_value_bytes(argv[0]);
p->sParse.zJson = sqlite3RCStrNew( n+1 );
if( p->sParse.zJson==0 ) return SQLITE_NOMEM;
memcpy(p->sParse.zJson, z, (size_t)n+1);
}
p->sParse.bJsonIsRCStr = 1;
p->zJson = p->sParse.zJson;
if( jsonParse(&p->sParse, 0) ){
int rc = SQLITE_NOMEM;
if( p->sParse.oom==0 ){
sqlite3_free(cur->pVtab->zErrMsg);