diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2020-07-26 19:46:26 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2020-07-26 19:46:26 +0100 |
commit | fe186c2865c9a54cfb488f14fb6142257d712b94 (patch) | |
tree | 239f38a7edd9158e8dc87d9e64e69587d3336456 /pkg/datamaps/reader.go | |
parent | c89f3b3e5680e06b93ed32c27e8b7632c66c3de9 (diff) |
extraction now uses a named datamap from the database instead of the file
Diffstat (limited to 'pkg/datamaps/reader.go')
-rw-r--r-- | pkg/datamaps/reader.go | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 050853a..61771c0 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -147,7 +147,9 @@ func ReadXLSX(ssheet string) FileData { return outer } -func DMLFromDB(name string, db *sql.DB) []DatamapLine { +// DMLFromDB fetches a slice of DatamapLine from the database given +// the name of a datamap. +func DMLFromDB(name string, db *sql.DB) ([]DatamapLine, error) { var out []DatamapLine @@ -159,7 +161,7 @@ func DMLFromDB(name string, db *sql.DB) []DatamapLine { ` rows, err := db.Query(query, name) if err != nil { - log.Fatal(err) + return nil, err } defer rows.Close() @@ -170,20 +172,39 @@ func DMLFromDB(name string, db *sql.DB) []DatamapLine { cellref string ) if err := rows.Scan(&key, &sheet, &cellref); err != nil { - log.Fatal(err) + return nil, err } - // log.Printf("key %s\nsheet %s\ncellref %v", key, sheet, cellref) out = append(out, DatamapLine{Key: key, Sheet: sheet, Cellref: cellref}) } - return out + return out, nil } -// func ExtractDBDM(name string, file string) ExtractedData { -// xdata := ReadXLSX(file) -// // ddata, err := DMLFromDB(name) // this will need to return a []DatamapLine -// } +func ExtractDBDM(name string, file string, db *sql.DB) ExtractedData { + xdata := ReadXLSX(file) + ddata, err := DMLFromDB(name, db) // this will need to return a []DatamapLine + + if err != nil { + log.Fatal(err) + } + + names := getSheetNames(ddata) + outer := make(ExtractedData, len(names)) + inner := make(map[string]xlsx.Cell) + + for _, i := range ddata { + sheet := i.Sheet + cellref := i.Cellref + + if val, ok := xdata[sheet][cellref]; ok { + inner[cellref] = *val.Cell + outer[sheet] = inner + } + } + + return outer +} //Extract returns the file's data as a map, // using the datamap as a filter, keyed on sheet name. All values |