3 Commits

Author SHA1 Message Date
006af98328 removing forced paths 2025-08-28 21:11:59 -04:00
8c317fc3b6 Merge pull request 'updating methods' (#1) from fix/connect-methodology into main
Reviewed-on: #1
2025-07-15 23:01:10 +00:00
efc047c965 updating methods
- removing connect method
- db is connected to on call to New
2025-07-15 19:00:33 -04:00
2 changed files with 7 additions and 45 deletions

View File

@@ -7,9 +7,3 @@ func WithFile(name string) Option {
d.file = name
}
}
func WithPath(path string) Option {
return func(d *database) {
d.path = path
}
}

View File

@@ -3,64 +3,44 @@ package lite
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"github.com/jmoiron/sqlx"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
)
var (
ErrNotConnected = errors.New("the connect method must be called before any queries can be executed")
)
type database struct {
file string
path string
sqlite *sqlx.DB
}
type Database interface {
Connect() error
Close() error
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
QueryRow(ctx context.Context, query string, args ...any) (*sqlx.Row, error)
QueryRow(ctx context.Context, query string, args ...any) *sqlx.Row
QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
}
func New(opts ...Option) (Database, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
defaultFile := "lite-db"
db := &database{
file: defaultFile,
path: home,
}
for _, opt := range opts {
opt(db)
}
return db, nil
}
// connects to database file
func (d *database) Connect() error {
db, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s/%s", d.path, d.file))
slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s", db.file))
if err != nil {
return err
return nil, err
}
db.sqlite = slite
d.sqlite = db
return nil
return db, nil
}
// closes database file connection
@@ -70,33 +50,21 @@ func (d *database) Close() error {
// runs any updates, inserts, or any other queries that do not return rows
func (d *database) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
if d.sqlite == nil {
return nil, ErrNotConnected
}
return d.sqlite.ExecContext(ctx, query, args...)
}
// runs basic data queries
func (d *database) Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) {
if d.sqlite == nil {
return nil, ErrNotConnected
}
return d.sqlite.QueryxContext(ctx, query, args...)
}
// retrieves a single row from database
func (d *database) QueryRow(ctx context.Context, query string, args ...any) (*sqlx.Row, error) {
if d.sqlite == nil {
return nil, ErrNotConnected
}
return d.sqlite.QueryRowxContext(ctx, query, args...), nil
func (d *database) QueryRow(ctx context.Context, query string, args ...any) *sqlx.Row {
return d.sqlite.QueryRowxContext(ctx, query, args...)
}
// runs queries that require parameters in an IN clause
func (d *database) QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) {
if d.sqlite == nil {
return nil, ErrNotConnected
}
q, ar, err := sqlx.In(query, args...)
if err != nil {
return nil, err