BaseArchitectural.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import 'package:flutter/material.dart';
  2. import 'package:sqflite/sqflite.dart';
  3. import 'SqfliteDataBase.dart';
  4. abstract class BaseProvider extends ChangeNotifier {
  5. BaseProvider(BuildContext context) {
  6. init(context);
  7. }
  8. /// 初始化方法,子类覆写
  9. void init(BuildContext context) {}
  10. }
  11. abstract class BaseModel extends ChangeNotifier {
  12. BaseModel(BuildContext context) {
  13. init(context);
  14. asyncInit(context);
  15. }
  16. void init(BuildContext context) {}
  17. Future<void> asyncInit(BuildContext context) async {}
  18. void refresh() {
  19. notifyListeners();
  20. }
  21. }
  22. /* demo mapper
  23. class {ENTITY_NAME}Mapper extends CommonMapper<{ENTITY_NAME}> {
  24. {ENTITY_NAME}Mapper._() : super({ENTITY_NAME}());
  25. static {ENTITY_NAME}Mapper _instance = {ENTITY_NAME}Mapper._();
  26. factory {ENTITY_NAME}Mapper() {
  27. return _instance;
  28. }
  29. }
  30. */
  31. abstract class CommonMapper<T extends dynamic> {
  32. late dynamic _currentTypeInstance;
  33. CommonMapper(dynamic entityI) {
  34. _currentTypeInstance = entityI;
  35. }
  36. // Batch batch;
  37. //
  38. // void startBatch() {
  39. // if (batch == null) {
  40. // batch = SqfliteDataBase.getInstance().batch();
  41. // }
  42. // else {
  43. // throw "already started a batch";
  44. // }
  45. // }
  46. //
  47. // Future<void> commitBatch() async {
  48. // if (batch == null) {
  49. // throw "there is no batch exist";
  50. // }
  51. // else {
  52. // List list = await batch.commit();
  53. // batch = null;
  54. // debugPrint(list.toString());
  55. // }
  56. // }
  57. // C
  58. Future<bool> insert(T entity) async {
  59. try {
  60. Database database = SqfliteDataBase.getInstance()!;
  61. await database.insert(_currentTypeInstance.tableName, entity.value);
  62. debugPrint("[CommonMapper] insert: ${entity.toString()}");
  63. return true;
  64. } catch (e) {
  65. debugPrint("[CommonMapper] insert fail ${e.toString()}");
  66. return false;
  67. }
  68. }
  69. // R
  70. Future<List<T>?> selectAll({int? limit, String? orderBy}) async {
  71. try {
  72. Database database = SqfliteDataBase.getInstance()!;
  73. List<Map<String, dynamic>> dbResult = await database.query(
  74. _currentTypeInstance.tableName,
  75. limit: limit,
  76. orderBy: orderBy,
  77. );
  78. debugPrint(
  79. "[CommonMapper] selectAll result: ${dbResult.length.toString()}");
  80. return _currentTypeInstance.resultAsList(dbResult);
  81. } catch (e) {
  82. debugPrint("[CommonMapper] selectAll fail ${e.toString()}");
  83. return null;
  84. }
  85. }
  86. Future<List<T>?> selectWhere(String where,
  87. {int? limit, String? orderBy}) async {
  88. try {
  89. Database database = SqfliteDataBase.getInstance()!;
  90. List<Map<String, dynamic>> dbResult = await database.query(
  91. _currentTypeInstance.tableName,
  92. limit: limit,
  93. orderBy: orderBy,
  94. where: where,
  95. );
  96. debugPrint("[CommonMapper] selectWhere result: ${dbResult.length.toString()}");
  97. return _currentTypeInstance.resultAsList(dbResult);
  98. } catch (e) {
  99. debugPrint("[CommonMapper] selectWhere fail ${e.toString()}");
  100. return null;
  101. }
  102. }
  103. Future<List<T>?> selectMatchBy(dynamic entity,
  104. {int? limit, String? orderBy}) async {
  105. try {
  106. Database database = SqfliteDataBase.getInstance()!;
  107. String matches = "";
  108. entity.value.forEach((key, value) {
  109. if (value != null) {
  110. matches += "$key = '${value.toString()}' AND ";
  111. }
  112. });
  113. if (matches.endsWith("AND ")) {
  114. matches = matches.substring(0, matches.length - 4);
  115. }
  116. List<Map<String, dynamic>> dbResult = await database.query(
  117. _currentTypeInstance.tableName,
  118. where: matches,
  119. limit: limit,
  120. orderBy: orderBy,
  121. );
  122. debugPrint(
  123. "[CommonMapper] selectMatchBy result: ${dbResult.length.toString()}");
  124. return _currentTypeInstance.resultAsList(dbResult);
  125. } catch (e) {
  126. debugPrint("[CommonMapper] selectMatchBy fail ${e.toString()}");
  127. return null;
  128. }
  129. }
  130. // U
  131. // Future<bool> updateByFirstKey(T entity) async {
  132. // try {
  133. // Database database = SqfliteDataBase.getInstance();
  134. // await database.update(currentTypeInstance.tableName, entity.value,
  135. // where:
  136. // "${entity.value.keys.elementAt(0)} = '${entity.value.values.elementAt(0)}'");
  137. // debugPrint("[CommonMapper] update: ${entity.toString()}");
  138. // return true;
  139. // } catch (e) {
  140. // debugPrint("[CommonMapper] update fail ${e.toString()}");
  141. // return false;
  142. // }
  143. // }
  144. Future<bool> updateByFirstKeySelective(T entity) async {
  145. try {
  146. Database database = SqfliteDataBase.getInstance()!;
  147. entity.value.removeWhere((_, value) {
  148. return value == null;
  149. });
  150. await database.update(_currentTypeInstance.tableName, entity.value,
  151. where:
  152. "${entity.value.keys.elementAt(0)} = '${entity.value.values.elementAt(0)}'");
  153. debugPrint("[CommonMapper] update where: ${entity.toString()}");
  154. return true;
  155. } catch (e) {
  156. debugPrint("[CommonMapper] update fail ${e.toString()}");
  157. return false;
  158. }
  159. }
  160. // D
  161. Future<int> delete(T entity) async {
  162. Database database = SqfliteDataBase.getInstance()!;
  163. String matches = "";
  164. entity.value.forEach((key, value) {
  165. if (value != null) {
  166. matches += "$key = '${value.toString()}' AND ";
  167. }
  168. });
  169. if (matches.endsWith("AND ")) {
  170. matches = matches.substring(0, matches.length - 4);
  171. }
  172. int count =
  173. await database.delete(_currentTypeInstance.tableName, where: matches);
  174. debugPrint("[CommonMapper] delete $count row");
  175. return count;
  176. }
  177. }