open method

FutureOr<Database?> open()

Implementation

FutureOr<Database?> open() async {
  if (_database != null) {
    return _database;
  }
  if (openingCompleter != null) {
    return await openingCompleter!.future;
  }
  openingCompleter = Completer();
  Database? db;
  try {
    name ??= "database.db";

    /// For windows & linux
    if (Platform.isWindows || Platform.isLinux) {
      // example/.dart_tool/sqflite_common_ffi/databases/~/Downloads/database.db
      path ??= "~/Downloads/";
    }

    path ??= "${await getDatabasesPath()}/$name";
    if (path?.endsWith("/") ?? false) {
      path = "$path$name";
    }
    String dbPath = path!;
    BoxerLogger.i(null, "[BoxerDatabase] - opening database, local path is: $dbPath");
    assert(() {
      () async {
        File file = File(dbPath);
        FileStat stat = await file.stat();
        print('DB file absolute path is: ${file.absolute.path}');
        print('DB file mode: ${stat.mode.toRadixString(16)}, ${stat.modeString()}');
      }();
      return true;
    }());

    /// will sync call onConfigure -> onCreate -> onUpgrade -> onDowngrade -> onOpen if needed
    db = await openDB(path: dbPath, version: version);
    setDatabase(db);
    openingCompleter?.complete(db);
    BoxerLogger.i(null, "[BoxerDatabase] - open database done");
  } catch (e, s) {
    openingCompleter?.complete(null);
    BoxerLogger.f(null, "[BoxerDatabase] - open database error: $e, $s");
    BoxerLogger.reportFatal(e, s);
  }
  // so far, the same instance of properties [_database]
  return db;
}