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

Convert json_type() to use JSONB internally.

FossilOrigin-Name: 83074835b900ce85cf67059e674ce959801505c37592671af25ca0af7ed483f1
This commit is contained in:
drh
2023-11-30 19:29:56 +00:00
parent 0ab1e25468
commit 276042bd23
3 changed files with 40 additions and 52 deletions

View File

@@ -1887,42 +1887,6 @@ static char *jsonPathSyntaxError(const char *zErr, sqlite3_context *ctx){
return 0;
}
/*
** Do a node lookup using zPath. Return a pointer to the node on success.
** Return NULL if not found or if there is an error.
**
** On an error, write an error message into pCtx and increment the
** pParse->nErr counter.
**
** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
** nodes are appended.
*/
static JsonNode *jsonLookup(
JsonParse *pParse, /* The JSON to search */
const char *zPath, /* The path to search */
int *pApnd, /* Append nodes to complete path if not NULL */
sqlite3_context *pCtx /* Report errors here, if not NULL */
){
const char *zErr = 0;
JsonNode *pNode = 0;
if( zPath==0 ) return 0;
if( zPath[0]!='$' ){
zErr = zPath;
goto lookup_err;
}
zPath++;
pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
if( zErr==0 ) return pNode;
lookup_err:
pParse->nErr++;
assert( zErr!=0 && pCtx!=0 );
jsonPathSyntaxError(zErr, pCtx);
return 0;
}
/*
** Report the wrong number of arguments for json_insert(), json_replace()
** or json_set().
@@ -4666,20 +4630,45 @@ static void jsonTypeFunc(
sqlite3_value **argv
){
JsonParse *p; /* The parse */
const char *zPath;
JsonNode *pNode;
const char *zPath = 0;
u32 i;
p = jsonParseCached(ctx, argv[0], ctx, 0);
p = jsonParseFuncArg(ctx, argv[0], 0);
if( p==0 ) return;
if( argc==2 ){
if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
goto json_type_done;
}
if( sqlite3_value_bytes(argv[1])==0 ){
jsonBadPathError(ctx, "");
goto json_type_done;
}
zPath = (const char*)sqlite3_value_text(argv[1]);
pNode = jsonLookup(p, zPath, 0, ctx);
if( zPath==0 ){
sqlite3_result_error_nomem(ctx);
goto json_type_done;
}
if( zPath[0]!='$' ){
jsonBadPathError(ctx, zPath);
goto json_type_done;
}
i = jsonLookupBlobStep(p, 0, zPath+1, 0);
if( JSON_BLOB_ISERROR(i) ){
if( i==JSON_BLOB_NOTFOUND ){
/* no-op */
}else if( i==JSON_BLOB_PATHERROR ){
jsonBadPathError(ctx, zPath);
}else{
sqlite3_result_error(ctx, "malformed JSON", -1);
}
goto json_type_done;
}
}else{
pNode = p->aNode;
}
if( pNode ){
sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
i = 0;
}
sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC);
json_type_done:
jsonParseFree(p);
}
/*