How-to guide·QueryJavaScript · Java

Use query parameters

Write $name placeholders in a query and bind their values at call time, instead of building query strings from user input.

For an application whose queries depend on user input: a type picked from a list, a threshold from a slider, a selection from the viewer. A parameter is $ followed by a name, written where a literal would be; the values travel separately as a parameters object and are bound by the server. No quoting, no escaping, and the same query text every time, which also keeps the server's query cache useful.

Before you start#

1. Write the query with placeholders#

A parameter may replace a literal in a comparison (= $x, > $min, BETWEEN $from AND $to, CONTAINS $text), a list in IN (type IN $types, qid IN $selection) and the argument of CLASHES_WITH. Names are identifiers; a query may use one parameter several times.

BQL#
GET qid, attribute.Name, calculated.quantity.volume UNIT m3
WHERE type IN $types AND calculated.quantity.volume > $minVolume

2. Bind the values when you call#

Over HTTP, Query takes the values as parameters, an object of name to value:

POST/openapi/ifc/QueryQuery reference
Authorization
Bearer <auth token>
Content-Type
application/json
Body
1 2 3 4 5
Authorization: Bearer <auth token>
Content-Type: application/json
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/ifc/Query" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
  "query": "GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type IN $types AND calculated.quantity.volume > $minVolume",
  "parameters": {"types": ["IFCWALL", "IFCSLAB"], "minVolume": 2}
}'
Send to see the response

Values are strings, numbers, booleans or arrays of those. parameters works with every format of the operation except ifc and ifcxml, so "format": "count" counts with them (Count results).

In JavaScript the parameters object is the fifth argument of query and the fourth of queryCount (Bim), and QueryHelper.toParameterValues converts a QidSet from the viewer into a value for qid IN $selection:

JavaScript#
const query = "GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type IN $types AND calculated.quantity.volume > $minVolume";
const objects = await client.bim.query(["<version uuid of the first demo model>"], query, false, false, {
	types: ["IFCWALL", "IFCSLAB"],
	minVolume: 2
});

The Java SDK's BimApi.query, the .NET SDK and the PHP client have no parameters argument in this release: call the HTTP operation from those languages.

3. Read the errors#

A placeholder without a value fails with MISSING_PARAMETER, and a value of the wrong kind (a string for a number, a scalar for IN) with PARAMETER_TYPE_MISMATCH. The parameters field must be an object of name to value; anything else is a type mismatch too.

Note

USING is not a parameter mechanism. It is the clause that sets the units a query converts to (USING LENGTH_UNIT = mm VOLUME_UNIT = m3), see USING and parameters; the UNIT keyword on a single column does the same for that column.

Check#

Bind minVolume: 2, then minVolume: 2000: the second call returns fewer (or no) objects with the same query text. Leave types out: the call fails with MISSING_PARAMETER.