Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (4,82 KB) Statistiken
| Zweig: | Revision:
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();

}

Database::Database(QString databaseType, QString connectionName, QString databasename) {
426974c6 Christian Ehringfeld
this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
81c23b56 Christian Ehringfeld
this->connectionName = connectionName;
this->database.setDatabaseName(databasename);
this->init();
}

426974c6 Christian Ehringfeld
Database::Database(QString databaseType, QString connectionName, QString hostname, QString databasename,
QString username, QString password, qint64 port) {
this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
81c23b56 Christian Ehringfeld
this->connectionName = connectionName;
this->database.setHostName(hostname);
this->database.setDatabaseName(databasename);
this->database.setUserName(username);
this->database.setPassword(password);
this->database.setPort(port);
this->init();
}

void Database::init() {
this->database.open();
this->supportTransactions = this->database.driver()->hasFeature(QSqlDriver::Transactions);
}

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) {
81c23b56 Christian Ehringfeld
this->database.transaction();
QSqlQuery sqlquery = QSqlQuery(this->database);
sqlquery.exec(query);
426974c6 Christian Ehringfeld
if (!this->database.commit()) {
81c23b56 Christian Ehringfeld
this->database.rollback();
}
} else {
rc = this->exec(query);
}
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));
}
426974c6 Christian Ehringfeld
if (!this->database.commit()) {
81c23b56 Christian Ehringfeld
this->database.rollback();
} else {
ok = true;
}
} else {
ok = this->exec(queries);
}
return ok;
}

bool Database::transaction(QSqlQuery &query) {
this->database.transaction();
query.exec();
426974c6 Christian Ehringfeld
if (!this->database.commit()) {
81c23b56 Christian Ehringfeld
this->database.rollback();
return false;
}
return true;
}

bool Database::transaction(QList<QSqlQuery> &queries) {
this->database.transaction();
QSqlQuery q;
for (int var = 0; var < queries.size(); ++var) {
q = queries.at(var);
q.exec();
}
426974c6 Christian Ehringfeld
if (!this->database.commit()) {
81c23b56 Christian Ehringfeld
this->database.rollback();
return false;
}
return true;
}

bool Database::exec(QString query) {
this->database.transaction();
QSqlQuery q = QSqlQuery(this->database);
q.exec(query);
426974c6 Christian Ehringfeld
if (!this->database.commit()) {
81c23b56 Christian Ehringfeld
this->database.rollback();
return false;
}
return true;
}

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));
426974c6 Christian Ehringfeld
if (!ok) {
81c23b56 Christian Ehringfeld
break;
}
}
return ok;
}

bool Database::exec(QSqlQuery query) {
return query.exec();
}

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();
426974c6 Christian Ehringfeld
if (!ok) {
81c23b56 Christian Ehringfeld
break;
}
}
return ok;
}

bool Database::select(QSqlQuery &query) {
query.setForwardOnly(true);
return query.exec();
}

QSqlQuery Database::select(const QString &query) {
QSqlQuery q = QSqlQuery(this->database);
q.exec(query);
return q;
}

QSqlDatabase Database::getDatabase() {
return this->database;
}