Skip to content
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

Make Pattern instance variables and avoid re calculating each time. #3656

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
import java.util.regex.Pattern;

public class ChangelogRewriter {

public static final String XSD_PATTERN_STRING = "([dbchangelog|liquibase-pro])-3.[0-9]?[0-9]?.xsd";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking maybe can append a REGX/REGEX at the end of these regex constants. Also, I would lightly change the name of some of the patterns changing XSD_PATTERN for XSD_NAME_PATTERN. What do you think about it?

public static final Pattern XSD_PATTERN = Pattern.compile(XSD_PATTERN_STRING);
private static final String PATTERN_STRING = "(?ms).*<databaseChangeLog[^>]*>";
private static final Pattern PATTERN = Pattern.compile(PATTERN_STRING);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe changing this to something a bit more representative. Do you agree?


/**
*
* Remove the changelog ID from the changelog file
Expand Down Expand Up @@ -117,17 +123,13 @@ public static ChangeLogRewriterResult addChangeLogId(String changeLogFile, Strin
String encoding = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();
String changeLogString = StreamUtil.readStreamAsString(is, encoding);
if (changeLogFile.toLowerCase().endsWith(".xml")) {
String patternString = "(?ms).*<databaseChangeLog[^>]*>";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(changeLogString);
Matcher matcher = PATTERN.matcher(changeLogString);
if (matcher.find()) {
//
// Update the XSD versions
//
String header = changeLogString.substring(matcher.start(), matcher.end() - 1);
String xsdPatternString = "([dbchangelog|liquibase-pro])-3.[0-9]?[0-9]?.xsd";
Pattern xsdPattern = Pattern.compile(xsdPatternString);
Matcher xsdMatcher = xsdPattern.matcher(header);
Matcher xsdMatcher = XSD_PATTERN.matcher(header);
String editedString = xsdMatcher.replaceAll("$1-" + XMLChangeLogSAXParser.getSchemaVersion() + ".xsd");

//
Expand All @@ -138,7 +140,7 @@ public static ChangeLogRewriterResult addChangeLogId(String changeLogFile, Strin
changeLogString = changeLogString.replaceFirst("/>", outputChangeLogString + "/>");
} else {
String outputHeader = editedString + outputChangeLogString + ">";
changeLogString = changeLogString.replaceFirst(patternString, outputHeader);
changeLogString = changeLogString.replaceFirst(PATTERN_STRING, outputHeader);
}
}
} else if (changeLogFile.toLowerCase().endsWith(".sql")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ public class OfflineConnection implements DatabaseConnection {
private boolean sendsStringParametersAsUnicode = true;
private String connectionUserName;

private static final Pattern PATTERN = Pattern.compile("offline:(\\w+)\\??(.*)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, regarding naming. Also, I would follow the same convention of extracting the regex as a separate constant.


public OfflineConnection() {}

public OfflineConnection(String url, ResourceAccessor resourceAccessor) {
this.url = url;
Matcher matcher = Pattern.compile("offline:(\\w+)\\??(.*)").matcher(url);
Matcher matcher = PATTERN.matcher(url);
if (!matcher.matches()) {
throw new UnexpectedLiquibaseException("Could not parse offline url " + url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class CockroachDatabase extends PostgresDatabase {
private Integer databaseMajorVersion;
private Integer databaseMinorVersion;

private static final Pattern VERSION_PATTERN = Pattern.compile("v(\\d+)\\.(\\d+)\\.(\\d+)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I would extract the regex as a separate constant.


public CockroachDatabase() {
super.setCurrentDateTimeFunction("NOW()");
}
Expand Down Expand Up @@ -107,7 +109,7 @@ public void setConnection(DatabaseConnection conn) {
String version = Scope.getCurrentScope().getSingleton(ExecutorService.class).
getExecutor("jdbc", this).queryForObject(new RawSqlStatement("SELECT version()"), String.class);

final Matcher versionMatcher = Pattern.compile("v(\\d+)\\.(\\d+)\\.(\\d+)").matcher(version);
final Matcher versionMatcher = VERSION_PATTERN.matcher(version);
if (versionMatcher.find()) {
this.databaseMajorVersion = Integer.parseInt(versionMatcher.group(1));
this.databaseMinorVersion = Integer.parseInt(versionMatcher.group(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

public class H2Database extends AbstractJdbcDatabase {

private static final Pattern PATCH_VERSION_PATTERN = Pattern.compile("^(?:\\d+\\.)(?:\\d+\\.)(\\d+).*$");
private static String START_CONCAT = "CONCAT(";
private static String END_CONCAT = ")";
private static String SEP_CONCAT = ", ";
Expand Down Expand Up @@ -384,8 +385,7 @@ && getBuildVersion() >= BUILD_VERSION_FOR_MINMAX_IN_SEQUENCES) {

private int getBuildVersion() throws DatabaseException {

Pattern patchVersionPattern = Pattern.compile("^(?:\\d+\\.)(?:\\d+\\.)(\\d+).*$");
Matcher matcher = patchVersionPattern.matcher(getDatabaseProductVersion());
Matcher matcher = PATCH_VERSION_PATTERN.matcher(getDatabaseProductVersion());

if (matcher.matches()) {
return Integer.parseInt(matcher.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
public class OracleDatabase extends AbstractJdbcDatabase {
public static final Pattern PROXY_USER = Pattern.compile(".*(?:thin|oci)\\:(.+)/@.*");

private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\..*");

public static final String PRODUCT_NAME = "oracle";
private static ResourceBundle coreBundle = getBundle("liquibase/i18n/liquibase-core");
protected final int SHORT_IDENTIFIERS_LENGTH = 30;
Expand Down Expand Up @@ -156,7 +158,7 @@ public void setConnection(DatabaseConnection conn) {

String compatibleVersion = statement.getString(2);
if (compatibleVersion != null) {
Matcher majorVersionMatcher = Pattern.compile("(\\d+)\\.(\\d+)\\..*").matcher(compatibleVersion);
Matcher majorVersionMatcher = VERSION_PATTERN.matcher(compatibleVersion);
if (majorVersionMatcher.matches()) {
this.databaseMajorVersion = Integer.valueOf(majorVersionMatcher.group(1));
this.databaseMinorVersion = Integer.valueOf(majorVersionMatcher.group(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class TimeType extends LiquibaseDataType {

protected static final int MSSQL_TYPE_TIME_DEFAULT_PRECISION = 7;
public static final Pattern PATTERN = Pattern.compile("(\\(\\d+\\))");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, regarding naming and regex as a separate constant.


@Override
public DatabaseDataType toDatabaseDataType(Database database) {
Expand Down Expand Up @@ -63,7 +64,7 @@ public DatabaseDataType toDatabaseDataType(Database database) {

final Object[] parameters = getParameters();
if (parameters == null || parameters.length == 0) {
final Matcher precisionMatcher = Pattern.compile("(\\(\\d+\\))").matcher(rawDefinition);
final Matcher precisionMatcher = PATTERN.matcher(rawDefinition);
if (precisionMatcher.find()) {
datatype = new DatabaseDataType(datatype.getType() + precisionMatcher.group(1));
}
Expand Down