Ver Fonte

Merge remote-tracking branch 'origin/devel-db-entry-architecture' into devel-db-getEntryData

Felix Turo há 4 anos atrás
pai
commit
0ff795fa16

+ 9 - 9
src/experimental/DataBase.cpp

@@ -6,7 +6,7 @@ DataBase* DataBase::instance = nullptr;
 
 DataBase* DataBase::getInstance()
 {
-    if (!instance) instance = new DataBase();
+    if(!instance) instance = new DataBase();
     return instance;
 }
 
@@ -58,13 +58,13 @@ bool DataBase::remove(Entry entry)
         return false;
     }
 
-    QString statement = "DELETE FROM " + entry.position.first +
-            " WHERE _rowid_=" + QString::number(entry.position.second);
+    QString statement = "DELETE FROM " + entry.position.tableName +
+            " WHERE _rowid_=" + QString::number(entry.position.rowId);
     QSqlQuery q(statement);
     //check result.
     if (q.lastError().type() == QSqlError::NoError)
     {
-        DEB("Entry " << entry.position.first << entry.position.second << " removed.");
+        DEB("Entry " << entry.position.tableName << entry.position.rowId << " removed.");
         return true;
     } else {
         DEB("Unable to delete.");
@@ -80,8 +80,8 @@ bool DataBase::exists(Entry entry)
         return false;
 
     //Check database for row id
-    QString statement = "SELECT COUNT(*) FROM " + entry.position.first +
-            " WHERE _rowid_=" + QString::number(entry.position.second);
+    QString statement = "SELECT COUNT(*) FROM " + entry.position.tableName +
+            " WHERE _rowid_=" + QString::number(entry.position.rowId);
     //this returns either 1 or 0 since row ids are unique
     QSqlQuery q(statement);
     q.next();
@@ -99,7 +99,7 @@ bool DataBase::exists(Entry entry)
 bool DataBase::update(Entry updated_entry)
 {
     auto data = updated_entry.getData();
-    QString statement = "UPDATE " + updated_entry.position.first + " SET ";
+    QString statement = "UPDATE " + updated_entry.position.tableName + " SET ";
     for (auto i = data.constBegin(); i != data.constEnd(); ++i) {
         if (i.key() != QString()) {
             statement += i.key() + QLatin1String("='") + i.value() + QLatin1String("', ");
@@ -108,7 +108,7 @@ bool DataBase::update(Entry updated_entry)
         }
     }
     statement.chop(2); // Remove last comma
-    statement.append(QLatin1String(" WHERE _rowid_=") + QString::number(updated_entry.position.second));
+    statement.append(QLatin1String(" WHERE _rowid_=") + QString::number(updated_entry.position.rowId));
 
     DEB("UPDATE QUERY: " << statement);
     QSqlQuery q(statement);
@@ -129,7 +129,7 @@ bool DataBase::insert(Entry newEntry)
 {
     auto data = newEntry.getData();
     DEB("Inserting...");
-    QString statement = "INSERT INTO " + newEntry.position.first + QLatin1String(" (");
+    QString statement = "INSERT INTO " + newEntry.position.tableName + QLatin1String(" (");
     QMap<QString, QString>::iterator i;
     for (i = data.begin(); i != data.end(); ++i) {
         statement += i.key() + QLatin1String(", ");

+ 10 - 8
src/experimental/Decl.h

@@ -4,6 +4,7 @@
 #include <QString>
 #include <QPair>
 #include <QMap>
+#include <QObject>
 
 namespace experimental {
 
@@ -13,27 +14,28 @@ using TableName = QString;
 using RowId = int;
 
 using TableNames = QStringList;
+/// [G]: May lead to some confusion. TableData suggest data for the entire table.
+/// but in reallity it is data per column *of single row* (unless i misunderstand)
 using TableData = QMap<ColName, ColData>;
 using ColumnData = QPair<ColName, ColData>;
 using ColumnNames = QStringList;
 using TableColumns = QMap<TableName, ColumnNames>;
 
+/// [G]: Needs some work. Inheriting from QPair may be helpful but
+/// may also be overkill. Lets determine the specific uses of DataPosition
+/// and provide our own interface i would say.
 struct DataPosition : QPair<TableName, RowId> {
-    TableName& tableName;
-    RowId& rowId;
+    TableName tableName;
+    RowId rowId;
     DataPosition()
-        : QPair<TableName, RowId>::QPair(), tableName(first), rowId(second)
+        : tableName(first), rowId(second)
     {}
     DataPosition(TableName table_name, RowId row_id)
         : QPair<TableName, RowId>::QPair(table_name, row_id),
           tableName(first), rowId(second)
     {}
     DataPosition(const DataPosition& other) = default;
-    void operator=(const DataPosition& other)
-    {
-        tableName = other.tableName;
-        rowId = other.rowId;
-    }
+    DataPosition& operator=(const DataPosition& other) = default;
 };
 
 auto const DEFAULT_PILOT_POSITION = DataPosition("pilots", 0);

+ 0 - 16
src/experimental/Entry.cpp

@@ -6,12 +6,6 @@ Entry::Entry(DataPosition position_)
     : position(position_)
 {}
 
-void Entry::operator=(const Entry& other)
-{
-    position = other.position;
-    tableData = other.tableData;
-}
-
 void Entry::setData(TableData table_data)
 {
     tableData = table_data;
@@ -22,16 +16,6 @@ const TableData& Entry::getData()
     return tableData;
 }
 
-PilotEntry::PilotEntry(const PilotEntry& pe)
-    : Entry::Entry(pe)
-{}
-
-void PilotEntry::operator=(const PilotEntry& pe)
-{
-    position = pe.position;
-    tableData = pe.tableData;
-}
-
 PilotEntry::PilotEntry(int row_id)
     : Entry::Entry(DataPosition("pilots", row_id))
 {}

+ 4 - 4
src/experimental/Entry.h

@@ -22,7 +22,7 @@ protected:
 public:
     Entry() = default;
     Entry(const Entry&) = default;
-    void operator=(const Entry&);
+    Entry& operator=(const Entry&) = default;
     Entry(DataPosition position_);
     void setData(TableData table_data);
     const TableData& getData();
@@ -32,10 +32,10 @@ public:
 class PilotEntry : public Entry {
 public:
     PilotEntry() = default;
-    PilotEntry(const PilotEntry& pe);
-    void operator=(const PilotEntry& pe);
+    PilotEntry(const PilotEntry& pe) = default;
+    PilotEntry& operator=(const PilotEntry& pe) = default;
     PilotEntry(int row_id);
-    PilotEntry(TableData fromNewPilotDialog);
+    PilotEntry(TableData newPilotData);
 };
 
 }