mysqldb.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. 'use strict'
  7. module.exports={
  8. query:function(query,callback){
  9. MysqlPool.getConnection(function(err,con){
  10. con.query(query,function(err, results, fields) {
  11. con.release();
  12. if(callback && typeof(callback)=='function') callback(err,results,fields);
  13. });
  14. });
  15. }
  16. };
  17. var Module = function(table){
  18. let instance = this;
  19. let tableName = table;
  20. let method,where;
  21. let rows,value;
  22. instance.get = function(row){
  23. method = 'select';
  24. if(!row) row = '*';
  25. if(typeof row ==='string') rows = row.split(',');
  26. if(typeof row ==='object' && row.length) rows = row;
  27. return instance;
  28. };
  29. instance.update = function(row,val){
  30. method = 'update';
  31. if(!val) return instance;
  32. let rowArr,valArr,pair;
  33. if(typeof row ==='string') rowArr = row.split(',');
  34. if(typeof row ==='object' && row.length) rowArr = row;
  35. if(typeof val ==='string') valArr = val.split(',');
  36. if(typeof val ==='object' && val.length) valArr = val;
  37. if(rowArr.length == valArr.length){
  38. for(let i in rowArr){
  39. if(Number(valArr[i])==valArr[i]){
  40. valArr[i] = Number(valArr[i]);
  41. }else{
  42. valArr[i] = '"'+String(valArr[i])+'"';
  43. }
  44. }
  45. rows = rowArr.join(',');
  46. value = valArr.join(',');
  47. }
  48. return instance;
  49. };
  50. instance.del = function(){
  51. };
  52. instance.where = function(row,val){
  53. if(!row || !val) return instance;
  54. let rowArr,valArr;
  55. if(typeof row ==='string') rowArr = row.split(',');
  56. if(typeof row ==='object' && row.length) rowArr = row;
  57. if(typeof val ==='string') valArr = val.split(',');
  58. if(typeof val ==='object' && val.length) valArr = val;
  59. if(rowArr.length == valArr.length){
  60. where = [];
  61. for(let i in rowArr){
  62. if(Number(valArr[i])==valArr[i]){
  63. valArr[i] = Number(valArr[i]);
  64. }else{
  65. valArr[i] = '"'+String(valArr[i])+'"';
  66. }
  67. where.push(([rowArr[i],valArr[i]]).join('='));
  68. }
  69. }
  70. return instance;
  71. };
  72. instance.run = function(callback){
  73. let query = [],queryStr;
  74. let whereStr = where?('where '+where.join(' and ')):'';
  75. switch(method){
  76. case 'select':
  77. query = ['select',rows.join(','),'from',tableName,whereStr];
  78. break;
  79. case 'update':
  80. query = ['update',tableName,'set','(',rows.join(','),')','values(',value.join(','),')',whereStr];
  81. break;
  82. }
  83. queryStr = query.join(' ');
  84. MysqlDB.query(queryStr,function(err,result,fields){
  85. if(callback && typeof callback === 'function') callback(err,result);
  86. })
  87. }
  88. };