summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Bertrand <bertrandop@gmail.com>2018-02-16 15:16:11 +0100
committerOlivier Bertrand <bertrandop@gmail.com>2018-02-16 15:16:11 +0100
commit18e6a81bab2ea148b008b9d0106bbcc0d42ac026 (patch)
tree7976a211066aa1893f6eb3eb140b7779e2874f5c
parent46f3e320dafe409cb1abc29199d80c04111638c1 (diff)
downloadmariadb-git-18e6a81bab2ea148b008b9d0106bbcc0d42ac026.tar.gz
- Make Json_Array_Add to accept a non JSON item as 1st parameter
This is a test that will extended to some other UDF functions. modified: storage/connect/json.cpp modified: storage/connect/json.h modified: storage/connect/jsonudf.cpp
-rw-r--r--storage/connect/json.cpp19
-rw-r--r--storage/connect/json.h3
-rw-r--r--storage/connect/jsonudf.cpp67
3 files changed, 76 insertions, 13 deletions
diff --git a/storage/connect/json.cpp b/storage/connect/json.cpp
index f6ed48c4d06..ce5ac457a65 100644
--- a/storage/connect/json.cpp
+++ b/storage/connect/json.cpp
@@ -1224,6 +1224,7 @@ PJVAL JARRAY::AddValue(PGLOBAL g, PJVAL jvp, int *x)
Last->Next = jvp;
Last = jvp;
+ Last->Next = NULL;
} // endif x
return jvp;
@@ -1319,6 +1320,24 @@ bool JARRAY::IsNull(void)
/* -------------------------- Class JVALUE- -------------------------- */
/***********************************************************************/
+/* Constructor for a JSON. */
+/***********************************************************************/
+JVALUE::JVALUE(PJSON jsp) : JSON()
+{
+ if (jsp->GetType() == TYPE_JVAL) {
+ Jsp = NULL;
+ Value = jsp->GetValue();
+ } else {
+ Jsp = jsp;
+ Value = NULL;
+ } // endif Type
+
+ Next = NULL;
+ Del = false;
+ Size = 1;
+} // end of JVALUE constructor
+
+/***********************************************************************/
/* Constructor for a Value with a given string or numeric value. */
/***********************************************************************/
JVALUE::JVALUE(PGLOBAL g, PVAL valp) : JSON()
diff --git a/storage/connect/json.h b/storage/connect/json.h
index 375532212c4..c264a729b68 100644
--- a/storage/connect/json.h
+++ b/storage/connect/json.h
@@ -258,8 +258,7 @@ class JVALUE : public JSON {
friend bool SerializeValue(JOUT *, PJVAL);
public:
JVALUE(void) : JSON() {Clear();}
- JVALUE(PJSON jsp) : JSON()
- {Jsp = jsp; Value = NULL; Next = NULL; Del = false; Size = 1;}
+ JVALUE(PJSON jsp);
JVALUE(PGLOBAL g, PVAL valp);
JVALUE(PGLOBAL g, PCSZ strp);
diff --git a/storage/connect/jsonudf.cpp b/storage/connect/jsonudf.cpp
index 952cd76ef8d..ab63f42bc3c 100644
--- a/storage/connect/jsonudf.cpp
+++ b/storage/connect/jsonudf.cpp
@@ -1760,6 +1760,41 @@ static PJVAL MakeValue(PGLOBAL g, UDF_ARGS *args, uint i, PJSON *top = NULL)
} // end of MakeValue
/*********************************************************************************/
+/* Try making a JSON value of the passed type from the passed argument. */
+/*********************************************************************************/
+static PJVAL MakeTypedValue(PGLOBAL g, UDF_ARGS *args, uint i,
+ JTYP type, PJSON *top = NULL)
+{
+ char *sap;
+ PJSON jsp;
+ PJVAL jvp = MakeValue(g, args, i, top);
+
+ //if (type == TYPE_JSON) {
+ // if (jvp->GetValType() >= TYPE_JSON)
+ // return jvp;
+
+ //} else if (jvp->GetValType() == type)
+ // return jvp;
+
+ if (jvp->GetValType() == TYPE_STRG) {
+ sap = jvp->GetString(g);
+
+ if ((jsp = ParseJson(g, sap, strlen(sap)))) {
+ if (type == TYPE_JSON || jsp->GetType() == type) {
+ if (top)
+ *top = jsp;
+
+ jvp->SetValue(jsp);
+ } // endif Type
+
+ } // endif jsp
+
+ } // endif Type
+
+ return jvp;
+} // end of MakeTypedValue
+
+/*********************************************************************************/
/* Make a Json value containing the parameter. */
/*********************************************************************************/
my_bool jsonvalue_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
@@ -1953,9 +1988,9 @@ my_bool json_array_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
if (args->arg_count < 2) {
strcpy(message, "This function must have at least 2 arguments");
return true;
- } else if (!IsJson(args, 0)) {
- strcpy(message, "First argument must be a json item");
- return true;
+ //} else if (!IsJson(args, 0)) {
+ // strcpy(message, "First argument must be a json item");
+ // return true;
} else
CalcLen(args, false, reslen, memlen, true);
@@ -1994,22 +2029,32 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
PJVAL jvp;
PJAR arp;
- jvp = MakeValue(g, args, 0, &top);
+ jvp = MakeTypedValue(g, args, 0, TYPE_JSON, &top);
jsp = jvp->GetJson();
x = GetIntArgPtr(g, args, n);
if (CheckPath(g, args, jsp, jvp, 2))
PUSH_WARNING(g->Message);
- else if (jvp && jvp->GetValType() == TYPE_JAR) {
+ else if (jvp) {
PGLOBAL gb = GetMemPtr(g, args, 0);
- arp = jvp->GetArray();
+ if (jvp->GetValType() != TYPE_JAR) {
+ arp = new(gb)JARRAY;
+ arp->AddValue(gb, new(gb) JVALUE(jvp));
+ jvp->SetValue(arp);
+
+ if (!top)
+ top = arp;
+
+ } else
+ arp = jvp->GetArray();
+
arp->AddValue(gb, MakeValue(gb, args, 1), x);
arp->InitArray(gb);
str = MakeResult(g, args, top, n);
} else {
- PUSH_WARNING("First argument target is not an array");
-// if (g->Mrr) *error = 1; (only if no path)
+ PUSH_WARNING("Target is not an array");
+ // if (g->Mrr) *error = 1; (only if no path)
} // endif jvp
} // endif CheckMemory
@@ -4458,9 +4503,9 @@ my_bool jbin_array_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
if (args->arg_count < 2) {
strcpy(message, "This function must have at least 2 arguments");
return true;
- } else if (!IsJson(args, 0)) {
- strcpy(message, "First argument must be a json item");
- return true;
+ //} else if (!IsJson(args, 0)) {
+ // strcpy(message, "First argument must be a json item");
+ // return true;
} else
CalcLen(args, false, reslen, memlen, true);