Module:Cargo

local Cargo = {}

local checkType = require( "libraryUtil" ).checkType local cargoQuery = mw.ext.cargo.query

-- Clear empty string values from a given table and all subtables. -- -- @param tbl local function clearEmptyValues( tbl ) local newTbl = {} for k,v in pairs( tbl ) do   if type( v ) == "table" then newTbl[k] = clearEmptyValues( v ) elseif v ~= "" then newTbl[k] = v   end end

return newTbl end

-- Run a Cargo query. -- -- @param tables -- @param fields -- @param args function Cargo.query( tables, fields, args ) checkType( "query", 1, tables, "string" ) checkType( "query", 2, fields, "string" ) checkType( "query", 3, args, "table", true )

local results = cargoQuery( tables, fields, args ) results = clearEmptyValues( results )

return results end

-- Store values to a Cargo table. -- -- @param dbtable -- @param values function Cargo.store(dbtable, values) checkType( "store", 1, dbtable, "string" ) checkType( "store", 2, values, "table" ) local data = {} for k,v in pairs( values ) do		k = tostring( k ) if type(v) == "boolean" then data[k] = v and 1 or 0 else data[k] = tostring( v ) end end local frame = mw.getCurrentFrame return frame:callParserFunction{name = "#cargo_store:_table=" .. dbtable, args = data} end

return Cargo