From 152a79bf8c4192e0f7a0a4856d67df1ef57c70c7 Mon Sep 17 00:00:00 2001 From: serge-rider Date: Sat, 2 Nov 2024 11:02:17 +0100 Subject: [PATCH] dbeaver/dbeaver#23361 Determine resultset column type from data --- .../jdbc/driver/libsql/LibSqlResultSet.java | 7 ++-- .../libsql/LibSqlResultSetMetaData.java | 37 ++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSet.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSet.java index 0200851..a24f11f 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSet.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSet.java @@ -19,6 +19,7 @@ import com.dbeaver.jdbc.driver.libsql.client.LibSqlExecutionResult; import com.dbeaver.jdbc.model.AbstractJdbcResultSet; import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; import org.jkiss.utils.CommonUtils; import java.io.InputStream; @@ -31,19 +32,19 @@ public class LibSqlResultSet extends AbstractJdbcResultSet { - @NotNull + @Nullable private final LibSqlExecutionResult result; private transient int cursor = 0; private transient boolean closed; private transient boolean wasNull; private transient Map nameMap; - public LibSqlResultSet(@NotNull LibSqlStatement statement, @NotNull LibSqlExecutionResult result) { + public LibSqlResultSet(@NotNull LibSqlStatement statement, @Nullable LibSqlExecutionResult result) { super(statement, null); this.result = result; } - @NotNull + @Nullable public LibSqlExecutionResult getResult() { return result; } diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSetMetaData.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSetMetaData.java index 566a23a..45d8de7 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSetMetaData.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlResultSetMetaData.java @@ -16,14 +16,24 @@ */ package com.dbeaver.jdbc.driver.libsql; +import com.dbeaver.jdbc.driver.libsql.client.LibSqlExecutionResult; import com.dbeaver.jdbc.model.AbstractJdbcResultSetMetaData; import org.jkiss.code.NotNull; +import org.jkiss.utils.CommonUtils; import java.sql.SQLException; import java.sql.Types; public class LibSqlResultSetMetaData extends AbstractJdbcResultSetMetaData { + // We have json data. + // Thus we can distinguish strings, numbers and booleans + private enum ResultColumnDataType { + BOOLEAN, + NUMBER, + STRING, + } + @NotNull private final LibSqlResultSet resultSet; @@ -32,6 +42,19 @@ public LibSqlResultSetMetaData(@NotNull LibSqlResultSet resultSet) throws SQLExc this.resultSet = resultSet; } + private ResultColumnDataType getDataTypeFromData(int column) { + LibSqlExecutionResult result = resultSet.getResult(); + if (result != null && !CommonUtils.isEmpty(result.getRows())) { + Object columnValue = result.getRows().get(0)[column]; + if (columnValue instanceof Boolean) { + return ResultColumnDataType.BOOLEAN; + } else if (columnValue instanceof Number) { + return ResultColumnDataType.NUMBER; + } + } + return ResultColumnDataType.STRING; + } + @Override public int getColumnCount() throws SQLException { return resultSet.getResult() == null ? 0 : resultSet.getResult().getColumns().size(); @@ -109,12 +132,22 @@ public String getTableName(int column) throws SQLException { @Override public int getColumnType(int column) throws SQLException { - return Types.VARCHAR; + return switch (getDataTypeFromData(column)) { + case BOOLEAN -> Types.BOOLEAN; + case NUMBER -> Types.NUMERIC; + case STRING -> Types.VARCHAR; + default -> Types.OTHER; + }; } @Override public String getColumnTypeName(int column) throws SQLException { - return "VARCHAR"; + return switch (getDataTypeFromData(column)) { + case BOOLEAN -> "BOOLEAN"; + case NUMBER -> "NUMERIC"; + case STRING -> "VARCHAR"; + default -> "UNKNOWN"; + }; } @Override