File indexing completed on 2024-03-24 15:18:04

0001 /*
0002     SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "import_skycomp.h"
0008 
0009 QString db_path()
0010 {
0011     return QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation))
0012            .filePath("skycomponents.sqlite");
0013 }
0014 
0015 std::pair<bool, QSqlDatabase> SkyComponentsImport::get_skycomp_db(const QString &path)
0016 {
0017     if (!QFileInfo(path).exists())
0018         return { false, {} };
0019 
0020     auto db = QSqlDatabase::addDatabase("QSQLITE");
0021     db.setDatabaseName(path);
0022 
0023     if (!db.open())
0024         return { false, {} };
0025 
0026     return { true, db };
0027 }
0028 
0029 std::pair<bool, QSqlDatabase> SkyComponentsImport::get_skycomp_db()
0030 {
0031     return get_skycomp_db(db_path());
0032 }
0033 
0034 std::tuple<bool, QString, CatalogsDB::CatalogObjectVector>
0035 SkyComponentsImport::get_objects(QSqlDatabase db, const std::list<int> &ids)
0036 {
0037     QVector<QString> const placeholders(ids.size(), "?");
0038     if (!db.open())
0039     {
0040         qDebug() << Q_FUNC_INFO << "here";
0041         return { false, {}, {} };
0042     }
0043 
0044     QSqlQuery query{ db };
0045     query.prepare(
0046         QString("SELECT UID, RA, Dec, Type, Magnitude, PositionAngle, MajorAxis, "
0047                 "MinorAxis, "
0048                 "Flux, LongName, id_Catalog FROM DSO INNER JOIN ObjectDesignation ON "
0049                 "DSO.UID "
0050                 "= "
0051                 "ObjectDesignation.UID_DSO WHERE id_Catalog IN (%1)")
0052         .arg(QStringList::fromVector(placeholders).join(", ")));
0053 
0054     for (auto const &i : ids)
0055         query.addBindValue(i);
0056 
0057     query.setForwardOnly(true);
0058 
0059     if (!query.exec())
0060         return { false, query.lastError().text(), {} };
0061 
0062     CatalogsDB::CatalogObjectVector objs{};
0063     while (query.next())
0064     {
0065         const SkyObject::TYPE type = static_cast<SkyObject::TYPE>(query.value(3).toInt());
0066 
0067         const double ra         = query.value(1).toDouble();
0068         const double dec        = query.value(2).toDouble();
0069         const float mag         = query.isNull(4) ? NaN::f : query.value(4).toFloat();
0070         const QString name      = query.value(9).toString();
0071         const QString long_name = "";
0072         const QString catalog_identifier = "";
0073         const float major                = query.value(6).toFloat();
0074         const float minor                = query.value(7).toFloat();
0075         const double position_angle      = query.value(5).toDouble();
0076         const float flux                 = query.value(8).toFloat();
0077         const int catalog_id             = 0;
0078 
0079         objs.emplace_back(CatalogObject::oid{}, type, dms(ra), dms(dec), mag, name,
0080                           long_name, catalog_identifier, catalog_id, major, minor,
0081                           position_angle, flux, "");
0082     }
0083 
0084     db.close();
0085     return { true, {}, objs };
0086 };