2023-01-12 12:42:16 +00:00
|
|
|
package model
|
2022-11-05 14:40:02 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-07 08:08:15 +00:00
|
|
|
"context"
|
2023-02-22 08:53:53 +00:00
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-01-18 15:02:59 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2022-11-05 14:40:02 +00:00
|
|
|
"reflect"
|
2023-02-22 08:53:53 +00:00
|
|
|
"sync"
|
2022-11-05 14:40:02 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
var ctx = context.Background()
|
2023-01-18 15:02:59 +00:00
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
var glob = safety.NewMap[string, dbQuery[Model]]()
|
|
|
|
var dbMap = sync.Map{}
|
2023-01-18 15:02:59 +00:00
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
var sq *sqlx.DB
|
2023-01-18 15:02:59 +00:00
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func anyDb[T Model]() *SqlxQuery[T] {
|
|
|
|
var a T
|
|
|
|
db, ok := dbMap.Load(a.Table())
|
|
|
|
if ok {
|
|
|
|
return db.(*SqlxQuery[T])
|
|
|
|
}
|
|
|
|
dbb := NewSqlxQuery[T](sq, UniversalDb[T]{nil, nil})
|
|
|
|
dbMap.Store(a.Table(), dbb)
|
|
|
|
return dbb
|
2023-01-18 15:02:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
func init() {
|
2023-01-18 15:02:59 +00:00
|
|
|
db, err := sqlx.Open("mysql", "root:root@tcp(192.168.66.47:3306)/wordpress?charset=utf8mb4&parseTime=True&loc=Local")
|
2022-11-05 14:44:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
sq = db
|
|
|
|
//glob = NewSqlxQuery(db, NewUniversalDb(nil, nil))
|
|
|
|
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func Test_selects(t *testing.T) {
|
|
|
|
type args[T Model] struct {
|
|
|
|
db dbQuery[T]
|
|
|
|
ctx context.Context
|
|
|
|
q *QueryCondition
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
type testCase[T Model] struct {
|
2022-11-05 14:40:02 +00:00
|
|
|
name string
|
2023-02-22 08:53:53 +00:00
|
|
|
args args[T]
|
|
|
|
want []T
|
2022-11-05 14:40:02 +00:00
|
|
|
wantErr bool
|
2023-02-22 08:53:53 +00:00
|
|
|
}
|
|
|
|
tests := []testCase[options]{
|
2022-11-05 14:40:02 +00:00
|
|
|
{
|
|
|
|
name: "t1",
|
2023-02-22 08:53:53 +00:00
|
|
|
args: args[options]{
|
|
|
|
anyDb[options](),
|
|
|
|
ctx,
|
|
|
|
Conditions(Where(SqlBuilder{{"option_name", "blogname"}})),
|
2022-11-05 14:40:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-22 08:53:53 +00:00
|
|
|
got, err := finds[options](tt.args.db, tt.args.ctx, tt.args.q)
|
2022-11-05 14:40:02 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2023-02-22 08:53:53 +00:00
|
|
|
t.Errorf("finds() error = %v, wantErr %v", err, tt.wantErr)
|
2022-11-05 14:40:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2023-02-22 08:53:53 +00:00
|
|
|
t.Errorf("finds() got = %v, want %v", got, tt.want)
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
func BenchmarkSelectXX(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-02-22 12:07:57 +00:00
|
|
|
_, err := finds[options](anyDb[options](), ctx, Conditions(
|
|
|
|
Where(SqlBuilder{{"option_id", "<", "50", "int"}}),
|
|
|
|
//In(slice.ToAnySlice(number.Range[uint64](1, 50, 1))),
|
|
|
|
))
|
2023-02-22 08:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
func BenchmarkScannerXX(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-02-22 12:07:57 +00:00
|
|
|
_, err := scanners[options](anyDb[options](), ctx, Conditions(
|
|
|
|
Where(SqlBuilder{{"option_id", "<", "50", "int"}}),
|
|
|
|
//In(slice.ToAnySlice(number.Range[uint64](1, 50, 1))),
|
|
|
|
))
|
2023-02-22 08:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 11:12:26 +00:00
|
|
|
func BenchmarkSqlXQueryXX(b *testing.B) {
|
2023-02-22 12:07:57 +00:00
|
|
|
var r []options
|
|
|
|
/*var r []options
|
|
|
|
x := number.Range[uint64](1, 50, 1)
|
|
|
|
j := strings.TrimRight(strings.Repeat("?,", 50), ",")
|
|
|
|
s := str.NewBuilder()
|
|
|
|
s.Sprintf("select * from wp_options where option_id in (%s)", j)
|
|
|
|
ss := s.String()
|
|
|
|
a := slice.ToAnySlice(x)*/
|
|
|
|
ss := "select * from wp_options where option_id < ?"
|
2023-02-22 11:12:26 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2023-02-22 12:07:57 +00:00
|
|
|
err := sq.Select(&r, ss, 50)
|
2023-02-22 11:12:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 08:53:53 +00:00
|
|
|
type options struct {
|
|
|
|
OptionId uint64 `gorm:"column:option_id" db:"option_id" json:"option_id" form:"option_id"`
|
|
|
|
OptionName string `gorm:"column:option_name" db:"option_name" json:"option_name" form:"option_name"`
|
|
|
|
OptionValue string `gorm:"column:option_value" db:"option_value" json:"option_value" form:"option_value"`
|
|
|
|
Autoload string `gorm:"column:autoload" db:"autoload" json:"autoload" form:"autoload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w options) PrimaryKey() string {
|
|
|
|
return "option_id"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w options) Table() string {
|
|
|
|
return "wp_options"
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_scanners(t *testing.T) {
|
|
|
|
type args[T Model] struct {
|
|
|
|
db dbQuery[T]
|
|
|
|
ctx context.Context
|
|
|
|
q *QueryCondition
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
type testCase[T Model] struct {
|
2022-11-05 14:40:02 +00:00
|
|
|
name string
|
2023-02-22 08:53:53 +00:00
|
|
|
args args[T]
|
2022-11-05 14:40:02 +00:00
|
|
|
wantErr bool
|
|
|
|
}
|
2023-02-22 08:53:53 +00:00
|
|
|
tests := []testCase[options]{
|
2023-01-13 04:31:35 +00:00
|
|
|
{
|
|
|
|
name: "t1",
|
2023-02-22 08:53:53 +00:00
|
|
|
args: args[options]{
|
|
|
|
anyDb[options](),
|
|
|
|
ctx,
|
|
|
|
Conditions(Where(SqlBuilder{{"option_name", "blogname"}})),
|
2023-01-13 04:31:35 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-22 08:53:53 +00:00
|
|
|
if _, err := scanners[options](tt.args.db, tt.args.ctx, tt.args.q); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("scanners() error = %v, wantErr %v", err, tt.wantErr)
|
2022-11-05 14:40:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|