-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_8.go
More file actions
53 lines (44 loc) · 1.21 KB
/
tutorial_8.go
File metadata and controls
53 lines (44 loc) · 1.21 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//go:build tutorial_8
// +build tutorial_8
package main
import (
"context"
"fmt"
"log"
"github.com/FrancoLiberali/cql"
"github.com/FrancoLiberali/cql-tutorial/conditions"
"github.com/FrancoLiberali/cql-tutorial/models"
)
// Target: edit Paris France population to 2102650
func tutorial(db *cql.DB) {
var cities []models.City
updated, err := cql.Update[models.City](
context.Background(),
db,
conditions.City.Name.Is().Eq(cql.String("Paris")),
conditions.City.Country(
conditions.Country.Name.Is().Eq(cql.String("France")),
),
).Returning(&cities).Set(
conditions.City.Population.Set().Eq(cql.Int64(2102650)),
)
// SQL executed:
// UPDATE cities
// SET population=2102650
// FROM countries Country
// WHERE cities.name = "Paris" AND
// (Country.id = cities.country_id AND Country.name = "France")
// RETURNING *
if err != nil {
log.Panicln(err)
}
parisFrance := cities[0]
fmt.Println("--------------------------")
fmt.Printf("Updated %v city: %v\n", updated, parisFrance)
fmt.Println("Initial population was 2161000")
// go back to initial situation with gorm's Save method
parisFrance.Population = 2161000
if err := db.GormDB.Save(&parisFrance).Error; err != nil {
log.Panicln(err)
}
}