Compare commits
6 Commits
v0.1.1
...
fe00943b01
Author | SHA1 | Date | |
---|---|---|---|
fe00943b01 | |||
20f6e45150 | |||
a2cb04fd09 | |||
006af98328 | |||
8c317fc3b6 | |||
efc047c965 |
17
option.go
17
option.go
@@ -1,15 +1,26 @@
|
|||||||
package lite
|
package lite
|
||||||
|
|
||||||
type Option func(d *database)
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrBadFilename error = errors.New("database filename cannot be empty")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Option func(d *database) error
|
||||||
|
|
||||||
func WithFile(name string) Option {
|
func WithFile(name string) Option {
|
||||||
return func(d *database) {
|
return func(d *database) error {
|
||||||
|
if name == "" {
|
||||||
|
return ErrBadFilename
|
||||||
|
}
|
||||||
d.file = name
|
d.file = name
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithPath(path string) Option {
|
func WithPath(path string) Option {
|
||||||
return func(d *database) {
|
return func(d *database) error {
|
||||||
d.path = path
|
d.path = path
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
47
sqlite.go
47
sqlite.go
@@ -3,19 +3,14 @@ package lite
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"path"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/ncruces/go-sqlite3/driver"
|
_ "github.com/ncruces/go-sqlite3/driver"
|
||||||
_ "github.com/ncruces/go-sqlite3/embed"
|
_ "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 {
|
type database struct {
|
||||||
file string
|
file string
|
||||||
path string
|
path string
|
||||||
@@ -23,44 +18,36 @@ type database struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Connect() error
|
|
||||||
Close() error
|
Close() error
|
||||||
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
|
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||||
Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, 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)
|
QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(opts ...Option) (Database, error) {
|
func New(opts ...Option) (Database, error) {
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultFile := "lite-db"
|
defaultFile := "lite-db"
|
||||||
|
|
||||||
db := &database{
|
db := &database{
|
||||||
file: defaultFile,
|
file: defaultFile,
|
||||||
path: home,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(db)
|
opt(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
return db, nil
|
filePath := db.file
|
||||||
|
if db.path != "" {
|
||||||
|
filePath = path.Join(db.path, db.file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// connects to database file
|
slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s", filePath))
|
||||||
func (d *database) Connect() error {
|
|
||||||
db, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s/%s", d.path, d.file))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
db.sqlite = slite
|
||||||
|
|
||||||
d.sqlite = db
|
return db, nil
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// closes database file connection
|
// closes database file connection
|
||||||
@@ -70,33 +57,21 @@ func (d *database) Close() error {
|
|||||||
|
|
||||||
// runs any updates, inserts, or any other queries that do not return rows
|
// 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) {
|
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...)
|
return d.sqlite.ExecContext(ctx, query, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runs basic data queries
|
// runs basic data queries
|
||||||
func (d *database) Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) {
|
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...)
|
return d.sqlite.QueryxContext(ctx, query, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieves a single row from database
|
// retrieves a single row from database
|
||||||
func (d *database) QueryRow(ctx context.Context, query string, args ...any) (*sqlx.Row, error) {
|
func (d *database) QueryRow(ctx context.Context, query string, args ...any) *sqlx.Row {
|
||||||
if d.sqlite == nil {
|
return d.sqlite.QueryRowxContext(ctx, query, args...)
|
||||||
return nil, ErrNotConnected
|
|
||||||
}
|
|
||||||
return d.sqlite.QueryRowxContext(ctx, query, args...), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runs queries that require parameters in an IN clause
|
// runs queries that require parameters in an IN clause
|
||||||
func (d *database) QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) {
|
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...)
|
q, ar, err := sqlx.In(query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user