Skip to content
Merged
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
@@ -0,0 +1,68 @@
package com.github.magnusp.libsql.jdbc;

import com.github.magnusp.libsql.hrana.codec.Col;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

final class DeclaredType {

private static final Pattern COLUMN_TYPENAME = Pattern.compile("([^\\(]*)");
private static final Pattern COLUMN_TYPECAST =
Pattern.compile("cast\\(.*?\\s+as\\s+(.*?)\\s*\\)");
private static final Pattern COLUMN_PRECISION = Pattern.compile(".*?\\((.*?)\\)");

private final String declType;

private DeclaredType(String declType) {
this.declType = declType;
}

static DeclaredType of(Col col) {
String declType = col.decltype();
if (declType == null) {
String name = col.name();
if (name != null) {
Matcher matcher = COLUMN_TYPECAST.matcher(name);
declType = matcher.find() ? matcher.group(1) : null;
}
}
return new DeclaredType(declType);
}

boolean isDeclared() {
return declType != null;
}

String typeName() {
if (declType != null) {
Matcher matcher = COLUMN_TYPENAME.matcher(declType);
matcher.find();
return matcher.group(1).toUpperCase(Locale.ENGLISH);
}
return "NUMERIC";
}

int precision() {
if (declType == null) {
return 0;
}
Matcher matcher = COLUMN_PRECISION.matcher(declType);
return matcher.find() ? Integer.parseInt(matcher.group(1).split(",")[0].trim()) : 0;
}

int scale() {
if (declType == null) {
return 0;
}
Matcher matcher = COLUMN_PRECISION.matcher(declType);
if (matcher.find()) {
String[] parts = matcher.group(1).split(",");
if (parts.length == 2) {
return Integer.parseInt(parts[1].trim());
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LibsqlResultSetMetaData implements ResultSetMetaData {

private static final Pattern COLUMN_TYPENAME = Pattern.compile("([^\\(]*)");
private static final Pattern COLUMN_TYPECAST =
Pattern.compile("cast\\(.*?\\s+as\\s+(.*?)\\s*\\)");
private static final Pattern COLUMN_PRECISION = Pattern.compile(".*?\\((.*?)\\)");

private final List<Col> cols;
private final List<Row> rows;
private final String sql;
Expand Down Expand Up @@ -104,8 +97,9 @@ public int isNullable(int column) throws SQLException {

@Override
public boolean isSigned(int column) throws SQLException {
if (declType(column) != null) {
return switch (getColumnTypeName(column)) {
DeclaredType declared = DeclaredType.of(getCol(column));
if (declared.isDeclared()) {
return switch (declared.typeName()) {
case "NUMERIC", "INTEGER", "REAL" -> true;
default -> false;
};
Expand Down Expand Up @@ -143,28 +137,12 @@ public String getSchemaName(int column) throws SQLException {

@Override
public int getPrecision(int column) throws SQLException {
String declType = declType(column);
if (declType == null) {
return 0;
}
Matcher matcher = COLUMN_PRECISION.matcher(declType);
return matcher.find() ? Integer.parseInt(matcher.group(1).split(",")[0].trim()) : 0;
return DeclaredType.of(getCol(column)).precision();
}

@Override
public int getScale(int column) throws SQLException {
String declType = declType(column);
if (declType == null) {
return 0;
}
Matcher matcher = COLUMN_PRECISION.matcher(declType);
if (matcher.find()) {
String[] parts = matcher.group(1).split(",");
if (parts.length == 2) {
return Integer.parseInt(parts[1].trim());
}
}
return 0;
return DeclaredType.of(getCol(column)).scale();
}

@Override
Expand All @@ -189,14 +167,7 @@ public int getColumnType(int column) throws SQLException {

@Override
public String getColumnTypeName(int column) throws SQLException {
getCol(column);
String declType = declType(column);
if (declType != null) {
Matcher matcher = COLUMN_TYPENAME.matcher(declType);
matcher.find();
return matcher.group(1).toUpperCase(Locale.ENGLISH);
}
return "NUMERIC";
return DeclaredType.of(getCol(column)).typeName();
}

@Override
Expand All @@ -223,19 +194,6 @@ public String getColumnClassName(int column) throws SQLException {
return XerialColumnType.className(runtimeValue(column));
}

private String declType(int column) throws SQLException {
Col col = getCol(column);
String declType = col.decltype();
if (declType == null) {
String name = col.name();
if (name != null) {
Matcher matcher = COLUMN_TYPECAST.matcher(name);
declType = matcher.find() ? matcher.group(1) : null;
}
}
return declType;
}

private String columnName(int column) throws SQLException {
String name = getCol(column).name();
return name != null ? name.toLowerCase(Locale.ROOT) : "";
Expand Down
Loading