@Override public DbRowsImpl handle(ResultSet rs) throws SQLException { DbRowsImpl dbRows = new DbRowsImpl(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { if (rsmd == null) rsmd = rs.getMetaData(); DbRowBuilder rowBuilder = DbRowUtils.newDbRow(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { rowBuilder.addValue(rsmd.getColumnName(i), rsmd.getColumnType(i), this.processValue(rs.getObject(i))); } dbRows.addRow(rowBuilder.getRow()); } // Set base row if query yielded no rows if (dbRows.getRowCount() == 0) { DbRowBuilder rowBuilder = DbRowUtils.newDbRow(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { rowBuilder.addValue(rsmd.getColumnName(i), rsmd.getColumnType(i), null); } dbRows.setBaseRow(rowBuilder.getRow()); } return dbRows; }
@Test public void testNull() { DbRow row = DbRowUtils.newDbRow() .addValue("lower", 1, null) .getRow(); Assert.assertNull(row.getValue("lower")); Assert.assertNull(row.getValue("something")); Assert.assertNull(row.getValue(null)); try { DbRowUtils.newDbRow() .addValue(null, 1, null); Assert.fail("Adding null column should throw NullPointerException"); } catch (NullPointerException ex) { // Expected } } }
@Test public void testUpperAndLowerCase() { DbRow row = DbRowUtils.newDbRow() .addValue("lower", 1, "lower") .addValue("Mixed", 1, "Mixed") .addValue("UPPER", 1, "UPPER") .getRow(); Assert.assertEquals("LOWER", row.getColumnName(0)); Assert.assertEquals("MIXED", row.getColumnName(1)); Assert.assertEquals("UPPER", row.getColumnName(2)); Assert.assertEquals("lower", row.getValue("lower")); Assert.assertEquals("lower", row.getValue("Lower")); Assert.assertEquals("lower", row.getValue("LOWER")); }
@Test public void testEqualsAndHashCode() { DbRowBuilder r1Builder = DbRowUtils.newDbRow(); DbRowBuilder r2Builder = DbRowUtils.newDbRow(); Assert.assertFalse(r1Builder.getRow().hashCode() == r2Builder.getRow().hashCode()); DbRowBuilder r3Builder = DbRowUtils.newDbRow(); r3Builder.addValue("col1", 1, 1); r3Builder.addValue("col2", 2, 2);
@Override public DbAction createUndoAction() { DbRowBuilder targetRowBuilder = DbRowUtils.newDbRow(); for (int i = 0; i < this.getRow().getColumnCount(); i++) { String columnName = this.getRow().getColumnName(i); targetRowBuilder.addValue(columnName, this.getRow().getColumnType(i), this.getVal(columnName).getValue()); } DbAction undoAction = new DeleteDbRowDbAction(this.getDbConnection(), targetRowBuilder.getRow(), this.getTableName()); return undoAction; } }
@Override public DbAction createUndoAction() { DbRowBuilder targetRowBuilder = DbRowUtils.newDbRow(); for (int i = 0; i < this.getRow().getColumnCount(); i++) { String columnName = this.getRow().getColumnName(i); targetRowBuilder.addValue(columnName, this.getRow().getColumnType(i), this.getVal(columnName).getValue()); } DbAction undoAction = new UpdateDbRowDbAction(this.getDbConnection(), targetRowBuilder.getRow(), this.getTableName()); for (Val val : this.getChangedColumns()) { undoAction.setValue(val.getColumnName(), val.getOriginalValue()); } return undoAction; } }