Module:VarQuery

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

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' local prefix = args['prefix'] or 'cargo_'

assert( tables, i18n.error.no_table )

-- Clear already existing variables that could interfere with the new ones. for _,field in ipairs( split( fields, '%s*,%s*' ) ) do   local var_key = prefix .. field if varexists( var_key ) then vardefine( var_key, '' ) end end 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 = prefix .. key vardefine( var_key, value ) end end

return p