select method

Future<List<Map<String, dynamic>>> select(
  1. {@required String table,
  2. String columns = "*",
  3. String where,
  4. String orderBy,
  5. int limit,
  6. int offset,
  7. String groupBy,
  8. bool verbose = false}
)

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;
  }
}