|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 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 casts |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + |
| 21 | + "github.com/cockroachdb/errors" |
| 22 | + "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" |
| 23 | + "github.com/dolthub/dolt/go/store/hash" |
| 24 | + |
| 25 | + "github.com/dolthub/doltgresql/core/id" |
| 26 | + "github.com/dolthub/doltgresql/core/rootobject/objinterface" |
| 27 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 28 | +) |
| 29 | + |
| 30 | +// DeserializeRootObject implements the interface objinterface.Collection. |
| 31 | +func (pgc *Collection) DeserializeRootObject(ctx context.Context, data []byte) (objinterface.RootObject, error) { |
| 32 | + return DeserializeCast(ctx, data) |
| 33 | +} |
| 34 | + |
| 35 | +// DiffRootObjects implements the interface objinterface.Collection. |
| 36 | +func (pgc *Collection) DiffRootObjects(ctx context.Context, fromHash string, ours objinterface.RootObject, theirs objinterface.RootObject, ancestor objinterface.RootObject) ([]objinterface.RootObjectDiff, objinterface.RootObject, error) { |
| 37 | + return nil, nil, errors.New("cast conflict detection has not yet been implemented") |
| 38 | +} |
| 39 | + |
| 40 | +// DropRootObject implements the interface objinterface.Collection. |
| 41 | +func (pgc *Collection) DropRootObject(ctx context.Context, identifier id.Id) error { |
| 42 | + if identifier.Section() != id.Section_Cast { |
| 43 | + return errors.Errorf(`cast %s does not exist`, identifier.String()) |
| 44 | + } |
| 45 | + return pgc.DropCast(ctx, id.Cast(identifier)) |
| 46 | +} |
| 47 | + |
| 48 | +// GetFieldType implements the interface objinterface.Collection. |
| 49 | +func (pgc *Collection) GetFieldType(ctx context.Context, fieldName string) *pgtypes.DoltgresType { |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// GetID implements the interface objinterface.Collection. |
| 54 | +func (pgc *Collection) GetID() objinterface.RootObjectID { |
| 55 | + return objinterface.RootObjectID_Casts |
| 56 | +} |
| 57 | + |
| 58 | +// GetRootObject implements the interface objinterface.Collection. |
| 59 | +func (pgc *Collection) GetRootObject(ctx context.Context, identifier id.Id) (objinterface.RootObject, bool, error) { |
| 60 | + if identifier.Section() != id.Section_Cast { |
| 61 | + return nil, false, nil |
| 62 | + } |
| 63 | + c, err := pgc.getCast(ctx, id.Cast(identifier), nil, nil, CastType_Explicit) |
| 64 | + return c, err == nil && c.ID.IsValid(), err |
| 65 | +} |
| 66 | + |
| 67 | +// HasRootObject implements the interface objinterface.Collection. |
| 68 | +func (pgc *Collection) HasRootObject(ctx context.Context, identifier id.Id) (bool, error) { |
| 69 | + if identifier.Section() != id.Section_Cast { |
| 70 | + return false, nil |
| 71 | + } |
| 72 | + return pgc.HasCast(ctx, id.Cast(identifier)), nil |
| 73 | +} |
| 74 | + |
| 75 | +// IDToTableName implements the interface objinterface.Collection. |
| 76 | +func (pgc *Collection) IDToTableName(identifier id.Id) doltdb.TableName { |
| 77 | + if identifier.Section() != id.Section_Cast { |
| 78 | + return doltdb.TableName{} |
| 79 | + } |
| 80 | + return CastIDToTableName(id.Cast(identifier)) |
| 81 | +} |
| 82 | + |
| 83 | +// IterAll implements the interface objinterface.Collection. |
| 84 | +func (pgc *Collection) IterAll(ctx context.Context, callback func(rootObj objinterface.RootObject) (stop bool, err error)) error { |
| 85 | + return pgc.IterateCasts(ctx, func(c Cast) (stop bool, err error) { |
| 86 | + return callback(c) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// IterIDs implements the interface objinterface.Collection. |
| 91 | +func (pgc *Collection) IterIDs(ctx context.Context, callback func(identifier id.Id) (stop bool, err error)) error { |
| 92 | + err := pgc.underlyingMap.IterAll(ctx, func(k string, _ hash.Hash) error { |
| 93 | + stop, err := callback(id.Id(k)) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } else if stop { |
| 97 | + return io.EOF |
| 98 | + } else { |
| 99 | + return nil |
| 100 | + } |
| 101 | + }) |
| 102 | + return err |
| 103 | +} |
| 104 | + |
| 105 | +// PutRootObject implements the interface objinterface.Collection. |
| 106 | +func (pgc *Collection) PutRootObject(ctx context.Context, rootObj objinterface.RootObject) error { |
| 107 | + c, ok := rootObj.(Cast) |
| 108 | + if !ok { |
| 109 | + return errors.Newf("invalid cast root object: %T", rootObj) |
| 110 | + } |
| 111 | + return pgc.AddCast(ctx, c) |
| 112 | +} |
| 113 | + |
| 114 | +// RenameRootObject implements the interface objinterface.Collection. |
| 115 | +func (pgc *Collection) RenameRootObject(ctx context.Context, oldName id.Id, newName id.Id) error { |
| 116 | + if !oldName.IsValid() || !newName.IsValid() || oldName.Section() != newName.Section() || oldName.Section() != id.Section_Cast { |
| 117 | + return errors.New("cannot rename cast due to invalid id") |
| 118 | + } |
| 119 | + oldCastName := id.Cast(oldName) |
| 120 | + newCastName := id.Cast(newName) |
| 121 | + c, err := pgc.getCast(ctx, oldCastName, nil, nil, CastType_Explicit) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + if err = pgc.DropCast(ctx, newCastName); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + c.ID = newCastName |
| 129 | + return pgc.AddCast(ctx, c) |
| 130 | +} |
| 131 | + |
| 132 | +// ResolveName implements the interface objinterface.Collection. |
| 133 | +func (pgc *Collection) ResolveName(ctx context.Context, name doltdb.TableName) (doltdb.TableName, id.Id, error) { |
| 134 | + rawID, err := pgc.resolveName(ctx, name.Schema, name.Name) |
| 135 | + if err != nil || !rawID.IsValid() { |
| 136 | + return doltdb.TableName{}, id.Null, err |
| 137 | + } |
| 138 | + return CastIDToTableName(rawID), rawID.AsId(), nil |
| 139 | +} |
| 140 | + |
| 141 | +// TableNameToID implements the interface objinterface.Collection. |
| 142 | +func (pgc *Collection) TableNameToID(name doltdb.TableName) id.Id { |
| 143 | + return pgc.tableNameToID(name.Schema, name.Name).AsId() |
| 144 | +} |
| 145 | + |
| 146 | +// UpdateField implements the interface objinterface.Collection. |
| 147 | +func (pgc *Collection) UpdateField(ctx context.Context, rootObject objinterface.RootObject, fieldName string, newValue any) (objinterface.RootObject, error) { |
| 148 | + return nil, errors.New("updating through the conflicts table for this object type is not yet supported") |
| 149 | +} |
0 commit comments