00001 #ifndef MYSQLPP_CONNECTION_H
00002 #define MYSQLPP_CONNECTION_H
00003
00011
00012 #include "platform.h"
00013
00014 #include "exceptions.h"
00015 #include "result.h"
00016
00017 #include <mysql.h>
00018
00019 #include <vector>
00020 #include <deque>
00021 #include <list>
00022 #include <set>
00023 #include <map>
00024
00025 #ifdef HAVE_EXT_SLIST
00026 # include <ext/slist>
00027 #else
00028 # ifdef HAVE_STD_SLIST
00029 # include <slist>
00030 # endif
00031 #endif
00032
00033 namespace mysqlpp {
00034
00035 class Query;
00036
00038
00039 class Connection {
00040 private:
00041 friend class ResNSel;
00042 friend class ResUse;
00043 friend class Query;
00044
00045 bool throw_exceptions;
00046 MYSQL mysql;
00047 bool is_connected;
00048 bool locked;
00049 bool Success;
00050
00051 public:
00055 Connection();
00056
00062 Connection(bool te);
00063
00078 Connection(const char* db, const char* host = "",
00079 const char* user = "", const char* passwd = "",
00080 bool te = true);
00081
00107 Connection(const char* db, const char* host, const char* user,
00108 const char* passwd, uint port, my_bool compress = 0,
00109 unsigned int connect_timeout = 60, bool te = true,
00110 cchar* socket_name = 0, unsigned int client_flag = 0);
00111
00112 ~Connection();
00113
00121 bool connect(cchar* db = "", cchar* host = "", cchar* user = "",
00122 cchar* passwd = "");
00123
00134 bool real_connect(cchar* db = "", cchar* host = "",
00135 cchar* user = "", cchar* passwd = "", uint port = 0,
00136 my_bool compress = 0, unsigned int connect_timeout = 60,
00137 cchar* socket_name = 0, unsigned int client_flag = 0);
00138
00142 void close()
00143 {
00144 mysql_close(&mysql);
00145 is_connected = false;
00146 }
00147
00150 std::string info();
00151
00155 bool connected() const
00156 {
00157 return is_connected;
00158 }
00159
00163 bool success() const
00164 {
00165 return Success;
00166 }
00167
00168 bool lock()
00169 {
00170 if (locked) {
00171 return true;
00172 }
00173 locked = true;
00174 return false;
00175 }
00176 void unlock() { locked = false; }
00177
00178 void purge() { close(); }
00179
00187 Query query();
00188
00204 operator bool() { return success(); }
00205
00209 const char *error() { return mysql_error(&mysql); }
00210
00211 int errnum() { return mysql_errno(&mysql); }
00212 int refresh(unsigned int refresh_options)
00213 {
00214 return mysql_refresh(&mysql, refresh_options);
00215 }
00216 int ping() { return mysql_ping(&mysql); }
00217 int kill(unsigned long pid) { return mysql_kill(&mysql, pid); }
00218
00219 std::string client_info()
00220 {
00221 return std::string(mysql_get_client_info());
00222 }
00223
00224 std::string host_info()
00225 {
00226 return std::string(mysql_get_host_info(&mysql));
00227 }
00228
00229 int proto_info()
00230 {
00231 return mysql_get_proto_info(&mysql);
00232 }
00233
00234 std::string server_info()
00235 {
00236 return std::string(mysql_get_server_info(&mysql));
00237 }
00238
00239 std::string stat() { return std::string(mysql_stat(&mysql)); }
00240
00241 Result store(const std::string& str)
00242 {
00243 return store(str, throw_exceptions);
00244 }
00245 ResUse use(const std::string& str) {
00246 return use(str, throw_exceptions);
00247 }
00248 ResNSel execute(const std::string& str) {
00249 return execute(str, throw_exceptions);
00250 }
00251 bool exec(const std::string& str);
00252 Result store(const std::string& str, bool te);
00253 ResUse use(const std::string& str, bool te);
00254 ResNSel execute(const std::string& str, bool te);
00255
00256 bool create_db(std::string db)
00257 {
00258 return !execute("CREATE DATABASE " + db);
00259 }
00260 bool drop_db(std::string db)
00261 {
00262 return !execute("DROP DATABASE " + db);
00263 }
00264 bool select_db(std::string db) { return select_db(db.c_str()); }
00265 bool select_db(const char *db);
00266 bool reload();
00267 bool shutdown();
00268 std::string infoo() { return info(); }
00269 st_mysql_options get_options() const { return mysql.options; }
00270 int read_options(enum mysql_option option, const char *arg)
00271 {
00272 return mysql_options(&mysql, option, arg);
00273 }
00274 my_ulonglong affected_rows() { return mysql_affected_rows(&mysql); }
00275 my_ulonglong insert_id() { return mysql_insert_id(&mysql); }
00276
00277 template <class Sequence>
00278 void storein_sequence(Sequence&, const std::string&);
00279
00280 template <class Set>
00281 void storein_set(Set&, const std::string&);
00282
00283 template <class T>
00284 void storein(std::vector<T>& con, const std::string& s)
00285 {
00286 storein_sequence(con, s);
00287 }
00288
00289 template <class T>
00290 void storein(std::deque<T>& con, const std::string& s)
00291 {
00292 storein_sequence(con, s);
00293 }
00294
00295 template <class T>
00296 void storein(std::list<T>& con, const std::string& s)
00297 {
00298 storein_sequence(con, s);
00299 }
00300
00301 #if defined(HAVE_EXT_SLIST)
00302 template <class T>
00303 void storein(__gnu_cxx::slist<T>& con, const std::string& s)
00304 {
00305 storein_sequence(con, s);
00306 }
00307 #elif defined(HAVE_STD_SLIST)
00308 template <class T>
00309 void storein(slist<T>& con, const std::string& s)
00310 {
00311 storein_sequence(con, s);
00312 }
00313 #endif
00314
00315 template <class T>
00316 void storein(std::set<T>& con, const std::string& s)
00317 {
00318 storein_set(con, s);
00319 }
00320
00321 template <class T>
00322 void storein(std::multiset<T>& con, const std::string& s)
00323 {
00324 storein_set(con, s);
00325 }
00326 };
00327
00328
00329 template <class Sequence>
00330 void Connection::storein_sequence(Sequence& seq, const std::string& str)
00331 {
00332 ResUse result = use(str);
00333 while (1) {
00334 MYSQL_ROW d = mysql_fetch_row(result.mysql_result());
00335 if (!d)
00336 break;
00337 Row row(d, &result, mysql_fetch_lengths(result.mysql_result()),
00338 true);
00339 if (!row)
00340 break;
00341 seq.push_back(typename Sequence::value_type(row));
00342 }
00343 }
00344
00345 template <class Set>
00346 void Connection::storein_set(Set& sett, const std::string& str)
00347 {
00348 ResUse result = use(str);
00349 while (1) {
00350 MYSQL_ROW d = mysql_fetch_row(result.mysql_result());
00351 if (!d)
00352 return;
00353 Row row(d, &result, mysql_fetch_lengths(result.mysql_result()),
00354 true);
00355 if (!row)
00356 break;
00357 sett.insert(typename Set::value_type(row));
00358 }
00359 }
00360
00361 }
00362
00363 #endif
00364