You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
11 KiB

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