2020-02-13 18:27:33 +01: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-08-19 16:38:27 +02:00
|
|
|
"fmt"
|
2020-02-13 18:27:33 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-26 16:42:42 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const eventTypesSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_event_types (
|
|
|
|
event_type_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
event_type TEXT NOT NULL UNIQUE
|
|
|
|
);
|
|
|
|
INSERT INTO roomserver_event_types (event_type_nid, event_type) VALUES
|
|
|
|
(1, 'm.room.create'),
|
|
|
|
(2, 'm.room.power_levels'),
|
|
|
|
(3, 'm.room.join_rules'),
|
|
|
|
(4, 'm.room.third_party_invite'),
|
|
|
|
(5, 'm.room.member'),
|
|
|
|
(6, 'm.room.redaction'),
|
|
|
|
(7, 'm.room.history_visibility') ON CONFLICT DO NOTHING;
|
|
|
|
`
|
|
|
|
|
|
|
|
// Assign a new numeric event type ID.
|
|
|
|
// The usual case is that the event type is not in the database.
|
|
|
|
// In that case the ID will be assigned using the next value from the sequence.
|
|
|
|
// We use `RETURNING` to tell postgres to return the assigned ID.
|
|
|
|
// But it's possible that the type was added in a query that raced with us.
|
|
|
|
// This will result in a conflict on the event_type_unique constraint, in this
|
|
|
|
// case we do nothing. Postgresql won't return a row in that case so we rely on
|
|
|
|
// the caller catching the sql.ErrNoRows error and running a select to get the row.
|
|
|
|
// We could get postgresql to return the row on a conflict by updating the row
|
|
|
|
// but it doesn't seem like a good idea to modify the rows just to make postgresql
|
|
|
|
// return it. Modifying the rows will cause postgres to assign a new tuple for the
|
|
|
|
// row even though the data doesn't change resulting in unncesssary modifications
|
|
|
|
// to the indexes.
|
|
|
|
const insertEventTypeNIDSQL = `
|
|
|
|
INSERT INTO roomserver_event_types (event_type) VALUES ($1)
|
2022-03-17 19:24:27 +01:00
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
RETURNING event_type_nid;
|
2020-02-13 18:27:33 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectEventTypeNIDSQL = `
|
|
|
|
SELECT event_type_nid FROM roomserver_event_types WHERE event_type = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
// Bulk lookup from string event type to numeric ID for that event type.
|
|
|
|
// Takes an array of strings as the query parameter.
|
|
|
|
const bulkSelectEventTypeNIDSQL = `
|
|
|
|
SELECT event_type, event_type_nid FROM roomserver_event_types
|
|
|
|
WHERE event_type IN ($1)
|
|
|
|
`
|
|
|
|
|
|
|
|
type eventTypeStatements struct {
|
2022-03-17 19:24:27 +01:00
|
|
|
db *sql.DB
|
|
|
|
insertEventTypeNIDStmt *sql.Stmt
|
|
|
|
selectEventTypeNIDStmt *sql.Stmt
|
|
|
|
bulkSelectEventTypeNIDStmt *sql.Stmt
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 15:30:32 +02:00
|
|
|
func CreateEventTypesTable(db *sql.DB) error {
|
2021-04-26 14:25:57 +02:00
|
|
|
_, err := db.Exec(eventTypesSchema)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-09 15:30:32 +02:00
|
|
|
func PrepareEventTypesTable(db *sql.DB) (tables.EventTypes, error) {
|
2020-07-21 11:48:49 +02:00
|
|
|
s := &eventTypeStatements{
|
2020-08-19 16:38:27 +02:00
|
|
|
db: db,
|
2020-07-21 11:48:49 +02:00
|
|
|
}
|
2020-02-13 18:27:33 +01:00
|
|
|
|
2021-07-28 19:30:04 +02:00
|
|
|
return s, sqlutil.StatementList{
|
2020-02-13 18:27:33 +01:00
|
|
|
{&s.insertEventTypeNIDStmt, insertEventTypeNIDSQL},
|
|
|
|
{&s.selectEventTypeNIDStmt, selectEventTypeNIDSQL},
|
|
|
|
{&s.bulkSelectEventTypeNIDStmt, bulkSelectEventTypeNIDSQL},
|
2020-05-27 12:03:47 +02:00
|
|
|
}.Prepare(db)
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventTypeStatements) InsertEventTypeNID(
|
2020-08-19 16:38:27 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, eventType string,
|
2022-03-17 19:24:27 +01:00
|
|
|
) (eventTypeNID types.EventTypeNID, err error) {
|
2020-08-19 16:38:27 +02:00
|
|
|
insertStmt := sqlutil.TxStmt(txn, s.insertEventTypeNIDStmt)
|
2022-03-17 19:24:27 +01:00
|
|
|
if err = insertStmt.QueryRowContext(ctx, eventType).Scan(&eventTypeNID); err != nil {
|
2020-08-19 16:38:27 +02:00
|
|
|
return 0, fmt.Errorf("resultStmt.QueryRowContext.Scan: %w", err)
|
|
|
|
}
|
2022-03-17 19:24:27 +01:00
|
|
|
return
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventTypeStatements) SelectEventTypeNID(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, tx *sql.Tx, eventType string,
|
|
|
|
) (types.EventTypeNID, error) {
|
|
|
|
var eventTypeNID int64
|
2020-06-12 15:55:57 +02:00
|
|
|
selectStmt := sqlutil.TxStmt(tx, s.selectEventTypeNIDStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
err := selectStmt.QueryRowContext(ctx, eventType).Scan(&eventTypeNID)
|
|
|
|
return types.EventTypeNID(eventTypeNID), err
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (s *eventTypeStatements) BulkSelectEventTypeNID(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventTypes []string,
|
2020-02-13 18:27:33 +01:00
|
|
|
) (map[string]types.EventTypeNID, error) {
|
|
|
|
///////////////
|
|
|
|
iEventTypes := make([]interface{}, len(eventTypes))
|
|
|
|
for k, v := range eventTypes {
|
|
|
|
iEventTypes[k] = v
|
|
|
|
}
|
2020-06-12 15:55:57 +02:00
|
|
|
selectOrig := strings.Replace(bulkSelectEventTypeNIDSQL, "($1)", sqlutil.QueryVariadic(len(iEventTypes)), 1)
|
2020-02-13 18:27:33 +01:00
|
|
|
selectPrep, err := s.db.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-24 11:03:22 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, selectPrep, "selectPrep.close() failed")
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, selectPrep)
|
2020-02-13 18:27:33 +01:00
|
|
|
///////////////
|
|
|
|
|
2022-02-04 11:39:34 +01:00
|
|
|
rows, err := stmt.QueryContext(ctx, iEventTypes...)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventTypeNID: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
result := make(map[string]types.EventTypeNID, len(eventTypes))
|
2022-05-09 15:30:32 +02:00
|
|
|
var eventType string
|
|
|
|
var eventTypeNID int64
|
2020-02-13 18:27:33 +01:00
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Scan(&eventType, &eventTypeNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[eventType] = types.EventTypeNID(eventTypeNID)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|