trobleshooting and changeing formating

This commit is contained in:
Peder Vatn Austad
2024-12-26 17:46:05 +01:00
parent aecf92afbd
commit 83e8b7106e
3 changed files with 66 additions and 10 deletions

View File

@@ -2,16 +2,47 @@ package database
import (
"database/sql"
"errors"
"math/rand"
"fmt"
)
func InsertOrUpdateEstimatedCall(db *sql.DB, estimatedValues []string) (int, string, error) {
if len(estimatedValues) == 0 {
return 0, "", errors.New("no estimated values provided")
func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, error) {
query := `
INSERT INTO calls (
estimatedvehiclejourney, "order", stoppointref,
aimeddeparturetime, expecteddeparturetime,
aimedarrivaltime, expectedarrivaltime,
cancellation, estimated_data
)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
ON CONFLICT (estimatedvehiclejourney, "order")
DO UPDATE SET
stoppointref = EXCLUDED.stoppointref,
aimeddeparturetime = EXCLUDED.aimeddeparturetime,
expecteddeparturetime = EXCLUDED.expecteddeparturetime,
aimedarrivaltime = EXCLUDED.aimedarrivaltime,
expectedarrivaltime = EXCLUDED.expectedarrivaltime,
cancellation = EXCLUDED.cancellation,
estimated_data = EXCLUDED.estimated_data
RETURNING CASE WHEN xmax = 0 THEN 'insert' ELSE 'update' END, id;
`
stmt, err := db.Prepare(query)
if err != nil {
return 0, "", fmt.Errorf("error preparing statement: %v", err)
}
defer stmt.Close()
id := rand.Intn(100)
action := "tmp"
var action string
var id int
err = stmt.QueryRow(values...).Scan(&action, &id)
if err != nil {
if 1 == 0 {
fmt.Println("Executing query:", query)
for i, v := range values {
fmt.Printf("Value %d: (%v)\n", i+1, v)
}
}
return 0, "", fmt.Errorf("error executing statement: %v", err)
}
return id, action, nil
}