reSIProcate/repro  9694
MySqlDb.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_MYSQLDB_HXX)
00002 #define RESIP_MYSQLDB_HXX 
00003 
00004 #ifdef WIN32
00005 #include <mysql.h>
00006 #else 
00007 #include <mysql/mysql.h>
00008 #endif
00009 
00010 #include "rutil/Data.hxx"
00011 #include "repro/AbstractDb.hxx"
00012 
00013 namespace resip
00014 {
00015   class TransactionUser;
00016 }
00017 
00018 namespace repro
00019 {
00020 
00021 class MySqlDb: public AbstractDb
00022 {
00023    public:
00024       MySqlDb(const resip::Data& dbServer, 
00025               const resip::Data& user, 
00026               const resip::Data& password, 
00027               const resip::Data& databaseName, 
00028               unsigned int port, 
00029               const resip::Data& customUserAuthQuery);
00030       
00031       virtual ~MySqlDb();
00032       
00033       virtual bool isSane() {return mConnected;}
00034 
00035       virtual bool addUser( const Key& key, const UserRecord& rec );
00036       virtual void eraseUser( const Key& key );
00037       virtual UserRecord getUser( const Key& key ) const;
00038       virtual resip::Data getUserAuthInfo(  const Key& key ) const;
00039       virtual Key firstUserKey();// return empty if no more
00040       virtual Key nextUserKey(); // return empty if no more 
00041 
00042       // Perform a query that expects a single result/row - returns all column/field data in a vector
00043       virtual int singleResultQuery(const resip::Data& queryCommand, std::vector<resip::Data>& fields) const;
00044 
00045    private:
00046       // Db manipulation routines
00047       virtual bool dbWriteRecord(const Table table, 
00048                                  const resip::Data& key, 
00049                                  const resip::Data& data);
00050       virtual bool dbReadRecord(const Table table, 
00051                                 const resip::Data& key, 
00052                                 resip::Data& data) const; // return false if not found
00053       virtual void dbEraseRecord(const Table table, 
00054                                  const resip::Data& key,
00055                                  bool isSecondaryKey=false);  // allows deleting records from a table that supports secondary keying using a secondary key
00056       virtual resip::Data dbNextKey(const Table table, 
00057                                     bool first=true); // return empty if no more
00058       virtual bool dbNextRecord(const Table table,
00059                                 const resip::Data& key,
00060                                 resip::Data& data,
00061                                 bool forUpdate, // specifying to add SELECT ... FOR UPDATE so the rows are locked
00062                                 bool first=false);  // return false if no more
00063       virtual bool dbBeginTransaction(const Table table);
00064       virtual bool dbCommitTransaction(const Table table);
00065       virtual bool dbRollbackTransaction(const Table table);
00066 
00067       void initialize() const;
00068       void disconnectFromDatabase() const;
00069       int connectToDatabase() const;
00070       int query(const resip::Data& queryCommand, MYSQL_RES** result) const;
00071       resip::Data& escapeString(const resip::Data& str, resip::Data& escapedStr) const;
00072 
00073       resip::Data mDBServer;
00074       resip::Data mDBUser;
00075       resip::Data mDBPassword;
00076       resip::Data mDBName;
00077       unsigned int mDBPort;
00078       resip::Data mCustomUserAuthQuery;
00079 
00080       mutable MYSQL* mConn;
00081       mutable MYSQL_RES* mResult[MaxTable];
00082       mutable volatile bool mConnected;
00083       // when multiple threads are in use with the same connection, you need to 
00084       // mutex calls to mysql_query and mysql_store_result: 
00085       // http://dev.mysql.com/doc/refman/5.1/en/threaded-clients.html
00086       mutable resip::Mutex mMutex;  
00087 
00088       const char* tableName( Table table ) const;
00089       void userWhereClauseToDataStream(const Key& key, resip::DataStream& ds) const;
00090       void getUserAndDomainFromKey(const AbstractDb::Key& key, resip::Data& user, resip::Data& domain) const;
00091 };
00092 
00093 }
00094 #endif  
00095 
00096 /* ====================================================================
00097  * The Vovida Software License, Version 1.0 
00098  * 
00099  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00100  * 
00101  * Redistribution and use in source and binary forms, with or without
00102  * modification, are permitted provided that the following conditions
00103  * are met:
00104  * 
00105  * 1. Redistributions of source code must retain the above copyright
00106  *    notice, this list of conditions and the following disclaimer.
00107  * 
00108  * 2. Redistributions in binary form must reproduce the above copyright
00109  *    notice, this list of conditions and the following disclaimer in
00110  *    the documentation and/or other materials provided with the
00111  *    distribution.
00112  * 
00113  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00114  *    and "Vovida Open Communication Application Library (VOCAL)" must
00115  *    not be used to endorse or promote products derived from this
00116  *    software without prior written permission. For written
00117  *    permission, please contact vocal@vovida.org.
00118  *
00119  * 4. Products derived from this software may not be called "VOCAL", nor
00120  *    may "VOCAL" appear in their name, without prior written
00121  *    permission of Vovida Networks, Inc.
00122  * 
00123  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00124  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00125  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00126  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00127  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00128  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00129  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00130  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00131  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00132  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00133  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00134  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00135  * DAMAGE.
00136  * 
00137  * ====================================================================
00138  */