-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_4.go
38 lines (31 loc) · 959 Bytes
/
tutorial_4.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
//go:build tutorial_4
// +build tutorial_4
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: get all cities whose name is 'Paris' and that the country to which they belong is called 'France'.
func tutorial(db *gorm.DB) {
parisFrance, err := cql.Query[models.City](
db,
conditions.City.Name.Is().Eq("Paris"),
conditions.City.Country(
conditions.Country.Name.Is().Eq("France"),
),
).FindOne()
// SQL executed:
// SELECT cities.* FROM cities
// INNER JOIN countries Country ON
// Country.id = cities.country_id AND Country.name = "France" AND Country.deleted_at IS NULL
// WHERE cities.name = "Paris" AND cities.deleted_at IS NULL
if err != nil {
log.Panicln(err)
}
fmt.Println("--------------------------")
fmt.Printf("City named 'Paris' in 'France' is: %+v\n", parisFrance)
}