/** * Perform an additional SELECT .. FOR UPDATE to check if the underlying * database record has been changed compared to this record. */ private final void checkIfChanged(TableField<R, ?>[] keys) { SelectQuery<R> select = create().selectQuery(getTable()); Tools.addConditions(select, this, keys); // [#1547] MS Access and SQLite doesn't support FOR UPDATE. CUBRID and SQL Server // can emulate it, though! if (!NO_SUPPORT_FOR_UPDATE.contains(create().configuration().dialect().family())) select.setForUpdate(true); R record = select.fetchOne(); if (record == null) throw new DataChangedException("Database record no longer exists"); for (Field<?> field : fields.fields.fields) { Object thisObject = original(field); Object thatObject = record.original(field); if (!StringUtils.equals(thisObject, thatObject)) throw new DataChangedException("Database record has been changed"); } }
/** * Perform an additional SELECT .. FOR UPDATE to check if the underlying * database record has been changed compared to this record. */ private final void checkIfChanged(TableField<R, ?>[] keys) { SelectQuery<R> select = create().selectQuery(getTable()); Utils.addConditions(select, this, keys); // [#1547] MS Access and SQLite doesn't support FOR UPDATE. CUBRID and SQL Server // can simulate it, though! if (!asList(SQLITE).contains(create().configuration().dialect().family())) { select.setForUpdate(true); } R record = select.fetchOne(); if (record == null) { throw new DataChangedException("Database record no longer exists"); } for (Field<?> field : fields.fields.fields) { Value<?> thisValue = getValue0(field); Value<?> thatValue = ((AbstractRecord) record).getValue0(field); Object thisObject = thisValue.getOriginal(); Object thatObject = thatValue.getOriginal(); if (!StringUtils.equals(thisObject, thatObject)) { throw new DataChangedException("Database record has been changed"); } } }
private final int storeInsert0() { DSLContext create = create(); InsertQuery<R> insert = create.insertQuery(getTable()); addChangedValues(insert);
private final int delete0() { TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray(); try { DeleteQuery<R> delete1 = create().deleteQuery(getTable()); Utils.addConditions(delete1, this, keys); if (isExecuteWithOptimisticLocking()) { // [#1596] Add additional conditions for version and/or timestamp columns if (isTimestampOrVersionAvailable()) { addConditionForVersionAndTimestamp(delete1); } // [#1547] Try fetching the Record again first, and compare this // Record's original values with the ones in the database else { checkIfChanged(keys); } } int result = delete1.execute(); checkIfChanged(result, null, null); return result; } // [#673] If store() is called after delete(), a new INSERT should // be executed and the record should be recreated finally { changed(true); } }
@Override public final void refresh(final Field<?>... refreshFields) { SelectQuery<Record> select = create().selectQuery(); select.addSelect(refreshFields); select.addFrom(getTable()); Tools.addConditions(select, this, getPrimaryKey().getFieldsArray()); if (select.execute() == 1) { final AbstractRecord source = (AbstractRecord) select.getResult().get(0); delegate(configuration(), (Record) this, REFRESH) .operate(new RecordOperation<Record, RuntimeException>() { @Override public Record operate(Record record) throws RuntimeException { setValues(refreshFields, source); return record; } }); } else { throw new NoDataFoundException("Exactly one row expected for refresh. Record does not exist in database."); } }
@Override public final void refresh(final Field<?>... f) { SelectQuery<Record> select = create().selectQuery(); select.addSelect(f); select.addFrom(getTable()); Utils.addConditions(select, this, getPrimaryKey().getFieldsArray()); if (select.execute() == 1) { final AbstractRecord source = (AbstractRecord) select.getResult().get(0); delegate(configuration(), (Record) this, REFRESH) .operate(new RecordOperation<Record, RuntimeException>() { @Override public Record operate(Record record) throws RuntimeException { setValues(f, source); return record; } }); } else { throw new InvalidResultException("Exactly one row expected for refresh. Record does not exist in database."); } }
private final int delete0() { TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray(); try { DeleteQuery<R> delete1 = create().deleteQuery(getTable()); Tools.addConditions(delete1, this, keys); if (isExecuteWithOptimisticLocking()) { // [#1596] Add additional conditions for version and/or timestamp columns if (isTimestampOrVersionAvailable()) { addConditionForVersionAndTimestamp(delete1); } // [#1547] Try fetching the Record again first, and compare this // Record's original values with the ones in the database // [#5384] Do this only if the exclusion flag for unversioned records is off else if (isExecuteWithOptimisticLockingIncludeUnversioned()) { checkIfChanged(keys); } } int result = delete1.execute(); checkIfChanged(result, null, null); return result; } // [#673] [#3363] If store() is called after delete(), a new INSERT should // be executed and the record should be recreated finally { changed(true); fetched = false; } }
private final int storeUpdate0(TableField<R, ?>[] keys) { UpdateQuery<R> update = create().updateQuery(getTable()); addChangedValues(update); Utils.addConditions(update, this, keys); // Don't store records if no value was set by client code if (!update.isExecutable()) return 0; // [#1596] Set timestamp and/or version columns to appropriate values BigInteger version = addRecordVersion(update); Timestamp timestamp = addRecordTimestamp(update); if (isExecuteWithOptimisticLocking()) { // [#1596] Add additional conditions for version and/or timestamp columns if (isTimestampOrVersionAvailable()) { addConditionForVersionAndTimestamp(update); } // [#1547] Try fetching the Record again first, and compare this // Record's original values with the ones in the database else { checkIfChanged(keys); } } // [#1596] Check if the record was really changed in the database int result = update.execute(); checkIfChanged(result, version, timestamp); if (result > 0) { changed(false); } return result; }
private final int storeUpdate0(Field<?>[] storeFields, TableField<R, ?>[] keys) { UpdateQuery<R> update = create().updateQuery(getTable()); addChangedValues(storeFields, update); Tools.addConditions(update, this, keys);