delete method
- @Deprecated('This method is deprecated and will be deleted on the next release. Please use `deleteSingle` or `batchDelete` instead.')
This function deletes a record from the database.
The function takes an object obj and a boolean all as parameters. The all parameter is used to indicate whether you want to delete the entire table or not.
by default it is set to true.
If all is true, the entire table is deleted.
If all is false, only the record passed as a parameter is deleted.
If you do not want to delete the entire table, a WHERE clause is generated with the primary and foreign keys.
The function also takes an optional parameter whereArgs which can be used to specify additional conditions for the WHERE clause.
Returns a Future that completes with the number of deleted records.
Implementation
@Deprecated(
'This method is deprecated and will be deleted on the next release. Please use `deleteSingle` or `batchDelete` instead.')
Future<int> delete(dynamic obj,
{bool all = true, Map<String, String> whereArgs = const {'': ''}}) async {
final db = await DBProvider.db.database;
String sql = 'DELETE FROM ${obj.runtimeType}';
/* If we're not deleting the entire table, retrieve the primary and foreign keys. */
if (!all) {
InstanceMirror reflectNew = reflector.reflect(obj);
List<String> primary =
reflectNew.type.invokeGetter("primary") as List<String>;
/* Construct the WHERE clause. */
String where = ' WHERE ';
// Primary keys
for (var x in primary) {
if (x == primary.last) {
where = "$where $x = '${reflectNew.invokeGetter(x)}'";
} else {
where = "$where $x = '${reflectNew.invokeGetter(x)}' and";
}
}
sql = '$sql$where';
}
if (whereArgs.keys.first != '') {
Iterable<String> campos = whereArgs.keys;
String where = ' WHERE ';
for (var x in campos) {
if (x == campos.last) {
where = "$where $x = '${whereArgs[x]}'";
} else {
where = "$where $x = '${whereArgs[x]}' and";
}
}
sql = '$sql$where';
}
final res = await db!.rawDelete(sql);
PrintHandler.warningLogger.e(
'sqflite_simple_dao_backend: You just deleted $res items from ${obj.runtimeType}.⚠️');
return res;
}