Module:CargoQuery: Difference between revisions

664 bytes added ,  22:04, 2 September 2020
m
no edit summary
(change meta separator thru arguments)
mNo edit summary
Line 10: Line 10:
local p = {}
local p = {}
function p.main(frame)
function p.main(frame)
function p.main(frame)
local args = h.merge()
local args = h.merge()
result = p.query(args)
local query = {}
 
for k, v in pairs(args) do
        local tbl = {}
if string.sub(k, 0, 2) == 'q?' then
local key = string.sub(k, 3)
query[PARAM_LOOKUP[key] or key] = v
elseif PARAM_LOOKUP[k] then
query[PARAM_LOOKUP[k]] = v
else
query[k] = v
end
end
if args.one_to_many then
query.fields = query.fields .. ',' .. query.one_to_many
end
local result = mw.ext.cargo.query(query.tables, query.fields, query)
if #result == 0 then
return frame:preprocess(args.default or '')
end
if args.one_to_many then
result = h.groupOneToManyFields(result, h.getOneToManyTableFromArgs(args))
h.concatOneToManyFieldsInEachRow(result, args.one_to_many_sep or ',')
end
local tbl = {}
for i, row in ipairs(result) do
for i, row in ipairs(result) do
row.index = i
row.index = i
Line 22: Line 45:
local outro = frame:preprocess(args.outro or '')
local outro = frame:preprocess(args.outro or '')
return intro .. table.concat(tbl,args.delimiter or '') .. outro
return intro .. table.concat(tbl,args.delimiter or '') .. outro
end
end
end