Jump to content

DataBase, save User TOPTIME


Recommended Posts

Hello again with another incredible doubt and again there is no time to try alone.

Well, I have a system of Top TIMES. In this case, also shows the color of the user. more if the user changes the color of the name he can do Doubletop. I wanted to know what they have to do to block it, let down the syntax of the database, and if anyone can help, how is the new syntax. I really agradesco. Thank you and good forum for everyone.

-- 
-- databasetable_server.lua
--
-- A Lua table which is loaded/saved from the sqlite database
-- Handled column types are TEXT and REAL
--
 
SDatabaseTable = {}
SDatabaseTable.__index = SDatabaseTable
SDatabaseTable.instances = {}
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:create()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:create(name,columns,columnTypes)
    local id = #SDatabaseTable.instances + 1
    SDatabaseTable.instances[id] = setmetatable(
        {
            id = id,
            name = name,
            columns = columns,
            columnTypes = columnTypes,
            rows = {},
        },
        self
    )
    SDatabaseTable.instances[id]:postCreate()
    return SDatabaseTable.instances[id]
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:destroy()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:destroy()
    SDatabaseTable.instances[self.id] = nil
    self.id = 0
    ped = nil
    vehicle = nil
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:postCreate()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:postCreate()
    -- Set column types as strings if not set
    while #self.columnTypes < #self.columns do
        table.insert( self.columnTypes, 'TEXT' )
    end
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:safestring()
--
--
--
---------------------------------------------------------------------------
function safestring( s )
    -- escape '
    return s:gsub( "(['])", "''" )
end
 
function qsafestring( s )
    -- ensure is wrapped in '
    return "'" .. safestring(s) .. "'"
end
 
function qsafetablename( s )
    return qsafestring(s)
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:load()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:load()
    for i=1,10 do
        if self:tryLoad() then
            return
        end
    end
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:tryLoad()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:tryLoad()
    outputDebug( 'TOPTIMES', 'SDatabaseTable: Loading ' .. self.name )
    self.rows = {}
 
    local cmd
 
    -- CREATE TABLE
    self:createTable()
 
 
    -- SELECT
 
    -- Build command
    cmd = 'SELECT * FROM ' .. qsafetablename( self.name )
 
    local sqlResults = executeSQLQuery( cmd )
 
    if not sqlResults then
        return false
    end
 
    -- Process into rows
    self.rows = {}
    for r,sqlRow in ipairs(sqlResults) do
        local row = {}
        for c,column in ipairs(self.columns) do
            row[column] = sqlRow[column]
        end
        table.insert( self.rows, row )
    end
 
    -- Make copy to detect changes
    self.rowsCopy = table.deepcopy(self.rows)
 
    return true
end
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:save()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:save()
 
    -- See if save required
    local bChanged = false
    if not self.rowsCopy or #self.rows ~= #self.rowsCopy then
        bChanged = true
    else
        for r,row in ipairs(self.rows) do
            for c,col in ipairs(self.columns) do
                if self.rows[r][col] ~= self.rowsCopy[r][col] then
                    bChanged = true
                    break
                end
            end
            if bChanged then
                break
            end
        end
    end
 
    if not bChanged then
        return
    end
 
    outputDebug( 'TOPTIMES', 'SDatabaseTable: Saving ' .. self.name )
 
 
    -- Being save
    executeSQLQuery( 'BEGIN TRANSACTION' );
 
    local cmd
 
    -- DELETE TABLE
 
    -- Build command
    --cmd = 'DELETE FROM ' .. qsafetablename( self.name )
    cmd = 'DROP TABLE IF EXISTS ' .. qsafetablename( self.name )
    executeSQLQuery( cmd )
 
 
    -- CREATE TABLE
    self:createTable()
 
 
    -- Rebuild
    -- For each row
    for r,row in ipairs(self.rows) do
 
        -- INSERT INTO
 
        cmd = 'INSERT INTO ' .. qsafetablename( self.name ) .. ' VALUES ('
        for c=1,#self.columns do
            if c > 1 then
                cmd = cmd .. ', '
            end
            local key = self.columns[c]
            if type(row[key]) == 'number' then
                cmd = cmd .. row[key] or 0
            else
                cmd = cmd .. qsafestring( row[key] or '' )
            end
        end
        cmd = cmd .. ')'
 
        executeSQLQuery( cmd )
 
    end
 
    executeSQLQuery( 'END TRANSACTION' );
 
    -- Make copy to detect changes
    self.rowsCopy = table.deepcopy(self.rows)
 
end
 
 
 
 
---------------------------------------------------------------------------
--
-- SDatabaseTable:createTable()
--
--
--
---------------------------------------------------------------------------
function SDatabaseTable:createTable()
 
    local cmd
 
    -- CREATE TABLE
 
    -- Build command
    cmd = 'CREATE TABLE IF NOT EXISTS ' .. qsafetablename( self.name ) .. ' ('
    for c=1,#self.columns do
        if c > 1 then
            cmd = cmd ..
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...