MCPcopy Create free account
hub / github.com/dolthub/doltgresql / ConvertToType

Method ConvertToType

server/types/type.go:512–553  ·  view source on GitHub ↗

ConvertToType implements the types.ExtendedType interface.

(ctx *sql.Context, typ sql.ExtendedType, val any)

Source from the content-addressed store, hash-verified

510
511// ConvertToType implements the types.ExtendedType interface.
512func (t *DoltgresType) ConvertToType(ctx *sql.Context, typ sql.ExtendedType, val any) (any, sql.ConvertInRange, error) {
513 dt, ok := typ.(*DoltgresType)
514 if !ok {
515 return nil, sql.InRange, errors.Errorf("expected DoltgresType, got %T", typ)
516 }
517
518 cast, err := GetAssignmentCast(ctx, dt, t)
519 if err != nil {
520 return nil, sql.InRange, err
521 }
522 if cast == nil {
523 // In the case that we have an unknown type string literal, we attempt to parse it with the target type's
524 // input function
525 // TODO: this is probably not the best place to perform this conversion, it would probably be better as an
526 // analyzer step. This currently only takes place for foreign key checks and some index lookups, and could be
527 // generalized to many more places.
528 if dt.ID.TypeName() == "unknown" {
529 strVal, ok, err := sql.Unwrap[string](ctx, val)
530 if err != nil {
531 return nil, sql.InRange, err
532 }
533 if ok {
534 converted, err := t.IoInput(ctx, strVal)
535 if err != nil {
536 return nil, sql.InRange, err
537 }
538 return converted, sql.InRange, nil
539 }
540 }
541 return nil, sql.InRange, errors.Errorf("no assignment cast from %s to %s", dt.Name(), t.Name())
542 }
543
544 castResult, err := cast.Eval(ctx, val, dt, t)
545 if err != nil && errors.Is(err, ErrCastOutOfRange) {
546 // TODO: this could be either an overflow or an underflow, we should distinguish
547 return castResult, sql.Overflow, nil
548 } else if err != nil {
549 return nil, sql.InRange, err
550 }
551
552 return castResult, sql.InRange, nil
553}
554
555// DomainUnderlyingBaseType returns an underlying base type of this domain type.
556// It can be a nested domain type, so it recursively searches for a valid base type.

Callers 1

ConvertSerializedMethod · 0.95

Calls 6

IoInputMethod · 0.95
NameMethod · 0.95
TypeNameMethod · 0.80
ErrorfMethod · 0.65
NameMethod · 0.65
EvalMethod · 0.65

Tested by

no test coverage detected