pqdataprovider.cpp

Go to the documentation of this file.
00001 /*
00002  *  The Mana World Server
00003  *  Copyright 2004 The Mana World Development Team
00004  *
00005  *  This file is part of The Mana World.
00006  *
00007  *  The Mana World  is free software; you can redistribute  it and/or modify it
00008  *  under the terms of the GNU General  Public License as published by the Free
00009  *  Software Foundation; either version 2 of the License, or any later version.
00010  *
00011  *  The Mana  World is  distributed in  the hope  that it  will be  useful, but
00012  *  WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
00013  *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00014  *  more details.
00015  *
00016  *  You should  have received a  copy of the  GNU General Public  License along
00017  *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
00018  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  *
00020  *  $Id: pqdataprovider.cpp 3153 2007-02-27 16:39:07Z crush_tmw $
00021  */
00022 
00023 
00024 #include "pqdataprovider.h"
00025 #include "dalexcept.h"
00026 
00027 namespace dal
00028 {
00029 
00030 
00034 PqDataProvider::PqDataProvider(void)
00035     throw()
00036         : mDb(0)
00037 {
00038     // NOOP
00039 }
00040 
00041 
00045 PqDataProvider::~PqDataProvider(void)
00046     throw()
00047 {
00048     if (mIsConnected) {
00049         disconnect();
00050     }
00051 }
00052 
00053 
00057 DbBackends
00058 PqDataProvider::getDbBackend(void) const
00059     throw()
00060 {
00061     return DB_BKEND_POSTGRESQL;
00062 }
00063 
00064 
00068 void
00069 PqDataProvider::connect(const std::string& dbName,
00070                         const std::string& userName,
00071                         const std::string& password)
00072 {
00073     // Create string to pass to PQconnectdb
00074     std::string connStr = "dbname = " + dbName + " "; // database name
00075     if (userName != "")
00076         connStr += "user = " + userName + " "; // username
00077     if (password != "")
00078         connStr += "password = " + password; // password
00079 
00080     // Connect to database
00081     mDb = PQconnectdb(connStr.c_str());
00082 
00083     if (PQstatus(mDb) != CONNECTION_OK)
00084     {
00085         std::string error = PQerrorMessage(mDb);
00086         PQfinish(mDb);
00087         throw DbConnectionFailure(error);
00088     }
00089 
00090     // Save the Db Name.
00091     mDbName = dbName;
00092 
00093     mIsConnected = true;
00094 }
00095 
00096 
00100 const RecordSet&
00101 PqDataProvider::execSql(const std::string& sql,
00102                         const bool refresh)
00103 {
00104     if (!mIsConnected) {
00105         throw std::runtime_error("not connected to database");
00106     }
00107 
00108     if (refresh || (sql != mSql)) {
00109         mRecordSet.clear();
00110 
00111         // execute the query
00112         PGresult *res;
00113 
00114         res = PQexec(mDb, sql.c_str());
00115         if (PQresultStatus(res) != PGRES_COMMAND_OK) {
00116             PQclear(res);
00117             throw DbSqlQueryExecFailure(PQerrorMessage(mDb));
00118         }
00119 
00120         // get field count
00121         unsigned int nFields = PQnfields(res);
00122 
00123         // fill column names
00124         Row fieldNames;
00125         for (unsigned int i = 0; i < nFields; i++) {
00126             fieldNames.push_back(PQfname(res, i));
00127         }
00128         mRecordSet.setColumnHeaders(fieldNames);
00129 
00130         // fill rows
00131         for (unsigned int r = 0; r < PQntuples(res); r++) {
00132             Row row;
00133 
00134             for (unsigned int i = 0; i < nFields; i++) {
00135                 row.push_back(PQgetvalue(res, r, i));
00136             }
00137 
00138             mRecordSet.add(row);
00139         }
00140 
00141         // clear results
00142         PQclear(res);
00143     }
00144     return mRecordSet;
00145 }
00146 
00147 
00151 void
00152 PqDataProvider::disconnect(void)
00153 {
00154     if (!mIsConnected) {
00155         return;
00156     }
00157 
00158     // finish up with Postgre.
00159     PQfinish(mDb);
00160 
00161     mDb = 0;
00162     mIsConnected = false;
00163 }
00164 
00165 
00166 } // namespace dal

Generated on Fri Mar 30 15:39:16 2007 for TMW Server by  doxygen 1.3.9.1