github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/parse_string.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    15  	"github.com/cockroachdb/errors"
    16  )
    17  
    18  // ParseAndRequireString parses s as type t for simple types. Arrays and collated
    19  // strings are not handled.
    20  func ParseAndRequireString(t *types.T, s string, ctx ParseTimeContext) (Datum, error) {
    21  	switch t.Family() {
    22  	case types.ArrayFamily:
    23  		return ParseDArrayFromString(ctx, s, t.ArrayContents())
    24  	case types.BitFamily:
    25  		return ParseDBitArray(s)
    26  	case types.BoolFamily:
    27  		return ParseDBool(s)
    28  	case types.BytesFamily:
    29  		return ParseDByte(s)
    30  	case types.DateFamily:
    31  		return ParseDDate(ctx, s)
    32  	case types.DecimalFamily:
    33  		return ParseDDecimal(s)
    34  	case types.FloatFamily:
    35  		return ParseDFloat(s)
    36  	case types.INetFamily:
    37  		return ParseDIPAddrFromINetString(s)
    38  	case types.IntFamily:
    39  		return ParseDInt(s)
    40  	case types.IntervalFamily:
    41  		itm, err := t.IntervalTypeMetadata()
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  		return ParseDIntervalWithTypeMetadata(s, itm)
    46  	case types.GeographyFamily:
    47  		return ParseDGeography(s)
    48  	case types.GeometryFamily:
    49  		return ParseDGeometry(s)
    50  	case types.JsonFamily:
    51  		return ParseDJSON(s)
    52  	case types.OidFamily:
    53  		i, err := ParseDInt(s)
    54  		return NewDOid(*i), err
    55  	case types.StringFamily:
    56  		return NewDString(s), nil
    57  	case types.TimeFamily:
    58  		return ParseDTime(ctx, s, TimeFamilyPrecisionToRoundDuration(t.Precision()))
    59  	case types.TimeTZFamily:
    60  		return ParseDTimeTZ(ctx, s, TimeFamilyPrecisionToRoundDuration(t.Precision()))
    61  	case types.TimestampFamily:
    62  		return ParseDTimestamp(ctx, s, TimeFamilyPrecisionToRoundDuration(t.Precision()))
    63  	case types.TimestampTZFamily:
    64  		return ParseDTimestampTZ(ctx, s, TimeFamilyPrecisionToRoundDuration(t.Precision()))
    65  	case types.UuidFamily:
    66  		return ParseDUuidFromString(s)
    67  	case types.EnumFamily:
    68  		return MakeDEnumFromLogicalRepresentation(t, s)
    69  	default:
    70  		return nil, errors.AssertionFailedf("unknown type %s (%T)", t, t)
    71  	}
    72  }