github.com/godaddy-x/freego@v1.0.156/mysql_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/godaddy-x/freego/ormx/sqlc" 6 "github.com/godaddy-x/freego/ormx/sqld" 7 "github.com/godaddy-x/freego/utils" 8 "testing" 9 ) 10 11 func init() { 12 initDriver() 13 } 14 15 func TestMysqlSave(t *testing.T) { 16 initMysqlDB() 17 db, err := sqld.NewMysql(sqld.Option{OpenTx: true, AutoID: true}) 18 if err != nil { 19 panic(err) 20 } 21 defer db.Close() 22 var vs []sqlc.Object 23 for i := 0; i < 1; i++ { 24 wallet := OwAuth{ 25 //Ctime: utils.UnixMilli(), 26 } 27 vs = append(vs, &wallet) 28 } 29 l := utils.UnixMilli() 30 if err := db.Save(vs...); err != nil { 31 fmt.Println(err) 32 } 33 fmt.Println("cost: ", utils.UnixMilli()-l) 34 } 35 36 func TestMysqlUpdate(t *testing.T) { 37 initMysqlDB() 38 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: true}) 39 if err != nil { 40 panic(err) 41 } 42 defer db.Close() 43 var vs []sqlc.Object 44 for i := 0; i < 1; i++ { 45 wallet := OwAuth{ 46 Id: 1649040212178763776, 47 //Secret: "1111122", 48 //Ctime: utils.UnixMilli(), 49 //Seed: "3321", 50 } 51 vs = append(vs, &wallet) 52 } 53 l := utils.UnixMilli() 54 if err := db.Update(vs...); err != nil { 55 fmt.Println(err) 56 } 57 fmt.Println("cost: ", utils.UnixMilli()-l) 58 } 59 60 func TestMysqlUpdateByCnd(t *testing.T) { 61 initMysqlDB() 62 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: true}) 63 if err != nil { 64 panic(err) 65 } 66 defer db.Close() 67 l := utils.UnixMilli() 68 if _, err := db.UpdateByCnd(sqlc.M(&OwWallet{}).Upset([]string{"appID", "utime"}, "123456789", utils.UnixMilli()).Eq("id", 1649040212178763776)); err != nil { 69 fmt.Println(err) 70 } 71 fmt.Println("cost: ", utils.UnixMilli()-l) 72 } 73 74 func TestMysqlDelete(t *testing.T) { 75 initMysqlDB() 76 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: true}) 77 if err != nil { 78 panic(err) 79 } 80 defer db.Close() 81 var vs []sqlc.Object 82 for i := 0; i < 2000; i++ { 83 wallet := OwWallet{ 84 Id: 1570732354786295848, 85 } 86 vs = append(vs, &wallet) 87 } 88 l := utils.UnixMilli() 89 if err := db.Delete(vs...); err != nil { 90 fmt.Println(err) 91 } 92 fmt.Println("cost: ", utils.UnixMilli()-l) 93 } 94 95 func TestMysqlFindById(t *testing.T) { 96 initMysqlDB() 97 db, err := new(sqld.MysqlManager).Get() 98 if err != nil { 99 panic(err) 100 } 101 defer db.Close() 102 l := utils.UnixMilli() 103 wallet := OwWallet{ 104 Id: 1570732354786295849, 105 } 106 if err := db.FindById(&wallet); err != nil { 107 fmt.Println(err) 108 } 109 fmt.Println("cost: ", utils.UnixMilli()-l) 110 } 111 112 func TestMysqlFindOne(t *testing.T) { 113 initMysqlDB() 114 db, err := sqld.NewMysql() 115 if err != nil { 116 panic(err) 117 } 118 defer db.Close() 119 wallet := OwWallet{} 120 if err := db.FindOne(sqlc.M().Eq("id", 1109996130134917121), &wallet); err != nil { 121 panic(err) 122 } 123 } 124 125 func TestMysqlDeleteById(t *testing.T) { 126 initMysqlDB() 127 db, err := sqld.NewMysql() 128 if err != nil { 129 panic(err) 130 } 131 defer db.Close() 132 ret, err := db.DeleteById(&OwWallet{}, 1136187262606770177, 1136187262606770178) 133 if err != nil { 134 panic(err) 135 } 136 fmt.Println(ret) 137 } 138 139 func TestMysqlFindList(t *testing.T) { 140 initMysqlDB() 141 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: false}) 142 if err != nil { 143 panic(err) 144 } 145 defer db.Close() 146 l := utils.UnixMilli() 147 var result []*OwWallet 148 if err := db.FindList(sqlc.M(&OwWallet{}).Eq("id", 1109996130134917121).Orderby("id", sqlc.DESC_).Limit(1, 5), &result); err != nil { 149 fmt.Println(err) 150 } 151 fmt.Println("cost: ", utils.UnixMilli()-l) 152 } 153 154 func TestMysqlFindListComplex(t *testing.T) { 155 initMysqlDB() 156 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: false}) 157 if err != nil { 158 panic(err) 159 } 160 defer db.Close() 161 l := utils.UnixMilli() 162 var result []*OwWallet 163 if err := db.FindListComplex(sqlc.M(&OwWallet{}).UnEscape().Fields("count(a.id) as `id`").From("ow_wallet a").Orderby("a.id", sqlc.DESC_).Limit(1, 5), &result); err != nil { 164 fmt.Println(err) 165 } 166 fmt.Println("cost: ", utils.UnixMilli()-l) 167 } 168 169 func TestMysqlFindOneComplex(t *testing.T) { 170 initMysqlDB() 171 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: false}) 172 if err != nil { 173 panic(err) 174 } 175 defer db.Close() 176 l := utils.UnixMilli() 177 result := OwWallet{} 178 if err := db.FindOneComplex(sqlc.M().UnEscape().Fields("count(a.id) as id").From("ow_wallet a").Orderby("a.id", sqlc.DESC_).Limit(1, 5), &result); err != nil { 179 fmt.Println(err) 180 } 181 fmt.Println("cost: ", utils.UnixMilli()-l) 182 } 183 184 func TestMysqlCount(t *testing.T) { 185 initMysqlDB() 186 db, err := new(sqld.MysqlManager).Get(sqld.Option{OpenTx: false}) 187 if err != nil { 188 panic(err) 189 } 190 defer db.Close() 191 l := utils.UnixMilli() 192 if c, err := db.Count(sqlc.M(&OwWallet{}).Orderby("id", sqlc.DESC_).Limit(1, 30)); err != nil { 193 fmt.Println(err) 194 } else { 195 fmt.Println(c) 196 } 197 fmt.Println("cost: ", utils.UnixMilli()-l) 198 }