fetchQueryRecords method
Implementation
Future<Map<String,dynamic>> fetchQueryRecords(
{String query = "",
String fetchMode = "LIST",
Map<String, List<String>> formatColumns = const {}}) async {
Map<String,dynamic> result = Simplify.getDefaultResult();
try {
if (mySqlConnection != null) {
if (fetchMode == "COUNT") {
query = "SELECT COUNT(*) AS records_count FROM (" +
query +
") AS SELECT_QUERY;";
}
if (logQuery) {
_debug(query);
}
Results? results = await mySqlConnection?.query(query);
List<Map<String,dynamic>> records = List.empty(growable: true);
results?.forEach((orgRecord) {
Map<String,dynamic >record = Map.from(orgRecord as Map);
formatColumns.forEach((fieldName, fieldFormats) {
if (record.containsKey(fieldName)) {
if (fieldFormats.contains(SimpleDatabase.formatEncrypt)) {
record[fieldName] =
EncryptionHelper.decrypt(record[fieldName].toString());
}
if (fieldFormats.contains(SimpleDatabase.formatJson)) {
if (record[fieldName] != null && record[fieldName] != "null") {
if (record[fieldName].toString().isEmpty) {
record[fieldName] = null;
} else {
try {
record[fieldName] =
jsonDecode(record[fieldName].toString());
} catch (ex, stack) {
Simplify.debug(
"Cannot Format Value ${record[fieldName]}");
}
}
}
}
}
});
records.add(Map.from(record));
});
result["message"] = "query executed successfully";
result["query"] = query;
result["records"] = records;
if (fetchMode == "COUNT") {
result["records"] = records.first["records_count"];
}
result["status"] = "success";
}
} catch (ex, stack) {
result["query"] = query;
result["message"] = Simplify.getExceptionMessage(ex, stack: stack);
}
return result;
}