Unverified Commit 30258e4d authored by Licho's avatar Licho Committed by GitHub

feat: watch multi table one watch statemnt (#1872)

* feat: watch multi table one select statemnt Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent a0fb72a1
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.dinky.service.impl; package org.dinky.service.impl;
import org.dinky.db.service.impl.SuperServiceImpl; import org.dinky.db.service.impl.SuperServiceImpl;
import org.dinky.explainer.watchTable.WatchStatementExplainer;
import org.dinky.mapper.StatementMapper; import org.dinky.mapper.StatementMapper;
import org.dinky.model.Statement; import org.dinky.model.Statement;
import org.dinky.parser.SqlType; import org.dinky.parser.SqlType;
...@@ -55,7 +56,7 @@ public class StatementServiceImpl extends SuperServiceImpl<StatementMapper, Stat ...@@ -55,7 +56,7 @@ public class StatementServiceImpl extends SuperServiceImpl<StatementMapper, Stat
final String[] statements = SqlUtil.getStatements(statement); final String[] statements = SqlUtil.getStatements(statement);
return Arrays.stream(statements) return Arrays.stream(statements)
.filter(t -> SqlType.WATCH.equals(Operations.getOperationType(t))) .filter(t -> SqlType.WATCH.equals(Operations.getOperationType(t)))
.map(t -> t.substring(t.lastIndexOf(" ")).trim()) .flatMap(t -> Arrays.stream(WatchStatementExplainer.splitTableNames(t)))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }
...@@ -128,11 +128,14 @@ public class Explainer { ...@@ -128,11 +128,14 @@ public class Explainer {
} else if (operationType.equals(SqlType.WATCH)) { } else if (operationType.equals(SqlType.WATCH)) {
WatchStatementExplainer watchStatementExplainer = WatchStatementExplainer watchStatementExplainer =
new WatchStatementExplainer(statement); new WatchStatementExplainer(statement);
String tableName = watchStatementExplainer.getTableName();
String[] tableNames = watchStatementExplainer.getTableNames();
for (String tableName : tableNames) {
trans.add( trans.add(
new StatementParam( new StatementParam(
watchStatementExplainer.getCreateStatement(tableName), WatchStatementExplainer.getCreateStatement(tableName),
SqlType.CTAS)); SqlType.CTAS));
}
} else { } else {
UDF udf = UDFUtil.toUDF(statement); UDF udf = UDFUtil.toUDF(statement);
if (Asserts.isNotNull(udf)) { if (Asserts.isNotNull(udf)) {
......
...@@ -43,13 +43,18 @@ public class WatchStatementExplainer { ...@@ -43,13 +43,18 @@ public class WatchStatementExplainer {
this.statement = statement; this.statement = statement;
} }
public String getTableName() { public String[] getTableNames() {
return splitTableNames(statement);
}
public static String[] splitTableNames(String statement) {
Matcher matcher = PATTERN.matcher(statement); Matcher matcher = PATTERN.matcher(statement);
matcher.find(); matcher.find();
return matcher.group(1); String tableNames = matcher.group(1);
return tableNames.replaceAll(" ", "").split(",");
} }
public String getCreateStatement(String tableName) { public static String getCreateStatement(String tableName) {
Optional<InetAddress> address = getSystemLocalIp(); Optional<InetAddress> address = getSystemLocalIp();
String ip = address.isPresent() ? address.get().getHostAddress() : "127.0.0.1"; String ip = address.isPresent() ? address.get().getHostAddress() : "127.0.0.1";
return MessageFormat.format(CREATE_SQL_TEMPLATE, tableName, ip, PORT); return MessageFormat.format(CREATE_SQL_TEMPLATE, tableName, ip, PORT);
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.watchTable;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class WatchStatementExplainerTest {
@Test
void getTableNames() {
String sql = "watch VersionT";
WatchStatementExplainer watchStatementExplainer = new WatchStatementExplainer(sql);
assertArrayEquals(new String[] {"VersionT"}, watchStatementExplainer.getTableNames());
sql = "watch VersionT, Buyers, r, rr, vvv";
WatchStatementExplainer wse = new WatchStatementExplainer(sql);
assertArrayEquals(
new String[] {"VersionT", "Buyers", "r", "rr", "vvv"}, wse.getTableNames());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment