Ver código fonte

Refactored copy consructors so that they actually work, hopefully

Georgios Kotzampopoulos 4 anos atrás
pai
commit
738bfbed03

+ 11 - 10
src/experimental/DataBase.cpp

@@ -2,11 +2,12 @@
 
 namespace experimental {
 
-DataBase* const DataBase::instance = nullptr;
+DataBase* DataBase::instance = nullptr;
 
 DataBase* DataBase::getInstance()
 {
-    return instance ?: new DataBase();
+    if(!instance) instance = new DataBase();
+    return instance;
 }
 
 bool DataBase::connect()
@@ -56,13 +57,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.");
@@ -78,8 +79,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();
@@ -97,7 +98,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("', ");
@@ -106,7 +107,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);
@@ -127,7 +128,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(", ");

+ 1 - 1
src/experimental/DataBase.h

@@ -26,7 +26,7 @@ class DataBase {
 private:
     TableNames tableNames;
     TableColumns tableColumns;
-    static DataBase* const instance;
+    static DataBase* instance;
     DataBase() = default;
 public:
     // Ensure DB is not copiable or assignable

+ 8 - 8
src/experimental/Decl.h

@@ -4,6 +4,7 @@
 #include <QString>
 #include <QPair>
 #include <QMap>
+#include <QObject>
 
 namespace experimental {
 
@@ -18,22 +19,21 @@ 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 - 12
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;
@@ -26,12 +20,6 @@ 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))
 {}

+ 2 - 2
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();
@@ -33,7 +33,7 @@ class PilotEntry : public Entry {
 public:
     PilotEntry() = default;
     PilotEntry(const PilotEntry& pe);
-    void operator=(const PilotEntry& pe);
+    PilotEntry& operator=(const PilotEntry& pe) = default;
     PilotEntry(int row_id);
     PilotEntry(TableData fromNewPilotDialog);
 };