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_videos_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
22 "CREATE TABLE IF NOT EXISTS videos ("
23 "id INTEGER PRIMARY KEY, "
29 fprintf(stderr, "ERROR: could not create 'videos' table: %s\n", errmsg);
35 "CREATE INDEX IF NOT EXISTS videos_title_idx ON videos ("
41 "ERROR: could not create 'videos_title_idx' index: %s\n",
48 "CREATE INDEX IF NOT EXISTS videos_artist_idx ON videos ("
54 "ERROR: could not create 'videos_artist_idx' index: %s\n",
60 ret = lms_db_create_trigger_if_not_exists(db,
61 "delete_videos_on_files_deleted "
62 "DELETE ON files FOR EACH ROW BEGIN "
63 " DELETE FROM videos WHERE id = OLD.id; END;");
67 ret = lms_db_create_trigger_if_not_exists(db,
68 "delete_files_on_videos_deleted "
69 "DELETE ON videos FOR EACH ROW BEGIN "
70 " DELETE FROM files WHERE id = OLD.id; END;");
76 static lms_db_table_updater_t _db_table_updater_videos[] = {
77 _db_table_updater_videos_0
82 _db_create_table_if_required(sqlite3 *db)
84 return lms_db_table_update_if_required(db, "videos",
85 LMS_ARRAY_SIZE(_db_table_updater_videos),
86 _db_table_updater_videos);
90 * Create video DB access tool.
92 * Creates or get a reference to tools to access 'videos' table in an
93 * optimized and easy way.
95 * This is usually called from plugin's @b setup() callback with the @p db
98 * @param db database connection.
100 * @return DB access tool handle.
101 * @ingroup LMS_Plugins
104 lms_db_video_new(sqlite3 *db)
109 if (lms_db_cache_get(&_cache, db, &p) == 0) {
118 if (_db_create_table_if_required(db) != 0) {
119 fprintf(stderr, "ERROR: could not create table.\n");
123 ldv = calloc(1, sizeof(lms_db_video_t));
124 ldv->_references = 1;
127 if (lms_db_cache_add(&_cache, db, ldv) != 0) {
128 lms_db_video_free(ldv);
136 * Start video DB access tool.
138 * Compile SQL statements and other initialization functions.
140 * This is usually called from plugin's @b start() callback.
142 * @param ldv handle returned by lms_db_video_new().
144 * @return On success 0 is returned.
145 * @ingroup LMS_Plugins
148 lms_db_video_start(lms_db_video_t *ldv)
152 if (ldv->_is_started)
155 ldv->insert = lms_db_compile_stmt(ldv->db,
156 "INSERT OR REPLACE INTO videos (id, title, artist) VALUES (?, ?, ?)");
160 ldv->_is_started = 1;
165 * Free video DB access tool.
167 * Unreference and possible free resources allocated to access tool.
169 * This is usually called from plugin's @b finish() callback.
171 * @param ldv handle returned by lms_db_video_new().
173 * @return On success 0 is returned.
174 * @ingroup LMS_Plugins
177 lms_db_video_free(lms_db_video_t *ldv)
183 if (ldv->_references == 0) {
184 fprintf(stderr, "ERROR: over-called lms_db_video_free(%p)\n", ldv);
189 if (ldv->_references > 0)
193 lms_db_finalize_stmt(ldv->insert, "insert");
195 r = lms_db_cache_del(&_cache, ldv->db, ldv);
202 _db_insert(lms_db_video_t *ldv, const struct lms_video_info *info)
209 ret = lms_db_bind_int64(stmt, 1, info->id);
213 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
217 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
221 r = sqlite3_step(stmt);
222 if (r != SQLITE_DONE) {
223 fprintf(stderr, "ERROR: could not insert video info: %s\n",
224 sqlite3_errmsg(ldv->db));
232 lms_db_reset_stmt(stmt);
238 * Add video file to DB.
240 * This is usually called from plugin's @b parse() callback.
242 * @param ldv handle returned by lms_db_video_new().
243 * @param info video information to store.
245 * @return On success 0 is returned.
246 * @ingroup LMS_Plugins
249 lms_db_video_add(lms_db_video_t *ldv, struct lms_video_info *info)
258 return _db_insert(ldv, info);