MCPcopy Create free account
hub / github.com/apecloud/myduckserver / ModifyColumn

Method ModifyColumn

catalog/table.go:349–479  ·  view source on GitHub ↗

ModifyColumn implements sql.AlterableTable.

(ctx *sql.Context, columnName string, column *sql.Column, order *sql.ColumnOrder)

Source from the content-addressed store, hash-verified

347 }
348 // Update the PK ordinals only after the column is successfully added.
349 if column.PrimaryKey {
350 t.hasPrimaryKey = true
351 t.comment.Meta.PkOrdinals = tableInfo.PkOrdinals
352 }
353 return t.withSchema(ctx)
354}
355
356// DropColumn implements sql.AlterableTable.
357func (t *Table) DropColumn(ctx *sql.Context, columnName string) error {
358 t.mu.Lock()
359 defer t.mu.Unlock()
360
361 // Check if the column is AUTO_INCREMENT
362 autoIncrement := false
363 for _, column := range t.schema.Schema {
364 if column.AutoIncrement && strings.EqualFold(column.Name, columnName) {
365 autoIncrement = true
366 break
367 }
368 }
369
370 sql := `ALTER TABLE ` + FullTableName(t.db.catalog, t.db.name, t.name) + ` DROP COLUMN ` + QuoteIdentifierANSI(columnName)
371
372 if autoIncrement {
373 // Drop the sequence
374 sql += `; DROP SEQUENCE IF EXISTS ` + t.comment.Meta.Sequence
375 // Remove the sequence name from the table comment
376 extraInfo := t.comment.Meta
377 extraInfo.Sequence = ""
378 comment := NewCommentWithMeta(t.comment.Text, extraInfo)
379 sql += `; COMMENT ON TABLE ` + FullTableName(t.db.catalog, t.db.name, t.name) + ` IS '` + comment.Encode() + `'`
380 }
381
382 _, err := adapter.Exec(ctx, sql)
383 if err != nil {
384 return ErrDuckDB.New(err)
385 }
386
387 // Update the sequence name only after the column is successfully dropped.
388 if autoIncrement {
389 t.comment.Meta.Sequence = ""
390 }
391 return t.withSchema(ctx)
392}
393
394// ModifyColumn implements sql.AlterableTable.
395func (t *Table) ModifyColumn(ctx *sql.Context, columnName string, column *sql.Column, order *sql.ColumnOrder) error {
396 t.mu.Lock()
397 defer t.mu.Unlock()
398
399 typ, err := DuckdbDataType(column.Type)
400 if err != nil {
401 return err
402 }
403
404 // Find existing column to check for AUTO_INCREMENT and PRIMARY KEY
405 var oldColumn *sql.Column
406 var oldColumnIndex int

Callers

nothing calls this directly

Calls 12

withSchemaMethod · 0.95
ExecFunction · 0.92
DuckdbDataTypeFunction · 0.85
FullTableNameFunction · 0.85
QuoteIdentifierANSIFunction · 0.85
parseDefaultValueFunction · 0.85
getCreateSequenceFunction · 0.85
NewCommentWithMetaFunction · 0.85
FullColumnNameFunction · 0.85
EqualsMethod · 0.80
StringMethod · 0.45
EncodeMethod · 0.45

Tested by

no test coverage detected