From f871259f20806ad2b54592eaa5f11bafa9fd6395 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Tue, 3 Mar 2020 21:13:45 +1300 Subject: [PATCH] Support for @Identity - split IdentityMode and DeployIdentityMode --- .../dbmigration/model/MTable.java | 4 +- .../server/deploy/BeanDescriptor.java | 10 +- .../server/deploy/BeanDescriptorManager.java | 3 +- .../server/deploy/IdentityMode.java | 120 +++++----------- .../deploy/meta/DeployBeanDescriptor.java | 21 +-- .../deploy/meta/DeployIdentityMode.java | 135 ++++++++++++++++++ .../server/deploy/parse/AnnotationFields.java | 4 +- 7 files changed, 191 insertions(+), 106 deletions(-) create mode 100644 src/main/java/io/ebeaninternal/server/deploy/meta/DeployIdentityMode.java diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java index 115f38ef6..5a1dc8ba8 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java @@ -13,8 +13,8 @@ import io.ebeaninternal.dbmigration.migration.DropTable; import io.ebeaninternal.dbmigration.migration.ForeignKey; import io.ebeaninternal.dbmigration.migration.UniqueConstraint; import io.ebeaninternal.server.deploy.BeanDescriptor; -import io.ebeaninternal.server.deploy.IdentityMode; import io.ebeaninternal.server.deploy.BeanProperty; +import io.ebeaninternal.server.deploy.IdentityMode; import io.ebeaninternal.server.deploy.PartitionMeta; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -142,7 +142,7 @@ public class MTable { */ public MTable(String name) { this.name = name; - this.identityMode = IdentityMode.none(); + this.identityMode = IdentityMode.NONE; } /** diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java index 10c9cc914..d2adb35f4 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java @@ -154,19 +154,13 @@ public class BeanDescriptor implements BeanType, STreeType { */ private final EntityType entityType; - /** - * Type of Identity generation strategy used. - */ - private final IdType idType; - /** * Set when Id property is marked with GeneratedValue annotation. */ private final boolean idGeneratedValue; - private final PlatformIdGenerator idGenerator; - private final IdentityMode identityMode; + private final IdType idType; /** * SQL used to return last inserted id. Used for Identity columns where @@ -447,7 +441,7 @@ public class BeanDescriptor implements BeanType, STreeType { this.changeLogFilter = deploy.getChangeLogFilter(); this.defaultSelectClause = deploy.getDefaultSelectClause(); - this.identityMode = deploy.getIdentityMode(); + this.identityMode = deploy.buildIdentityMode(); this.idType = identityMode.getIdType(); this.idGeneratedValue = deploy.isIdGeneratedValue(); this.idGenerator = deploy.getIdGenerator(); diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java index 4899349ef..79ef7d295 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java @@ -46,6 +46,7 @@ import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne; import io.ebeaninternal.server.deploy.meta.DeployBeanTable; import io.ebeaninternal.server.deploy.meta.DeployOrderColumn; import io.ebeaninternal.server.deploy.meta.DeployTableJoin; +import io.ebeaninternal.server.deploy.meta.DeployIdentityMode; import io.ebeaninternal.server.deploy.parse.DeployBeanInfo; import io.ebeaninternal.server.deploy.parse.DeployCreateProperties; import io.ebeaninternal.server.deploy.parse.DeployInherit; @@ -1354,7 +1355,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap { return; } - final IdentityMode identityMode = desc.getIdentityMode(); + final DeployIdentityMode identityMode = desc.getIdentityMode(); if (identityMode.isSequence() && !dbIdentity.isSupportsSequence()) { // explicit sequence but not supported by the DatabasePlatform logger.info("Explicit sequence on " + desc.getFullName() + " but not supported by DB Platform - ignored"); diff --git a/src/main/java/io/ebeaninternal/server/deploy/IdentityMode.java b/src/main/java/io/ebeaninternal/server/deploy/IdentityMode.java index 03c6af391..83d201f97 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/IdentityMode.java +++ b/src/main/java/io/ebeaninternal/server/deploy/IdentityMode.java @@ -1,106 +1,58 @@ package io.ebeaninternal.server.deploy; import io.ebean.annotation.IdentityGenerated; -import io.ebean.annotation.IdentityType; import io.ebean.config.dbplatform.IdType; +import io.ebeaninternal.server.deploy.meta.DeployIdentityMode; public class IdentityMode { - public static IdentityMode auto() { - return new IdentityMode(IdType.AUTO); - } + public static IdentityMode NONE = new IdentityMode(); - public static IdentityMode none() { - return new IdentityMode(null); - } + private final IdType type; + private final IdentityGenerated generated; + private final int start; + private final int increment; + private final int cache; + private final String sequenceName; + private final boolean platformDefault; - public static IdType idType(IdentityType type) { - switch (type) { - case AUTO: - return IdType.AUTO; - case SEQUENCE: - return IdType.SEQUENCE; - case IDENTITY: - return IdType.IDENTITY; - case APPLICATION: - return IdType.EXTERNAL; - default: - throw new IllegalStateException("type " + type + " not expected?"); - } - } - - private IdType type; - private IdentityGenerated generated; - private int start; - private int increment; - private int cache; - private String sequenceName; - private boolean platformDefault; - - public IdentityMode(IdType type, IdentityGenerated generated, int start, int increment, String seqName) { - this.type = type; - this.generated = generated; - this.start = start; - this.increment = increment; - this.sequenceName = seqName; + /** + * Create from deployment. + */ + public IdentityMode(DeployIdentityMode deploy) { + this.type = deploy.getIdType(); + this.generated = deploy.getGenerated(); + this.start = deploy.getStart(); + this.increment = deploy.getIncrement(); + this.cache = deploy.getCache(); + this.sequenceName = deploy.getSequenceName(); + this.platformDefault = deploy.isPlatformDefault(); } /** - * Create from @SequenceGenerator annotation. + * Create from migration model CreateTable. */ - public IdentityMode(int initialValue, int allocationSize, String sequenceName) { - this.type = IdType.AUTO; - this.generated = IdentityGenerated.AUTO; - this.sequenceName = sequenceName; - this.start = initialValue; - this.increment = allocationSize; - this.cache = 0; + public IdentityMode(IdType type, IdentityGenerated auto, int start, int increment, String seqName) { + this.type = type; + this.generated = auto; + this.start = start; + this.increment = increment; + this.sequenceName = seqName; + this.cache = 0; //TODO cache + this.platformDefault = false; } - private IdentityMode(IdType type) { - this.type = type; + /** + * NONE constructor. + */ + private IdentityMode() { + this.type = null; this.generated = IdentityGenerated.AUTO; - this.sequenceName = ""; this.start = 0; this.increment = 0; this.cache = 0; - } - - public void setPlatformType(IdType type) { - this.type = type; - this.platformDefault = true; - } - - public void setSequence(int initialValue, int allocationSize, String sequenceName) { - this.start = initialValue; - this.increment = allocationSize; - this.sequenceName = sequenceName; - } - - public void setSequenceGenerator(String genName) { - if (sequenceName == null || sequenceName.isEmpty()) { - sequenceName = genName; - } - } - - public void setSequenceBatchMode() { - this.increment = 1; - } - - public void setIdType(IdType type) { - this.type = type; - } - - public void setStart(int start) { - this.start = start; - } - - public void setIncrement(int increment) { - this.increment = increment; - } - - public void setSequenceName(String sequenceName) { - this.sequenceName = sequenceName; + this.sequenceName = ""; + this.platformDefault = false; } public boolean isPlatformDefault() { diff --git a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java index 522499bf3..3ac8f72dc 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java +++ b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java @@ -3,6 +3,7 @@ package io.ebeaninternal.server.deploy.meta; import io.ebean.annotation.Cache; import io.ebean.annotation.DocStore; import io.ebean.annotation.DocStoreMode; +import io.ebean.annotation.Identity; import io.ebean.config.ServerConfig; import io.ebean.config.TableName; import io.ebean.config.dbplatform.IdType; @@ -17,6 +18,7 @@ import io.ebean.event.changelog.ChangeLogFilter; import io.ebean.text.PathProperties; import io.ebeaninternal.api.ConcurrencyMode; import io.ebeaninternal.server.core.CacheOptions; +import io.ebeaninternal.server.deploy.IdentityMode; import io.ebeaninternal.server.deploy.BeanDescriptor.EntityType; import io.ebeaninternal.server.deploy.BeanDescriptorManager; import io.ebeaninternal.server.deploy.ChainedBeanPersistController; @@ -25,7 +27,6 @@ import io.ebeaninternal.server.deploy.ChainedBeanPostConstructListener; import io.ebeaninternal.server.deploy.ChainedBeanPostLoad; import io.ebeaninternal.server.deploy.ChainedBeanQueryAdapter; import io.ebeaninternal.server.deploy.DeployPropertyParserMap; -import io.ebeaninternal.server.deploy.IdentityMode; import io.ebeaninternal.server.deploy.IndexDefinition; import io.ebeaninternal.server.deploy.InheritInfo; import io.ebeaninternal.server.deploy.PartitionMeta; @@ -90,7 +91,7 @@ public class DeployBeanDescriptor { private DeployBeanPropertyAssocOne idClassProperty; - private IdentityMode identityMode = IdentityMode.auto(); + private DeployIdentityMode identityMode = DeployIdentityMode.auto(); private PlatformIdGenerator idGenerator; @@ -377,15 +378,19 @@ public class DeployBeanDescriptor { return entityType; } - public IdentityMode getIdentityMode() { + /** + * Return the immutable IdentityMode. + */ + public IdentityMode buildIdentityMode() { + return new IdentityMode(identityMode); + } + + public DeployIdentityMode getIdentityMode() { return identityMode; } - /** - * Set from @Identity - */ - public void setIdentityMode(IdentityMode identityMode) { - this.identityMode = identityMode; + public void setIdentityMode(Identity identity) { + this.identityMode = new DeployIdentityMode(identity); } /** diff --git a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployIdentityMode.java b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployIdentityMode.java new file mode 100644 index 000000000..25172fceb --- /dev/null +++ b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployIdentityMode.java @@ -0,0 +1,135 @@ +package io.ebeaninternal.server.deploy.meta; + +import io.ebean.annotation.Identity; +import io.ebean.annotation.IdentityGenerated; +import io.ebean.annotation.IdentityType; +import io.ebean.config.dbplatform.IdType; + +public class DeployIdentityMode { + + public static DeployIdentityMode auto() { + return new DeployIdentityMode(IdType.AUTO); + } + + private IdType type; + private IdentityGenerated generated; + private int start; + private int increment; + private int cache; + private String sequenceName; + private boolean platformDefault; + + public DeployIdentityMode(Identity id) { + this.type = idType(id.type()); + this.generated = id.generated(); + this.start = id.start(); + this.increment = id.increment(); + this.cache = id.cache(); + this.sequenceName = id.sequenceName(); + } + + private DeployIdentityMode(IdType type) { + this.type = type; + this.generated = IdentityGenerated.AUTO; + this.sequenceName = ""; + this.start = 0; + this.increment = 0; + this.cache = 0; + } + + public void setPlatformType(IdType type) { + this.type = type; + this.platformDefault = true; + } + + public void setSequence(int initialValue, int allocationSize, String sequenceName) { + this.start = initialValue; + this.increment = allocationSize; + this.sequenceName = sequenceName; + } + + public void setSequenceGenerator(String genName) { + if (sequenceName == null || sequenceName.isEmpty()) { + sequenceName = genName; + } + } + + public void setSequenceBatchMode() { + this.increment = 1; + } + + public void setIdType(IdType type) { + this.type = type; + } + + public void setStart(int start) { + this.start = start; + } + + public void setIncrement(int increment) { + this.increment = increment; + } + + public void setSequenceName(String sequenceName) { + this.sequenceName = sequenceName; + } + + public boolean isPlatformDefault() { + return platformDefault; + } + + public IdType getIdType() { + return type; + } + + public IdentityGenerated getGenerated() { + return generated; + } + + public int getStart() { + return start; + } + + public int getIncrement() { + return increment; + } + + public int getCache() { + return cache; + } + + public String getSequenceName() { + return sequenceName; + } + + public boolean isSequence() { + return type == IdType.SEQUENCE; + } + + public boolean isIdentity() { + return type == IdType.IDENTITY; + } + + public boolean isExternal() { + return type == IdType.EXTERNAL; + } + + public boolean isAuto() { + return type == IdType.AUTO; + } + + private static IdType idType(IdentityType type) { + switch (type) { + case AUTO: + return IdType.AUTO; + case SEQUENCE: + return IdType.SEQUENCE; + case IDENTITY: + return IdType.IDENTITY; + case APPLICATION: + return IdType.EXTERNAL; + default: + throw new IllegalStateException("type " + type + " not expected?"); + } + } +} diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationFields.java b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationFields.java index 68638299b..14a35ac31 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationFields.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationFields.java @@ -40,7 +40,6 @@ import io.ebean.config.dbplatform.DbEncryptFunction; import io.ebean.config.dbplatform.IdType; import io.ebean.config.dbplatform.PlatformIdGenerator; import io.ebeaninternal.server.deploy.DbMigrationInfo; -import io.ebeaninternal.server.deploy.IdentityMode; import io.ebeaninternal.server.deploy.IndexDefinition; import io.ebeaninternal.server.deploy.generatedproperty.GeneratedPropertyFactory; import io.ebeaninternal.server.deploy.meta.DeployBeanProperty; @@ -385,8 +384,7 @@ public class AnnotationFields extends AnnotationParser { } private void readIdentity(Identity identity) { - final IdType idType = IdentityMode.idType(identity.type()); - descriptor.setIdentityMode(new IdentityMode(idType, identity.generated(), identity.start(), identity.increment(), identity.sequenceName())); + descriptor.setIdentityMode(identity); } private void readDbMigration(DeployBeanProperty prop) {