mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
FIX: mutationDetection = SOURCE works also for special json types
This commit is contained in:
@@ -289,7 +289,7 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
|
||||
@Override
|
||||
public ScalarType<?> getDbMapScalarType() {
|
||||
return (postgres) ? hstoreType : ScalarTypeJsonMap.typeFor(false, Types.VARCHAR);
|
||||
return (postgres) ? hstoreType : ScalarTypeJsonMap.typeFor(false, Types.VARCHAR, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -312,7 +312,7 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
return arrayTypeSetFactory.typeFor(valueType, nullable);
|
||||
}
|
||||
// fallback to JSON storage in VARCHAR column
|
||||
return new ScalarTypeJsonSet.Varchar(getDocType(valueType), nullable);
|
||||
return new ScalarTypeJsonSet.Varchar(getDocType(valueType), nullable, false); // TODO: keepSource for @DbArray?
|
||||
}
|
||||
|
||||
private ScalarType<?> getArrayScalarTypeList(Type valueType, boolean nullable) {
|
||||
@@ -323,7 +323,7 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
return arrayTypeListFactory.typeFor(valueType, nullable);
|
||||
}
|
||||
// fallback to JSON storage in VARCHAR column
|
||||
return new ScalarTypeJsonList.Varchar(getDocType(valueType), nullable);
|
||||
return new ScalarTypeJsonList.Varchar(getDocType(valueType), nullable, false); // TODO: keepSource for @DbArray?
|
||||
}
|
||||
|
||||
private Class<? extends Enum<?>> asEnumClass(Type valueType) {
|
||||
@@ -340,10 +340,11 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
Type genericType = prop.getGenericType();
|
||||
boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop);
|
||||
|
||||
boolean keepSource = prop.getMutationDetection() == MutationDetection.SOURCE;
|
||||
if (type.equals(List.class)) {
|
||||
DocPropertyType docType = getDocType(genericType);
|
||||
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
|
||||
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable());
|
||||
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource);
|
||||
} else {
|
||||
return createJsonObjectMapperType(prop, dbType, docType);
|
||||
}
|
||||
@@ -351,14 +352,14 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
if (type.equals(Set.class)) {
|
||||
DocPropertyType docType = getDocType(genericType);
|
||||
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
|
||||
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable());
|
||||
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource);
|
||||
} else {
|
||||
return createJsonObjectMapperType(prop, dbType, docType);
|
||||
}
|
||||
}
|
||||
if (type.equals(Map.class)) {
|
||||
if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) {
|
||||
return ScalarTypeJsonMap.typeFor(postgres, dbType);
|
||||
return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource);
|
||||
} else {
|
||||
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
|
||||
}
|
||||
|
||||
@@ -24,24 +24,24 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ public class ScalarTypeJsonList {
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ public class ScalarTypeJsonList {
|
||||
* 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 class ScalarTypeJsonList {
|
||||
*/
|
||||
@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() {
|
||||
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 class ScalarTypeJsonList {
|
||||
}
|
||||
|
||||
@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 @@ public class ScalarTypeJsonList {
|
||||
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 @@ public class ScalarTypeJsonList {
|
||||
|
||||
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
|
||||
|
||||
@@ -15,6 +15,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
@@ -26,27 +27,21 @@ import java.util.Map;
|
||||
@SuppressWarnings("rawtypes")
|
||||
public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
|
||||
private static final ScalarTypeJsonMap CLOB = new ScalarTypeJsonMap.Clob();
|
||||
private static final ScalarTypeJsonMap BLOB = new ScalarTypeJsonMap.Blob();
|
||||
private static final ScalarTypeJsonMap VARCHAR = new ScalarTypeJsonMap.Varchar();
|
||||
private static final ScalarTypeJsonMap JSON = new ScalarTypeJsonMapPostgres.JSON();
|
||||
private static final ScalarTypeJsonMap JSONB = new ScalarTypeJsonMapPostgres.JSONB();
|
||||
|
||||
/**
|
||||
* Return the ScalarType for the requested dbType and postgres.
|
||||
*/
|
||||
public static ScalarTypeJsonMap typeFor(boolean postgres, int dbType) {
|
||||
public static ScalarTypeJsonMap typeFor(boolean postgres, int dbType, boolean keepSource) {
|
||||
switch (dbType) {
|
||||
case Types.VARCHAR:
|
||||
return VARCHAR;
|
||||
return new ScalarTypeJsonMap.Varchar(keepSource);
|
||||
case Types.BLOB:
|
||||
return BLOB;
|
||||
return new ScalarTypeJsonMap.Blob(keepSource);
|
||||
case Types.CLOB:
|
||||
return CLOB;
|
||||
return new ScalarTypeJsonMap.Clob(keepSource);
|
||||
case DbPlatformType.JSONB:
|
||||
return postgres ? JSONB : CLOB;
|
||||
return postgres ? new ScalarTypeJsonMapPostgres.JSONB(keepSource) : new ScalarTypeJsonMap.Clob(keepSource);
|
||||
case DbPlatformType.JSON:
|
||||
return postgres ? JSON : CLOB;
|
||||
return postgres ? new ScalarTypeJsonMapPostgres.JSON(keepSource) : new ScalarTypeJsonMap.Clob(keepSource);
|
||||
default:
|
||||
throw new IllegalStateException("Unknown dbType " + dbType);
|
||||
}
|
||||
@@ -54,41 +49,50 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
|
||||
public static class Clob extends ScalarTypeJsonMap {
|
||||
|
||||
public Clob() {
|
||||
super(Types.CLOB);
|
||||
public Clob(boolean keepSource) {
|
||||
super(Types.CLOB, keepSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map read(DataReader reader) throws SQLException {
|
||||
String content = reader.getStringFromStream();
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
return parse(content);
|
||||
protected String readJson(DataReader reader) throws SQLException {
|
||||
return reader.getStringFromStream();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Varchar extends ScalarTypeJsonMap {
|
||||
|
||||
public Varchar() {
|
||||
super(Types.VARCHAR);
|
||||
public Varchar(boolean keepSource) {
|
||||
super(Types.VARCHAR, keepSource);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Blob extends ScalarTypeJsonMap {
|
||||
public Blob() {
|
||||
super(Types.BLOB);
|
||||
public Blob(boolean keepSource) {
|
||||
super(Types.BLOB, keepSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map read(DataReader reader) throws SQLException {
|
||||
InputStream is = reader.getBinaryStream();
|
||||
if (is == null) {
|
||||
if (isJsonMapper()) {
|
||||
reader.pushJson(null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
try (InputStreamReader inputStreamReader = new InputStreamReader(is)) {
|
||||
return parse(inputStreamReader);
|
||||
if (isJsonMapper()) {
|
||||
StringWriter rawJson = new StringWriter();
|
||||
try (InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
|
||||
inputStreamReader.transferTo(rawJson);
|
||||
}
|
||||
reader.pushJson(rawJson.toString());
|
||||
return parse(rawJson.toString());
|
||||
|
||||
} else {
|
||||
try (InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
|
||||
return parse(inputStreamReader);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error reading Blob stream from DB", e);
|
||||
@@ -96,18 +100,22 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, Map value) throws SQLException {
|
||||
if (value == null) {
|
||||
binder.setNull(Types.BLOB);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
binder.setBytes(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
protected void bindNull(DataBinder binder) throws SQLException {
|
||||
binder.setNull(Types.BLOB);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void bindJson(DataBinder binder, String rawJson) throws SQLException {
|
||||
binder.setBytes(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ScalarTypeJsonMap(int jdbcType) {
|
||||
private final boolean keepSource;
|
||||
|
||||
public ScalarTypeJsonMap(int jdbcType, boolean keepSource) {
|
||||
super(Map.class, false, jdbcType);
|
||||
this.keepSource = keepSource;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,26 +133,56 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
public boolean isDirty(Object value) {
|
||||
return TypeJsonManager.checkIsDirty(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isJsonMapper() {
|
||||
return keepSource;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map read(DataReader reader) throws SQLException {
|
||||
String rawJson = reader.getString();
|
||||
String rawJson = readJson(reader);
|
||||
if (isJsonMapper()) {
|
||||
reader.pushJson(rawJson);
|
||||
}
|
||||
|
||||
if (rawJson == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parse(rawJson);
|
||||
}
|
||||
|
||||
|
||||
protected String readJson(DataReader reader) throws SQLException {
|
||||
return reader.getString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, Map value) throws SQLException {
|
||||
public final void bind(DataBinder binder, Map value) throws SQLException {
|
||||
String rawJson = isJsonMapper() ? binder.popJson() : null;
|
||||
if (rawJson == null && value != null) {
|
||||
rawJson = formatValue(value);
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
binder.setNull(Types.VARCHAR);
|
||||
bindNull(binder);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
binder.setString(rawJson);
|
||||
bindJson(binder, rawJson);
|
||||
}
|
||||
}
|
||||
|
||||
protected void bindNull(DataBinder binder) throws SQLException {
|
||||
binder.setNull(Types.VARCHAR);
|
||||
}
|
||||
|
||||
protected void bindJson(DataBinder binder, String rawJson) throws SQLException {
|
||||
binder.setString(rawJson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return value;
|
||||
|
||||
+12
-10
@@ -4,7 +4,6 @@ import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Support for the Postgres DB types JSON and JSONB.
|
||||
@@ -13,15 +12,18 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
|
||||
|
||||
final String postgresType;
|
||||
|
||||
ScalarTypeJsonMapPostgres(int jdbcType, String postgresType) {
|
||||
super(jdbcType);
|
||||
ScalarTypeJsonMapPostgres(int jdbcType, String postgresType, boolean keepSource) {
|
||||
super(jdbcType, keepSource);
|
||||
this.postgresType = postgresType;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void bind(DataBinder binder, Map value) throws SQLException {
|
||||
String rawJson = (value == null) ? null : formatValue(value);
|
||||
protected void bindNull(DataBinder binder) throws SQLException {
|
||||
binder.setObject(PostgresHelper.asObject(postgresType, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void bindJson(DataBinder binder, String rawJson) throws SQLException {
|
||||
binder.setObject(PostgresHelper.asObject(postgresType, rawJson));
|
||||
}
|
||||
|
||||
@@ -30,8 +32,8 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
|
||||
*/
|
||||
public static class JSON extends ScalarTypeJsonMapPostgres {
|
||||
|
||||
public JSON() {
|
||||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE);
|
||||
public JSON(boolean keepSource) {
|
||||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, keepSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +42,8 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
|
||||
*/
|
||||
public static class JSONB extends ScalarTypeJsonMapPostgres {
|
||||
|
||||
public JSONB() {
|
||||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE);
|
||||
public JSONB(boolean keepSource) {
|
||||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, keepSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,24 +26,24 @@ public class ScalarTypeJsonSet {
|
||||
/**
|
||||
* Return the appropriate ScalarType for the requested dbType and Postgres.
|
||||
*/
|
||||
public static ScalarType<?> typeFor(boolean postgres, int dbType, DocPropertyType docPropertyType, boolean nullable) {
|
||||
public static ScalarType<?> typeFor(boolean postgres, int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
if (postgres) {
|
||||
switch (dbType) {
|
||||
case DbPlatformType.JSONB:
|
||||
return new ScalarTypeJsonSet.JsonB(docPropertyType, nullable);
|
||||
return new ScalarTypeJsonSet.JsonB(docPropertyType, nullable, keepSource);
|
||||
case DbPlatformType.JSON:
|
||||
return new ScalarTypeJsonSet.Json(docPropertyType, nullable);
|
||||
return new ScalarTypeJsonSet.Json(docPropertyType, nullable, keepSource);
|
||||
}
|
||||
}
|
||||
return new ScalarTypeJsonSet.Varchar(docPropertyType, nullable);
|
||||
return new ScalarTypeJsonSet.Varchar(docPropertyType, nullable, keepSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* List mapped to DB VARCHAR.
|
||||
*/
|
||||
public static class Varchar extends ScalarTypeJsonSet.Base {
|
||||
public Varchar(DocPropertyType docPropertyType, boolean nullable) {
|
||||
super(Types.VARCHAR, docPropertyType, nullable);
|
||||
public Varchar(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
super(Types.VARCHAR, docPropertyType, nullable, keepSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ public class ScalarTypeJsonSet {
|
||||
* List mapped to Postgres JSON.
|
||||
*/
|
||||
private static class Json extends ScalarTypeJsonSet.PgBase {
|
||||
public Json(DocPropertyType docPropertyType, boolean nullable) {
|
||||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docPropertyType, nullable);
|
||||
public Json(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docPropertyType, nullable, keepSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +60,8 @@ public class ScalarTypeJsonSet {
|
||||
* List mapped to Postgres JSONB.
|
||||
*/
|
||||
private static class JsonB extends ScalarTypeJsonSet.PgBase {
|
||||
public JsonB(DocPropertyType docPropertyType, boolean nullable) {
|
||||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docPropertyType, nullable);
|
||||
public JsonB(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docPropertyType, nullable, keepSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,13 +71,25 @@ public class ScalarTypeJsonSet {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private abstract static class Base extends ScalarTypeJsonCollection<Set> {
|
||||
|
||||
public Base(int dbType, DocPropertyType docPropertyType, boolean nullable) {
|
||||
private boolean keepSource;
|
||||
|
||||
public Base(int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
super(Set.class, dbType, docPropertyType, nullable);
|
||||
this.keepSource = keepSource;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isJsonMapper() {
|
||||
return keepSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set read(DataReader reader) throws SQLException {
|
||||
String json = reader.getString();
|
||||
if (isJsonMapper()) {
|
||||
reader.pushJson(json);
|
||||
}
|
||||
try {
|
||||
// parse JSON into modifyAware list
|
||||
return EJson.parseSet(json, true);
|
||||
@@ -87,17 +99,15 @@ public class ScalarTypeJsonSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, Set value) throws SQLException {
|
||||
public final void bind(DataBinder binder, Set 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 Set into JSON content", e);
|
||||
}
|
||||
bindRawJson(binder, rawJson);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,8 +120,15 @@ public class ScalarTypeJsonSet {
|
||||
}
|
||||
}
|
||||
|
||||
protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException {
|
||||
binder.setString(rawJson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(Set value) {
|
||||
if (value.isEmpty()) {
|
||||
return "[]";
|
||||
}
|
||||
try {
|
||||
return EJson.write(value);
|
||||
} catch (IOException e) {
|
||||
@@ -151,19 +168,14 @@ public class ScalarTypeJsonSet {
|
||||
|
||||
final String pgType;
|
||||
|
||||
PgBase(int jdbcType, String pgType, DocPropertyType docPropertyType, boolean nullable) {
|
||||
super(jdbcType, docPropertyType, nullable);
|
||||
PgBase(int jdbcType, String pgType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) {
|
||||
super(jdbcType, docPropertyType, nullable, keepSource);
|
||||
this.pgType = pgType;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void bind(DataBinder binder, Set 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
|
||||
|
||||
Reference in New Issue
Block a user