Advertisement
ToxicHawk

Untitled

Nov 13th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. db = require("luasql.postgres")
  2. env = assert(db.postgres())
  3. con = assert(env:connect("vr"))
  4.  
  5. function dbGetUserID(name)
  6. local cur=assert(con:execute("select id from users where name = '"..name.."';"))
  7. local ROW = cur:fetch({})
  8. if ROW == nil then
  9. return 0
  10. else
  11. return ROW[1]
  12. end
  13. end
  14.  
  15. function dbAddGame(id, points)
  16. assert(con:execute("insert into games (userid, points) values ("..tostring(id)..", "..points.. ");"))
  17. end
  18.  
  19. function dbAddUser(user)
  20. local cur = assert(con:execute("insert into users (name) values ('"..user.."') RETURNING id;"))
  21. local ROW = cur:fetch({})
  22. return ROW[1]
  23. end
  24.  
  25. function dbCountGames(id, points)
  26. if id == 0 then
  27. return 0
  28. else
  29. if points == 0 then
  30. local cur = assert(con:execute("select count(userid) from games where userid = '"..tostring(id).."';"))
  31. local ROW = cur:fetch({})
  32. return ROW[1]
  33. elseif points == 3 or points == 1 or points == -3 then
  34. local cur = assert(con:execute("select count(userid) from games where userid = '"..tostring(id).."' AND points ='".. tostring(points).."';"))
  35. local ROW = cur:fetch({})
  36. return ROW[1]
  37. end
  38. end
  39. end
  40.  
  41. function dbGetRaiting(id)
  42. cur = assert(con:execute("select raiting from users where id = "..id..";"))
  43. local ROW = cur:fetch({})
  44. return ROW[1]
  45. end
  46.  
  47. function dbGetRank(id)
  48. cur = assert(con:execute('select r from (select dense_rank() over (order by raiting desc) as r, id from users) ranksel where id='..tostring(id)..';'))
  49.  
  50. local ROW = cur:fetch({})
  51.  
  52. if ROW[1] == nil then
  53. return 'неизвестно'
  54. else
  55. return ROW[1]
  56. end
  57. end
  58.  
  59. function dbGetDateLast(id, points)
  60. cur = assert(con:execute('select max(date) from games where userid='..
  61. tostring(id)..' and points='..tostring(points)..';'))
  62. local ROW = cur:fetch({})
  63. if ROW[1] == nil then
  64. return 'неизвестна'
  65. else
  66. return ROW[1]
  67. end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement