1 16 17 package org.pentaho.data.connection.mdx; 18 19 import java.util.Enumeration ; 20 import java.util.List ; 21 import java.util.Properties ; 22 23 import mondrian.olap.Connection; 24 import mondrian.olap.DriverManager; 25 import mondrian.olap.Query; 26 import mondrian.olap.Result; 27 import mondrian.olap.Util; 28 29 import org.pentaho.core.connection.IPentahoConnection; 30 import org.pentaho.core.connection.IPentahoResultSet; 31 import org.pentaho.messages.Messages; 32 import org.pentaho.util.logging.ILogger; 33 34 40 public class MDXConnection implements IPentahoConnection { 41 45 public static final String CONNECTION_STRING_KEY = "mdx-connection-string"; 47 Connection nativeConnection = null; 48 49 String lastQuery = null; 50 51 IPentahoResultSet resultSet = null; 52 53 ILogger logger = null; 54 55 public MDXConnection(Properties props, ILogger logger) { 56 super(); 57 58 init(props); 59 this.logger = logger; 60 } 61 62 71 public MDXConnection(String driver, String provider, String userName, String password) { 72 super(); 73 74 init(driver, provider, userName, password); 75 } 76 77 public MDXConnection(String connectStr, ILogger logger) { 78 super(); 79 init(connectStr); 80 this.logger = logger; 81 } 82 83 private void init(String connectStr) { 84 try { 85 if (nativeConnection != null) { close(); 87 } 88 nativeConnection = DriverManager.getConnection(connectStr, null, false); 89 if (nativeConnection == null) { 90 logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", connectStr)); } 92 } catch (Throwable t) { 93 logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage())); } 95 } 96 97 private void init(Properties properties) { 98 try { 99 if (nativeConnection != null) { close(); 101 } 102 103 Util.PropertyList pl = new Util.PropertyList(); 104 Enumeration enum1 = properties.keys(); 105 while(enum1.hasMoreElements()) { 106 Object key = enum1.nextElement(); 107 Object value = properties.get(key); 108 pl.put(key.toString(), value.toString()); 109 } 110 nativeConnection = DriverManager.getConnection(pl, null, false); 111 } catch (Throwable t) { 112 logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage())); } 114 } 115 116 private void init(String driver, String provider, String userName, String password) { 117 StringBuffer buffer = new StringBuffer (); 118 buffer.append("provider=" + provider); 126 131 if (driver.indexOf("dataSource=") >=0 ) { buffer.append("; ").append(driver); } else { 134 buffer.append("; Jdbc=" + driver); } 136 if (userName != null) { 137 buffer.append("; user=" + userName); } 139 if (password != null) { 140 buffer.append("; password=" + password); } 142 init(buffer.toString()); 143 } 144 145 public boolean initialized() { 146 return nativeConnection != null; 147 } 148 149 public IPentahoResultSet prepareAndExecuteQuery(String query, List parameters) throws Exception { 150 throw new UnsupportedOperationException (); 151 } 152 153 public boolean preparedQueriesSupported() { 154 return false; 155 } 156 157 162 public void close() { 163 nativeConnection.close(); 164 } 165 166 171 public String getLastQuery() { 172 return lastQuery; 173 } 174 175 180 public IPentahoResultSet executeQuery(String query) { 181 Query mdxQuery = nativeConnection.parseQuery(query); 182 Result result = nativeConnection.execute(mdxQuery); 183 resultSet = new MDXResultSet(result, nativeConnection); 184 return resultSet; 185 } 186 187 192 public boolean isClosed() { 193 return false; 194 } 195 196 201 public boolean isReadOnly() { 202 return true; 203 } 204 205 210 public void clearWarnings() { 211 213 } 214 215 public IPentahoResultSet getResultSet() { 216 return resultSet; 217 } 218 219 public boolean connect(Properties props) { 220 if (nativeConnection != null) { close(); 222 } 223 init(props); 224 String query = props.getProperty(IPentahoConnection.QUERY_KEY); 225 if (query != null && query.length() > 0 && nativeConnection != null) { 226 executeQuery(query); 227 } 228 return nativeConnection != null; 229 } 230 231 236 public void setMaxRows(int maxRows) { 237 throw new UnsupportedOperationException (); 239 } 240 241 246 public void setFetchSize(int fetchSize) { 247 throw new UnsupportedOperationException (); 249 } 250 251 public Connection getConnection() { 252 return nativeConnection; 253 } 254 } | Popular Tags |