delete.tpl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
  2. {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
  3. if err!=nil{
  4. return err
  5. }
  6. {{end}} {{.keys}}
  7. _, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
  8. query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
  9. return conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}})
  10. }, {{.keyValues}}){{else}}query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
  11. _,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}}
  12. return err
  13. }
  14. func (m *default{{.upperStartCamelObject}}Model) DeleteWithTx(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
  15. {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
  16. if err!=nil{
  17. return err
  18. }
  19. {{end}} {{.keys}}
  20. _, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
  21. query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
  22. return session.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}})
  23. }, {{.keyValues}}){{else}}query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table)
  24. _,err:=session.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}}
  25. return err
  26. }
  27. func (m *default{{.upperStartCamelObject}}Model) BatchDelete(ctx context.Context, {{.lowerStartCamelPrimaryKey}}s []{{.dataType}}) error {
  28. if len({{.lowerStartCamelPrimaryKey}}s) == 0 {
  29. return nil
  30. }
  31. {{if .withCache}}{{if .containsIndexCache}}primaryKeys := make([]interface{}, 0, len({{.lowerStartCamelPrimaryKey}}s))
  32. for _, key := range {{.lowerStartCamelPrimaryKey}}s {
  33. primaryKeys = append(primaryKeys, key)
  34. }
  35. oldDataList, err := m.findListByPrimaryKeys(ctx, primaryKeys)
  36. if err != nil {
  37. return err
  38. }{{end}}
  39. keys := make([]string, 0)
  40. for _, {{if .containsIndexCache}}data{{else}}{{.lowerStartCamelPrimaryKey}}{{end}} := range {{if .containsIndexCache}}oldDataList{{else}}{{.lowerStartCamelPrimaryKey}}s{{end}} { {{if .containsIndexCache}}
  41. {{.lowerStartCamelPrimaryKey}} := m.getPrimaryKeyValue(data){{end}}
  42. {{.keys}}
  43. keys = append(keys, {{.keyValues}})
  44. }{{end}}
  45. placeholders := make([]string, 0, len({{.lowerStartCamelPrimaryKey}}s))
  46. args := make([]interface{}, 0, len({{.lowerStartCamelPrimaryKey}}s))
  47. for {{if .postgreSql}}i, {{else}}_, {{end}}{{.lowerStartCamelPrimaryKey}} := range {{.lowerStartCamelPrimaryKey}}s {
  48. {{if .postgreSql}}placeholders = append(placeholders, fmt.Sprintf("$%d", i+1)){{else}}placeholders = append(placeholders, "?"){{end}}
  49. args = append(args, {{.lowerStartCamelPrimaryKey}})
  50. }
  51. {{if .withCache}}_, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
  52. query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} IN (%s)", m.table, strings.Join(placeholders, ","))
  53. return conn.ExecCtx(ctx, query, args...)
  54. }, keys...){{else}}query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} IN (%s)", m.table, strings.Join(placeholders, ","))
  55. _, err := m.conn.ExecCtx(ctx, query, args...){{end}}
  56. return err
  57. }
  58. func (m *default{{.upperStartCamelObject}}Model) BatchDeleteWithTx(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}}s []{{.dataType}}) error {
  59. if len({{.lowerStartCamelPrimaryKey}}s) == 0 {
  60. return nil
  61. }
  62. {{if .withCache}}{{if .containsIndexCache}}primaryKeys := make([]interface{}, 0, len({{.lowerStartCamelPrimaryKey}}s))
  63. for _, key := range {{.lowerStartCamelPrimaryKey}}s {
  64. primaryKeys = append(primaryKeys, key)
  65. }
  66. oldDataList, err := m.findListByPrimaryKeys(ctx, primaryKeys)
  67. if err != nil {
  68. return err
  69. }{{end}}
  70. keys := make([]string, 0)
  71. for _, {{if .containsIndexCache}}data{{else}}{{.lowerStartCamelPrimaryKey}}{{end}} := range {{if .containsIndexCache}}oldDataList{{else}}{{.lowerStartCamelPrimaryKey}}s{{end}} { {{if .containsIndexCache}}
  72. {{.lowerStartCamelPrimaryKey}} := m.getPrimaryKeyValue(data){{end}}
  73. {{.keys}}
  74. keys = append(keys, {{.keyValues}})
  75. }{{end}}
  76. placeholders := make([]string, 0, len({{.lowerStartCamelPrimaryKey}}s))
  77. args := make([]interface{}, 0, len({{.lowerStartCamelPrimaryKey}}s))
  78. for {{if .postgreSql}}i, {{else}}_, {{end}}{{.lowerStartCamelPrimaryKey}} := range {{.lowerStartCamelPrimaryKey}}s {
  79. {{if .postgreSql}}placeholders = append(placeholders, fmt.Sprintf("$%d", i+1)){{else}}placeholders = append(placeholders, "?"){{end}}
  80. args = append(args, {{.lowerStartCamelPrimaryKey}})
  81. }
  82. query := fmt.Sprintf("DELETE FROM %s WHERE {{.originalPrimaryKey}} IN (%s)", m.table, strings.Join(placeholders, ","))
  83. {{if .withCache}}_, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
  84. return session.ExecCtx(ctx, query, args...)
  85. }, keys...){{else}}_, err := session.ExecCtx(ctx, query, args...){{end}}
  86. return err
  87. }