2023-10-28 09:45:30 +00:00
|
|
|
// Copyright 2023 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2023-10-30 05:40:19 +00:00
|
|
|
"xorm.io/builder"
|
2023-10-28 09:45:30 +00:00
|
|
|
"xorm.io/xorm/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Executor[T any] struct {
|
|
|
|
client xorm.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func New[T any](c xorm.Interface) *Executor[T] {
|
|
|
|
return &Executor[T]{
|
|
|
|
client: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 05:40:19 +00:00
|
|
|
func (q *Executor[T]) Where(cond builder.Cond) *Executor[T] {
|
|
|
|
q.client.Where(cond)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Executor[T]) Get(ctx context.Context) (*T, error) {
|
|
|
|
var result T
|
|
|
|
if has, err := q.client.Get(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Executor[T]) InsertOne(ctx context.Context, obj *T) error {
|
|
|
|
_, err := q.client.InsertOne(obj)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Executor[T]) Insert(ctx context.Context, results []T) (int64, error) {
|
|
|
|
return q.client.Insert(results)
|
|
|
|
}
|
|
|
|
|
2023-10-28 09:45:30 +00:00
|
|
|
func (q *Executor[T]) Exec(ctx context.Context) (sql.Result, error) {
|
|
|
|
return q.client.Exec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Executor[T]) All(ctx context.Context) ([]T, error) {
|
|
|
|
var result []T
|
|
|
|
return result, q.client.Find(&result)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filter interface{}
|
|
|
|
|
|
|
|
func (q *Executor[T]) Filter(ctx context.Context, filter ...Filter) ([]T, error) {
|
|
|
|
// implementation
|
|
|
|
return nil, nil
|
|
|
|
}
|