Skip to content

Commit

Permalink
Replace flaky persistent connection with connection per query
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hjpotter92 committed May 23, 2015
1 parent 2f27539 commit 3265fc0
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 99 deletions.
92 changes: 68 additions & 24 deletions external/info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -86,6 +102,9 @@ _G.tFunction = {
else
sReturn = ""
end
SQLCur:close()
SQLCon1:close()
SQLEnv1:close()
return sReturn
end,

Expand Down Expand Up @@ -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) )
Expand All @@ -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) )
Expand All @@ -174,22 +195,23 @@ _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) )
tRow = SQLCur:fetch( {}, "a" )
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,

Expand All @@ -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
Expand All @@ -209,37 +233,51 @@ _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 )
if Core.GetUser( sRecipient ) then
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,

Expand All @@ -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
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 3265fc0

Please sign in to comment.