Skip to content
/ sqlc Public
forked from yiplee/sqlc

sqlc: A simple dynamic query builer for [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)

License

Notifications You must be signed in to change notification settings

badgeek/sqlc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlc: A simple dynamic query builder for kyleconroy/sqlc

How to use

package sqlc_test

import (
    "context"
    "database/sql"
    "log"

    "github.com/badgeek/sqlc"
    "github.com/badgeek/sqlc/example"
)

func Example_build() {
    ctx := context.Background()

    db, err := sql.Open("postgres", "dsn")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // Wrap the db in order to hook the query
    query := example.New(sqlc.Wrap(db))

    /*
    the original query must be simple and not contain any WHERE/ORDER/LIMIT/OFFSET clause

    -- name: ListAuthors :many
    SELECT * FROM authors;
    */

    // customize the builder
    authors, err := query.ListAuthors(sqlc.Build(ctx, func(b *sqlc.Builder) {
        b.Where("age > $1", 10)
        b.Where("name = $2", "foo")
        b.Order("age,name DESC")
        b.Limit(10)
    }))

    if err != nil {
        log.Fatalln("ListAuthors", err)
    }

    log.Printf("list %d authors", len(authors))
}

Not perfect, but enough for now.

About

sqlc: A simple dynamic query builer for [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%