Module:Sandbox/KickahaOta/QueryDistances
Jump to navigation
Jump to search
You might want to create a documentation page for this module.
Editors can experiment in this module's sandbox and testcases pages.
Please add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Please add categories to the /doc subpage. Subpages of this module.
local getArgs = require('Module:Arguments').getArgs
local m_util = require('Module:Util')
local m_cargo = require('Module:Cargo')
local m_tablecalc = {}
m_tablecalc.simpleval = 23
function m_tablecalc.computedistance(query_results, args)
-- Takes 2 arguments:
-- query_results - table returned from m_cargo.query
-- args
-- idfield - string; the name of the field containing a row's id for connection purposes (for example, "passive_skills.id")
-- connectionidsfield - string: the name of the field containing the ids of the rows that a given row connects to
-- (for example, "passive_skills.connections")
-- connectionidsdelimiter - string; the delimeter separating ids in the connectiond id field (for example, ",")
-- originrowid - string; the id of a row that should be used as the starting point of the calculation. (For example,
-- "AscendancyBerserkerStart" to start at the starting node in the Berserker's Ascendancy tree.)
-- distancefield - string; the name of the field that should hold each row's calculated distance from the starting
-- point. (This will be an integer. The row identified by originrowid will have a distance value of 0; the rows
-- connected to that row will have a distance value of 1; the rows connected to those rows will have a distance value
-- of 2; and so on.)
-- Returns: A table that is a copy of the table in query_results, except that each row will have the field identified by
-- distancefield set to the calculated distance. The field will be created if it does not already exist in each row.
-- ### error-check arguments
-- Start by creating two tables:
-- calc_results: A copy of query_results, where we'll eventually insert the computed distances into each row.
-- calculables: A table containing the indexes of rows in calcResults where we now know the distance for that row, but we
-- haven't yet used that knowledge to set the distances of the other rows connected to it. At first, this should contain
-- a single index -- the index of the row identified by originrowid.
local calc_results = {}
local calculables = {}
for i, row in ipairs(query_results) do
calc_results[i] = row
if calc_results[i][args.idfield] == nil then
error("No idfield")
end
if calc_results[i][args.idfield] == args.originrowid then
calc_results[i][args.distancefield] = 0
table.insert(calculables, i)
else
calc_results[i][args.distancefield] = nil
end
end
if #calculables == 0 then
error("Origin row not found")
elseif #calculables > 1 then
error("Multiple origin rows found")
end
return calc_results
end
function m_tablecalc.testquery()
local results = m_cargo.query(
{'passive_skills'},
{'passive_skills.name', 'passive_skills.id', 'passive_skills.connections', 'passive_skills.ascendancy_class', 'CONCAT("")=distance'},
{where='passive_skills.ascendancy_class="Berserker"', order_by='passive_skills.id'}
)
local calc_args = {
idfield = "passive_skills.id",
connectionidsfield="passive_skills.connections",
connectionidsdelimiter=",",
originrowid="AscendancyBerserkerStart",
distancefield="distance"
}
local calc_results = m_tablecalc.computedistance(results, calc_args)
return calc_results
end
return m_tablecalc