12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import 'package:vital_local_database/index.dart';
- import 'package:vitalapp/database/db_patch/v1.dart';
- import 'package:vitalapp/database/entities/diagnosis.dart';
- import 'package:vitalapp/database/entities/followup.dart';
- import 'package:vitalapp/database/entities/patient.dart';
- import 'package:vitalapp/database/repositories/followup.dart';
- import 'package:vitalapp/database/repositories/interfaces/followup.dart';
- import 'db_patch/v2.dart';
- import 'repositories/interfaces/diagnosis.dart';
- import 'repositories/interfaces/exam.dart';
- import 'repositories/interfaces/exam_batch.dart';
- import 'repositories/interfaces/patient.dart';
- import 'repositories/diagnosis.dart';
- import 'repositories/patient.dart';
- import 'package:fis_common/logger/logger.dart';
- /// vital 本地数据库对象
- final db = VitalDatabaseAccessor();
- /// vital本地数据库访问器
- class VitalDatabaseAccessor
- extends BaseDbAccessor<VitalDatabaseRepositoryStore> {
- final _store = VitalDatabaseRepositoryStore();
- final _connection = SqliteConnection(path: "vital.db");
- late IDatabase _database;
- @override
- IDatabase get database => _database;
- @override
- VitalDatabaseRepositoryStore get repositories => _store;
- /// 初始化
- Future<void> init() async {
- _database = await _connection.open();
- await checkAndUpgradeDatabaseVersion();
- _store.regRepository<IPatientRepository>(PatientRepository(database));
- _store.regRepository<IDiagnosisRepository>(DiagnosisRepository(database));
- _store.regRepository<IFollowUpRepository>(FollowUpRepository(database));
- }
- ///检查本地数据库版本,以判断是否需要升级
- Future<void> checkAndUpgradeDatabaseVersion() async {
- var originalVersion = await database.getVersion();
- logger.w(
- "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion originalVersion:$originalVersion.");
- if (originalVersion == 0) {
- // 数据库首次创建,V0
- await database.execute(PatientEntity.TABLE_CREATE_SQL);
- await database.execute(DiagnosisEntity.TABLE_CREATE_SQL);
- }
- int version = originalVersion;
- version = await DatabasePatchV1(database).perform(version);
- version = await DatabasePatchV2(database).perform(version);
- if (version != originalVersion) {
- // 数据库升级
- await db.database.setVersion(version);
- // 重启DB
- await _connection.close();
- _database = await _connection.open();
- }
- logger.w(
- "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion version:$version.");
- }
- void dispose() {
- _connection.close();
- }
- }
- class VitalDatabaseRepositoryStore extends BaseDbAccessorRepositoryStore {
- /// 居民仓储
- IPatientRepository get patient => findRepository<IPatientRepository>()!;
- /// 检测记录仓储
- IDiagnosisRepository get diagnosis => findRepository<IDiagnosisRepository>()!;
- /// 随访仓储
- IFollowUpRepository get followUp => findRepository<IFollowUpRepository>()!;
- /// 体检仓储
- IExamRepository get exam => findRepository<IExamRepository>()!;
- /// 体检批次仓储
- IExamBatchRepository get examBatch => findRepository<IExamBatchRepository>()!;
- }
|