|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:huixiang/im/database/message.dart';
|
|
|
|
import 'package:huixiang/im/database/migration.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class HxDatabase {
|
|
|
|
|
|
|
|
Database db;
|
|
|
|
|
|
|
|
void open() async {
|
|
|
|
|
|
|
|
// _migrations.add(Migration(1, 2, (Database database) async {
|
|
|
|
// database.execute('ALTER TABLE `Message` ADD COLUMN `replyId` VARCHAR(20) DEFAULT NULL AFTER `toId`');
|
|
|
|
// }));
|
|
|
|
|
|
|
|
await openDatabase(
|
|
|
|
'hx.db',
|
|
|
|
version: 2,
|
|
|
|
onCreate: (Database db, int version) async {
|
|
|
|
db.execute('CREATE TABLE IF NOT EXISTS `Message` (`id` INTEGER, `fromId` VARCHAR(20), `toId` VARCHAR(20), `replyId` VARCHAR(20), `content` TEXT, `attach` TEXT, `msgType` INTEGER, `time` VARCHAR(20), `state` INTEGER, `isDelete` INTEGER, PRIMARY KEY (`id`))');
|
|
|
|
},
|
|
|
|
onConfigure: (database) async {
|
|
|
|
await database.execute('PRAGMA foreign_keys = ON');
|
|
|
|
},
|
|
|
|
onUpgrade: (database, startVersion, endVersion) async {
|
|
|
|
await runMigrations(database, startVersion, endVersion, _migrations);
|
|
|
|
},
|
|
|
|
onOpen: (Database db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Message>> queryList(userId) {
|
|
|
|
if (db == null) {
|
|
|
|
return Future.value(<Message>[]);
|
|
|
|
}
|
|
|
|
String sql = 'SELECT * FROM Message WHERE toId = ? OR fromId = ? GROUP BY toId,fromId ORDER BY time DESC';
|
|
|
|
return db.rawQuery(sql, [userId, userId]).then((value) {
|
|
|
|
return value.map((e) {
|
|
|
|
debugPrint("Message: ${e}");
|
|
|
|
return Message.fromJson(e);
|
|
|
|
}).toList();
|
|
|
|
}, onError: (error) {
|
|
|
|
debugPrint("Messageerror: $error");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Message>> queryUList(userId) {
|
|
|
|
if (db == null) {
|
|
|
|
return Future.value(<Message>[]);
|
|
|
|
}
|
|
|
|
String sql = 'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC';
|
|
|
|
return db.rawQuery(sql, [userId, userId]).then((value) {
|
|
|
|
return value.map((e) => Message.fromJson(e)).toList();
|
|
|
|
}, onError: (error) {
|
|
|
|
debugPrint("Messageerror: $error");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Map>> queryListAll() {
|
|
|
|
if (db == null) {
|
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
String sql = 'SELECT * FROM Message ORDER BY time DESC';
|
|
|
|
return db.rawQuery(sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> deleteAll() async {
|
|
|
|
return db.delete("Message");
|
|
|
|
}
|
|
|
|
|
|
|
|
update(Map<dynamic, dynamic> message) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> insert(Map message) async {
|
|
|
|
if (db == null) {
|
|
|
|
return Future.value(0);
|
|
|
|
}
|
|
|
|
debugPrint("Messageinsert: ${message}");
|
|
|
|
return db.insert("Message", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<Migration> _migrations = [];
|
|
|
|
|
|
|
|
addMigrations(List<Migration> migrations) {
|
|
|
|
_migrations.addAll(migrations);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> runMigrations(
|
|
|
|
final Database migrationDatabase,
|
|
|
|
final int startVersion,
|
|
|
|
final int endVersion,
|
|
|
|
final List<Migration> migrations,
|
|
|
|
) async {
|
|
|
|
final relevantMigrations = migrations
|
|
|
|
.where((migration) => migration.startVersion >= startVersion)
|
|
|
|
.toList()
|
|
|
|
..sort(
|
|
|
|
(first, second) => first.startVersion.compareTo(second.startVersion));
|
|
|
|
|
|
|
|
if (relevantMigrations.isEmpty ||
|
|
|
|
relevantMigrations.last.endVersion != endVersion) {
|
|
|
|
throw StateError(
|
|
|
|
'There is no migration supplied to update the database to the current version.'
|
|
|
|
' Aborting the migration.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final migration in relevantMigrations) {
|
|
|
|
await migration.migrate(migrationDatabase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|