Commit 4a0181f7 authored by louiz’'s avatar louiz’

Remove all references to database module

parent 86e95a62
......@@ -2,15 +2,6 @@
GPATH
GRTAGS
GTAGS
# CODE BLOCKS
*.cbp
*.depend
*.layout
src/database/batajelo.conf
src/database/bin
src/database/obj
# DLL
src/database/libmysql.dll
# CONF
src/batajelo.conf
# DOXYGEN
......@@ -32,4 +23,3 @@ bin
CMakeFiles
output_lib
Win32
......@@ -17,4 +17,3 @@ For SFGUI:
For Batajelo:
boost-devel
cryptopp-devel
mysql-devel
\ No newline at end of file
......@@ -11,6 +11,5 @@ libopenal-dev
libsndfile1-dev
For Batajelo:
libmysqlclient-dev
libboost1.48-all-dev
libcrypto++-dev
# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
#
# The MySQL Connector/C++ is licensed under the terms of the GPLv2
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
# MySQL Connectors. There are special exceptions to the terms and
# conditions of the GPLv2 as it is applied to this software, see the
# FLOSS License Exception
# <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
#
# This program 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; version 2 of the License.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
##########################################################################
MACRO(_MYSQL_CONFIG VAR _regex _opt)
EXECUTE_PROCESS(COMMAND ${MYSQL_CONFIG_EXECUTABLE} ${_opt}
OUTPUT_VARIABLE _mysql_config_output
)
SET(_var ${_mysql_config_output})
STRING(REGEX MATCHALL "${_regex}([^ ]+)" _mysql_config_output "${_mysql_config_output}")
STRING(REGEX REPLACE "^[ \t]+" "" _mysql_config_output "${_mysql_config_output}")
STRING(REGEX REPLACE "[\r\n]$" "" _mysql_config_output "${_mysql_config_output}")
STRING(REGEX REPLACE "${_regex}" "" _mysql_config_output "${_mysql_config_output}")
SEPARATE_ARGUMENTS(_mysql_config_output)
SET(${VAR} ${_mysql_config_output})
ENDMACRO(_MYSQL_CONFIG _regex _opt)
IF (NOT MYSQL_CONFIG_EXECUTABLE AND NOT WIN32)
IF (EXISTS "$ENV{MYSQL_DIR}/bin/mysql_config")
SET(MYSQL_CONFIG_EXECUTABLE "$ENV{MYSQL_DIR}/bin/mysql_config")
ELSE (EXISTS "$ENV{MYSQL_DIR}/bin/mysql_config")
FIND_PROGRAM(MYSQL_CONFIG_EXECUTABLE
NAMES mysql_config
DOC "full path of mysql_config"
PATHS /usr/bin
/usr/local/bin
/opt/mysql/mysql/bin
/usr/local/mysql/bin
)
ENDIF (EXISTS "$ENV{MYSQL_DIR}/bin/mysql_config")
ENDIF (NOT MYSQL_CONFIG_EXECUTABLE AND NOT WIN32)
#-------------- FIND MYSQL_INCLUDE_DIR ------------------
SET(MYSQL_CXXFLAGS "")
IF(MYSQL_CONFIG_EXECUTABLE AND NOT WIN32)
_MYSQL_CONFIG(MYSQL_INCLUDE_DIR "(^| )-I" "--include")
MESSAGE(STATUS "mysql_config was found ${MYSQL_CONFIG_EXECUTABLE}")
EXECUTE_PROCESS(COMMAND ${MYSQL_CONFIG_EXECUTABLE} "--cflags"
OUTPUT_VARIABLE _mysql_config_output
)
STRING(REGEX MATCHALL "-m([^\r\n]+)" MYSQL_LINK_FLAGS "${_mysql_config_output}")
STRING(REGEX REPLACE "[\r\n]$" "" MYSQL_CXXFLAGS "${_mysql_config_output}")
# ADD_DEFINITIONS("${MYSQL_CXXFLAGS}")
ELSE (MYSQL_CONFIG_EXECUTABLE AND NOT WIN32)
MESSAGE(STATUS "ENV{MYSQL_DIR} = $ENV{MYSQL_DIR}")
FIND_PATH(MYSQL_INCLUDE_DIR mysql.h
$ENV{MYSQL_INCLUDE_DIR}
$ENV{MYSQL_DIR}/include
/usr/include/mysql
/usr/local/include/mysql
/opt/mysql/mysql/include
/opt/mysql/mysql/include/mysql
/usr/local/mysql/include
/usr/local/mysql/include/mysql
$ENV{ProgramFiles}/MySQL/*/include
$ENV{SystemDrive}/MySQL/*/include)
ENDIF (MYSQL_CONFIG_EXECUTABLE AND NOT WIN32)
#----------------- FIND MYSQL_LIB_DIR -------------------
IF (WIN32)
# Set lib path suffixes
# dist = for mysql binary distributions
# build = for custom built tree
IF (CMAKE_BUILD_TYPE STREQUAL Debug)
SET(libsuffixDist debug)
SET(libsuffixBuild Debug)
ELSE (CMAKE_BUILD_TYPE STREQUAL Debug)
SET(libsuffixDist opt)
SET(libsuffixBuild Release)
ADD_DEFINITIONS(-DDBUG_OFF)
ENDIF (CMAKE_BUILD_TYPE STREQUAL Debug)
FIND_LIBRARY(MYSQL_LIB NAMES mysqlclient
PATHS
$ENV{MYSQL_DIR}/lib/${libsuffixDist}
$ENV{MYSQL_DIR}/lib #mysqlclient may be in lib for some c/c distros
$ENV{MYSQL_DIR}/libmysql/${libsuffixBuild}
$ENV{MYSQL_DIR}/client/${libsuffixBuild}
$ENV{ProgramFiles}/MySQL/*/lib/${libsuffixDist}
$ENV{ProgramFiles}/MySQL/*/lib
$ENV{SystemDrive}/MySQL/*/lib/${libsuffixDist}
$ENV{SystemDrive}/MySQL/*/lib)
IF(MYSQL_LIB)
SET(MYSQLCPPCONN_DYNLOAD_MYSQL_LIB MYSQL_LIB)
GET_FILENAME_COMPONENT(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
ENDIF(MYSQL_LIB)
ADD_DEFINITIONS("-DDYNLOAD_MYSQL_LIB=\"${MYSQLCPPCONN_DYNLOAD_MYSQL_LIB}\"")
ELSE (WIN32)
IF (MYSQL_CONFIG_EXECUTABLE)
_MYSQL_CONFIG(MYSQL_LIBRARIES "(^| )-l" "--libs_r")
_MYSQL_CONFIG(MYSQL_LIB_DIR "(^| )-L" "--libs_r")
EXECUTE_PROCESS(COMMAND ${MYSQL_CONFIG_EXECUTABLE} "--version"
OUTPUT_VARIABLE __MYSQL_VERSION
)
# Test it
#SET(__MYSQL_VERSION "4.0.10")
# trim the string
STRING(REGEX REPLACE "[\r\n]$" "" __MYSQL_VERSION "${__MYSQL_VERSION}")
STRING(REGEX MATCHALL "(4.1.[0-9]+|[5-9].[0-9]+.[0-9]+)" MYSQL_VERSION "${__MYSQL_VERSION}")
FIND_LIBRARY(MYSQLCPPCONN_DYNLOAD_MYSQL_LIB NAMES mysqlclient_r
PATHS
$ENV{MYSQL_DIR}/libmysql_r/.libs
$ENV{MYSQL_DIR}/lib
$ENV{MYSQL_DIR}/lib/mysql
/usr/lib/mysql
/usr/local/lib/mysql
/usr/local/mysql/lib
/usr/local/mysql/lib/mysql
/opt/mysql/mysql/lib
/opt/mysql/mysql/lib/mysql)
ADD_DEFINITIONS("-DDYNLOAD_MYSQL_LIB=\"${MYSQLCPPCONN_DYNLOAD_MYSQL_LIB}\"")
ELSE (MYSQL_CONFIG_EXECUTABLE)
FIND_LIBRARY(MYSQL_LIB NAMES mysqlclient_r
PATHS
$ENV{MYSQL_DIR}/libmysql_r/.libs
$ENV{MYSQL_DIR}/lib
$ENV{MYSQL_DIR}/lib/mysql
/usr/lib/mysql
/usr/local/lib/mysql
/usr/local/mysql/lib
/usr/local/mysql/lib/mysql
/opt/mysql/mysql/lib
/opt/mysql/mysql/lib/mysql)
SET(MYSQL_LIBRARIES mysqlclient_r )
IF(MYSQL_LIB)
GET_FILENAME_COMPONENT(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
SET(MYSQLCPPCONN_DYNLOAD_MYSQL_LIB MYSQL_LIB)
ENDIF(MYSQL_LIB)
ENDIF (MYSQL_CONFIG_EXECUTABLE)
ENDIF (WIN32)
SET(VERBOSE 1)
IF (MYSQL_INCLUDE_DIR AND MYSQL_LIB_DIR)
MESSAGE(STATUS "MySQL Include dir: ${MYSQL_INCLUDE_DIR}")
MESSAGE(STATUS "MySQL Library : ${MYSQL_LIB}")
MESSAGE(STATUS "MySQL Library dir: ${MYSQL_LIB_DIR}")
MESSAGE(STATUS "MySQL CXXFLAGS: ${MYSQL_CXXFLAGS}")
MESSAGE(STATUS "MySQL Link flags: ${MYSQL_LINK_FLAGS}")
IF (MYSQL_VERSION)
MESSAGE(STATUS "MySQL Version: ${MYSQL_VERSION}")
ELSE (MYSQL_VERSION)
IF(WIN32)
#SET(CMAKE_REQUIRED_LIBRARIES ${MYSQL_LIB})
ELSE(WIN32)
# For now this works only on *nix
SET(CMAKE_REQUIRED_LIBRARIES ${MYSQL_LIBRARIES})
SET(CMAKE_REQUIRED_INCLUDES ${MYSQL_INCLUDE_DIR})
CHECK_FUNCTION_EXISTS("mysql_set_character_set" HAVE_SET_CHARSET)
SET(CMAKE_REQUIRED_LIBRARIES)
SET(CMAKE_REQUIRED_INCLUDES)
IF (HAVE_SET_CHARSET)
MESSAGE(STATUS "libmysql version - ok")
ELSE (HAVE_SET_CHARSET)
MESSAGE(FATAL_ERROR "Versions < 4.1.13 (for MySQL 4.1.x) and < 5.0.7 for (MySQL 5.0.x) are not supported. Please update your libraries.")
ENDIF (HAVE_SET_CHARSET)
ENDIF(WIN32)
ENDIF(MYSQL_VERSION)
MESSAGE(STATUS "MySQL dynamic load test library: ${MYSQLCPPCONN_DYNLOAD_MYSQL_LIB}")
#IF(MYSQLCPPCONN_DYNLOAD_MYSQL_LIB)
#ADD_DEFINITIONS("-DDYNLOAD_MYSQL_LIB=\"${MYSQLCPPCONN_DYNLOAD_MYSQL_LIB}\"")
#ENDIF(MYSQLCPPCONN_DYNLOAD_MYSQL_LIB)
INCLUDE_DIRECTORIES(${MYSQL_INCLUDE_DIR})
LINK_DIRECTORIES(${MYSQL_LIB_DIR})
SET(MYSQL_FOUND TRUE)
ELSE (MYSQL_INCLUDE_DIR AND MYSQL_LIB_DIR)
IF(NOT WIN32)
MESSAGE(SEND_ERROR "mysql_config wasn't found, -DMYSQL_CONFIG_EXECUTABLE=...")
ENDIF(NOT WIN32)
MESSAGE(FATAL_ERROR "Cannot find MySQL. Include dir: ${MYSQL_INCLUDE_DIR} library dir: ${MYSQL_LIB_DIR} cxxflags: ${MYSQL_CXXFLAGS}")
ENDIF (MYSQL_INCLUDE_DIR AND MYSQL_LIB_DIR)
/** @defgroup Database
*
* This module groups the database manager & objects.
*
*/
/** @defgroup User
*
* This module groups the user actions.
......@@ -15,4 +9,4 @@
*
* This module groups the user interface.
*
*/
\ No newline at end of file
*/
#include <database/database.hpp>
#include <logging/logging.hpp>
#include <config/config.hpp>
Database* Database::instance = nullptr;
Database::Database():
mysql(NULL),
connected(false)
{
if (this->init() == false)
throw "5";
}
Database::~Database()
{
this->close();
}
bool Database::init()
{
this->mysql = mysql_init(NULL);
if (this->mysql == NULL)
{
log_error("Could'nt init a mysql connection.");
return false;
}
return true;
}
Database* Database::inst()
{
if (instance == nullptr)
instance = new Database;
return instance;
}
bool Database::connect()
{
if (this->connected == true)
return true;
log_debug("Connecting to database using: host:" << Config::get("db_host", "localhost").c_str() << " user:" << Config::get("db_user", "root").c_str() << " pass:" << Config::get("db_password", "").c_str() << " db:" << Config::get("db_database", "batajelo").c_str());
if (mysql_real_connect(this->mysql,
Config::get("db_host", "localhost").c_str(),
Config::get("db_user", "root").c_str(),
Config::get("db_password", "").c_str(),
Config::get("db_database", "batajelo").c_str(),
DB_PORT, DB_UNIX_SOCKET, DB_CLIENT_FLAG) == NULL)
{
throw "5";
return false;
}
else
{
this->connected = true;
return true;
}
}
void Database::close()
{
if (this->mysql != NULL)
mysql_close(this->mysql);
this->connected = false;
}
MYSQL_RES* Database::do_query(const std::string& query)
{
if (this->connect() == false)
throw "5";
log_debug("Doing query [" << query << "]");
const unsigned int error = mysql_query(this->mysql, query.c_str());
if (error != 0)
{
log_error("Couldn't query the database: " << error);
this->connected = false;
throw "5";
}
MYSQL_RES* result = mysql_use_result(this->mysql);
if (!result)
throw "5";
return result;
}
bool Database::do_update(const std::string& query)
{
if (!this->connect())
return false;
log_debug("Doing update [" << query << "]");
const unsigned int error = mysql_query(this->mysql, query.c_str());
if (error != 0)
{
log_error("Couldn't query the database: " << error);
this->connected = false;
return false;
}
if (mysql_affected_rows(this->mysql) == 0)
return false;
return true;
}
bool Database::do_remove(const std::string& query)
{
if (!this->connect())
return false;
log_debug("Doing delete [" << query << "]");
const unsigned int error = mysql_query(this->mysql, query.c_str());
if (error != 0)
{
log_error("Couldn't query the database: " << error);
this->connected = false;
return false;
}
if (mysql_affected_rows(this->mysql) == 0)
return false;
return true;
}
DbObject* Database::get_object(const std::string& columns,
const std::string& table,
const std::string& where)
{
MYSQL_RES* result = this->do_query("SELECT " + columns + " FROM " + table + " WHERE " + where);
if (!result)
return NULL;
MYSQL_ROW mysql_row = mysql_fetch_row(result);
// If there’s no result, we just return NULL
if (mysql_row == NULL )
{
log_warning("No row returned");
return NULL;
}
// We can fill the object with the data from the database
DbObject* db_object = new DbObject;
unsigned int fields_number = mysql_num_fields(result);
unsigned int field_id = 0;
MYSQL_FIELD* fields = mysql_fetch_fields(result);
for (; field_id < fields_number; field_id++)
if (mysql_row[field_id]) // if the value is NULL, we don’t append anything
db_object->values.insert(std::make_pair(fields[field_id].name, mysql_row[field_id]));
#ifdef DEBUG
// If there are more than one result, we might want to use get_objects instead
if (mysql_fetch_row(result) != NULL)
log_warning("More than one row returned in get_object.");
#endif
mysql_free_result(result);
return db_object;
}
std::vector<DbObject*> Database::get_objects(const std::string& columns,
const std::string& table,
const std::string& where)
{
MYSQL_RES* result = this->do_query("SELECT " + columns + " FROM " + table + " WHERE " + where);
std::vector<DbObject*> db_objects;
// It's OK to return an empty list. We must always check that the vector
// is not empty when using this method.
if (!result)
return db_objects;
unsigned int field_id = 0;
unsigned int fields_number = mysql_num_fields(result);
MYSQL_FIELD* fields = mysql_fetch_fields(result);
MYSQL_ROW mysql_row = NULL;
while ((mysql_row = mysql_fetch_row(result)))
{
DbObject* db_object = new DbObject;
for(field_id = 0; field_id < fields_number; field_id++)
if (mysql_row[field_id]) // if the value is NULL, we don’t append anything
db_object->values.insert(std::make_pair(fields[field_id].name, mysql_row[field_id]));
db_objects.push_back(db_object);
}
mysql_free_result(result);
return db_objects;
}
bool Database::update(const DbObject* object, const std::string& table_name)
{
std::string query = "INSERT INTO " + table_name;
std::string fields_str = "(";
std::string values_str = "VALUES (";
std::string update_str = "ON DUPLICATE KEY UPDATE ";
std::map<std::string, std::string>::const_iterator it;
// Use this iterator to detect the last element in the vector
std::map<std::string, std::string>::const_iterator final_it = object->values.end();
--final_it;
// unsigned int i;
for (it = object->values.begin(); it != object->values.end(); ++it)
{
fields_str += "`" + it->first + "`";
values_str += "'" + it->second + "'";
update_str += "`" + it->first + "`='" + it->second + "'";
if (it != final_it)
{
fields_str += ",";
values_str += ",";
update_str += ",";
}
}
query += fields_str + ") " + values_str + ") " + update_str;
if (this->do_update(query) == false)
return false;
return true;
}
bool Database::remove(const DbObject* object, const std::string& table_name)
{
std::string query = "DELETE FROM " + table_name + " WHERE ";
std::map<std::string, std::string>::const_iterator it;
// Use this iterator to detect the last element in the vector
std::map<std::string, std::string>::const_iterator final_it = object->values.end();
--final_it;
for (it = object->values.begin(); it != object->values.end(); ++it)
{
query += "`" + it->first + "`=";
query += "'" + it->second + "'";
if (it != final_it)
query += " AND ";
}
if (this->do_remove(query) == false)
return false;
return true;
}
/** @addtogroup Database
* @{
*/
/**
* Database Manager
* @class Database
*/
#if defined(_WIN32) || defined(_WIN64)
# include <winsock.h>
#endif /* WIN32 */
#include <mysql.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <database/db_object.hpp>
#ifndef __DATABASE_HPP__
# define __DATABASE_HPP__
/**
* @def DB_PORT
* The database port.
*/
# define DB_PORT 0
/**
* @def DB_UNIX_SOCKET
* The database unix socket
*/
# define DB_UNIX_SOCKET NULL
/**
* @def DB_CLIENT_FLAG
* The database client flag
*/
#define DB_CLIENT_FLAG CLIENT_SSL
class Database
{
public:
~Database();
/**
* Create a single instance of Database.
* @return Database*
*/
static Database* inst();
/**
* Get an object of the database. If the SQL query would return more than one
* row, you should use get_objects instead.
* If there’s no row, it returns NULL.
* @param columns The database columns to fetch.
* @param table The database table to query.
* @param where The where query.
* @return DbObject*
*/
DbObject* get_object(const std::string&, const std::string&, const std::string&);
/**
* Get a list of objects of the database.
* If no row matches, the returned vector is empty, otherwise it contains
* one or more objects.
* @param columns The database columns to fetch.
* @param table The database table to query.
* @param where The where query.
* @return std::vector<DbObject*>
*/
std::vector<DbObject*> get_objects(const std::string&, const std::string&, const std::string&);
/**
* Update a database object (insert it if it doesn’t already exist
* or create it if it’s new).
* @param object The database object
* @param table The database table
* @return bool
*/
bool update(const DbObject*, const std::string&);
/**
* Remove a database object.
* @param object The database object
* @param table The database table
* @return bool
*/
bool remove(const DbObject*, const std::string&);
private:
/**
* Do a SELECT query on the database and get the results. Returns NULL if an
* error occured or if there are no result.
* @param query The SQL query.
* @return MYSQL_RES*
*/
MYSQL_RES* do_query(const std::string&);
/**
* Do a UPDATE or INSERT query on the database.
* @param query The SQL query.
* @return bool
*/
bool do_update(const std::string&);
/**
* Do a DELETE query on the database.
* @param query The SQL query.
* @return bool
*/
bool do_remove(const std::string&);
Database();
/**
* Try to connect to the database, or just succeed if we are
* already connected.
* @return false on failure, true on success.
*/
bool connect();
/**
* Init the mysql connection.
*/
bool init();
void close();
static Database* instance;
MYSQL *mysql;
bool connected;
};
#endif
/**@}*/
#include <database/db_object.hpp>
#include <database/database.hpp>
#include <logging/logging.hpp>
DbObject::DbObject() {}
DbObject::~DbObject() {}
void DbObject::set(const std::string& field, const std::string& value)
{
std::map<std::string, std::string>::iterator it = this->values.find(field);
if (it != this->values.end())
this->values.erase(it);
this->values.insert(std::make_pair(field, value));
}
const std::string DbObject::get(const std::string& field) const
{
std::map<std::string, std::string>::const_iterator it = this->values.find(field);
if (it != this->values.end())
return it->second;
else
{
log_error("The field " << field.c_str() << " is not found");
return "";
}