001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Sep 15, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.data.connection.xquery;
018:
019: import java.io.FileNotFoundException;
020: import java.util.List;
021: import java.util.Properties;
022: import net.sf.saxon.Configuration;
023: import net.sf.saxon.query.DynamicQueryContext;
024: import net.sf.saxon.query.StaticQueryContext;
025: import net.sf.saxon.query.XQueryExpression;
026: import net.sf.saxon.trans.XPathException;
027:
028: import org.pentaho.commons.connection.IPentahoConnection;
029: import org.pentaho.commons.connection.IPentahoResultSet;
030: import org.pentaho.messages.Messages;
031: import org.pentaho.util.logging.ILogger;
032:
033: /**
034: * @author wseyler
035: *
036: * TODO To change the template for this generated type comment go to Window -
037: * Preferences - Java - Code Style - Code Templates
038: */
039: public class XQConnection implements IPentahoConnection {
040: protected Configuration config = null;
041:
042: protected StaticQueryContext sqc = null;
043:
044: protected String lastQuery = null;
045:
046: protected ILogger logger = null;
047:
048: IPentahoResultSet resultSet = null;
049:
050: public XQConnection(Properties props, ILogger logger) {
051: this (logger);
052:
053: connect(props);
054: }
055:
056: public XQConnection(ILogger logger) {
057: super ();
058:
059: this .logger = logger;
060: config = new Configuration();
061: sqc = new StaticQueryContext(config);
062: }
063:
064: public boolean initialized() {
065: // TODO create a good test
066: return true;
067: }
068:
069: public IPentahoResultSet prepareAndExecuteQuery(String query,
070: List parameters) throws Exception {
071: throw new UnsupportedOperationException();
072: }
073:
074: public boolean preparedQueriesSupported() {
075: return false;
076: }
077:
078: /**
079: * return datasource type MDX
080: * @return datasource type
081: */
082: public int getDatasourceType() {
083: return XML_DATASOURCE;
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.pentaho.connection.IPentahoConnection#close()
090: */
091: public void close() {
092: // TODO Auto-generated method stub
093:
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @see org.pentaho.connection.IPentahoConnection#getLastQuery()
100: */
101: public String getLastQuery() {
102: return lastQuery;
103: }
104:
105: /*
106: * (non-Javadoc)
107: *
108: * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
109: */
110: public IPentahoResultSet executeQuery(String query)
111: throws XPathException {
112: return executeQuery(query, null);
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
119: */
120: public IPentahoResultSet executeQuery(String query,
121: String columnTypes[]) throws XPathException {
122: XQueryExpression exp = sqc.compileQuery(query);
123: DynamicQueryContext dynamicContext = new DynamicQueryContext(
124: config);
125: try {
126: resultSet = new XQResultSet(exp, dynamicContext,
127: columnTypes);
128: } catch (XPathException e) {
129: if (e.getException() instanceof FileNotFoundException) {
130: logger
131: .error(Messages
132: .getString(
133: "XQConnection.ERROR_0001_UNABLE_TO_READ", query)); //$NON-NLS-1$
134: } else {
135: logger
136: .error(
137: Messages
138: .getString(
139: "XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), e); //$NON-NLS-1$
140: }
141: } catch (Throwable t) {
142: logger
143: .error(
144: Messages
145: .getErrorString(
146: "XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), t); //$NON-NLS-1$
147: }
148: lastQuery = query;
149: return resultSet;
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see org.pentaho.connection.IPentahoConnection#isClosed()
156: */
157: public boolean isClosed() {
158: return false;
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.pentaho.connection.IPentahoConnection#isReadOnly()
165: */
166: public boolean isReadOnly() {
167: return true;
168: }
169:
170: /*
171: * (non-Javadoc)
172: *
173: * @see org.pentaho.connection.IPentahoConnection#clearWarnings()
174: */
175: public void clearWarnings() {
176: // TODO Auto-generated method stub
177:
178: }
179:
180: public IPentahoResultSet getResultSet() {
181: return resultSet;
182: }
183:
184: public boolean connect(Properties props) {
185: String query = props.getProperty(IPentahoConnection.QUERY_KEY);
186: if (query != null && query.length() > 0) {
187: try {
188: executeQuery(query);
189: } catch (XPathException e) {
190: logger.error(e.getLocalizedMessage());
191: return false;
192: }
193: }
194: return true;
195: }
196:
197: /*
198: * (non-Javadoc)
199: *
200: * @see org.pentaho.connection.IPentahoConnection#setMaxRows(int)
201: */
202: public void setMaxRows(int maxRows) {
203: // TODO Auto-generated method stub
204: // throw new UnsupportedOperationException();
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see org.pentaho.connection.IPentahoConnection#setFetchSize(int)
211: */
212: public void setFetchSize(int fetchSize) {
213: // TODO Auto-generated method stub
214: // throw new UnsupportedOperationException();
215: }
216:
217: }
|