1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Copyright (C) 2009,2011 Jens Georg <mail@jensge.org>.
* Copyright (C) 2012, 2013 Intel Corporation.
*
* Author: Jens Georg <mail@jensge.org>
*
* This file is part of Rygel.
*
* Rygel is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Rygel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <glib/gi18n-lib.h>
#include <sqlite3.h>
#include "rygel-media-export-collate.h"
#include "rygel-media-export-database.h"
/**
* This class is a thin wrapper around SQLite's database object.
*
* It adds statement preparation based on GValue and a cancellable exec
* function.
*/
G_DEFINE_TYPE (RygelMediaExportDatabase,
rygel_media_export_database,
RYGEL_MEDIA_EXPORT_TYPE_SQLITE_WRAPPER)
/**
* Function to implement the custom SQL function 'contains'
*/
static
void rygel_media_export_database_utf8_contains (sqlite3_context *context,
sqlite3_value **args,
int args_length) {
const gchar *args_1_text;
gchar* pattern;
g_return_if_fail (context != NULL);
g_return_if_fail (args_length == 2);
args_1_text = (const gchar *) sqlite3_value_text (args[1]);
if (args_1_text == NULL) {
sqlite3_result_int (context, 0);
return;
}
pattern = g_regex_escape_string (args_1_text, -1);
if (g_regex_match_simple (pattern, (const gchar *) sqlite3_value_text (args[0]), G_REGEX_CASELESS, 0)) {
sqlite3_result_int (context, 1);
} else {
sqlite3_result_int (context, 0);
}
g_free (pattern);
}
/**
* Function to implement the custom SQLite collation 'CASEFOLD'.
*
* Uses utf8 case-fold to compare the strings.
*/
static gint
rygel_media_export_database_utf8_collate (gint alen G_GNUC_UNUSED,
gconstpointer a,
gint blen G_GNUC_UNUSED,
gconstpointer b) {
return rygel_media_export_utf8_collate_str (a, b);
}
static void
rygel_media_export_database_utf8_contains_sqlite_user_func_callback (sqlite3_context *context,
int values_length,
sqlite3_value **values) {
rygel_media_export_database_utf8_contains (context, values, values_length);
}
static gint rygel_media_export_database_utf8_collate_sqlite_compare_callback (gpointer self G_GNUC_UNUSED,
gint alen,
gconstpointer a,
gint blen,
gconstpointer b) {
return rygel_media_export_database_utf8_collate (alen, a, blen, b);
}
/**
* Open a database in the user's cache directory as defined by XDG
*
* @param name of the database, used to build full path
* (<cache-dir>/rygel/<name>.db)
*/
RygelMediaExportDatabase*
rygel_media_export_database_new (const gchar *name,
GError **error) {
RygelMediaExportDatabase *self;
gchar *dirname;
gchar *dbname;
gchar *db_file;
sqlite3 *db;
GError *inner_error;
g_return_val_if_fail (name != NULL, NULL);
dirname = g_build_filename (g_get_user_cache_dir (), "rygel", NULL);
g_mkdir_with_parents (dirname, 0750);
dbname = g_strdup_printf ("%s.db", name);
db_file = g_build_filename (dirname, dbname, NULL);
g_free (dbname);
g_free (dirname);
db = NULL;
sqlite3_open (db_file, &db);
g_free (db_file);
sqlite3_extended_result_codes (db, 1);
self = RYGEL_MEDIA_EXPORT_DATABASE (g_object_new (RYGEL_MEDIA_EXPORT_TYPE_DATABASE,
"db", db,
"own-db", db,
NULL));
inner_error = NULL;
rygel_media_export_sqlite_wrapper_throw_if_db_has_error (RYGEL_MEDIA_EXPORT_SQLITE_WRAPPER (self),
&inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (self);
return NULL;
}
rygel_media_export_database_exec (self, "PRAGMA synchronous = OFF", NULL, 0, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (self);
return NULL;
}
rygel_media_export_database_exec (self, "PRAGMA temp_store = MEMORY", NULL, 0, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (self);
return NULL;
}
rygel_media_export_database_exec (self, "PRAGMA count_changes = OFF", NULL, 0, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (self);
return NULL;
}
sqlite3_create_function (db,
"contains",
2,
SQLITE_UTF8,
NULL,
rygel_media_export_database_utf8_contains_sqlite_user_func_callback,
NULL,
NULL);
sqlite3_create_collation (db,
"CASEFOLD",
SQLITE_UTF8,
NULL,
rygel_media_export_database_utf8_collate_sqlite_compare_callback);
return self;
}
/**
* SQL query function.
*
* Use for all queries that return a result set.
*
* @param sql The SQL query to run.
* @param args Values to bind in the SQL query or null.
* @throws DatabaseError if the underlying SQLite operation fails.
*/
RygelMediaExportDatabaseCursor*
rygel_media_export_database_exec_cursor (RygelMediaExportDatabase *self,
const gchar *sql,
GValue *arguments,
int arguments_length,
GError **error) {
RygelMediaExportDatabaseCursor *result;
sqlite3 *db;
GError *inner_error;
g_return_val_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self), NULL);
g_return_val_if_fail (sql != NULL, NULL);
db = rygel_media_export_sqlite_wrapper_get_db (RYGEL_MEDIA_EXPORT_SQLITE_WRAPPER (self));
inner_error = NULL;
result = rygel_media_export_database_cursor_new (db,
sql,
arguments,
arguments_length,
&inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return NULL;
}
return result;
}
/**
* Simple SQL query execution function.
*
* Use for all queries that don't return anything.
*
* @param sql The SQL query to run.
* @param args Values to bind in the SQL query or null.
* @throws DatabaseError if the underlying SQLite operation fails.
*/
void
rygel_media_export_database_exec (RygelMediaExportDatabase *self,
const gchar *sql,
GValue *arguments,
int arguments_length,
GError **error) {
RygelMediaExportDatabaseCursor *cursor;
GError *inner_error;
g_return_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self));
g_return_if_fail (sql != NULL);
inner_error = NULL;
if (!arguments) {
RygelMediaExportSqliteWrapper* self_wrapper = RYGEL_MEDIA_EXPORT_SQLITE_WRAPPER (self);
sqlite3* db = rygel_media_export_sqlite_wrapper_get_db (self_wrapper);
rygel_media_export_sqlite_wrapper_throw_if_code_is_error (self_wrapper,
sqlite3_exec (db, sql, NULL, NULL, NULL),
&inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
}
return;
}
cursor = rygel_media_export_database_exec_cursor (self,
sql,
arguments,
arguments_length,
&inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return;
}
while (rygel_media_export_database_cursor_has_next (cursor)) {
rygel_media_export_database_cursor_next (cursor, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
break;
}
}
g_object_unref (cursor);
}
/**
* Execute a SQL query that returns a single number.
*
* @param sql The SQL query to run.
* @param args Values to bind in the SQL query or null.
* @return The contents of the first row's column as an int.
* @throws DatabaseError if the underlying SQLite operation fails.
*/
gint
rygel_media_export_database_query_value (RygelMediaExportDatabase *self,
const gchar *sql,
GValue *args,
int args_length,
GError **error) {
gint result;
RygelMediaExportDatabaseCursor *cursor;
sqlite3_stmt *statement;
GError *inner_error;
g_return_val_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self), 0);
g_return_val_if_fail (sql != NULL, 0);
inner_error = NULL;
cursor = rygel_media_export_database_exec_cursor (self, sql, args, args_length, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return 0;
}
statement = rygel_media_export_database_cursor_next (cursor, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
g_object_unref (cursor);
return 0;
}
result = sqlite3_column_int (statement, 0);
g_object_unref (cursor);
return result;
}
/**
* Analyze triggers of database
*/
void
rygel_media_export_database_analyze (RygelMediaExportDatabase *self) {
g_return_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self));
sqlite3_exec (rygel_media_export_sqlite_wrapper_get_db (RYGEL_MEDIA_EXPORT_SQLITE_WRAPPER (self)),
"ANALYZE",
NULL,
NULL,
NULL);
}
/**
* Special GValue to pass to exec or exec_cursor to bind a column to
* NULL
*/
void
rygel_media_export_database_null (GValue *result) {
g_value_init (result, G_TYPE_POINTER);
g_value_set_pointer (result, NULL);
}
/**
* Start a transaction
*/
void rygel_media_export_database_begin (RygelMediaExportDatabase* self, GError** error) {
GError *inner_error;
g_return_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self));
inner_error = NULL;
rygel_media_export_database_exec (self, "BEGIN", NULL, 0, &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
}
}
/**
* Commit a transaction
*/
void rygel_media_export_database_commit (RygelMediaExportDatabase* self, GError** error) {
GError *inner_error;
g_return_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self));
inner_error = NULL;
rygel_media_export_database_exec (self,
"COMMIT",
NULL,
0,
&inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
}
}
/**
* Rollback a transaction
*/
void
rygel_media_export_database_rollback (RygelMediaExportDatabase *self) {
GError *inner_error;
g_return_if_fail (RYGEL_MEDIA_EXPORT_IS_DATABASE (self));
inner_error = NULL;
rygel_media_export_database_exec (self, "ROLLBACK", NULL, 0, &inner_error);
if (inner_error) {
g_critical (_("Failed to roll back transaction: %s"), inner_error->message);
g_error_free (inner_error);
}
}
static void
rygel_media_export_database_class_init (RygelMediaExportDatabaseClass *database_class G_GNUC_UNUSED) {
}
static void
rygel_media_export_database_init (RygelMediaExportDatabase *self G_GNUC_UNUSED) {
}
|