2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
21 #include <lightmediascanner_db.h>
22 #include "lightmediascanner_db_private.h"
26 struct lms_db_playlist {
29 unsigned int _references;
30 unsigned int _is_started:1;
33 static struct lms_db_cache _cache = {0, NULL};
36 _db_table_updater_playlists_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
42 "CREATE TABLE IF NOT EXISTS playlists ("
43 "id INTEGER PRIMARY KEY, "
45 "n_entries INTEGER NOT NULL"
49 fprintf(stderr, "ERROR: could not create 'playlists' table: %s\n",
56 "CREATE INDEX IF NOT EXISTS playlists_title_idx ON "
61 "ERROR: could not create 'playlists_title_idx' index: %s\n",
67 ret = lms_db_create_trigger_if_not_exists(db,
68 "delete_playlists_on_files_deleted "
69 "DELETE ON files FOR EACH ROW BEGIN "
70 " DELETE FROM playlists WHERE id = OLD.id; END;");
74 ret = lms_db_create_trigger_if_not_exists(db,
75 "delete_files_on_playlists_deleted "
76 "DELETE ON playlists FOR EACH ROW BEGIN "
77 " DELETE FROM files WHERE id = OLD.id; END;");
83 static lms_db_table_updater_t _db_table_updater_playlists[] = {
84 _db_table_updater_playlists_0
89 _db_create_table_if_required(sqlite3 *db)
91 return lms_db_table_update_if_required(db, "playlists",
92 LMS_ARRAY_SIZE(_db_table_updater_playlists),
93 _db_table_updater_playlists);
97 * Create playlist DB access tool.
99 * Creates or get a reference to tools to access 'playlists' table in an
100 * optimized and easy way.
102 * This is usually called from plugin's @b setup() callback with the @p db
105 * @param db database connection.
107 * @return DB access tool handle.
108 * @ingroup LMS_Plugins
111 lms_db_playlist_new(sqlite3 *db)
113 lms_db_playlist_t *ldp;
116 if (lms_db_cache_get(&_cache, db, &p) == 0) {
125 if (_db_create_table_if_required(db) != 0) {
126 fprintf(stderr, "ERROR: could not create table.\n");
130 ldp = calloc(1, sizeof(lms_db_playlist_t));
131 ldp->_references = 1;
134 if (lms_db_cache_add(&_cache, db, ldp) != 0) {
135 lms_db_playlist_free(ldp);
143 * Start playlist DB access tool.
145 * Compile SQL statements and other initialization functions.
147 * This is usually called from plugin's @b start() callback.
149 * @param ldp handle returned by lms_db_playlist_new().
151 * @return On success 0 is returned.
152 * @ingroup LMS_Plugins
155 lms_db_playlist_start(lms_db_playlist_t *ldp)
159 if (ldp->_is_started)
162 ldp->insert = lms_db_compile_stmt(ldp->db,
163 "INSERT OR REPLACE INTO playlists (id, title, n_entries) "
168 ldp->_is_started = 1;
173 * Free playlist DB access tool.
175 * Unreference and possible free resources allocated to access tool.
177 * This is usually called from plugin's @b finish() callback.
179 * @param ldp handle returned by lms_db_playlist_new().
181 * @return On success 0 is returned.
182 * @ingroup LMS_Plugins
185 lms_db_playlist_free(lms_db_playlist_t *ldp)
191 if (ldp->_references == 0) {
192 fprintf(stderr, "ERROR: over-called lms_db_playlist_free(%p)\n", ldp);
197 if (ldp->_references > 0)
201 lms_db_finalize_stmt(ldp->insert, "insert");
203 r = lms_db_cache_del(&_cache, ldp->db, ldp);
210 _db_insert(lms_db_playlist_t *ldp, const struct lms_playlist_info *info)
217 ret = lms_db_bind_int64(stmt, 1, info->id);
221 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
225 ret = lms_db_bind_int(stmt, 3, info->n_entries);
229 r = sqlite3_step(stmt);
230 if (r != SQLITE_DONE) {
231 fprintf(stderr, "ERROR: could not insert playlist info: %s\n",
232 sqlite3_errmsg(ldp->db));
240 lms_db_reset_stmt(stmt);
246 * Add playlist file to DB.
248 * This is usually called from plugin's @b parse() callback.
250 * @param ldp handle returned by lms_db_playlist_new().
251 * @param info playlist information to store.
253 * @return On success 0 is returned.
254 * @ingroup LMS_Plugins
257 lms_db_playlist_add(lms_db_playlist_t *ldp, struct lms_playlist_info *info)
266 return _db_insert(ldp, info);