|
@@ -36,12 +36,12 @@ class Entry {
|
|
|
public:
|
|
|
const DataPosition position;
|
|
|
protected:
|
|
|
- TableData table_data;
|
|
|
+ TableData tableData;
|
|
|
public:
|
|
|
Entry() = delete;
|
|
|
Entry(DataPosition position_) : position(position_) {}
|
|
|
- void populate_data(TableData data) { table_data = data; }
|
|
|
- const TableData& data() { return table_data; }
|
|
|
+ void setData(TableData data) { tableData = data; }
|
|
|
+ const TableData& getData() { return tableData; }
|
|
|
};
|
|
|
|
|
|
// [George]: Either with polymorphism or simple functions the result will be the same.
|
|
@@ -51,43 +51,12 @@ public:
|
|
|
// constructor
|
|
|
class PilotEntry : public Entry {
|
|
|
PilotEntry() = delete;
|
|
|
+public:
|
|
|
PilotEntry(int row_id) : Entry::Entry(DataPosition("pilots", row_id)) {}
|
|
|
-};
|
|
|
-
|
|
|
-Entry newPilotEntry(int row_id) { return Entry(DataPosition("pilots", row_id)); }
|
|
|
-
|
|
|
-static
|
|
|
-bool insertPilot(UserInput& uin)
|
|
|
-{
|
|
|
- DEB("Inserting...");
|
|
|
- auto data = uin.all();
|
|
|
- auto position = DEFAULT_PILOT_POSITION;
|
|
|
-
|
|
|
- if (data.isEmpty()) {
|
|
|
- DEB("Object Contains no data. Aborting.");
|
|
|
- return false;
|
|
|
- }
|
|
|
- QString statement = "INSERT INTO " + position.first + QLatin1String(" (");
|
|
|
- for (auto i = data.cbegin(); i != data.cend(); ++i) {
|
|
|
- statement += i.key() + QLatin1String(", ");
|
|
|
+ PilotEntry(TableData fromNewPilotDialog) : Entry::Entry(DataPosition("pilots", 0)) {
|
|
|
+ setData(fromNewPilotDialog);
|
|
|
}
|
|
|
- statement.chop(2);
|
|
|
- statement += QLatin1String(") VALUES (");
|
|
|
- for (auto i = data.cbegin(); i != data.cend(); ++i) {
|
|
|
- statement += QLatin1String("'") + i.value() + QLatin1String("', ");
|
|
|
- }
|
|
|
- statement.chop(2);
|
|
|
- statement += QLatin1String(")");
|
|
|
-
|
|
|
- QSqlQuery q(statement);
|
|
|
- if (q.lastError().type() == QSqlError::NoError) {
|
|
|
- DEB("Entry successfully committed.");
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- DEB("Unable to commit. Query Error: " << q.lastError().text());
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
/*!
|
|
|
* \brief The DB class encapsulates the SQL database by providing fast access
|
|
@@ -95,68 +64,45 @@ bool insertPilot(UserInput& uin)
|
|
|
*/
|
|
|
class DB {
|
|
|
private:
|
|
|
- static QStringList tableNames;
|
|
|
- static QMap<TableName, QStringList> tableColumns;
|
|
|
+ static inline QStringList tableNames;
|
|
|
+ static inline QMap<TableName, QStringList> tableColumns;
|
|
|
public:
|
|
|
/*!
|
|
|
- * \brief Initialise DB class and populate columns.
|
|
|
+ * \brief Connect to the database and populate database information.
|
|
|
*/
|
|
|
- static bool init();
|
|
|
+ static bool connect();
|
|
|
|
|
|
/*!
|
|
|
- * \brief Create new entry in the databse based on UserInput
|
|
|
+ * \brief closes the database connection.
|
|
|
*/
|
|
|
- static
|
|
|
- bool insert(UserInput uin)
|
|
|
- {
|
|
|
- switch (uin.meta_tag)
|
|
|
- {
|
|
|
- case UserInput::MetaTag::Pilot:
|
|
|
- DEB("Inserting NEW Pilot...");
|
|
|
- return insertPilot(uin);
|
|
|
- case UserInput::MetaTag::Flight:
|
|
|
- case UserInput::MetaTag::Aircraft:
|
|
|
- default:
|
|
|
- DEB("FALLTHROUGH in cases. Missing implementations");
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
+ static bool disconnect();
|
|
|
+
|
|
|
+ /*!
|
|
|
+ * \brief Checks if an entry exists in the database, based on position data
|
|
|
+ */
|
|
|
+ static bool exists(Entry entry);
|
|
|
|
|
|
- static bool remove(UserInput uin) { return false; }
|
|
|
+ /*!
|
|
|
+ * \brief commits an entry to the database, calls either insert or update,
|
|
|
+ * based on position data
|
|
|
+ */
|
|
|
+ static bool commit(Entry entry);
|
|
|
|
|
|
- static bool exists(UserInput uin) { return false; }
|
|
|
+ /*!
|
|
|
+ * \brief Create new entry in the databse based on UserInput
|
|
|
+ */
|
|
|
+ static bool insert(Entry newEntry);
|
|
|
|
|
|
/*!
|
|
|
* \brief Updates entry in database from existing entry tweaked by the user.
|
|
|
- * New entry data is verified before commiting.
|
|
|
*/
|
|
|
- static bool update(Entry updated_entry)
|
|
|
- {
|
|
|
- auto position = updated_entry.position;
|
|
|
- auto data = updated_entry.data();
|
|
|
- QString statement = "UPDATE " + position.first + " SET ";
|
|
|
- for (auto i = data.constBegin(); i != data.constEnd(); ++i) {
|
|
|
- if (i.key() != QString()) {
|
|
|
- statement += i.key() + QLatin1String("='") + i.value() + QLatin1String("', ");
|
|
|
- } else {
|
|
|
- DEB(i.key() << "is empty key. skipping.");
|
|
|
- }
|
|
|
- }
|
|
|
- statement.chop(2); // Remove last comma
|
|
|
- statement.append(QLatin1String(" WHERE _rowid_=") + QString::number(position.second));
|
|
|
-
|
|
|
- DEB("UPDATE QUERY: " << statement);
|
|
|
- QSqlQuery q(statement);
|
|
|
- //check result. Upon success, error should be " "
|
|
|
- if (q.lastError().type() == QSqlError::NoError)
|
|
|
- {
|
|
|
- DEB("Entry successfully committed.");
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- DEB("Unable to commit. Query Error: " << q.lastError().text());
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
+ static bool update(Entry updated_entry);
|
|
|
+
|
|
|
+ /*!
|
|
|
+ * \brief deletes an entry from the database.
|
|
|
+ */
|
|
|
+ static bool remove(Entry entry);
|
|
|
+
|
|
|
};
|
|
|
|
|
|
} // namespace experimental
|