# HG changeset patch # User Matti Hamalainen # Date 1525780817 -10800 # Node ID 4d2b37a0acf23d2fdd077d4cce66ec49e2db08e6 # Parent ede0fde2ae6e2146afcb06e82511600e88b974d4 Move SQL table creation from schema array to util-module. diff -r ede0fde2ae6e -r 4d2b37a0acf2 src/main.cpp --- 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

"). - arg(db.lastError().text()) - ); - return 1; - } - - query.finish(); - } + slErrorMsg( + QObject::tr("Tietokantataulua ei voitu luoda"), + QObject::tr("Virhe: %1

"). + arg(db.lastError().text()) + ); + return 1; } // diff -r ede0fde2ae6e -r 4d2b37a0acf2 src/main.h --- 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 diff -r ede0fde2ae6e -r 4d2b37a0acf2 src/util.cpp --- 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; +} diff -r ede0fde2ae6e -r 4d2b37a0acf2 src/util.h --- 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