📄 dbgnomedb.c
字号:
#include <glib.h>#include "dbwrapper.h"#ifdef _GNOMEDB#include "dbgnomedb.h"#include "debug.h"#include "dbuniqueid.h"#include "dbsqlparse.h"/*=========================================================================================== * Connection based functions. *=========================================================================================== *//** * db_dbconnect: * @initstring: Postgresql initilising string * * Direct wrapper for PQconnectdb(@initstring). * * Returns: Connection to database. */DbConnection *db_dbconnect(gchar * initstring) { DbConnection *ret; g_assert(initstring); ret = (DbConnection *) mem_alloc(sizeof(DbConnection)); ret->connstring = mem_strdup(initstring); ret->conn = PQconnectdb(initstring); ret->dbserver = DB_PGSQL; return ret; }/** * db_dbstatus: * @conn: Database Connection * * Direct wrapper for gda_connection_is_open * * Returns: status, a non-zero on failure */gintdb_dbstatus(DbConnection * conn) { g_assert(conn); if (!gda_connection_is_open(conn->conn) ) return 1; return 0; }/** * db_dberrormsg: * @conn: Database Connection * * Calls gda_connection_get_errors and rolls the list into a string * * Returns: gchar* not to be freed. */gchar *db_dberrormsg(DbConnection * conn) { GList *errors, *walk; gchar *retstring; GdaError *err; g_assert(conn); errors = gda_connection_get_errors(conn->conn); for (walk=errors;walk!=NULL;walk=walk->next()){ err = walk->data; retstring = mem_strdup_printf("%s %s\n",retstring, gda_error_get_description(err)); } return retstring; }/** * db_dbfinish: * @conn: Database Connection * * Direct wrapper for gda_connection_close(@conn). */voiddb_dbfinish(DbConnection * conn) { g_assert(conn); gda_connection_close(conn->conn); mem_free(conn->connstring); mem_free(conn); }/** * db_dbreset: * @conn: Database Connection * * This function is not essential and i dont think it is * even called anywhere. So no need to make use of it. * hey, I wont even write a libgda wrapper for it then. */voiddb_dbreset(DbConnection * conn) { errormsg("Not implemented for libgda"); }/*=========================================================================================== * Result based functions. *=========================================================================================== *//** * db_dbcheckresult: * @result: Query result * * checks to see if the recordset is null, or * if the GdaResultSet is null * * * * Returns: non-zero on error */gintdb_dbcheckresult(DbRecordSet * result) { int err; g_assert(result); if (result == NULL) { errormsg("Result is NULL"); return -1; } if (result->res == NULL) { mem_free(result); errormsg("Result->res is NULL"); return -3; } return 0; }/** * db_dbclear: * @result: Query result * * Direct wrapper for gda_recordset_free(@result). */voiddb_dbclear(DbRecordSet * result) { if (result == NULL) return ; gda_recordset_free(result->res); mem_free(result->query); if (result->basetable != NULL) mem_free(result->basetable); mem_free(result); result = NULL; }/** * db_dbexec: * @conn: Database connection * @query: Query string * * Direct wrapper for gda_connection_exec(@conn, @query). * * Returns: DbRecordSet to database. */DbRecordSet *db_dbexec(DbConnection * conn, gchar * query) { DbRecordSet *result; g_assert(conn); g_assert(query); result = (DbRecordSet *) mem_alloc(sizeof(DbRecordSet)); result->query = mem_strdup(query); result->res = gda_connection_execute(conn->conn, query, &(conn->rec_count), 0); result->basetable = NULL; return result; }/** * db_dbnumrows: * @result: Result from query * * Direct wrapper for db_dbnumrows(@result);. * * Returns: Number of rows. */gintdb_dbnumrows(DbRecordSet * result) { g_assert(result); g_assert(result->res); return int(result->res->rec_count); }/** * db_dbgetvalue: * @result: Result from query * @row: Position in recordset * @field: Field position * * moves to the row, gets the value, moves back * * Returns: String of field */gchar *db_dbgetvalue(DbRecordSet * result, gint row, gint field) { GdaField *gfield; if(gda_recordset_move(result->res, row)==GDA_RECORDSET_INVALID_POSITION) { errormsg("Tried to move to an invalid row in the recordset"); return null; } gfield = gda_recordset_field_idx(result->res, field); gda_recordset_move(result->res, -row); /*try this, not sure if it will only work if the field is a * string, or if it will convert to String auto like */ return gda_stringify_value(NULL, 0, gfield); }/** * db_dbnumfields: * @result: Result from a query * * Direct wrapper for gda_recordset_rowsize(@result);. * * Returns: Number of fields in this row of the @result. */gintdb_dbnumfields(DbRecordSet * result) { g_assert(result); g_assert(result->res); return gda_recordset_rowsize(result->res); }/** * db_dbfieldname: * @result: Result from a query * @fieldpos: Field position * * gets the specified field from the current row and returns its name * * Returns: Name of a field in recordset. */gchar *db_dbfieldname(DbRecordSet * result, gint fieldpos) { GdaField field; field = gda_recordset_field_idx(result->res, fieldpos); return gda_field_name(field); }/** * db_dbuniqueid: * @result: Result from a insert * @fieldpos: Field position * * Direct wrapper for PQoidValue(@result);. * Assuming you run am insert query with db_dbexec(), this will return the unique id assocated with * that record. Note this works on just getting the oid value from postgresql, on other databases * a bit of a work around will be needed. * * Returns: Name of a field in recordset. */DbUniqueId *db_dbuniqueid(DbRecordSet * result) { DbUniqueId *id; g_assert(result); g_assert(result->res); oid = PQoidValue(result->res); if (oid <= 0) return NULL; g_assert(result->basetable); id = db_id_createblank(result->basetable); id->pg_oid = oid; return id; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -