openDuckDB method

DynamicLibrary openDuckDB()

Opens the DynamicLibrary from which duckdb.dart is going to DynamicLibrary.lookup duckdb's methods that will be used. This method is meant to be called by duckdb.dart only.

Implementation

DynamicLibrary openDuckDB() {
  /// If the process already provides has the symbol, we can use it directly.
  /// This is useful when we have loaded the library and are now on an isolate.
  final process = DynamicLibrary.process();
  if (process.providesSymbol('duckdb_open')) {
    return process;
  }

  /// If we are in a test environment, we will load the library from the package.
  if (_isTestEnvironment()) {
    final prefix = '${Directory.current.path}/../duckdb';

    if (_overriddenPlatforms[OperatingSystem.windows] == null) {
      _overriddenPlatforms[OperatingSystem.windows] =
          () => DynamicLibrary.open(
                '$prefix/windows/Libraries/release/duckdb.dll',
              );
    }

    if (_overriddenPlatforms[OperatingSystem.macOS] == null) {
      _overriddenPlatforms[OperatingSystem.macOS] = () => DynamicLibrary.open(
            '$prefix/macos/Libraries/release/libduckdb.dylib',
          );
    }

    if (_overriddenPlatforms[OperatingSystem.linux] == null) {
      _overriddenPlatforms[OperatingSystem.linux] = () => DynamicLibrary.open(
            '$prefix/linux/Libraries/release/libduckdb.so',
          );
    }
  }

  try {
    final forPlatform = _overriddenPlatforms[os];
    if (forPlatform != null) {
      return forPlatform();
    }
  } catch (error) {
    // ignore
  }

  return _defaultOpen();
}