MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / ReplaceSetClause

Function ReplaceSetClause

pkg/transform/set.go:138–159  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

136// "updated_at": "NOW()",
137// }))
138func 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}

Calls 4

RuleFuncFuncType · 0.85
parseValueExprFunction · 0.85
stmtTypeNameFunction · 0.70
ErrorfMethod · 0.65