Search in sources :

Example 1 with DbKeyGenerator

use of org.apache.cayenne.map.DbKeyGenerator in project cayenne by apache.

the class PKCustomSequenceGeneratorPanel method onInitInternal.

protected void onInitInternal(DbEntity entity) {
    resetStrategy(entity, false, true);
    if (entity.getPrimaryKeyGenerator() == null) {
        DbKeyGenerator generator = new DbKeyGenerator();
        generator.setGeneratorType(DbKeyGenerator.ORACLE_TYPE);
        entity.setPrimaryKeyGenerator(generator);
    } else {
        setDbEntity(entity);
    }
}
Also used : DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator)

Example 2 with DbKeyGenerator

use of org.apache.cayenne.map.DbKeyGenerator in project cayenne by apache.

the class PKCustomSequenceGeneratorPanel method setDbEntity.

public void setDbEntity(DbEntity entity) {
    DbKeyGenerator generator = entity.getPrimaryKeyGenerator();
    if (generator != null) {
        customPKName.setText(generator.getGeneratorName());
        customPKSize.setText(generator.getKeyCacheSize() != null ? generator.getKeyCacheSize().toString() : "0");
    } else {
        customPKName.setText(null);
        customPKSize.setText(null);
    }
}
Also used : DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator)

Example 3 with DbKeyGenerator

use of org.apache.cayenne.map.DbKeyGenerator in project cayenne by apache.

the class OraclePkGeneratorIT method testSequenceNameCustom1.

@Test
public void testSequenceNameCustom1() throws Exception {
    DbEntity entity = new DbEntity("TEST_ENTITY");
    DbKeyGenerator customGenerator = new DbKeyGenerator();
    customGenerator.setGeneratorType(DbKeyGenerator.ORACLE_TYPE);
    customGenerator.setGeneratorName("CUSTOM_GENERATOR");
    entity.setPrimaryKeyGenerator(customGenerator);
    assertEquals("custom_generator", pkGenerator.sequenceName(entity));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator) Test(org.junit.Test)

Example 4 with DbKeyGenerator

use of org.apache.cayenne.map.DbKeyGenerator in project cayenne by apache.

the class JdbcPkGenerator method generatePk.

/**
 * Generates a unique and non-repeating primary key for specified dbEntity.
 * <p>
 * This implementation is naive since it does not lock the database rows
 * when executing select and subsequent update. Adapter-specific
 * implementations are more robust.
 * </p>
 *
 * @since 3.0
 */
public Object generatePk(DataNode node, DbAttribute pk) throws Exception {
    DbEntity entity = pk.getEntity();
    switch(pk.getType()) {
        case Types.BINARY:
        case Types.VARBINARY:
            return IDUtil.pseudoUniqueSecureByteSequence(pk.getMaxLength());
    }
    DbKeyGenerator pkGenerator = entity.getPrimaryKeyGenerator();
    long cacheSize;
    if (pkGenerator != null && pkGenerator.getKeyCacheSize() != null) {
        cacheSize = pkGenerator.getKeyCacheSize();
    } else {
        cacheSize = getPkCacheSize();
    }
    Long value;
    // if no caching, always generate fresh
    if (cacheSize <= 1) {
        value = longPkFromDatabase(node, entity);
    } else {
        Queue<Long> pks = pkCache.get(entity.getName());
        if (pks == null) {
            // created exhausted LongPkRange
            pks = new ConcurrentLinkedQueue<>();
            Queue<Long> previousPks = pkCache.putIfAbsent(entity.getName(), pks);
            if (previousPks != null) {
                pks = previousPks;
            }
        }
        value = pks.poll();
        if (value == null) {
            value = longPkFromDatabase(node, entity);
            for (long i = value + 1; i < value + cacheSize; i++) {
                pks.add(i);
            }
        }
    }
    if (pk.getType() == Types.BIGINT) {
        return value;
    } else {
        // leaving it up to the user to ensure that PK does not exceed max int...
        return value.intValue();
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator)

Example 5 with DbKeyGenerator

use of org.apache.cayenne.map.DbKeyGenerator in project cayenne by apache.

the class OraclePkGenerator method longPkFromDatabase.

/**
 * Generates primary key by calling Oracle sequence corresponding to the
 * <code>dbEntity</code>. Executed SQL looks like this:
 *
 * <pre>
 *   SELECT pk_table_name.nextval FROM DUAL
 * </pre>
 *
 * @since 3.0
 */
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    DbKeyGenerator pkGenerator = entity.getPrimaryKeyGenerator();
    String pkGeneratingSequenceName;
    if (pkGenerator != null && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType()) && pkGenerator.getGeneratorName() != null) {
        pkGeneratingSequenceName = pkGenerator.getGeneratorName();
    } else {
        pkGeneratingSequenceName = sequenceName(entity);
    }
    try (Connection con = node.getDataSource().getConnection()) {
        try (Statement st = con.createStatement()) {
            String sql = selectNextValQuery(pkGeneratingSequenceName);
            adapter.getJdbcEventLogger().log(sql);
            try (ResultSet rs = st.executeQuery(sql)) {
                if (!rs.next()) {
                    throw new CayenneRuntimeException("Error generating pk for DbEntity %s", entity.getName());
                }
                return rs.getLong(1);
            }
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator)

Aggregations

DbKeyGenerator (org.apache.cayenne.map.DbKeyGenerator)11 DbEntity (org.apache.cayenne.map.DbEntity)4 Test (org.junit.Test)3 EntityEvent (org.apache.cayenne.map.event.EntityEvent)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 MySQLAdapter (org.apache.cayenne.dba.mysql.MySQLAdapter)1 DataMap (org.apache.cayenne.map.DataMap)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 ValidationException (org.apache.cayenne.validation.ValidationException)1