Documentation
¶
Overview ¶
Package trigger builds database triggers as type-checked Go values, independent of any SQL dialect. BeforeInsert/BeforeUpdate/ BeforeDelete/AfterInsert/AfterUpdate/AfterDelete start a trigger on a gorm model; Set/SetColumns assign NEW columns, validated against the model's real gorm schema at Build time, and Body supplies raw SQL for anything Set can't express -- required for DELETE triggers, since there's no NEW row to assign into.
tr := trigger.BeforeUpdate(&User{}).Set("updated_at", trigger.Now())
A built Definition is dialect-agnostic; github.com/bg12345/gorm-dbobjects's Client resolves it to the connected engine's actual DDL.
Example ¶
package main
import (
"fmt"
"github.com/bg12345/gorm-dbobjects/trigger"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string
}
func main() {
tr := trigger.BeforeUpdate(&User{}).
Set("updated_at", trigger.Now())
def, err := tr.Build()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(def.Table, def.Timing, def.Event)
}
Output: users BEFORE UPDATE
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Definition ¶
type Definition struct {
Table string
Timing string // "BEFORE" or "AFTER"
Event string // "INSERT", "UPDATE", or "DELETE"
Sets map[string]Expr
Body string // raw full trigger body; required for DELETE (no NEW row to assign into)
Name string // optional; if empty, dbobjects will generate one
// PrimaryKey is the table's single primary key column, resolved
// best-effort whenever Sets is non-empty (regardless of Timing) and
// the table has exactly one primary key field; empty otherwise. Used
// by dialects that render Set() as a follow-up UPDATE targeting this
// column instead of NEW.col = expr -- Postgres/MySQL for AFTER
// triggers (NEW isn't assignable once an AFTER trigger fires, the
// row's already written) and SQL Server for every Set()-based
// trigger (SQL Server has no BEFORE DML trigger at all, so Set()
// always renders AFTER there). Unused by SQLite, which targets
// rowid instead. See dialect.go's renderTrigger per dialect.
PrimaryKey string
}
Definition is the DB-agnostic description of a trigger, produced by Build() and consumed by whatever applies it (e.g. dbobjects.Register).
type Expr ¶
Expr represents a value assigned to a column inside the trigger body. Raw is written into the generated SQL verbatim, so callers should only build it via Now()/Raw() rather than embedding untrusted input.
type ExprKind ¶
type ExprKind int
ExprKind distinguishes what an Expr represents, so a dialect can render it in its own engine's spelling (Postgres/MySQL's NOW() vs SQL Server's SYSUTCDATETIME() vs Oracle's SYSDATE, for ExprNow) instead of Expr baking in one dialect's spelling itself.
type Trigger ¶
type Trigger interface {
Kind() string
Set(column string, value Expr) Trigger
SetColumns(columns map[string]Expr) Trigger
Body(sql string) Trigger
Name(n string) Trigger
Build() (*Definition, error)
}