first commit of new line of development.
This commit is contained in:
sjc
2021-07-06 08:35:02 -04:00
parent d36dcb81ef
commit 829ab54a4d
5 changed files with 145 additions and 3 deletions

View File

@@ -0,0 +1,40 @@
package tradebook
import (
"database/sql"
_ "modernc.org/sqlite"
)
type tradebook struct {
driver string
dsn string
db *sql.DB
}
type openOption func(*tradebook) error
func Open(opts ...openOption) (*tradebook, error) {
t := tradebook{}
// Apply Options
for _, opt := range opts {
opt(&t)
}
db, err := sql.Open(t.driver, t.dsn)
t.db = db
if (err == nil) {
err = db.Ping()
}
return &t, err
}
func Sqlite(fpath string) openOption {
return func(t *tradebook) error {
t.driver = "sqlite"
t.dsn = fpath
return nil
}
}

View File

@@ -0,0 +1,29 @@
package tradebook
import (
"testing"
"path/filepath"
)
func TestOpenSQLite(t *testing.T) {
tdir := t.TempDir()
dpath := filepath.Join(tdir, "db")
tb, err := Open(Sqlite(dpath))
if (err != nil) {
t.Errorf("Open returned error: %v", err)
}
if (tb == nil) {
t.Error("Db is nil")
}
// for sqlite dsn is just a file path
if (tb.dsn != dpath) {
t.Errorf( "DSN does not match! (%v != %v).", dpath, tb.dsn)
}
if (tb.driver != "sqlite") {
t.Errorf("Driver is not sqlite (%v != %v).", tb.driver, "sqlite")
}
}