oujMySql.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var mysql = require('mysql');
  2. var Q = require('q');
  3. var configs = require('../../conf/config.inc.js');
  4. var AppErrors = require('./AppErrors.js');
  5. var objPools = {};
  6. /**
  7. * 基本类,提供增删改查
  8. * @param {string} dbKey
  9. */
  10. function oujMySql(dbKey) {
  11. this.dbKey = dbKey || 'default';
  12. return this;
  13. }
  14. oujMySql.prototype.getPool = function() {
  15. if (!objPools[this.dbKey]) {
  16. var config = configs.dbInfo[this.dbKey];
  17. if (config) {
  18. objPools[this.dbKey] = mysql.createPool(config);
  19. } else {
  20. throw new AppErrors.DbError("数据库:" + this.dbKey + "没有配置");
  21. }
  22. }
  23. return objPools[this.dbKey];
  24. }
  25. oujMySql.prototype.query = function(sql, values, cb) {
  26. var deferred = Q.defer();
  27. this.getPool().query(sql, values, function(err, result) {
  28. if (err) {
  29. deferred.reject(err);
  30. throw new AppErrors.DbError(err);
  31. } else {
  32. deferred.resolve(result);
  33. }
  34. });
  35. return deferred.promise.nodeify(cb);
  36. };
  37. /**
  38. * 更新数据
  39. * @param {string} sql
  40. * @param {function} cb function(array err, array res)
  41. */
  42. oujMySql.prototype.update = function(sql, cb) {
  43. return this.query(sql, []. cb);
  44. };
  45. /**
  46. * 获取一个值
  47. * @param {string} sql
  48. * @param cb function(array err, string one)
  49. * @return Promise
  50. */
  51. oujMySql.prototype.getOne = function(sql, cb) {
  52. var deferred = Q.defer();
  53. var allDeferred = this.getAll(sql, 1);
  54. allDeferred.then(function(rows) {
  55. var row = rows[0];
  56. var val = null;
  57. if (row) {
  58. for (var k in row) {
  59. val = row[k];
  60. break;
  61. }
  62. }
  63. deferred.resolve(val);
  64. }, deferred.reject);
  65. deferred.promise.nodeify(cb);
  66. return deferred.promise;
  67. };
  68. /**
  69. * 获取一列数据
  70. * @param sql
  71. * @param limit 行数
  72. * @param cb function(array err, array col)
  73. */
  74. oujMySql.prototype.getCol = function(sql, limit, cb) {
  75. if (typeof limit == 'number' && limit > 0) {
  76. sql += " LIMIT " + limit;
  77. } else if (typeof limit == 'function') {
  78. cb = limit;
  79. }
  80. var deferred = Q.defer();
  81. var allDeferred = this.getAll(sql, limit);
  82. allDeferred.then(function(rows) {
  83. var cols = [];
  84. if (rows[0] && rows.length) {
  85. for (var k in rows[0]) {
  86. var key = k;
  87. break;
  88. }
  89. for (var i = 0; i < rows.length; i++) {
  90. cols.push(rows[i][key]);
  91. }
  92. }
  93. deferred.resolve(cols);
  94. }, deferred.reject);
  95. deferred.promise.nodeify(cb);
  96. return deferred.promise;
  97. };
  98. /**
  99. * 获取一行数据
  100. * @param sql
  101. * @param cb function(array err, array row)
  102. */
  103. oujMySql.prototype.getRow = function(sql, cb) {
  104. var deferred = Q.defer();
  105. var allDeferred = this.getAll(sql, 1);
  106. allDeferred.then(function(rows) {
  107. deferred.resolve(rows[0] || null);
  108. }, deferred.reject);
  109. deferred.promise.nodeify(cb);
  110. return deferred.promise;
  111. };
  112. /**
  113. * 获取所有行数据
  114. * @param sql
  115. * @param limit 行数
  116. * @param cb function(array err, array row)
  117. */
  118. oujMySql.prototype.getAll = function(sql, limit, cb) {
  119. if (typeof limit == 'number' && limit > 0) {
  120. sql += " LIMIT " + limit;
  121. } else if (typeof limit == 'function') {
  122. cb = limit;
  123. }
  124. return this.query(sql, cb);
  125. };
  126. /**
  127. * 关闭连接池
  128. * @param cb
  129. */
  130. oujMySql.prototype.close = function(cb) {
  131. this.getPool().end(cb);
  132. };
  133. module.exports = oujMySql;