insertMap method
Insert a new record using a map values
into a given table
.
This returns the id of the inserted row.
Implementation
@override
Future<int?> insertMap(TableName table, Map<String, dynamic> values) async {
List<dynamic> args = [];
var keys;
var questions;
values.forEach((key, value) {
if (keys == null) {
keys = key;
questions = "?";
} else {
keys = keys + "," + key;
questions = questions + ",?";
}
args.add(value);
});
var sql =
"insert into ${table.fixedDoubleName} ( $keys ) values ( $questions );";
await execute(sql, arguments: args, getLastInsertId: false);
var pkName = await getPrimaryKey(table);
var maxPk = 0;
if (values.containsKey(pkName)) {
sql = "select max($pkName) from ${table.fixedDoubleName}";
var queryResult = await select(sql);
maxPk = queryResult?.first.getAt(0);
}
return maxPk;
}