Express调用mssql驱动公共类dbHelper

2024-02-10 23:08

本文主要是介绍Express调用mssql驱动公共类dbHelper,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码:

/**
* Created by chaozhou on 2015/9/18.
*/
var mssql = require('mssql');
var user = "sa",
password = "sa",
server = "192.168.20.132",
database = "ggcms";

/**
* 默认config对象
* @type {{user: string, password: string, server: string, database: string, options: {encrypt: boolean}, pool: {min: number, idleTimeoutMillis: number}}}
*/
var config = {
user: user,
password: password,
server: server, // You can use 'localhost\\instance' to connect to named instance
database: database,
options: {
encrypt: true // Use this if you're on Windows Azure
},
pool: {
min: 0,
idleTimeoutMillis: 3000
}
};

/**
* 初始化config
* @param user
* @param password
* @param server
* @param database
*/
var initConfig = function (user, password, server, database) {
config = {
user: user,
password: password,
server: server, // You can use 'localhost\\instance' to connect to named instance
database: database,
options: {
encrypt: true // Use this if you're on Windows Azure
},
pool: {
min: 0,
idleTimeoutMillis: 3000
}
}
};

/**
* 恢复默认config
*/
var restoreDefaults = function () {
config = {
user: user,
password: password,
server: server, // You can use 'localhost\\instance' to connect to named instance
database: database,
options: {
encrypt: true // Use this if you're on Windows Azure
},
pool: {
min: 0,
idleTimeoutMillis: 3000
}
};
};

/**
* 执行原生Sql
* @param sql
* @params 参数对象(可为空,为空表示不加参数)
* @param callBack(err,recordset)
*/
var querySql = function (sql, params, callBack) {
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
if (params != "") {
for (var index in params) {
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
console.log("sql:" + sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute(params, function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) {
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

/**
* 带参数查询
* @param tableName 表名
* @param topNumber 前topNumber条
* @param whereSql whereSql
* @param params 查询参数对象(可为"",为""表示不加任何参数,如果此项为"",则whereSql必须也为"")
* @param orderSql 排序Sql(可为"",为""表示不排序)
* @param callBack
*/
var select = function (tableName, topNumber, whereSql, params, orderSql, callBack) {
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
var sql = "select * from " + tableName + " ";
if (topNumber != "") {
sql = "select top(" + topNumber + ") * from " + tableName + " ";
}
sql += whereSql + " ";
if (params != "") {
for (var index in params) {
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
sql += orderSql;
console.log(sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute(params, function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) {
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

//select("dbo.userInfo",3,"where id = @id",{id:1},"order by id",function(err,recordset){
// console.log(recordset);
//});

/**
* 查询所有
* @param tableName
* @param callBack
*/
var selectAll = function (tableName, callBack) {
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
var sql = "select * from " + tableName + " ";
console.log("sql:" + sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute("", function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) {
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

//selectAll("dbo.userTable",function(err,recordset){
// console.log(recordset);
//});

/**
* 添加
* @param addObj 添加对象(必填)
* @param tableName 表名
* @param callBack(err,recordset)
*/
var add = function(addObj,tableName,callBack){ //{id:3,userName:'admin'...} insert into dbo.tags(id,name) values(@id,@name)
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
var sql = "insert into " + tableName + "(";
if (addObj != "") {
for (var index in addObj) {
if (typeof addObj[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof addObj[index] == "string") {
ps.input(index, mssql.NVarChar);
} else if (typeof addObj[index] == "object") {
ps.input(index, mssql.DateTime);
}
sql += index + ",";
}
sql = sql.substr(0, sql.length - 1) + ")values(";
for (var index in addObj) {
sql = sql + "@" + index + ",";
}
}
sql = sql.substr(0, sql.length - 1) + ")";
console.log(sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute(addObj, function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) {
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

//add({"updateTime":new Date(),name:'awdaw,3awdwa,3434'},"dbo.template",function(err,recordset){
// console.log(recordset);
//});

//var add = function (addObj, tableName, callBack) {
// var connection = new mssql.Connection(config, function (err) {
// var ps = new mssql.PreparedStatement(connection);
// var sql = "insert into " + tableName + "(";
// if (addObj != "") {
// for (var index in addObj) {
// if (typeof addObj[index] == "number") {
// ps.input(index, mssql.Int);
// } else if (typeof addObj[index] == "string") {
// ps.input(index, mssql.NVarChar);
// }
// sql += index + ",";
// }
// sql = sql.substring(0, sql.length - 1) + ") values(";
// for (var index in addObj) {
// if (typeof addObj[index] == "number") {
// sql += addObj[index] + ",";
// } else if (typeof addObj[index] == "string") {
// sql += "'" + addObj[index] + "'" + ",";
// }
// }
// }
// sql = sql.substring(0, sql.length - 1) + ")";
// console.log("sql:" + sql);
// ps.prepare(sql, function (err) {
// if (err)
// console.log(err);
// ps.execute(addObj, function (err, recordset) {
// callBack(err, recordset);
// ps.unprepare(function (err) { //回收连接至连接池
// if (err)
// console.log(err);
// });
// });
// });
// });
// restoreDefaults();
//};



/**
* 修改
* @param updateObj 修改内容(必填)
* @param whereObj 修改对象(必填)
* @param tableName 表名
* @param callBack(err,recordset)
*/
var update = function(updateObj, whereObj, tableName, callBack){
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
var sql = "update " + tableName + " set ";
if (updateObj != "") {
for (var index in updateObj) {
if (typeof updateObj[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof updateObj[index] == "string") {
ps.input(index, mssql.NVarChar);
} else if (typeof updateObj[index] == "object") {
ps.input(index, mssql.DateTime);
}
sql += index + "=@" + index + ",";
}
sql = sql.substr(0, sql.length - 1) + " where ";
}
if (whereObj != "") {
for (var index in whereObj) {
if (typeof whereObj[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof whereObj[index] == "string") {
ps.input(index, mssql.NVarChar);
} else if (typeof whereObj[index] == "object") {
ps.input(index, mssql.DateTime);
}
sql += index + "=@" + index + ",";
}
}
sql = sql.substr(0, sql.length - 1);
var whereStr = JSON.stringify(whereObj);
var updateStr = JSON.stringify(updateObj);
whereObj = JSON.parse(updateStr.substr(0,updateStr.length -1) + "," + whereStr.substr(1,whereStr.length));
console.log(sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute(whereObj, function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) {
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

//update({name:"awdawd",context:'awdaw33434',updateTime:'2015-09-25'},{id:2},"dbo.template",function(err,recordset){
// console.log(recordset);
//});

//var update = function (updateObj, whereObj, tableName, callBack) {
// var connection = new mssql.Connection(config, function (err) {
// var ps = new mssql.PreparedStatement(connection);
// var sql = "update " + tableName + " set "; //update userTable set userName = 'admin',loginTimes = 12,password = 'admin'
// if (updateObj != "") {
// for (var index in updateObj) {
// if (typeof updateObj[index] == "number") {
// ps.input(index, mssql.Int);
// sql += index + "=" + updateObj[index] + ",";
// } else if (typeof updateObj[index] == "string") {
// ps.input(index, mssql.NVarChar);
// sql += index + "=" + "'" + updateObj[index] + "'" + ",";
// }
// }
// }
// sql = sql.substring(0, sql.length - 1) + " where ";
// if (whereObj != "") {
// for (var index in whereObj) {
// if (typeof whereObj[index] == "number") {
// ps.input(index, mssql.Int);
// sql += index + "=" + whereObj[index] + " and ";
// } else if (typeof whereObj[index] == "string") {
// ps.input(index, mssql.NVarChar);
// sql += index + "=" + "'" + whereObj[index] + "'" + " and ";
// }
// }
// }
// sql = sql.substring(0, sql.length - 5);
// console.log("sql:" + sql);
// ps.prepare(sql, function (err) {
// if (err)
// console.log(err);
// ps.execute(updateObj, function (err, recordset) {
// callBack(err, recordset);
// ps.unprepare(function (err) { //回收连接至连接池
// if (err)
// console.log(err);
// });
// });
// });
// });
// restoreDefaults();
//};



/**
* 删除
* @param deleteObj 删除对象
* @param tableName 表名
* @param callBack(err,recordset)
*/
var del = function (whereSql, params, tableName, callBack) {
var connection = new mssql.Connection(config, function (err) {
var ps = new mssql.PreparedStatement(connection);
var sql = "delete from " + tableName + " ";
if (params != "") {
for (var index in params) {
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
sql += whereSql;
console.log("sql:" + sql);
ps.prepare(sql, function (err) {
if (err)
console.log(err);
ps.execute(params, function (err, recordset) {
callBack(err, recordset);
ps.unprepare(function (err) { //回收连接至连接池
if (err)
console.log(err);
});
});
});
});
restoreDefaults();
};

//del("where id = @id",{id:16},"dbo.userTable",function(err,recordset){
// console.log(recordset);
//});

exports.initConfig = initConfig;
exports.config = config;
exports.del = del;
exports.select = select;
exports.update = update;
exports.querySql = querySql;
exports.restoreDefaults = restoreDefaults;
exports.selectAll = selectAll;
exports.add = add;

 

这篇关于Express调用mssql驱动公共类dbHelper的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/698231

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

一分钟带你上手Python调用DeepSeek的API

《一分钟带你上手Python调用DeepSeek的API》最近DeepSeek非常火,作为一枚对前言技术非常关注的程序员来说,自然都想对接DeepSeek的API来体验一把,下面小编就来为大家介绍一下... 目录前言免费体验API-Key申请首次调用API基本概念最小单元推理模型智能体自定义界面总结前言最

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

redis防止短信恶意调用的实现

《redis防止短信恶意调用的实现》本文主要介绍了在场景登录或注册接口中使用短信验证码时遇到的恶意调用问题,并通过使用Redis分布式锁来解决,具有一定的参考价值,感兴趣的可以了解一下... 目录1.场景2.排查3.解决方案3.1 Redis锁实现3.2 方法调用1.场景登录或注册接口中,使用短信验证码场

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

Idea调用WebService的关键步骤和注意事项

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选... 目录前言一、理解WebService的基本概念二、获取WSDL文件三、阅读和理解WSDL文件四、选择对接