where<T extends Model> static method
Filters records with comparison.
Supported: ==, !=, >, <, >=, <=
Implementation
static Future<List<T>> where<T extends Model>({required String field, String comp = "==", required dynamic value}) async {
try {
final constructor = _jsonConstructors[T];
if (constructor == null) return [];
const allowedOps = ['=', '==', '!=', '<', '>', '<=', '>=', 'LIKE'];
if (!allowedOps.contains(comp.toUpperCase()) && comp != "==") {
// Todo- Look out for quirks / inconsistent results before changing
// "==" is usually handled as "=" in SQL or logic
// Adjust logic if "==" is strictly Dart side or SQL side. SQLite uses "=".
if (comp == "==") {
comp = "=";
} else {
throw ArgumentError("Invalid SQL operator: $comp");
}
} else if (comp == "==") {
comp = "=";
}
final maps = await _database.query(_getTableName<T>(), where: '$field $comp ?', whereArgs: [value]);
return maps.map((map) => constructor(map) as T).toList();
} catch (e) {
print('SQLite where error: $e');
return [];
}
}