github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/test/types.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package test 是用于辅助测试的包。仅限于内部使用
    16  package test
    17  
    18  import (
    19  	"database/sql"
    20  	"database/sql/driver"
    21  	"encoding/json"
    22  	"fmt"
    23  
    24  	"github.com/ecodeclub/ekit"
    25  )
    26  
    27  // SimpleStruct 包含所有 eorm 支持的类型
    28  type SimpleStruct struct {
    29  	Id      uint64 `eorm:"primary_key,column=int_c"`
    30  	Bool    bool
    31  	BoolPtr *bool
    32  
    33  	Int    int
    34  	IntPtr *int
    35  
    36  	Int8    int8
    37  	Int8Ptr *int8
    38  
    39  	Int16    int16
    40  	Int16Ptr *int16
    41  
    42  	Int32    int32
    43  	Int32Ptr *int32
    44  
    45  	Int64    int64
    46  	Int64Ptr *int64
    47  
    48  	Uint    uint
    49  	UintPtr *uint
    50  
    51  	Uint8    uint8
    52  	Uint8Ptr *uint8
    53  
    54  	Uint16    uint16
    55  	Uint16Ptr *uint16
    56  
    57  	Uint32    uint32
    58  	Uint32Ptr *uint32
    59  
    60  	Uint64    uint64
    61  	Uint64Ptr *uint64
    62  
    63  	Float32    float32
    64  	Float32Ptr *float32
    65  
    66  	Float64    float64
    67  	Float64Ptr *float64
    68  
    69  	// Byte byte
    70  	// BytePtr *byte
    71  	ByteArray []byte
    72  
    73  	String string
    74  
    75  	// 特殊类型
    76  	NullStringPtr *sql.NullString
    77  	NullInt16Ptr  *sql.NullInt16
    78  	NullInt32Ptr  *sql.NullInt32
    79  	NullInt64Ptr  *sql.NullInt64
    80  	NullBoolPtr   *sql.NullBool
    81  	// NullTimePtr    *sql.NullTime
    82  	NullFloat64Ptr *sql.NullFloat64
    83  	JsonColumn     *JsonColumn
    84  }
    85  
    86  // JsonColumn 是自定义的 JSON 类型字段
    87  // Val 字段必须是结构体指针
    88  type JsonColumn struct {
    89  	Val   User
    90  	Valid bool
    91  }
    92  
    93  type User struct {
    94  	Name string
    95  }
    96  
    97  func (j *JsonColumn) Scan(src any) error {
    98  	if src == nil {
    99  		return nil
   100  	}
   101  	var bs []byte
   102  	switch val := src.(type) {
   103  	case string:
   104  		bs = []byte(val)
   105  	case []byte:
   106  		bs = val
   107  	case *[]byte:
   108  		if val == nil {
   109  			return nil
   110  		}
   111  		bs = *val
   112  	default:
   113  		return fmt.Errorf("不合法类型 %+v", src)
   114  	}
   115  	if len(bs) == 0 {
   116  		return nil
   117  	}
   118  	err := json.Unmarshal(bs, &j.Val)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	j.Valid = true
   123  	return nil
   124  }
   125  
   126  // Value 参考 sql.NullXXX 类型定义的
   127  func (j *JsonColumn) Value() (driver.Value, error) {
   128  	if j == nil || !j.Valid {
   129  		return nil, nil
   130  	}
   131  	bs, err := json.Marshal(j.Val)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return bs, nil
   136  }
   137  
   138  func NewSimpleStruct(id uint64) *SimpleStruct {
   139  	return &SimpleStruct{
   140  		Id:             id,
   141  		Bool:           true,
   142  		BoolPtr:        ekit.ToPtr[bool](false),
   143  		Int:            12,
   144  		IntPtr:         ekit.ToPtr[int](13),
   145  		Int8:           8,
   146  		Int8Ptr:        ekit.ToPtr[int8](-8),
   147  		Int16:          16,
   148  		Int16Ptr:       ekit.ToPtr[int16](-16),
   149  		Int32:          32,
   150  		Int32Ptr:       ekit.ToPtr[int32](-32),
   151  		Int64:          64,
   152  		Int64Ptr:       ekit.ToPtr[int64](-64),
   153  		Uint:           14,
   154  		UintPtr:        ekit.ToPtr[uint](15),
   155  		Uint8:          8,
   156  		Uint8Ptr:       ekit.ToPtr[uint8](18),
   157  		Uint16:         16,
   158  		Uint16Ptr:      ekit.ToPtr[uint16](116),
   159  		Uint32:         32,
   160  		Uint32Ptr:      ekit.ToPtr[uint32](132),
   161  		Uint64:         64,
   162  		Uint64Ptr:      ekit.ToPtr[uint64](164),
   163  		Float32:        3.2,
   164  		Float32Ptr:     ekit.ToPtr[float32](-3.2),
   165  		Float64:        6.4,
   166  		Float64Ptr:     ekit.ToPtr[float64](-6.4),
   167  		ByteArray:      []byte("hello"),
   168  		String:         "world",
   169  		NullStringPtr:  &sql.NullString{String: "null string", Valid: true},
   170  		NullInt16Ptr:   &sql.NullInt16{Int16: 16, Valid: true},
   171  		NullInt32Ptr:   &sql.NullInt32{Int32: 32, Valid: true},
   172  		NullInt64Ptr:   &sql.NullInt64{Int64: 64, Valid: true},
   173  		NullBoolPtr:    &sql.NullBool{Bool: true, Valid: true},
   174  		NullFloat64Ptr: &sql.NullFloat64{Float64: 6.4, Valid: true},
   175  		JsonColumn: &JsonColumn{
   176  			Val:   User{Name: "Tom"},
   177  			Valid: true,
   178  		},
   179  	}
   180  }
   181  
   182  type BaseEntity struct {
   183  	CreateTime uint64
   184  	UpdateTime uint64
   185  }
   186  
   187  type CombinedModel struct {
   188  	BaseEntity
   189  	Id        int64 `eorm:"auto_increment,primary_key"`
   190  	FirstName string
   191  	Age       int8
   192  	LastName  *string
   193  }
   194  
   195  func NewCombinedModel(id int64) *CombinedModel {
   196  	return &CombinedModel{
   197  		BaseEntity: BaseEntity{
   198  			CreateTime: 10000,
   199  			UpdateTime: 10000,
   200  		},
   201  		Id:        id,
   202  		FirstName: "Tom" + fmt.Sprintln(id),
   203  		Age:       20,
   204  		LastName:  ekit.ToPtr[string]("Jerry" + fmt.Sprintln(id)),
   205  	}
   206  }
   207  
   208  type Order struct {
   209  	Id        int
   210  	UsingCol1 string
   211  	UsingCol2 string
   212  }
   213  
   214  type OrderDetail struct {
   215  	OrderId   int
   216  	ItemId    int
   217  	UsingCol1 string
   218  	UsingCol2 string
   219  }
   220  
   221  type Item struct {
   222  	Id int
   223  }