Skip to content

Commit 7237ec9

Browse files
committed
share update auto_inc func
1 parent 408f146 commit 7237ec9

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

memory/table.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,7 @@ func addColumnToSchema(ctx *sql.Context, data *TableData, newCol *sql.Column, or
12841284
data.autoIncVal = 0
12851285
}
12861286

1287-
nextVal := data.autoIncVal + 1
1288-
if _, inRange, err := newCol.Type.Convert(ctx, nextVal); err == nil && inRange == sql.InRange {
1289-
data.autoIncVal = nextVal
1290-
}
1287+
updateAutoIncrementSafe(ctx, newCol, &data.autoIncVal)
12911288
}
12921289

12931290
newPkOrds := data.schema.PkOrdinals
@@ -2581,3 +2578,21 @@ func (t *TableRevision) AddColumn(ctx *sql.Context, column *sql.Column, order *s
25812578
func (t *TableRevision) IgnoreSessionData() bool {
25822579
return true
25832580
}
2581+
2582+
// updateAutoIncrementSafe safely increments an auto_increment value, handling overflow
2583+
// by ensuring it doesn't exceed the column type's maximum value or wrap around.
2584+
func updateAutoIncrementSafe(ctx *sql.Context, autoCol *sql.Column, autoIncVal *uint64) {
2585+
currentVal := *autoIncVal
2586+
2587+
// Check for arithmetic overflow before adding 1
2588+
if currentVal == math.MaxUint64 {
2589+
// At maximum uint64 value, can't increment further
2590+
return
2591+
}
2592+
2593+
nextVal := currentVal + 1
2594+
if _, inRange, err := autoCol.Type.Convert(ctx, nextVal); err == nil && inRange == sql.InRange {
2595+
*autoIncVal = nextVal
2596+
}
2597+
// If next value would be out of range for the column type, stay at current value
2598+
}

memory/table_editor.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package memory
1616

1717
import (
1818
"fmt"
19-
"math"
2019
"reflect"
2120
"strings"
2221

@@ -193,9 +192,10 @@ func (t *tableEditor) Insert(ctx *sql.Context, row sql.Row) error {
193192
if err != nil {
194193
return err
195194
}
196-
t.updateAutoIncrementValue(ctx, autoCol, insertedVal.(uint64))
195+
t.ea.TableData().autoIncVal = insertedVal.(uint64)
196+
updateAutoIncrementSafe(ctx, autoCol, &t.ea.TableData().autoIncVal)
197197
} else if cmp == 0 {
198-
t.updateAutoIncrementValue(ctx, autoCol, t.ea.TableData().autoIncVal)
198+
updateAutoIncrementSafe(ctx, autoCol, &t.ea.TableData().autoIncVal)
199199
}
200200
}
201201

@@ -888,22 +888,3 @@ func verifyRowTypes(row sql.Row, schema sql.Schema) error {
888888
}
889889
return nil
890890
}
891-
892-
// updateAutoIncrementValue safely increments the auto_increment value, handling overflow
893-
// by ensuring it doesn't exceed the column type's maximum value or wrap around.
894-
func (t *tableEditor) updateAutoIncrementValue(ctx *sql.Context, autoCol *sql.Column, currentVal uint64) {
895-
// Check for arithmetic overflow before adding 1
896-
if currentVal == math.MaxUint64 {
897-
// At maximum uint64 value, can't increment further
898-
t.ea.TableData().autoIncVal = currentVal
899-
return
900-
}
901-
902-
nextVal := currentVal + 1
903-
if _, inRange, err := autoCol.Type.Convert(ctx, nextVal); err == nil && inRange == sql.InRange {
904-
t.ea.TableData().autoIncVal = nextVal
905-
} else {
906-
// If next value would be out of range for the column type, stay at current value
907-
t.ea.TableData().autoIncVal = currentVal
908-
}
909-
}

0 commit comments

Comments
 (0)