sqlc

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(ctx context.Context, f func(builder *Builder)) context.Context

func WithBuilder

func WithBuilder(ctx context.Context, b *Builder) context.Context

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func BuilderFrom

func BuilderFrom(ctx context.Context) (*Builder, bool)

func (*Builder) Build

func (b *Builder) Build(query string, args ...interface{}) (string, []interface{})

Build returns compiled SELECT string and args.

func (*Builder) GroupBy added in v1.0.5

func (b *Builder) GroupBy(cols string) *Builder

GroupBy sets columns of GROUP BY in SELECT.

func (*Builder) In

func (b *Builder) In(column string, args ...interface{}) *Builder

In is an equivalent of Where("column IN (?,?,?)", args...). In("id", 1, 2, 3)

func (*Builder) Limit

func (b *Builder) Limit(x int) *Builder

Limit sets the limit in SELECT.

func (*Builder) Offset

func (b *Builder) Offset(x int) *Builder

Offset sets the offset in SELECT.

func (*Builder) Order

func (b *Builder) Order(cols string) *Builder

Order sets columns of ORDER BY in SELECT. Order("name, age DESC")

func (*Builder) Where

func (b *Builder) Where(query string, args ...interface{}) *Builder

Where set conditions of where in SELECT Where("user = ?","tom") Where("a = ? OR b = ?",1,2) Where("foo = $1","bar")

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

func Wrap

func Wrap(db DBTX) DBTX

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL