aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/db.c b/db.c
index a193b24..f48148e 100644
--- a/db.c
+++ b/db.c
@@ -304,18 +304,35 @@ void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){
}
struct db_message_list db_get_messages(i64 roomid,i64 count){
+ return db_get_messages_before(roomid,count,-1);
+}
+
+struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid){
assert(count>=0);
sqlite3_stmt *stmt;
- SQLITE(prepare_v2,database,
- "select id, user, time, message "
- "from Messages "
- "where room = ? "
- "order by time desc "
- "limit ?"
- ,-1,&stmt,NULL);
- SQLITE(bind_int64,stmt,1,roomid);
- SQLITE(bind_int64,stmt,2,count);
+ if(beforeid<0){
+ SQLITE(prepare_v2,database,
+ "select id, user, time, message "
+ "from Messages "
+ "where room = ? "
+ "order by time desc "
+ "limit ?"
+ ,-1,&stmt,NULL);
+ SQLITE(bind_int64,stmt,1,roomid);
+ SQLITE(bind_int64,stmt,2,count);
+ } else {
+ SQLITE(prepare_v2,database,
+ "select id, user, time, message "
+ "from Messages "
+ "where room = ? and time < (select time from Messages where id = ?) "
+ "order by time desc "
+ "limit ?"
+ ,-1,&stmt,NULL);
+ SQLITE(bind_int64,stmt,1,roomid);
+ SQLITE(bind_int64,stmt,2,beforeid);
+ SQLITE(bind_int64,stmt,3,count);
+ }
struct db_message_list ml;
i64 cap=count;