init commit
This commit is contained in:
commit
9552e197a8
14
go.mod
Normal file
14
go.mod
Normal file
@ -0,0 +1,14 @@
|
||||
module code.jakeyoungdev.com/go/lite
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/jmoiron/sqlx v1.4.0
|
||||
github.com/ncruces/go-sqlite3 v0.26.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ncruces/julianday v1.0.0 // indirect
|
||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
)
|
20
go.sum
Normal file
20
go.sum
Normal file
@ -0,0 +1,20 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/ncruces/go-sqlite3 v0.26.1 h1:lBXmbmucH1Bsj57NUQR6T84UoMN7jnNImhF+ibEITJU=
|
||||
github.com/ncruces/go-sqlite3 v0.26.1/go.mod h1:XFTPtFIo1DmGCh+XVP8KGn9b/o2f+z0WZuT09x2N6eo=
|
||||
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
|
||||
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
|
||||
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
|
||||
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
15
option.go
Normal file
15
option.go
Normal file
@ -0,0 +1,15 @@
|
||||
package lite
|
||||
|
||||
type Option func(d *database)
|
||||
|
||||
func WithFile(name string) Option {
|
||||
return func(d *database) {
|
||||
d.file = name
|
||||
}
|
||||
}
|
||||
|
||||
func WithPath(path string) Option {
|
||||
return func(d *database) {
|
||||
d.path = path
|
||||
}
|
||||
}
|
89
sqlite.go
Normal file
89
sqlite.go
Normal file
@ -0,0 +1,89 @@
|
||||
package lite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
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
|
||||
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))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.sqlite = db
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// closes database file connection
|
||||
func (d *database) Close() error {
|
||||
return d.sqlite.Close()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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) {
|
||||
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 {
|
||||
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) {
|
||||
q, ar, err := sqlx.In(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return d.sqlite.QueryxContext(ctx, q, ar...)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user