diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/db.go | 34 |
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)) +} |