Модуль:CargoQuery

Материал из Terraria Wiki
Перейти к навигации Перейти к поиску
См. также этот модуль на английском языке: Module:CargoQuery. В нём может содержаться более полная или подробная информация.

Документация для этого модуля отсутствует. Создайте её.


local query = function(args)
	local query = {}
	for k, v in pairs(args) do
		if string.sub(k, 0, 2) == 'q?' then
			query[string.sub(k, 3)] = v
		end
	end
	return mw.ext.cargo.query(query.tables, query.fields, query)
end

local parse = function(x)
	local currentFrame = mw.getCurrentFrame()
	if x == currentFrame then
		-- being called from template, x is the frame object.
		return require('Module:ProcessArgs').merge(currentFrame.args, currentFrame:getParent().args, true), currentFrame
	else
		-- being called from module, x is the args table.
		return x, currentFrame
	end
end

local proc = function(x, rowHandle)
	local args, frame = parse(x)
	local result = query(args)
	if not next(result) then
		return args.default and frame:preprocess(args.default) or ''
	end
	local tbl = {}
	for _, row in ipairs(result) do
		tbl[#tbl+1] = rowHandle(row, args, frame)
	end
	return table.concat(tbl, args.delimiter or '')
end

------------------------------------------------------------------------

return {
	-- only for templates, export the query result as an arrayFunctions array.
	afExport = function(x)
		local args, frame = parse(x)
		local result = query(args)
		if args.plainList then
			local field = args.plainList
			local tbl = {}
			for _, row in ipairs(result) do
				tbl[#tbl+1] = row[field]
			end
			return mw.af.export(tbl)
		else
			return mw.af.export(result)
		end
	end,
	
	-- from template: {{#invoke:CargoQuery|main|q?tables=<tables>|q?fields=<fields>| ... }}
	-- from module: require('Module:cargoQuery').main({['q?tables']=<tables>, ["q?fields"]=<fields>, ... })
	main = function(x)
		return proc(x, function(row, args, frame)
			return frame:expandTemplate{ title = args.template, args = row }
		end)
	end,
	
	-- from template: {{#invoke:CargoQuery|list|q?tables=<tables>|q?fields=<field>| ... }}
	-- from module: require('Module:cargoQuery').list({['q?tables']=<tables>, ["q?fields"]=<field>, ... })
	list = function(x)
		return proc(x, function(row, args, frame)
			for _,v in pairs(row) do
				return v
			end
		end)
	end,
	
	-- from module only: require('Module:cargoQuery').callback(args, function(row, args, frame) --[[do something]] end)
	custom = proc,
}