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