github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/mysql/util.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package mysql
    15  
    16  // GetDefaultFieldLength is used for Interger Types, Flen is the display length.
    17  // Call this when no Flen assigned in ddl.
    18  // or column value is calculated from an expression.
    19  // For example: "select count(*) from t;", the column type is int64 and Flen in ResultField will be 21.
    20  // See: https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
    21  func GetDefaultFieldLength(tp byte) int {
    22  	switch tp {
    23  	case TypeTiny:
    24  		return 4
    25  	case TypeShort:
    26  		return 6
    27  	case TypeInt24:
    28  		return 9
    29  	case TypeLong:
    30  		return 11
    31  	case TypeLonglong:
    32  		return 21
    33  	case TypeDecimal:
    34  		// See: https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
    35  		return 10
    36  	case TypeBit, TypeBlob:
    37  		return -1
    38  	default:
    39  		//TODO: add more types
    40  		return -1
    41  	}
    42  }
    43  
    44  // GetDefaultDecimal returns the default decimal length for column.
    45  func GetDefaultDecimal(tp byte) int {
    46  	switch tp {
    47  	case TypeDecimal:
    48  		// See: https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
    49  		return 0
    50  	default:
    51  		//TODO: add more types
    52  		return -1
    53  	}
    54  }