github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/table/types/types.go (about) 1 package types 2 3 import ( 4 "bytes" 5 6 "github.com/ydb-platform/ydb-go-sdk/v3/internal/scanner" 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/types" 8 ) 9 10 const ( 11 decimalPrecision uint32 = 22 12 decimalScale uint32 = 9 13 ) 14 15 // Type describes YDB data types. 16 type Type = types.Type 17 18 // Equal checks for type equivalence 19 func Equal(lhs, rhs Type) bool { 20 return types.Equal(lhs, rhs) 21 } 22 23 func List(t Type) Type { 24 return types.NewList(t) 25 } 26 27 func Tuple(elems ...Type) Type { 28 return types.NewTuple(elems...) 29 } 30 31 type tStructType struct { 32 fields []types.StructField 33 } 34 35 type StructOption func(*tStructType) 36 37 func StructField(name string, t Type) StructOption { 38 return func(s *tStructType) { 39 s.fields = append(s.fields, types.StructField{ 40 Name: name, 41 T: t, 42 }) 43 } 44 } 45 46 func Struct(opts ...StructOption) Type { 47 var s tStructType 48 for _, opt := range opts { 49 if opt != nil { 50 opt(&s) 51 } 52 } 53 54 return types.NewStruct(s.fields...) 55 } 56 57 func Dict(k, v Type) Type { 58 return types.NewDict(k, v) 59 } 60 61 func VariantStruct(opts ...StructOption) Type { 62 var s tStructType 63 for _, opt := range opts { 64 if opt != nil { 65 opt(&s) 66 } 67 } 68 69 return types.NewVariantStruct(s.fields...) 70 } 71 72 func VariantTuple(elems ...Type) Type { 73 return types.NewVariantTuple(elems...) 74 } 75 76 func Void() Type { 77 return types.NewVoid() 78 } 79 80 func Optional(t Type) Type { 81 return types.NewOptional(t) 82 } 83 84 var DefaultDecimal = DecimalType(decimalPrecision, decimalScale) 85 86 func DecimalType(precision, scale uint32) Type { 87 return types.NewDecimal(precision, scale) 88 } 89 90 func DecimalTypeFromDecimal(d *Decimal) Type { 91 return types.NewDecimal(d.Precision, d.Scale) 92 } 93 94 // Primitive types known by YDB. 95 const ( 96 TypeUnknown = types.Unknown 97 TypeBool = types.Bool 98 TypeInt8 = types.Int8 99 TypeUint8 = types.Uint8 100 TypeInt16 = types.Int16 101 TypeUint16 = types.Uint16 102 TypeInt32 = types.Int32 103 TypeUint32 = types.Uint32 104 TypeInt64 = types.Int64 105 TypeUint64 = types.Uint64 106 TypeFloat = types.Float 107 TypeDouble = types.Double 108 TypeDate = types.Date 109 TypeDatetime = types.Datetime 110 TypeTimestamp = types.Timestamp 111 TypeInterval = types.Interval 112 TypeTzDate = types.TzDate 113 TypeTzDatetime = types.TzDatetime 114 TypeTzTimestamp = types.TzTimestamp 115 TypeString = types.Bytes 116 TypeBytes = types.Bytes 117 TypeUTF8 = types.Text 118 TypeText = types.Text 119 TypeYSON = types.YSON 120 TypeJSON = types.JSON 121 TypeUUID = types.UUID 122 TypeJSONDocument = types.JSONDocument 123 TypeDyNumber = types.DyNumber 124 ) 125 126 // WriteTypeStringTo writes ydb type string representation into buffer 127 // 128 // Deprecated: use types.Type.Yql() instead. 129 // Will be removed after Oct 2024. 130 // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated 131 func WriteTypeStringTo(buf *bytes.Buffer, t Type) { //nolint: interfacer 132 buf.WriteString(t.Yql()) 133 } 134 135 type ( 136 RawValue = scanner.RawValue 137 Scanner = scanner.Scanner 138 )