customized.tpl 1.1 KB

12345678910111213141516171819202122232425
  1. func (m *default{{.upperStartCamelObject}}Model) findListByPrimaryKeys(ctx context.Context, {{.lowerStartCamelPrimaryKey}}s []interface{}) ([]*{{.upperStartCamelObject}}, error) {
  2. if len({{.lowerStartCamelPrimaryKey}}s) == 0 {
  3. return []*{{.upperStartCamelObject}}{}, nil
  4. }
  5. placeholders := make([]string, len({{.lowerStartCamelPrimaryKey}}s))
  6. args := make([]interface{}, len({{.lowerStartCamelPrimaryKey}}s))
  7. for i, {{.lowerStartCamelPrimaryKey}} := range {{.lowerStartCamelPrimaryKey}}s {
  8. {{if .postgreSql}}placeholders[i] = fmt.Sprintf("$%d", i+1){{else}}placeholders[i] = "?"{{end}}
  9. args[i] = {{.lowerStartCamelPrimaryKey}}
  10. }
  11. var resp []*{{.upperStartCamelObject}}
  12. query := fmt.Sprintf("SELECT %s FROM %s WHERE {{.originalPrimaryKey}} IN (%s)", {{.lowerStartCamelObject}}Rows, m.table, strings.Join(placeholders, ","))
  13. err := {{if .withCache}}m.QueryRowsNoCacheCtx{{else}}m.conn.QueryRowsCtx{{end}}(ctx, &resp, query, args...)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return resp, nil
  18. }
  19. func (m *default{{.upperStartCamelObject}}Model) getPrimaryKeyValue(data *{{.upperStartCamelObject}}) interface{} {
  20. return data.{{.upperStartCamelPrimaryKey}}
  21. }