feature: add sqlite kv db

This commit is contained in:
Seraphim Strub 2024-07-12 13:50:20 +00:00
parent 3671bf1f80
commit 759aae3b54
9 changed files with 152 additions and 2 deletions

3
.gitignore vendored
View file

@ -1,3 +1,2 @@
.idea
badger
db
/db

6
embed.go Normal file
View file

@ -0,0 +1,6 @@
package discordBots
import _ "embed"
//go:embed schema.sql
var Schema string

1
go.mod
View file

@ -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 (

31
pkg/db/db.go Normal file
View file

@ -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,
}
}

16
pkg/db/models.go Normal file
View file

@ -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
}

58
pkg/db/query.sql.go Normal file
View file

@ -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
}

15
query.sql Normal file
View file

@ -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;

15
schema.sql Normal file
View file

@ -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;

9
sqlc.yaml Normal file
View file

@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "sqlite"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "db"
out: "pkg/db"