Mon, 06 Jun 2022 20:19:27 +0200
Code cleanup in database functions.
4 | 1 | /** |
2 | * database.cpp is part of bmsapp. | |
3 | * | |
4 | * bmsapp is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * bmsapp is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | #include "database.h" | |
3 | 18 | |
19 | ||
256
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
20 | DataBase::DataBase(QObject *parent) : QObject(parent) {} |
6
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
21 | |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
22 | |
256
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
23 | DataBase::~DataBase() {} |
3 | 24 | |
6
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
25 | |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
26 | bool DataBase::openDataBase(bool develop) |
3 | 27 | { |
28 | QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); | |
29 | ||
6
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
30 | if (develop) |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
31 | settings.beginGroup("dbdev"); |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
32 | else |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
33 | settings.beginGroup("dbprod"); |
f8474f2c5db9
We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
34 | |
3 | 35 | qDebug() << settings.value("host").toString() << settings.value("port").toString() << settings.value("name").toString() << settings.value("user").toString() << settings.value("pass").toString(); |
36 | db = QSqlDatabase::addDatabase("QMYSQL"); | |
37 | db.setHostName(settings.value("host").toString()); | |
38 | db.setPort(settings.value("port").toInt()); | |
39 | db.setDatabaseName(settings.value("name").toString()); | |
40 | db.setUserName(settings.value("user").toString()); | |
41 | db.setPassword(settings.value("pass").toString()); | |
42 | ||
43 | if (db.open()) { | |
256
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
44 | qInfo() << "Database" << settings.value("name").toString() << "open success"; |
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
45 | settings.endGroup(); |
3 | 46 | return true; |
47 | } | |
256
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
48 | |
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
49 | qWarning() << "Database open failed" << settings.value("host").toString() << settings.value("port").toString() << settings.value("name").toString() << settings.value("user").toString(); |
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
50 | settings.endGroup(); |
c136bd15b084
Make use of new logging handler.
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
51 | return false; |
3 | 52 | } |
85 | 53 | |
3 | 54 | void DataBase::closeDataBase() |
55 | { | |
56 | db.close(); | |
57 | } | |
58 |