sqlc

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: MIT Imports: 6 Imported by: 1

README

sqlc: A simple dynamic query builder for kyleconroy/sqlc

How to use

package sqlc_test

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

    "github.com/yiplee/sqlc"
    "github.com/yiplee/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

Overview

Example (Build)
package main

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

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

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

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

	query := example.New(sqlc.Wrap(db))
	authors, err := query.ListAuthors(sqlc.Build(ctx, func(b *sqlc.Builder) {
		b.Where("age > $1", 10)
		b.Where("name = $2", "foo")
		b.In("id", 1, 2, 3)
		b.Order("age,name DESC")
		b.Limit(10)
	}))

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

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

Index

Examples

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) In added in v1.0.2

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 DB added in v1.0.1

type DB struct {
	*nap.DB
}

func Connect added in v1.0.1

func Connect(driverName, master string, slaves ...string) (*DB, error)

func (*DB) PrepareContext added in v1.0.1

func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext creates a prepared statement for later queries or executions.

Read or update can not be determined by the query string currently, use master database.

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.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
JackTT - Gopher 🇻🇳