-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_8.go
55 lines (45 loc) · 1.09 KB
/
tutorial_8.go
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
54
55
//go:build tutorial_8
// +build tutorial_8
package main
import (
"fmt"
"log"
"github.com/FrancoLiberali/cql"
"github.com/FrancoLiberali/cql-tutorial/conditions"
"github.com/FrancoLiberali/cql-tutorial/models"
"gorm.io/gorm"
)
// Target: create Rennes in France and then delete it
func tutorial(db *gorm.DB) {
// get country called France
france, err := cql.Query[models.Country](
db,
conditions.Country.Name.Is().Eq("France"),
).FindOne()
if err != nil {
log.Panicln(err)
}
// create Rennes using gorm's Create method
rennes := models.City{
Country: france,
Name: "Rennes",
Population: 215366,
}
if err := db.Create(&rennes).Error; err != nil {
log.Panicln(err)
}
// delete city called Rennes
deleted, err := cql.Delete[models.City](
db,
conditions.City.Name.Is().Eq("Rennes"),
).Exec()
// SQL executed:
// UPDATE cities
// SET deleted_at="2023-09-11 10:54:12.598"
// WHERE cities.name = "Rennes" AND cities.deleted_at IS NULL
if err != nil {
log.Panicln(err)
}
fmt.Println("--------------------------")
fmt.Printf("Deleted %v city\n", deleted)
}