Guest User

Untitled

a guest
Dec 13th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. var Mysql = require('mysql').createClient({
  2. host: 'ensky.tw',
  3. user: 'comic',
  4. password: 'enskya04cj84j;3',
  5. // port: '/tmp/mysql.sock', // 使用sock連線
  6. port: '3306',
  7. database: 'comic'
  8. });
  9.  
  10. var DB = function(){
  11. this.cleanup = function(){
  12. this.ignore = false;
  13. }
  14. this.cleanup();
  15. };
  16.  
  17. DB.prototype = Mysql;
  18.  
  19. DB.Method('Param', function(param){
  20. var keys = [],
  21. vals = [],
  22. ques = [];
  23. for(x in param){
  24. keys.push(x);
  25. vals.push(param[x]);
  26. ques.push('?');
  27. }
  28. return {keys:keys, vals:vals, ques:ques};
  29. });
  30.  
  31. DB.Method('Insert', function(table, param, cb){
  32. cb = cb || function(){};
  33. self = this;
  34. var myparam = self.Param(param);
  35. var ignore = this.ignore == true ? 'IGNORE ' : '';
  36. var sql = 'INSERT '+ ignore +'INTO `'+ table +'` (`'+ myparam.keys.join('`,`') +'`) VALUES ('+ myparam.ques.join(',') +')';
  37. Mysql.query(sql, myparam.vals, cb);
  38. });
  39.  
  40. DB.Method('Replace', function(table, param, cb){
  41. cb = cb || function(){};
  42. self = this;
  43. var myparam = self.Param(param);
  44. var sql = 'REPLACE INTO `'+ table +'` (`'+ myparam.keys.join('`,`') +'`) VALUES ('+ myparam.ques.join(',') +')';
  45. Mysql.query(sql, myparam.vals, cb);
  46. });
  47.  
  48. DB.Method('Insert', function(table, param, cb){
  49. cb = cb || function(){};
  50. self = this;
  51. var myparam = self.Param(param);
  52. var ignore = this.ignore == true ? 'IGNORE ' : '';
  53. var sql = 'INSERT '+ ignore +'INTO `'+ table +'` (`'+ myparam.keys.join('`,`') +'`) VALUES ('+ myparam.ques.join(',') +')';
  54. Mysql.query(sql, myparam.vals, cb);
  55. });
  56.  
  57. module.exports = new DB();
Add Comment
Please, Sign In to add comment