Skip to content
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

fix(db-browser): cannot get table charset in native mysql mode #592

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1001,11 +1001,10 @@ public DBTableOptions getTableOptions(String schemaName, String tableName) {
public DBTableOptions getTableOptions(String schemaName, String tableName, @lombok.NonNull String ddl) {
DBTableOptions dbTableOptions = new DBTableOptions();
obtainOptionsByQuery(schemaName, tableName, dbTableOptions);
DBSchemaAccessorUtil.obtainOptionsByParse(dbTableOptions, ddl);
PeachThinking marked this conversation as resolved.
Show resolved Hide resolved
return dbTableOptions;
}

private void obtainOptionsByQuery(String schemaName, String tableName, DBTableOptions dbTableOptions) {
protected void obtainOptionsByQuery(String schemaName, String tableName, DBTableOptions dbTableOptions) {
String sql = this.sqlMapper.getSql(Statements.GET_TABLE_OPTION);
jdbcOperations.query(sql, new Object[] {schemaName, tableName}, t -> {
dbTableOptions.setCreateTime(t.getTimestamp("CREATE_TIME"));
Expand All @@ -1014,6 +1013,13 @@ private void obtainOptionsByQuery(String schemaName, String tableName, DBTableOp
dbTableOptions.setCollationName(t.getString("TABLE_COLLATION"));
dbTableOptions.setComment(t.getString("TABLE_COMMENT"));
});
MySQLSqlBuilder getCharset = new MySQLSqlBuilder();
getCharset.append("SELECT b.character_set_name FROM `information_schema`.`TABLES` a,")
.append(" `information_schema`.`COLLATION_CHARACTER_SET_APPLICABILITY` b")
.append(" WHERE b.collation_name = a.table_collation")
.append(" AND a.table_schema = ")
.value(schemaName).append(" AND a.table_name = ").value(tableName);
dbTableOptions.setCharsetName(jdbcOperations.queryForObject(getCharset.toString(), String.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.oceanbase.tools.dbbrowser.model.DBObjectIdentity;
import com.oceanbase.tools.dbbrowser.model.DBObjectType;
import com.oceanbase.tools.dbbrowser.model.DBObjectWarningDescriptor;
import com.oceanbase.tools.dbbrowser.model.DBTable.DBTableOptions;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableIndex;
import com.oceanbase.tools.dbbrowser.parser.SqlParser;
Expand Down Expand Up @@ -135,6 +136,13 @@ public List<DBObjectIdentity> listAllSystemViews() {
return results;
}

@Override
public DBTableOptions getTableOptions(String schemaName, String tableName, @lombok.NonNull String ddl) {
DBTableOptions dbTableOptions = super.getTableOptions(schemaName, tableName, ddl);
DBSchemaAccessorUtil.obtainOptionsByParse(dbTableOptions, ddl);
return dbTableOptions;
}

@Override
public List<DBTableColumn> listTableColumns(String schemaName, String tableName) {
List<DBTableColumn> columns = super.listTableColumns(schemaName, tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.junit.AfterClass;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class MySQLNoGreaterThan5740SchemaAccessorTest extends BaseTestEnv {
private static List<MySQLNoGreaterThan5740SchemaAccessorTest.DataType> verifyDataTypes = new ArrayList<>();
private static List<MySQLNoGreaterThan5740SchemaAccessorTest.ColumnAttributes> columnAttributes = new ArrayList<>();
private static JdbcTemplate jdbcTemplate = new JdbcTemplate(getMySQLDataSource());
private static DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();

@BeforeClass
public static void setUp() throws Exception {
Expand Down Expand Up @@ -96,21 +98,18 @@ private static void batchExecuteSql(String str, String delimiter) {

@Test
public void getDatabase_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBDatabase database = accessor.getDatabase(getMySQLDataBaseName());
Assert.assertNotNull(database);
}

@Test
public void listDatabases_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBDatabase> databases = accessor.listDatabases();
Assert.assertTrue(databases.size() > 0);
}

@Test
public void listTableColumns_TestAllColumnDataTypes_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableColumn> columns =
accessor.listTableColumns(getMySQLDataBaseName(), "test_data_type");
Assert.assertEquals(columns.size(), verifyDataTypes.size());
Expand All @@ -124,7 +123,6 @@ public void listTableColumns_TestAllColumnDataTypes_Success() {

@Test
public void listTableColumns_TestColumnAttributesOtherThanDataType_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableColumn> columns =
accessor.listTableColumns(getMySQLDataBaseName(), "test_other_than_data_type");
Assert.assertEquals(columns.size(), columnAttributes.size());
Expand All @@ -140,7 +138,6 @@ public void listTableColumns_TestColumnAttributesOtherThanDataType_Success() {

@Test
public void listTableIndex_TestIndexType_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableIndex> indexList = accessor.listTableIndexes(getMySQLDataBaseName(), "test_index_type");
Assert.assertEquals(5, indexList.size());
Assert.assertEquals(DBIndexAlgorithm.BTREE, indexList.get(0).getAlgorithm());
Expand All @@ -156,14 +153,12 @@ public void listTableIndex_TestIndexType_Success() {

@Test
public void listTableIndex_TestIndexAvailable_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableIndex> indexList = accessor.listTableIndexes(getMySQLDataBaseName(), "test_index_type");
Assert.assertTrue(indexList.get(0).getAvailable());
}

@Test
public void listTableIndex_get_all_index_in_schema_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
Map<String, List<DBTableIndex>> map = accessor.listTableIndexes(getMySQLDataBaseName());
Assert.assertNotNull(map);
Assert.assertTrue(map.size() > 0);
Expand All @@ -172,7 +167,6 @@ public void listTableIndex_get_all_index_in_schema_Success() {

@Test
public void listTableConstraint_TestForeignKey_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableConstraint> constraintListList =
accessor.listTableConstraints(getMySQLDataBaseName(), "test_fk_child");
Assert.assertEquals(1, constraintListList.size());
Expand All @@ -182,7 +176,6 @@ public void listTableConstraint_TestForeignKey_Success() {

@Test
public void listTableConstraint_TestPrimaryKey_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBTableConstraint> constraintListList =
accessor.listTableConstraints(getMySQLDataBaseName(), "test_fk_parent");
Assert.assertEquals(1, constraintListList.size());
Expand All @@ -192,28 +185,24 @@ public void listTableConstraint_TestPrimaryKey_Success() {

@Test
public void listSystemViews_information_schema_not_empty() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<String> viewNames = accessor.showSystemViews("information_schema");
Assert.assertTrue(!viewNames.isEmpty());
}

@Test
public void listAllSystemViews_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBObjectIdentity> sysViews = accessor.listAllSystemViews();
Assert.assertTrue(sysViews != null && sysViews.size() > 0);
}

@Test
public void listSystemViews_databaseNotFound_empty() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<String> viewNames = accessor.showSystemViews("databaseNotExists");
Assert.assertTrue(viewNames.isEmpty());
}

@Test
public void getPartition_Hash_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBTablePartition partition =
accessor.getPartition(getMySQLDataBaseName(), "part_hash");
Assert.assertEquals(5L, partition.getPartitionOption().getPartitionsNum().longValue());
Expand All @@ -222,7 +211,6 @@ public void getPartition_Hash_Success() {

@Test
public void getPartition_List_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBTablePartition partition =
accessor.getPartition(getMySQLDataBaseName(), "part_list");
Assert.assertEquals(5L, partition.getPartitionOption().getPartitionsNum().longValue());
Expand All @@ -232,7 +220,6 @@ public void getPartition_List_Success() {

@Test
public void getPartition_Range_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBTablePartition partition =
accessor.getPartition(getMySQLDataBaseName(), "part_range");
Assert.assertEquals(3L, partition.getPartitionOption().getPartitionsNum().longValue());
Expand All @@ -242,36 +229,31 @@ public void getPartition_Range_Success() {

@Test
public void listTableOptions_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
Map<String, DBTableOptions> table2Options =
accessor.listTableOptions(getMySQLDataBaseName());
Assert.assertTrue(table2Options.containsKey("part_hash"));
}

@Test
public void showTablelike_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBObjectIdentity> tables = accessor.listTables(getMySQLDataBaseName(), null);
Assert.assertTrue(tables != null && tables.size() > 0);
}

@Test
public void showViews_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBObjectIdentity> views = accessor.listViews(getMySQLDataBaseName());
Assert.assertTrue(views != null && views.size() == 2);
}

@Test
public void getView_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBView view = accessor.getView(getMySQLDataBaseName(), "view_test1");
Assert.assertTrue(view != null && view.getColumns().size() == 2);
}

@Test
public void showVariables_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<DBVariable> variables = accessor.showVariables();
List<DBVariable> sessionVariables = accessor.showSessionVariables();
List<DBVariable> globalVariables = accessor.showGlobalVariables();
Expand All @@ -282,7 +264,6 @@ public void showVariables_Success() {

@Test
public void getFunction_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBFunction function = accessor.getFunction(getMySQLDataBaseName(), "function_test");
Assert.assertTrue(function != null
&& function.getParams().size() == 2
Expand All @@ -291,7 +272,6 @@ public void getFunction_Success() {

@Test
public void getProcedure_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
DBProcedure procedure = accessor.getProcedure(getMySQLDataBaseName(), "procedure_detail_test");
Assert.assertTrue(procedure != null
&& procedure.getParams().size() == 2
Expand All @@ -300,11 +280,18 @@ public void getProcedure_Success() {

@Test
public void showTables_unknowDatabase_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getMySQLDataSource()).createMysql();
List<String> tables = accessor.showTables("abc");
Assert.assertTrue(tables.size() == 0);
}

@Test
public void getTableOptions_Success() {
DBTableOptions options =
accessor.getTableOptions(getMySQLDataBaseName(), "test_data_type");
Assert.assertTrue(Objects.nonNull(options.getCharsetName()));
Assert.assertTrue(Objects.nonNull(options.getCollationName()));
}

private static void initVerifyColumnAttributes() {
columnAttributes.addAll(Arrays.asList(
MySQLNoGreaterThan5740SchemaAccessorTest.ColumnAttributes.of("col1", false, false, true, null,
Expand Down
Loading