···
7
+
_ "github.com/mattn/go-sqlite3"
10
+
type SqliteStore struct {
15
+
type SqliteStoreOpt func(*SqliteStore)
17
+
func WithTableName(name string) SqliteStoreOpt {
18
+
return func(s *SqliteStore) {
23
+
func NewSQLiteStore(dbPath string, opts ...SqliteStoreOpt) (*SqliteStore, error) {
24
+
db, err := sql.Open("sqlite3", dbPath)
26
+
return nil, fmt.Errorf("failed to open sqlite database: %w", err)
29
+
store := &SqliteStore{
31
+
tableName: "cursors",
34
+
for _, o := range opts {
38
+
if err := store.init(); err != nil {
45
+
func (s *SqliteStore) init() error {
46
+
createTable := fmt.Sprintf(`
47
+
create table if not exists %s (
48
+
knot text primary key,
51
+
_, err := s.db.Exec(createTable)
55
+
func (s *SqliteStore) Set(knot string, cursor int64) {
56
+
query := fmt.Sprintf(`
57
+
insert into %s (knot, cursor)
59
+
on conflict(knot) do update set cursor=excluded.cursor;
62
+
_, err := s.db.Exec(query, knot, cursor)
69
+
func (s *SqliteStore) Get(knot string) (cursor int64) {
70
+
query := fmt.Sprintf(`
71
+
select cursor from %s where knot = ?;
73
+
err := s.db.QueryRow(query, knot).Scan(&cursor)
76
+
if err != sql.ErrNoRows {