TableHelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var MySql = require('./oujMySql');
  2. /**
  3. * 基本类,提供增删改查
  4. * @param {string} tableName 表名
  5. * @param {string} dbKey
  6. */
  7. function TableHelper(tableName, dbKey) {
  8. this.tableName = tableName;
  9. this.dbKey = dbKey || 'default';
  10. this.objMySql = new MySql(this.dbKey);
  11. }
  12. TableHelper.prototype.setTableName = function(tableName) {
  13. this.tableName = tableName;
  14. };
  15. /**
  16. * 读取数据
  17. * @param {object} where 参数列表,特殊参数前缀
  18. * @param {object} keyWord 查询关键字['_field', '_where', '_limit', '_sortKey', '_sortDir', '_lockRow', '_tableName', '_groupby']
  19. * @return {object}
  20. */
  21. TableHelper.prototype.getOne = function(where, keyWord, cb) {
  22. var sql = this.buildSelectSql(where, keyWord);
  23. return this.objMySql.getOne(sql, cb);
  24. }
  25. TableHelper.prototype.getCol = function(where, keyWord, cb) {
  26. var sql = this.buildSelectSql(where, keyWord);
  27. return this.objMySql.getCol(sql, cb);
  28. }
  29. TableHelper.prototype.getRow = function(where, keyWord, cb) {
  30. var sql = this.buildSelectSql(where, keyWord);
  31. return this.objMySql.getRow(sql, cb);
  32. }
  33. TableHelper.prototype.getAll = function(where, keyWord, cb) {
  34. var sql = this.buildSelectSql(where, keyWord);
  35. return this.objMySql.getAll(sql, 0, cb);
  36. }
  37. TableHelper.prototype.updateObject = function(newData, where, cb) {
  38. if (!where) {
  39. throw new SQLException('updateObject没有传入where');
  40. }
  41. var _where = this.buildWhereSql(where);
  42. var sql = 'UPDATE `' + this.tableName + '` SET ';
  43. sql += this.buildValueSql(newData) + ' WHERE ' + _where;
  44. return this.objMySql.update(sql, cb);
  45. }
  46. TableHelper.prototype.delObject = function(where, cb) {
  47. if (!where) {
  48. throw new SQLException('updateObject没有传入where');
  49. }
  50. var _where = this.buildWhereSql(where);
  51. var sql = 'DELETE FROM `' + this.tableName + '` WHERE ' + _where;
  52. return this.objMySql.update(sql, cb);
  53. }
  54. TableHelper.prototype.replaceObject = function(newData, cb) {
  55. var sql = 'REPLACE INTO `' + this.tableName + '` SET ';
  56. sql += this.buildValueSql(newData);
  57. return this.objMySql.update(sql, cb);
  58. }
  59. TableHelper.prototype.addObject = function(data, cb) {
  60. var sql = 'INSERT `' + this.tableName + '` SET ';
  61. sql += this.buildValueSql(data);
  62. return this.objMySql.update(sql, cb);
  63. }
  64. TableHelper.prototype.close = function(cb) {
  65. return this.objMySql.close(cb);
  66. }
  67. TableHelper.prototype.buildValueSql = function(data, separator) {
  68. separator = separator || ',';
  69. var pool = this.objMySql.getPool();
  70. var values = [];
  71. for (var key in data) {
  72. values.push(pool.escapeId(key) + '=' + pool.escape(data[key]));
  73. }
  74. return values.join(separator);
  75. }
  76. TableHelper.prototype.buildWhereSql = function(where, keyWord) {
  77. keyWord = keyWord || {};
  78. var _sql = keyWord['_where'] || '1';
  79. var pool = this.objMySql.getPool();
  80. where = where || {};
  81. for (var key in where) {
  82. _sql += " AND " + pool.escapeId(key) + "=" + pool.escape(where[key]);
  83. }
  84. if (keyWord['_groupby']) {
  85. _sql += " GROUP BY " + keyWord['_groupby'];
  86. }
  87. if (keyWord['_sortKey']) {
  88. _sql += " ORDER BY " + keyWord['_sortKey'];
  89. if (keyWord['_sortDir']) {
  90. _sql += " " + keyWord['_sortDir'];
  91. }
  92. }
  93. return _sql;
  94. }
  95. TableHelper.prototype.buildSelectSql = function(where, keyWord) {
  96. keyWord = keyWord || {};
  97. var _field = keyWord['_field'] || '*';
  98. var pool = this.objMySql.getPool();
  99. var tableName = keyWord['_tableName'] || this.tableName;
  100. var _where = this.buildWhereSql(where, keyWord);
  101. var _sql = 'SELECT ' + _field + ' FROM ' + pool.escapeId(tableName) + ' WHERE ' + _where;
  102. return _sql;
  103. }
  104. /**
  105. * @type {TableHelper}
  106. */
  107. module.exports = TableHelper;