1 #include <lightmediascanner_db.h>
2 #include "lightmediascanner_db_private.h"
12 lms_db_image_t *_singleton = NULL;
15 _db_create_table_if_required(sqlite3 *db)
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 "thumb_width INTEGER NOT NULL, "
31 "thumb_height INTEGER NOT NULL, "
32 "gps_lat REAL DEFAULT 0.0, "
33 "gps_long REAL DEFAULT 0.0, "
34 "gps_alt REAL DEFAULT 0.0"
38 fprintf(stderr, "ERROR: could not create 'images' table: %s\n", errmsg);
44 "CREATE INDEX IF NOT EXISTS images_date_idx ON images ("
49 fprintf(stderr, "ERROR: could not create 'images_date_idx' index: %s\n",
56 "CREATE TRIGGER IF NOT EXISTS"
57 " delete_images_on_files_deleted "
60 " DELETE FROM images WHERE id = OLD.id;"
64 fprintf(stderr, "ERROR: could not create trigger to delete images on "
65 "files deletion: %s\n",
72 "CREATE TRIGGER IF NOT EXISTS"
73 " delete_files_on_images_deleted "
76 " DELETE FROM files WHERE id = OLD.id;"
80 fprintf(stderr, "ERROR: could not create trigger to delete files on "
81 "images deletion: %s\n",
91 _db_compile_all_stmts(lms_db_image_t *ldi)
93 ldi->insert = lms_db_compile_stmt(ldi->db,
94 "INSERT OR REPLACE INTO images ("
95 "id, title, artist, date, width, height, orientation, "
96 "thumb_width, thumb_height, gps_lat, gps_long, gps_alt) VALUES ("
97 "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
105 lms_db_image_new(sqlite3 *db)
110 _singleton->_references++;
117 if (_db_create_table_if_required(db) != 0) {
118 fprintf(stderr, "ERROR: could not create table.\n");
122 ldi = calloc(1, sizeof(lms_db_image_t));
123 ldi->_references = 1;
126 if (_db_compile_all_stmts(ldi) != 0) {
127 fprintf(stderr, "ERROR: could not compile image statements.\n");
128 lms_db_image_free(ldi);
136 lms_db_image_free(lms_db_image_t *ldi)
142 if (ldi->_references > 0)
146 lms_db_finalize_stmt(ldi->insert, "insert");
155 _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
159 unsigned long tw, th;
161 if (info->height < info->width) {
163 th = (info->height * 128) / info->width;
166 } else if (info->height == info->width)
170 tw = (info->width * 128) / info->height;
177 ret = lms_db_bind_int64(stmt, 1, info->id);
181 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
185 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
189 ret = lms_db_bind_int(stmt, 4, info->date);
193 ret = lms_db_bind_int(stmt, 5, info->width);
197 ret = lms_db_bind_int(stmt, 6, info->height);
201 ret = lms_db_bind_int(stmt, 7, info->orientation);
205 ret = lms_db_bind_int(stmt, 8, tw);
209 ret = lms_db_bind_int(stmt, 9, th);
213 ret = lms_db_bind_double(stmt, 10, info->gps.latitude);
217 ret = lms_db_bind_double(stmt, 11, info->gps.longitude);
221 ret = lms_db_bind_double(stmt, 12, info->gps.altitude);
225 r = sqlite3_step(stmt);
226 if (r != SQLITE_DONE) {
227 fprintf(stderr, "ERROR: could not insert image info: %s\n",
228 sqlite3_errmsg(ldi->db));
236 lms_db_reset_stmt(stmt);
242 lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
251 return _db_insert(ldi, info);