|
22 | 22 | import java.util.Set; |
23 | 23 | import java.util.function.Supplier; |
24 | 24 |
|
| 25 | +import org.apache.commons.collections4.CollectionUtils; |
| 26 | +import org.springframework.jdbc.core.ConnectionCallback; |
25 | 27 | import org.springframework.jdbc.core.JdbcOperations; |
26 | 28 |
|
| 29 | +import com.oceanbase.odc.common.util.StringUtils; |
27 | 30 | import com.oceanbase.odc.core.shared.constant.DialectType; |
| 31 | +import com.oceanbase.odc.service.plugin.SchemaPluginUtil; |
28 | 32 | import com.oceanbase.odc.service.sqlcheck.SqlCheckContext; |
29 | 33 | import com.oceanbase.odc.service.sqlcheck.SqlCheckRule; |
30 | 34 | import com.oceanbase.odc.service.sqlcheck.SqlCheckUtil; |
|
36 | 40 | import com.oceanbase.tools.dbbrowser.model.DBObjectIdentity; |
37 | 41 | import com.oceanbase.tools.dbbrowser.model.DBObjectType; |
38 | 42 | 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; |
39 | 51 |
|
40 | 52 | import lombok.NonNull; |
41 | 53 |
|
@@ -78,24 +90,66 @@ public List<CheckViolation> check(@NonNull Statement statement, @NonNull SqlChec |
78 | 90 | if (allowedDBObjectTypes.contains(DBObjectType.TABLE.name())) { |
79 | 91 | DBTableCheckRationalityChecker dbTableCheckRationalityChecker = new DBTableCheckRationalityChecker(); |
80 | 92 | // 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 | + } |
83 | 126 | // 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()))); |
86 | 135 | if (Boolean.FALSE.equals(existed)) { |
87 | 136 | checkViolationlist.add( |
88 | 137 | SqlCheckUtil.buildViolation(statement.getText(), statement, getType(), |
89 | 138 | new Object[] {})); |
90 | 139 | } |
91 | 140 | } |
92 | 141 | // todo 获取需要检验不存在的表对象 |
93 | | - List<DBObjectIdentity> shouldNotExistedTables = |
94 | | - dbTableCheckRationalityChecker.ExtractShouldExistedDBObjects(schemaSupplier, statement); |
| 142 | + List<DBObjectIdentity> shouldNotExistedTables = null; |
| 143 | + |
95 | 144 | // 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()))); |
99 | 153 | if (Boolean.FALSE.equals(existed)) { |
100 | 154 | checkViolationlist.add( |
101 | 155 | SqlCheckUtil.buildViolation(statement.getText(), statement, getType(), |
|
0 commit comments