diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | reader/reader.go | 19 | ||||
-rw-r--r-- | reader/reader_test.go | 28 |
4 files changed, 50 insertions, 0 deletions
@@ -1,2 +1,4 @@ .vscode/* .idea/* +reader/testdata/test.db +go.sum @@ -3,6 +3,7 @@ module github.com/yulqen/datamaps-go go 1.14 require ( + github.com/mattn/go-sqlite3 v1.14.0 github.com/tealeg/xlsx v1.0.5 github.com/yulqen/coords v0.1.0 ) diff --git a/reader/reader.go b/reader/reader.go index 0aa477f..d99f2a8 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -1,6 +1,7 @@ package reader import ( + "database/sql" "encoding/csv" "errors" "fmt" @@ -10,6 +11,8 @@ import ( "path/filepath" "strings" + _ "github.com/mattn/go-sqlite3" + "github.com/tealeg/xlsx" "github.com/yulqen/coords" ) @@ -38,6 +41,22 @@ type ExtractedCell struct { Value string } +func SetupDB(path string) (*sql.DB, error) { + db, err := sql.Open("sqlite3", path) + if err != nil { + return db, errors.New("Cannot open that damn database file") + } + stmt := `drop table if exists datamap; + create table datamap(id integer no null primary key, name text); + ` + _, err = db.Exec(stmt) + if err != nil { + log.Printf("%q: %s\n", err, stmt) + } + + return db, nil +} + //sheetInSlice is a helper which returns true // if a string is in a slice of strings. func sheetInSlice(list []string, key string) bool { diff --git a/reader/reader_test.go b/reader/reader_test.go index ca886bc..0335b03 100644 --- a/reader/reader_test.go +++ b/reader/reader_test.go @@ -1,9 +1,37 @@ package reader import ( + "fmt" "testing" ) +func TestOpenSQLiteFile(t *testing.T) { + // db, err := sql.Open("sqlite3", "testdata/test.db") + db, err := SetupDB("testdata/test.db") + defer db.Close() + if err != nil { + t.Fatal("Expected to be able to set up the database.") + } + stmt := `insert into datamap(id, name) values(1,'cock')` + _, err = db.Exec(stmt) + if err != nil { + t.Errorf("Cannot add record to db") + } + rows, err := db.Query("select name from datamap") + if err != nil { + t.Errorf("Cannot run select statement") + } + defer rows.Close() + for rows.Next() { + var name string + err = rows.Scan(&name) + if err != nil { + t.Errorf("Cannot scan resulting row") + } + fmt.Println(name) + } +} + func TestReadDML(t *testing.T) { d, _ := ReadDML("testdata/datamap.csv") cases := []struct { |