zsw
4 months ago
14 changed files with 705 additions and 334 deletions
@ -0,0 +1,18 @@ |
|||||||
|
|
||||||
|
|
||||||
|
import 'dart:async'; |
||||||
|
|
||||||
|
import 'package:floor/floor.dart'; |
||||||
|
import 'package:flutter/cupertino.dart'; |
||||||
|
import 'package:huixiang/im/database/message_dao.dart'; |
||||||
|
import 'package:huixiang/im/database/message.dart'; |
||||||
|
import 'package:sqflite/sqflite.dart' as sqflite; |
||||||
|
|
||||||
|
part 'hx_database.g.dart'; |
||||||
|
|
||||||
|
@Database(version: 1, entities: [Message]) |
||||||
|
abstract class HxDatabase extends FloorDatabase { |
||||||
|
|
||||||
|
MessageDao get messageDao; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,144 @@ |
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||||
|
|
||||||
|
part of 'hx_database.dart'; |
||||||
|
|
||||||
|
// ************************************************************************** |
||||||
|
// FloorGenerator |
||||||
|
// ************************************************************************** |
||||||
|
|
||||||
|
// ignore: avoid_classes_with_only_static_members |
||||||
|
class $FloorHxDatabase { |
||||||
|
/// Creates a database builder for a persistent database. |
||||||
|
/// Once a database is built, you should keep a reference to it and re-use it. |
||||||
|
static _$HxDatabaseBuilder databaseBuilder(String name) => |
||||||
|
_$HxDatabaseBuilder(name); |
||||||
|
|
||||||
|
/// Creates a database builder for an in memory database. |
||||||
|
/// Information stored in an in memory database disappears when the process is killed. |
||||||
|
/// Once a database is built, you should keep a reference to it and re-use it. |
||||||
|
static _$HxDatabaseBuilder inMemoryDatabaseBuilder() => |
||||||
|
_$HxDatabaseBuilder(null); |
||||||
|
} |
||||||
|
|
||||||
|
class _$HxDatabaseBuilder { |
||||||
|
_$HxDatabaseBuilder(this.name); |
||||||
|
|
||||||
|
final String name; |
||||||
|
|
||||||
|
final List<Migration> _migrations = []; |
||||||
|
|
||||||
|
Callback _callback; |
||||||
|
|
||||||
|
/// Adds migrations to the builder. |
||||||
|
_$HxDatabaseBuilder addMigrations(List<Migration> migrations) { |
||||||
|
_migrations.addAll(migrations); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/// Adds a database [Callback] to the builder. |
||||||
|
_$HxDatabaseBuilder addCallback(Callback callback) { |
||||||
|
_callback = callback; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
/// Creates the database and initializes it. |
||||||
|
Future<HxDatabase> build() async { |
||||||
|
final path = name != null |
||||||
|
? await sqfliteDatabaseFactory.getDatabasePath(name) |
||||||
|
: ':memory:'; |
||||||
|
final database = _$HxDatabase(); |
||||||
|
database.database = await database.open( |
||||||
|
path, |
||||||
|
_migrations, |
||||||
|
_callback, |
||||||
|
); |
||||||
|
return database; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class _$HxDatabase extends HxDatabase { |
||||||
|
_$HxDatabase([StreamController<String> listener]) { |
||||||
|
changeListener = listener ?? StreamController<String>.broadcast(); |
||||||
|
} |
||||||
|
|
||||||
|
MessageDao _messageDaoInstance; |
||||||
|
|
||||||
|
Future<sqflite.Database> open( |
||||||
|
String path, |
||||||
|
List<Migration> migrations, [ |
||||||
|
Callback callback, |
||||||
|
]) async { |
||||||
|
final databaseOptions = sqflite.OpenDatabaseOptions( |
||||||
|
version: 1, |
||||||
|
onConfigure: (database) async { |
||||||
|
await database.execute('PRAGMA foreign_keys = ON'); |
||||||
|
await callback?.onConfigure?.call(database); |
||||||
|
}, |
||||||
|
onOpen: (database) async { |
||||||
|
await callback?.onOpen?.call(database); |
||||||
|
}, |
||||||
|
onUpgrade: (database, startVersion, endVersion) async { |
||||||
|
await MigrationAdapter.runMigrations( |
||||||
|
database, startVersion, endVersion, migrations); |
||||||
|
|
||||||
|
await callback?.onUpgrade?.call(database, startVersion, endVersion); |
||||||
|
}, |
||||||
|
onCreate: (database, version) async { |
||||||
|
await database.execute( |
||||||
|
'CREATE TABLE IF NOT EXISTS `Message` (`id` INTEGER, `fromId` INTEGER, `toId` INTEGER, `content` TEXT, `attach` TEXT, `msgType` INTEGER, `time` INTEGER, `state` INTEGER, `isDelete` INTEGER, PRIMARY KEY (`id`))'); |
||||||
|
|
||||||
|
await callback?.onCreate?.call(database, version); |
||||||
|
}, |
||||||
|
); |
||||||
|
return sqfliteDatabaseFactory.openDatabase(path, options: databaseOptions); |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
MessageDao get messageDao { |
||||||
|
return _messageDaoInstance ??= _$MessageDao(database, changeListener); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class _$MessageDao extends MessageDao { |
||||||
|
_$MessageDao( |
||||||
|
this.database, |
||||||
|
this.changeListener, |
||||||
|
) : _queryAdapter = QueryAdapter(database, changeListener), |
||||||
|
_messageInsertionAdapter = InsertionAdapter( |
||||||
|
database, |
||||||
|
'Message', |
||||||
|
(Message item) => item.toJson(), |
||||||
|
changeListener); |
||||||
|
|
||||||
|
final sqflite.DatabaseExecutor database; |
||||||
|
|
||||||
|
final StreamController<String> changeListener; |
||||||
|
|
||||||
|
final QueryAdapter _queryAdapter; |
||||||
|
|
||||||
|
final InsertionAdapter<Message> _messageInsertionAdapter; |
||||||
|
|
||||||
|
@override |
||||||
|
Stream<List<Message>> findMessageByToId(int toId) { |
||||||
|
return _queryAdapter.queryListStream( |
||||||
|
'SELECT * FROM Message WHERE toId = ?1', |
||||||
|
mapper: (Map<String, Object> row) => Message.fromJson(row), |
||||||
|
arguments: [toId], |
||||||
|
queryableName: 'Message', |
||||||
|
isView: false); |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
Future<List<Message>> findMessageByGroup(int userId) { |
||||||
|
debugPrint("findMessageByGroup: $userId"); |
||||||
|
return _queryAdapter.queryList( |
||||||
|
'SELECT * FROM Message WHERE toId = ?1 OR fromId = ?2 GROUP BY toId,fromId ORDER BY time DESC', |
||||||
|
mapper: (Map<String, Object> row) => Message.fromJson(row), |
||||||
|
arguments: [userId, userId]); |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
Future<void> insertMessage(Message message) async { |
||||||
|
await _messageInsertionAdapter.insert(message, OnConflictStrategy.abort); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,61 @@ |
|||||||
|
import 'package:floor/floor.dart'; |
||||||
|
|
||||||
|
@entity |
||||||
|
class Message { |
||||||
|
@primaryKey |
||||||
|
int id; |
||||||
|
|
||||||
|
int fromId; |
||||||
|
|
||||||
|
int toId; |
||||||
|
|
||||||
|
String content; |
||||||
|
|
||||||
|
String attach; |
||||||
|
|
||||||
|
int msgType; |
||||||
|
|
||||||
|
int time; |
||||||
|
|
||||||
|
int state; |
||||||
|
|
||||||
|
int isDelete; |
||||||
|
|
||||||
|
Message(id, fromId, toId, content, attach, msgType, time, state, isDelete); |
||||||
|
|
||||||
|
factory Message.fromJson(Map<String, dynamic> json) => Message( |
||||||
|
json["id"], |
||||||
|
json["fromId"], |
||||||
|
json["toId"], |
||||||
|
json["content"], |
||||||
|
json["attach"], |
||||||
|
json["msgType"], |
||||||
|
json["time"], |
||||||
|
json["state"], |
||||||
|
json["isDelete"]); |
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => <String, dynamic>{ |
||||||
|
"id": id, |
||||||
|
"fromId": fromId, |
||||||
|
"toId": toId, |
||||||
|
"content": content, |
||||||
|
"attach": attach, |
||||||
|
"msgType": msgType, |
||||||
|
"time": time, |
||||||
|
"state": state, |
||||||
|
"isDelete": isDelete == null ? 0 : isDelete |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
createMessage(var toId, String content, {String attach, int msgType, userId}) { |
||||||
|
return Message.fromJson(<String, dynamic>{ |
||||||
|
"fromId": userId, |
||||||
|
"toId": toId, |
||||||
|
"content": content, |
||||||
|
"attach": attach, |
||||||
|
"msgType": msgType ?? 0, |
||||||
|
"time": DateTime.now().millisecondsSinceEpoch, |
||||||
|
"state": 0, |
||||||
|
"isDelete": 0 |
||||||
|
}); |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
import 'package:floor/floor.dart'; |
||||||
|
import 'package:huixiang/im/database/message.dart'; |
||||||
|
|
||||||
|
|
||||||
|
@dao |
||||||
|
abstract class MessageDao { |
||||||
|
|
||||||
|
@Query('SELECT * FROM Message WHERE toId = :toId') |
||||||
|
Stream<List<Message>> findMessageByToId(int toId); |
||||||
|
|
||||||
|
@insert |
||||||
|
Future<void> insertMessage(Message message); |
||||||
|
|
||||||
|
@Query('SELECT * FROM Message WHERE toId = :userId OR fromId = :userId GROUP BY toId,fromId ORDER BY time DESC') |
||||||
|
Future<List<Message>> findMessageByGroup(int userId); |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue