Herunterladen als
root/src/database.cpp @ 77dbed04
81c23b56 | Christian Ehringfeld | /*
|
|
6899f814 | Christian Ehringfeld | * Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
81c23b56 | Christian Ehringfeld | ||
#include "database.h"
|
|||
45135a14 | Christian Ehringfeld | #include "logger.h"
|
|
#include <QDir>
|
|||
2d59cb88 | Christian Ehringfeld | #include "schema/mysqlschema.h"
|
|
9cf4747e | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | ||
3938a37e | Christian Ehringfeld | Database::Database(QSqlDatabase database, bool logQueries,
|
|
f12670e9 | Christian Ehringfeld | bool logErrors, MsgType type) {
|
|
81c23b56 | Christian Ehringfeld | this->database = database;
|
|
this->init();
|
|||
this->connectionName = this->database.connectionName();
|
|||
3938a37e | Christian Ehringfeld | this->initLogger(logQueries, logErrors, type);
|
|
81c23b56 | Christian Ehringfeld | }
|
|
e0e1ead8 | Christian Ehringfeld | Database::Database(QString databaseType, QString connectionName,
|
|
QString hostname,
|
|||
QString databasename,
|
|||
54538efb | Christian Ehringfeld | QString username, QString password, qint64 port,
|
|
f12670e9 | Christian Ehringfeld | bool logQueries, bool logErrors, QString databaseOptions, MsgType type) {
|
|
426974c6 | Christian Ehringfeld | this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
|
|
81c23b56 | Christian Ehringfeld | this->connectionName = connectionName;
|
|
9971e7d2 | Christian Ehringfeld | if (!hostname.isEmpty()) {
|
|
a82e4cea | Christian Ehringfeld | this->database.setHostName(hostname);
|
|
}
|
|||
9971e7d2 | Christian Ehringfeld | if (!databasename.isEmpty()) {
|
|
a82e4cea | Christian Ehringfeld | this->database.setDatabaseName(databasename);
|
|
}
|
|||
9971e7d2 | Christian Ehringfeld | if (!username.isEmpty()) {
|
|
a82e4cea | Christian Ehringfeld | this->database.setUserName(username);
|
|
}
|
|||
9971e7d2 | Christian Ehringfeld | if (!password.isEmpty()) {
|
|
a82e4cea | Christian Ehringfeld | this->database.setPassword(password);
|
|
}
|
|||
if (port != 0) {
|
|||
this->database.setPort(port);
|
|||
}
|
|||
2d59cb88 | Christian Ehringfeld | if (!databaseOptions.isEmpty()) {
|
|
9971e7d2 | Christian Ehringfeld | this->database.setConnectOptions(databaseOptions);
|
|
}
|
|||
81c23b56 | Christian Ehringfeld | this->init();
|
|
3938a37e | Christian Ehringfeld | this->initLogger(logQueries, logErrors, type);
|
|
}
|
|||
Logger *Database::getLogger() const {
|
|||
return logger;
|
|||
81c23b56 | Christian Ehringfeld | }
|
|
void Database::init() {
|
|||
this->database.open();
|
|||
e0e1ead8 | Christian Ehringfeld | this->supportTransactions = this->database.driver()->hasFeature(
|
|
66704054 | Christian Ehringfeld | QSqlDriver::Transactions);
|
|
45135a14 | Christian Ehringfeld | }
|
|
3938a37e | Christian Ehringfeld | void Database::initLogger(bool logQueries, bool logErrors,
|
|
f12670e9 | Christian Ehringfeld | MsgType type) {
|
|
45135a14 | Christian Ehringfeld | this->logQueries = logQueries;
|
|
this->logErrors = logErrors;
|
|||
3938a37e | Christian Ehringfeld | this->logger = new Logger(QDir::currentPath() + "/db" + this->connectionName +
|
|
".log", type);
|
|||
81c23b56 | Christian Ehringfeld | }
|
|
Database::~Database() {
|
|||
66704054 | Christian Ehringfeld | if (this->logger) {
|
|
delete this->logger;
|
|||
this->logger = nullptr;
|
|||
45135a14 | Christian Ehringfeld | }
|
|
426974c6 | Christian Ehringfeld | if (this->database.isOpen()) {
|
|
81c23b56 | Christian Ehringfeld | this->database.close();
|
|
}
|
|||
57d6da31 | Christian Ehringfeld | this->database = QSqlDatabase();
|
|
81c23b56 | Christian Ehringfeld | QSqlDatabase::removeDatabase(this->connectionName);
|
|
}
|
|||
QString Database::getConnectionName() {
|
|||
return this->connectionName;
|
|||
}
|
|||
QSqlQuery Database::getQuery() {
|
|||
return QSqlQuery(this->database);
|
|||
}
|
|||
QSqlQuery Database::getQuery(const QString &prepare) {
|
|||
QSqlQuery q = QSqlQuery(this->database);
|
|||
q.prepare(prepare);
|
|||
return q;
|
|||
}
|
|||
bool Database::transaction(const QStringList &queries) {
|
|||
bool ok = false;
|
|||
426974c6 | Christian Ehringfeld | if (this->supportTransactions) {
|
|
81c23b56 | Christian Ehringfeld | this->database.transaction();
|
|
QSqlQuery sqlquery = QSqlQuery(this->database);
|
|||
for (int var = 0; var < queries.size(); ++var) {
|
|||
sqlquery.exec(queries.at(var));
|
|||
45135a14 | Christian Ehringfeld | this->debugQuery(sqlquery);
|
|
81c23b56 | Christian Ehringfeld | }
|
|
11bbe9a6 | Christian Ehringfeld | ok = this->commitTransaction();
|
|
81c23b56 | Christian Ehringfeld | } else {
|
|
ok = this->exec(queries);
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
bool Database::transaction(QList<QSqlQuery> &queries) {
|
|||
11bbe9a6 | Christian Ehringfeld | this->startTransaction();
|
|
81c23b56 | Christian Ehringfeld | QSqlQuery q;
|
|
for (int var = 0; var < queries.size(); ++var) {
|
|||
q = queries.at(var);
|
|||
q.exec();
|
|||
d568923d | Christian Ehringfeld | this->debugQuery(q);
|
|
81c23b56 | Christian Ehringfeld | }
|
|
11bbe9a6 | Christian Ehringfeld | return this->commitTransaction();
|
|
}
|
|||
bool Database::commitTransaction() {
|
|||
d2c13492 | Christian Ehringfeld | return this->supportTransactions && this->database.commit();
|
|
95b60eb2 | Christian Ehringfeld | }
|
|
bool Database::rollbackTransaction() {
|
|||
1701766b | Christian Ehringfeld | this->logger->logMsg("Transaction rolled back!" +
|
|
this->database.lastError().text(), MsgType::WARNING);
|
|||
95b60eb2 | Christian Ehringfeld | return supportTransactions && this->database.rollback();
|
|
81c23b56 | Christian Ehringfeld | }
|
|
3820ae33 | Christian Ehringfeld | DatabaseType Database::getDatabaseType(QString s) {
|
|
if (s == "qmysql") {
|
|||
return DatabaseType::MYSQL;
|
|||
} else if (s == "qpgsql") {
|
|||
return DatabaseType::PGSQL;
|
|||
} else {
|
|||
return DatabaseType::SQLITE;
|
|||
}
|
|||
}
|
|||
57d6da31 | Christian Ehringfeld | Schema *Database::getSchema(DatabaseType db,
|
|
QSharedPointer<Database> database) {
|
|||
3820ae33 | Christian Ehringfeld | switch (db) {
|
|
35cf13b7 | Christian Ehringfeld | case DatabaseType::SQLITE:
|
|
57d6da31 | Christian Ehringfeld | return new SqliteSchema(database);
|
|
3820ae33 | Christian Ehringfeld | break;
|
|
66704054 | Christian Ehringfeld | // case PGSQL:
|
|
// return QSharedPointer<Schema>(new PgSqlSchema());
|
|||
// break;
|
|||
2d59cb88 | Christian Ehringfeld | case DatabaseType::MYSQL:
|
|
57d6da31 | Christian Ehringfeld | return new MysqlSchema(database);
|
|
2d59cb88 | Christian Ehringfeld | break;
|
|
3820ae33 | Christian Ehringfeld | default:
|
|
57d6da31 | Christian Ehringfeld | return new SqliteSchema(database);
|
|
3820ae33 | Christian Ehringfeld | break;
|
|
}
|
|||
}
|
|||
d568923d | Christian Ehringfeld | bool Database::exec(const QString &query) {
|
|
81c23b56 | Christian Ehringfeld | QSqlQuery q = QSqlQuery(this->database);
|
|
11bbe9a6 | Christian Ehringfeld | bool ok = q.exec(query);
|
|
d568923d | Christian Ehringfeld | this->debugQuery(q);
|
|
11bbe9a6 | Christian Ehringfeld | return ok;
|
|
81c23b56 | Christian Ehringfeld | }
|
|
426974c6 | Christian Ehringfeld | bool Database::exec(QStringList queries) {
|
|
81c23b56 | Christian Ehringfeld | QSqlQuery q = QSqlQuery(this->database);
|
|
bool ok = true;
|
|||
for (int var = 0; var < queries.size() && ok; ++var) {
|
|||
ok = q.exec(queries.at(var));
|
|||
d568923d | Christian Ehringfeld | this->debugQuery(q);
|
|
426974c6 | Christian Ehringfeld | if (!ok) {
|
|
81c23b56 | Christian Ehringfeld | break;
|
|
}
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
d568923d | Christian Ehringfeld | bool Database::exec(QSqlQuery &query) {
|
|
bool ok = query.exec();
|
|||
this->debugQuery(query);
|
|||
return ok;
|
|||
81c23b56 | Christian Ehringfeld | }
|
|
bool Database::exec(QList<QSqlQuery> queries) {
|
|||
bool ok = true;
|
|||
QSqlQuery q = QSqlQuery(this->database);
|
|||
for (int var = 0; var < queries.size() && ok; ++var) {
|
|||
q = queries.at(var);
|
|||
ok = q.exec();
|
|||
d568923d | Christian Ehringfeld | this->debugQuery(q);
|
|
426974c6 | Christian Ehringfeld | if (!ok) {
|
|
81c23b56 | Christian Ehringfeld | break;
|
|
}
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
d568923d | Christian Ehringfeld | void Database::debugQuery(const QSqlQuery &query) const {
|
|
66704054 | Christian Ehringfeld | if (this->logger && this->logErrors) {
|
|
this->logger->lastError(query, this->logQueries);
|
|||
45135a14 | Christian Ehringfeld | } else {
|
|
qDebug() << query.executedQuery();
|
|||
}
|
|||
d568923d | Christian Ehringfeld | }
|
|
81c23b56 | Christian Ehringfeld | bool Database::select(QSqlQuery &query) {
|
|
query.setForwardOnly(true);
|
|||
ed9ffe4a | Christian Ehringfeld | bool ok = query.exec();
|
|
d568923d | Christian Ehringfeld | this->debugQuery(query);
|
|
return ok;
|
|||
81c23b56 | Christian Ehringfeld | }
|
|
QSqlQuery Database::select(const QString &query) {
|
|||
QSqlQuery q = QSqlQuery(this->database);
|
|||
q.exec(query);
|
|||
d568923d | Christian Ehringfeld | this->debugQuery(q);
|
|
81c23b56 | Christian Ehringfeld | return q;
|
|
}
|
|||
11bbe9a6 | Christian Ehringfeld | void Database::startTransaction() {
|
|
this->database.transaction();
|
|||
}
|
|||
81c23b56 | Christian Ehringfeld | QSqlDatabase Database::getDatabase() {
|
|
return this->database;
|
|||
}
|