-
-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX: mutationDetection = SOURCE works also for special json types #2309
Merged
rbygrave
merged 2 commits into
ebean-orm:master
from
FOCONIS:mutationdetection-source-special-json-types
Aug 15, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -24,42 +24,42 @@ public class ScalarTypeJsonList { | |
/** | ||
* Return the appropriate ScalarType based requested dbType and if Postgres. | ||
*/ | ||
public static ScalarType<?> typeFor(boolean postgres, int dbType, DocPropertyType docType, boolean nullable) { | ||
public static ScalarType<?> typeFor(boolean postgres, int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
if (postgres) { | ||
switch (dbType) { | ||
case DbPlatformType.JSONB: | ||
return new ScalarTypeJsonList.JsonB(docType, nullable); | ||
return new ScalarTypeJsonList.JsonB(docType, nullable, keepSource); | ||
case DbPlatformType.JSON: | ||
return new ScalarTypeJsonList.Json(docType, nullable); | ||
return new ScalarTypeJsonList.Json(docType, nullable, keepSource); | ||
} | ||
} | ||
return new ScalarTypeJsonList.Varchar(docType, nullable); | ||
return new ScalarTypeJsonList.Varchar(docType, nullable, keepSource); | ||
} | ||
|
||
/** | ||
* List mapped to DB VARCHAR. | ||
*/ | ||
public static class Varchar extends ScalarTypeJsonList.Base { | ||
public Varchar(DocPropertyType docType, boolean nullable) { | ||
super(Types.VARCHAR, docType, nullable); | ||
public Varchar(DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
super(Types.VARCHAR, docType, nullable, keepSource); | ||
} | ||
} | ||
|
||
/** | ||
* List mapped to Postgres JSON. | ||
*/ | ||
private static class Json extends ScalarTypeJsonList.PgBase { | ||
public Json(DocPropertyType docType, boolean nullable) { | ||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docType, nullable); | ||
public Json(DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docType, nullable, keepSource); | ||
} | ||
} | ||
|
||
/** | ||
* List mapped to Postgres JSONB. | ||
*/ | ||
private static class JsonB extends ScalarTypeJsonList.PgBase { | ||
public JsonB(DocPropertyType docType, boolean nullable) { | ||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docType, nullable); | ||
public JsonB(DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docType, nullable, keepSource); | ||
} | ||
} | ||
|
||
|
@@ -68,14 +68,24 @@ public JsonB(DocPropertyType docType, boolean nullable) { | |
*/ | ||
@SuppressWarnings("rawtypes") | ||
private abstract static class Base extends ScalarTypeJsonCollection<List> { | ||
private final boolean keepSource; | ||
|
||
public Base(int dbType, DocPropertyType docType, boolean nullable) { | ||
public Base(int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
super(List.class, dbType, docType, nullable); | ||
this.keepSource = keepSource; | ||
} | ||
|
||
@Override | ||
public boolean isJsonMapper() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkme: Should we refactor that getter to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review this ... |
||
return keepSource; | ||
} | ||
|
||
@Override | ||
public List read(DataReader reader) throws SQLException { | ||
String json = reader.getString(); | ||
if (isJsonMapper()) { | ||
reader.pushJson(json); | ||
} | ||
try { | ||
// parse JSON into modifyAware list | ||
return EJson.parseList(json, true); | ||
|
@@ -85,17 +95,15 @@ public List read(DataReader reader) throws SQLException { | |
} | ||
|
||
@Override | ||
public void bind(DataBinder binder, List value) throws SQLException { | ||
public final void bind(DataBinder binder, List value) throws SQLException { | ||
String rawJson = isJsonMapper() ? binder.popJson() : null; | ||
if (rawJson == null && value != null) { | ||
rawJson = formatValue(value); | ||
} | ||
if (value == null) { | ||
bindNull(binder); | ||
} else if (value.isEmpty()) { | ||
binder.setString("[]"); | ||
} else { | ||
try { | ||
binder.setString(EJson.write(value)); | ||
} catch (IOException e) { | ||
throw new SQLException("Failed to format List into JSON content", e); | ||
} | ||
bindRawJson(binder, rawJson); | ||
} | ||
} | ||
|
||
|
@@ -107,9 +115,16 @@ protected void bindNull(DataBinder binder) throws SQLException { | |
binder.setString("[]"); | ||
} | ||
} | ||
|
||
protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException { | ||
binder.setString(rawJson); | ||
} | ||
|
||
@Override | ||
public String formatValue(List value) { | ||
if (value.isEmpty()) { | ||
return "[]"; | ||
} | ||
try { | ||
return EJson.write(value); | ||
} catch (IOException e) { | ||
|
@@ -144,19 +159,14 @@ private static class PgBase extends ScalarTypeJsonList.Base { | |
|
||
final String pgType; | ||
|
||
PgBase(int jdbcType, String pgType, DocPropertyType docType, boolean nullable) { | ||
super(jdbcType, docType, nullable); | ||
PgBase(int jdbcType, String pgType, DocPropertyType docType, boolean nullable, boolean keepSource) { | ||
super(jdbcType, docType, nullable, keepSource); | ||
this.pgType = pgType; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public void bind(DataBinder binder, List value) throws SQLException { | ||
if (value == null) { | ||
bindNull(binder); | ||
} else { | ||
binder.setObject(PostgresHelper.asObject(pgType, formatValue(value))); | ||
} | ||
protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException { | ||
binder.setObject(PostgresHelper.asObject(pgType, rawJson)); | ||
} | ||
|
||
@Override | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just remove this altogether ?