2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
21 #include <lightmediascanner_db.h>
22 #include "lightmediascanner_db_private.h"
29 unsigned int _references;
30 unsigned int _is_started:1;
33 static struct lms_db_cache _cache = {0, NULL};
36 _db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
42 "CREATE TABLE IF NOT EXISTS images ("
43 "id INTEGER PRIMARY KEY, "
46 "date INTEGER NOT NULL, "
47 "width INTEGER NOT NULL, "
48 "height INTEGER NOT NULL, "
49 "orientation INTEGER NOT NULL, "
50 "gps_lat REAL DEFAULT 0.0, "
51 "gps_long REAL DEFAULT 0.0, "
52 "gps_alt REAL DEFAULT 0.0"
56 fprintf(stderr, "ERROR: could not create 'images' table: %s\n", errmsg);
62 "CREATE INDEX IF NOT EXISTS images_date_idx ON images ("
67 fprintf(stderr, "ERROR: could not create 'images_date_idx' index: %s\n",
73 ret = lms_db_create_trigger_if_not_exists(db,
74 "delete_images_on_files_deleted "
75 "DELETE ON files FOR EACH ROW BEGIN "
76 " DELETE FROM images WHERE id = OLD.id; END;");
80 ret = lms_db_create_trigger_if_not_exists(db,
81 "delete_files_on_images_deleted "
82 "DELETE ON images FOR EACH ROW BEGIN "
83 " DELETE FROM files WHERE id = OLD.id; END;");
89 static lms_db_table_updater_t _db_table_updater_images[] = {
90 _db_table_updater_images_0
95 _db_create_table_if_required(sqlite3 *db)
97 return lms_db_table_update_if_required(db, "images",
98 LMS_ARRAY_SIZE(_db_table_updater_images),
99 _db_table_updater_images);
103 * Create image DB access tool.
105 * Creates or get a reference to tools to access 'images' table in an
106 * optimized and easy way.
108 * This is usually called from plugin's @b setup() callback with the @p db
111 * @param db database connection.
113 * @return DB access tool handle.
114 * @ingroup LMS_Plugins
117 lms_db_image_new(sqlite3 *db)
122 if (lms_db_cache_get(&_cache, db, &p) == 0) {
131 if (_db_create_table_if_required(db) != 0) {
132 fprintf(stderr, "ERROR: could not create table.\n");
136 ldi = calloc(1, sizeof(lms_db_image_t));
137 ldi->_references = 1;
140 if (lms_db_cache_add(&_cache, db, ldi) != 0) {
141 lms_db_image_free(ldi);
149 * Start image DB access tool.
151 * Compile SQL statements and other initialization functions.
153 * This is usually called from plugin's @b start() callback.
155 * @param ldi handle returned by lms_db_image_new().
157 * @return On success 0 is returned.
158 * @ingroup LMS_Plugins
161 lms_db_image_start(lms_db_image_t *ldi)
165 if (ldi->_is_started)
168 ldi->insert = lms_db_compile_stmt(ldi->db,
169 "INSERT OR REPLACE INTO images ("
170 "id, title, artist, date, width, height, orientation, "
171 "gps_lat, gps_long, gps_alt) VALUES ("
172 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
176 ldi->_is_started = 1;
181 * Free image DB access tool.
183 * Unreference and possible free resources allocated to access tool.
185 * This is usually called from plugin's @b finish() callback.
187 * @param ldi handle returned by lms_db_image_new().
189 * @return On success 0 is returned.
190 * @ingroup LMS_Plugins
193 lms_db_image_free(lms_db_image_t *ldi)
199 if (ldi->_references == 0) {
200 fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
205 if (ldi->_references > 0)
209 lms_db_finalize_stmt(ldi->insert, "insert");
211 r = lms_db_cache_del(&_cache, ldi->db, ldi);
218 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
225 ret = lms_db_bind_int64(stmt, 1, info->id);
229 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
233 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
237 ret = lms_db_bind_int(stmt, 4, info->date);
241 ret = lms_db_bind_int(stmt, 5, info->width);
245 ret = lms_db_bind_int(stmt, 6, info->height);
249 ret = lms_db_bind_int(stmt, 7, info->orientation);
253 ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
257 ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
261 ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
265 r = sqlite3_step(stmt);
266 if (r != SQLITE_DONE) {
267 fprintf(stderr, "ERROR: could not insert image info: %s\n",
268 sqlite3_errmsg(ldi->db));
276 lms_db_reset_stmt(stmt);
282 * Add image file to DB.
284 * This is usually called from plugin's @b parse() callback.
286 * @param ldi handle returned by lms_db_image_new().
287 * @param info image information to store.
289 * @return On success 0 is returned.
290 * @ingroup LMS_Plugins
293 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
302 return _db_insert(ldi, info);