qb

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchProcessingError

func BatchProcessingError(recordIndex int, cause error) error

func DialectError

func DialectError(message string, cause error) error

func FieldMappingError

func FieldMappingError(fieldName, dbName string, cause error) error

func InvalidInputError

func InvalidInputError(message string, cause error) error

Common error constructors

func StructParsingError

func StructParsingError(structType reflect.Type, cause error) error

func ValidationError

func ValidationError(message string) error

Types

type AndCondition

type AndCondition struct {
	Conditions []WhereClause
}

AndCondition represents multiple conditions joined by AND

func (AndCondition) Build

func (c AndCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type BetweenCondition

type BetweenCondition struct {
	Field string
	Start any
	End   any
}

BetweenCondition for BETWEEN operator

func (BetweenCondition) Build

func (c BetweenCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type ComparisonCondition

type ComparisonCondition struct {
	LeftField  string
	Operator   string
	RightField string
}

ComparisonCondition for field-to-field comparisons

func (ComparisonCondition) Build

func (c ComparisonCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type InCondition

type InCondition struct {
	Field  string
	Values []any
}

InCondition for IN operator

func (InCondition) Build

func (c InCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type LiteralCondition

type LiteralCondition struct {
	SQL    string
	Values []any
}

LiteralCondition represents a raw SQL fragment with placeholders

func (LiteralCondition) Build

func (c LiteralCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type NullCondition

type NullCondition struct {
	Field  string
	IsNull bool
}

NullCondition for IS NULL / IS NOT NULL

func (NullCondition) Build

func (c NullCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type OrCondition

type OrCondition struct {
	Conditions []WhereClause
}

OrCondition represents multiple conditions joined by OR

func (OrCondition) Build

func (c OrCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type RawCondition

type RawCondition struct {
	SQL string
}

RawCondition for completely raw SQL (no placeholder processing)

func (RawCondition) Build

func (c RawCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type SimpleCondition

type SimpleCondition struct {
	Field    string
	Operator string
	Value    any
}

SimpleCondition represents a single WHERE condition (replaces WhereCondition)

func (SimpleCondition) Build

func (c SimpleCondition) Build(dialect interface {
	Field(string) string
	Placeholder(int) string
}, start int) (string, []any, int)

type SqlBuilder

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

func NewSqlBuilder

func NewSqlBuilder(dialect SqlDialect) *SqlBuilder

func (*SqlBuilder) BuildSQLBatchInsert

func (s *SqlBuilder) BuildSQLBatchInsert(tableName string, data []any) (string, []any, error)

BuildSQLBatchInsert generates a batch INSERT SQL statement for multiple records

Important behavior notes: - All records must have the same struct type - The first record determines which columns to include based on omitnil/omitempty rules - All subsequent records must have the same columns (same omit behavior) - If any record has different columns, an error is returned - Fields with custom mappers are skipped (not supported) - Auto-generated fields are excluded from the INSERT

func (*SqlBuilder) BuildSQLInsert

func (s *SqlBuilder) BuildSQLInsert(tableName string, data any) (string, []any, error)

BuildSQLInsert generates an INSERT SQL statement and parameter list

func (*SqlBuilder) Dialect

func (s *SqlBuilder) Dialect() SqlDialect

func (*SqlBuilder) InsertReturning

func (s *SqlBuilder) InsertReturning(tableName string, data any, returningFields []string) (string, []any, error)

InsertReturning

func (*SqlBuilder) Update

func (s *SqlBuilder) Update(tableName string, record any) *UpdateBuilder

Update creates a new UpdateBuilder instance

type SqlDialect

type SqlDialect struct {
	PlaceHolderFragment   string
	IncludePlaceholderNum bool
	QuoteTable            string
	QuoteField            string
	QuoteSchema           string
	QuoteDatabase         string
	QuoteSeparator        string
}

func DefaultSqlDialect

func DefaultSqlDialect() SqlDialect

func PostgreSQLDialect

func PostgreSQLDialect() SqlDialect

func (SqlDialect) Field

func (d SqlDialect) Field(name string) string

func (SqlDialect) Placeholder

func (d SqlDialect) Placeholder(count int) string

func (SqlDialect) Table

func (d SqlDialect) Table(name string) (string, error)

func (SqlDialect) TableSchema

func (d SqlDialect) TableSchema(schema, name string) string

type SqlError

type SqlError struct {
	Message string
	Cause   error
	Context map[string]string
}

Simple error wrapper that adds context

func NewError

func NewError(message string, cause error) *SqlError

Error constructor

func (*SqlError) Error

func (e *SqlError) Error() string

func (*SqlError) Unwrap

func (e *SqlError) Unwrap() error

func (*SqlError) WithContext

func (e *SqlError) WithContext(key, value string) *SqlError

Add context to error

type UpdateBuilder

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

UpdateBuilder provides a fluent interface for building UPDATE statements with complex WHERE clauses

func (*UpdateBuilder) AddReturning

func (b *UpdateBuilder) AddReturning(fields ...string) *UpdateBuilder

AddReturning adds additional fields to the RETURNING clause

func (*UpdateBuilder) Build

func (b *UpdateBuilder) Build() (string, []any, error)

Build generates the final SQL statement and arguments

func (*UpdateBuilder) ByID

func (b *UpdateBuilder) ByID(id any) *UpdateBuilder

ByID sets WHERE id = value (common pattern)

func (*UpdateBuilder) ExcludeFields

func (b *UpdateBuilder) ExcludeFields(fields ...string) *UpdateBuilder

ExcludeFields excludes specific fields from the update

func (*UpdateBuilder) FieldsValues

func (b *UpdateBuilder) FieldsValues(fieldValues map[string]any) *UpdateBuilder

FieldsValues use only the fields/values for update

func (*UpdateBuilder) HasReturnFields

func (b *UpdateBuilder) HasReturnFields() bool

HasReturnFields returns true if clause has return fields

func (*UpdateBuilder) IncludeFields

func (b *UpdateBuilder) IncludeFields(fields ...string) *UpdateBuilder

IncludeFields includes only specific fields in the update

func (*UpdateBuilder) IncludeZeroValues

func (b *UpdateBuilder) IncludeZeroValues(include bool) *UpdateBuilder

IncludeZeroValues includes fields with zero values

func (*UpdateBuilder) Returning

func (b *UpdateBuilder) Returning(fields ...string) *UpdateBuilder

Returning sets the fields to return after the update

func (*UpdateBuilder) ReturningAll

func (b *UpdateBuilder) ReturningAll() *UpdateBuilder

ReturningAll returns all fields after the update (using *)

func (*UpdateBuilder) Set

func (b *UpdateBuilder) Set(where WhereClause, options *UpdateOptions) *UpdateBuilder

Set allows setting WHERE and OPTIONS in one call for simple cases

func (*UpdateBuilder) UpdateAutoFields

func (b *UpdateBuilder) UpdateAutoFields(update bool) *UpdateBuilder

UpdateAutoFields includes auto fields in the update

func (*UpdateBuilder) Where

func (b *UpdateBuilder) Where(clause WhereClause) *UpdateBuilder

Where sets the WHERE clause for the update

func (*UpdateBuilder) WhereAnd

func (b *UpdateBuilder) WhereAnd(conditions ...WhereClause) *UpdateBuilder

WhereAnd adds an AND condition

func (*UpdateBuilder) WhereAndBuild

func (b *UpdateBuilder) WhereAndBuild(conditions ...WhereClause) (string, []any, error)

Where sets a complex WHERE clause and immediately builds

func (*UpdateBuilder) WhereBetween

func (b *UpdateBuilder) WhereBetween(field string, start, end any) *UpdateBuilder

WhereBetween adds a BETWEEN condition

func (*UpdateBuilder) WhereEq

func (b *UpdateBuilder) WhereEq(field string, value any) *UpdateBuilder

WhereEq adds a simple equality condition

func (*UpdateBuilder) WhereIn

func (b *UpdateBuilder) WhereIn(field string, values ...any) *UpdateBuilder

WhereIn adds an IN condition

func (*UpdateBuilder) WhereLiteral

func (b *UpdateBuilder) WhereLiteral(sql string, values ...any) *UpdateBuilder

WhereLiteral adds a literal SQL condition with placeholders

func (*UpdateBuilder) WhereNotNull

func (b *UpdateBuilder) WhereNotNull(field string) *UpdateBuilder

WhereNotNull adds an IS NOT NULL condition

func (*UpdateBuilder) WhereNull

func (b *UpdateBuilder) WhereNull(field string) *UpdateBuilder

WhereNull adds an IS NULL condition

func (*UpdateBuilder) WhereOr

func (b *UpdateBuilder) WhereOr(conditions ...WhereClause) *UpdateBuilder

WhereOr adds an OR condition

func (*UpdateBuilder) WhereRaw

func (b *UpdateBuilder) WhereRaw(sql string) *UpdateBuilder

WhereRaw adds raw SQL without any processing

func (*UpdateBuilder) WithOptions

func (b *UpdateBuilder) WithOptions(options *UpdateOptions) *UpdateBuilder

WithOptions sets the update options

type UpdateOptions

type UpdateOptions struct {
	// OnlyChanged when true, only includes fields that have changed from their zero values
	OnlyChanged bool

	// IncludeZeroValues when true, includes fields with zero values in the update
	IncludeZeroValues bool

	// ExcludeFields is a list of field names to exclude from the update
	ExcludeFields []string

	// IncludeFields is a list of field names to explicitly include (if specified, only these fields are included)
	IncludeFields []string

	// UpdateAutoFields when true, includes fields marked as auto:true in updates
	UpdateAutoFields bool

	// ReturningFields is a list of field names to return after the update
	ReturningFields []string
}

UpdateOptions configures how UPDATE statements are generated

func DefaultUpdateOptions

func DefaultUpdateOptions() *UpdateOptions

DefaultUpdateOptions returns sensible default options for UPDATE statements

func (*UpdateOptions) ShouldSkipField

func (o *UpdateOptions) ShouldSkipField(structFieldName, dbFieldName string) bool

ShouldSkipField determines if a field should be skipped based on options Accepts both the struct field name and database column name for matching

type WhereClause

type WhereClause interface {
	// Build generates the SQL string and collects values
	// Returns: SQL string, values array, next placeholder number
	Build(dialect interface {
		Field(string) string
		Placeholder(int) string
	}, startPlaceholder int) (sql string, values []any, nextPlaceholder int)
}

WhereClause represents a complex WHERE clause with AND/OR support

func And

func And(conditions ...WhereClause) WhereClause

Helper functions for building WHERE clauses

func Between

func Between(field string, start, end any) WhereClause

func Compare

func Compare(leftField, operator, rightField string) WhereClause

func Cond

func Cond(field, operator string, value any) WhereClause

func Eq

func Eq(field string, value any) WhereClause

Convenience functions for common patterns

func Gt

func Gt(field string, value any) WhereClause

func Gte

func Gte(field string, value any) WhereClause

func In

func In(field string, values ...any) WhereClause

func IsNotNull

func IsNotNull(field string) WhereClause

func IsNull

func IsNull(field string) WhereClause

func Like

func Like(field string, pattern string) WhereClause

func Literal

func Literal(sql string, values ...any) WhereClause

func Lt

func Lt(field string, value any) WhereClause

func Lte

func Lte(field string, value any) WhereClause

func NotEq

func NotEq(field string, value any) WhereClause

func NotLike

func NotLike(field string, pattern string) WhereClause

func Or

func Or(conditions ...WhereClause) WhereClause

func Raw

func Raw(sql string) WhereClause

Jump to

Keyboard shortcuts

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