Skip to content

Commit

Permalink
add check table exist in table extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
krihy committed Sep 2, 2023
1 parent e693470 commit 7ec3ee4
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ public interface TableExtensionPoint extends ExtensionPoint {

List<String> showNamesLike(Connection connection, String schemaName, String tableNameLike);

boolean checkTableExist(Connection connection, String schemaName, String tableName);

DBTable getDetail(Connection connection, String schemaName, String tableName);

void drop(Connection connection, String schemaName, String tableName);

String generateCreateDDL(Connection connection, DBTable table);

String generateUpdateDDL(Connection connection, DBTable oldTable, DBTable newTable);

}
4 changes: 4 additions & 0 deletions server/plugins/schema-plugin-ob-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>odc-test</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.pf4j.Extension;

import com.oceanbase.odc.common.util.VersionUtils;
Expand All @@ -41,6 +42,7 @@
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.schema.DBSchemaAccessor;
import com.oceanbase.tools.dbbrowser.stats.DBStatsAccessor;
import com.oceanbase.tools.dbbrowser.util.StringUtils;

import lombok.NonNull;

Expand Down Expand Up @@ -69,6 +71,16 @@ public List<String> showNamesLike(@NonNull Connection connection, @NonNull Strin
return getSchemaAccessor(connection).showTablesLike(schemaName, tableNameLike);
}

@Override
public boolean checkTableExist(Connection connection, String schemaName, String tableName) {
if (tableName == null) {
return false;
}
List<String> names = showNamesLike(connection, schemaName,
StringUtils.unquoteMySqlIdentifier(tableName).toLowerCase());
return CollectionUtils.isNotEmpty(names);
}

@Override
public DBTable getDetail(@NonNull Connection connection, @NonNull String schemaName, @NonNull String tableName) {
DBSchemaAccessor schemaAccessor = getSchemaAccessor(connection);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.odc.plugin.schema.obmysql.parser;

import java.sql.Connection;
import java.text.MessageFormat;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.plugin.connect.obmysql.util.JdbcOperationsUtil;
import com.oceanbase.odc.plugin.schema.api.TableExtensionPoint;
import com.oceanbase.odc.plugin.schema.obmysql.OBMySQLTableExtension;
import com.oceanbase.odc.test.database.TestDBConfiguration;
import com.oceanbase.odc.test.database.TestDBConfigurations;

/**
* @author yaobin
* @date 2023-09-02
* @since 4.2.0
*/
public class OBMySQLTableExtensionTest {

private static final TestDBConfiguration configuration =
TestDBConfigurations.getInstance().getTestOBMysqlConfiguration();
private static Connection connection;
private static TableExtensionPoint tableEp;

@BeforeClass
public static void setUp() throws Exception {
connection = configuration.getDataSource().getConnection();
tableEp = new OBMySQLTableExtension();
}


@Test
public void test_checkLowerCaseTableExist() {
String t = "check_table0";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkUpperCaseTableExist() {
String t = "CHECK_TABLE1";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkUpperCaseTableWithQuoteExist() {
String t = "`CHECK_TABLE2`";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkLowerCaseTableWithQuoteExist() {
String t = "`check_table3`";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkRandomTableNotExist() {
String t = StringUtils.uuidNoHyphen();
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertFalse(checkResult);
}


private String getCreateSql(String tableName) {
return MessageFormat.format("CREATE TABLE {0} (id int(11))", tableName);
}

private String getDropSql(String tableName) {
return MessageFormat.format("DROP TABLE {0} ", tableName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import org.apache.commons.collections4.CollectionUtils;
import org.pf4j.Extension;

import com.oceanbase.odc.plugin.connect.obmysql.util.JdbcOperationsUtil;
Expand Down Expand Up @@ -57,6 +59,29 @@ public class OBOracleTableExtension extends OBMySQLTableExtension {
private static final String ORACLE_COLUMN_COMMENT_DDL_TEMPLATE =
"COMMENT ON COLUMN ${schemaName}.${tableName}.${columnName} IS ${comment}";

@Override
public boolean checkTableExist(Connection connection, String schemaName, String tableName) {
if (tableName == null) {
return false;
}
// tableName wrapped by ""
if ((tableName.startsWith("\"") && tableName.endsWith("\""))) {
return doCheckTableExist(connection, schemaName, () -> StringUtils.unquoteOracleIdentifier(tableName));
}
String lowerCaseVarSql = "show variables like 'lower_case_table_names'";
Integer queryResult = JdbcOperationsUtil.getJdbcOperations(connection)
.query(lowerCaseVarSql, rs -> rs.next() ? rs.getInt("VALUE") : null);
if (queryResult == null || queryResult == 0) {
return doCheckTableExist(connection, schemaName, () -> tableName);
}
return doCheckTableExist(connection, schemaName, tableName::toUpperCase);
}

private boolean doCheckTableExist(Connection connection, String schemaName, Supplier<String> tableName) {
List<String> names = showNamesLike(connection, schemaName, tableName.get());
return CollectionUtils.isNotEmpty(names);
}

@Override
public DBTable getDetail(@NonNull Connection connection, @NonNull String schemaName, @NonNull String tableName) {
DBSchemaAccessor schemaAccessor = getSchemaAccessor(connection);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.odc.plugin.schema.oboracle.parser;

import java.sql.Connection;
import java.text.MessageFormat;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.plugin.connect.obmysql.util.JdbcOperationsUtil;
import com.oceanbase.odc.plugin.schema.api.TableExtensionPoint;
import com.oceanbase.odc.plugin.schema.oboracle.OBOracleTableExtension;
import com.oceanbase.odc.test.database.TestDBConfiguration;
import com.oceanbase.odc.test.database.TestDBConfigurations;

/**
* @author yaobin
* @date 2023-09-02
* @since 4.2.0
*/
public class OBOracleTableExtensionTest {

private static final TestDBConfiguration configuration =
TestDBConfigurations.getInstance().getTestOBOracleConfiguration();
private static Connection connection;
private static TableExtensionPoint tableEp;

@BeforeClass
public static void setUp() throws Exception {
connection = configuration.getDataSource().getConnection();
tableEp = new OBOracleTableExtension();
}


@Test
public void test_checkLowerCaseTableExist() {
String t = "check_table0";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkUpperCaseTableExist() {
String t = "CHECK_TABLE1";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkUpperCaseTableWithQuoteExist() {
String t = "\"CHECK_TABLE2\"";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkLowerCaseTableWithQuoteExist() {
String t = "\"check_table3\"";
JdbcOperationsUtil.getJdbcOperations(connection).execute(getCreateSql(t));
try {
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertTrue(checkResult);
} finally {
JdbcOperationsUtil.getJdbcOperations(connection).execute(getDropSql(t));
}
}

@Test
public void test_checkRandomTableNotExist() {
String t = StringUtils.uuidNoHyphen();
boolean checkResult = tableEp.checkTableExist(connection, configuration.getDefaultDBName(), t);
Assert.assertFalse(checkResult);
}


private String getCreateSql(String tableName) {
return MessageFormat.format("CREATE TABLE {0} (id number(11))", tableName);
}

private String getDropSql(String tableName) {
return MessageFormat.format("DROP TABLE {0} ", tableName);
}

}

0 comments on commit 7ec3ee4

Please sign in to comment.