Compare commits
No commits in common. "8c317fc3b6b3b17acbaefc7224bbb4373045e856" and "5e9ba524e9c8689ee4e8a0fa7e48d786e7968309" have entirely different histories.
8c317fc3b6
...
5e9ba524e9
42
sqlite.go
42
sqlite.go
@ -3,6 +3,7 @@ package lite
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -11,6 +12,10 @@ import (
|
|||||||
_ "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
|
||||||
@ -18,10 +23,11 @@ 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
|
QueryRow(ctx context.Context, query string, args ...any) (*sqlx.Row, error)
|
||||||
QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
|
QueryIn(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,15 +48,21 @@ func New(opts ...Option) (Database, error) {
|
|||||||
opt(db)
|
opt(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s/%s", db.path, db.file))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
db.sqlite = slite
|
|
||||||
|
|
||||||
return db, nil
|
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
|
// closes database file connection
|
||||||
func (d *database) Close() error {
|
func (d *database) Close() error {
|
||||||
return d.sqlite.Close()
|
return d.sqlite.Close()
|
||||||
@ -58,21 +70,33 @@ 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 {
|
func (d *database) QueryRow(ctx context.Context, query string, args ...any) (*sqlx.Row, error) {
|
||||||
return d.sqlite.QueryRowxContext(ctx, query, args...)
|
if d.sqlite == nil {
|
||||||
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user