mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-28 01:30:29 +08:00
Refactor jsonb_utils
Adjust jsonb utility functions to always take and return char * as string types to reduce conversion requirements on caller.
This commit is contained in:
parent
cde5af31b8
commit
82bf1c458f
@ -34,6 +34,7 @@ ts_jsonb_add_str(JsonbParseState *state, const char *key, const char *value)
|
|||||||
{
|
{
|
||||||
JsonbValue json_value;
|
JsonbValue json_value;
|
||||||
|
|
||||||
|
Assert(value != NULL);
|
||||||
/* If there is a null entry, don't add it to the JSON */
|
/* If there is a null entry, don't add it to the JSON */
|
||||||
if (value == NULL)
|
if (value == NULL)
|
||||||
return;
|
return;
|
||||||
@ -91,8 +92,8 @@ ts_jsonb_add_pair(JsonbParseState *state, JsonbValue *key, JsonbValue *value)
|
|||||||
pushJsonbValue(&state, WJB_VALUE, value);
|
pushJsonbValue(&state, WJB_VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
text *
|
char *
|
||||||
ts_jsonb_get_text_field(Jsonb *json, text *field_name)
|
ts_jsonb_get_str_field(Jsonb *jsonb, const char *key)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* `jsonb_object_field_text` returns NULL when the field is not found so
|
* `jsonb_object_field_text` returns NULL when the field is not found so
|
||||||
@ -103,33 +104,22 @@ ts_jsonb_get_text_field(Jsonb *json, text *field_name)
|
|||||||
|
|
||||||
InitFunctionCallInfoData(*fcinfo, NULL, 2, InvalidOid, NULL, NULL);
|
InitFunctionCallInfoData(*fcinfo, NULL, 2, InvalidOid, NULL, NULL);
|
||||||
|
|
||||||
FC_SET_ARG(fcinfo, 0, PointerGetDatum(json));
|
FC_SET_ARG(fcinfo, 0, PointerGetDatum(jsonb));
|
||||||
FC_SET_ARG(fcinfo, 1, PointerGetDatum(field_name));
|
FC_SET_ARG(fcinfo, 1, PointerGetDatum(cstring_to_text(key)));
|
||||||
|
|
||||||
result = jsonb_object_field_text(fcinfo);
|
result = jsonb_object_field_text(fcinfo);
|
||||||
|
|
||||||
if (fcinfo->isnull)
|
if (fcinfo->isnull)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return DatumGetTextP(result);
|
return text_to_cstring(DatumGetTextP(result));
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
ts_jsonb_get_str_field(Jsonb *license, text *field_name)
|
|
||||||
{
|
|
||||||
text *text_str = ts_jsonb_get_text_field(license, field_name);
|
|
||||||
|
|
||||||
if (text_str == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return text_to_cstring(text_str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TimestampTz
|
TimestampTz
|
||||||
ts_jsonb_get_time_field(Jsonb *license, text *field_name, bool *field_found)
|
ts_jsonb_get_time_field(Jsonb *jsonb, const char *key, bool *field_found)
|
||||||
{
|
{
|
||||||
Datum time_datum;
|
Datum time_datum;
|
||||||
text *time_str = ts_jsonb_get_text_field(license, field_name);
|
char *time_str = ts_jsonb_get_str_field(jsonb, key);
|
||||||
|
|
||||||
if (time_str == NULL)
|
if (time_str == NULL)
|
||||||
{
|
{
|
||||||
@ -138,7 +128,7 @@ ts_jsonb_get_time_field(Jsonb *license, text *field_name, bool *field_found)
|
|||||||
}
|
}
|
||||||
|
|
||||||
time_datum = DirectFunctionCall3(timestamptz_in,
|
time_datum = DirectFunctionCall3(timestamptz_in,
|
||||||
/* str= */ CStringGetDatum(text_to_cstring(time_str)),
|
/* str= */ CStringGetDatum(time_str),
|
||||||
/* unused */ Int32GetDatum(-1),
|
/* unused */ Int32GetDatum(-1),
|
||||||
/* typmod= */ Int32GetDatum(-1));
|
/* typmod= */ Int32GetDatum(-1));
|
||||||
|
|
||||||
@ -147,10 +137,10 @@ ts_jsonb_get_time_field(Jsonb *license, text *field_name, bool *field_found)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int32
|
int32
|
||||||
ts_jsonb_get_int32_field(Jsonb *json, text *field_name, bool *field_found)
|
ts_jsonb_get_int32_field(Jsonb *json, const char *key, bool *field_found)
|
||||||
{
|
{
|
||||||
Datum int_datum;
|
Datum int_datum;
|
||||||
char *int_str = ts_jsonb_get_str_field(json, field_name);
|
char *int_str = ts_jsonb_get_str_field(json, key);
|
||||||
|
|
||||||
if (int_str == NULL)
|
if (int_str == NULL)
|
||||||
{
|
{
|
||||||
|
@ -22,10 +22,9 @@ extern TSDLLEXPORT void ts_jsonb_add_numeric(JsonbParseState *state, const char
|
|||||||
|
|
||||||
extern void ts_jsonb_add_value(JsonbParseState *state, const char *key, JsonbValue *value);
|
extern void ts_jsonb_add_value(JsonbParseState *state, const char *key, JsonbValue *value);
|
||||||
|
|
||||||
extern TSDLLEXPORT text *ts_jsonb_get_text_field(Jsonb *json, text *field_name);
|
extern TSDLLEXPORT char *ts_jsonb_get_str_field(Jsonb *jsonb, const char *key);
|
||||||
extern TSDLLEXPORT char *ts_jsonb_get_str_field(Jsonb *license, text *field_name);
|
extern TSDLLEXPORT TimestampTz ts_jsonb_get_time_field(Jsonb *jsonb, const char *key,
|
||||||
extern TSDLLEXPORT TimestampTz ts_jsonb_get_time_field(Jsonb *license, text *field_name,
|
|
||||||
bool *field_found);
|
bool *field_found);
|
||||||
extern TSDLLEXPORT int32 ts_jsonb_get_int32_field(Jsonb *json, text *field_name, bool *field_found);
|
extern TSDLLEXPORT int32 ts_jsonb_get_int32_field(Jsonb *json, const char *key, bool *field_found);
|
||||||
|
|
||||||
#endif /* TIMESCALEDB_JSONB_UTILS_H */
|
#endif /* TIMESCALEDB_JSONB_UTILS_H */
|
||||||
|
@ -264,21 +264,20 @@ license_info_init_from_jsonb(Jsonb *json_license, LicenseInfo *out)
|
|||||||
static char *
|
static char *
|
||||||
json_get_id(Jsonb *license)
|
json_get_id(Jsonb *license)
|
||||||
{
|
{
|
||||||
return ts_jsonb_get_str_field(license, cstring_to_text(ID_FIELD));
|
return ts_jsonb_get_str_field(license, ID_FIELD);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
json_get_kind(Jsonb *license)
|
json_get_kind(Jsonb *license)
|
||||||
{
|
{
|
||||||
return ts_jsonb_get_str_field(license, cstring_to_text(KIND_FIELD));
|
return ts_jsonb_get_str_field(license, KIND_FIELD);
|
||||||
}
|
}
|
||||||
|
|
||||||
static TimestampTz
|
static TimestampTz
|
||||||
json_get_start_time(Jsonb *license)
|
json_get_start_time(Jsonb *license)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
TimestampTz start_time =
|
TimestampTz start_time = ts_jsonb_get_time_field(license, START_TIME_FIELD, &found);
|
||||||
ts_jsonb_get_time_field(license, cstring_to_text(START_TIME_FIELD), &found);
|
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
elog(ERRCODE_FEATURE_NOT_SUPPORTED, FIELD_NOT_FOUND_ERRSTRING, START_TIME_FIELD);
|
elog(ERRCODE_FEATURE_NOT_SUPPORTED, FIELD_NOT_FOUND_ERRSTRING, START_TIME_FIELD);
|
||||||
@ -289,8 +288,7 @@ static TimestampTz
|
|||||||
json_get_end_time(Jsonb *license)
|
json_get_end_time(Jsonb *license)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
TimestampTz end_time =
|
TimestampTz end_time = ts_jsonb_get_time_field(license, END_TIME_FIELD, &found);
|
||||||
ts_jsonb_get_time_field(license, cstring_to_text(END_TIME_FIELD), &found);
|
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
elog(ERRCODE_FEATURE_NOT_SUPPORTED, FIELD_NOT_FOUND_ERRSTRING, END_TIME_FIELD);
|
elog(ERRCODE_FEATURE_NOT_SUPPORTED, FIELD_NOT_FOUND_ERRSTRING, END_TIME_FIELD);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user