aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/train_stop.go
blob: 27fb91011f6736ce0b18dcecadf7fa50b92e5287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package database

import "git.adyxax.org/adyxax/trains/pkg/model"

func (env *DBEnv) ReplaceAndImportTrainStops(trainStops []model.TrainStop) error {
	pre_query := `DELETE FROM train_stops;`
	query := `
		INSERT INTO train_stops
			(id, name)
		VALUES
			($1, $2);`
	tx, err := env.db.Begin()
	if err != nil {
		return newTransactionError("Could not Begin()", err)
	}
	_, err = tx.Exec(pre_query)
	if err != nil {
		tx.Rollback()
		return newQueryError("Could not run database query: most likely the schema is corrupted", err)
	}
	for i := 0; i < len(trainStops); i++ {
		_, err = tx.Exec(
			query,
			trainStops[i].Id,
			trainStops[i].Name,
		)
		if err != nil {
			tx.Rollback()
			return newQueryError("Could not run database query: ", err)
		}
	}
	if err := tx.Commit(); err != nil {
		return newTransactionError("Could not commit transaction", err)
	}
	return nil
}