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);
111 static lms_db_table_updater_t _db_table_updater_images[] = {
112 _db_table_updater_images_0,
113 _db_table_updater_images_1
118 _db_create_table_if_required(sqlite3 *db)
120 return lms_db_table_update_if_required(db, "images",
121 LMS_ARRAY_SIZE(_db_table_updater_images),
122 _db_table_updater_images);
126 * Create image DB access tool.
128 * Creates or get a reference to tools to access 'images' table in an
129 * optimized and easy way.
131 * This is usually called from plugin's @b setup() callback with the @p db
134 * @param db database connection.
136 * @return DB access tool handle.
137 * @ingroup LMS_Plugins
140 lms_db_image_new(sqlite3 *db)
145 if (lms_db_cache_get(&_cache, db, &p) == 0) {
154 if (_db_create_table_if_required(db) != 0) {
155 fprintf(stderr, "ERROR: could not create table.\n");
159 ldi = calloc(1, sizeof(lms_db_image_t));
160 ldi->_references = 1;
163 if (lms_db_cache_add(&_cache, db, ldi) != 0) {
164 lms_db_image_free(ldi);
172 * Start image DB access tool.
174 * Compile SQL statements and other initialization functions.
176 * This is usually called from plugin's @b start() callback.
178 * @param ldi handle returned by lms_db_image_new().
180 * @return On success 0 is returned.
181 * @ingroup LMS_Plugins
184 lms_db_image_start(lms_db_image_t *ldi)
188 if (ldi->_is_started)
191 ldi->insert = lms_db_compile_stmt(ldi->db,
192 "INSERT OR REPLACE INTO images ("
193 "id, title, artist, date, width, height, orientation, "
194 "gps_lat, gps_long, gps_alt, dlna_profile, dlna_mime) VALUES ("
195 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
199 ldi->_is_started = 1;
204 * Free image DB access tool.
206 * Unreference and possible free resources allocated to access tool.
208 * This is usually called from plugin's @b finish() callback.
210 * @param ldi handle returned by lms_db_image_new().
212 * @return On success 0 is returned.
213 * @ingroup LMS_Plugins
216 lms_db_image_free(lms_db_image_t *ldi)
222 if (ldi->_references == 0) {
223 fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
228 if (ldi->_references > 0)
232 lms_db_finalize_stmt(ldi->insert, "insert");
234 r = lms_db_cache_del(&_cache, ldi->db, ldi);
241 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
248 ret = lms_db_bind_int64(stmt, 1, info->id);
252 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
256 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
260 ret = lms_db_bind_int(stmt, 4, info->date);
264 ret = lms_db_bind_int(stmt, 5, info->width);
268 ret = lms_db_bind_int(stmt, 6, info->height);
272 ret = lms_db_bind_int(stmt, 7, info->orientation);
276 ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
280 ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
284 ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
288 ret = lms_db_bind_text(stmt, 11, info->dlna_profile.str,
289 info->dlna_profile.len);
293 ret = lms_db_bind_text(stmt, 12, info->dlna_mime.str, info->dlna_mime.len);
297 r = sqlite3_step(stmt);
298 if (r != SQLITE_DONE) {
299 fprintf(stderr, "ERROR: could not insert image info: %s\n",
300 sqlite3_errmsg(ldi->db));
308 lms_db_reset_stmt(stmt);
314 * Add image file to DB.
316 * This is usually called from plugin's @b parse() callback.
318 * @param ldi handle returned by lms_db_image_new().
319 * @param info image information to store.
321 * @return On success 0 is returned.
322 * @ingroup LMS_Plugins
325 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
334 return _db_insert(ldi, info);