summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2005-06-19 21:46:57 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2005-06-19 21:46:57 +0000
commitd344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f (patch)
tree0508d527cc8b4128df0243a5d98a90509fd5116d /src
parent1308a690ac40a109cefaf59ff6c9384351a8bced (diff)
downloadmanaserv-d344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f.tar.gz
manaserv-d344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f.tar.bz2
manaserv-d344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f.tar.xz
manaserv-d344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f.zip
Correcting indent and removing obsoleted files.
Diffstat (limited to 'src')
-rw-r--r--src/dal/pqdataprovider.cpp74
-rw-r--r--src/dal/pqdataprovider.h26
-rw-r--r--src/dal/sqlitedataprovider.cpp4
-rw-r--r--src/log.cpp100
-rw-r--r--src/log.h65
-rw-r--r--src/sqlite/SQLiteWrapper.cpp265
-rw-r--r--src/sqlite/SQLiteWrapper.h139
-rw-r--r--src/testsmain.cpp4
8 files changed, 54 insertions, 623 deletions
diff --git a/src/dal/pqdataprovider.cpp b/src/dal/pqdataprovider.cpp
index b409151c..3fa8db45 100644
--- a/src/dal/pqdataprovider.cpp
+++ b/src/dal/pqdataprovider.cpp
@@ -59,24 +59,24 @@ PqDataProvider::getDbBackend(void) const
*/
void
PqDataProvider::connect(const std::string& dbName,
- const std::string& userName,
- const std::string& password)
+ const std::string& userName,
+ const std::string& password)
{
// Create string to pass to PQconnectdb
std::string connStr = "dbname = " + dbName + " "; // database name
if (userName != "")
- connStr += "user = " + userName + " "; // username
+ connStr += "user = " + userName + " "; // username
if (password != "")
- connStr += "password = " + password; // password
+ connStr += "password = " + password; // password
// Connect to database
mDb = PQconnectdb(connStr.c_str());
if (PQstatus(mDb) != CONNECTION_OK)
{
- std::string error = PQerrorMessage(mDb);
- PQfinish(mDb);
- throw DbConnectionFailure(error);
+ std::string error = PQerrorMessage(mDb);
+ PQfinish(mDb);
+ throw DbConnectionFailure(error);
}
mIsConnected = true;
@@ -87,58 +87,58 @@ PqDataProvider::connect(const std::string& dbName,
*/
const RecordSet&
PqDataProvider::execSql(const std::string& sql,
- const bool refresh)
+ const bool refresh)
{
if (!mIsConnected) {
- throw std::runtime_error("not connected to database");
+ throw std::runtime_error("not connected to database");
}
if (refresh || (sql != mSql)) {
- mRecordSet.clear();
+ mRecordSet.clear();
- // execute the query
- PGresult *res;
+ // execute the query
+ PGresult *res;
- res = PQexec(mDb, sql.c_str());
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- PQclear(res);
- throw DbSqlQueryExecFailure(PQerrorMessage(mDb));
- }
+ res = PQexec(mDb, sql.c_str());
+ if (PQresultStatus(res) != PGRES_COMMAND_OK) {
+ PQclear(res);
+ throw DbSqlQueryExecFailure(PQerrorMessage(mDb));
+ }
- // get field count
- unsigned int nFields = PQnfields(res);
+ // get field count
+ unsigned int nFields = PQnfields(res);
- // fill column names
- Row fieldNames;
- for (unsigned int i = 0; i < nFields; i++) {
- fieldNames.push_back(PQfname(res, i));
- }
- mRecordSet.setColumnHeaders(fieldNames);
+ // fill column names
+ Row fieldNames;
+ for (unsigned int i = 0; i < nFields; i++) {
+ fieldNames.push_back(PQfname(res, i));
+ }
+ mRecordSet.setColumnHeaders(fieldNames);
- // fill rows
- for (unsigned int r = 0; r < PQntuples(res); r++) {
- Row row;
+ // fill rows
+ for (unsigned int r = 0; r < PQntuples(res); r++) {
+ Row row;
- for (unsigned int i = 0; i < nFields; i++) {
- row.push_back(PQgetvalue(res, r, i));
- }
+ for (unsigned int i = 0; i < nFields; i++) {
+ row.push_back(PQgetvalue(res, r, i));
+ }
- mRecordSet.add(row);
- }
+ mRecordSet.add(row);
+ }
- // clear results
- PQclear(res);
+ // clear results
+ PQclear(res);
}
}
/**
- * Close connection to databse.
+ * Close connection to database.
*/
void
PqDataProvider::disconnect(void)
{
if (!mIsConnected) {
- return;
+ return;
}
// finish up with Postgre
diff --git a/src/dal/pqdataprovider.h b/src/dal/pqdataprovider.h
index 1e8beedc..069228d5 100644
--- a/src/dal/pqdataprovider.h
+++ b/src/dal/pqdataprovider.h
@@ -39,14 +39,14 @@ class PqDataProvider : public DataProvider
* Constructor
*/
PqDataProvider()
- throw();
-
+ throw();
+
/**
* Destructor
*/
~PqDataProvider()
- throw();
-
+ throw();
+
/**
* Get name of the database backend
*
@@ -54,8 +54,8 @@ class PqDataProvider : public DataProvider
*/
DbBackends
getDbBackend(void) const
- throw();
-
+ throw();
+
/**
* Create a connection to the database.
*
@@ -67,10 +67,10 @@ class PqDataProvider : public DataProvider
*/
void
connect(const std::string& dbName,
- const std::string& userName,
- const std::string& password);
-
-
+ const std::string& userName,
+ const std::string& password);
+
+
/**
* Execute a SQL query.
*
@@ -84,9 +84,9 @@ class PqDataProvider : public DataProvider
*/
const RecordSet&
execSql(const std::string& sql,
- const bool refresh = false);
-
-
+ const bool refresh = false);
+
+
/**
* Close the connection to the database.
*
diff --git a/src/dal/sqlitedataprovider.cpp b/src/dal/sqlitedataprovider.cpp
index 30de91c5..38001acb 100644
--- a/src/dal/sqlitedataprovider.cpp
+++ b/src/dal/sqlitedataprovider.cpp
@@ -119,8 +119,8 @@ SqLiteDataProvider::execSql(const std::string& sql,
mRecordSet.clear();
int errCode = sqlite3_get_table(
- mDb, // an open database
- sql.c_str(), // SQL to be executed
+ mDb, // an open database
+ sql.c_str(), // SQL to be executed
&result, // result of the query
&nRows, // number of result rows
&nCols, // number of result columns
diff --git a/src/log.cpp b/src/log.cpp
deleted file mode 100644
index a3b07abe..00000000
--- a/src/log.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * The Mana World Server
- * Copyright 2004 The Mana World Development Team
- *
- * This file is part of The Mana World.
- *
- * The Mana World is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * any later version.
- *
- * The Mana World is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with The Mana World; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "log.h"
-#ifdef WIN32
-#include <windows.h>
-#endif
-
-#include <sstream>
-
-
-Logger::Logger(const std::string &logFilename)
-{
- logFile.open(logFilename.c_str(), std::ios_base::trunc);
-
- if (!logFile.is_open())
- {
- std::cout << "Warning: error while opening " << logFilename <<
- " for writing.\n";
- }
-}
-
-Logger::~Logger()
-{
- if (logFile.is_open())
- {
- logFile.close();
- }
-}
-
-void Logger::log(const char *log_text, ...)
-{
- if (logFile.is_open())
- {
- char* buf = new char[1024];
- va_list ap;
- time_t t;
-
- // Use a temporary buffer to fill in the variables
- va_start(ap, log_text);
- vsprintf(buf, log_text, ap);
- va_end(ap);
-
- // Get the current system time
- time(&t);
-
- // Print the log entry
- std::stringstream timeStr;
- timeStr << "[";
- timeStr << ((((t / 60) / 60) % 24 < 10) ? "0" : "");
- timeStr << (int)(((t / 60) / 60) % 24);
- timeStr << ":";
- timeStr << (((t / 60) % 60 < 10) ? "0" : "");
- timeStr << (int)((t / 60) % 60);
- timeStr << ":";
- timeStr << ((t % 60 < 10) ? "0" : "");
- timeStr << (int)(t % 60);
- timeStr << "] ";
-
- logFile << timeStr.str() << buf << std::endl;
- std::cout << timeStr.str() << buf << std::endl;
-
- // Delete temporary buffer
- delete[] buf;
- }
-}
-
-void Logger::error(const std::string &error_text)
-{
- log("Error: %s", error_text.c_str());
-#ifdef WIN32
- MessageBox(NULL, error_text.c_str(), "Error", MB_ICONERROR | MB_OK);
-#else
- std::cerr << "Error: " << error_text << std::endl;
-#endif
- exit(1);
-}
-
-/**
- * And the instance used in the server
- */
-Logger *logger;
diff --git a/src/log.h b/src/log.h
deleted file mode 100644
index bb34ac21..00000000
--- a/src/log.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * The Mana World Server
- * Copyright 2004 The Mana World Development Team
- *
- * This file is part of The Mana World.
- *
- * The Mana World is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * any later version.
- *
- * The Mana World is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with The Mana World; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef _LOG_H
-#define _LOG_H
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <time.h>
-#include <string>
-#include <iostream>
-#include <fstream>
-
-/**
- * The Log Class : Useful to write debug or info messages
- */
-class Logger
-{
- public:
- /**
- * Constructor, opens log file for writing.
- */
- Logger(const std::string &logFilename);
-
- /**
- * Destructor, closes log file.
- */
- ~Logger();
-
- /**
- * Enters a message in the log. The message will be timestamped.
- */
- void log(const char *log_text, ...);
-
- /**
- * Log an error and quit. The error will pop-up in Windows and will be
- * printed to standard error everywhere else.
- */
- void error(const std::string &error_text);
-
- private:
- std::ofstream logFile;
-};
-
-extern Logger *logger;
-
-#endif
diff --git a/src/sqlite/SQLiteWrapper.cpp b/src/sqlite/SQLiteWrapper.cpp
deleted file mode 100644
index f5b484b7..00000000
--- a/src/sqlite/SQLiteWrapper.cpp
+++ /dev/null
@@ -1,265 +0,0 @@
-/*
- SQLiteWrapper.cpp
-
- Copyright (C) 2004 René Nyffenegger
-
- This source code is provided 'as-is', without any express or implied
- warranty. In no event will the author be held liable for any damages
- arising from the use of this software.
-
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
-
- 1. The origin of this source code must not be misrepresented; you must not
- claim that you wrote the original source code. If you use this source code
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
-
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original source code.
-
- 3. This notice may not be removed or altered from any source distribution.
-
- René Nyffenegger rene.nyffenegger@adp-gmbh.ch
- Modified by Mateusz Kaduk mateusz.kaduk@gmail.com
-
-*/
-
-#include "SQLiteWrapper.h"
-
-SQLiteWrapper::SQLiteWrapper() : db_(0) {
-}
-
-bool SQLiteWrapper::Open(std::string const& db_file) {
- if (sqlite3_open(db_file.c_str(), &db_) != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteWrapper::Close() {
- if (sqlite3_close(db_) != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteWrapper::SelectStmt(std::string const& stmt, ResultTable& res) {
- char *errmsg;
- int ret;
-
- res.reset();
-
- ret = sqlite3_exec(db_, stmt.c_str(), SelectCallback, static_cast<void*> (&res), &errmsg);
-
- if (ret != SQLITE_OK) {
- return false;
- }
- return true;
-// if (ret != SQLITE_OK) {
-// std::cout << stmt << " [" << errmsg << "]" << std::endl;
-// }
-}
-
-// TODO parameter p_col_names
-int SQLiteWrapper::SelectCallback(void *p_data, int num_fields, char **p_fields, char** /*p_col_names*/) {
- ResultTable* res = reinterpret_cast<ResultTable*>(p_data);
-
- ResultRecord record;
-
- for(int i=0; i < num_fields; i++) {
- record.fields_.push_back(p_fields[i]);
- }
-
- res->records_.push_back(record);
-
- return 0;
-}
-
-SQLiteStatement* SQLiteWrapper::Statement(std::string const& statement) {
- SQLiteStatement* stmt;
- try {
- stmt = new SQLiteStatement(statement, db_);
- return stmt;
- }
- catch (const char* e) {
- return 0;
- }
-}
-
-SQLiteStatement::SQLiteStatement(std::string const& statement, sqlite3* db) {
- if ( sqlite3_prepare(
- db,
- statement.c_str(), // stmt
- -1, // If than zero, then stmt is read up to the first nul terminator
- &stmt_,
- 0 // Pointer to unused portion of stmt
- )
- != SQLITE_OK) {
- throw sqlite3_errmsg(db);
- }
-
- if (!stmt_) {
- throw "stmt_ is 0";
- }
-}
-
-SQLiteStatement::~SQLiteStatement() {
-}
-
-SQLiteStatement::SQLiteStatement() :
- stmt_ (0)
-{
-}
-
-bool SQLiteStatement::Bind(int pos_zero_indexed, std::string const& value) {
- if (sqlite3_bind_text (
- stmt_,
- pos_zero_indexed+1, // Index of wildcard
- value.c_str(),
- value.length(), // length of text
- SQLITE_TRANSIENT // SQLITE_TRANSIENT: SQLite makes its own copy
- )
- != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::Bind(int pos_zero_indexed, double value) {
- if (sqlite3_bind_double(
- stmt_,
- pos_zero_indexed+1, // Index of wildcard
- value
- )
- != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::Bind(int pos_zero_indexed, int value) {
- if (sqlite3_bind_int(
- stmt_,
- pos_zero_indexed+1, // Index of wildcard
- value
- )
- != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::BindNull(int pos_zero_indexed) {
- if (sqlite3_bind_null(
- stmt_,
- pos_zero_indexed+1 // Index of wildcard
- )
- != SQLITE_OK) {
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::Execute() {
- int rc = sqlite3_step(stmt_);
- if (rc == SQLITE_BUSY) {
- //::MessageBox(0, "SQLITE_BUSY", 0, 0);
- return false;
- }
- if (rc == SQLITE_ERROR) {
- //::MessageBox(0, "SQLITE_ERROR", 0, 0);
- return false;
- }
- if (rc == SQLITE_MISUSE) {
- //::MessageBox(0, "SQLITE_ERROR", 0, 0);
- return false;
- }
- if (rc != SQLITE_DONE) {
- //sqlite3_reset(stmt_);
- return false;
- }
- sqlite3_reset(stmt_);
- return true;
-}
-
-SQLiteStatement::dataType SQLiteStatement::DataType(int pos_zero_indexed) {
- return dataType(sqlite3_column_type(stmt_, pos_zero_indexed));
-}
-
-int SQLiteStatement::ValueInt(int pos_zero_indexed) {
- return sqlite3_column_int(stmt_, pos_zero_indexed);
-}
-
-std::string SQLiteStatement::ValueString(int pos_zero_indexed) {
- return std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt_, pos_zero_indexed)));
-}
-
-bool SQLiteStatement::RestartSelect() {
- sqlite3_reset(stmt_);
- return true;
-}
-
-bool SQLiteStatement::Reset() {
- int rc = sqlite3_step(stmt_);
-
- sqlite3_reset(stmt_);
-
- if (rc == SQLITE_ROW) return true;
- return false;
-}
-
-bool SQLiteStatement::NextRow() {
- int rc = sqlite3_step(stmt_);
-
- if (rc == SQLITE_ROW ) {
- return true;
- }
- if (rc == SQLITE_DONE ) {
- sqlite3_reset(stmt_);
- return false;
- }
- else if (rc == SQLITE_MISUSE) {
- //::MessageBox(0, "SQLiteStatement::NextRow SQLITE_MISUSE", 0, 0);
- }
- else if (rc == SQLITE_BUSY ) {
- //::MessageBox(0, "SQLiteStatement::NextRow SQLITE_BUSY", 0, 0);
- }
- else if (rc == SQLITE_ERROR ) {
- //::MessageBox(0, "SQLiteStatement::NextRow SQLITE_ERROR", 0, 0);
- }
- return false;
-}
-
-bool SQLiteWrapper::DirectStatement(std::string const& stmt) {
- char *errmsg;
- int ret;
-
- ret = sqlite3_exec(db_, stmt.c_str(), 0, 0, &errmsg);
-
- if(ret != SQLITE_OK) {
- return false;
- }
- return true;
-
- //if(ret != SQLITE_OK) {
- // std::cout << stmt << " [" << errmsg << "]" << std::endl;
- //}
-}
-
-std::string SQLiteWrapper::LastError() {
- return sqlite3_errmsg(db_);
-}
-
-bool SQLiteWrapper::Begin() {
- return DirectStatement("begin");
-}
-
-bool SQLiteWrapper::Commit() {
- return DirectStatement("commit");
-}
-
-bool SQLiteWrapper::Rollback() {
- return DirectStatement("rollback");
-}
diff --git a/src/sqlite/SQLiteWrapper.h b/src/sqlite/SQLiteWrapper.h
deleted file mode 100644
index 419e3d50..00000000
--- a/src/sqlite/SQLiteWrapper.h
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- SQLiteWrapper.h
-
- Copyright (C) 2004 René Nyffenegger
-
- This source code is provided 'as-is', without any express or implied
- warranty. In no event will the author be held liable for any damages
- arising from the use of this software.
-
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
-
- 1. The origin of this source code must not be misrepresented; you must not
- claim that you wrote the original source code. If you use this source code
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
-
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original source code.
-
- 3. This notice may not be removed or altered from any source distribution.
-
- René Nyffenegger rene.nyffenegger@adp-gmbh.ch
- Modified by Mateusz Kaduk mateusz.kaduk@gmail.com
-*/
-
-#ifndef SQLITE_WRAPPER_H__
-#define SQLITE_WRAPPER_H__
-
-#include <string>
-#include <vector>
-
-#include "sqlite3.h"
-
-class SQLiteStatement {
- private:
- // SQLiteStatement's ctor only to be called by SQLiteWrapper
- friend class SQLiteWrapper;
- SQLiteStatement(std::string const& statement, sqlite3* db);
-
- public:
- SQLiteStatement();
-
- enum dataType {
- INT = SQLITE_INTEGER,
- FLT = SQLITE_FLOAT ,
- TXT = SQLITE_TEXT ,
- BLB = SQLITE_BLOB ,
- NUL = SQLITE_NULL ,
- };
-
- dataType DataType(int pos_zero_indexed);
-
- int ValueInt (int pos_zero_indexed);
- std::string ValueString(int pos_zero_indexed);
-
-// SQLiteStatement(const SQLiteStatement&);
- ~SQLiteStatement();
-
- //SQLiteStatement& operator=(SQLiteStatement const&);
-
- bool Bind (int pos_zero_indexed, std::string const& value);
- bool Bind (int pos_zero_indexed, double value);
- bool Bind (int pos_zero_indexed, int value);
- bool BindNull(int pos_zero_indexed);
-
- bool Execute();
-
- bool NextRow();
-
- /* Call Reset if not depending on the NextRow cleaning up.
- For example for select count(*) statements*/
- bool Reset();
-
- bool RestartSelect();
-
- private:
- //void DecreaseRefCounter();
-
- //int* ref_counter_; // not yet used...
- sqlite3_stmt* stmt_;
-};
-
-class SQLiteWrapper {
- public:
- SQLiteWrapper();
-
- bool Open(std::string const& db_file);
- bool Close();
-
- class ResultRecord {
- public:
- std::vector<std::string> fields_;
- };
-
- class ResultTable {
- friend class SQLiteWrapper;
- public:
- ResultTable() : ptr_cur_record_(0) {}
-
- std::vector<ResultRecord> records_;
-
- ResultRecord* next() {
- if (ptr_cur_record_ < records_.size()) {
- return &(records_[ptr_cur_record_++]);
- }
- return 0;
- }
-
- private:
- void reset() {
- records_.clear();
- ptr_cur_record_=0;
- }
-
- private:
- unsigned int ptr_cur_record_;
- };
-
- bool SelectStmt (std::string const& stmt, ResultTable& res);
- bool DirectStatement (std::string const& stmt);
- SQLiteStatement* Statement(std::string const& stmt);
-
- std::string LastError();
-
- // Transaction related
- bool Begin ();
- bool Commit ();
- bool Rollback();
-
- private:
-
- static int SelectCallback(void *p_data, int num_fields, char **p_fields, char **p_col_names);
-
- sqlite3* db_;
-};
-
-#endif
diff --git a/src/testsmain.cpp b/src/testsmain.cpp
index b9beadec..2f24cc48 100644
--- a/src/testsmain.cpp
+++ b/src/testsmain.cpp
@@ -37,8 +37,8 @@ int main(int argc, char* argv[])
runner.addTest(suite);
// run the tests.
- bool wasSucessful = runner.run();
+ bool wasSuccessful = runner.run();
// return error code 1 if the one of test failed.
- return wasSucessful ? 0 : 1;
+ return wasSuccessful ? 0 : 1;
}