connect method

Future<void> connect()

Connects to the SQLite database

Implementation

Future<void> connect() async {
  try {
    if (_connectionString.isEmpty) {
      throw DatabaseConnectionError('Connection string cannot be empty');
    }

    final uri = Uri.parse(_connectionString);
    if (uri.scheme != 'sqlite') {
      throw DatabaseConnectionError(
          'Invalid SQLite connection string: $_connectionString');
    }

    // Extract path from URI
    // For sqlite://example/data/file.db, the host is 'example' and path is '/data/file.db'
    // We need to reconstruct the full path
    String path;

    if (uri.host.isNotEmpty) {
      // URI like sqlite://example/data/file.db
      // host='example', path='/data/file.db'
      path = uri.host + uri.path;
    } else {
      // URI like sqlite:///absolute/path or sqlite://C:/path
      path = uri.path;
      if (path.startsWith('/') && !path.startsWith('//')) {
        // Check if it's a Windows absolute path like /C:/
        if (path.length > 3 && path[2] == ':') {
          path = path.substring(1); // Remove leading slash for C:/path
        } else {
          // It's a Unix absolute path, keep as is
        }
      }
    }

    _db = sqlite.sqlite3.open(path);
    _isConnected = true;
  } catch (e) {
    throw DatabaseConnectionError('Failed to connect to SQLite: $e');
  }
}