00001 /* 00002 * A database abstraction layer for C++ and ACE framework 00003 * 00004 * (C) 2006-2007 Thralling Penguin LLC. All rights reserved. 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 #include <fstream> 00021 #include <iomanip> 00022 #include "ace/os_include/os_stddef.h" 00023 #include "ace/OS_Memory.h" 00024 #include "db.h" 00025 #include "mysql/mysql.h" 00026 00027 namespace DB 00028 { 00029 class MySQL_Connection; 00030 00031 class MySQL_ResultSet : public ResultSet 00032 { 00033 friend class MySQL_Connection; 00034 protected: 00035 MySQL_ResultSet(MYSQL_RES *res) : res_(res) {}; 00036 ~MySQL_ResultSet(); 00037 private: 00038 MySQL_ResultSet() {}; 00039 MySQL_ResultSet(const MySQL_ResultSet &old); 00040 const MySQL_ResultSet &operator=(const MySQL_ResultSet &old); 00041 00042 public: 00043 bool close(void); 00044 bool next(void); 00045 00046 unsigned long recordCount(void) const; 00047 unsigned int findColumn(const char *field) const; 00048 00049 const char *getString(const int idx) const; 00050 const int getInteger(const int idx) const; 00051 const bool getBool(const int idx) const; 00052 const time_t getUnixTime(const int idx) const; 00053 const double getDouble(const int idx) const; 00054 const float getFloat(const int idx) const; 00055 const long getLong(const int idx) const; 00056 const short getShort(const int idx) const; 00057 00058 // Overload the new/delete opertors so the object will be 00059 // created/deleted using the memory allocator associated with the 00060 // DLL/SO. 00061 void *operator new (size_t bytes); 00062 #if defined (ACE_HAS_NEW_NOTHROW) 00063 // Overloaded new operator, nothrow_t variant. 00064 void *operator new (size_t bytes, const ACE_nothrow_t&); 00065 #if !defined (ACE_LACKS_PLACEMENT_OPERATOR_DELETE) 00066 void operator delete (void *p, const ACE_nothrow_t&) throw (); 00067 #endif /* ACE_LACKS_PLACEMENT_OPERATOR_DELETE */ 00068 #endif 00069 void operator delete (void *ptr); 00070 00071 private: 00072 MYSQL_RES *res_; 00073 MYSQL_ROW row_; 00074 }; 00075 00076 class MySQL_Connection : public Connection 00077 { 00078 private: 00079 MySQL_Connection(const MySQL_Connection &old); 00080 const MySQL_Connection &operator=(const MySQL_Connection &old); 00081 00082 public: 00083 MySQL_Connection() : mysql_(NULL) {}; 00084 ~MySQL_Connection() { close(); } 00085 00086 bool open(const char *database, const char *host, const int port, const char *user, const char *pass); 00087 bool close(void); 00088 bool isConnected(void); 00089 bool execute(const char *sql); 00090 ResultSet *executeQuery(const char *sql); 00091 char *escape(const char *); 00092 const char *unixtimeToSql(const time_t); 00093 const unsigned long insertId(void); 00094 00095 bool beginTrans(void); 00096 bool commitTrans(void); 00097 bool rollbackTrans(void); 00098 bool setTransactionMode(const enum TRANS_MODE mode); 00099 00100 unsigned int errorno(void) const; 00101 const char *errormsg(void) const; 00102 00103 const char *version(void) const; 00104 00105 // Overload the new/delete opertors so the object will be 00106 // created/deleted using the memory allocator associated with the 00107 // DLL/SO. 00108 void *operator new (size_t bytes); 00109 #if defined (ACE_HAS_NEW_NOTHROW) 00110 // Overloaded new operator, nothrow_t variant. 00111 void *operator new (size_t bytes, const ACE_nothrow_t&); 00112 #if !defined (ACE_LACKS_PLACEMENT_OPERATOR_DELETE) 00113 void operator delete (void *p, const ACE_nothrow_t&) throw (); 00114 #endif /* ACE_LACKS_PLACEMENT_OPERATOR_DELETE */ 00115 #endif 00116 void operator delete (void *ptr); 00117 00118 private: 00119 MYSQL *mysql_; 00120 }; 00121 }
1.4.7