This interface describes the execute immediate functionality for
expressions. This object can be created from the
XQConnection
and the execution can be done using the
executeQuery()
or
executeCommand()
method, passing in the XQuery expression.
All external variables defined in the prolog of the expression to be executed must
be set in the dynamic context of this expression using the bind methods.
Also, variables bound in this expression but not defined as external in
the prolog of the expression to be executed, are simply ignored.
For example, if variables $var1
and $var2
are bound,
but the query only defines $var1
as external, no error
will be reported for the binding of $var2
. It will simply
be ignored.
When the expression is executed using the executeQuery
method, if the execution is successful, then
an XQResultSequence
object is returned.
The XQResultSequence
object is tied to
the XQExpression
from which it was prepared and is
closed implicitly if that XQExpression
is either closed or re-executed.
The XQExpression
object is dependent on
the XQConnection
object from which it was created and is only
valid for the duration of that object.
Thus, if the XQConnection
object is closed then
this XQExpression
object will be implicitly closed
and it can no longer be used.
An XQJ driver is not required to provide finalizer methods for
the connection and other objects. Hence it is strongly recommended that
users call close method explicitly to free any resources. It is also
recommended that they do so under a final block to ensure that the object
is closed even when there are exceptions. Not closing this object implicitly
or explicitly might result in serious memory leaks.
When the XQExpression
is closed any XQResultSequence
object obtained from it is also implicitly closed.
Example -
XQConnection conn = XQDatasource.getConnection();
XQExpression expr = conn.createExpression();
expr.bindInt(new QName("x"), 21, null);
XQSequence result = expr.executeQuery(
"declare variable $x as xs:integer external;
for $i in $x return $i");
while (result.next())
{
// process results ...
}
// Execute some other expression on the same object
XQSequence result = expr.executeQuery("for $i in doc('foo.xml') return $i");
...
result.close(); // close the result
expr.close();
conn.close();