Module:VarQuery

local p = {} local cargo = mw.ext.cargo local getArgs = require( 'Module:Arguments' ).getArgs local vardefine = require( 'Module:Variables' ).vardefine

local i18n = { error = { multiple_results = 'More than one result.', no_results = 'No results found.', no_table = 'No table given', } }

function p.main( frame ) local args = getArgs( frame ) local tables = args['table'] or args['tables'] local fields = args['field'] or args['fields'] or '_pageName'

assert( tables, i18n.error.no_table ) local data = cargo.query( tables, fields, {   where = args['where'],    join = args['join on'],    groupBy = args['group by'],    having = args['having'],    orderBy = args['order by'],    limit = args['limit']  } )

if #data ~= 1 then if #data == 0 then error( i18n.error.no_results ) else error( i18n.error.multiple_results ) end end

for key, value in pairs( data[1] ) do   local var_key = ( args['prefix'] or 'cargo_' ) .. key vardefine( var_key, value ) end end

return p