Package org.h2.schema

Class Sequence

  • All Implemented Interfaces:
    HasSQL

    public final class Sequence
    extends SchemaObject
    A sequence is created using the statement CREATE SEQUENCE
    • Field Detail

      • DEFAULT_CACHE_SIZE

        public static final int DEFAULT_CACHE_SIZE
        The default cache size for sequences.
        See Also:
        Constant Field Values
      • baseValue

        private long baseValue
      • margin

        private long margin
      • increment

        private long increment
      • cacheSize

        private long cacheSize
      • startValue

        private long startValue
      • minValue

        private long minValue
      • maxValue

        private long maxValue
      • belongsToTable

        private boolean belongsToTable
      • writeWithMargin

        private boolean writeWithMargin
    • Constructor Detail

      • Sequence

        public Sequence​(SessionLocal session,
                        Schema schema,
                        int id,
                        java.lang.String name,
                        SequenceOptions options,
                        boolean belongsToTable)
        Creates a new sequence.
        Parameters:
        session - the session
        schema - the schema
        id - the object id
        name - the sequence name
        options - the sequence options
        belongsToTable - whether this sequence belongs to a table (for generated columns)
    • Method Detail

      • modify

        public void modify​(java.lang.Long baseValue,
                           java.lang.Long startValue,
                           java.lang.Long minValue,
                           java.lang.Long maxValue,
                           java.lang.Long increment,
                           Sequence.Cycle cycle,
                           java.lang.Long cacheSize)
        Allows the base value, start value, min value, max value, increment and cache size to be updated atomically, including atomic validation. Useful because setting these attributes one after the other could otherwise result in an invalid sequence state (e.g. min value > max value, start value < min value, etc).
        Parameters:
        baseValue - the base value (null if restart is not requested)
        startValue - the new start value (null if no change)
        minValue - the new min value (null if no change)
        maxValue - the new max value (null if no change)
        increment - the new increment (null if no change)
        cycle - the new cycle value, or null if no change
        cacheSize - the new cache size (null if no change)
      • checkOptions

        private long checkOptions​(long baseValue,
                                  long startValue,
                                  long minValue,
                                  long maxValue,
                                  long increment,
                                  long cacheSize,
                                  boolean mayAdjustCacheSize)
        Validates the specified prospective base value, start value, min value, max value, increment, and cache size relative to each other, since each of their respective validities are contingent on the values of the other parameters.
        Parameters:
        baseValue - the prospective base value
        startValue - the prospective start value
        minValue - the prospective min value
        maxValue - the prospective max value
        increment - the prospective increment
        cacheSize - the prospective cache size
        mayAdjustCacheSize - whether cache size may be adjusted, cache size 0 is adjusted unconditionally to 1
        Returns:
        the prospective or adjusted cache size
      • getMaxCacheSize

        private static long getMaxCacheSize​(long range,
                                            long increment)
      • getDefaultMinValue

        public static long getDefaultMinValue​(java.lang.Long startValue,
                                              long increment,
                                              long[] bounds)
        Calculates default min value.
        Parameters:
        startValue - the start value of the sequence.
        increment - the increment of the sequence value.
        bounds - min and max bounds of data type of the sequence
        Returns:
        min value.
      • getDefaultMaxValue

        public static long getDefaultMaxValue​(java.lang.Long startValue,
                                              long increment,
                                              long[] bounds)
        Calculates default max value.
        Parameters:
        startValue - the start value of the sequence.
        increment - the increment of the sequence value.
        bounds - min and max bounds of data type of the sequence
        Returns:
        min value.
      • getBelongsToTable

        public boolean getBelongsToTable()
      • getDataType

        public TypeInfo getDataType()
      • getEffectivePrecision

        public int getEffectivePrecision()
      • getIncrement

        public long getIncrement()
      • getStartValue

        public long getStartValue()
      • getMinValue

        public long getMinValue()
      • getMaxValue

        public long getMaxValue()
      • getDropSQL

        public java.lang.String getDropSQL()
        Description copied from class: DbObject
        Construct a DROP ... SQL statement for this object.
        Overrides:
        getDropSQL in class DbObject
        Returns:
        the SQL statement
      • getCreateSQLForCopy

        public java.lang.String getCreateSQLForCopy​(Table table,
                                                    java.lang.String quotedName)
        Description copied from class: DbObject
        Build a SQL statement to re-create the object, or to create a copy of the object with a different name or referencing a different table
        Specified by:
        getCreateSQLForCopy in class DbObject
        Parameters:
        table - the new table
        quotedName - the quoted name
        Returns:
        the SQL statement
      • getCreateSQL

        public java.lang.String getCreateSQL()
        Description copied from class: DbObject
        Construct the CREATE ... SQL statement for this object.
        Specified by:
        getCreateSQL in class DbObject
        Returns:
        the SQL statement
      • getSequenceOptionsSQL

        public java.lang.StringBuilder getSequenceOptionsSQL​(java.lang.StringBuilder builder)
        Append the options part of the SQL statement to create the sequence.
        Parameters:
        builder - the builder
        Returns:
        the builder
      • getSequenceOptionsSQL

        private java.lang.StringBuilder getSequenceOptionsSQL​(java.lang.StringBuilder builder,
                                                              long value)
      • increment

        private boolean increment​(long oldBase,
                                  long newBase)
      • decrement

        private boolean decrement​(long oldBase,
                                  long newBase)
      • flushWithoutMargin

        public void flushWithoutMargin()
        Flush the current value to disk.
      • flush

        public void flush​(SessionLocal session)
        Flush the current value, including the margin, to disk.
        Parameters:
        session - the session
      • flushInternal

        private void flushInternal​(SessionLocal session)
      • close

        public void close()
        Flush the current value to disk and close this object.
      • getType

        public int getType()
        Description copied from class: DbObject
        Get the object type.
        Specified by:
        getType in class DbObject
        Returns:
        the object type
      • removeChildrenAndResources

        public void removeChildrenAndResources​(SessionLocal session)
        Description copied from class: DbObject
        Delete all dependent children objects and resources of this object.
        Specified by:
        removeChildrenAndResources in class DbObject
        Parameters:
        session - the session
      • getBaseValue

        public long getBaseValue()
      • getCurrentValue

        public long getCurrentValue()
      • setBelongsToTable

        public void setBelongsToTable​(boolean b)
      • getCacheSize

        public long getCacheSize()