diff --git a/database.cpp b/database.cpp index 968a2e6..28529bf 100644 --- a/database.cpp +++ b/database.cpp @@ -22,16 +22,32 @@ bool Database::init() if(m_database.isValid()) { - m_database.setDatabaseName(dir.absoluteFilePath("database.db")); + m_database.setDatabaseName(dir.absoluteFilePath("database2.db")); if(m_database.open()) { m_database.exec("PRAGMA foreign_keys = ON"); - m_database.exec("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE)"); - m_database.exec("CREATE TABLE IF NOT EXISTS fits_files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE, mtime DATETIME)"); - m_database.exec("CREATE TABLE IF NOT EXISTS fits_headers (id INTEGER PRIMARY KEY AUTOINCREMENT, id_file INTEGER," - "key VARCHAR(81), value VARCHAR(81), comment VARCHAR(81), FOREIGN KEY(id_file) REFERENCES fits_files(id) ON DELETE CASCADE)"); - m_database.exec("CREATE INDEX IF NOT EXISTS key_value ON fits_headers(key, value)"); - m_database.exec("CREATE INDEX IF NOT EXISTS id_file ON fits_headers(id_file)"); + int version = checkVersion(); + if(version == 0) + { + m_database.exec("PRAGMA user_version = 1"); + m_database.exec("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE)"); + m_database.exec("CREATE TABLE IF NOT EXISTS fits_files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE, mtime DATETIME," + " minRa REAL, maxRa REAL, minDec REAL, maxDec REAL)"); + m_database.exec("CREATE TABLE IF NOT EXISTS fits_headers (id INTEGER PRIMARY KEY AUTOINCREMENT, id_file INTEGER," + "key VARCHAR(81), value VARCHAR(81), comment VARCHAR(81), FOREIGN KEY(id_file) REFERENCES fits_files(id) ON DELETE CASCADE)"); + m_database.exec("CREATE INDEX IF NOT EXISTS key_value ON fits_headers(key, value)"); + m_database.exec("CREATE INDEX IF NOT EXISTS id_file ON fits_headers(id_file)"); + m_database.exec("CREATE INDEX IF NOT EXISTS minRa_idx ON fits_files(minRa)"); + m_database.exec("CREATE INDEX IF NOT EXISTS maxRa_idx ON fits_files(maxRa)"); + m_database.exec("CREATE INDEX IF NOT EXISTS minDec_idx ON fits_files(minDec)"); + m_database.exec("CREATE INDEX IF NOT EXISTS maxDec_idx ON fits_files(maxDec)"); + } + else if(version > 1) + { + qDebug() << "Database version is too new"; + return false; + } + QSqlError error = m_database.lastError(); if(error.type() == QSqlError::NoError) @@ -45,6 +61,8 @@ bool Database::init() m_insertFile = QSqlQuery(m_database); m_insertFile.prepare("INSERT INTO fits_files (file, mtime) VALUES (?, ?)"); + m_insertFileWcs = QSqlQuery(m_database); + m_insertFileWcs.prepare("INSERT INTO fits_files (file, mtime, minRa, maxRa, minDec, maxDec) VALUES (?, ?, ?, ?, ?, ?)"); m_insertFitsHeader = QSqlQuery(m_database); m_insertFitsHeader.prepare("INSERT INTO fits_headers (id_file, key, value, comment) VALUES (?, ?, ?, ?)"); m_checkFile = QSqlQuery(m_database); @@ -55,7 +73,6 @@ bool Database::init() m_deleteFile.prepare("DELETE FROM fits_files WHERE id=?"); return true; } - qDebug() << error.text(); } } @@ -129,6 +146,15 @@ bool Database::checkError(QSqlQuery &query) } } +int Database::checkVersion() +{ + QSqlDatabase db = QSqlDatabase::database(); + QSqlQuery query = db.exec("PRAGMA user_version"); + if(query.next()) + return query.value(0).toInt(); + return -1; +} + static QStringList nameFilters = {"*.fit", "*.fits", "*.xisf"}; static int countFiles(const QDir &dir, int count = 0) @@ -242,14 +268,36 @@ bool Database::indexFile(const QFileInfo &file) qlonglong last_id = -1; if(ok) { - m_insertFile.bindValue(0, filePath); - m_insertFile.bindValue(1, mtime); - if(!m_insertFile.exec()) + if(info.wcs) { - qDebug() << m_insertFile.lastError(); - return false; + double minRa, maxRa, minDec, maxDec; + info.wcs->calculateBounds(minRa, maxRa, minDec, maxDec); + qDebug() << "bounds" << minRa << maxRa << minDec << maxDec; + m_insertFileWcs.bindValue(0, filePath); + m_insertFileWcs.bindValue(1, mtime); + m_insertFileWcs.bindValue(2, minRa); + m_insertFileWcs.bindValue(3, maxRa); + m_insertFileWcs.bindValue(4, minDec); + m_insertFileWcs.bindValue(5, maxDec); + if(!m_insertFileWcs.exec()) + { + qDebug() << m_insertFileWcs.lastError(); + return false; + } + last_id = m_insertFileWcs.lastInsertId().toLongLong(); } - last_id = m_insertFile.lastInsertId().toLongLong(); + else + { + m_insertFile.bindValue(0, filePath); + m_insertFile.bindValue(1, mtime); + if(!m_insertFile.exec()) + { + qDebug() << m_insertFile.lastError(); + return false; + } + last_id = m_insertFile.lastInsertId().toLongLong(); + } + QVariantList file_id, keys, values, comments; for(auto &record : info.fitsHeader) { diff --git a/database.h b/database.h index 3ea81e5..f85a53a 100644 --- a/database.h +++ b/database.h @@ -15,6 +15,7 @@ class Database : public QObject QSqlQuery m_isMarkedQuery; QSqlQuery m_insertFile; + QSqlQuery m_insertFileWcs; QSqlQuery m_insertFitsHeader; QSqlQuery m_checkFile; QSqlQuery m_headerKeywords; @@ -39,6 +40,7 @@ protected: bool indexDir2(const QDir &dir, QProgressDialog *progress); bool indexFile(const QFileInfo &file); bool checkError(QSqlQuery &query); + int checkVersion(); }; #endif // DATABASE_H diff --git a/imageinfo.cpp b/imageinfo.cpp index 5a3a159..e816eac 100644 --- a/imageinfo.cpp +++ b/imageinfo.cpp @@ -58,7 +58,9 @@ void WCSData::freeWCS() wcs = nullptr; } -WCSData::WCSData(char *header, int nrec) +WCSData::WCSData(int width, int height, char *header, int nrec) : + width(width), + height(height) { int stat[NWCSFIX]; int nreject = 0; @@ -117,7 +119,7 @@ bool WCSData::worldToPixel(const SkyPoint &point, QPointF &pixel) const return false; } -void WCSData::calculateBounds(int w, int h, double &minRa, double &maxRa, double &minDec, double &maxDec) +void WCSData::calculateBounds(double &minRa, double &maxRa, double &minDec, double &maxDec) const { if(wcs == nullptr)return; @@ -136,21 +138,21 @@ void WCSData::calculateBounds(int w, int h, double &minRa, double &maxRa, double maxDec = std::max(maxDec, point.DEC()); }; - for(int x=0; x(header, nrec); + info.wcs = std::make_shared(naxes[0], naxes[1], header, nrec); if(!info.wcs->valid())info.wcs.reset(); } fits_free_memory(header, &status);