connect method
Implementation
Future<void> connect() async {
try {
if (_connectionString.isEmpty) {
throw DatabaseConnectionError('Connection string cannot be empty');
}
final uri = Uri.parse(_connectionString);
if (uri.scheme != 'mysql') {
throw DatabaseConnectionError(
'Invalid MySQL connection string: $_connectionString');
}
// Parse connection details
final userInfo = uri.userInfo.split(':');
final username = userInfo.isNotEmpty ? userInfo[0] : 'root';
final password = userInfo.length > 1 ? userInfo[1] : '';
final database =
uri.pathSegments.isNotEmpty ? uri.pathSegments.first : 'mysql';
_connection = await mysql.MySQLConnection.createConnection(
host: uri.host.isEmpty ? 'localhost' : uri.host,
port: uri.port == 0 ? 3306 : uri.port,
userName: username,
password: password,
databaseName: database,
);
await _connection!.connect();
_isConnected = true;
} catch (e) {
throw DatabaseConnectionError('Failed to connect to MySQL: $e');
}
}