32 qlonglong PureImageCache::ConnCounter=0;
44 if(!d.exists(gtilecache))
47 #ifdef DEBUG_PUREIMAGECACHE
48 qDebug()<<
"Create Cache directory";
49 #endif //DEBUG_PUREIMAGECACHE
52 QString db=gtilecache+
"Data.qmdb";
53 if(!QFileInfo(db).exists())
55 #ifdef DEBUG_PUREIMAGECACHE
56 qDebug()<<
"Try to create EmptyDB";
57 #endif //DEBUG_PUREIMAGECACHE
72 #ifdef DEBUG_PUREIMAGECACHE
73 qDebug()<<
"Create database at!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:"<<
file;
74 #endif //DEBUG_PUREIMAGECACHE
76 QDir dir=File.absoluteDir();
77 QString path=dir.absolutePath();
78 QString filename=File.fileName();
80 QFile(filename).remove();
83 #ifdef DEBUG_PUREIMAGECACHE
84 qDebug()<<
"CreateEmptyDB: Cache path doesn't exist, try to create";
85 #endif //DEBUG_PUREIMAGECACHE
88 #ifdef DEBUG_PUREIMAGECACHE
89 qDebug()<<
"CreateEmptyDB: Could not create path";
90 #endif //DEBUG_PUREIMAGECACHE
96 db = QSqlDatabase::addDatabase(
"QSQLITE",QLatin1String(
"CreateConn"));
97 db.setDatabaseName(file);
100 #ifdef DEBUG_PUREIMAGECACHE
101 qDebug()<<
"CreateEmptyDB: Unable to create database";
102 #endif //DEBUG_PUREIMAGECACHE
107 query.exec(
"CREATE TABLE IF NOT EXISTS Tiles (id INTEGER NOT NULL PRIMARY KEY, X INTEGER NOT NULL, Y INTEGER NOT NULL, Zoom INTEGER NOT NULL, Type INTEGER NOT NULL,Date TEXT)");
108 if(query.numRowsAffected()==-1)
110 #ifdef DEBUG_PUREIMAGECACHE
111 qDebug()<<
"CreateEmptyDB: "<<query.lastError().driverText();
112 #endif //DEBUG_PUREIMAGECACHE
116 query.exec(
"CREATE TABLE IF NOT EXISTS TilesData (id INTEGER NOT NULL PRIMARY KEY CONSTRAINT fk_Tiles_id REFERENCES Tiles(id) ON DELETE CASCADE, Tile BLOB NULL)");
117 if(query.numRowsAffected()==-1)
119 #ifdef DEBUG_PUREIMAGECACHE
120 qDebug()<<
"CreateEmptyDB: "<<query.lastError().driverText();
121 #endif //DEBUG_PUREIMAGECACHE
126 "CREATE TRIGGER fki_TilesData_id_Tiles_id "
127 "BEFORE INSERT ON [TilesData] "
128 "FOR EACH ROW BEGIN "
129 "SELECT RAISE(ROLLBACK, 'insert on table TilesData violates foreign key constraint fki_TilesData_id_Tiles_id') "
130 "WHERE (SELECT id FROM Tiles WHERE id = NEW.id) IS NULL; "
132 if(query.numRowsAffected()==-1)
134 #ifdef DEBUG_PUREIMAGECACHE
135 qDebug()<<
"CreateEmptyDB: "<<query.lastError().driverText();
136 #endif //DEBUG_PUREIMAGECACHE
141 "CREATE TRIGGER fku_TilesData_id_Tiles_id "
142 "BEFORE UPDATE ON [TilesData] "
143 "FOR EACH ROW BEGIN "
144 "SELECT RAISE(ROLLBACK, 'update on table TilesData violates foreign key constraint fku_TilesData_id_Tiles_id') "
145 "WHERE (SELECT id FROM Tiles WHERE id = NEW.id) IS NULL; "
147 if(query.numRowsAffected()==-1)
149 #ifdef DEBUG_PUREIMAGECACHE
150 qDebug()<<
"CreateEmptyDB: "<<query.lastError().driverText();
151 #endif //DEBUG_PUREIMAGECACHE
156 "CREATE TRIGGER fkdc_TilesData_id_Tiles_id "
157 "BEFORE DELETE ON Tiles "
158 "FOR EACH ROW BEGIN "
159 "DELETE FROM TilesData WHERE TilesData.id = OLD.id; "
161 if(query.numRowsAffected()==-1)
163 #ifdef DEBUG_PUREIMAGECACHE
164 qDebug()<<
"CreateEmptyDB: "<<query.lastError().driverText();
165 #endif //DEBUG_PUREIMAGECACHE
171 QSqlDatabase::removeDatabase(QLatin1String(
"CreateConn"));
176 if(gtilecache.isEmpty()|gtilecache.isNull())
179 #ifdef DEBUG_PUREIMAGECACHE
180 qDebug()<<
"PutImageToCache Start:";
181 #endif //DEBUG_PUREIMAGECACHE
183 qlonglong
id=++ConnCounter;
187 cn = QSqlDatabase::addDatabase(
"QSQLITE",QString::number(
id));
188 QString db=gtilecache+
"Data.qmdb";
189 cn.setDatabaseName(db);
190 cn.setConnectOptions(
"QSQLITE_ENABLE_SHARED_CACHE");
195 query.prepare(
"INSERT INTO Tiles(X, Y, Zoom, Type,Date) VALUES(?, ?, ?, ?,?)");
196 query.addBindValue(pos.
X());
197 query.addBindValue(pos.
Y());
198 query.addBindValue(zoom);
200 query.addBindValue((
int)type);
201 query.addBindValue(QDateTime::currentDateTime().toString());
206 query.prepare(
"INSERT INTO TilesData(id, Tile) VALUES((SELECT last_insert_rowid()), ?)");
207 query.addBindValue(tile);
213 QSqlDatabase::removeDatabase(QString::number(
id));
221 if(gtilecache.isEmpty()|gtilecache.isNull())
223 QString dir=gtilecache;
225 qlonglong
id=++ConnCounter;
227 #ifdef DEBUG_PUREIMAGECACHE
228 qDebug()<<
"Cache dir="<<dir<<
" Try to GET:"<<pos.
X()+
","+pos.
Y();
229 #endif //DEBUG_PUREIMAGECACHE
232 QString db=dir+
"Data.qmdb";
236 cn = QSqlDatabase::addDatabase(
"QSQLITE",QString::number(
id));
238 cn.setDatabaseName(db);
239 cn.setConnectOptions(
"QSQLITE_ENABLE_SHARED_CACHE");
244 query.exec(QString(
"SELECT Tile FROM TilesData WHERE id = (SELECT id FROM Tiles WHERE X=%1 AND Y=%2 AND Zoom=%3 AND Type=%4)").arg(pos.
X()).arg(pos.
Y()).arg(zoom).arg((
int) type));
248 ar=query.value(0).toByteArray();
255 QSqlDatabase::removeDatabase(QString::number(
id));
261 if(gtilecache.isEmpty()|gtilecache.isNull())
265 QString dir=gtilecache;
267 QString db=dir+
"Data.qmdb";
268 ret=QFileInfo(db).exists();
273 qlonglong
id=++ConnCounter;
275 cn = QSqlDatabase::addDatabase(
"QSQLITE",QString::number(
id));
276 cn.setDatabaseName(db);
277 cn.setConnectOptions(
"QSQLITE_ENABLE_SHARED_CACHE");
282 query.exec(QString(
"SELECT id, X, Y, Zoom, Type, Date FROM Tiles"));
285 if(QDateTime::fromString(query.value(5).toString()).daysTo(QDateTime::currentDateTime())>days)
286 add.append(query.value(0).toLongLong());
290 query.exec(QString(
"DELETE FROM Tiles WHERE id = %1;").arg(i));
296 QSqlDatabase::removeDatabase(QString::number(
id));
305 if(!QFileInfo(destFile).exists())
307 #ifdef DEBUG_PUREIMAGECACHE
308 qDebug()<<
"Try to create EmptyDB";
309 #endif //DEBUG_PUREIMAGECACHE
312 if(!ret)
return false;
313 QSqlDatabase ca = QSqlDatabase::addDatabase(
"QSQLITE",
"ca");
314 ca.setDatabaseName(sourceFile);
318 QSqlDatabase cb = QSqlDatabase::addDatabase(
"QSQLITE",
"cb");
319 cb.setDatabaseName(destFile);
322 QSqlQuery queryb(cb);
323 queryb.exec(QString(
"ATTACH DATABASE \"%1\" AS Source").arg(sourceFile));
324 QSqlQuery querya(ca);
325 querya.exec(
"SELECT id, X, Y, Zoom, Type, Date FROM Tiles");
328 long id=querya.value(0).toLongLong();
329 queryb.exec(QString(
"SELECT id FROM Tiles WHERE X=%1 AND Y=%2 AND Zoom=%3 AND Type=%4;").arg(querya.value(1).toLongLong()).arg(querya.value(2).toLongLong()).arg(querya.value(3).toLongLong()).arg(querya.value(4).toLongLong()));
339 queryb.exec(QString(
"INSERT INTO Tiles(X, Y, Zoom, Type, Date) SELECT X, Y, Zoom, Type, Date FROM Source.Tiles WHERE id=%1").arg(f));
340 queryb.exec(QString(
"INSERT INTO TilesData(id, Tile) Values((SELECT last_insert_rowid()), (SELECT Tile FROM Source.TilesData WHERE id=%1))").arg(f));
350 QSqlDatabase::removeDatabase(
"ca");
351 QSqlDatabase::removeDatabase(
"cb");
T * query(Aggregate *obj)