Files
upstream-sqlc/internal/sql/ast/on_conflict_clause.go
upstream-mirror 0f4df23290
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 02:17:34 +05:00

76 lines
1.8 KiB
Go

package ast
import "github.com/sqlc-dev/sqlc/internal/sql/format"
type OnConflictClause struct {
Tag NodeTag[OnConflictClause] `json:"tag"`
Action OnConflictAction `json:"action"`
Infer *InferClause `json:"infer,omitempty"`
TargetList *List `json:"target_list,omitempty"`
WhereClause Node `json:"where_clause,omitempty"`
Location int `json:"location"`
}
func (n *OnConflictClause) Pos() int {
return n.Location
}
// OnConflictAction values matching pg_query_go
const (
OnConflictActionUndefined OnConflictAction = 0
OnConflictActionNone OnConflictAction = 1
OnConflictActionNothing OnConflictAction = 2
OnConflictActionUpdate OnConflictAction = 3
)
func (n *OnConflictClause) Format(buf *TrackedBuffer, d format.Dialect) {
if n == nil {
return
}
buf.WriteString("ON CONFLICT ")
if n.Infer != nil {
buf.astFormat(n.Infer, d)
buf.WriteString(" ")
}
switch n.Action {
case OnConflictActionNothing:
buf.WriteString("DO NOTHING")
case OnConflictActionUpdate:
buf.WriteString("DO UPDATE SET")
buf.Group()
buf.Indent()
// Format as assignment list: name = val, one per line when the
// author broke the list (or a comment forces it open).
if n.TargetList != nil {
for i, item := range n.TargetList.Items {
if i == 0 {
buf.boundary(item)
buf.Line()
} else {
buf.WriteString(",")
buf.boundary(item)
buf.Line()
}
if rt, ok := item.(*ResTarget); ok {
if rt.Name != nil {
buf.WriteString(*rt.Name)
}
buf.WriteString(" = ")
buf.astFormat(rt.Val, d)
} else {
buf.astFormat(item, d)
}
}
}
buf.EndIndent()
if set(n.WhereClause) {
buf.boundary(n.WhereClause)
buf.Line()
buf.WriteString("WHERE ")
buf.condition(n.WhereClause, d)
}
buf.EndGroup()
}
}