trigger

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

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, set only when
	// Timing is AFTER and Sets is non-empty. NEW isn't assignable once an
	// AFTER trigger fires (the row's already written), so the dialect
	// renders Set() there as an explicit follow-up UPDATE targeting this
	// column instead of NEW.col = expr -- see dialect.go's renderTrigger.
	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

type Expr struct {
	Kind ExprKind
	Raw  string
}

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.

func Now

func Now() Expr

Now renders as the Postgres NOW() function.

func Raw

func Raw(sql string) Expr

Raw renders sql verbatim in the trigger body, e.g. Raw("version + 1").

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.

const (
	ExprRaw ExprKind = iota // zero value: Raw holds the literal SQL to emit verbatim
	ExprNow                 // "current timestamp" -- each dialect resolves its own spelling
)

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)
}

func AfterDelete added in v0.2.0

func AfterDelete(model any) Trigger

func AfterInsert added in v0.2.0

func AfterInsert(model any) Trigger

func AfterUpdate added in v0.2.0

func AfterUpdate(model any) Trigger

func BeforeDelete added in v0.2.0

func BeforeDelete(model any) Trigger

func BeforeInsert

func BeforeInsert(model any) Trigger

func BeforeUpdate

func BeforeUpdate(model any) Trigger

Jump to

Keyboard shortcuts

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