diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-08-01 16:58:07 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-08-01 16:58:07 +0100 |
commit | c9e481b4548193af2c52865d78c86a2d7d4f1f3e (patch) | |
tree | 12d08ad2d6c16c77797acd75a974b6a7d435e466 | |
parent | d3524fa33dc38804f3e4ec3149e3453dd4351aa5 (diff) |
first very rough writing of the datamap from the database - VERY ROUGH
-rw-r--r-- | pkg/datamaps/writer.go | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/pkg/datamaps/writer.go b/pkg/datamaps/writer.go index 54ea789..e1cb713 100644 --- a/pkg/datamaps/writer.go +++ b/pkg/datamaps/writer.go @@ -38,11 +38,6 @@ func ExportMaster(opts *Options) error { wb := xlsx.NewFile() sh, err := wb.AddSheet("Master Data") - testRow, err := sh.Row(1) - if err != nil { - log.Fatal(err) - } - // SQLITE CODE db, err := sql.Open("sqlite3", opts.DBPath) @@ -101,7 +96,7 @@ func ExportMaster(opts *Options) error { INNER JOIN return on return_data.ret_id=return.id) WHERE datamap.name=? AND return.name=? AND datamap_line.key=?;` - var values = make([]string, rowCount+1) + var values = make(map[string][]string) for _, k := range datamapKeys { masterData, err := db.Query(getDataSQL, opts.DMName, opts.ReturnName, k) if err != nil { @@ -114,15 +109,23 @@ func ExportMaster(opts *Options) error { if err := masterData.Scan(&key, &value, &filename); err != nil { return err } - values = append(values, value) + values = appendValueMap(key, value, values) } } } - if sl := testRow.WriteSlice(values, -1); sl == -1 { - log.Printf("not a slice type") + var masterRow int64 + for masterRow = 1; masterRow <= datamapKeysNumber; masterRow++ { + r, err := sh.Row(int(masterRow)) + if err != nil { + log.Fatal(err) + } + dmlKey := datamapKeys[masterRow-1] + if sl := r.WriteSlice(values[dmlKey], -1); sl == -1 { + log.Printf("not a slice type") + } + log.Printf("writing slice to row\n") } - log.Printf("writing slice to row\n") log.Printf("saving master at %s", opts.MasterOutPutPath) if err := wb.Save(filepath.Join(opts.MasterOutPutPath, "master.xlsx")); err != nil { @@ -131,3 +134,27 @@ func ExportMaster(opts *Options) error { sh.Close() return nil } + +func appendValueMap(k, v string, values map[string][]string) map[string][]string { + + var keyIn bool + for kv, _ := range values { + if kv == k { + keyIn = true + break + } + } + + if keyIn { + slice, ok := values[k] + if !ok { + log.Fatalf("%s is not a key in this map", k) + } + slice = append(slice, v) + values[k] = slice + return values + } else { + values[k] = []string{v} + return values + } +} |