Condition Token¶
π± Overview¶
The condition.Token type represents a SQL condition (predicate) such as
id = 1, age > 18, or status IN ('active', 'pending').
It provides a structured, validated, and renderable abstraction for WHERE and HAVING clauses.
Built on top of Entiqonβs shared contracts (e.g., Renderable, Validable, Debuggable),
Token ensures safe construction, immutability, and diagnostic visibility of SQL conditions.
Construction Rules¶
Conditions are created using condition.New(...) or convenience helpers:
-
Basic expression
go c := condition.New(ct.Single, "id = 1") // β id = :id, value=1 -
Expression with parameter
go c := condition.New(ct.Single, "id = ?", 123) // β id = :id, value=123 -
Named parameter
go c := condition.New(ct.Single, "id = :id", 123) // β id = :id, value=123 -
Operator with value
go c := condition.New(ct.Single, "age", operator.GreaterThan, 18) // β age > :age, value=18 -
Logical AND / OR ```go c := condition.NewAnd("status = ?", "active") // Kind=And
c = condition.NewOr("deleted = ?", false) // Kind=Or ```
-
Inline collections
go c := condition.New(ct.Single, "id IN (1, 2, 3)") // β id IN (:id), value=[1 2 3] -
Special operators
go c := condition.New(ct.Single, "id IS NULL") // β id IS NULL, value=nil -
Invalid cases
- Empty kind β errored (
invalid condition type) - Empty expression β errored
- Wrong type (non-string expr) β errored
- Invalid operator/value list (e.g.,
INwith empty slice) β errored
Contracts Implemented¶
- Kindable β classification (
Kind(),SetKind()) - Identifiable β core identity (
Input(),Expr(),Name()) - Errorable β
Error(),IsErrored(),SetError() - Debuggable β
Debug()(full diagnostic output) - Rawable β
Raw(),IsRaw() - Renderable β
Render()(SQL form) - Stringable β
String()(concise log form) - Validable β
IsValid()(inverse ofIsErrored())
Examples¶
Example: Simple condition¶
c := condition.New(ct.Single, "id = ?", 42)
fmt.Println(c.Render())
// Output: id = :id
fmt.Println(c.Value())
// Output: 42
Example: Named parameter¶
c := condition.New(ct.Single, "id = :id", 42)
fmt.Println(c.String())
// Output: Condition("id = :id"): name="id", value=42, errored=false
Example: With operator¶
c := condition.New(ct.Single, "age", operator.GreaterThan, 18)
fmt.Println(c.Render())
// Output: age > :age
Example: Logical AND¶
c := condition.NewAnd("status = ?", "active")
fmt.Println(c.Kind())
// Output: And
Example: Debug output¶
c := condition.New(ct.Single, "id", operator.In, []int{1, 2, 3})
fmt.Println(c.Debug())
// Output: Condition{Input="id IN [1 2 3]", Type:"Single", Expression="id IN (:id)", Value=[1 2 3], Error=<nil>}
Example: Invalid expression¶
c := condition.New(ct.Single, "")
fmt.Println(c.Error())
// Output: empty expression
π License¶
MIT β Β© Entiqon Project