aboutsummaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-21 19:26:56 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-21 19:28:56 +0300
commitce3111b0efe91e275ce070f9511b5b1b9801a46d (patch)
tree09fa4f10dfb1e17761339c798eefa73c6b18484f /db
parente9a64f3b41b5eae47dec7c0ecfd1caae83136abc (diff)
Множество улучшенийv0.0.2
Diffstat (limited to 'db')
-rw-r--r--db/db.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/db/db.go b/db/db.go
new file mode 100644
index 0000000..5241cd1
--- /dev/null
+++ b/db/db.go
@@ -0,0 +1,34 @@
+package db
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "log/slog"
+
+ "github.com/uptrace/bun"
+ "github.com/uptrace/bun/dialect/sqlitedialect"
+)
+
+func GetDB(dbFile string) (*bun.DB, error) {
+ db, err := sql.Open("sqlite3", dbFile)
+ if err != nil {
+ return nil, fmt.Errorf("open db failed: %w", err)
+ }
+
+ orm := bun.NewDB(db, sqlitedialect.New())
+ orm.AddQueryHook(&queryHook{})
+
+ return orm, nil
+}
+
+type queryHook struct {
+}
+
+func (*queryHook) BeforeQuery(ctx context.Context, qe *bun.QueryEvent) context.Context {
+ return ctx
+}
+
+func (*queryHook) AfterQuery(ctx context.Context, qe *bun.QueryEvent) {
+ slog.Debug("query", slog.String("query", qe.Query))
+}