From 3265fc0d995bff1d43c3093b5fb572b9c0217368 Mon Sep 17 00:00:00 2001 From: Amritanshu Varshney Date: Sat, 23 May 2015 00:23:07 +0530 Subject: [PATCH] Replace flaky persistent connection with connection per query Connections to Mysql are opened on query and closed after. Tuning of wait_read parameter in mysql settings needed. Amend this commit and fix syntax alignment. --- external/info.lua | 92 ++++++++++++++------ external/offliner.lua | 166 +++++++++++++++++++++++++++--------- external/stats/chat.lua | 47 ++++++++-- external/stats/hubtopic.lua | 14 ++- external/stats/toks.lua | 103 ++++++++++++++++------ 5 files changed, 323 insertions(+), 99 deletions(-) diff --git a/external/info.lua b/external/info.lua index a44b523..32f538b 100644 --- a/external/info.lua +++ b/external/info.lua @@ -18,6 +18,11 @@ tConfig.sHubFAQ = "http://"..tConfig.sHub.."/faq/%s/%04d" function OnError( sError ) Core.SendToOpChat( sError ) end +if not luasql then + luasql = require "luasql.mysql" +end + +Connection = require 'config' _G.tFunction = { Connect = function() @@ -48,34 +53,45 @@ _G.tFunction = { end, GetCategories = function() - local tReturn, sCategoryQuery = {}, [[SELECT `name` FROM `ctgtable`]] - local SQLCur = assert( SQLCon:execute(sCategoryQuery) ) + local tReturn, sCategoryQuery = {}, [[SELECT `name` FROM `ctgtable`]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sCategoryQuery) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tReturn, tRow.name ) tRow = SQLCur:fetch( {}, "a" ) end SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, FetchRow = function( sTable, iID ) - local sFields, sQuery, tReturn = "msg, nick", "SELECT %s FROM `%s` WHERE id = %d LIMIT 1", {} + local sFields, sQuery, tReturn = "msg, nick", "SELECT %s FROM `%s` WHERE id = %d LIMIT 1", {} + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + if sTable == "requests" or sTable == "suggestions" then sFields = "`ctg`, "..sFields elseif sTable == "buynsell" then sFields = sFields..", CASE `type` WHEN 'B' THEN UPPER('buy') WHEN 'S' THEN UPPER('sell') WHEN 'H' THEN UPPER('hire') WHEN 'L' THEN UPPER('loan') WHEN 'T' THEN UPPER('bought') WHEN 'D' THEN UPPER('sold') END `type`" end sQuery = sQuery:format( sFields, sTable, iID ) - local SQLCur = assert( SQLCon:execute(sQuery) ) + local SQLCur = assert( SQLCon1:execute(sQuery) ) tReturn = SQLCur:fetch( {}, "a" ) SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, FetchReplies = function( iID ) - local sReturn, tTemporary, sQuery = "", {}, ([[SELECT id, nick, msg, dated FROM replies WHERE bns_id = %d ORDER BY id ASC]]):format( iID ) - local SQLCur = assert( SQLCon:execute(sQuery) ) + local sReturn, tTemporary, sQuery = "", {}, ([[SELECT id, nick, msg, dated FROM replies WHERE bns_id = %d ORDER BY id ASC]]):format( iID ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sQuery) ) local tRow = SQLCur:fetch( {}, "a" ) if tRow then while tRow do @@ -86,6 +102,9 @@ _G.tFunction = { else sReturn = "" end + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return sReturn end, @@ -152,9 +171,11 @@ _G.tInfobot = { if bReadAll then sReturnList = "Table: "..sTable:upper().."\t\t\tLimit: "..tostring(iLimit).."\n\n" end + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) if sTable == "suggestions" then sFields = "`ctg`, "..sFields - SQLCur = assert( SQLCon:execute(sReadQuery:format( sFields, sTable, iLimit )) ) + SQLCur = assert( SQLCon1:execute(sReadQuery:format( sFields, sTable, iLimit )) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] - %s"):format(tRow.ctg, tRow.msg), tRow.nick, tRow.dated) ) @@ -164,7 +185,7 @@ _G.tInfobot = { elseif sTable == "requests" then sFields = sFields:gsub( "`msg`", "CASE `filled` WHEN 'Y' THEN CONCAT(`msg`, ' (Filled by ', `filledby`, ' on ', filldate, ')') WHEN 'C' THEN CONCAT(`msg`, ' (Closed by ', `filledby`, ' on ', filldate, ')') WHEN 'N' THEN `msg` END `msg`" )..", `ctg`, CASE `filled` WHEN 'Y' THEN UPPER('filled') WHEN 'N' THEN UPPER('empty') WHEN 'C' THEN UPPER('closed') END `filled`" sReadQuery = sReadQuery:format( sFields, sTable, iLimit ) - SQLCur = assert( SQLCon:execute(sReadQuery) ) + SQLCur = assert( SQLCon1:execute(sReadQuery) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] [%s] - %s"):format(tRow.filled, tRow.ctg, tRow.msg), tRow.nick, tRow.dated) ) @@ -174,7 +195,7 @@ _G.tInfobot = { elseif sTable == "buynsell" then sFields = sFields..", CASE `type` WHEN 'B' THEN UPPER('buy') WHEN 'S' THEN UPPER('sell') WHEN 'H' THEN UPPER('hire') WHEN 'L' THEN UPPER('loan') WHEN 'T' THEN UPPER('bought') WHEN 'D' THEN UPPER('sold') END `type`" sReadQuery = sReadQuery:format( sFields, sTable, iLimit ) - SQLCur = assert( SQLCon:execute(sReadQuery) ) + SQLCur = assert( SQLCon1:execute(sReadQuery) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] - %s"):format(tRow.type, tRow.msg), tRow.nick, tRow.dated)..tFunction.FetchReplies(tRow.id) ) @@ -182,14 +203,15 @@ _G.tInfobot = { end else - SQLCur = assert( SQLCon:execute(sReadQuery:format( sFields, sTable, iLimit )) ) + SQLCur = assert( SQLCon1:execute(sReadQuery:format( sFields, sTable, iLimit )) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tTemporary, sEntry:format(tRow.id, tRow.msg, tRow.nick, tRow.dated) ) tRow = SQLCur:fetch( {}, "a" ) end end - + SQLCon1:close() + SQLEnv1:close() return ( sReturnList..table.concat(tTemporary, "\n") ) end, @@ -198,9 +220,11 @@ _G.tInfobot = { Core.SendPmToUser( tUser, tConfig.sBotName, "The string to be added must be at least 20 characters." ) return false end - local sFields, sValues = "`msg`, `nick`, `dated`", ("'%s', '%s', NOW()"):format( SQLCon:escape(tInput.sMsg), SQLCon:escape(tUser.sNick) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local sFields, sValues = "`msg`, `nick`, `dated`", ("'%s', '%s', NOW()"):format( SQLCon1:escape(tInput.sMsg), SQLCon1:escape(tUser.sNick) ) if tInput.sTable:lower() == "requests" or tInput.sTable:lower() == "suggestions" then - sFields, sValues = "`ctg`, "..sFields, ("'%s', %s"):format( SQLCon:escape(tInput.sCtg), sValues ) + sFields, sValues = "`ctg`, "..sFields, ("'%s', %s"):format( SQLCon1:escape(tInput.sCtg), sValues ) elseif tInput.sTable:lower() == "buynsell" then sFields, sValues = "`type`, "..sFields, ("'%s', %s"):format( tInput.sType, sValues ) elseif tInput.sTable:lower() == "replies" then @@ -209,13 +233,16 @@ _G.tInfobot = { local sQuery = [[INSERT IGNORE INTO `%s`(%s) VALUES (%s) ]] sQuery = sQuery:format( tInput.sTable, sFields, sValues ) - local SQLCur = assert( SQLCon:execute(sQuery) ) + local SQLCur = assert( SQLCon1:execute(sQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() else SQLCur = nil end - return SQLCon:getlastautoid() + local ret = SQLCon1:getlastautoid() + SQLCon1:close() + SQLEnv1:close() + return ret end, StoreMessage = function( sSender, sRecipient, sMessage ) @@ -223,23 +250,34 @@ _G.tInfobot = { Core.SendPmToNick( sRecipient, sSender, sMessage ) return 0, "Message delivered" end + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) local sStorageQuery = [[INSERT INTO messages(message, `from`, `to`, dated) VALUES ( '%s', '%s', '%s', NOW() ) ]] - sStorageQuery = sStorageQuery:format( SQLCon:escape(sMessage), SQLCon:escape(sSender), SQLCon:escape(sRecipient) ) - local SQLCur = assert( SQLCon:execute(sStorageQuery) ) - return SQLCon:getlastautoid() + + sStorageQuery = sStorageQuery:format( SQLCon1:escape(sMessage), SQLCon1:escape(sSender), SQLCon1:escape(sRecipient) ) + local SQLCur = assert( SQLCon1:execute(sStorageQuery) ) + ret = SQLCon1:getlastautoid() + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() + return ret end, del = function( tUser, tInput ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) if tInput.sTable == "buynsell" then local sDeleteQuery = string.format( "DELETE b.*, r.* FROM `buynsell` b LEFT JOIN `replies` r ON r.`bns_id` = b.`id` WHERE b.`id` = %d", tInput.iID ) - local SQLCur = assert( SQLCon:execute(sDeleteQuery) ) + local SQLCur = assert( SQLCon1:execute(sDeleteQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() end return true end - local sDeleteQuery = string.format( "DELETE FROM `%s` WHERE `id` = %d", SQLCon:escape(tInput.sTable), tInput.iID ) - local SQLCur = assert( SQLCon:execute(sDeleteQuery) ) + local sDeleteQuery = string.format( "DELETE FROM `%s` WHERE `id` = %d", SQLCon1:escape(tInput.sTable), tInput.iID ) + local SQLCur = assert( SQLCon1:execute(sDeleteQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -250,8 +288,10 @@ _G.tInfobot = { filledby = '%s' WHERE id = %d LIMIT 1]] - sUpdateQuery = sUpdateQuery:format( (bClosure and 'C') or 'Y', SQLCon:escape(tUser.sNick), iID ) - local SQLCur = assert( SQLCon:execute(sUpdateQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + sUpdateQuery = sUpdateQuery:format( (bClosure and 'C') or 'Y', SQLCon1:escape(tUser.sNick), iID ) + local SQLCur = assert( SQLCon1:execute(sUpdateQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() else @@ -266,12 +306,16 @@ _G.tInfobot = { WHERE `id` = %d LIMIT 1]] sUpdateQuery = sUpdateQuery:format( iID ) - local SQLCur = assert( SQLCon:execute(sUpdateQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sUpdateQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() else SQLCur = nil end + SQLCon1:close() + SQLEnv1:close() return true end } diff --git a/external/offliner.lua b/external/offliner.lua index 0b5c9ab..c3f05bd 100644 --- a/external/offliner.lua +++ b/external/offliner.lua @@ -20,6 +20,10 @@ function OnError( sError ) Core.SendToOpChat( sError ) end +if not luasql then + luasql = require "luasql.mysql" +end + _G.tFunction = { Connect = function() local luasql @@ -62,14 +66,18 @@ _G.tFunction = { end, GetCategories = function() - local tReturn, sCategoryQuery = {}, [[SELECT name FROM ctgtable]] - local SQLCur = assert( SQLCon:execute(sCategoryQuery) ) + local tReturn, sCategoryQuery = {}, [[SELECT name FROM ctgtable]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sCategoryQuery) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tReturn, tRow.name ) tRow = SQLCur:fetch( {}, "a" ) end SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, @@ -83,13 +91,17 @@ _G.tFunction = { GetModerators = function() local tReturn, sCategoryQuery = {}, "SELECT nick FROM modtable WHERE active = 'Y' AND deletions < 11 ORDER BY id ASC" - local SQLCur = assert( SQLCon:execute(sCategoryQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sCategoryQuery) ) local tRow = SQLCur:fetch( {}, "a" ) while tRow do table.insert( tReturn, tRow.nick ) tRow = SQLCur:fetch( {}, "a" ) end SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, @@ -102,11 +114,15 @@ _G.tFunction = { sInsertQuery, sIDQuery = [[CALL NewEntry( '%s', '%s', '%s', '%s', '%s', '%s', @eid, @mgid )]], [[SELECT @eid AS entryID, @mgid AS magnetID]] sInsertQuery = sInsertQuery:format( tInput.ctg, tInput.msg, tInput.nick, tInput.tth, tInput.name, tInput.size ) end - local SQLCur = assert( SQLCon:execute(sInsertQuery) ) - SQLCur = assert( SQLCon:execute(sIDQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert(SQLCon1:execute(sInsertQuery) ) + SQLCur = assert( SQLCon1:execute(sIDQuery) ) if type( SQLCur ) ~= "string" then local tRow = SQLCur:fetch( {}, "a" ) SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tRow end return false @@ -114,9 +130,13 @@ _G.tFunction = { FetchRow = function( iID ) local tReturn, sQuery = {}, string.format( "SELECT e.id, c.name AS ctg, e.msg, m.nick AS nick, e.date FROM entries e INNER JOIN ctgtable c ON c.id = e.ctg INNER JOIN modtable m ON m.id = e.nick WHERE e.id = %d LIMIT 1", iID ) - local SQLCur = assert( SQLCon:execute(sQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sQuery) ) tReturn = SQLCur:fetch( {}, "a" ) SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, @@ -135,9 +155,13 @@ _G.tFunction = { ON m2.id = m.nick WHERE m.id = %d LIMIT 1]], iMID ) - local SQLCur = assert( SQLCon:execute(sQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sQuery) ) tReturn = SQLCur:fetch( {}, "a" ) SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return tReturn end, @@ -156,7 +180,9 @@ _G.tFunction = { WHERE eid = %d ORDER BY date DESC LIMIT 5]], "%s. [%s] magnet:?xt=urn:tree:tiger:%s&xl=%s (Added by %s on %s)" - local SQLCur = assert( SQLCon:execute(sQuery:format( iEID )) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sQuery:format( iEID )) ) if SQLCur:numrows() == 0 then return nil end @@ -169,6 +195,9 @@ _G.tFunction = { table.insert( tReturn, sTemplate:format(tRow.id, sSize, tRow.tth, tRow.size, tRow.nick, tRow.date) ) tRow = SQLCur:fetch( {}, "a" ) end + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return table.concat( tReturn, "\n\t" ) end, @@ -268,7 +297,9 @@ _G.tOffliner = { end sSuffix = tConfig.sLatestPage..sSuffix sLatestQuery = sLatestQuery:format( sCategory, iLimit ) - local SQLCur = assert( SQLCon:execute(sLatestQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sLatestQuery) ) if SQLCur:numrows() == 0 then Core.SendPmToUser( tUser, tConfig.sBotName, sPMToUser.."No result was obtained for your query.|" ) return @@ -281,7 +312,10 @@ _G.tOffliner = { sPMToUser = sPMToUser..table.concat( tTemporary, "\n\n" ) Core.SendPmToUser( tUser, tConfig.sBotName, sPMToUser.."\n\nThese results are displayed on the hub-webpage: "..sSuffix.."|" ) if type(SQLCur) ~= "number" then SQLCur:close() end - return true + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() + return true end, s = function( tUser, sSearchString ) @@ -316,13 +350,16 @@ _G.tOffliner = { ORDER BY e.id DESC LIMIT 20 ) AS temp ORDER BY id ASC]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + if #tTemporary == 0 then - sSearchQuery = sSearchQuery:format( SQLCon:escape(sSearchString) ) + sSearchQuery = sSearchQuery:format( SQLCon1:escape(sSearchString) ) else - sSearchQuery = sSearchQuery:format( SQLCon:escape(sSearchString), "'"..table.concat(tTemporary, "', '").."'" ) + sSearchQuery = sSearchQuery:format( SQLCon1:escape(sSearchString), "'"..table.concat(tTemporary, "', '").."'" ) end tTemporary = {} - local SQLCur = assert( SQLCon:execute(sSearchQuery) ) + local SQLCur = assert( SQLCon1:execute(sSearchQuery) ) if SQLCur:numrows() == 0 then Core.SendPmToUser( tUser, tConfig.sBotName, sPMToUser.."No result was obtained for your query.|" ) return true @@ -336,6 +373,8 @@ _G.tOffliner = { sPMToUser = sPMToUser..table.concat( tTemporary, "\n\n" ) Core.SendPmToUser( tUser, tConfig.sBotName, sPMToUser.."\n\nThese search results are linked to: "..sSuffix.."|" ) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -349,7 +388,9 @@ _G.tOffliner = { Core.SendPmToUser( tUser, tConfig.sBotName, "Too long entry" ) return false end - tMagnet.ctg, tMagnet.msg, tMagnet.nick = sCategory, SQLCon:escape( sEntry ), SQLCon:escape( tUser.sNick ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + tMagnet.ctg, tMagnet.msg, tMagnet.nick = sCategory, SQLCon1:escape( sEntry ), SQLCon1:escape( tUser.sNick ) local tOutput = tFunction.InsertProcedure( 'newentry', tMagnet ) if not tOutput then Core.SendPmToUser( tUser, tConfig.sBotName, sError ) @@ -372,11 +413,15 @@ _G.tOffliner = { LEFT JOIN filenames f ON m.id = f.magnet_id WHERE e.id = %d]], iID ) - local SQLCur = assert( SQLCon:execute(sDeleteQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sDeleteQuery) ) if sModNick:lower() ~= tUser.sNick:lower() then - local SQLCur = assert( SQLCon:execute("UPDATE modtable SET deletions = deletions + 2 WHERE nick = '"..SQLCon:escape(sModNick).."'") ) + local SQLCur = assert( SQLCon1:execute("UPDATE modtable SET deletions = deletions + 2 WHERE nick = '"..SQLCon1:escape(sModNick).."'") ) end if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -384,10 +429,14 @@ _G.tOffliner = { local iID, sContent, sUpdateQuery = tonumber(tInput[1]), table.concat(tInput, " ", 2), [[UPDATE entries SET msg = '%s' WHERE id = %d]] - sUpdateQuery = sUpdateQuery:format( SQLCon:escape(sContent), iID ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + sUpdateQuery = sUpdateQuery:format( SQLCon1:escape(sContent), iID ) if not sContent:find( "magnet%:%?" ) then - local SQLCur = assert( SQLCon:execute(sUpdateQuery) ) + local SQLCur = assert( SQLCon1:execute(sUpdateQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true else Core.SendPmToUser( tUser, tConfig.sBotName, "Sorry! You must remove magnet when updating." ) @@ -404,9 +453,13 @@ _G.tOffliner = { added_by = '%s', deletions = 0, date = NOW() ]] - sAddModerator = sAddModerator:format( SQLCon:escape(sModNick), SQLCon:escape(tUser.sNick), SQLCon:escape(tUser.sNick) ) - local SQLCur = assert( SQLCon:execute(sAddModerator) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + sAddModerator = sAddModerator:format( SQLCon1:escape(sModNick), SQLCon1:escape(tUser.sNick), SQLCon:escape(tUser.sNick) ) + local SQLCur = assert( SQLCon1:execute(sAddModerator) ) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -416,22 +469,35 @@ _G.tOffliner = { deletions = 50, date = NOW() WHERE nick = '%s' ]] - sRemoveModerator = sRemoveModerator:format( SQLCon:escape(sModNick) ) - local SQLCur = assert( SQLCon:execute(sRemoveModerator) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + sRemoveModerator = sRemoveModerator:format( SQLCon1:escape(sModNick) ) + local SQLCur = assert( SQLCon1:execute(sRemoveModerator) ) + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return true end, addctg = function( tUser, sCategory ) - local sAddCategory = string.format( "INSERT INTO ctgtable (name) VALUES( '%s')", SQLCon:escape(sCategory) ) - local SQLCur = assert( SQLCon:execute(sAddCategory)) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local sAddCategory = string.format( "INSERT INTO ctgtable (name) VALUES( '%s')", SQLCon1:escape(sCategory) ) + local SQLCur = assert( SQLCon1:execute(sAddCategory)) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, delctg = function( tUser, sCategory ) local sRemoveCategory = "DELETE FROM ctgtable WHERE name = '"..sCategory.."'" - local SQLCur = assert( SQLCon:execute(sRemoveCategory)) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sRemoveCategory)) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -467,9 +533,13 @@ _G.tOffliner = { return false end sMagnetQuery, sNameQuery = sMagnetQuery:format( tMagnet.tth, tMagnet.size, iMID ), sNameQuery:format( iMID, tMagnet.name, tMagnet.name ) - local SQLCur = assert( SQLCon:execute(sMagnetQuery) ) - SQLCur = assert( SQLCon:execute(sNameQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sMagnetQuery) ) + SQLCur = assert( SQLCon1:execute(sNameQuery) ) if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, @@ -478,17 +548,23 @@ _G.tOffliner = { FROM magnets m LEFT JOIN filenames f ON m.id = f.magnet_id - WHERE m.id = %d ]] - local SQLCur = assert( SQLCon:execute(sMagnetQuery:format(iMID)) ) + WHERE m.id = %d ]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local SQLCur = assert( SQLCon1:execute(sMagnetQuery:format(iMID)) ) if sModNick:lower() ~= tUser.sNick:lower() then - local SQLCur = assert( SQLCon:execute("UPDATE modtable SET deletions = deletions + 1 WHERE nick = '"..SQLCon:escape(sModNick).."'") ) + local SQLCur = assert( SQLCon1:execute("UPDATE modtable SET deletions = deletions + 1 WHERE nick = '"..SQLCon1:escape(sModNick).."'") ) end if type(SQLCur) ~= "number" then SQLCur:close() end + SQLCon1:close() + SQLEnv1:close() return true end, StoreMessage = function( sSender, sRecipient, sMessage ) - local tRecipient = Core.GetUser( sRecipient ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + local tRecipient = Core.GetUser( sRecipient ) if tRecipient then Core.SendPmToUser( tRecipient, sSender, sMessage ) Core.SendPmToNick( sSender, tConfig.sBotName, "User was online. Message delivered." ) @@ -496,10 +572,13 @@ _G.tOffliner = { end local sStorageQuery = [[INSERT INTO messages (message, `from`, `to`, dated) VALUES ( '%s', '%s', '%s', NOW() ) ]] - sStorageQuery = sStorageQuery:format( SQLCon:escape(sMessage), SQLCon:escape(sSender), SQLCon:escape(sRecipient) ) - local SQLCur = assert( SQLCon:execute(sStorageQuery) ) + sStorageQuery = sStorageQuery:format( SQLCon1:escape(sMessage), SQLCon1:escape(sSender), SQLCon1:escape(sRecipient) ) + local SQLCur = assert( SQLCon1:execute(sStorageQuery) ) local sReply = "The message has been stored with ID: #%d. It'll be delivered to %s when they connect to hub." - Core.SendPmToNick( sSender, tConfig.sBotName, sReply:format(SQLCon:getlastautoid(), sRecipient) ) + Core.SendPmToNick( sSender, tConfig.sBotName, sReply:format(SQLCon1:getlastautoid(), sRecipient) ) + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() return true end, @@ -510,21 +589,28 @@ _G.tOffliner = { message FROM messages WHERE `to` = '%s' - AND delivered = 'N' ]] - sSearchUserQuery = sSearchUserQuery:format( SQLCon:escape(sNick) ) - local SQLCur = assert( SQLCon:execute(sSearchUserQuery) ) + AND delivered = 'N' ]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'latest')) + sSearchUserQuery = sSearchUserQuery:format( SQLCon1:escape(sNick) ) + local SQLCur = assert( SQLCon1:execute(sSearchUserQuery) ) if SQLCur:numrows() == 0 then - return false + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() + return false end local tRow, sMessage, sEditMessage = SQLCur:fetch( {}, "a" ), "An offline message with ID #%04d was sent to you by %s on %s. The message is: \n\t%s\n\n\tThank you for using offline message services.", "UPDATE messages SET delivered = 'Y' WHERE id = %d" while tRow do - local SQLTemp = assert( SQLCon:execute(sEditMessage:format(tRow.id)) ) + local SQLTemp = assert( SQLCon1:execute(sEditMessage:format(tRow.id)) ) Core.SendPmToNick( sNick, tConfig.sBotName, sMessage:format(tRow.id, tRow.from, tRow.dated, tRow.message) ) tRow = SQLCur:fetch( {}, "a" ) - SQLTemp = nil + SQLTemp:close() end tRow = nil SQLCur:close() + SQLCon1:close() + SQLEnv1:close() end } diff --git a/external/stats/chat.lua b/external/stats/chat.lua index c27ada1..e0d6116 100644 --- a/external/stats/chat.lua +++ b/external/stats/chat.lua @@ -7,6 +7,12 @@ --]] +if not luasql then + luasql = require "luasql.mysql" +end + +Connection = require 'config' + function VerifyBots( sInput ) if sInput:find "^%[BOT%]" then return true @@ -88,9 +94,13 @@ function UpdateUserScore( sNick, sDate, iMain, iPM ) ON DUPLICATE KEY UPDATE `count` = `count` + %d, messages = messages + %d]] - sQuery = sQuery:format( sqlCon:escape(sNick), iMain, iPM, sDate, iMain, iPM ) - local sqlCur = assert( sqlCon:execute(sQuery) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + sQuery = sQuery:format( SQLCon1:escape(sNick), iMain, iPM, sDate, iMain, iPM ) + local sqlCur = assert( SQLCon1:execute(sQuery) ) sqlCur = nil + SQLCon1:close() + SQLEnv1:close() end function UpdateBotStats( sBotName, sDate, iRegs, iUnregs ) @@ -98,10 +108,14 @@ function UpdateBotStats( sBotName, sDate, iRegs, iUnregs ) VALUES ('%s', %d, %d, '%s') ON DUPLICATE KEY UPDATE regs = regs + %d, - unregs = unregs + %d]] - sQuery = sQuery:format( sqlCon:escape(sBotName), iRegs, iUnregs, sDate, iRegs, iUnregs ) - local sqlCur = assert( sqlCon:execute(sQuery) ) + unregs = unregs + %d]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + sQuery = sQuery:format( SQLCon1:escape(sBotName), iRegs, iUnregs, sDate, iRegs, iUnregs ) + local sqlCur = assert( SQLCon1:execute(sQuery) ) sqlCur = nil + SQLCon1:close() + SQLEnv1:close() end function AllTimeTop( iLimit ) @@ -111,9 +125,14 @@ function AllTimeTop( iLimit ) GROUP BY nick ORDER BY `total` DESC LIMIT %d]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) local tTemp, iCounter ={}, 1 - local sqlCur, sResult = assert( sqlCon:execute(sQuery:format(iLimit)) ), ( "\n\n\t\tShowing %d of all time top chatterers\n\n" ):format( iLimit ) + local sqlCur, sResult = assert( SQLCon1:execute(sQuery:format(iLimit)) ), ( "\n\n\t\tShowing %d of all time top chatterers\n\n" ):format( iLimit ) tTemp = List( sqlCur ) + sqlCur:close() + SQLCon1:close() + SQLEnv1:close() return sResult..tFormat.sHeader..table.concat( tTemp, "\n\t" ) end @@ -127,8 +146,13 @@ function DailyTop( iLimit, sDate ) ORDER BY `total` DESC LIMIT %d]] local sDate = ( sDate and "'"..sDate.."'" ) or "CURDATE()" - local sqlCur = assert( sqlCon:execute(sQuery:format(sDate, iLimit)) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + local sqlCur = assert( SQLCon1:execute(sQuery:format(sDate, iLimit)) ) tTemp = List( sqlCur ) + sqlCur:close() + SQLCon1:close() + SQLEnv1:close() return sResult..tFormat.sHeader..table.concat( tTemp, "\n\t" ) end @@ -169,11 +193,18 @@ function NickStats( sNick ) Most recent score of %d on %s Highest activity on %s (with score of %d) -------------------------------------]].."\n\n" - local sqlCur = assert( sqlCon:execute(sQuery:format( sqlCon:escape(sNick) )) ) + + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + local sqlCur = assert( SQLCon1:execute(sQuery:format( SQLCon1:escape(sNick) )) ) tRow = sqlCur:fetch( tRow, "a" ) if not tRow then return "No record was obtained. This user might never have used mainchat/sent a PM." else return sResult:format( tRow.nick, tRow.nick, tRow.total, tRow.avrg, tRow.pm, tRow.recent, tRow.rcntdate, tRow.dated, tRow.highest ) end + sqlCur:close() + SQLCon1:close() + SQLEnv1:close() end diff --git a/external/stats/hubtopic.lua b/external/stats/hubtopic.lua index 427f8cd..8c1fc75 100644 --- a/external/stats/hubtopic.lua +++ b/external/stats/hubtopic.lua @@ -7,11 +7,21 @@ --]] +if not luasql then + luasql = require "luasql.mysql" +end + +Connection = require 'config' NewHubTopic = (function() local sQuery = [[INSERT INTO hubtopics (topic, assignee, dated) VALUES( '%s', '%s', NOW() )]] return function ( sNick, sTopic ) - local sNick, sTopic = sqlCon:escape( sNick ), sqlCon:escape( sTopic ) - assert( sqlCon:execute(sQuery:format( sTopic, sNick )) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + local sNick, sTopic = SQLCon1:escape( sNick ), SQLCon1:escape( sTopic ) + assert( SQLCon1:execute(sQuery:format( sTopic, sNick )) ) + SQLCon1:close() + SQLEnv1:close() end end)() diff --git a/external/stats/toks.lua b/external/stats/toks.lua index 8fb8957..2b2fd69 100644 --- a/external/stats/toks.lua +++ b/external/stats/toks.lua @@ -7,9 +7,16 @@ --]] +if not luasql then + luasql = require "luasql.mysql" +end + +Connection = require 'config' function UpdateUserToks( tUser ) - local sNick = sqlCon:escape(tUser.sNick) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + local sNick = SQLCon1:escape(tUser.sNick) local sQuery = [[INSERT INTO `toks`(`username`, `toks`,`maxtoks`,`maxtoksdate`) VALUES( '%s','%.3f','%.3f',CURDATE() ) ON DUPLICATE KEY @@ -19,8 +26,11 @@ function UpdateUserToks( tUser ) if iSharesize < tToksConfig.iMinShareLimit then return end - iLogSharesize=math.log(iSharesize) - local SQLCur = assert( sqlCon:execute(string.format(sQuery, sNick,iLogSharesize,iLogSharesize,iLogSharesize)) ) + iLogSharesize= math.log(iSharesize) + local SQLCur = assert( SQLCon1:execute(string.format(sQuery, sNick,iLogSharesize,iLogSharesize,iLogSharesize)) ) + SQLCur = nil + SQLCon1:close() + SQLEnv1:close() end function UpdateToks() @@ -35,42 +45,62 @@ end function Inflation() local sQuery = [[UPDATE `toks` SET `toks` = %f*`toks`]] - local SQLCur = assert( sqlCon:execute(sQuery:format(tToksConfig.fInflationConstant)) ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + local SQLCur = assert( SQLCon1:execute(sQuery:format(tToksConfig.fInflationConstant)) ) + SQLCur:close() + SQLCon1:close() + SQLEnv1:close() end function GrantAllowance() - local sQuery1=[[UPDATE `toks` SET `allowance` = CASE WHEN %f*toks < 100 then 100 else %f*toks end ]] - assert( sqlCon:execute(string.format(sQuery1,tToksConfig.fRegUserAllowanceFactor,tToksConfig.fRegUserAllowanceFactor)) ) + local sQuery1=[[UPDATE `toks` SET `allowance` = CASE WHEN %f*toks < 100 then 100 else %f*toks end ]] + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + assert( SQLCon1:execute(string.format(sQuery1,tToksConfig.fRegUserAllowanceFactor,tToksConfig.fRegUserAllowanceFactor)) ) local sQuery2= [[UPDATE `toks` SET `allowance` = `allowance` + %.3f WHERE username = '%s']] local tModerators=RegMan.GetRegsByProfile(4) for i,tMod in ipairs(tModerators) do - assert( sqlCon:execute(sQuery2:format(tToksConfig.fModAllowance,tMod.sNick)) ) + assert( SQLCon1:execute(sQuery2:format(tToksConfig.fModAllowance,tMod.sNick)) ) end local tVips = RegMan.GetRegsByProfile(3) for i,tVip in ipairs(tVips) do - assert( sqlCon:execute(sQuery2:format(tToksConfig.fVipAllowance,tVip.sNick)) ) + assert( SQLCon1:execute(sQuery2:format(tToksConfig.fVipAllowance,tVip.sNick)) ) end local tOperators = RegMan.GetOps() for i,tOp in ipairs(tOperators) do - assert( sqlCon:execute(sQuery2:format(tToksConfig.fOpAllowance,tOp.sNick)) ) + assert( SQLCon1:execute(sQuery2:format(tToksConfig.fOpAllowance,tOp.sNick)) ) end + SQLCon1:close() + SQLEnv1:close() end local function GetAttribute (sNick ,sAttribute) - local sQuery = "SELECT `%s` FROM toks WHERE username='%s'" - sNick = sqlCon:escape( sNick) - tSQLResults = assert( sqlCon:execute(sQuery:format(sAttribute,sNick)) ) + local sQuery = "SELECT `%s` FROM toks WHERE username='%s'" + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + sNick = SQLCon1:escape( sNick) + tSQLResults = assert( SQLCon1:execute(sQuery:format(sAttribute,sNick)) ) tRow = tSQLResults:fetch( {}, "a" ) if tRow and tRow[sAttribute] then return tRow[sAttribute] else return nil end + tSQLResults:close() + SQLCon1:close() + SQLEnv1:close() end function gift(sDonorNick,sDoneeNick,fAmount,sMessage) - sDonorNick=sqlCon:escape(sDonorNick) - sDoneeNick=sqlCon:escape(sDoneeNick) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + sDonorNick=SQLCon1:escape(sDonorNick) + sDoneeNick=SQLCon1:escape(sDoneeNick) fAmount=math.abs(fAmount) if sDonorNick == sDoneeNick then return "You cant gift toks to yourself !!" @@ -82,9 +112,9 @@ function gift(sDonorNick,sDoneeNick,fAmount,sMessage) sDonorReply="\nYou didn't have enough allowance to transfer the requested sum.Whatever was left was transferred" end local sQuery1 = "UPDATE `toks` SET allowance = allowance - %f, toks = toks + %f WHERE username = '%s'" - assert( sqlCon:execute(sQuery1:format(fAmount, fAmount, sDonorNick)) ) + assert( SQLCon1:execute(sQuery1:format(fAmount, fAmount, sDonorNick)) ) local sQuery2 = "INSERT INTO `transactions`(`from`,`to`,`amount`,`date`) VALUES ('%s','%s',%.3f,CURRENT_TIMESTAMP)" - assert( sqlCon:execute(sQuery2:format(sDonorNick,sDoneeNick,fAmount)) ) + assert( SQLCon1:execute(sQuery2:format(sDonorNick,sDoneeNick,fAmount)) ) sDonorReply=sDonorReply..string.format("\n %.2f toks were gifted to %s. You have %.2f allowance left",fAmount,sDoneeNick,fAllowance-fAmount) if string.len(sMessage)~= 0 then @@ -93,14 +123,19 @@ function gift(sDonorNick,sDoneeNick,fAmount,sMessage) sDoneeReply=string.format("You received %.2f toks from %s .",fAmount,sDonorNick) end Core.SendPmToNick(sDoneeNick,tConfig.tBot.sName,sDoneeReply) + SQLCon1:close() + SQLEnv1:close() return sDonorReply end - function CurrentTopToks( iLimit ) +function CurrentTopToks( iLimit ) + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + local sList = "\n\r\t\tRichest Users on HiT Hi FiT Hai \n" sList = sList..string.format( "\t%03s\t%-32s\t%-7s\n\t", "S. No.", "UserName", "Toks" ) local sQuery = [[SELECT username, toks FROM toks ORDER BY toks DESC LIMIT %d]] - tSQLResults = assert( sqlCon:execute(sQuery:format( iLimit )) ) + tSQLResults = assert( SQLCon1:execute(sQuery:format( iLimit )) ) tRow = tSQLResults:fetch ({}, "a") i = 1 while tRow do @@ -115,8 +150,11 @@ end function AllTimeTopToks( iLimit ) local sList = "\n\r\t\tRichest Users of all time on HiT Hi FiT Hai \n" sList = sList..string.format( "\t%10s\t%-30s\t%-10s\t%-25s\n\t", "S. No.", "UserName", "MaxToks" ,"Date") + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + local sQuery = [[SELECT username, maxtoks,maxtoksdate FROM toks ORDER BY maxtoks DESC LIMIT %d]] - tSQLResults = assert( sqlCon:execute( string.format( sQuery, iLimit ) ) ) + tSQLResults = assert( SQLCon1:execute( string.format( sQuery, iLimit ) ) ) tRow = tSQLResults:fetch ({}, "a") i = 1 while tRow do @@ -125,14 +163,20 @@ function AllTimeTopToks( iLimit ) tRow = tSQLResults:fetch ({}, "a") i = i + 1 end + tSQLResults:close() + SQLCon1:close() + SQLEnv1:close() return sList end function Transactions( tUser, sNick ) - local sQuery ="SELECT * FROM transactions WHERE `From`='%s' or`To`='%s' " - sNick=sqlCon:escape(sNick) + local sQuery ="SELECT * FROM transactions WHERE `From`='%s' or`To`='%s' " + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + sNick=SQLCon1:escape(sNick) if tUser.sNick == sNick or tUser.iProfile == 0 then - tSQLResults = assert( sqlCon:execute( string.format( sQuery, sNick,sNick ) ) ) + tSQLResults = assert( SQLCon1:execute( string.format( sQuery, sNick,sNick ) ) ) sList = "Showing transactions for "..sNick sList= sList..string.format( "\n\t%-5s\t%-25s\t%-25s\t%-10s\t%-15s\n\t", "ID","From","To","Amount","Date") tRow = tSQLResults:fetch ({}, "a") @@ -145,12 +189,18 @@ function Transactions( tUser, sNick ) else return ("You cant check transactions of other users") end + tSQLResults:close() + SQLCon1:close() + SQLEnv1:close() end function NickToks( tUser, sNick ) - local sQuery = "SELECT * FROM toks WHERE username='%s'" - sNick = sqlCon:escape(sNick) - tSQLResults = assert( sqlCon:execute( string.format( sQuery, sNick ) ) ) + local sQuery = "SELECT * FROM toks WHERE username='%s'" + local SQLEnv1 = assert(luasql.mysql()) + local SQLCon1 = assert(SQLEnv1:connect(Connection 'stats')) + + sNick = SQLCon1:escape(sNick) + tSQLResults = assert( SQLCon1:execute( string.format( sQuery, sNick ) ) ) tRow = tSQLResults:fetch ({}, "a") if tRow then if tUser.sNick == sNick then @@ -165,4 +215,7 @@ function NickToks( tUser, sNick ) else return ("Toks record for "..sNick.." not found.Wait for some time.") end + tSQLResults:close() + SQLCon1:close() + SQLEnv1:close() end