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"
26 #include "utils/StringUtils.h"
27 #include "utils/Variant.h"
48 CDatabaseQueryRule::FIELD_TYPE type;
53 static const translateField fields[] = {
54 { "none", TF_None, CDatabaseQueryRule::TEXT_FIELD },
55 { "textureid", TF_Id, CDatabaseQueryRule::NUMERIC_FIELD },
56 { "url", TF_Url, CDatabaseQueryRule::TEXT_FIELD },
57 { "cachedurl", TF_CachedUrl, CDatabaseQueryRule::TEXT_FIELD },
58 { "lasthashcheck", TF_LastHashCheck, CDatabaseQueryRule::TEXT_FIELD },
59 { "imagehash", TF_ImageHash, CDatabaseQueryRule::TEXT_FIELD },
60 { "width", TF_Width, CDatabaseQueryRule::NUMERIC_FIELD },
61 { "height", TF_Height, CDatabaseQueryRule::NUMERIC_FIELD },
62 { "usecount", TF_UseCount, CDatabaseQueryRule::NUMERIC_FIELD },
63 { "lastused", TF_LastUsed, CDatabaseQueryRule::TEXT_FIELD }
66 static const size_t NUM_FIELDS = sizeof(fields) / sizeof(translateField);
68 int CTextureRule::TranslateField(const char *field) const
70 for (unsigned int i = 0; i < NUM_FIELDS; i++)
71 if (StringUtils::EqualsNoCase(field, fields[i].string)) return fields[i].field;
75 CStdString CTextureRule::TranslateField(int field) const
77 for (unsigned int i = 0; i < NUM_FIELDS; i++)
78 if (field == fields[i].field) return fields[i].string;
82 CStdString CTextureRule::GetField(int field, const CStdString &type) const
84 if (field == TF_Id) return "texture.id";
85 else if (field == TF_Url) return "texture.url";
86 else if (field == TF_CachedUrl) return "texture.cachedurl";
87 else if (field == TF_LastHashCheck) return "texture.lasthashcheck";
88 else if (field == TF_ImageHash) return "texture.imagehash";
89 else if (field == TF_Width) return "sizes.width";
90 else if (field == TF_Height) return "sizes.height";
91 else if (field == TF_UseCount) return "sizes.usecount";
92 else if (field == TF_LastUsed) return "sizes.lastusetime";
96 CDatabaseQueryRule::FIELD_TYPE CTextureRule::GetFieldType(int field) const
98 for (unsigned int i = 0; i < NUM_FIELDS; i++)
99 if (field == fields[i].field) return fields[i].type;
103 CStdString CTextureRule::FormatParameter(const CStdString &operatorString, const CStdString ¶m, const CDatabase &db, const CStdString &strType) const
105 CStdString parameter(param);
106 if (m_field == TF_Url)
107 parameter = CTextureUtils::UnwrapImageURL(param);
108 return CDatabaseQueryRule::FormatParameter(operatorString, parameter, db, strType);
111 void CTextureRule::GetAvailableFields(std::vector<std::string> &fieldList)
113 // start at 1 to skip TF_None
114 for (unsigned int i = 1; i < NUM_FIELDS; i++)
115 fieldList.push_back(fields[i].string);
118 CStdString CTextureUtils::GetWrappedImageURL(const CStdString &image, const CStdString &type, const CStdString &options)
120 if (StringUtils::StartsWith(image, "image://"))
121 return image; // already wrapped
124 url.SetProtocol("image");
125 url.SetUserName(type);
126 url.SetHostName(image);
127 if (!options.empty())
129 url.SetFileName("transform");
130 url.SetOptions("?" + options);
135 CStdString CTextureUtils::GetWrappedThumbURL(const CStdString &image)
137 return GetWrappedImageURL(image, "", "size=thumb");
140 CStdString CTextureUtils::UnwrapImageURL(const CStdString &image)
142 if (StringUtils::StartsWith(image, "image://"))
145 if (url.GetUserName().empty() && url.GetOptions().empty())
146 return url.GetHostName();
151 CTextureDatabase::CTextureDatabase()
155 CTextureDatabase::~CTextureDatabase()
159 bool CTextureDatabase::Open()
161 return CDatabase::Open();
164 void CTextureDatabase::CreateTables()
166 CLog::Log(LOGINFO, "create texture table");
167 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, imagehash text, lasthashcheck text)");
169 CLog::Log(LOGINFO, "create sizes table, index, and trigger");
170 m_pDS->exec("CREATE TABLE sizes (idtexture integer, size integer, width integer, height integer, usecount integer, lastusetime text)");
172 CLog::Log(LOGINFO, "create path table");
173 m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
176 void CTextureDatabase::CreateAnalytics()
178 CLog::Log(LOGINFO, "%s creating indices", __FUNCTION__);
179 m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
180 m_pDS->exec("CREATE INDEX idxSize ON sizes(idtexture, size)");
181 m_pDS->exec("CREATE INDEX idxSize2 ON sizes(idtexture, width, height)");
182 // TODO: Should the path index be a covering index? (we need only retrieve texture)
183 m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
185 CLog::Log(LOGINFO, "%s creating triggers", __FUNCTION__);
186 m_pDS->exec("CREATE TRIGGER textureDelete AFTER delete ON texture FOR EACH ROW BEGIN delete from sizes where sizes.idtexture=old.id; END");
189 void CTextureDatabase::UpdateTables(int version)
192 { // update all old thumb://foo urls to image://foo?size=thumb
193 m_pDS->query("select id,texture from path where texture like 'thumb://%'");
194 while (!m_pDS->eof())
196 unsigned int id = m_pDS->fv(0).get_asInt();
197 CURL url(m_pDS->fv(1).get_asString());
198 m_pDS2->exec(PrepareSQL("update path set texture='image://%s?size=thumb' where id=%u", url.GetHostName().c_str(), id));
201 m_pDS->query("select id, url from texture where url like 'thumb://%'");
202 while (!m_pDS->eof())
204 unsigned int id = m_pDS->fv(0).get_asInt();
205 CURL url(m_pDS->fv(1).get_asString());
206 m_pDS2->exec(PrepareSQL("update texture set url='image://%s?size=thumb', urlhash=0 where id=%u", url.GetHostName().c_str(), id));
212 { // get rid of old cached thumbs as they were previously set to the cached thumb name instead of the source thumb
213 m_pDS->exec("delete from path");
216 { // get rid of the old path table and add the type column
217 m_pDS->exec("DROP TABLE IF EXISTS path");
218 m_pDS->exec("CREATE TABLE path (id integer primary key, urlhash integer, url text, type text, texture text)\n");
221 { // get rid of urlhash in both tables...
222 m_pDS->exec("DROP TABLE IF EXISTS path");
223 m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
225 m_pDS->exec("CREATE TEMPORARY TABLE texture_backup(id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck)");
226 m_pDS->exec("INSERT INTO texture_backup SELECT id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck FROM texture");
227 m_pDS->exec("DROP TABLE texture");
228 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, usecount integer, lastusetime text, imagehash text, lasthashcheck text)");
229 m_pDS->exec("INSERT INTO texture SELECT * FROM texture_backup");
230 m_pDS->exec("DROP TABLE texture_backup");
233 { // get rid of cached URLs that don't have the correct extension
234 m_pDS->exec("DELETE FROM texture WHERE SUBSTR(cachedUrl,-4,4) NOT IN ('.jpg', '.png')");
237 { // create new sizes table and move usecount info to it.
238 m_pDS->exec("DROP TABLE IF EXISTS texture");
239 m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, imagehash text, lasthashcheck text)");
240 m_pDS->exec("CREATE TABLE sizes (idtexture integer, size integer, width integer, height integer, usecount integer, lastusetime text)");
244 bool CTextureDatabase::IncrementUseCount(const CTextureDetails &details)
246 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);
247 return ExecuteQuery(sql);
250 bool CTextureDatabase::GetCachedTexture(const CStdString &url, CTextureDetails &details)
254 if (NULL == m_pDB.get()) return false;
255 if (NULL == m_pDS.get()) return false;
257 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());
258 m_pDS->query(sql.c_str());
260 { // have some information
261 details.id = m_pDS->fv(0).get_asInt();
262 details.file = m_pDS->fv(1).get_asString();
264 lastCheck.SetFromDBDateTime(m_pDS->fv(2).get_asString());
265 if (lastCheck.IsValid() && lastCheck + CDateTimeSpan(1,0,0,0) < CDateTime::GetCurrentDateTime())
266 details.hash = m_pDS->fv(3).get_asString();
267 details.width = m_pDS->fv(4).get_asInt();
268 details.height = m_pDS->fv(5).get_asInt();
276 CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
281 bool CTextureDatabase::GetTextures(CVariant &items, const Filter &filter)
285 if (NULL == m_pDB.get()) return false;
286 if (NULL == m_pDS.get()) return false;
288 CStdString sql = "SELECT %s FROM texture JOIN sizes ON (texture.id=sizes.idtexture AND sizes.size=1)";
289 CStdString sqlFilter;
290 if (!CDatabase::BuildSQL("", filter, sqlFilter))
293 sql = PrepareSQL(sql, !filter.fields.empty() ? filter.fields.c_str() : "*") + sqlFilter;
294 if (!m_pDS->query(sql.c_str()))
297 while (!m_pDS->eof())
300 texture["textureid"] = m_pDS->fv(0).get_asInt();
301 texture["url"] = m_pDS->fv(1).get_asString();
302 texture["cachedurl"] = m_pDS->fv(2).get_asString();
303 texture["imagehash"] = m_pDS->fv(3).get_asString();
304 texture["lasthashcheck"] = m_pDS->fv(4).get_asString();
305 CVariant size(CVariant::VariantTypeObject);
306 // 5 is sizes.idtexture
307 size["size"] = m_pDS->fv(6).get_asInt();
308 size["width"] = m_pDS->fv(7).get_asInt();
309 size["height"] = m_pDS->fv(8).get_asInt();
310 size["usecount"] = m_pDS->fv(9).get_asInt();
311 size["lastused"] = m_pDS->fv(10).get_asString();
312 texture["sizes"] = CVariant(CVariant::VariantTypeArray);
313 texture["sizes"].push_back(size);
314 items.push_back(texture);
322 CLog::Log(LOGERROR, "%s, failed", __FUNCTION__);
327 bool CTextureDatabase::SetCachedTextureValid(const CStdString &url, bool updateable)
329 CStdString date = updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
330 CStdString sql = PrepareSQL("UPDATE texture SET lasthashcheck='%s' WHERE url='%s'", date.c_str(), url.c_str());
331 return ExecuteQuery(sql);
334 bool CTextureDatabase::AddCachedTexture(const CStdString &url, const CTextureDetails &details)
338 if (NULL == m_pDB.get()) return false;
339 if (NULL == m_pDS.get()) return false;
341 CStdString sql = PrepareSQL("DELETE FROM texture WHERE url='%s'", url.c_str());
342 m_pDS->exec(sql.c_str());
344 CStdString date = details.updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
345 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());
346 m_pDS->exec(sql.c_str());
347 int textureID = (int)m_pDS->lastinsertid();
349 // set the size information
350 sql = PrepareSQL("INSERT INTO sizes (idtexture, size, usecount, lastusetime, width, height) VALUES(%u, 1, 1, CURRENT_TIMESTAMP, %u, %u)", textureID, details.width, details.height);
351 m_pDS->exec(sql.c_str());
355 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
360 bool CTextureDatabase::ClearCachedTexture(const CStdString &url, CStdString &cacheFile)
362 std::string id = GetSingleValue(PrepareSQL("select id from texture where url='%s'", url.c_str()));
363 return !id.empty() ? ClearCachedTexture(strtol(id.c_str(), NULL, 10), cacheFile) : false;
366 bool CTextureDatabase::ClearCachedTexture(int id, CStdString &cacheFile)
370 if (NULL == m_pDB.get()) return false;
371 if (NULL == m_pDS.get()) return false;
373 CStdString sql = PrepareSQL("select cachedurl from texture where id=%u", id);
374 m_pDS->query(sql.c_str());
377 { // have some information
378 cacheFile = m_pDS->fv(0).get_asString();
381 sql = PrepareSQL("delete from texture where id=%u", id);
382 m_pDS->exec(sql.c_str());
389 CLog::Log(LOGERROR, "%s, failed on texture id %u", __FUNCTION__, id);
394 bool CTextureDatabase::InvalidateCachedTexture(const CStdString &url)
396 CStdString date = (CDateTime::GetCurrentDateTime() - CDateTimeSpan(2, 0, 0, 0)).GetAsDBDateTime();
397 CStdString sql = PrepareSQL("UPDATE texture SET lasthashcheck='%s' WHERE url='%s'", date.c_str(), url.c_str());
398 return ExecuteQuery(sql);
401 CStdString CTextureDatabase::GetTextureForPath(const CStdString &url, const CStdString &type)
405 if (NULL == m_pDB.get()) return "";
406 if (NULL == m_pDS.get()) return "";
411 CStdString sql = PrepareSQL("select texture from path where url='%s' and type='%s'", url.c_str(), type.c_str());
412 m_pDS->query(sql.c_str());
415 { // have some information
416 CStdString texture = m_pDS->fv(0).get_asString();
424 CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
429 void CTextureDatabase::SetTextureForPath(const CStdString &url, const CStdString &type, const CStdString &texture)
433 if (NULL == m_pDB.get()) return;
434 if (NULL == m_pDS.get()) return;
439 CStdString sql = PrepareSQL("select id from path where url='%s' and type='%s'", url.c_str(), type.c_str());
440 m_pDS->query(sql.c_str());
443 int pathID = m_pDS->fv(0).get_asInt();
445 sql = PrepareSQL("update path set texture='%s' where id=%u", texture.c_str(), pathID);
446 m_pDS->exec(sql.c_str());
451 sql = PrepareSQL("insert into path (id, url, type, texture) values(NULL, '%s', '%s', '%s')", url.c_str(), type.c_str(), texture.c_str());
452 m_pDS->exec(sql.c_str());
457 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
462 void CTextureDatabase::ClearTextureForPath(const CStdString &url, const CStdString &type)
466 if (NULL == m_pDB.get()) return;
467 if (NULL == m_pDS.get()) return;
469 CStdString sql = PrepareSQL("DELETE FROM path WHERE url='%s' and type='%s'", url.c_str(), type.c_str());
470 m_pDS->exec(sql.c_str());
474 CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
479 CDatabaseQueryRule *CTextureDatabase::CreateRule() const
481 return new CTextureRule();
484 CDatabaseQueryRuleCombination *CTextureDatabase::CreateCombination() const
486 return new CDatabaseQueryRuleCombination();