CivConnection.cpp 3.69 KB
Newer Older
1
#include "CivConnection.h"
刘乐's avatar
刘乐 committed
2 3
#include "CivCommonUtils.h"
#include <iostream>
4 5 6 7 8 9 10 11 12 13 14

CivConnection::CivConnection()
{

}

CivConnection::~CivConnection()
{

}

刘乐's avatar
刘乐 committed
15 16 17
bool CivConnection::update(const std::string& table,
    const std::map<std::string, std::string>& result,
    const std::string& where)
18
{
刘乐's avatar
刘乐 committed
19 20 21 22 23 24 25 26 27 28 29 30 31
    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);
刘乐's avatar
刘乐 committed
32 33
   
    if (!execSql(sql))
刘乐's avatar
刘乐 committed
34 35 36 37 38 39 40 41 42 43
        return false;

    return true;
}

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

刘乐's avatar
刘乐 committed
44
    std::string sql = "delete from "+table +" where "+where;
刘乐's avatar
刘乐 committed
45 46
    
    if (!execSql(sql))
刘乐's avatar
刘乐 committed
47 48 49 50 51
        return false;

    return true;
}

刘乐's avatar
刘乐 committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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);
  
刘乐's avatar
刘乐 committed
88
    
刘乐's avatar
刘乐 committed
89
    //2  执行插入操作
刘乐's avatar
刘乐 committed
90
    if (!execSql(sql))
刘乐's avatar
刘乐 committed
91 92 93 94 95 96
        return false;

    return true;

}

刘乐's avatar
刘乐 committed
97 98
bool CivConnection::insert(const std::string& table,
    const std::map<std::string, std::string>& result)
刘乐's avatar
刘乐 committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
{
    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);

刘乐's avatar
刘乐 committed
123
   
刘乐's avatar
刘乐 committed
124
    //2  执行插入操作
刘乐's avatar
刘乐 committed
125
    if (!execSql(sql))
刘乐's avatar
刘乐 committed
126 127 128 129 130
        return false;

    return true;
}

刘乐's avatar
刘乐 committed
131 132 133 134
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)
刘乐's avatar
刘乐 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148
{
    //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);
刘乐's avatar
刘乐 committed
149
    sql.append(" from ");
刘乐's avatar
刘乐 committed
150 151 152 153
    sql.append(table);
    
    if (!where.empty())
    {
刘乐's avatar
刘乐 committed
154
        sql.append(" where ");
刘乐's avatar
刘乐 committed
155 156
        sql.append(where);
    }
刘乐's avatar
刘乐 committed
157

刘乐's avatar
刘乐 committed
158
   
刘乐's avatar
刘乐 committed
159
    // 2 执行sql语句
刘乐's avatar
刘乐 committed
160
    if (!execSql(sql))
刘乐's avatar
刘乐 committed
161 162 163 164 165 166 167 168
    {
        return false;
    }

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

    return true;
169 170 171 172 173 174 175
}

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