0006-Moving-common-functions-into-jsonapi.c.patch.WIP

application/octet-stream

Filename: 0006-Moving-common-functions-into-jsonapi.c.patch.WIP
Type: application/octet-stream
Part: 5
Message: Re: making the backend's json parser work in frontend code
From 59cc79e5f7984ab07d3ef724d2ed272539225ff0 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 21 Jan 2020 12:47:29 -0800
Subject: [PATCH 06/11] Moving common functions into jsonapi.c

Moving functions common to frontend and backend code
out of src/backend/utils and into new file
src/common/jsonapi.c
---
 src/backend/utils/adt/json.c      | 33 -------------------------
 src/backend/utils/adt/jsonfuncs.c | 20 +++++++++++++++
 src/common/Makefile               |  1 +
 src/common/jsonapi.c              | 41 +++++++++++++++++++++++++++++++
 src/include/common/jsonapi.h      |  1 -
 src/include/utils/jsonfuncs.h     |  1 +
 6 files changed, 63 insertions(+), 34 deletions(-)
 create mode 100644 src/common/jsonapi.c

diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 6175fba447..dc71909a3c 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -286,39 +286,6 @@ json_recv(PG_FUNCTION_ARGS)
 	PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
 }
 
-/*
- * makeJsonLexContext
- *
- * lex constructor, with or without StringInfo object
- * for de-escaped lexemes.
- *
- * Without is better as it makes the processing faster, so only make one
- * if really required.
- *
- * If you already have the json as a text* value, use the first of these
- * functions, otherwise use  makeJsonLexContextCstringLen().
- */
-JsonLexContext *
-makeJsonLexContext(text *json, bool need_escapes)
-{
-	return makeJsonLexContextCstringLen(VARDATA_ANY(json),
-										VARSIZE_ANY_EXHDR(json),
-										need_escapes);
-}
-
-JsonLexContext *
-makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
-{
-	JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
-
-	lex->input = lex->token_terminator = lex->line_start = json;
-	lex->line_number = 1;
-	lex->input_length = len;
-	if (need_escapes)
-		lex->strval = makeStringInfo();
-	return lex;
-}
-
 /*
  * pg_parse_json
  *
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index e59e99c0d0..3979145ecc 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -483,6 +483,26 @@ static void transform_string_values_object_field_start(void *state, char *fname,
 static void transform_string_values_array_element_start(void *state, bool isnull);
 static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
 
+/*
+ * makeJsonLexContext
+ *
+ * lex constructor, with or without StringInfo object
+ * for de-escaped lexemes.
+ *
+ * Without is better as it makes the processing faster, so only make one
+ * if really required.
+ *
+ * If you already have the json as a text* value, use the first of these
+ * functions, otherwise use  makeJsonLexContextCstringLen().
+ */
+JsonLexContext *
+makeJsonLexContext(text *json, bool need_escapes)
+{
+	return makeJsonLexContextCstringLen(VARDATA_ANY(json),
+										VARSIZE_ANY_EXHDR(json),
+										need_escapes);
+}
+
 /*
  * SQL function json_object_keys
  *
diff --git a/src/common/Makefile b/src/common/Makefile
index 3882cd7a3d..e9caa08418 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -56,6 +56,7 @@ OBJS_COMMON = \
 	f2s.o \
 	file_perm.o \
 	ip.o \
+	jsonapi.o \
 	keywords.o \
 	kwlookup.o \
 	link-canary.o \
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c
new file mode 100644
index 0000000000..69fa42705f
--- /dev/null
+++ b/src/common/jsonapi.c
@@ -0,0 +1,41 @@
+/*-------------------------------------------------------------------------
+ *
+ * jsonapi.c
+ *	  Functions for working with json formatted data.
+ *
+ * Portions Copyright (c) 1998-2020, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *	  src/common/jsonapi.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "c.h"
+
+#include "common/jsonapi.h"
+
+#ifndef FRONTEND
+#include "utils/palloc.h"
+#endif
+
+JsonLexContext *
+makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
+{
+	JsonLexContext *lex;
+
+#ifndef FRONTEND
+	lex = (JsonLexContext*) palloc0fast(sizeof(JsonLexContext));
+#else
+	lex = (JsonLexContext*) malloc(sizeof(JsonLexContext));
+	memset(lex, 0, sizeof(JsonLexContext));
+#endif
+
+	lex->input = lex->token_terminator = lex->line_start = json;
+	lex->line_number = 1;
+	lex->input_length = len;
+	if (need_escapes)
+		lex->strval = makeStringInfo();
+	return lex;
+}
+
+
diff --git a/src/include/common/jsonapi.h b/src/include/common/jsonapi.h
index 2aa35dbb3c..fd1d94d1d7 100644
--- a/src/include/common/jsonapi.h
+++ b/src/include/common/jsonapi.h
@@ -119,7 +119,6 @@ extern int	json_count_array_elements(JsonLexContext *lex);
  * If you already have the json as a text* value, use the first of these
  * functions, otherwise use  makeJsonLexContextCstringLen().
  */
-extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
 extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
 													int len,
 													bool need_escapes);
diff --git a/src/include/utils/jsonfuncs.h b/src/include/utils/jsonfuncs.h
index 8d7abe73e6..bade7248f9 100644
--- a/src/include/utils/jsonfuncs.h
+++ b/src/include/utils/jsonfuncs.h
@@ -24,5 +24,6 @@ extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid,
 								const int *tzp);
 extern text *transform_json_string_values(text *json, void *action_state,
 										  JsonTransformStringValuesAction transform_action);
+extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
 
 #endif							/* JSONFUNCS_H */
-- 
2.21.1 (Apple Git-122.3)