func Test014_TakeTableFields(t *testing.T) { var dbRequest = Default().SetPageSize(2) dbRequest.TableName = "sys_dept" dbRequest.FieldsName = "dept_id,dept_name" var result = dbRequest.GeneralTakeTable() golog.Info(result) }
INFO[2024-10-1013 10:48:23]C:/Users/leijmdas/go/pkg/mod/gitee.com/leijmdas/gobase/[email protected]/common/golog/go_logger.go:12 gitee.com/leijmdas/gobase/goconfig/common/golog.Info() {
"code": 200,
"msg": "成功",
"page_size": 2,
"current": 1,
"total": 10,
"data": {
"DeptId": 100,
"DeptName": "若依科技"
},
"data_agg": null,
"data_highlight": null,
"hosturl": ""
}
func (self *PagedbRequest) GeneralTakeTable() *page.PageResult { var metadataFactroy = service.NewMetadataFactroy() var metadata = metadataFactroy.FindMetadata(self.TableName) if !metadata.TableExist { return page.ResultFailedPageResultErr(errors.New("table not exist")) } var dbentity = self.CreateDbTableStru(metadata) return self.PageRequest.TakeTable(self.TableName, dbentity.Addr().Interface()) }
unc (self *PagedbRequest) CreateDbTableStru(meta *metadata.MetadataTable) reflect.Value { // logrus.Info("CreateDbTableStru meta=", meta.ToPrettyString()) var metaColService = service.NewMetadataFactroy() dbFields := []reflect.StructField{} for _, col := range meta.Columns { //logrus.Info("CreateDbTableStru col=", col.ToPrettyString()) var colType = metaColService.FindGoType(col.DataType) var ColTyp8 = reflect.TypeOf(int8(0)) var ct = func(colType string) *reflect.Type { var ColTyp = reflect.TypeOf(int8(0)) switch colType { case "float64": ColTyp = reflect.TypeOf(float64(0)) case "int64": ColTyp = reflect.TypeOf(int64(0)) case "int32": ColTyp = reflect.TypeOf(int64(0)) case "string": ColTyp = reflect.TypeOf("") case "bool": ColTyp = reflect.TypeOf(true) case "byte": ColTyp = reflect.TypeOf(byte(0)) case "[]byte": ColTyp = reflect.TypeOf([]byte{}) case "time.Time": ColTyp = reflect.TypeOf(time.Now()) } return &ColTyp }(colType) if ColTyp8 != *ct { var colField = reflect.StructField{ Name: stringutils.Case2Camel(col.ColumnName), Type: *ct, } if self.IfExistField(col.ColumnName) { //colField.Tag = reflect.StructTag(fmt.Sprintf("json:\"%s\"", col.ColumnName)) dbFields = append(dbFields, colField) } } } dbtableType := reflect.StructOf(dbFields) dbentity := reflect.New(dbtableType).Elem() return dbentity }
func (self *PageRequest) TakeTable(table string, model interface{}) *PageResult { ///fileName := table + "_pagerequest.json" self.InitPage() count, err := self.CountTable(table) if err != nil { golog.Error(err) return ResultFailedPageResultErr(err) } if count > 0 { err = self.FindTakeTable(table, model) if err != nil { logrus.Error(err) return NewPageResultError(err.Error()) } } var result = PageResultOf(self) result.Total = count result.Data = model return result }
func (self *PageRequest) FindTakeTable(table string, result interface{}) error { dbc := self.GetDB().Table(table) //.Select("id,name") dbc = self.BuildWhere(dbc) dbc = self.Order(dbc) dbc = self.SetLimit(dbc) dbc = dbc.Take(result) return dbc.Error }
func (self *PageRequest) CountTable(table string) (int, error) { dbc := self.GetDB().Table(table) dbc = self.BuildWhere(dbc).Offset(0).Limit(1) var count int if err := dbc.Count(&count).Error; err != nil { logrus.Error(err) return 0, err } logrus.Info("\ncount=", count) return count, nil }标签:err,StructField,self,reflect,var,dbc,TypeOf,go,gorm From: https://blog.csdn.net/leijmdas/article/details/142895172