]> err.no Git - mapper/blob - src/db.c
Add proper protection to db.h
[mapper] / src / db.c
1 #include "config.h"
2
3 #include <glib.h>
4 #include <sqlite3.h>
5
6 #include "db.h"
7 #include "utils.h"
8
9 /**
10  * A simple check to test if a table exists
11  *
12  */
13 gboolean db_check(sqlite3 *db, const gchar *table)
14 {
15 gchar **pszResult;
16 guint nRow, nColumn;
17 gchar sql[64];
18
19 snprintf(sql, sizeof(sql),"select count(*) from %s", table);
20
21 if (SQLITE_OK != sqlite3_get_table(db, sql, &pszResult, &nRow, &nColumn, NULL))
22         return FALSE;
23
24 sqlite3_free_table(pszResult);
25 return TRUE;
26 }
27
28 /**
29  * Close the database connection
30  *
31  */
32 gboolean db_close(sqlite3 **db)
33 {
34 if (db && *db) {
35         sqlite3_close(*db);
36         *db=NULL;
37 }
38 return TRUE;
39 }
40
41 /**
42  * Connect to given sqlite database
43  *
44  */
45 gboolean db_connect(sqlite3 **ndb, const gchar *db_file)
46 {
47 sqlite3 *db;
48
49 printf("%s()\n", __PRETTY_FUNCTION__);
50
51 if (ndb && *ndb)
52         db=*ndb;
53 else
54         db=NULL;
55
56 if (db) {
57         sqlite3_close(db);
58         db = NULL;
59 }
60
61 if (!db_file)
62         return FALSE;
63
64 if (SQLITE_OK != (sqlite3_open(db_file, &db))) {
65         sqlite3_close(db);
66         db = NULL;
67         *ndb=db;
68         return FALSE;
69 }
70
71 *ndb=db;
72
73 printf("%s(): return\n", __PRETTY_FUNCTION__);
74 return TRUE;
75 }