sqlc

package
v0.0.36-pre1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 6 Imported by: 9

README

Overview

SQLC is a great tool to use raw sql query with golang. But currently sqlc lacks of dynamic query support.

It's currently only support static query that compiled to golang code, but sometimes we need dynamic query for condition/order/limit/etc. So i write this code as wrapper for sqlc for dynamic query.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(ctx context.Context, fn func(b *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 GetBuilder

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

func (*Builder) And

func (b *Builder) And(column string, args ...any) BuilderInterface

Like method Where() but will add "AND" logic to your expression

Then your query will look like "AND {{ your expression here }}"

Example:

And("name LIKE ?", "%me%")

func (*Builder) Build

func (b *Builder) Build(query string, args ...any) (string, []any)

Build or compile your queries

func (*Builder) In

func (b *Builder) In(column string, args ...any) BuilderInterface

Is equal with Where("id IN (?,?,?,...)", args...)

Example:

In("id", 1,2,3)

In("OR id", 1,2,3)

func (*Builder) Limit

func (b *Builder) Limit(limit int) BuilderInterface

Limit for rows that returned on SELECT query.

Example:

Limit(10)

func (*Builder) Offset

func (b *Builder) Offset(offset int) BuilderInterface

Offset for rows that returned on SELECT query.

Example:

Offset(10)

func (*Builder) Or

func (b *Builder) Or(column string, args ...any) BuilderInterface

Like method Where() but will add "OR" logic to your expression

Then your query will look like "OR {{ your expression here }}"

Example:

Or("name LIKE ?", "%me%")

func (*Builder) Order

func (b *Builder) Order(cols string, args ...any) BuilderInterface

Order columns on SELECT query. Your query will like "SELECT x FROM x WHERE x ORDER BY {{ cols }}"

Example:

Order("id DESC")

Order("id, age DESC")

func (*Builder) Where

func (b *Builder) Where(expression string, args ...any) BuilderInterface

Determine condition on "SELECT x FROM x WHERE {{ this is your expression }}".

Where("id = ?", 1) - If no logic is set, then will use "AND" by default

Where("OR email = ?", "x@gmail.com") - If logic has set, then will use it instead of "AND" (default)

Where("is_deleted = $1", 0)

Where("AND id IN (SELECT user_id FROM user_roles WHERE role_id = $1)", 2)

type BuilderInterface

type BuilderInterface interface {
	Where(expression string, args ...any) BuilderInterface
	In(column string, args ...any) BuilderInterface
	Or(column string, args ...any) BuilderInterface
	And(column string, args ...any) BuilderInterface
	Order(cols string, args ...any) BuilderInterface
	Limit(limit int) BuilderInterface
	Offset(offset int) BuilderInterface
	Build(query string, args ...any) (string, []any)
}

type DBTX

type DBTX interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

func Wrap

func Wrap(db DBTX, opts WrappedOpts) DBTX

type WrappedOpts added in v0.0.34

type WrappedOpts struct {
	ShowQuery bool
	ShowArgs  bool
}

Jump to

Keyboard shortcuts

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