Spanner: Add trivial sequence number generator. (#101777)

* Add trivial sequence number generator.
This commit is contained in:
Peter Štibraný
2025-03-10 17:37:44 +01:00
committed by GitHub
parent 8142aef64d
commit 9858e40a02
5 changed files with 128 additions and 15 deletions
+19 -14
View File
@@ -345,20 +345,25 @@ func (session *Session) innerInsert(bean any) (int64, error) {
return 0, err
}
//// XXX: hack to handle autoincrement in spanner
//if len(table.AutoIncrement) > 0 && session.engine.dialect.DBType() == "spanner" {
// var found bool
// for _, col := range colNames {
// if col == table.AutoIncrement {
// found = true
// break
// }
// }
// if !found {
// colNames = append(colNames, table.AutoIncrement)
// args = append(args, rand.Int63n(9e15))
// }
//}
// If engine has a sequence number generator, use it to produce values for auto-increment columns.
if len(table.AutoIncrement) > 0 && session.engine.sequenceGenerator != nil {
var found bool
for _, col := range colNames {
if col == table.AutoIncrement {
found = true
break
}
}
if !found {
seq, err := session.engine.sequenceGenerator.Next(session.ctx, table.Name, table.AutoIncrement)
if err != nil {
return 0, fmt.Errorf("failed to generate next value for auto_increment columns: %v", err)
}
colNames = append(colNames, table.AutoIncrement)
args = append(args, seq)
}
}
exprs := session.statement.exprColumns
colPlaces := strings.Repeat("?, ", len(colNames))