Skip to content

Commit

Permalink
Merge pull request #3346 from ebean-orm/feature/lengthCheck-postgres
Browse files Browse the repository at this point in the history
#3341 String length validation with Postgres PGobject (JSON/JSONB)
  • Loading branch information
rbygrave authored Mar 1, 2024
2 parents 6babb2e + 9e461e4 commit 9ce0e92
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,33 @@ final class UTF8 implements BindMaxLength {

@Override
public long length(int dbLength, Object obj) {
if (obj instanceof String) {
if (obj == null) {
return -1;
} else 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;
}
}
}

/**
Expand All @@ -63,12 +71,17 @@ final class Standard implements BindMaxLength {

@Override
public long length(int dbLength, Object obj) {
if (obj instanceof String) {
if (obj == null) {
return -1;
} else if (obj instanceof String) {
return ((String) obj).length();
} 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 : value.length();
} else {
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions ebean-test/src/test/resources/ebean.properties
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ datasource.oracle.url=jdbc:oracle:thin:@localhost:1521:XE
## set this to use timestamp rather than timestamptz
#ebean.postgres.timestamp=timestamp

ebean.pg.lengthCheck=on
datasource.pg.username=unit
datasource.pg.password=test
datasource.pg.url=jdbc:postgresql://127.0.0.1:6432/unit
Expand Down
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-cockroach.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=cockroach
ebean.test.dbName=unit
datasource.default=cockroach
#ebean.test.cockroach.version=v21.2.6

ebean.lengthCheck=on
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-postgres.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=postgres
datasource.default=postgres
ebean.test.dbName=unit
ebean.test.postgres.version=15

ebean.lengthCheck=on
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-yugabyte.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=yugabyte
datasource.default=yugabyte
ebean.test.dbName=unit
ebean.test.yugabyte.version=2.18.0.0-b65

ebean.lengthCheck=on

0 comments on commit 9ce0e92

Please sign in to comment.