#275 - ENH: Add support for @SequenceGenerator initialValue and allocationSize

This commit is contained in:
rbygrave
2015-04-12 12:10:57 +12:00
parent 6c37dc494b
commit 2ed5e026e0
6 changed files with 70 additions and 14 deletions
@@ -424,4 +424,23 @@ public class DatabasePlatform {
public boolean isDisallowBatchOnCascade() {
return disallowBatchOnCascade;
}
/**
* Generate and return the create sequence DDL.
*/
public String ddlCreateSequence(String sequenceName, int initialValue, int allocationSize) {
StringBuilder sb = new StringBuilder("create sequence ");
sb.append(sequenceName);
if (initialValue > 1) {
sb.append(" start with ").append(initialValue);
}
if (allocationSize > 0 && allocationSize != 50) {
// at this stage ignoring allocationSize 50 as this is the 'default' and
// not consistent with the way Ebean batch fetches sequence values
sb.append(" increment by ").append(allocationSize);
}
sb.append(";");
return sb.toString();
}
}
@@ -36,12 +36,14 @@ public class CreateSequenceVisitor implements BeanVisitor {
+" as DatabasePlatform does not support sequences";
logger.warn(msg);
return false;
}
}
if (descriptor.getSequenceName() != null) {
ctx.write("create sequence ");
ctx.write(descriptor.getSequenceName());
ctx.write(";").writeNewLine().writeNewLine();
String sequenceName = descriptor.getSequenceName();
if (sequenceName != null) {
int initialValue = descriptor.getSequenceInitialValue();
int allocationSize = descriptor.getSequenceAllocationSize();
String createSeq = ctx.getDbPlatform().ddlCreateSequence(sequenceName, initialValue, allocationSize);
ctx.write(createSeq).writeNewLine().writeNewLine();
}
return true;
}
@@ -92,6 +92,10 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
*/
private final String sequenceName;
private final int sequenceInitialValue;
private final int sequenceAllocationSize;
/**
* SQL used to return last inserted id. Used for Identity columns where
* getGeneratedKeys is not supported.
@@ -332,6 +336,8 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
this.idType = deploy.getIdType();
this.idGenerator = deploy.getIdGenerator();
this.sequenceName = deploy.getSequenceName();
this.sequenceInitialValue = deploy.getSequenceInitialValue();
this.sequenceAllocationSize = deploy.getSequenceAllocationSize();
this.selectLastInsertedId = deploy.getSelectLastInsertedId();
this.lazyFetchIncludes = InternString.intern(deploy.getLazyFetchIncludes());
this.concurrencyMode = deploy.getConcurrencyMode();
@@ -1686,6 +1692,20 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return sequenceName;
}
/**
* Return the sequence initial value.
*/
public int getSequenceInitialValue() {
return sequenceInitialValue;
}
/**
* Return the sequence allocation size.
*/
public int getSequenceAllocationSize() {
return sequenceAllocationSize;
}
/**
* Return the SQL used to return the last inserted id.
* <p>
@@ -91,6 +91,10 @@ public class DeployBeanDescriptor<T> {
*/
private String sequenceName;
private int sequenceInitialValue;
private int sequenceAllocationSize;
/**
* Used with Identity columns but no getGeneratedKeys support.
*/
@@ -263,6 +267,22 @@ public class DeployBeanDescriptor<T> {
return entityType;
}
public void setSequenceInitialValue(int sequenceInitialValue) {
this.sequenceInitialValue = sequenceInitialValue;
}
public void setSequenceAllocationSize(int sequenceAllocationSize) {
this.sequenceAllocationSize = sequenceAllocationSize;
}
public int getSequenceInitialValue() {
return sequenceInitialValue;
}
public int getSequenceAllocationSize() {
return sequenceAllocationSize;
}
public void add(DRawSqlMeta rawSqlMeta) {
rawSqlMetas.put(rawSqlMeta.getName(), rawSqlMeta);
if ("default".equals(rawSqlMeta.getName())) {
@@ -349,6 +349,8 @@ public class AnnotationFields extends AnnotationParser {
if (sequenceGenerator.name().equals(genName)) {
genName = sequenceGenerator.sequenceName();
}
descriptor.setSequenceInitialValue(sequenceGenerator.initialValue());
descriptor.setSequenceAllocationSize(sequenceGenerator.allocationSize());
}
GenerationType strategy = gen.strategy();