changeset 252:4d2b37a0acf2

Move SQL table creation from schema array to util-module.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 08 May 2018 15:00:17 +0300
parents ede0fde2ae6e
children eadffc38ab43
files src/main.cpp src/main.h src/util.cpp src/util.h
diffstat 4 files changed, 56 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.cpp	Tue May 08 13:30:47 2018 +0300
+++ b/src/main.cpp	Tue May 08 15:00:17 2018 +0300
@@ -224,33 +224,14 @@
     //
     // Create tables
     //
-    for (int ntable = 0; ntable < nslSQLSchemaData; ntable++)
+    if (!slConditionallyCreateSQLTables(db, slSQLSchemaData, nslSQLSchemaData))
     {
-        const SLSQLSchemaDef &table = slSQLSchemaData[ntable];
-        if (!db.tables().contains(table.name))
-        {
-            QSqlQuery query;
-            QString sql =
-                QStringLiteral("CREATE TABLE %1 (%2)").
-                arg(table.name).
-                arg(table.schema);
-
-            query.exec(sql);
-
-            if (!slCheckAndReportSQLError(
-                QStringLiteral("CREATE TABLE %1").arg(table.name),
-                query.lastError(), true))
-            {
-                slErrorMsg(
-                    QObject::tr("Tietokantataulua ei voitu luoda"),
-                    QObject::tr("Virhe: %1<br><br>").
-                    arg(db.lastError().text())
-                    );
-                return 1;
-            }
-
-            query.finish();
-        }
+        slErrorMsg(
+            QObject::tr("Tietokantataulua ei voitu luoda"),
+            QObject::tr("Virhe: %1<br><br>").
+            arg(db.lastError().text())
+            );
+        return 1;
     }
 
     //
--- a/src/main.h	Tue May 08 13:30:47 2018 +0300
+++ b/src/main.h	Tue May 08 15:00:17 2018 +0300
@@ -71,16 +71,6 @@
 
 
 //
-// SQL schema / table definitions
-//
-typedef struct
-{
-    QString name;
-    QString schema;
-} SLSQLSchemaDef;
-
-
-//
 // Person information record
 //
 class SLPersonInfo : public QObject
--- a/src/util.cpp	Tue May 08 13:30:47 2018 +0300
+++ b/src/util.cpp	Tue May 08 15:00:17 2018 +0300
@@ -135,3 +135,31 @@
         return true;
     }
 }
+
+
+bool slConditionallyCreateSQLTables(QSqlDatabase &db, const SLSQLSchemaDef *schema, const int nschema)
+{
+    for (int ntable = 0; ntable < nschema; ntable++)
+    {
+        const SLSQLSchemaDef &table = schema[ntable];
+        if (!db.tables().contains(table.name))
+        {
+            // Attempt to create the table
+            QSqlQuery tcreate(db);
+            QString sql =
+                QStringLiteral("CREATE TABLE %1 (%2)").
+                arg(table.name).
+                arg(table.schema);
+
+            tcreate.exec(sql);
+
+            if (!slCheckAndReportSQLError(
+                sql, tcreate.lastError(), true))
+                return false;
+
+            tcreate.finish();
+        }
+    }
+
+    return true;
+}
--- a/src/util.h	Tue May 08 13:30:47 2018 +0300
+++ b/src/util.h	Tue May 08 15:00:17 2018 +0300
@@ -9,16 +9,37 @@
 #ifndef SL_UTIL_H
 #define SL_UTIL_H
 
+
+#define SQL_MAX_SCHEMA_INSERTS 16
+
+
+//
+// SQL schema / table definitions
+//
+typedef struct
+{
+    QString name;
+    QString schema;
+} SLSQLSchemaDef;
+
+
+//
+// Various helper utility functions
+//
 double slMoneyStrToValue(const QString &str);
 QString slMoneyValueToStr(double val);
 QString slMoneyValueToStrSign(double val);
+
 QString slCleanupStr(const QString &str);
+
 const QDateTime slDateTimeToLocal(const QDateTime &val);
 const QString slDateTimeToStr(const QDateTime &val);
 
 void slLog(const QString &mtype, const QString &msg);
 int slErrorMsg(const QString &title, const QString &msg);
+
 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false);
+bool slConditionallyCreateSQLTables(QSqlDatabase &db, const SLSQLSchemaDef *schema, const int nschema);
 
 
 #endif // SL_UTIL_H