/** * Save a model to the database. Creates a new row if the model does not have an ID, otherwise updates the row with * the corresponding row ID. If a new row is inserted, the model will have its ID set to the corresponding row ID. * Any constraint violations will be resolved using the specified * {@link com.yahoo.squidb.sql.TableStatement.ConflictAlgorithm ConflictAlgorithm}. * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if current the model data is stored in the database * @see #persist(TableModel) */ public boolean persistWithOnConflict(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isSaved()) { return insertRow(item, conflictAlgorithm); } if (!item.isModified()) { return true; } return updateRow(item, conflictAlgorithm); }
/** * Same as {@link #updateRow(TableModel)} with the ability to specify a ConflictAlgorithm for handling constraint * violations * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if success, false otherwise */ protected final boolean updateRow(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isModified()) { // nothing changed return true; } if (!item.isSaved()) { return false; } Class<? extends TableModel> modelClass = item.getClass(); Table table = getTable(modelClass); Update update = Update.table(table).fromTemplate(item).where(table.getRowIdProperty().eq(item.getRowId())); if (conflictAlgorithm != null) { update.onConflict(conflictAlgorithm); } boolean result = updateInternal(update) > 0; if (result) { notifyForTable(DataChangedNotifier.DBOperation.UPDATE, item, table, item.getRowId()); item.markSaved(); } return result; }
/** * Save a model to the database. Creates a new row if the model does not have an ID, otherwise updates the row with * the corresponding row ID. If a new row is inserted, the model will have its ID set to the corresponding row ID. * Any constraint violations will be resolved using the specified * {@link com.yahoo.squidb.sql.TableStatement.ConflictAlgorithm ConflictAlgorithm}. * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if current the model data is stored in the database * @see #persist(TableModel) */ public boolean persistWithOnConflict(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isSaved()) { return insertRow(item, conflictAlgorithm); } if (!item.isModified()) { return true; } return updateRow(item, conflictAlgorithm); }
/** * Same as {@link #updateRow(TableModel)} with the ability to specify a ConflictAlgorithm for handling constraint * violations * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if success, false otherwise */ protected final boolean updateRow(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isModified()) { // nothing changed return true; } if (!item.isSaved()) { return false; } Class<? extends TableModel> modelClass = item.getClass(); Table table = getTable(modelClass); Update update = Update.table(table).fromTemplate(item).where(table.getRowIdProperty().eq(item.getRowId())); if (conflictAlgorithm != null) { update.onConflict(conflictAlgorithm); } boolean result = updateInternal(update) > 0; if (result) { notifyForTable(DataChangedNotifier.DBOperation.UPDATE, item, table, item.getRowId()); item.markSaved(); } return result; }