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"
29 unsigned int _references;
30 unsigned int _is_started:1;
33 static struct lms_db_cache _cache = {0, NULL};
36 _db_table_updater_videos_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
42 "CREATE TABLE IF NOT EXISTS videos ("
43 "id INTEGER PRIMARY KEY, "
49 fprintf(stderr, "ERROR: could not create 'videos' table: %s\n", errmsg);
55 "CREATE INDEX IF NOT EXISTS videos_title_idx ON videos ("
61 "ERROR: could not create 'videos_title_idx' index: %s\n",
68 "CREATE INDEX IF NOT EXISTS videos_artist_idx ON videos ("
74 "ERROR: could not create 'videos_artist_idx' index: %s\n",
80 ret = lms_db_create_trigger_if_not_exists(db,
81 "delete_videos_on_files_deleted "
82 "DELETE ON files FOR EACH ROW BEGIN "
83 " DELETE FROM videos WHERE id = OLD.id; END;");
87 ret = lms_db_create_trigger_if_not_exists(db,
88 "delete_files_on_videos_deleted "
89 "DELETE ON videos FOR EACH ROW BEGIN "
90 " DELETE FROM files WHERE id = OLD.id; END;");
96 static lms_db_table_updater_t _db_table_updater_videos[] = {
97 _db_table_updater_videos_0
102 _db_create_table_if_required(sqlite3 *db)
104 return lms_db_table_update_if_required(db, "videos",
105 LMS_ARRAY_SIZE(_db_table_updater_videos),
106 _db_table_updater_videos);
110 * Create video DB access tool.
112 * Creates or get a reference to tools to access 'videos' table in an
113 * optimized and easy way.
115 * This is usually called from plugin's @b setup() callback with the @p db
118 * @param db database connection.
120 * @return DB access tool handle.
121 * @ingroup LMS_Plugins
124 lms_db_video_new(sqlite3 *db)
129 if (lms_db_cache_get(&_cache, db, &p) == 0) {
138 if (_db_create_table_if_required(db) != 0) {
139 fprintf(stderr, "ERROR: could not create table.\n");
143 ldv = calloc(1, sizeof(lms_db_video_t));
144 ldv->_references = 1;
147 if (lms_db_cache_add(&_cache, db, ldv) != 0) {
148 lms_db_video_free(ldv);
156 * Start video DB access tool.
158 * Compile SQL statements and other initialization functions.
160 * This is usually called from plugin's @b start() callback.
162 * @param ldv handle returned by lms_db_video_new().
164 * @return On success 0 is returned.
165 * @ingroup LMS_Plugins
168 lms_db_video_start(lms_db_video_t *ldv)
172 if (ldv->_is_started)
175 ldv->insert = lms_db_compile_stmt(ldv->db,
176 "INSERT OR REPLACE INTO videos (id, title, artist) VALUES (?, ?, ?)");
180 ldv->_is_started = 1;
185 * Free video DB access tool.
187 * Unreference and possible free resources allocated to access tool.
189 * This is usually called from plugin's @b finish() callback.
191 * @param ldv handle returned by lms_db_video_new().
193 * @return On success 0 is returned.
194 * @ingroup LMS_Plugins
197 lms_db_video_free(lms_db_video_t *ldv)
203 if (ldv->_references == 0) {
204 fprintf(stderr, "ERROR: over-called lms_db_video_free(%p)\n", ldv);
209 if (ldv->_references > 0)
213 lms_db_finalize_stmt(ldv->insert, "insert");
215 r = lms_db_cache_del(&_cache, ldv->db, ldv);
222 _db_insert(lms_db_video_t *ldv, const struct lms_video_info *info)
229 ret = lms_db_bind_int64(stmt, 1, info->id);
233 ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
237 ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
241 r = sqlite3_step(stmt);
242 if (r != SQLITE_DONE) {
243 fprintf(stderr, "ERROR: could not insert video info: %s\n",
244 sqlite3_errmsg(ldv->db));
252 lms_db_reset_stmt(stmt);
258 * Add video file to DB.
260 * This is usually called from plugin's @b parse() callback.
262 * @param ldv handle returned by lms_db_video_new().
263 * @param info video information to store.
265 * @return On success 0 is returned.
266 * @ingroup LMS_Plugins
269 lms_db_video_add(lms_db_video_t *ldv, struct lms_video_info *info)
278 return _db_insert(ldv, info);