summaryrefslogtreecommitdiff
path: root/storage/connect/tabmgo.h
blob: 66676794e4747f4e5b93eb0193befece3fc1d3e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**************** tabmgo H Declares Source Code File (.H) **************/
/*  Name: tabmgo.h   Version 1.0                                       */
/*                                                                     */
/*  (C) Copyright to the author Olivier BERTRAND          2017         */
/*                                                                     */
/*  This file contains the MongoDB classes declares.                   */
/***********************************************************************/
#include "osutil.h"
#include "block.h"
#include "colblk.h"

/***********************************************************************/
/*  Include MongoDB library header files.                       	  	 */
/***********************************************************************/
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>

typedef class MGODEF *PMGODEF;
typedef class TDBMGO *PTDBMGO;
typedef class MGOCOL *PMGOCOL;
typedef class INCOL  *PINCOL;

typedef struct _bncol {
	struct _bncol *Next;
	char *Name;
	char *Fmt;
	int   Type;
	int   Len;
	int   Scale;
	bool  Cbn;
	bool  Found;
} BCOL, *PBCOL;

typedef struct KEYCOL {
	KEYCOL *Next;
	PINCOL  Incolp;
	PCOL    Colp;
	char   *Key;
} *PKC;

/***********************************************************************/
/*  Class used to get the columns of a mongo collection.               */
/***********************************************************************/
class MGODISC : public BLOCK {
public:
	// Constructor
	MGODISC(PGLOBAL g, int *lg);

	// Functions
	int  GetColumns(PGLOBAL g, char *db, PTOS topt);
	bool FindInDoc(PGLOBAL g, bson_iter_t *iter, const bson_t *doc,
		             char *pcn, char *pfmt, int i, int k, bool b);

	// Members
	BCOL    bcol;
	PBCOL   bcp, fbcp, pbcp;
	PMGODEF tdp;
	TDBMGO *tmgp;
	int    *length;
	int     n, k, lvl;
	bool    all;
}; // end of MGODISC

/***********************************************************************/
/*  MongoDB table.                                                     */
/***********************************************************************/
class DllExport MGODEF : public EXTDEF {          /* Table description */
	friend class TDBMGO;
	friend class MGOFAM;
	friend class MGODISC;
	friend PQRYRES MGOColumns(PGLOBAL, char *, PTOS, bool);
public:
	// Constructor
	MGODEF(void);

	// Implementation
	virtual const char *GetType(void) { return "MONGO"; }

	// Methods
	virtual bool DefineAM(PGLOBAL g, LPCSTR am, int poff);
	virtual PTDB GetTable(PGLOBAL g, MODE m);

protected:
	// Members
	PCSZ  Uri;							      /* MongoDB connection URI              */
	PCSZ  Colist;                 /* Options list                        */
	PCSZ  Filter;									/* Filtering query                     */						
	int   Level;                  /* Used for catalog table              */
	int   Base;                   /* The array index base                */
	bool  Pipe;                   /* True is Colist is a pipeline        */
}; // end of MGODEF

/* ------------------------- TDBMGO classes -------------------------- */

/***********************************************************************/
/*  Used when inserting values in a MongoDB collection.                */
/***********************************************************************/
class INCOL : public BLOCK {
public:
	// Constructor
	INCOL(void) { Klist = NULL; }

	// Methods
	void AddCol(PGLOBAL g, PCOL colp, char *jp);

	//Members
	bson_t Child;
	PKC    Klist;
}; // end of INCOL;

/***********************************************************************/
/*  This is the MongoDB Table Type class declaration.                  */
/*  The table is a collection, each record being a document.           */
/***********************************************************************/
class DllExport TDBMGO : public TDBEXT {
	friend class MGOCOL;
	friend class MGODEF;
	friend class MGODISC;
	friend PQRYRES MGOColumns(PGLOBAL, char *, PTOS, bool);
public:
	// Constructor
	TDBMGO(PMGODEF tdp);
	TDBMGO(TDBMGO *tdbp);

	// Implementation
	virtual AMT  GetAmType(void) {return TYPE_AM_MGO;}
	virtual PTDB Duplicate(PGLOBAL g) {return (PTDB)new(g) TDBMGO(this);}

	// Methods
	virtual PTDB Clone(PTABS t);
	virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n);
	virtual PCOL InsertSpecialColumn(PCOL colp);
	virtual void SetFilter(PFIL fp);
	virtual int  RowNumber(PGLOBAL g, bool b = FALSE) {return N;}

	// Database routines
	virtual int  Cardinality(PGLOBAL g);
	virtual int  GetMaxSize(PGLOBAL g);
	virtual bool OpenDB(PGLOBAL g);
	virtual int  ReadDB(PGLOBAL g);
	virtual int  WriteDB(PGLOBAL g);
	virtual int  DeleteDB(PGLOBAL g, int irc);
	virtual void CloseDB(PGLOBAL g);
	virtual bool ReadKey(PGLOBAL g, OPVAL op, const key_range *kr);

protected:
	bool Init(PGLOBAL g);
	mongoc_cursor_t *MakeCursor(PGLOBAL g);
	void ShowDocument(bson_iter_t *i, const bson_t *b, const char *k);
	void MakeColumnGroups(PGLOBAL g);
	bool DocWrite(PGLOBAL g, PINCOL icp);

	// Members
	PGLOBAL               G;					// Needed by SetFilter
	mongoc_uri_t         *Uri;
	mongoc_client_pool_t *Pool;				// Thread safe client pool
	mongoc_client_t      *Client;		  // The MongoDB client
	mongoc_database_t    *Database;	  // The MongoDB database
	mongoc_collection_t  *Collection; // The MongoDB collection
	mongoc_cursor_t      *Cursor;
	const bson_t         *Document;
	bson_t               *Query;			// MongoDB cursor filter
	bson_t               *Opts;			  // MongoDB cursor options
	bson_error_t          Error;
	PINCOL                Fpc;				// To insert INCOL classes
	const Item           *Cnd;			  // The first condition
	const char           *Uristr;
	const char           *Db_name;
	const char           *Coll_name;
	const char           *Options;		// The MongoDB options
	const char           *Filter;			// The filtering query
	int                   Fpos;       // The current row index
	int                   N;          // The current Rownum
	int                   B;          // Array index base
	bool                  Done;			  // Init done
	bool                  Pipe;			  // True for pipeline
}; // end of class TDBMGO

/* --------------------------- MGOCOL class -------------------------- */

/***********************************************************************/
/*  Class MGOCOL: MongoDB access method column descriptor.             */
/***********************************************************************/
class DllExport MGOCOL : public EXTCOL {
	friend class TDBMGO;
	friend class FILTER;
public:
	// Constructors
	MGOCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i);
	MGOCOL(MGOCOL *colp, PTDB tdbp); // Constructor used in copy process

	// Implementation
	virtual int  GetAmType(void) { return Tmgp->GetAmType(); }

	// Methods
//virtual bool SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check);
	virtual void ReadColumn(PGLOBAL g);
	virtual void WriteColumn(PGLOBAL g);
	        bool AddValue(PGLOBAL g, bson_t *doc, char *key, bool upd);

protected:
	// Default constructor not to be used
	MGOCOL(void) {}
	char *GetProjPath(PGLOBAL g);
	char *Mini(PGLOBAL g, const bson_t *bson, bool b);

	// Members
	TDBMGO *Tmgp;                 // To the MGO table block
	bson_iter_t Iter;						  // Used to retrieve column value
	bson_iter_t Desc;						  // Descendant iter
	char   *Jpath;                // The json path
	char   *Mbuf;									// The Mini buffer
}; // end of class MGOCOL

/***********************************************************************/
/*  This is the class declaration for the MONGO catalog table.         */
/***********************************************************************/
class DllExport TDBGOL : public TDBCAT {
public:
	// Constructor
	TDBGOL(PMGODEF tdp);

protected:
	// Specific routines
	virtual PQRYRES GetResult(PGLOBAL g);

	// Members
	PTOS  Topt;
	char *Db;
}; // end of class TDBGOL