[#7179] PostgresUtils.toPGXYZString() methods (and others) should avoid using String.replace() pre JDK 9

This commit is contained in:
lukaseder 2018-02-16 09:57:00 +01:00
parent 57fed5a7ad
commit da08a01fb7
5 changed files with 32 additions and 15 deletions

View File

@ -344,6 +344,7 @@ import org.jooq.conf.RenderNameStyle;
import org.jooq.conf.Settings;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.tools.Convert;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jooq.types.DayToSecond;
import org.jooq.types.UByte;
@ -12921,7 +12922,11 @@ public class DSL {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static String escape(String value, char escape) {
String esc = "" + escape;
return value.replace(esc, esc + esc).replace("%", esc + "%").replace("_", esc + "_");
return StringUtils.replace(
StringUtils.replace(
StringUtils.replace(value, esc, esc + esc), "%", esc + "%"
), "_", esc + "_"
);
}
/**

View File

@ -2467,7 +2467,7 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
String format = formatISO(val);
// Replace the ISO standard Z character for UTC, as some databases don't like that
return (format.substring(0, 10) + ' ' + format.substring(11)).replace("Z", "+00:00");
return StringUtils.replace(format.substring(0, 10) + ' ' + format.substring(11), "Z", "+00:00");
}
private static final String formatISO(OffsetDateTime val) {
@ -2545,7 +2545,7 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
private static final String format(OffsetTime val) {
// Replace the ISO standard Z character for UTC, as some databases don't like that
return val.format(DateTimeFormatter.ISO_OFFSET_TIME).replace("Z", "+00:00");
return StringUtils.replace(val.format(DateTimeFormatter.ISO_OFFSET_TIME), "Z", "+00:00");
}
}

View File

@ -554,10 +554,14 @@ final class ResultImpl<R extends Record> implements Result<R> {
else
writer.append(' ');
String value = format0(getValue(i, index), get(i).changed(index), true)
.replace("\n", "{lf}")
.replace("\r", "{cr}")
.replace("\t", "{tab}");
String value =
StringUtils.replace(
StringUtils.replace(
StringUtils.replace(
format0(getValue(i, index), get(i).changed(index), true), "\n", "{lf}"
), "\r", "{cr}"
), "\t", "{tab}"
);
String padded;
if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
@ -875,10 +879,11 @@ final class ResultImpl<R extends Record> implements Result<R> {
// no break
case ALWAYS:
default:
return format.quoteString()
+ result.replace("\\", "\\\\")
.replace(format.quoteString(), format.quoteString() + format.quoteString())
+ format.quoteString();
return StringUtils.replace(
StringUtils.replace(
format.quoteString(), "\\", "\\\\"
), format.quoteString(), format.quoteString() + format.quoteString()
) + format.quoteString();
}
}

View File

@ -42,6 +42,8 @@ import java.util.regex.Pattern;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.jooq.tools.StringUtils;
/**
* An {@link XmlAdapter} that implements useful features after parsing XML
* strings with JAXB.
@ -67,8 +69,12 @@ public class StringAdapter extends XmlAdapter<String, String> {
String result = v.trim();
Matcher matcher = PROPERTY_PATTERN.matcher(result);
while (matcher.find())
result = result.replace(matcher.group(0), System.getProperty(matcher.group(1), matcher.group(0)));
while (matcher.find()) {
String group0 = matcher.group(0);
String group1 = matcher.group(1);
result = StringUtils.replace(result, group0, System.getProperty(group1, group0));
}
return result;
}

View File

@ -53,6 +53,7 @@ import java.util.List;
import org.jooq.Converter;
import org.jooq.Record;
import org.jooq.exception.DataTypeException;
import org.jooq.tools.StringUtils;
import org.jooq.tools.reflect.Reflect;
import org.jooq.types.DayToSecond;
import org.jooq.types.YearToMonth;
@ -445,7 +446,7 @@ public class PostgresUtils {
sb.append(toPGString((byte[]) o));
else
sb.append("\"")
.append(toPGString(o).replace("\\", "\\\\").replace("\"", "\\\""))
.append(StringUtils.replace(StringUtils.replace(toPGString(o), "\\", "\\\\"), "\"", "\\\""))
.append("\"");
separator = ",";
@ -492,7 +493,7 @@ public class PostgresUtils {
sb.append(toPGString((byte[]) a));
else
sb.append("\"")
.append(toPGString(a).replace("\\", "\\\\").replace("\"", "\\\""))
.append(StringUtils.replace(StringUtils.replace(toPGString(a), "\\", "\\\\"), "\"", "\\\""))
.append("\"");
}