-- Safe iterator: never throws. On any SQL/connect error it yields nothing. local function rows(connection, sql_statement) if connection == nil or type(connection.execute) ~= "function" then return function() return nil end end local ok, cursor_or_err = pcall(function() return connection:execute(sql_statement) end) local cursor = ok and cursor_or_err or nil if cursor == nil or type(cursor.fetch) ~= "function" then return function() return nil end end local closed = false return function() if closed then return nil end local v = cursor:fetch() if v == nil then closed = true pcall(function() cursor:close() end) return nil end return v end end local function _get_con() if type(_G.Sql_con) ~= "function" then return nil end local con = _G.Sql_con() if con == nil or type(con.execute) ~= "function" then return nil end return con end local function _close_con(con) if con ~= nil and type(con.close) == "function" then pcall(function() con:close() end) end end function family_dragon_sql(FamilyID, DragonExp, add) local con = _get_con() if con == nil then return 0 end if add == 1 then local sql = "SELECT * FROM family_dragon WHERE FamilyID = '%s'" local FamilyID_id = 0 for familyIDRow in rows(con, sql:format(FamilyID)) do FamilyID_id = familyIDRow end if FamilyID_id == 0 then sql = "INSERT INTO family_dragon VALUES ('%s', '%s', 0)" pcall(function() con:execute(sql:format(FamilyID, DragonExp)) end) else sql = "Update family_dragon set DragonExp = DragonExp+'%s' where FamilyID = '%s'" pcall(function() con:execute(sql:format(DragonExp, FamilyID)) end) end end local sql = "SELECT DragonExp FROM family_dragon WHERE FamilyID = '%s'" local my_rating_point = 0 for DragonExp in rows(con, sql:format(FamilyID)) do my_rating_point = DragonExp end _close_con(con) return my_rating_point end function family_dragon_Active(FamilyID, Active, add) local con = _get_con() if con == nil then return 0 end if add == 1 then local sql = "SELECT * FROM family_dragon WHERE FamilyID = '%s'" local FamilyID_id = 0 for familyIDRow in rows(con, sql:format(FamilyID)) do FamilyID_id = familyIDRow end if FamilyID_id == 0 then sql = "INSERT INTO family_dragon VALUES ('%s', '%s', 0)" pcall(function() con:execute(sql:format(FamilyID, Active)) end) else sql = "Update family_dragon set Active = '%s' where FamilyID = '%s'" pcall(function() con:execute(sql:format(Active, FamilyID)) end) end end local sql = "SELECT Active FROM family_dragon WHERE FamilyID = '%s'" local my_active = 0 for Active in rows(con, sql:format(FamilyID)) do my_active = Active end _close_con(con) return my_active end function GonkaSaveAct(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE familyact_gonka_data SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM familyact_gonka_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO familyact_gonka_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function GonkaSaveAct2(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE familyact_gonka_data SET Gift = 1 WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT Gift FROM familyact_gonka_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO familyact_gonka_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function GonkaData(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE familydata_gonka SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 2 then local sql = "UPDATE familydata_gonka SET count = count+'%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 3 then local sql = "UPDATE familydata_gonka SET count = '%s'" pcall(function() con:execute(sql:format(count)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM familydata_gonka WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO familydata_gonka VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function RoleGonkaData(RoleID, i, Gift) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE roledata_gonka SET Gift = '%s' WHERE RoleID = '%s'" pcall(function() con:execute(sql:format(Gift, RoleID)) end) end local points = 0 for point_Gift in rows(con, string.format("SELECT Gift FROM roledata_gonka WHERE RoleID = %s", RoleID)) do points = point_Gift end if points == 0 then local sql = "INSERT INTO roledata_gonka VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(RoleID, Gift)) end) end _close_con(con) return points end function GonkaData_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE familydata_gonka" pcall(function() con:execute(sql) end) _close_con(con) end function GonkaDataSave_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE familyact_gonka_data" pcall(function() con:execute(sql) end) _close_con(con) end function GonkaAct(ActID, i, gift_count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE familyact_gonka SET gift_count = '%s' WHERE ActID = '%s'" pcall(function() con:execute(sql:format(gift_count, ActID)) end) elseif i == 2 then local sql = "UPDATE familyact_gonka SET gift_count = gift_count+'%s' WHERE ActID = '%s'" pcall(function() con:execute(sql:format(gift_count, ActID)) end) end local gifts = 0 for gift_gift_count in rows(con, string.format("SELECT gift_count FROM familyact_gonka WHERE ActID = %s", ActID)) do gifts = gift_gift_count end if gifts == 0 then local sql = "INSERT INTO familyact_gonka VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(ActID, gift_count)) end) end _close_con(con) return gifts end function GonkaAct2(ActID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE familyact_gonka SET count = '%s' WHERE ActID = '%s'" pcall(function() con:execute(sql:format(count, ActID)) end) elseif i == 2 then local sql = "UPDATE familyact_gonka SET count = count+'%s' WHERE ActID = '%s'" pcall(function() con:execute(sql:format(count, ActID)) end) end local gifts = 0 for gift_gift_count in rows(con, string.format("SELECT count FROM familyact_gonka WHERE ActID = %s", ActID)) do gifts = gift_gift_count end if gifts == 0 then local sql = "INSERT INTO familyact_gonka VALUES ('%s', '0', '%s')" pcall(function() con:execute(sql:format(ActID, count)) end) end _close_con(con) return gifts end function GonkaAct_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE familyact_gonka" pcall(function() con:execute(sql) end) _close_con(con) end function GonkaActRole_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE roledata_gonka" pcall(function() con:execute(sql) end) _close_con(con) end function CheckDragonActive() local con = _get_con() if con == nil then return end local sql = "UPDATE family_dragon SET DragonExp = DragonExp-100 WHERE Active = 0" pcall(function() con:execute(sql) end) _close_con(con) end function CheckDragonExp() local con = _get_con() if con == nil then return end local sql = "UPDATE family_dragon SET DragonExp = 0 WHERE DragonExp < 0" pcall(function() con:execute(sql) end) _close_con(con) end function BroActRole_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE roledata_broweapon" pcall(function() con:execute(sql) end) _close_con(con) end function BroData_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE family_broweapon" pcall(function() con:execute(sql) end) _close_con(con) end function BroDataSave_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE family_broweapon_data" pcall(function() con:execute(sql) end) _close_con(con) end function FamLevel(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_level SET Level = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT Level FROM family_level WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_level VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function BroData(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_broweapon SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 2 then local sql = "UPDATE family_broweapon SET count = count+'%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 3 then local sql = "UPDATE family_broweapon SET count = '%s'" pcall(function() con:execute(sql:format(count)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM family_broweapon WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_broweapon VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function BroSaveAct(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_broweapon_data SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM family_broweapon_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_broweapon_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function BroSaveAct2(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_broweapon_data SET Gift = 1 WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT Gift FROM family_broweapon_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_broweapon_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function RoleBroData(RoleID, i, Gift) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE roledata_broweapon SET Gift = '%s' WHERE RoleID = '%s'" pcall(function() con:execute(sql:format(Gift, RoleID)) end) end local points = 0 for point_Gift in rows(con, string.format("SELECT Gift FROM roledata_broweapon WHERE RoleID = %s", RoleID)) do points = point_Gift end if points == 0 then local sql = "INSERT INTO roledata_broweapon VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(RoleID, Gift)) end) end _close_con(con) return points end function GreziActRole_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE roledata_grezi" pcall(function() con:execute(sql) end) _close_con(con) end function GreziData_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE family_grezi" pcall(function() con:execute(sql) end) _close_con(con) end function GreziDataSave_Delete() local con = _get_con() if con == nil then return end local sql = "TRUNCATE family_grezi_data" pcall(function() con:execute(sql) end) _close_con(con) end function GreziData(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_grezi SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 2 then local sql = "UPDATE family_grezi SET count = count+'%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) elseif i == 3 then local sql = "UPDATE family_grezi SET count = '%s'" pcall(function() con:execute(sql:format(count)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM family_grezi WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_grezi VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function GreziSaveAct(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_grezi_data SET count = '%s' WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(count, FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT count FROM family_grezi_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_grezi_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function GreziSaveAct2(FamilyID, i, count) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE family_grezi_data SET Gift = 1 WHERE FamilyID = '%s'" pcall(function() con:execute(sql:format(FamilyID)) end) end local points = 0 for point_count in rows(con, string.format("SELECT Gift FROM family_grezi_data WHERE FamilyID = %s", FamilyID)) do points = point_count end if points == 0 then local sql = "INSERT INTO family_grezi_data VALUES ('%s', '%s', '0')" pcall(function() con:execute(sql:format(FamilyID, count)) end) end _close_con(con) return points end function RoleGreziData(RoleID, i, Gift) local con = _get_con() if con == nil then return 0 end if i == 1 then local sql = "UPDATE roledata_grezi SET Gift = '%s' WHERE RoleID = '%s'" pcall(function() con:execute(sql:format(Gift, RoleID)) end) end local points = 0 for point_Gift in rows(con, string.format("SELECT Gift FROM roledata_grezi WHERE RoleID = %s", RoleID)) do points = point_Gift end if points == 0 then local sql = "INSERT INTO roledata_grezi VALUES ('%s', '%s')" pcall(function() con:execute(sql:format(RoleID, Gift)) end) end _close_con(con) return points end