Module:CargoUtil
Jump to navigation
Jump to search
local argsUtil = require("Module:ArgsUtil")
local cargo = mw.ext.cargo
local p = {}
-- Function to escape strings for SQL
function p.escapeString(input)
input = mw.text.nowiki(input)
input = mw.ustring.gsub(input, "'", "''")
return input
end
-- Function to generate the Cargo Store parser function call
local function generateCargoStoreCall(tableName, data)
local args = { '_table=' .. tableName }
for k, v in pairs(data) do
table.insert(args, k .. '=' .. p.escapeString(v))
end
return args
end
-- Main function to store data
function p.storeData(args)
local title = mw.title.getCurrentTitle()
-- Check if we are in the Template namespace
if title.namespace == 10 then
return 'Error: Storing data is not allowed in the Template namespace.'
end
local tableName = args._table
args._table = nil -- Remove the table name from args
if not tableName then
return 'Error: Table name is required.'
end
local cargoArgs = generateCargoStoreCall(tableName, args)
-- Prepare the parser function call
local result = mw.getCurrentFrame():callParserFunction('#cargo_store', cargoArgs)
return result or 'Data stored successfully.'
end
-- Public function to query data from a Cargo table
function p.queryData(options)
local cargoQueryOptions = {
tables = options.tables,
fields = options.fields,
join = options.join or "",
where = options.where or "",
limit = options.limit or 100,
orderBy = options.orderBy or "",
groupBy = options.groupBy or ""
}
local results = cargo.query(cargoQueryOptions.tables, cargoQueryOptions.fields, cargoQueryOptions)
return results
end
return p