123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- var mysql = require('mysql');
- var Q = require('q');
- var configs = require('../../conf/config.inc.js');
- var AppErrors = require('./AppErrors.js');
- var objPools = {};
- /**
- * 基本类,提供增删改查
- * @param {string} dbKey
- */
- function oujMySql(dbKey) {
- this.dbKey = dbKey || 'default';
- return this;
- }
- oujMySql.prototype.getPool = function() {
- if (!objPools[this.dbKey]) {
- var config = configs.dbInfo[this.dbKey];
- if (config) {
- objPools[this.dbKey] = mysql.createPool(config);
- } else {
- throw new AppErrors.DbError("数据库:" + this.dbKey + "没有配置");
- }
- }
- return objPools[this.dbKey];
- }
- oujMySql.prototype.query = function(sql, values, cb) {
- var deferred = Q.defer();
- this.getPool().query(sql, values, function(err, result) {
- if (err) {
- deferred.reject(err);
- throw new AppErrors.DbError(err);
- } else {
- deferred.resolve(result);
- }
- });
- return deferred.promise.nodeify(cb);
- };
- /**
- * 更新数据
- * @param {string} sql
- * @param {function} cb function(array err, array res)
- */
- oujMySql.prototype.update = function(sql, cb) {
- return this.query(sql, []. cb);
- };
- /**
- * 获取一个值
- * @param {string} sql
- * @param cb function(array err, string one)
- * @return Promise
- */
- oujMySql.prototype.getOne = function(sql, cb) {
- var deferred = Q.defer();
- var allDeferred = this.getAll(sql, 1);
- allDeferred.then(function(rows) {
- var row = rows[0];
- var val = null;
- if (row) {
- for (var k in row) {
- val = row[k];
- break;
- }
- }
- deferred.resolve(val);
- }, deferred.reject);
- deferred.promise.nodeify(cb);
- return deferred.promise;
- };
- /**
- * 获取一列数据
- * @param sql
- * @param limit 行数
- * @param cb function(array err, array col)
- */
- oujMySql.prototype.getCol = function(sql, limit, cb) {
- if (typeof limit == 'number' && limit > 0) {
- sql += " LIMIT " + limit;
- } else if (typeof limit == 'function') {
- cb = limit;
- }
- var deferred = Q.defer();
- var allDeferred = this.getAll(sql, limit);
- allDeferred.then(function(rows) {
- var cols = [];
- if (rows[0] && rows.length) {
- for (var k in rows[0]) {
- var key = k;
- break;
- }
- for (var i = 0; i < rows.length; i++) {
- cols.push(rows[i][key]);
- }
- }
- deferred.resolve(cols);
- }, deferred.reject);
- deferred.promise.nodeify(cb);
- return deferred.promise;
- };
- /**
- * 获取一行数据
- * @param sql
- * @param cb function(array err, array row)
- */
- oujMySql.prototype.getRow = function(sql, cb) {
- var deferred = Q.defer();
- var allDeferred = this.getAll(sql, 1);
- allDeferred.then(function(rows) {
- deferred.resolve(rows[0] || null);
- }, deferred.reject);
- deferred.promise.nodeify(cb);
- return deferred.promise;
- };
- /**
- * 获取所有行数据
- * @param sql
- * @param limit 行数
- * @param cb function(array err, array row)
- */
- oujMySql.prototype.getAll = function(sql, limit, cb) {
- if (typeof limit == 'number' && limit > 0) {
- sql += " LIMIT " + limit;
- } else if (typeof limit == 'function') {
- cb = limit;
- }
- return this.query(sql, cb);
- };
- /**
- * 关闭连接池
- * @param cb
- */
- oujMySql.prototype.close = function(cb) {
- this.getPool().end(cb);
- };
- module.exports = oujMySql;
|