connect method
Implementation
Future<void> connect() async {
try {
if (_connectionString.isEmpty) {
throw DatabaseConnectionError('Connection string cannot be empty');
}
final uri = Uri.parse(_connectionString);
if (!['postgresql', 'postgres'].contains(uri.scheme)) {
throw DatabaseConnectionError(
'Invalid PostgreSQL connection string: $_connectionString');
}
// Parse connection details
final userInfo = uri.userInfo.split(':');
final username = userInfo.isNotEmpty ? userInfo[0] : 'postgres';
final password = userInfo.length > 1 ? userInfo[1] : '';
final database =
uri.pathSegments.isNotEmpty ? uri.pathSegments.first : 'postgres';
_connection = await pg.Connection.open(
pg.Endpoint(
host: uri.host.isEmpty ? 'localhost' : uri.host,
port: uri.port == 0 ? 5432 : uri.port,
database: database,
username: username,
password: password,
),
settings: pg.ConnectionSettings(
sslMode: pg.SslMode.disable,
),
);
_isConnected = true;
} catch (e) {
throw DatabaseConnectionError('Failed to connect to PostgreSQL: $e');
}
}