BryghtShadow (talk | contribs) (allow join table) |
BryghtShadow (talk | contribs) (increment variable on query.) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
+ | local h = {} |
||
function p.query(query) |
function p.query(query) |
||
Line 22: | Line 23: | ||
end |
end |
||
local rows = mw.ext.cargo.query(tables, fields, query) |
local rows = mw.ext.cargo.query(tables, fields, query) |
||
+ | h.incrementQueryCounter() |
||
for i, row in ipairs(rows) do |
for i, row in ipairs(rows) do |
||
for k,v in pairs(row) do |
for k,v in pairs(row) do |
||
Line 33: | Line 35: | ||
function p.rawquery(tables, fields, args) |
function p.rawquery(tables, fields, args) |
||
− | + | local rows = mw.ext.cargo.query(tables, fields, args) |
|
+ | h.incrementQueryCounter() |
||
+ | return rows |
||
end |
end |
||
Line 49: | Line 53: | ||
args = tbl |
args = tbl |
||
} |
} |
||
+ | end |
||
+ | |||
+ | function h.incrementQueryCounter() |
||
+ | -- count the number of times "cargo_query" is called |
||
+ | local name = 'cargo query count' |
||
+ | return h.setVar(name, (h.getVar(name) or 0) + 1) |
||
+ | end |
||
+ | |||
+ | -- Module:VarsUtil.getVar |
||
+ | function h.getVar(var) |
||
+ | local val = mw.getCurrentFrame():callParserFunction('#var', var) |
||
+ | if val == '' then |
||
+ | return nil |
||
+ | end |
||
+ | return val |
||
+ | end |
||
+ | -- Module:VarsUtil.setVar |
||
+ | function h.setVar(var, val) |
||
+ | mw.getCurrentFrame():callParserFunction('#vardefine:' .. var, val) |
||
+ | return val |
||
end |
end |
||
Revision as of 01:37, 9 February 2021
To edit the documentation or categories for this module, click here.
local p = {}
local h = {}
function p.query(query)
local tables = query.tables
if type(tables) == 'table' then
tables = table.concat(tables, ',')
end
local fields = query.fields
if type(fields) == 'table' then
fields = table.concat(fields, ',')
end
if type(query.join) == 'table' then
query.join = table.concat(query.join, ',')
end
query.limit = query.limit or 9999
if type(query.where) == 'table' then
local arr = {}
for _, v in pairs(query.where) do
arr[#arr+1] = string.format('(%s)', v)
end
query.where = #arr > 0 and '('..table.concat(arr, ' AND ')..')' or nil
end
local rows = mw.ext.cargo.query(tables, fields, query)
h.incrementQueryCounter()
for i, row in ipairs(rows) do
for k,v in pairs(row) do
if v == '' then
row[k] = nil
end
end
end
return rows
end
function p.rawquery(tables, fields, args)
local rows = mw.ext.cargo.query(tables, fields, args)
h.incrementQueryCounter()
return rows
end
function p.store(values)
local tbl = {''}
for k,v in pairs(values) do
if type(v) == 'boolean' then
tbl[k] = v and 'Yes' or 'No'
else
tbl[k] = v
end
end
return mw.getCurrentFrame():callParserFunction{
name = '#cargo_store',
args = tbl
}
end
function h.incrementQueryCounter()
-- count the number of times "cargo_query" is called
local name = 'cargo query count'
return h.setVar(name, (h.getVar(name) or 0) + 1)
end
-- Module:VarsUtil.getVar
function h.getVar(var)
local val = mw.getCurrentFrame():callParserFunction('#var', var)
if val == '' then
return nil
end
return val
end
-- Module:VarsUtil.setVar
function h.setVar(var, val)
mw.getCurrentFrame():callParserFunction('#vardefine:' .. var, val)
return val
end
return p