search method
Search rows.
var userEloquent = UserEloquent();
// get rows where any column has word 'j'.
userEloquent.search('j');
// get rows where country has 'UK' and any other rows has 'j'.
userEloquent.where('country','UK').search('j');
//specify searchable columns
userEloquent.search('j',searchableColumns:['name']);
Implementation
@override
Future<List<Map<String, Object?>>> search(String keyword,
{List<String>? searchableColumns}) async {
String _key = '%$keyword%';
String q = 'Select';
try {
List<String>? _usedColumns;
var _wheres = getWhereColumns();
if (_wheres.isNotEmpty) {
_usedColumns = _wheres.map((e) => e.columnName).toList();
}
if (searchableColumns != null && searchableColumns.isNotEmpty) {
for (var column in searchableColumns) {
where(column, _key, operator: Operator.like, conjuncation: 'or');
}
} else {
for (var column in columns) {
if (_usedColumns != null && _usedColumns.contains(column)) {
continue;
}
where(column, _key, operator: Operator.like, conjuncation: 'or');
}
}
q += generateQuery(getSelectedColumns() ?? ' * from $tableName');
resetAll();
Database _db = await getDatabase;
return await _db.rawQuery(q);
} catch (e) {
throw Exception('Generated query: "$q" \n' + e.toString());
}
}