16 package org.zorbaxquery.api.xqj;
18 import java.io.InputStream;
19 import java.io.Reader;
20 import java.io.StringWriter;
21 import java.io.Writer;
22 import java.nio.CharBuffer;
24 import javax.xml.namespace.QName;
25 import javax.xml.stream.XMLStreamReader;
26 import javax.xml.transform.Source;
27 import javax.xml.xquery.*;
28 import org.w3c.dom.Node;
29 import org.zorbaxquery.api.DynamicContext;
30 import org.zorbaxquery.api.Item;
31 import org.zorbaxquery.api.XQuery;
72 private XQConnection connection;
73 private boolean closed =
false;
74 private boolean cancel =
false;
75 private Collection<XQResultSequence> resultSequences =
new ArrayList<XQResultSequence>();
76 private XQStaticContext staticContext;
77 private Map<String, Item> itemsToBind =
new HashMap<String, Item>();
78 private TimeZone implicitTimeZone;
81 if (conn.isClosed()) {
82 throw new XQException (
"Connection is closed");
88 if (conn.isClosed()) {
89 throw new XQException (
"Connection is closed");
102 public void cancel() throws XQException {
103 isClosedXQException();
124 public void close() throws XQException {
126 for (XQResultSequence rs: resultSequences) {
140 throw new UnsupportedOperationException(
"Not supported yet.");
152 throw new UnsupportedOperationException(
"Not supported yet.");
164 public XQResultSequence
executeQuery(String value)
throws XQException {
165 isClosedXQException();
166 isNullXQException(value);
168 XQuery query = lConnection.getZorbaInstance().createQuery();
169 XQResultSequence result = null;
171 int scrollable = XQConstants.SCROLLTYPE_FORWARD_ONLY;
172 if (staticContext!=null) {
174 scrollable = staticContext.getScrollability();
176 query.compile(value);
178 DynamicContext dynamicContext = query.getDynamicContext();
179 if (implicitTimeZone!=null) {
180 dynamicContext.setImplicitTimezone((implicitTimeZone.getRawOffset()/60000));
181 implicitTimeZone=null;
183 for (String key: itemsToBind.keySet()){
184 dynamicContext.setVariable(key, itemsToBind.get(key) );
187 if (scrollable == XQConstants.SCROLLTYPE_FORWARD_ONLY) {
192 resultSequences.add(result);
193 }
catch (Exception e) {
194 throw new XQException (
"Error executing query: " + e.getLocalizedMessage());
208 public XQResultSequence
executeQuery(Reader value)
throws XQException {
209 isClosedXQException();
210 isNullXQException(value);
212 StringBuffer
string =
new StringBuffer();
213 CharBuffer buffer = CharBuffer.allocate(1024);
214 Writer writer =
new StringWriter();
217 while( value.read(buffer) >= 0 ) {
219 writer.append(buffer);
223 }
catch (Exception ex) {
224 throw new XQException(
"Error preparing expression" + ex.getLocalizedMessage());
239 public XQResultSequence
executeQuery(InputStream value)
throws XQException {
240 isClosedXQException();
241 isNullXQException(value);
242 StringBuffer out =
new StringBuffer ();
244 byte[] b =
new byte[4096];
245 for (
int n; (n = value.read(b)) != -1;) {
246 out.append(
new String(b, 0, n));
248 }
catch (Exception ex) {
249 throw new XQException(
"Error preparing expression" + ex.getLocalizedMessage());
263 isClosedXQException();
264 if (staticContext==null) {
265 return connection.getStaticContext();
267 return staticContext;
278 isClosedXQException();
279 if (implicitTimeZone!=null) {
280 return implicitTimeZone;
283 DynamicContext dc = query.getDynamicContext();
284 Integer timeZone = (dc.getImplicitTimezone()/60);
285 TimeZone result = TimeZone.getTimeZone(
"GMT"+timeZone.toString());
299 public void bindAtomicValue(QName varName, String value, XQItemType type)
throws XQException {
300 isClosedXQException();
301 isNullXQException(varName);
302 isNullXQException(value);
304 XQItem item = connection.createItemFromAtomicValue(value, type);
305 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
306 }
catch (Exception e) {
307 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
322 public void bindString(QName varName, String value, XQItemType type)
throws XQException {
323 isClosedXQException();
324 isNullXQException(varName);
325 isNullXQException(value);
327 XQItem item = connection.createItemFromString(value, type);
328 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
329 }
catch (Exception e) {
330 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
349 public void bindDocument(QName varName, String value, String baseURI, XQItemType type)
throws XQException {
350 isClosedXQException();
351 isNullXQException(varName);
352 isNullXQException(value);
354 XQItem item = connection.createItemFromDocument(value, baseURI, type);
355 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
356 }
catch (Exception e) {
357 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
376 public void bindDocument(QName varName, Reader value, String baseURI, XQItemType type)
throws XQException {
377 isClosedXQException();
378 isNullXQException(varName);
379 isNullXQException(value);
381 XQItem item = connection.createItemFromDocument(value, baseURI, type);
382 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
383 }
catch (Exception e) {
384 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
403 public void bindDocument(QName varName, InputStream value, String baseURI, XQItemType type)
throws XQException {
404 isClosedXQException();
405 isNullXQException(varName);
406 isNullXQException(value);
408 XQItem item = connection.createItemFromDocument(value, baseURI, type);
409 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
410 }
catch (Exception e) {
411 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
429 public void bindDocument(QName varName, XMLStreamReader value, XQItemType type)
throws XQException {
430 isClosedXQException();
431 isNullXQException(varName);
432 isNullXQException(value);
434 XQItem item = connection.createItemFromDocument(value, type);
435 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
436 }
catch (Exception e) {
437 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
460 public void bindDocument(QName varName, Source value, XQItemType type)
throws XQException {
461 isClosedXQException();
462 isNullXQException(varName);
463 isNullXQException(value);
465 XQItem item = connection.createItemFromDocument(value, type);
466 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
467 }
catch (Exception e) {
468 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
479 isClosedXQException();
480 isNullXQException(value);
482 implicitTimeZone = value;
483 }
catch (Exception e) {
484 throw new XQException(
"Error setting implicit TimeZone: " + e.getLocalizedMessage());
497 public void bindItem(QName varName, XQItem value)
throws XQException {
498 isClosedXQException();
499 isNullXQException(varName);
500 isNullXQException(value);
502 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)value).getZorbaItem());
503 }
catch (Exception e) {
504 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
517 public void bindSequence(QName varName, XQSequence value)
throws XQException {
518 isClosedXQException();
519 isNullXQException(varName);
520 isNullXQException(value);
522 Item item =
new Item(((org.zorbaxquery.api.xqj.ZorbaXQItem)value.getItem()).getZorbaItem());
523 itemsToBind.put(varName.getLocalPart(), item);
524 }
catch (Exception e) {
525 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
539 public void bindObject(QName varName, Object value, XQItemType type)
throws XQException {
540 isClosedXQException();
541 isNullXQException(varName);
542 isNullXQException(value);
545 if (value instanceof XQItem) {
546 item = (XQItem)value;
548 item = connection.createItemFromObject(value, type);
550 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
551 }
catch (Exception e) {
552 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
566 public void bindBoolean(QName varName,
boolean value, XQItemType type)
throws XQException {
567 isClosedXQException();
568 isNullXQException(varName);
570 XQItem item = connection.createItemFromBoolean(value, type);
571 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
572 }
catch (Exception e) {
573 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
587 public void bindByte(QName varName, byte value, XQItemType type)
throws XQException {
588 isClosedXQException();
589 isNullXQException(varName);
591 XQItem item = connection.createItemFromByte(value, type);
592 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
593 }
catch (Exception e) {
594 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
608 public void bindDouble(QName varName,
double value, XQItemType type)
throws XQException {
609 isClosedXQException();
610 isNullXQException(varName);
612 XQItem item = connection.createItemFromDouble(value, type);
613 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
614 }
catch (Exception e) {
615 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
629 public void bindFloat(QName varName,
float value, XQItemType type)
throws XQException {
630 isClosedXQException();
631 isNullXQException(varName);
633 XQItem item = connection.createItemFromFloat(value, type);
634 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
635 }
catch (Exception e) {
636 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
650 public void bindInt(QName varName,
int value, XQItemType type)
throws XQException {
651 isClosedXQException();
652 isNullXQException(varName);
654 XQItem item = connection.createItemFromInt(value, type);
655 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
656 }
catch (Exception e) {
657 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
671 public void bindLong(QName varName,
long value, XQItemType type)
throws XQException {
672 isClosedXQException();
673 isNullXQException(varName);
675 XQItem item = connection.createItemFromLong(value, type);
676 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
677 }
catch (Exception e) {
678 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
692 public void bindNode(QName varName, Node value, XQItemType type)
throws XQException {
693 isClosedXQException();
694 isNullXQException(varName);
695 isNullXQException(value);
697 XQItem item = connection.createItemFromNode(value, type);
698 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
699 }
catch (Exception e) {
700 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
714 public void bindShort(QName varName,
short value, XQItemType type)
throws XQException {
715 isClosedXQException();
716 isNullXQException(varName);
718 XQItem item = connection.createItemFromShort(value, type);
719 itemsToBind.put(varName.getLocalPart(),((org.zorbaxquery.api.xqj.ZorbaXQItem)item).getZorbaItem());
720 }
catch (Exception e) {
721 throw new XQException (
"Error binding object: " + e.getLocalizedMessage());
725 private void isClosedXQException() throws XQException {
727 throw new XQException(
"This expression is closed");
730 private void isNullXQException(Object value)
throws XQException {
732 throw new XQException(
"Parameter shouldn't be null");
void bindLong(QName varName, long value, XQItemType type)
Binds a value to the given external variable or the context item.
An ZorbaXQStaticContext represents default values for various XQuery Static Context Components...
void bindFloat(QName varName, float value, XQItemType type)
Binds a value to the given external variable or the context item.
XQResultSequence executeQuery(InputStream value)
Executes a query expression.
ZorbaXQExpression(XQConnection conn)
A connection (session) with a specific XQuery engine.
void bindString(QName varName, String value, XQItemType type)
Binds a value to the given external variable or the context item.
XQResultSequence executeQuery(String value)
Executes a query expression.
void bindByte(QName varName, byte value, XQItemType type)
Binds a value to the given external variable or the context item.
void bindNode(QName varName, Node value, XQItemType type)
Binds a value to the given external variable or the context item.
void bindDocument(QName varName, String value, String baseURI, XQItemType type)
Binds a value to the given external variable or the context item.
void bindInt(QName varName, int value, XQItemType type)
Binds a value to the given external variable or the context item.
This interface describes the execute immediate functionality for expressions.
void bindBoolean(QName varName, boolean value, XQItemType type)
Binds a value to the given external variable or the context item.
void bindShort(QName varName, short value, XQItemType type)
Binds a value to the given external variable or the context item.
TimeZone getImplicitTimeZone()
Gets the implicit timezone.
XQStaticContext getStaticContext()
Gets an ZorbaXQStaticContext representing the values for all expression properties.
XQResultSequence executeQuery(Reader value)
Executes a query expression.
void bindSequence(QName varName, XQSequence value)
Binds a value to the given external variable.
void bindDocument(QName varName, XMLStreamReader value, XQItemType type)
Binds a value to the given external variable or the context item.
boolean isClosed()
Checks if the expression is in a closed state.
void cancel()
Attempts to cancel the execution if both the XQuery engine and XQJ driver support aborting the execut...
void executeCommand(Reader reader)
Executes an implementation-defined command.
void bindObject(QName varName, Object value, XQItemType type)
Binds a value to the given external variable or the context item.
void bindItem(QName varName, XQItem value)
Binds a value to the given external variable.
void bindDocument(QName varName, Reader value, String baseURI, XQItemType type)
Binds a value to the given external variable or the context item.
This class represents a sequence of items obtained as a result of evaluation XQuery expressions...
void bindDocument(QName varName, InputStream value, String baseURI, XQItemType type)
Binds a value to the given external variable or the context item.
void setImplicitTimeZone(TimeZone value)
Sets the implicit timezone.
void close()
Closes the expression object and release associated resources.
void bindDouble(QName varName, double value, XQItemType type)
Binds a value to the given external variable or the context item.
ZorbaXQExpression(XQConnection conn, XQStaticContext sc)
void bindAtomicValue(QName varName, String value, XQItemType type)
Binds a value to the given external variable or the context item.
void bindDocument(QName varName, Source value, XQItemType type)
Binds a value to the given external variable or the context item.
void executeCommand(String string)
Executes an implementation-defined command.