Add skeleton for new storage support using SQLite3
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sqlite3.h>
27
28 #include "connman.h"
29
30 static sqlite3 *db = NULL;
31
32 static int create_tables(void)
33 {
34         char *msg;
35         int err;
36
37         DBG("");
38
39         err = sqlite3_exec(db, "CREATE TABLE properties ("
40                                         "element TEXT NOT NULL,"
41                                         "name TEXT NOT NULL,"
42                                         "value TEXT NOT NULL,"
43                                         "PRIMARY KEY(element, name))",
44                                                         NULL, NULL, &msg);
45
46         if (err != SQLITE_OK) {
47                 connman_error("SQL error: %s", msg);
48                 sqlite3_free(msg);
49                 return -1;
50         }
51
52         return 0;
53 }
54
55 int __connman_storage_init(void)
56 {
57         int err;
58
59         DBG("");
60
61 #if 0
62         if (!sqlite3_threadsafe()) {
63                 connman_error("SQLite is missing thread support");
64                 return -1;
65         }
66 #endif
67
68         err = sqlite3_open(STORAGEDIR "/config.db", &db);
69         if (err != SQLITE_OK) {
70                 connman_error("Can't open database: %s", sqlite3_errmsg(db));
71                 sqlite3_close(db);
72                 return -1;
73         }
74
75         create_tables();
76
77         return 0;
78 }
79
80 void __connman_storage_cleanup(void)
81 {
82         DBG("");
83
84         sqlite3_close(db);
85 }
86
87 int __connman_element_load(struct connman_element *element)
88 {
89         return 0;
90 }
91
92 int __connman_element_store(struct connman_element *element)
93 {
94         char *sql, *msg;
95
96         DBG("");
97
98         if (element->priority > 0) {
99                 sql = g_strdup_printf("INSERT INTO properties "
100                                                 "VALUES ('%s','%s','%d')",
101                                                 element->path, "Priority",
102                                                         element->priority);
103
104                 if (sqlite3_exec(db, sql, NULL, NULL, &msg) != SQLITE_OK) {
105                         connman_error("SQL error: %s", msg);
106                         sqlite3_free(msg);
107                 }
108         }
109
110         return 0;
111 }