139 lines
2.9 KiB
C++
139 lines
2.9 KiB
C++
#include "databasemanager.h"
|
|||
|
|
|
||
|
|
#include <QSqlError>
|
||
|
|
|
||
|
|
DatabaseManager &DatabaseManager::instance()
|
||
|
|
{
|
||
|
|
static DatabaseManager manager;
|
||
|
|
return manager;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager::DatabaseManager()
|
||
|
|
: m_config(DbConfig::defaultConfig())
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void DatabaseManager::setConfig(const DbConfig &config)
|
||
|
|
{
|
||
|
|
if (isOpen()) {
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
m_config = config;
|
||
|
|
}
|
||
|
|
|
||
|
|
DbConfig DatabaseManager::config() const
|
||
|
|
{
|
||
|
|
return m_config;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool DatabaseManager::open(QString *error)
|
||
|
|
{
|
||
|
|
QSqlDatabase db;
|
||
|
|
if (QSqlDatabase::contains(m_config.connectionName)) {
|
||
|
|
db = QSqlDatabase::database(m_config.connectionName);
|
||
|
|
} else {
|
||
|
|
db = QSqlDatabase::addDatabase(m_config.driverName, m_config.connectionName);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (db.isOpen()) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
db.setHostName(m_config.hostName);
|
||
|
|
db.setPort(m_config.port);
|
||
|
|
db.setDatabaseName(m_config.databaseName);
|
||
|
|
db.setUserName(m_config.userName);
|
||
|
|
db.setPassword(m_config.password);
|
||
|
|
|
||
|
|
if (!db.open()) {
|
||
|
|
m_lastErrorText = db.lastError().text();
|
||
|
|
if (error) {
|
||
|
|
*error = m_lastErrorText;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
m_lastErrorText.clear();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void DatabaseManager::close()
|
||
|
|
{
|
||
|
|
const QString connectionName = m_config.connectionName;
|
||
|
|
if (!QSqlDatabase::contains(connectionName)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName, false);
|
||
|
|
if (db.isValid() && db.isOpen()) {
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
QSqlDatabase::removeDatabase(connectionName);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool DatabaseManager::isOpen() const
|
||
|
|
{
|
||
|
|
if (!QSqlDatabase::contains(m_config.connectionName)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return QSqlDatabase::database(m_config.connectionName, false).isOpen();
|
||
|
|
}
|
||
|
|
|
||
|
|
QSqlDatabase DatabaseManager::database() const
|
||
|
|
{
|
||
|
|
if (!QSqlDatabase::contains(m_config.connectionName)) {
|
||
|
|
return QSqlDatabase();
|
||
|
|
}
|
||
|
|
return QSqlDatabase::database(m_config.connectionName);
|
||
|
|
}
|
||
|
|
|
||
|
|
QString DatabaseManager::lastErrorText() const
|
||
|
|
{
|
||
|
|
return m_lastErrorText;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool DatabaseManager::transaction(QString *error)
|
||
|
|
{
|
||
|
|
if (!open(error)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
QSqlDatabase db = database();
|
||
|
|
if (!db.transaction()) {
|
||
|
|
m_lastErrorText = db.lastError().text();
|
||
|
|
if (error) {
|
||
|
|
*error = m_lastErrorText;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool DatabaseManager::commit(QString *error)
|
||
|
|
{
|
||
|
|
QSqlDatabase db = database();
|
||
|
|
if (!db.commit()) {
|
||
|
|
m_lastErrorText = db.lastError().text();
|
||
|
|
if (error) {
|
||
|
|
*error = m_lastErrorText;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool DatabaseManager::rollback(QString *error)
|
||
|
|
{
|
||
|
|
QSqlDatabase db = database();
|
||
|
|
if (!db.rollback()) {
|
||
|
|
m_lastErrorText = db.lastError().text();
|
||
|
|
if (error) {
|
||
|
|
*error = m_lastErrorText;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|