Skip to content

Commit

Permalink
cleanup Action enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed May 2, 2023
1 parent 2538c96 commit 18ddbe1
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions hibernate-core/src/main/java/org/hibernate/tool/schema/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum Action {
* <p>
* Valid in JPA; compatible with Hibernate's HBM2DDL action of the same name.
*/
NONE( "none" ),
NONE,
/**
* Create the schema.
* <p>
Expand All @@ -36,22 +36,22 @@ public enum Action {
*
* @see org.hibernate.tool.schema.spi.SchemaCreator
*/
CREATE_ONLY( "create", "create-only" ),
CREATE_ONLY,
/**
* Drop the schema.
* <p>
* Valid in JPA; compatible with Hibernate's HBM2DDL action of the same name.
*
* @see org.hibernate.tool.schema.spi.SchemaDropper
*/
DROP( "drop" ),
DROP,
/**
* Drop and then recreate the schema.
*
* @see org.hibernate.tool.schema.spi.SchemaDropper
* @see org.hibernate.tool.schema.spi.SchemaCreator
*/
CREATE( "drop-and-create", "create" ),
CREATE,
/**
* Drop the schema and then recreate it on {@code SessionFactory} startup.
* Additionally, drop the schema on {@code SessionFactory} shutdown.
Expand All @@ -70,23 +70,23 @@ public enum Action {
* @see org.hibernate.tool.schema.spi.SchemaDropper
* @see org.hibernate.tool.schema.spi.SchemaCreator
*/
CREATE_DROP( null, "create-drop" ),
CREATE_DROP,
/**
* Validate the database schema.
* <p>
* This action is not defined by JPA.
*
* @see org.hibernate.tool.schema.spi.SchemaValidator
*/
VALIDATE( null, "validate" ),
VALIDATE,
/**
* Update (alter) the database schema.
* <p>
* This action is not defined by JPA.
*
* @see org.hibernate.tool.schema.spi.SchemaMigrator
*/
UPDATE( null, "update" ),
UPDATE,
/**
* Truncate the tables in the schema.
* <p>
Expand All @@ -96,31 +96,47 @@ public enum Action {
*
* @since 6.2
*/
TRUNCATE( null, null);

private final String externalJpaName;
private final String externalHbm2ddlName;

Action(String externalJpaName) {
this( externalJpaName, externalJpaName );
}

Action(String externalJpaName, String externalHbm2ddlName) {
this.externalJpaName = externalJpaName;
this.externalHbm2ddlName = externalHbm2ddlName;
}
TRUNCATE;

public String getExternalJpaName() {
return externalJpaName;
switch (this) {
case NONE:
return "none";
case CREATE_ONLY:
return "create";
case DROP:
return "drop";
case CREATE_DROP:
return "drop-and-create";
default:
return null;
}
}

public String getExternalHbm2ddlName() {
return externalHbm2ddlName;
switch (this) {
case NONE:
return "none";
case CREATE_ONLY:
return "create-only";
case DROP:
return "drop";
case CREATE:
return "create";
case CREATE_DROP:
return "create-drop";
case VALIDATE:
return "validate";
case UPDATE:
return "update";
default:
return null;
}
}

@Override
public String toString() {
return getClass().getSimpleName() + "(externalJpaName=" + externalJpaName + ", externalHbm2ddlName=" + externalHbm2ddlName + ")";
return getClass().getSimpleName() + "(externalJpaName=" + getExternalJpaName() + ", externalHbm2ddlName=" + getExternalHbm2ddlName() + ")";
}

/**
Expand All @@ -144,29 +160,29 @@ public static Action interpretJpaSetting(Object value) {
}

final String name = value.toString().trim();
if ( name.isEmpty() || NONE.externalJpaName.equals( name ) ) {
if ( name.isEmpty() || NONE.getExternalJpaName().equals( name ) ) {
// default is NONE
return NONE;
}

// prefer JPA external names
for ( Action action : values() ) {
if ( action.externalJpaName == null ) {
if ( action.getExternalJpaName() == null ) {
continue;
}

if ( action.externalJpaName.equals( name ) ) {
if ( action.getExternalJpaName().equals( name ) ) {
return action;
}
}

// then check hbm2ddl names
for ( Action action : values() ) {
if ( action.externalHbm2ddlName == null ) {
if ( action.getExternalHbm2ddlName() == null ) {
continue;
}

if ( action.externalHbm2ddlName.equals( name ) ) {
if ( action.getExternalHbm2ddlName().equals( name ) ) {
return action;
}
}
Expand Down Expand Up @@ -200,29 +216,29 @@ public static Action interpretHbm2ddlSetting(Object value) {
}

final String name = value.toString().trim();
if ( name.isEmpty() || NONE.externalJpaName.equals( name ) ) {
if ( name.isEmpty() || NONE.getExternalJpaName().equals( name ) ) {
// default is NONE
return NONE;
}

// prefer hbm2ddl names
for ( Action action : values() ) {
if ( action.externalHbm2ddlName == null ) {
if ( action.getExternalHbm2ddlName() == null ) {
continue;
}

if ( action.externalHbm2ddlName.equals( name ) ) {
if ( action.getExternalHbm2ddlName().equals( name ) ) {
return hbm2ddlSetting( action );
}
}

// then check JPA external names
for ( Action action : values() ) {
if ( action.externalJpaName == null ) {
if ( action.getExternalJpaName() == null ) {
continue;
}

if ( action.externalJpaName.equals( name ) ) {
if ( action.getExternalJpaName().equals( name ) ) {
return hbm2ddlSetting( action );
}
}
Expand Down

0 comments on commit 18ddbe1

Please sign in to comment.