RowIter implements the interface sql.ExecSourceRel.
(ctx *sql.Context, r sql.Row)
| 70 | |
| 71 | // RowIter implements the interface sql.ExecSourceRel. |
| 72 | func (c *DropType) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) { |
| 73 | var userRole auth.Role |
| 74 | auth.LockRead(func() { |
| 75 | userRole = auth.GetRole(ctx.Client().User) |
| 76 | }) |
| 77 | if !userRole.IsValid() { |
| 78 | return nil, errors.Errorf(`role "%s" does not exist`, ctx.Client().User) |
| 79 | } |
| 80 | |
| 81 | currentDb := ctx.GetCurrentDatabase() |
| 82 | if len(c.database) > 0 && c.database != currentDb { |
| 83 | return nil, errors.Errorf("DROP TYPE is currently only supported for the current database") |
| 84 | } |
| 85 | schema, err := core.GetSchemaName(ctx, nil, c.schName) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | collection, err := core.GetTypesCollectionFromContext(ctx, "") |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | typeID := id.NewType(schema, c.typName) |
| 94 | typ, err := collection.GetType(ctx, typeID) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | if typ == nil { |
| 99 | if c.ifExists { |
| 100 | // TODO: issue a notice |
| 101 | return sql.RowsToRowIter(), nil |
| 102 | } else { |
| 103 | return nil, types.ErrTypeDoesNotExist.New(c.typName) |
| 104 | } |
| 105 | } |
| 106 | if c.cascade { |
| 107 | // TODO: handle cascade |
| 108 | return nil, errors.Errorf(`cascading type drops are not yet supported`) |
| 109 | } |
| 110 | |
| 111 | if _, ok := types.IDToBuiltInDoltgresType[typ.ID]; ok { |
| 112 | return nil, types.ErrCannotDropSystemType.New(typ.String()) |
| 113 | } |
| 114 | |
| 115 | // TODO: use .IsArrayType() when we support OIDs, so Elem OID isn't 0 |
| 116 | if typ.TypCategory == types.TypeCategory_ArrayTypes { |
| 117 | // TODO: get the base type name |
| 118 | // add HINT: You can drop type ___ instead. (base type) |
| 119 | arrTypeName := typ.String() |
| 120 | return nil, types.ErrCannotDropArrayType.New(arrTypeName, strings.TrimSuffix(arrTypeName, "[]")) |
| 121 | } |
| 122 | |
| 123 | // iterate on all table columns to check if this type is currently used. |
| 124 | db, err := core.GetSqlDatabaseFromContext(ctx, "") |
| 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | tableNames, err := db.GetTableNames(ctx) |
| 129 | if err != nil { |
nothing calls this directly
no test coverage detected