Commit 69ce20c4 authored by Olivier Bertrand's avatar Olivier Bertrand

Add new json UDF Json_Object_List.

  modified:   storage/connect/json.cpp
  modified:   storage/connect/json.h
  modified:   storage/connect/jsonudf.cpp
  modified:   storage/connect/jsonudf.h
parent 55cb3d8b
......@@ -892,6 +892,20 @@ PJPR JOBJECT::AddPair(PGLOBAL g, PSZ key)
return jpp;
} // end of AddPair
/***********************************************************************/
/* Return all keys as an array. */
/***********************************************************************/
PJAR JOBJECT::GetKeyList(PGLOBAL g)
{
PJAR jarp = new(g) JARRAY();
for (PJPR jpp = First; jpp; jpp = jpp->Next)
jarp->AddValue(g, new(g) JVALUE(g, jpp->GetKey()));
jarp->InitArray(g);
return jarp;
} // end of GetKeyList
/***********************************************************************/
/* Get the value corresponding to the given key. */
/***********************************************************************/
......@@ -1106,6 +1120,17 @@ JVALUE::JVALUE(PGLOBAL g, PVAL valp) : JSON()
Del = false;
} // end of JVALUE constructor
/***********************************************************************/
/* Constructor for a given string. */
/***********************************************************************/
JVALUE::JVALUE(PGLOBAL g, PSZ strp) : JSON()
{
Jsp = NULL;
Value = AllocateValue(g, strp, TYPE_STRING);
Next = NULL;
Del = false;
} // end of JVALUE constructor
/***********************************************************************/
/* Returns the type of the Value's value. */
/***********************************************************************/
......
......@@ -149,10 +149,11 @@ class JSON : public BLOCK {
virtual void InitArray(PGLOBAL g) {X}
virtual PJVAL AddValue(PGLOBAL g, PJVAL jvp = NULL, int *x = NULL) {X return NULL;}
virtual PJPR AddPair(PGLOBAL g, PSZ key) {X return NULL;}
virtual PJVAL GetValue(const char *key) {X return NULL;}
virtual PJAR GetKeyList(PGLOBAL g) {X return NULL;}
virtual PJVAL GetValue(const char *key) {X return NULL;}
virtual PJOB GetObject(void) {return NULL;}
virtual PJAR GetArray(void) {return NULL;}
virtual PJVAL GetValue(int i) {X return NULL;}
virtual PJVAL GetValue(int i) {X return NULL;}
virtual PVAL GetValue(void) {X return NULL;}
virtual PJSON GetJson(void) {X return NULL;}
virtual PJPR GetFirst(void) {X return NULL;}
......@@ -193,7 +194,8 @@ class JOBJECT : public JSON {
virtual PJPR AddPair(PGLOBAL g, PSZ key);
virtual PJOB GetObject(void) {return this;}
virtual PJVAL GetValue(const char* key);
virtual PSZ GetText(PGLOBAL g, PSZ text);
virtual PJAR GetKeyList(PGLOBAL g);
virtual PSZ GetText(PGLOBAL g, PSZ text);
virtual void SetValue(PGLOBAL g, PJVAL jvp, PSZ key);
virtual void DeleteKey(char *k);
virtual bool IsNull(void);
......@@ -245,6 +247,7 @@ class JVALUE : public JSON {
JVALUE(PJSON jsp) : JSON()
{Jsp = jsp; Value = NULL; Next = NULL; Del = false;}
JVALUE(PGLOBAL g, PVAL valp);
JVALUE(PGLOBAL g, PSZ strp);
using JSON::GetValue;
using JSON::SetValue;
......
......@@ -994,7 +994,9 @@ static int IsJson(UDF_ARGS *args, int i)
{
int n = 0;
if (!strnicmp(args->attributes[i], "Json_", 5))
if (i >= args->arg_count)
n = 0;
else if (!strnicmp(args->attributes[i], "Json_", 5))
n = 1; // arg is a json item
else if (args->arg_type[i] == STRING_RESULT &&
!strnicmp(args->attributes[i], "Jfile_", 6))
......@@ -1138,6 +1140,8 @@ static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args,
{
unsigned long rl, ml;
n = MY_MIN(n, args->arg_count);
for (uint i = 0; i < n; i++)
if (IsJson(args, i) == 2) {
if (CalcLen(args, obj, rl, ml))
......@@ -1367,7 +1371,7 @@ my_bool Json_Array_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
unsigned long reslen, memlen;
CalcLen(args, false, reslen, memlen);
return JsonInit(initid, message, false,reslen, memlen);
return JsonInit(initid, message, false, reslen, memlen);
} // end of Json_Array_init
char *Json_Array(UDF_INIT *initid, UDF_ARGS *args, char *result,
......@@ -1537,10 +1541,10 @@ my_bool Json_Array_Delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
strcpy(message, "Json_Array_Delete must have at lest 2 arguments");
return true;
} else if (!IsJson(args, 0)) {
strcpy(message, "Json_Aray_Delete first argument must be a json item");
strcpy(message, "Json_Array_Delete first argument must be a json item");
return true;
} else if (args->arg_type[1] != INT_RESULT) {
PUSH_WARNING("Json_Aray_Delete second argument is not an integer (index)");
strcpy(message, "Json_Array_Delete second argument is not an integer (index)");
return true;
} else
CalcLen(args, false, reslen, memlen);
......@@ -1780,6 +1784,55 @@ void Json_Object_Delete_deinit(UDF_INIT* initid)
PlugExit((PGLOBAL)initid->ptr);
} // end of Json_Object_Delete_deinit
/***********************************************************************/
/* Returns an array of the Json object keys. */
/***********************************************************************/
my_bool Json_Object_List_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen;
if (args->arg_count != 1) {
strcpy(message, "Json_Object_List must have 1 arguments");
return true;
} else if (!IsJson(args, 0)) {
strcpy(message, "Json_Object_List argument must be a json item");
return true;
} else
CalcLen(args, false, reslen, memlen);
return JsonInit(initid, message, true, reslen, memlen);
} // end of Json_Object_List_init
char *Json_Object_List(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *, char *)
{
char *str = NULL;
PGLOBAL g = (PGLOBAL)initid->ptr;
if (!CheckMemory(g, initid, args, 1, false)) {
PJVAL jvp = MakeValue(g, args, 0);
if (jvp && jvp->GetValType() == TYPE_JOB) {
PJOB jobp = jvp->GetObject();
PJAR jarp = jobp->GetKeyList(g);
if (!(str = Serialize(g, jarp, NULL, 0)))
PUSH_WARNING(g->Message);
} else
PUSH_WARNING("First argument is not an object");
} // endif CheckMemory
*res_length = (str) ? strlen(str) : 0;
return str;
} // end of Json_Object_List
void Json_Object_List_deinit(UDF_INIT* initid)
{
PlugExit((PGLOBAL)initid->ptr);
} // end of Json_Object_List_deinit
/***********************************************************************/
/* Make a Json array from values coming from rows. */
/***********************************************************************/
......@@ -1964,7 +2017,7 @@ char *Json_Get_String(UDF_INIT *initid, UDF_ARGS *args, char *result,
jsp = jvp->GetJson();
path = MakePSZ(g, args, 1);
jsx = new(g)JSNX(g, jsp, TYPE_STRING);
jsx = new(g)JSNX(g, jsp, TYPE_STRING, initid->max_length);
if (jsx->SetJpath(g, path)) {
PUSH_WARNING(g->Message);
......@@ -2229,10 +2282,10 @@ my_bool Json_File_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
strcpy(message, "Json_File first argument must be a (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)");
strcpy(message, "Second argument is not an integer (check)");
return true;
} else if (args->arg_count > 2 && args->arg_type[2] != INT_RESULT) {
strcpy(message, "Third argument is not an integer (check)");
strcpy(message, "Third argument is not an integer (pretty)");
return true;
} else if (args->arg_count > 3) {
if (args->arg_type[3] != INT_RESULT) {
......@@ -2262,14 +2315,14 @@ char *Json_File(UDF_INIT *initid, UDF_ARGS *args, char *result,
PlugSubSet(g, g->Sarea, g->Sarea_Size);
fn = MakePSZ(g, args, 0);
if (args->arg_count > 2 && *(longlong*)args->args[2]) {
if (args->arg_count > 1 && *(longlong*)args->args[1]) {
char *memory;
int len, pretty;
HANDLE hFile;
MEMMAP mm;
PJSON jsp;
pretty = (int)*(longlong*)args->args[1];
pretty = (args->arg_count > 2) ? (int)*(longlong*)args->args[2] : 3;
/*******************************************************************/
/* Create the mapping file object. */
......
......@@ -52,6 +52,10 @@ extern "C" {
DllExport char *Json_Array_Delete(UDF_EXEC_ARGS);
DllExport void Json_Array_Delete_deinit(UDF_INIT*);
DllExport my_bool Json_Object_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Object(UDF_EXEC_ARGS);
DllExport void Json_Object_deinit(UDF_INIT*);
DllExport my_bool Json_Object_Nonull_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Object_Nonull(UDF_EXEC_ARGS);
DllExport void Json_Object_Nonull_deinit(UDF_INIT*);
......@@ -64,9 +68,9 @@ extern "C" {
DllExport char *Json_Object_Delete(UDF_EXEC_ARGS);
DllExport void Json_Object_Delete_deinit(UDF_INIT*);
DllExport my_bool Json_Object_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Object(UDF_EXEC_ARGS);
DllExport void Json_Object_deinit(UDF_INIT*);
DllExport my_bool Json_Object_List_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Object_List(UDF_EXEC_ARGS);
DllExport void Json_Object_List_deinit(UDF_INIT*);
DllExport my_bool Json_Array_Grp_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport void Json_Array_Grp_add(UDF_INIT *, UDF_ARGS *, char *, char *);
......
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