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

Oceanbase code submission #1103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,9 +1,197 @@
package ai.chat2db.plugin.oceanbase;

import ai.chat2db.plugin.oceanbase.builder.OceanBaseSqlBuilder;
import ai.chat2db.plugin.oceanbase.type.*;
import ai.chat2db.spi.MetaData;
import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.jdbc.DefaultMetaService;
import ai.chat2db.spi.model.*;
import ai.chat2db.spi.sql.SQLExecutor;
import jakarta.validation.constraints.NotEmpty;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class OceanBaseMetaData extends DefaultMetaService implements MetaData {

@Override
public SqlBuilder getSqlBuilder() {
return new OceanBaseSqlBuilder();
}

@Override
public List<Table> tables(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = "SHOW TABLE STATUS FROM "+ format(databaseName) + " where name = '" + tableName + "';";
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
List<Table> tables = new ArrayList<>();
while (resultSet.next()) {
Table table = new Table();
table.setDatabaseName(databaseName);
table.setSchemaName(schemaName);
table.setName(resultSet.getString("NAME"));
table.setComment(resultSet.getString("COMMENT"));
tables.add(table);
}
return tables;
});
}

private static String SELECT_TABLE_COLUMNS = "SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s' order by ORDINAL_POSITION";
@Override
public List<TableColumn> columns(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = String.format(SELECT_TABLE_COLUMNS, databaseName, tableName);
List<TableColumn> tableColumns = new ArrayList<>();
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
while (resultSet.next()) {
TableColumn column = new TableColumn();
column.setDatabaseName(databaseName);
column.setTableName(tableName);
column.setOldName(resultSet.getString("COLUMN_NAME"));
column.setName(resultSet.getString("COLUMN_NAME"));
//column.setColumnType(resultSet.getString("COLUMN_TYPE"));
column.setColumnType(resultSet.getString("DATA_TYPE").toUpperCase());
//column.setDataType(resultSet.getInt("DATA_TYPE"));
column.setDefaultValue(resultSet.getString("COLUMN_DEFAULT"));
column.setAutoIncrement(resultSet.getString("EXTRA").contains("auto_increment"));
column.setComment(resultSet.getString("COLUMN_COMMENT"));
column.setPrimaryKey("PRI".equalsIgnoreCase(resultSet.getString("COLUMN_KEY")));
column.setNullable("YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")) ? 1 : 0);
column.setOrdinalPosition(resultSet.getInt("ORDINAL_POSITION"));
column.setDecimalDigits(resultSet.getInt("NUMERIC_SCALE"));
column.setCharSetName(resultSet.getString("CHARACTER_SET_NAME"));
column.setCollationName(resultSet.getString("COLLATION_NAME"));
setColumnSize(column, resultSet.getString("COLUMN_TYPE"));
tableColumns.add(column);
}
return tableColumns;
});

}

private void setColumnSize(TableColumn column, String columnType) {
try {
if (columnType.contains("(")) {
String size = columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")"));
if ("SET".equalsIgnoreCase(column.getColumnType()) || "ENUM".equalsIgnoreCase(column.getColumnType())) {
column.setValue(size);
} else {
if (size.contains(",")) {
String[] sizes = size.split(",");
if (StringUtils.isNotBlank(sizes[0])) {
column.setColumnSize(Integer.parseInt(sizes[0]));
}
if (StringUtils.isNotBlank(sizes[1])) {
column.setDecimalDigits(Integer.parseInt(sizes[1]));
}
} else {
column.setColumnSize(Integer.parseInt(size));
}
}
}
} catch (Exception e) {
}
}

@Override
public TableMeta getTableMeta(String databaseName, String schemaName, String tableName) {
return TableMeta.builder()
.columnTypes(OceanBaseColumnTypeEnum.getTypes())
.charsets(OceanBaseCharsetEnum.getCharsets())
.collations(OceanBaseCollationEnum.getCollations())
.indexTypes(OceanBaseIndexTypeEnum.getIndexTypes())
.defaultValues(OceanBaseDefaultValueEnum.getDefaultValues())
.build();
}

@Override
public String getMetaDataName(String... names) {
return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> format(name)).collect(Collectors.joining("."));
}


@Override
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
@NotEmpty String tableName) {
String sql = "SHOW CREATE TABLE " + format(databaseName) + "."
+ format(tableName);
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
if (resultSet.next()) {
return resultSet.getString("Create Table");
}
return null;
});
}

@Override
public List<TableIndex> indexes(Connection connection, String databaseName, String schemaName, String tableName) {
StringBuilder queryBuf = new StringBuilder("SHOW INDEX FROM ");
queryBuf.append(format(tableName));
queryBuf.append(".");
queryBuf.append(format(databaseName));
return SQLExecutor.getInstance().execute(connection, queryBuf.toString(), resultSet -> {
LinkedHashMap<String, TableIndex> map = new LinkedHashMap();
while (resultSet.next()) {
String keyName = resultSet.getString("Key_name");
TableIndex tableIndex = map.get(keyName);
if (tableIndex != null) {
List<TableIndexColumn> columnList = tableIndex.getColumnList();
columnList.add(getTableIndexColumn(resultSet));
columnList = columnList.stream().sorted(Comparator.comparing(TableIndexColumn::getOrdinalPosition))
.collect(Collectors.toList());
tableIndex.setColumnList(columnList);
} else {
TableIndex index = new TableIndex();
index.setDatabaseName(databaseName);
index.setSchemaName(schemaName);
index.setTableName(tableName);
index.setName(keyName);
index.setUnique(!resultSet.getBoolean("Non_unique"));
index.setType(resultSet.getString("Index_type"));
index.setComment(resultSet.getString("Index_comment"));
List<TableIndexColumn> tableIndexColumns = new ArrayList<>();
tableIndexColumns.add(getTableIndexColumn(resultSet));
index.setColumnList(tableIndexColumns);
if ("PRIMARY".equalsIgnoreCase(keyName)) {
index.setType(OceanBaseIndexTypeEnum.PRIMARY_KEY.getName());
} else if (index.getUnique()) {
index.setType(OceanBaseIndexTypeEnum.UNIQUE.getName());
} else if ("SPATIAL".equalsIgnoreCase(index.getType())) {
index.setType(OceanBaseIndexTypeEnum.SPATIAL.getName());
} else if ("FULLTEXT".equalsIgnoreCase(index.getType())) {
index.setType(OceanBaseIndexTypeEnum.FULLTEXT.getName());
} else {
index.setType(OceanBaseIndexTypeEnum.NORMAL.getName());
}
map.put(keyName, index);
}
}
return map.values().stream().collect(Collectors.toList());
});

}

private TableIndexColumn getTableIndexColumn(ResultSet resultSet) throws SQLException {
TableIndexColumn tableIndexColumn = new TableIndexColumn();
tableIndexColumn.setColumnName(resultSet.getString("Column_name"));
tableIndexColumn.setOrdinalPosition(resultSet.getShort("Seq_in_index"));
tableIndexColumn.setCollation(resultSet.getString("Collation"));
tableIndexColumn.setCardinality(resultSet.getLong("Cardinality"));
tableIndexColumn.setSubPart(resultSet.getLong("Sub_part"));
String collation = resultSet.getString("Collation");
if ("a".equalsIgnoreCase(collation)) {
tableIndexColumn.setAscOrDesc("ASC");
} else if ("d".equalsIgnoreCase(collation)) {
tableIndexColumn.setAscOrDesc("DESC");
}
return tableIndexColumn;
}

public static String format(String tableName) {
return "`" + tableName + "`";
}

}
Loading