Commit 8a154ecd authored by Olivier Bertrand's avatar Olivier Bertrand

Add Json_Get_Item (and Bson_File?) functions

  modified:   storage/connect/jsonudf.cpp
  modified:   storage/connect/jsonudf.h
parent 7915abff
...@@ -56,13 +56,25 @@ JSNX::JSNX(PGLOBAL g, PJSON row, int type, int len, int prec) ...@@ -56,13 +56,25 @@ JSNX::JSNX(PGLOBAL g, PJSON row, int type, int len, int prec)
/*********************************************************************************/ /*********************************************************************************/
/* SetJpath: set and parse the json path. */ /* SetJpath: set and parse the json path. */
/*********************************************************************************/ /*********************************************************************************/
my_bool JSNX::SetJpath(PGLOBAL g, char *path) my_bool JSNX::SetJpath(PGLOBAL g, char *path, my_bool jb)
{ {
// Check Value was allocated // Check Value was allocated
if (!Value) if (!Value)
return true; return true;
Value->SetNullable(true); Value->SetNullable(true);
if (jb) {
// Path must return a Json item
size_t n = strlen(path);
if (!n || path[n - 1] != '*') {
Jpath = (char*)PlugSubAlloc(g, NULL, n + 3);
strcat(strcpy(Jpath, path), (n) ? ":*" : "*");
} else
Jpath = path;
} else
Jpath = path; Jpath = path;
// Parse the json path // Parse the json path
...@@ -780,7 +792,8 @@ typedef struct BSON *PBSON; ...@@ -780,7 +792,8 @@ typedef struct BSON *PBSON;
/*********************************************************************************/ /*********************************************************************************/
struct BSON { struct BSON {
char Msg[140]; char Msg[140];
void *Memp; char *Filename;
int Pretty;
ulong Reslen; ulong Reslen;
my_bool Changed; my_bool Changed;
PJSON Jsp; PJSON Jsp;
...@@ -795,7 +808,8 @@ static PBSON BsonAlloc(PGLOBAL g, ulong len, PJSON jsp) ...@@ -795,7 +808,8 @@ static PBSON BsonAlloc(PGLOBAL g, ulong len, PJSON jsp)
strcpy(bsp->Msg, "Binary Json"); strcpy(bsp->Msg, "Binary Json");
bsp->Msg[139] = 0; bsp->Msg[139] = 0;
bsp->Memp = g->Sarea; bsp->Filename = NULL;
bsp->Pretty = 2;
bsp->Reslen = len; bsp->Reslen = len;
bsp->Changed = false; bsp->Changed = false;
bsp->Jsp = jsp; bsp->Jsp = jsp;
...@@ -825,13 +839,10 @@ inline void JsonMemSave(PGLOBAL g) ...@@ -825,13 +839,10 @@ inline void JsonMemSave(PGLOBAL g)
/*********************************************************************************/ /*********************************************************************************/
/* Program for freeing the memory pools. */ /* Program for freeing the memory pools. */
/*********************************************************************************/ /*********************************************************************************/
inline void JsonMemFree(PGLOBAL g) inline void JsonFreeMem(PGLOBAL g)
{ {
if (g->Alchecked)
g->Sarea = NULL;
PlugExit(g); PlugExit(g);
} /* end of JsonMemFree */ } /* end of JsonFreeMem */
/*********************************************************************************/ /*********************************************************************************/
/* Allocate and initialise the memory area. */ /* Allocate and initialise the memory area. */
...@@ -846,13 +857,9 @@ static my_bool JsonInit(UDF_INIT *initid, UDF_ARGS *args, ...@@ -846,13 +857,9 @@ static my_bool JsonInit(UDF_INIT *initid, UDF_ARGS *args,
strcpy(message, "Allocation error"); strcpy(message, "Allocation error");
return true; return true;
} else if (g->Sarea_Size == 0) { } else if (g->Sarea_Size == 0) {
if (memlen) {
strcpy(message, g->Message); strcpy(message, g->Message);
PlugExit(g); PlugExit(g);
return true; return true;
} else
assert(IsJson(args, 0) == 3);
} // endif g } // endif g
g->Mrr = (args->arg_count && args->args[0]) ? 1 : 0; g->Mrr = (args->arg_count && args->args[0]) ? 1 : 0;
...@@ -909,14 +916,23 @@ static char *MakeResult(PGLOBAL g, UDF_ARGS *args, PJSON top, int n = 2) ...@@ -909,14 +916,23 @@ static char *MakeResult(PGLOBAL g, UDF_ARGS *args, PJSON top, int n = 2)
PUSH_WARNING(msg); PUSH_WARNING(msg);
str = NULL; str = NULL;
} else { } else if (IsJson(args, 0) == 3) {
if (!(str = Serialize(g, top, NULL, 0))) PBSON bsp = (PBSON)args->args[0];
PUSH_WARNING(g->Message);
if (bsp->Filename) {
// Make the change in the json file
char *msg;
if (IsJson(args, 0) == 3) if ((msg = Serialize(g, top, bsp->Filename, bsp->Pretty)))
((PBSON)args->args[0])->Changed = 1; PUSH_WARNING(msg);
} // endif IsJson str = bsp->Filename;
} else if (!(str = Serialize(g, top, NULL, 0)))
PUSH_WARNING(g->Message);
bsp->Changed = 1;
} else if (!(str = Serialize(g, top, NULL, 0)))
PUSH_WARNING(g->Message);
return str; return str;
} // end of MakeResult } // end of MakeResult
...@@ -1025,10 +1041,7 @@ static my_bool CalcLen(UDF_ARGS *args, my_bool obj, ...@@ -1025,10 +1041,7 @@ static my_bool CalcLen(UDF_ARGS *args, my_bool obj,
} // endfor i } // endfor i
if (IsJson(args, 0) == 3) { // Calculate the amount of memory needed
memlen = 0L; // We'll use arg memory
return false;
} else // Calculate the amount of memory needed
memlen = MEMFIX + sizeof(JOUTSTR) + reslen; memlen = MEMFIX + sizeof(JOUTSTR) + reslen;
for (i = 0; i < args->arg_count; i++) { for (i = 0; i < args->arg_count; i++) {
...@@ -1090,18 +1103,6 @@ static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args, ...@@ -1090,18 +1103,6 @@ static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args,
{ {
unsigned long rl, ml; unsigned long rl, ml;
if (IsJson(args, 0) == 3) {
if (!g->Sarea) {
PPOOLHEADER p = (PPOOLHEADER)((PBSON)args->args[0])->Memp;
g->Sarea = p;
g->Sarea_Size = p->To_Free + p->FreeBlk;
g->Alchecked = 1;
} // endif Sarea
return false;
} // endif IsJson
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++)
...@@ -1338,7 +1339,7 @@ char *Json_Value(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1338,7 +1339,7 @@ char *Json_Value(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Value_deinit(UDF_INIT* initid) void Json_Value_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Value_deinit } // end of Json_Value_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1384,7 +1385,7 @@ char *Json_Array(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1384,7 +1385,7 @@ char *Json_Array(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Array_deinit(UDF_INIT* initid) void Json_Array_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Array_deinit } // end of Json_Array_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1451,7 +1452,7 @@ char *Json_Array_Add_Values(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1451,7 +1452,7 @@ char *Json_Array_Add_Values(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Array_Add_Values_deinit(UDF_INIT* initid) void Json_Array_Add_Values_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Array_Add_Values_deinit } // end of Json_Array_Add_Values_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1533,7 +1534,7 @@ char *Json_Array_Add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1533,7 +1534,7 @@ char *Json_Array_Add(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Array_Add_deinit(UDF_INIT* initid) void Json_Array_Add_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Array_Add_deinit } // end of Json_Array_Add_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1606,7 +1607,7 @@ char *Json_Array_Delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1606,7 +1607,7 @@ char *Json_Array_Delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Array_Delete_deinit(UDF_INIT* initid) void Json_Array_Delete_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Array_Delete_deinit } // end of Json_Array_Delete_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1650,7 +1651,7 @@ char *Json_Object(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1650,7 +1651,7 @@ char *Json_Object(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Object_deinit(UDF_INIT* initid) void Json_Object_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_deinit } // end of Json_Object_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1697,7 +1698,7 @@ char *Json_Object_Nonull(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1697,7 +1698,7 @@ char *Json_Object_Nonull(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Object_Nonull_deinit(UDF_INIT* initid) void Json_Object_Nonull_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_nonull_deinit } // end of Json_Object_nonull_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1766,7 +1767,7 @@ char *Json_Object_Add(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1766,7 +1767,7 @@ char *Json_Object_Add(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Object_Add_deinit(UDF_INIT* initid) void Json_Object_Add_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_Add_deinit } // end of Json_Object_Add_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1838,7 +1839,7 @@ char *Json_Object_Delete(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1838,7 +1839,7 @@ char *Json_Object_Delete(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Object_Delete_deinit(UDF_INIT* initid) void Json_Object_Delete_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_Delete_deinit } // end of Json_Object_Delete_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1904,7 +1905,7 @@ char *Json_Object_List(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -1904,7 +1905,7 @@ char *Json_Object_List(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Object_List_deinit(UDF_INIT* initid) void Json_Object_List_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_List_deinit } // end of Json_Object_List_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -1977,7 +1978,7 @@ void Json_Array_Grp_clear(UDF_INIT *initid, char*, char*) ...@@ -1977,7 +1978,7 @@ void Json_Array_Grp_clear(UDF_INIT *initid, char*, char*)
void Json_Array_Grp_deinit(UDF_INIT* initid) void Json_Array_Grp_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Array_Grp_deinit } // end of Json_Array_Grp_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2048,9 +2049,117 @@ void Json_Object_Grp_clear(UDF_INIT *initid, char*, char*) ...@@ -2048,9 +2049,117 @@ void Json_Object_Grp_clear(UDF_INIT *initid, char*, char*)
void Json_Object_Grp_deinit(UDF_INIT* initid) void Json_Object_Grp_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Object_Grp_deinit } // end of Json_Object_Grp_deinit
/*********************************************************************************/
/* Get a Json item from a Json document. */
/*********************************************************************************/
my_bool Json_Get_Item_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen;
int n = IsJson(args, 0);
if (args->arg_count < 2) {
strcpy(message, "Json_Get_Item must have at least 2 arguments");
return true;
} else if (!n && args->arg_type[0] != STRING_RESULT) {
strcpy(message, "Json_Get_Item first argument must be a json item");
return true;
} else if (args->arg_type[1] != STRING_RESULT) {
strcpy(message, "Second argument is not a string (jpath)");
return true;
} else
CalcLen(args, false, reslen, memlen);
if (n == 2) {
char fn[_MAX_PATH];
long fl;
memcpy(fn, args->args[0], args->lengths[0]);
fn[args->lengths[0]] = 0;
fl = GetFileLength(fn);
memlen += fl * 3;
} else if (n != 3)
memlen += args->lengths[0] * 3;
return JsonInit(initid, args, message, true, reslen, memlen);
} // end of Json_Get_Item_init
char *Json_Get_Item(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *)
{
char *str = NULL;
PGLOBAL g = (PGLOBAL)initid->ptr;
if (g->N) {
str = (char*)g->Xchk;
goto fin;
} else if (initid->const_item)
g->N = 1;
if (!CheckMemory(g, initid, args, 1, false)) {
char *p, *path;
PJSON jsp;
PJSNX jsx;
PJVAL jvp;
if (!g->Xchk) {
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
g->Xchk = jsp;
JsonMemSave(g);
} // endif Mrr
} else
jsp = (PJSON)g->Xchk;
path = MakePSZ(g, args, 1);
jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length);
if (jsx->SetJpath(g, path, true)) {
PUSH_WARNING(g->Message);
*is_null = 1;
return NULL;
} // endif SetJpath
jsx->ReadValue(g);
if (!jsx->GetValue()->IsNull())
str = jsx->GetValue()->GetCharValue();
if (initid->const_item)
// Keep result of constant function
g->Xchk = str;
} // endif CheckMemory
fin:
if (!str) {
*is_null = 1;
*res_length = 0;
} else
*res_length = strlen(str);
return str;
} // end of Json_Get_Item
void Json_Get_Item_deinit(UDF_INIT* initid)
{
JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Get_Item_deinit
/*********************************************************************************/ /*********************************************************************************/
/* Get a string value from a Json item. */ /* Get a string value from a Json item. */
/*********************************************************************************/ /*********************************************************************************/
...@@ -2066,7 +2175,7 @@ my_bool JsonGetString_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2066,7 +2175,7 @@ my_bool JsonGetString_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
strcpy(message, "JsonGetString first argument must be a json item"); strcpy(message, "JsonGetString first argument must be a json item");
return true; return true;
} else if (args->arg_type[1] != STRING_RESULT) { } else if (args->arg_type[1] != STRING_RESULT) {
strcpy(message, "Second argument is not a (jpath) string"); strcpy(message, "Second argument is not a string (jpath)");
return true; return true;
} else } else
CalcLen(args, false, reslen, memlen); CalcLen(args, false, reslen, memlen);
...@@ -2144,7 +2253,7 @@ char *JsonGetString(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2144,7 +2253,7 @@ char *JsonGetString(UDF_INIT *initid, UDF_ARGS *args, char *result,
} // endif CheckMemory } // endif CheckMemory
fin: fin:
if (!str) { if (!str) {
*is_null = 1; *is_null = 1;
*res_length = 0; *res_length = 0;
...@@ -2156,7 +2265,7 @@ char *JsonGetString(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2156,7 +2265,7 @@ char *JsonGetString(UDF_INIT *initid, UDF_ARGS *args, char *result,
void JsonGetString_deinit(UDF_INIT* initid) void JsonGetString_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of JsonGetString_deinit } // end of JsonGetString_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2264,7 +2373,7 @@ long long JsonGetInt(UDF_INIT *initid, UDF_ARGS *args, ...@@ -2264,7 +2373,7 @@ long long JsonGetInt(UDF_INIT *initid, UDF_ARGS *args,
void JsonGetInt_deinit(UDF_INIT* initid) void JsonGetInt_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of JsonGetInt_deinit } // end of JsonGetInt_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2380,7 +2489,7 @@ double JsonGetReal(UDF_INIT *initid, UDF_ARGS *args, ...@@ -2380,7 +2489,7 @@ double JsonGetReal(UDF_INIT *initid, UDF_ARGS *args,
void JsonGetReal_deinit(UDF_INIT* initid) void JsonGetReal_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of JsonGetReal_deinit } // end of JsonGetReal_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2507,7 +2616,7 @@ char *JsonLocate(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2507,7 +2616,7 @@ char *JsonLocate(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Locate_deinit(UDF_INIT* initid) void Json_Locate_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of JsonLocate_deinit } // end of JsonLocate_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2636,7 +2745,7 @@ char *Json_Locate_All(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2636,7 +2745,7 @@ char *Json_Locate_All(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Json_Locate_All_deinit(UDF_INIT* initid) void Json_Locate_All_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_Locate_All_deinit } // end of Json_Locate_All_deinit
/*********************************************************************************/ /*********************************************************************************/
...@@ -2779,13 +2888,13 @@ fin: ...@@ -2779,13 +2888,13 @@ fin:
void Json_File_deinit(UDF_INIT* initid) void Json_File_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Json_File_deinit } // end of Json_File_deinit
/*********************************************************************************/ /*********************************************************************************/
/* Make a json file from a json item. */ /* Make a json file from a json item. */
/*********************************************************************************/ /*********************************************************************************/
my_bool JsonMakeFile_init(UDF_INIT *initid, UDF_ARGS *args, char *message) my_bool Jfile_Make_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{ {
unsigned long reslen, memlen, more = 1024; unsigned long reslen, memlen, more = 1024;
...@@ -2805,9 +2914,9 @@ my_bool JsonMakeFile_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2805,9 +2914,9 @@ my_bool JsonMakeFile_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
CalcLen(args, false, reslen, memlen); CalcLen(args, false, reslen, memlen);
return JsonInit(initid, args, message, true, reslen, memlen); return JsonInit(initid, args, message, true, reslen, memlen);
} // end of JsonMakeFile_init } // end of Jfile_Make_init
char *JsonMakeFile(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 *str, *fn, *msg; char *str, *fn, *msg;
...@@ -2854,12 +2963,12 @@ char *JsonMakeFile(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2854,12 +2963,12 @@ char *JsonMakeFile(UDF_INIT *initid, UDF_ARGS *args, char *result,
*res_length = strlen(str); *res_length = strlen(str);
return str; return str;
} // end of JsonMakeFile } // end of Jfile_Make
void JsonMakeFile_deinit(UDF_INIT* initid) void Jfile_Make_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of JsonMakeFile_deinit } // end of Jfile_Make_deinit
/*********************************************************************************/ /*********************************************************************************/
/* Make and return a binary Json array containing all the parameters. */ /* Make and return a binary Json array containing all the parameters. */
...@@ -2869,7 +2978,6 @@ my_bool Bson_Array_init(UDF_INIT *initid, UDF_ARGS *args, char *message) ...@@ -2869,7 +2978,6 @@ my_bool Bson_Array_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
unsigned long reslen, memlen; unsigned long reslen, memlen;
CalcLen(args, false, reslen, memlen); CalcLen(args, false, reslen, memlen);
memlen *= 2; // TODO: Make this adjustable
return JsonInit(initid, args, message, false, reslen, memlen); return JsonInit(initid, args, message, false, reslen, memlen);
} // end of Bson_Array_init } // end of Bson_Array_init
...@@ -2904,5 +3012,181 @@ char *Bson_Array(UDF_INIT *initid, UDF_ARGS *args, char *result, ...@@ -2904,5 +3012,181 @@ char *Bson_Array(UDF_INIT *initid, UDF_ARGS *args, char *result,
void Bson_Array_deinit(UDF_INIT* initid) void Bson_Array_deinit(UDF_INIT* initid)
{ {
JsonMemFree((PGLOBAL)initid->ptr); JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Bson_Array_deinit } // end of Bson_Array_deinit
/*********************************************************************************/
/* Make a Json Oject containing all the parameters. */
/*********************************************************************************/
my_bool Bson_Object_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen;
CalcLen(args, true, reslen, memlen);
return JsonInit(initid, args, message, false, reslen, memlen);
} // end of Bson_Object_init
char *Bson_Object(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *, char *)
{
PGLOBAL g = (PGLOBAL)initid->ptr;
PBSON bsp = (PBSON)g->Xchk;
if (!bsp || bsp->Changed) {
if (!CheckMemory(g, initid, args, args->arg_count, true)) {
PJOB objp = new(g)JOBJECT;
for (uint i = 0; i < args->arg_count; i++)
objp->SetValue(g, MakeValue(g, args, i), MakeKey(g, args, i));
bsp = BsonAlloc(g, initid->max_length, objp);
strcat(bsp->Msg, " object");
} else {
bsp = BsonAlloc(g, initid->max_length, NULL);
strncpy(bsp->Msg, g->Message, 139);
} // endif CheckMemory
// Keep result of constant function
g->Xchk = (initid->const_item) ? bsp : NULL;
} // endif bsp
*res_length = sizeof(BSON);
return (char*)bsp;
} // end of Bson_Object
void Bson_Object_deinit(UDF_INIT* initid)
{
JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Bson_Object_deinit
/*********************************************************************************/
/* Returns a json file as a json item. */
/*********************************************************************************/
my_bool Bson_File_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen, fl, more = 1024;
if (args->arg_count < 1 || args->arg_count > 3) {
strcpy(message, "Bson_File only accepts 1 to 3 arguments");
return true;
} else if (args->arg_type[0] != STRING_RESULT || !args->args[0]) {
strcpy(message, "Bson_File first argument must be a constant string (file name)");
return true;
} else if (args->arg_count > 1 && args->arg_type[1] != INT_RESULT) {
strcpy(message, "Second argument is not an integer (pretty)");
return true;
} else if (args->arg_count > 2) {
if (args->arg_type[2] != INT_RESULT) {
strcpy(message, "Third argument is not an integer (memory)");
return true;
} else
more += (ulong)*(longlong*)args->args[2];
} // endifs
initid->maybe_null = 1;
CalcLen(args, false, reslen, memlen);
fl = GetFileLength(args->args[0]);
more += fl * M;
memlen += more;
return JsonInit(initid, args, message, false, reslen, memlen);
} // end of Bson_File_init
char *Bson_File(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error)
{
char *fn, *memory;
int len, pretty;
HANDLE hFile;
MEMMAP mm;
PJSON jsp;
PGLOBAL g = (PGLOBAL)initid->ptr;
PBSON bsp = (PBSON)g->Xchk;
if (bsp && !bsp->Changed)
goto fin;
PlugSubSet(g, g->Sarea, g->Sarea_Size);
g->Xchk = NULL;
fn = MakePSZ(g, args, 0);
pretty = (args->arg_count > 1) ? (int)*(longlong*)args->args[1] : 3;
/*******************************************************************************/
/* Create the mapping file object. */
/*******************************************************************************/
hFile = CreateFileMap(g, fn, &mm, MODE_READ, false);
if (hFile == INVALID_HANDLE_VALUE) {
DWORD rc = GetLastError();
if (!(*g->Message))
sprintf(g->Message, MSG(OPEN_MODE_ERROR), "map", (int)rc, fn);
PUSH_WARNING(g->Message);
if (g->Mrr) *error = 1;
*is_null = 1;
return NULL;
} // endif hFile
/*******************************************************************************/
/* Get the file size (assuming file is smaller than 4 GB) */
/*******************************************************************************/
len = mm.lenL;
memory = (char *)mm.memory;
if (!len) { // Empty or deleted file
CloseFileHandle(hFile);
*is_null = 1;
return NULL;
} // endif len
if (!memory) {
CloseFileHandle(hFile);
sprintf(g->Message, MSG(MAP_VIEW_ERROR), fn, GetLastError());
PUSH_WARNING(g->Message);
*is_null = 1;
if (g->Mrr) *error = 1;
return NULL;
} // endif Memory
CloseFileHandle(hFile); // Not used anymore
hFile = INVALID_HANDLE_VALUE; // For Fblock
/*********************************************************************************/
/* Parse the json file and allocate its tree structure. */
/*********************************************************************************/
g->Message[0] = 0;
if (!(jsp = ParseJson(g, memory, len, pretty))) {
PUSH_WARNING(g->Message);
if (g->Mrr) *error = 1;
} // endif jsp
CloseMemMap(memory, len);
if (jsp) {
bsp = BsonAlloc(g, len, jsp);
strcat(bsp->Msg, " file");
bsp->Filename = fn;
bsp->Pretty = pretty;
} else
bsp = NULL;
if (initid->const_item)
// Keep result of constant function
g->Xchk = bsp;
fin:
if (!bsp) {
*res_length = 0;
*is_null = 1;
} else
*res_length = sizeof(BSON);
return (char*)bsp;
} // end of Bson_File
void Bson_File_deinit(UDF_INIT* initid)
{
JsonFreeMem((PGLOBAL)initid->ptr);
} // end of Bson_File_deinit
...@@ -85,6 +85,10 @@ extern "C" { ...@@ -85,6 +85,10 @@ extern "C" {
DllExport void Json_Object_Grp_clear(UDF_INIT *, char *, char *); DllExport void Json_Object_Grp_clear(UDF_INIT *, char *, char *);
DllExport void Json_Object_Grp_deinit(UDF_INIT*); DllExport void Json_Object_Grp_deinit(UDF_INIT*);
DllExport my_bool Json_Get_Item_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Get_Item(UDF_EXEC_ARGS);
DllExport void Json_Get_Item_deinit(UDF_INIT*);
DllExport my_bool JsonGetString_init(UDF_INIT*, UDF_ARGS*, char*); DllExport my_bool JsonGetString_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *JsonGetString(UDF_EXEC_ARGS); DllExport char *JsonGetString(UDF_EXEC_ARGS);
DllExport void JsonGetString_deinit(UDF_INIT*); DllExport void JsonGetString_deinit(UDF_INIT*);
...@@ -109,13 +113,21 @@ extern "C" { ...@@ -109,13 +113,21 @@ extern "C" {
DllExport char *Json_File(UDF_EXEC_ARGS); DllExport char *Json_File(UDF_EXEC_ARGS);
DllExport void Json_File_deinit(UDF_INIT*); DllExport void Json_File_deinit(UDF_INIT*);
DllExport my_bool JsonMakeFile_init(UDF_INIT*, UDF_ARGS*, char*); DllExport my_bool Jfile_Make_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *JsonMakeFile(UDF_EXEC_ARGS); DllExport char *Jfile_Make(UDF_EXEC_ARGS);
DllExport void JsonMakeFile_deinit(UDF_INIT*); DllExport void Jfile_Make_deinit(UDF_INIT*);
DllExport my_bool Bson_Array_init(UDF_INIT*, UDF_ARGS*, char*); DllExport my_bool Bson_Array_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Bson_Array(UDF_EXEC_ARGS); DllExport char *Bson_Array(UDF_EXEC_ARGS);
DllExport void Bson_Array_deinit(UDF_INIT*); DllExport void Bson_Array_deinit(UDF_INIT*);
DllExport my_bool Bson_Object_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Bson_Object(UDF_EXEC_ARGS);
DllExport void Bson_Object_deinit(UDF_INIT*);
DllExport my_bool Bson_File_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Bson_File(UDF_EXEC_ARGS);
DllExport void Bson_File_deinit(UDF_INIT*);
} // extern "C" } // extern "C"
/*********************************************************************************/ /*********************************************************************************/
...@@ -140,7 +152,7 @@ public: ...@@ -140,7 +152,7 @@ public:
PVAL GetValue(void) {return Value;} PVAL GetValue(void) {return Value;}
// Methods // Methods
my_bool SetJpath(PGLOBAL g, char *path); my_bool SetJpath(PGLOBAL g, char *path, my_bool jb = false);
my_bool ParseJpath(PGLOBAL g); my_bool ParseJpath(PGLOBAL g);
void ReadValue(PGLOBAL g); void ReadValue(PGLOBAL g);
PJVAL GetJson(PGLOBAL g); PJVAL GetJson(PGLOBAL g);
......
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