-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_example_test.go
More file actions
48 lines (40 loc) · 1.16 KB
/
query_example_test.go
File metadata and controls
48 lines (40 loc) · 1.16 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
package querybm_test
import (
"context"
"database/sql"
"fmt"
"github.com/tecowl/querybm"
. "github.com/tecowl/querybm/expr" // nolint:revive
"github.com/tecowl/querybm/statement"
)
// This is an example to show the concept.
// See https://github.com/tecowl/querybm/tree/main/tests/mysql/queries/authors for a complete test case.
func ExampleNew() { // nolint:testableexamples
buildFuncGen := func(name string) func(st *statement.Statement) {
return func(st *statement.Statement) {
st.Where.Add(Field("name", LikeContains(name)))
}
}
type Author struct {
AuthorID int
Name string
}
searchingName := "Martin"
var db *sql.DB // Assume db is already initialized
q := querybm.New(db, "authors",
querybm.NewFields(
[]string{"author_id", "name"},
func(rows querybm.Scanner, author *Author) error {
return rows.Scan(&author.AuthorID, &author.Name)
},
),
querybm.NewBuilder(buildFuncGen(searchingName)),
querybm.NewSortItem("name", false),
querybm.NewLimitOffset(100, 0),
)
ctx := context.Background()
list, _ := q.List(ctx)
for _, author := range list {
fmt.Printf("Author ID: %d, Name: %s\n", author.AuthorID, author.Name)
}
}