diff options
Diffstat (limited to 'db.c')
-rw-r--r-- | db.c | 35 |
1 files changed, 26 insertions, 9 deletions
@@ -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; |