Hibok
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

131 行
3.4 KiB

  1. import 'dart:async';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/utils/sql_util.dart';
  4. import 'package:sqflite/sqflite.dart';
  5. class BaseModel {
  6. Database db;
  7. final String table = '';
  8. var query;
  9. BaseModel(this.db) {
  10. query = db.query;
  11. }
  12. }
  13. class Sql extends BaseModel {
  14. final String tableName;
  15. Sql.setTable(String name)
  16. : tableName = name,
  17. super(SqlUtil.getCurrentDatabase());
  18. Future<List> get() async {
  19. var myId = UserData().basicInfo.userId;
  20. return await this.query(tableName, where: 'userId = $myId');
  21. }
  22. Future<List> getAll() async {
  23. var myId = UserData().basicInfo.userId;
  24. var result = await this.query(tableName, where: 'userId = $myId');
  25. return result.toList();
  26. }
  27. String getTableName() {
  28. return tableName;
  29. }
  30. Future<int> delete(String value, String key) async {
  31. return await this.db.delete(tableName,
  32. where: '$key = ? and userId = ?',
  33. whereArgs: [value, UserData().basicInfo.userId]);
  34. }
  35. Future<void> createTable(String tableName, String colums) async {
  36. return await this
  37. .db
  38. .execute('CREATE TABLE IF NOT EXISTS $tableName ($colums)');
  39. }
  40. Future<int> clearTable(String tableName) async {
  41. return await this.db.delete(tableName);
  42. }
  43. Future<List> getByCondition({Map<dynamic, dynamic> conditions}) async {
  44. if (conditions == null || conditions.isEmpty) {
  45. return this.get();
  46. }
  47. String stringConditions = '';
  48. int index = 0;
  49. conditions.forEach((key, value) {
  50. if (value == null) {
  51. return;
  52. }
  53. if (value.runtimeType == String) {
  54. stringConditions = '$stringConditions $key = "$value"';
  55. }
  56. if (value.runtimeType == int) {
  57. stringConditions = '$stringConditions $key = $value';
  58. }
  59. if (index >= 0 && index < conditions.length - 1) {
  60. stringConditions = '$stringConditions and';
  61. }
  62. index++;
  63. });
  64. var myId = UserData().basicInfo.userId;
  65. if (stringConditions.length > 0) {
  66. stringConditions = '$stringConditions and userId = $myId';
  67. }
  68. return await this.query(tableName, where: stringConditions);
  69. }
  70. Future<Map<String, dynamic>> insert(Map<String, dynamic> json) async {
  71. var id = await this.db.insert(tableName, json);
  72. json['id'] = id;
  73. return json;
  74. }
  75. ///
  76. /// 搜索
  77. /// @param Object condition
  78. /// @mods [And, Or] default is Or
  79. /// search({'name': "hanxu', 'id': 1};
  80. ///
  81. Future<List> search(
  82. {Map<String, dynamic> conditions, String mods = 'Or'}) async {
  83. if (conditions == null || conditions.isEmpty) {
  84. return this.get();
  85. }
  86. String stringConditions = '';
  87. int index = 0;
  88. conditions.forEach((key, value) {
  89. if (value == null) {
  90. return;
  91. }
  92. if (value.runtimeType == String) {
  93. stringConditions = '$stringConditions $key like "%$value%"';
  94. }
  95. if (value.runtimeType == int) {
  96. stringConditions = '$stringConditions $key = "%$value%"';
  97. }
  98. if (index >= 0 && index < conditions.length - 1) {
  99. stringConditions = '$stringConditions $mods';
  100. }
  101. index++;
  102. });
  103. var myId = UserData().basicInfo.userId;
  104. if (stringConditions.length > 0) {
  105. stringConditions = '$stringConditions and userId = $myId';
  106. }
  107. return await this.query(tableName, where: stringConditions);
  108. }
  109. }