#include <QString>
#include <QStringList>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include "sdbmanager.h"

void SDBManager::addDatabase(const QString& _dbName,const QString& _host,const QString& _id, const QString& _pw, const QString& _db, const int& _port)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", _dbName);
    db.setHostName(_host);
    db.setUserName(_id);
    db.setPassword(_pw);
    db.setDatabaseName(_db);
    db.setPort(_port);
}


void SDBManager::addDatabase(const QString& _dbName, const E_DATABASE& _eDatabase, const QString& _host, const QString& _id, const QString& _pw, const QString& _db, const int& _port)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", _dbName);
    db.setHostName(_host);
    db.setUserName(_id);
    db.setPassword(_pw);
    db.setDatabaseName(_db);
    db.setPort(_port);
    m_mapDBname.insert(_eDatabase, _dbName);
}

QSqlDatabase SDBManager::getDBConnection(const QString& _dbName) const
{
    return QSqlDatabase::database(_dbName);
}

QSqlDatabase SDBManager::getDBConnection(const E_DATABASE& _eDatabase) const
{
    return QSqlDatabase::database(m_mapDBname.value(_eDatabase));
}

bool SDBManager::open(const QString& _dbName)
{
    QSqlDatabase db = QSqlDatabase::database(_dbName);
    return db.open();
}

bool SDBManager::open(const E_DATABASE& _eDatabase)
{
    QSqlDatabase db = QSqlDatabase::database(m_mapDBname.value(_eDatabase));
    return db.open();
}

void SDBManager::close(const E_DATABASE& _eDatabase)
{
    QSqlDatabase db = QSqlDatabase::database(m_mapDBname.value(_eDatabase));
    db.close();
}

void SDBManager::close(const QString& _dbName)
{
    QSqlDatabase db = QSqlDatabase::database(_dbName);
    db.close();
}

void SDBManager::close()
{
    QStringList strlist = QSqlDatabase::connectionNames();
    foreach (const QString& str, strlist)
        QSqlDatabase::database(str).close();
}

QSqlQuery SDBManager::sqlQuery(const E_DATABASE& _eDatabase)
{
    QSqlQuery query(QSqlDatabase::database(m_mapDBname.value(_eDatabase)));
    return query;
}

QSqlQuery SDBManager::sqlQuery(const QString& _dbName)
{
    QSqlQuery query(QSqlDatabase::database(_dbName));
    return query;
}

bool SDBManager::reOpen(const QString& _dbName)
{
    close(_dbName);
    return open(_dbName);
}

bool SDBManager::reOpen(const E_DATABASE& _eDatabase)
{
    close(_eDatabase);
    return open(_eDatabase);
}

bool SDBManager::isConnectionError(const QSqlError& _error)
{
    if(_error.number() == 2013 || _error.number() == 2006 || _error.number() == 10054)
        return true;
    return false;
}

SDBManager::~SDBManager()
{
    QStringList strlist = QSqlDatabase::connectionNames();
    foreach (const QString& str, strlist)
    {
        QSqlDatabase db = QSqlDatabase::database(str);
        if(db.isOpen())
            db.close();
    }
    foreach (const QString& str, strlist)
        QSqlDatabase::removeDatabase(str);
}

QSqlDatabase SDBManager::operator[](const QString& _dbName) const
{
    return QSqlDatabase::database(_dbName);
}

QSqlDatabase SDBManager::operator[](const E_DATABASE& _eDatabase) const
{
    return QSqlDatabase::database(m_mapDBname.value(_eDatabase));
}
