From f45d893b4706217939f5e94da74b185da3e78c8f Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 22 Nov 2013 19:32:54 +0100 Subject: [PATCH] Add error checking to some DB functions --- wmud/db.c | 21 +++++++++++++++++++-- wmud/db.h | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/wmud/db.c b/wmud/db.c index 3559483..9624a85 100644 --- a/wmud/db.c +++ b/wmud/db.c @@ -91,6 +91,7 @@ wmud_db_load_players(GError **err) GdaStatement *sth = NULL; GdaDataModel *res; GdaDataModelIter *iter; + GError *local_err = NULL; g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading players"); if (dbh == NULL) { @@ -101,7 +102,15 @@ wmud_db_load_players(GError **err) } sth = gda_sql_parser_parse_string(parser, "SELECT id, login, password, email FROM players", NULL, NULL); - res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); + + /* TODO: error checking! */ + if ((res = gda_connection_statement_execute_select(dbh, sth, NULL, &local_err)) == NULL) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to load players: %s", local_err->message); + g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR, "SELECT error: %s", local_err->message); + + return FALSE; + } + iter = gda_data_model_create_iter(res); gda_data_model_iter_move_next(iter); @@ -287,8 +296,10 @@ wmud_db_load_directions(GSList **directions, GError **err) GdaStatement *sth = NULL; GdaDataModel *res = NULL; GdaDataModelIter *iter; + GError *local_err = NULL; g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading directions"); + if (dbh == NULL) { if (err) g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); @@ -297,7 +308,13 @@ wmud_db_load_directions(GSList **directions, GError **err) } sth = gda_sql_parser_parse_string(parser, "SELECT id, short_name, name FROM directions", NULL, NULL); - res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); + + if ((res = gda_connection_statement_execute_select(dbh, sth, NULL, &local_err)) == NULL) { + g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR, "Unable to load directions: %s", local_err->message); + + return FALSE; + } + iter = gda_data_model_create_iter(res); gda_data_model_iter_move_next(iter); diff --git a/wmud/db.h b/wmud/db.h index eb89482..ffe25e3 100644 --- a/wmud/db.h +++ b/wmud/db.h @@ -37,7 +37,8 @@ GQuark wmud_db_error_quark(); typedef enum { WMUD_DB_ERROR_CANTOPEN, WMUD_DB_ERROR_NOINIT, - WMUD_DB_ERROR_BADQUERY + WMUD_DB_ERROR_BADQUERY, + WMUD_DB_ERROR_SELECT_ERROR } wmudDbError; gboolean wmud_db_init(GError **err);