ReplaceSetClause returns a Rule that completely replaces all SET assignments with the ones provided in the map. Keys are column names, values are SQL expression strings. This is useful for wholesale rewrites of the SET clause. Returns ErrUnsupportedStatement for non-UPDATE statements. Example: t
(assignments map[string]string)
| 136 | // "updated_at": "NOW()", |
| 137 | // })) |
| 138 | func ReplaceSetClause(assignments map[string]string) Rule { |
| 139 | return RuleFunc(func(stmt ast.Statement) error { |
| 140 | upd, ok := stmt.(*ast.UpdateStatement) |
| 141 | if !ok { |
| 142 | return &ErrUnsupportedStatement{Transform: "ReplaceSetClause", Got: stmtTypeName(stmt)} |
| 143 | } |
| 144 | |
| 145 | newAssignments := make([]ast.UpdateExpression, 0, len(assignments)) |
| 146 | for col, valueSQL := range assignments { |
| 147 | valueExpr, err := parseValueExpr(valueSQL) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("ReplaceSetClause: column %q: %w", col, err) |
| 150 | } |
| 151 | newAssignments = append(newAssignments, ast.UpdateExpression{ |
| 152 | Column: &ast.Identifier{Name: col}, |
| 153 | Value: valueExpr, |
| 154 | }) |
| 155 | } |
| 156 | upd.Assignments = newAssignments |
| 157 | return nil |
| 158 | }) |
| 159 | } |