not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB

  1. const mysql = require('mysql');
  2. const sanitizer = require('sanitizer');
  3. var Promise = require('promise');
  4. const con = mysql.createConnection({
  5. host: "localhost",
  6. user: "trendingUser",
  7. password: "password",
  8. database: "googleTrends"
  9. });
  10. con.connect(function(err) {
  11. if (err) throw err;
  12. console.log("Connected to database!");
  13. });
  14. module.exports=
  15. {
  16. /**
  17. * Function used to use insert statements into the database
  18. *
  19. * Don't worry, the input gets sanitized
  20. *
  21. * @param sqlStatement
  22. * @return the id of the new record - if there is one
  23. */
  24. insert : function(sqlStatement)
  25. {
  26. return new Promise(function(resolve, reject)
  27. {
  28. con.query(sanitizer.sanitize(sqlStatement), function (err, result)
  29. {
  30. if (err)
  31. {
  32. console.log(err);
  33. resolve(0);
  34. }
  35. resolve(result.insertId);
  36. });
  37. })
  38. },
  39. /**
  40. * Insert all of the user's data into the sql database
  41. * @param player
  42. */
  43. insertData : function(player)
  44. {
  45. return new Promise(function(resolve, reject)
  46. {
  47. var q = "insert into users(name) values('" + player.name + "')";
  48. this.insert(q).then(function(keyId)
  49. {
  50. player.log.forEach(function(data)
  51. {
  52. var q2 = "insert into data(user_id, word, score) values " +
  53. "('" + keyId + "','" + data.word + "','" + data.score + "')";
  54. this.insert(q2);
  55. });
  56. resolve();
  57. })
  58. })
  59. },
  60. dumpRoom: function(room)
  61. {
  62. room.users.forEach(function(user)
  63. {
  64. this.insertData(user);
  65. });
  66. }
  67. };