From 3b0df3e9b766b728887e5ab91fb279ab1d5f0a78 Mon Sep 17 00:00:00 2001 From: akm Date: Tue, 19 Aug 2025 21:03:30 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20Usage=20to=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index 2a297a6..9505c67 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,53 @@ See [godoc](https://pkg.go.dev/github.com/tecowl/querybm) for more details. go get -u github.com/tecowl/querybm ``` +## USAGE + +```go +// You can use model generated by sqlc instead of defining your own +type Author struct { + AuthorID int32 + Name string +} + +type Condition struct { + Name string +} + +func (c *Condition) Build(s *querybm.Statement) { + if c.Name != "" { + s.Where.Add(Field("name", LikeContains(c.Name))) + } +} + +func New(db *sql.DB, condition *Condition) *querybm.Query[Author] { + return querybm.New( + db, + "authors", + querybm.NewFields( + []string{"author_id", "name"}, + func(rows querybm.Scanner, author *models.Author) error { + return rows.Scan(&author.AuthorID, &author.Name) + }, + ), + condition, + querybm.NewSortItem("name", false), + querybm.NewLimitOffset(100, 0), + ) +} + +func main() { + db, _ := sql.Open("..", "...") + defer db.Close() + + condition := &Condition{Name: "John"} + q := New(db, condition) + + cnt, _ := q.Count(ctx) + list, _ := q.List(ctx) +} +``` + ## EXAMPLES - [Simple Dynamic Query](./tests/mysql/queries/authors/)