setAutoIncrementValue is a helper function to update the sequence value
(ctx *sql.Context, value uint64)
| 869 | } |
| 870 | |
| 871 | return val, nil |
| 872 | } |
| 873 | |
| 874 | // GetNextAutoIncrementValue implements sql.AutoIncrementTable. |
| 875 | func (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. |
| 919 | func (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 |
| 927 | func (t *Table) setAutoIncrementValue(ctx *sql.Context, value uint64) error { |
| 928 | // DuckDB does not support setting the sequence value directly, |
no test coverage detected