File
Blob: kv/sqlite3/simple.go
| 1 | package sqlite3 |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | ) |
| 7 | |
| 8 | func (s *SqliteKV) Put(ctx context.Context, key []byte, value []byte) error { |
| 9 | return withWriteTx(ctx, s.writer, func(tx *sql.Tx) error { |
| 10 | _, err := tx.StmtContext(ctx, s.stmts.simplePut).Exec(key, value) |
| 11 | if err != nil { |
| 12 | return err |
| 13 | } |
| 14 | return s.updateKeyTracker(ctx, tx, key, SimpleFlag, 0) |
| 15 | }) |
| 16 | } |
| 17 | |
| 18 | func (s *SqliteKV) Get(ctx context.Context, key []byte) ([]byte, error) { |
| 19 | var value []byte |
| 20 | err := s.stmts.simpleGet.QueryRowContext(ctx, key).Scan(&value) |
| 21 | if err != nil { |
| 22 | if err == sql.ErrNoRows { |
| 23 | return nil, nil |
| 24 | } |
| 25 | return nil, err |
| 26 | } |
| 27 | // Preserve behavior: distinguish between NULL (missing) and empty blob |
| 28 | if value == nil { |
| 29 | value = []byte{} |
| 30 | } |
| 31 | return value, nil |
| 32 | } |
| 33 | |
| 34 | func (s *SqliteKV) Delete(ctx context.Context, key []byte) error { |
| 35 | return withWriteTx(ctx, s.writer, func(tx *sql.Tx) error { |
| 36 | _, err := tx.StmtContext(ctx, s.stmts.simpleDel).Exec(key) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | return s.updateKeyTracker(ctx, tx, key, 0, SimpleFlag) |
| 41 | }) |
| 42 | } |