-- f1 farm SQL helpers (Lua 5.4.6) -- Uses project-standard MySQL connector: server_data/script/Sql_con.lua local M = {} local function _ensure_sql() if type(_G.Sql_con) == "function" then return true end pcall(require, "Sql_con") if type(_G.Sql_con) == "function" then return true end pcall(dofile, "server_data/script/Sql_con.lua") if type(_G.Sql_con) == "function" then return true end pcall(dofile, "../server_data/script/Sql_con.lua") return type(_G.Sql_con) == "function" end local function _to_int(v, default) local n = tonumber(v) if n == nil then return default or 0 end if n ~= n or n == math.huge or n == -math.huge then return default or 0 end return math.floor(n) end local function _exec(con, sql) if con == nil or type(con.execute) ~= "function" then return false end local ok, r1, r2 = pcall(function() return con:execute(sql) end) if ok and r1 ~= nil then return true end _G._farm_f1_sql_last_error = tostring(ok and r2 or r1) return false end local function _select1(con, sql) if con == nil or type(con.execute) ~= "function" then return nil end local ok, cur_or_val, err = pcall(function() return con:execute(sql) end) if not ok or cur_or_val == nil then _G._farm_f1_sql_last_error = tostring(ok and err or cur_or_val) return nil end local cur = cur_or_val local v = nil if type(cur) == "userdata" and type(cur.fetch) == "function" then v = cur:fetch() pcall(function() cur:close() end) end return v end function M.getCon() _ensure_sql() if type(_G.Sql_con) ~= "function" then return nil end return _G.Sql_con() end function M.ensureSchema() local con = M.getCon() if con == nil then return false end local role_exists = _select1(con, [[ SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'sm_data' AND table_name = 'farm_f1_role' ]]) local plot_exists = _select1(con, [[ SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'sm_data' AND table_name = 'farm_f1_plot' ]]) if _to_int(role_exists) < 1 or _to_int(plot_exists) < 1 then _G._farm_f1_sql_last_error = "farm_f1 schema missing: run server_data/script/activity/провер/Ферма/schema_f1.sql" return false end return true end -- Expose helpers M.to_int = _to_int M.exec = _exec M.select1 = _select1 return M