-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_11.go
More file actions
53 lines (45 loc) · 1.22 KB
/
tutorial_11.go
File metadata and controls
53 lines (45 loc) · 1.22 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_11
// +build tutorial_11
package main
import (
"context"
"fmt"
"log"
"github.com/FrancoLiberali/cql"
"github.com/FrancoLiberali/cql-tutorial/conditions"
"github.com/FrancoLiberali/cql-tutorial/models"
)
type CityAndCountryNames struct {
CityName string
CountryName string
}
// Target: obtain only the city and country name of Paris, France
func tutorial(db *cql.DB) {
results, err := cql.Select(
cql.Query[models.City](
context.Background(),
db,
conditions.City.Name.Is().Eq(cql.String("Paris")),
conditions.City.Country(
conditions.Country.Name.Is().Eq(cql.String("France")),
),
),
cql.ValueInto(conditions.City.Name, func(value string, result *CityAndCountryNames) {
result.CityName = value
}),
cql.ValueInto(conditions.Country.Name, func(value string, result *CityAndCountryNames) {
result.CountryName = value
}),
)
// SQL executed:
// SELECT cities.name, Country.name
// FROM cities
// INNER JOIN countries Country ON
// Country.id = cities.country_id AND Country.name = "France"
// WHERE cities.name = "Paris"
if err != nil {
log.Panicln(err)
}
fmt.Println("--------------------------")
fmt.Printf("City named 'Paris' in 'France' is: %+v\n", results)
}