Skip to content

Commit

Permalink
dbeaver/dbeaver#23361 Determine resultset column type from data
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Nov 2, 2024
1 parent 84dc877 commit 152a79b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,19 +32,19 @@

public class LibSqlResultSet extends AbstractJdbcResultSet<LibSqlStatement, LibSqlResultSetMetaData> {

@NotNull
@Nullable
private final LibSqlExecutionResult result;
private transient int cursor = 0;
private transient boolean closed;
private transient boolean wasNull;
private transient Map<String, Integer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LibSqlStatement> {

// We have json data.
// Thus we can distinguish strings, numbers and booleans
private enum ResultColumnDataType {
BOOLEAN,
NUMBER,
STRING,
}

@NotNull
private final LibSqlResultSet resultSet;

Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 152a79b

Please sign in to comment.