2020-02-13 11:27:33 -06:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-13 11:27:33 -06: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 08:02:17 -06:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("receipt", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2021-01-19 12:00:42 -06: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 06:11:19 -05:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("presence", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2022-08-05 06:44:20 -05:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("notification", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2022-10-13 08:50:52 -05:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("relation", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2020-02-13 11:27:33 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
const increaseStreamIDStmt = "" +
|
2022-03-17 12:05:21 -05:00
|
|
|
"UPDATE syncapi_stream_id SET stream_id = stream_id + 1 WHERE stream_name = $1" +
|
|
|
|
" RETURNING stream_id"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
type StreamIDStatements struct {
|
2020-07-21 09:48:21 -05:00
|
|
|
db *sql.DB
|
2020-02-13 11:27:33 -06:00
|
|
|
increaseStreamIDStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) Prepare(db *sql.DB) (err error) {
|
2020-07-21 09:48:21 -05:00
|
|
|
s.db = db
|
2020-02-13 11:27:33 -06:00
|
|
|
_, err = db.Exec(streamIDTableSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 07:52:53 -05:00
|
|
|
return sqlutil.StatementList{
|
|
|
|
{&s.increaseStreamIDStmt, increaseStreamIDStmt},
|
|
|
|
}.Prepare(db)
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) nextPDUID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2020-06-12 08:55:57 -05:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 12:05:21 -05:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "global").Scan(&pos)
|
2020-02-13 11:27:33 -06:00
|
|
|
return
|
|
|
|
}
|
2020-12-11 08:02:17 -06:00
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) nextReceiptID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2020-12-11 08:02:17 -06:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 12:05:21 -05:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "receipt").Scan(&pos)
|
2020-12-11 08:02:17 -06:00
|
|
|
return
|
|
|
|
}
|
2021-01-19 12:00:42 -06:00
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) nextInviteID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2021-01-19 12:00:42 -06:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 12:05:21 -05:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "invite").Scan(&pos)
|
2021-01-19 12:00:42 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) nextAccountDataID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2021-01-19 12:00:42 -06:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
2022-03-17 12:05:21 -05:00
|
|
|
err = increaseStmt.QueryRowContext(ctx, "accountdata").Scan(&pos)
|
2021-01-19 12:00:42 -06:00
|
|
|
return
|
|
|
|
}
|
2022-04-06 06:11:19 -05:00
|
|
|
|
2022-04-08 11:53:24 -05:00
|
|
|
func (s *StreamIDStatements) nextPresenceID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2022-04-06 06:11:19 -05:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
err = increaseStmt.QueryRowContext(ctx, "presence").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|
2022-08-05 06:44:20 -05:00
|
|
|
|
|
|
|
func (s *StreamIDStatements) nextNotificationID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
err = increaseStmt.QueryRowContext(ctx, "notification").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|
2022-10-13 08:50:52 -05:00
|
|
|
|
|
|
|
func (s *StreamIDStatements) nextRelationID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
err = increaseStmt.QueryRowContext(ctx, "relation").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|