/* pgsql-error.c -- PostgreSQL database driver for eurephia * * GPLv2 only - Copyright (C) 2012 * David Sommerseth * * 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 * @date 2012-01-15 * * @brief eurephia database driver for the PostgreSQL database. * Shared functions for the PostgreSQL driver * */ #include #include #include #ifdef HAVE_LIBXML2 #include #endif #include #include #include #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); } } int ePGgetValue_bool(PGresult *res, int row, int col) { if( (res == NULL) || (PQgetisnull(res, row, col) == 1) ) { return 0; } else { // FIXME: should we check if we have the row number accesible first? row > PQntuples() char *str = PQgetvalue(res, row, col); return (str[0] == 't' ? 1 : 0); } } 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); } } #ifdef HAVE_LIBXML2 xmlNode * __ePGerrorMessageXML(eurephiaCTX *ctx, PGresult *dbr, int logdst, ePG_prepID prepid, const char *errfile, const long errline) { xmlNode *ret_n = NULL; ret_n = xmlNewNode(NULL, (xmlChar *) "SQLError"); if( ret_n != NULL ) { xmlNode *err_n = NULL; xmlChar *errstr = NULL; xmlNewProp(ret_n, (xmlChar *) "driver", (xmlChar *) "edb-sqlite.so"); #ifdef ENABLE_DEBUG { xmlNode *dbg_n = xmlNewNode(NULL, (xmlChar *) "debug"); xmlChar linestr[10]; xmlStrPrintf(linestr, 8, (xmlChar *) "%u", errline); xmlNewProp(dbg_n, (xmlChar *) "file", (xmlChar *) errfile); xmlNewProp(dbg_n, (xmlChar *) "line", linestr); xmlAddChild(ret_n, dbg_n); } #endif errstr = xmlCharStrdup(dbr != NULL ? PQresultErrorMessage(dbr) : (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle) : "[unknown error]")); err_n = xmlNewTextChild(ret_n, NULL, (xmlChar *) "ErrorMessage", errstr); if( prepid != PREPSQL_NONE ) { xmlNewProp(err_n, (xmlChar *) "prepstatement", (xmlChar *) ePGprepStatementGetName(ctx, prepid)); } if( dbr != NULL ) { xmlNewProp(err_n, (xmlChar *) "resultstatus", (xmlChar *) PQresStatus(PQresultStatus(dbr))); } else { xmlNewProp(err_n, (xmlChar *) "resultstatus", (xmlChar *) "(unknown/NULL)"); } xmlNewProp(err_n, (xmlChar *) "severity", (xmlChar *) eurephia_logPrioString(logdst)); free_nullsafe(NULL, errstr); } if( dbr ) { PQclear(dbr); } if( (logdst == LOG_EMERG) && (ctx->dbc != NULL) && (PQstatus(ctx->dbc->dbhandle) != CONNECTION_OK) ) { PQfinish(ctx->dbc->dbhandle); } return ret_n; } #endif