TableHelper.js 3.6 KB

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