import 'package:drift/drift.dart'; import 'package:uto_manager/infrastructure/database/app_database.dart'; part 'unit_dao.g.dart'; /// Data Access Object for [Units] table operations. @DriftAccessor(tables: [Units]) class UnitDao extends DatabaseAccessor with _$UnitDaoMixin { UnitDao(super.db); /// Returns all unit rows. Future> getAllUnits() => select(units).get(); /// Returns the unit with [unitId], or null if not found. Future getUnitById(String unitId) => (select(units)..where((t) => t.unitId.equals(unitId))).getSingleOrNull(); /// Returns all direct children of [parentUnitId]. Future> getChildren(String parentUnitId) => (select(units)..where((t) => t.parentUnitId.equals(parentUnitId))).get(); /// Returns all root units (no parent). Future> getRoots() => (select(units)..where((t) => t.parentUnitId.isNull())).get(); /// Returns all units at the given [echelon] string value. Future> getByEchelon(String echelon) => (select(units)..where((t) => t.echelon.equals(echelon))).get(); /// Inserts a new unit row. Throws on duplicate primary key. Future insertUnit(UnitsCompanion entry) => into(units).insert(entry, mode: InsertMode.insertOrFail); /// Inserts multiple unit rows in a single transaction. Future insertAllUnits(List entries) => batch((b) => b.insertAll(units, entries, mode: InsertMode.insertOrFail)); /// Updates an existing unit row. Future updateUnit(UnitsCompanion entry) => update(units).replace(entry); /// Deletes the unit with [unitId]. Future deleteUnit(String unitId) => (delete(units)..where((t) => t.unitId.equals(unitId))).go(); /// Deletes all units. Future deleteAllUnits() => delete(units).go(); /// Returns a stream that emits all units whenever the table changes. Stream> watchAllUnits() => select(units).watch(); /// Returns a stream that emits children of [parentUnitId] on changes. Stream> watchChildren(String parentUnitId) => (select(units)..where((t) => t.parentUnitId.equals(parentUnitId))).watch(); }