Parameters

Build 1501 on 14/Nov/2017  This topic last edited on: 5/Aug/2014, at 16:53

It is possible to pass parameters to queries – i.e. values identified by a name that can be used in the query expressions. For example the query

gn4:story[gn4:title=$title]

returns all stories with a title equal to the parameter ‘title’.  Parameters are specified in srv4 export using the ‘-pars’ option, e.g.:

srv4 export -conditions "gn4:story[gn4:title=$title]" -pars title:Test -out c:\temp\s.xml

exports all stories with title ‘Test’. All the parameters used in the query expression must be specified – missing parameters cause an error – e.g.:

srv4 export -conditions "gn4:story[gn4:title=$title]" -out c:\temp\s.xml

causes:

  Error in <string>: Ln: 1 Col: 27

  gn4:story[gn4:title=$title]

                            ^

  Unknown variable 'title'

   (ERR1332)

Parameters can be either type or un-typed. Un-typed parameters are simply strings that GNQuery will try to convert to the appropriate type depending on where they are used in the query expression. Parameters passed via command line or URL are always un-typed.

In the example above ‘$title’ was compared to a string, so it remained a string – in this case though:

gn4:story[@nWords>=$minWords]

‘$minWords’ is compared to a numeric value, so the un-typed string will be converted by GNQuery to a number, and there will be an error if such string is not a valid number.

Single parameters can specify a list of values when they are passed to functions expecting multiple values of the same type. For example in

gn4:story[fn:in(@id,$ids)]

the un-typed parameter ‘$ids’ is expected to be a list of comma-separated strings because ‘fn:in’ accepts multiple (unlimited) parameters  of the same type, so

srv4 export -conditions "gn4:story[fn:in(@id,$ids)]" -pars ids:obj312,obj313 –out . . .

exports the stories with database id 312 and 313. To specify the database ids as number use the ‘fn:objectIdFromString()’ function – e.g.:

srv4 export -conditions "gn4:story[fn:in(fn:objectIdFromString(@id),$ids)]" 

    -pars ids:312,313 –out c:\temp\s.xml

has the same result of the previous example.

Typed parameters can be used only internally from code; they are values of different types (integers, strings, date-time, array of integers etc.) that are passed to GNQuery and that are used as-is, without conversions. As such they must match the expected type within the query expression – so in

gn4:story[@nWords>=$minWords]

the typed parameter ‘$minWords’ must be an integer, and in

gn4:story[fn:in(fn:objectIdFromString(@id),$ids)]

the typed parameter ‘$ids’ must be an array of integers.