<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="de">
	<id>https://www.verzauberte-welten.de/index.php?action=history&amp;feed=atom&amp;title=Modul%3ACargoUtil</id>
	<title>Modul:CargoUtil - Versionsgeschichte</title>
	<link rel="self" type="application/atom+xml" href="https://www.verzauberte-welten.de/index.php?action=history&amp;feed=atom&amp;title=Modul%3ACargoUtil"/>
	<link rel="alternate" type="text/html" href="https://www.verzauberte-welten.de/index.php?title=Modul:CargoUtil&amp;action=history"/>
	<updated>2026-11-01T00:13:58Z</updated>
	<subtitle>Versionsgeschichte dieser Seite in Verzauberte Welten e.V.</subtitle>
	<generator>MediaWiki 1.39.8</generator>
	<entry>
		<id>https://www.verzauberte-welten.de/index.php?title=Modul:CargoUtil&amp;diff=4393&amp;oldid=prev</id>
		<title>imported&gt;Oetterer am 11. März 2022 um 09:48 Uhr</title>
		<link rel="alternate" type="text/html" href="https://www.verzauberte-welten.de/index.php?title=Modul:CargoUtil&amp;diff=4393&amp;oldid=prev"/>
		<updated>2022-03-11T09:48:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Neue Seite&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
local getArgs = require(&amp;#039;Module:Arguments&amp;#039;).getArgs&lt;br /&gt;
local classDebug = require(&amp;#039;Module:Debug/class&amp;#039;)&lt;br /&gt;
local _TT = require(&amp;#039;Module:TableTools&amp;#039;)&lt;br /&gt;
local cargo&lt;br /&gt;
if mw.ext and mw.ext.cargo then&lt;br /&gt;
	cargo = mw.ext.cargo&lt;br /&gt;
else&lt;br /&gt;
	cargo = {}&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function prepareQueryParam(query)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.prepareQueryParam&amp;#039;, &amp;#039;CargoUtil&amp;#039;)&lt;br /&gt;
	if query.tables and #query.tables &amp;gt; 0 and query.fields and #query.fields &amp;gt; 0 then&lt;br /&gt;
		if type(query.tables) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			query.tables = table.concat(query.tables, &amp;#039;, &amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		if not query.joins then&lt;br /&gt;
			query.joins = query.join&lt;br /&gt;
		end&lt;br /&gt;
		if query.joins and type(query.joins) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			query.joins = table.concat(query.joins, &amp;#039;, &amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		if type(query.fields) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			query.fields = table.concat(query.fields, &amp;#039;, &amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		if query.where and type(query.where) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			if query.where[1] then	-- sequence assumed&lt;br /&gt;
				query.where = table.concat(query.where, &amp;#039; AND &amp;#039;)&lt;br /&gt;
			else&lt;br /&gt;
				local where = &amp;#039;&amp;#039;&lt;br /&gt;
				for k, v in pairs(query.where) do&lt;br /&gt;
					where = where .. k .. &amp;#039;=&amp;#039; .. v .. &amp;#039; AND &amp;#039;&lt;br /&gt;
				end&lt;br /&gt;
				query.where = where .. &amp;#039;1 &amp;#039;&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		if query.group and type(query.group) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			query.group = table.concat(query.group, &amp;#039;, &amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		if query.having and type(query.having) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			if query.having[1] then	-- sequence assumed&lt;br /&gt;
					query.having = table.concat(query.having, &amp;#039;, &amp;#039;)&lt;br /&gt;
			else&lt;br /&gt;
				local having = &amp;#039;&amp;#039;&lt;br /&gt;
				for k, v in pairs(query.having) do&lt;br /&gt;
					having = having .. k .. &amp;#039;=&amp;#039; .. v .. &amp;#039;, &amp;#039;&lt;br /&gt;
				end&lt;br /&gt;
				query.having = mw.ustring.sub(having, 1, -3)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		if query.order and type(query.order) == &amp;#039;table&amp;#039; then&lt;br /&gt;
			query.order = table.concat(query.order, &amp;#039;, &amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		return query&lt;br /&gt;
	else&lt;br /&gt;
		if tables and #tables &amp;gt; 0 then&lt;br /&gt;
			error(&amp;#039;Argument query.tables is missing or empty! (calling from &amp;#039; .. tostring(self) .. &amp;#039;)&amp;#039;, 2)&lt;br /&gt;
		else&lt;br /&gt;
			error(&amp;#039;Argument query.fields is missing or empty! (calling from &amp;#039; .. tostring(self) .. &amp;#039;)&amp;#039;, 2)&lt;br /&gt;
		end&lt;br /&gt;
		return nil&lt;br /&gt;
	end	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.attach(dbtable)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.attach&amp;#039;, &amp;#039;CargoUtil.attach&amp;#039;)&lt;br /&gt;
	classDebug:log(22, &amp;#039; with table &amp;quot;&amp;#039; .. dbtable .. &amp;#039;&amp;quot;&amp;#039;)&lt;br /&gt;
	local frame = mw.getCurrentFrame()&lt;br /&gt;
	local ret = false&lt;br /&gt;
	ret = frame:callParserFunction(&amp;#039;#cargo_attach:_table=&amp;#039; .. dbtable)&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.declare(dbtable, dbfields)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.declare&amp;#039;, &amp;#039;CargoUtil.declare&amp;#039;)&lt;br /&gt;
	classDebug:log(22, &amp;#039; with table &amp;quot;&amp;#039; .. dbtable .. &amp;#039;&amp;quot; and fields &amp;#039; ..&lt;br /&gt;
		(type(dbfields) == &amp;quot;string&amp;quot; and &amp;#039;&amp;quot;&amp;#039; .. dbfields .. &amp;#039;&amp;quot;&amp;#039; or &amp;#039;of type &amp;#039; .. type(dbfields)), &amp;#039;CargoUtil.declare&amp;#039;)&lt;br /&gt;
	if type(dbfields) == &amp;quot;table&amp;quot; then&lt;br /&gt;
		classDebug:log(23, &amp;#039; datatable: &amp;#039; .. _TT.printTable(dbfields), &amp;#039;CargoUtil.declare&amp;#039;)&lt;br /&gt;
	end&lt;br /&gt;
	local frame = mw.getCurrentFrame()&lt;br /&gt;
	local ret = false&lt;br /&gt;
	if type(dbfields) == &amp;quot;string&amp;quot; then&lt;br /&gt;
		ret = frame:callParserFunction(&amp;#039;#cargo_declare:_table=&amp;#039; .. dbtable, dbfields)&lt;br /&gt;
	elseif type(dbfields) == &amp;quot;table&amp;quot; then&lt;br /&gt;
		ret = frame:callParserFunction{name = &amp;#039;#cargo_declare:_table=&amp;#039; .. dbtable, args = dbfields}&lt;br /&gt;
	end&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.query(query, attributes, retainBlanks)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.query&amp;#039;, &amp;#039;CargoUtil.query&amp;#039;)&lt;br /&gt;
	classDebug:log(23, &amp;#039; with query: &amp;#039; .. (query and _TT.printTable(query) or &amp;#039;NONE&amp;#039;), &amp;#039;CargoUtil.query&amp;#039;)&lt;br /&gt;
	classDebug:log(23, &amp;#039; with attributes: &amp;#039; .. (attributes and _TT.printTable(attributes) or &amp;#039;NONE&amp;#039;), &amp;#039;CargoUtil.query&amp;#039;)&lt;br /&gt;
	local attributes = attributes or {}&lt;br /&gt;
	if type(attributes) ~= &amp;#039;table&amp;#039; then&lt;br /&gt;
		attributes = {attributes}&lt;br /&gt;
	end&lt;br /&gt;
	-- CargoUtil.query() allows for query.paramters (like table, where, etc) to be tables. cargo.query only takes strings. remedy this here&lt;br /&gt;
	local query = prepareQueryParam(query)&lt;br /&gt;
	local args = {&lt;br /&gt;
		where = query.where,&lt;br /&gt;
		join = query.joins,&lt;br /&gt;
		groupBy = query.group,&lt;br /&gt;
		having = query.having,&lt;br /&gt;
		orderBy = query.order,&lt;br /&gt;
		limit = attributes.limit&lt;br /&gt;
	}&lt;br /&gt;
	local result = cargo.query(query.tables, query.fields, args)&lt;br /&gt;
&lt;br /&gt;
	local ret = {}&lt;br /&gt;
	for num, row in pairs(result) do&lt;br /&gt;
		local newRow = {}&lt;br /&gt;
		for key, value in pairs(row) do&lt;br /&gt;
			if mw.ustring.len(mw.text.trim(value)) &amp;gt; 0 or retainBlanks then&lt;br /&gt;
				newRow[key] = value&lt;br /&gt;
				-- cargo interprets underscored (_) as spaces in fieldnames (keys). so copy the entry&lt;br /&gt;
				if mw.ustring.find(key, &amp;#039; &amp;#039;) then&lt;br /&gt;
					local newKey = mw.ustring.gsub(key, &amp;#039; &amp;#039;, &amp;#039;_&amp;#039;)&lt;br /&gt;
					if not newRow[newKey] then&lt;br /&gt;
						newRow[newKey] = newRow[key]&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		ret[num] = newRow&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.rawquery(query, attributes)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.rawquery&amp;#039;, &amp;#039;CargoUtil.rawquery&amp;#039;)&lt;br /&gt;
	classDebug:log(23, &amp;#039; with query: &amp;#039; .. (query and _TT.printTable(query) or &amp;#039;NONE&amp;#039;), &amp;#039;CargoUtil.rawquery&amp;#039;)&lt;br /&gt;
	classDebug:log(23, &amp;#039; with attributes: &amp;#039; .. (attributes and _TT.printTable(attributes) or &amp;#039;NONE&amp;#039;), &amp;#039;CargoUtil.rawquery&amp;#039;)&lt;br /&gt;
	local query = prepareQueryParam(query)&lt;br /&gt;
	local frame = mw.getCurrentFrame()&lt;br /&gt;
	local tables = query.tables&lt;br /&gt;
	local arguments = attributes or {}&lt;br /&gt;
	arguments.fields = query.fields&lt;br /&gt;
	arguments[&amp;#039;join on&amp;#039;] = query.joins&lt;br /&gt;
	arguments.where = query.where&lt;br /&gt;
	arguments[&amp;#039;group by&amp;#039;] = query.group&lt;br /&gt;
	arguments.having = query.having&lt;br /&gt;
	arguments[&amp;#039;order by&amp;#039;] = query.order&lt;br /&gt;
	classDebug:log(24, &amp;#039; calling parser function with tables: &amp;#039; .. tables .. &amp;#039; and arguments: &amp;#039; .. _TT.printTable(arguments))&lt;br /&gt;
	return frame:callParserFunction{ name=&amp;#039;#cargo_query:tables=&amp;#039; .. tables, args = arguments }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.store(dbtable, data)&lt;br /&gt;
	classDebug:log(21, &amp;#039;entering CargoUtil.store&amp;#039;)&lt;br /&gt;
	classDebug:log(22, &amp;#039; with table &amp;quot;&amp;#039; .. dbtable .. &amp;#039;&amp;quot; and data &amp;#039; ..&lt;br /&gt;
		(type(data) == &amp;quot;string&amp;quot; and &amp;#039;&amp;quot;&amp;#039; .. data .. &amp;#039;&amp;quot;&amp;#039; or &amp;#039;of type &amp;#039; .. type(data)))&lt;br /&gt;
	if type(data) == &amp;quot;table&amp;quot; then&lt;br /&gt;
		classDebug:log(23, &amp;#039; datatable: &amp;#039; .. _TT.printTable(data))&lt;br /&gt;
	end&lt;br /&gt;
	local ret = false&lt;br /&gt;
	if type(data) == &amp;quot;table&amp;quot; then&lt;br /&gt;
		for k, v in pairs(data) do&lt;br /&gt;
			if type(v) == &amp;#039;boolean&amp;#039; then&lt;br /&gt;
				data[k] = v and 1 or 0&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		local frame = mw.getCurrentFrame()&lt;br /&gt;
		ret = frame:callParserFunction{name = &amp;#039;#cargo_store:_table=&amp;#039; .. dbtable, args = data}&lt;br /&gt;
	end&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Oetterer</name></author>
	</entry>
</feed>