KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > ExecutionPlan


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.plan;
24
25 import java.util.*;
26
27 import org.xquark.mediator.DOMUtils.BufferTuple;
28 import org.xquark.mediator.DOMUtils.Tuple;
29 import org.xquark.mediator.runtime.MediatorException;
30 import org.xquark.mediator.runtime.MediatorResultSet;
31 import org.xquark.mediator.runtime.MediatorStatement;
32 import org.xquark.schema.*;
33 import org.xquark.util.NamespaceContextStack;
34 import org.xquark.xml.xdbc.*;
35 import org.xquark.xpath.datamodel.TypedNode;
36 import org.xquark.xpath.datamodel.TypedValue;
37 import org.xquark.xquery.metadata.MetaDataImpl;
38 import org.xquark.xquery.parser.*;
39 import org.xquark.xquery.parser.hinttree.HintTree;
40 import org.xquark.xquery.typing.*;
41 import org.xquark.xquery.xdbc.XDBCResultSetInterface;
42
43 public class ExecutionPlan {
44     // **********************************************************************
45
// * VERSIONING
46
// **********************************************************************
47
private static final String JavaDoc RCSRevision = "$Revision: 1.20 $";
48     private static final String JavaDoc RCSName = "$Name: $";
49
50     // **********************************************************************
51
// * CLASS VARIABLES
52
// **********************************************************************
53

54     private Operator root = null;
55     private ResultSet resultset = null;
56     private TypeVisitor typevisitor;
57     private String JavaDoc declarations = null;
58     private ArrayList idvars = null;
59     private HashMap aliases = null;
60     private String JavaDoc baseURI = null;
61     private boolean qaOnly = false;
62     private XQueryExpression qmem;
63     //private XQueryExpression origExpr = null;
64
private String JavaDoc queryStringImports = null;
65     private String JavaDoc queryString = null;
66     private XQueryModule module = null;
67     private MetaDataImpl metadata = null;
68
69     private final int defaultsubqueryhandling = HintTree.MERGE_TYPE;
70     //private final int defaultsubqueryhandling = HintTree.NESTED_TYPE;
71

72     // to handle types generated by depnodesource
73
SchemaManager schemamanager = null;
74     Schema schema = null;
75     // ***********************************************************************
76
// * INITIALIZATION
77
// ***********************************************************************
78
public ExecutionPlan(MetaDataImpl metadata, TypeVisitor typevisitor, ArrayList idvars, String JavaDoc queryStringImports, String JavaDoc queryString, XQueryModule module, String JavaDoc baseURI, boolean qaOnly) throws MediatorException {
79         if (typevisitor == null)
80             throw new MediatorException("TypeVisitor in plan constructor cannot be null");
81         this.metadata = metadata;
82         this.typevisitor = typevisitor;
83         this.idvars = idvars;
84         //this.origExpr = origExpr;
85
this.queryString = queryString;
86         this.queryStringImports = queryStringImports;
87         this.module = module;
88         this.baseURI = baseURI;
89         this.qaOnly = qaOnly;
90         // compute declaration string
91
NamespaceContextStack declarations = module.getDeclarations();
92         if (declarations == null)
93             this.declarations = null;
94         else {
95             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
96             Collection declPrefixes = declarations.getPrefixes();
97             // Iterator itValues = declPrefixes.iterator();
98
// while (itValues.hasNext()) { buf.append(" "); buf.append((()itValues.next()).pprint()); }
99
// this.declarations = buf.toString();
100
Iterator itValues = declPrefixes.iterator();
101             String JavaDoc prefix = null;
102             while (itValues.hasNext()) {
103                 prefix = (String JavaDoc) itValues.next();
104                 buf.append("declare ");
105                 if (prefix != null) {
106                     buf.append("namespace ");
107                     buf.append(prefix);
108                 } else
109                     buf.append("default element namespace");
110                 buf.append(" = \"");
111                 buf.append(declarations.getNamespaceURI(prefix));
112                 buf.append("\";\n");
113             }
114             this.declarations = buf.toString();
115         }
116         // create schemamanager and schema
117
schemamanager = typevisitor.getTypingSchemaManager();
118         schema = schemamanager.getSchema("http://www.xquark.org/EXECUTIONPLAN");
119     }
120
121     // ***********************************************************************
122
// * GET/SET METHODS
123
// ***********************************************************************
124
public XDBCResultSetInterface getResultSet() throws MediatorException {
125         try {
126             PlanResultSet presultset = new PlanResultSet(resultset);
127             return presultset;
128         } catch (XMLDBCException xmldbce) {
129             throw new MediatorException("ExecutionPlan.getResultSet: " + xmldbce.getMessage(), xmldbce);
130         }
131     }
132
133     public SchemaManager getSchemaManager() {
134         return schemamanager;
135     }
136
137     public String JavaDoc getDeclarations() {
138         return declarations;
139     }
140     public TypeVisitor getTypeVisitor() {
141         return typevisitor;
142     }
143     public Operator getRoot() {
144         return root;
145     }
146     public void setRoot(Operator algebra) throws MediatorException {
147         root = algebra;
148     }
149     public ArrayList getIdVars() {
150         return idvars;
151     }
152     public void setIdVars(ArrayList idvars) {
153         this.idvars = idvars;
154     }
155
156     public String JavaDoc getAlias(String JavaDoc varstr) {
157         if (aliases == null)
158             return null;
159         for (Iterator it = aliases.keySet().iterator(); it.hasNext();) {
160             String JavaDoc stri = (String JavaDoc) it.next();
161             if (varstr.startsWith(stri))
162                 return (String JavaDoc) aliases.get(stri) + varstr.substring(stri.length());
163         }
164         return null;
165     }
166
167     public byte getDefaultSubHandling() {
168         return defaultsubqueryhandling;
169     }
170     // ***********************************************************************
171
// * METHODS
172
// ***********************************************************************
173
public void makeAlias(Variable var, XQueryExpression path) {
174         if (aliases == null)
175             aliases = new HashMap();
176         aliases.put(var.getStringValue(), path.getStringValue());
177     }
178
179     /*
180     public void addReplaceValues(Tuple tuple) {
181         BufferTuple bt = tuple.getParent();
182         for (int i=0;i<tuple.size();i++) {
183             ArrayList tmplist = tuple.getNodesAtIndex(i);
184             if (tmplist.size() != 1) continue;
185             this.replaceValues.put(bt.getPathList().get(i),tmplist.get(0));
186         }
187     }
188     
189     public void deleteReplaceValues(Tuple tuple) {
190         BufferTuple bt = tuple.getParent();
191         for (int i=0;i<tuple.size();i++) {
192             this.replaceValues.remove(bt.getPathList().get(i));
193         }
194     }
195     
196     public HashMap getCurrentValues() {
197         return this.replaceValues;
198     }
199     */

200
201     public String JavaDoc getBaseURI() {
202         return baseURI;
203     }
204
205     public boolean getQAOnly() {
206         return qaOnly;
207     }
208
209     public XQueryModule getModule() {
210         return module;
211     }
212
213     // ***********************************************************************
214
// * EXECUTE QUERY
215
// ***********************************************************************
216
/**
217      *
218      */

219     public XMLResultSet executeQuery(MediatorStatement statement) throws XQueryException, XMLDBCException {
220         ResultSet resultset = null;
221         // test source number
222
DynamicContext context = new DynamicContext(this, statement);
223
224         if (queryStringImports != null) {
225             // create String to add for external variables
226
PreparedXMLStatement preparedStatement = null;
227             Iterator it = module.getSourceNames().iterator();
228             String JavaDoc sourceName = (String JavaDoc) it.next();
229             try {
230                 preparedStatement = context.getPreparedXMLStatement(null, sourceName, queryStringImports);
231             } catch (XMLDBCException xe) {
232                 throw new MediatorException(xe.getMessage(), xe);
233             }
234             if (preparedStatement != null) {
235                 preparedStatement.setBaseURI(baseURI);
236                 HashMap varMap = statement.getVariables();
237                 if (varMap != null) {
238                     for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) {
239                         String JavaDoc nsStr = (String JavaDoc) itns.next();
240                         HashMap nsMap = (HashMap) varMap.get(nsStr);
241                         for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) {
242                             String JavaDoc vnStr = (String JavaDoc) itvn.next();
243                             TypedValue typedValue = (TypedValue) nsMap.get(vnStr);
244                             Object JavaDoc value = typedValue.getTypedValue();
245                             preparedStatement.setObject(nsStr, vnStr, value);
246                         }
247                     }
248                 }
249                 return preparedStatement.executeQuery();
250             } else {
251                 XMLConnection connection = null;
252                 connection = context.getConnection().getSubConnectionByName(sourceName);
253                 if (connection == null)
254                     throw new MediatorException("AlgSource: couldn't established connection to " + sourceName);
255                 XMLStatement stat = connection.createStatement();
256                 stat.setBaseURI(baseURI);
257                 // add to quryString all external variables defined in statment
258
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
259                 HashMap varMap = statement.getVariables();
260                 if (varMap != null) {
261                     for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) {
262                         String JavaDoc nsStr = (String JavaDoc) itns.next();
263                         HashMap nsMap = (HashMap) varMap.get(nsStr);
264                         HashMap moduleNsMap = (HashMap) module.getVariables().get(nsStr);
265                         for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) {
266                             String JavaDoc vnStr = (String JavaDoc) itvn.next();
267                             TypedValue typedValue = (TypedValue) nsMap.get(vnStr);
268                             Object JavaDoc value = typedValue.getTypedValue();
269                             Variable var = (Variable) moduleNsMap.get(vnStr);
270                             if (value != null) {
271                                 sb.append("declare variable $");
272                                 if (nsStr != null) {
273                                     sb.append(" {");
274                                     sb.append(nsStr);
275                                     sb.append("}");
276                                 }
277                                 sb.append(vnStr);
278                                 if (var != null && var.getTypeDeclaration() != null) {
279                                     sb.append(" as ");
280                                     sb.append(var.getTypeDeclaration());
281                                     sb.append("");
282                                 }
283                                 sb.append(" {");
284                                 if (value instanceof String JavaDoc) {
285                                     sb.append("\"");
286                                     sb.append(value.toString());
287                                     sb.append("\"");
288                                 } else
289                                     sb.append(value.toString());
290                                 sb.append("};\n");
291                             }
292                         }
293                     }
294                 }
295                 sb.append(queryString);
296                 return stat.executeQuery(sb.toString());
297             }
298         } else {
299             // create tuple in context
300
HashMap varMap = statement.getVariables();
301             if (varMap != null) {
302                 ArrayList paths = new ArrayList();
303                 ArrayList nodes = new ArrayList();
304                 for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) {
305                     String JavaDoc nsStr = (String JavaDoc) itns.next();
306                     HashMap nsMap = (HashMap) varMap.get(nsStr);
307                     HashMap moduleNsMap = (HashMap) module.getVariables().get(nsStr);
308                     for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) {
309                         String JavaDoc vnStr = (String JavaDoc) itvn.next();
310                         TypedNode typedNode = (TypedNode) nsMap.get(vnStr);
311                         Variable var = (Variable) moduleNsMap.get(vnStr);
312                         if (var != null) {
313                             paths.add(var);
314                             nodes.add(typedNode);
315                         }
316                     }
317                 }
318                 BufferTuple buf = new BufferTuple(paths, 0);
319                 Tuple tuple = buf.newTuple();
320                 for (int i = 0; i < nodes.size(); i++)
321                     tuple.addNodeAtIndex(i, (TypedNode) nodes.get(i));
322                 buf.add(tuple);
323                 context.addCurrentTuple(tuple);
324             }
325             if (root != null) {
326                 resultset = root.executeQuery(context);
327             } else
328                 resultset = new FakeResultSet(null, context);
329             return new MediatorResultSet(new PlanResultSet(resultset), getQMEM(), getIdVars(), statement);
330         }
331     }
332     // **********************************************************************
333
// * OPTIMIZE
334
// **********************************************************************
335
/**
336      *
337      */

338     public void optimize() throws MediatorException {
339         //root = root.optimize() ;
340
}
341
342     // type name handling
343
private final String JavaDoc TYPE_NAME = "PlanType";
344     private int typeIndex = 0;
345     public void setTypeName(Type type) throws SchemaException {
346         type.setName(TYPE_NAME + (++typeIndex));
347         schema.register(type);
348         this.schema.initialize();
349     }
350     // **********************************************************************
351
// * DEBUG
352
// **********************************************************************
353
/**
354      *
355      */

356     public String JavaDoc toString() {
357         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
358         buf.append("<Plan>\n");
359         if (root != null)
360             buf.append(root.toCompleteString(1));
361         buf.append("</Plan>\n");
362         return buf.toString();
363     }
364
365     public void setQMEM(XQueryExpression expression) {
366         qmem = expression;
367     }
368     public XQueryExpression getQMEM() {
369         return qmem;
370     }
371
372     public void appendExternalVariables(StringBuffer JavaDoc sbvars, ArrayList externalVariables) {
373         int length = sbvars.length();
374         for (int i = 0; i < externalVariables.size(); i++) {
375             Variable vari = (Variable) externalVariables.get(i);
376             sbvars.append("declare variable ");
377             sbvars.append(vari.getStringValue(module.getDeclarations()));
378             Type typ = vari.getQType().getType();
379             if (typ == null) {
380                 typ = schemamanager.getAnySimpleType();
381             }
382             if (typ.getValueType() == null) {
383                 sbvars.setLength(length);
384                 return;
385             } else {
386                 sbvars.append(" as xs:");
387                 sbvars.append((typ.getValueType()).getPrimitive().getName());
388             }
389             sbvars.append(" external;\n");
390         }
391     }
392
393     /**
394      * @return
395      */

396     public MetaDataImpl getMetadata() {
397         return metadata;
398     }
399
400 }
401
Popular Tags