2 * Copyright (C) 2008-2011 by ProFUSION embedded systems
3 * Copyright (C) 2007 by INdT
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
23 #include <lightmediascanner_db.h>
24 #include "lightmediascanner_db_private.h"
31 unsigned int _references;
32 unsigned int _is_started:1;
35 static struct lms_db_cache _cache = { };
38 _db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
44 "CREATE TABLE IF NOT EXISTS images ("
45 "id INTEGER PRIMARY KEY, "
48 "date INTEGER NOT NULL, "
49 "width INTEGER NOT NULL, "
50 "height INTEGER NOT NULL, "
51 "orientation INTEGER NOT NULL, "
52 "gps_lat REAL DEFAULT 0.0, "
53 "gps_long REAL DEFAULT 0.0, "
54 "gps_alt REAL DEFAULT 0.0"
58 fprintf(stderr, "ERROR: could not create 'images' table: %s\n", errmsg);
64 "CREATE INDEX IF NOT EXISTS images_date_idx ON images ("
69 fprintf(stderr, "ERROR: could not create 'images_date_idx' index: %s\n",
75 ret = lms_db_create_trigger_if_not_exists(db,
76 "delete_images_on_files_deleted "
77 "DELETE ON files FOR EACH ROW BEGIN "
78 " DELETE FROM images WHERE id = OLD.id; END;");
82 ret = lms_db_create_trigger_if_not_exists(db,
83 "delete_files_on_images_deleted "
84 "DELETE ON images FOR EACH ROW BEGIN "
85 " DELETE FROM files WHERE id = OLD.id; END;");
92 _db_table_updater_images_1(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run)
98 db, "BEGIN TRANSACTION;"
99 "ALTER TABLE images ADD COLUMN dlna_profile TEXT DEFAULT NULL;"
100 "ALTER TABLE images ADD COLUMN dlna_mime TEXT DEFAULT NULL;"
103 if (ret != SQLITE_OK) {
104 fprintf(stderr, "ERROR: could add columns to images table: %s\n", err);
112 _db_table_updater_images_2(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run)
118 db, "BEGIN TRANSACTION;"
119 "ALTER TABLE images ADD COLUMN container TEXT DEFAULT NULL;"
122 if (ret != SQLITE_OK) {
123 fprintf(stderr, "ERROR: could add columns to images table: %s\n", err);
130 static lms_db_table_updater_t _db_table_updater_images[] = {
131 _db_table_updater_images_0,
132 _db_table_updater_images_1,
133 _db_table_updater_images_2
138 _db_create_table_if_required(sqlite3 *db)
140 return lms_db_table_update_if_required(db, "images",
141 LMS_ARRAY_SIZE(_db_table_updater_images),
142 _db_table_updater_images);
146 * Create image DB access tool.
148 * Creates or get a reference to tools to access 'images' table in an
149 * optimized and easy way.
151 * This is usually called from plugin's @b setup() callback with the @p db
154 * @param db database connection.
156 * @return DB access tool handle.
157 * @ingroup LMS_Plugins
160 lms_db_image_new(sqlite3 *db)
165 if (lms_db_cache_get(&_cache, db, &p) == 0) {
174 if (_db_create_table_if_required(db) != 0) {
175 fprintf(stderr, "ERROR: could not create table.\n");
179 ldi = calloc(1, sizeof(lms_db_image_t));
180 ldi->_references = 1;
183 if (lms_db_cache_add(&_cache, db, ldi) != 0) {
184 lms_db_image_free(ldi);
192 * Start image DB access tool.
194 * Compile SQL statements and other initialization functions.
196 * This is usually called from plugin's @b start() callback.
198 * @param ldi handle returned by lms_db_image_new().
200 * @return On success 0 is returned.
201 * @ingroup LMS_Plugins
204 lms_db_image_start(lms_db_image_t *ldi)
208 if (ldi->_is_started)
211 ldi->insert = lms_db_compile_stmt(ldi->db,
212 "INSERT OR REPLACE INTO images ("
213 "id, title, artist, date, width, height, orientation, "
214 "gps_lat, gps_long, gps_alt, dlna_profile, dlna_mime, container) "
215 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
219 ldi->_is_started = 1;
224 * Free image DB access tool.
226 * Unreference and possible free resources allocated to access tool.
228 * This is usually called from plugin's @b finish() callback.
230 * @param ldi handle returned by lms_db_image_new().
232 * @return On success 0 is returned.
233 * @ingroup LMS_Plugins
236 lms_db_image_free(lms_db_image_t *ldi)
242 if (ldi->_references == 0) {
243 fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
248 if (ldi->_references > 0)
252 lms_db_finalize_stmt(ldi->insert, "insert");
254 r = lms_db_cache_del(&_cache, ldi->db, ldi);
261 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
268 ret = lms_db_bind_int64(stmt, 1, info->id);
272 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
276 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
280 ret = lms_db_bind_int(stmt, 4, info->date);
284 ret = lms_db_bind_int(stmt, 5, info->width);
288 ret = lms_db_bind_int(stmt, 6, info->height);
292 ret = lms_db_bind_int(stmt, 7, info->orientation);
296 ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
300 ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
304 ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
308 ret = lms_db_bind_text(stmt, 11, info->dlna_profile.str,
309 info->dlna_profile.len);
313 ret = lms_db_bind_text(stmt, 12, info->dlna_mime.str, info->dlna_mime.len);
317 ret = lms_db_bind_text(stmt, 13, info->container.str, info->container.len);
321 r = sqlite3_step(stmt);
322 if (r != SQLITE_DONE) {
323 fprintf(stderr, "ERROR: could not insert image info: %s\n",
324 sqlite3_errmsg(ldi->db));
332 lms_db_reset_stmt(stmt);
338 * Add image file to DB.
340 * This is usually called from plugin's @b parse() callback.
342 * @param ldi handle returned by lms_db_image_new().
343 * @param info image information to store.
345 * @return On success 0 is returned.
346 * @ingroup LMS_Plugins
349 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
358 return _db_insert(ldi, info);