1 21 package org.dbunit.database; 22 23 import org.dbunit.dataset.AbstractTable; 24 import org.dbunit.dataset.DataSetException; 25 import org.dbunit.dataset.ITableMetaData; 26 import org.dbunit.dataset.datatype.IDataTypeFactory; 27 28 import java.sql.Connection ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 33 38 public abstract class AbstractResultSetTable extends AbstractTable 39 implements IResultSetTable 40 { 41 protected ITableMetaData _metaData; 42 private Statement _statement; 43 protected ResultSet _resultSet; 44 45 public AbstractResultSetTable(ITableMetaData metaData, ResultSet resultSet) 46 throws SQLException , DataSetException 47 { 48 _metaData = metaData; 49 _resultSet = resultSet; 50 } 51 52 public AbstractResultSetTable(String tableName, String selectStatement, 53 IDatabaseConnection connection) 54 throws DataSetException, SQLException 55 { 56 Connection jdbcConnection = connection.getConnection(); 57 _statement = jdbcConnection.createStatement(); 58 60 DatabaseConfig config = connection.getConfig(); 61 IDataTypeFactory dataTypeFactory = (IDataTypeFactory)config.getProperty( 62 DatabaseConfig.PROPERTY_DATATYPE_FACTORY); 63 64 try 65 { 66 _resultSet = _statement.executeQuery(selectStatement); 67 _metaData = DatabaseTableMetaData.createMetaData(tableName, 68 _resultSet, dataTypeFactory); 69 } 70 catch (SQLException e) 71 { 72 _statement.close(); 73 _statement = null; 74 throw e; 75 } 76 } 77 78 public AbstractResultSetTable(ITableMetaData metaData, 79 IDatabaseConnection connection) throws DataSetException, SQLException 80 { 81 Connection jdbcConnection = connection.getConnection(); 82 String escapePattern = (String )connection.getConfig().getProperty( 83 DatabaseConfig.PROPERTY_ESCAPE_PATTERN); 84 _statement = jdbcConnection.createStatement(); 85 87 try 88 { 89 String schema = connection.getSchema(); 90 String selectStatement = getSelectStatement(schema, metaData, escapePattern); 91 _resultSet = _statement.executeQuery(selectStatement); 92 _metaData = metaData; 93 } 94 catch (SQLException e) 95 { 96 _statement.close(); 97 _statement = null; 98 throw e; 99 } 100 } 101 102 static String getSelectStatement(String schema, ITableMetaData metaData, String escapePattern) 103 throws DataSetException 104 { 105 return DatabaseDataSet.getSelectStatement(schema, metaData, escapePattern); 106 } 107 108 111 public ITableMetaData getTableMetaData() 112 { 113 return _metaData; 114 } 115 116 119 public void close() throws DataSetException 120 { 121 try 122 { 123 if (_resultSet != null) 124 { 125 _resultSet.close(); 126 _resultSet = null; 127 } 128 129 if (_statement != null) 130 { 131 _statement.close(); 132 _statement = null; 133 } 134 } 135 catch (SQLException e) 136 { 137 throw new DataSetException(e); 138 } 139 } 140 } 141 | Popular Tags |