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 lms_db_image_new(sqlite3 *db)
88 if (lms_db_cache_get(&_cache, db, &p) == 0) {
97 if (_db_create_table_if_required(db) != 0) {
98 fprintf(stderr, "ERROR: could not create table.\n");
102 ldi = calloc(1, sizeof(lms_db_image_t));
103 ldi->_references = 1;
106 if (lms_db_cache_add(&_cache, db, ldi) != 0) {
107 lms_db_image_free(ldi);
115 lms_db_image_start(lms_db_image_t *ldi)
119 if (ldi->_is_started)
122 ldi->insert = lms_db_compile_stmt(ldi->db,
123 "INSERT OR REPLACE INTO images ("
124 "id, title, artist, date, width, height, orientation, "
125 "gps_lat, gps_long, gps_alt) VALUES ("
126 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
130 ldi->_is_started = 1;
135 lms_db_image_free(lms_db_image_t *ldi)
141 if (ldi->_references == 0) {
142 fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
147 if (ldi->_references > 0)
151 lms_db_finalize_stmt(ldi->insert, "insert");
153 r = lms_db_cache_del(&_cache, ldi->db, ldi);
160 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
167 ret = lms_db_bind_int64(stmt, 1, info->id);
171 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
175 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
179 ret = lms_db_bind_int(stmt, 4, info->date);
183 ret = lms_db_bind_int(stmt, 5, info->width);
187 ret = lms_db_bind_int(stmt, 6, info->height);
191 ret = lms_db_bind_int(stmt, 7, info->orientation);
195 ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
199 ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
203 ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
207 r = sqlite3_step(stmt);
208 if (r != SQLITE_DONE) {
209 fprintf(stderr, "ERROR: could not insert image info: %s\n",
210 sqlite3_errmsg(ldi->db));
218 lms_db_reset_stmt(stmt);
224 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
233 return _db_insert(ldi, info);