zsw
4 months ago
19 changed files with 804 additions and 610 deletions
@ -1,18 +1,123 @@ |
|||||||
|
|
||||||
|
|
||||||
import 'dart:async'; |
|
||||||
|
|
||||||
import 'package:floor/floor.dart'; |
|
||||||
import 'package:flutter/cupertino.dart'; |
import 'package:flutter/cupertino.dart'; |
||||||
import 'package:huixiang/im/database/message_dao.dart'; |
|
||||||
import 'package:huixiang/im/database/message.dart'; |
import 'package:huixiang/im/database/message.dart'; |
||||||
import 'package:sqflite/sqflite.dart' as sqflite; |
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; |
||||||
|
} |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
part 'hx_database.g.dart'; |
void close() { |
||||||
|
db.close(); |
||||||
|
} |
||||||
|
|
||||||
@Database(version: 1, entities: [Message]) |
Future<List<Message>> queryList(userId) { |
||||||
abstract class HxDatabase extends FloorDatabase { |
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"); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
MessageDao get messageDao; |
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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
} |
} |
||||||
|
@ -1,144 +1,146 @@ |
|||||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
// // GENERATED CODE - DO NOT MODIFY BY HAND |
||||||
|
// |
||||||
part of 'hx_database.dart'; |
// part of 'hx_database.dart'; |
||||||
|
// |
||||||
// ************************************************************************** |
// // ************************************************************************** |
||||||
// FloorGenerator |
// // FloorGenerator |
||||||
// ************************************************************************** |
// // ************************************************************************** |
||||||
|
// |
||||||
// ignore: avoid_classes_with_only_static_members |
// // ignore: avoid_classes_with_only_static_members |
||||||
class $FloorHxDatabase { |
// import 'package:floor/floor.dart'; |
||||||
/// Creates a database builder for a persistent database. |
// |
||||||
/// Once a database is built, you should keep a reference to it and re-use it. |
// class $FloorHxDatabase { |
||||||
static _$HxDatabaseBuilder databaseBuilder(String name) => |
// /// Creates a database builder for a persistent database. |
||||||
_$HxDatabaseBuilder(name); |
// /// Once a database is built, you should keep a reference to it and re-use it. |
||||||
|
// static _$HxDatabaseBuilder databaseBuilder(String name) => |
||||||
/// Creates a database builder for an in memory database. |
// _$HxDatabaseBuilder(name); |
||||||
/// 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. |
// /// Creates a database builder for an in memory database. |
||||||
static _$HxDatabaseBuilder inMemoryDatabaseBuilder() => |
// /// Information stored in an in memory database disappears when the process is killed. |
||||||
_$HxDatabaseBuilder(null); |
// /// 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); |
// |
||||||
|
// class _$HxDatabaseBuilder { |
||||||
final String name; |
// _$HxDatabaseBuilder(this.name); |
||||||
|
// |
||||||
final List<Migration> _migrations = []; |
// final String name; |
||||||
|
// |
||||||
Callback _callback; |
// final List<Migration> _migrations = []; |
||||||
|
// |
||||||
/// Adds migrations to the builder. |
// Callback _callback; |
||||||
_$HxDatabaseBuilder addMigrations(List<Migration> migrations) { |
// |
||||||
_migrations.addAll(migrations); |
// /// Adds migrations to the builder. |
||||||
return this; |
// _$HxDatabaseBuilder addMigrations(List<Migration> migrations) { |
||||||
} |
// _migrations.addAll(migrations); |
||||||
|
// return this; |
||||||
/// Adds a database [Callback] to the builder. |
// } |
||||||
_$HxDatabaseBuilder addCallback(Callback callback) { |
// |
||||||
_callback = callback; |
// /// Adds a database [Callback] to the builder. |
||||||
return this; |
// _$HxDatabaseBuilder addCallback(Callback callback) { |
||||||
} |
// _callback = callback; |
||||||
|
// return this; |
||||||
/// Creates the database and initializes it. |
// } |
||||||
Future<HxDatabase> build() async { |
// |
||||||
final path = name != null |
// /// Creates the database and initializes it. |
||||||
? await sqfliteDatabaseFactory.getDatabasePath(name) |
// Future<HxDatabase> build() async { |
||||||
: ':memory:'; |
// final path = name != null |
||||||
final database = _$HxDatabase(); |
// ? await sqfliteDatabaseFactory.getDatabasePath(name) |
||||||
database.database = await database.open( |
// : ':memory:'; |
||||||
path, |
// final database = _$HxDatabase(); |
||||||
_migrations, |
// database.database = await database.open( |
||||||
_callback, |
// path, |
||||||
); |
// _migrations, |
||||||
return database; |
// _callback, |
||||||
} |
// ); |
||||||
} |
// return database; |
||||||
|
// } |
||||||
class _$HxDatabase extends HxDatabase { |
// } |
||||||
_$HxDatabase([StreamController<String> listener]) { |
// |
||||||
changeListener = listener ?? StreamController<String>.broadcast(); |
// class _$HxDatabase extends HxDatabase { |
||||||
} |
// _$HxDatabase([StreamController<String> listener]) { |
||||||
|
// changeListener = listener ?? StreamController<String>.broadcast(); |
||||||
MessageDao _messageDaoInstance; |
// } |
||||||
|
// |
||||||
Future<sqflite.Database> open( |
// MessageDao _messageDaoInstance; |
||||||
String path, |
// |
||||||
List<Migration> migrations, [ |
// Future<sqflite.Database> open( |
||||||
Callback callback, |
// String path, |
||||||
]) async { |
// List<Migration> migrations, [ |
||||||
final databaseOptions = sqflite.OpenDatabaseOptions( |
// Callback callback, |
||||||
version: 1, |
// ]) async { |
||||||
onConfigure: (database) async { |
// final databaseOptions = sqflite.OpenDatabaseOptions( |
||||||
await database.execute('PRAGMA foreign_keys = ON'); |
// version: 1, |
||||||
await callback?.onConfigure?.call(database); |
// onConfigure: (database) async { |
||||||
}, |
// await database.execute('PRAGMA foreign_keys = ON'); |
||||||
onOpen: (database) async { |
// await callback?.onConfigure?.call(database); |
||||||
await callback?.onOpen?.call(database); |
// }, |
||||||
}, |
// onOpen: (database) async { |
||||||
onUpgrade: (database, startVersion, endVersion) async { |
// await callback?.onOpen?.call(database); |
||||||
await MigrationAdapter.runMigrations( |
// }, |
||||||
database, startVersion, endVersion, migrations); |
// onUpgrade: (database, startVersion, endVersion) async { |
||||||
|
// await MigrationAdapter.runMigrations( |
||||||
await callback?.onUpgrade?.call(database, startVersion, endVersion); |
// database, startVersion, endVersion, migrations); |
||||||
}, |
// |
||||||
onCreate: (database, version) async { |
// await callback?.onUpgrade?.call(database, startVersion, endVersion); |
||||||
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`))'); |
// onCreate: (database, version) async { |
||||||
|
// await database.execute( |
||||||
await callback?.onCreate?.call(database, version); |
// '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); |
// }, |
||||||
} |
// ); |
||||||
|
// return sqfliteDatabaseFactory.openDatabase(path, options: databaseOptions); |
||||||
@override |
// } |
||||||
MessageDao get messageDao { |
// |
||||||
return _messageDaoInstance ??= _$MessageDao(database, changeListener); |
// @override |
||||||
} |
// MessageDao get messageDao { |
||||||
} |
// return _messageDaoInstance ??= _$MessageDao(database, changeListener); |
||||||
|
// } |
||||||
class _$MessageDao extends MessageDao { |
// } |
||||||
_$MessageDao( |
// |
||||||
this.database, |
// class _$MessageDao extends MessageDao { |
||||||
this.changeListener, |
// _$MessageDao( |
||||||
) : _queryAdapter = QueryAdapter(database, changeListener), |
// this.database, |
||||||
_messageInsertionAdapter = InsertionAdapter( |
// this.changeListener, |
||||||
database, |
// ) : _queryAdapter = QueryAdapter(database, changeListener), |
||||||
'Message', |
// _messageInsertionAdapter = InsertionAdapter( |
||||||
(Message item) => item.toJson(), |
// database, |
||||||
changeListener); |
// 'Message', |
||||||
|
// (Message item) => item.toJson(), |
||||||
final sqflite.DatabaseExecutor database; |
// changeListener); |
||||||
|
// |
||||||
final StreamController<String> changeListener; |
// final sqflite.DatabaseExecutor database; |
||||||
|
// |
||||||
final QueryAdapter _queryAdapter; |
// final StreamController<String> changeListener; |
||||||
|
// |
||||||
final InsertionAdapter<Message> _messageInsertionAdapter; |
// final QueryAdapter _queryAdapter; |
||||||
|
// |
||||||
@override |
// final InsertionAdapter<Message> _messageInsertionAdapter; |
||||||
Stream<List<Message>> findMessageByToId(int toId) { |
// |
||||||
return _queryAdapter.queryListStream( |
// @override |
||||||
'SELECT * FROM Message WHERE toId = ?1', |
// Stream<List<Message>> findMessageByToId(int toId) { |
||||||
mapper: (Map<String, Object> row) => Message.fromJson(row), |
// return _queryAdapter.queryListStream( |
||||||
arguments: [toId], |
// 'SELECT * FROM Message WHERE toId = ?1', |
||||||
queryableName: 'Message', |
// mapper: (Map<String, Object> row) => Message.fromJson(row), |
||||||
isView: false); |
// arguments: [toId], |
||||||
} |
// queryableName: 'Message', |
||||||
|
// isView: false); |
||||||
@override |
// } |
||||||
Future<List<Message>> findMessageByGroup(int userId) { |
// |
||||||
debugPrint("findMessageByGroup: $userId"); |
// @override |
||||||
return _queryAdapter.queryList( |
// Future<List<Message>> findMessageByGroup(int userId) { |
||||||
'SELECT * FROM Message WHERE toId = ?1 OR fromId = ?2 GROUP BY toId,fromId ORDER BY time DESC', |
// debugPrint("findMessageByGroup: $userId"); |
||||||
mapper: (Map<String, Object> row) => Message.fromJson(row), |
// return _queryAdapter.queryList( |
||||||
arguments: [userId, userId]); |
// '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); |
// @override |
||||||
} |
// Future<void> insertMessage(Message message) async { |
||||||
} |
// await _messageInsertionAdapter.insert(message, OnConflictStrategy.abort); |
||||||
|
// } |
||||||
|
// } |
||||||
|
@ -1,17 +1,17 @@ |
|||||||
import 'package:floor/floor.dart'; |
// import 'package:floor/floor.dart'; |
||||||
import 'package:huixiang/im/database/message.dart'; |
// import 'package:huixiang/im/database/message.dart'; |
||||||
|
// |
||||||
|
// |
||||||
@dao |
// @dao |
||||||
abstract class MessageDao { |
// abstract class MessageDao { |
||||||
|
// |
||||||
@Query('SELECT * FROM Message WHERE toId = :toId') |
// @Query('SELECT * FROM Message WHERE toId = :toId') |
||||||
Stream<List<Message>> findMessageByToId(int toId); |
// Stream<List<Message>> findMessageByToId(int toId); |
||||||
|
// |
||||||
@insert |
// @insert |
||||||
Future<void> insertMessage(Message message); |
// Future<void> insertMessage(Message message); |
||||||
|
// |
||||||
@Query('SELECT * FROM Message WHERE toId = :userId OR fromId = :userId GROUP BY toId,fromId ORDER BY time DESC') |
// @Query('SELECT * FROM Message WHERE toId = :userId OR fromId = :userId GROUP BY toId,fromId ORDER BY time DESC') |
||||||
Future<List<Message>> findMessageByGroup(int userId); |
// Future<List<Message>> findMessageByGroup(int userId); |
||||||
|
// |
||||||
} |
// } |
@ -0,0 +1,41 @@ |
|||||||
|
import 'package:sqflite/sqflite.dart' as sqflite; |
||||||
|
|
||||||
|
/// Base class for a database migration. |
||||||
|
/// |
||||||
|
/// Each migration can move between 2 versions that are defined by |
||||||
|
/// [startVersion] and [endVersion]. |
||||||
|
class Migration { |
||||||
|
/// The start version of the database. |
||||||
|
final int startVersion; |
||||||
|
|
||||||
|
/// The start version of the database. |
||||||
|
final int endVersion; |
||||||
|
|
||||||
|
/// Function that performs the migration. |
||||||
|
final Future<void> Function(sqflite.Database database) migrate; |
||||||
|
|
||||||
|
/// Creates a new migration between [startVersion] and [endVersion]. |
||||||
|
/// [migrate] will be called by the database and performs the actual |
||||||
|
/// migration. |
||||||
|
Migration(this.startVersion, this.endVersion, this.migrate) |
||||||
|
: assert(startVersion > 0), |
||||||
|
assert(startVersion < endVersion); |
||||||
|
|
||||||
|
@override |
||||||
|
bool operator ==(Object other) => |
||||||
|
identical(this, other) || |
||||||
|
other is Migration && |
||||||
|
runtimeType == other.runtimeType && |
||||||
|
startVersion == other.startVersion && |
||||||
|
endVersion == other.endVersion && |
||||||
|
migrate == other.migrate; |
||||||
|
|
||||||
|
@override |
||||||
|
int get hashCode => |
||||||
|
startVersion.hashCode ^ endVersion.hashCode ^ migrate.hashCode; |
||||||
|
|
||||||
|
@override |
||||||
|
String toString() { |
||||||
|
return 'Migration{startVersion: $startVersion, endVersion: $endVersion, migrate: $migrate}'; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue