MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DbUtil.hpp
1 /*
2  Copyright (C) 2007, 2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 // dbutil.h: interface for the database utilities class.
20 // Supplies a database to the test application
21 
22 #ifndef DBUTIL_HPP
23 #define DBUTIL_HPP
24 
25 #include <NDBT.hpp>
26 #include <BaseString.hpp>
27 #include <Properties.hpp>
28 #include <Vector.hpp>
29 #include <mysql.h>
30 
31 //#define DEBUG
32 #define DIE_UNLESS(expr) \
33  ((void) ((expr) ? 0 : (Die(__FILE__, __LINE__, #expr), 0)))
34 #define DIE(expr) \
35  Die(__FILE__, __LINE__, #expr)
36 #define myerror(msg) printError(msg)
37 #define mysterror(stmt, msg) printStError(stmt, msg)
38 #define CheckStmt(stmt) \
39 { \
40 if ( stmt == 0) \
41  myerror(NULL); \
42 DIE_UNLESS(stmt != 0); \
43 }
44 
45 #define check_execute(stmt, r) \
46 { \
47 if (r) \
48  mysterror(stmt, NULL); \
49 DIE_UNLESS(r == 0);\
50 }
51 
52 
53 class SqlResultSet : public Properties {
54 public:
55 
56  // Get row with number
57  bool get_row(int row_num);
58  // Load next row
59  bool next(void);
60  // Reset iterator
61  void reset(void);
62  // Remove current row from resultset
63  void remove();
64  // Clear result
65  void clear();
66 
67  SqlResultSet();
68  ~SqlResultSet();
69 
70  const char* column(const char* col_name);
71  uint columnAsInt(const char* col_name);
72  unsigned long long columnAsLong(const char* col_name);
73 
74  unsigned long long insertId();
75  unsigned long long affectedRows();
76  uint numRows(void);
77  uint mysqlErrno();
78  const char* mysqlError();
79  const char* mysqlSqlstate();
80 
81 private:
82  uint get_int(const char* name);
83  unsigned long long get_long(const char* name);
84  const char* get_string(const char* name);
85 
86  const Properties* m_curr_row;
87  uint m_curr_row_num;
88 };
89 
90 
91 class DbUtil
92 {
93 public:
94 
95  DbUtil(MYSQL* mysql);
96  DbUtil(const char* dbname = "mysql",
97  const char* suffix = NULL);
98  ~DbUtil();
99 
100  bool doQuery(const char* query);
101  bool doQuery(const char* query, SqlResultSet& result);
102  bool doQuery(const char* query, const Properties& args, SqlResultSet& result);
103  bool doQuery(const char* query, const Properties& args);
104 
105  bool doQuery(BaseString& str);
106  bool doQuery(BaseString& str, SqlResultSet& result);
107  bool doQuery(BaseString& str, const Properties& args, SqlResultSet& result);
108  bool doQuery(BaseString& str, const Properties& args);
109 
110  bool waitConnected(int timeout = 120);
111 
112  bool databaseLogin(const char * host,
113  const char * user,
114  const char * password,
115  unsigned int portIn,
116  const char * sockIn,
117  bool transactional);
118 
119  const char * getDbName() {return m_dbname.c_str();};
120  const char * getUser() {return m_user.c_str();};
121  const char * getPassword(){return m_pass.c_str();};
122  const char * getHost() {return m_host.c_str();};
123  const char * getSocket() {return m_socket.c_str();};
124  const char * getServerType(){return mysql_get_server_info(m_mysql);};
125  const char * getError();
126 
127  MYSQL * getMysql(){return m_mysql;};
128  MYSQL_STMT * STDCALL mysqlSimplePrepare(const char *query);
129 
130  void databaseLogout();
131  void mysqlCloseStmHandle(MYSQL_STMT *my_stmt);
132 
133  bool connect();
134  void disconnect();
135  bool selectDb();
136  bool selectDb(const char *);
137  bool createDb(BaseString&);
138  int getErrorNumber();
139  const char* last_error() const { return m_last_error.c_str(); }
140  int last_errno() const { return m_last_errno; }
141 
142  unsigned long long selectCountTable(const char * table);
143 
144  void silent() { m_silent= true; };
145 
146 protected:
147 
148  bool runQuery(const char* query,
149  const Properties& args,
150  SqlResultSet& rows);
151 
152  bool isConnected();
153 
154  MYSQL * m_mysql;
155  bool m_free_mysql; /* Don't free mysql* if allocated elsewhere */
156 
157 private:
158 
159  bool m_connected;
160 
161  BaseString m_host; // Computer to connect to
162  BaseString m_user; // MySQL User
163  BaseString m_pass; // MySQL User Password
164  BaseString m_dbname; // Database to use
165  BaseString m_socket; // MySQL Server Unix Socket
166  BaseString m_default_file;
167  BaseString m_default_group;
168 
169  unsigned int m_port; // MySQL Server port
170 
171  int m_last_errno;
172  BaseString m_last_error;
173 
174  bool m_silent;
175 
176  void report_error(const char* message, MYSQL* mysql);
177  void clear_error(void) { m_last_errno= 0; m_last_error.clear(); }
178 
179  void setDbName(const char * name){m_dbname.assign(name);};
180  void setUser(const char * user_name){m_user.assign(user_name);};
181  void setPassword(const char * password){m_pass.assign(password);};
182  void setHost(const char * system){m_host.assign(system);};
183  void setPort(unsigned int portIn){m_port=portIn;};
184  void setSocket(const char * sockIn){m_socket.assign(sockIn);};
185  void printError(const char *msg);
186  void printStError(MYSQL_STMT *stmt, const char *msg);
187  void die(const char *file, int line, const char *expr); // stop program
188 
189 };
190 #endif
191