Herunterladen als
root/src/database.cpp @ 9e62667d
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"
|
|||
9cf4747e | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | ||
Database::Database(QSqlDatabase database) {
|
|||
this->database = database;
|
|||
this->init();
|
|||
this->connectionName = this->database.connectionName();
|
|||
}
|
|||
e0e1ead8 | Christian Ehringfeld | Database::Database(QString databaseType, QString connectionName,
|
|
QString hostname,
|
|||
QString databasename,
|
|||
426974c6 | Christian Ehringfeld | QString username, QString password, qint64 port) {
|
|
this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
|
|||
81c23b56 | Christian Ehringfeld | this->connectionName = connectionName;
|
|
a82e4cea | Christian Ehringfeld | if (hostname != QString("")) {
|
|
this->database.setHostName(hostname);
|
|||
}
|
|||
if (databasename != QString("")) {
|
|||
this->database.setDatabaseName(databasename);
|
|||
}
|
|||
if (username != QString("")) {
|
|||
this->database.setUserName(username);
|
|||
}
|
|||
if (password != QString("")) {
|
|||
this->database.setPassword(password);
|
|||
}
|
|||
if (port != 0) {
|
|||
this->database.setPort(port);
|
|||
}
|
|||
81c23b56 | Christian Ehringfeld | this->init();
|
|
}
|
|||
void Database::init() {
|
|||
this->database.open();
|
|||
e0e1ead8 | Christian Ehringfeld | this->supportTransactions = this->database.driver()->hasFeature(
|
|
QSqlDriver::Transactions);
|
|||
81c23b56 | Christian Ehringfeld | }
|
|
Database::~Database() {
|
|||
426974c6 | Christian Ehringfeld | if (this->database.isOpen()) {
|
|
81c23b56 | Christian Ehringfeld | this->database.close();
|
|
}
|
|||
QSqlDatabase::removeDatabase(this->connectionName);
|
|||
}
|
|||
QString Database::getConnectionName() {
|
|||
return this->connectionName;
|
|||
}
|
|||
bool Database::transaction(const QString &query) {
|
|||
bool rc = false;
|
|||
426974c6 | Christian Ehringfeld | if (supportTransactions) {
|
|
11bbe9a6 | Christian Ehringfeld | this->startTransaction();
|
|
81c23b56 | Christian Ehringfeld | QSqlQuery sqlquery = QSqlQuery(this->database);
|
|
sqlquery.exec(query);
|
|||
11bbe9a6 | Christian Ehringfeld | this->commitTransaction();
|
|
81c23b56 | Christian Ehringfeld | } else {
|
|
rc = this->exec(query);
|
|||
}
|
|||
d568923d | Christian Ehringfeld | qDebug() << "Executed Query:" << query;
|
|
81c23b56 | Christian Ehringfeld | return rc;
|
|
}
|
|||
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));
|
|||
}
|
|||
11bbe9a6 | Christian Ehringfeld | ok = this->commitTransaction();
|
|
81c23b56 | Christian Ehringfeld | } else {
|
|
ok = this->exec(queries);
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
bool Database::transaction(QSqlQuery &query) {
|
|||
11bbe9a6 | Christian Ehringfeld | this->startTransaction();
|
|
81c23b56 | Christian Ehringfeld | query.exec();
|
|
d568923d | Christian Ehringfeld | this->debugQuery(query);
|
|
11bbe9a6 | Christian Ehringfeld | return this->commitTransaction();
|
|
81c23b56 | Christian Ehringfeld | }
|
|
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() {
|
|||
719a0ba1 | Christian Ehringfeld | qDebug() << "Transaction rolled back!" << this->database.lastError().text();
|
|
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;
|
|||
}
|
|||
}
|
|||
35cf13b7 | Christian Ehringfeld | QSharedPointer<Schema> Database::getSchema(DatabaseType db,
|
|
e0e1ead8 | Christian Ehringfeld | QSharedPointer<Database> database) {
|
|
3820ae33 | Christian Ehringfeld | switch (db) {
|
|
35cf13b7 | Christian Ehringfeld | case DatabaseType::SQLITE:
|
|
3820ae33 | Christian Ehringfeld | return QSharedPointer<Schema>(new SqliteSchema(database));;
|
|
break;
|
|||
// case PGSQL:
|
|||
// return QSharedPointer<Schema>(new PgSqlSchema());
|
|||
// break;
|
|||
// case MYSQL:
|
|||
// return QSharedPointer<Schema>(new MysqlSchema());
|
|||
// break;
|
|||
default:
|
|||
return QSharedPointer<Schema>(new SqliteSchema(database));
|
|||
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 {
|
|
qDebug() << query.executedQuery();
|
|||
}
|
|||
81c23b56 | Christian Ehringfeld | bool Database::select(QSqlQuery &query) {
|
|
query.setForwardOnly(true);
|
|||
d568923d | Christian Ehringfeld | bool ok = query.exec();
|
|
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;
|
|||
}
|