BaseDAO.cpp Example File
pushCollector/10.0/pushCollector/src/dao/BaseDAO.cpp
#include "BaseDAO.hpp"
#include <QDebug>
BaseDAO::BaseDAO()
{
}
BaseDAO::~BaseDAO()
{
if (m_connection.isOpen()) {
QSqlDatabase::removeDatabase(DATABASE_PATH);
m_connection.removeDatabase(DATABASE_DRIVER);
}
}
QSqlDatabase& BaseDAO::SQLConnection()
{
if (m_connection.isOpen()) {
return m_connection;
}
if (QSqlDatabase::contains(DATABASE_CONNECTION_NAME)){
m_connection = QSqlDatabase::database(DATABASE_CONNECTION_NAME);
if (!m_connection.isOpen()){
qWarning() << "Could not open connection to database: " << m_connection.lastError().text();
}
} else {
m_connection = QSqlDatabase::addDatabase(DATABASE_DRIVER, DATABASE_CONNECTION_NAME);
m_connection.setDatabaseName(DATABASE_PATH);
if (!m_connection.isValid()) {
qDebug() << "Could not set data base name probably due to invalid driver.";
} else if (!m_connection.open()) {
qWarning() << "Could not open connection to database: " << m_connection.lastError().text();
}
}
return m_connection;
}