mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#3341 String length validation with Postgres PGobject (JSON/JSONB)
This commit is contained in:
@@ -37,23 +37,29 @@ public interface BindMaxLength {
|
||||
public long length(int dbLength, Object obj) {
|
||||
if (obj instanceof String) {
|
||||
String s = (String) obj;
|
||||
int stringLength = s.length();
|
||||
if (stringLength > dbLength) {
|
||||
return stringLength;
|
||||
} else if (stringLength * 4 <= dbLength) {
|
||||
return -1;
|
||||
} else {
|
||||
return s.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
|
||||
return utf8String(dbLength, s);
|
||||
} else if (obj instanceof byte[]) {
|
||||
return ((byte[]) obj).length;
|
||||
} else if (obj instanceof InputStreamInfo) {
|
||||
return ((InputStreamInfo) obj).length();
|
||||
} else if ("org.postgresql.util.PGobject".equals(obj.getClass().getCanonicalName())) {
|
||||
String value = ((org.postgresql.util.PGobject) obj).getValue();
|
||||
return value == null ? -1 : utf8String(dbLength, value);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static int utf8String(int dbLength, String s) {
|
||||
int stringLength = s.length();
|
||||
if (stringLength > dbLength) {
|
||||
return stringLength;
|
||||
} else if (stringLength * 4 <= dbLength) {
|
||||
return -1;
|
||||
} else {
|
||||
return s.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,6 +75,9 @@ public interface BindMaxLength {
|
||||
return ((byte[]) obj).length;
|
||||
} else if (obj instanceof InputStreamInfo) {
|
||||
return ((InputStreamInfo) obj).length();
|
||||
} else if ("org.postgresql.util.PGobject".equals(obj.getClass().getCanonicalName())) {
|
||||
String value = ((org.postgresql.util.PGobject) obj).getValue();
|
||||
return value == null ? -1 : value.length();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user