Debian Codegraph @

src/cd-profile-db.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2014 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25 #include <glib-object.h>
26 #include <sqlite3.h>
27
28 #include "cd-common.h"
29 #include "cd-profile-db.h"
30
31 static void cd_profile_db_finalize (GObject *object);
32
33 #define GET_PRIVATE(o) (cd_profile_db_get_instance_private (o))
34
35 typedef struct
36 {
37 sqlite3 *db;
38 } CdProfileDbPrivate;
39
40 static gpointer cd_profile_db_object = NULL;
41
42 G_DEFINE_TYPE_WITH_PRIVATE (CdProfileDb, cd_profile_db, G_TYPE_OBJECT)
43
44 gboolean
45 cd_profile_db_load (CdProfileDb *pdb,
46 const gchar *filename,
47 GError **error)
48 {
49 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
50 const gchar *statement;
51 gint rc;
52 g_autofree gchar *path = NULL;
53
54 g_return_val_if_fail (CD_IS_PROFILE_DB (pdb), FALSE);
55 g_return_val_if_fail (priv->db == NULL, FALSE);
56
57 /* ensure the path exists */
58 path = g_path_get_dirname (filename);
59 if (!cd_main_mkdir_with_parents (path, error))
60 return FALSE;
61
62 g_debug ("CdProfileDb: trying to open database '%s'", filename);
63 g_info ("Using profile database file %s", filename);
64 rc = sqlite3_open (filename, &priv->db);
65 if (rc != SQLITE_OK) {
66 g_set_error (error,
67 CD_CLIENT_ERROR,
68 CD_CLIENT_ERROR_INTERNAL,
69 "Can't open database: %s\n",
70 sqlite3_errmsg (priv->db));
71 sqlite3_close (priv->db);
72 return FALSE;
73 }
74
75 /* we don't need to keep doing fsync */
76 sqlite3_exec (priv->db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
77
78 /* check schema */
79 rc = sqlite3_exec (priv->db, "SELECT * FROM properties_pu LIMIT 1", NULL, NULL, NULL);
80 if (rc != SQLITE_OK) {
81 statement = "CREATE TABLE properties_pu ("
82 "profile_id TEXT,"
83 "property TEXT,"
84 "uid INTEGER,"
85 "value TEXT,"
86 "PRIMARY KEY (profile_id, property, uid));";
87 sqlite3_exec (priv->db, statement, NULL, NULL, NULL);
88 }
89 return TRUE;
90 }
91
92 gboolean
93 cd_profile_db_empty (CdProfileDb *pdb, GError **error)
94 {
95 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
96 const gchar *statement;
97 gint rc;
98
99 g_return_val_if_fail (CD_IS_PROFILE_DB (pdb), FALSE);
100 g_return_val_if_fail (priv->db != NULL, FALSE);
101
102 statement = "DELETE FROM properties_pu;";
103 rc = sqlite3_exec (priv->db, statement, NULL, NULL, NULL);
104 if (rc != SQLITE_OK) {
105 g_set_error (error,
106 CD_CLIENT_ERROR,
107 CD_CLIENT_ERROR_INTERNAL,
108 "SQL error: %s",
109 sqlite3_errmsg(priv->db));
110 return FALSE;
111 }
112 return TRUE;
113 }
114
115 gboolean
116 cd_profile_db_set_property (CdProfileDb *pdb,
117 const gchar *profile_id,
118 const gchar *property,
119 guint uid,
120 const gchar *value,
121 GError **error)
122 {
123 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
124 gboolean ret = TRUE;
125 gchar *statement;
126 gint rc;
127
128 g_return_val_if_fail (CD_IS_PROFILE_DB (pdb), FALSE);
129 g_return_val_if_fail (priv->db != NULL, FALSE);
130
131 g_debug ("CdProfileDb: add profile property %s [%s=%s]",
132 profile_id, property, value);
133 statement = sqlite3_mprintf ("INSERT OR REPLACE INTO properties_pu (profile_id, "
134 "property, uid, value) "
135 "VALUES ('%q', '%q', '%u', '%q');",
136 profile_id, property, uid, value);
137
138 /* insert the entry */
139 rc = sqlite3_exec (priv->db, statement, NULL, NULL, NULL);
140 if (rc != SQLITE_OK) {
141 g_set_error (error,
142 CD_CLIENT_ERROR,
143 CD_CLIENT_ERROR_INTERNAL,
144 "SQL error: %s",
145 sqlite3_errmsg(priv->db));
146 ret = FALSE;
147 goto out;
148 }
149 out:
150 sqlite3_free (statement);
151 return ret;
152 }
153
154 gboolean
155 cd_profile_db_remove (CdProfileDb *pdb,
156 const gchar *profile_id,
157 const gchar *property,
158 guint uid,
159 GError **error)
160 {
161 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
162 gboolean ret = TRUE;
163 gchar *statement = NULL;
164 gint rc;
165
166 g_return_val_if_fail (CD_IS_PROFILE_DB (pdb), FALSE);
167 g_return_val_if_fail (priv->db != NULL, FALSE);
168
169 /* remove the entry */
170 g_debug ("CdProfileDb: remove profile %s", profile_id);
171 statement = sqlite3_mprintf ("DELETE FROM properties_pu WHERE "
172 "profile_id = '%q' AND "
173 "uid = '%i' AND "
174 "property = '%q' LIMIT 1;",
175 profile_id, uid, property);
176 rc = sqlite3_exec (priv->db, statement, NULL, NULL, NULL);
177 if (rc != SQLITE_OK) {
178 g_set_error (error,
179 CD_CLIENT_ERROR,
180 CD_CLIENT_ERROR_INTERNAL,
181 "SQL error: %s",
182 sqlite3_errmsg(priv->db));
183 ret = FALSE;
184 goto out;
185 }
186 out:
187 sqlite3_free (statement);
188 return ret;
189 }
190
191 static gint
192 cd_profile_db_sqlite_cb (void *data,
193 gint argc,
194 gchar **argv,
195 gchar **col_name)
196 {
197 gchar **value = (gchar **) data;
198
199 /* should only be one entry */
200 g_debug ("CdProfileDb: got sql result %s", argv[0]);
201 *value = g_strdup (argv[0]);
202 return 0;
203 }
204
205 gboolean
206 cd_profile_db_get_property (CdProfileDb *pdb,
207 const gchar *profile_id,
208 const gchar *property,
209 guint uid,
210 gchar **value,
211 GError **error)
212 {
213 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
214 gboolean ret = TRUE;
215 gchar *statement;
216 gint rc;
217
218 g_return_val_if_fail (CD_IS_PROFILE_DB (pdb), FALSE);
219 g_return_val_if_fail (priv->db != NULL, FALSE);
220
221 g_debug ("CdProfileDb: get property %s for %s", property, profile_id);
222 statement = sqlite3_mprintf ("SELECT value FROM properties_pu WHERE "
223 "profile_id = '%q' AND "
224 "uid = '%i' AND "
225 "property = '%q' LIMIT 1;",
226 profile_id, uid, property);
227
228 /* retrieve the entry */
229 rc = sqlite3_exec (priv->db,
230 statement,
231 cd_profile_db_sqlite_cb,
232 value,
233 NULL);
234 if (rc != SQLITE_OK) {
235 ret = FALSE;
236 g_set_error (error,
237 CD_CLIENT_ERROR,
238 CD_CLIENT_ERROR_INTERNAL,
239 "SQL error: %s",
240 sqlite3_errmsg(priv->db));
241 goto out;
242 }
243 out:
244 sqlite3_free (statement);
245 return ret;
246 }
247
248 static void
249 cd_profile_db_class_init (CdProfileDbClass *klass)
250 {
251 GObjectClass *object_class = G_OBJECT_CLASS (klass);
252 object_class->finalize = cd_profile_db_finalize;
253 }
254
255 static void
256 cd_profile_db_init (CdProfileDb *pdb)
257 {
258 }
259
260 static void
261 cd_profile_db_finalize (GObject *object)
262 {
263 CdProfileDb *pdb = CD_PROFILE_DB (object);
264 CdProfileDbPrivate *priv = GET_PRIVATE (pdb);
265
266 /* close the database */
267 sqlite3_close (priv->db);
268
269 G_OBJECT_CLASS (cd_profile_db_parent_class)->finalize (object);
270 }
271
272 CdProfileDb *
273 cd_profile_db_new (void)
274 {
275 if (cd_profile_db_object != NULL) {
276 g_object_ref (cd_profile_db_object);
277 } else {
278 cd_profile_db_object = g_object_new (CD_TYPE_PROFILE_DB, NULL);
279 g_object_add_weak_pointer (cd_profile_db_object, &cd_profile_db_object);
280 }
281 return CD_PROFILE_DB (cd_profile_db_object);
282 }
283