execute method
Executes a raw SQL query with optional parameter bindings.
Example:
await connection.execute('SELECT * FROM users WHERE id = ?', [1]);
Implementation
@override
Future<DatabaseResponse> execute(
String sql, [
List<dynamic> bindings = const [],
]) async {
if (_connection == null) {
await connect();
}
try {
return await _executeInternal(sql, bindings);
} catch (e) {
// Check if it's a connection error and retry once
if (_isConnectionError(e)) {
Log.warning('Database connection lost. Reconnecting...');
try {
await disconnect();
await connect();
return await _executeInternal(sql, bindings);
} catch (reconnectError) {
throw DatabaseException(
'Failed to reconnect and execute query: $reconnectError',
details: e,
sql: sql,
bindings: bindings,
);
}
}
Log.info('SQL Error: $e\nQuery: $sql\nBindings: $bindings');
throw DatabaseException(
'SQL Execution Error: $e',
details: e,
sql: sql,
bindings: bindings,
);
}
}