#include "CivConnection.h"
#include "CivCommonUtils.h"
#include <iostream>

CivConnection::CivConnection()
{

}

CivConnection::~CivConnection()
{

}

bool CivConnection::update(const std::string& table, const std::map<std::string, std::string>& result, const std::string& where)
{
    std::string sql = "update " + table + "set ";

    std::map<std::string, std::string>::const_iterator iter = result.begin();
    for (; iter != result.end(); iter++)
    {
        std::string filedName = iter->first;
        std::string fieldVallue = iter->second;

        sql.append(filedName + "=" + fieldVallue);
        sql.append(",");
    }

    sql = sql.substr(0, sql.length() - 1);
    std::string utf8Sql = CivCommonUtils::string_To_UTF8(sql);
    if (!execSql(sql))
        return false;

    return true;
}

bool CivConnection::del(const std::string& table, const std::string& where)
{
    if (table.empty() || where.empty())
        return false;

    std::string sql = "delete from "+table +"where "+where;
    std::string utf8Sql = CivCommonUtils::string_To_UTF8(sql);
    if (!execSql(sql))
        return false;

    return true;
}

bool CivConnection::insertBulk(const std::string& table,
    const std::vector<std::string>& fields,
    const std::vector<std::vector<std::string>>& result)
{
    if (table.empty() || result.size() <= 0)
        return false;

    // 拼接sql语句
    std::string sql = "INSERT INTO  \"" + table + "\"(";
    size_t total = fields.size();
    for (int i = 0; i < total; i++)
    {
        std::string  field = fields[i];
        sql.append(field);
        sql.append(",");
    }
    sql = sql.substr(0, sql.length() - 1);
    sql.append(") values");

    // 拼接值
    size_t rows = result.size();
    for (int i =0;i<rows;i++)
    {
        sql.append("(");
        std::vector<std::string> filedvals = result[i];
        size_t cols = filedvals.size();
        for (int j = 0; j < cols; j++)
        {
            sql.append(filedvals[j]);
            sql.append(",");
        }
        sql = sql.substr(0, sql.length() - 1);
        sql.append("),");
    }
    sql = sql.substr(0, sql.length() - 1);
  
    std::cout << sql << std::endl;
    std::string utf8Sql = CivCommonUtils::string_To_UTF8(sql);
    //2  执行插入操作
    if (!execSql(utf8Sql))
        return false;

    return true;

}

bool CivConnection::insert(const std::string& table, const std::map<std::string, std::string>& result)
{
    if (table.empty() || result.size() <= 0)
        return false;

    // 拼接sql语句
    std::string sql = "INSERT INTO  \""+table+"\"(";
    std::string vals = "(";
    for (auto iter = result.begin(); iter != result.end(); iter++)
    {
       std::string field =  iter->first;
       std::string fieldValue = iter->second;

       sql.append(field);
       sql.append(",");

       vals.append(fieldValue);
       vals.append(",");
    }
    sql = sql.substr(0, sql.length() - 1);
    vals = vals.substr(0, vals.length() - 1);

    sql.append(") VALUES");
    sql.append(vals);

    std::string utf8Sql = CivCommonUtils::string_To_UTF8(sql);
    //2  执行插入操作
    if (!execSql(utf8Sql))
        return false;

    return true;
}

bool CivConnection::query(const std::string& table, const std::vector<std::string>& fields, std::vector<std::map<std::string,std::string>>& result, const std::string& where)
{
    //1 拼接sql语句
    if (fields.size() <= 0 || table.empty())
        return false;

    std::string sql = "select ";
    int total = fields.size();
    for (int i = 0; i < total; i++)
    {
        sql.append(fields[i]);
        sql.append(",");
    }

    sql = sql.substr(0, sql.length() - 1);
    sql.append(" from ");
    sql.append(table);
    
    if (!where.empty())
    {
        sql.append(" where ");
        sql.append(where);
    }
    std::cout << sql << std::endl;
    std::string utf8Sql = CivCommonUtils::string_To_UTF8(sql);
    // 2 执行sql语句
    if (!execSql(utf8Sql))
    {
        return false;
    }

    // 3 获取执行结果集
    queryResult(result);

    return true;
}

std::string CivConnection::getLastError() const
{
	return mLastError;
}

