const pool = require('../connection/mysqlConn')
const dbBase = require('./dbBase')

function Table(tableName) {
    if (!tableName || typeof tableName !== 'string' || !tableName.trim())
        throw new Error('error table name.')
    else this.tableName = tableName
}
const proto = Table.prototype

proto.add = function (data) {
    let insert = data ? 'SET' : ''
    if (data) {
        for (let key in data) {
            let value = data[key]
            if (value) {
                let set = ` ${key}='${value}',`
                insert += set
            }
        }
        if (insert.lastIndexOf(',') === insert.length - 1) insert = insert.substr(0, insert.length - 1)
    }

    let sqlStr = `INSERT INTO ${this.tableName} ${insert}`
    return dbBase.execute(sqlStr)
}

proto.find = function (query) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }

    let sqlStr = `SELECT * FROM ${this.tableName} ${where}`;
    return dbBase.execute(sqlStr);
}

proto.findOne = function (query) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }

    let sqlStr = `SELECT * FROM ${this.tableName} ${where} LIMIT 1`;
    return dbBase.execute(sqlStr);
}

proto.update = function (query, updateData) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }
    let updateStr = ''
    if (updateData) {
        for (let key in updateData) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            updateStr += condition
        }
    }
    let sqlStr = `UPDATE ${this.tableName} SET ${updateStr}`;
    return dbBase.execute(sqlStr);
    /**
     * UPDATE [LOW_PRIORITY] [IGNORE] tbl_name  
     * SET col_name1=expr1 [, col_name2=expr2 ...]  
     * [WHERE where_definition]  
     * [ORDER BY ...]  
     * [LIMIT row_count] 
     */
}

proto.remove = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.save = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.query = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

module.exports = Table
@@ -1766,21 +1766,24 @@
 pdateStr
+Arr
  = 
-''
+%5B%5D
 %0A    if 
@@ -1851,37 +1851,42 @@
     let value = 
-query
+updateData
 %5Bkey%5D%0A          
@@ -1945,20 +1945,25 @@
 pdateStr
- += 
+Arr.push(
 conditio
@@ -1955,32 +1955,33 @@
 r.push(condition
+)
 %0A        %7D%0A    %7D
@@ -1973,32 +1973,75 @@
         %7D%0A    %7D%0A
+    let updateStr = updateStrArr.join(',')%0A
     let sqlStr =
@@ -2083,16 +2083,25 @@
 dateStr%7D
+ $%7Bwhere%7D
 %60;%0A    r

修改update方法，该方法中拼接set语句的时候没有用逗号分割，updateData拼接的时候，updateData写的是query