小优化代码
This commit is contained in:
parent
df11d6522d
commit
409ffd8081
|
@ -100,24 +100,19 @@ func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T,
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) {
|
func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (r T, err error) {
|
||||||
var r T
|
s, args, err := BuildQuerySql[T](&QueryCondition{
|
||||||
var w string
|
where: where,
|
||||||
var args []any
|
fields: fields,
|
||||||
var err error
|
order: order,
|
||||||
if where != nil {
|
in: in,
|
||||||
w, args, err = where.ParseWhere(&in)
|
limit: 1,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return
|
||||||
}
|
}
|
||||||
}
|
err = globalBb.Get(ctx, &r, s, args...)
|
||||||
tp := "select %s from %s %s %s"
|
return
|
||||||
sq := fmt.Sprintf(tp, fields, r.Table(), w, order.parseOrderBy())
|
|
||||||
err = globalBb.Get(ctx, &r, sq, args...)
|
|
||||||
if err != nil {
|
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error) {
|
func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error) {
|
||||||
|
@ -140,24 +135,16 @@ func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in .
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) ([]T, error) {
|
func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error) {
|
||||||
var r []T
|
s, args, err := BuildQuerySql[T](&QueryCondition{
|
||||||
var rr T
|
where: where,
|
||||||
var err error
|
fields: fields,
|
||||||
var args []any
|
in: in,
|
||||||
var w string
|
})
|
||||||
if where != nil {
|
|
||||||
w, args, err = where.ParseWhere(&in)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return
|
||||||
}
|
|
||||||
}
|
|
||||||
tp := "select %s from %s %s"
|
|
||||||
sq := fmt.Sprintf(tp, fields, rr.Table(), w)
|
|
||||||
err = globalBb.Select(ctx, &r, sq, args...)
|
|
||||||
if err != nil {
|
|
||||||
return r, err
|
|
||||||
}
|
}
|
||||||
|
err = globalBb.Select(ctx, &r, s, args...)
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,40 +160,21 @@ func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Find[T Model](ctx context.Context, where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) {
|
func Find[T Model](ctx context.Context, where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) {
|
||||||
var rr T
|
q := QueryCondition{
|
||||||
w := ""
|
where: where,
|
||||||
var args []any
|
fields: fields,
|
||||||
if where != nil {
|
group: group,
|
||||||
w, args, err = where.ParseWhere(&in)
|
order: order,
|
||||||
|
join: join,
|
||||||
|
having: having,
|
||||||
|
limit: limit,
|
||||||
|
in: in,
|
||||||
|
}
|
||||||
|
s, args, err := BuildQuerySql[T](&q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return
|
||||||
}
|
}
|
||||||
}
|
err = globalBb.Select(ctx, &r, s, args...)
|
||||||
h := ""
|
|
||||||
if having != nil {
|
|
||||||
hh, arg, err := having.ParseWhere(&in)
|
|
||||||
if err != nil {
|
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
args = append(args, arg...)
|
|
||||||
h = strings.Replace(hh, " where", " having", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
j := join.parseJoin()
|
|
||||||
groupBy := ""
|
|
||||||
if group != "" {
|
|
||||||
g := strings.Builder{}
|
|
||||||
g.WriteString(" group by ")
|
|
||||||
g.WriteString(group)
|
|
||||||
groupBy = g.String()
|
|
||||||
}
|
|
||||||
tp := "select %s from %s %s %s %s %s %s %s"
|
|
||||||
l := ""
|
|
||||||
if limit > 0 {
|
|
||||||
l = fmt.Sprintf(" limit %d", limit)
|
|
||||||
}
|
|
||||||
sq := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), l)
|
|
||||||
err = globalBb.Select(ctx, &r, sq, args...)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user