1 #include <lightmediascanner_db.h>
2 #include "lightmediascanner_db_private.h"
9 unsigned int _references;
10 unsigned int _is_started:1;
13 static struct lms_db_cache _cache = {0, NULL};
16 _db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
22 "CREATE TABLE IF NOT EXISTS images ("
23 "id INTEGER PRIMARY KEY, "
26 "date INTEGER NOT NULL, "
27 "width INTEGER NOT NULL, "
28 "height INTEGER NOT NULL, "
29 "orientation INTEGER NOT NULL, "
30 "gps_lat REAL DEFAULT 0.0, "
31 "gps_long REAL DEFAULT 0.0, "
32 "gps_alt REAL DEFAULT 0.0"
36 fprintf(stderr, "ERROR: could not create 'images' table: %s\n", errmsg);
42 "CREATE INDEX IF NOT EXISTS images_date_idx ON images ("
47 fprintf(stderr, "ERROR: could not create 'images_date_idx' index: %s\n",
53 ret = lms_db_create_trigger_if_not_exists(db,
54 "delete_images_on_files_deleted "
55 "DELETE ON files FOR EACH ROW BEGIN "
56 " DELETE FROM images WHERE id = OLD.id; END;");
60 ret = lms_db_create_trigger_if_not_exists(db,
61 "delete_files_on_images_deleted "
62 "DELETE ON images FOR EACH ROW BEGIN "
63 " DELETE FROM files WHERE id = OLD.id; END;");
69 static lms_db_table_updater_t _db_table_updater_images[] = {
70 _db_table_updater_images_0
75 _db_create_table_if_required(sqlite3 *db)
77 return lms_db_table_update_if_required(db, "images",
78 LMS_ARRAY_SIZE(_db_table_updater_images),
79 _db_table_updater_images);
83 * Create image DB access tool.
85 * Creates or get a reference to tools to access 'images' table in an
86 * optimized and easy way.
88 * This is usually called from plugin's @b setup() callback with the @p db
91 * @param db database connection.
93 * @return DB access tool handle.
94 * @ingroup LMS_Plugins
97 lms_db_image_new(sqlite3 *db)
102 if (lms_db_cache_get(&_cache, db, &p) == 0) {
111 if (_db_create_table_if_required(db) != 0) {
112 fprintf(stderr, "ERROR: could not create table.\n");
116 ldi = calloc(1, sizeof(lms_db_image_t));
117 ldi->_references = 1;
120 if (lms_db_cache_add(&_cache, db, ldi) != 0) {
121 lms_db_image_free(ldi);
129 * Start image DB access tool.
131 * Compile SQL statements and other initialization functions.
133 * This is usually called from plugin's @b start() callback.
135 * @param ldi handle returned by lms_db_image_new().
137 * @return On success 0 is returned.
138 * @ingroup LMS_Plugins
141 lms_db_image_start(lms_db_image_t *ldi)
145 if (ldi->_is_started)
148 ldi->insert = lms_db_compile_stmt(ldi->db,
149 "INSERT OR REPLACE INTO images ("
150 "id, title, artist, date, width, height, orientation, "
151 "gps_lat, gps_long, gps_alt) VALUES ("
152 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
156 ldi->_is_started = 1;
161 * Free image DB access tool.
163 * Unreference and possible free resources allocated to access tool.
165 * This is usually called from plugin's @b finish() callback.
167 * @param ldi handle returned by lms_db_image_new().
169 * @return On success 0 is returned.
170 * @ingroup LMS_Plugins
173 lms_db_image_free(lms_db_image_t *ldi)
179 if (ldi->_references == 0) {
180 fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
185 if (ldi->_references > 0)
189 lms_db_finalize_stmt(ldi->insert, "insert");
191 r = lms_db_cache_del(&_cache, ldi->db, ldi);
198 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
205 ret = lms_db_bind_int64(stmt, 1, info->id);
209 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
213 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
217 ret = lms_db_bind_int(stmt, 4, info->date);
221 ret = lms_db_bind_int(stmt, 5, info->width);
225 ret = lms_db_bind_int(stmt, 6, info->height);
229 ret = lms_db_bind_int(stmt, 7, info->orientation);
233 ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
237 ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
241 ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
245 r = sqlite3_step(stmt);
246 if (r != SQLITE_DONE) {
247 fprintf(stderr, "ERROR: could not insert image info: %s\n",
248 sqlite3_errmsg(ldi->db));
256 lms_db_reset_stmt(stmt);
262 * Add image file to DB.
264 * This is usually called from plugin's @b parse() callback.
266 * @param ldi handle returned by lms_db_image_new().
267 * @param info image information to store.
269 * @return On success 0 is returned.
270 * @ingroup LMS_Plugins
273 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
282 return _db_insert(ldi, info);