]> ToastFreeware Gitweb - toast/confclerk.git/blobdiff - src/orm/ormrecord.h
activities tab implemented
[toast/confclerk.git] / src / orm / ormrecord.h
index 8d4212a12cc4e31ab7c631d431afcc8c87f9ca40..2328549c9e28e83b4bdacdfd42c12cd777ce1642 100644 (file)
@@ -9,6 +9,19 @@
 #include <QDateTime>
 #include <QDebug>
 
+// INFO:
+// all record items/columns may be defined in one table (1.), or
+// they can be splitted in two separate tables (2.) (eg. for FTS support)
+// 1.) you have to define "static QString const sTableName"
+// 2.) you have to define two table names:
+//     "static QString const sTable1Name"
+//     "static QString const sTable2Name"
+// and since all record items/columns are handled by one QSqlRecord,
+// you have to also define number of columns that belongs to table 1 (1.), and table 2 (2.)
+// 1.) "static int const sTable1ColCount"
+// 2.) "static int const sTable2ColCount"
+// there are also defined auxiliary methods for 1-Table/2-Tables approach, see bellow
+
 class OrmException
 {
 };
@@ -43,10 +56,14 @@ protected:
     static QList<T> load(QSqlQuery query);
 
     // auxiliary methods
+    static QSqlRecord toRecord(const QList<QSqlField> & columnList);
+    // all record items/columns are in one table
     static QString columnsForSelect(const QString& prefix = QString());
     static QString selectQuery();
     static QString updateQuery();
-    static QSqlRecord toRecord(const QList<QSqlField> & columnList);
+    // record items/columns are stored in two tables
+    static QString columnsForSelectJoin2T(); // for joining two tables
+    static QString selectQueryJoin2T(const QString &key); // for joining two tables
 
     static QVariant convertToC(QVariant value, QVariant::Type colType);
     static QVariant convertToDb(QVariant value, QVariant::Type colType);
@@ -103,13 +120,13 @@ T OrmRecord<T>::loadOne(QSqlQuery query)
     {
         if (!query.exec())
         {
-            throw new OrmSqlException(query.lastError().text());
+            throw OrmSqlException(query.lastError().text());
         }
     }
 
     if (!query.next())
     {
-        throw new OrmNoObjectException();
+        throw OrmNoObjectException();
     }
 
     return hydrate(query.record());
@@ -122,7 +139,12 @@ QList<T> OrmRecord<T>::load(QSqlQuery query)
     {
         if (!query.exec())
         {
-            throw new OrmSqlException(query.lastError().text());
+            qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type();
+            throw OrmSqlException(query.lastError().text());
+        }
+        else
+        {
+            qDebug() << "SQL OK";
         }
     }
 
@@ -131,7 +153,7 @@ QList<T> OrmRecord<T>::load(QSqlQuery query)
     {
         objects << hydrate(query.record());
     }
-
+    qDebug() << "Fetch done";
     return objects;
 }
 
@@ -148,16 +170,47 @@ QString OrmRecord<T>::columnsForSelect(const QString& prefix)
     return prefixedColumns.join(",");
 }
 
+template <typename T>
+QString OrmRecord<T>::columnsForSelectJoin2T()
+{
+    Q_ASSERT((T::sTable1ColCount+T::sTable2ColCount) == T::sColumns.count());
+
+    QStringList prefixedColumns;
+    for (int i=0; i<T::sTable1ColCount; i++)
+    {
+        prefixedColumns.append(QString("%1.%2").arg(T::sTable1Name, T::sColumns.field(i).name()));
+    }
+    for (int j=0; j<T::sTable2ColCount; j++)
+    {
+        prefixedColumns.append(QString("%1.%2").arg(T::sTable2Name, T::sColumns.field(T::sTable1ColCount+j).name()));
+    }
+    return prefixedColumns.join(",");
+}
+
 template <typename T>
 QString OrmRecord<T>::selectQuery()
 {
     return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
 }
 
+
+template <typename T>
+QString OrmRecord<T>::selectQueryJoin2T(const QString &key)
+{
+    return QString("SELECT %1 FROM %2 INNER JOIN %3 ON %4.%5 = %6.%7 ").arg(
+            columnsForSelectJoin2T(),
+            T::sTable1Name,
+            T::sTable2Name,
+            T::sTable1Name,
+            key,
+            T::sTable2Name,
+            key);
+}
+
 template <typename T>
 QString OrmRecord<T>::updateQuery()
 {
-    return QString("UPDATE %1 ").arg(T::sTableName);
+    return QString("UPDATE %1 ").arg(T::sTable1Name);
 }
 
 template <typename T>
@@ -197,3 +250,4 @@ QVariant OrmRecord<T>::convertToDb(QVariant value, QVariant::Type colType)
 }
 
 #endif // ORMRECORD_H
+