package com.dbxml.db.common.xupdate;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: XUpdateQuery.java,v 1.5 2006/02/02 18:53:52 bradford Exp $
*/
import com.dbxml.db.common.adapters.DOMAdapter;
import com.dbxml.db.common.query.QueryBase;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.query.CompilationException;
import com.dbxml.db.core.query.ProcessingException;
import com.dbxml.db.core.query.QueryException;
import com.dbxml.db.core.query.ResultSet;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.xml.NamespaceMap;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* XUpdateQuery
*/
final class XUpdateQuery extends QueryBase {
private XUpdateImpl xu;
private DOMAdapter domAdapter;
public XUpdateQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
super(context, query, nsMap, keys);
domAdapter = new DOMAdapter(context);
}
public void compileQuery() throws CompilationException {
try {
xu = new XUpdateImpl();
xu.setQString(query);
}
catch ( Exception e ) {
throw new CompilationException("Error Compiling XUpdate Query", e);
}
}
public String getQueryStyle() {
return XUpdateQueryResolver.STYLE_XUPDATE;
}
public ResultSet execute(Transaction tx) throws QueryException {
try {
if ( keys != null ) {
try {
for ( int i = 0; i < keys.length; i++ ) {
Document doc = domAdapter.getDocument(tx, keys[i]);
if ( doc == null )
continue;
Node ctxNode = doc.getDocumentElement();
xu.execute(ctxNode);
// If there's still a Document Element, then update.
// Otherwise, XUpdate wanted to delete the Document
if ( doc.getDocumentElement() != null )
domAdapter.setDocument(tx, keys[i], doc);
else
domAdapter.remove(tx, keys[i]);
}
}
catch ( Exception e ) {
e.printStackTrace(System.err);
}
}
else
xu.execute(tx, domAdapter);
return new XUpdateResultSet(tx, context, this, xu.getModified());
}
catch ( Exception e ) {
throw new ProcessingException("Error executing XUpdate query", e);
}
}
}