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

Method setAutoIncrementValue

catalog/table.go:871–931  ·  view source on GitHub ↗

setAutoIncrementValue is a helper function to update the sequence value

(ctx *sql.Context, value uint64)

Source from the content-addressed store, hash-verified

869 }
870
871 return val, nil
872}
873
874// GetNextAutoIncrementValue implements sql.AutoIncrementTable.
875func (t *Table) GetNextAutoIncrementValue(ctx *sql.Context, insertVal any) (uint64, error) {
876 t.mu.Lock()
877 defer t.mu.Unlock()
878
879 if t.comment.Meta.Sequence == "" {
880 return 0, sql.ErrNoAutoIncrementCol
881 }
882
883 nextVal, err := t.getNextAutoIncrementValue(ctx)
884 if err != nil {
885 return 0, err
886 }
887
888 // If insertVal is provided and greater than the next sequence value, update sequence
889 if insertVal != nil {
890 var start uint64
891 switch v := insertVal.(type) {
892 case uint64:
893 start = v
894 case int64:
895 if v > 0 {
896 start = uint64(v)
897 }
898 }
899 if start > 0 && start > nextVal {
900 err := t.setAutoIncrementValue(ctx, start)
901 if err != nil {
902 return 0, err
903 }
904 return start, nil
905 }
906 }
907
908 // Get next value from sequence
909 var val uint64
910 err = adapter.QueryRowCatalog(ctx, `SELECT nextval('`+t.comment.Meta.Sequence+`')`).Scan(&val)
911 if err != nil {
912 return 0, ErrDuckDB.New(err)
913 }
914
915 return val, nil
916}
917
918// AutoIncrementSetter implements sql.AutoIncrementTable.
919func (t *Table) AutoIncrementSetter(ctx *sql.Context) sql.AutoIncrementSetter {
920 if t.comment.Meta.Sequence == "" {
921 return nil
922 }
923 return &autoIncrementSetter{t: t}
924}
925
926// setAutoIncrementValue is a helper function to update the sequence value
927func (t *Table) setAutoIncrementValue(ctx *sql.Context, value uint64) error {
928 // DuckDB does not support setting the sequence value directly,

Callers 2

SetAutoIncrementValueMethod · 0.80

Calls 7

updateExtraTableInfoMethod · 0.95
withSchemaMethod · 0.95
ExecFunction · 0.92
getCreateSequenceFunction · 0.85
FullTableNameFunction · 0.85
QuoteIdentifierANSIFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected