Skip to content

Commit

Permalink
Improve MonetResultSetMetaData contructor by checking on valid arrays…
Browse files Browse the repository at this point in the history
… once, such that this does not need to be checked in methods for each result column.
  • Loading branch information
mvdvm committed Apr 3, 2024
1 parent 3a66680 commit ac20ce9
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions src/main/java/org/monetdb/jdbc/MonetResultSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,26 @@ final class MonetResultSetMetaData
schemas = header.getSchemaNames();
tables = header.getTableNames();
columns = header.getNames();
lengths = header.getColumnLengths();
types = header.getTypes();
lengths = header.getColumnLengths();
precisions = header.getColumnPrecisions();
scales = header.getColumnScales();
if (schemas == null) {
throw new IllegalArgumentException("Schemas may not be null!");
}
if (tables == null) {
throw new IllegalArgumentException("Tables may not be null!");
}
if (columns == null) {
throw new IllegalArgumentException("Columns may not be null!");
}
if (types == null) {
throw new IllegalArgumentException("MonetDB Types may not be null!");
}
if (lengths == null) {
throw new IllegalArgumentException("Lengths may not be null!");
}
// Note: the precisions and scales arrays are null when the statement is a PLAN, EXPLAIN or TRACE statement !!

colCount = columns.length;
if (columns.length != tables.length || columns.length != types.length ) {
Expand Down Expand Up @@ -394,22 +410,19 @@ public boolean isSigned(final int column) throws SQLException {
@Override
public int getColumnDisplaySize(final int column) throws SQLException {
checkColumnIndexValidity(column);
if (lengths != null) {
try {
int len = lengths[column - 1];
if (len == 0) {
final String monettype = types[column - 1];
// in case of inet it always has 0 as length. we need to correct it.
if ("inet".equals(monettype)) {
len = 18; // 128.127.126.125/24
}
try {
int len = lengths[column - 1];
if (len == 0) {
final String monettype = types[column - 1];
// in case of inet it always has 0 as length. we need to correct it.
if ("inet".equals(monettype)) {
len = 18; // 128.127.126.125/24
}
return len;
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
return len;
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
return 1;
}

/**
Expand Down Expand Up @@ -471,14 +484,11 @@ public String getCatalogName(final int column) throws SQLException {
@Override
public String getSchemaName(final int column) throws SQLException {
checkColumnIndexValidity(column);
if (schemas != null) {
try {
return schemas[column - 1];
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
try {
return schemas[column - 1];
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
return "";
}

/**
Expand All @@ -491,14 +501,11 @@ public String getSchemaName(final int column) throws SQLException {
@Override
public String getTableName(final int column) throws SQLException {
checkColumnIndexValidity(column);
if (tables != null) {
try {
return tables[column - 1];
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
try {
return tables[column - 1];
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
return "";
}

/**
Expand Down Expand Up @@ -669,7 +676,6 @@ public int getScale(final int column) throws SQLException {
switch (getColumnType(column)) {
case Types.DECIMAL:
case Types.NUMERIC:
{
// these data types may have a variable scale, max scale is 38
try {
// Special handling for: day_interval and sec_interval as they are
Expand All @@ -684,24 +690,23 @@ public int getScale(final int column) throws SQLException {
if (scales != null) {
return scales[column - 1];
}
return 0;
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
return 0;
}
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
if (scales != null) {
try {
try {
if (scales != null) {
return scales[column - 1];
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
// support microseconds, so scale 6
return 6; // 21:51:34.399753
} catch (IndexOutOfBoundsException e) {
throw MonetResultSet.newSQLInvalidColumnIndexException(column);
}
// support microseconds, so scale 6
return 6; // 21:51:34.399753

// All other types should return 0
// case Types.BIGINT:
Expand Down

0 comments on commit ac20ce9

Please sign in to comment.