1 #include <lightmediascanner_db.h>
2 #include "lightmediascanner_db_private.h"
6 struct lms_db_playlist {
9 unsigned int _references;
10 unsigned int _is_started:1;
13 static struct lms_db_cache _cache = {0, NULL};
16 _db_table_updater_playlists_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
22 "CREATE TABLE IF NOT EXISTS playlists ("
23 "id INTEGER PRIMARY KEY, "
25 "n_entries INTEGER NOT NULL"
29 fprintf(stderr, "ERROR: could not create 'playlists' table: %s\n",
36 "CREATE INDEX IF NOT EXISTS playlists_title_idx ON "
41 "ERROR: could not create 'playlists_title_idx' index: %s\n",
47 ret = lms_db_create_trigger_if_not_exists(db,
48 "delete_playlists_on_files_deleted "
49 "DELETE ON files FOR EACH ROW BEGIN "
50 " DELETE FROM playlists WHERE id = OLD.id; END;");
54 ret = lms_db_create_trigger_if_not_exists(db,
55 "delete_files_on_playlists_deleted "
56 "DELETE ON playlists FOR EACH ROW BEGIN "
57 " DELETE FROM files WHERE id = OLD.id; END;");
63 static lms_db_table_updater_t _db_table_updater_playlists[] = {
64 _db_table_updater_playlists_0
69 _db_create_table_if_required(sqlite3 *db)
71 return lms_db_table_update_if_required(db, "playlists",
72 LMS_ARRAY_SIZE(_db_table_updater_playlists),
73 _db_table_updater_playlists);
77 * Create playlist DB access tool.
79 * Creates or get a reference to tools to access 'playlists' table in an
80 * optimized and easy way.
82 * This is usually called from plugin's @b setup() callback with the @p db
85 * @param db database connection.
87 * @return DB access tool handle.
88 * @ingroup LMS_Plugins
91 lms_db_playlist_new(sqlite3 *db)
93 lms_db_playlist_t *ldp;
96 if (lms_db_cache_get(&_cache, db, &p) == 0) {
105 if (_db_create_table_if_required(db) != 0) {
106 fprintf(stderr, "ERROR: could not create table.\n");
110 ldp = calloc(1, sizeof(lms_db_playlist_t));
111 ldp->_references = 1;
114 if (lms_db_cache_add(&_cache, db, ldp) != 0) {
115 lms_db_playlist_free(ldp);
123 * Start playlist DB access tool.
125 * Compile SQL statements and other initialization functions.
127 * This is usually called from plugin's @b start() callback.
129 * @param ldp handle returned by lms_db_playlist_new().
131 * @return On success 0 is returned.
132 * @ingroup LMS_Plugins
135 lms_db_playlist_start(lms_db_playlist_t *ldp)
139 if (ldp->_is_started)
142 ldp->insert = lms_db_compile_stmt(ldp->db,
143 "INSERT OR REPLACE INTO playlists (id, title, n_entries) "
148 ldp->_is_started = 1;
153 * Free playlist DB access tool.
155 * Unreference and possible free resources allocated to access tool.
157 * This is usually called from plugin's @b finish() callback.
159 * @param ldp handle returned by lms_db_playlist_new().
161 * @return On success 0 is returned.
162 * @ingroup LMS_Plugins
165 lms_db_playlist_free(lms_db_playlist_t *ldp)
171 if (ldp->_references == 0) {
172 fprintf(stderr, "ERROR: over-called lms_db_playlist_free(%p)\n", ldp);
177 if (ldp->_references > 0)
181 lms_db_finalize_stmt(ldp->insert, "insert");
183 r = lms_db_cache_del(&_cache, ldp->db, ldp);
190 _db_insert(lms_db_playlist_t *ldp, const struct lms_playlist_info *info)
197 ret = lms_db_bind_int64(stmt, 1, info->id);
201 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
205 ret = lms_db_bind_int(stmt, 3, info->n_entries);
209 r = sqlite3_step(stmt);
210 if (r != SQLITE_DONE) {
211 fprintf(stderr, "ERROR: could not insert playlist info: %s\n",
212 sqlite3_errmsg(ldp->db));
220 lms_db_reset_stmt(stmt);
226 * Add playlist file to DB.
228 * This is usually called from plugin's @b parse() callback.
230 * @param ldp handle returned by lms_db_playlist_new().
231 * @param info playlist information to store.
233 * @return On success 0 is returned.
234 * @ingroup LMS_Plugins
237 lms_db_playlist_add(lms_db_playlist_t *ldp, struct lms_playlist_info *info)
246 return _db_insert(ldp, info);