annotate src/util.cpp @ 254:0e0ad52994ca

Fix 10L.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 10 May 2018 01:16:30 +0300
parents eadffc38ab43
children 55581d90c55d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
246
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 // Syntilista - debt list/management database program
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 // Programmed and designed by Matti Hämäläinen <ccr@tnsp.org>
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 // (C) Copyright 2017-2018 Tecnic Software productions (TNSP)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 // Distributed under 3-clause BSD style license, refer to
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 // included file "COPYING" for exact terms.
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "main.h"
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "util.h"
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <QMessageBox>
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 // Convert QString to a double value, replacing comma
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 double slMoneyStrToValue(const QString &str)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 QString str2 = str;
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 return str2.replace(",", ".").toDouble();
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 // Convert double value to formatted QString
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 QString slMoneyValueToStr(double val)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 return QStringLiteral("%1").arg(val, 1, 'f', 2);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 QString slMoneyValueToStrSign(double val)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 return QStringLiteral("%1%2").
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 arg(val > 0 ? "+" : "").
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 arg(val, 1, 'f', 2);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 // Trim and cleanup given QString (removing double whitespace etc.)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 QString slCleanupStr(const QString &str)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 return str.simplified().trimmed();
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 // Manipulate given QDateTime value to get desired
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 // correct timestamp.
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 const QDateTime slDateTimeToLocal(const QDateTime &val)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 QDateTime tmp = val;
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 tmp.setOffsetFromUtc(0);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 return tmp.toLocalTime();
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 // Return a string representation of given QDateTime
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 // converted to local time.
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 const QString slDateTimeToStr(const QDateTime &val)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 // return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm"));
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 return slDateTimeToLocal(val).toString(QStringLiteral("dd.MM.yyyy hh:mm"));
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 // Error logging
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 void slLog(const QString &mtype, const QString &msg)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE;
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 QFile fh(filename);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 QTextStream out(&fh);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 out <<
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 << " [" << mtype << "]: " << msg << "\n";
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 fh.close();
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 // Display an error dialog with given title and message
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 //
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 int slErrorMsg(const QString &title, const QString &msg)
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 QMessageBox dlg;
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 slLog("ERROR", msg);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 dlg.setText(title);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 dlg.setInformativeText(msg);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 dlg.setTextFormat(Qt::RichText);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 dlg.setIcon(QMessageBox::Critical);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 dlg.setStandardButtons(QMessageBox::Ok);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 dlg.setDefaultButton(QMessageBox::Ok);
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 return dlg.exec();
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
43a5e09bb832 Split some utility functions to util.{h,cpp}
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
248
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
112 //
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
113 // Check if an SQL error has occured (for given QSqlError) and
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
114 // report it to stdout if so. Return "false" if error has occured,
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
115 // true otherwise.
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
116 //
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
117 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report)
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
118 {
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
119 if (err.isValid())
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
120 {
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
121 // If an error has occured, log it
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
122 slLog("ERROR",
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
123 QStringLiteral("SQL %1: %2").
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
124 arg(where).arg(err.text()));
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
125 return false;
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
126 }
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
127 else
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
128 {
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
129 // If no error, but event reporting requested, log it
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
130 if (report)
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
131 {
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
132 slLog("NOTE",
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
133 QStringLiteral("SQL OK %1").arg(where));
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
134 }
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
135 return true;
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
136 }
4f947840c806 Oops, forgot to move slCheckAndReportSQLError() to util.cpp. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
137 }
252
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
138
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
139
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
140 bool slConditionallyCreateSQLTables(QSqlDatabase &db, const SLSQLSchemaDef *schema, const int nschema)
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
141 {
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
142 for (int ntable = 0; ntable < nschema; ntable++)
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
143 {
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
144 const SLSQLSchemaDef &table = schema[ntable];
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
145 if (!db.tables().contains(table.name))
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
146 {
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
147 // Attempt to create the table
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
148 QSqlQuery tcreate(db);
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
149 QString sql =
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
150 QStringLiteral("CREATE TABLE %1 (%2)").
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
151 arg(table.name).
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
152 arg(table.schema);
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
153
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
154 tcreate.exec(sql);
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
155
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
156 if (!slCheckAndReportSQLError(
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
157 sql, tcreate.lastError(), true))
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
158 return false;
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
159
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
160 tcreate.finish();
253
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
161
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
162 // If any inserts are specified, do them
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
163 for (int n = 0; n < SQL_MAX_SCHEMA_INSERTS; n++)
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
164 {
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
165 const QString str = table.inserts[n];
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
166 if (!str.isEmpty())
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
167 {
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
168 QSqlQuery insert(db);
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
169 sql =
254
0e0ad52994ca Fix 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 253
diff changeset
170 QStringLiteral("INSERT INTO %1 VALUES (%2)").
253
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
171 arg(table.name).
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
172 arg(str);
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
173
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
174 insert.exec(sql);
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
175
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
176 if (!slCheckAndReportSQLError(
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
177 sql, insert.lastError(), true))
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
178 return false;
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
179
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
180 insert.finish();
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
181 }
eadffc38ab43 Add support for initial INSERT statements for database creation in schema array.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
182 }
252
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
183 }
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
184 }
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
185
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
186 return true;
4d2b37a0acf2 Move SQL table creation from schema array to util-module.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
187 }