github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/column_type.go (about)

     1  package builder
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	typex "github.com/go-courier/x/types"
     9  )
    10  
    11  func ColumnTypeFromTypeAndTag(typ typex.Type, nameAndFlags string) *ColumnType {
    12  	ct := &ColumnType{}
    13  	ct.Type = typex.Deref(typ)
    14  
    15  	if strings.Contains(nameAndFlags, ",") {
    16  		for _, flag := range strings.Split(nameAndFlags, ",")[1:] {
    17  			nameAndValue := strings.Split(flag, "=")
    18  			switch strings.ToLower(nameAndValue[0]) {
    19  			case "null":
    20  				ct.Null = true
    21  			case "autoincrement":
    22  				ct.AutoIncrement = true
    23  			case "deprecated":
    24  				rename := ""
    25  				if len(nameAndValue) > 1 {
    26  					rename = nameAndValue[1]
    27  				}
    28  				ct.DeprecatedActions = &DeprecatedActions{RenameTo: rename}
    29  			case "size":
    30  				if len(nameAndValue) == 1 {
    31  					panic(fmt.Errorf("missing size value"))
    32  				}
    33  				length, err := strconv.ParseUint(nameAndValue[1], 10, 64)
    34  				if err != nil {
    35  					panic(fmt.Errorf("invalid size value: %s", err))
    36  				}
    37  				ct.Length = length
    38  			case "decimal":
    39  				if len(nameAndValue) == 1 {
    40  					panic(fmt.Errorf("missing size value"))
    41  				}
    42  				decimal, err := strconv.ParseUint(nameAndValue[1], 10, 64)
    43  				if err != nil {
    44  					panic(fmt.Errorf("invalid decimal value: %s", err))
    45  				}
    46  				ct.Decimal = decimal
    47  			case "default":
    48  				if len(nameAndValue) == 1 {
    49  					panic(fmt.Errorf("missing default value"))
    50  				}
    51  				ct.Default = &nameAndValue[1]
    52  			case "onupdate":
    53  				if len(nameAndValue) == 1 {
    54  					panic(fmt.Errorf("missing onupdate value"))
    55  				}
    56  				ct.OnUpdate = &nameAndValue[1]
    57  			}
    58  		}
    59  	}
    60  	return ct
    61  }
    62  
    63  type ColumnType struct {
    64  	Type              typex.Type
    65  	DataType          string
    66  	Length            uint64
    67  	Decimal           uint64
    68  	Default           *string
    69  	OnUpdate          *string
    70  	Null              bool
    71  	AutoIncrement     bool
    72  	DeprecatedActions *DeprecatedActions
    73  	Comment           string
    74  	Description       []string
    75  	Relation          []string
    76  }
    77  
    78  type DeprecatedActions struct {
    79  	RenameTo string `name:"rename"`
    80  }