/** ****************************************************************************** * * @file pureimagecache.cpp * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief * @see The GNU Public License (GPL) Version 3 * @defgroup OPMapWidget * @{ * *****************************************************************************/ /* * 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 3 of the License, 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 this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "pureimagecache.h" #include #include //#define DEBUG_PUREIMAGECACHE namespace core { static QMutex addDatabaseMutex; // QSqlDatabase::addDatabase is not thread safe when loadingn plugins qlonglong PureImageCache::ConnCounter=0; PureImageCache::PureImageCache() { } void PureImageCache::setGtileCache(const QString &value) { lock.lockForWrite(); gtilecache=value; QDir d; if(!d.exists(gtilecache)) { d.mkdir(gtilecache); #ifdef DEBUG_PUREIMAGECACHE qDebug()<<"Create Cache directory"; #endif //DEBUG_PUREIMAGECACHE } { QString db=gtilecache+"Data.qmdb"; if(!QFileInfo(db).exists()) { #ifdef DEBUG_PUREIMAGECACHE qDebug()<<"Try to create EmptyDB"; #endif //DEBUG_PUREIMAGECACHE CreateEmptyDB(db); } } lock.unlock(); } QString PureImageCache::GtileCache() { return gtilecache; } bool PureImageCache::CreateEmptyDB(const QString &file) { #ifdef DEBUG_PUREIMAGECACHE qDebug()<<"Create database at!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:"< add; bool ret=true; QString dir=gtilecache; { QString db=dir+"Data.qmdb"; ret=QFileInfo(db).exists(); if(ret) { Mcounter.lock(); qlonglong id=++ConnCounter; Mcounter.unlock(); addDatabaseMutex.lock(); QSqlDatabase cn(QSqlDatabase::addDatabase("QSQLITE",QString::number(id))); addDatabaseMutex.unlock(); cn.setDatabaseName(db); cn.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE"); if(cn.open()) { { QSqlQuery query(cn); query.exec(QString("SELECT id, X, Y, Zoom, Type, Date FROM Tiles")); while(query.next()) { if(QDateTime::fromString(query.value(5).toString()).daysTo(QDateTime::currentDateTime())>days) add.append(query.value(0).toLongLong()); } foreach(long i,add) { query.exec(QString("DELETE FROM Tiles WHERE id = %1;").arg(i)); } } cn.close(); } QSqlDatabase::removeDatabase(QString::number(id)); } } } // PureImageCache::ExportMapDataToDB("C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data.qmdb","C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data2.qmdb"); bool PureImageCache::ExportMapDataToDB(QString sourceFile, QString destFile) { bool ret=true; QList add; if(!QFileInfo(destFile).exists()) { #ifdef DEBUG_PUREIMAGECACHE qDebug()<<"Try to create EmptyDB"; #endif //DEBUG_PUREIMAGECACHE ret=CreateEmptyDB(destFile); } if(!ret) return false; addDatabaseMutex.lock(); QSqlDatabase ca(QSqlDatabase::addDatabase("QSQLITE","ca")); addDatabaseMutex.unlock(); ca.setDatabaseName(sourceFile); if(ca.open()) { addDatabaseMutex.lock(); QSqlDatabase cb(QSqlDatabase::addDatabase("QSQLITE","cb")); addDatabaseMutex.unlock(); cb.setDatabaseName(destFile); if(cb.open()) { QSqlQuery queryb(cb); queryb.exec(QString("ATTACH DATABASE \"%1\" AS Source").arg(sourceFile)); QSqlQuery querya(ca); querya.exec("SELECT id, X, Y, Zoom, Type, Date FROM Tiles"); while(querya.next()) { long id=querya.value(0).toLongLong(); 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())); if(!queryb.next()) { add.append(id); } } long f; foreach(f,add) { 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)); queryb.exec(QString("INSERT INTO TilesData(id, Tile) Values((SELECT last_insert_rowid()), (SELECT Tile FROM Source.TilesData WHERE id=%1))").arg(f)); } add.clear(); ca.close(); cb.close(); } else return false; } else return false; QSqlDatabase::removeDatabase("ca"); QSqlDatabase::removeDatabase("cb"); return true; } }