Skip to content

Commit

Permalink
[Fix-2125][metadata] Fix clickhouse Nullable columns (#2503)
Browse files Browse the repository at this point in the history
Co-authored-by: wenmo <[email protected]>
  • Loading branch information
aiwenmo and aiwenmo authored Nov 6, 2023
1 parent 628cc53 commit 0b9aac2
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.dinky.metadata.driver;

import org.dinky.assertion.Asserts;
import org.dinky.data.model.Column;
import org.dinky.data.model.Table;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.metadata.ast.Clickhouse20CreateTableStatement;
Expand All @@ -31,6 +33,8 @@

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -164,4 +168,98 @@ private SqlExplainResult checkDropTable(SQLDropTableStatement sqlStatement, Stri
public Map<String, String> getFlinkColumnTypeConversion() {
return new HashMap<>();
}

@Override
public List<Column> listColumns(String schemaName, String tableName) {
List<Column> columns = new ArrayList<>();
PreparedStatement preparedStatement = null;
ResultSet results = null;
IDBQuery dbQuery = getDBQuery();
String tableFieldsSql = dbQuery.columnsSql(schemaName, tableName);
try {
preparedStatement = conn.get().prepareStatement(tableFieldsSql);
results = preparedStatement.executeQuery();
ResultSetMetaData metaData = results.getMetaData();
List<String> columnList = new ArrayList<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
columnList.add(metaData.getColumnLabel(i));
}
while (results.next()) {
Column field = new Column();
String columnName = results.getString(dbQuery.columnName());
if (columnList.contains(dbQuery.columnKey())) {
String key = results.getString(dbQuery.columnKey());
field.setKeyFlag(Asserts.isNotNullString(key) && Asserts.isEqualsIgnoreCase(dbQuery.isPK(), key));
}
field.setName(columnName);
if (columnList.contains(dbQuery.columnType())) {
String columnType = results.getString(dbQuery.columnType());
if (columnType.indexOf("Nullable") >= 0) {
field.setNullable(true);
columnType = columnType.replaceAll("Nullable\\(", "").replaceAll("\\)", "");
}
if (columnType.contains("(")) {
String type = columnType.replaceAll("\\(.*\\)", "");
if (!columnType.contains(",")) {
Integer length = Integer.valueOf(columnType.replaceAll("\\D", ""));
field.setLength(length);
} else {
// some database does not have precision
if (dbQuery.precision() != null) {
// 例如浮点类型的长度和精度是一样的,decimal(10,2)
field.setLength(results.getInt(dbQuery.precision()));
}
}
field.setType(type);
} else {
field.setType(columnType);
}
}
if (columnList.contains(dbQuery.columnComment())
&& Asserts.isNotNull(results.getString(dbQuery.columnComment()))) {
String columnComment =
results.getString(dbQuery.columnComment()).replaceAll("\"|'", "");
field.setComment(columnComment);
}
if (columnList.contains(dbQuery.columnLength())) {
int length = results.getInt(dbQuery.columnLength());
if (!results.wasNull()) {
field.setLength(length);
}
}
if (columnList.contains(dbQuery.characterSet())) {
field.setCharacterSet(results.getString(dbQuery.characterSet()));
}
if (columnList.contains(dbQuery.collation())) {
field.setCollation(results.getString(dbQuery.collation()));
}
if (columnList.contains(dbQuery.columnPosition())) {
field.setPosition(results.getInt(dbQuery.columnPosition()));
}
if (columnList.contains(dbQuery.precision())) {
field.setPrecision(results.getInt(dbQuery.precision()));
}
if (columnList.contains(dbQuery.scale())) {
field.setScale(results.getInt(dbQuery.scale()));
}
if (columnList.contains(dbQuery.defaultValue())) {
field.setDefaultValue(results.getString(dbQuery.defaultValue()));
}
if (columnList.contains(dbQuery.autoIncrement())) {
field.setAutoIncrement(
Asserts.isEqualsIgnoreCase(results.getString(dbQuery.autoIncrement()), "auto_increment"));
}
if (columnList.contains(dbQuery.defaultValue())) {
field.setDefaultValue(results.getString(dbQuery.defaultValue()));
}
field.setJavaType(getTypeConvert().convert(field, config));
columns.add(field);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(preparedStatement, results);
}
return columns;
}
}

0 comments on commit 0b9aac2

Please sign in to comment.