Documentation
¶
Overview ¶
Package sql is a database/sql-backed dict driver.
Supports two drivers out of the box, selected via Config.Settings:
driver: "sqlite" — modernc.org/sqlite (pure Go, no CGO) driver: "postgres" — github.com/jackc/pgx/v5/stdlib (pure Go)
Both are statically compiled in so the choice is runtime-config-only, per yarilo's config-not-binary rule.
Schema:
CREATE TABLE <table> (
namespace TEXT NOT NULL,
k TEXT NOT NULL,
v BLOB NOT NULL,
expires BIGINT, -- unix seconds; NULL = no TTL
PRIMARY KEY (namespace, k)
);
CREATE INDEX <table>_expires_idx ON <table>(expires) WHERE expires IS NOT NULL;
"namespace" is the per-Dict prefix (matches Redis driver's prefix concept) — lets multiple yarilo features share one database without key collisions. ExpireScan runs `DELETE FROM <table> WHERE expires IS NOT NULL AND expires <= ?` and reports the row count via the driver's slog logger.
Multi-value: not stored. A second Set on the same key overwrites. Use SQL directly for any feature that genuinely needs multi-value.
Transactions: native database/sql transactions. AtomicInc uses UPDATE ... SET v = CAST(CAST(v AS BIGINT) + ? AS BLOB) WHERE k=? and reports CommitNotFound when zero rows were affected.
Index ¶
- func New(cfg dict.Config) (dict.Dict, error)
- type Dict
- func (d *Dict) Begin(ctx context.Context, set *dict.OpSettings) (dict.Tx, error)
- func (d *Dict) Close() error
- func (d *Dict) ExpireScan(ctx context.Context) error
- func (d *Dict) Iterate(ctx context.Context, _ *dict.OpSettings, path string, flags dict.IterFlag) (dict.Iterator, error)
- func (d *Dict) Lookup(ctx context.Context, _ *dict.OpSettings, key string) ([][]byte, bool, error)
- func (d *Dict) Name() string
- func (d *Dict) Wait(_ context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New constructs an sql dict. Recognised settings:
"driver" string — "sqlite" | "postgres" (required) "dsn" string — go-database/sql DSN (required) "table" string — table name; default "dict_kv" "namespace" string — per-Dict prefix; default ""
The table is auto-created on Open if it does not exist. To opt out of auto-create (e.g. when the schema is managed by a migration tool), pre-create the table; the IF NOT EXISTS guard is idempotent.