Module:CargoQuery/doc

From Terraria Wiki
Jump to navigation Jump to search

Lua.svgThis is the documentation page for Module:CargoQuery.


This module lets you get around Cargo bugs with html encoding.

To use, preface query arguments with q?. Even if you are only using one table or one field, use |q?tables= or |q?fields=.

Use Lua names of all query parameters, so |q?join=, |q?groupBy, etc.

Each method has several other parameters, which are detailed in their respective sections.

Methods

main

An example of the "no html" bug

Use this method to get around the bug with |no html|format=template. When using {{#cargo_query:tables=...|no html|template|template=... in templates, you may see html code on the page.

{{#invoke:CargoQuery|main
|q?tables= 	<!-- corresponds to table / tables -->
|q?join= 	<!-- corresponds to join on -->
|q?fields= 	<!-- corresponds to fields -->
|q?where= 	<!-- corresponds to where -->
|q?groupBy= 	<!-- corresponds to group by -->
|q?having= 	<!-- corresponds to having -->
|q?orderBy= 	<!-- corresponds to order by -->
|q?limit= 	<!-- corresponds to limit -->
|template=      <!-- pagename of template (required) -->
|delimiter=
|default=
}}

For simplicity of code, the named args parameter is required to be Yes, and you do not need to specify it.

Unlike |format=template, this wrapper will NOT rename parameters with underscores in them to use spaces instead.

This method can also be called from other modules, with arguments table as first parameter. For example:

local cargoQuery = require('Module:cargoQuery')
local tableRows = cargoQuery.main({['q?tables']=<tables>, ["q?fields"]=<fields>, template="cargoRow", default="no result", ... })

list

Use this method to get around the encoded html entity bug with |format=list. For example, you may see &#039; instead of ' in the result.

{{#invoke:CargoQuery|main
|q?tables= 	<!-- corresponds to table / tables -->
|q?join= 	<!-- corresponds to join on -->
|q?fields= 	<!-- corresponds to fields -->
|q?where= 	<!-- corresponds to where -->
|q?groupBy= 	<!-- corresponds to group by -->
|q?having= 	<!-- corresponds to having -->
|q?orderBy= 	<!-- corresponds to order by -->
|q?limit= 	<!-- corresponds to limit -->
|delimiter=
|default=
}}

Only the first field in the results will be used.

This method can be called from other modules, with arguments table as first parameter. For example:

local cargoQuery = require('Module:cargoQuery')
local listString = cargoQuery.list({['q?tables']=<tables>, ["q?fields"]=<field>, delimiter="/", default="no result", ... })

custom

This method can be called from other modules only. It accepts a callback function, can be used to customize result row formatting. For example:

local cargoQuery = require('Module:cargoQuery')
local output = cargoQuery.custom(
    {['q?tables']=<tables>, ["q?fields"]=<field>, delimiter="<br/>", default="no result", ... }, 
    function(row, args, frame)
        local tbl = {}
        for k, v in pairs(row) do
	        tbl[#tbl+1] = k .. ":" .. frame:expandTemplate{ title = "tr", args = {v} }
	    end
	    return table.concat(tbl, args.fieldsep or ',')
    end
)

afExport

Export the query result as an ArrayFunctions Array. This method can be used in templates only.

{{#invoke:CargoQuery|afExport
|q?tables= 	<!-- corresponds to table / tables -->
|q?join= 	<!-- corresponds to join on -->
|q?fields= 	<!-- corresponds to fields -->
|q?where= 	<!-- corresponds to where -->
|q?groupBy= 	<!-- corresponds to group by -->
|q?having= 	<!-- corresponds to having -->
|q?orderBy= 	<!-- corresponds to order by -->
|q?limit= 	<!-- corresponds to limit -->
|plainList = <!-- a fieldName in q?fields list-->
}}

The return array is a 2-dimensional array by default, for example(represented in JSON format):

{
    {name: 'John', age: 56},
    {name: 'Bob', age: 24},
    {name: 'Harry', age: 12}
}

If `plainList` is specified, the return array is a 1-dimensional array with values from that field only. In the above example, if `|plainList=name` is specified, the return array is:

{ 'John', 'Bob', 'Harry' }

Dependencies