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:huixiang/im/database/message_dao.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]) |
||||
abstract class HxDatabase extends FloorDatabase { |
||||
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"); |
||||
}); |
||||
} |
||||
|
||||
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 |
||||
|
||||
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); |
||||
} |
||||
} |
||||
// // GENERATED CODE - DO NOT MODIFY BY HAND |
||||
// |
||||
// part of 'hx_database.dart'; |
||||
// |
||||
// // ************************************************************************** |
||||
// // FloorGenerator |
||||
// // ************************************************************************** |
||||
// |
||||
// // ignore: avoid_classes_with_only_static_members |
||||
// import 'package:floor/floor.dart'; |
||||
// |
||||
// 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); |
||||
// } |
||||
// } |
||||
|
@ -1,17 +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); |
||||
|
||||
} |
||||
// 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); |
||||
// |
||||
// } |
@ -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