Files
community_bridge/lib/sql/server/sqlHandler.lua
T

68 lines
2.0 KiB
Lua
Raw Normal View History

2025-02-05 17:42:50 -06:00
SQL = {}
Require("lib/MySQL.lua", "oxmysql")
function SQL.Create(tableName, columns)
assert(MySQL, "Tried using module SQL without MySQL being loaded")
local columnsList = {}
for i, column in pairs(columns) do
table.insert(columnsList, string.format("%s %s", column.name, column.type))
end
2025-02-05 19:52:48 -05:00
2025-02-05 17:42:50 -06:00
local query = string.format("CREATE TABLE IF NOT EXISTS %s (%s);",
tableName,
table.concat(columnsList, ", ")
)
2025-02-05 19:52:48 -05:00
2025-02-05 17:42:50 -06:00
MySQL.query.await(query)
end
-- insert if not exist otherwise update
function SQL.InsertOrUpdate(tableName, data)
assert(MySQL, "Tried using module SQL without MySQL being loaded")
local columns = {}
local values = {}
local updates = {}
for column, value in pairs(data) do
table.insert(columns, column)
table.insert(values, "'" .. value .. "'") -- Ensure values are properly quoted
table.insert(updates, column .. " = VALUES(" .. column .. ")") -- Use VALUES() for update
end
local query = string.format(
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s;",
tableName,
table.concat(columns, ", "),
table.concat(values, ", "),
table.concat(updates, ", ")
)
MySQL.query.await(query)
end
function SQL.Get(tableName, where)
assert(MySQL, "Tried using module SQL without MySQL being loaded")
local query = string.format("SELECT * FROM %s WHERE %s;", tableName, where)
local result = MySQL.query.await(query)
return result
end
function SQL.GetAll(tableName)
assert(MySQL, "Tried using module SQL without MySQL being loaded")
local query = string.format("SELECT * FROM %s;", tableName)
local result = MySQL.query.await(query)
return result
end
function SQL.Delete(tableName, where)
assert(MySQL, "Tried using module SQL without MySQL being loaded")
local query = string.format("DELETE FROM %s WHERE %s;", tableName, where)
MySQL.query.await(query)
end
exports('SQL', function()
return SQL
end)
2025-02-05 17:42:50 -06:00
return SQL