-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_9.go
More file actions
60 lines (48 loc) · 1.14 KB
/
tutorial_9.go
File metadata and controls
60 lines (48 loc) · 1.14 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
54
55
56
57
58
59
60
//go:build tutorial_9
// +build tutorial_9
package main
import (
"context"
"fmt"
"log"
"github.com/FrancoLiberali/cql"
"github.com/FrancoLiberali/cql-tutorial/conditions"
"github.com/FrancoLiberali/cql-tutorial/models"
)
// Target: create Rennes in France and then delete it
func tutorial(db *cql.DB) {
// get country called France
france, err := cql.Query[models.Country](
context.Background(),
db,
conditions.Country.Name.Is().Eq(cql.String("France")),
).FindOne()
if err != nil {
log.Panicln(err)
}
// create Rennes
rennes := models.City{
CountryID: france.ID,
Name: "Rennes",
Population: 215366,
}
inserted, err := cql.Insert(context.Background(), db, &rennes).Exec()
if err != nil {
log.Panicln(err)
}
fmt.Printf("Inserted %v city\n", inserted)
// delete city called Rennes
deleted, err := cql.Delete[models.City](
context.Background(),
db,
conditions.City.Name.Is().Eq(cql.String("Rennes")),
).Exec()
// SQL executed:
// DELETE FROM cities
// WHERE cities.name = "Rennes"
if err != nil {
log.Panicln(err)
}
fmt.Println("--------------------------")
fmt.Printf("Deleted %v city\n", deleted)
}