changeset 253:eadffc38ab43

Add support for initial INSERT statements for database creation in schema array.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 08 May 2018 15:00:41 +0300
parents 4d2b37a0acf2
children 0e0ad52994ca
files src/main.cpp src/util.cpp src/util.h
diffstat 3 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.cpp	Tue May 08 15:00:17 2018 +0300
+++ b/src/main.cpp	Tue May 08 15:00:41 2018 +0300
@@ -39,7 +39,8 @@
 
         "added DATETIME NOT NULL,"
         "updated DATETIME NOT NULL"
-        )
+        ),
+        { }
     },
     {
         QStringLiteral("transactions"),
@@ -48,7 +49,8 @@
         "person INT NOT NULL, "
         "value REAL, "
         "added DATETIME NOT NULL"
-        )
+        ),
+        { }
     },
 };
 
--- a/src/util.cpp	Tue May 08 15:00:17 2018 +0300
+++ b/src/util.cpp	Tue May 08 15:00:41 2018 +0300
@@ -158,6 +158,28 @@
                 return false;
 
             tcreate.finish();
+
+            // If any inserts are specified, do them
+            for (int n = 0; n < SQL_MAX_SCHEMA_INSERTS; n++)
+            {
+                const QString str = table.inserts[n];
+                if (!str.isEmpty())
+                {
+                    QSqlQuery insert(db);
+                    sql =
+                        QStringLiteral("INSERT INTO %1 VALUES(%2)").
+                        arg(table.name).
+                        arg(str);
+
+                    insert.exec(sql);
+
+                    if (!slCheckAndReportSQLError(
+                        sql, insert.lastError(), true))
+                        return false;
+
+                    insert.finish();
+                }
+            }
         }
     }
 
--- a/src/util.h	Tue May 08 15:00:17 2018 +0300
+++ b/src/util.h	Tue May 08 15:00:41 2018 +0300
@@ -20,6 +20,7 @@
 {
     QString name;
     QString schema;
+    QString inserts[SQL_MAX_SCHEMA_INSERTS];
 } SLSQLSchemaDef;