Commit 18e6a81b authored by Olivier Bertrand's avatar Olivier Bertrand

- 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
parent 46f3e320
...@@ -1224,6 +1224,7 @@ PJVAL JARRAY::AddValue(PGLOBAL g, PJVAL jvp, int *x) ...@@ -1224,6 +1224,7 @@ PJVAL JARRAY::AddValue(PGLOBAL g, PJVAL jvp, int *x)
Last->Next = jvp; Last->Next = jvp;
Last = jvp; Last = jvp;
Last->Next = NULL;
} // endif x } // endif x
return jvp; return jvp;
...@@ -1318,6 +1319,24 @@ bool JARRAY::IsNull(void) ...@@ -1318,6 +1319,24 @@ bool JARRAY::IsNull(void)
/* -------------------------- Class JVALUE- -------------------------- */ /* -------------------------- 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. */ /* Constructor for a Value with a given string or numeric value. */
/***********************************************************************/ /***********************************************************************/
......
...@@ -258,8 +258,7 @@ class JVALUE : public JSON { ...@@ -258,8 +258,7 @@ class JVALUE : public JSON {
friend bool SerializeValue(JOUT *, PJVAL); friend bool SerializeValue(JOUT *, PJVAL);
public: public:
JVALUE(void) : JSON() {Clear();} JVALUE(void) : JSON() {Clear();}
JVALUE(PJSON jsp) : JSON() JVALUE(PJSON jsp);
{Jsp = jsp; Value = NULL; Next = NULL; Del = false; Size = 1;}
JVALUE(PGLOBAL g, PVAL valp); JVALUE(PGLOBAL g, PVAL valp);
JVALUE(PGLOBAL g, PCSZ strp); JVALUE(PGLOBAL g, PCSZ strp);
......
...@@ -1759,6 +1759,41 @@ static PJVAL MakeValue(PGLOBAL g, UDF_ARGS *args, uint i, PJSON *top = NULL) ...@@ -1759,6 +1759,41 @@ static PJVAL MakeValue(PGLOBAL g, UDF_ARGS *args, uint i, PJSON *top = NULL)
return jvp; return jvp;
} // end of MakeValue } // 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. */ /* Make a Json value containing the parameter. */
/*********************************************************************************/ /*********************************************************************************/
...@@ -1953,9 +1988,9 @@ my_bool json_array_add_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) { if (args->arg_count < 2) {
strcpy(message, "This function must have at least 2 arguments"); strcpy(message, "This function must have at least 2 arguments");
return true; return true;
} else if (!IsJson(args, 0)) { //} else if (!IsJson(args, 0)) {
strcpy(message, "First argument must be a json item"); // strcpy(message, "First argument must be a json item");
return true; // return true;
} else } else
CalcLen(args, false, reslen, memlen, true); CalcLen(args, false, reslen, memlen, true);
...@@ -1994,22 +2029,32 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1994,22 +2029,32 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
PJVAL jvp; PJVAL jvp;
PJAR arp; PJAR arp;
jvp = MakeValue(g, args, 0, &top); jvp = MakeTypedValue(g, args, 0, TYPE_JSON, &top);
jsp = jvp->GetJson(); jsp = jvp->GetJson();
x = GetIntArgPtr(g, args, n); x = GetIntArgPtr(g, args, n);
if (CheckPath(g, args, jsp, jvp, 2)) if (CheckPath(g, args, jsp, jvp, 2))
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
else if (jvp && jvp->GetValType() == TYPE_JAR) { else if (jvp) {
PGLOBAL gb = GetMemPtr(g, args, 0); 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->AddValue(gb, MakeValue(gb, args, 1), x);
arp->InitArray(gb); arp->InitArray(gb);
str = MakeResult(g, args, top, n); str = MakeResult(g, args, top, n);
} else { } else {
PUSH_WARNING("First argument target is not an array"); PUSH_WARNING("Target is not an array");
// if (g->Mrr) *error = 1; (only if no path) // if (g->Mrr) *error = 1; (only if no path)
} // endif jvp } // endif jvp
} // endif CheckMemory } // endif CheckMemory
...@@ -4458,9 +4503,9 @@ my_bool jbin_array_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -4458,9 +4503,9 @@ my_bool jbin_array_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
if (args->arg_count < 2) { if (args->arg_count < 2) {
strcpy(message, "This function must have at least 2 arguments"); strcpy(message, "This function must have at least 2 arguments");
return true; return true;
} else if (!IsJson(args, 0)) { //} else if (!IsJson(args, 0)) {
strcpy(message, "First argument must be a json item"); // strcpy(message, "First argument must be a json item");
return true; // return true;
} else } else
CalcLen(args, false, reslen, memlen, true); CalcLen(args, false, reslen, memlen, true);
......
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