diff src/util.cpp @ 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 4f947840c806
children eadffc38ab43
line wrap: on
line diff
--- 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;
+}