0.2.4 EstVeJurnah gota get it to return its id tho nut other than that diz good

This commit is contained in:
Peder Vatn Austad
2024-05-26 21:03:20 +02:00
parent 662ca02a7f
commit 4f01023df7
5 changed files with 74 additions and 235 deletions

View File

@@ -0,0 +1,38 @@
package database
import (
"database/sql"
"fmt"
)
func InsertOrUpdateEstimatedVehicleJourney(db *sql.DB, values []interface{}) error {
query := `
INSERT INTO estimatedvehiclejourney (servicedelivery, recordedattime, lineref, directionref, datasource, datedvehiclejourneyref, vehiclemode, dataframeref, originref, destinationref, operatorref, vehicleref, cancellation, other)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (lineref, directionref, datasource, datedvehiclejourneyref)
DO UPDATE SET
servicedelivery = EXCLUDED.servicedelivery,
recordedattime = EXCLUDED.recordedattime,
vehiclemode = COALESCE(EXCLUDED.vehiclemode, estimatedvehiclejourney.vehiclemode),
dataframeref = COALESCE(EXCLUDED.dataframeref, estimatedvehiclejourney.dataframeref),
originref = COALESCE(EXCLUDED.originref, estimatedvehiclejourney.originref),
destinationref = COALESCE(EXCLUDED.destinationref, estimatedvehiclejourney.destinationref),
operatorref = COALESCE(EXCLUDED.operatorref, estimatedvehiclejourney.operatorref),
vehicleref = COALESCE(EXCLUDED.vehicleref, estimatedvehiclejourney.vehicleref),
cancellation = COALESCE(EXCLUDED.cancellation, estimatedvehiclejourney.cancellation),
other = COALESCE(EXCLUDED.other, estimatedvehiclejourney.other);
`
stmt, err := db.Prepare(query)
if err != nil {
return fmt.Errorf("error preparing statement: %v", err)
}
defer stmt.Close()
_, err = stmt.Exec(values...)
if err != nil {
return fmt.Errorf("error executing statement: %v", err)
}
return nil
}