loadOdbcLibrary function

DynamicLibrary loadOdbcLibrary()

Returns the loaded DynamicLibrary instance.

Resolution order (aligned with hook/build.dart / doc/BUILD.md):

  1. Preferred on-disk file — local release vs ~/.cache/odbc_fast/... using ODBC_FAST_PREFER_LOCAL_BUILD and mtime (same policy as the hook)
  2. Native Assets — package:odbc_fast/<lib> (hook-registered asset)
  3. System library paths — PATH / LD_LIBRARY_PATH

Implementation

DynamicLibrary loadOdbcLibrary() {
  final name = _libraryName();
  final packageRoot = _findPackageRoot();
  final preferred = resolvePreferredOdbcEngineFilePath(
    cwd: Directory.current.path,
    packageRoot: packageRoot,
  );
  if (preferred != null) {
    return DynamicLibrary.open(preferred);
  }

  // Native Assets (production) - package:odbc_fast/
  try {
    return DynamicLibrary.open('package:odbc_fast/$name');
  } on Object catch (_) {
    // Native Assets not available, continue to next option
  }

  // System - PATH/LD_LIBRARY_PATH
  try {
    return DynamicLibrary.open(name);
  } catch (e) {
    throw StateError(
      'ODBC engine library not found.\n\n'
      'Options:\n'
      '1. For development: Build Rust library first\n'
      '   cd native && cargo build --release\n\n'
      '2. Automatic download: Run "dart pub get" again\n'
      '   (Binary will be downloaded from GitHub Releases)\n\n'
      '3. Manual download: Get binary from GitHub Releases\n'
      '   https://github.com/cesar-carlos/dart_odbc_fast/releases\n\n'
      'Error: $e',
    );
  }
}