From: Kaj-Michael Lang Date: Tue, 19 Jun 2007 09:21:27 +0000 (+0300) Subject: Add proper protection to db.h X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21ea2ea3a43054bd14c917cd2d7bb60421df9fc2;p=mapper Add proper protection to db.h Remove unused variables from db.c Remove all direct gui stuff from db.c --- diff --git a/src/db.c b/src/db.c index bf72ef2..29ef530 100644 --- a/src/db.c +++ b/src/db.c @@ -1,17 +1,15 @@ #include "config.h" -#include +#include #include -#define _(String) gettext(String) - -#include -#include - +#include "db.h" #include "utils.h" -#include "ui-common.h" -#include "poi.h" +/** + * A simple check to test if a table exists + * + */ gboolean db_check(sqlite3 *db, const gchar *table) { gchar **pszResult; @@ -20,14 +18,17 @@ gchar sql[64]; snprintf(sql, sizeof(sql),"select count(*) from %s", table); -if (SQLITE_OK != sqlite3_get_table(db, sql, - &pszResult, &nRow, &nColumn, NULL)) +if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL)) return FALSE; sqlite3_free_table(pszResult); return TRUE; } +/** + * Close the database connection + * + */ gboolean db_close(sqlite3 **db) { if (db && *db) { @@ -37,43 +38,38 @@ if (db && *db) { return TRUE; } +/** + * Connect to given sqlite database + * + */ gboolean db_connect(sqlite3 **ndb, const gchar *db_file) { - gchar buffer[100]; - gchar *perror; - gchar **pszResult; - guint nRow, nColumn; - sqlite3 *db; - - printf("%s()\n", __PRETTY_FUNCTION__); +sqlite3 *db; - if (ndb && *ndb) - db=*ndb; - else - db=NULL; +printf("%s()\n", __PRETTY_FUNCTION__); - if (db) { - sqlite3_close(db); - db = NULL; - } +if (ndb && *ndb) + db=*ndb; +else + db=NULL; - if (!db_file) - return FALSE; - - if (SQLITE_OK != (sqlite3_open(db_file, &db))) { - gchar buffer2[200]; +if (db) { + sqlite3_close(db); + db = NULL; +} - snprintf(buffer2, sizeof(buffer2), - "%s: %s", _("Problem with database"), sqlite3_errmsg(db)); - sqlite3_close(db); - db = NULL; - *ndb=db; - popup_error(_window, buffer2); - return FALSE; - } +if (!db_file) + return FALSE; +if (SQLITE_OK != (sqlite3_open(db_file, &db))) { + sqlite3_close(db); + db = NULL; *ndb=db; + return FALSE; +} - printf("%s(): return\n", __PRETTY_FUNCTION__); - return TRUE; +*ndb=db; + +printf("%s(): return\n", __PRETTY_FUNCTION__); +return TRUE; } diff --git a/src/db.h b/src/db.h index d96dddc..ea53d82 100644 --- a/src/db.h +++ b/src/db.h @@ -1,6 +1,11 @@ -#include +#include #include +#ifndef _MAPPER_DB_H +#define _MAPPER_DB_H + gboolean db_connect(sqlite3 **db, const gchar *poi_db); gboolean db_check(sqlite3 *db, const gchar *table); gboolean db_close(sqlite3 **db); + +#endif