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