github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datatype/primitive.go (about)

     1  // Copyright 2020 DataStax
     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 datatype
    16  
    17  import (
    18  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    19  )
    20  
    21  var (
    22  	Ascii     = &PrimitiveType{code: primitive.DataTypeCodeAscii}
    23  	Bigint    = &PrimitiveType{code: primitive.DataTypeCodeBigint}
    24  	Blob      = &PrimitiveType{code: primitive.DataTypeCodeBlob}
    25  	Boolean   = &PrimitiveType{code: primitive.DataTypeCodeBoolean}
    26  	Counter   = &PrimitiveType{code: primitive.DataTypeCodeCounter}
    27  	Date      = &PrimitiveType{code: primitive.DataTypeCodeDate}
    28  	Decimal   = &PrimitiveType{code: primitive.DataTypeCodeDecimal}
    29  	Double    = &PrimitiveType{code: primitive.DataTypeCodeDouble}
    30  	Duration  = &PrimitiveType{code: primitive.DataTypeCodeDuration}
    31  	Float     = &PrimitiveType{code: primitive.DataTypeCodeFloat}
    32  	Inet      = &PrimitiveType{code: primitive.DataTypeCodeInet}
    33  	Int       = &PrimitiveType{code: primitive.DataTypeCodeInt}
    34  	Smallint  = &PrimitiveType{code: primitive.DataTypeCodeSmallint}
    35  	Time      = &PrimitiveType{code: primitive.DataTypeCodeTime}
    36  	Timestamp = &PrimitiveType{code: primitive.DataTypeCodeTimestamp}
    37  	Timeuuid  = &PrimitiveType{code: primitive.DataTypeCodeTimeuuid}
    38  	Tinyint   = &PrimitiveType{code: primitive.DataTypeCodeTinyint}
    39  	Uuid      = &PrimitiveType{code: primitive.DataTypeCodeUuid}
    40  	Varchar   = &PrimitiveType{code: primitive.DataTypeCodeVarchar}
    41  	Varint    = &PrimitiveType{code: primitive.DataTypeCodeVarint}
    42  )
    43  
    44  // PrimitiveType is a datatype that is represented by a CQL scalar type.
    45  // +k8s:deepcopy-gen=true
    46  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/datatype.DataType
    47  type PrimitiveType struct {
    48  	code primitive.DataTypeCode
    49  }
    50  
    51  func (t *PrimitiveType) Code() primitive.DataTypeCode {
    52  	return t.code
    53  }
    54  
    55  func (t *PrimitiveType) String() string {
    56  	return t.AsCql()
    57  }
    58  
    59  func (t *PrimitiveType) AsCql() string {
    60  	switch t.Code() {
    61  	case primitive.DataTypeCodeAscii:
    62  		return "ascii"
    63  	case primitive.DataTypeCodeBigint:
    64  		return "bigint"
    65  	case primitive.DataTypeCodeBlob:
    66  		return "blob"
    67  	case primitive.DataTypeCodeBoolean:
    68  		return "boolean"
    69  	case primitive.DataTypeCodeCounter:
    70  		return "counter"
    71  	case primitive.DataTypeCodeDecimal:
    72  		return "decimal"
    73  	case primitive.DataTypeCodeDouble:
    74  		return "double"
    75  	case primitive.DataTypeCodeFloat:
    76  		return "float"
    77  	case primitive.DataTypeCodeInt:
    78  		return "int"
    79  	case primitive.DataTypeCodeTimestamp:
    80  		return "timestamp"
    81  	case primitive.DataTypeCodeUuid:
    82  		return "uuid"
    83  	case primitive.DataTypeCodeVarchar:
    84  		return "varchar"
    85  	case primitive.DataTypeCodeVarint:
    86  		return "varint"
    87  	case primitive.DataTypeCodeTimeuuid:
    88  		return "timeuuid"
    89  	case primitive.DataTypeCodeInet:
    90  		return "inet"
    91  	case primitive.DataTypeCodeDate:
    92  		return "date"
    93  	case primitive.DataTypeCodeTime:
    94  		return "time"
    95  	case primitive.DataTypeCodeSmallint:
    96  		return "smallint"
    97  	case primitive.DataTypeCodeTinyint:
    98  		return "tinyint"
    99  	case primitive.DataTypeCodeDuration:
   100  		return "duration"
   101  	}
   102  	return "?"
   103  }