insert method

  1. @override
Future insert(
  1. String table,
  2. Map<String, dynamic> values
)
override

Inserts a record and returns the auto-generated primary key (if applicable).

Implementation

@override
Future<dynamic> insert(String table, Map<String, dynamic> values) async {
  // Manually reconstructs the SQL string to ensure the repository logic
  // produced the correct keys and structure for logging purposes.
  final keys = values.keys.map(grammar.wrap).join(', ');
  final placeholders = List.filled(values.length, '?').join(', ');
  final sql = 'INSERT INTO ${grammar.wrap(table)} ($keys) VALUES ($placeholders)';

  lastSql = sql;
  lastArgs = values.values.toList();
  history.add(sql);

  if (_inTransaction) {
    transactionHistory.add(sql);

    if (shouldFailTransaction) {
      throw Exception('Simulated transaction failure');
    }
  }

  // Check if a specific ID return is mocked, otherwise default to 1.
  if (_smartResponses.containsKey('last_insert_row_id')) {
    final row = _smartResponses['last_insert_row_id']!.first;
    return row['id'] ?? 1;
  }

  return 1;
}