RowIter implements the interface sql.ExecSourceRel.
(ctx *sql.Context, r sql.Row)
| 74 | |
| 75 | // RowIter implements the interface sql.ExecSourceRel. |
| 76 | func (c *CreateSequence) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) { |
| 77 | if strings.HasPrefix(strings.ToLower(c.sequence.Id.SequenceName()), "dolt") { |
| 78 | return nil, errors.Errorf("sequences cannot be prefixed with 'dolt'") |
| 79 | } |
| 80 | schema, err := core.GetSchemaName(ctx, nil, c.schema) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | // The sequence won't have the schema filled in, so we have to do that now |
| 85 | c.sequence.Id = id.NewSequence(schema, c.sequence.Id.SequenceName()) |
| 86 | |
| 87 | // Check that the sequence name is free across all relation types (tables, sequences, views, indexes). |
| 88 | // GetSqlDatabaseFromContext returns a plain sqle.Database (not a PgDatabase), so we wrap it |
| 89 | // with tables.WrapSqleDatabase before calling GetSchema so that ValidateNewRelationName is available. |
| 90 | // TODO: Consider always wrapping the returned db from GetSqlDatabaseFromContext() |
| 91 | rawDb, err := core.GetSqlDatabaseFromContext(ctx, "") |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | if rawDb != nil { |
| 96 | if sdb, ok := rawDb.(sqle.Database); ok { |
| 97 | pgDb := tables.WrapSqleDatabase(sdb) |
| 98 | if schemaDb, found, dbErr := pgDb.GetSchema(ctx, schema); dbErr != nil { |
| 99 | return nil, dbErr |
| 100 | } else if found { |
| 101 | if v, ok := schemaDb.(sql.SchemaObjectNameValidator); ok { |
| 102 | nameAlreadyUsed, err := v.ValidateNewSequenceName(ctx, c.sequence.Id.SequenceName(), c.ifNotExists) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } else if nameAlreadyUsed && c.ifNotExists { |
| 106 | return sql.RowsToRowIter(), nil |
| 107 | } |
| 108 | } |
| 109 | } else if !found { |
| 110 | return nil, fmt.Errorf(`schema "%s" not found`, schema) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | // Check that the OWNED BY is valid, if it exists |
| 115 | var table sql.Table |
| 116 | var tableSch sql.Schema |
| 117 | var tableColumn *sql.Column |
| 118 | if c.sequence.OwnerTable.IsValid() { |
| 119 | // The table will only have its name set, so we need to fill in the schema as well |
| 120 | c.sequence.OwnerTable = id.NewTable(schema, c.sequence.OwnerTable.TableName()) |
| 121 | relationType, err := core.GetRelationType(ctx, schema, c.sequence.OwnerTable.TableName()) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | if relationType == core.RelationType_DoesNotExist { |
| 126 | return nil, errors.Errorf(`relation "%s" does not exist`, c.sequence.OwnerTable.TableName()) |
| 127 | } else if relationType != core.RelationType_Table { |
| 128 | return nil, errors.Errorf(`sequence cannot be owned by relation "%s"`, c.sequence.OwnerTable.TableName()) |
| 129 | } |
| 130 | |
| 131 | table, err = core.GetSqlTableFromContext(ctx, "", doltdb.TableName{Name: c.sequence.OwnerTable.TableName(), Schema: schema}) |
| 132 | if err != nil { |
| 133 | return nil, err |
nothing calls this directly
no test coverage detected