-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2248 from ebean-orm/feature/DbJson-DirtyDetection…
…-Refactor Refactor DbJson Jackson handling adding DatabaseConfig.setJsonDirtyByDefault()
- Loading branch information
Showing
10 changed files
with
166 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
ebean-core/src/main/java/io/ebeaninternal/server/type/CheckMarkedDirty.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
ebean-core/src/main/java/io/ebeaninternal/server/type/TypeJsonManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.ebeaninternal.server.type; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.ebean.ModifyAwareType; | ||
import io.ebean.config.DatabaseConfig; | ||
import io.ebean.config.dbplatform.DbPlatformType; | ||
|
||
class TypeJsonManager { | ||
|
||
interface DirtyHandler { | ||
boolean isDirty(Object value); | ||
} | ||
|
||
private final boolean postgres; | ||
private final ObjectMapper objectMapper; | ||
private final DirtyHandler defaultHandler; | ||
private final DirtyHandler modifyAwareHandler; | ||
|
||
TypeJsonManager(boolean postgres, Object objectMapper, boolean defaultDirty) { | ||
this.postgres = postgres; | ||
this.objectMapper = (ObjectMapper) objectMapper; | ||
this.defaultHandler = new DefaultHandler(defaultDirty); | ||
this.modifyAwareHandler = new ModifyAwareHandler(); | ||
} | ||
|
||
ObjectMapper objectMapper() { | ||
return objectMapper; | ||
} | ||
|
||
String postgresType(int dbType) { | ||
if (postgres) { | ||
switch (dbType) { | ||
case DbPlatformType.JSON: | ||
return PostgresHelper.JSON_TYPE; | ||
case DbPlatformType.JSONB: | ||
return PostgresHelper.JSONB_TYPE; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Return the DirtyHandler to use. | ||
*/ | ||
DirtyHandler dirtyHandler(Class<?> cls, Class<?> rawType) { | ||
if (!Object.class.equals(cls) || ModifyAwareType.class.isAssignableFrom(rawType)) { | ||
// Set, List and Map are modify aware | ||
return modifyAwareHandler; | ||
} | ||
return defaultHandler; | ||
} | ||
|
||
/** | ||
* Return true if the value should be considered dirty (and included in an update). | ||
*/ | ||
static boolean checkIsDirty(Object value) { | ||
if (value instanceof ModifyAwareType) { | ||
return checkModifyAware(value); | ||
} | ||
return true; | ||
} | ||
|
||
private static boolean checkModifyAware(Object value) { | ||
ModifyAwareType modifyAware = (ModifyAwareType) value; | ||
if (modifyAware.isMarkedDirty()) { | ||
// reset the dirty state (consider not dirty after update) | ||
modifyAware.setMarkedDirty(false); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
static final class ModifyAwareHandler implements DirtyHandler { | ||
@Override | ||
public boolean isDirty(Object value) { | ||
return checkModifyAware(value); | ||
} | ||
} | ||
|
||
/** | ||
* Effectively constant based on {@link DatabaseConfig#isJsonDirtyByDefault()} | ||
*/ | ||
static final class DefaultHandler implements DirtyHandler { | ||
|
||
private final boolean dirty; | ||
|
||
DefaultHandler(boolean dirty) { | ||
this.dirty = dirty; | ||
} | ||
|
||
@Override | ||
public boolean isDirty(Object value) { | ||
return dirty; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.