Halo Esports Wiki
[checked revision][checked revision]
m (Syncing content across wikis)
m (Syncing content across wikis)
Line 47: Line 47:
 
else
 
else
 
return nil
 
return nil
  +
end
  +
end
  +
  +
function p.getOneRow(tbl, fields, query)
  +
local result = mw.ext.cargo.query(tbl, table.concat(fields,','),query)
  +
if result[1] then
  +
for k, v in pairs(result[1]) do
  +
if v == '' then
  +
result[1][k] = nil
  +
end
  +
end
  +
return result[1]
  +
else
  +
return {}
 
end
 
end
 
end
 
end

Revision as of 06:12, 31 August 2018

To edit the documentation or categories for this module, click here.


-- CargoUtil
local p = {}

-- modifies a pre-existing query to add an extra set of conditions to get the max/min value of some field
-- order will be either MIN or MAX, and orderby is usually going to be a date/datetime
-- example: c.makeMinMaxQuery(cargoquery, 'SP.Champion','SP.Time','MAX')
--to get the most-recent played champions
function p.makeMinMaxQuery(cargoquery, field, orderby, order)
	result = mw.ext.cargo.query(cargoquery.tables,
		string.format("%s(%s)=thisvalue, %s=thisfield",
			order,
			orderby,
			field
		),
		cargoquery)
	local newcondition = {}
	if not next(result) then
		return cargoquery.where
	end
	for _, row in ipairs(result) do
		newcondition[#newcondition+1] = string.format(
			'(%s="%s" AND %s="%s")',
			field,
			row.thisfield,
			orderby,
			row.thisvalue
		)
	end
	local newwhere = {
		string.format("(%s)",table.concat(newcondition, ' OR ')),
	}
	if cargoquery.where and cargoquery.where ~= '' then
		newwhere[2] = string.format("(%s)",cargoquery.where)
	end
	local cargowhere = table.concat(newwhere, ' AND ')
	return cargowhere
end

function p.getOneResult(tbl, field, query)
	local result = mw.ext.cargo.query(tbl, field, query)
	if result[1] then
		if result[1][field] == '' then
			return nil
		else
			return result[1][field]
		end
	else
		return nil
	end
end

function p.getOneRow(tbl, fields, query)
	local result = mw.ext.cargo.query(tbl, table.concat(fields,','),query)
	if result[1] then
		for k, v in pairs(result[1]) do
			if v == '' then
				result[1][k] = nil
			end
		end
		return result[1]
	else
		return {}
	end
end

return p