KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > connection > xquery > XQConnection


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Sep 15, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.data.connection.xquery;
18
19 import java.io.FileNotFoundException JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22 import net.sf.saxon.Configuration;
23 import net.sf.saxon.query.DynamicQueryContext;
24 import net.sf.saxon.query.StaticQueryContext;
25 import net.sf.saxon.query.XQueryExpression;
26 import net.sf.saxon.trans.XPathException;
27
28 import org.pentaho.core.connection.IPentahoConnection;
29 import org.pentaho.core.connection.IPentahoResultSet;
30 import org.pentaho.messages.Messages;
31 import org.pentaho.util.logging.ILogger;
32
33 /**
34  * @author wseyler
35  *
36  * TODO To change the template for this generated type comment go to Window -
37  * Preferences - Java - Code Style - Code Templates
38  */

39 public class XQConnection implements IPentahoConnection {
40     protected Configuration config = null;
41
42     protected StaticQueryContext sqc = null;
43
44     protected String JavaDoc lastQuery = null;
45
46     protected ILogger logger = null;
47
48     IPentahoResultSet resultSet = null;
49
50     public XQConnection(Properties JavaDoc props, ILogger logger) {
51         this(logger);
52
53         connect(props);
54     }
55
56     public XQConnection(ILogger logger) {
57         super();
58
59         this.logger = logger;
60         config = new Configuration();
61         sqc = new StaticQueryContext(config);
62     }
63
64     public boolean initialized() {
65         // TODO create a good test
66
return true;
67     }
68
69     public IPentahoResultSet prepareAndExecuteQuery(String JavaDoc query, List JavaDoc parameters) throws Exception JavaDoc {
70       throw new UnsupportedOperationException JavaDoc();
71     }
72     
73     public boolean preparedQueriesSupported() {
74       return false;
75     }
76     
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.pentaho.connection.IPentahoConnection#close()
81      */

82     public void close() {
83         // TODO Auto-generated method stub
84

85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.pentaho.connection.IPentahoConnection#getLastQuery()
91      */

92     public String JavaDoc getLastQuery() {
93         return lastQuery;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
100      */

101     public IPentahoResultSet executeQuery(String JavaDoc query) throws XPathException {
102         return executeQuery(query, null);
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
109      */

110     public IPentahoResultSet executeQuery(String JavaDoc query, String JavaDoc columnTypes[]) throws XPathException {
111         XQueryExpression exp = sqc.compileQuery(query);
112         DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
113         try {
114             resultSet = new XQResultSet(exp, dynamicContext, columnTypes);
115         } catch (XPathException e) {
116             if (e.getException() instanceof FileNotFoundException JavaDoc) {
117                 logger.error(Messages.getString("XQConnection.ERROR_0001_UNABLE_TO_READ", query)); //$NON-NLS-1$
118
} else {
119                 logger.error(Messages.getString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), e); //$NON-NLS-1$
120
}
121         } catch (Throwable JavaDoc t) {
122             logger.error(Messages.getErrorString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), t); //$NON-NLS-1$
123
}
124         lastQuery = query;
125         return resultSet;
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.pentaho.connection.IPentahoConnection#isClosed()
132      */

133     public boolean isClosed() {
134         return false;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.pentaho.connection.IPentahoConnection#isReadOnly()
141      */

142     public boolean isReadOnly() {
143         return true;
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.pentaho.connection.IPentahoConnection#clearWarnings()
150      */

151     public void clearWarnings() {
152         // TODO Auto-generated method stub
153

154     }
155
156     public IPentahoResultSet getResultSet() {
157         return resultSet;
158     }
159
160     public boolean connect(Properties JavaDoc props) {
161         String JavaDoc query = props.getProperty(IPentahoConnection.QUERY_KEY);
162         if (query != null && query.length() > 0) {
163             try {
164                 executeQuery(query);
165             } catch (XPathException e) {
166                 logger.error(e.getLocalizedMessage());
167                 return false;
168             }
169         }
170         return true;
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.pentaho.connection.IPentahoConnection#setMaxRows(int)
177      */

178     public void setMaxRows(int maxRows) {
179         // TODO Auto-generated method stub
180
// throw new UnsupportedOperationException();
181
}
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.pentaho.connection.IPentahoConnection#setFetchSize(int)
187      */

188     public void setFetchSize(int fetchSize) {
189         // TODO Auto-generated method stub
190
// throw new UnsupportedOperationException();
191
}
192
193 }
194
Popular Tags