OpenWGA 7.6 - TMLScript reference
DatabaseMethod :
query(queryType, query, [attributes, [queryParams, [context]]])
query(query [,config])
On object | Database |
Usage | Performs a query on the database |
Description |
This method is the TMLScript pendant to WebTML tag <tml:query> and works quite equal. The mandatory parameters "queryType" and "query" are equal to attribute type and the contents of the query tag. The Lookup table argument "attributes" uses the names of other <tml:query>-Attributes as keys and interprets them the same way. So filling it with an entry of key "max" and a value 100 will have the same effect as specifying attribute max="100" on a query tag. Specifying or omitting an attribute here has the same effect as it would have on <tml:query>. The Lookup table argument "queryParams" takes query parameters that in WebTML you would be added to the query by using <tml:param>. Specify parameter names as keys and parameter values as values. The argument "context" determines the WebTML context for which the query runs, which is important for some query types like "lucene" or "hdbmodel:*". |
Parameters |
Variant 1: queryType (String): The type of query to execute. See Query languages reference for valid values query (String): The actual query to execute attributes (Lookup table, optional): Additional attributes for the query. Specify names of <tml:query> attributes and use the desired attribute values. Values might either be in string or in native type. So value of attribute "max" could be specified as string "100" or as native JavaScript number 100. queryParams (Lookup table, optional): Query parameters injected to the query. Specify parameter names as keys. Values may be any value that would be valid as a result of <tml:param>-attribute expression. context (TMLContext, optional): The context to execute the query for. Omit this to execute the query under the context of the script. Variant 2 (compact format): query (String): The actual query to execute config (java-Map or JavaScript Object - optional) A map containing query parameters to be set. Specify names of <tml:query> attributes and use the desired attribute values. Values might either be in string or in native type. So value of attribute "max" could be specified as string "100" or as native JavaScript number 100. Query-Parameter are specified using p_param syntax just like in <tml:query> like p_name="walter". In addition to the <tml:query> attributes a Context may be specified with key "context" in the config map. This can either be a TMLContext object or a context-expression as string. If no "type" parameter is contained in "config" the default type "native" is used (equals to "hql" für JDBC contentstores). |
Return value | A QueryResult object |
Allowed in script types |
|
Examples |
Executing a simple HQL query: WGA.app().query("content.author = 'ow'"); or WGA.app().query("hql", "content.author = 'ow'"); This returns the same results as: <tml:query type="hql">content.author = 'ow'</tml:query> Controlling query behaviour using some additional attributes. Note that we use the JSON object syntax on the "attributes" argument instead of a native Lookup table for simplicity (JavaScript objects can be used wherever Lookup Tables are expected). WGA.app().query( "hql", "content.author = 'ow'", {max: 100, cache:true} ); Extending this even further by injecting the author name to query as query parameter: WGA.app().query( "hql", "content.author = :author", {max: 100, cache:true}, {author: "ow"} ); or (same query in variant 2): WGA.app().query("content.author = :author", { max: 100, p_author: "ow" An example of traversing query results, which puts all resulting TMLContext-s in a list: var list = WGA.createList(); var docs = WGA.app().query("content.author = 'ow'"); for (let doc in Iterator(docs)) { list.add(doc); } |