aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-07-05 22:28:51 +0200
committertomsmeding <tom.smeding@gmail.com>2017-07-05 22:28:51 +0200
commit4b4b42877fc81ad055f5326185e80530d23361e7 (patch)
tree5fcda074409854f921ca83f07bda5ae60b62d1d3 /db.c
parent7c30c3f9c9312500bbb86a98f81ce15982b6907a (diff)
server: Get history before id
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;