Modul:CargoUtil/doc

ZIM HilfeWiki - das Wiki

This module provides serveral useful function that can be used in other module to store or retrive data operating a cargo store.

Usage[Quelltext bearbeiten]

local _CARGO = require('Module:CargoUtil')
if dataStore == 'cargo' and mw.title.getCurrentTitle().namespace == 10 then
	-- create table declaration, then call
	_CARGO.declare('tablename', declaration)
end
if stash then
	_CARGO.store('tablename', stash)
end
local query = {
	tables = {'table1'}
	fields = {'_pageName', '_ID', 'field1'}
	where = {'field1="hello world"', '_ID > 1'}
	order = {'_ID ASC', '_pageName DESC'}
}
local attributes = {
	limit = 1
}
local result = _CARGO.query(query, attributes)

Functions[Quelltext bearbeiten]

Provided functions are as follow:

declare(dbtable, declaration)[Quelltext bearbeiten]

Calls #cargo_declare with the provided parameter. Note that this parser function must be called (and therefore placed) on the template page, that later fills the table via store(dbtable, stash). See Usage-example above. Please refer to the Cargo documentation page fore more information about declaration options.

dbtable
string, mandatory
takes the name of the database table that should be declared
declaration
table or string, mandatory
holds the declaration. if provided a table, it must be formatted {fieldName : 'declaration' }
return
string, return values of the declaration call

query(query, attributes, retainBlanks)[Quelltext bearbeiten]

Runs a query on the cargo store according to the parameters provided and returns the result as a lua-table. The first parameter (query) has the following mandatory fields:

tables
table or string, mandatory
the name(s) of the table(s) to query data from
fields
table or string, mandatory
the fields, you want to get data from. format is fieldname or fieldname=headline. In case of more than one table, please use tablename.fieldname or tablealias.fieldname
joins
table or string, mandatory if you have more than one table. you need one join statement for each table after the first
the list of join statements

Also, there are several optional fields:

where
table or string, optional
you can filter the results with where clauses
group
table or string, optional
the group statements
having
table or string, optional
filter your query, similar to WHERE, but applies to values computed for "groups"
order
table or string, optional
the fields, you wish to sort the result for. please use tablename.fieldname or tablealias.fieldname

Please keep in mind:

  • attributes may contain any attribute you find in the documentation, but intro and outro will be NILed, default and more results text will be set to the empty string, and format will be overwritten
  • per default, does only return fields that contain a value. that can result in an "unbalanced" result, where one row contains more elements than others. if you want to get all fields, set retainBlanks to true
query
table, mandatory
the arguments to construct the query. see above for details
attributes
table, optional
more attributes, you want to pass on to the query (like limit, for instance)
retainBlanks
boolean, optional
if set to true, result can contain entries with empty values. per default, these entries will be deleted
return
table, see the following printout as example

The result has the following format: <-- cut 1: 1 = 'Extension:Cargo' 2 = '7' 3 = '901' 4 = 'Cargo provides a lightweight way to store and query the data contained within the calls to templates, such as infoboxes in mysql tables.' 2: 1 = 'Extension:SyntaxHighlight GeSHi' 2 = '6' 3 = '911' 4 = 'The SyntaxHighlight GeSHi extension provides rich formatting of source code using the "syntaxhighlight"-tag. ...' -->

{
	1 = {
		id = '7'
		pageid = '901'
		description = 'Cargo provides a lightweight way to store and query the data contained within the calls to templates, such as infoboxes in mysql tables.'
		page = 'Extension:Cargo'
	}
	2 = {
		id = '6'
		pageid = '911'
		description = 'The SyntaxHighlight GeSHi extension provides rich formatting of source code using the "syntaxhighlight"-tag. ...'
		page = 'Extension:SyntaxHighlight GeSHi'
	}
}

As you can see, the data fields can be indexed either by number or by the provided headline in table fields. (The headline is either the fieldname or a special string you provided via fieldname=headline.

rawquery(query, attributes)[Quelltext bearbeiten]

Calls the parser function #cargo_query according to the provided parameters and passes the result on directly. The format of the output is depending on attributes.format. The first parameter (query) has the following mandatory fields:

tables
table or string, mandatory
the name(s) of the table(s) to query data from
fields
table or string, mandatory
the fields, you want to get data from. format is fieldname or fieldname=headline. In case of more than one table, please use tablename.fieldname or tablealias.fieldname
joins
table or string, mandatory if you have more than one table. you need one join statement for each table after the first
the list of join statements

Also, there are several optional fields:

where
table or string, optional
you can filter the results with where clauses
group
table or string, optional
the group statements
having
table or string, optional
filter your query, similar to WHERE, but applies to values computed for "groups"
order
table or string, optional
the fields, you wish to sort the result for. please use tablename.fieldname or tablealias.fieldname

Please keep in mind:

  • attributes may contain any attribute you find in the documentation, but intro and outro will be NILed, default and more results text will be set to the empty string, and format will be overwritten
  • per default, does only return fields that contain a value. that can result in an "unbalanced" result, where one row contains more elements than others. if you want to get all fields, set retainBlanks to true
query
table, mandatory
the arguments to construct the query. see above for details
attributes
table, optional
more attributes, you want to pass on to the query (like limit, for instance)
return
string, the renderd output

store(dbtable, stash)[Quelltext bearbeiten]

Calls #cargo_store with the provided parameters. Note that the table must be declared and created before you can actually store anything.

dbtable
string, mandatory
takes the name of the database table to be filled
stash
table (!), mandatory
holds the data to store. it must be formatted {fieldName : data of type string or integer }
return
string, return values of the store call
Cookies helfen uns bei der Bereitstellung des ZIM HilfeWikis. Bei der Nutzung vom ZIM HilfeWiki werden die in der Datenschutzerklärung beschriebenen Cookies gespeichert.