on_conflict.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package clause
  2. type OnConflict struct {
  3. Columns []Column
  4. Where Where
  5. OnConstraint string
  6. DoNothing bool
  7. DoUpdates Set
  8. UpdateAll bool
  9. }
  10. func (OnConflict) Name() string {
  11. return "ON CONFLICT"
  12. }
  13. // Build build onConflict clause
  14. func (onConflict OnConflict) Build(builder Builder) {
  15. if len(onConflict.Columns) > 0 {
  16. builder.WriteByte('(')
  17. for idx, column := range onConflict.Columns {
  18. if idx > 0 {
  19. builder.WriteByte(',')
  20. }
  21. builder.WriteQuoted(column)
  22. }
  23. builder.WriteString(`) `)
  24. }
  25. if len(onConflict.Where.Exprs) > 0 {
  26. builder.WriteString("WHERE ")
  27. onConflict.Where.Build(builder)
  28. builder.WriteByte(' ')
  29. }
  30. if onConflict.OnConstraint != "" {
  31. builder.WriteString("ON CONSTRAINT ")
  32. builder.WriteString(onConflict.OnConstraint)
  33. builder.WriteByte(' ')
  34. }
  35. if onConflict.DoNothing {
  36. builder.WriteString("DO NOTHING")
  37. } else {
  38. builder.WriteString("DO UPDATE SET ")
  39. onConflict.DoUpdates.Build(builder)
  40. }
  41. }
  42. // MergeClause merge onConflict clauses
  43. func (onConflict OnConflict) MergeClause(clause *Clause) {
  44. clause.Expression = onConflict
  45. }