2012-03-23 15:45:38 +00:00
|
|
|
/* wMUD - Yet another MUD codebase by W00d5t0ck
|
|
|
|
* Copyright (C) 2012 - Gergely POLONKAI
|
|
|
|
*
|
|
|
|
* db.c: database handling routines
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-03-26 14:55:36 +00:00
|
|
|
|
2012-03-22 09:49:19 +00:00
|
|
|
#include <glib.h>
|
2012-03-21 14:59:39 +00:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2012-03-22 17:34:39 +00:00
|
|
|
#include "main.h"
|
2012-03-21 14:59:39 +00:00
|
|
|
#include "db.h"
|
2012-03-22 17:34:39 +00:00
|
|
|
#include "players.h"
|
2012-03-26 14:55:36 +00:00
|
|
|
#include "configuration.h"
|
2012-03-21 14:59:39 +00:00
|
|
|
|
2012-03-24 22:52:36 +00:00
|
|
|
/**
|
|
|
|
* SECTION:db
|
|
|
|
* @short_description: Database handling
|
|
|
|
* @title: Database handling routines
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-03-26 14:55:36 +00:00
|
|
|
/**
|
|
|
|
* WMUD_DB_ERROR:
|
|
|
|
*
|
|
|
|
* the GQuark for the database error GError
|
|
|
|
*/
|
|
|
|
GQuark WMUD_DB_ERROR = 0;
|
|
|
|
static sqlite3 *dbh = NULL;
|
2012-03-21 14:59:39 +00:00
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* wmud_db_init:
|
2012-03-24 22:52:36 +00:00
|
|
|
* @err: a GError to put error messages in it
|
2012-03-24 19:34:36 +00:00
|
|
|
*
|
|
|
|
* Initializes the wMUD database system. Checks and opens database files.
|
|
|
|
*/
|
2012-03-21 14:59:39 +00:00
|
|
|
gboolean
|
2012-03-22 09:49:19 +00:00
|
|
|
wmud_db_init(GError **err)
|
2012-03-21 14:59:39 +00:00
|
|
|
{
|
2012-03-22 17:34:39 +00:00
|
|
|
GString *db_file = g_string_new(WMUD_STATEDIR);
|
|
|
|
int sqlite_code;
|
|
|
|
|
2012-03-26 14:55:36 +00:00
|
|
|
g_string_append_printf(db_file, "/%s", active_config->database_file);
|
2012-03-22 17:34:39 +00:00
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_open(db_file->str, &dbh)) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN, "Can not open databsae file (%s): %s", db_file->str, sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* wmud_db_players_load:
|
2012-03-24 22:52:36 +00:00
|
|
|
* @err: a GError to put error messages in it
|
2012-03-24 19:34:36 +00:00
|
|
|
*
|
|
|
|
* Loads all player records from the database
|
|
|
|
*/
|
2012-03-22 17:34:39 +00:00
|
|
|
gboolean
|
|
|
|
wmud_db_players_load(GError **err)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *sth = NULL;
|
|
|
|
int sqlite_code;
|
|
|
|
|
|
|
|
if (dbh == NULL)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, login, password, email FROM players", -1, &sth, NULL)) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_players_load(): %s", sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
sqlite_code = sqlite3_step(sth);
|
|
|
|
if (sqlite_code == SQLITE_ROW)
|
|
|
|
{
|
|
|
|
wmudPlayer *player = g_new0(wmudPlayer, 1);
|
|
|
|
player->id = sqlite3_column_int(sth, 0);
|
|
|
|
player->player_name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
|
|
|
|
player->cpassword = g_strdup((gchar *)sqlite3_column_text(sth, 2));
|
|
|
|
player->email = g_strdup((gchar *)sqlite3_column_text(sth, 3));
|
|
|
|
|
|
|
|
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", player->player_name);
|
|
|
|
|
|
|
|
players = g_slist_prepend(players, player);
|
|
|
|
}
|
|
|
|
else if (sqlite_code == SQLITE_DONE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_players_load(): %s", sqlite3_errmsg(dbh));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(sth);
|
|
|
|
|
2012-03-22 09:49:19 +00:00
|
|
|
return FALSE;
|
2012-03-21 14:59:39 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* wmud_db_save_player:
|
|
|
|
* @player: the player record to save
|
2012-03-24 22:52:36 +00:00
|
|
|
* @err: a GError to put error messages in it
|
2012-03-24 19:34:36 +00:00
|
|
|
*
|
|
|
|
* Saves a player record to the database backend.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE on success. Upon failure, %FALSE is returned, and err is
|
|
|
|
* set accordingly.
|
|
|
|
*/
|
2012-03-22 17:34:39 +00:00
|
|
|
gboolean
|
|
|
|
wmud_db_save_player(wmudPlayer *player, GError **err)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *sth = NULL;
|
|
|
|
int sqlite_code;
|
|
|
|
|
|
|
|
if (dbh == NULL)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_prepare_v2(dbh, "INSERT INTO players (id, login, password, email) VALUES (NULL, ?, NULL, ?)", -1, &sth, NULL)) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_player_save(): %s", sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_bind_text(sth, 1, player->player_name, -1, SQLITE_STATIC)) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_player_save(): %s", sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_bind_text(sth, 2, player->email, -1, SQLITE_STATIC)) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_player_save(): %s", sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sqlite_code = sqlite3_step(sth)) != SQLITE_DONE)
|
|
|
|
{
|
|
|
|
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Statement cannot be executed in wmud_db_player_save(): %s", sqlite3_errmsg(dbh));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_clear_error(err);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-26 15:33:58 +00:00
|
|
|
gboolean
|
|
|
|
wmud_db_load_planes(GSList **planes, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_db_load_planets(GSList **planets, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_db_load_directions(GSList **directions, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_db_load_areas(GSList **areas, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_db_load_rooms(GSList **rooms, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_db_load_exits(GSList **exits, GError **err)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|