diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2012-01-15 23:53:12 +0100 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2013-06-13 01:01:55 +0200 |
| commit | 6ce2be3d09484d393c894a291dc9429525d3d914 (patch) | |
| tree | df9680acfeac526974173445341d25afdc144476 /database/postgresql/pgsql-common.c | |
| parent | 6c1f8ff4f7966f0d1f9cb1b2ff447f0dfc5f8aeb (diff) | |
| download | eurephia-6ce2be3d09484d393c894a291dc9429525d3d914.tar.gz eurephia-6ce2be3d09484d393c894a291dc9429525d3d914.tar.xz eurephia-6ce2be3d09484d393c894a291dc9429525d3d914.zip | |
edb-pgsql: Reworked the prepared statements infrastructure
Moved all SQL statements out of each function and into a const
struct which is loaded at startup.
Implemented a safer way of handling parameters to these prepared
statements as well.
Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
Diffstat (limited to 'database/postgresql/pgsql-common.c')
| -rw-r--r-- | database/postgresql/pgsql-common.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/database/postgresql/pgsql-common.c b/database/postgresql/pgsql-common.c new file mode 100644 index 0000000..35ae03a --- /dev/null +++ b/database/postgresql/pgsql-common.c @@ -0,0 +1,93 @@ +/* pgsql-error.c -- PostgreSQL database driver for eurephia + * + * GPLv2 only - Copyright (C) 2012 + * David Sommerseth <dazo@users.sourceforge.net> + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/** + * @file pgsql-error.c + * @author David Sommerseth <dazo@users.sourceforge.net> + * @date 2012-01-15 + * + * @brief eurephia database driver for the PostgreSQL database. + * Shared functions for the PostgreSQL driver + * + */ + +#include <string.h> +#include <stdarg.h> +#include <libpq-fe.h> + +#include <eurephia_context.h> +#include <eurephia_log.h> +#include "prepared-sql.h" + + +char *ePGgetValue(PGresult *res, int row, int col) +{ + if( (res == NULL) || (PQgetisnull(res, row, col) == 1) ) { + return NULL; + } else { + // FIXME: should we check if we have the row number accesible first? row > PQntuples() + return PQgetvalue(res, row, col); + } +} + + +void __ePGerrorMessage(eurephiaCTX *ctx, PGresult *dbr, int logdst, int loglvl, + ePG_prepID prepid, const char *errfile, const long errline, + const char *fmt, ...) +{ + char msgfmt[514]; + va_list ap; + + memset(&msgfmt, 0, 514); + va_start(ap, fmt); + if( prepid != PREPSQL_NONE ) { + snprintf(msgfmt, 512, "[%s] SQL query failed: %s %s: %s", + ePGprepStatementGetName(ctx, prepid), + (dbr != NULL ? PQresStatus(PQresultStatus(dbr)) : ""), fmt, + (dbr != NULL ? PQresultErrorMessage(dbr) + : (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle) + : "[unknown error]") + )); + } else { + snprintf(msgfmt, 512, "SQL query failed: %s %s: %s", + (dbr != NULL ? PQresStatus(PQresultStatus(dbr)) : ""), + fmt, + (dbr != NULL ? PQresultErrorMessage(dbr) + : (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle) + : "[unknown error]") + )); + } +#ifdef ENABLE_DEBUG + _veurephia_log_func(ctx, logdst, loglvl, errfile, errline, ap, msgfmt); +#else + veurephia_log(ctx, logdst, loglvl, ap, msgfmt); +#endif + va_end(ap); + memset(&msgfmt, 0, 514); + if( dbr ) { + PQclear(dbr); + } + if( (loglvl == LOG_EMERG) && (ctx->dbc != NULL) + && (PQstatus(ctx->dbc->dbhandle) != CONNECTION_OK) ) { + PQfinish(ctx->dbc->dbhandle); + } +} + |
