aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmd/main.go9
-rw-r--r--pkg/db/setup.go39
-rw-r--r--pkg/db/setup_test.go9
-rw-r--r--pkg/db/testdata/test.dbbin12288 -> 135168 bytes
4 files changed, 51 insertions, 6 deletions
diff --git a/cmd/main.go b/cmd/main.go
index dfe24fb..5e77583 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -9,6 +9,8 @@ import (
"log"
"os"
"path/filepath"
+
+ "github.com/yulqen/datamaps-go/pkg/reader"
)
func setUp() (string, error) {
@@ -64,7 +66,7 @@ func main() {
initialFlg := datamapCmd.Bool("initial", false, "This option must be used where no datamap table yet exists.")
if len(os.Args) < 2 {
- fmt.Println("expected 'datamap' or 'setup' subcommand")
+ fmt.Println("Expected 'datamap' or 'setup' subcommand")
os.Exit(1)
}
@@ -77,6 +79,11 @@ func main() {
fmt.Println(" name:", *nameFlg)
fmt.Println(" overwrite:", *overwriteFlg)
fmt.Println(" initial:", *initialFlg)
+ data, err := reader.ReadDML(*importFlg)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(data[3].Key)
case "setup":
setupCmd.Parse(os.Args[2:])
_, err := setUp()
diff --git a/pkg/db/setup.go b/pkg/db/setup.go
index e57b046..ef53cf6 100644
--- a/pkg/db/setup.go
+++ b/pkg/db/setup.go
@@ -5,8 +5,10 @@ import (
"errors"
"log"
"os"
+ "time"
_ "github.com/mattn/go-sqlite3"
+ "github.com/yulqen/datamaps-go/pkg/reader"
)
func SetupDB(path string) (*sql.DB, error) {
@@ -45,3 +47,40 @@ func SetupDB(path string) (*sql.DB, error) {
return db, nil
}
+
+//DatamapToDB takes a slice of DatamapLine and writes it to a sqlite3 db file.
+func DatamapToDB(data []reader.DatamapLine, db *sql.DB, dm_name string) error {
+ tx, err := db.Begin()
+ if err != nil {
+ return err
+ }
+ pragma := "PRAGMA foreign_keys = ON;"
+ _, err = db.Exec(pragma)
+ if err != nil {
+ log.Printf("%q: %s\n", err, pragma)
+ return err
+ }
+ stmt_dm, err := tx.Prepare("INSERT INTO datamap (name, date_created) VALUES(?,?)")
+ if err != nil {
+ return err
+ }
+ _, err = stmt_dm.Exec(dm_name, time.Now())
+
+ stmt_dml, err := tx.Prepare("INSERT INTO datamap_line (dm_id, key, sheet, cellref) VALUES(?,?,?,?);")
+ if err != nil {
+ return err
+ }
+ defer stmt_dm.Close()
+ defer stmt_dml.Close()
+ for _, dml := range data {
+ _, err = stmt_dml.Exec(1, dml.Key, dml.Sheet, dml.Cellref)
+ if err != nil {
+ return err
+ }
+ }
+ err = tx.Commit()
+ if err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/pkg/db/setup_test.go b/pkg/db/setup_test.go
index d5f12ba..3787d2e 100644
--- a/pkg/db/setup_test.go
+++ b/pkg/db/setup_test.go
@@ -59,10 +59,9 @@ func TestDatamapGoesIntoDB(t *testing.T) {
if err != nil {
t.Fatal("Expected to be able to set up the database.")
}
-
- d, _ := reader.ReadDML("testdata/datamap.csv")
- for _, x := range d {
- t.Log(x.Key)
+ d, _ := reader.ReadDML("./testdata/datamap.csv")
+ err = DatamapToDB(d, db, "First Datamap")
+ if err != nil {
+ t.Errorf("Unable to write datamap to database file because %v.", err)
}
-
}
diff --git a/pkg/db/testdata/test.db b/pkg/db/testdata/test.db
index 26c2e03..81de353 100644
--- a/pkg/db/testdata/test.db
+++ b/pkg/db/testdata/test.db
Binary files differ