From a2cb04fd097f0b8b477880cfb68a6641d553e9ca Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 7 Sep 2025 01:29:18 -0400 Subject: [PATCH] adding in file path option --- option.go | 23 +++++++++++++++++++++-- sqlite.go | 9 ++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/option.go b/option.go index 977b5d0..b5729c6 100644 --- a/option.go +++ b/option.go @@ -1,9 +1,28 @@ package lite -type Option func(d *database) +import ( + "fmt" +) + +var ( + ErrBadFilename error = fmt.Errorf("database filename cannot be empty") +) + +type Option func(d *database) error func WithFile(name string) Option { - return func(d *database) { + return func(d *database) error { + if name == "" { + return ErrBadFilename + } d.file = name + return nil + } +} + +func WithPath(path string) Option { + return func(d *database) error { + d.path = path + return nil } } diff --git a/sqlite.go b/sqlite.go index caa62ef..4cefb07 100644 --- a/sqlite.go +++ b/sqlite.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "path" "github.com/jmoiron/sqlx" _ "github.com/ncruces/go-sqlite3/driver" @@ -12,6 +13,7 @@ import ( type database struct { file string + path string sqlite *sqlx.DB } @@ -34,7 +36,12 @@ func New(opts ...Option) (Database, error) { opt(db) } - slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s", db.file)) + filePath := db.file + if db.path != "" { + filePath = path.Join(db.path, db.file) + } + + slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s", filePath)) if err != nil { return nil, err }