nodejs拼接SQL实现快速调用得简单例子

nodejs拼接SQL实现快速调用得简单例子

var sqlfilter = {
     };
//数据格式,select情况,insert update delete 语法同理
//自用得写的比较个人化可能用模块更简单点把
var sql={
     
	function:'select',
	main:'select * from user',
	//where条件
	where:{
     
		username:
		password:
	}
	//分页条件
	limit:{
     
		page:
		limit:
	}
	
	
}
sqlfilter.builder = function(filters) {
     
    filters.reqdata = []
    filters.statement = ''

    switch (filters.function) {
     
        case 'select':
            //进行查询
            var whereStr = ''
            var limitStr = ''
            for (key in filters.where) {
     
                whereStr = whereStr + key + '=' + '?' + ' ' + 'and '
                filters.reqdata.push(filters.where[key])
            }
            whereStr = whereStr + '1=1 '
            if (filters.limit.page) {
     
                var limitStr = 'limit ' + String((filters.limit.page - 1) * filters.limit.limit) + ',' + String(filters.limit.limit)

            }
            filters.statement = filters.main + ' where ' + whereStr + limitStr
            console.log(filters)
            return (filters)
            break
        case 'insert':
            //插入
            var insertStr = '('
            for (key in filters.insert) {
     

                insertStr = insertStr + key + ','
                filters.reqdata.push(filters.insert[key])
            }

            insertStr = insertStr.substr(0, insertStr.length - 1)
            insertStr = insertStr + ')' + ' values('
            for (key in filters.insert) {
     

                insertStr = insertStr + '?' + ','

            }
            insertStr = insertStr.substr(0, insertStr.length - 1)
            insertStr = insertStr + ')'
            filters.statement = filters.main + ' ' + insertStr;

            return (filters)
            break;

        case 'update':
	    //更新
            var updateStr = ' set '
            var whereStr = ''
            for (key in filters.update) {
     
                updateStr = updateStr + key + '=?' + ' ' + ', '
                filters.reqdata.push(filters.update[key])
            }
            for (key in filters.where) {
     
                whereStr = whereStr + key + '=' + '?' + ' ' + 'and '
                filters.reqdata.push(filters.where[key])
            }
            whereStr = whereStr + '1=1 '
            updateStr = updateStr.substr(0, updateStr.length - 2) + ' '
            filters.statement = filters.main+updateStr + 'where ' + whereStr
            return (filters);
            break
        case 'delete':
          
            var whereStr = ''
            var limitStr = ''
            for (key in filters.where) {
     
                whereStr = whereStr + key + '=' + '?' + ' ' + 'and '
                filters.reqdata.push(filters.where[key])
            }
            whereStr = whereStr + '1=1 '
            filters.statement = filters.main + ' where ' + whereStr + limitStr
            console.log(filters)
            return (filters)
            break
        default:
            break;



    }
}

module.exports=sqlfilter

你可能感兴趣的:(nodejs开发,nodejs,sql,mysql)