|
UniSet
2.0.0
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2002 Free Software Foundation, Inc. 00003 * Copyright (c) 2002 Pavel Vainerman 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00023 //---------------------------------------------------------------------------- 00024 #ifndef MySQLInterface_H_ 00025 #define MySQLInterface_H_ 00026 // --------------------------------------------------------------------------- 00027 #include <string> 00028 #include <vector> 00029 #include <deque> 00030 #include <iostream> 00031 //#warning Для использования mysql_create нужен define USE_OLD_FUNCTIONS 00032 //#define USE_OLD_FUNCTIONS 00033 #include <mysql/mysql.h> 00034 // ---------------------------------------------------------------------------- 00035 class MySQLResult; 00036 // ---------------------------------------------------------------------------- 00037 class MySQLInterface 00038 { 00039 public: 00040 00041 MySQLInterface(); 00042 ~MySQLInterface(); 00043 00044 // MySQLResult listFields( const std::string& table, const std::string& wild ); 00045 00046 bool connect( const std::string& host, const std::string& user, const std::string& pswd, 00047 const std::string& dbname); 00048 bool close(); 00049 00050 bool query_ok( const std::string& q ); 00051 00052 // \param finalize - освободить буфер после запроса 00053 MySQLResult query( const std::string& q ); 00054 00055 const std::string lastQuery(); 00056 bool insert( const std::string& q ); 00057 00058 std::string addslashes(const std::string& str); 00059 00064 bool ping(); 00065 00067 bool isConnection(); 00068 00069 int insert_id(); 00070 00071 const std::string error(); 00072 00073 // ******************* 00074 const char* gethostinfo(); 00075 protected: 00076 00077 private: 00078 00079 MYSQL *mysql; 00080 std::string lastQ; 00081 bool connected; 00082 }; 00083 // ---------------------------------------------------------------------------------- 00084 class MySQLResult 00085 { 00086 public: 00087 MySQLResult(){} 00088 MySQLResult( MYSQL_RES* r, bool finalize=true ); 00089 ~MySQLResult(); 00090 00091 typedef std::vector<std::string> COL; 00092 typedef std::deque<COL> ROW; 00093 00094 typedef ROW::iterator iterator; 00095 00096 inline iterator begin(){ return res.begin(); } 00097 inline iterator end(){ return res.end(); } 00098 00099 inline operator bool(){ return !res.empty(); } 00100 00101 inline size_t size(){ return res.size(); } 00102 inline bool empty(){ return res.empty(); } 00103 00104 protected: 00105 00106 ROW res; 00107 }; 00108 // ---------------------------------------------------------------------------------- 00109 int num_cols( MySQLResult::iterator& ); 00110 // ROW 00111 int as_int( MySQLResult::iterator&, int col ); 00112 double as_double( MySQLResult::iterator&, int col ); 00113 std::string as_text( MySQLResult::iterator&, int col ); 00114 // ---------------------------------------------------------------------------- 00115 // COL 00116 int as_int( MySQLResult::COL::iterator& ); 00117 double as_double( MySQLResult::COL::iterator& ); 00118 std::string as_string( MySQLResult::COL::iterator& ); 00119 // ---------------------------------------------------------------------------- 00120 #endif
1.7.6.1