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);
}
}
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);
}
}
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));
}
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();
}
}
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);
}
}
}
}
Aggregations