Skip to content

Commit 2bb4280

Browse files
committed
This commit includes changes done in a previous (deleted) branch plus new ones.
From the previous branch: commit eda4928ff122a0845baf5ade83b4aa29244a3a89 Author: Olivier Bertrand <bertrandop@gmail.com> Date: Mon Mar 9 22:34:56 2015 +0100 - Add discovery to JSON tables When columns are not defined, CONNECT analyses the json file to find column definitions. This wors only on table that are an array of objects. Pair keys are used to generate the column names and pair values are used for its definition. When the LEVEL option is defined as a not null integer, the eventual JPATH is scanned up to the LEVEL value. From the current one: - Fix MDEV-7521 when column names are utf8 encoded (not a general multi-charset fix) - Adds more to JSON discovery processing and UDF's - Use PlugDup everywhere it replaces PlugSubAlloc + strcpy.
1 parent 57aacce commit 2bb4280

20 files changed

+958
-393
lines changed

storage/connect/filamap.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* COPYRIGHT: */
77
/* ---------- */
8-
/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
8+
/* (C) Copyright to the author Olivier BERTRAND 2005-2015 */
99
/* */
1010
/* WHAT THIS PROGRAM DOES: */
1111
/* ----------------------- */
@@ -207,8 +207,7 @@ bool MAPFAM::OpenTableFile(PGLOBAL g)
207207
/*******************************************************************/
208208
fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
209209
fp->Type = TYPE_FB_MAP;
210-
fp->Fname = (char*)PlugSubAlloc(g, NULL, strlen(filename) + 1);
211-
strcpy((char*)fp->Fname, filename);
210+
fp->Fname = PlugDup(g, filename);
212211
fp->Next = dbuserp->Openlist;
213212
dbuserp->Openlist = fp;
214213
fp->Count = 1;

storage/connect/filamtxt.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* COPYRIGHT: */
77
/* ---------- */
8-
/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
8+
/* (C) Copyright to the author Olivier BERTRAND 2005-2015 */
99
/* */
1010
/* WHAT THIS PROGRAM DOES: */
1111
/* ----------------------- */
@@ -289,8 +289,7 @@ bool TXTFAM::AddListValue(PGLOBAL g, int type, void *val, PPARM *top)
289289
pp->Intval = *(int*)val;
290290
break;
291291
// case TYPE_STRING:
292-
// pp->Value = PlugSubAlloc(g, NULL, strlen((char*)val) + 1);
293-
// strcpy((char*)pp->Value, (char*)val);
292+
// pp->Value = PlugDup(g, (char*)val);
294293
// break;
295294
case TYPE_PCHAR:
296295
pp->Value = val;
@@ -325,8 +324,7 @@ int TXTFAM::StoreValues(PGLOBAL g, bool upd)
325324
if (Tdbp->PrepareWriting(g))
326325
return RC_FX;
327326

328-
buf = (char*)PlugSubAlloc(g, NULL, strlen(Tdbp->GetLine()) + 1);
329-
strcpy(buf, Tdbp->GetLine());
327+
buf = PlugDup(g, Tdbp->GetLine());
330328
rc = AddListValue(g, TYPE_PCHAR, buf, &To_Upd);
331329
} // endif upd
332330

storage/connect/filamvct.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* COPYRIGHT: */
77
/* ---------- */
8-
/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
8+
/* (C) Copyright to the author Olivier BERTRAND 2005-2015 */
99
/* */
1010
/* WHAT THIS PROGRAM DOES: */
1111
/* ----------------------- */
@@ -1451,8 +1451,7 @@ bool VCMFAM::OpenTableFile(PGLOBAL g)
14511451
/*******************************************************************/
14521452
fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
14531453
fp->Type = TYPE_FB_MAP;
1454-
fp->Fname = (char*)PlugSubAlloc(g, NULL, strlen(filename) + 1);
1455-
strcpy((char*)fp->Fname, filename);
1454+
fp->Fname = PlugDup(g, filename);
14561455
fp->Next = dbuserp->Openlist;
14571456
dbuserp->Openlist = fp;
14581457
fp->Count = 1;
@@ -2844,8 +2843,7 @@ bool VMPFAM::MapColumnFile(PGLOBAL g, MODE mode, int i)
28442843
/*******************************************************************/
28452844
fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
28462845
fp->Type = TYPE_FB_MAP;
2847-
fp->Fname = (char*)PlugSubAlloc(g, NULL, strlen(filename) + 1);
2848-
strcpy((char*)fp->Fname, filename);
2846+
fp->Fname = PlugDup(g, filename);
28492847
fp->Next = dup->Openlist;
28502848
dup->Openlist = fp;
28512849
fp->Count = 1;

storage/connect/ha_connect.cc

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
#define JSONMAX 10 // JSON Default max grp size
172172

173173
extern "C" {
174-
char version[]= "Version 1.03.0006 February 06, 2015";
174+
char version[]= "Version 1.03.0006 March 16, 2015";
175175

176176
#if defined(WIN32)
177177
char compver[]= "Version 1.03.0006 " __DATE__ " " __TIME__;
@@ -211,6 +211,8 @@ extern "C" {
211211
/***********************************************************************/
212212
PQRYRES OEMColumns(PGLOBAL g, PTOS topt, char *tab, char *db, bool info);
213213
PQRYRES VirColumns(PGLOBAL g, char *tab, char *db, bool info);
214+
PQRYRES JSONColumns(PGLOBAL g, char *dp, const char *fn, char *objn,
215+
int pretty, int lvl, int mxr, bool info);
214216
void PushWarning(PGLOBAL g, THD *thd, int level);
215217
bool CheckSelf(PGLOBAL g, TABLE_SHARE *s, const char *host,
216218
const char *db, char *tab, const char *src, int port);
@@ -1020,8 +1022,7 @@ char *GetListOption(PGLOBAL g, const char *opname,
10201022
} // endif pv
10211023

10221024
if (!stricmp(opname, key)) {
1023-
opval= (char*)PlugSubAlloc(g, NULL, strlen(val) + 1);
1024-
strcpy(opval, val);
1025+
opval= PlugDup(g, val);
10251026
break;
10261027
} else if (!pn)
10271028
break;
@@ -1506,8 +1507,7 @@ PIXDEF ha_connect::GetIndexInfo(TABLE_SHARE *s)
15061507

15071508
// Now get index information
15081509
pn= (char*)s->keynames.type_names[n];
1509-
name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
1510-
strcpy(name, pn); // This is probably unuseful
1510+
name= PlugDup(g, pn);
15111511
unique= (kp.flags & 1) != 0;
15121512
pkp= NULL;
15131513

@@ -1517,8 +1517,7 @@ PIXDEF ha_connect::GetIndexInfo(TABLE_SHARE *s)
15171517
// Get the the key parts info
15181518
for (int k= 0; (unsigned)k < kp.user_defined_key_parts; k++) {
15191519
pn= (char*)kp.key_part[k].field->field_name;
1520-
name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
1521-
strcpy(name, pn); // This is probably unuseful
1520+
name= PlugDup(g, pn);
15221521

15231522
// Allocate the key part description block
15241523
kpp= new(g) KPARTDEF(name, k + 1);
@@ -4738,9 +4737,9 @@ static char *encode(PGLOBAL g, const char *cnm)
47384737
@return
47394738
Return 0 if ok
47404739
*/
4741-
static bool add_field(String *sql, const char *field_name, int typ,
4742-
int len, int dec, char *key, uint tm, const char *rem,
4743-
char *dft, char *xtra, int flag, bool dbf, char v)
4740+
static bool add_field(String *sql, const char *field_name, int typ, int len,
4741+
int dec, char *key, uint tm, const char *rem, char *dft,
4742+
char *xtra, char *fmt, int flag, bool dbf, char v)
47444743
{
47454744
char var = (len > 255) ? 'V' : v;
47464745
bool q, error= false;
@@ -4809,6 +4808,12 @@ static bool add_field(String *sql, const char *field_name, int typ,
48094808
error|= sql->append("'");
48104809
} // endif rem
48114810

4811+
if (fmt && *fmt) {
4812+
error|= sql->append(" FIELD_FORMAT='");
4813+
error|= sql->append_for_single_quote(fmt, strlen(fmt));
4814+
error|= sql->append("'");
4815+
} // endif flag
4816+
48124817
if (flag) {
48134818
error|= sql->append(" FLAG=");
48144819
error|= sql->append_ulonglong(flag);
@@ -4952,12 +4957,12 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
49524957
const char *fncn= "?";
49534958
const char *user, *fn, *db, *host, *pwd, *sep, *tbl, *src;
49544959
const char *col, *ocl, *rnk, *pic, *fcl, *skc;
4955-
char *tab, *dsn, *shm, *dpath;
4960+
char *tab, *dsn, *shm, *dpath, *objn;
49564961
#if defined(WIN32)
49574962
char *nsp= NULL, *cls= NULL;
49584963
#endif // WIN32
49594964
int port= 0, hdr= 0, mxr __attribute__((unused))= 0, mxe= 0, rc= 0;
4960-
int cop __attribute__((unused)) = 0;
4965+
int cop __attribute__((unused))= 0, pty= 2, lrecl= 0, lvl= 0;
49614966
#if defined(ODBC_SUPPORT)
49624967
POPARM sop = NULL;
49634968
char *ucnc = NULL;
@@ -4987,7 +4992,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
49874992
if (!g)
49884993
return HA_ERR_INTERNAL_ERROR;
49894994

4990-
user= host= pwd= tbl= src= col= ocl= pic= fcl= skc= rnk= dsn= NULL;
4995+
user= host= pwd= tbl= src= col= ocl= pic= fcl= skc= rnk= dsn= objn= NULL;
49914996

49924997
// Get the useful create options
49934998
ttp= GetTypeID(topt->type);
@@ -5003,6 +5008,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
50035008
hdr= (int)topt->header;
50045009
tbl= topt->tablist;
50055010
col= topt->colist;
5011+
lrecl= (int)topt->lrecl;
50065012

50075013
if (topt->oplist) {
50085014
host= GetListOption(g, "host", topt->oplist, "localhost");
@@ -5017,6 +5023,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
50175023
skc= GetListOption(g, "skipcol", topt->oplist, NULL);
50185024
rnk= GetListOption(g, "rankcol", topt->oplist, NULL);
50195025
pwd= GetListOption(g, "password", topt->oplist);
5026+
objn= GetListOption(g, "Object", topt->oplist, NULL);
50205027
#if defined(WIN32)
50215028
nsp= GetListOption(g, "namespace", topt->oplist);
50225029
cls= GetListOption(g, "class", topt->oplist);
@@ -5034,6 +5041,8 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
50345041
#if defined(PROMPT_OK)
50355042
cop= atoi(GetListOption(g, "checkdsn", topt->oplist, "0"));
50365043
#endif // PROMPT_OK
5044+
pty= atoi(GetListOption(g,"Pretty", topt->oplist, "2"));
5045+
lvl= atoi(GetListOption(g,"Level", topt->oplist, "0"));
50375046
} else {
50385047
host= "localhost";
50395048
user= (ttp == TAB_ODBC ? NULL : "root");
@@ -5077,8 +5086,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
50775086
goto err;
50785087
} // endif tbl
50795088

5080-
tab= (char*)PlugSubAlloc(g, NULL, strlen(tbl) + 1);
5081-
strcpy(tab, tbl);
5089+
tab= PlugDup(g, tbl);
50825090

50835091
if ((p= strchr(tab, ',')))
50845092
*p= 0;
@@ -5204,6 +5212,13 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
52045212
else
52055213
strcpy(g->Message, "Missing OEM module or subtype");
52065214

5215+
break;
5216+
case TAB_JSON:
5217+
if (!fn)
5218+
sprintf(g->Message, "Missing %s file name", topt->type);
5219+
else
5220+
ok= true;
5221+
52075222
break;
52085223
case TAB_VIR:
52095224
ok= true;
@@ -5226,7 +5241,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
52265241
} // endif src
52275242

52285243
if (ok) {
5229-
char *cnm, *rem, *dft, *xtra, *key;
5244+
char *cnm, *rem, *dft, *xtra, *key, *fmt;
52305245
int i, len, prec, dec, typ, flg;
52315246

52325247
// if (cat)
@@ -5315,6 +5330,9 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
53155330
case TAB_VIR:
53165331
qrp= VirColumns(g, tab, (char*)db, fnc == FNC_COL);
53175332
break;
5333+
case TAB_JSON:
5334+
qrp= JSONColumns(g, (char*)db, fn, objn, pty, lrecl, lvl, fnc == FNC_COL);
5335+
break;
53185336
case TAB_OEM:
53195337
qrp= OEMColumns(g, topt, tab, (char*)db, fnc == FNC_COL);
53205338
break;
@@ -5331,7 +5349,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
53315349
if (fnc != FNC_NO || src || ttp == TAB_PIVOT) {
53325350
// Catalog like table
53335351
for (crp= qrp->Colresp; !rc && crp; crp= crp->Next) {
5334-
cnm= encode(g, crp->Name);
5352+
cnm= (ttp == TAB_PIVOT) ? crp->Name : encode(g, crp->Name);
53355353
typ= crp->Type;
53365354
len= crp->Length;
53375355
dec= crp->Prec;
@@ -5347,7 +5365,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
53475365
NOT_NULL_FLAG, "", flg, dbf, v);
53485366
#else // !NEW_WAY
53495367
if (add_field(&sql, cnm, typ, len, dec, NULL, NOT_NULL_FLAG,
5350-
NULL, NULL, NULL, flg, dbf, v))
5368+
NULL, NULL, NULL, NULL, flg, dbf, v))
53515369
rc= HA_ERR_OUT_OF_MEM;
53525370
#endif // !NEW_WAY
53535371
} // endfor crp
@@ -5368,7 +5386,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
53685386
typ= len= prec= dec= 0;
53695387
tm= NOT_NULL_FLAG;
53705388
cnm= (char*)"noname";
5371-
dft= xtra= key= NULL;
5389+
dft= xtra= key= fmt= NULL;
53725390
v= ' ';
53735391
#if defined(NEW_WAY)
53745392
rem= "";
@@ -5407,6 +5425,9 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
54075425
if (crp->Kdata->GetIntValue(i))
54085426
tm= 0; // Nullable
54095427

5428+
break;
5429+
case FLD_FORMAT:
5430+
fmt= (crp->Kdata) ? crp->Kdata->GetCharValue(i) : NULL;
54105431
break;
54115432
case FLD_REM:
54125433
rem= crp->Kdata->GetCharValue(i);
@@ -5485,7 +5506,7 @@ static int connect_assisted_discovery(handlerton *hton, THD* thd,
54855506
tm, rem, 0, dbf, v);
54865507
#else // !NEW_WAY
54875508
if (add_field(&sql, cnm, typ, prec, dec, key, tm, rem, dft, xtra,
5488-
0, dbf, v))
5509+
fmt, 0, dbf, v))
54895510
rc= HA_ERR_OUT_OF_MEM;
54905511
#endif // !NEW_WAY
54915512
} // endfor i
@@ -6545,7 +6566,7 @@ maria_declare_plugin(connect)
65456566
0x0103, /* version number (1.03) */
65466567
NULL, /* status variables */
65476568
connect_system_variables, /* system variables */
6548-
"1.03.0005", /* string version */
6569+
"1.03.0006", /* string version */
65496570
MariaDB_PLUGIN_MATURITY_BETA /* maturity */
65506571
}
65516572
maria_declare_plugin_end;

storage/connect/ha_connect.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) Olivier Bertrand 2004 - 2014
1+
/* Copyright (C) Olivier Bertrand 2004 - 2015
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -56,11 +56,7 @@ class XCHK : public BLOCK {
5656
oldopn= newopn= NULL;
5757
oldpix= newpix= NULL;}
5858

59-
inline char *SetName(PGLOBAL g, char *name) {
60-
char *nm= NULL;
61-
if (name) {nm= (char*)PlugSubAlloc(g, NULL, strlen(name) + 1);
62-
strcpy(nm, name);}
63-
return nm;}
59+
inline char *SetName(PGLOBAL g, char *name) {return PlugDup(g, name);}
6460

6561
bool oldsep; // Sepindex before create/alter
6662
bool newsep; // Sepindex after create/alter

storage/connect/json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*************** json CPP Declares Source Code File (.H) ***************/
2-
/* Name: json.cpp Version 1.0 */
2+
/* Name: json.cpp Version 1.1 */
33
/* */
44
/* (C) Copyright to the author Olivier BERTRAND 2014 - 2015 */
55
/* */
@@ -1157,6 +1157,6 @@ void JVALUE::SetString(PGLOBAL g, PSZ s)
11571157
/***********************************************************************/
11581158
bool JVALUE::IsNull(void)
11591159
{
1160-
return (Jsp) ? Jsp->IsNull() : (Value) ? Value->IsNull() : true;
1160+
return (Jsp) ? Jsp->IsNull() : (Value) ? Value->IsZero() : true;
11611161
} // end of IsNull
11621162

0 commit comments

Comments
 (0)