|
|
@ -1,5 +1,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart'; |
|
|
|
import 'package:flutter/cupertino.dart'; |
|
|
|
|
|
|
|
import 'package:huixiang/im/database/contact.dart'; |
|
|
|
import 'package:huixiang/im/database/message.dart'; |
|
|
|
import 'package:huixiang/im/database/message.dart'; |
|
|
|
import 'package:huixiang/im/database/migration.dart'; |
|
|
|
import 'package:huixiang/im/database/migration.dart'; |
|
|
|
import 'package:sqflite/sqflite.dart'; |
|
|
|
import 'package:sqflite/sqflite.dart'; |
|
|
@ -9,27 +10,32 @@ class HxDatabase { |
|
|
|
|
|
|
|
|
|
|
|
Database db; |
|
|
|
Database db; |
|
|
|
|
|
|
|
|
|
|
|
void open() async { |
|
|
|
void open({String key}) async { |
|
|
|
|
|
|
|
|
|
|
|
// _migrations.add(Migration(1, 2, (Database database) async { |
|
|
|
// _migrations.add(Migration(1, 2, (Database database) async { |
|
|
|
// database.execute('ALTER TABLE `Message` ADD COLUMN `replyId` VARCHAR(20) DEFAULT NULL AFTER `toId`'); |
|
|
|
// database.execute('ALTER TABLE `Message` ADD COLUMN `replyId` VARCHAR(20) DEFAULT NULL AFTER `toId`'); |
|
|
|
// })); |
|
|
|
// })); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String databaseName = 'hx.db'; |
|
|
|
|
|
|
|
if (key?.isNotEmpty ?? false) { |
|
|
|
|
|
|
|
databaseName = 'hx_$key.db'; |
|
|
|
|
|
|
|
} |
|
|
|
await openDatabase( |
|
|
|
await openDatabase( |
|
|
|
'hx.db', |
|
|
|
databaseName, |
|
|
|
version: 2, |
|
|
|
version: 2, |
|
|
|
onCreate: (Database db, int version) async { |
|
|
|
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`))'); |
|
|
|
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`))'); |
|
|
|
}, |
|
|
|
db.execute('CREATE TABLE IF NOT EXISTS `Contact` (`id` INTEGER, `userId` VARCHAR(20), `nickName` VARCHAR(20), `imageUrl` VARCHAR(200), `state` INTEGER, `isDelete` INTEGER, PRIMARY KEY (`id`))'); |
|
|
|
onConfigure: (database) async { |
|
|
|
}, |
|
|
|
await database.execute('PRAGMA foreign_keys = ON'); |
|
|
|
onConfigure: (database) async { |
|
|
|
}, |
|
|
|
await database.execute('PRAGMA foreign_keys = ON'); |
|
|
|
onUpgrade: (database, startVersion, endVersion) async { |
|
|
|
}, |
|
|
|
await runMigrations(database, startVersion, endVersion, _migrations); |
|
|
|
onUpgrade: (database, startVersion, endVersion) async { |
|
|
|
}, |
|
|
|
await runMigrations(database, startVersion, endVersion, _migrations); |
|
|
|
onOpen: (Database db) { |
|
|
|
}, |
|
|
|
this.db = db; |
|
|
|
onOpen: (Database db) { |
|
|
|
} |
|
|
|
this.db = db; |
|
|
|
|
|
|
|
} |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -37,6 +43,22 @@ class HxDatabase { |
|
|
|
db.close(); |
|
|
|
db.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<Message> lastMessage(String userId) async { |
|
|
|
|
|
|
|
if (db == null) { |
|
|
|
|
|
|
|
return Future.value(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String sql = 'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC LIMIT 1'; |
|
|
|
|
|
|
|
List<Message> messages = await db.rawQuery(sql, [userId, userId]).then((value) { |
|
|
|
|
|
|
|
return value.map((e) { |
|
|
|
|
|
|
|
debugPrint("Message: ${e}"); |
|
|
|
|
|
|
|
return Message.fromJson(e); |
|
|
|
|
|
|
|
}).toList(); |
|
|
|
|
|
|
|
}, onError: (error) { |
|
|
|
|
|
|
|
debugPrint("Messageerror: $error"); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return (messages?.isNotEmpty ?? false) ? messages.first : null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Future<List<Message>> queryList(userId) { |
|
|
|
Future<List<Message>> queryList(userId) { |
|
|
|
if (db == null) { |
|
|
|
if (db == null) { |
|
|
|
return Future.value(<Message>[]); |
|
|
|
return Future.value(<Message>[]); |
|
|
@ -77,17 +99,54 @@ class HxDatabase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
update(Map<dynamic, dynamic> message) { |
|
|
|
update(Map<dynamic, dynamic> message) { |
|
|
|
|
|
|
|
if (db == null) { |
|
|
|
|
|
|
|
return Future.value(0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
debugPrint("Message_insert: $message"); |
|
|
|
|
|
|
|
return db.update("Message", message, where: 'id = ?', whereArgs: [message['id']]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Future<int> insert(Map message) async { |
|
|
|
Future<int> insert(Map message) async { |
|
|
|
if (db == null) { |
|
|
|
if (db == null) { |
|
|
|
return Future.value(0); |
|
|
|
return Future.value(0); |
|
|
|
} |
|
|
|
} |
|
|
|
debugPrint("Messageinsert: ${message}"); |
|
|
|
debugPrint("Message_insert: $message"); |
|
|
|
return db.insert("Message", message); |
|
|
|
return db.insert("Message", message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<int> insertContact(Map contactMap) async { |
|
|
|
|
|
|
|
if (db == null) { |
|
|
|
|
|
|
|
return Future.value(0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
debugPrint("contact_insert: $contactMap"); |
|
|
|
|
|
|
|
return db.insert("Contact", contactMap); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<List<Contact>> queryContact(List<String> userIds) async { |
|
|
|
|
|
|
|
if (db == null) { |
|
|
|
|
|
|
|
return Future.value(<Contact>[]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String sql = 'SELECT * FROM Contact WHERE userId IN (?) AND state = 0 AND isDelete = 0'; |
|
|
|
|
|
|
|
return db.rawQuery(sql, userIds).then((value) { |
|
|
|
|
|
|
|
return value.map((e) => Contact.fromJson(e)).toList(); |
|
|
|
|
|
|
|
}, onError: (error) { |
|
|
|
|
|
|
|
debugPrint("Contact_error: $error"); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<Contact> queryContactById(String userId) async { |
|
|
|
|
|
|
|
if (db == null) { |
|
|
|
|
|
|
|
return Future.value(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<Contact> contact = await db.query("Contact", distinct: true, where: "userId = ?", whereArgs: [userId]) |
|
|
|
|
|
|
|
.then((value) { |
|
|
|
|
|
|
|
return value.map((e) => Contact.fromJson(e)).toList(); |
|
|
|
|
|
|
|
}, onError: (error) { |
|
|
|
|
|
|
|
debugPrint("Contact_error: $error"); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return (contact?.isNotEmpty ?? false) ? contact.first : null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final List<Migration> _migrations = []; |
|
|
|
final List<Migration> _migrations = []; |
|
|
|
|
|
|
|
|
|
|
|
addMigrations(List<Migration> migrations) { |
|
|
|
addMigrations(List<Migration> migrations) { |
|
|
|