Commit ff41a5de authored by Peter Eisentraut's avatar Peter Eisentraut

Clean up new JSON API typedefs

The new JSON API uses a bit of an unusual typedef scheme, where for
example OkeysState is a pointer to okeysState.  And that's not applied
consistently either.  Change that to the more usual PostgreSQL style
where struct typedefs are upper case, and use pointers explicitly.
parent 6737aa72
...@@ -51,11 +51,11 @@ typedef enum /* contexts of JSON parser */ ...@@ -51,11 +51,11 @@ typedef enum /* contexts of JSON parser */
static inline void json_lex(JsonLexContext *lex); static inline void json_lex(JsonLexContext *lex);
static inline void json_lex_string(JsonLexContext *lex); static inline void json_lex_string(JsonLexContext *lex);
static inline void json_lex_number(JsonLexContext *lex, char *s); static inline void json_lex_number(JsonLexContext *lex, char *s);
static inline void parse_scalar(JsonLexContext *lex, JsonSemAction sem); static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
static void parse_object_field(JsonLexContext *lex, JsonSemAction sem); static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
static void parse_object(JsonLexContext *lex, JsonSemAction sem); static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
static void parse_array_element(JsonLexContext *lex, JsonSemAction sem); static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
static void parse_array(JsonLexContext *lex, JsonSemAction sem); static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex); static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
static void report_invalid_token(JsonLexContext *lex); static void report_invalid_token(JsonLexContext *lex);
static int report_json_context(JsonLexContext *lex); static int report_json_context(JsonLexContext *lex);
...@@ -70,12 +70,11 @@ static void array_to_json_internal(Datum array, StringInfo result, ...@@ -70,12 +70,11 @@ static void array_to_json_internal(Datum array, StringInfo result,
bool use_line_feeds); bool use_line_feeds);
/* the null action object used for pure validation */ /* the null action object used for pure validation */
static jsonSemAction nullSemAction = static JsonSemAction nullSemAction =
{ {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL NULL, NULL, NULL, NULL, NULL
}; };
static JsonSemAction NullSemAction = &nullSemAction;
/* Recursive Descent parser support routines */ /* Recursive Descent parser support routines */
...@@ -170,7 +169,7 @@ json_in(PG_FUNCTION_ARGS) ...@@ -170,7 +169,7 @@ json_in(PG_FUNCTION_ARGS)
/* validate it */ /* validate it */
lex = makeJsonLexContext(result, false); lex = makeJsonLexContext(result, false);
pg_parse_json(lex, NullSemAction); pg_parse_json(lex, &nullSemAction);
/* Internal representation is the same as text, for now */ /* Internal representation is the same as text, for now */
PG_RETURN_TEXT_P(result); PG_RETURN_TEXT_P(result);
...@@ -222,7 +221,7 @@ json_recv(PG_FUNCTION_ARGS) ...@@ -222,7 +221,7 @@ json_recv(PG_FUNCTION_ARGS)
/* Validate it. */ /* Validate it. */
lex = makeJsonLexContext(result, false); lex = makeJsonLexContext(result, false);
pg_parse_json(lex, NullSemAction); pg_parse_json(lex, &nullSemAction);
PG_RETURN_TEXT_P(result); PG_RETURN_TEXT_P(result);
} }
...@@ -260,7 +259,7 @@ makeJsonLexContext(text *json, bool need_escapes) ...@@ -260,7 +259,7 @@ makeJsonLexContext(text *json, bool need_escapes)
* pointer to a state object to be passed to those routines. * pointer to a state object to be passed to those routines.
*/ */
void void
pg_parse_json(JsonLexContext *lex, JsonSemAction sem) pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
{ {
JsonTokenType tok; JsonTokenType tok;
...@@ -296,7 +295,7 @@ pg_parse_json(JsonLexContext *lex, JsonSemAction sem) ...@@ -296,7 +295,7 @@ pg_parse_json(JsonLexContext *lex, JsonSemAction sem)
* - object field * - object field
*/ */
static inline void static inline void
parse_scalar(JsonLexContext *lex, JsonSemAction sem) parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
{ {
char *val = NULL; char *val = NULL;
json_scalar_action sfunc = sem->scalar; json_scalar_action sfunc = sem->scalar;
...@@ -332,7 +331,7 @@ parse_scalar(JsonLexContext *lex, JsonSemAction sem) ...@@ -332,7 +331,7 @@ parse_scalar(JsonLexContext *lex, JsonSemAction sem)
} }
static void static void
parse_object_field(JsonLexContext *lex, JsonSemAction sem) parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
{ {
/* /*
* an object field is "fieldname" : value where value can be a scalar, * an object field is "fieldname" : value where value can be a scalar,
...@@ -380,7 +379,7 @@ parse_object_field(JsonLexContext *lex, JsonSemAction sem) ...@@ -380,7 +379,7 @@ parse_object_field(JsonLexContext *lex, JsonSemAction sem)
} }
static void static void
parse_object(JsonLexContext *lex, JsonSemAction sem) parse_object(JsonLexContext *lex, JsonSemAction *sem)
{ {
/* /*
* an object is a possibly empty sequence of object fields, separated by * an object is a possibly empty sequence of object fields, separated by
...@@ -428,7 +427,7 @@ parse_object(JsonLexContext *lex, JsonSemAction sem) ...@@ -428,7 +427,7 @@ parse_object(JsonLexContext *lex, JsonSemAction sem)
} }
static void static void
parse_array_element(JsonLexContext *lex, JsonSemAction sem) parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
{ {
json_aelem_action astart = sem->array_element_start; json_aelem_action astart = sem->array_element_start;
json_aelem_action aend = sem->array_element_end; json_aelem_action aend = sem->array_element_end;
...@@ -459,7 +458,7 @@ parse_array_element(JsonLexContext *lex, JsonSemAction sem) ...@@ -459,7 +458,7 @@ parse_array_element(JsonLexContext *lex, JsonSemAction sem)
} }
static void static void
parse_array(JsonLexContext *lex, JsonSemAction sem) parse_array(JsonLexContext *lex, JsonSemAction *sem)
{ {
/* /*
* an array is a possibly empty sequence of array elements, separated by * an array is a possibly empty sequence of array elements, separated by
......
...@@ -99,17 +99,17 @@ typedef enum ...@@ -99,17 +99,17 @@ typedef enum
} JsonSearch; } JsonSearch;
/* state for json_object_keys */ /* state for json_object_keys */
typedef struct okeysState typedef struct OkeysState
{ {
JsonLexContext *lex; JsonLexContext *lex;
char **result; char **result;
int result_size; int result_size;
int result_count; int result_count;
int sent_count; int sent_count;
} okeysState, *OkeysState; } OkeysState;
/* state for json_get* functions */ /* state for json_get* functions */
typedef struct getState typedef struct GetState
{ {
JsonLexContext *lex; JsonLexContext *lex;
JsonSearch search_type; JsonSearch search_type;
...@@ -127,17 +127,17 @@ typedef struct getState ...@@ -127,17 +127,17 @@ typedef struct getState
bool *pathok; bool *pathok;
int *array_level_index; int *array_level_index;
int *path_level_index; int *path_level_index;
} getState, *GetState; } GetState;
/* state for json_array_length */ /* state for json_array_length */
typedef struct alenState typedef struct AlenState
{ {
JsonLexContext *lex; JsonLexContext *lex;
int count; int count;
} alenState, *AlenState; } AlenState;
/* state for json_each */ /* state for json_each */
typedef struct eachState typedef struct EachState
{ {
JsonLexContext *lex; JsonLexContext *lex;
Tuplestorestate *tuple_store; Tuplestorestate *tuple_store;
...@@ -147,20 +147,20 @@ typedef struct eachState ...@@ -147,20 +147,20 @@ typedef struct eachState
bool normalize_results; bool normalize_results;
bool next_scalar; bool next_scalar;
char *normalized_scalar; char *normalized_scalar;
} eachState, *EachState; } EachState;
/* state for json_array_elements */ /* state for json_array_elements */
typedef struct elementsState typedef struct ElementsState
{ {
JsonLexContext *lex; JsonLexContext *lex;
Tuplestorestate *tuple_store; Tuplestorestate *tuple_store;
TupleDesc ret_tdesc; TupleDesc ret_tdesc;
MemoryContext tmp_cxt; MemoryContext tmp_cxt;
char *result_start; char *result_start;
} elementsState, *ElementsState; } ElementsState;
/* state for get_json_object_as_hash */ /* state for get_json_object_as_hash */
typedef struct jhashState typedef struct JhashState
{ {
JsonLexContext *lex; JsonLexContext *lex;
HTAB *hash; HTAB *hash;
...@@ -168,16 +168,16 @@ typedef struct jhashState ...@@ -168,16 +168,16 @@ typedef struct jhashState
char *save_json_start; char *save_json_start;
bool use_json_as_text; bool use_json_as_text;
char *function_name; char *function_name;
} jhashState, *JHashState; } JHashState;
/* used to build the hashtable */ /* used to build the hashtable */
typedef struct jsonHashEntry typedef struct JsonHashEntry
{ {
char fname[NAMEDATALEN]; char fname[NAMEDATALEN];
char *val; char *val;
char *json; char *json;
bool isnull; bool isnull;
} jsonHashEntry, *JsonHashEntry; } JsonHashEntry;
/* these two are stolen from hstore / record_out, used in populate_record* */ /* these two are stolen from hstore / record_out, used in populate_record* */
typedef struct ColumnIOData typedef struct ColumnIOData
...@@ -197,7 +197,7 @@ typedef struct RecordIOData ...@@ -197,7 +197,7 @@ typedef struct RecordIOData
} RecordIOData; } RecordIOData;
/* state for populate_recordset */ /* state for populate_recordset */
typedef struct populateRecordsetState typedef struct PopulateRecordsetState
{ {
JsonLexContext *lex; JsonLexContext *lex;
HTAB *json_hash; HTAB *json_hash;
...@@ -209,7 +209,7 @@ typedef struct populateRecordsetState ...@@ -209,7 +209,7 @@ typedef struct populateRecordsetState
HeapTupleHeader rec; HeapTupleHeader rec;
RecordIOData *my_extra; RecordIOData *my_extra;
MemoryContext fn_mcxt; /* used to stash IO funcs */ MemoryContext fn_mcxt; /* used to stash IO funcs */
} populateRecordsetState, *PopulateRecordsetState; } PopulateRecordsetState;
/* /*
* SQL function json_object-keys * SQL function json_object-keys
...@@ -229,22 +229,22 @@ Datum ...@@ -229,22 +229,22 @@ Datum
json_object_keys(PG_FUNCTION_ARGS) json_object_keys(PG_FUNCTION_ARGS)
{ {
FuncCallContext *funcctx; FuncCallContext *funcctx;
OkeysState state; OkeysState *state;
int i; int i;
if (SRF_IS_FIRSTCALL()) if (SRF_IS_FIRSTCALL())
{ {
text *json = PG_GETARG_TEXT_P(0); text *json = PG_GETARG_TEXT_P(0);
JsonLexContext *lex = makeJsonLexContext(json, true); JsonLexContext *lex = makeJsonLexContext(json, true);
JsonSemAction sem; JsonSemAction *sem;
MemoryContext oldcontext; MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT(); funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
state = palloc(sizeof(okeysState)); state = palloc(sizeof(OkeysState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
state->lex = lex; state->lex = lex;
state->result_size = 256; state->result_size = 256;
...@@ -272,7 +272,7 @@ json_object_keys(PG_FUNCTION_ARGS) ...@@ -272,7 +272,7 @@ json_object_keys(PG_FUNCTION_ARGS)
} }
funcctx = SRF_PERCALL_SETUP(); funcctx = SRF_PERCALL_SETUP();
state = (OkeysState) funcctx->user_fctx; state = (OkeysState *) funcctx->user_fctx;
if (state->sent_count < state->result_count) if (state->sent_count < state->result_count)
{ {
...@@ -293,7 +293,7 @@ json_object_keys(PG_FUNCTION_ARGS) ...@@ -293,7 +293,7 @@ json_object_keys(PG_FUNCTION_ARGS)
static void static void
okeys_object_field_start(void *state, char *fname, bool isnull) okeys_object_field_start(void *state, char *fname, bool isnull)
{ {
OkeysState _state = (OkeysState) state; OkeysState *_state = (OkeysState *) state;
/* only collecting keys for the top level object */ /* only collecting keys for the top level object */
if (_state->lex->lex_level != 1) if (_state->lex->lex_level != 1)
...@@ -314,7 +314,7 @@ okeys_object_field_start(void *state, char *fname, bool isnull) ...@@ -314,7 +314,7 @@ okeys_object_field_start(void *state, char *fname, bool isnull)
static void static void
okeys_array_start(void *state) okeys_array_start(void *state)
{ {
OkeysState _state = (OkeysState) state; OkeysState *_state = (OkeysState *) state;
/* top level must be a json object */ /* top level must be a json object */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -326,7 +326,7 @@ okeys_array_start(void *state) ...@@ -326,7 +326,7 @@ okeys_array_start(void *state)
static void static void
okeys_scalar(void *state, char *token, JsonTokenType tokentype) okeys_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
OkeysState _state = (OkeysState) state; OkeysState *_state = (OkeysState *) state;
/* top level must be a json object */ /* top level must be a json object */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -491,16 +491,16 @@ get_worker(text *json, ...@@ -491,16 +491,16 @@ get_worker(text *json,
int npath, int npath,
bool normalize_results) bool normalize_results)
{ {
GetState state; GetState *state;
JsonLexContext *lex = makeJsonLexContext(json, true); JsonLexContext *lex = makeJsonLexContext(json, true);
JsonSemAction sem; JsonSemAction *sem;
/* only allowed to use one of these */ /* only allowed to use one of these */
Assert(elem_index < 0 || (tpath == NULL && ipath == NULL && field == NULL)); Assert(elem_index < 0 || (tpath == NULL && ipath == NULL && field == NULL));
Assert(tpath == NULL || field == NULL); Assert(tpath == NULL || field == NULL);
state = palloc0(sizeof(getState)); state = palloc0(sizeof(GetState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
state->lex = lex; state->lex = lex;
/* is it "_as_text" variant? */ /* is it "_as_text" variant? */
...@@ -560,7 +560,7 @@ get_worker(text *json, ...@@ -560,7 +560,7 @@ get_worker(text *json,
static void static void
get_object_start(void *state) get_object_start(void *state)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0 && _state->search_type == JSON_SEARCH_ARRAY) if (_state->lex->lex_level == 0 && _state->search_type == JSON_SEARCH_ARRAY)
...@@ -572,7 +572,7 @@ get_object_start(void *state) ...@@ -572,7 +572,7 @@ get_object_start(void *state)
static void static void
get_object_field_start(void *state, char *fname, bool isnull) get_object_field_start(void *state, char *fname, bool isnull)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
bool get_next = false; bool get_next = false;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
...@@ -624,7 +624,7 @@ get_object_field_start(void *state, char *fname, bool isnull) ...@@ -624,7 +624,7 @@ get_object_field_start(void *state, char *fname, bool isnull)
static void static void
get_object_field_end(void *state, char *fname, bool isnull) get_object_field_end(void *state, char *fname, bool isnull)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
bool get_last = false; bool get_last = false;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
...@@ -674,7 +674,7 @@ get_object_field_end(void *state, char *fname, bool isnull) ...@@ -674,7 +674,7 @@ get_object_field_end(void *state, char *fname, bool isnull)
static void static void
get_array_start(void *state) get_array_start(void *state)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
/* json structure check */ /* json structure check */
...@@ -695,7 +695,7 @@ get_array_start(void *state) ...@@ -695,7 +695,7 @@ get_array_start(void *state)
static void static void
get_array_element_start(void *state, bool isnull) get_array_element_start(void *state, bool isnull)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
bool get_next = false; bool get_next = false;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
...@@ -754,7 +754,7 @@ get_array_element_start(void *state, bool isnull) ...@@ -754,7 +754,7 @@ get_array_element_start(void *state, bool isnull)
static void static void
get_array_element_end(void *state, bool isnull) get_array_element_end(void *state, bool isnull)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
bool get_last = false; bool get_last = false;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
...@@ -792,7 +792,7 @@ get_array_element_end(void *state, bool isnull) ...@@ -792,7 +792,7 @@ get_array_element_end(void *state, bool isnull)
static void static void
get_scalar(void *state, char *token, JsonTokenType tokentype) get_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
GetState _state = (GetState) state; GetState *_state = (GetState *) state;
if (_state->lex->lex_level == 0 && _state->search_type != JSON_SEARCH_PATH) if (_state->lex->lex_level == 0 && _state->search_type != JSON_SEARCH_PATH)
ereport(ERROR, ereport(ERROR,
...@@ -816,12 +816,12 @@ json_array_length(PG_FUNCTION_ARGS) ...@@ -816,12 +816,12 @@ json_array_length(PG_FUNCTION_ARGS)
{ {
text *json = PG_GETARG_TEXT_P(0); text *json = PG_GETARG_TEXT_P(0);
AlenState state; AlenState *state;
JsonLexContext *lex = makeJsonLexContext(json, false); JsonLexContext *lex = makeJsonLexContext(json, false);
JsonSemAction sem; JsonSemAction *sem;
state = palloc0(sizeof(alenState)); state = palloc0(sizeof(AlenState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
/* palloc0 does this for us */ /* palloc0 does this for us */
#if 0 #if 0
...@@ -847,7 +847,7 @@ json_array_length(PG_FUNCTION_ARGS) ...@@ -847,7 +847,7 @@ json_array_length(PG_FUNCTION_ARGS)
static void static void
alen_object_start(void *state) alen_object_start(void *state)
{ {
AlenState _state = (AlenState) state; AlenState *_state = (AlenState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -859,7 +859,7 @@ alen_object_start(void *state) ...@@ -859,7 +859,7 @@ alen_object_start(void *state)
static void static void
alen_scalar(void *state, char *token, JsonTokenType tokentype) alen_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
AlenState _state = (AlenState) state; AlenState *_state = (AlenState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -871,7 +871,7 @@ alen_scalar(void *state, char *token, JsonTokenType tokentype) ...@@ -871,7 +871,7 @@ alen_scalar(void *state, char *token, JsonTokenType tokentype)
static void static void
alen_array_element_start(void *state, bool isnull) alen_array_element_start(void *state, bool isnull)
{ {
AlenState _state = (AlenState) state; AlenState *_state = (AlenState *) state;
/* just count up all the level 1 elements */ /* just count up all the level 1 elements */
if (_state->lex->lex_level == 1) if (_state->lex->lex_level == 1)
...@@ -905,14 +905,14 @@ each_worker(PG_FUNCTION_ARGS, bool as_text) ...@@ -905,14 +905,14 @@ each_worker(PG_FUNCTION_ARGS, bool as_text)
{ {
text *json = PG_GETARG_TEXT_P(0); text *json = PG_GETARG_TEXT_P(0);
JsonLexContext *lex = makeJsonLexContext(json, true); JsonLexContext *lex = makeJsonLexContext(json, true);
JsonSemAction sem; JsonSemAction *sem;
ReturnSetInfo *rsi; ReturnSetInfo *rsi;
MemoryContext old_cxt; MemoryContext old_cxt;
TupleDesc tupdesc; TupleDesc tupdesc;
EachState state; EachState *state;
state = palloc0(sizeof(eachState)); state = palloc0(sizeof(EachState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
rsi = (ReturnSetInfo *) fcinfo->resultinfo; rsi = (ReturnSetInfo *) fcinfo->resultinfo;
...@@ -968,7 +968,7 @@ each_worker(PG_FUNCTION_ARGS, bool as_text) ...@@ -968,7 +968,7 @@ each_worker(PG_FUNCTION_ARGS, bool as_text)
static void static void
each_object_field_start(void *state, char *fname, bool isnull) each_object_field_start(void *state, char *fname, bool isnull)
{ {
EachState _state = (EachState) state; EachState *_state = (EachState *) state;
/* save a pointer to where the value starts */ /* save a pointer to where the value starts */
if (_state->lex->lex_level == 1) if (_state->lex->lex_level == 1)
...@@ -988,7 +988,7 @@ each_object_field_start(void *state, char *fname, bool isnull) ...@@ -988,7 +988,7 @@ each_object_field_start(void *state, char *fname, bool isnull)
static void static void
each_object_field_end(void *state, char *fname, bool isnull) each_object_field_end(void *state, char *fname, bool isnull)
{ {
EachState _state = (EachState) state; EachState *_state = (EachState *) state;
MemoryContext old_cxt; MemoryContext old_cxt;
int len; int len;
text *val; text *val;
...@@ -1035,7 +1035,7 @@ each_object_field_end(void *state, char *fname, bool isnull) ...@@ -1035,7 +1035,7 @@ each_object_field_end(void *state, char *fname, bool isnull)
static void static void
each_array_start(void *state) each_array_start(void *state)
{ {
EachState _state = (EachState) state; EachState *_state = (EachState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -1047,7 +1047,7 @@ each_array_start(void *state) ...@@ -1047,7 +1047,7 @@ each_array_start(void *state)
static void static void
each_scalar(void *state, char *token, JsonTokenType tokentype) each_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
EachState _state = (EachState) state; EachState *_state = (EachState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -1074,14 +1074,14 @@ json_array_elements(PG_FUNCTION_ARGS) ...@@ -1074,14 +1074,14 @@ json_array_elements(PG_FUNCTION_ARGS)
/* elements doesn't need any escaped strings, so use false here */ /* elements doesn't need any escaped strings, so use false here */
JsonLexContext *lex = makeJsonLexContext(json, false); JsonLexContext *lex = makeJsonLexContext(json, false);
JsonSemAction sem; JsonSemAction *sem;
ReturnSetInfo *rsi; ReturnSetInfo *rsi;
MemoryContext old_cxt; MemoryContext old_cxt;
TupleDesc tupdesc; TupleDesc tupdesc;
ElementsState state; ElementsState *state;
state = palloc0(sizeof(elementsState)); state = palloc0(sizeof(ElementsState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
rsi = (ReturnSetInfo *) fcinfo->resultinfo; rsi = (ReturnSetInfo *) fcinfo->resultinfo;
...@@ -1134,7 +1134,7 @@ json_array_elements(PG_FUNCTION_ARGS) ...@@ -1134,7 +1134,7 @@ json_array_elements(PG_FUNCTION_ARGS)
static void static void
elements_array_element_start(void *state, bool isnull) elements_array_element_start(void *state, bool isnull)
{ {
ElementsState _state = (ElementsState) state; ElementsState *_state = (ElementsState *) state;
/* save a pointer to where the value starts */ /* save a pointer to where the value starts */
if (_state->lex->lex_level == 1) if (_state->lex->lex_level == 1)
...@@ -1144,7 +1144,7 @@ elements_array_element_start(void *state, bool isnull) ...@@ -1144,7 +1144,7 @@ elements_array_element_start(void *state, bool isnull)
static void static void
elements_array_element_end(void *state, bool isnull) elements_array_element_end(void *state, bool isnull)
{ {
ElementsState _state = (ElementsState) state; ElementsState *_state = (ElementsState *) state;
MemoryContext old_cxt; MemoryContext old_cxt;
int len; int len;
text *val; text *val;
...@@ -1176,7 +1176,7 @@ elements_array_element_end(void *state, bool isnull) ...@@ -1176,7 +1176,7 @@ elements_array_element_end(void *state, bool isnull)
static void static void
elements_object_start(void *state) elements_object_start(void *state)
{ {
ElementsState _state = (ElementsState) state; ElementsState *_state = (ElementsState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -1188,7 +1188,7 @@ elements_object_start(void *state) ...@@ -1188,7 +1188,7 @@ elements_object_start(void *state)
static void static void
elements_scalar(void *state, char *token, JsonTokenType tokentype) elements_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
ElementsState _state = (ElementsState) state; ElementsState *_state = (ElementsState *) state;
/* json structure check */ /* json structure check */
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
...@@ -1232,7 +1232,7 @@ json_populate_record(PG_FUNCTION_ARGS) ...@@ -1232,7 +1232,7 @@ json_populate_record(PG_FUNCTION_ARGS)
Datum *values; Datum *values;
bool *nulls; bool *nulls;
char fname[NAMEDATALEN]; char fname[NAMEDATALEN];
JsonHashEntry hashentry; JsonHashEntry *hashentry;
use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2); use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2);
...@@ -1423,21 +1423,21 @@ get_json_object_as_hash(text *json, char *funcname, bool use_json_as_text) ...@@ -1423,21 +1423,21 @@ get_json_object_as_hash(text *json, char *funcname, bool use_json_as_text)
{ {
HASHCTL ctl; HASHCTL ctl;
HTAB *tab; HTAB *tab;
JHashState state; JHashState *state;
JsonLexContext *lex = makeJsonLexContext(json, true); JsonLexContext *lex = makeJsonLexContext(json, true);
JsonSemAction sem; JsonSemAction *sem;
memset(&ctl, 0, sizeof(ctl)); memset(&ctl, 0, sizeof(ctl));
ctl.keysize = NAMEDATALEN; ctl.keysize = NAMEDATALEN;
ctl.entrysize = sizeof(jsonHashEntry); ctl.entrysize = sizeof(JsonHashEntry);
ctl.hcxt = CurrentMemoryContext; ctl.hcxt = CurrentMemoryContext;
tab = hash_create("json object hashtable", tab = hash_create("json object hashtable",
100, 100,
&ctl, &ctl,
HASH_ELEM | HASH_CONTEXT); HASH_ELEM | HASH_CONTEXT);
state = palloc0(sizeof(jhashState)); state = palloc0(sizeof(JHashState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
state->function_name = funcname; state->function_name = funcname;
state->hash = tab; state->hash = tab;
...@@ -1458,7 +1458,7 @@ get_json_object_as_hash(text *json, char *funcname, bool use_json_as_text) ...@@ -1458,7 +1458,7 @@ get_json_object_as_hash(text *json, char *funcname, bool use_json_as_text)
static void static void
hash_object_field_start(void *state, char *fname, bool isnull) hash_object_field_start(void *state, char *fname, bool isnull)
{ {
JHashState _state = (JHashState) state; JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level > 1) if (_state->lex->lex_level > 1)
return; return;
...@@ -1483,8 +1483,8 @@ hash_object_field_start(void *state, char *fname, bool isnull) ...@@ -1483,8 +1483,8 @@ hash_object_field_start(void *state, char *fname, bool isnull)
static void static void
hash_object_field_end(void *state, char *fname, bool isnull) hash_object_field_end(void *state, char *fname, bool isnull)
{ {
JHashState _state = (JHashState) state; JHashState *_state = (JHashState *) state;
JsonHashEntry hashentry; JsonHashEntry *hashentry;
bool found; bool found;
char name[NAMEDATALEN]; char name[NAMEDATALEN];
...@@ -1525,7 +1525,7 @@ hash_object_field_end(void *state, char *fname, bool isnull) ...@@ -1525,7 +1525,7 @@ hash_object_field_end(void *state, char *fname, bool isnull)
static void static void
hash_array_start(void *state) hash_array_start(void *state)
{ {
JHashState _state = (JHashState) state; JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
ereport(ERROR, ereport(ERROR,
...@@ -1536,7 +1536,7 @@ hash_array_start(void *state) ...@@ -1536,7 +1536,7 @@ hash_array_start(void *state)
static void static void
hash_scalar(void *state, char *token, JsonTokenType tokentype) hash_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
JHashState _state = (JHashState) state; JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
ereport(ERROR, ereport(ERROR,
...@@ -1573,8 +1573,8 @@ json_populate_recordset(PG_FUNCTION_ARGS) ...@@ -1573,8 +1573,8 @@ json_populate_recordset(PG_FUNCTION_ARGS)
RecordIOData *my_extra; RecordIOData *my_extra;
int ncolumns; int ncolumns;
JsonLexContext *lex; JsonLexContext *lex;
JsonSemAction sem; JsonSemAction *sem;
PopulateRecordsetState state; PopulateRecordsetState *state;
use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2); use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2);
...@@ -1602,8 +1602,8 @@ json_populate_recordset(PG_FUNCTION_ARGS) ...@@ -1602,8 +1602,8 @@ json_populate_recordset(PG_FUNCTION_ARGS)
*/ */
(void) get_call_result_type(fcinfo, NULL, &tupdesc); (void) get_call_result_type(fcinfo, NULL, &tupdesc);
state = palloc0(sizeof(populateRecordsetState)); state = palloc0(sizeof(PopulateRecordsetState));
sem = palloc0(sizeof(jsonSemAction)); sem = palloc0(sizeof(JsonSemAction));
/* make these in a sufficiently long-lived memory context */ /* make these in a sufficiently long-lived memory context */
...@@ -1690,7 +1690,7 @@ json_populate_recordset(PG_FUNCTION_ARGS) ...@@ -1690,7 +1690,7 @@ json_populate_recordset(PG_FUNCTION_ARGS)
static void static void
populate_recordset_object_start(void *state) populate_recordset_object_start(void *state)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
int lex_level = _state->lex->lex_level; int lex_level = _state->lex->lex_level;
HASHCTL ctl; HASHCTL ctl;
...@@ -1706,7 +1706,7 @@ populate_recordset_object_start(void *state) ...@@ -1706,7 +1706,7 @@ populate_recordset_object_start(void *state)
/* set up a new hash for this entry */ /* set up a new hash for this entry */
memset(&ctl, 0, sizeof(ctl)); memset(&ctl, 0, sizeof(ctl));
ctl.keysize = NAMEDATALEN; ctl.keysize = NAMEDATALEN;
ctl.entrysize = sizeof(jsonHashEntry); ctl.entrysize = sizeof(JsonHashEntry);
ctl.hcxt = CurrentMemoryContext; ctl.hcxt = CurrentMemoryContext;
_state->json_hash = hash_create("json object hashtable", _state->json_hash = hash_create("json object hashtable",
100, 100,
...@@ -1717,7 +1717,7 @@ populate_recordset_object_start(void *state) ...@@ -1717,7 +1717,7 @@ populate_recordset_object_start(void *state)
static void static void
populate_recordset_object_end(void *state) populate_recordset_object_end(void *state)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
HTAB *json_hash = _state->json_hash; HTAB *json_hash = _state->json_hash;
Datum *values; Datum *values;
bool *nulls; bool *nulls;
...@@ -1726,7 +1726,7 @@ populate_recordset_object_end(void *state) ...@@ -1726,7 +1726,7 @@ populate_recordset_object_end(void *state)
RecordIOData *my_extra = _state->my_extra; RecordIOData *my_extra = _state->my_extra;
int ncolumns = my_extra->ncolumns; int ncolumns = my_extra->ncolumns;
TupleDesc tupdesc = _state->ret_tdesc; TupleDesc tupdesc = _state->ret_tdesc;
JsonHashEntry hashentry; JsonHashEntry *hashentry;
HeapTupleHeader rec = _state->rec; HeapTupleHeader rec = _state->rec;
HeapTuple rettuple; HeapTuple rettuple;
...@@ -1830,7 +1830,7 @@ populate_recordset_object_end(void *state) ...@@ -1830,7 +1830,7 @@ populate_recordset_object_end(void *state)
static void static void
populate_recordset_array_element_start(void *state, bool isnull) populate_recordset_array_element_start(void *state, bool isnull)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level == 1 && if (_state->lex->lex_level == 1 &&
_state->lex->token_type != JSON_TOKEN_OBJECT_START) _state->lex->token_type != JSON_TOKEN_OBJECT_START)
...@@ -1842,7 +1842,7 @@ populate_recordset_array_element_start(void *state, bool isnull) ...@@ -1842,7 +1842,7 @@ populate_recordset_array_element_start(void *state, bool isnull)
static void static void
populate_recordset_array_start(void *state) populate_recordset_array_start(void *state)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level != 0 && !_state->use_json_as_text) if (_state->lex->lex_level != 0 && !_state->use_json_as_text)
ereport(ERROR, ereport(ERROR,
...@@ -1853,7 +1853,7 @@ populate_recordset_array_start(void *state) ...@@ -1853,7 +1853,7 @@ populate_recordset_array_start(void *state)
static void static void
populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype) populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level == 0) if (_state->lex->lex_level == 0)
ereport(ERROR, ereport(ERROR,
...@@ -1867,7 +1867,7 @@ populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype) ...@@ -1867,7 +1867,7 @@ populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype)
static void static void
populate_recordset_object_field_start(void *state, char *fname, bool isnull) populate_recordset_object_field_start(void *state, char *fname, bool isnull)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level > 2) if (_state->lex->lex_level > 2)
return; return;
...@@ -1890,8 +1890,8 @@ populate_recordset_object_field_start(void *state, char *fname, bool isnull) ...@@ -1890,8 +1890,8 @@ populate_recordset_object_field_start(void *state, char *fname, bool isnull)
static void static void
populate_recordset_object_field_end(void *state, char *fname, bool isnull) populate_recordset_object_field_end(void *state, char *fname, bool isnull)
{ {
PopulateRecordsetState _state = (PopulateRecordsetState) state; PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
JsonHashEntry hashentry; JsonHashEntry *hashentry;
bool found; bool found;
char name[NAMEDATALEN]; char name[NAMEDATALEN];
......
...@@ -74,7 +74,7 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke ...@@ -74,7 +74,7 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke
* to doing a pure parse with no side-effects, and is therefore exactly * to doing a pure parse with no side-effects, and is therefore exactly
* what the json input routines do. * what the json input routines do.
*/ */
typedef struct jsonSemAction typedef struct JsonSemAction
{ {
void *semstate; void *semstate;
json_struct_action object_start; json_struct_action object_start;
...@@ -86,8 +86,7 @@ typedef struct jsonSemAction ...@@ -86,8 +86,7 @@ typedef struct jsonSemAction
json_aelem_action array_element_start; json_aelem_action array_element_start;
json_aelem_action array_element_end; json_aelem_action array_element_end;
json_scalar_action scalar; json_scalar_action scalar;
} jsonSemAction, } JsonSemAction;
*JsonSemAction;
/* /*
* parse_json will parse the string in the lex calling the * parse_json will parse the string in the lex calling the
...@@ -98,7 +97,7 @@ typedef struct jsonSemAction ...@@ -98,7 +97,7 @@ typedef struct jsonSemAction
* points to. If the action pointers are NULL the parser * points to. If the action pointers are NULL the parser
* does nothing and just continues. * does nothing and just continues.
*/ */
extern void pg_parse_json(JsonLexContext *lex, JsonSemAction sem); extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
/* /*
* constructor for JsonLexContext, with or without strval element. * constructor for JsonLexContext, with or without strval element.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment