Skip to content

Commit cc3b52c

Browse files
committed
4
1 parent 9f44580 commit cc3b52c

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

server/odc-service/src/main/java/com/oceanbase/odc/service/sqlcheck/rule/MySQLCheckRationalityForDBObjects.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
import java.util.Set;
2323
import java.util.function.Supplier;
2424

25+
import org.apache.commons.collections4.CollectionUtils;
26+
import org.springframework.jdbc.core.ConnectionCallback;
2527
import org.springframework.jdbc.core.JdbcOperations;
2628

29+
import com.oceanbase.odc.common.util.StringUtils;
2730
import com.oceanbase.odc.core.shared.constant.DialectType;
31+
import com.oceanbase.odc.service.plugin.SchemaPluginUtil;
2832
import com.oceanbase.odc.service.sqlcheck.SqlCheckContext;
2933
import com.oceanbase.odc.service.sqlcheck.SqlCheckRule;
3034
import com.oceanbase.odc.service.sqlcheck.SqlCheckUtil;
@@ -36,6 +40,14 @@
3640
import com.oceanbase.tools.dbbrowser.model.DBObjectIdentity;
3741
import com.oceanbase.tools.dbbrowser.model.DBObjectType;
3842
import com.oceanbase.tools.sqlparser.statement.Statement;
43+
import com.oceanbase.tools.sqlparser.statement.common.BraceBlock;
44+
import com.oceanbase.tools.sqlparser.statement.createtable.CreateTable;
45+
import com.oceanbase.tools.sqlparser.statement.select.ExpressionReference;
46+
import com.oceanbase.tools.sqlparser.statement.select.FromReference;
47+
import com.oceanbase.tools.sqlparser.statement.select.JoinReference;
48+
import com.oceanbase.tools.sqlparser.statement.select.NameReference;
49+
import com.oceanbase.tools.sqlparser.statement.select.Select;
50+
import com.oceanbase.tools.sqlparser.statement.select.SelectBody;
3951

4052
import lombok.NonNull;
4153

@@ -78,24 +90,66 @@ public List<CheckViolation> check(@NonNull Statement statement, @NonNull SqlChec
7890
if (allowedDBObjectTypes.contains(DBObjectType.TABLE.name())) {
7991
DBTableCheckRationalityChecker dbTableCheckRationalityChecker = new DBTableCheckRationalityChecker();
8092
// todo 获取需要校验存在的表对象
81-
List<DBObjectIdentity> shouldExistedTable =
82-
dbTableCheckRationalityChecker.ExtractShouldExistedDBObjects(schemaSupplier, statement);
93+
List<DBObjectIdentity> shouldExistedTables = new ArrayList<>();
94+
if (statement.getClass() == Select.class) {
95+
// todo 这里校验表对象存在的合理性
96+
Select select = (Select) statement;
97+
SelectBody selectBody = select.getSelectBody();
98+
List<FromReference> froms = selectBody.getFroms();
99+
// todo 获取需要校验存在的表对象
100+
if (CollectionUtils.isNotEmpty(froms)) {
101+
for (FromReference from : froms) {
102+
if (null != from) {
103+
if (from.getClass() == NameReference.class) {
104+
NameReference nameReference = (NameReference) from;
105+
if (null != nameReference.getSchema()) {
106+
shouldExistedTables
107+
.add(DBObjectIdentity.of(nameReference.getSchema(), DBObjectType.TABLE,
108+
StringUtils.unquoteMySqlIdentifier(nameReference.getRelation())));
109+
} else {
110+
shouldExistedTables.add(DBObjectIdentity.of(schemaSupplier.get(), DBObjectType.TABLE,
111+
StringUtils.unquoteMySqlIdentifier(nameReference.getRelation())));
112+
}
113+
} else if (from.getClass() == JoinReference.class) {
114+
115+
} else if (from.getClass() == BraceBlock.class) {
116+
117+
} else if (from.getClass() == ExpressionReference.class) {
118+
119+
}
120+
}
121+
}
122+
}
123+
} else if (statement.getClass() == CreateTable.class) {
124+
125+
}
83126
// todo 校验表对象是否存在
84-
for (DBObjectIdentity dbObjectIdentity : shouldExistedTable) {
85-
Boolean existed = dbTableCheckRationalityChecker.checkObjectExistence(dbObjectIdentity, jdbcOperations);
127+
for (DBObjectIdentity dbObjectIdentity : shouldExistedTables) {
128+
Boolean existed = jdbcOperations
129+
.execute((ConnectionCallback<Boolean>) con -> SchemaPluginUtil.getTableExtension(
130+
DialectType.OB_MYSQL)
131+
.list(con,
132+
dbObjectIdentity.getSchemaName(), DBObjectType.TABLE)
133+
.stream().anyMatch(
134+
table -> table.getName().equals(dbObjectIdentity.getName())));
86135
if (Boolean.FALSE.equals(existed)) {
87136
checkViolationlist.add(
88137
SqlCheckUtil.buildViolation(statement.getText(), statement, getType(),
89138
new Object[] {}));
90139
}
91140
}
92141
// todo 获取需要检验不存在的表对象
93-
List<DBObjectIdentity> shouldNotExistedTables =
94-
dbTableCheckRationalityChecker.ExtractShouldExistedDBObjects(schemaSupplier, statement);
142+
List<DBObjectIdentity> shouldNotExistedTables = null;
143+
95144
// todo 执行校验表对象不存在逻辑
96-
for (DBObjectIdentity shouldNotExistedTable : shouldNotExistedTables) {
97-
Boolean existed =
98-
dbTableCheckRationalityChecker.checkObjectNonExistence(shouldNotExistedTable, jdbcOperations);
145+
for (DBObjectIdentity dbObjectIdentity : shouldNotExistedTables) {
146+
Boolean existed = !jdbcOperations
147+
.execute((ConnectionCallback<Boolean>) con -> SchemaPluginUtil.getTableExtension(
148+
DialectType.OB_MYSQL)
149+
.list(con,
150+
dbObjectIdentity.getSchemaName(), DBObjectType.TABLE)
151+
.stream().anyMatch(
152+
table -> table.getName().equals(dbObjectIdentity.getName())));
99153
if (Boolean.FALSE.equals(existed)) {
100154
checkViolationlist.add(
101155
SqlCheckUtil.buildViolation(statement.getText(), statement, getType(),

server/odc-service/src/main/java/com/oceanbase/odc/service/sqlcheck/rule/MySQLCheckRationalityForDBObjects1.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public class MySQLCheckRationalityForDBObjects1 implements SqlCheckRule {
5454

5555
private final JdbcOperations jdbcOperations;
5656

57-
List<DBObjectType> supportedDBObjectType = Arrays.asList(DBObjectType.TABLE, DBObjectType.COLUMN);
58-
5957
public MySQLCheckRationalityForDBObjects1(Boolean supportedSimulation, Set<String> allowedDBObjectTypes, Supplier<String> schemaSupplier, JdbcOperations jdbcOperations) {
6058
this.supportedSimulation = supportedSimulation;
6159
this.allowedDBObjectTypes = allowedDBObjectTypes;
@@ -103,7 +101,7 @@ public List<CheckViolation> check(@NonNull Statement statement, @NonNull SqlChec
103101
}
104102
}
105103
// todo 这里校验列对象存在的合理性
106-
else if (supportedDBObjectType.contains(DBObjectType.COLUMN)) {
104+
else if (allowedDBObjectTypes.contains(DBObjectType.COLUMN)) {
107105
DBColumnCheckRationalityChecker dbColumnCheckRationalityChecker = new DBColumnCheckRationalityChecker();
108106
// todo 获取需要校验存在的表对象
109107
List<DBObjectIdentity> shouldExistedColumn =
@@ -131,7 +129,7 @@ else if (supportedDBObjectType.contains(DBObjectType.COLUMN)) {
131129
new Object[] {}));
132130
}
133131
}
134-
} else if (supportedDBObjectType.contains(DBObjectType.INDEX)) {
132+
} else if (allowedDBObjectTypes.contains(DBObjectType.INDEX)) {
135133
// todo 这里校验索引对象存在的合理性
136134
}
137135
return checkViolationlist;

0 commit comments

Comments
 (0)