Module:Commerce form query sandbox

From Caves of Qud Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Commerce form query sandbox/doc

local p = {}
local cargo = mw.ext.cargo

function p.Main(frame)

local tierexist = false
local categoryexist = false
local weightexist = false

local argstring= {"", "", ""}

 
--[Defining table and fields...]--
local table='GeneralData, _pageData'
local fields='GeneralData._pageName=Item,POWER(Commerce,1)/Weight=Value per Pound,Commerce,Weight'
--[Defining individual arguments for later...]--
local joinonargs='GeneralData._pageName=_pageData._pageName'

--[Get user defined fields set]--

--[Get Tier range]--
local mintier = frame.args.mintier
local maxtier = frame.args.maxtier
if mintier ~= nil and mintier ~= '' then
  tierexist = true                                              --[The user wants to specify by tier]
  if maxtier ~= nil and maxtier ~= '' then
    if maxtier < mintier then
      maxtier=mintier   --[if maxtier is smaller than mintier, set equal to mintier, else left alone]
    end
  else 
    maxtier=999999
  end
end
if tierexist ==true then
  argstring[1]='Tier <= \'' .. tostring(maxtier).. '\' AND Tier >= \'' .. tostring(mintier) .. '\''
end

--[Get Weight range]--
local minweight = frame.args.minweight
local maxweight = frame.args.maxweight
if minweight ~= nil and minweight ~= '' then
  weightexist = true                                         --[The user wants to specify by weight]
  if maxweight ~= nil and maxweight ~= '' then
    if maxweight < minweight then
      maxweight=minweight --[if maxweight is smaller than minweight, set equal to minweight, else left alone]
    end
  else 
    maxweight=999999
  end
end
if weightexist == true then
  argstring[2]='Weight <= \'' .. tostring(maxweight).. '\' AND Weight >= \'' .. tostring(minweight) .. '\''
end

--[Concatenate all the where clauses...]--

local concatargstring= ''
for i=1,2 do
  if concatargstring ~= '' and argstring[i]~=nil then
     concatargstring = concatargstring .. ' AND ' .. argstring[i]
  else 
     concatargstring = argstring[i]
  end
end


--[Define args...]--
local args = { join='GeneralData._pagename=_pageData._pageName',
where=concatargstring .. 'AND GeneralData._pageNamespace="0"',
groupBy='GeneralData._pageName',
orderBy='Value per Pound'
}

--[Call query!]--
local result = p.getquery(table, fields, args)
if not next(result) then
  return "Nothing could match the specified results."
else
  --return result
  frame:expandTemplate{title="Commerce form query", args={concatargstring} }

end

end

function p.getquery(table, fields, args)
  return cargo.query(table, fields, args)
end

return p