-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_9.go
43 lines (36 loc) · 984 Bytes
/
tutorial_9.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
//go:build tutorial_9
// +build tutorial_9
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: obtain all the countries that have a city called 'Paris'
func tutorial(db *gorm.DB) {
countries, err := cql.Query[models.Country](
db,
conditions.Country.Cities.Any(
conditions.City.Name.Is().Eq("Paris"),
),
).Find()
// SQL executed:
// SELECT countries.* FROM countries
// WHERE (EXISTS (
// SELECT(1) FROM cities
// WHERE cities.country_id = countries.id AND
// cities.name = "Paris" AND
// cities.deleted_at IS NULL
// )) AND countries.deleted_at IS NULL
if err != nil {
log.Panicln(err)
}
fmt.Println("--------------------------")
fmt.Println("Countries that have a city called 'Paris' are:")
for i, country := range countries {
fmt.Printf("\t%v: %+v\n", i+1, country)
}
}