github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/server/column.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 server
    15  
    16  import (
    17  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    18  )
    19  
    20  const maxDeferredCausetNameSize = 256
    21  
    22  // DeferredCausetInfo contains information of a defCausumn
    23  type DeferredCausetInfo struct {
    24  	Schema             string
    25  	Block              string
    26  	OrgBlock           string
    27  	Name               string
    28  	OrgName            string
    29  	DeferredCausetLength       uint32
    30  	Charset            uint16
    31  	Flag               uint16
    32  	Decimal            uint8
    33  	Type               uint8
    34  	DefaultValueLength uint64
    35  	DefaultValue       []byte
    36  }
    37  
    38  // Dump dumps DeferredCausetInfo to bytes.
    39  func (defCausumn *DeferredCausetInfo) Dump(buffer []byte) []byte {
    40  	nameDump, orgnameDump := []byte(defCausumn.Name), []byte(defCausumn.OrgName)
    41  	if len(nameDump) > maxDeferredCausetNameSize {
    42  		nameDump = nameDump[0:maxDeferredCausetNameSize]
    43  	}
    44  	if len(orgnameDump) > maxDeferredCausetNameSize {
    45  		orgnameDump = orgnameDump[0:maxDeferredCausetNameSize]
    46  	}
    47  	buffer = dumpLengthEncodedString(buffer, []byte("def"))
    48  	buffer = dumpLengthEncodedString(buffer, []byte(defCausumn.Schema))
    49  	buffer = dumpLengthEncodedString(buffer, []byte(defCausumn.Block))
    50  	buffer = dumpLengthEncodedString(buffer, []byte(defCausumn.OrgBlock))
    51  	buffer = dumpLengthEncodedString(buffer, nameDump)
    52  	buffer = dumpLengthEncodedString(buffer, orgnameDump)
    53  
    54  	buffer = append(buffer, 0x0c)
    55  
    56  	buffer = dumpUint16(buffer, defCausumn.Charset)
    57  	buffer = dumpUint32(buffer, defCausumn.DeferredCausetLength)
    58  	buffer = append(buffer, dumpType(defCausumn.Type))
    59  	buffer = dumpUint16(buffer, dumpFlag(defCausumn.Type, defCausumn.Flag))
    60  	buffer = append(buffer, defCausumn.Decimal)
    61  	buffer = append(buffer, 0, 0)
    62  
    63  	if defCausumn.DefaultValue != nil {
    64  		buffer = dumpUint64(buffer, uint64(len(defCausumn.DefaultValue)))
    65  		buffer = append(buffer, defCausumn.DefaultValue...)
    66  	}
    67  
    68  	return buffer
    69  }
    70  
    71  func dumpFlag(tp byte, flag uint16) uint16 {
    72  	switch tp {
    73  	case allegrosql.TypeSet:
    74  		return flag | uint16(allegrosql.SetFlag)
    75  	case allegrosql.TypeEnum:
    76  		return flag | uint16(allegrosql.EnumFlag)
    77  	default:
    78  		if allegrosql.HasBinaryFlag(uint(flag)) {
    79  			return flag | uint16(allegrosql.NotNullFlag)
    80  		}
    81  		return flag
    82  	}
    83  }
    84  
    85  func dumpType(tp byte) byte {
    86  	switch tp {
    87  	case allegrosql.TypeSet, allegrosql.TypeEnum:
    88  		return allegrosql.TypeString
    89  	default:
    90  		return tp
    91  	}
    92  }