github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/globalstate/auto_increment_tracker.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package globalstate
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
    22  )
    23  
    24  // AutoIncrementTracker knows how to get and set the current auto increment value for a table. It's defined as an
    25  // interface here because implementations need to reach into session state, requiring a dependency on this package.
    26  type AutoIncrementTracker interface {
    27  	// Current returns the current auto increment value for the given table.
    28  	Current(tableName string) uint64
    29  	// Next returns the next auto increment value for the given table, and increments the current value.
    30  	Next(tbl string, insertVal interface{}) (uint64, error)
    31  	// AddNewTable adds a new table to the tracker, initializing the auto increment value to 1.
    32  	AddNewTable(tableName string)
    33  	// DropTable removes a table from the tracker.
    34  	DropTable(ctx *sql.Context, tableName string, wses ...*doltdb.WorkingSet) error
    35  	// CoerceAutoIncrementValue coerces the given value to a uint64, returning an error if it can't be done.
    36  	CoerceAutoIncrementValue(val interface{}) (uint64, error)
    37  	// Set sets the auto increment value for the given table. This operation may silently do nothing if this value is
    38  	// below the current value for this table. The table in the provided working set is assumed to already have the value
    39  	// given, so the new global maximum is computed without regard for its value in that working set.
    40  	Set(ctx *sql.Context, tableName string, table *doltdb.Table, ws ref.WorkingSetRef, newAutoIncVal uint64) (*doltdb.Table, error)
    41  
    42  	// AcquireTableLock acquires the auto increment lock on a table, and reutrns a callback function to release the lock.
    43  	// Depending on the value of the `innodb_autoinc_lock_mode` system variable, the engine may need to acquire and hold
    44  	// the lock for the duration of an insert statement.
    45  	AcquireTableLock(ctx *sql.Context, tableName string) (func(), error)
    46  }