summaryrefslogtreecommitdiff
path: root/storage/connect/tabrest.cpp
diff options
context:
space:
mode:
authorOlivier Bertrand <bertrandop@gmail.com>2019-08-17 16:58:58 +0200
committerOlivier Bertrand <bertrandop@gmail.com>2019-08-17 16:58:58 +0200
commit4d93c7f3b0bfc59da369702c2eca725f1e362305 (patch)
tree83ea1ff5f3e4d4b7df712679132fd42771fda76e /storage/connect/tabrest.cpp
parentd302cb3534f4b4e8a5ae6e293b6ef2c2c9180862 (diff)
downloadmariadb-git-4d93c7f3b0bfc59da369702c2eca725f1e362305.tar.gz
In CONNECT version 1.6.10 NOSQL facility is enhanced by a new way to retrieve NOSQL data.
In addition to files and Mongo collections, JSON as well as XML and CSV data can be retrieved from the net as answers from REST queries. Because it uses and external package (cpprestsdk) this is currently available only to MariaDB servers compiled from source. -- Add compile flags needed on Windows /MD or /MDd (debug) -- Also include some changes needed on Linux modified: storage/connect/CMakeLists.txt - Add the xtrc tracing function modified: storage/connect/global.h modified: storage/connect/plugutil.cpp - Modify tracing to use xtrc and some typo modified: storage/connect/array.cpp modified: storage/connect/block.h modified: storage/connect/restget.cpp - Fix compilation error when ZIP is not supported modified: storage/connect/ha_connect.cc modified: storage/connect/tabfmt.cpp - Add some tracing + typo modified: storage/connect/mycat.cc modified: storage/connect/tabjson.cpp - Add conditional code based on MARIADB This to be able to use the same code in CONNECT and EOM modules modified: storage/connect/osutil.h modified: storage/connect/tabrest.cpp - Replace PlugSetPath by some concat (crashed on Fedora) + typo modified: storage/connect/reldef.cpp - Try to fix test failures modified: zlib/CMakeLists.txt
Diffstat (limited to 'storage/connect/tabrest.cpp')
-rw-r--r--storage/connect/tabrest.cpp157
1 files changed, 112 insertions, 45 deletions
diff --git a/storage/connect/tabrest.cpp b/storage/connect/tabrest.cpp
index e800411a36d..eef90f0dc1d 100644
--- a/storage/connect/tabrest.cpp
+++ b/storage/connect/tabrest.cpp
@@ -1,13 +1,23 @@
/*************** Rest C++ Program Source Code File (.CPP) **************/
-/* PROGRAM NAME: Rest Version 1.3 */
+/* PROGRAM NAME: Rest Version 1.5 */
/* (C) Copyright to the author Olivier BERTRAND 2018 - 2019 */
-/* This program is the REST OEM (Web API support) module definition. */
+/* This program is the REST Web API support for MariaDB. */
+/* When compiled without MARIADB defined, it is the EOM module code. */
/***********************************************************************/
/***********************************************************************/
/* Definitions needed by the included files. */
/***********************************************************************/
+#if defined(MARIADB)
#include <my_global.h> // All MariaDB stuff
+#else // !MARIADB OEM module
+#include "mini-global.h"
+#define _MAX_PATH 260
+#if !defined(__WIN__)
+#define __stdcall
+#endif // !__WIN__
+#define _OS_H_INCLUDED // Prevent os.h to be called
+#endif // !MARIADB
/***********************************************************************/
/* Include application header files: */
@@ -19,12 +29,12 @@
#include "plgdbsem.h"
#include "xtable.h"
#include "filamtxt.h"
-#include "plgxml.h"
#include "tabdos.h"
-#include "tabfmt.h"
+#include "plgxml.h"
+#include "tabxml.h"
#include "tabjson.h"
+#include "tabfmt.h"
#include "tabrest.h"
-#include "tabxml.h"
/***********************************************************************/
/* Get the file from the Web. */
@@ -32,25 +42,61 @@
int restGetFile(PGLOBAL g, PCSZ http, PCSZ uri, PCSZ fn);
#if defined(__WIN__)
-static PCSZ slash= "\\";
+static PCSZ slash = "\\";
#else // !__WIN__
-static PCSZ slash= "/";
+static PCSZ slash = "/";
#define stricmp strcasecmp
#endif // !__WIN__
+#if !defined(MARIADB)
+/***********************************************************************/
+/* DB static variables. */
+/***********************************************************************/
+int TDB::Tnum;
+int DTVAL::Shift;
+int CSORT::Limit = 0;
+double CSORT::Lg2 = log(2.0);
+size_t CSORT::Cpn[1000] = { 0 };
+
+/***********************************************************************/
+/* These functions are exported from the REST library. */
+/***********************************************************************/
+extern "C" {
+ PTABDEF __stdcall GetREST(PGLOBAL, void*);
+ PQRYRES __stdcall ColREST(PGLOBAL, PTOS, char*, char*, bool);
+} // extern "C"
+
+/***********************************************************************/
+/* This function returns a table definition class. */
+/***********************************************************************/
+PTABDEF __stdcall GetREST(PGLOBAL g, void *memp)
+{
+ return new(g, memp) RESTDEF;
+} // end of GetREST
+#endif // !MARIADB
+
/***********************************************************************/
/* Return the columns definition to MariaDB. */
/***********************************************************************/
+#if defined(MARIADB)
PQRYRES RESTColumns(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
+#else // !MARIADB
+PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
+#endif // !MARIADB
{
PQRYRES qrp= NULL;
- char filename[_MAX_PATH];
+ char filename[_MAX_PATH + 1]; // MAX PATH ???
PCSZ http, uri, fn, ftype;
- http= GetStringTableOption(g, tp, "Http", NULL);
- uri= GetStringTableOption(g, tp, "Uri", NULL);
- fn= GetStringTableOption(g, tp, "Filename", "rest.json");
+ http = GetStringTableOption(g, tp, "Http", NULL);
+ uri = GetStringTableOption(g, tp, "Uri", NULL);
+ fn = GetStringTableOption(g, tp, "Filename", "rest.json");
+#if defined(MARIADB)
ftype = GetStringTableOption(g, tp, "Type", "JSON");
+#else // !MARIADB
+ // OEM tables must specify the file type
+ ftype = GetStringTableOption(g, tp, "Ftype", "JSON");
+#endif // !MARIADB
// We used the file name relative to recorded datapath
strcat(strcat(strcat(strcpy(filename, "."), slash), db), slash);
@@ -60,11 +106,11 @@ PQRYRES RESTColumns(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
if (http && restGetFile(g, http, uri, filename)) {
// sprintf(g->Message, "Failed to get file at %s", http);
} else if (!stricmp(ftype, "XML"))
- qrp= XMLColumns(g, db, tab, tp, info);
+ qrp = XMLColumns(g, db, tab, tp, info);
else if (!stricmp(ftype, "JSON"))
- qrp= JSONColumns(g, db, NULL, tp, info);
+ qrp = JSONColumns(g, db, NULL, tp, info);
else if (!stricmp(ftype, "CSV"))
- qrp= CSVColumns(g, NULL, tp, info);
+ qrp = CSVColumns(g, NULL, tp, info);
else
sprintf(g->Message, "Usupported file type %s", ftype);
@@ -78,40 +124,59 @@ PQRYRES RESTColumns(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
/***********************************************************************/
bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
{
- char filename[_MAX_PATH];
- TABTYPE type= GetTypeID(am);
-
- switch (type) {
- case TAB_JSON:
- case TAB_XML:
- case TAB_CSV:
- break;
- default:
- sprintf(g->Message, "Unsupported REST table type %s", am);
- return true;
- } // endswitch type
-
- Http= GetStringCatInfo(g, "Http", NULL);
- Uri= GetStringCatInfo(g, "Uri", NULL);
- Fn= GetStringCatInfo(g, "Filename", "rest.json");
+ char filename[_MAX_PATH + 1];
+ int rc = 0, n;
+ LPCSTR ftype;
+
+#if defined(MARIADB)
+ ftype = GetStringCatInfo(g, "Type", "JSON");
+#else // !MARIADB
+ // OEM tables must specify the file type
+ ftype = GetStringCatInfo(g, "Ftype", "JSON");
+#endif // !MARIADB
+
+ if (trace(1))
+ htrc("ftype = %s am = %s\n", ftype, SVP(am));
+
+ n = (!stricmp(ftype, "JSON")) ? 1
+ : (!stricmp(ftype, "XML")) ? 2
+ : (!stricmp(ftype, "CSV")) ? 3 : 0;
+
+ if (n == 0) {
+ htrc("DefineAM: Unsupported REST table type %s", am);
+ sprintf(g->Message, "Unsupported REST table type %s", am);
+ return true;
+ } // endif n
+
+ Http = GetStringCatInfo(g, "Http", NULL);
+ Uri = GetStringCatInfo(g, "Uri", NULL);
+ Fn = GetStringCatInfo(g, "Filename", "rest.json");
// We used the file name relative to recorded datapath
- PlugSetPath(filename, Fn, GetPath());
+ //PlugSetPath(filename, Fn, GetPath());
+ strncat(strcpy(filename, GetPath()), Fn, _MAX_PATH);
// Retrieve the file from the web and copy it locally
- if (Http && restGetFile(g, Http, Uri, filename)) {}
- else if (type == TAB_JSON)
- Tdp= new (g) JSONDEF;
- else if (type == TAB_XML)
- Tdp= new (g) XMLDEF;
- else if (type == TAB_CSV)
- Tdp= new (g) CSVDEF;
- else
- sprintf(g->Message, "Unsupported REST table type %s", am);
+ rc = restGetFile(g, Http, Uri, filename);
+
+ if (trace(1))
+ htrc("Return from restGetFile: rc=%d\n", rc);
+
+ if (rc)
+ return true;
+ else switch (n) {
+ case 1: Tdp = new (g) JSONDEF; break;
+ case 2: Tdp = new (g) XMLDEF; break;
+ case 3: Tdp = new (g) CSVDEF; break;
+ default: Tdp = NULL;
+ } // endswitch n
// Do make the table/view definition
if (Tdp && Tdp->Define(g, Cat, Name, Schema, "REST"))
- Tdp= NULL; // Error occured
+ Tdp = NULL; // Error occured
+
+ if (trace(1))
+ htrc("Tdp defined\n", rc);
// Return true in case of error
return (Tdp == NULL);
@@ -122,10 +187,12 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
/***********************************************************************/
PTDB RESTDEF::GetTable(PGLOBAL g, MODE m)
{
- if (m != MODE_READ && m != MODE_READX) {
- strcpy(g->Message, "REST tables are currently read only");
- return NULL;
- } // endif m
+ xtrc(515, "REST GetTable mode=%d\n", m);
+
+ if (m != MODE_READ && m != MODE_READX) {
+ strcpy(g->Message, "REST tables are currently read only");
+ return NULL;
+ } // endif m
return Tdp->GetTable(g, m); // Leave file type do the job
} // end of GetTable