@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); } }; });
@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()); }
/** * Commits and closes a connection. * @param conn Connection * @throws JuDbException If the commit and/or close fails */ public static void commitAndClose(Connection conn) throws JuDbException { try { log.debug("Commiting and closing connection [ID=" + JuObjectUtils.getIdentityString(conn) + "]"); DbUtils.commitAndClose(conn); } catch (SQLException ex) { throw new JuDbException("Couldn't commit and close connection", ex); } }
/** * Rolls back and closes a connection. * @param conn Connection * @throws JuDbException If the rollback and/or close fails */ public static void rollbackAndClose(Connection conn) throws JuDbException { try { log.debug("Rolling back and closing connection [ID=" + JuObjectUtils.getIdentityString(conn) + "]"); DbUtils.rollbackAndClose(conn); } catch (SQLException ex) { throw new JuDbException("Couldn't roll back and close connection", ex); } }
@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); } } });
/** * URL to XML of expected data. * @param xmlUrl URL to XML file location * @return This builder to allow for chaining */ public AssertBuilder expected(URL xmlUrl) { try { this.dataSetUrl = xmlUrl; flatXmlDataSet = new FlatXmlDataSetBuilder().build(xmlUrl); return this; } catch (Exception ex) { throw new JuDbException("Couldn't import data from XML: xmlUrl", ex); } }
@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 void execute(IDataSet dataSet) { try { XmlOutputConverter xmlConv = new XmlOutputConverter(); ExportBuilder.writeToXml(dataSet, xmlConv.getOutputStream()); doc.setValue(xmlConv); } catch (Exception ex) { throw new JuDbException("Couldn't write DB data to XML Output Converter", ex); } } });
@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); } }
@Override public int update(String query, Object... params) throws JuDbException { try { QueryRunner qr = new QueryRunner(); return qr.update(this.getConnection(), query, this.processParams(params)); } catch (SQLException ex) { throw new JuDbException("Couldn't execute update: " + query, ex); } }
@Override public DbRowsImpl query(String query, Object... params) throws JuDbException { try { QueryRunner qr = new QueryRunner(); return qr.query(this.getConnection(), query, new DbRowResultSetHandler(), this.processParams(params)); } catch (SQLException ex) { throw new JuDbException("Couldn't execute query: " + query, ex); } }
@Override public DbRow primaryKeyQuery(String tableName, Object primaryKeyValue) throws JuDbException { String selectQry = "SELECT * FROM " + tableName + " WHERE " + this.dbConnection.getPrimaryColumnName(tableName) + "=?"; DbRows dbRows = this.query(selectQry, primaryKeyValue); if (dbRows.getRowCount() > 1) { throw new JuDbException("PrimaryKeyQuery for " + tableName + " with key=" + primaryKeyValue + ". Expected exactly 1 row, but got " + dbRows.getRowCount()); } return dbRows.getRowCount() == 0 ? null : dbRows.getRow(0); }
@Override public void execute() { XString deleteQry = null; try { deleteQry = new XString("DELETE FROM " + this.getTableName() + " WHERE " + this.getPrimaryKeyValue().getColumnName() + "=?"); int res = this.getDbConnection().getQueryRunner().update(deleteQry.toString(), this.getPrimaryKeyValue().getOriginalValue()); if (res != 1) throw new JuDbException("Execution of query returned " + res + ", expected 1: " + deleteQry); } catch (Exception ex) { throw new JuRuntimeException("Failed to execute delete: " + deleteQry, ex); } }