2020-02-13 17:27:33 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-09-24 11:10:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-14 09:53:55 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2020-02-13 17:27:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const accountDataSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_account_data_type (
|
2020-02-20 09:28:03 +00:00
|
|
|
id INTEGER PRIMARY KEY,
|
2020-02-13 17:27:33 +00:00
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
room_id TEXT NOT NULL,
|
|
|
|
type TEXT NOT NULL,
|
|
|
|
UNIQUE (user_id, room_id, type)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertAccountDataSQL = "" +
|
|
|
|
"INSERT INTO syncapi_account_data_type (id, user_id, room_id, type) VALUES ($1, $2, $3, $4)" +
|
|
|
|
" ON CONFLICT (user_id, room_id, type) DO UPDATE" +
|
2021-02-04 11:45:49 +00:00
|
|
|
" SET id = $5"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
2022-04-11 08:05:23 +01:00
|
|
|
// further parameters are added by prepareWithFilters
|
2020-02-13 17:27:33 +00:00
|
|
|
const selectAccountDataInRangeSQL = "" +
|
2022-04-26 14:50:56 +01:00
|
|
|
"SELECT id, room_id, type FROM syncapi_account_data_type" +
|
2022-04-11 08:05:23 +01:00
|
|
|
" WHERE user_id = $1 AND id > $2 AND id <= $3"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
|
|
|
const selectMaxAccountDataIDSQL = "" +
|
|
|
|
"SELECT MAX(id) FROM syncapi_account_data_type"
|
|
|
|
|
|
|
|
type accountDataStatements struct {
|
2020-07-21 15:48:21 +01:00
|
|
|
db *sql.DB
|
2022-04-08 17:53:24 +01:00
|
|
|
streamIDStatements *StreamIDStatements
|
2020-02-13 17:27:33 +00:00
|
|
|
insertAccountDataStmt *sql.Stmt
|
|
|
|
selectMaxAccountDataIDStmt *sql.Stmt
|
2020-02-20 09:28:03 +00:00
|
|
|
selectAccountDataInRangeStmt *sql.Stmt
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 17:53:24 +01:00
|
|
|
func NewSqliteAccountDataTable(db *sql.DB, streamID *StreamIDStatements) (tables.AccountData, error) {
|
2020-05-14 09:53:55 +01:00
|
|
|
s := &accountDataStatements{
|
2020-07-21 15:48:21 +01:00
|
|
|
db: db,
|
2020-05-14 09:53:55 +01:00
|
|
|
streamIDStatements: streamID,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(accountDataSchema)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
2020-05-14 09:53:55 +01:00
|
|
|
return nil, err
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
2023-03-23 12:52:53 +00:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertAccountDataStmt, insertAccountDataSQL},
|
|
|
|
{&s.selectMaxAccountDataIDStmt, selectMaxAccountDataIDSQL},
|
|
|
|
{&s.selectAccountDataInRangeStmt, selectAccountDataInRangeSQL},
|
|
|
|
}.Prepare(db)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 09:53:55 +01:00
|
|
|
func (s *accountDataStatements) InsertAccountData(
|
2020-02-13 17:27:33 +00:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
userID, roomID, dataType string,
|
|
|
|
) (pos types.StreamPosition, err error) {
|
2021-01-19 18:00:42 +00:00
|
|
|
pos, err = s.streamIDStatements.nextAccountDataID(ctx, txn)
|
2020-08-21 10:42:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-04 11:45:49 +00:00
|
|
|
_, err = sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, pos, userID, roomID, dataType, pos)
|
2020-08-21 10:42:08 +01:00
|
|
|
return
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 09:53:55 +01:00
|
|
|
func (s *accountDataStatements) SelectAccountDataInRange(
|
2022-09-28 10:18:03 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2020-02-13 17:27:33 +00:00
|
|
|
userID string,
|
2020-05-15 09:41:12 +01:00
|
|
|
r types.Range,
|
2022-04-11 08:05:23 +01:00
|
|
|
filter *gomatrixserverlib.EventFilter,
|
2022-04-26 14:50:56 +01:00
|
|
|
) (data map[string][]string, pos types.StreamPosition, err error) {
|
2020-02-13 17:27:33 +00:00
|
|
|
data = make(map[string][]string)
|
2022-04-11 08:05:23 +01:00
|
|
|
stmt, params, err := prepareWithFilters(
|
2022-09-28 10:18:03 +01:00
|
|
|
s.db, txn, selectAccountDataInRangeSQL,
|
2022-04-11 08:05:23 +01:00
|
|
|
[]interface{}{
|
|
|
|
userID, r.Low(), r.High(),
|
|
|
|
},
|
|
|
|
filter.Senders, filter.NotSenders,
|
|
|
|
filter.Types, filter.NotTypes,
|
2022-04-13 12:16:02 +01:00
|
|
|
[]string{}, nil, filter.Limit, FilterOrderAsc)
|
2023-02-07 13:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-04-11 08:05:23 +01:00
|
|
|
rows, err := stmt.QueryContext(ctx, params...)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountDataInRange: rows.close() failed")
|
2020-02-20 09:28:03 +00:00
|
|
|
|
2022-04-26 14:50:56 +01:00
|
|
|
var dataType string
|
|
|
|
var roomID string
|
2022-04-26 16:07:13 +01:00
|
|
|
var id types.StreamPosition
|
2020-02-13 17:27:33 +00:00
|
|
|
|
2022-04-26 14:50:56 +01:00
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Scan(&id, &roomID, &dataType); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data[roomID]) > 0 {
|
|
|
|
data[roomID] = append(data[roomID], dataType)
|
|
|
|
} else {
|
|
|
|
data[roomID] = []string{dataType}
|
|
|
|
}
|
2022-04-27 14:53:11 +01:00
|
|
|
if id > pos {
|
|
|
|
pos = id
|
2022-04-26 14:50:56 +01:00
|
|
|
}
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
2022-04-27 14:53:11 +01:00
|
|
|
if pos == 0 {
|
|
|
|
pos = r.High()
|
2022-04-27 14:45:51 +01:00
|
|
|
}
|
2022-04-26 14:50:56 +01:00
|
|
|
return data, pos, nil
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 09:53:55 +01:00
|
|
|
func (s *accountDataStatements) SelectMaxAccountDataID(
|
2020-02-13 17:27:33 +00:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
) (id int64, err error) {
|
|
|
|
var nullableID sql.NullInt64
|
2020-09-24 11:10:14 +01:00
|
|
|
err = sqlutil.TxStmt(txn, s.selectMaxAccountDataIDStmt).QueryRowContext(ctx).Scan(&nullableID)
|
2020-02-13 17:27:33 +00:00
|
|
|
if nullableID.Valid {
|
|
|
|
id = nullableID.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|