/** * Delete all rows matching the given {@link Criterion} * * @param modelClass model class for the table to delete from * @param where the Criterion to match. Note: passing null will delete all rows! * @return the number of deleted rows */ public int deleteWhere(Class<? extends TableModel> modelClass, Criterion where) { Table table = getTable(modelClass); Delete delete = Delete.from(table); if (where != null) { delete.where(where); } int rowsUpdated = deleteInternal(delete); if (rowsUpdated > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, table, TableModel.NO_ID); } return rowsUpdated; }
/** * Construct a new Delete statement on the specified {@link View}. Note that deletes on a View are only permissible * when an INSTEAD OF {@link com.yahoo.squidb.sql.Trigger} is constructed on that View. */ public static Delete from(View view) { return new Delete(view); }
/** * Executes a {@link Delete} statement. * <p> * Note: Generally speaking, you should prefer to use {@link #delete(Class, long) delete} or * {@link #deleteWhere(Class, Criterion) deleteWhere} for deleting database rows. This is provided as a convenience * in case there exists a non-ORM case where a more traditional SQL delete statement is required. * * @param delete the statement to execute * @return the number of rows deleted on success, -1 on failure */ public int delete(Delete delete) { int result = deleteInternal(delete); if (result > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, delete.getTable(), TableModel.NO_ID); } return result; }
public void testDeleteWhere() { Criterion criterion = TestModel.LUCKY_NUMBER.lte(0); // check preconditions TestModel shouldBeFound = database.fetchByCriterion(TestModel.class, criterion, TestModel.PROPERTIES); assertNotNull(shouldBeFound); // delete from testModels where testModels.luckyNumber <= 0; Delete delete = Delete.from(TestModel.TABLE).where(criterion); CompiledStatement compiled = delete.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 1, 0); assertEquals(1, database.delete(delete)); int numRows = database.countAll(TestModel.class); assertEquals(3, numRows); TestModel shouldNotBeFound = database.fetchByCriterion(TestModel.class, criterion, TestModel.PROPERTIES); assertNull(shouldNotBeFound); }
public void testDeleteAll() { // check preconditions int numRows = database.countAll(TestModel.class); assertTrue(numRows > 0); // delete from testModels Delete delete = Delete.from(TestModel.TABLE); CompiledStatement compiled = delete.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 0); assertEquals(numRows, database.delete(delete)); numRows = database.countAll(TestModel.class); assertEquals(0, numRows); } }
/** * Execute a SQL {@link com.yahoo.squidb.sql.Delete} statement * * @return the number of rows deleted on success, -1 on failure */ private int deleteInternal(Delete delete) { CompiledStatement compiled = delete.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeUpdateDelete(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
@Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("DELETE FROM ").append(table.getExpression()); visitWhere(builder, forSqlValidation); }
/** * Adds a WHERE clause to this statement. Calling this method multiple times will combine all the {@link Criterion * criterions} with AND. * * @param criterion A criterion to use in the where clause * @return this Delete object, to allow chaining method calls */ public Delete where(Criterion criterion) { if (criterion != null) { this.criterions.add(criterion); invalidateCompileCache(); } return this; }
/** * Execute a SQL {@link com.yahoo.squidb.sql.Delete} statement * * @return the number of rows deleted on success, -1 on failure */ private int deleteInternal(Delete delete) { CompiledStatement compiled = delete.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeUpdateDelete(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
@Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("DELETE FROM ").append(table.getExpression()); visitWhere(builder, forSqlValidation); }
/** * Adds a WHERE clause to this statement. Calling this method multiple times will combine all the {@link Criterion * criterions} with AND. * * @param criterion A criterion to use in the where clause * @return this Delete object, to allow chaining method calls */ public Delete where(Criterion criterion) { if (criterion != null) { this.criterions.add(criterion); invalidateCompileCache(); } return this; }
/** * Delete the row with the given row ID * * @param modelClass the model class corresponding to the table to delete from * @param id the row ID of the record * @return true if delete was successful */ public boolean delete(Class<? extends TableModel> modelClass, long id) { Table table = getTable(modelClass); int rowsUpdated = deleteInternal(Delete.from(table).where(table.getRowIdProperty().eq(id))); if (rowsUpdated > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, table, id); } return rowsUpdated > 0; }
/** * Construct a new Delete statement on the specified {@link Table} or {@link VirtualTable} */ public static Delete from(Table table) { return new Delete(table); }
/** * Executes a {@link Delete} statement. * <p> * Note: Generally speaking, you should prefer to use {@link #delete(Class, long) delete} or * {@link #deleteWhere(Class, Criterion) deleteWhere} for deleting database rows. This is provided as a convenience * in case there exists a non-ORM case where a more traditional SQL delete statement is required. * * @param delete the statement to execute * @return the number of rows deleted on success, -1 on failure */ public int delete(Delete delete) { int result = deleteInternal(delete); if (result > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, delete.getTable(), TableModel.NO_ID); } return result; }
/** * Construct a new Delete statement on the specified {@link View}. Note that deletes on a View are only permissible * when an INSTEAD OF {@link com.yahoo.squidb.sql.Trigger} is constructed on that View. */ public static Delete from(View view) { return new Delete(view); }
public void testMissingTriggerEventThrowsIllegalStateException() { Delete delete = Delete.from(TestModel.TABLE).where(TestModel.IS_HAPPY.isFalse()); final Trigger trigger = Trigger.after("trigger").when(TestModel.IS_HAPPY.isFalse()) .perform(delete); testThrowsException(new Runnable() { public void run() { trigger.compile(database.getCompileContext()); } }, IllegalStateException.class); }
/** * Construct a new Delete statement on the specified {@link Table} or {@link VirtualTable} */ public static Delete from(Table table) { return new Delete(table); }
/** * Delete all rows matching the given {@link Criterion} * * @param modelClass model class for the table to delete from * @param where the Criterion to match. Note: passing null will delete all rows! * @return the number of deleted rows */ public int deleteWhere(Class<? extends TableModel> modelClass, Criterion where) { Table table = getTable(modelClass); Delete delete = Delete.from(table); if (where != null) { delete.where(where); } int rowsUpdated = deleteInternal(delete); if (rowsUpdated > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, table, TableModel.NO_ID); } return rowsUpdated; }
/** * Delete the row with the given row ID * * @param modelClass the model class corresponding to the table to delete from * @param id the row ID of the record * @return true if delete was successful */ public boolean delete(Class<? extends TableModel> modelClass, long id) { Table table = getTable(modelClass); int rowsUpdated = deleteInternal(Delete.from(table).where(table.getRowIdProperty().eq(id))); if (rowsUpdated > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, table, id); } return rowsUpdated > 0; }