select method
A select query
Implementation
Future<List<Map<String, dynamic>>?> select(
{required String? table,
String columns = "*",
String? where,
String? orderBy,
int? limit,
int? offset,
String? groupBy,
bool verbose = false}) async {
/// [table] the table to select from
/// [columns] the columns to return
/// [where] the sql where clause
/// [orderBy] the sql order_by clause
/// [limit] the sql limit clause
/// [offset] the sql offset clause
/// [verbose] print the query
/// returns the selected data
try {
if (!_isReady) {
throw DatabaseNotReady();
}
final timer = Stopwatch()..start();
var q = "SELECT $columns FROM $table";
if (where != null) {
q += " WHERE $where";
}
if (groupBy != null) {
q += " GROUP BY $groupBy";
}
if (orderBy != null) {
q += " ORDER BY $orderBy";
}
if (limit != null) {
q += " LIMIT $limit";
}
if (offset != null) {
q += " OFFSET $offset";
}
List<Map<String, dynamic>>? res;
await _db!.transaction((txn) async {
res = await txn.rawQuery(q);
}).catchError((dynamic e) =>
throw ReadQueryException("Can not select from table $table $e"));
timer.stop();
if (verbose) {
final msg = "$q in ${timer.elapsedMilliseconds} ms";
print(msg);
}
return res;
} catch (e) {
rethrow;
}
}