-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilderx_test.go
73 lines (58 loc) · 1.63 KB
/
builderx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
. "github.com/x-ream/sqlxb"
"testing"
)
var Db *sqlx.DB
func InitSqlxDB() *sqlx.DB {
var err interface{}
Db, err = sqlx.Connect("mysql",
"root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True")
if err != nil {
fmt.Printf("connect DB failed, err:%v\n", err)
if Db != nil {
Db.Close()
}
}
Db.SetMaxOpenConns(16)
Db.SetMaxIdleConns(8)
return Db
}
func TestBulderX(t *testing.T) {
pet := Pet{}
cat := Cat{}
dog := Dog{}
//"COUNT(DISTINCT d.id) AS `d.id_count`"
builder := NewBuilderX(&cat,"c")
builder.ResultKeys( "distinct c.color","COUNT(DISTINCT d.id) AS `d.id_count`")//"COUNT(DISTINCT d.id) AS `d.id_count`"
builder.Eq("p.id", 1)
subP := Sub()
subP.ResultKeys("id").Source(&pet)
builder.SourceBuilder().Sub(subP).Alia("p").JoinOn(LEFT_JOIN,ON("id","c","pet_id"))
arr := []interface{}{3000,4000,5000,6000}
sub := Sub()
sub.ResultKeys("pet_id").Source(&dog).Eq("age",2).In("weight",arr...)
builder.SourceBuilder().Sub(sub).Alia("d").JoinOn(LEFT_JOIN,ON("id","c","pet_id"))
builder.SourceBuilder().Source(&cat).Alia("cat").JoinOn(INNER_JOIN,ON("pet_id","p","id"))
builder.
GroupBy("c.color").
Having(Gt,"id",1000).
Sort("p.id",DESC).
Paged().Rows(10).Last(101)
vs, dataSql, countSql, kmp:= builder.WithoutOptimization().Build().Sql()
fmt.Println(dataSql)
fmt.Println(vs)
fmt.Println(kmp)
fmt.Println(countSql)
InitSqlxDB()
catList := []Cat{}
err := Db.Select(&catList, dataSql,vs...)
if err != nil {
fmt.Println(err)
}
s := fmt.Sprintf("price : %v", *(catList[0].Price))
fmt.Println(s)
}