Commits

Andrew Dunstan committed 871c08b

a little code tidying

Comments (0)

Files changed (2)

src/backend/utils/adt/json.c

 };
 static JsonSemAction NullSemAction = &nullSemAction;
 
-
 /* Recursive Descent parser support routines */
 
 static inline JsonTokenType
 		report_parse_error(ctx, lex);;
 }
 
-
-
 /* fake type category for JSON so we can distinguish it in datum_to_json */
 #define TYPCATEGORY_JSON 'j'
 /* letters appearing in numeric output that aren't valid in a JSON number */
 	 (c) == '_' || \
 	 IS_HIGHBIT_SET(c))
 
-
 /*
  * Input.
  */
 /*
  * parse routines
  */
-
-
 void
 pg_parse_json(JsonLexContext *lex, JsonSemAction sem)
 {
 
 }
 
-
 static void
 parse_scalar(JsonLexContext *lex, JsonSemAction sem)
 {

src/backend/utils/adt/jsonfuncs.c

 
 /* turn a json object into a hash table */
 static HTAB *get_json_object_as_hash(char * jsonstr, char *funcname);
-/* semantic action functions for het_json_object_as_hash */
+/* semantic action functions for get_json_object_as_hash */
 static void hash_object_start(void *state);
 static void hash_object_field_start(void *state, char *fname, bool isnull);
 static void hash_object_field_end(void *state, char *fname, bool isnull);
 	char  *result_start;
 } unnestState, *UnnestState;
 
+/* state for get_json_object_as_hash */
 typedef struct
 {
 	JsonLexContext *lex;
 	char  *function_name;
 } jhashState, *JHashState;
 
-/* used to build the hashtable used in populate-record */
+/* used to build the hashtable */
 typedef struct 
 {
 	char fname[NAMEDATALEN];
 	char *val;
+	char *json;
 	bool isnull;
 } jsonHashEntry, *JsonHashEntry;
 
 	ColumnIOData columns[1];	/* VARIABLE LENGTH ARRAY */
 } RecordIOData;
 
+/*
+ * SQL function json_object-keys
+ *
+ * Returns the set of keys for the object argument.
+ */
 
 PG_FUNCTION_INFO_V1(json_object_keys);
 
 }
 
 
+/*
+ * SQL function json_array_length
+ */
+
 PG_FUNCTION_INFO_V1(json_array_length);
 
 Datum
 		_state->count++;
 }
 
+/*
+ * SQL function json_each
+ *
+ * decompose a json object into key value pairs.
+ */
+
 PG_FUNCTION_INFO_V1(json_each);
 
 Datum
 	PG_RETURN_NULL();
 }
 
+/*
+ * SQL function json_each_as_text
+ *
+ * decompose a json object into key value pairs with
+ * de-escaped scalar string values.
+ */
+
 PG_FUNCTION_INFO_V1(json_each_as_text);
 
 Datum
 		_state->normalized_scalar = token;
 }
 
+/*
+ * SQL function json_unnest
+ *
+ * get the elements from a json array
+ */
+
 PG_FUNCTION_INFO_V1(json_unnest);
 
 Datum
 				 errmsg("cannot call json_unnest on a scalar")));
 }
 
-static HTAB *
-get_json_object_as_hash(char * jsonstr, char *funcname)
-{
-
-	HASHCTL     ctl;
-	HTAB       *tab;
-	JHashState	   state;
-	JsonLexContext lex;
-	JsonSemAction  sem;
-
-
-	memset(&ctl, 0, sizeof(ctl));
-    ctl.keysize = NAMEDATALEN;
-    ctl.entrysize = sizeof(jsonHashEntry);
-	tab = hash_create("json object hashtable",
-					  32,
-					  &ctl,
-					  HASH_ELEM);
-
-	state = palloc0(sizeof(jhashState));
-	sem = palloc0(sizeof(jsonSemAction));
-
-	/* palloc0 does this for us */
-#if 0	 
-	state->lex_level = 0;
-#endif
-
-	state->function_name = funcname;
-	state->hash = tab;
-	state->lex = &lex;
-
-	sem->semstate = (void *) state;
-	sem->object_start = hash_object_start;
-	sem->array_start = hash_array_start;
-	sem->scalar = hash_scalar;
-	sem->object_field_start = hash_object_field_start;
-	sem->object_field_end = hash_object_field_end;
-
-	/* Set up lexing context. */
-	lex.input = jsonstr;
-	lex.token_terminator = lex.input;
-	lex.line_number = 1;
-	lex.line_start = jsonstr;
-	lex.strval = makeStringInfo();
-
-	pg_parse_json(&lex, sem);
-
-	return tab;
-}
+/*
+ * SQL function json_populate_record
+ *
+ * set fields in a record from the argument json
+ *
+ * Code adapted shamelessly from hstore's populate_record
+ * which is in turn partly adapted from record_out.
+ *
+ * The json is decomposed into a hash table, in which each
+ * field in the record is then looked up by name.
+ */
 
 PG_FUNCTION_INFO_V1(json_populate_record);
 
 	PG_RETURN_DATUM(HeapTupleGetDatum(rettuple));
 }
 
-static void hash_object_start(void *state)
+/*
+ * get_json_object_as_hash
+ *
+ * decompose a json object into a hash table.
+ *
+ * Currently doesn't allow anything but a flat object. Should this
+ * change?
+ *
+ * funcname argument allows caller to pass in its name for use in
+ * error messages.
+ */
+static HTAB *
+get_json_object_as_hash(char * jsonstr, char *funcname)
 {
-	JHashState	_state = (JHashState) state;
 
-	_state->lex_level++;
+	HASHCTL     ctl;
+	HTAB       *tab;
+	JHashState	   state;
+	JsonLexContext lex;
+	JsonSemAction  sem;
+
+
+	memset(&ctl, 0, sizeof(ctl));
+    ctl.keysize = NAMEDATALEN;
+    ctl.entrysize = sizeof(jsonHashEntry);
+	tab = hash_create("json object hashtable",
+					  100,
+					  &ctl,
+					  HASH_ELEM);
+
+	state = palloc0(sizeof(jhashState));
+	sem = palloc0(sizeof(jsonSemAction));
+
+	/* palloc0 does this for us */
+#if 0	 
+	state->lex_level = 0;
+#endif
+
+	state->function_name = funcname;
+	state->hash = tab;
+	state->lex = &lex;
+
+	sem->semstate = (void *) state;
+	sem->object_start = hash_object_start;
+	sem->array_start = hash_array_start;
+	sem->scalar = hash_scalar;
+	sem->object_field_start = hash_object_field_start;
+	sem->object_field_end = hash_object_field_end;
+
+	/* Set up lexing context. */
+	lex.input = jsonstr;
+	lex.token_terminator = lex.input;
+	lex.line_number = 1;
+	lex.line_start = jsonstr;
+	lex.strval = makeStringInfo();
+
+	pg_parse_json(&lex, sem);
+
+	return tab;
 }
 
-/*
-static void hash_object_end(void *state)
+static void hash_object_start(void *state)
 {
 	JHashState	_state = (JHashState) state;
 
-	_state->lex_level--;
+	_state->lex_level++;
 }
-*/
 
 static void hash_object_field_start(void *state, char *fname, bool isnull)
 {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.