diff options
author | Olivier Bertrand <bertrandop@gmail.com> | 2015-08-18 12:03:29 +0200 |
---|---|---|
committer | Olivier Bertrand <bertrandop@gmail.com> | 2015-08-18 12:03:29 +0200 |
commit | 69ce20c4bdcfc32b3e78a4108313031688204cb9 (patch) | |
tree | 265ea9ac9d73e5147aed472bcb831fbfd0dd87ca | |
parent | 55cb3d8b44d6651e587190c49df1247b8ec02628 (diff) | |
download | mariadb-git-69ce20c4bdcfc32b3e78a4108313031688204cb9.tar.gz |
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
-rw-r--r-- | storage/connect/json.cpp | 25 | ||||
-rw-r--r-- | storage/connect/json.h | 9 | ||||
-rw-r--r-- | storage/connect/jsonudf.cpp | 71 | ||||
-rw-r--r-- | storage/connect/jsonudf.h | 10 |
4 files changed, 100 insertions, 15 deletions
diff --git a/storage/connect/json.cpp b/storage/connect/json.cpp index 2b339e9e9f6..60c42a7c277 100644 --- a/storage/connect/json.cpp +++ b/storage/connect/json.cpp @@ -893,6 +893,20 @@ PJPR JOBJECT::AddPair(PGLOBAL g, PSZ key) } // 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. */ /***********************************************************************/ PJVAL JOBJECT::GetValue(const char* key) @@ -1107,6 +1121,17 @@ JVALUE::JVALUE(PGLOBAL g, PVAL valp) : JSON() } // 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. */ /***********************************************************************/ JTYP JVALUE::GetValType(void) diff --git a/storage/connect/json.h b/storage/connect/json.h index bc45e08acce..28f18404e1c 100644 --- a/storage/connect/json.h +++ b/storage/connect/json.h @@ -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; diff --git a/storage/connect/jsonudf.cpp b/storage/connect/jsonudf.cpp index 300f294595b..f4485124fc9 100644 --- a/storage/connect/jsonudf.cpp +++ b/storage/connect/jsonudf.cpp @@ -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); @@ -1781,6 +1785,55 @@ void Json_Object_Delete_deinit(UDF_INIT* initid) } // 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. */ /***********************************************************************/ my_bool Json_Array_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message) @@ -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. */ diff --git a/storage/connect/jsonudf.h b/storage/connect/jsonudf.h index b6991e13941..73ae5c0ccee 100644 --- a/storage/connect/jsonudf.h +++ b/storage/connect/jsonudf.h @@ -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 *); |