diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2020-07-28 16:15:10 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2020-07-28 16:15:10 +0100 |
commit | 46741fc3259420f6310ebbfaae2ecd44c64b4373 (patch) | |
tree | 635f270d3eb60775d4720101b2584cdf5ff6674c /pkg/datamaps/db.go | |
parent | d8e1e24b7ae915ed16be22b3f85ac722acbaa824 (diff) |
I think I fixed the bug whereby multiple returns where created
Diffstat (limited to 'pkg/datamaps/db.go')
-rw-r--r-- | pkg/datamaps/db.go | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/pkg/datamaps/db.go b/pkg/datamaps/db.go index 30ebd6a..416fc94 100644 --- a/pkg/datamaps/db.go +++ b/pkg/datamaps/db.go @@ -175,23 +175,37 @@ func importXLSXtoDB(dm_name string, return_name string, file string, db *sql.DB) } fmt.Printf("Extracting from %s\n", file) - stmtReturn, err := db.Prepare("insert into return(name, date_created) values(?,?)") + // If there is already a return with a matching name, use that. + rtnQuery, err := db.Prepare("select id from return where (return.name=?)") if err != nil { log.Fatal(err) } + defer rtnQuery.Close() - defer stmtReturn.Close() - - res, err := stmtReturn.Exec(return_name, time.Now()) + var retId int64 + err = rtnQuery.QueryRow(return_name).Scan(&retId) if err != nil { - err := fmt.Errorf("%v\nCannot create %s", err, return_name) - fmt.Println(err.Error()) - os.Exit(1) + fmt.Printf("Couldn't find an existing return named %s\n", return_name) } - retId, err := res.LastInsertId() - if err != nil { - log.Fatal(err) + if retId == 0 { + stmtReturn, err := db.Prepare("insert into return(name, date_created) values(?,?)") + if err != nil { + log.Fatal(err) + } + defer stmtReturn.Close() + + res, err := stmtReturn.Exec(return_name, time.Now()) + if err != nil { + err := fmt.Errorf("%v\nCannot create %s", err, return_name) + fmt.Println(err.Error()) + os.Exit(1) + } + + retId, err = res.LastInsertId() + if err != nil { + log.Fatal(err) + } } // We're going to need a transaction for the big stuff @@ -225,7 +239,7 @@ func importXLSXtoDB(dm_name string, return_name string, file string, db *sql.DB) } defer insertStmt.Close() - res, err = insertStmt.Exec(dmlId, retId, cellData.Value) + _, err = insertStmt.Exec(dmlId, retId, cellData.Value) if err != nil { log.Fatal(err) } |