Skip to content

Commit d947356

Browse files
committed
fix myql tinyint error
1 parent 79bbfe6 commit d947356

8 files changed

Lines changed: 119 additions & 63 deletions

File tree

chat2db-client/src/components/ConnectionEdit/config/dataSource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ export const dataSourceFormConfigs: IConnectionConfig[] = [
254254
{
255255
"key": "useInformationSchema",
256256
"value": "true"
257+
},
258+
{
259+
"key": "tinyInt1isBit",
260+
"value": "false"
257261
}
258262
],
259263
type: DatabaseTypeCode.MYSQL,

chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@ public List<Database> databases(Connection connection) {
3131
}
3232

3333

34+
private static String TABLES_SQL
35+
= "SELECT TABLE_SCHEMA, TABLE_NAME, ENGINE, VERSION, TABLE_ROWS, DATA_LENGTH, AUTO_INCREMENT, CREATE_TIME, UPDATE_TIME, TABLE_COLLATION, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '%s'";
36+
@Override
37+
public List<Table> tables(Connection connection, @NotEmpty String databaseName, String schemaName, String tableName) {
38+
String sql = String.format(TABLES_SQL, databaseName);
39+
if(StringUtils.isNotBlank(tableName)){
40+
sql += " AND TABLE_NAME = '" + tableName + "'";
41+
}
42+
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
43+
List<Table> tables = new ArrayList<>();
44+
while (resultSet.next()) {
45+
Table table = new Table();
46+
table.setDatabaseName(databaseName);
47+
table.setSchemaName(schemaName);
48+
table.setName(resultSet.getString("TABLE_NAME"));
49+
table.setEngine(resultSet.getString("ENGINE"));
50+
table.setRows(resultSet.getLong("TABLE_ROWS"));
51+
table.setDataLength(resultSet.getLong("DATA_LENGTH"));
52+
table.setCreateTime(resultSet.getString("CREATE_TIME"));
53+
table.setUpdateTime(resultSet.getString("UPDATE_TIME"));
54+
table.setCollate(resultSet.getString("TABLE_COLLATION"));
55+
table.setComment(resultSet.getString("TABLE_COMMENT"));
56+
tables.add(table);
57+
}
58+
return tables;
59+
});
60+
}
61+
62+
3463
@Override
3564
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
3665
@NotEmpty String tableName) {
@@ -70,12 +99,12 @@ public Function function(Connection connection, @NotEmpty String databaseName, S
7099
}
71100
return f;
72101
});
73-
String functionDDlSql =String.format("SHOW CREATE FUNCTION %s", functionName);
74-
SQLExecutor.getInstance().execute(connection,functionDDlSql, resultSet -> {
102+
String functionDDlSql = String.format("SHOW CREATE FUNCTION %s", functionName);
103+
SQLExecutor.getInstance().execute(connection, functionDDlSql, resultSet -> {
75104
if (resultSet.next()) {
76105
function.setFunctionBody(resultSet.getString("Create Function"));
77106
}
78-
} );
107+
});
79108
return function;
80109

81110
}
@@ -103,6 +132,7 @@ public List<Trigger> triggers(Connection connection, String databaseName, String
103132
});
104133
}
105134

135+
106136
@Override
107137
public Trigger trigger(Connection connection, @NotEmpty String databaseName, String schemaName,
108138
String triggerName) {
@@ -123,15 +153,15 @@ public Trigger trigger(Connection connection, @NotEmpty String databaseName, Str
123153
@Override
124154
public List<Procedure> procedures(Connection connection, String databaseName, String schemaName) {
125155
String sql = "SHOW PROCEDURE STATUS WHERE Db = DATABASE()";
126-
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
127-
ArrayList<Procedure> procedures = new ArrayList<>();
128-
while (resultSet.next()){
129-
Procedure procedure = new Procedure();
130-
procedure.setProcedureName(resultSet.getString("Name"));
131-
procedures.add(procedure);
132-
}
133-
return procedures;
134-
});
156+
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
157+
ArrayList<Procedure> procedures = new ArrayList<>();
158+
while (resultSet.next()) {
159+
Procedure procedure = new Procedure();
160+
procedure.setProcedureName(resultSet.getString("Name"));
161+
procedures.add(procedure);
162+
}
163+
return procedures;
164+
});
135165
}
136166

137167
@Override
@@ -214,7 +244,7 @@ private void setColumnSize(TableColumn column, String columnType) {
214244
}
215245
}
216246

217-
private static String VIEW_DDL_SQL="show create view %s";
247+
private static String VIEW_DDL_SQL = "show create view %s";
218248

219249
@Override
220250
public Table view(Connection connection, String databaseName, String schemaName, String viewName) {

chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlValueHandler.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ public class MysqlValueHandler extends DefaultValueHandler {
1717

1818
@Override
1919
public String getString(ResultSet rs, int index, boolean limitSize) throws SQLException {
20-
Object obj = rs.getObject(index);
21-
if (obj == null) {
22-
return null;
23-
}
24-
String columnTypeName = rs.getMetaData().getColumnTypeName(index);
25-
if (MysqlColumnTypeEnum.GEOMETRY.name().equalsIgnoreCase(columnTypeName)
26-
|| MysqlColumnTypeEnum.POINT.name().equalsIgnoreCase(columnTypeName)
27-
|| MysqlColumnTypeEnum.LINESTRING.name().equalsIgnoreCase(columnTypeName)
28-
|| MysqlColumnTypeEnum.POLYGON.name().equalsIgnoreCase(columnTypeName)
29-
|| MysqlColumnTypeEnum.MULTIPOINT.name().equalsIgnoreCase(columnTypeName)
30-
|| MysqlColumnTypeEnum.MULTILINESTRING.name().equalsIgnoreCase(columnTypeName)
31-
|| MysqlColumnTypeEnum.MULTIPOLYGON.name().equalsIgnoreCase(columnTypeName)
32-
|| MysqlColumnTypeEnum.GEOMETRYCOLLECTION.name().equalsIgnoreCase(columnTypeName)
33-
) {
34-
ValueHandler handler = VALUE_HANDLER_MAP.get(MysqlColumnTypeEnum.GEOMETRY.name());
35-
return handler.getString(rs, index, limitSize);
36-
} else {
37-
return super.getString(rs, index, limitSize);
20+
try {
21+
Object obj = rs.getObject(index);
22+
if (obj == null) {
23+
return null;
24+
}
25+
String columnTypeName = rs.getMetaData().getColumnTypeName(index);
26+
if (MysqlColumnTypeEnum.GEOMETRY.name().equalsIgnoreCase(columnTypeName)
27+
|| MysqlColumnTypeEnum.POINT.name().equalsIgnoreCase(columnTypeName)
28+
|| MysqlColumnTypeEnum.LINESTRING.name().equalsIgnoreCase(columnTypeName)
29+
|| MysqlColumnTypeEnum.POLYGON.name().equalsIgnoreCase(columnTypeName)
30+
|| MysqlColumnTypeEnum.MULTIPOINT.name().equalsIgnoreCase(columnTypeName)
31+
|| MysqlColumnTypeEnum.MULTILINESTRING.name().equalsIgnoreCase(columnTypeName)
32+
|| MysqlColumnTypeEnum.MULTIPOLYGON.name().equalsIgnoreCase(columnTypeName)
33+
|| MysqlColumnTypeEnum.GEOMETRYCOLLECTION.name().equalsIgnoreCase(columnTypeName)
34+
) {
35+
ValueHandler handler = VALUE_HANDLER_MAP.get(MysqlColumnTypeEnum.GEOMETRY.name());
36+
return handler.getString(rs, index, limitSize);
37+
} else {
38+
return super.getString(rs, index, limitSize);
39+
}
40+
}catch (Exception e){
41+
return rs.getString(index);
3842
}
3943
}
4044

chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/mysql.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"key": "zeroDateTimeBehavior",
1818
"value": "convertToNull",
1919
"required": false
20+
},
21+
{
22+
"key": "tinyInt1isBit",
23+
"value": "false",
24+
"required": false
2025
}
2126
]
2227
},
@@ -34,6 +39,11 @@
3439
"key": "characterEncoding",
3540
"value": "UTF-8",
3641
"required": false
42+
},
43+
{
44+
"key": "tinyInt1isBit",
45+
"value": "false",
46+
"required": false
3747
}
3848
]
3949
}

chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
1515

1616
BIT("BIT", true, false, true, false, false, false, true, true, false, false),
1717

18-
TINYINT("TINYINT", true, false, true, true, false, false, true, true, false, false),
18+
TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false),
1919

20-
TINYINT_UNSIGNED("TINYINT UNSIGNED", true, false, true, true, false, false, true, true, false, false),
20+
TINYINT_UNSIGNED("TINYINT UNSIGNED", false, false, true, true, false, false, true, true, false, false),
2121

2222
SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false),
2323

@@ -284,7 +284,7 @@ private String buildDataType(TableColumn column, MysqlColumnTypeEnum type) {
284284
}
285285

286286

287-
if (Arrays.asList(DECIMAL, FLOAT, DOUBLE,TINYINT).contains(type)) {
287+
if (Arrays.asList(DECIMAL, FLOAT, DOUBLE).contains(type)) {
288288
if (column.getColumnSize() == null || column.getDecimalDigits() == null) {
289289
return columnType;
290290
}
@@ -296,7 +296,7 @@ private String buildDataType(TableColumn column, MysqlColumnTypeEnum type) {
296296
}
297297
}
298298

299-
if (Arrays.asList(DECIMAL_UNSIGNED, FLOAT_UNSIGNED, DECIMAL_UNSIGNED,TINYINT_UNSIGNED).contains(type)) {
299+
if (Arrays.asList(DECIMAL_UNSIGNED, FLOAT_UNSIGNED, DECIMAL_UNSIGNED).contains(type)) {
300300
if (column.getColumnSize() == null || column.getDecimalDigits() == null) {
301301
return columnType;
302302
}

chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/jdbc/DefaultValueHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public class DefaultValueHandler implements ValueHandler {
1818

1919
@Override
2020
public String getString(ResultSet rs, int index, boolean limitSize) throws SQLException {
21-
Object obj = rs.getObject(index);
22-
if (obj == null) {
23-
return null;
24-
}
2521
try {
22+
Object obj = rs.getObject(index);
23+
if (obj == null) {
24+
return null;
25+
}
2626
if (obj instanceof BigDecimal bigDecimal) {
2727
return bigDecimal.toPlainString();
2828
} else if (obj instanceof Double d) {
@@ -41,8 +41,8 @@ public String getString(ResultSet rs, int index, boolean limitSize) throws SQLEx
4141
return obj.toString();
4242
}
4343
} catch (Exception e) {
44-
log.warn("Failed to parse number:{},{}", index, obj, e);
45-
return obj.toString();
44+
log.warn("Failed to parse number:{},", index, e);
45+
return rs.getString(index);
4646
}
4747
}
4848

chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Table.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,13 @@ public class Table implements Serializable {
9898

9999

100100
private String tablespace;
101+
102+
private Long rows;
103+
104+
private Long dataLength;
105+
106+
private String createTime;
107+
108+
private String updateTime;
101109
}
102110

chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/sql/SQLExecutor.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -412,28 +412,28 @@ public List<Table> tables(Connection connection, String databaseName, String sch
412412
DatabaseMetaData metadata = connection.getMetaData();
413413
ResultSet resultSet = metadata.getTables(databaseName, schemaName, tableName,
414414
types);
415-
// If connection is mysql
416-
if ("MySQL".equalsIgnoreCase(metadata.getDatabaseProductName())) {
417-
// Get the comment of mysql table
418-
List<Table> tables = ResultSetUtils.toObjectList(resultSet, Table.class);
419-
if (CollectionUtils.isNotEmpty(tables)) {
420-
for (Table table : tables) {
421-
String sql = "show table status where name = '" + table.getName() + "'";
422-
try (Statement stmt = connection.createStatement()) {
423-
boolean query = stmt.execute(sql);
424-
if (query) {
425-
try (ResultSet rs = stmt.getResultSet();) {
426-
while (rs.next()) {
427-
table.setComment(rs.getString("Comment"));
428-
}
429-
}
430-
}
431-
}
432-
}
433-
434-
return tables;
435-
}
436-
}
415+
// // If connection is mysql
416+
// if ("MySQL".equalsIgnoreCase(metadata.getDatabaseProductName())) {
417+
// // Get the comment of mysql table
418+
// List<Table> tables = ResultSetUtils.toObjectList(resultSet, Table.class);
419+
// if (CollectionUtils.isNotEmpty(tables)) {
420+
// for (Table table : tables) {
421+
// String sql = "show table status where name = '" + table.getName() + "'";
422+
// try (Statement stmt = connection.createStatement()) {
423+
// boolean query = stmt.execute(sql);
424+
// if (query) {
425+
// try (ResultSet rs = stmt.getResultSet();) {
426+
// while (rs.next()) {
427+
// table.setComment(rs.getString("Comment"));
428+
// }
429+
// }
430+
// }
431+
// }
432+
// }
433+
//
434+
// return tables;
435+
// }
436+
// }
437437
return ResultSetUtils.toObjectList(resultSet, Table.class);
438438
} catch (SQLException e) {
439439
throw new RuntimeException(e);

0 commit comments

Comments
 (0)