github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/data_type/data_type.go (about)

     1  package data_type
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type DataType string
     9  
    10  func (dataType DataType) Is(dataTypes []DataType) bool {
    11  	for _, d := range dataTypes {
    12  		if dataType.String() == d.String() {
    13  			return true
    14  		}
    15  	}
    16  	return false
    17  }
    18  
    19  func (dataType DataType) String() string {
    20  	return strings.ToLower(string(dataType))
    21  }
    22  
    23  func (dataType DataType) WithArgs(args ...string) string {
    24  	return fmt.Sprintf("%s(%s)", dataType.String(), strings.Join(args, ","))
    25  }
    26  
    27  func (dataType DataType) IsInteger() bool {
    28  	return dataType.Is([]DataType{
    29  		"INTEGER",
    30  		"INT",
    31  		"SMALLINT",
    32  		"TINYINT",
    33  		"MEDIUMINT",
    34  		"BIGINT",
    35  	})
    36  }
    37  
    38  func (dataType DataType) IsFloating() bool {
    39  	return dataType.Is([]DataType{
    40  		"FLOAT",
    41  		"DOUBLE",
    42  	})
    43  }
    44  
    45  func (dataType DataType) IsFixed() bool {
    46  	return dataType.Is([]DataType{
    47  		"DECIMAL",
    48  		"NUMERIC",
    49  	})
    50  }
    51  
    52  func (dataType DataType) IsChar() bool {
    53  	return dataType.Is([]DataType{
    54  		"CHAR",
    55  		"VARCHAR",
    56  		"BINARY",
    57  		"VARBINARY",
    58  	})
    59  }
    60  
    61  func (dataType DataType) IsText() bool {
    62  	return dataType.Is([]DataType{
    63  		"TINYTEXT",
    64  		"MEDIUMTEXT",
    65  		"TEXT",
    66  		"LONGTEXT",
    67  	})
    68  }
    69  
    70  func (dataType DataType) IsEnum() bool {
    71  	return dataType.Is([]DataType{
    72  		"ENUM",
    73  	})
    74  }
    75  
    76  func (dataType DataType) IsSet() bool {
    77  	return dataType.Is([]DataType{
    78  		"SET",
    79  	})
    80  }
    81  
    82  func (dataType DataType) IsBlob() bool {
    83  	return dataType.Is([]DataType{
    84  		"TINYBLOB",
    85  		"MEDIUMBLOB",
    86  		"BLOB",
    87  		"LONGBLOB",
    88  	})
    89  }
    90  
    91  func (dataType DataType) IsDate() bool {
    92  	return dataType.Is([]DataType{
    93  		"DATE",
    94  		"TIME",
    95  		"DATETIME",
    96  		"TIMESTAMP",
    97  		"YEAR",
    98  	})
    99  }