Documentation
¶
Index ¶
- Constants
- type Builder
- type Case
- type CaseNext
- type Column
- type ColumnBuilder
- type ColumnType
- type ColumnWriter
- type Condition
- type ConditionNext
- type DeleteStmt
- type Engine
- func (e *Engine) NewDelete(ctx context.Context, table string) (DeleteStmt, ColumnBuilder)
- func (e *Engine) NewInsert(ctx context.Context, table string) (InsertStmt, ColumnBuilder)
- func (e *Engine) NewSelect(ctx context.Context, table string) (SelectStmt, ColumnBuilder)
- func (e *Engine) NewUpdate(ctx context.Context, table string) (UpdateStmt, ColumnBuilder)
- type Executor
- type From
- type GroupBy
- type Having
- type Insert
- type InsertStmt
- type Join
- type JoinEnd
- type JoinNext
- type Limit
- type Offset
- type Options
- type OrderBy
- type OrderByNext
- type Prepare
- type Scanner
- type Select
- type SelectNext
- type SelectStmt
- type Update
- type UpdateStmt
- type Value
- type Values
- type Where
Constants ¶
const ( TypeColumnDef = core.ColTypDef TypeColumnExpr = core.ColTypExpr TypeColumnAlias = core.ColTypAlias TypeColumnBase = core.ColTypBase )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Case ¶
type Case interface {
// When starts a CASE WHEN block
//
// @SQL: CASE WHEN `when`
When(when Value) CaseNext
// Else writes the ELSE value
//
// @SQL: ELSE value END
Else(els Value)
}
Case represents a SQL CASE expression, allowing conditional branching inside statements.
type CaseNext ¶
CaseNext represents the THEN part of a CASE WHEN branch, allowing chained WHEN/THEN blocks.
type Column ¶
type Column interface {
// Def writes the definition of the column
//
// e.g: if we have this column being built: COUNT(id) AS count:
// - Def() would write: COUNT(id) AS count; Fallbacks: Expr() -> Base()
// - Expr() would write: COUNT(id); Fallbacks: Base()
// - Alias() would write: count; Fallbacks: Expr() -> Base()
// - Base() would write: id Fallbacks: none
//
// @SQL: used to build column definitions
Def() Builder
// Expr writes the expression of the column
//
// e.g: if we have this column being built: COUNT(id) AS count:
// - Def() would write: COUNT(id) AS count; Fallbacks: Expr() -> Base()
// - Expr() would write: COUNT(id); Fallbacks: Base()
// - Alias() would write: count; Fallbacks: Expr() -> Base()
// - Base() would write: id Fallbacks: none
Expr() Builder
// Alias writes the column alias
//
// e.g: if we have this column being built: COUNT(id) AS count:
// - Def() would write: COUNT(id) AS count; Fallbacks: Expr() -> Base()
// - Expr() would write: COUNT(id); Fallbacks: Base()
// - Alias() would write: count; Fallbacks: Expr() -> Base()
// - Base() would write: id Fallbacks: none
Alias() Builder
// Base writes the intrinsic value of the column
//
// If this column was generated using a complex builder
// such as Concat() or Search(), this method does nothing,
// and that may break the statement.
//
// e.g: if we have this column being built: COUNT(id) AS count:
// - Def() would write: COUNT(id) AS count; Fallbacks: Expr() -> Base()
// - Expr() would write: COUNT(id); Fallbacks: Base()
// - Alias() would write: count; Fallbacks: Expr() -> Base()
// - Base() would write: id Fallbacks: none
Base() Builder
// Count writes COUNT(column) or COUNT(DISTINCT column)
//
// @SQL: COUNT([SELF]) / COUNT(DISTINCT [SELF])
Count(distinct ...bool) Column
// Sum writes SUM(column) or SUM(DISTINCT column)
//
// @SQL: SUM([SELF])
Sum(distinct ...bool) Column
// Min writes MIN(column)
//
// @SQL: MIN([SELF])
Min(distinct ...bool) Column
// Max writes MAX(column)
//
// @SQL: MAX([SELF])
Max(distinct ...bool) Column
// Avg writes AVG(column)
//
// @SQL: AVG([SELF])
Avg(distinct ...bool) Column
// Add writes column + value
//
// @SQL: [SELF] + value
Add(v Value) Column
// Sub writes column - value
//
// @SQL: [SELF] - value
Sub(v Value) Column
// Mul writes column * value
//
// @SQL: [SELF] * value
Mul(v Value) Column
// Div writes column / value
//
// @SQL: [SELF] / value
Div(v Value) Column
// Mod writes column % value
//
// @SQL: [SELF] % value
Mod(v Value) Column
// Wrap wraps the entire column expression in parentheses
//
// @SQL: ([SELF])
Wrap() Column
// Lower writes LOWER(column)
//
// @SQL: LOWER([SELF])
Lower() Column
// Upper writes UPPER(column)
//
// @SQL: UPPER([SELF])
Upper() Column
// Trim writes TRIM(column)
//
// @SQL: TRIM([SELF])
Trim() Column
// Round writes ROUND(column)
//
// @SQL: ROUND([SELF])
Round() Column
// Abs writes ABS(column)
//
// @SQL: ABS([SELF])
Abs() Column
// As writes an alias for the column
//
// @SQL: [SELF] AS `name`
As(name string) Column
}
Column represents a selectable or computable SQL column. Provides builders for expressions, aliases, aggregates, arithmetic operations, and transformations.
type ColumnBuilder ¶
type ColumnBuilder interface {
// Col creates a new column reference
//
// @SQL: `internal_alias`.`col`
Col(name string, table ...string) Column
// All writes the wildcard *
//
// You can specify a table to provide context when joins exist.
// If not specified, the default table will be used.
//
// @SQL: *
All(table ...string) Column
// Param writes a placeholder
//
// You can omit the value for lazy parameters, useful in prepared statements.
//
// @SQL: ? / $[number]
Param(value ...Value) Builder
// Cond starts a condition block with the given identifier
//
// @SQL: `ident` ... [Condition]
Cond(ident Value) Condition
// Concat writes CONCAT(...)
//
// @SQL: CONCAT(val1, val2, ...)
Concat(v ...Value) Column
// Switch writes a simple CASE expression for this column.
//
// The argument `cond` is evaluated and compared against literals
// in each WHEN branch.
//
// @SQL:
// CASE `cond`
// WHEN compartion1 THEN ...
// WHEN comparation2 THEN ...
// ELSE ...
// END
Switch(cond Value, fn func(cs Case)) Column
// Search writes a searched CASE expression for this column.
//
// Each WHEN branch can use Cond() to define a boolean condition.
//
// @SQL:
// CASE
// WHEN boolean1 THEN ...
// WHEN boolean2 THEN ...
// ELSE ...
// END
Search(fn func(cs Case)) Column
}
ColumnBuilder provides constructors for columns, placeholders, conditions, concatenations, and CASE-based expressions.
type ColumnType ¶
type ColumnType = core.ColumnType
type ColumnWriter ¶
type ColumnWriter = core.ColumnWriter
type Condition ¶
type Condition interface {
// Not negates the current condition
//
// @SQL: NOT (`cond`)
Not() Condition
// Equal writes column = value
//
// @SQL: `col` = value
Equal(v Value) ConditionNext
// Like writes column LIKE value
//
// @SQL: `col` LIKE value
Like(v Value) ConditionNext
// Greater writes column > value
//
// @SQL: `col` > value
Greater(v Value) ConditionNext
// Less writes column < value
//
// @SQL: `col` < value
Less(v Value) ConditionNext
// In writes column IN (...)
//
// @SQL: `col` IN (value1, value2, ...)
In(v []Value) ConditionNext
// Between writes column BETWEEN min AND max
//
// @SQL: `col` BETWEEN min AND max
Between(minV, maxV Value) ConditionNext
// IsNull writes column IS NULL
//
// @SQL: `col` IS NULL
IsNull() ConditionNext
}
Condition represents a SQL condition used in WHERE and HAVING. It supports comparisons, pattern matching, ranges, null checks, and negation.
type ConditionNext ¶
type ConditionNext interface {
// And links the next condition with AND
//
// @SQL: [Condition] ... AND `ident` ... [Condition]
And(ident Value) Condition
// Or links the next condition with OR
//
// @SQL: [Condition] ... OR `ident` ... [Condition]
Or(ident Value) Condition
}
ConditionNext represents the continuation of a condition, allowing logical chaining with AND / OR.
type DeleteStmt ¶ added in v0.1.1
DeleteStmt represents a full DELETE statement
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) NewDelete ¶ added in v0.1.1
func (e *Engine) NewDelete(ctx context.Context, table string) (DeleteStmt, ColumnBuilder)
func (*Engine) NewInsert ¶ added in v0.1.1
func (e *Engine) NewInsert(ctx context.Context, table string) (InsertStmt, ColumnBuilder)
func (*Engine) NewSelect ¶
func (e *Engine) NewSelect(ctx context.Context, table string) (SelectStmt, ColumnBuilder)
func (*Engine) NewUpdate ¶ added in v0.1.1
func (e *Engine) NewUpdate(ctx context.Context, table string) (UpdateStmt, ColumnBuilder)
type From ¶
type From interface {
// From writes the FROM clause
//
// # This method is auto-generated for the statement’s default table.
// Only use it for complex FROM clauses (such as subqueries)
//
// @SQL: FROM `table`
From(table string)
}
From represents the FROM clause, defining the statement's source table or subquery.
type GroupBy ¶
type GroupBy interface {
// GroupBy writes the GROUP BY clause
//
// @SQL: GROUP BY `group1`, `group2`, `group3` ...
GroupBy(group ...Value)
}
GroupBy represents the GROUP BY clause, grouping the selected rows.
type Having ¶
type Having interface {
// Having writes the HAVING clause
//
// @SQL: HAVING `having` ... [Condition]
Having(having Value) Condition
}
Having represents the HAVING clause, filtering grouped results using conditions.
type Insert ¶ added in v0.1.1
type Insert interface {
// Insert specifies the columns to insert and returns a Values handler.
//
// The argument can be:
// - a struct
// - a map[string]any
// - individual columns
// - any type where columns can be read dynamically
//
// Example SQL: INSERT INTO `table` (`columns`) VALUES (...)
Insert(reference ...Value) Values
}
Insert represents the INSERT INTO clause, defining data to insert into a table.
type InsertStmt ¶ added in v0.1.1
InsertStmt represents a full INSERT statement
type Join ¶
type Join interface {
// Join writes the JOIN clause
//
// @SQL: INNER JOIN `table 'alias'` ... [JoinNext]
Join(table string) JoinNext
}
Join represents the JOIN clause, initiating a table join operation.
type JoinNext ¶
type JoinNext interface {
// Left changes the current join type to LEFT JOIN.
//
// @SQL: LEFT JOIN `table` ... [JoinEnd]
Left() JoinEnd
// Right changes the current join type to RIGHT JOIN.
//
// @SQL: RIGHT JOIN `table` ... [JoinEnd]
Right() JoinEnd
// Full changes the current join type to FULL JOIN.
//
// @SQL: FULL JOIN `table` ... [JoinEnd]
Full() JoinEnd
// Cross changes the current join type to CROSS JOIN.
//
// CROSS JOIN does not use an ON clause.
//
// @SQL: CROSS JOIN `table`
Cross()
// On writes the join condition
//
// @SQL: [join] ... ON `on` ... [Condition]
On(on Value) Condition
}
JoinNext represents the ON clause for a join, returning a condition builder.
type Limit ¶
type Limit interface {
// Limit writes the LIMIT clause value
//
// # Values less than 1 will be ignored
//
// @SQL: LIMIT `limit`
Limit(limit int)
}
Limit represents the LIMIT clause, restricting the maximum number of returned rows.
type Offset ¶
type Offset interface {
// Offset writes the OFFSET clause value
//
// # Values less than 1 will be ignored
//
// @SQL: OFFSET `offset`
Offset(offset int)
}
Offset represents the OFFSET clause, skipping a number of rows before returning results.
type Options ¶
type Options struct {
// ColumnWriter
// Allows selecting the default way a column would be written
//
// the default configuration is:
// - Select = TypeColumnDef
// - Join = TypeColumnBase
// - Where = TypeColumnExpr
// - GroupBy = TypeColumnExpr
// - Having = TypeColumnExpr
// - OrderBy = TypeColumnAlias
ColumnWriter ColumnWriter
// ColumnTag
// Defines the tag that the mapper uses to read and bind values.
//
// Default:
// - "col"
ColumnTag string
// Limit
// Sets an auto limit clause if the provided value is greater than 0
Limit int
// MaxLimit
// If the provided limit is greater than the maximum, it will be capped at this value
MaxLimit int
}
type OrderBy ¶
type OrderBy interface {
// OrderBy writes the ORDER BY clause
//
// # Can be called multiple times for multiple orderings
//
// @SQL: ORDER BY `order` ... [OrderByNext]
OrderBy(order Value) OrderByNext
}
OrderBy represents the ORDER BY clause, sorting rows by one or more identifiers.
type OrderByNext ¶
type OrderByNext interface {
// Desc writes the descending direction for the current order
//
// @SQL: [OrderBy] ... DESC
Desc()
}
OrderByNext represents ordering modifiers, such as descending direction.
type Select ¶
type Select interface {
// Distinct writes DISTINCT in the SELECT clause
//
// @SQL: SELECT `DISTINCT` ...
Distinct() SelectNext
// Select writes the SELECT clause values
//
// @SQL: SELECT `DISTINCT?` `sel1`, `sel2`, `sel3` ... [SelectNext]
Select(sel ...Value)
}
Select represents the SELECT clause of a statement, supporting DISTINCT and selectable identifiers.
type SelectNext ¶
type SelectNext interface {
// Select writes the SELECT clause values
//
// @SQL: SELECT `DISTINCT?` `sel1`, `sel2`, `sel3` ... [SelectNext]
Select(sel ...Value)
}
SelectNext represents additional chained SELECT values when DISTINCT is already applied.
type SelectStmt ¶
SelectStmt represents a full SELECT statement
type Update ¶ added in v0.1.1
type Update interface {
// Update specifies the columns to update and returns a Values handler.
//
// The argument can be:
// - a struct
// - a map[string]any
// - individual columns
// - any type where columns can be read dynamically
//
// Example SQL: UPDATE `table` SET `column` = ...
Update(reference ...Value) Values
}
Update represents the UPDATE clause, defining data to update in a table.
type UpdateStmt ¶ added in v0.1.1
UpdateStmt represents a full UPDATE statement
type Values ¶ added in v0.1.1
type Values interface {
// Values sets the actual values to insert or update.
//
// Always provide the raw values; placeholders ("?") are handled internally.
// You can omit this call if using prepared columns without values.
Values(values Value)
}
Values represents the VALUES clause for INSERT or the assignment values for UPDATE.
type Where ¶
type Where interface {
// Where writes the WHERE clause
//
// # Can be called multiple times,
// Conditions are combined using the logical "AND".
// e.g: (cond1) AND (cond2) AND (cond3) ...
//
// @SQL: WHERE `ident` ... [Condition]
Where(ident Value) Condition
}
Where represents the WHERE clause, supporting multiple AND-combined conditions.