{{- define "name" -}} Update{{ if not .Return }}NoReturn{{ end }}_{{ .Suffix }} {{- end -}} {{- define "signature" -}} {{- template "name" . }}({{ ctxparam .Args }}, update {{ .Struct.UpdateStructName }}) ( {{ if .Return }}{{ param .Return }}, {{ end }}err error) {{- end -}} {{- define "invoke" -}} {{- template "name" . }}({{ ctxarg .Args }}, update) {{- end -}} {{- define "body" -}} {{ embedplaceholders .Info }} {{ embedsql .Info "__embed_stmt" }} __sets_sql := __sqlbundle_Literals{Join: ", "} var __values []interface{} var __args []interface{} {{ range .Struct.UpdatableFields }} if update.{{ .Name }}._set { __values = append(__values, update.{{ .Name }}.value()) __sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("{{ .Column }} = ?")) } {{ end }} {{- if .NeedsNow }} __now := obj.db.Hooks.Now().UTC() {{ end -}} {{ range .AutoFields }} __values = append(__values, {{ .InitVal }}) __sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("{{ .Name }} = ?")) {{ end }} {{ if not .AutoFields }} if len(__sets_sql.SQLs) == 0 { {{- if .Return }} return nil, emptyUpdate() {{- else }} return emptyUpdate() {{- end }} } {{ end }} {{ embedvalues .Args "__args" }} __values = append(__values, __args...) __sets.SQL = __sets_sql var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) obj.logStmt(__stmt, __values...) {{- if not .Return }} _, err = obj.driver.ExecContext(ctx, __stmt, __values...) if err != nil { return obj.makeErr(err) } return nil {{- else }} {{ init .Return }} {{- if .SupportsReturning }} err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan({{ addrof (flatten .Return) }}) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, obj.makeErr(err) } {{- else }} _, err = obj.driver.ExecContext(ctx, __stmt, __values...) if err != nil { return nil, obj.makeErr(err) } {{ embedsql .InfoGet "__embed_stmt_get" }} var __stmt_get = __sqlbundle_Render(obj.dialect, __embed_stmt_get) obj.logStmt("(IMPLIED) " + __stmt_get, __args...) err = obj.driver.QueryRowContext(ctx, __stmt_get, __args...).Scan({{ addrof (flatten .Return) }}) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, obj.makeErr(err) } {{- end }} return {{ arg .Return }}, nil {{- end }} {{- end -}}