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

Method GetNextAutoIncrementValue

catalog/table.go:819–860  ·  view source on GitHub ↗

GetNextAutoIncrementValue implements sql.AutoIncrementTable.

(ctx *sql.Context, insertVal any)

Source from the content-addressed store, hash-verified

817
818 columnInfo := &ColumnInfo{
819 ColumnName: columnName,
820 ColumnIndex: columnIndex,
821 DataType: dataType,
822 IsNullable: isNullable,
823 ColumnDefault: columnDefault,
824 Comment: comment,
825 }
826 columns = append(columns, columnInfo)
827 }
828
829 if err = rows.Err(); err != nil {
830 return nil, err
831 }
832
833 return columns, nil
834}
835
836func (t *IndexedTable) LookupPartitions(ctx *sql.Context, lookup sql.IndexLookup) (sql.PartitionIter, error) {
837 return nil, fmt.Errorf("unimplemented(LookupPartitions) (table: %s, query: %s)", t.name, ctx.Query())
838}
839
840// PeekNextAutoIncrementValue implements sql.AutoIncrementTable.
841func (t *Table) PeekNextAutoIncrementValue(ctx *sql.Context) (uint64, error) {
842 t.mu.RLock()
843 defer t.mu.RUnlock()
844
845 if t.comment.Meta.Sequence == "" {
846 return 0, sql.ErrNoAutoIncrementCol
847 }
848 return t.getNextAutoIncrementValue(ctx)
849}
850
851func (t *Table) getNextAutoIncrementValue(ctx *sql.Context) (uint64, error) {
852 // For PeekNextAutoIncrementValue, we want to see what the next value would be
853 // without actually incrementing. We can do this by getting currval + 1.
854 var val uint64
855 err := adapter.QueryRowCatalog(ctx, `SELECT currval('`+t.comment.Meta.Sequence+`') + 1`).Scan(&val)
856 if err != nil {
857 // https://duckdb.org/docs/sql/statements/create_sequence.html#selecting-the-current-value
858 // > Note that the nextval function must have already been called before calling currval,
859 // > otherwise a Serialization Error (sequence is not yet defined in this session) will be thrown.
860 if !strings.Contains(err.Error(), "sequence is not yet defined in this session") {
861 return 0, ErrDuckDB.New(err)
862 }
863 // If the sequence has not been used yet, we can get the start value from the sequence.

Callers

nothing calls this directly

Calls 3

setAutoIncrementValueMethod · 0.95
QueryRowCatalogFunction · 0.92

Tested by

no test coverage detected