From 759aae3b54ba1224a3c450ed7b19e5cad7aee15f Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Fri, 12 Jul 2024 13:50:20 +0000 Subject: [PATCH] feature: add sqlite kv db --- .gitignore | 3 +-- embed.go | 6 +++++ go.mod | 1 + pkg/db/db.go | 31 ++++++++++++++++++++++++ pkg/db/models.go | 16 +++++++++++++ pkg/db/query.sql.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ query.sql | 15 ++++++++++++ schema.sql | 15 ++++++++++++ sqlc.yaml | 9 +++++++ 9 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 embed.go create mode 100644 pkg/db/db.go create mode 100644 pkg/db/models.go create mode 100644 pkg/db/query.sql.go create mode 100644 query.sql create mode 100644 schema.sql create mode 100644 sqlc.yaml diff --git a/.gitignore b/.gitignore index 418f5c3..63c59b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .idea -badger -db +/db diff --git a/embed.go b/embed.go new file mode 100644 index 0000000..34fcb62 --- /dev/null +++ b/embed.go @@ -0,0 +1,6 @@ +package discordBots + +import _ "embed" + +//go:embed schema.sql +var Schema string diff --git a/go.mod b/go.mod index d3a672d..ca548f0 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/disgoorg/log v1.2.1 github.com/disgoorg/snowflake/v2 v2.0.1 golang.org/x/net v0.27.0 + modernc.org/sqlite v1.30.1 ) require ( diff --git a/pkg/db/db.go b/pkg/db/db.go new file mode 100644 index 0000000..bdb151c --- /dev/null +++ b/pkg/db/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package db + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/pkg/db/models.go b/pkg/db/models.go new file mode 100644 index 0000000..04d6413 --- /dev/null +++ b/pkg/db/models.go @@ -0,0 +1,16 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package db + +import ( + "time" +) + +type Store struct { + ID string + Data []byte + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/pkg/db/query.sql.go b/pkg/db/query.sql.go new file mode 100644 index 0000000..2bbb837 --- /dev/null +++ b/pkg/db/query.sql.go @@ -0,0 +1,58 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: query.sql + +package db + +import ( + "context" +) + +const countItems = `-- name: CountItems :one +SELECT count(*) +FROM store +` + +func (q *Queries) CountItems(ctx context.Context) (int64, error) { + row := q.db.QueryRowContext(ctx, countItems) + var count int64 + err := row.Scan(&count) + return count, err +} + +const createItem = `-- name: CreateItem :exec +INSERT INTO store (id, data) +VALUES (?1, ?2) +ON CONFLICT(id) + DO UPDATE SET data = ?2 +` + +type CreateItemParams struct { + ID string + Data []byte +} + +func (q *Queries) CreateItem(ctx context.Context, arg CreateItemParams) error { + _, err := q.db.ExecContext(ctx, createItem, arg.ID, arg.Data) + return err +} + +const getItem = `-- name: GetItem :one +SELECT id, data, created_at, updated_at +FROM store +WHERE id = ? +LIMIT 1 +` + +func (q *Queries) GetItem(ctx context.Context, id string) (Store, error) { + row := q.db.QueryRowContext(ctx, getItem, id) + var i Store + err := row.Scan( + &i.ID, + &i.Data, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} diff --git a/query.sql b/query.sql new file mode 100644 index 0000000..38fbf4d --- /dev/null +++ b/query.sql @@ -0,0 +1,15 @@ +-- name: GetItem :one +SELECT * +FROM store +WHERE id = ? +LIMIT 1; + +-- name: CountItems :one +SELECT count(*) +FROM store; + +-- name: CreateItem :exec +INSERT INTO store (id, data) +VALUES (?1, ?2) +ON CONFLICT(id) + DO UPDATE SET data = ?2; \ No newline at end of file diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..8615064 --- /dev/null +++ b/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS store +( + id TEXT NOT NULL PRIMARY KEY, + data BLOB NOT NULL, + created_at DATE NOT NULL DEFAULT current_timestamp, + updated_at DATE NOT NULL DEFAULT current_timestamp +); + +-- automatic updated at +CREATE TRIGGER IF NOT EXISTS update_store_updated_at + AFTER UPDATE ON store + WHEN OLD.updated_at <> current_timestamp +BEGIN +UPDATE store SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +END; \ No newline at end of file diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 0000000..7531d20 --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,9 @@ +version: "2" +sql: + - engine: "sqlite" + queries: "query.sql" + schema: "schema.sql" + gen: + go: + package: "db" + out: "pkg/db" \ No newline at end of file