query

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 13 Imported by: 0

README

query — SQL query/clause builder

import "github.com/downsized-devs/sdk-go/query"

Stability: Beta — bulk-insert helper carries a "find a better way" marker. See STABILITY.md.

Dynamic SQL clause builder driven by struct tags. Builds WHERE, ORDER BY, LIMIT, and OFFSET from typed parameter structs. Includes a bulk-insert helper (FormatQueryForRows).

Features

  • Struct-tag-driven WHERE clause builder (matches db/param/field tags)
  • Cursor-based pagination (Cursor interface)
  • Sort param normalisation (sort_by, sort-by, sortBy, sortby all accepted)
  • FormatQueryForRows — turn INSERT ... VALUES + [][]any into a single parameterised query
  • Typed converters for int/int8/.../uint64, float, string, bool, time, plus their *Arr variants

Installation

go get github.com/downsized-devs/sdk-go/query

Quick Start

type ProductParam struct {
    Name     string `db:"name"     param:"name"     field:"Name"`
    IsActive bool   `db:"is_active" param:"is_active" field:"IsActive"`
    SortBy   string `param:"sort_by"`
    Page     int64  `param:"page"`
    Limit    int64  `param:"limit"`
}

// (Use the SQL builder via the package's exported helpers — see source for the
// current API shape; the builder is reflected into sql.Interface queries.)
Bulk insert
q := "INSERT INTO events(name, payload) VALUES"
full, args, err := query.FormatQueryForRows(ctx, q, [][]any{
    {"login", "{}"},
    {"logout", "{}"},
})
// full == "INSERT INTO events(name, payload) VALUES (?, ?), (?, ?)"
// args == ["login", "{}", "logout", "{}"]

API Reference

Symbol Signature
FormatQueryForRows (ctx, q string, inputs [][]any) (string, []any, error)
Cursor interface { DecodeCursor(string) error; EncodeCursor() (string, error) }
Option { DisableLimit, IsActive, IsInactive bool }
Int, Int64, String, etc. primitive-type constants used by the clause builder.

Error Handling

  • Empty rows or columns → coded error from codes (CodeSQLPrepareStmt).

Dependencies

Testing

go test ./query/...

Eleven test files cover the clause-builder side. Bulk-insert helper has minimal tests.

Contributing

See CONTRIBUTING.md. The "I hate this" comment in query/query.go:12 is a real signal — proposals for a better bulk-insert API are welcome.

  • sql — provides the connection/transaction the builder ultimately runs against.
  • null — nullable types in parameter structs.

Documentation

Index

Constants

View Source
const (
	Int int8 = iota
	IntArr
	Int64
	Int64Arr
	Int32
	Int32Arr
	Int16
	Int16Arr
	Int8
	Int8Arr
	Uint
	UintArr
	Uint64
	Uint64Arr
	Uint32
	Uint32Arr
	Uint16
	Uint16Arr
	Uint8
	Uint8Arr
	String
	StringArr
	Float32
	Float32Arr
	Float64
	Float64Arr
	Bool
	Time
	TimeArr
)
View Source
const (
	MaxFloat64 float64 = math.MaxFloat64
	MaxFloat32 float32 = math.MaxFloat32
	MinFloat64 float64 = math.MaxFloat64 * -1
	MinFloat32 float32 = math.MaxFloat32 * -1
)
View Source
const (
	MaxUint64 = ^uint64(0)
	MinUint64 = uint64(0)
	MaxUint32 = ^uint32(0)
	MinUint32 = uint32(0)
	MaxUint16 = ^uint16(0)
	MinUint16 = uint16(0)
	MaxUint8  = ^uint8(0)
	MinUint8  = uint8(0)
	MaxUint   = ^uint(0)
	MinUint   = uint(0)
	MaxInt64  = int64(MaxUint64 >> 1)
	MinInt64  = -MaxInt64 - 1
	MaxInt32  = int32(MaxUint32 >> 1)
	MinInt32  = -MaxInt32 - 1
	MaxInt16  = int16(MaxUint16 >> 1)
	MinInt16  = -MaxInt16 - 1
	MaxInt8   = int8(MaxUint8 >> 1)
	MinInt8   = -MaxInt8 - 1
	MaxInt    = int(MaxUint >> 1)
	MinInt    = -MaxInt - 1
)

Variables

This section is empty.

Functions

func FormatQueryForRows

func FormatQueryForRows(ctx context.Context, q string, inputs [][]interface{}) (string, []interface{}, error)

FormatQueryForRows I hate this, find a better way for insert many rows

func NewSQLQueryBuilder

func NewSQLQueryBuilder(db sql.Interface, paramTag, dbTag string, option *Option) *sqlClausebuilder

Initiate new query builder object

Types

type Cursor

type Cursor interface {
	DecodeCursor(v string) error
	EncodeCursor() (string, error)
}

type Option

type Option struct {
	DisableLimit bool `form:"disableLimit"`
	IsActive     bool
	IsInactive   bool
}

Jump to

Keyboard shortcuts

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