/
TextureDatabase.cpp
306 lines (278 loc) · 10.1 KB
/
TextureDatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (C) 2005-2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "TextureDatabase.h"
#include "utils/log.h"
#include "XBDateTime.h"
#include "dbwrappers/dataset.h"
#include "URL.h"
CTextureDatabase::CTextureDatabase()
{
}
CTextureDatabase::~CTextureDatabase()
{
}
bool CTextureDatabase::Open()
{
return CDatabase::Open();
}
bool CTextureDatabase::CreateTables()
{
try
{
CDatabase::CreateTables();
CLog::Log(LOGINFO, "create texture table");
m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, usecount integer, lastusetime text, imagehash text, lasthashcheck text)\n");
CLog::Log(LOGINFO, "create textures index");
m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
CLog::Log(LOGINFO, "create path table");
m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
// TODO: Should the path index be a covering index? (we need only retrieve texture)
CLog::Log(LOGINFO, "create path index");
m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
}
catch (...)
{
CLog::Log(LOGERROR, "%s unable to create tables", __FUNCTION__);
return false;
}
return true;
}
bool CTextureDatabase::UpdateOldVersion(int version)
{
BeginTransaction();
try
{
if (version < 7)
{ // update all old thumb://foo urls to image://foo?size=thumb
m_pDS->query("select id,texture from path where texture like 'thumb://%'");
while (!m_pDS->eof())
{
unsigned int id = m_pDS->fv(0).get_asInt();
CURL url(m_pDS->fv(1).get_asString());
m_pDS2->exec(PrepareSQL("update path set texture='image://%s?size=thumb' where id=%u", url.GetHostName().c_str(), id));
m_pDS->next();
}
m_pDS->query("select id, url from texture where url like 'thumb://%'");
while (!m_pDS->eof())
{
unsigned int id = m_pDS->fv(0).get_asInt();
CURL url(m_pDS->fv(1).get_asString());
m_pDS2->exec(PrepareSQL("update texture set url='image://%s?size=thumb', urlhash=0 where id=%u", url.GetHostName().c_str(), id));
m_pDS->next();
}
m_pDS->close();
}
if (version < 8)
{ // get rid of old cached thumbs as they were previously set to the cached thumb name instead of the source thumb
m_pDS->exec("delete from path");
}
if (version < 9)
{ // get rid of the old path table and add the type column
m_pDS->dropIndex("path", "idxPath");
m_pDS->exec("DROP TABLE path");
m_pDS->exec("CREATE TABLE path (id integer primary key, urlhash integer, url text, type text, texture text)\n");
m_pDS->exec("CREATE INDEX idxPath ON path(urlhash, type)");
}
if (version < 10)
{ // get rid of urlhash in both tables...
m_pDS->dropIndex("path", "idxPath");
m_pDS->exec("DROP TABLE path");
m_pDS->exec("CREATE TABLE path (id integer primary key, url text, type text, texture text)\n");
m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
m_pDS->dropIndex("texture", "idxTexture");
m_pDS->exec("CREATE TEMPORARY TABLE texture_backup(id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck)");
m_pDS->exec("INSERT INTO texture_backup SELECT id,url,cachedurl,usecount,lastusetime,imagehash,lasthashcheck FROM texture");
m_pDS->exec("DROP TABLE texture");
m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, usecount integer, lastusetime text, imagehash text, lasthashcheck text)");
m_pDS->exec("CREATE INDEX idxTexture ON texture(url)");
m_pDS->exec("INSERT INTO texture SELECT * FROM texture_backup");
m_pDS->exec("DROP TABLE texture_backup");
}
if (version < 11)
{ // get rid of cached URLs that don't have the correct extension
m_pDS->exec("DELETE FROM texture WHERE SUBSTR(cachedUrl,-4,4) NOT IN ('.jpg', '.png')");
}
}
catch (...)
{
CLog::Log(LOGERROR, "%s(%d) failed", __FUNCTION__, version);
RollbackTransaction();
return false;
}
CommitTransaction();
return true;
}
bool CTextureDatabase::GetCachedTexture(const CStdString &url, CStdString &cacheFile, CStdString &imageHash)
{
try
{
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;
CStdString sql = PrepareSQL("select id, cachedurl, lasthashcheck, imagehash from texture where url='%s'", url.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // have some information
int textureID = m_pDS->fv(0).get_asInt();
cacheFile = m_pDS->fv(1).get_asString();
CDateTime lastCheck;
lastCheck.SetFromDBDateTime(m_pDS->fv(2).get_asString());
if (!lastCheck.IsValid() || lastCheck + CDateTimeSpan(1,0,0,0) < CDateTime::GetCurrentDateTime())
imageHash = m_pDS->fv(3).get_asString();
m_pDS->close();
// update the use count
sql = PrepareSQL("update texture set usecount=usecount+1, lastusetime=CURRENT_TIMESTAMP where id=%u", textureID);
m_pDS->exec(sql.c_str());
return true;
}
m_pDS->close();
}
catch (...)
{
CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
}
return false;
}
bool CTextureDatabase::AddCachedTexture(const CStdString &url, const CStdString &cacheFile, const CStdString &imageHash)
{
try
{
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;
CStdString cacheURL(cacheFile);
CStdString date = CDateTime::GetCurrentDateTime().GetAsDBDateTime();
CStdString sql = PrepareSQL("select id,cachedurl from texture where url='%s'", url.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // update
int textureID = m_pDS->fv(0).get_asInt();
if (cacheURL.IsEmpty())
cacheURL = m_pDS->fv(1).get_asString();
m_pDS->close();
if (!imageHash.IsEmpty())
sql = PrepareSQL("update texture set cachedurl='%s', usecount=1, lastusetime=CURRENT_TIMESTAMP, imagehash='%s', lasthashcheck='%s' where id=%u", cacheURL.c_str(), imageHash.c_str(), date.c_str(), textureID);
else
sql = PrepareSQL("update texture set cachedurl='%s', usecount=1, lastusetime=CURRENT_TIMESTAMP where id=%u", cacheURL.c_str(), textureID);
m_pDS->exec(sql.c_str());
}
else if (!cacheURL.IsEmpty())
{ // add the texture
m_pDS->close();
sql = PrepareSQL("insert into texture (id, url, cachedurl, usecount, lastusetime, imagehash, lasthashcheck) values(NULL, '%s', '%s', 1, CURRENT_TIMESTAMP, '%s', '%s')", url.c_str(), cacheURL.c_str(), imageHash.c_str(), date.c_str());
m_pDS->exec(sql.c_str());
}
}
catch (...)
{
CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
}
return true;
}
bool CTextureDatabase::ClearCachedTexture(const CStdString &url, CStdString &cacheFile)
{
try
{
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;
CStdString sql = PrepareSQL("select id, cachedurl from texture where url='%s'", url.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // have some information
int textureID = m_pDS->fv(0).get_asInt();
cacheFile = m_pDS->fv(1).get_asString();
m_pDS->close();
// remove it
sql = PrepareSQL("delete from texture where id=%u", textureID);
m_pDS->exec(sql.c_str());
return true;
}
m_pDS->close();
}
catch (...)
{
CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
}
return false;
}
CStdString CTextureDatabase::GetTextureForPath(const CStdString &url, const CStdString &type)
{
try
{
if (NULL == m_pDB.get()) return "";
if (NULL == m_pDS.get()) return "";
CStdString sql = PrepareSQL("select texture from path where url='%s' and type='%s'", url.c_str(), type.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // have some information
CStdString texture = m_pDS->fv(0).get_asString();
m_pDS->close();
return texture;
}
m_pDS->close();
}
catch (...)
{
CLog::Log(LOGERROR, "%s, failed on url '%s'", __FUNCTION__, url.c_str());
}
return "";
}
void CTextureDatabase::SetTextureForPath(const CStdString &url, const CStdString &type, const CStdString &texture)
{
try
{
if (NULL == m_pDB.get()) return;
if (NULL == m_pDS.get()) return;
CStdString sql = PrepareSQL("select id from path where url='%s' and type='%s'", url.c_str(), type.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // update
int pathID = m_pDS->fv(0).get_asInt();
m_pDS->close();
sql = PrepareSQL("update path set texture='%s' where id=%u", texture.c_str(), pathID);
m_pDS->exec(sql.c_str());
}
else
{ // add the texture
m_pDS->close();
sql = PrepareSQL("insert into path (id, url, type, texture) values(NULL, '%s', '%s', '%s')", url.c_str(), type.c_str(), texture.c_str());
m_pDS->exec(sql.c_str());
}
}
catch (...)
{
CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
}
return;
}
void CTextureDatabase::ClearTextureForPath(const CStdString &url, const CStdString &type)
{
try
{
if (NULL == m_pDB.get()) return;
if (NULL == m_pDS.get()) return;
CStdString sql = PrepareSQL("DELETE FROM path WHERE url='%s' and type='%s'", url.c_str(), type.c_str());
m_pDS->exec(sql.c_str());
}
catch (...)
{
CLog::Log(LOGERROR, "%s failed on url '%s'", __FUNCTION__, url.c_str());
}
return;
}