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 (isTestEnvironment()) {
    final prefix = '${Directory.current.path}/../duckdb.dart';

    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',
          );
    }
  }

  try {
    final forAll = _overriddenForAll;
    if (forAll != null) {
      return forAll();
    }

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

  return _defaultOpen();
}