db.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'package:flutter/foundation.dart';
  2. import 'package:vital_local_database/index.dart';
  3. import 'package:vitalapp/database/db_patch/v1.dart';
  4. import 'package:vitalapp/database/entities/diagnosis.dart';
  5. import 'package:vitalapp/database/entities/patient.dart';
  6. import 'package:vitalapp/database/repositories/followup.dart';
  7. import 'package:vitalapp/database/repositories/interfaces/followup.dart';
  8. import 'db_patch/v2.dart';
  9. import 'repositories/interfaces/diagnosis.dart';
  10. import 'repositories/interfaces/exam.dart';
  11. import 'repositories/interfaces/exam_batch.dart';
  12. import 'repositories/interfaces/patient.dart';
  13. import 'repositories/diagnosis.dart';
  14. import 'repositories/patient.dart';
  15. import 'package:fis_common/logger/logger.dart';
  16. /// vital 本地数据库对象
  17. final db = VitalDatabaseAccessor();
  18. /// vital本地数据库访问器
  19. class VitalDatabaseAccessor
  20. extends BaseDbAccessor<VitalDatabaseRepositoryStore> {
  21. final _store = VitalDatabaseRepositoryStore();
  22. final _connection = SqliteConnection(path: "vital.db");
  23. late IDatabase _database;
  24. @override
  25. IDatabase get database => _database;
  26. @override
  27. VitalDatabaseRepositoryStore get repositories => _store;
  28. /// 初始化
  29. Future<void> init() async {
  30. _database = await _connection.open();
  31. if (kDebugMode) {
  32. await _debugBatch();
  33. }
  34. try {
  35. await checkAndUpgradeDatabaseVersion();
  36. } catch (e) {
  37. logger.e(
  38. "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion error.", e);
  39. }
  40. _store.regRepository<IPatientRepository>(PatientRepository(database));
  41. _store.regRepository<IDiagnosisRepository>(DiagnosisRepository(database));
  42. _store.regRepository<IFollowUpRepository>(FollowUpRepository(database));
  43. }
  44. Future<void> _debugBatch() async {
  45. // TODO: 此处仅限开发调试时临时执行,提交前请清除!!!!
  46. // database.execute("DROP TABLE ${PatientEntity().tableName};");
  47. // database.execute("DROP TABLE ${DiagnosisEntity().tableName};");
  48. // database.execute("DROP TABLE ${FollowUpEntity().tableName};");
  49. // database.execute("DROP TABLE ${ExamBatchEntity().tableName};");
  50. // database.execute("DROP TABLE ${ExamEntity().tableName};");
  51. database.execute("DELETE FROM diagnosis WHERE 1=1;");
  52. database.execute("DELETE FROM followup WHERE 1=1;");
  53. database.execute("DELETE FROM patients WHERE 1=1;");
  54. }
  55. ///检查本地数据库版本,以判断是否需要升级
  56. Future<void> checkAndUpgradeDatabaseVersion() async {
  57. var originalVersion = await database.getVersion();
  58. logger.w(
  59. "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion originalVersion:$originalVersion.");
  60. if (originalVersion == 0) {
  61. // 数据库首次创建,V0
  62. await database.execute(PatientEntity.TABLE_CREATE_SQL);
  63. await database.execute(DiagnosisEntity.TABLE_CREATE_SQL);
  64. }
  65. int version = originalVersion;
  66. version = await DatabasePatchV1(database).perform(version);
  67. version = await DatabasePatchV2(database).perform(version);
  68. if (version != originalVersion) {
  69. // 数据库升级
  70. await db.database.setVersion(version);
  71. // 重启DB
  72. await _connection.close();
  73. _database = await _connection.open();
  74. }
  75. logger.w(
  76. "VitalDatabaseAccessor checkAndUpgradeDatabaseVersion version:$version.");
  77. }
  78. void dispose() {
  79. _connection.close();
  80. }
  81. }
  82. class VitalDatabaseRepositoryStore extends BaseDbAccessorRepositoryStore {
  83. /// 居民仓储
  84. IPatientRepository get patient => findRepository<IPatientRepository>()!;
  85. /// 检测记录仓储
  86. IDiagnosisRepository get diagnosis => findRepository<IDiagnosisRepository>()!;
  87. /// 随访仓储
  88. IFollowUpRepository get followUp => findRepository<IFollowUpRepository>()!;
  89. /// 体检仓储
  90. IExamRepository get exam => findRepository<IExamRepository>()!;
  91. /// 体检批次仓储
  92. IExamBatchRepository get examBatch => findRepository<IExamBatchRepository>()!;
  93. }