You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
4 years ago
|
package sqlmodel
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"text/template"
|
||
|
|
||
|
"zero/core/stringx"
|
||
|
)
|
||
|
|
||
|
var updateTemplate = `
|
||
|
func ({{.pointer}} *{{.model}}Model) Update(data {{.model}}{{.conditions}}) error {
|
||
|
sql := ` + "`" + `update ` + "` + " + `{{.pointer}}.table` + " + `" + ` set ` + "` +" + ` {{.modelWithLowerStart}}RowsWithPlaceHolder + ` + "` where" + ` {{.valueConditions}}` + "`\n" +
|
||
|
` _, err := {{.pointer}}.conn.Exec(sql, {{.values}})
|
||
|
return err
|
||
|
}
|
||
|
`
|
||
|
|
||
|
func (s *structExp) genUpdate() (string, error) {
|
||
|
var updateValues []string
|
||
|
var conditionsValues []string
|
||
|
for _, field := range s.Fields {
|
||
|
key := fmt.Sprintf("data.%s", field.name)
|
||
|
if stringx.Contains(s.conditions, field.tag) ||
|
||
|
stringx.Contains(s.conditions, field.name) {
|
||
|
conditionsValues = append(conditionsValues, key)
|
||
|
} else if !stringx.Contains(s.ignoreFields, field.name) && !stringx.Contains(s.ignoreFields, field.tag) {
|
||
|
updateValues = append(updateValues, key)
|
||
|
}
|
||
|
}
|
||
|
conditionExp, valueConditions := s.buildCondition()
|
||
|
if len(s.conditions) == 1 && s.conditions[0] == s.primaryKey {
|
||
|
conditionExp = ""
|
||
|
} else {
|
||
|
conditionExp = ", " + conditionExp
|
||
|
}
|
||
|
t := template.Must(template.New("updateTemplate").Parse(updateTemplate))
|
||
|
var tmplBytes bytes.Buffer
|
||
|
err := t.Execute(&tmplBytes, map[string]string{
|
||
|
"pointer": "m",
|
||
|
"model": s.name,
|
||
|
"values": strings.Join(append(updateValues, conditionsValues...), ", "),
|
||
|
"primaryKey": s.primaryKey,
|
||
|
"valueConditions": valueConditions,
|
||
|
"conditions": conditionExp,
|
||
|
"modelWithLowerStart": fmtUnderLine2Camel(s.name, false),
|
||
|
})
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return tmplBytes.String(), nil
|
||
|
}
|