123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import 'package:flutter/foundation.dart';
- 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/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();
- if (kDebugMode) {
- await _debugBatch();
- }
- try {
- await checkAndUpgradeDatabaseVersion();
- } catch (e) {
- logger.e(
- "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion error.", e);
- }
- _store.regRepository<IPatientRepository>(PatientRepository(database));
- _store.regRepository<IDiagnosisRepository>(DiagnosisRepository(database));
- _store.regRepository<IFollowUpRepository>(FollowUpRepository(database));
- }
- Future<void> _debugBatch() async {
- // TODO: 此处仅限开发调试时临时执行,提交前请清除!!!!
- // database.execute("DROP TABLE ${PatientEntity().tableName};");
- // database.execute("DROP TABLE ${DiagnosisEntity().tableName};");
- // database.execute("DROP TABLE ${FollowUpEntity().tableName};");
- // database.execute("DROP TABLE ${ExamBatchEntity().tableName};");
- // database.execute("DROP TABLE ${ExamEntity().tableName};");
- database.execute("DELETE FROM diagnosis WHERE 1=1;");
- database.execute("DELETE FROM followup WHERE 1=1;");
- database.execute("DELETE FROM patients WHERE 1=1;");
- }
- ///检查本地数据库版本,以判断是否需要升级
- 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>()!;
- }
|