query

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 12 Imported by: 0

README

query — SQL query/clause builder

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

Stability: Stable. See STABILITY.md.

Dynamic SQL clause builder driven by struct tags. Builds WHERE, ORDER BY, LIMIT, and OFFSET from typed parameter structs.

Features

  • Struct-tag-driven WHERE clause builder (matches db/param/field tags)
  • Sort param normalisation (sort_by, sort-by, sortBy, sortby all accepted)
  • Typed converters for int/int8/.../uint64, float, string, bool, time, plus their *Arr variants

Cursor pagination: the Cursor interface is present as a placeholder for a future implementation; the builder does not yet apply cursor filtering automatically. Callers that need keyset pagination today must append the cursor predicate manually via AddPrefixQuery (or equivalent) on the parameter struct before handing it to the builder.

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

For bulk inserts, use the sql package's NamedExec with a slice of structs (sqlx expands the named placeholders automatically):

type Event struct {
    Name    string `db:"name"`
    Payload string `db:"payload"`
}
events := []Event{{"login", "{}"}, {"logout", "{}"}}
_, err := db.Leader().NamedExec(ctx, "insert-events",
    "INSERT INTO events (name, payload) VALUES (:name, :payload)", events)

API Reference

Symbol Signature
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/...

Test files cover the clause builder, converters, sort normalisation, and cursor encoding.

Contributing

See CONTRIBUTING.md.

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

Documentation

Overview

Package query is a struct-tag-driven SQL clause builder. It turns a typed parameter struct (matching `db` / `param` / `field` tags) into WHERE, ORDER BY, LIMIT, and OFFSET fragments that the sql package can execute.

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 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