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