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
23 #include <BaseString.hpp>
24 #include <Properties.hpp>
29 #define DIE_UNLESS(expr) \
30 ((void) ((expr) ? 0 : (Die(__FILE__, __LINE__, #expr), 0)))
32 Die(__FILE__, __LINE__, #expr)
33 #define myerror(msg) printError(msg)
34 #define mysterror(stmt, msg) printStError(stmt, msg)
35 #define CheckStmt(stmt) \
39 DIE_UNLESS(stmt != 0); \
42 #define check_execute(stmt, r) \
45 mysterror(stmt, NULL); \
50 class SqlResultSet
: public Properties
{
53 // Get row with number
54 bool get_row(int row_num
);
59 // Remove current row from resultset
65 const char* column(const char* col_name
);
66 uint
columnAsInt(const char* col_name
);
72 const char* mysqlError();
73 const char* mysqlSqlstate();
76 uint
get_int(const char* name
);
77 const char* get_string(const char* name
);
79 const Properties
* m_curr_row
;
92 DbUtil(const char* dbname
= "mysql",
93 const char* user
= "root",
94 const char* pass
= "",
95 const char* suffix
= NULL
);
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
,
111 const char * password
,
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
);
133 int selectDb(const char *);
134 int createDb(BaseString
&);
135 int getErrorNumber();
137 unsigned long selectCountTable(const char * table
);
141 bool runQuery(const char* query
,
142 const Properties
& args
,
148 bool m_free_mysql
; /* Don't free mysql* if allocated elsewhere */
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