2017-07-26 08:53:11 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
package sqlite3
|
2017-07-26 08:53:11 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-18 08:15:27 -05:00
|
|
|
"context"
|
2017-07-26 08:53:11 -05:00
|
|
|
"database/sql"
|
2020-06-18 12:36:03 -05:00
|
|
|
"encoding/json"
|
2020-09-24 05:10:14 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-02-18 07:51:59 -06:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2022-11-04 07:09:49 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-07-26 08:53:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const accountDataSchema = `
|
|
|
|
-- Stores data about accounts data.
|
2022-10-18 09:59:08 -05:00
|
|
|
CREATE TABLE IF NOT EXISTS userapi_account_datas (
|
2017-07-26 08:53:11 -05:00
|
|
|
-- The Matrix user ID localpart for this account
|
|
|
|
localpart TEXT NOT NULL,
|
2022-11-04 06:09:43 -05:00
|
|
|
server_name TEXT NOT NULL,
|
2017-07-26 08:53:11 -05:00
|
|
|
-- The room ID for this data (empty string if not specific to a room)
|
|
|
|
room_id TEXT,
|
|
|
|
-- The account data type
|
|
|
|
type TEXT NOT NULL,
|
|
|
|
-- The account data content
|
|
|
|
content TEXT NOT NULL,
|
|
|
|
|
2022-11-07 10:52:28 -06:00
|
|
|
PRIMARY KEY(localpart, server_name, room_id, type)
|
2017-07-26 08:53:11 -05:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertAccountDataSQL = `
|
2022-11-04 07:09:49 -05:00
|
|
|
INSERT INTO userapi_account_datas(localpart, server_name, room_id, type, content) VALUES($1, $2, $3, $4, $5)
|
2022-11-08 04:27:01 -06:00
|
|
|
ON CONFLICT (localpart, server_name, room_id, type) DO UPDATE SET content = $5
|
2017-07-26 08:53:11 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectAccountDataSQL = "" +
|
2022-11-04 07:09:49 -05:00
|
|
|
"SELECT room_id, type, content FROM userapi_account_datas WHERE localpart = $1 AND server_name = $2"
|
2017-07-26 08:53:11 -05:00
|
|
|
|
2017-08-02 10:21:35 -05:00
|
|
|
const selectAccountDataByTypeSQL = "" +
|
2022-11-04 07:09:49 -05:00
|
|
|
"SELECT content FROM userapi_account_datas WHERE localpart = $1 AND server_name = $2 AND room_id = $3 AND type = $4"
|
2017-08-02 10:21:35 -05:00
|
|
|
|
2017-07-26 08:53:11 -05:00
|
|
|
type accountDataStatements struct {
|
2020-07-21 09:48:21 -05:00
|
|
|
db *sql.DB
|
2017-08-02 10:21:35 -05:00
|
|
|
insertAccountDataStmt *sql.Stmt
|
|
|
|
selectAccountDataStmt *sql.Stmt
|
|
|
|
selectAccountDataByTypeStmt *sql.Stmt
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
func NewSQLiteAccountDataTable(db *sql.DB) (tables.AccountDataTable, error) {
|
|
|
|
s := &accountDataStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(accountDataSchema)
|
2017-07-26 08:53:11 -05:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, err
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
return s, sqlutil.StatementList{
|
2021-07-28 12:30:04 -05:00
|
|
|
{&s.insertAccountDataStmt, insertAccountDataSQL},
|
|
|
|
{&s.selectAccountDataStmt, selectAccountDataSQL},
|
|
|
|
{&s.selectAccountDataByTypeStmt, selectAccountDataByTypeSQL},
|
|
|
|
}.Prepare(db)
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
func (s *accountDataStatements) InsertAccountData(
|
2022-11-04 07:09:49 -05:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string, content json.RawMessage,
|
2020-09-01 05:28:35 -05:00
|
|
|
) error {
|
2022-11-04 07:09:49 -05:00
|
|
|
_, err := sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, localpart, serverName, roomID, dataType, content)
|
2020-09-01 05:28:35 -05:00
|
|
|
return err
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
func (s *accountDataStatements) SelectAccountData(
|
2022-11-04 07:09:49 -05:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2017-09-18 08:15:27 -05:00
|
|
|
) (
|
2020-06-18 12:36:03 -05:00
|
|
|
/* global */ map[string]json.RawMessage,
|
|
|
|
/* rooms */ map[string]map[string]json.RawMessage,
|
|
|
|
error,
|
2017-07-26 08:53:11 -05:00
|
|
|
) {
|
2022-11-04 07:09:49 -05:00
|
|
|
rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart, serverName)
|
2017-07-26 08:53:11 -05:00
|
|
|
if err != nil {
|
2020-06-18 12:36:03 -05:00
|
|
|
return nil, nil, err
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:36:03 -05:00
|
|
|
global := map[string]json.RawMessage{}
|
|
|
|
rooms := map[string]map[string]json.RawMessage{}
|
2017-07-26 08:53:11 -05:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var roomID string
|
|
|
|
var dataType string
|
|
|
|
var content []byte
|
|
|
|
|
2017-09-18 08:15:27 -05:00
|
|
|
if err = rows.Scan(&roomID, &dataType, &content); err != nil {
|
2020-06-18 12:36:03 -05:00
|
|
|
return nil, nil, err
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:36:03 -05:00
|
|
|
if roomID != "" {
|
|
|
|
if _, ok := rooms[roomID]; !ok {
|
|
|
|
rooms[roomID] = map[string]json.RawMessage{}
|
|
|
|
}
|
|
|
|
rooms[roomID][dataType] = content
|
2017-07-26 08:53:11 -05:00
|
|
|
} else {
|
2020-06-18 12:36:03 -05:00
|
|
|
global[dataType] = content
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2020-06-18 12:36:03 -05:00
|
|
|
return global, rooms, nil
|
2017-07-26 08:53:11 -05:00
|
|
|
}
|
2017-08-02 10:21:35 -05:00
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
func (s *accountDataStatements) SelectAccountDataByType(
|
2022-11-04 07:09:49 -05:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string,
|
2020-06-18 12:36:03 -05:00
|
|
|
) (data json.RawMessage, err error) {
|
|
|
|
var bytes []byte
|
2017-09-18 08:15:27 -05:00
|
|
|
stmt := s.selectAccountDataByTypeStmt
|
2022-11-04 07:09:49 -05:00
|
|
|
if err = stmt.QueryRowContext(ctx, localpart, serverName, roomID, dataType).Scan(&bytes); err != nil {
|
2019-10-07 07:15:58 -05:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-08-02 10:21:35 -05:00
|
|
|
return
|
|
|
|
}
|
2020-06-18 12:36:03 -05:00
|
|
|
data = json.RawMessage(bytes)
|
2017-08-02 10:21:35 -05:00
|
|
|
return
|
|
|
|
}
|