1 // TDBMYSQL.H     Olivier Bertrand    2007-2017
2 #include "myconn.h"               // MySQL connection declares
3 
4 typedef class MYSQLDEF *PMYDEF;
5 typedef class TDBMYSQL *PTDBMY;
6 typedef class MYSQLCOL *PMYCOL;
7 typedef class TDBMYEXC *PTDBMYX;
8 typedef class MYXCOL   *PMYXCOL;
9 typedef class MYSQLC   *PMYC;
10 
11 /* ------------------------- MYSQL classes --------------------------- */
12 
13 /***********************************************************************/
14 /*  MYSQL: table type that are MySQL tables.                           */
15 /*  Using embedded MySQL library (or optionally calling a MySQL server)*/
16 /***********************************************************************/
17 
18 /***********************************************************************/
19 /*  MYSQL table.                                                       */
20 /***********************************************************************/
21 class MYSQLDEF : public EXTDEF           {/* Logical table description */
22   friend class TDBMYSQL;
23   friend class TDBMYEXC;
24   friend class TDBMCL;
25   friend class ha_connect;
26  public:
27   // Constructor
28   MYSQLDEF(void);
29 
30   // Implementation
GetType(void)31   virtual const char *GetType(void) {return "MYSQL";}
GetHostname(void)32   inline  PSZ  GetHostname(void) {return Hostname;};
33 //inline  PSZ  GetDatabase(void) {return Tabschema;};
34 //inline  PSZ  GetTabname(void) {return Tabname;}
35 //inline  PSZ  GetSrcdef(void) {return Srcdef;}
36 //inline  PSZ  GetUsername(void) {return Username;};
37 //inline  PSZ  GetPassword(void) {return Password;};
GetPortnumber(void)38   inline  int  GetPortnumber(void) {return Portnumber;}
39 
40   // Methods
41 //virtual int  Indexable(void) {return 2;}
42   virtual bool DefineAM(PGLOBAL g, LPCSTR am, int poff);
43   virtual PTDB GetTable(PGLOBAL g, MODE m);
44           bool ParseURL(PGLOBAL g, char *url, bool b = true);
45           bool GetServerInfo(PGLOBAL g, const char *server_name);
46 
47  protected:
48   // Members
49   PSZ     Hostname;           /* Host machine to use                   */
50 //PSZ     Tabschema;          /* Database to be used by server         */
51 //PSZ     Tabname;            /* External table name                   */
52 //PSZ     Srcdef;             /* The source table SQL definition       */
53 //PSZ     Username;           /* User logon name                       */
54 //PSZ     Password;           /* Password logon info                   */
55   PSZ     Server;             /* PServerID                             */
56 //PSZ     Qrystr;             /* The original query                    */
57   int     Portnumber;         /* MySQL port number (0 = default)       */
58 //int     Maxerr;             /* Maxerr for an Exec table              */
59 //int     Quoted;             /* Identifier quoting level              */
60   bool    Isview;             /* true if this table is a MySQL view    */
61   bool    Bind;               /* Use prepared statement on insert      */
62   bool    Delayed;            /* Delayed insert                        */
63 //bool    Xsrc;               /* Execution type                        */
64   bool    Huge;               /* True for big table                    */
65   }; // end of MYSQLDEF
66 
67 /***********************************************************************/
68 /*  This is the class declaration for the MYSQL table.                 */
69 /***********************************************************************/
70 class TDBMYSQL : public TDBEXT {
71   friend class MYSQLCOL;
72 	friend class TDBTBM;
73  public:
74   // Constructor
75   TDBMYSQL(PMYDEF tdp);
76   TDBMYSQL(PTDBMY tdbp);
77 
78   // Implementation
GetAmType(void)79   virtual AMT  GetAmType(void) {return TYPE_AM_MYSQL;}
Duplicate(PGLOBAL g)80   virtual PTDB Duplicate(PGLOBAL g) {return (PTDB)new(g) TDBMYSQL(this);}
81 
82   // Methods
83   virtual PTDB Clone(PTABS t);
84 //virtual int  GetAffectedRows(void) {return AftRows;}
GetRecpos(void)85   virtual int  GetRecpos(void) {return N;}
86   virtual int  GetProgMax(PGLOBAL g);
ResetDB(void)87   virtual void ResetDB(void) {N = 0;}
88   virtual int  RowNumber(PGLOBAL g, bool b = false);
IsView(void)89   virtual bool IsView(void) {return Isview;}
GetServer(void)90   virtual PCSZ GetServer(void) {return Server;}
SetDatabase(LPCSTR db)91           void SetDatabase(LPCSTR db) {Schema = (char*)db;}
92 
93   // Schema routines
94   virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n);
95   virtual int  Cardinality(PGLOBAL g);
96 //virtual int  GetMaxSize(PGLOBAL g);
97   virtual bool OpenDB(PGLOBAL g);
98   virtual int  ReadDB(PGLOBAL g);
99   virtual int  WriteDB(PGLOBAL g);
100   virtual int  DeleteDB(PGLOBAL g, int irc);
101   virtual void CloseDB(PGLOBAL g);
102   virtual bool ReadKey(PGLOBAL g, OPVAL op, const key_range *kr);
103 
104   // Specific routines
105           bool SetColumnRanks(PGLOBAL g);
106           PCOL MakeFieldColumn(PGLOBAL g, char *name);
107           PSZ  FindFieldColumn(char *name);
108 
109  protected:
110   // Internal functions
111   bool MakeSelect(PGLOBAL g, bool mx);
112   bool MakeInsert(PGLOBAL g);
113   int  BindColumns(PGLOBAL g __attribute__((unused)));
114   virtual bool MakeCommand(PGLOBAL g);
115 //int  MakeUpdate(PGLOBAL g);
116 //int  MakeDelete(PGLOBAL g);
117   int  SendCommand(PGLOBAL g);
118 
119   // Members
120   MYSQLC      Myc;            // MySQL connection class
121   MYSQL_BIND *Bind;           // To the MySQL bind structure array
122 //PSTRG       Query;          // Constructed SQL query
123   char       *Host;           // Host machine to use
124 //char       *User;           // User logon info
125 //char       *Pwd;            // Password logon info
126 //char       *Schema;         // Database to be used by server
127 //char       *TableName;      // External table name
128 //char       *Srcdef;         // The source table SQL definition
129   char       *Server;         // The server ID
130 //char       *Qrystr;         // The original query
131   bool        Fetched;        // True when fetch was done
132   bool        Isview;         // True if this table is a MySQL view
133   bool        Prep;           // Use prepared statement on insert
134   bool        Delayed;        // Use delayed insert
135   int         m_Rc;           // Return code from command
136 //int         AftRows;        // The number of affected rows
137   int         N;              // The current table index
138   unsigned    Port;          // MySQL port number (0 = default)
139 //int         Nparm;          // The number of statement parameters
140 //int         Quoted;         // The identifier quoting level
141   }; // end of class TDBMYSQL
142 
143 /***********************************************************************/
144 /*  Class MYSQLCOL: MySQL table column.                                */
145 /***********************************************************************/
146 class MYSQLCOL : public COLBLK {
147   friend class TDBMYSQL;
148  public:
149   // Constructors
150   MYSQLCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i,  PCSZ am = "MYSQL");
151   MYSQLCOL(MYSQL_FIELD *fld, PTDB tdbp, int i,  PCSZ am = "MYSQL");
152   MYSQLCOL(MYSQLCOL *colp, PTDB tdbp); // Constructor used in copy process
153 
154   // Implementation
GetAmType(void)155   virtual int  GetAmType(void) {return TYPE_AM_MYSQL;}
156           void InitBind(PGLOBAL g);
157 
158   // Methods
159   virtual bool SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check);
160   virtual void ReadColumn(PGLOBAL g);
161   virtual void WriteColumn(PGLOBAL g);
162           bool FindRank(PGLOBAL g);
163 
164  protected:
165   // Members
166   MYSQL_BIND   *Bind;            // This column bind structure pointer
167   PVAL          To_Val;          // To value used for Update/Insert
168   unsigned long Slen;            // Bind string lengh
169   int           Rank;            // Rank (position) number in the query
170   }; // end of class MYSQLCOL
171 
172 /***********************************************************************/
173 /*  This is the class declaration for the exec command MYSQL table.    */
174 /***********************************************************************/
175 class TDBMYEXC : public TDBMYSQL {
176   friend class MYXCOL;
177  public:
178   // Constructors
179   TDBMYEXC(PMYDEF tdp);
180   TDBMYEXC(PTDBMYX tdbp);
181 
182   // Implementation
GetAmType(void)183   virtual AMT  GetAmType(void) {return TYPE_AM_MYX;}
Duplicate(PGLOBAL g)184   virtual PTDB Duplicate(PGLOBAL g) {return (PTDB)new(g) TDBMYEXC(this);}
185 
186   // Methods
187   virtual PTDB Clone(PTABS t);
IsView(void)188   virtual bool IsView(void) {return Isview;}
189 
190   // Database routines
191   virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n);
192   virtual int  GetMaxSize(PGLOBAL g);
193   virtual bool OpenDB(PGLOBAL g);
194   virtual int  ReadDB(PGLOBAL g);
195   virtual int  WriteDB(PGLOBAL g);
196 
197  protected:
198   // Internal functions
199   PCMD MakeCMD(PGLOBAL g);
200 
201   // Members
202   PCMD     Cmdlist;           // The commands to execute
203   char    *Cmdcol;            // The name of the Xsrc command column
204   bool     Shw;               // Show warnings
205   bool     Havew;             // True when processing warnings
206   bool     Isw;               // True for warning lines
207   int      Warnings;          // Warnings number
208   int      Mxr;               // Maximum errors before closing
209   int      Nerr;              // Number of errors so far
210   }; // end of class TDBMYEXC
211 
212 /***********************************************************************/
213 /*  Class MYXCOL: MySQL exec command table column.                     */
214 /***********************************************************************/
215 class MYXCOL : public MYSQLCOL {
216   friend class TDBMYEXC;
217  public:
218   // Constructors
219   MYXCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i,  PCSZ am = "MYSQL");
220   MYXCOL(MYSQL_FIELD *fld, PTDB tdbp, int i,  PCSZ am = "MYSQL");
221   MYXCOL(MYXCOL *colp, PTDB tdbp);   // Constructor used in copy process
222 
223   // Methods
224   virtual void ReadColumn(PGLOBAL g);
225   virtual void WriteColumn(PGLOBAL g);
226 
227  protected:
228   // Members
229   char    *Buffer;              // To get returned message
230   int      Flag;                // Column content desc
231   }; // end of class MYXCOL
232 
233 /***********************************************************************/
234 /*  This is the class declaration for the MYSQL column catalog table.  */
235 /***********************************************************************/
236 class TDBMCL : public TDBCAT {
237  public:
238   // Constructor
239   TDBMCL(PMYDEF tdp);
240 
241  protected:
242 	// Specific routines
243 	virtual PQRYRES GetResult(PGLOBAL g);
244 
245   // Members
246   PCSZ Host;                      // Host machine to use
247 	PCSZ Db;                        // Database to be used by server
248 	PCSZ Tab;                       // External table name
249 	PCSZ User;                      // User logon name
250 	PCSZ Pwd;                       // Password logon info
251 	int  Port;                      // MySQL port number (0 = default)
252   }; // end of class TDBMCL
253