diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 17:02:41 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-22 17:02:41 +0100 |
commit | c3d4c6bb51a3d7b89e0fcbb69230db423577ae17 (patch) | |
tree | 4ee56ca481c865634cf810db3fa0ec0eb78187d0 /pkg/reader | |
parent | d2d94cfb6e7c676ad9f709641b86417c17093657 (diff) |
reconfigured opts
Diffstat (limited to 'pkg/reader')
-rw-r--r-- | pkg/reader/db.go | 17 | ||||
-rw-r--r-- | pkg/reader/db_test.go | 7 | ||||
-rw-r--r-- | pkg/reader/testdata/test.db | bin | 135168 -> 135168 bytes |
3 files changed, 18 insertions, 6 deletions
diff --git a/pkg/reader/db.go b/pkg/reader/db.go index 6f62de1..91b6b85 100644 --- a/pkg/reader/db.go +++ b/pkg/reader/db.go @@ -11,6 +11,12 @@ import ( _ "github.com/mattn/go-sqlite3" ) +type Opts struct { + DBPath, ImportPath, Name string + Overwrite, Initial bool + DMData []DatamapLine +} + func SetupDB(path string) (*sql.DB, error) { stmt_base := `DROP TABLE IF EXISTS datamap; CREATE TABLE datamap(id INTEGER PRIMARY KEY, name TEXT, date_created TEXT); @@ -52,13 +58,14 @@ func SetupDB(path string) (*sql.DB, error) { // TODO - how do we avoid passing in all these params!??! //DatamapToDB takes a slice of DatamapLine and writes it to a sqlite3 db file. -func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path string) error { - fmt.Printf("Importing datamap file %s and naming it %s.\n", dm_path, dm_name) +//func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path string) error { +func DatamapToDB(opts Opts) error { + fmt.Printf("Importing datamap file %s and naming it %s.\n", opts.ImportPath, opts.Name) // db, err := SetupDB("/home/lemon/.config/datamaps-go/datamaps.db") // if err != nil { // return err // } - d, err := sql.Open("sqlite3", d_path) + d, err := sql.Open("sqlite3", opts.DBPath) if err != nil { return errors.New("Cannot open that damn database file") } @@ -76,7 +83,7 @@ func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path stri if err != nil { return err } - _, err = stmt_dm.Exec(dm_name, time.Now()) + _, err = stmt_dm.Exec(opts.Name, time.Now()) stmt_dml, err := tx.Prepare("INSERT INTO datamap_line (dm_id, key, sheet, cellref) VALUES(?,?,?,?);") if err != nil { @@ -84,7 +91,7 @@ func DatamapToDB(d_path string, data []DatamapLine, dm_name string, dm_path stri } defer stmt_dm.Close() defer stmt_dml.Close() - for _, dml := range data { + for _, dml := range opts.DMData { _, err = stmt_dml.Exec(1, dml.Key, dml.Sheet, dml.Cellref) if err != nil { return err diff --git a/pkg/reader/db_test.go b/pkg/reader/db_test.go index 5c33343..5a96485 100644 --- a/pkg/reader/db_test.go +++ b/pkg/reader/db_test.go @@ -34,7 +34,12 @@ func TestDatamapGoesIntoDB(t *testing.T) { if err != nil { t.Fatal(err) } - err = DatamapToDB("./testdata/test.db", d, "First Datamap", "./testdata/test.db") + opts := Opts{ + DBPath: "./testdata/test.db", + Name: "First Datamap", + DMData: d, + } + err = DatamapToDB(opts) if err != nil { t.Errorf("Unable to write datamap to database file because %v.", err) } diff --git a/pkg/reader/testdata/test.db b/pkg/reader/testdata/test.db Binary files differindex bbdc08f..d497380 100644 --- a/pkg/reader/testdata/test.db +++ b/pkg/reader/testdata/test.db |