/** * Executes a select * query on the specified table that returns no rows. Can be used * to obtain an empty DbRows instance. * @param tableName Table name * @return DbRows instance with no rows * @throws JuDbException If the query fails */ protected static DbRows emptyRowsQuery(DbConnection dbConn, String tableName) throws JuDbException { String selectQry = "SELECT * FROM " + tableName + " WHERE 1=0"; return dbConn.getQueryRunner(). query(selectQry); } }
/** * Gets a new Delete Action for the specified row, using a primaryKeyValue. * @param dbConn DbConnection * @param tableName Table name * @param primaryKeyValue Primary key value * @return DbAction instance to delete the row * @throws IllegalArgumentException If the specified row cannot be found */ public static DbAction newDeleteAction(DbConnection dbConn, String tableName, Object primaryKeyValue) { DbRow row = dbConn.getQueryRunner().primaryKeyQuery(tableName, primaryKeyValue); if (row == null) throw new IllegalArgumentException("No row found on table " + tableName + " with ID " + primaryKeyValue); return DbActionUtils.newDeleteAction(dbConn, row, tableName); }
/** * Gets a new UpdateActionBuilder for the specified row, using a primaryKeyValue. * @param dbConn DbConnection * @param tableName Table name * @param primaryKeyValue Primary key value * @return AbstractActionBuilder instance to build the action * @throws IllegalArgumentException If the specified row cannot be found */ public static AbstractActionBuilder newUpdateAction(DbConnection dbConn, String tableName, Object primaryKeyValue) { DbRow row = dbConn.getQueryRunner().primaryKeyQuery(tableName, primaryKeyValue); if (row == null) throw new IllegalArgumentException("No row found on table " + tableName + " with ID " + primaryKeyValue); return new UpdateActionBuilder(dbConn, row, tableName); }
private InsertActionBuilder(DbConnection dbConn, String tableName) { super(new InsertDbRowDbAction(dbConn, dbConn.getQueryRunner().emptyRowQuery(tableName), tableName)); } }
@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); } }
@Override public void execute() { XString insertQry = null; try { insertQry = new XString("INSERT INTO " + this.getTableName() + " ("); ArrayList<Object> values = new ArrayList<Object>(); XString valuesQry = new XString("VALUES ("); for (int i = 0; i < this.getRow().getColumnCount(); i++) { String columnName = this.getRow().getColumnName(i); insertQry.assertText("(", ", "); insertQry.addText(columnName); valuesQry.assertText("(", ", "); valuesQry.addText("?"); values.add(this.getVal(columnName).getValue()); } insertQry.addText(") "); insertQry.addText(valuesQry); insertQry.addText(")"); int res = this.getDbConnection().getQueryRunner().update(insertQry.toString(), (Object[])values.toArray(new Object[0])); if (res != 1) throw new JuDbException("Execution of query returned " + res + ", expected 1: " + insertQry); } catch (Exception ex) { throw new JuRuntimeException("Failed to execute insert: " + insertQry, ex); } }
@Override public void execute() { XString updateQry = null; try { Val[] changedColumns = this.getChangedColumns(); if (changedColumns.length < 1) return; Object[] changedVals = new Object[changedColumns.length + 1]; updateQry = new XString("UPDATE " + this.getTableName() + " SET "); int i = 0; for (Val changedColumn : changedColumns) { updateQry.assertText("SET ", ", "); updateQry.addText(changedColumn.getColumnName() + "=?"); changedVals[i] = changedColumns[i].getChangedValue(); i++; } updateQry.addText(" WHERE " + this.getPrimaryKeyValue().getColumnName() + "=?"); changedVals[i] = this.getPrimaryKeyValue().getOriginalValue(); int res = this.getDbConnection().getQueryRunner().update(updateQry.toString(), changedVals); if (res != 1) throw new JuDbException("Execution of query returned " + res + ", expected 1: " + updateQry); } catch (Exception ex) { throw new JuRuntimeException("Failed to execute update: " + updateQry, ex); } }