catalogdb.h 7.04 KB
Newer Older
Rishab Arora's avatar
Rishab Arora committed
1
/***************************************************************************
Rishab Arora's avatar
Rishab Arora committed
2
                catalogDB.h  -  K Desktop Planetarium
Rishab Arora's avatar
Rishab Arora committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
                             -------------------
    begin                : 2012/03/08
    copyright            : (C) 2012 by Rishab Arora
    email                : ra.rishab@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18 19
#ifndef CATALOGDB_H
#define CATALOGDB_H
Rishab Arora's avatar
Rishab Arora committed
20

Rishab Arora's avatar
Rishab Arora committed
21
#include <datahandlers/ksparser.h>
Rishab Arora's avatar
Rishab Arora committed
22
#include <datahandlers/catalogdata.h>
23
#include <datahandlers/catalogentrydata.h>
Rishab Arora's avatar
Rishab Arora committed
24 25 26
#include "kstars/skyobjects/starobject.h"
#include "kstars/skyobjects/deepskyobject.h"
#include "kstars/skycomponents/skycomponent.h"
Rishab Arora's avatar
Rishab Arora committed
27 28 29
#include <kstandarddirs.h>
#include <klocale.h>
#include <KDebug>
Rishab Arora's avatar
Rishab Arora committed
30
#include <KMessageBox>
31 32 33 34 35 36 37 38 39 40
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QString>
#include <QHash>
#include <QSqlError>
#include <QVariant>
#include <QFile>
41
#include <QDir>
Rishab Arora's avatar
Rishab Arora committed
42 43


44 45 46 47
/* Some notes about the database. (skycomponents.db)
 * 1) The uid for Object Designation is the uid being used by objects in KStars
 *    hence, the uid is a qint64 i.e. a 64 bit signed integer. Coincidentaly,
 *    this is the max limit of an int in Sqlite3.
Rishab Arora's avatar
Rishab Arora committed
48
 *    Hence, the db is compatible with the uid, but doesn't use it as of now.
49 50
 */

Rishab Arora's avatar
Rishab Arora committed
51 52
class CatalogDB {
 public:
Rishab Arora's avatar
Rishab Arora committed
53 54 55 56 57 58 59 60
   /**
    * @brief Initializes the database and sets up pointers to Catalog DB
    * Performs the following actions:
    * 1. Checks if database file exists
    * 2. Checks if database can be opened
    * 3. If DB file is missing, creates new DB
    * 4. Sets up pointer to Catalog DB
    *
61 62
    * usage: Call QSqlDatabase::removeDatabase("skydb"); after the object
    * of this class is deallocated
Rishab Arora's avatar
Rishab Arora committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    * @return bool
    **/
   bool Initialize();
   /**
    * @brief Attempt to close database and remove reference from the DB List
    *
    **/
   ~CatalogDB();
   /**
    * @brief Accessor for list of all available catalogs in db
    *
    * @return QStringList*
    **/
   QStringList* Catalogs();
   /**
    * @brief Rechecks the database and builds the Catalog listing.
    * New listing is directly updated into catalogs (accessed using Catalogs())
    *
    * @return void
    **/
   void RefreshCatalogList();
84 85 86 87 88 89 90 91
  /**
    * @short Add contents of custom catalog to the program database
    *
    * @p filename the name of the file containing the data to be read
    * @return true if catalog was successfully added
  */
  bool AddCatalogContents(const QString &filename);

Rishab Arora's avatar
Rishab Arora committed
92 93 94 95 96 97 98 99
  /**
   * @brief returns the id of the row if it matches with certain fuzz.
   * Else return -1 if none found
   *
   * @param ra Right Ascension of new object to be added
   * @param dec Declination of new object to be added
   * @return int RowUID of the new row
   **/
100
  int FindFuzzyEntry(const double ra, const double dec,
Rishab Arora's avatar
Rishab Arora committed
101 102
                     const double magnitude);

Rishab Arora's avatar
Rishab Arora committed
103 104 105
  /**
   * @brief Used to add a cross referenced entry into the database
   *
106
   * @param catalog_entry Data structure with entry details
Rishab Arora's avatar
Rishab Arora committed
107 108
   * @return void
   **/
109
  void AddEntry(const CatalogEntryData &catalog_entry);
Rishab Arora's avatar
Rishab Arora committed
110

Rishab Arora's avatar
Rishab Arora committed
111 112 113 114 115 116
  /**
   * @brief Removes the catalog from the database and refreshes the listing.
   *
   * @param catalog_name Name of the catalog
   * @return void
   **/
117
  void RemoveCatalog(const QString& catalog_name);
118 119 120

  /**
   * @brief Creates objects of type SkyObject and assigns them to references
Rishab Arora's avatar
Rishab Arora committed
121
   *
122 123 124 125 126 127 128
   * @param catalog Name of the catalog whose objects are needed.
   * @param sky_list List of all skyobjects stored in database (assigns)
   * @param names List of named objects in database (assigns)
   * @param catalog_pointer pointer to the catalogcomponent objects
   *                        (needed for building skyobjects)
   * @return void
   **/
Rishab Arora's avatar
Rishab Arora committed
129
  void GetAllObjects(const QString &catalog_name,
130
                     QList< SkyObject* > &sky_list,
Rishab Arora's avatar
Rishab Arora committed
131
                     QList < QPair <int, QString> > &object_names,
132 133 134 135 136 137
                     CatalogComponent *catalog_pointer);

  /**
   * @brief Get information about the catalog like Prefix etc
   *
   * @param catalog_name Name of catalog whose details are required
138
   * @param catalog_data Data structure assigned with required data
139 140
   * @return void
   **/
141 142
  void GetCatalogData(const QString& catalog_name,
                      CatalogData& catalog_data);
Rishab Arora's avatar
Rishab Arora committed
143

Rishab Arora's avatar
Rishab Arora committed
144
 private:
145 146 147 148
  /**
   * @brief Database object for the sky object. Assigned and Initialized by
   *        Initialize()
   **/
149
  QSqlDatabase skydb_;
150 151 152 153 154 155

  /**
   * @brief Returns the last error the database encountered
   *
   * @return QSqlError
   **/
Rishab Arora's avatar
Rishab Arora committed
156
  QSqlError LastError();
157 158 159 160 161

  /**
   * @brief List of all the catalogs contained in the database.
   * This variable is accessed through Catalogs()
   **/
162
  QStringList catalog_list_;
163 164 165 166 167 168 169 170

  /**
   * @brief Returns database ID of the required catalog.
   * Returns -1 if not found.
   *
   * @param name Name of the class being searched
   * @return int
   **/
Rishab Arora's avatar
Rishab Arora committed
171
  int FindCatalog(const QString &catalog_name);
Rishab Arora's avatar
Rishab Arora committed
172 173 174 175 176 177 178 179

  /**
   * @short Add the catalog name and details to the db.
   * This does not store the contents. It only adds the catalog info
   * to the database. Hence, it is step 1 in AddCatalogContents
   *
   * @param lines List of lines to use for extraction of details
   * @param Columns Stores the read Columns in this list
180 181
   * @param catalog_name Name retrieved from file header
   * @param delimiter Delimeter retrieved from file header
Rishab Arora's avatar
Rishab Arora committed
182 183
   * @return bool
   **/
184
  bool ParseCatalogInfoToDB(const QStringList &lines, QStringList &columns,
185
                            QString &catalog_name, char &delimiter);
Rishab Arora's avatar
Rishab Arora committed
186

187 188 189
  /**
   * @brief Add the catalog with given details into the database
   *
190
   * @param catalog_data CatalogData object encompassing all catalog info
191 192
   * @return void
   **/
193 194
  void AddCatalog(const CatalogData& catalog_data);

195 196 197 198 199 200 201 202 203
  /**
   * @brief Prepares the sequence required by KSParser according to header.
   * Information on the sequence is stored inside the header
   *
   * @param Columns List of the columns names as strings
   * @return QList of the format usable by KSParser
   **/
  QList< QPair< QString, KSParser::DataTypes > >
                              buildParserSequence(const QStringList& Columns);
Rishab Arora's avatar
Rishab Arora committed
204 205 206 207 208 209
  /**
   * @brief Clears out the DSO table for the given catalog ID
   *
   * @param catalog_id DB generated catalog ID
   * @return void
  **/
210
  void ClearDSOEntries(int catalog_id);
Rishab Arora's avatar
Rishab Arora committed
211 212 213 214 215
  /**
   * @brief Contains setup routines to intitialize a database for catalog storage
   *
   * @return void
   **/
Rishab Arora's avatar
Rishab Arora committed
216
  void FirstRun();
Rishab Arora's avatar
Rishab Arora committed
217 218
};

Rishab Arora's avatar
Rishab Arora committed
219
#endif  // CATALOGDB_H