-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1871 from ClickHouse/clientv2_fix_column_to_gette…
…r_matching added matching strategy option
- Loading branch information
Showing
4 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...t-v2/src/main/java/com/clickhouse/client/api/metadata/ColumnToMethodMatchingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.clickhouse.client.api.metadata; | ||
|
||
|
||
/** | ||
* Strategy to match column names to method names. | ||
*/ | ||
public interface ColumnToMethodMatchingStrategy { | ||
|
||
/** | ||
* Normalizes method name to match column name. | ||
* @param methodName original method name | ||
* @return normalized method name | ||
*/ | ||
String normalizeMethodName(String methodName); | ||
|
||
/** | ||
* Checks if the method is a setter. | ||
* @param methodName original (not normalized) method name | ||
* @return true if the method is a setter | ||
*/ | ||
boolean isSetter(String methodName); | ||
|
||
/** | ||
* Checks if the method is a getter. | ||
* @param methodName original (not normalized) method name | ||
* @return true if the method is a getter | ||
*/ | ||
boolean isGetter(String methodName); | ||
|
||
/** | ||
* Normalizes column name to match method name. | ||
* @param columnName original column name | ||
* @return normalized column name | ||
*/ | ||
String normalizeColumnName(String columnName); | ||
} |
58 changes: 58 additions & 0 deletions
58
...c/main/java/com/clickhouse/client/api/metadata/DefaultColumnToMethodMatchingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.clickhouse.client.api.metadata; | ||
|
||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Default implementation of {@link ColumnToMethodMatchingStrategy} takes the following rules: | ||
* <ul> | ||
* <li>Method name is normalized by removing prefixes like "get", "set", "is", "has".</li> | ||
* <li>Column name is normalized by removing special characters like "-", "_", ".".</li> | ||
* <li>Normalized method name and column name are compared case-insensitively.</li> | ||
* </ul> | ||
* | ||
* | ||
*/ | ||
public class DefaultColumnToMethodMatchingStrategy implements ColumnToMethodMatchingStrategy { | ||
|
||
public static final DefaultColumnToMethodMatchingStrategy INSTANCE = new DefaultColumnToMethodMatchingStrategy(); | ||
|
||
private final Pattern getterPattern; | ||
private final Pattern setterPattern; | ||
|
||
private final Pattern methodReplacePattern; | ||
|
||
private final Pattern columnReplacePattern; | ||
|
||
|
||
public DefaultColumnToMethodMatchingStrategy() { | ||
this("^(get|is|has).+", "^(set).+", "^(get|set|is|has)|_", "[-_.]"); | ||
} | ||
|
||
public DefaultColumnToMethodMatchingStrategy(String getterPatternRegEx, String setterPaternRegEx, String methodReplacePatternRegEx, String columnReplacePatternRegEx) { | ||
this.getterPattern = Pattern.compile(getterPatternRegEx); | ||
this.setterPattern = Pattern.compile(setterPaternRegEx); | ||
this.methodReplacePattern = Pattern.compile(methodReplacePatternRegEx); | ||
this.columnReplacePattern = Pattern.compile(columnReplacePatternRegEx); | ||
} | ||
|
||
@Override | ||
public String normalizeMethodName(String methodName) { | ||
return methodReplacePattern.matcher(methodName).replaceAll("").toLowerCase(); | ||
} | ||
|
||
@Override | ||
public boolean isSetter(String methodName) { | ||
return setterPattern.matcher(methodName).matches(); | ||
} | ||
|
||
@Override | ||
public boolean isGetter(String methodName) { | ||
return getterPattern.matcher(methodName).matches(); | ||
} | ||
|
||
@Override | ||
public String normalizeColumnName(String columnName) { | ||
return columnReplacePattern.matcher(columnName).replaceAll("").toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters