From 83f038e12ba0bb727ff89e1d719d105520f7152c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20S=C3=B6lver?= Date: Mon, 27 Jul 2020 14:19:30 +0200 Subject: [PATCH] Don't use more than 999 variables in SQLite querys. (#1224) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1223 Signed-off-by: Henrik Sölver Co-authored-by: Neil Alexander --- .../sqlite3/current_room_state_table.go | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/syncapi/storage/sqlite3/current_room_state_table.go b/syncapi/storage/sqlite3/current_room_state_table.go index 08b42f5b4..6edc99aa0 100644 --- a/syncapi/storage/sqlite3/current_room_state_table.go +++ b/syncapi/storage/sqlite3/current_room_state_table.go @@ -243,6 +243,13 @@ func (s *currentRoomStateStatements) UpsertRoomState( }) } +func minOfInts(a, b int) int { + if a <= b { + return a + } + return b +} + func (s *currentRoomStateStatements) SelectEventsWithEventIDs( ctx context.Context, txn *sql.Tx, eventIDs []string, ) ([]types.StreamEvent, error) { @@ -250,13 +257,24 @@ func (s *currentRoomStateStatements) SelectEventsWithEventIDs( for k, v := range eventIDs { iEventIDs[k] = v } - query := strings.Replace(selectEventsWithEventIDsSQL, "($1)", sqlutil.QueryVariadic(len(iEventIDs)), 1) - rows, err := txn.QueryContext(ctx, query, iEventIDs...) - if err != nil { - return nil, err + res := make([]types.StreamEvent, 0, len(eventIDs)) + var start int + for start < len(eventIDs) { + n := minOfInts(len(eventIDs)-start, 999) + query := strings.Replace(selectEventsWithEventIDsSQL, "($1)", sqlutil.QueryVariadic(n), 1) + rows, err := txn.QueryContext(ctx, query, iEventIDs[start:start+n]...) + if err != nil { + return nil, err + } + start = start + n + events, err := rowsToStreamEvents(rows) + internal.CloseAndLogIfError(ctx, rows, "selectEventsWithEventIDs: rows.close() failed") + if err != nil { + return nil, err + } + res = append(res, events...) } - defer internal.CloseAndLogIfError(ctx, rows, "selectEventsWithEventIDs: rows.close() failed") - return rowsToStreamEvents(rows) + return res, nil } func rowsToEvents(rows *sql.Rows) ([]gomatrixserverlib.HeaderedEvent, error) {