# HG changeset patch # User Matti Hamalainen # Date 1525725232 -10800 # Node ID 9626307f3eebd7ee5277ca33349703073d16747c # Parent d8c3043c00a013e9bd4a963bd8cb9ffd54dd5211 Streamline SQL table creation. diff -r d8c3043c00a0 -r 9626307f3eeb src/main.cpp --- a/src/main.cpp Mon May 07 20:07:39 2018 +0300 +++ b/src/main.cpp Mon May 07 23:33:52 2018 +0300 @@ -35,6 +35,44 @@ } settings; +// +// SQL schema / table definitions +// +typedef struct +{ + QString name; + QString schema; +} SLSQLSchemaDef; + + +static const SLSQLSchemaDef slSQLSchemaData[] = +{ + { + QStringLiteral("people"), + QStringLiteral( + "id INTEGER PRIMARY KEY," + + "first_name VARCHAR(" SL_STR(SQL_LEN_FIRST_NAME) ") NOT NULL," + "last_name VARCHAR(" SL_STR(SQL_LEN_LAST_NAME) ") NOT NULL," + "extra_info VARCHAR(" SL_STR(SQL_LEN_EXTRA_INFO) ")," + + "added DATETIME NOT NULL," + "updated DATETIME NOT NULL" + ) + }, + { + QStringLiteral("transactions"), + QStringLiteral( + "id INTEGER PRIMARY KEY, " + "person INT NOT NULL, " + "value REAL, " + "added DATETIME NOT NULL" + ) + }, +}; + +static const int nslSQLSchemaData = sizeof(slSQLSchemaData) / sizeof(slSQLSchemaData[0]); + // // Convert QString to a double value, replacing comma @@ -327,53 +365,41 @@ return 1; } - QSqlQuery query; - if (!db.tables().contains("people")) + // + // Create tables + // + for (int ntable = 0; ntable < nslSQLSchemaData; ntable++) { - query.exec(QStringLiteral( - "CREATE TABLE people (id INTEGER PRIMARY KEY, " - "first_name VARCHAR(%1) NOT NULL, " - "last_name VARCHAR(%2) NOT NULL, " - "extra_info VARCHAR(%3), " - "added DATETIME NOT NULL, " - "updated DATETIME NOT NULL)"). - arg(SQL_LEN_FIRST_NAME). - arg(SQL_LEN_LAST_NAME). - arg(SQL_LEN_EXTRA_INFO)); + 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("CREATE TABLE people", query.lastError(), true)) - { - slErrorMsg( - QObject::tr("Tietokantataulua ei voitu luoda"), - QObject::tr("Virhe: %1

"). - arg(db.lastError().text()) - ); - return 1; + 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(); } } - if (!db.tables().contains("transactions")) - { - query.exec(QStringLiteral( - "CREATE TABLE transactions (" - "id INTEGER PRIMARY KEY, " - "person INT NOT NULL, " - "value REAL, " - "added DATETIME NOT NULL)")); - - if (!slCheckAndReportSQLError("CREATE TABLE transactions", query.lastError(), true)) - { - slErrorMsg( - QObject::tr("Tietokantataulua ei voitu luoda"), - QObject::tr("Virhe: %1

"). - arg(db.lastError().text()) - ); - return 1; - } - } - - query.finish(); - + // + // Execute main app + // SyntilistaMainWindow swin; swin.show(); return sapp.exec(); diff -r d8c3043c00a0 -r 9626307f3eeb src/main.h --- a/src/main.h Mon May 07 20:07:39 2018 +0300 +++ b/src/main.h Mon May 07 23:33:52 2018 +0300 @@ -44,6 +44,10 @@ #define BACKUP_HTTP 1 // HTTP(s) POST to a PHP script +// Helper macros +#define SL_STR_HELPER(x) #x +#define SL_STR(x) SL_STR_HELPER(x) + // // Person information record