2020-08-19 10:41:19 +08:00
package template
2020-07-29 17:11:41 +08:00
2022-04-01 22:19:52 +08:00
const (
// Update defines a template for generating update codes
Update = `
2022-05-23 09:12:06 +08:00
func ( m * default { { . upperStartCamelObject } } Model ) Update ( ctx context . Context , { { if . containsIndexCache } } newData { { else } } data { { end } } * { { . upperStartCamelObject } } ) error {
{ { if . withCache } } { { if . containsIndexCache } } data , err := m . FindOne ( ctx , newData . { { . upperStartCamelPrimaryKey } } )
if err != nil {
return err
}
{ { end } } { { . keys } }
_ , { { if . containsIndexCache } } err { { else } } err : { { end } } = m . ExecCtx ( ctx , func ( ctx context . Context , conn sqlx . SqlConn ) ( result sql . Result , err error ) {
2021-07-23 11:45:15 +08:00
query := fmt . Sprintf ( "update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}" , m . table , { { . lowerStartCamelObject } } RowsWithPlaceHolder )
2022-02-17 10:28:55 +08:00
return conn . ExecCtx ( ctx , query , { { . expressionValues } } )
2021-07-23 11:45:15 +08:00
} , { { . keyValues } } ) { { else } } query := fmt . Sprintf ( "update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}" , m . table , { { . lowerStartCamelObject } } RowsWithPlaceHolder )
2022-02-17 10:28:55 +08:00
_ , err := m . conn . ExecCtx ( ctx , query , { { . expressionValues } } ) { { end } }
2020-10-27 22:42:53 +08:00
return err
2020-07-29 17:11:41 +08:00
}
`
2020-11-24 22:36:23 +08:00
2022-04-01 22:19:52 +08:00
// UpdateMethod defines an interface method template for generating update codes
2022-06-30 23:25:54 +08:00
UpdateMethod = ` Update(ctx context.Context, {{ if .containsIndexCache }} newData {{ else }} data {{ end }} * {{ .upperStartCamelObject }} ) error `
2022-04-01 22:19:52 +08:00
)