Add FITS files indexing support to database
This commit is contained in:
+100
-1
@@ -3,6 +3,8 @@
|
||||
#include <QDir>
|
||||
#include <QSqlError>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include "loadrunable.h"
|
||||
|
||||
Database::Database(QObject *parent) : QObject(parent)
|
||||
{
|
||||
@@ -23,7 +25,11 @@ bool Database::init()
|
||||
m_database.setDatabaseName(dir.absoluteFilePath("database.db"));
|
||||
if(m_database.open())
|
||||
{
|
||||
m_database.exec("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT, file VARCHAR(255) UNIQUE);");
|
||||
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)");
|
||||
QSqlError error = m_database.lastError();
|
||||
|
||||
if(error.type() == QSqlError::NoError)
|
||||
@@ -34,6 +40,17 @@ bool Database::init()
|
||||
m_unmarkQuery.prepare("DELETE FROM files WHERE file = (?)");
|
||||
m_isMarkedQuery = QSqlQuery(m_database);
|
||||
m_isMarkedQuery.prepare("SELECT * FROM files WHERE file = (:name)");
|
||||
|
||||
m_insertFile = QSqlQuery(m_database);
|
||||
m_insertFile.prepare("INSERT INTO fits_files (file, mtime) VALUES (?, ?)");
|
||||
m_insertFitsHeader = QSqlQuery(m_database);
|
||||
m_insertFitsHeader.prepare("INSERT INTO fits_headers (id_file, key, value, comment) VALUES (?, ?, ?, ?)");
|
||||
m_checkFile = QSqlQuery(m_database);
|
||||
m_checkFile.prepare("SELECT id,mtime FROM fits_files WHERE file=?");
|
||||
m_headerKeywords = QSqlQuery(m_database);
|
||||
m_headerKeywords.prepare("SELECT DISTINCT key FROM fits_headers");
|
||||
m_deleteFile = QSqlQuery(m_database);
|
||||
m_deleteFile.prepare("DELETE FROM fits_files WHERE id=?");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -90,3 +107,85 @@ bool Database::checkError()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Database::indexDir(const QDir &dir)
|
||||
{
|
||||
//m_database.exec("DROP TABLE fits_files");
|
||||
m_database.transaction();
|
||||
if(indexDir2(dir))
|
||||
m_database.commit();
|
||||
else
|
||||
m_database.rollback();
|
||||
}
|
||||
|
||||
QStringList Database::getFitsKeywords()
|
||||
{
|
||||
m_headerKeywords.exec();
|
||||
QStringList keywords;
|
||||
while(m_headerKeywords.next())
|
||||
{
|
||||
keywords << m_headerKeywords.value(0).toString();
|
||||
}
|
||||
return keywords;
|
||||
}
|
||||
|
||||
bool Database::indexDir2(const QDir &dir)
|
||||
{
|
||||
static QStringList nameFilters = {"*.fit", "*.fits"};
|
||||
QFileInfoList files = dir.entryInfoList(nameFilters, QDir::Files);
|
||||
QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
for(const QString &d : dirs)
|
||||
{
|
||||
if(!indexDir2(dir.filePath(d)))
|
||||
return false;
|
||||
}
|
||||
ImageInfoData info;
|
||||
for(const QFileInfo &file : files)
|
||||
{
|
||||
QString filePath = file.absoluteFilePath();
|
||||
QString mtime = file.fileTime(QFileDevice::FileModificationTime).toString(Qt::ISODate);
|
||||
m_checkFile.bindValue(0, filePath);
|
||||
m_checkFile.exec();
|
||||
if(m_checkFile.next())
|
||||
{
|
||||
if(m_checkFile.value(1).toString() == file.fileTime(QFileDevice::FileModificationTime).toString(Qt::ISODate))
|
||||
continue;
|
||||
else
|
||||
{
|
||||
m_deleteFile.bindValue(0, m_checkFile.value(0).toLongLong());
|
||||
m_deleteFile.exec();
|
||||
}
|
||||
}
|
||||
|
||||
readFITSHeader(filePath, info);
|
||||
m_insertFile.bindValue(0, filePath);
|
||||
m_insertFile.bindValue(1, mtime);
|
||||
if(!m_insertFile.exec())
|
||||
{
|
||||
qDebug() << m_insertFile.lastError();
|
||||
return false;
|
||||
}
|
||||
qlonglong last_id = m_insertFile.lastInsertId().toLongLong();
|
||||
QVariantList file_id, keys, values, comments;
|
||||
for(auto &record : info.fitsHeader)
|
||||
{
|
||||
file_id << last_id;
|
||||
keys << QString(record.key);
|
||||
values << record.value.toString();
|
||||
comments << QString(record.comment);
|
||||
}
|
||||
m_insertFitsHeader.bindValue(0, file_id);
|
||||
m_insertFitsHeader.bindValue(1, keys);
|
||||
m_insertFitsHeader.bindValue(2, values);
|
||||
m_insertFitsHeader.bindValue(3, comments);
|
||||
if(!m_insertFitsHeader.execBatch())
|
||||
{
|
||||
qDebug() << m_insertFitsHeader.lastError();
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << filePath << last_id;
|
||||
info.fitsHeader.clear();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user