Commit 2113cab7 authored by Olivier Bertrand's avatar Olivier Bertrand

Make REST tables default file name. Commit before continuing BSON development

parent 24c18ce8
......@@ -101,17 +101,19 @@ BDOC::BDOC(PGLOBAL G) : BJSON(G, NULL)
jp = NULL;
s = NULL;
len = 0;
pretty = 3;
pty[0] = pty[1] = pty[2] = true;
comma = false;
} // end of BDOC constructor
/***********************************************************************/
/* Parse a json string. */
/* Note: when pretty is not known, the caller set pretty to 3. */
/***********************************************************************/
PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng)
{
int i, pretty = (ptyp) ? *ptyp : 3;
bool b = false;
int i;
bool b = false, ptyp = (bool *)pty;
PBVAL bvp = NULL;
s = js;
......@@ -121,8 +123,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
if (!s || !len) {
strcpy(g->Message, "Void JSON object");
return NULL;
} else if (comma)
*comma = false;
} // endif s
// Trying to guess the pretty format
if (s[0] == '[' && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n')))
......@@ -136,7 +137,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
switch (s[i]) {
case '[':
if (bvp->Type != TYPE_UNKNOWN)
bvp->To_Val = ParseAsArray(i, pretty, ptyp);
bvp->To_Val = ParseAsArray(i);
else
bvp->To_Val = ParseArray(++i);
......@@ -144,7 +145,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
break;
case '{':
if (bvp->Type != TYPE_UNKNOWN) {
bvp->To_Val = ParseAsArray(i, pretty, ptyp);
bvp->To_Val = ParseAsArray(i);
bvp->Type = TYPE_JAR;
} else {
bvp->To_Val = ParseObject(++i);
......@@ -159,9 +160,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
break;
case ',':
if (bvp->Type != TYPE_UNKNOWN && (pretty == 1 || pretty == 3)) {
if (comma)
*comma = true;
comma = true;
pty[0] = pty[2] = false;
break;
} // endif pretty
......@@ -179,7 +178,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
default:
if (bvp->Type != TYPE_UNKNOWN) {
bvp->To_Val = ParseAsArray(i, pretty, ptyp);
bvp->To_Val = ParseAsArray(i);
bvp->Type = TYPE_JAR;
} else if ((bvp->To_Val = MOF(ParseValue(i))))
bvp->Type = TYPE_JVAL;
......@@ -191,12 +190,10 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
if (bvp->Type == TYPE_UNKNOWN)
sprintf(g->Message, "Invalid Json string '%.*s'", MY_MIN((int)len, 50), s);
else if (ptyp && pretty == 3) {
*ptyp = 3; // Not recognized pretty
else if (pretty == 3) {
for (i = 0; i < 3; i++)
if (pty[i]) {
*ptyp = i;
pretty = i;
break;
} // endif pty
......@@ -218,12 +215,12 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma)
/***********************************************************************/
/* Parse several items as being in an array. */
/***********************************************************************/
OFFSET BDOC::ParseAsArray(int& i, int pretty, int* ptyp) {
OFFSET BDOC::ParseAsArray(int& i) {
if (pty[0] && (!pretty || pretty > 2)) {
OFFSET jsp;
if ((jsp = ParseArray((i = 0))) && ptyp && pretty == 3)
*ptyp = (pty[0]) ? 0 : 3;
if ((jsp = ParseArray((i = 0))) && pretty == 3)
pretty = (pty[0]) ? 0 : 3;
return jsp;
} else
......
......@@ -167,7 +167,12 @@ class BDOC : public BJSON {
public:
BDOC(PGLOBAL G);
PBVAL ParseJson(PGLOBAL g, char* s, size_t n, int* prty = NULL, bool* b = NULL);
bool GetComma(void) { return comma; }
int GetPretty(void) { return pretty; }
void SetPretty(int pty) { pretty = pty; }
// Methods
PBVAL ParseJson(PGLOBAL g, char* s, size_t n);
PSZ Serialize(PGLOBAL g, PBVAL bvp, char* fn, int pretty);
protected:
......@@ -176,7 +181,7 @@ class BDOC : public BJSON {
PBVAL ParseValue(int& i);
OFFSET ParseString(int& i);
void ParseNumeric(int& i, PBVAL bvp);
OFFSET ParseAsArray(int& i, int pretty, int* ptyp);
OFFSET ParseAsArray(int& i);
bool SerializeArray(OFFSET arp, bool b);
bool SerializeObject(OFFSET obp);
bool SerializeValue(PBVAL vp);
......@@ -185,7 +190,9 @@ class BDOC : public BJSON {
JOUT* jp; // Used with serialize
char* s; // The Json string to parse
int len; // The Json string length
int pretty; // The pretty style of the file to parse
bool pty[3]; // Used to guess what pretty is
bool comma; // True if Pretty = 1
// Default constructor not to be used
BDOC(void) {}
......
......@@ -170,7 +170,7 @@
#define JSONMAX 10 // JSON Default max grp size
extern "C" {
char version[]= "Version 1.07.0002 December 18, 2020";
char version[]= "Version 1.07.0002 December 19, 2020";
#if defined(__WIN__)
char compver[]= "Version 1.07.0002 " __DATE__ " " __TIME__;
char slash= '\\';
......@@ -5701,14 +5701,20 @@ static int connect_assisted_discovery(handlerton *, THD* thd,
goto err;
#if defined(REST_SUPPORT)
} else if (topt->http) {
switch (ttp) {
if (ttp == TAB_UNDEF) {
topt->type = "JSON";
ttp= GetTypeID(topt->type);
sprintf(g->Message, "No table_type. Was set to %s", topt->type);
push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
} // endif ttp
switch (ttp) {
case TAB_JSON:
#if defined(BSON_SUPPORT)
case TAB_BSON:
#endif // BSON_SUPPORT
case TAB_XML:
case TAB_CSV:
case TAB_UNDEF:
ttp = TAB_REST;
break;
default:
......@@ -5894,7 +5900,7 @@ static int connect_assisted_discovery(handlerton *, THD* thd,
#if defined(BSON_SUPPORT)
case TAB_BSON:
#endif // BSON_SUPPORT
dsn= strz(g, create_info->connect_string);
dsn= strz(g, create_info->connect_string);
if (!fn && !zfn && !mul && !dsn)
sprintf(g->Message, "Missing %s file name", topt->type);
......@@ -6132,10 +6138,8 @@ static int connect_assisted_discovery(handlerton *, THD* thd,
} // endif !nblin
// Restore language type
if (ttp == TAB_REST) {
if (ttp == TAB_REST)
ttp = GetTypeID(topt->type);
ttp = (ttp == TAB_UNDEF) ? TAB_JSON : ttp;
} // endif ttp
for (i= 0; !rc && i < qrp->Nblin; i++) {
typ= len= prec= dec= flg= 0;
......@@ -6436,6 +6440,9 @@ int ha_connect::create(const char *name, TABLE *table_arg,
// Check table type
if (type == TAB_UNDEF) {
options->type= (options->srcdef) ? "MYSQL" :
#if defined(BSON_SUPPORT)
(options->http) ? "JSON" :
#endif // BSON_SUPPORT
(options->tabname) ? "PROXY" : "DOS";
type= GetTypeID(options->type);
sprintf(g->Message, "No table_type. Will be set to %s", options->type);
......
......@@ -626,9 +626,11 @@ PBVAL BTUTIL::FindRow(PGLOBAL g)
/***********************************************************************/
/* Parse the read line. */
/***********************************************************************/
PBVAL BTUTIL::ParseLine(PGLOBAL g, int *pretty, bool *comma)
PBVAL BTUTIL::ParseLine(PGLOBAL g, int prty, bool cma)
{
return ParseJson(g, Tp->To_Line, strlen(Tp->To_Line), pretty, comma);
pretty = prty;
comma = cma;
return ParseJson(g, Tp->To_Line, strlen(Tp->To_Line));
} // end of ParseLine
/***********************************************************************/
......@@ -1296,6 +1298,7 @@ TDBBSN::TDBBSN(PGLOBAL g, PBDEF tdp, PTXF txfp) : TDBDOS(tdp, txfp)
SameRow = 0;
Xval = -1;
Comma = false;
Bp->SetPretty(Pretty);
} // end of TDBBSN standard constructor
TDBBSN::TDBBSN(TDBBSN* tdbp) : TDBDOS(NULL, tdbp)
......@@ -1527,7 +1530,7 @@ int TDBBSN::ReadDB(PGLOBAL g)
// Recover the memory used for parsing
Bp->SubSet();
if ((Row = Bp->ParseLine(g, &Pretty, &Comma))) {
if ((Row = Bp->ParseLine(g, Pretty, Comma))) {
Top = Row;
Row = Bp->FindRow(g);
SameRow = 0;
......@@ -2081,6 +2084,7 @@ TDBBSON::TDBBSON(PGLOBAL g, PBDEF tdp, PTXF txfp) : TDBBSN(g, tdp, txfp)
Docp = NULL;
Multiple = tdp->Multiple;
Done = Changed = false;
Bp->SetPretty(2);
} // end of TDBBSON standard constructor
TDBBSON::TDBBSON(PBTDB tdbp) : TDBBSN(tdbp)
......@@ -2165,7 +2169,7 @@ int TDBBSON::MakeDocument(PGLOBAL g)
/* Parse the json file and allocate its tree structure. */
/*********************************************************************/
g->Message[0] = 0;
jsp = Top = Bp->ParseJson(g, memory, len, &Pretty);
jsp = Top = Bp->ParseJson(g, memory, len);
Txfp->CloseTableFile(g, false);
Mode = mode; // Restore saved Mode
......
......@@ -110,7 +110,7 @@ class BTUTIL : public BDOC {
// Utility functions
PBVAL FindRow(PGLOBAL g);
PBVAL ParseLine(PGLOBAL g, int *pretty, bool *comma);
PBVAL ParseLine(PGLOBAL g, int prty, bool cma);
PBVAL MakeTopTree(PGLOBAL g, int type);
PSZ SerialVal(PGLOBAL g, PBVAL top, int pretty);
......
......@@ -13,6 +13,8 @@
/***********************************************************************/
#if defined(MARIADB)
#include <my_global.h> // All MariaDB stuff
#include <mysqld.h>
#include <sql_error.h>
#else // !MARIADB OEM module
#include "mini-global.h"
#define _MAX_PATH 260
......@@ -45,6 +47,12 @@
#include "tabfmt.h"
#include "tabrest.h"
#if defined(connect_EXPORTS)
#define PUSH_WARNING(M) push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, 0, M)
#else
#define PUSH_WARNING(M) htrc(M)
#endif
#if defined(__WIN__) || defined(_WINDOWS)
#define popen _popen
#define pclose _pclose
......@@ -223,6 +231,8 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
fn = filename;
tp->filename = PlugDup(g, fn);
sprintf(g->Message, "No file name. Table will use %s", fn);
PUSH_WARNING(g->Message);
} // endif fn
// We used the file name relative to recorded datapath
......
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