PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/storage/ndb/test/include/DbUtil.hpp

https://github.com/suhailsherif/MySQL-5.1-
C++ Header | 176 lines | 117 code | 37 blank | 22 comment | 5 complexity | 5b255a59e4133ba09eb5ce0dd293424f MD5 | raw file
  1. /* Copyright (C) 2007 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. // dbutil.h: interface for the database utilities class.
  13. // Supplies a database to the test application
  14. #ifndef DBUTIL_HPP
  15. #define DBUTIL_HPP
  16. #include <NDBT.hpp>
  17. #include <BaseString.hpp>
  18. #include <Properties.hpp>
  19. #include <Vector.hpp>
  20. #include <mysql.h>
  21. //#define DEBUG
  22. #define DIE_UNLESS(expr) \
  23. ((void) ((expr) ? 0 : (Die(__FILE__, __LINE__, #expr), 0)))
  24. #define DIE(expr) \
  25. Die(__FILE__, __LINE__, #expr)
  26. #define myerror(msg) printError(msg)
  27. #define mysterror(stmt, msg) printStError(stmt, msg)
  28. #define CheckStmt(stmt) \
  29. { \
  30. if ( stmt == 0) \
  31. myerror(NULL); \
  32. DIE_UNLESS(stmt != 0); \
  33. }
  34. #define check_execute(stmt, r) \
  35. { \
  36. if (r) \
  37. mysterror(stmt, NULL); \
  38. DIE_UNLESS(r == 0);\
  39. }
  40. class SqlResultSet : public Properties {
  41. public:
  42. // Get row with number
  43. bool get_row(int row_num);
  44. // Load next row
  45. bool next(void);
  46. // Reset iterator
  47. void reset(void);
  48. // Remove current row from resultset
  49. void remove();
  50. SqlResultSet();
  51. ~SqlResultSet();
  52. const char* column(const char* col_name);
  53. uint columnAsInt(const char* col_name);
  54. uint insertId();
  55. uint affectedRows();
  56. uint numRows(void);
  57. uint mysqlErrno();
  58. const char* mysqlError();
  59. const char* mysqlSqlstate();
  60. private:
  61. uint get_int(const char* name);
  62. const char* get_string(const char* name);
  63. const Properties* m_curr_row;
  64. uint m_curr_row_num;
  65. };
  66. #define DBU_FAILED 1
  67. #define DBU_OK 0
  68. class DbUtil
  69. {
  70. public:
  71. DbUtil(MYSQL* mysql);
  72. DbUtil(const char* dbname = "mysql",
  73. const char* user = "root",
  74. const char* pass = "",
  75. const char* suffix = NULL);
  76. ~DbUtil();
  77. bool doQuery(const char* query);
  78. bool doQuery(const char* query, SqlResultSet& result);
  79. bool doQuery(const char* query, const Properties& args, SqlResultSet& result);
  80. bool doQuery(BaseString& str);
  81. bool doQuery(BaseString& str, SqlResultSet& result);
  82. bool doQuery(BaseString& str, const Properties& args, SqlResultSet& result);
  83. bool waitConnected(int timeout);
  84. /* Deprecated, see connect() */
  85. void databaseLogin(const char * system,
  86. const char * usr,
  87. const char * password,
  88. unsigned int portIn,
  89. const char * sockIn,
  90. bool transactional);
  91. const char * getDbName() {return m_dbname.c_str();};
  92. const char * getUser() {return m_user.c_str();};
  93. const char * getPassword(){return m_pass.c_str();};
  94. const char * getHost() {return m_host.c_str();};
  95. const char * getSocket() {return m_socket.c_str();};
  96. const char * getServerType(){return mysql_get_server_info(m_mysql);};
  97. const char * getError();
  98. MYSQL * getMysql(){return m_mysql;};
  99. MYSQL_STMT * STDCALL mysqlSimplePrepare(const char *query);
  100. void databaseLogout();
  101. void mysqlCloseStmHandle(MYSQL_STMT *my_stmt);
  102. int connect();
  103. void disconnect();
  104. int selectDb();
  105. int selectDb(const char *);
  106. int createDb(BaseString&);
  107. int getErrorNumber();
  108. unsigned long selectCountTable(const char * table);
  109. protected:
  110. bool runQuery(const char* query,
  111. const Properties& args,
  112. SqlResultSet& rows);
  113. bool isConnected();
  114. MYSQL * m_mysql;
  115. bool m_free_mysql; /* Don't free mysql* if allocated elsewhere */
  116. private:
  117. bool m_connected;
  118. BaseString m_host; // Computer to connect to
  119. BaseString m_user; // MySQL User
  120. BaseString m_pass; // MySQL User Password
  121. BaseString m_dbname; // Database to use
  122. BaseString m_socket; // MySQL Server Unix Socket
  123. BaseString m_default_file;
  124. BaseString m_default_group;
  125. unsigned int m_port; // MySQL Server port
  126. void setDbName(const char * name){m_dbname.assign(name);};
  127. void setUser(const char * user_name){m_user.assign(user_name);};
  128. void setPassword(const char * password){m_pass.assign(password);};
  129. void setHost(const char * system){m_host.assign(system);};
  130. void setPort(unsigned int portIn){m_port=portIn;};
  131. void setSocket(const char * sockIn){m_socket.assign(sockIn);};
  132. void printError(const char *msg);
  133. void printStError(MYSQL_STMT *stmt, const char *msg);
  134. void die(const char *file, int line, const char *expr); // stop program
  135. };
  136. #endif