updateReg method
- @Deprecated('This method is deprecated and will be deleted on the next release. Please use `updateSingle` or `batchUpdate` instead.')
- dynamic newReg
This function updates a record in the database with the changes specified in newReg.
The function takes an object newReg that contains the modified data. It retrieves the primary and foreign keys from newReg and uses them to find the corresponding record in the database.
The function then compares the old record with newReg, identifies the changes, and constructs an SQL query that updates the record with these changes.
Note: Boolean fields should be added to the if condition to prevent errors.
Returns a Future that completes with the number of updated records.
Implementation
@Deprecated(
'This method is deprecated and will be deleted on the next release. Please use `updateSingle` or `batchUpdate` instead.')
Future<int> updateReg(dynamic newReg) async {
final db = await DBProvider.db.database;
Map<String, String> changes = {};
List<String> auxPr = [];
String sql = '';
/* Retrieve the primary keys of the table. */
InstanceMirror reflectNew = reflector.reflect(newReg);
var classMirror = reflector.reflectType(newReg) as ClassMirror;
List<String> primary =
reflectNew.type.invokeGetter("primary") as List<String>;
/* Retrieve all the field names. */
Iterable<String> names =
reflectNew.type.invokeGetter("names") as Iterable<String>;
/* Retrieve the fields for comparison. */
Map<String, String> fields =
classMirror.invokeGetter("fields") as Map<String, String>;
/* Retrieve the record to be updated. */
for (var x in primary) {
auxPr.add(reflectNew.invokeGetter(x).toString());
}
List oldReg = await getReg(newReg, primaryKeys: auxPr);
InstanceMirror reflect = reflector.reflect(oldReg.first);
/* Compare the old record with [newReg] and identify the changes. */
for (var x in names) {
if (reflectNew.invokeGetter(x) != reflect.invokeGetter(x)) {
if (fields[x]!.toLowerCase().contains('bool')) {
changes.addAll({x: "${reflectNew.invokeGetter(x) == true ? 1 : 0}"});
} else if (fields[x]!.toLowerCase().contains('date')) {
changes
.addAll({x: reflectNew.invokeGetter(x).toString().split(' ')[0]});
} else {
changes.addAll({x: "${reflectNew.invokeGetter(x)}"});
}
}
}
/* If there are no changes, exit the function. */
if (changes.isEmpty) {
return 0;
}
/* Construct the SQL query. */
String start = "UPDATE ${newReg.runtimeType} SET";
String changesStr = '';
for (var x in changes.keys) {
if (x != changes.keys.last) {
changesStr =
'$changesStr $x = ${changes[x] == 'true' ? 1 : changes[x] == 'false' ? 0 : changes[x]},';
} else {
changesStr =
'$changesStr $x = ${changes[x] == 'true' ? 1 : changes[x] == 'false' ? 0 : changes[x]}';
}
}
String finalStr = '';
/* Add the primary keys to the SQL query. */
for (var x in primary) {
if (x == primary.first) {
if (fields[x]!.toLowerCase().contains('date')) {
finalStr =
' WHERE $x = "${reflectNew.invokeGetter(x).toString().split(' ')[0]}"';
} else {
finalStr = ' WHERE $x = "${reflectNew.invokeGetter(x)}"';
}
} else {
if (fields[x]!.toLowerCase().contains('date')) {
finalStr =
'$finalStr and $x = "${reflectNew.invokeGetter(x).toString().split(' ')[0]}"';
} else {
finalStr = '$finalStr and $x = "${reflectNew.invokeGetter(x)}"';
}
}
}
sql = '$start $changesStr $finalStr';
final res = db!.rawUpdate(sql);
PrintHandler.warningLogger.w(
'sqflite_simple_dao_backend: You just updated $res items to ${newReg.runtimeType}.📖');
return res;
}