2020-02-13 17:27:33 +00:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-13 17:27:33 +00: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 14:02:17 +00:00
|
|
|
INSERT INTO syncapi_stream_id (stream_name, stream_id) VALUES ("receipt", 0)
|
|
|
|
ON CONFLICT DO NOTHING;
|
2021-01-19 18:00:42 +00: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;
|
2020-02-13 17:27:33 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const increaseStreamIDStmt = "" +
|
|
|
|
"UPDATE syncapi_stream_id SET stream_id = stream_id + 1 WHERE stream_name = $1"
|
|
|
|
|
|
|
|
const selectStreamIDStmt = "" +
|
|
|
|
"SELECT stream_id FROM syncapi_stream_id WHERE stream_name = $1"
|
|
|
|
|
|
|
|
type streamIDStatements struct {
|
2020-07-21 14:48:21 +00:00
|
|
|
db *sql.DB
|
2020-02-13 17:27:33 +00:00
|
|
|
increaseStreamIDStmt *sql.Stmt
|
|
|
|
selectStreamIDStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamIDStatements) prepare(db *sql.DB) (err error) {
|
2020-07-21 14:48:21 +00:00
|
|
|
s.db = db
|
2020-02-13 17:27:33 +00:00
|
|
|
_, err = db.Exec(streamIDTableSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.increaseStreamIDStmt, err = db.Prepare(increaseStreamIDStmt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.selectStreamIDStmt, err = db.Prepare(selectStreamIDStmt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:00:42 +00:00
|
|
|
func (s *streamIDStatements) nextPDUID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
2020-06-12 13:55:57 +00:00
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
selectStmt := sqlutil.TxStmt(txn, s.selectStreamIDStmt)
|
2020-08-21 09:42:08 +00:00
|
|
|
if _, err = increaseStmt.ExecContext(ctx, "global"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = selectStmt.QueryRowContext(ctx, "global").Scan(&pos)
|
2020-02-13 17:27:33 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-11 14:02:17 +00:00
|
|
|
|
|
|
|
func (s *streamIDStatements) nextReceiptID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
selectStmt := sqlutil.TxStmt(txn, s.selectStreamIDStmt)
|
|
|
|
if _, err = increaseStmt.ExecContext(ctx, "receipt"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = selectStmt.QueryRowContext(ctx, "receipt").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|
2021-01-19 18:00:42 +00:00
|
|
|
|
|
|
|
func (s *streamIDStatements) nextInviteID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
selectStmt := sqlutil.TxStmt(txn, s.selectStreamIDStmt)
|
|
|
|
if _, err = increaseStmt.ExecContext(ctx, "invite"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = selectStmt.QueryRowContext(ctx, "invite").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamIDStatements) nextAccountDataID(ctx context.Context, txn *sql.Tx) (pos types.StreamPosition, err error) {
|
|
|
|
increaseStmt := sqlutil.TxStmt(txn, s.increaseStreamIDStmt)
|
|
|
|
selectStmt := sqlutil.TxStmt(txn, s.selectStreamIDStmt)
|
|
|
|
if _, err = increaseStmt.ExecContext(ctx, "accountdata"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = selectStmt.QueryRowContext(ctx, "accountdata").Scan(&pos)
|
|
|
|
return
|
|
|
|
}
|