Commit 1ad5a8df authored by Olivier Bertrand's avatar Olivier Bertrand

Fix memory error when a plain string argument is parsed.

Parsing memory, not added in CalcLen, is added in CheckMemory.
Adding also the file length.
  modified:   storage/connect/jsonudf.cpp
parent 0ec89291
...@@ -28,8 +28,8 @@ tabvct.cpp tabvir.cpp tabxcl.cpp valblk.cpp value.cpp xindex.cpp xobject.cpp ...@@ -28,8 +28,8 @@ tabvct.cpp tabvir.cpp tabxcl.cpp valblk.cpp value.cpp xindex.cpp xobject.cpp
array.h blkfil.h block.h catalog.h checklvl.h colblk.h connect.h csort.h array.h blkfil.h block.h catalog.h checklvl.h colblk.h connect.h csort.h
engmsg.h filamap.h filamdbf.h filamfix.h filamtxt.h filamvct.h filamzip.h engmsg.h filamap.h filamdbf.h filamfix.h filamtxt.h filamvct.h filamzip.h
filter.h global.h ha_connect.h inihandl.h json.h maputil.h msgid.h mycat.h filter.h global.h ha_connect.h inihandl.h json.h jsonudf.h maputil.h msgid.h
myconn.h myutil.h os.h osutil.h plgcnx.h plgdbsem.h preparse.h reldef.h mycat.h myconn.h myutil.h os.h osutil.h plgcnx.h plgdbsem.h preparse.h reldef.h
resource.h tabcol.h tabdos.h tabfix.h tabfmt.h tabjson.h tabmul.h tabmysql.h resource.h tabcol.h tabdos.h tabfix.h tabfmt.h tabjson.h tabmul.h tabmysql.h
taboccur.h tabpivot.h tabsys.h tabtbl.h tabutil.h tabvct.h tabvir.h tabxcl.h taboccur.h tabpivot.h tabsys.h tabtbl.h tabutil.h tabvct.h tabvir.h tabxcl.h
user_connect.h valblk.h value.h xindex.h xobject.h xtable.h) user_connect.h valblk.h value.h xindex.h xobject.h xtable.h)
......
...@@ -1263,7 +1263,7 @@ static int IsJson(UDF_ARGS *args, uint i) ...@@ -1263,7 +1263,7 @@ static int IsJson(UDF_ARGS *args, uint i)
static PGLOBAL GetMemPtr(PGLOBAL g, UDF_ARGS *args, uint i) static PGLOBAL GetMemPtr(PGLOBAL g, UDF_ARGS *args, uint i)
{ {
return (IsJson(args, i) == 3) ? ((PBSON)args->args[i])->G : g; return (IsJson(args, i) == 3) ? ((PBSON)args->args[i])->G : g;
} // end of IsJson } // end of GetMemPtr
/*********************************************************************************/ /*********************************************************************************/
/* GetFileLength: returns file size in number of bytes. */ /* GetFileLength: returns file size in number of bytes. */
...@@ -1408,18 +1408,32 @@ static my_bool CalcLen(UDF_ARGS *args, my_bool obj, ...@@ -1408,18 +1408,32 @@ static my_bool CalcLen(UDF_ARGS *args, my_bool obj,
/*********************************************************************************/ /*********************************************************************************/
/* Check if the calculated memory is enough. */ /* Check if the calculated memory is enough. */
/*********************************************************************************/ /*********************************************************************************/
static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args, static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args, uint n,
uint n, my_bool obj, my_bool mod = false) my_bool m, my_bool obj = false, my_bool mod = false)
{ {
unsigned long rl, ml; unsigned long rl, ml;
my_bool b = false;
n = MY_MIN(n, args->arg_count); n = MY_MIN(n, args->arg_count);
for (uint i = 0; i < n; i++) for (uint i = 0; i < n; i++)
if (IsJson(args, i) == 2) { if (IsJson(args, i) == 2 ||
(b = (m && !i && args->arg_type[0] == STRING_RESULT && !IsJson(args, 0)))) {
if (CalcLen(args, obj, rl, ml, mod)) if (CalcLen(args, obj, rl, ml, mod))
return true; return true;
else if (ml > g->Sarea_Size) { else if (b) {
ulong len;
char *p = args->args[0];
// Is this a file name?
if (!strchr("[{ \t\r\n", *p) && (len = GetFileLength(p)))
ml += len * (M + 1);
else
ml += args->lengths[0] * M;
} // endif b
if (ml > g->Sarea_Size) {
free(g->Sarea); free(g->Sarea);
if (!(g->Sarea = PlugAllocMem(g, ml))) { if (!(g->Sarea = PlugAllocMem(g, ml))) {
...@@ -1794,7 +1808,7 @@ char *json_array_add_values(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1794,7 +1808,7 @@ char *json_array_add_values(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (!g->Xchk) { if (!g->Xchk) {
if (!CheckMemory(g, initid, args, args->arg_count, false)) { if (!CheckMemory(g, initid, args, args->arg_count, true)) {
char *p; char *p;
PJSON top; PJSON top;
PJAR arp; PJAR arp;
...@@ -1878,7 +1892,7 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1878,7 +1892,7 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
goto fin; goto fin;
} // endif Xchk } // endif Xchk
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, false, true)) {
int *x; int *x;
uint n = 2; uint n = 2;
PJSON jsp, top; PJSON jsp, top;
...@@ -1913,7 +1927,7 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1913,7 +1927,7 @@ char *json_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*res_length = 0; *res_length = 0;
*is_null = 1; *is_null = 1;
...@@ -1960,7 +1974,7 @@ char *json_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1960,7 +1974,7 @@ char *json_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
goto fin; goto fin;
} // endif Xchk } // endif Xchk
if (!CheckMemory(g, initid, args, 1, false, true)) { if (!CheckMemory(g, initid, args, 1, false, false, true)) {
int *x; int *x;
uint n = 1; uint n = 1;
PJSON top; PJSON top;
...@@ -1991,7 +2005,7 @@ char *json_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1991,7 +2005,7 @@ char *json_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*error = 1; *error = 1;
...@@ -2025,7 +2039,7 @@ char *json_object(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2025,7 +2039,7 @@ char *json_object(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (!g->Xchk) { if (!g->Xchk) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) { if (!CheckMemory(g, initid, args, args->arg_count, false, false, true)) {
PJOB objp = new(g)JOBJECT; PJOB objp = new(g)JOBJECT;
for (uint i = 0; i < args->arg_count; i++) for (uint i = 0; i < args->arg_count; i++)
...@@ -2070,7 +2084,7 @@ char *json_object_nonull(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2070,7 +2084,7 @@ char *json_object_nonull(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (!g->Xchk) { if (!g->Xchk) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) { if (!CheckMemory(g, initid, args, args->arg_count, false, true)) {
PJVAL jvp; PJVAL jvp;
PJOB objp = new(g)JOBJECT; PJOB objp = new(g)JOBJECT;
...@@ -2121,7 +2135,7 @@ char *json_object_key(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2121,7 +2135,7 @@ char *json_object_key(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (!g->Xchk) { if (!g->Xchk) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) { if (!CheckMemory(g, initid, args, args->arg_count, false, true)) {
PJOB objp = new(g)JOBJECT; PJOB objp = new(g)JOBJECT;
for (uint i = 0; i < args->arg_count; i += 2) for (uint i = 0; i < args->arg_count; i += 2)
...@@ -2161,7 +2175,7 @@ my_bool json_object_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2161,7 +2175,7 @@ my_bool json_object_add_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
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, true, reslen, memlen, true);
return JsonInit(initid, args, message, true, reslen, memlen); return JsonInit(initid, args, message, true, reslen, memlen);
} // end of json_object_add_init } // end of json_object_add_init
...@@ -2178,7 +2192,7 @@ char *json_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2178,7 +2192,7 @@ char *json_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
goto fin; goto fin;
} // endif Xchk } // endif Xchk
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, true, true)) {
PJOB jobp; PJOB jobp;
PJVAL jvp; PJVAL jvp;
PJSON jsp, top; PJSON jsp, top;
...@@ -2210,7 +2224,7 @@ char *json_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2210,7 +2224,7 @@ char *json_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*error = 1; *error = 1;
...@@ -2243,7 +2257,7 @@ my_bool json_object_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2243,7 +2257,7 @@ my_bool json_object_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
strcpy(message, "Second argument must be a key string"); strcpy(message, "Second argument must be a key string");
return true; return true;
} else } else
CalcLen(args, false, reslen, memlen, true); CalcLen(args, true, reslen, memlen, true);
return JsonInit(initid, args, message, true, reslen, memlen); return JsonInit(initid, args, message, true, reslen, memlen);
} // end of json_object_delete_init } // end of json_object_delete_init
...@@ -2260,7 +2274,7 @@ char *json_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2260,7 +2274,7 @@ char *json_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
goto fin; goto fin;
} // endif Xchk } // endif Xchk
if (!CheckMemory(g, initid, args, 1, false, true)) { if (!CheckMemory(g, initid, args, 1, false, true, true)) {
char *key; char *key;
PJOB jobp; PJOB jobp;
PJSON jsp, top; PJSON jsp, top;
...@@ -2290,7 +2304,7 @@ char *json_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2290,7 +2304,7 @@ char *json_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*error = 1; *error = 1;
...@@ -2332,7 +2346,7 @@ char *json_object_list(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2332,7 +2346,7 @@ char *json_object_list(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (!g->N) { if (!g->N) {
if (!CheckMemory(g, initid, args, 1, false)) { if (!CheckMemory(g, initid, args, 1, true, true)) {
char *p; char *p;
PJSON jsp; PJSON jsp;
PJVAL jvp = MakeValue(g, args, 0); PJVAL jvp = MakeValue(g, args, 0);
...@@ -2560,7 +2574,7 @@ char *json_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2560,7 +2574,7 @@ char *json_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result,
goto fin; goto fin;
} // endif Xchk } // endif Xchk
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, false, true)) {
PJSON top; PJSON top;
PJVAL jvp; PJVAL jvp;
PJSON jsp[2] = {NULL, NULL}; PJSON jsp[2] = {NULL, NULL};
...@@ -2595,7 +2609,7 @@ char *json_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2595,7 +2609,7 @@ char *json_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*error = 1; *error = 1;
...@@ -2648,7 +2662,10 @@ my_bool json_get_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2648,7 +2662,10 @@ my_bool json_get_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *json_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result, char *json_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *) unsigned long *res_length, char *is_null, char *)
{ {
char *str = NULL; char *p, *path, *str = NULL;
PJSON jsp;
PJVAL jvp;
PJSNX jsx;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -2657,50 +2674,47 @@ char *json_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2657,50 +2674,47 @@ char *json_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true, true)) {
PJSON jsp; PUSH_WARNING("CheckMemory error");
PJSNX jsx; goto fin;
} else
if (!g->Xchk) { jvp = MakeValue(g, args, 0);
PJVAL jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message);
return NULL;
} // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant if ((p = jvp->GetString())) {
g->Xchk = jsp; if (!(jsp = ParseJson(g, p, strlen(p)))) {
JsonMemSave(g); PUSH_WARNING(g->Message);
} // endif Mrr return NULL;
} // endif jsp
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
path = MakePSZ(g, args, 1); if (g->Mrr) { // First argument is a constant
jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length); g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
if (jsx->SetJpath(g, path, true)) { } else
PUSH_WARNING(g->Message); jsp = (PJSON)g->Xchk;
*is_null = 1;
return NULL;
} // endif SetJpath
jsx->ReadValue(g); path = MakePSZ(g, args, 1);
jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length);
if (!jsx->GetValue()->IsNull()) if (jsx->SetJpath(g, path, true)) {
str = jsx->GetValue()->GetCharValue(); PUSH_WARNING(g->Message);
*is_null = 1;
return NULL;
} // endif SetJpath
if (initid->const_item) jsx->ReadValue(g);
// Keep result of constant function
g->Activityp = (PACTIVITY)str;
} // endif CheckMemory if (!jsx->GetValue()->IsNull())
str = jsx->GetValue()->GetCharValue();
if (initid->const_item)
// Keep result of constant function
g->Activityp = (PACTIVITY)str;
fin: fin:
if (!str) { if (!str) {
...@@ -2762,8 +2776,11 @@ my_bool jsonget_string_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2762,8 +2776,11 @@ my_bool jsonget_string_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *jsonget_string(UDF_INIT *initid, UDF_ARGS *args, char *result, char *jsonget_string(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *) unsigned long *res_length, char *is_null, char *)
{ {
char *p, *path, *str = NULL;
int rc; int rc;
char *str = NULL; PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -2772,67 +2789,64 @@ char *jsonget_string(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2772,67 +2789,64 @@ char *jsonget_string(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { // Save stack and allocation environment and prepare error return
char *p, *path; if (g->jump_level == MAX_JUMP) {
PJSON jsp; PUSH_WARNING(MSG(TOO_MANY_JUMPS));
PJSNX jsx; *is_null = 1;
PJVAL jvp; return NULL;
} // endif jump_level
// Save stack and allocation environment and prepare error return if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
if (g->jump_level == MAX_JUMP) { PUSH_WARNING(g->Message);
PUSH_WARNING(MSG(TOO_MANY_JUMPS)); str = NULL;
*is_null = 1; goto err;
return NULL; } // endif rc
} // endif jump_level
if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) { if (!g->Xchk) {
PUSH_WARNING(g->Message); if (CheckMemory(g, initid, args, 1, true)) {
str = NULL; PUSH_WARNING("CheckMemory error");
goto err; goto err;
} // endif rc } else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
goto err; goto err;
} // endif jsp } // endif jsp
} else } else
jsp = jvp->GetJson(); jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant if (g->Mrr) { // First argument is a constant
g->Xchk = jsp; g->Xchk = jsp;
JsonMemSave(g); JsonMemSave(g);
} // endif Mrr } // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = (PJSON)g->Xchk;
path = MakePSZ(g, args, 1); path = MakePSZ(g, args, 1);
jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length); jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length);
if (jsx->SetJpath(g, path)) { if (jsx->SetJpath(g, path)) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
goto err; goto err;
} // endif SetJpath } // endif SetJpath
jsx->ReadValue(g); jsx->ReadValue(g);
if (!jsx->GetValue()->IsNull()) if (!jsx->GetValue()->IsNull())
str = jsx->GetValue()->GetCharValue(); str = jsx->GetValue()->GetCharValue();
if (initid->const_item) if (initid->const_item)
// Keep result of constant function // Keep result of constant function
g->Activityp = (PACTIVITY)str; g->Activityp = (PACTIVITY)str;
err: err:
g->jump_level--; g->jump_level--;
} // endif CheckMemory
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*res_length = 0; *res_length = 0;
...@@ -2875,6 +2889,11 @@ my_bool jsonget_int_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2875,6 +2889,11 @@ my_bool jsonget_int_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
long long jsonget_int(UDF_INIT *initid, UDF_ARGS *args, long long jsonget_int(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error) char *is_null, char *error)
{ {
char *p, *path;
long long n;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -2887,67 +2906,60 @@ long long jsonget_int(UDF_INIT *initid, UDF_ARGS *args, ...@@ -2887,67 +2906,60 @@ long long jsonget_int(UDF_INIT *initid, UDF_ARGS *args,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true)) {
long long n; PUSH_WARNING("CheckMemory error");
PJSON jsp; if (g->Mrr) *error = 1;
PJSNX jsx; *is_null = 1;
PJVAL jvp; return 0LL;
} else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
if (g->Mrr) *error = 1; if (g->Mrr) *error = 1;
*is_null = 1; *is_null = 1;
return 0; return 0;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
path = MakePSZ(g, args, 1); if (g->Mrr) { // First argument is a constant
jsx = new(g) JSNX(g, jsp, TYPE_BIGINT); g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
if (jsx->SetJpath(g, path)) { } else
PUSH_WARNING(g->Message); jsp = (PJSON)g->Xchk;
*is_null = 1;
return 0;
} // endif SetJpath
jsx->ReadValue(g); path = MakePSZ(g, args, 1);
jsx = new(g) JSNX(g, jsp, TYPE_BIGINT);
if (jsx->GetValue()->IsNull()) { if (jsx->SetJpath(g, path)) {
// PUSH_WARNING("Value not found"); PUSH_WARNING(g->Message);
*is_null = 1; *is_null = 1;
return 0; return 0;
} // endif IsNull } // endif SetJpath
n = jsx->GetValue()->GetBigintValue(); jsx->ReadValue(g);
if (initid->const_item) { if (jsx->GetValue()->IsNull()) {
// Keep result of constant function *is_null = 1;
long long *np = (long long*)PlugSubAlloc(g, NULL, sizeof(long long)); return 0;
*np = n; } // endif IsNull
g->Activityp = (PACTIVITY)np;
} // endif const_item
return n; n = jsx->GetValue()->GetBigintValue();
} // endif CheckMemory
if (g->Mrr) *error = 1; if (initid->const_item) {
*is_null = 1; // Keep result of constant function
return 0LL; long long *np = (long long*)PlugSubAlloc(g, NULL, sizeof(long long));
*np = n;
g->Activityp = (PACTIVITY)np;
} // endif const_item
return n;
} // end of jsonget_int } // end of jsonget_int
void jsonget_int_deinit(UDF_INIT* initid) void jsonget_int_deinit(UDF_INIT* initid)
...@@ -2992,6 +3004,11 @@ my_bool jsonget_real_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2992,6 +3004,11 @@ my_bool jsonget_real_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
double jsonget_real(UDF_INIT *initid, UDF_ARGS *args, double jsonget_real(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error) char *is_null, char *error)
{ {
char *p, *path;
double d;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -3004,66 +3021,59 @@ double jsonget_real(UDF_INIT *initid, UDF_ARGS *args, ...@@ -3004,66 +3021,59 @@ double jsonget_real(UDF_INIT *initid, UDF_ARGS *args,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true)) {
double d; PUSH_WARNING("CheckMemory error");
PJSON jsp; if (g->Mrr) *error = 1;
PJSNX jsx; *is_null = 1;
PJVAL jvp; return 0.0;
} else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
*is_null = 1; *is_null = 1;
return 0.0; return 0.0;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
path = MakePSZ(g, args, 1); if (g->Mrr) { // First argument is a constant
jsx = new(g) JSNX(g, jsp, TYPE_DOUBLE); g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
if (jsx->SetJpath(g, path)) { } else
PUSH_WARNING(g->Message); jsp = (PJSON)g->Xchk;
*is_null = 1;
return 0.0;
} // endif SetJpath
jsx->ReadValue(g); path = MakePSZ(g, args, 1);
jsx = new(g) JSNX(g, jsp, TYPE_DOUBLE);
if (jsx->GetValue()->IsNull()) { if (jsx->SetJpath(g, path)) {
// PUSH_WARNING("Value not found"); PUSH_WARNING(g->Message);
*is_null = 1; *is_null = 1;
return 0.0; return 0.0;
} // endif IsNull } // endif SetJpath
d = jsx->GetValue()->GetFloatValue(); jsx->ReadValue(g);
if (initid->const_item) { if (jsx->GetValue()->IsNull()) {
// Keep result of constant function *is_null = 1;
double *dp = (double*)PlugSubAlloc(g, NULL, sizeof(double)); return 0.0;
*dp = d; } // endif IsNull
g->Activityp = (PACTIVITY)dp;
} // endif const_item
return d; d = jsx->GetValue()->GetFloatValue();
} // endif CheckMemory
if (g->Mrr) *error = 1; if (initid->const_item) {
*is_null = 1; // Keep result of constant function
return 0.0; double *dp = (double*)PlugSubAlloc(g, NULL, sizeof(double));
*dp = d;
g->Activityp = (PACTIVITY)dp;
} // endif const_item
return d;
} // end of jsonget_real } // end of jsonget_real
void jsonget_real_deinit(UDF_INIT* initid) void jsonget_real_deinit(UDF_INIT* initid)
...@@ -3105,7 +3115,11 @@ my_bool jsonlocate_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -3105,7 +3115,11 @@ my_bool jsonlocate_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *jsonlocate(UDF_INIT *initid, UDF_ARGS *args, char *result, char *jsonlocate(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
char *path = NULL; char *p, *path = NULL;
int k, rc;
PJVAL jvp, jvp2;
PJSON jsp;
PJSNX jsx;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -3122,75 +3136,68 @@ char *jsonlocate(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3122,75 +3136,68 @@ char *jsonlocate(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { // Save stack and allocation environment and prepare error return
char *p; if (g->jump_level == MAX_JUMP) {
int k, rc; PUSH_WARNING(MSG(TOO_MANY_JUMPS));
PJVAL jvp, jvp2; *error = 1;
PJSON jsp; *is_null = 1;
PJSNX jsx; return NULL;
} // endif jump_level
// Save stack and allocation environment and prepare error return if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
if (g->jump_level == MAX_JUMP) { PUSH_WARNING(g->Message);
PUSH_WARNING(MSG(TOO_MANY_JUMPS)); *error = 1;
*error = 1; path = NULL;
*is_null = 1; goto err;
return NULL; } // endif rc
} // endif jump_level
if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) { if (!g->Xchk) {
PUSH_WARNING(g->Message); if (CheckMemory(g, initid, args, 1, !g->Xchk)) {
PUSH_WARNING("CheckMemory error");
*error = 1; *error = 1;
path = NULL;
goto err; goto err;
} // endif rc } else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
goto err; goto err;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
// The item to locate if (g->Mrr) { // First argument is a constant
jvp2 = MakeValue(g, args, 1); g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
k = (args->arg_count > 2) ? (int)*(long long*)args->args[2] : 1; } else
jsp = (PJSON)g->Xchk;
jsx = new(g) JSNX(g, jsp, TYPE_STRING); // The item to locate
path = jsx->Locate(g, jsp, jvp2, k); jvp2 = MakeValue(g, args, 1);
if (initid->const_item) k = (args->arg_count > 2) ? (int)*(long long*)args->args[2] : 1;
// Keep result of constant function
g->Activityp = (PACTIVITY)path;
err: jsx = new(g) JSNX(g, jsp, TYPE_STRING);
g->jump_level--; path = jsx->Locate(g, jsp, jvp2, k);
if (!path) { if (initid->const_item)
*res_length = 0; // Keep result of constant function
*is_null = 1; g->Activityp = (PACTIVITY)path;
} else
*res_length = strlen(path);
return path; err:
} // endif CheckMemory g->jump_level--;
*error = 1; if (!path) {
*is_null = 1; *res_length = 0;
return NULL; *is_null = 1;
} else
*res_length = strlen(path);
return path;
} // end of jsonlocate } // end of jsonlocate
void jsonlocate_deinit(UDF_INIT* initid) void jsonlocate_deinit(UDF_INIT* initid)
...@@ -3232,7 +3239,11 @@ my_bool json_locate_all_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -3232,7 +3239,11 @@ my_bool json_locate_all_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *json_locate_all(UDF_INIT *initid, UDF_ARGS *args, char *result, char *json_locate_all(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
char *path = NULL; char *p, *path = NULL;
int rc, mx = 10;
PJVAL jvp, jvp2;
PJSON jsp;
PJSNX jsx;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -3250,76 +3261,69 @@ char *json_locate_all(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3250,76 +3261,69 @@ char *json_locate_all(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { // Save stack and allocation environment and prepare error return
char *p; if (g->jump_level == MAX_JUMP) {
int rc, mx = 10; PUSH_WARNING(MSG(TOO_MANY_JUMPS));
PJVAL jvp, jvp2; *error = 1;
PJSON jsp; *is_null = 1;
PJSNX jsx; return NULL;
} // endif jump_level
// Save stack and allocation environment and prepare error return if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
if (g->jump_level == MAX_JUMP) { PUSH_WARNING(g->Message);
PUSH_WARNING(MSG(TOO_MANY_JUMPS)); *error = 1;
*error = 1; path = NULL;
*is_null = 1; goto err;
return NULL; } // endif rc
} // endif jump_level
if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) { if (!g->Xchk) {
PUSH_WARNING(g->Message); if (CheckMemory(g, initid, args, 1, true)) {
PUSH_WARNING("CheckMemory error");
*error = 1; *error = 1;
path = NULL;
goto err; goto err;
} // endif rc } else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
goto err; goto err;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
// The item to locate } else
jvp2 = MakeValue(g, args, 1); jsp = (PJSON)g->Xchk;
if (args->arg_count > 2) // The item to locate
mx = (int)*(long long*)args->args[2]; jvp2 = MakeValue(g, args, 1);
jsx = new(g) JSNX(g, jsp, TYPE_STRING); if (args->arg_count > 2)
path = jsx->LocateAll(g, jsp, jvp2, mx); mx = (int)*(long long*)args->args[2];
if (initid->const_item) jsx = new(g) JSNX(g, jsp, TYPE_STRING);
// Keep result of constant function path = jsx->LocateAll(g, jsp, jvp2, mx);
g->Activityp = (PACTIVITY)path;
err: if (initid->const_item)
g->jump_level--; // Keep result of constant function
g->Activityp = (PACTIVITY)path;
if (!path) { err:
*res_length = 0; g->jump_level--;
*is_null = 1;
} else
*res_length = strlen(path);
return path; if (!path) {
} // endif CheckMemory *res_length = 0;
*is_null = 1;
} else
*res_length = strlen(path);
*error = 1; return path;
*is_null = 1;
return NULL;
} // end of json_locate_all } // end of json_locate_all
void json_locate_all_deinit(UDF_INIT* initid) void json_locate_all_deinit(UDF_INIT* initid)
...@@ -3416,6 +3420,11 @@ my_bool jsoncontains_path_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -3416,6 +3420,11 @@ my_bool jsoncontains_path_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
long long jsoncontains_path(UDF_INIT *initid, UDF_ARGS *args, char *result, long long jsoncontains_path(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
char *p, *path;
long long n;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
...@@ -3428,56 +3437,50 @@ long long jsoncontains_path(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3428,56 +3437,50 @@ long long jsoncontains_path(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true)) {
long long n; PUSH_WARNING("CheckMemory error");
PJSON jsp; goto err;
PJSNX jsx; } else
PJVAL jvp;
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
if (g->Mrr) *error = 1; goto err;
*is_null = 1; } // endif jsp
return 0;
} // endif jsp
} else } else
jsp = jvp->GetJson(); jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant if (g->Mrr) { // First argument is a constant
g->Xchk = jsp; g->Xchk = jsp;
JsonMemSave(g); JsonMemSave(g);
} // endif Mrr } // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = (PJSON)g->Xchk;
path = MakePSZ(g, args, 1); path = MakePSZ(g, args, 1);
jsx = new(g)JSNX(g, jsp, TYPE_BIGINT); jsx = new(g)JSNX(g, jsp, TYPE_BIGINT);
if (jsx->SetJpath(g, path)) { if (jsx->SetJpath(g, path)) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
*is_null = 1; goto err;
return 0; } // endif SetJpath
} // endif SetJpath
n = (jsx->CheckPath(g)) ? 1LL : 0LL; n = (jsx->CheckPath(g)) ? 1LL : 0LL;
if (initid->const_item) { if (initid->const_item) {
// Keep result of constant function // Keep result of constant function
long long *np = (long long*)PlugSubAlloc(g, NULL, sizeof(long long)); long long *np = (long long*)PlugSubAlloc(g, NULL, sizeof(long long));
*np = n; *np = n;
g->Activityp = (PACTIVITY)np; g->Activityp = (PACTIVITY)np;
} // endif const_item } // endif const_item
return n; return n;
} // endif CheckMemory
err:
if (g->Mrr) *error = 1; if (g->Mrr) *error = 1;
*is_null = 1; *is_null = 1;
return 0LL; return 0LL;
...@@ -3522,10 +3525,14 @@ my_bool json_set_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -3522,10 +3525,14 @@ my_bool json_set_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *json_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result, char *json_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
char *p, *path, *str = NULL;
int w, rc; int w, rc;
my_bool b = true; my_bool b = true;
char *str = NULL; PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
PGLOBAL gb = GetMemPtr(g, args, 0);
if (g->N) { if (g->N) {
str = (char*)g->Activityp; str = (char*)g->Activityp;
...@@ -3540,81 +3547,77 @@ char *json_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3540,81 +3547,77 @@ char *json_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
else else
w = 0; w = 0;
if (!CheckMemory(g, initid, args, 1, false)) { // Save stack and allocation environment and prepare error return
char *p, *path; if (g->jump_level == MAX_JUMP) {
PJSON jsp; PUSH_WARNING(MSG(TOO_MANY_JUMPS));
PJSNX jsx; *error = 1;
PJVAL jvp; goto fin;
PGLOBAL gb = GetMemPtr(g, args, 0); } // endif jump_level
// Save stack and allocation environment and prepare error return if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
if (g->jump_level == MAX_JUMP) { PUSH_WARNING(g->Message);
PUSH_WARNING(MSG(TOO_MANY_JUMPS)); str = NULL;
*is_null = 1; goto err;
return NULL; } // endif rc
} // endif jump_level
if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) { if (!g->Xchk) {
PUSH_WARNING(g->Message); if (CheckMemory(g, initid, args, 1, true, false, true)) {
str = NULL; PUSH_WARNING("CheckMemory error");
goto err; goto err;
} // endif rc } else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
goto err; goto err;
} // endif jsp } // endif jsp
} else } else
jsp = jvp->GetJson(); jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant if (g->Mrr) { // First argument is a constant
g->Xchk = jsp; g->Xchk = jsp;
JsonMemSave(g); JsonMemSave(g);
} // endif Mrr } // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = (PJSON)g->Xchk;
jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length, 0, true); jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length, 0, true);
for (uint i = 1; i+1 < args->arg_count; i += 2) { for (uint i = 1; i+1 < args->arg_count; i += 2) {
jvp = MakeValue(gb, args, i); jvp = MakeValue(gb, args, i);
path = MakePSZ(g, args, i+1); path = MakePSZ(g, args, i+1);
if (jsx->SetJpath(g, path, false)) { if (jsx->SetJpath(g, path, false)) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
continue; continue;
} // endif SetJpath } // endif SetJpath
if (w) { if (w) {
jsx->ReadValue(g); jsx->ReadValue(g);
b = jsx->GetValue()->IsNull(); b = jsx->GetValue()->IsNull();
b = (w == 1) ? b : !b; b = (w == 1) ? b : !b;
} // endif w } // endif w
if (b && jsx->WriteValue(gb, jvp)) if (b && jsx->WriteValue(gb, jvp))
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
} // endfor i } // endfor i
// In case of error or file, return unchanged argument // In case of error or file, return unchanged argument
if (!(str = MakeResult(g, args, jsp, INT_MAX32))) if (!(str = MakeResult(g, args, jsp, INT_MAX32)))
str = MakePSZ(g, args, 0); str = MakePSZ(g, args, 0);
if (initid->const_item) if (initid->const_item)
// Keep result of constant function // Keep result of constant function
g->Activityp = (PACTIVITY)str; g->Activityp = (PACTIVITY)str;
err: err:
g->jump_level--; g->jump_level--;
} // endif CheckMemory
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*res_length = 0; *res_length = 0;
...@@ -3770,7 +3773,7 @@ char *json_file(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3770,7 +3773,7 @@ char *json_file(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = str; g->Xchk = str;
fin: fin:
if (!str) { if (!str) {
*res_length = 0; *res_length = 0;
*is_null = 1; *is_null = 1;
...@@ -3807,7 +3810,7 @@ my_bool jfile_make_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -3807,7 +3810,7 @@ my_bool jfile_make_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result, char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *) unsigned long *res_length, char *is_null, char *)
{ {
char *p, *str, *msg, *fn = NULL; char *p, *msg, *str = NULL, *fn = NULL;
int n, pretty = 2; int n, pretty = 2;
PJSON jsp; PJSON jsp;
PJVAL jvp; PJVAL jvp;
...@@ -3819,8 +3822,6 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3819,8 +3822,6 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
PlugSubSet(g, g->Sarea, g->Sarea_Size);
if ((n = IsJson(args, 0)) == 3) { if ((n = IsJson(args, 0)) == 3) {
// Get default file name and pretty // Get default file name and pretty
PBSON bsp = (PBSON)args->args[0]; PBSON bsp = (PBSON)args->args[0];
...@@ -3831,14 +3832,18 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3831,14 +3832,18 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result,
fn = args->args[0]; fn = args->args[0];
if (!g->Xchk) { if (!g->Xchk) {
jvp = MakeValue(g, args, 0); if (CheckMemory(g, initid, args, 1, true)) {
PUSH_WARNING("CheckMemory error");
goto fin;
} else
jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!strchr("[{ \t\r\n", *p)) { if (!strchr("[{ \t\r\n", *p)) {
// Is this a file name? // Is this a file name?
if (!(p = GetJsonFile(g, p))) { if (!(p = GetJsonFile(g, p))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
return NULL; goto fin;
} else } else
fn = jvp->GetString(); fn = jvp->GetString();
...@@ -3846,7 +3851,7 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3846,7 +3851,7 @@ char *jfile_make(UDF_INIT *initid, UDF_ARGS *args, char *result,
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
return NULL; goto fin;
} // endif jsp } // endif jsp
jvp->SetValue(jsp); jvp->SetValue(jsp);
...@@ -3965,7 +3970,7 @@ char *jbin_array_add_values(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -3965,7 +3970,7 @@ char *jbin_array_add_values(UDF_INIT *initid, UDF_ARGS *args, char *result,
PBSON bsp = (PBSON)g->Xchk; PBSON bsp = (PBSON)g->Xchk;
if (!bsp || bsp->Changed) { if (!bsp || bsp->Changed) {
if (!CheckMemory(g, initid, args, args->arg_count, false)) { if (!CheckMemory(g, initid, args, args->arg_count, true)) {
char *p; char *p;
PJSON top; PJSON top;
PJAR arp; PJAR arp;
...@@ -4042,7 +4047,7 @@ char *jbin_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4042,7 +4047,7 @@ char *jbin_array_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
return (char*)bsp; return (char*)bsp;
} // endif bsp } // endif bsp
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, false, true)) {
int *x = NULL; int *x = NULL;
uint n = 2; uint n = 2;
// PJSON jsp; // PJSON jsp;
...@@ -4111,7 +4116,7 @@ char *jbin_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4111,7 +4116,7 @@ char *jbin_array_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
return (char*)bsp; return (char*)bsp;
} // endif bsp } // endif bsp
if (!CheckMemory(g, initid, args, 1, false, true)) { if (!CheckMemory(g, initid, args, 1, false, false, true)) {
int *x; int *x;
uint n = 1; uint n = 1;
PJAR arp; PJAR arp;
...@@ -4224,7 +4229,7 @@ char *jbin_object_nonull(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4224,7 +4229,7 @@ char *jbin_object_nonull(UDF_INIT *initid, UDF_ARGS *args, char *result,
PBSON bsp = (PBSON)g->Xchk; PBSON bsp = (PBSON)g->Xchk;
if (!bsp || bsp->Changed) { if (!bsp || bsp->Changed) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) { if (!CheckMemory(g, initid, args, args->arg_count, false, true)) {
PJVAL jvp; PJVAL jvp;
PJOB objp = new(g)JOBJECT; PJOB objp = new(g)JOBJECT;
...@@ -4281,7 +4286,7 @@ char *jbin_object_key(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4281,7 +4286,7 @@ char *jbin_object_key(UDF_INIT *initid, UDF_ARGS *args, char *result,
PBSON bsp = (PBSON)g->Xchk; PBSON bsp = (PBSON)g->Xchk;
if (!bsp || bsp->Changed) { if (!bsp || bsp->Changed) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) { if (!CheckMemory(g, initid, args, args->arg_count, false, true)) {
PJOB objp = new(g)JOBJECT; PJOB objp = new(g)JOBJECT;
for (uint i = 0; i < args->arg_count; i += 2) for (uint i = 0; i < args->arg_count; i += 2)
...@@ -4335,7 +4340,7 @@ char *jbin_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4335,7 +4340,7 @@ char *jbin_object_add(UDF_INIT *initid, UDF_ARGS *args, char *result,
return (char*)bsp; return (char*)bsp;
} // endif bsp } // endif bsp
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, true, true)) {
char *key; char *key;
PJOB jobp; PJOB jobp;
PJVAL jvp = MakeValue(g, args, 0, &top); PJVAL jvp = MakeValue(g, args, 0, &top);
...@@ -4401,7 +4406,7 @@ char *jbin_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4401,7 +4406,7 @@ char *jbin_object_delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
return (char*)bsp; return (char*)bsp;
} // endif bsp } // endif bsp
if (!CheckMemory(g, initid, args, 1, false, true)) { if (!CheckMemory(g, initid, args, 1, false, true, true)) {
char *key; char *key;
PJOB jobp; PJOB jobp;
PJVAL jvp = MakeValue(g, args, 0, &top); PJVAL jvp = MakeValue(g, args, 0, &top);
...@@ -4458,7 +4463,7 @@ char *jbin_object_list(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4458,7 +4463,7 @@ char *jbin_object_list(UDF_INIT *initid, UDF_ARGS *args, char *result,
PBSON bsp = (PBSON)g->Xchk; PBSON bsp = (PBSON)g->Xchk;
if (!bsp || bsp->Changed) { if (!bsp || bsp->Changed) {
if (!CheckMemory(g, initid, args, 1, false)) { if (!CheckMemory(g, initid, args, 1, true, true)) {
char *p; char *p;
PJSON jsp; PJSON jsp;
PJVAL jvp = MakeValue(g, args, 0); PJVAL jvp = MakeValue(g, args, 0);
...@@ -4514,8 +4519,12 @@ my_bool jbin_get_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -4514,8 +4519,12 @@ my_bool jbin_get_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *jbin_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result, char *jbin_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
PGLOBAL g = (PGLOBAL)initid->ptr; char *p, *path;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PBSON bsp = NULL; PBSON bsp = NULL;
PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) { if (g->N) {
bsp = (PBSON)g->Activityp; bsp = (PBSON)g->Activityp;
...@@ -4523,59 +4532,54 @@ char *jbin_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4523,59 +4532,54 @@ char *jbin_get_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
} else if (initid->const_item) } else if (initid->const_item)
g->N = 1; g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true, true)) {
PJSON jsp; PUSH_WARNING("CheckMemory error");
PJSNX jsx; goto fin;
PJVAL jvp; } else
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
return NULL; goto fin;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
path = MakePSZ(g, args, 1); if (g->Mrr) { // First argument is a constant
jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length); g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
if (jsx->SetJpath(g, path, false)) { } else
PUSH_WARNING(g->Message); jsp = (PJSON)g->Xchk;
*is_null = 1;
return NULL;
} // endif SetJpath
// Get the json tree path = MakePSZ(g, args, 1);
if ((jvp = jsx->GetRowValue(g, jsp, 0, false))) { jsx = new(g) JSNX(g, jsp, TYPE_STRING, initid->max_length);
jsp = (jvp->GetJsp()) ? jvp->GetJsp() : new(g) JVALUE(g, jvp->GetValue());
if ((bsp = JbinAlloc(g, args, initid->max_length, jsp))) if (jsx->SetJpath(g, path, false)) {
strcat(bsp->Msg, " item"); PUSH_WARNING(g->Message);
else goto fin;
*error = 1; } // endif SetJpath
} // endif jvp // Get the json tree
if ((jvp = jsx->GetRowValue(g, jsp, 0, false))) {
jsp = (jvp->GetJsp()) ? jvp->GetJsp() : new(g) JVALUE(g, jvp->GetValue());
if (initid->const_item) if ((bsp = JbinAlloc(g, args, initid->max_length, jsp)))
// Keep result of constant function strcat(bsp->Msg, " item");
g->Activityp = (PACTIVITY)bsp; else
*error = 1;
} // endif CheckMemory } // endif jvp
fin: if (initid->const_item)
// Keep result of constant function
g->Activityp = (PACTIVITY)bsp;
fin:
if (!bsp) { if (!bsp) {
*is_null = 1; *is_null = 1;
*res_length = 0; *res_length = 0;
...@@ -4611,7 +4615,7 @@ char *jbin_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4611,7 +4615,7 @@ char *jbin_item_merge(UDF_INIT *initid, UDF_ARGS *args, char *result,
return (char*)bsp; return (char*)bsp;
} // endif bsp } // endif bsp
if (!CheckMemory(g, initid, args, 2, false, true)) { if (!CheckMemory(g, initid, args, 2, false, false, true)) {
PJVAL jvp; PJVAL jvp;
PJSON jsp[2] = {NULL, NULL}; PJSON jsp[2] = {NULL, NULL};
PGLOBAL gb = GetMemPtr(g, args, 0); PGLOBAL gb = GetMemPtr(g, args, 0);
...@@ -4666,10 +4670,15 @@ my_bool jbin_set_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -4666,10 +4670,15 @@ my_bool jbin_set_item_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
char *jbin_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result, char *jbin_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error) unsigned long *res_length, char *is_null, char *error)
{ {
char *p, *path;
int w; int w;
my_bool b = true; my_bool b = true;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
PBSON bsp = NULL; PBSON bsp = NULL;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
PGLOBAL gb = GetMemPtr(g, args, 0);
if (g->N) { if (g->N) {
bsp = (PBSON)g->Activityp; bsp = (PBSON)g->Activityp;
...@@ -4684,65 +4693,59 @@ char *jbin_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4684,65 +4693,59 @@ char *jbin_set_item(UDF_INIT *initid, UDF_ARGS *args, char *result,
else else
w = 0; w = 0;
if (!CheckMemory(g, initid, args, 1, false)) { if (!g->Xchk) {
char *p, *path; if (CheckMemory(g, initid, args, 1, true, false, true)) {
PJSON jsp; PUSH_WARNING("CheckMemory error");
PJSNX jsx; } else
PJVAL jvp;
PGLOBAL gb = GetMemPtr(g, args, 0);
if (!g->Xchk) {
jvp = MakeValue(g, args, 0); jvp = MakeValue(g, args, 0);
if ((p = jvp->GetString())) { if ((p = jvp->GetString())) {
if (!(jsp = ParseJson(g, p, strlen(p)))) { if (!(jsp = ParseJson(g, p, strlen(p)))) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
return NULL; goto fin;
} // endif jsp } // endif jsp
} else
jsp = jvp->GetJson();
if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else } else
jsp = (PJSON)g->Xchk; jsp = jvp->GetJson();
jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length, 0, true); if (g->Mrr) { // First argument is a constant
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
for (uint i = 1; i+1 < args->arg_count; i += 2) { } else
jvp = MakeValue(gb, args, i); jsp = (PJSON)g->Xchk;
path = MakePSZ(g, args, i+1);
if (jsx->SetJpath(g, path, false)) { jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length, 0, true);
PUSH_WARNING(g->Message);
continue;
} // endif SetJpath
if (w) { for (uint i = 1; i+1 < args->arg_count; i += 2) {
jsx->ReadValue(g); jvp = MakeValue(gb, args, i);
b = jsx->GetValue()->IsNull(); path = MakePSZ(g, args, i+1);
b = (w == 1) ? b : !b;
} // endif w
if (b && jsx->WriteValue(gb, jvp)) if (jsx->SetJpath(g, path, false)) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
continue;
} // endif SetJpath
} // endfor i if (w) {
jsx->ReadValue(g);
b = jsx->GetValue()->IsNull();
b = (w == 1) ? b : !b;
} // endif w
if (!(bsp = MakeBinResult(g, args, jsp, initid->max_length, INT_MAX32))) if (b && jsx->WriteValue(gb, jvp))
*error = 1; PUSH_WARNING(g->Message);
if (initid->const_item) } // endfor i
// Keep result of constant function
g->Activityp = (PACTIVITY)bsp;
} // endif CheckMemory if (!(bsp = MakeBinResult(g, args, jsp, initid->max_length, INT_MAX32)))
*error = 1;
fin: if (initid->const_item)
// Keep result of constant function
g->Activityp = (PACTIVITY)bsp;
fin:
if (!bsp) { if (!bsp) {
*is_null = 1; *is_null = 1;
*res_length = 0; *res_length = 0;
...@@ -4889,7 +4892,7 @@ char *jbin_file(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -4889,7 +4892,7 @@ char *jbin_file(UDF_INIT *initid, UDF_ARGS *args, char *result,
// Keep result of constant function // Keep result of constant function
g->Xchk = bsp; g->Xchk = bsp;
fin: fin:
if (!bsp) { if (!bsp) {
*res_length = 0; *res_length = 0;
*is_null = 1; *is_null = 1;
......
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