#include "CSqlite3DBProc.h"
//#include "CharsetPorc.h"
#include "logproc.h"
#include <QString>

const std::string CSqlite3DBProc::m_sDBEncode = "Qmthoec!@#";
const int CSqlite3DBProc::m_nDBEncodeKeyNum = 10;

std::shared_ptr<CSqlite3DBProc> g_DBProcPtr = nullptr;
std::shared_ptr<CSqlite3DBProc> g_sysDBProcPtr = nullptr;
//#define MAX_COL_SIZE 10240000


CSqlite3DBProc::CSqlite3DBProc()
{
    m_pDB = NULL;
    m_bIsEof = false;
}

CSqlite3DBProc::~CSqlite3DBProc()
{
    CloseBD();
}

bool CSqlite3DBProc::OpenDB(std::string sFile)
{
    try
    {
        int res = sqlite3_open_v2(/*UTF8ToGBK*/(sFile.c_str())/*.c_str()*/, &m_pDB,
            SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);

        if(res != SQLITE_OK)
        {
            myServerLog() << QString::fromLocal8Bit("数据库异常,")<< "Can't open database:" << sqlite3_errmsg(m_pDB);
            return false;
        }
        else
        {
            myServerLog()<< QString::fromLocal8Bit("数据库正常启动,")<<"open database successfully";
        }

        if(sqlite3_key(m_pDB, m_sDBEncode.c_str(), m_nDBEncodeKeyNum) != SQLITE_OK ||
                sqlite3_rekey(m_pDB, m_sDBEncode.c_str(), m_nDBEncodeKeyNum) != SQLITE_OK)
        {
            myServerLog()<< QString::fromLocal8Bit("数据库异常,")<<"Open database with key Error";
            return false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< "Open database error:" << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::CloseBD()
{
    try
    {
        sqlite3_close(m_pDB);
        return true;
    }
    catch(...)
    {
        myServerLog()<< QString::fromLocal8Bit("数据库异常,")<<"Close database error:" << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::ExcuteSql(std::string sSql)
{
    try
    {
        char* errMsg;
        if(sqlite3_exec(m_pDB , /*GBKToUTF8*/(sSql.c_str())/*.c_str()*/, 0, 0, &errMsg) != SQLITE_OK)
        {
            myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("执行SQL出错:") << sSql.c_str() << "ErrorMsg:" << errMsg;
            return false;
        }
        return true;
    }
    catch(...)
    {
         myServerLog() << QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("执行SQL出错") << sqlite3_errmsg(m_pDB);
        return false;
    }
}


bool CSqlite3DBProc::QuerySql(std::string sSql, sqlite3_stmt** m_stmt)
{
    try
    {
        m_bIsEof = false;
        const char *zTail;
        if(sqlite3_prepare_v2(m_pDB, /*GBKToUTF8*/(sSql.c_str())/*.c_str()*/, /*GBKToUTF8*/(sSql/*.c_str()*/).length(), m_stmt, &zTail) !=SQLITE_OK)
        {
            myServerLog() << QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("执行SQL出错") << sSql.c_str() << "ErrorMsg:" << sqlite3_errmsg(m_pDB);
            return false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("执行SQL出错") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::BeginGetFields(sqlite3_stmt *m_stmt)
{
    return Next(m_stmt);
}

bool CSqlite3DBProc::EndGetFields(sqlite3_stmt *m_stmt)
{
    try
    {
        sqlite3_finalize(m_stmt);
        return true;
    }
    catch(...)
    {
        myServerLog()<< QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("EndGetFields 出错:") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::Eof()
{
    return m_bIsEof;
}

bool CSqlite3DBProc::Next(sqlite3_stmt *m_stmt)
{
    try
    {
        if( sqlite3_step(m_stmt) != SQLITE_ROW )
        {
            m_bIsEof = true;
        }
        else
        {
            m_bIsEof = false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("获取下一条记录出错:") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

int CSqlite3DBProc::GetFields(int nCol, int &nField, sqlite3_stmt *m_stmt)
{
    try
    {
        nField = sqlite3_column_int(m_stmt, nCol);
        return nField;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("获取记录出错:") << sqlite3_errmsg(m_pDB);
        return -1;
    }
}


__int64 CSqlite3DBProc::GetFields(int nCol, __int64 &nField, sqlite3_stmt *m_stmt)
{
    try
    {
        nField = sqlite3_column_int64(m_stmt, nCol);
        return nField;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("获取记录出错:") << sqlite3_errmsg(m_pDB);
        return -1;
    }
}

double CSqlite3DBProc::GetFields(int nCol, double &fField, sqlite3_stmt *m_stmt)
{
    try
    {
        fField = sqlite3_column_double(m_stmt, nCol);
        return fField;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("获取记录出错:") << sqlite3_errmsg(m_pDB);
        return -1;
    }
}

std::string CSqlite3DBProc::GetFields(int nCol, std::string &sField, sqlite3_stmt *m_stmt)
{
    try
    {
        sField = (char*)sqlite3_column_text(m_stmt, nCol);
        return sField;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("获取记录出错:") << sqlite3_errmsg(m_pDB);
        return "";
    }
}

bool CSqlite3DBProc::BeginTransaction()
{
    try
    {
        char *errMsg;
        if(sqlite3_exec(m_pDB,"begin transaction;",0,0, &errMsg) != SQLITE_OK)
        {
            myServerLog()<< QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("启动事务出错，ErrorMsg:") << errMsg;
            return false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog()<< QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("启动事务出错，ErrorMsg:") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::CommitTransaction()
{
    try
    {
        char *errMsg;
        if(sqlite3_exec(m_pDB,"commit transaction;",0,0, &errMsg) != SQLITE_OK)
        {
            myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("提交事务出错，ErrorMsg:") << errMsg;
            return false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("提交事务出错，ErrorMsg:") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

bool CSqlite3DBProc::RollbakTransaction()
{
    try
    {
        char *errMsg;
        if(sqlite3_exec(m_pDB,"rollback transaction;",0,0, &errMsg) != SQLITE_OK)
        {
            myServerLog()<< QString::fromLocal8Bit("数据库异常,") << QString::fromLocal8Bit("回滚事务出错，ErrorMsg:") << errMsg;
            return false;
        }
        return true;
    }
    catch(...)
    {
        myServerLog() << QString::fromLocal8Bit("数据库异常,")<< QString::fromLocal8Bit("回滚事务出错，ErrorMsg:") << sqlite3_errmsg(m_pDB);
        return false;
    }
}

const std::string CSqlite3DBProc::ErrMsg()
{
    return sqlite3_errmsg(m_pDB);
}
