1 16 17 package org.pentaho.data.connection.xquery; 18 19 import java.io.FileNotFoundException ; 20 import java.util.List ; 21 import java.util.Properties ; 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 39 public class XQConnection implements IPentahoConnection { 40 protected Configuration config = null; 41 42 protected StaticQueryContext sqc = null; 43 44 protected String lastQuery = null; 45 46 protected ILogger logger = null; 47 48 IPentahoResultSet resultSet = null; 49 50 public XQConnection(Properties 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 return true; 67 } 68 69 public IPentahoResultSet prepareAndExecuteQuery(String query, List parameters) throws Exception { 70 throw new UnsupportedOperationException (); 71 } 72 73 public boolean preparedQueriesSupported() { 74 return false; 75 } 76 77 82 public void close() { 83 85 } 86 87 92 public String getLastQuery() { 93 return lastQuery; 94 } 95 96 101 public IPentahoResultSet executeQuery(String query) throws XPathException { 102 return executeQuery(query, null); 103 } 104 105 110 public IPentahoResultSet executeQuery(String query, String 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 ) { 117 logger.error(Messages.getString("XQConnection.ERROR_0001_UNABLE_TO_READ", query)); } else { 119 logger.error(Messages.getString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), e); } 121 } catch (Throwable t) { 122 logger.error(Messages.getErrorString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), t); } 124 lastQuery = query; 125 return resultSet; 126 } 127 128 133 public boolean isClosed() { 134 return false; 135 } 136 137 142 public boolean isReadOnly() { 143 return true; 144 } 145 146 151 public void clearWarnings() { 152 154 } 155 156 public IPentahoResultSet getResultSet() { 157 return resultSet; 158 } 159 160 public boolean connect(Properties props) { 161 String 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 178 public void setMaxRows(int maxRows) { 179 } 182 183 188 public void setFetchSize(int fetchSize) { 189 } 192 193 } 194 | Popular Tags |