2020-02-13 18:27:33 +01:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const streamIDTableSchema = `
|
|
|
|
-- Global stream ID counter, used by other tables.
|
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_stream_id (
|
|
|
|
stream_name TEXT NOT NULL PRIMARY KEY,
|
|
|
|
stream_id INT DEFAULT 0,
|
|
|
|
|
|
|
|
UNIQUE(stream_name)
|
|
|
|
);
|
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("global", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2020-12-11 15:02:17 +01:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("receipt", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2021-01-19 19:00:42 +01:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("accountdata", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("invite", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2022-04-06 13:11:19 +02:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("presence", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2020-02-13 18:27:33 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const increaseStreamIDStmt = "" +
|
2022-03-17 18:05:21 +01:00
|
|
|
"UPDATE syncapi_stream_id SET stream_id = stream_id + 1 WHERE stream_name = $1" +
|
|
|
|
" RETURNING stream_id"
|
2020-02-13 18:27:33 +01:00
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
type StreamIDStatements struct {
|
2020-07-21 16:48:21 +02:00
|
|
|
db *sql.DB
|
2020-02-13 18:27:33 +01:00
|
|
|
increaseStreamIDStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) Prepare(db *sql.DB) (err error) {
|
2020-07-21 16:48:21 +02:00
|
|
|
s.db = db
|
2020-02-13 18:27:33 +01:00
|
|
|
_, err = db.Exec(streamIDTableSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.increaseStreamIDStmt, err = db.Prepare(increaseStreamIDStmt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) nextPDUID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2020-06-12 15:55:57 +02:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 18:05:21 +01:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "global").Scan(&pos)
|
2020-02-13 18:27:33 +01:00
|
|
|
return
|
|
|
|
}
|
2020-12-11 15:02:17 +01:00
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) nextReceiptID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2020-12-11 15:02:17 +01:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 18:05:21 +01:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "receipt").Scan(&pos)
|
2020-12-11 15:02:17 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-19 19:00:42 +01:00
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) nextInviteID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2021-01-19 19:00:42 +01:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 18:05:21 +01:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "invite").Scan(&pos)
|
2021-01-19 19:00:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) nextAccountDataID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2021-01-19 19:00:42 +01:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 18:05:21 +01:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "accountdata").Scan(&pos)
|
2021-01-19 19:00:42 +01:00
|
|
|
return
|
|
|
|
}
|
2022-04-06 13:11:19 +02:00
|
|
|
|
2022-04-08 18:53:24 +02:00
|
|
|
func (s *StreamIDStatements) nextPresenceID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2022-04-06 13:11:19 +02:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
err = increaseStmt.QueryRowContext(ctx, "presence").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|