add new package executors

This commit is contained in:
Lunny Xiao 2023-10-28 11:58:27 +08:00
parent 607f715634
commit ee0978a108
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 68 additions and 0 deletions

33
executors/querier.go Normal file
View File

@ -0,0 +1,33 @@
// 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 executors
import (
"context"
"xorm.io/xorm/v2"
)
type Querier[T any] struct {
client xorm.Interface
}
func NewQuerier[T any](c xorm.Interface) *Querier[T] {
return &Querier[T]{
client: c,
}
}
func (q *Querier[T]) All(ctx context.Context) ([]T, error) {
var result []T
return result, q.client.Find(&result)
}
type Filter interface{}
func (q *Querier[T]) Filter(ctx context.Context, filter ...Filter) ([]T, error) {
// implementation
return nil, nil
}

35
executors/querier_test.go Normal file
View File

@ -0,0 +1,35 @@
// 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 executors
import (
"context"
"testing"
"xorm.io/xorm/v2"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
func TestQuerier(t *testing.T) {
type User struct {
Id int64
Name string
}
engine, err := xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
assert.NoError(t, err)
assert.NoError(t, engine.Sync(new(User)))
// create querier
querier := NewQuerier[User](engine)
users, err := querier.All(context.Background())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(users), 0)
}