/** * Truncates all tables included in the data set. */ public void executeDeleteAll() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Delete-All from: " + dataSetUrl); DatabaseOperation.DELETE_ALL.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt truncate data in DB", ex); } }; }); }
/** * Performs an update of the existing data in the DB, without inserting new data. */ public void executeUpdate() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Update from: " + dataSetUrl); DatabaseOperation.UPDATE.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt update data in DB", ex); } }; }); } }
/** * Asserts that the whole data set in the DB equals the expected data. * TODO: Add functionality to exclude (system) tables */ public void assertEqualsAll() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { IDataSet dbDataSet = conn.createDataSet(); Assertion.assertEquals(flatXmlDataSet, dbDataSet); } catch (Exception ex) { throw new JuDbException("Couldn't assert DB data", ex); } } }); }
/** * Performs a clean import of the data into the DB, i.e. cleans any existing * data in affected tables and imports the rows specified in in this builder. */ public void executeCleanInsert() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Clean-Insert from: " + dataSetUrl); DatabaseOperation.CLEAN_INSERT.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldn't clean and insert data into DB", ex); } } }); }
/** * Performs an import of the data into the DB, without cleaning any data * previously. */ public void executeInsert() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Insert from: " + dataSetUrl); DatabaseOperation.INSERT.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt insert data into DB", ex); } }; }); }
/** * Asserts that the data exported based on the result data set (i.e. all tables contained in the dataset, sorted * by primary key) equals the result data set. */ public void assertEquals() { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { IDataSet dbDataSet = dbDataUtil.buildExport() .addTablesByDataSet(dataSetUrl, true) .createDataSet(conn); Assertion.assertEquals(flatXmlDataSet, dbDataSet); } catch (Exception ex) { throw new JuDbException("Couldn't assert DB data", ex); } } }); }
/** * Asserts that the export from the specified table equals the expected data. * @param tableName Name of the table to assert * @param orderColumnName Name of the column to order data by for the export */ public void assertEqualsTable(final String tableName, final String orderColumnName) { this.dbDataUtil.execute(new DbUnitWork() { @Override public void execute(IDatabaseConnection conn) { try { QueryDataSet tableDataSet = new QueryDataSet(conn); tableDataSet.addTable(tableName, String.format("select * from %s order by %s", tableName, orderColumnName)); Assertion.assertEquals(flatXmlDataSet, tableDataSet); } catch (Exception ex) { throw new JuDbException("Couldn't assert DB data", ex); } } }); } }
@Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { ResultSet rs = dbmd.getPrimaryKeys(null, null, tableName.toUpperCase()); String columnName = null; if (rs.next()) columnName = rs.getString("COLUMN_NAME"); else throw new JuDbException("Couldn't evaluate primary key for table " + tableName); if (rs.next()) { throw new JuDbException("Driver returned multiple primary keys for table " + tableName); } rs.close(); return columnName.toUpperCase(); } });
@Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Insert from: " + dataSetUrl); DatabaseOperation.INSERT.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt insert data into DB", ex); } }; });
static DbType evaluateDbType(String productName) { if (productName.toLowerCase().contains("derby")) { return DERBY; } else if (productName.toLowerCase().contains("h2")) { return H2; } else if (productName.toLowerCase().contains("hsql")) { return HSQL; } else if (productName.toLowerCase().contains("mysql")) { return MYSQL; } else if (productName.toLowerCase().contains("oracle")) { return ORACLE; } else { throw new JuDbException("Unknown DB. Product name: " + productName); } }
@Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Delete-All from: " + dataSetUrl); DatabaseOperation.DELETE_ALL.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt truncate data in DB", ex); } }; });
@Override public void dropSchema(SchemaInfo schemaInfo, String... users) { throw new JuDbException("Schema Dropping not yet implemented for " + this.connUtil.getDbType()); } }
@Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Update from: " + dataSetUrl); DatabaseOperation.UPDATE.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldnt update data in DB", ex); } }; });
@Override public void execute(IDatabaseConnection conn) { try { logger.debug("Executing Clean-Insert from: " + dataSetUrl); DatabaseOperation.CLEAN_INSERT.execute(conn, getDataSet(conn)); } catch (Exception ex) { throw new JuDbException("Couldn't clean and insert data into DB", ex); } } });
@Override public void createSchema(DbSchemaBuilder builder) { throw new JuDbException("Schema Creation not yet implemented for " + this.connUtil.getDbType()); }
/** * Creates a new DbMetaData instance. If an exception occurs, close will be called * implicitly. * @throws JuDbException If the MetaData cannot be accessed */ public DbMetaData() throws JuDbException { try { this.metaData = DbConnectionImpl.this.getConnection().getMetaData(); } catch (SQLException ex) { throw new JuDbException("Couldn't access DatabaseMetaData", ex); } }
@Override public List<String> getSequenceNames() { throw new JuDbException("Sequences not supported by " + this.connUtil.getDbType()); }
@Override public Long getNextValueFromSequence(String sequenceName) { throw new JuDbException("Sequences not supported by " + this.connUtil.getDbType()); }
@Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { ResultSet rs = dbmd.getColumns(null, null, tableName.toUpperCase(), null); List<String> columnNames = new ArrayList<>(); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); columnNames.add(columnName.toUpperCase()); } rs.close(); if (columnNames.size() == 0) { throw new JuDbException("Couldn't evaluate column names for table " + tableName + ": Driver returned empty ResultSet."); } return columnNames; } });
@Override public void addToQueryDataSet(QueryDataSet dataSet, List<String> casedTableNames) { try { String actualTableName = this.tableName; for (String casedTableName : casedTableNames) { if (casedTableName.equalsIgnoreCase(this.tableName)) { actualTableName = casedTableName; break; } } dataSet.addTable(actualTableName, this.query); } catch (AmbiguousTableNameException ex) { throw new JuDbException(String.format("Couldn't add table %s to QueryDataSet: %s", this.tableName, this.query), ex); } } }