github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/extend/orm/test/gorm_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "fmt" 6 "github.com/isyscore/isc-gobase/config" 7 orm2 "github.com/isyscore/isc-gobase/extend/orm" 8 "github.com/isyscore/isc-gobase/logger" 9 "testing" 10 "time" 11 ) 12 13 func TestGorm1(t *testing.T) { 14 config.LoadYamlFile("./application-test1.yaml") 15 //orm2.AddGormHook(&GobaseOrmHookDemo{}) 16 db, _ := orm2.NewGormDb() 17 18 // 删除表 19 db.Exec("drop table isc_demo.gobase_demo1") 20 21 //新增表 22 db.Exec("CREATE TABLE gobase_demo(\n" + 23 " `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',\n" + 24 " `name` char(20) NOT NULL COMMENT '名字',\n" + 25 " `age` INT NOT NULL COMMENT '年龄',\n" + 26 " `address` char(20) NOT NULL COMMENT '名字',\n" + 27 " \n" + 28 " `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" + 29 " `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" + 30 "\n" + 31 " PRIMARY KEY (`id`)\n" + 32 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='测试表'") 33 34 // 新增 35 db.Create(&GobaseDemo{Name: "zhou", Age: 18, Address: "杭州"}) 36 db.Create(&GobaseDemo{Name: "zhou", Age: 11, Address: "杭州2"}) 37 38 // 查询:一行 39 var demo GobaseDemo 40 db.First(&demo).Where("name=?", "zhou") 41 42 dd, _ := db.DB() 43 dd.Query("select * from gobase_demo") 44 45 // 查询:多行 46 fmt.Println(demo) 47 } 48 49 func TestGormOfLoggerChange(t *testing.T) { 50 config.LoadYamlFile("./application-test1.yaml") 51 //orm2.AddGormHook(&GobaseOrmHookDemo{}) 52 db, _ := orm2.NewGormDb() 53 54 logger.InitLog() 55 56 //// 删除库 57 //db.Exec("drop database isc_demo") 58 // 59 //// 创建库 60 //db.Exec("create database isc_demo") 61 62 //新增表 63 //db.Exec("CREATE TABLE isc_demo.gobase_demo(\n" + 64 // " `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',\n" + 65 // " `name` char(20) NOT NULL COMMENT '名字',\n" + 66 // " `age` INT NOT NULL COMMENT '年龄',\n" + 67 // " `address` char(20) NOT NULL COMMENT '名字',\n" + 68 // " \n" + 69 // " `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" + 70 // " `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" + 71 // "\n" + 72 // " PRIMARY KEY (`id`)\n" + 73 // ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='测试表'") 74 75 // 新增 76 db.Create(&GobaseDemo{Name: "zhou", Age: 18, Address: "杭州"}) 77 db.Create(&GobaseDemo{Name: "zhou", Age: 11, Address: "杭州2"}) 78 79 // 查询:一行 80 var demo GobaseDemo 81 for i := 0; i < 100; i++ { 82 db.First(&demo).Where("name=?", "zhou") 83 time.Sleep(time.Second) 84 if i == 2 { 85 config.SetValue("base.orm.show-sql", true) 86 } 87 88 if i == 4 { 89 config.SetValue("base.orm.show-sql", false) 90 } 91 } 92 93 // 查询:多行 94 fmt.Println(demo) 95 } 96 97 type GobaseDemo struct { 98 Id uint64 99 Name string 100 Age int 101 Address string 102 } 103 104 func (GobaseDemo) TableName() string { 105 return "gobase_demo" 106 } 107 108 type GobaseOrmHookDemo struct { 109 } 110 111 func (*GobaseOrmHookDemo) Before(ctx context.Context, driverName string, parameters map[string]any) (context.Context, error){ 112 fmt.Println("before") 113 fmt.Println(parameters) 114 return ctx, nil 115 } 116 117 func (*GobaseOrmHookDemo) After(ctx context.Context, driverName string, parameters map[string]any) (context.Context, error){ 118 fmt.Println("after") 119 fmt.Println(parameters) 120 return ctx, nil 121 } 122 123 func (*GobaseOrmHookDemo) Err(ctx context.Context, driverName string, err error, parameters map[string]any) error { 124 fmt.Println("err") 125 fmt.Println(err.Error()) 126 return nil 127 } 128 129