Refactor internals - move DbOffline, split VisitAllUsing into VisitProperties

This commit is contained in:
rob bygrave
2020-10-09 18:02:40 +13:00
parent 60becb6346
commit a6f5ab8074
15 changed files with 137 additions and 131 deletions
@@ -1,4 +1,4 @@
package io.ebeaninternal.dbmigration;
package io.ebeaninternal.api;
import io.ebean.annotation.Platform;
import org.slf4j.Logger;
@@ -29,6 +29,7 @@ import io.ebean.config.dbplatform.sqlserver.SqlServer16Platform;
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
import io.ebean.dbmigration.DbMigration;
import io.ebean.migration.MigrationVersion;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
@@ -1,7 +1,7 @@
package io.ebeaninternal.dbmigration.model.build;
import io.ebeaninternal.dbmigration.model.MTable;
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
import io.ebeaninternal.server.deploy.visitor.VisitProperties;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanTable;
@@ -20,7 +20,7 @@ public class ModelBuildElementTable {
BeanDescriptor<?> targetDescriptor = manyProp.getTargetDescriptor();
MTable table = new MTable(beanTable.getBaseTable());
VisitAllUsing.visitOne(targetDescriptor, new ModelBuildPropertyVisitor(ctx, table, targetDescriptor));
VisitProperties.visit(targetDescriptor, new ModelBuildPropertyVisitor(ctx, table, targetDescriptor));
ctx.addTableElementCollection(table);
}
@@ -7,7 +7,7 @@ import io.ebeaninternal.dbmigration.model.MCompoundForeignKey;
import io.ebeaninternal.dbmigration.model.MCompoundUniqueConstraint;
import io.ebeaninternal.dbmigration.model.MIndex;
import io.ebeaninternal.dbmigration.model.MTable;
import io.ebeaninternal.dbmigration.model.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.server.deploy.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
@@ -2,11 +2,8 @@ package io.ebeaninternal.dbmigration.model.visitor;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.InheritInfoVisitor;
import io.ebeaninternal.server.deploy.visitor.BeanPropertyVisitor;
import io.ebeaninternal.server.deploy.visitor.VisitProperties;
import java.util.List;
@@ -14,25 +11,16 @@ import java.util.List;
* Makes use of BeanVisitor and PropertyVisitor to navigate BeanDescriptors
* and their properties.
*/
public class VisitAllUsing {
public class VisitAllUsing extends VisitProperties {
/**
* Visit a single Descriptor using the given visitor.
*/
public static void visitOne(BeanDescriptor<?> descriptor, BeanPropertyVisitor visitor) {
private final BeanVisitor visitor;
new VisitAllUsing().visitProperties(descriptor, visitor);
}
protected final BeanVisitor visitor;
protected final List<BeanDescriptor<?>> descriptors;
private final List<BeanDescriptor<?>> descriptors;
/**
* Visit all the descriptors for a given server.
*/
public VisitAllUsing(BeanVisitor visitor, SpiEbeanServer server) {
this(visitor, server.getBeanDescriptors());
}
@@ -44,11 +32,6 @@ public class VisitAllUsing {
this.descriptors = descriptors;
}
private VisitAllUsing() {
this.visitor = null;
this.descriptors = null;
}
public void visitAllBeans() {
for (BeanDescriptor<?> desc : descriptors) {
if (desc.isBaseTable()) {
@@ -61,104 +44,10 @@ public class VisitAllUsing {
* Visit the bean using a visitor.
*/
protected void visitBean(BeanDescriptor<?> desc, BeanVisitor visitor) {
BeanPropertyVisitor propertyVisitor = visitor.visitBean(desc);
if (propertyVisitor != null) {
visitProperties(desc, propertyVisitor);
}
}
private void visitProperties(BeanDescriptor<?> desc, BeanPropertyVisitor propertyVisitor) {
BeanProperty idProp = desc.getIdProperty();
if (idProp != null) {
visit(propertyVisitor, idProp);
}
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
if (unidirectional != null) {
visit(propertyVisitor, unidirectional);
}
BeanProperty[] propertiesNonTransient = desc.propertiesNonTransient();
for (BeanProperty p : propertiesNonTransient) {
if (p.isDDLColumn()) {
visit(propertyVisitor, p);
}
}
visitInheritanceProperties(desc, propertyVisitor);
propertyVisitor.visitEnd();
}
/**
* Visit the property.
*/
protected void visit(BeanPropertyVisitor pv, BeanProperty p) {
if (p instanceof BeanPropertyAssocMany<?>) {
// oneToMany or manyToMany
pv.visitMany((BeanPropertyAssocMany<?>) p);
} else if (p instanceof BeanPropertyAssocOne<?>) {
BeanPropertyAssocOne<?> assocOne = (BeanPropertyAssocOne<?>) p;
if (assocOne.isEmbedded()) {
// Embedded bean
pv.visitEmbedded(assocOne);
BeanProperty[] embProps = assocOne.getProperties();
for (BeanProperty embProp : embProps) {
pv.visitEmbeddedScalar(embProp, assocOne);
}
} else if (assocOne.isOneToOneExported()) {
// associated one exported
pv.visitOneExported(assocOne);
} else {
// associated one imported
pv.visitOneImported(assocOne);
}
} else {
// simple scalar type
pv.visitScalar(p);
}
}
/**
* Visit all the other inheritance properties that are not on the root.
*/
protected void visitInheritanceProperties(BeanDescriptor<?> descriptor, BeanPropertyVisitor pv) {
InheritInfo inheritInfo = descriptor.getInheritInfo();
if (inheritInfo != null && inheritInfo.isRoot()) {
// add all properties on the children objects
inheritInfo.visitChildren(new InheritChildVisitor(this, pv));
}
}
/**
* Helper used to visit all the inheritInfo/BeanDescriptor in
* the inheritance hierarchy (to add their 'local' properties).
*/
protected static class InheritChildVisitor implements InheritInfoVisitor {
private final VisitAllUsing owner;
private final BeanPropertyVisitor pv;
protected InheritChildVisitor(VisitAllUsing owner, BeanPropertyVisitor pv) {
this.owner = owner;
this.pv = pv;
}
@Override
public void visit(InheritInfo inheritInfo) {
for (BeanProperty beanProperty : inheritInfo.desc().propertiesLocal()) {
if (beanProperty.isDDLColumn()) {
owner.visit(pv, beanProperty);
}
}
}
}
}
@@ -21,7 +21,7 @@ import io.ebean.config.dbplatform.sqlite.SQLitePlatform;
import io.ebean.config.dbplatform.sqlserver.SqlServer16Platform;
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.dbmigration.DbOffline;
import io.ebeaninternal.api.DbOffline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,7 +13,7 @@ import io.ebean.config.dbplatform.h2.H2Platform;
import io.ebean.service.SpiContainer;
import io.ebeaninternal.api.SpiBackgroundExecutor;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.DbOffline;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.server.cluster.ClusterManager;
import io.ebeaninternal.server.core.bootup.BootupClassPathSearch;
import io.ebeaninternal.server.core.bootup.BootupClasses;
@@ -31,7 +31,7 @@ import io.ebeaninternal.api.SpiLogManager;
import io.ebeaninternal.api.SpiLogger;
import io.ebeaninternal.api.SpiLoggerFactory;
import io.ebeaninternal.api.SpiProfileHandler;
import io.ebeaninternal.dbmigration.DbOffline;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.server.autotune.AutoTuneService;
import io.ebeaninternal.server.autotune.AutoTuneServiceProvider;
import io.ebeaninternal.server.autotune.NoAutoTuneService;
@@ -4,8 +4,8 @@ import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiSqlUpdate;
import io.ebeaninternal.dbmigration.model.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
import io.ebeaninternal.server.deploy.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.server.deploy.visitor.VisitProperties;
import io.ebeaninternal.server.core.DefaultSqlUpdate;
import java.util.List;
@@ -50,7 +50,7 @@ class BeanPropertyAssocManySqlHelp<T> {
append(sb);
Cols cols = new Cols(sb);
VisitAllUsing.visitOne(many.targetDescriptor, cols);
VisitProperties.visit(many.targetDescriptor, cols);
sb.append(") values (");
appendBind(sb, exportedProperties.length, true);
appendBind(sb, cols.colCount, false);
@@ -1,4 +1,4 @@
package io.ebeaninternal.dbmigration.model.visitor;
package io.ebeaninternal.server.deploy.visitor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
@@ -1,4 +1,4 @@
package io.ebeaninternal.dbmigration.model.visitor;
package io.ebeaninternal.server.deploy.visitor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
@@ -0,0 +1,116 @@
package io.ebeaninternal.server.deploy.visitor;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.InheritInfoVisitor;
/**
* Makes use of BeanVisitor and PropertyVisitor to navigate BeanDescriptors
* and their properties.
*/
public class VisitProperties {
/**
* Visit a single Descriptor using the given visitor.
*/
public static void visit(BeanDescriptor<?> descriptor, BeanPropertyVisitor visitor) {
new VisitProperties().visitProperties(descriptor, visitor);
}
protected void visitProperties(BeanDescriptor<?> desc, BeanPropertyVisitor propertyVisitor) {
BeanProperty idProp = desc.getIdProperty();
if (idProp != null) {
visit(propertyVisitor, idProp);
}
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
if (unidirectional != null) {
visit(propertyVisitor, unidirectional);
}
BeanProperty[] propertiesNonTransient = desc.propertiesNonTransient();
for (BeanProperty p : propertiesNonTransient) {
if (p.isDDLColumn()) {
visit(propertyVisitor, p);
}
}
visitInheritanceProperties(desc, propertyVisitor);
propertyVisitor.visitEnd();
}
/**
* Visit the property.
*/
protected void visit(BeanPropertyVisitor pv, BeanProperty p) {
if (p instanceof BeanPropertyAssocMany<?>) {
// oneToMany or manyToMany
pv.visitMany((BeanPropertyAssocMany<?>) p);
} else if (p instanceof BeanPropertyAssocOne<?>) {
BeanPropertyAssocOne<?> assocOne = (BeanPropertyAssocOne<?>) p;
if (assocOne.isEmbedded()) {
// Embedded bean
pv.visitEmbedded(assocOne);
BeanProperty[] embProps = assocOne.getProperties();
for (BeanProperty embProp : embProps) {
pv.visitEmbeddedScalar(embProp, assocOne);
}
} else if (assocOne.isOneToOneExported()) {
// associated one exported
pv.visitOneExported(assocOne);
} else {
// associated one imported
pv.visitOneImported(assocOne);
}
} else {
// simple scalar type
pv.visitScalar(p);
}
}
/**
* Visit all the other inheritance properties that are not on the root.
*/
protected void visitInheritanceProperties(BeanDescriptor<?> descriptor, BeanPropertyVisitor pv) {
InheritInfo inheritInfo = descriptor.getInheritInfo();
if (inheritInfo != null && inheritInfo.isRoot()) {
// add all properties on the children objects
inheritInfo.visitChildren(new InheritChildVisitor(this, pv));
}
}
/**
* Helper used to visit all the inheritInfo/BeanDescriptor in
* the inheritance hierarchy (to add their 'local' properties).
*/
protected static class InheritChildVisitor implements InheritInfoVisitor {
private final VisitProperties owner;
private final BeanPropertyVisitor pv;
protected InheritChildVisitor(VisitProperties owner, BeanPropertyVisitor pv) {
this.owner = owner;
this.pv = pv;
}
@Override
public void visit(InheritInfo inheritInfo) {
for (BeanProperty beanProperty : inheritInfo.desc().propertiesLocal()) {
if (beanProperty.isDDLColumn()) {
owner.visit(pv, beanProperty);
}
}
}
}
}
@@ -18,7 +18,7 @@ import io.ebean.types.Cidr;
import io.ebean.types.Inet;
import io.ebean.util.AnnotationUtil;
import io.ebeaninternal.api.ExtraTypeFactory;
import io.ebeaninternal.dbmigration.DbOffline;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.server.core.bootup.BootupClasses;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import io.ebeanservice.docstore.api.mapping.DocPropertyType;