1 16 package org.pentaho.data.connection.sql; 17 18 import java.sql.Connection ; 19 import java.sql.Driver ; 20 import java.sql.DriverManager ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.util.List ; 26 import java.util.Properties ; 27 import javax.sql.DataSource ; 28 import org.pentaho.core.connection.IPentahoConnection; 29 import org.pentaho.core.connection.IPentahoResultSet; 30 import org.pentaho.core.util.DatasourceHelper; 31 import org.pentaho.messages.Messages; 32 import org.pentaho.util.logging.ILogger; 33 34 39 public class SQLConnection implements IPentahoConnection { 40 Connection nativeConnection; 41 44 Statement stmt; 46 IPentahoResultSet sqlResultSet = null; 47 ILogger logger = null; 48 int maxRows = -1; 49 int fetchSize = -1; 50 53 String lastQuery = null; 54 55 59 public SQLConnection(Properties props, ILogger logger) { 60 super(); 61 this.logger = logger; 62 connect(props); 63 } 64 65 public SQLConnection(String jndiName, ILogger logger) { 66 super(); 67 this.logger = logger; 68 initWithJNDI(jndiName); 69 } 70 71 public SQLConnection(String driverName, String location, String userName, String password, ILogger logger) { 72 super(); 73 this.logger = logger; 74 init(driverName, location, userName, password); 75 } 76 77 private void init(String driverName, String location, String userName, String password) { 78 try { 80 83 Driver driver = null; 84 try { 85 driver = DriverManager.getDriver(location); 86 } catch (Exception e) { 87 } 89 if (driver == null) { 90 Class driverClass = Class.forName(driverName); 91 driver = (Driver ) driverClass.newInstance(); 92 DriverManager.registerDriver(driver); 93 } 94 Properties info = new Properties (); 95 info.put("user", userName); info.put("password", password); nativeConnection = driver.connect(location, info); 98 if (nativeConnection == null) { 99 logger.error(Messages.getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION2", driverName, location)); } 101 } catch (Throwable t) { 102 logger.error(Messages.getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION2", driverName, location), t); } 104 } 105 106 public boolean initialized() { 107 return nativeConnection != null; 108 } 109 110 private void initWithJNDI(String jndiName) { 111 try { 114 DataSource dataSource = DatasourceHelper.getDataSourceFromJndi(jndiName); 115 if (dataSource != null) { 116 nativeConnection = dataSource.getConnection(); 117 if (nativeConnection == null) { 118 logger.error(Messages.getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", jndiName)); } 120 } else { 121 logger.error(Messages.getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", jndiName)); } 123 } catch (Exception e) { 124 logger.error(Messages.getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", jndiName), e); } 126 } 127 128 133 public void close() { 134 closeStatement(); 135 if (nativeConnection != null) { 136 try { 137 nativeConnection.close(); 138 } catch (SQLException e) { 139 e.printStackTrace(); 141 } 142 } 143 nativeConnection = null; 144 } 145 146 151 public String getLastQuery() { 152 return lastQuery; 153 } 154 155 160 public IPentahoResultSet executeQuery(String query) throws SQLException { 161 closeStatement(); 162 stmt = nativeConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 164 if (fetchSize > 0) { 165 stmt.setFetchSize(fetchSize); 166 } 167 if (maxRows != -1) { 168 stmt.setMaxRows(maxRows); 169 } 170 ResultSet resultSet = stmt.executeQuery(query); 171 sqlResultSet = new SQLResultSet(resultSet, this); 172 lastQuery = query; 173 return sqlResultSet; 174 } 175 176 public IPentahoResultSet prepareAndExecuteQuery(String query, List parameters) throws SQLException { 177 closeStatement(); 178 PreparedStatement pStmt = nativeConnection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 180 stmt = pStmt; 181 if (fetchSize > 0) { 182 stmt.setFetchSize(fetchSize); 183 } 184 if (maxRows != -1) { 185 stmt.setMaxRows(maxRows); 186 } 187 188 for (int i=0; i<parameters.size(); i++) { 189 pStmt.setObject(i+1, parameters.get(i)); 190 } 191 ResultSet resultSet = pStmt.executeQuery(); 192 sqlResultSet = new SQLResultSet(resultSet, this); 193 lastQuery = query; 194 return sqlResultSet; 195 } 196 197 public boolean preparedQueriesSupported() { 198 return true; 199 } 200 201 206 public boolean isClosed() { 207 try { 208 return nativeConnection.isClosed(); 209 } catch (SQLException e) { 210 e.printStackTrace(); 212 } 213 return true; } 216 217 224 public boolean isReadOnly() { 225 return true; 226 } 227 228 public void clearWarnings() { 229 try { 230 nativeConnection.clearWarnings(); 231 } catch (SQLException e) { 232 e.printStackTrace(); 234 } 235 } 236 237 private void closeStatement() { 238 if (stmt != null) { 239 try { 240 stmt.close(); 241 } catch (Exception ignored) { 242 } 243 } 244 stmt = null; 245 } 246 247 public IPentahoResultSet getResultSet() { 248 return sqlResultSet; 249 } 250 251 public boolean connect(Properties props) { 252 close(); 253 String jndiName = props.getProperty(IPentahoConnection.JNDI_NAME_KEY); 254 if (jndiName != null && jndiName.length() > 0) { 255 initWithJNDI(jndiName); 256 } else { 257 String driver = props.getProperty(IPentahoConnection.DRIVER_KEY); 258 String provider = props.getProperty(IPentahoConnection.LOCATION_KEY); 259 String userName = props.getProperty(IPentahoConnection.USERNAME_KEY); 260 String password = props.getProperty(IPentahoConnection.PASSWORD_KEY); 261 init(driver, provider, userName, password); 262 String query = props.getProperty(IPentahoConnection.QUERY_KEY); 263 if (query != null && query.length() > 0) { 264 try { 265 executeQuery(query); 266 } catch (SQLException e) { 267 e.printStackTrace(); 269 } 270 } 271 } 272 return (nativeConnection != null && !isClosed()); 273 } 274 275 public int execute(String query) throws SQLException { 276 closeStatement(); 277 stmt = nativeConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 279 int result = stmt.executeUpdate(query); 280 lastQuery = query; 281 return result; 282 } 283 284 287 public Connection getNativeConnection() { 288 return nativeConnection; 289 } 290 291 294 public int getFetchSize() { 295 return fetchSize; 296 } 297 298 302 public void setFetchSize(int fetchSize) { 303 this.fetchSize = fetchSize; 304 } 305 306 309 public int getMaxRows() { 310 return maxRows; 311 } 312 313 317 public void setMaxRows(int maxRows) { 318 this.maxRows = maxRows; 319 } 320 } 321 | Popular Tags |