From 7a0843cda0cab03ee6ee0f3f4cfa6cdf1b2eca75 Mon Sep 17 00:00:00 2001 From: adnan berandai Date: Sat, 18 Jan 2025 07:53:03 +0700 Subject: [PATCH] fix database --- jo_libs/modules/database/server.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/jo_libs/modules/database/server.lua b/jo_libs/modules/database/server.lua index 444b9e4..cdc6261 100644 --- a/jo_libs/modules/database/server.lua +++ b/jo_libs/modules/database/server.lua @@ -5,12 +5,13 @@ jo.file.load("@oxmysql.lib.MySQL") ---@param tableName string the name of the table ---@param definition string the definition of the table function jo.database.addTable(tableName, definition) - local isExist = MySQL.single.await("SHOW TABLES LIKE @tableName", { tableName = tableName }) + local isExist = MySQL.single.await("SHOW TABLES LIKE ?", { tableName }) if isExist then return false end + MySQL.update.await("CREATE TABLE IF NOT EXISTS " .. tableName .. " (" .. definition .. ")") - gprint("Database table created : " .. tableName) + gprint("Database table created: " .. tableName) return true end @@ -22,7 +23,7 @@ function jo.database.addTrigger(triggerName, definition) return false end MySQL.query.await("CREATE TRIGGER `" .. triggerName .. "` " .. definition) - gprint("Database trigger created : " .. triggerName) + gprint("Database trigger created: " .. triggerName) return true end @@ -30,11 +31,18 @@ end ---@param name string the name of the column ---@param definition string the definition of the column function jo.database.addColumn(tableName, name, definition) + local tableExists = MySQL.single.await("SHOW TABLES LIKE ?", { tableName }) + if not tableExists then + error("Table " .. tableName .. " does not exist") + return false + end + local isExist = MySQL.single.await("SHOW COLUMNS FROM " .. tableName .. " LIKE ?", { name }) if isExist then return false end + gprint("Database column " .. name .. " added to " .. tableName) MySQL.update.await("ALTER TABLE `" .. tableName .. "` ADD `" .. name .. "` " .. definition) return true -end +end \ No newline at end of file