2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "TextureDatabase.h"
22 #include "utils/log.h"
23 #include "XBDateTime.h"
24 #include "dbwrappers/dataset.h"
27 CTextureDatabase::CTextureDatabase()
31 CTextureDatabase::~CTextureDatabase()
35 bool CTextureDatabase::Open()
37 return CDatabase::Open();
40 bool CTextureDatabase::CreateTables()
44 CDatabase::CreateTables();
46 CLog::Log(LOGINFO, "create texture table");
47 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, imagehash text, lasthashcheck text)");
49 CLog::Log(LOGINFO, "create textures index");
50 m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
52 CLog::Log(LOGINFO, "create sizes table, index, and trigger");
53 m_pDS->exec("CREATE TABLE sizes (idtexture integer, size integer, width integer, height integer, usecount integer, lastusetime text)");
54 m_pDS->exec("CREATE INDEX idxSize ON sizes(idtexture, size)");
55 m_pDS->exec("CREATE INDEX idxSize2 ON sizes(idtexture, width, height)");
56 m_pDS->exec("CREATE TRIGGER textureDelete AFTER delete ON texture FOR EACH ROW BEGIN delete from sizes where sizes.idtexture=old.id; END");
58 CLog::Log(LOGINFO, "create path table");
59 m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
61 // TODO: Should the path index be a covering index? (we need only retrieve texture)
62 CLog::Log(LOGINFO, "create path index");
63 m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
67 CLog::Log(LOGERROR, "%s unable to create tables", __FUNCTION__);
74 bool CTextureDatabase::UpdateOldVersion(int version)
77 { // update all old thumb://foo urls to image://foo?size=thumb
78 m_pDS->query("select id,texture from path where texture like 'thumb://%'");
81 unsigned int id = m_pDS->fv(0).get_asInt();
82 CURL url(m_pDS->fv(1).get_asString());
83 m_pDS2->exec(PrepareSQL("update path set texture='image://%s?size=thumb' where id=%u", url.GetHostName().c_str(), id));
86 m_pDS->query("select id, url from texture where url like 'thumb://%'");
89 unsigned int id = m_pDS->fv(0).get_asInt();
90 CURL url(m_pDS->fv(1).get_asString());
91 m_pDS2->exec(PrepareSQL("update texture set url='image://%s?size=thumb', urlhash=0 where id=%u", url.GetHostName().c_str(), id));
97 { // get rid of old cached thumbs as they were previously set to the cached thumb name instead of the source thumb
98 m_pDS->exec("delete from path");
101 { // get rid of the old path table and add the type column
102 m_pDS->dropIndex("path", "idxPath");
103 m_pDS->exec("DROP TABLE path");
104 m_pDS->exec("CREATE TABLE path (id integer primary key, urlhash integer, url text, type text, texture text)\n");
105 m_pDS->exec("CREATE INDEX idxPath ON path(urlhash, type)");
108 { // get rid of urlhash in both tables...
109 m_pDS->dropIndex("path", "idxPath");
110 m_pDS->exec("DROP TABLE path");
111 m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
112 m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
114 m_pDS->dropIndex("texture", "idxTexture");
115 m_pDS->exec("CREATE TEMPORARY TABLE texture_backup(id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck)");
116 m_pDS->exec("INSERT INTO texture_backup SELECT id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck FROM texture");
117 m_pDS->exec("DROP TABLE texture");
118 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, usecount integer, lastusetime text, imagehash text, lasthashcheck text)");
119 m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
120 m_pDS->exec("INSERT INTO texture SELECT * FROM texture_backup");
121 m_pDS->exec("DROP TABLE texture_backup");
124 { // get rid of cached URLs that don't have the correct extension
125 m_pDS->exec("DELETE FROM texture WHERE SUBSTR(cachedUrl,-4,4) NOT IN ('.jpg', '.png')");
128 { // create new sizes table and move usecount info to it.
129 m_pDS->exec("DROP TABLE texture");
130 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, imagehash text, lasthashcheck text)");
131 m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
132 m_pDS->exec("CREATE TABLE sizes (idtexture integer, size integer, width integer, height integer, usecount integer, lastusetime text)");
133 m_pDS->exec("CREATE INDEX idxSize ON sizes(idtexture, size)");
134 m_pDS->exec("CREATE TRIGGER textureDelete AFTER delete ON texture FOR EACH ROW BEGIN delete from sizes where sizes.idtexture=old.id; END");
137 { // index for updateusecount
138 m_pDS->exec("CREATE INDEX idxSize2 ON sizes(idtexture, width, height)");
143 bool CTextureDatabase::IncrementUseCount(const CTextureDetails &details)
145 CStdString sql = PrepareSQL("UPDATE sizes SET usecount=usecount+1, lastusetime=CURRENT_TIMESTAMP WHERE idtexture=%u AND width=%u AND height=%u", details.id, details.width, details.height);
146 return ExecuteQuery(sql);
149 bool CTextureDatabase::GetCachedTexture(const CStdString &url, CTextureDetails &details)
153 if (NULL == m_pDB.get()) return false;
154 if (NULL == m_pDS.get()) return false;
156 CStdString sql = PrepareSQL("SELECT id, cachedurl, lasthashcheck, imagehash, width, height FROM texture JOIN sizes ON (texture.id=sizes.idtexture AND sizes.size=1) WHERE url='%s'", url.c_str());
157 m_pDS->query(sql.c_str());
159 { // have some information
160 details.id = m_pDS->fv(0).get_asInt();
161 details.file = m_pDS->fv(1).get_asString();
163 lastCheck.SetFromDBDateTime(m_pDS->fv(2).get_asString());
164 if (lastCheck.IsValid() && lastCheck + CDateTimeSpan(1,0,0,0) < CDateTime::GetCurrentDateTime())
165 details.hash = m_pDS->fv(3).get_asString();
166 details.width = m_pDS->fv(4).get_asInt();
167 details.height = m_pDS->fv(5).get_asInt();
175 CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
180 bool CTextureDatabase::SetCachedTextureValid(const CStdString &url, bool updateable)
182 CStdString date = updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
183 CStdString sql = PrepareSQL("UPDATE texture SET lasthashcheck='%s' WHERE url='%s'", date.c_str(), url.c_str());
184 return ExecuteQuery(sql);
187 bool CTextureDatabase::AddCachedTexture(const CStdString &url, const CTextureDetails &details)
191 if (NULL == m_pDB.get()) return false;
192 if (NULL == m_pDS.get()) return false;
194 CStdString sql = PrepareSQL("DELETE FROM texture WHERE url='%s'", url.c_str());
195 m_pDS->exec(sql.c_str());
197 CStdString date = details.updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
198 sql = PrepareSQL("INSERT INTO texture (id, url, cachedurl, imagehash, lasthashcheck) VALUES(NULL, '%s', '%s', '%s', '%s')", url.c_str(), details.file.c_str(), details.hash.c_str(), date.c_str());
199 m_pDS->exec(sql.c_str());
200 int textureID = (int)m_pDS->lastinsertid();
202 // set the size information
203 sql = PrepareSQL("INSERT INTO sizes (idtexture, size, usecount, lastusetime, width, height) VALUES(%u, 1, 1, CURRENT_TIMESTAMP, %u, %u)", textureID, details.width, details.height);
204 m_pDS->exec(sql.c_str());
208 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
213 bool CTextureDatabase::ClearCachedTexture(const CStdString &url, CStdString &cacheFile)
215 std::string id = GetSingleValue(PrepareSQL("select id from texture where url='%s'", url.c_str()));
216 return !id.empty() ? ClearCachedTexture(strtol(id.c_str(), NULL, 10), cacheFile) : false;
219 bool CTextureDatabase::ClearCachedTexture(int id, CStdString &cacheFile)
223 if (NULL == m_pDB.get()) return false;
224 if (NULL == m_pDS.get()) return false;
226 CStdString sql = PrepareSQL("select cachedurl from texture where id=%u", id);
227 m_pDS->query(sql.c_str());
230 { // have some information
231 cacheFile = m_pDS->fv(0).get_asString();
234 sql = PrepareSQL("delete from texture where id=%u", id);
235 m_pDS->exec(sql.c_str());
242 CLog::Log(LOGERROR, "%s, failed on texture id %u", __FUNCTION__, id);
247 bool CTextureDatabase::InvalidateCachedTexture(const CStdString &url)
249 CStdString date = (CDateTime::GetCurrentDateTime() - CDateTimeSpan(2, 0, 0, 0)).GetAsDBDateTime();
250 CStdString sql = PrepareSQL("UPDATE texture SET lasthashcheck='%s' WHERE url='%s'", date.c_str(), url.c_str());
251 return ExecuteQuery(sql);
254 CStdString CTextureDatabase::GetTextureForPath(const CStdString &url, const CStdString &type)
258 if (NULL == m_pDB.get()) return "";
259 if (NULL == m_pDS.get()) return "";
264 CStdString sql = PrepareSQL("select texture from path where url='%s' and type='%s'", url.c_str(), type.c_str());
265 m_pDS->query(sql.c_str());
268 { // have some information
269 CStdString texture = m_pDS->fv(0).get_asString();
277 CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
282 void CTextureDatabase::SetTextureForPath(const CStdString &url, const CStdString &type, const CStdString &texture)
286 if (NULL == m_pDB.get()) return;
287 if (NULL == m_pDS.get()) return;
292 CStdString sql = PrepareSQL("select id from path where url='%s' and type='%s'", url.c_str(), type.c_str());
293 m_pDS->query(sql.c_str());
296 int pathID = m_pDS->fv(0).get_asInt();
298 sql = PrepareSQL("update path set texture='%s' where id=%u", texture.c_str(), pathID);
299 m_pDS->exec(sql.c_str());
304 sql = PrepareSQL("insert into path (id, url, type, texture) values(NULL, '%s', '%s', '%s')", url.c_str(), type.c_str(), texture.c_str());
305 m_pDS->exec(sql.c_str());
310 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
315 void CTextureDatabase::ClearTextureForPath(const CStdString &url, const CStdString &type)
319 if (NULL == m_pDB.get()) return;
320 if (NULL == m_pDS.get()) return;
322 CStdString sql = PrepareSQL("DELETE FROM path WHERE url='%s' and type='%s'", url.c_str(), type.c_str());
323 m_pDS->exec(sql.c_str());
327 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());