Advertisement
Guest User

Untitled

a guest
May 8th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. var db = function () {
  2.  
  3. this.mysql = require("mysql");
  4. var jsesc = require('jsesc');
  5.  
  6. this.con = null;
  7.  
  8.  
  9. this.connectToDb = function(){
  10. this.con = this.mysql.createConnection({
  11. host: "localhost",
  12. user: "root",
  13. password: "root",
  14. database: "redirections",
  15. port: 8889
  16. });
  17.  
  18. this.con.connect(function(err){
  19. if(err) throw err;
  20. console.log('Connection established');
  21. });
  22. };
  23.  
  24.  
  25.  
  26. this.isNewUrl = function(url,callback){
  27.  
  28. // sanitize
  29. var url = jsesc(url);
  30.  
  31. this.con.query("SELECT COUNT('url_origine') AS urlCount FROM list WHERE url_origine = '"+url+"' ",function(err,res){
  32. if(err) throw err;
  33.  
  34. var isNew = (res[0].urlCount > 0) ? false : true;
  35.  
  36. callback(isNew);
  37.  
  38. });
  39.  
  40. }
  41.  
  42. this.recordRedirection = function(dataToInsert){
  43.  
  44. var that = this;
  45. // sanitize
  46. dataToInsert.url_destination = jsesc(dataToInsert.url_destination);
  47.  
  48. that.con.query('INSERT INTO list SET ?', dataToInsert, function(err,res){
  49. if(err) throw err;
  50. console.log('Last insert ID:', res.insertId);
  51. });
  52.  
  53.  
  54. };
  55.  
  56.  
  57. };
  58.  
  59.  
  60. module.exports = db;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement