Announcement

Collapse
No announcement yet.

Making SQL calls is boring...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Making SQL calls is boring...

    Hi,

    I read the docs about MariaDB and actually, I found the sql calls very boring (I'm used to async/await that Js provide).

    To explain my thoughts, let's explore a minimal example of a package that making some SQL calls in order to check if a user if whitelisted and banned:

    Code:
    function authentificationCheck(player)
        local steamId = GetPlayerSteamId(player)
        -- whitelist check if enable if the config
        if (config("whitelist")) then
            query("SELECT * FROM users WHERE steam = ?", {steamId}, whitelistCheck, player)
        end
        -- ban check: so Should I prepare another query here
    end
    
    function whitelistCheck(player)
        local rows = mariadb_get_row_count()
        if (rows) then
            local isWhitelisted = mariadb_get_value_name(1, "is_whitelisted");
    
            if (not(isWhitelisted)) then
                return KickPlayer(player, "Tu n'es pas whitelisté.")
            end
        end
        -- Should I prepare another query here
    end
    
    -- that both would be treated here?
    function banCheck(player)
    end
    AddEvent("OnPlayerSteamAuth", authentificationCheck)
    In fact, asynchrone is cool, but not readable, I like my script when they are linear. A -> B -> C. But with this architecture, I can't do it (or pretty badly).
    I'm used to FiveM, we had promise and coroutine and so.

    If anyone have a good workaround or an idea on how to improve this readability, please, let me know

    Izio.

    #2
    Hi,

    one thing comes to mind if you want to just rearrange the code : have you tried defining whitelistCheck(player) directly inline with the arguments of the query? It would look something like :
    Code:
    [FONT=courier new]query("SELECT * FROM users WHERE steam = ?", {steamId}, [/FONT][INDENT][FONT=courier new]function (player)     [/FONT][/INDENT][INDENT=2][FONT=courier new]local rows = mariadb_get_row_count()    
    if (rows) then         [/FONT][/INDENT][INDENT=3][FONT=courier new]local isWhitelisted = mariadb_get_value_name(1, "is_whitelisted");          
    
    if (not(isWhitelisted)) then             [/FONT][/INDENT][INDENT=4][FONT=courier new]return KickPlayer(player, "Tu n'es pas whitelisté.")         [/FONT][/INDENT][INDENT=3][FONT=courier new]end     [/FONT][/INDENT][INDENT=2][FONT=courier new]end  
    -- Should I prepare another query here [/FONT][/INDENT][INDENT]
    [FONT=courier new]end,[/FONT][/INDENT]
     [FONT=courier new]player)[/FONT]
    Although, to be honest, I prefer your current version than this one for better readability.
    Happy coding.
    Last edited by Bewick; 12-24-2019, 03:29 PM.

    Comment


      #3
      Also why the heck does : mariadb_get_assoc(index) returns all value as string?

      Comment


        #4
        Originally posted by Izio View Post
        Also why the heck does : mariadb_get_assoc(index) returns all value as string?
        It returns as a table.

        Comment


        • Izio
          Izio commented
          Editing a comment
          Yes indeed, but values are string, even if the sql column is integer or so.
      Working...
      X