-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 2 commits
7bc206f
1816ef7
72673ff
64efeee
a45f2dc
94cdc01
6307321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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"); | ||
|
||
// | ||
|
@@ -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")) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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+)\\??(.*)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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+)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()"); | ||
} | ||
|
@@ -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)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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+\\))"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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)); | ||
} | ||
|
There was a problem hiding this comment.
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
forXSD_NAME_PATTERN
. What do you think about it?