mirror of
https://github.com/postgres/postgres.git
synced 2025-11-24 00:23:06 +03:00
Add more SQL/JSON constructor functions
This Patch introduces three SQL standard JSON functions: JSON() JSON_SCALAR() JSON_SERIALIZE() JSON() produces json values from text, bytea, json or jsonb values, and has facilitites for handling duplicate keys. JSON_SCALAR() produces a json value from any scalar sql value, including json and jsonb. JSON_SERIALIZE() produces text or bytea from input which containis or represents json or jsonb; For the most part these functions don't add any significant new capabilities, but they will be of use to users wanting standard compliant JSON handling. Catversion bumped as this changes ruleutils.c. Author: Nikita Glukhov <n.gluhov@postgrespro.ru> Author: Teodor Sigaev <teodor@sigaev.ru> Author: Oleg Bartunov <obartunov@gmail.com> Author: Alexander Korotkov <aekorotkov@gmail.com> Author: Andrew Dunstan <andrew@dunslane.net> Author: Amit Langote <amitlangote09@gmail.com> Reviewers have included (in no particular order) Andres Freund, Alexander Korotkov, Pavel Stehule, Andrew Alsup, Erik Rijkers, Zihong Yu, Himanshu Upadhyaya, Daniel Gustafsson, Justin Pryzby, Álvaro Herrera, Peter Eisentraut Discussion: https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886deac4b@postgrespro.ru Discussion: https://postgr.es/m/20220616233130.rparivafipt6doj3@alap3.anarazel.de Discussion: https://postgr.es/m/abd9b83b-aa66-f230-3d6d-734817f0995d%40postgresql.org Discussion: https://postgr.es/m/CA+HiwqE4XTdfb1nW=Ojoy_tQSRhYt-q_kb6i5d4xcKyrLC1Nbg@mail.gmail.com
This commit is contained in:
@@ -294,6 +294,10 @@ format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
|
||||
else
|
||||
buf = pstrdup("character varying");
|
||||
break;
|
||||
|
||||
case JSONOID:
|
||||
buf = pstrdup("json");
|
||||
break;
|
||||
}
|
||||
|
||||
if (buf == NULL)
|
||||
|
||||
@@ -33,6 +33,7 @@ typedef struct JsonbInState
|
||||
{
|
||||
JsonbParseState *parseState;
|
||||
JsonbValue *res;
|
||||
bool unique_keys;
|
||||
Node *escontext;
|
||||
} JsonbInState;
|
||||
|
||||
@@ -45,7 +46,8 @@ typedef struct JsonbAggState
|
||||
Oid val_output_func;
|
||||
} JsonbAggState;
|
||||
|
||||
static inline Datum jsonb_from_cstring(char *json, int len, Node *escontext);
|
||||
static inline Datum jsonb_from_cstring(char *json, int len, bool unique_keys,
|
||||
Node *escontext);
|
||||
static bool checkStringLen(size_t len, Node *escontext);
|
||||
static JsonParseErrorType jsonb_in_object_start(void *pstate);
|
||||
static JsonParseErrorType jsonb_in_object_end(void *pstate);
|
||||
@@ -76,7 +78,7 @@ jsonb_in(PG_FUNCTION_ARGS)
|
||||
{
|
||||
char *json = PG_GETARG_CSTRING(0);
|
||||
|
||||
return jsonb_from_cstring(json, strlen(json), fcinfo->context);
|
||||
return jsonb_from_cstring(json, strlen(json), false, fcinfo->context);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -100,7 +102,7 @@ jsonb_recv(PG_FUNCTION_ARGS)
|
||||
else
|
||||
elog(ERROR, "unsupported jsonb version number %d", version);
|
||||
|
||||
return jsonb_from_cstring(str, nbytes, NULL);
|
||||
return jsonb_from_cstring(str, nbytes, false, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -147,10 +149,11 @@ jsonb_send(PG_FUNCTION_ARGS)
|
||||
* Turns json text string into a jsonb Datum.
|
||||
*/
|
||||
Datum
|
||||
jsonb_from_text(text *js)
|
||||
jsonb_from_text(text *js, bool unique_keys)
|
||||
{
|
||||
return jsonb_from_cstring(VARDATA_ANY(js),
|
||||
VARSIZE_ANY_EXHDR(js),
|
||||
unique_keys,
|
||||
NULL);
|
||||
}
|
||||
|
||||
@@ -247,7 +250,7 @@ jsonb_typeof(PG_FUNCTION_ARGS)
|
||||
* instead of being thrown.
|
||||
*/
|
||||
static inline Datum
|
||||
jsonb_from_cstring(char *json, int len, Node *escontext)
|
||||
jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
|
||||
{
|
||||
JsonLexContext *lex;
|
||||
JsonbInState state;
|
||||
@@ -257,6 +260,7 @@ jsonb_from_cstring(char *json, int len, Node *escontext)
|
||||
memset(&sem, 0, sizeof(sem));
|
||||
lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
|
||||
|
||||
state.unique_keys = unique_keys;
|
||||
state.escontext = escontext;
|
||||
sem.semstate = (void *) &state;
|
||||
|
||||
@@ -293,6 +297,7 @@ jsonb_in_object_start(void *pstate)
|
||||
JsonbInState *_state = (JsonbInState *) pstate;
|
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_BEGIN_OBJECT, NULL);
|
||||
_state->parseState->unique_keys = _state->unique_keys;
|
||||
|
||||
return JSON_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -10832,6 +10832,15 @@ get_json_constructor(JsonConstructorExpr *ctor, deparse_context *context,
|
||||
case JSCTOR_JSON_ARRAY:
|
||||
funcname = "JSON_ARRAY";
|
||||
break;
|
||||
case JSCTOR_JSON_PARSE:
|
||||
funcname = "JSON";
|
||||
break;
|
||||
case JSCTOR_JSON_SCALAR:
|
||||
funcname = "JSON_SCALAR";
|
||||
break;
|
||||
case JSCTOR_JSON_SERIALIZE:
|
||||
funcname = "JSON_SERIALIZE";
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "invalid JsonConstructorType %d", ctor->type);
|
||||
}
|
||||
@@ -10879,7 +10888,12 @@ get_json_constructor_options(JsonConstructorExpr *ctor, StringInfo buf)
|
||||
if (ctor->unique)
|
||||
appendStringInfoString(buf, " WITH UNIQUE KEYS");
|
||||
|
||||
get_json_returning(ctor->returning, buf, true);
|
||||
/*
|
||||
* Append RETURNING clause if needed; JSON() and JSON_SCALAR() don't
|
||||
* support one.
|
||||
*/
|
||||
if (ctor->type != JSCTOR_JSON_PARSE && ctor->type != JSCTOR_JSON_SCALAR)
|
||||
get_json_returning(ctor->returning, buf, true);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user