Skip to content

tecowl/querybm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

202 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

querybm

CI codecov Go Report Card Go project version Enabled Linters Documentation GitHub go.mod Go version GitHub License

A simple SQL query builder and mapper. querybm works well with models generated by sqlc. But it works with your models without sqlc.

FEATURES

  • No dependencies
  • Build SELECT statement dynamically with struct type
  • Mapping with generics instead of reflection
  • 100% test coverage

See godoc for more details.

INSTALL

go get -u github.com/tecowl/querybm

USAGE

// 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

TESTS

  • MySQL
  • PostgreSQL
  • SQLite3

LICENSE

MIT

About

A simple SQL query builder and mapper

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •