Module:User:Pival13/Cargo

local List = require 'Module:ListUtil' local cargo = mw.ext.cargo local DistributionData = require 'Module:ItemDistribution/data' local sources = DistributionData.sources

local getQueryParams = function (rewardSource) local cargoTables = {('%s=R'):format(rewardSource.rewardTable)} local cargoJoin = {} local cargoWhere = {rewardSource.cond} local xfield = 'R.StartTime' if rewardSource.eventTable then cargoTables[#cargoTables + 1] = ('%s=EV'):format(rewardSource.eventTable) cargoJoin[#cargoJoin + 1] = rewardSource.useWikiName and 'R.WikiName=EV.WikiName' or 'R._pageName=EV._pageName' if rewardSource.timeTable then cargoTables[#cargoTables + 1] = ('%s=T'):format(rewardSource.timeTable) cargoJoin[#cargoJoin + 1] = rewardSource.useWikiName and 'EV.WikiName=T.WikiName' or 'EV._pageName=T._pageName' xfield = 'IFNULL(R.StartTime,T.StartTime)' else xfield = 'IFNULL(R.StartTime,EV.StartTime)' end end return { tables = table.concat(cargoTables, ','), fields = ("R.Amount=count,MIN(%s)=t0"):format(xfield), join = #cargoJoin > 0 and table.concat(cargoJoin, ',') or nil, where = table.concat(List.map(cargoWhere, function (v) return '(' .. v .. ')' end), ' AND '), groupBy = 'R._ID', limit = '5000' } end

local compound = function (args, frame) local args = {} for _, source in pairs(sources) do		local params = getQueryParams(source) args[#args + 1] = ("tables=%s;fields=%s;where=%s;group by=%s;limit=5000"):format(params.tables, params.fields, params.where, params.groupBy) if params.join then args[#args] = args[#args] .. ";join on=" .. params.join end end return tostring(frame:callParserFunction { name = '#cargo_compound_query', args = args }) end

local multiQuery = function (args, frame) local s = "" for _, source in pairs(sources) do		local args = getQueryParams(source) local query = cargo.query(args.tables, args.fields, args) s = s .. tostring(query) end return s end

return require 'Module:MakeMWModule'.makeMWModule { compoundQuery = compound, multiQuery = multiQuery }