toListWhereValuesAre method

Future<List<ISQLiteItem>> toListWhereValuesAre(
  1. ISQLiteItem item,
  2. String columnName,
  3. List<String> columnValueList
)

Fetches multiple items from the database based on a specific column and a list of values.

This function uses a batch query to efficiently retrieve multiple items.

@param item The ISQLiteItem instance representing the type of items to fetch. @param columnName The name of the column to filter by. @param columnValueList A list of values to filter the query by. @return A list of ISQLiteItem objects that match the query criteria.

Implementation

Future<List<ISQLiteItem>> toListWhereValuesAre(
  ISQLiteItem item,
  String columnName,
  List<String> columnValueList,
) async {
  final db = await getOpenDatabase();
  final batch = db.batch();
  final resultList = <ISQLiteItem>[];
  try {
    // Add queries to the batch for each column value
    for (final columnValue in columnValueList) {
      batch.query(
        item.getTableName(),
        where: '$columnName = ?',
        whereArgs: [columnValue],
      );
    }

    // Commit batch and process results
    final rawResults = await batch.commit();

    // rawResults is a list of lists, where each sublist is the result of a query
    for (final queryResult in rawResults) {
      if (queryResult is List) {
        for (final result in queryResult) {
          final liteItem = item.fromMap(result);
          resultList.add(liteItem);
        }
      }
    }
    return resultList;
  } catch (error) {
    // Handle database error (log or rethrow)
    return [];
  }
}