/
TextureDatabase.cpp
239 lines (209 loc) · 6.98 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
/*
* 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 "utils/Crc32.h"
#include "XBDateTime.h"
#include "dbwrappers/dataset.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, urlhash integer, 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(urlhash)");
CLog::Log(LOGINFO, "create path table");
m_pDS->exec("CREATE TABLE path (id integer primary key, urlhash integer, url text, texture text)\n");
CLog::Log(LOGINFO, "create path index");
m_pDS->exec("CREATE INDEX idxPath ON path(urlhash)");
}
catch (...)
{
CLog::Log(LOGERROR, "%s unable to create tables", __FUNCTION__);
return false;
}
return true;
}
bool CTextureDatabase::UpdateOldVersion(int version)
{
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;
unsigned int hash = GetURLHash(url);
CStdString sql = PrepareSQL("select id, cachedurl, lasthashcheck, imagehash from texture where urlhash=%u", hash);
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;
unsigned int hash = GetURLHash(url);
CStdString date = CDateTime::GetCurrentDateTime().GetAsDBDateTime();
CStdString sql = PrepareSQL("select id from texture where urlhash=%u", hash);
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // update
int textureID = m_pDS->fv(0).get_asInt();
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", cacheFile.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", cacheFile.c_str(), textureID);
m_pDS->exec(sql.c_str());
}
else
{ // add the texture
m_pDS->close();
sql = PrepareSQL("insert into texture (id, urlhash, url, cachedurl, usecount, lastusetime, imagehash, lasthashcheck) values(NULL, %u, '%s', '%s', 1, CURRENT_TIMESTAMP, '%s', '%s')", hash, url.c_str(), cacheFile.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;
unsigned int hash = GetURLHash(url);
CStdString sql = PrepareSQL("select id, cachedurl from texture where urlhash=%u", hash);
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;
}
unsigned int CTextureDatabase::GetURLHash(const CStdString &url) const
{
Crc32 crc;
crc.ComputeFromLowerCase(url);
return (unsigned int)crc;
}
CStdString CTextureDatabase::GetTextureForPath(const CStdString &url)
{
try
{
if (NULL == m_pDB.get()) return "";
if (NULL == m_pDS.get()) return "";
unsigned int hash = GetURLHash(url);
CStdString sql = PrepareSQL("select texture from path where urlhash=%u", hash);
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 &texture)
{
try
{
if (NULL == m_pDB.get()) return;
if (NULL == m_pDS.get()) return;
unsigned int hash = GetURLHash(url);
CStdString sql = PrepareSQL("select id from path where urlhash=%u", hash);
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, urlhash, url, texture) values(NULL, %u, '%s', '%s')", hash, url.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;
}