SybasePkGenerator (cayenne-doc: Cayenne Documentation 5.0-M2 API)
Description: declaration: package: org.apache.cayenne.dba.sybase, class: SybasePkGenerator

# Class SybasePkGenerator

java.lang.Object
org.apache.cayenne.dba.JdbcPkGenerator
org.apache.cayenne.dba.sybase.SybasePkGenerator

All Implemented Interfaces:
  `PkGenerator`

public class SybasePkGenerator extends JdbcPkGenerator
Primary key generator implementation for Sybase. Uses a lookup table named "AUTO_PK_SUPPORT" and a stored procedure "auto_pk_for_table" to search and increment primary keys for tables.

- ## Field Summary

  ### Fields inherited from class JdbcPkGenerator

  `adapter, DEFAULT_PK_CACHE_SIZE, pkCache, pkCacheSize, pkStartValue`
  Modifier and Type
  Field
  Description
  `protected JdbcAdapter`
  `adapter`
  `static final int`
  `DEFAULT_PK_CACHE_SIZE`
  `protected ConcurrentMap<String, Queue<Long>>`
  `pkCache`
  `protected int`
  `pkCacheSize`
  `protected long`
  `pkStartValue`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  ``
  `SybasePkGenerator()`
  Used by DI
  `protected`
  `SybasePkGenerator(JdbcAdapter adapter)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `createAutoPk(DataNode node, List<DbEntity> dbEntities)`
  Generates database objects to provide automatic primary key support.
  `List<String>`
  `createAutoPkStatements(List<DbEntity> dbEntities)`
  Returns a list of SQL strings needed to generates database objects to provide automatic primary support for the list of entities.
  `void`
  `dropAutoPk(DataNode node, List<DbEntity> dbEntities)`
  Drops database objects related to automatic primary key support.
  `List<String>`
  `dropAutoPkStatements(List<DbEntity> dbEntities)`
  Returns SQL string needed to drop database objects associated with automatic primary key generation.
  `protected long`
  `longPkFromDatabase(DataNode node, DbEntity entity)`
  Performs primary key generation ignoring cache.
  `protected String`
  `pkTableCreateString()`

  ### Methods inherited from class JdbcPkGenerator

  `autoPkTableExists, dropAutoPkString, generatePk, getAdapter, getPkCacheSize, pkCreateString, pkDeleteString, pkSelectString, pkUpdateString, reset, runUpdate, setAdapter, setPkCacheSize`
  Modifier and Type
  Method
  Description
  `protected boolean`
  `autoPkTableExists(DataNode node)`
  Checks if AUTO_PK_TABLE already exists in the database.
  `protected String`
  `dropAutoPkString()`
  `Object`
  `generatePk(DataNode node, DbAttribute pk)`
  Generates a unique and non-repeating primary key for specified dbEntity.
  `JdbcAdapter`
  `getAdapter()`
  Get an adapter associated with current PkGenerator
  `int`
  `getPkCacheSize()`
  Returns a size of the entity primary key cache.
  `protected String`
  `pkCreateString(String entName)`
  `protected String`
  `pkDeleteString(List<DbEntity> dbEntities)`
  `protected String`
  `pkSelectString(String entName)`
  `protected String`
  `pkUpdateString(String entName)`
  `void`
  `reset()`
  Resets any cached primary keys forcing generator to go to the database next time id generation is requested.
  `int`
  `runUpdate(DataNode node, String sql)`
  Runs JDBC update over a Connection obtained from DataNode.
  `void`
  `setAdapter(DbAdapter adapter)`
  Install the adapter associated with current PkGenerator
  `void`
  `setPkCacheSize(int pkCacheSize)`
  Sets the size of the entity primary key cache.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### SybasePkGenerator

    public SybasePkGenerator()
    Used by DI
    Since:
      4.1

  - ### SybasePkGenerator

    protected SybasePkGenerator(JdbcAdapter adapter)

- ## Method Details

  - ### pkTableCreateString

    protected String pkTableCreateString()
    Overrides:
      `pkTableCreateString` in class `JdbcPkGenerator`

  - ### createAutoPk

    public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception
    Generates database objects to provide automatic primary key support. Method will execute the following SQL statements:

    1. Executed only if a corresponding table does not exist in the database.

           CREATE TABLE AUTO_PK_SUPPORT (
              TABLE_NAME VARCHAR(32) NOT NULL,
              NEXT_ID DECIMAL(19,0) NOT NULL
           )

    2. Executed under any circumstances.

        if exists (SELECT * FROM sysobjects WHERE name = 'auto_pk_for_table')
        BEGIN
           DROP PROCEDURE auto_pk_for_table
        END

    3. Executed under any circumstances.

    CREATE PROCEDURE auto_pk_for_table

        @tname VARCHAR(32),
        @pkbatchsize INT AS BEGIN BEGIN TRANSACTION UPDATE AUTO_PK_SUPPORT set NEXT_ID =
                     NEXT_ID +
        @pkbatchsize WHERE TABLE_NAME =
        @tname SELECT NEXT_ID from AUTO_PK_SUPPORT where NEXT_ID =
        @tname COMMIT END

    Specified by:
      `createAutoPk` in interface `PkGenerator`
    Overrides:
      `createAutoPk` in class `JdbcPkGenerator`
    Parameters:
      `node` - node that provides access to a DataSource.
      `dbEntities` - a list of entities that require primary key auto-generation support
    Throws:
      `Exception`

  - ### createAutoPkStatements

    public List<String> createAutoPkStatements(List<DbEntity> dbEntities)
    Description copied from interface: `PkGenerator`
    Returns a list of SQL strings needed to generates database objects to provide automatic primary support for the list of entities. No actual database operations are performed.
    Specified by:
      `createAutoPkStatements` in interface `PkGenerator`
    Overrides:
      `createAutoPkStatements` in class `JdbcPkGenerator`

  - ### dropAutoPk

    public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception
    Drops database objects related to automatic primary key support. Method will execute the following SQL statements:

        if exists (SELECT * FROM sysobjects WHERE name = 'AUTO_PK_SUPPORT')
        BEGIN
           DROP TABLE AUTO_PK_SUPPORT
        END

        if exists (SELECT * FROM sysobjects WHERE name = 'auto_pk_for_table')
        BEGIN
           DROP PROCEDURE auto_pk_for_table
        END

    Specified by:
      `dropAutoPk` in interface `PkGenerator`
    Overrides:
      `dropAutoPk` in class `JdbcPkGenerator`
    Parameters:
      `node` - node that provides access to a DataSource.
      `dbEntities` - a list of entities whose primary key auto-generation support should be dropped.
    Throws:
      `Exception`

  - ### dropAutoPkStatements

    public List<String> dropAutoPkStatements(List<DbEntity> dbEntities)
    Description copied from interface: `PkGenerator`
    Returns SQL string needed to drop database objects associated with automatic primary key generation. No actual database operations are performed.
    Specified by:
      `dropAutoPkStatements` in interface `PkGenerator`
    Overrides:
      `dropAutoPkStatements` in class `JdbcPkGenerator`

  - ### longPkFromDatabase

    protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception
    Description copied from class: `JdbcPkGenerator`
    Performs primary key generation ignoring cache. Generates a range of primary keys as specified by "pkCacheSize" bean property.

    This method is called internally from "generatePkForDbEntity" and then generated range of key values is saved in cache for performance. Subclasses that implement different primary key generation solutions should override this method, not "generatePkForDbEntity".

    Overrides:
      `longPkFromDatabase` in class `JdbcPkGenerator`
    Throws:
      `Exception`
    Since:
      3.0