2020-02-13 11:27:33 -06: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 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-09-24 05:10:14 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-14 03:53:55 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2020-02-13 11:27:33 -06: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 03:28:03 -06:00
|
|
|
id INTEGER PRIMARY KEY,
|
2020-02-13 11:27:33 -06: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 05:45:49 -06:00
|
|
|
" SET id = $5"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2022-04-11 02:05:23 -05:00
|
|
|
// further parameters are added by prepareWithFilters
|
2020-02-13 11:27:33 -06:00
|
|
|
const selectAccountDataInRangeSQL = "" +
|
2022-04-26 08:50:56 -05:00
|
|
|
"SELECT id, room_id, type FROM syncapi_account_data_type" +
|
2022-04-11 02:05:23 -05:00
|
|
|
" WHERE user_id = $1 AND id > $2 AND id <= $3"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
|
|
|
const selectMaxAccountDataIDSQL = "" +
|
|
|
|
"SELECT MAX(id) FROM syncapi_account_data_type"
|
|
|
|
|
|
|
|
type accountDataStatements struct {
|
2020-07-21 09:48:21 -05:00
|
|
|
db *sql.DB
|
2022-04-08 11:53:24 -05:00
|
|
|
streamIDStatements *StreamIDStatements
|
2020-02-13 11:27:33 -06:00
|
|
|
insertAccountDataStmt *sql.Stmt
|
|
|
|
selectMaxAccountDataIDStmt *sql.Stmt
|
2020-02-20 03:28:03 -06:00
|
|
|
selectAccountDataInRangeStmt *sql.Stmt
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func NewSqliteAccountDataTable(db *sql.DB, streamID *StreamIDStatements) (tables.AccountData, error) {
|
2020-05-14 03:53:55 -05:00
|
|
|
s := &accountDataStatements{
|
2020-07-21 09:48:21 -05:00
|
|
|
db: db,
|
2020-05-14 03:53:55 -05:00
|
|
|
streamIDStatements: streamID,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(accountDataSchema)
|
2020-02-13 11:27:33 -06:00
|
|
|
if err != nil {
|
2020-05-14 03:53:55 -05:00
|
|
|
return nil, err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
|
2020-05-14 03:53:55 -05:00
|
|
|
return nil, err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
2020-02-20 03:28:03 -06:00
|
|
|
if s.selectMaxAccountDataIDStmt, err = db.Prepare(selectMaxAccountDataIDSQL); err != nil {
|
2020-05-14 03:53:55 -05:00
|
|
|
return nil, err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
2020-02-20 03:28:03 -06:00
|
|
|
if s.selectAccountDataInRangeStmt, err = db.Prepare(selectAccountDataInRangeSQL); err != nil {
|
2020-05-14 03:53:55 -05:00
|
|
|
return nil, err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
2020-05-14 03:53:55 -05:00
|
|
|
return s, nil
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:53:55 -05:00
|
|
|
func (s *accountDataStatements) InsertAccountData(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
userID, roomID, dataType string,
|
|
|
|
) (pos types.StreamPosition, err error) {
|
2021-01-19 12:00:42 -06:00
|
|
|
pos, err = s.streamIDStatements.nextAccountDataID(ctx, txn)
|
2020-08-21 04:42:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-04 05:45:49 -06:00
|
|
|
_, err = sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, pos, userID, roomID, dataType, pos)
|
2020-08-21 04:42:08 -05:00
|
|
|
return
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:53:55 -05:00
|
|
|
func (s *accountDataStatements) SelectAccountDataInRange(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
2020-05-15 03:41:12 -05:00
|
|
|
r types.Range,
|
2022-04-11 02:05:23 -05:00
|
|
|
filter *gomatrixserverlib.EventFilter,
|
2022-04-26 08:50:56 -05:00
|
|
|
) (data map[string][]string, pos types.StreamPosition, err error) {
|
2020-02-13 11:27:33 -06:00
|
|
|
data = make(map[string][]string)
|
2022-04-11 02:05:23 -05:00
|
|
|
stmt, params, err := prepareWithFilters(
|
|
|
|
s.db, nil, selectAccountDataInRangeSQL,
|
|
|
|
[]interface{}{
|
|
|
|
userID, r.Low(), r.High(),
|
|
|
|
},
|
|
|
|
filter.Senders, filter.NotSenders,
|
|
|
|
filter.Types, filter.NotTypes,
|
2022-04-13 06:16:02 -05:00
|
|
|
[]string{}, nil, filter.Limit, FilterOrderAsc)
|
2022-04-11 02:05:23 -05:00
|
|
|
|
|
|
|
rows, err := stmt.QueryContext(ctx, params...)
|
2020-02-13 11:27:33 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountDataInRange: rows.close() failed")
|
2020-02-20 03:28:03 -06:00
|
|
|
|
2022-04-26 08:50:56 -05:00
|
|
|
var dataType string
|
|
|
|
var roomID string
|
|
|
|
var id types.StreamPosition
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2022-04-26 08:50:56 -05:00
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Scan(&id, &roomID, &dataType); err != nil {
|
2020-02-13 11:27:33 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data[roomID]) > 0 {
|
|
|
|
data[roomID] = append(data[roomID], dataType)
|
|
|
|
} else {
|
|
|
|
data[roomID] = []string{dataType}
|
|
|
|
}
|
2022-04-26 08:50:56 -05:00
|
|
|
if id > pos {
|
|
|
|
pos = id
|
|
|
|
}
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2022-04-26 08:50:56 -05:00
|
|
|
return data, pos, nil
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:53:55 -05:00
|
|
|
func (s *accountDataStatements) SelectMaxAccountDataID(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
) (id int64, err error) {
|
|
|
|
var nullableID sql.NullInt64
|
2020-09-24 05:10:14 -05:00
|
|
|
err = sqlutil.TxStmt(txn, s.selectMaxAccountDataIDStmt).QueryRowContext(ctx).Scan(&nullableID)
|
2020-02-13 11:27:33 -06:00
|
|
|
if nullableID.Valid {
|
|
|
|
id = nullableID.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|