1/*************** tabjson H Declares Source Code File (.H) **************/
2/* Name: tabjson.h Version 1.3 */
3/* */
4/* (C) Copyright to the author Olivier BERTRAND 2014 - 2017 */
5/* */
6/* This file contains the JSON classes declares. */
7/***********************************************************************/
8#include "osutil.h"
9#include "block.h"
10#include "colblk.h"
11#include "json.h"
12
13enum JMODE {MODE_OBJECT, MODE_ARRAY, MODE_VALUE};
14
15typedef class JSONDEF *PJDEF;
16typedef class TDBJSON *PJTDB;
17typedef class JSONCOL *PJCOL;
18class TDBJSN;
19PQRYRES JSONColumns(PGLOBAL g, PCSZ db, PCSZ dsn, PTOS topt, bool info);
20
21/***********************************************************************/
22/* The JSON tree node. Can be an Object or an Array. */
23/***********************************************************************/
24typedef struct _jnode {
25 PSZ Key; // The key used for object
26 OPVAL Op; // Operator used for this node
27 PVAL CncVal; // To cont value used for OP_CNC
28 PVAL Valp; // The internal array VALUE
29 int Rank; // The rank in array
30 int Rx; // Read row number
31 int Nx; // Next to read row number
32} JNODE, *PJNODE;
33
34typedef struct _jncol {
35 struct _jncol *Next;
36 char *Name;
37 char *Fmt;
38 int Type;
39 int Len;
40 int Scale;
41 bool Cbn;
42 bool Found;
43} JCOL, *PJCL;
44
45/***********************************************************************/
46/* Class used to get the columns of a mongo collection. */
47/***********************************************************************/
48class JSONDISC : public BLOCK {
49public:
50 // Constructor
51 JSONDISC(PGLOBAL g, uint *lg);
52
53 // Functions
54 int GetColumns(PGLOBAL g, PCSZ db, PCSZ dsn, PTOS topt);
55 bool Find(PGLOBAL g, PJVAL jvp, int j);
56 void AddColumn(PGLOBAL g);
57
58 // Members
59 JCOL jcol;
60 PJCL jcp, fjcp, pjcp;
61 PVAL valp;
62 PJDEF tdp;
63 TDBJSN *tjnp;
64 PJTDB tjsp;
65 PJPR jpp;
66 PJSON jsp;
67 PJOB row;
68 PCSZ sep;
69 char colname[65], fmt[129], buf[16];
70 uint *length;
71 int i, n, bf, ncol, lvl;
72 bool all;
73}; // end of JSONDISC
74
75/***********************************************************************/
76/* JSON table. */
77/***********************************************************************/
78class DllExport JSONDEF : public DOSDEF { /* Table description */
79 friend class TDBJSON;
80 friend class TDBJSN;
81 friend class TDBJCL;
82 friend class JSONDISC;
83#if defined(CMGO_SUPPORT)
84 friend class CMGFAM;
85#endif // CMGO_SUPPORT
86#if defined(JAVA_SUPPORT)
87 friend class JMGFAM;
88#endif // JAVA_SUPPORT
89public:
90 // Constructor
91 JSONDEF(void);
92
93 // Implementation
94 virtual const char *GetType(void) {return "JSON";}
95
96 // Methods
97 virtual bool DefineAM(PGLOBAL g, LPCSTR am, int poff);
98 virtual PTDB GetTable(PGLOBAL g, MODE m);
99
100 protected:
101 // Members
102 JMODE Jmode; /* MODE_OBJECT by default */
103 PCSZ Objname; /* Name of first level object */
104 PCSZ Xcol; /* Name of expandable column */
105 int Limit; /* Limit of multiple values */
106 int Pretty; /* Depends on file structure */
107 int Level; /* Used for catalog table */
108 int Base; /* The array index base */
109 bool Strict; /* Strict syntax checking */
110 char Sep; /* The Jpath separator */
111 const char *Uri; /* MongoDB connection URI */
112 PCSZ Collname; /* External collection name */
113 PSZ Options; /* Colist ; Pipe */
114 PSZ Filter; /* Filter */
115 PSZ Driver; /* MongoDB Driver (C or JAVA) */
116 bool Pipe; /* True if Colist is a pipeline */
117 int Version; /* Driver version */
118 PSZ Wrapname; /* MongoDB java wrapper name */
119 }; // end of JSONDEF
120
121/* -------------------------- TDBJSN class --------------------------- */
122
123/***********************************************************************/
124/* This is the JSN Access Method class declaration. */
125/* The table is a DOS file, each record being a JSON object. */
126/***********************************************************************/
127class DllExport TDBJSN : public TDBDOS {
128 friend class JSONCOL;
129 friend class JSONDEF;
130#if defined(CMGO_SUPPORT)
131 friend class CMGFAM;
132#endif // CMGO_SUPPORT
133#if defined(JAVA_SUPPORT)
134 friend class JMGFAM;
135#endif // JAVA_SUPPORT
136public:
137 // Constructor
138 TDBJSN(PJDEF tdp, PTXF txfp);
139 TDBJSN(TDBJSN *tdbp);
140
141 // Implementation
142 virtual AMT GetAmType(void) {return TYPE_AM_JSN;}
143 virtual bool SkipHeader(PGLOBAL g);
144 virtual PTDB Duplicate(PGLOBAL g) {return (PTDB)new(g) TDBJSN(this);}
145 PJSON GetRow(void) {return Row;}
146 void SetG(PGLOBAL g) {G = g;}
147
148 // Methods
149 virtual PTDB Clone(PTABS t);
150 virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n);
151 virtual PCOL InsertSpecialColumn(PCOL colp);
152 virtual int RowNumber(PGLOBAL g, bool b = FALSE)
153 {return (b) ? M : N;}
154 virtual bool CanBeFiltered(void)
155 {return Txfp->GetAmType() == TYPE_AM_MGO || !Xcol;}
156
157 // Database routines
158 virtual int Cardinality(PGLOBAL g);
159 virtual int GetMaxSize(PGLOBAL g);
160 virtual bool OpenDB(PGLOBAL g);
161 virtual int ReadDB(PGLOBAL g);
162 virtual bool PrepareWriting(PGLOBAL g);
163 virtual int WriteDB(PGLOBAL g);
164
165 protected:
166 PJSON FindRow(PGLOBAL g);
167 int MakeTopTree(PGLOBAL g, PJSON jsp);
168
169 // Members
170 PGLOBAL G; // Support of parse memory
171 PJSON Top; // The top JSON tree
172 PJSON Row; // The current row
173 PJSON Val; // The value of the current row
174 PJCOL Colp; // The multiple column
175 JMODE Jmode; // MODE_OBJECT by default
176 PCSZ Objname; // The table object name
177 PCSZ Xcol; // Name of expandable column
178 int Fpos; // The current row index
179 int N; // The current Rownum
180 int M; // Index of multiple value
181 int Limit; // Limit of multiple values
182 int Pretty; // Depends on file structure
183 int NextSame; // Same next row
184 int SameRow; // Same row nb
185 int Xval; // Index of expandable array
186 int B; // Array index base
187 char Sep; // The Jpath separator
188 bool Strict; // Strict syntax checking
189 bool Comma; // Row has final comma
190 }; // end of class TDBJSN
191
192/* -------------------------- JSONCOL class -------------------------- */
193
194/***********************************************************************/
195/* Class JSONCOL: JSON access method column descriptor. */
196/***********************************************************************/
197class DllExport JSONCOL : public DOSCOL {
198 friend class TDBJSN;
199 friend class TDBJSON;
200#if defined(CMGO_SUPPORT)
201 friend class CMGFAM;
202#endif // CMGO_SUPPORT
203#if defined(JAVA_SUPPORT)
204 friend class JMGFAM;
205#endif // JAVA_SUPPORT
206public:
207 // Constructors
208 JSONCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i);
209 JSONCOL(JSONCOL *colp, PTDB tdbp); // Constructor used in copy process
210
211 // Implementation
212 virtual int GetAmType(void) {return Tjp->GetAmType();}
213
214 // Methods
215 virtual bool SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check);
216 bool ParseJpath(PGLOBAL g);
217 virtual PSZ GetJpath(PGLOBAL g, bool proj);
218 virtual void ReadColumn(PGLOBAL g);
219 virtual void WriteColumn(PGLOBAL g);
220
221 protected:
222 bool CheckExpand(PGLOBAL g, int i, PSZ nm, bool b);
223 bool SetArrayOptions(PGLOBAL g, char *p, int i, PSZ nm);
224 PVAL GetColumnValue(PGLOBAL g, PJSON row, int i);
225 PVAL ExpandArray(PGLOBAL g, PJAR arp, int n);
226 PVAL CalculateArray(PGLOBAL g, PJAR arp, int n);
227 PVAL MakeJson(PGLOBAL g, PJSON jsp);
228 void SetJsonValue(PGLOBAL g, PVAL vp, PJVAL val, int n);
229 PJSON GetRow(PGLOBAL g);
230
231 // Default constructor not to be used
232 JSONCOL(void) {}
233
234 // Members
235 PGLOBAL G; // Support of parse memory
236 TDBJSN *Tjp; // To the JSN table block
237 PVAL MulVal; // To value used by multiple column
238 char *Jpath; // The json path
239 JNODE *Nodes; // The intermediate objects
240 int Nod; // The number of intermediate objects
241 int Xnod; // Index of multiple values
242 char Sep; // The Jpath separator
243 bool Xpd; // True for expandable column
244 bool Parsed; // True when parsed
245 }; // end of class JSONCOL
246
247/* -------------------------- TDBJSON class -------------------------- */
248
249/***********************************************************************/
250/* This is the JSON Access Method class declaration. */
251/***********************************************************************/
252class DllExport TDBJSON : public TDBJSN {
253 friend class JSONDEF;
254 friend class JSONCOL;
255 public:
256 // Constructor
257 TDBJSON(PJDEF tdp, PTXF txfp);
258 TDBJSON(PJTDB tdbp);
259
260 // Implementation
261 virtual AMT GetAmType(void) {return TYPE_AM_JSON;}
262 virtual PTDB Duplicate(PGLOBAL g) {return (PTDB)new(g) TDBJSON(this);}
263 PJAR GetDoc(void) {return Doc;}
264
265 // Methods
266 virtual PTDB Clone(PTABS t);
267
268 // Database routines
269 virtual int Cardinality(PGLOBAL g);
270 virtual int GetMaxSize(PGLOBAL g);
271 virtual void ResetSize(void);
272 virtual int GetProgCur(void) {return N;}
273 virtual int GetRecpos(void);
274 virtual bool SetRecpos(PGLOBAL g, int recpos);
275 virtual bool OpenDB(PGLOBAL g);
276 virtual int ReadDB(PGLOBAL g);
277 virtual bool PrepareWriting(PGLOBAL g) {return false;}
278 virtual int WriteDB(PGLOBAL g);
279 virtual int DeleteDB(PGLOBAL g, int irc);
280 virtual void CloseDB(PGLOBAL g);
281 int MakeDocument(PGLOBAL g);
282
283 // Optimization routines
284 virtual int MakeIndex(PGLOBAL g, PIXDEF pxdf, bool add);
285
286 protected:
287 int MakeNewDoc(PGLOBAL g);
288
289 // Members
290 PJAR Doc; // The document array
291 int Multiple; // 0: No 1: DIR 2: Section 3: filelist
292 bool Done; // True when document parsing is done
293 bool Changed; // After Update, Insert or Delete
294 }; // end of class TDBJSON
295
296/***********************************************************************/
297/* This is the class declaration for the JSON catalog table. */
298/***********************************************************************/
299class DllExport TDBJCL : public TDBCAT {
300 public:
301 // Constructor
302 TDBJCL(PJDEF tdp);
303
304 protected:
305 // Specific routines
306 virtual PQRYRES GetResult(PGLOBAL g);
307
308 // Members
309 PTOS Topt;
310 PCSZ Db;
311 PCSZ Dsn;
312 }; // end of class TDBJCL
313