|
|
@ -65,11 +65,26 @@ class HxDatabase { |
|
|
|
return (messages?.isNotEmpty ?? false) ? messages.first : null; |
|
|
|
return (messages?.isNotEmpty ?? false) ? messages.first : null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Future<List<Message>> queryList(userId) async{ |
|
|
|
Future<List<Message>> queryList() async{ |
|
|
|
await _dbIsOpen(); |
|
|
|
await _dbIsOpen(); |
|
|
|
String sql = |
|
|
|
String sql = '''SELECT * |
|
|
|
'SELECT * FROM (SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC) mm GROUP BY mm.toId,mm.fromId'; |
|
|
|
FROM `Message` |
|
|
|
return db.rawQuery(sql, [userId, userId]).then((value) { |
|
|
|
WHERE ROWID IN ( |
|
|
|
|
|
|
|
SELECT ROWID |
|
|
|
|
|
|
|
FROM ( |
|
|
|
|
|
|
|
SELECT ROWID, `fromId`, `toId`, MAX(`time`) AS max_time |
|
|
|
|
|
|
|
FROM `Message` |
|
|
|
|
|
|
|
GROUP BY `fromId` || '_' || `toId`, `toId` || '_' || `fromId` |
|
|
|
|
|
|
|
) AS grouped_messages |
|
|
|
|
|
|
|
WHERE max_time = ( |
|
|
|
|
|
|
|
SELECT MAX(`time`) |
|
|
|
|
|
|
|
FROM `Message` |
|
|
|
|
|
|
|
WHERE (`fromId` = grouped_messages.`fromId` AND `toId` = grouped_messages.`toId`) |
|
|
|
|
|
|
|
OR (`fromId` = grouped_messages.`toId` AND `toId` = grouped_messages.`fromId`) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
ORDER BY `time` DESC;'''; |
|
|
|
|
|
|
|
return db.rawQuery(sql).then((value) { |
|
|
|
return value.map((e) { |
|
|
|
return value.map((e) { |
|
|
|
debugPrint("Message: ${e}"); |
|
|
|
debugPrint("Message: ${e}"); |
|
|
|
return Message.fromJson(e); |
|
|
|
return Message.fromJson(e); |
|
|
|