builder

package
v1.128.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CTEDef

type CTEDef struct {
	Name      string   // CTE name (will be validated/quoted)
	Columns   []string // optional column list
	SelectSQL string   // the SELECT query (should be built safely)
}

CTEDef defines a safe Common Table Expression

type ConditionGroup

type ConditionGroup struct {
	Conditions []string
	Args       utils.JSON
	// contains filtered or unexported fields
}

ConditionGroup builds a set of AND-joined conditions with unique named parameters. Multiple ConditionGroups can be OR'd together via SelectQueryBuilder.OrGroups.

func NewConditionGroup

func NewConditionGroup(prefix string) *ConditionGroup

func (*ConditionGroup) And

func (cg *ConditionGroup) And(raw string) *ConditionGroup

And adds a raw condition string.

func (*ConditionGroup) Build

func (cg *ConditionGroup) Build() string

Build returns the AND-joined conditions string.

func (*ConditionGroup) Eq

func (cg *ConditionGroup) Eq(field string, value any) *ConditionGroup

Eq adds field = :param with a named parameter.

func (*ConditionGroup) InInt64

func (cg *ConditionGroup) InInt64(field string, values []int64) *ConditionGroup

InInt64 adds field IN (1, 2, 3) — literal integers, no named params needed.

func (*ConditionGroup) InStrings

func (cg *ConditionGroup) InStrings(field string, values []string) *ConditionGroup

InStrings adds field IN (:param0, :param1, ...) with named parameters.

func (*ConditionGroup) OrInStrings

func (cg *ConditionGroup) OrInStrings(fields []string, values []string) *ConditionGroup

OrInStrings adds (f1 IN (:p0,:p1) OR f2 IN (:p0,:p1)) with shared named parameters.

func (*ConditionGroup) SearchLike

func (cg *ConditionGroup) SearchLike(value string, fields ...string) *ConditionGroup

SearchLike adds (LOWER(f1) LIKE LOWER(:arg) OR LOWER(f2) LIKE LOWER(:arg)) with a single named parameter.

type DeleteQueryBuilder

type DeleteQueryBuilder struct {
	SourceName string              // Table name for DELETE FROM
	DbType     base.DXDatabaseType // Database type for syntax differences
	Error      error               // Accumulated error
	Conditions []string            // WHERE conditions
	Args       utils.JSON          // WHERE clause arguments
	OutFields  []string            // RETURNING/OUTPUT fields
}

DeleteQueryBuilder builds DELETE SQL statements with fluent API

func NewDeleteQueryBuilder

func NewDeleteQueryBuilder(dbType base.DXDatabaseType) *DeleteQueryBuilder

NewDeleteQueryBuilder creates a new DeleteQueryBuilder

func NewDeleteQueryBuilderWithSource

func NewDeleteQueryBuilderWithSource(dbType base.DXDatabaseType, tableName string) *DeleteQueryBuilder

NewDeleteQueryBuilderWithSource creates a new DeleteQueryBuilder with table name

func (*DeleteQueryBuilder) And

func (qb *DeleteQueryBuilder) And(condition string) *DeleteQueryBuilder

And adds a raw WHERE condition

func (*DeleteQueryBuilder) BuildOutputClause

func (qb *DeleteQueryBuilder) BuildOutputClause() (string, error)

BuildOutputClause returns the OUTPUT clause string for SQL Server

func (*DeleteQueryBuilder) BuildReturningClause

func (qb *DeleteQueryBuilder) BuildReturningClause() (string, error)

BuildReturningClause returns the RETURNING clause string for PostgreSQL/Oracle

func (*DeleteQueryBuilder) BuildWhereClause

func (qb *DeleteQueryBuilder) BuildWhereClause() (string, utils.JSON, error)

BuildWhereClause returns the WHERE clause string and Args

func (*DeleteQueryBuilder) From

func (qb *DeleteQueryBuilder) From(tableName string) *DeleteQueryBuilder

From sets the table name for DELETE

func (*DeleteQueryBuilder) QuoteIdentifier

func (qb *DeleteQueryBuilder) QuoteIdentifier(identifier string) string

QuoteIdentifier quotes a SQL identifier based on database type

func (*DeleteQueryBuilder) Returning

func (qb *DeleteQueryBuilder) Returning(fields ...string) *DeleteQueryBuilder

Returning specifies fields for RETURNING clause

func (*DeleteQueryBuilder) Where

func (qb *DeleteQueryBuilder) Where(fieldName string, value any) *DeleteQueryBuilder

Where adds a field = value WHERE condition

type HavingConditionDef

type HavingConditionDef struct {
	Expression string         // aggregate expression (e.g., "COUNT(*)", "SUM(amount)")
	Operator   HavingOperator // =, >, <, >=, <=, <>
	Value      any            // parameterized value
	ParamName  string         // parameter name for binding
}

HavingConditionDef defines a safe HAVING condition

type HavingOperator

type HavingOperator string

HavingOperator defines allowed operators for HAVING clause

const (
	HavingOpEq  HavingOperator = "="
	HavingOpNe  HavingOperator = "<>"
	HavingOpGt  HavingOperator = ">"
	HavingOpLt  HavingOperator = "<"
	HavingOpGte HavingOperator = ">="
	HavingOpLte HavingOperator = "<="
)

type InsertQueryBuilder

type InsertQueryBuilder struct {
	SourceName string              // Table name for INSERT INTO
	DbType     base.DXDatabaseType // Database type for syntax differences
	Error      error               // Accumulated error
	SetFields  utils.JSON          // Fields to insert (column -> value)
	OutFields  []string            // RETURNING/OUTPUT fields
}

InsertQueryBuilder builds INSERT SQL statements with fluent API

func NewInsertQueryBuilder

func NewInsertQueryBuilder(dbType base.DXDatabaseType) *InsertQueryBuilder

NewInsertQueryBuilder creates a new InsertQueryBuilder

func NewInsertQueryBuilderWithSource

func NewInsertQueryBuilderWithSource(dbType base.DXDatabaseType, tableName string) *InsertQueryBuilder

NewInsertQueryBuilderWithSource creates a new InsertQueryBuilder with table name

func (*InsertQueryBuilder) BuildOutputClause

func (qb *InsertQueryBuilder) BuildOutputClause() (string, error)

BuildOutputClause returns the OUTPUT clause string for SQL Server

func (*InsertQueryBuilder) BuildReturningClause

func (qb *InsertQueryBuilder) BuildReturningClause() (string, error)

BuildReturningClause returns the RETURNING clause string for PostgreSQL/Oracle

func (*InsertQueryBuilder) Into

func (qb *InsertQueryBuilder) Into(tableName string) *InsertQueryBuilder

Into sets the table name for INSERT

func (*InsertQueryBuilder) QuoteIdentifier

func (qb *InsertQueryBuilder) QuoteIdentifier(identifier string) string

QuoteIdentifier quotes a SQL identifier based on database type

func (*InsertQueryBuilder) Returning

func (qb *InsertQueryBuilder) Returning(fields ...string) *InsertQueryBuilder

Returning specifies fields for RETURNING clause

func (*InsertQueryBuilder) Set

func (qb *InsertQueryBuilder) Set(fieldName string, value any) *InsertQueryBuilder

Set adds a field-value pair to insert

func (*InsertQueryBuilder) SetAll

func (qb *InsertQueryBuilder) SetAll(fields utils.JSON) *InsertQueryBuilder

SetAll adds multiple field-value pairs to insert

type JoinDef

type JoinDef struct {
	Type    JoinType // INNER, LEFT, RIGHT, FULL
	Table   string   // table/view name (will be quoted)
	Alias   string   // optional alias
	OnLeft  string   // left field of ON condition (can include table prefix)
	OnRight string   // right field of ON condition (can include table prefix)
}

JoinDef defines a safe JOIN clause

type JoinType

type JoinType string

JoinType defines the type of SQL JOIN

const (
	JoinTypeInner JoinType = "INNER"
	JoinTypeLeft  JoinType = "LEFT"
	JoinTypeRight JoinType = "RIGHT"
	JoinTypeFull  JoinType = "FULL"
)

type OrderByDef

type OrderByDef struct {
	FieldName     string // field name (will be validated and quoted)
	Direction     string // "asc" or "desc" (use databases.DXOrderByDirection* constants)
	NullPlacement string // "first" or "last" (use databases.DXOrderByNullPlacement* constants), empty for default
}

OrderByDef defines a safe ORDER BY clause

type SelectQueryBuilder

type SelectQueryBuilder struct {
	// Source name for FROM clause (table or view name)
	SourceName string

	// WHERE clause building
	Conditions []string
	Args       utils.JSON
	DbType     base.DXDatabaseType
	Error      error

	// Extended SQL clause building
	Joins            []JoinDef
	CTEs             []CTEDef
	GroupByFields    []string
	HavingConditions []HavingConditionDef
	OrderByDefs      []OrderByDef
	RawOrderBys      []string // Raw ORDER BY expressions (e.g., PostGIS distance)
	OutFields        []string // OutFields for SELECT or RETURNING clause
	LimitValue       int64    // LIMIT clause value (0 = no limit)
	OffsetValue      int64    // OFFSET clause value (0 = no offset)
	ForUpdatePart    any      // FOR UPDATE clause (string like "FOR UPDATE", or bool true)
	// contains filtered or unexported fields
}

SelectQueryBuilder builds SQL clauses with fluent API

func NewSelectQueryBuilder

func NewSelectQueryBuilder(dbType base.DXDatabaseType) *SelectQueryBuilder

NewSelectQueryBuilder creates a new SelectQueryBuilder

func NewSelectQueryBuilderWithSource

func NewSelectQueryBuilderWithSource(dbType base.DXDatabaseType, sourceName string) *SelectQueryBuilder

NewSelectQueryBuilderWithSource creates a new SelectQueryBuilder with source name (table or view)

func (*SelectQueryBuilder) AddOrderBy

func (qb *SelectQueryBuilder) AddOrderBy(fieldName string, direction string, nullPlacement string) *SelectQueryBuilder

AddOrderBy adds an ORDER BY clause with field name, direction, and optional null placement direction: "asc" or "desc" nullPlacement: "first" or "last" (empty for default) Note: This method does NOT validate against allowed fields - use TableSelectQueryBuilder for that

func (*SelectQueryBuilder) AddOrderByRaw

func (qb *SelectQueryBuilder) AddOrderByRaw(rawExpr string) *SelectQueryBuilder

AddOrderByRaw adds a raw ORDER BY expression (e.g., PostGIS distance expressions). No validation is performed on the expression — caller is responsible for safety.

func (*SelectQueryBuilder) And

func (qb *SelectQueryBuilder) And(condition string) *SelectQueryBuilder

And adds a raw condition with AND

func (*SelectQueryBuilder) AndWithParam

func (qb *SelectQueryBuilder) AndWithParam(condition string, paramName string, paramValue any) *SelectQueryBuilder

AndWithParam adds a condition with a single parameter

func (*SelectQueryBuilder) AndWithParams

func (qb *SelectQueryBuilder) AndWithParams(condition string, params map[string]any) *SelectQueryBuilder

AndWithParams adds a condition with multiple parameters

func (*SelectQueryBuilder) Build

func (qb *SelectQueryBuilder) Build() (string, utils.JSON, error)

Build returns the WHERE clause string and Args

func (*SelectQueryBuilder) BuildCTEClause

func (qb *SelectQueryBuilder) BuildCTEClause() (string, error)

BuildCTEClause returns the WITH clause string

func (*SelectQueryBuilder) BuildGroupByClause

func (qb *SelectQueryBuilder) BuildGroupByClause() (string, error)

BuildGroupByClause returns the GROUP BY clause string

func (*SelectQueryBuilder) BuildHavingClause

func (qb *SelectQueryBuilder) BuildHavingClause() (string, utils.JSON, error)

BuildHavingClause returns the HAVING clause and additional args

func (*SelectQueryBuilder) BuildInClause

func (qb *SelectQueryBuilder) BuildInClause(fieldName string, values any) string

BuildInClause builds an IN clause for the given field and values

func (*SelectQueryBuilder) BuildJoinClause

func (qb *SelectQueryBuilder) BuildJoinClause() (string, error)

BuildJoinClause returns the JOIN clause string

func (*SelectQueryBuilder) BuildOrderByClause

func (qb *SelectQueryBuilder) BuildOrderByClause() (string, error)

BuildOrderByClause returns the ORDER BY clause string from OrderByDefs and RawOrderBys

func (*SelectQueryBuilder) BuildOutputClause

func (qb *SelectQueryBuilder) BuildOutputClause(prefix string) (string, error)

BuildOutputClause returns the OUTPUT clause string for SQL Server prefix should be "INSERTED" for INSERT, "DELETED" for DELETE, or "DELETED"/"INSERTED" for UPDATE

func (*SelectQueryBuilder) BuildReturningClause

func (qb *SelectQueryBuilder) BuildReturningClause() (string, error)

BuildReturningClause returns the RETURNING clause string for PostgreSQL/MariaDB For SQL Server OUTPUT clause, use BuildOutputClause instead

func (*SelectQueryBuilder) BuildWithPrefix

func (qb *SelectQueryBuilder) BuildWithPrefix(existingWhere string) (string, utils.JSON, error)

BuildWithPrefix returns WHERE clause with prefix for combining with existing Conditions

func (*SelectQueryBuilder) ForUpdate

func (qb *SelectQueryBuilder) ForUpdate() *SelectQueryBuilder

ForUpdate sets the FOR UPDATE clause

func (*SelectQueryBuilder) FullJoin

func (qb *SelectQueryBuilder) FullJoin(table, alias, onLeft, onRight string) *SelectQueryBuilder

FullJoin adds a FULL OUTER JOIN clause

func (*SelectQueryBuilder) GenerateParamName

func (qb *SelectQueryBuilder) GenerateParamName(fieldName string) string

GenerateParamName creates unique parameter names for filtering

func (*SelectQueryBuilder) GroupBy

func (qb *SelectQueryBuilder) GroupBy(fields ...string) *SelectQueryBuilder

GroupBy adds GROUP BY fields

func (*SelectQueryBuilder) Having

func (qb *SelectQueryBuilder) Having(expression string, operator HavingOperator, value any) *SelectQueryBuilder

Having adds a HAVING condition with parameterized value expression: aggregate expression like "COUNT(*)", "SUM(amount)", "MAX(price)" operator: comparison operator (use HavingOp* constants) value: the value to compare against (will be parameterized)

func (*SelectQueryBuilder) Join

func (qb *SelectQueryBuilder) Join(table, onLeft, onRight string) *SelectQueryBuilder

Join adds an INNER JOIN clause

func (*SelectQueryBuilder) JoinWithAlias

func (qb *SelectQueryBuilder) JoinWithAlias(table, alias, onLeft, onRight string) *SelectQueryBuilder

JoinWithAlias adds an INNER JOIN with alias

func (*SelectQueryBuilder) LeftJoin

func (qb *SelectQueryBuilder) LeftJoin(table, alias, onLeft, onRight string) *SelectQueryBuilder

LeftJoin adds a LEFT JOIN clause

func (*SelectQueryBuilder) Limit

func (qb *SelectQueryBuilder) Limit(limit int64) *SelectQueryBuilder

Limit sets the LIMIT clause value

func (*SelectQueryBuilder) NotIn

func (qb *SelectQueryBuilder) NotIn(fieldName string, values any) *SelectQueryBuilder

NotIn adds field NOT IN (values) condition

func (*SelectQueryBuilder) Offset

func (qb *SelectQueryBuilder) Offset(offset int64) *SelectQueryBuilder

Offset sets the OFFSET clause value

func (*SelectQueryBuilder) OrGroups

func (qb *SelectQueryBuilder) OrGroups(groups []*ConditionGroup) *SelectQueryBuilder

OrGroups adds multiple ConditionGroups joined by OR. Each group is AND-joined internally, then groups are OR'd together. Single group → no extra outer parens.

func (*SelectQueryBuilder) QuoteFieldWithPrefix

func (qb *SelectQueryBuilder) QuoteFieldWithPrefix(field string) string

QuoteFieldWithPrefix quotes a field that may have table prefix (e.g., "t.field_name" -> "t"."field_name")

func (*SelectQueryBuilder) QuoteIdentifier

func (qb *SelectQueryBuilder) QuoteIdentifier(identifier string) string

QuoteIdentifier quotes a SQL identifier based on database type to prevent SQL injection

func (*SelectQueryBuilder) RightJoin

func (qb *SelectQueryBuilder) RightJoin(table, alias, onLeft, onRight string) *SelectQueryBuilder

RightJoin adds a RIGHT JOIN clause

func (*SelectQueryBuilder) Select

func (qb *SelectQueryBuilder) Select(fields ...string) *SelectQueryBuilder

Select specifies fields for SELECT or RETURNING clause

func (*SelectQueryBuilder) WithCTE

func (qb *SelectQueryBuilder) WithCTE(name string, columns []string, selectSQL string) *SelectQueryBuilder

WithCTE adds a Common Table Expression Note: selectSQL should be built using safe methods, not raw user input

type UpdateQueryBuilder

type UpdateQueryBuilder struct {
	SourceName string              // Table name for UPDATE
	DbType     base.DXDatabaseType // Database type for syntax differences
	Error      error               // Accumulated error
	SetFields  utils.JSON          // Fields to update (column -> value)
	Conditions []string            // WHERE conditions
	Args       utils.JSON          // WHERE clause arguments
	OutFields  []string            // RETURNING/OUTPUT fields
}

UpdateQueryBuilder builds UPDATE SQL statements with fluent API

func NewUpdateQueryBuilder

func NewUpdateQueryBuilder(dbType base.DXDatabaseType) *UpdateQueryBuilder

NewUpdateQueryBuilder creates a new UpdateQueryBuilder

func NewUpdateQueryBuilderWithSource

func NewUpdateQueryBuilderWithSource(dbType base.DXDatabaseType, tableName string) *UpdateQueryBuilder

NewUpdateQueryBuilderWithSource creates a new UpdateQueryBuilder with table name

func (*UpdateQueryBuilder) And

func (qb *UpdateQueryBuilder) And(condition string) *UpdateQueryBuilder

And adds a raw WHERE condition

func (*UpdateQueryBuilder) BuildOutputClause

func (qb *UpdateQueryBuilder) BuildOutputClause(prefix string) (string, error)

BuildOutputClause returns the OUTPUT clause string for SQL Server prefix should be "INSERTED" for new values or "DELETED" for old values

func (*UpdateQueryBuilder) BuildReturningClause

func (qb *UpdateQueryBuilder) BuildReturningClause() (string, error)

BuildReturningClause returns the RETURNING clause string for PostgreSQL/Oracle

func (*UpdateQueryBuilder) BuildWhereClause

func (qb *UpdateQueryBuilder) BuildWhereClause() (string, utils.JSON, error)

BuildWhereClause returns the WHERE clause string and Args

func (*UpdateQueryBuilder) QuoteIdentifier

func (qb *UpdateQueryBuilder) QuoteIdentifier(identifier string) string

QuoteIdentifier quotes a SQL identifier based on database type

func (*UpdateQueryBuilder) Returning

func (qb *UpdateQueryBuilder) Returning(fields ...string) *UpdateQueryBuilder

Returning specifies fields for RETURNING clause

func (*UpdateQueryBuilder) Set

func (qb *UpdateQueryBuilder) Set(fieldName string, value any) *UpdateQueryBuilder

Set adds a field-value pair to update

func (*UpdateQueryBuilder) SetAll

func (qb *UpdateQueryBuilder) SetAll(fields utils.JSON) *UpdateQueryBuilder

SetAll adds multiple field-value pairs to update

func (*UpdateQueryBuilder) Table

func (qb *UpdateQueryBuilder) Table(tableName string) *UpdateQueryBuilder

Table sets the table name for UPDATE

func (*UpdateQueryBuilder) Where

func (qb *UpdateQueryBuilder) Where(fieldName string, value any) *UpdateQueryBuilder

Where adds a field = value WHERE condition

Jump to

Keyboard shortcuts

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