more refactor

This commit is contained in:
Lunny Xiao 2022-05-29 20:36:04 +08:00
parent 4e3ac37291
commit ee2b5ef320
2 changed files with 20 additions and 9 deletions

View File

@ -297,16 +297,19 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
} }
} }
var groupStr string if _, err := fmt.Fprintf(mssqlCondi, "(%s NOT IN (SELECT TOP %d %s%s%s%s",
if len(statement.GroupByStr) > 0 { column, statement.Start, column, fromStr, whereStr, orderByWriter.String()); err != nil {
groupStr = fmt.Sprintf(" GROUP BY %s", statement.GroupByStr)
}
if _, err := fmt.Fprintf(mssqlCondi, "(%s NOT IN (SELECT TOP %d %s%s%s%s%s))",
column, statement.Start, column, fromStr, whereStr, orderByWriter.String(), groupStr); err != nil {
return "", nil, err return "", nil, err
} }
mssqlCondi.Append(orderByWriter.Args()...) mssqlCondi.Append(orderByWriter.Args()...)
if err := statement.WriteGroupBy(mssqlCondi); err != nil {
return "", nil, err
}
if _, err := fmt.Fprint(mssqlCondi, "))"); err != nil {
return "", nil, err
}
} }
} }
@ -322,8 +325,8 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
buf.Append(mssqlCondi.Args()...) buf.Append(mssqlCondi.Args()...)
} }
if statement.GroupByStr != "" { if err := statement.WriteGroupBy(buf); err != nil {
fmt.Fprint(buf, " GROUP BY ", statement.GroupByStr) return "", nil, err
} }
if statement.HavingStr != "" { if statement.HavingStr != "" {
fmt.Fprint(buf, " ", statement.HavingStr) fmt.Fprint(buf, " ", statement.HavingStr)

View File

@ -598,6 +598,14 @@ func (statement *Statement) GroupBy(keys string) *Statement {
return statement return statement
} }
func (statement *Statement) WriteGroupBy(w builder.Writer) error {
if len(statement.GroupByStr) > 0 {
_, err := fmt.Fprintf(w, " GROUP BY %s", statement.GroupByStr)
return err
}
return nil
}
// Having generate "Having conditions" statement // Having generate "Having conditions" statement
func (statement *Statement) Having(conditions string) *Statement { func (statement *Statement) Having(conditions string) *Statement {
statement.HavingStr = fmt.Sprintf("HAVING %v", statement.ReplaceQuote(conditions)) statement.HavingStr = fmt.Sprintf("HAVING %v", statement.ReplaceQuote(conditions))