mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / test / include / DbUtil.hpp
blob0fa45172848fbe6673160d488e93c56f98f0af04
1 /* Copyright (c) 2007, 2008 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 // dbutil.h: interface for the database utilities class.
17 // Supplies a database to the test application
19 #ifndef DBUTIL_HPP
20 #define DBUTIL_HPP
22 #include <NDBT.hpp>
23 #include <BaseString.hpp>
24 #include <Properties.hpp>
25 #include <Vector.hpp>
26 #include <mysql.h>
28 //#define DEBUG
29 #define DIE_UNLESS(expr) \
30 ((void) ((expr) ? 0 : (Die(__FILE__, __LINE__, #expr), 0)))
31 #define DIE(expr) \
32 Die(__FILE__, __LINE__, #expr)
33 #define myerror(msg) printError(msg)
34 #define mysterror(stmt, msg) printStError(stmt, msg)
35 #define CheckStmt(stmt) \
36 { \
37 if ( stmt == 0) \
38 myerror(NULL); \
39 DIE_UNLESS(stmt != 0); \
42 #define check_execute(stmt, r) \
43 { \
44 if (r) \
45 mysterror(stmt, NULL); \
46 DIE_UNLESS(r == 0);\
50 class SqlResultSet : public Properties {
51 public:
53 // Get row with number
54 bool get_row(int row_num);
55 // Load next row
56 bool next(void);
57 // Reset iterator
58 void reset(void);
59 // Remove current row from resultset
60 void remove();
62 SqlResultSet();
63 ~SqlResultSet();
65 const char* column(const char* col_name);
66 uint columnAsInt(const char* col_name);
68 uint insertId();
69 uint affectedRows();
70 uint numRows(void);
71 uint mysqlErrno();
72 const char* mysqlError();
73 const char* mysqlSqlstate();
75 private:
76 uint get_int(const char* name);
77 const char* get_string(const char* name);
79 const Properties* m_curr_row;
80 uint m_curr_row_num;
84 #define DBU_FAILED 1
85 #define DBU_OK 0
87 class DbUtil
89 public:
91 DbUtil(MYSQL* mysql);
92 DbUtil(const char* dbname = "mysql",
93 const char* user = "root",
94 const char* pass = "",
95 const char* suffix = NULL);
96 ~DbUtil();
98 bool doQuery(const char* query);
99 bool doQuery(const char* query, SqlResultSet& result);
100 bool doQuery(const char* query, const Properties& args, SqlResultSet& result);
102 bool doQuery(BaseString& str);
103 bool doQuery(BaseString& str, SqlResultSet& result);
104 bool doQuery(BaseString& str, const Properties& args, SqlResultSet& result);
106 bool waitConnected(int timeout);
108 /* Deprecated, see connect() */
109 void databaseLogin(const char * system,
110 const char * usr,
111 const char * password,
112 unsigned int portIn,
113 const char * sockIn,
114 bool transactional);
116 const char * getDbName() {return m_dbname.c_str();};
117 const char * getUser() {return m_user.c_str();};
118 const char * getPassword(){return m_pass.c_str();};
119 const char * getHost() {return m_host.c_str();};
120 const char * getSocket() {return m_socket.c_str();};
121 const char * getServerType(){return mysql_get_server_info(m_mysql);};
122 const char * getError();
124 MYSQL * getMysql(){return m_mysql;};
125 MYSQL_STMT * STDCALL mysqlSimplePrepare(const char *query);
127 void databaseLogout();
128 void mysqlCloseStmHandle(MYSQL_STMT *my_stmt);
130 int connect();
131 void disconnect();
132 int selectDb();
133 int selectDb(const char *);
134 int createDb(BaseString&);
135 int getErrorNumber();
137 unsigned long selectCountTable(const char * table);
139 protected:
141 bool runQuery(const char* query,
142 const Properties& args,
143 SqlResultSet& rows);
145 bool isConnected();
147 MYSQL * m_mysql;
148 bool m_free_mysql; /* Don't free mysql* if allocated elsewhere */
150 private:
152 bool m_connected;
154 BaseString m_host; // Computer to connect to
155 BaseString m_user; // MySQL User
156 BaseString m_pass; // MySQL User Password
157 BaseString m_dbname; // Database to use
158 BaseString m_socket; // MySQL Server Unix Socket
159 BaseString m_default_file;
160 BaseString m_default_group;
162 unsigned int m_port; // MySQL Server port
164 void setDbName(const char * name){m_dbname.assign(name);};
165 void setUser(const char * user_name){m_user.assign(user_name);};
166 void setPassword(const char * password){m_pass.assign(password);};
167 void setHost(const char * system){m_host.assign(system);};
168 void setPort(unsigned int portIn){m_port=portIn;};
169 void setSocket(const char * sockIn){m_socket.assign(sockIn);};
170 void printError(const char *msg);
171 void printStError(MYSQL_STMT *stmt, const char *msg);
172 void die(const char *file, int line, const char *expr); // stop program
175 #endif