github.com/godaddy-x/freego@v1.0.156/ormx/sqld/model_manager.go (about) 1 package sqld 2 3 import ( 4 "github.com/godaddy-x/freego/ormx/sqlc" 5 "github.com/godaddy-x/freego/utils" 6 "github.com/godaddy-x/freego/utils/decimal" 7 "reflect" 8 ) 9 10 var ( 11 modelDrivers = make(map[string]*MdlDriver, 0) 12 ) 13 14 type FieldElem struct { 15 AutoId bool 16 Primary bool 17 Ignore bool 18 IsDate bool 19 IsBlob bool 20 FieldName string 21 FieldJsonName string 22 FieldBsonName string 23 FieldKind reflect.Kind 24 FieldType string 25 ValueKind interface{} 26 FieldDBType string 27 FieldComment string 28 FieldOffset uintptr 29 } 30 31 type MdlDriver struct { 32 TableName string 33 ToMongo bool 34 PkOffset uintptr 35 PkKind reflect.Kind 36 PkName string 37 PkBsonName string 38 AutoId bool 39 PkType string 40 Charset string 41 Collate string 42 FieldElem []*FieldElem 43 Object sqlc.Object 44 } 45 46 func isPk(key string) bool { 47 if len(key) > 0 && key == sqlc.True { 48 return true 49 } 50 return false 51 } 52 53 func ModelDriver(objects ...sqlc.Object) error { 54 if objects == nil || len(objects) == 0 { 55 panic("objects is nil") 56 } 57 for _, v := range objects { 58 if v == nil { 59 panic("object is nil") 60 } 61 if len(v.GetTable()) == 0 { 62 panic("object table name is nil") 63 } 64 model := v.NewObject() 65 if model == nil { 66 panic("NewObject value is nil") 67 } 68 if reflect.ValueOf(model).Kind() != reflect.Ptr { 69 panic("NewObject value must be pointer") 70 } 71 md := &MdlDriver{ 72 Object: v, 73 TableName: v.GetTable(), 74 FieldElem: []*FieldElem{}, 75 } 76 tof := reflect.TypeOf(model).Elem() 77 vof := reflect.ValueOf(model).Elem() 78 for i := 0; i < tof.NumField(); i++ { 79 f := &FieldElem{} 80 field := tof.Field(i) 81 value := vof.Field(i) 82 f.FieldName = field.Name 83 f.FieldKind = value.Kind() 84 f.FieldDBType = field.Tag.Get(sqlc.DB) 85 f.FieldComment = field.Tag.Get(sqlc.Comment) 86 f.FieldJsonName = field.Tag.Get(sqlc.Json) 87 f.FieldBsonName = field.Tag.Get(sqlc.Bson) 88 f.FieldOffset = field.Offset 89 f.FieldType = field.Type.String() 90 if field.Name == sqlc.Id || isPk(field.Tag.Get(sqlc.Key)) { 91 f.Primary = true 92 md.PkOffset = field.Offset 93 md.PkKind = value.Kind() 94 md.PkType = field.Type.String() 95 md.Charset = field.Tag.Get(sqlc.Charset) 96 if len(md.Charset) == 0 { 97 md.Charset = "utf8mb4" 98 } 99 md.Collate = field.Tag.Get(sqlc.Collate) 100 if len(md.Collate) == 0 { 101 md.Collate = "utf8mb4_general_ci" 102 } 103 md.PkName = field.Tag.Get(sqlc.Json) 104 md.PkBsonName = field.Tag.Get(sqlc.Bson) 105 mg := field.Tag.Get(sqlc.Mg) 106 if len(mg) > 0 && mg == sqlc.True { 107 md.ToMongo = true 108 } 109 auto := field.Tag.Get(sqlc.Auto) 110 if len(auto) > 0 && auto == sqlc.True { 111 md.AutoId = true 112 } 113 } 114 ignore := field.Tag.Get(sqlc.Ignore) 115 if len(ignore) > 0 && ignore == sqlc.True { 116 f.Ignore = true 117 } 118 isDate := field.Tag.Get(sqlc.Date) 119 if len(isDate) > 0 && isDate == sqlc.True { 120 f.IsDate = true 121 } 122 isBlob := field.Tag.Get(sqlc.Blob) 123 if len(isBlob) > 0 && isBlob == sqlc.True { 124 f.IsBlob = true 125 } 126 md.FieldElem = append(md.FieldElem, f) 127 } 128 if _, b := modelDrivers[md.TableName]; b { 129 panic("table name: " + md.TableName + " exist") 130 } 131 modelDrivers[md.TableName] = md 132 } 133 return nil 134 } 135 136 func GetValue(obj interface{}, elem *FieldElem) (interface{}, error) { 137 ptr := utils.GetPtr(obj, elem.FieldOffset) 138 switch elem.FieldKind { 139 case reflect.String: 140 return utils.GetString(ptr), nil 141 case reflect.Int: 142 ret := utils.GetInt(ptr) 143 if elem.IsDate { 144 if ret < 0 { 145 ret = 0 146 } 147 return utils.Time2Str(int64(ret)), nil 148 } 149 return ret, nil 150 case reflect.Int8: 151 return utils.GetInt8(ptr), nil 152 case reflect.Int16: 153 return utils.GetInt16(ptr), nil 154 case reflect.Int32: 155 ret := utils.GetInt32(ptr) 156 if elem.IsDate { 157 if ret < 0 { 158 ret = 0 159 } 160 return utils.Time2Str(int64(ret)), nil 161 } 162 return ret, nil 163 case reflect.Int64: 164 ret := utils.GetInt64(ptr) 165 if elem.IsDate { 166 if ret <= 0 { 167 return "", nil 168 } 169 return utils.Time2Str(ret), nil 170 } 171 return ret, nil 172 case reflect.Float32: 173 return utils.GetFloat32(ptr), nil 174 case reflect.Float64: 175 return utils.GetFloat64(ptr), nil 176 case reflect.Bool: 177 return utils.GetBool(ptr), nil 178 case reflect.Uint: 179 return utils.GetUint(ptr), nil 180 case reflect.Uint8: 181 return utils.GetUint8(ptr), nil 182 case reflect.Uint16: 183 return utils.GetUint16(ptr), nil 184 case reflect.Uint32: 185 return utils.GetUint32(ptr), nil 186 case reflect.Uint64: 187 return utils.GetUint64(ptr), nil 188 case reflect.Slice: 189 switch elem.FieldType { 190 case "[]string": 191 return getValueJsonStr(utils.GetStringArr(ptr)) 192 case "[]int": 193 return getValueJsonStr(utils.GetIntArr(ptr)) 194 case "[]int8": 195 return getValueJsonStr(utils.GetInt8Arr(ptr)) 196 case "[]int16": 197 return getValueJsonStr(utils.GetInt16Arr(ptr)) 198 case "[]int32": 199 return getValueJsonStr(utils.GetInt32Arr(ptr)) 200 case "[]int64": 201 return getValueJsonStr(utils.GetInt64Arr(ptr)) 202 case "[]float32": 203 return getValueJsonStr(utils.GetFloat32Arr(ptr)) 204 case "[]float64": 205 return getValueJsonStr(utils.GetFloat64Arr(ptr)) 206 case "[]bool": 207 return getValueJsonStr(utils.GetBoolArr(ptr)) 208 case "[]uint": 209 return getValueJsonStr(utils.GetUintArr(ptr)) 210 case "[]uint8": 211 if elem.IsBlob { 212 return utils.GetUint8Arr(ptr), nil 213 } 214 return getValueJsonStr(utils.GetUint8Arr(ptr)) 215 case "[]uint16": 216 return getValueJsonStr(utils.GetUint16Arr(ptr)) 217 case "[]uint32": 218 return getValueJsonStr(utils.GetUint32Arr(ptr)) 219 case "[]uint64": 220 return getValueJsonStr(utils.GetUint64Arr(ptr)) 221 } 222 case reflect.Map: 223 if v, err := getValueOfMapStr(obj, elem); err != nil { 224 return nil, err 225 } else if len(v) > 0 { 226 return v, nil 227 } else { 228 return nil, nil 229 } 230 case reflect.Ptr: 231 if v, err := getValueOfMapStr(obj, elem); err != nil { 232 return nil, err 233 } else if len(v) > 0 { 234 return v, nil 235 } else { 236 return nil, nil 237 } 238 case reflect.Struct: 239 return nil, utils.Error("please use pointer type: ", elem.FieldName) 240 } 241 return nil, nil 242 } 243 244 func SetValue(obj interface{}, elem *FieldElem, b []byte) error { 245 ptr := utils.GetPtr(obj, elem.FieldOffset) 246 switch elem.FieldKind { 247 case reflect.String: 248 if ret, err := utils.NewString(b); err != nil { 249 return err 250 } else { 251 utils.SetString(ptr, ret) 252 } 253 return nil 254 case reflect.Int: 255 if elem.IsDate { 256 if ret, err := utils.NewString(b); err != nil { 257 return err 258 } else if len(ret) > 0 { 259 if rdate, err := utils.Str2Time(ret); err != nil { 260 return err 261 } else { 262 utils.SetInt(ptr, int(rdate)) 263 } 264 } 265 return nil 266 } 267 if ret, err := utils.NewInt(b); err != nil { 268 return err 269 } else { 270 utils.SetInt(ptr, ret) 271 } 272 return nil 273 case reflect.Int8: 274 if ret, err := utils.NewInt8(b); err != nil { 275 return err 276 } else { 277 utils.SetInt8(ptr, ret) 278 } 279 return nil 280 case reflect.Int16: 281 if ret, err := utils.NewInt16(b); err != nil { 282 return err 283 } else { 284 utils.SetInt16(ptr, ret) 285 } 286 return nil 287 case reflect.Int32: 288 if elem.IsDate { 289 if ret, err := utils.NewString(b); err != nil { 290 return err 291 } else if len(ret) > 0 { 292 if rdate, err := utils.Str2Time(ret); err != nil { 293 return err 294 } else { 295 utils.SetInt32(ptr, int32(rdate)) 296 } 297 } 298 return nil 299 } 300 if ret, err := utils.NewInt32(b); err != nil { 301 return err 302 } else { 303 utils.SetInt32(ptr, ret) 304 } 305 return nil 306 case reflect.Int64: 307 if elem.IsDate { 308 if ret, err := utils.NewString(b); err != nil { 309 return err 310 } else if len(ret) > 0 { 311 if rdate, err := utils.Str2Time(ret); err != nil { 312 return err 313 } else { 314 utils.SetInt64(ptr, rdate) 315 } 316 } 317 return nil 318 } 319 if ret, err := utils.NewInt64(b); err != nil { 320 return err 321 } else { 322 utils.SetInt64(ptr, ret) 323 } 324 return nil 325 case reflect.Float32: 326 if ret, err := utils.NewFloat32(b); err != nil { 327 return err 328 } else { 329 utils.SetFloat32(ptr, ret) 330 } 331 return nil 332 case reflect.Float64: 333 if ret, err := utils.NewFloat64(b); err != nil { 334 return err 335 } else { 336 utils.SetFloat64(ptr, ret) 337 } 338 return nil 339 case reflect.Bool: 340 str, _ := utils.NewString(b) 341 if str == "true" { 342 utils.SetBool(ptr, true) 343 } else { 344 utils.SetBool(ptr, false) 345 } 346 return nil 347 case reflect.Uint: 348 if ret, err := utils.NewUint64(b); err != nil { 349 return err 350 } else { 351 utils.SetUint64(ptr, ret) 352 } 353 return nil 354 case reflect.Uint8: 355 if ret, err := utils.NewUint16(b); err != nil { 356 return err 357 } else { 358 utils.SetUint16(ptr, ret) 359 } 360 return nil 361 case reflect.Uint16: 362 if ret, err := utils.NewUint16(b); err != nil { 363 return err 364 } else { 365 utils.SetUint16(ptr, ret) 366 } 367 return nil 368 case reflect.Uint32: 369 if ret, err := utils.NewUint32(b); err != nil { 370 return err 371 } else { 372 utils.SetUint32(ptr, ret) 373 } 374 return nil 375 case reflect.Uint64: 376 if ret, err := utils.NewUint64(b); err != nil { 377 return err 378 } else { 379 utils.SetUint64(ptr, ret) 380 } 381 return nil 382 case reflect.Struct: 383 if elem.FieldType == "decimal.Decimal" { 384 if len(b) == 0 { 385 b = utils.Str2Bytes("0") 386 } 387 v, err := decimal.NewFromString(utils.Bytes2Str(b)) 388 if err != nil { 389 return err 390 } 391 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 392 } 393 case reflect.Slice: 394 switch elem.FieldType { 395 case "[]string": 396 if b == nil || len(b) == 0 { 397 return nil 398 } 399 v := make([]string, 0) 400 if err := getValueJsonObj(b, &v); err != nil { 401 return err 402 } 403 utils.SetStringArr(ptr, v) 404 return nil 405 case "[]int": 406 if b == nil || len(b) == 0 { 407 return nil 408 } 409 v := make([]int, 0) 410 if err := getValueJsonObj(b, &v); err != nil { 411 return err 412 } 413 utils.SetIntArr(ptr, v) 414 return nil 415 case "[]int8": 416 if b == nil || len(b) == 0 { 417 return nil 418 } 419 v := make([]int8, 0) 420 if err := getValueJsonObj(b, &v); err != nil { 421 return err 422 } 423 utils.SetInt8Arr(ptr, v) 424 return nil 425 case "[]int16": 426 if b == nil || len(b) == 0 { 427 return nil 428 } 429 v := make([]int16, 0) 430 if err := getValueJsonObj(b, &v); err != nil { 431 return err 432 } 433 utils.SetInt16Arr(ptr, v) 434 return nil 435 case "[]int32": 436 if b == nil || len(b) == 0 { 437 return nil 438 } 439 v := make([]int32, 0) 440 if err := getValueJsonObj(b, &v); err != nil { 441 return err 442 } 443 utils.SetInt32Arr(ptr, v) 444 return nil 445 case "[]int64": 446 if b == nil || len(b) == 0 { 447 return nil 448 } 449 v := make([]int64, 0) 450 if err := getValueJsonObj(b, &v); err != nil { 451 return err 452 } 453 utils.SetInt64Arr(ptr, v) 454 return nil 455 case "[]float32": 456 if b == nil || len(b) == 0 { 457 return nil 458 } 459 v := make([]float32, 0) 460 if err := getValueJsonObj(b, &v); err != nil { 461 return err 462 } 463 utils.SetFloat32Arr(ptr, v) 464 return nil 465 case "[]float64": 466 if b == nil || len(b) == 0 { 467 return nil 468 } 469 v := make([]float64, 0) 470 if err := getValueJsonObj(b, &v); err != nil { 471 return err 472 } 473 utils.SetFloat64Arr(ptr, v) 474 return nil 475 case "[]bool": 476 if b == nil || len(b) == 0 { 477 return nil 478 } 479 v := make([]bool, 0) 480 if err := getValueJsonObj(b, &v); err != nil { 481 return err 482 } 483 utils.SetBoolArr(ptr, v) 484 return nil 485 case "[]uint": 486 if b == nil || len(b) == 0 { 487 return nil 488 } 489 v := make([]uint, 0) 490 if err := getValueJsonObj(b, &v); err != nil { 491 return err 492 } 493 utils.SetUintArr(ptr, v) 494 return nil 495 case "[]uint8": 496 if b == nil || len(b) == 0 { 497 return nil 498 } 499 if elem.IsBlob { 500 utils.SetUint8Arr(ptr, b) 501 return nil 502 } 503 v := make([]uint8, 0) 504 if err := getValueJsonObj(b, &v); err != nil { 505 return err 506 } 507 utils.SetUint8Arr(ptr, v) 508 return nil 509 case "[]uint16": 510 if b == nil || len(b) == 0 { 511 return nil 512 } 513 v := make([]uint16, 0) 514 if err := getValueJsonObj(b, &v); err != nil { 515 return err 516 } 517 utils.SetUint16Arr(ptr, v) 518 return nil 519 case "[]uint32": 520 if b == nil || len(b) == 0 { 521 return nil 522 } 523 v := make([]uint32, 0) 524 if err := getValueJsonObj(b, &v); err != nil { 525 return err 526 } 527 utils.SetUint32Arr(ptr, v) 528 return nil 529 case "[]uint64": 530 if b == nil || len(b) == 0 { 531 return nil 532 } 533 v := make([]uint64, 0) 534 if err := getValueJsonObj(b, &v); err != nil { 535 return err 536 } 537 utils.SetUint64Arr(ptr, v) 538 return nil 539 } 540 case reflect.Map: 541 switch elem.FieldType { 542 case "map[string]string": 543 if b == nil || len(b) == 0 { 544 return nil 545 } 546 v := make(map[string]string, 0) 547 if err := getValueJsonObj(b, &v); err != nil { 548 return err 549 } 550 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 551 return nil 552 case "map[string]int": 553 if b == nil || len(b) == 0 { 554 return nil 555 } 556 v := make(map[string]int, 0) 557 if err := getValueJsonObj(b, &v); err != nil { 558 return err 559 } 560 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 561 return nil 562 case "map[string]int8": 563 if b == nil || len(b) == 0 { 564 return nil 565 } 566 v := make(map[string]int8, 0) 567 if err := getValueJsonObj(b, &v); err != nil { 568 return err 569 } 570 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 571 return nil 572 case "map[string]int16": 573 if b == nil || len(b) == 0 { 574 return nil 575 } 576 v := make(map[string]int16, 0) 577 if err := getValueJsonObj(b, &v); err != nil { 578 return err 579 } 580 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 581 return nil 582 case "map[string]int32": 583 if b == nil || len(b) == 0 { 584 return nil 585 } 586 v := make(map[string]int32, 0) 587 if err := getValueJsonObj(b, &v); err != nil { 588 return err 589 } 590 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 591 return nil 592 case "map[string]int64": 593 if b == nil || len(b) == 0 { 594 return nil 595 } 596 v := make(map[string]int64, 0) 597 if err := getValueJsonObj(b, &v); err != nil { 598 return err 599 } 600 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 601 return nil 602 case "map[string]float32": 603 if b == nil || len(b) == 0 { 604 return nil 605 } 606 v := make(map[string]float32, 0) 607 if err := getValueJsonObj(b, &v); err != nil { 608 return err 609 } 610 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 611 return nil 612 case "map[string]float64": 613 if b == nil || len(b) == 0 { 614 return nil 615 } 616 v := make(map[string]float64, 0) 617 if err := getValueJsonObj(b, &v); err != nil { 618 return err 619 } 620 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 621 return nil 622 case "map[string]bool": 623 if b == nil || len(b) == 0 { 624 return nil 625 } 626 v := make(map[string]bool, 0) 627 if err := getValueJsonObj(b, &v); err != nil { 628 return err 629 } 630 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 631 return nil 632 case "map[string]uint": 633 if b == nil || len(b) == 0 { 634 return nil 635 } 636 v := make(map[string]uint, 0) 637 if err := getValueJsonObj(b, &v); err != nil { 638 return err 639 } 640 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 641 return nil 642 case "map[string]uint8": 643 if b == nil || len(b) == 0 { 644 return nil 645 } 646 v := make(map[string]uint8, 0) 647 if err := getValueJsonObj(b, &v); err != nil { 648 return err 649 } 650 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 651 return nil 652 case "map[string]uint16": 653 if b == nil || len(b) == 0 { 654 return nil 655 } 656 v := make(map[string]uint16, 0) 657 if err := getValueJsonObj(b, &v); err != nil { 658 return err 659 } 660 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 661 return nil 662 case "map[string]uint32": 663 if b == nil || len(b) == 0 { 664 return nil 665 } 666 v := make(map[string]uint32, 0) 667 if err := getValueJsonObj(b, &v); err != nil { 668 return err 669 } 670 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 671 return nil 672 case "map[string]uint64": 673 if b == nil || len(b) == 0 { 674 return nil 675 } 676 v := make(map[string]uint64, 0) 677 if err := getValueJsonObj(b, &v); err != nil { 678 return err 679 } 680 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 681 return nil 682 case "map[string]interface {}": 683 if b == nil || len(b) == 0 { 684 return nil 685 } 686 v := make(map[string]interface{}, 0) 687 if err := getValueJsonObj(b, &v); err != nil { 688 return err 689 } 690 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 691 return nil 692 case "map[int]string": 693 if b == nil || len(b) == 0 { 694 return nil 695 } 696 v := make(map[int]string, 0) 697 if err := getValueJsonObj(b, &v); err != nil { 698 return err 699 } 700 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 701 return nil 702 case "map[int]int": 703 if b == nil || len(b) == 0 { 704 return nil 705 } 706 v := make(map[int]int, 0) 707 if err := getValueJsonObj(b, &v); err != nil { 708 return err 709 } 710 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 711 return nil 712 case "map[int]interface {}": 713 if b == nil || len(b) == 0 { 714 return nil 715 } 716 v := make(map[int]interface{}, 0) 717 if err := getValueJsonObj(b, &v); err != nil { 718 return err 719 } 720 reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName).Set(reflect.ValueOf(v)) 721 return nil 722 } 723 case reflect.Ptr: 724 if b == nil || len(b) == 0 { 725 return nil 726 } 727 structValue := reflect.ValueOf(obj).Elem() 728 pointerObjValue := structValue.FieldByName(elem.FieldName) 729 objType := pointerObjValue.Type().Elem() 730 newObj := reflect.New(objType).Elem() 731 if err := utils.JsonUnmarshal(b, newObj.Addr().Interface()); err != nil { 732 return err 733 } 734 pointerObjValue.Set(newObj.Addr()) 735 return nil 736 } 737 return nil 738 } 739 740 func getValueJsonStr(arr interface{}) (string, error) { 741 if ret, err := utils.JsonMarshal(&arr); err != nil { 742 return "", err 743 } else { 744 return utils.Bytes2Str(ret), nil 745 } 746 } 747 748 func getValueJsonObj(b []byte, v interface{}) error { 749 if len(b) == 0 || v == nil { 750 return nil 751 } 752 return utils.JsonUnmarshal(b, v) 753 } 754 755 func getValueOfMapStr(obj interface{}, elem *FieldElem) (string, error) { 756 if fv := reflect.ValueOf(obj).Elem().FieldByName(elem.FieldName); fv.IsNil() { 757 return "", nil 758 } else if v := fv.Interface(); v == nil { 759 return "", nil 760 } else if b, err := utils.JsonMarshal(&v); err != nil { 761 return "", err 762 } else { 763 return utils.Bytes2Str(b), nil 764 } 765 }