whereMapMatch method

Future<ISQLiteItem?> whereMapMatch(
  1. ISQLiteItem item,
  2. Map<String, dynamic> columnValues
)

Implementation

Future<ISQLiteItem?> whereMapMatch(
  ISQLiteItem item,
  Map<String, dynamic> columnValues,
) async {
  var db = await getOpenDatabase();
  String tableName = item.getTableName();

  // Check if the columnValues map is empty
  if (columnValues.isEmpty) {
    return null;
  }

  // Build the WHERE clause dynamically based on the provided column names
  List<String> whereConditions = [];
  List<dynamic> whereArgs = [];

  columnValues.forEach((columnName, value) {
    whereConditions.add('$columnName = ?');
    whereArgs.add(value);
  });

  String condition = whereConditions.join(' AND ');

  try {
    // Query the database
    var maps = await db.query(
      tableName,
      where: condition,
      whereArgs: whereArgs,
      limit: 1,
    );
    if (maps.isNotEmpty) {
      return item.fromMap(maps.first); // Return the first matching item
    } else {
      return null; // Return null if no match found
    }
  } catch (e) {
    // Handle any database errors that may occur
    //print('Error querying database: $e');
  }

  return null;
}