• Print

Author Topic: MySQLoo callback pause  (Read 5201 times)

0 Members and 1 Guest are viewing this topic.

Offline captain1342

  • Full Member
  • ***
  • Posts: 104
  • Karma: 6
  • Quality is our standard
    • Aperture Development - Quality is our standard
MySQLoo callback pause
« on: January 17, 2017, 12:35:07 pm »
Heyo Everyone,

I have a problem with MySQLoo cause i am using a callback to get playerdatas from a Database but .. I need a way to pause the script untill the callback is completed

Code: Lua
  1. MSync.CheckIfBanned(ply)
  2.                 local QcheckIfBan = MSync.DB:query("SELECT * FROM `mbsync` WHERE steamid = '" .. ply:SteamID() .. "'")
  3.                 local banTable = banTable or {
  4.                         bool    =       false
  5.                         Reason  =       nil
  6.                         Admin   =       nil
  7.                         banTime =       nil
  8.                         banDate =       nil
  9.                 }
  10.                
  11.                 QcheckIfBan.onData = function(Q,D)
  12.                         QcheckIfBan.onSuccess = function(q)
  13.                                 if checkQuery(q) then
  14.                                         banTable = {
  15.                                                 Reason  =       D.reason
  16.                                                 Admin   =       D.admin
  17.                                                 banTime =       D.ban_time
  18.                                                 banDate =       D.ban_date
  19.                                         }
  20.                                 end
  21.                         end
  22.                 end
  23.                 QcheckIfBan.onError = function(Q,E) print("Q1") print(E) end
  24.                
  25.                 QcheckIfBan:start()
  26.                 QcheckIfBan:wait()
  27.                
  28.                 if not(banTable.Reason==nil)then
  29.                         if(banTable.banTime + banTable.banDate>=os.time())then
  30.                                 banTable.bool = true
  31.                                 return banTable
  32.                         else
  33.                                 banTable.bool = false
  34.                                 return banTable
  35.                         end
  36.                 else
  37.                         banTable.bool = false
  38.                         return banTable
  39.                 end
  40.                
  41.         end

Thats the code I am Using.

Thanks for all answeres,

~cap
Aperture Development
Quality is our standard

Website - GitHub  - Forum  - Steam  - Discord

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: MySQLoo callback pause
« Reply #1 on: January 17, 2017, 03:34:16 pm »
All,
Would running a do while loop pause in an infinite loop until a condition (such as data from a DB appearing in a var) work for captain?
(Been too long, I'm unsure if stopping code in an infinite(until data appears) loop might halt all other scripts/crash the game too.

Also, captain, i forget where, but somewhere in ULX i could swear we have a bit of code that repeats itself until ULib is loaded/initialized and then moves on.
I might be dreaming.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: MySQLoo callback pause
« Reply #2 on: January 17, 2017, 10:16:46 pm »
I discourage querying databases synchronously. Databases can take a while to respond to a query. This will make a server halt until the query finishes.

I noticed that you used the asynchronous callbacks Query:onSucess and Query:onData in your code. These are not necessary when writing synchronous code. After Query:wait() finishes, you should have access to Query:getData(), unless an error occurs.

Synchronous query example:
Code: Lua
  1. local query = db:query("SELECT column FROM table WHERE id = 1")
  2. query:start()
  3. query:wait() -- Hang server until query finishes
  4.  
  5. if query:error() ~= "" then
  6.   print("Query error message: ", query:error())
  7.   return
  8. end
  9.  
  10. print("Query response data:")
  11. PrintTable(query:getData())

Would running a do while loop pause in an infinite loop until a condition (such as data from a DB appearing in a var) work for captain?
It wouldn't work.

An infinite loop that checks if a var was populated would lock up the server, unless the inside of the loop is going to change that condition or break at some point. The module would have to provide a function that continues to execute the query inside the loop.

---

Actually... you should be able to make these things synchronous with coroutines too. But, after some testing, MySQLOO didn't seem to like running in another thread. (Test code for people that want to experiment with that: https://gist.github.com/anonymous/863806a7968924630f54ee5999d284af :P)
« Last Edit: January 18, 2017, 04:33:43 pm by Timmy »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: MySQLoo callback pause
« Reply #3 on: January 18, 2017, 03:57:11 pm »
It wouldn't work.

An infinite loop that checks if a var was populated would lock up the server, unless the inside of the loop is going to change that condition or break at some point. The module would have to provide a function that continues to execute the query inside the loop.
Thanks -
I figured stuff would break, hence my parentheses.
Though your ideas for captain sound much better for SQL and his situation, perhaps for future, maybe a think hook with a var data check to call another function if data exists.
But hopefully, if not using SQL/DB/connection to other server stuff/lookups, one's functions would never take that long to fill a var in the first place.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print