Skip to content

fix: DatabaseUtils sql parse #212

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

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arex-storage-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.34</version>
<version>1.1.35</version>
</parent>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion arex-storage-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.34</version>
<version>1.1.35</version>
</parent>

<profiles>
Expand Down
2 changes: 1 addition & 1 deletion arex-storage-web-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.34</version>
<version>1.1.35</version>
</parent>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DatabaseUtils {
private DatabaseUtils() {
}

private static final Pattern PATTERN = Pattern.compile("\\s+");
private static final Pattern PATTERN = Pattern.compile("(\\s+|\"\\?\")");

public static void regenerateOperationName(Mocker mocker) {
if (!MockCategoryType.DATABASE.getName().equals(mocker.getCategoryType().getName())) {
Expand Down Expand Up @@ -89,8 +89,8 @@ public static String parseDbName(String operationName, Mocker.Target targetReque
}

/**
* @param operationName example: dbName@tableName, tableName, tableName@action@operationName;
* @return tableNames
* @param operationName eg: db1@table1,table2@select@operation1;db2@table3,table4@select@operation2;
* @return tableNames eg: ["table1,table2", "table3,table4"]
*/
public static List<String> parseTableNames(String operationName) {
if (StringUtils.isEmpty(operationName)) {
Expand All @@ -104,8 +104,8 @@ public static List<String> parseTableNames(String operationName) {
String[] operations = StringUtils.split(operationName, ';');
List<String> tableList = new ArrayList<>(operations.length);
for (String operation : operations) {
String[] subOperation = StringUtils.split(operation, '@');
if (subOperation.length < 1) {
String[] subOperation = StringUtils.splitPreserveAllTokens(operation, '@');
if (subOperation.length < 2 || StringUtils.isEmpty(subOperation[1])) {
continue;
}
tableList.add(subOperation[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,34 @@ public void testSelectWithInnerJoin() {
assertEquals("tk_project,tk_user_received_task_21", StringUtils.join(tableSchema.getTableNames(), ","));
assertEquals(DbParseConstants.SELECT.toLowerCase(), tableSchema.getAction().toLowerCase());
}

@Test
public void parseTableNames() {
// test operationName is Empty
List<String> tableNames = DatabaseUtils.parseTableNames("");
assertEquals(0, tableNames.size());

// test operationName contains one @
tableNames = DatabaseUtils.parseTableNames("db1@table1");
assertEquals(0, tableNames.size());

// test table name contain empty
tableNames = DatabaseUtils.parseTableNames("@@select@query");
assertEquals(0, tableNames.size());

tableNames = DatabaseUtils.parseTableNames("db1@table1,table2@select@operation1;db2@table3,table4@select@operation2");
assertEquals("table1,table2", tableNames.get(0));
assertEquals("table3,table4", tableNames.get(1));
}

@Test
public void parseSQLWithSubSelect() {
String sql = "select count(0) from ( select qi.code from qc_issue qi "
+ "where qi.enable = 1 "
+ "and (qi.title like \"%'?'%\" or qi.code like \"%\"?\"%\") "
+ "and qi.product_code in ( ? ) "
+ "order by qi.id desc ) tmp_count";
TableSchema tableSchema = DatabaseUtils.parse(sql);
assertEquals("qc_issue", StringUtils.join(tableSchema.getTableNames(), ","));
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,5 +427,5 @@
<url>https://github.com/arextest/arex-storage</url>
</scm>
<url>https://github.com/arextest/arex-storage</url>
<version>1.1.34</version>
<version>1.1.35</version>
</project>