Skip to content

Commit

Permalink
Misc code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Feb 26, 2024
1 parent be110b3 commit 98614ac
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 180 deletions.
33 changes: 10 additions & 23 deletions src/main/org/firebirdsql/ds/PooledConnectionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,28 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
// Methods from object
if (method.equals(TO_STRING)) {
return "Proxy for " + connection;
}
if (method.equals(EQUALS)) {
// Using parameter proxy (and not field) on purpose as field is
// nulled after closing
} else if (method.equals(EQUALS)) {
// Using parameter proxy (and not field) on purpose as field is nulled after closing
return proxy == args[0];
}
if (method.equals(HASH_CODE)) {
// Using parameter proxy (and not field) on purpose as field is
// nulled after closing
} else if (method.equals(HASH_CODE)) {
// Using parameter proxy (and not field) on purpose as field is nulled after closing
return System.identityHashCode(proxy);
}
// Other methods from object
if (method.getDeclaringClass().equals(Object.class)) {
} else if (method.getDeclaringClass().equals(Object.class)) {
// Other methods from object
try {
return method.invoke(connection, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}

// Methods from Connection or FirebirdConnection
if (method.equals(CONNECTION_IS_CLOSED)) {
} else if (method.equals(CONNECTION_IS_CLOSED)) {
return isClosed();
}
if (method.equals(RESET_KNOWN_CLIENT_INFO_PROPERTIES)) {
} else if (method.equals(RESET_KNOWN_CLIENT_INFO_PROPERTIES)) {
try {
method.invoke(connection, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
if (isClosed() && !method.equals(CONNECTION_CLOSE)) {
} else if (isClosed() && !method.equals(CONNECTION_CLOSE)) {
String message = forcedClose ? FORCIBLY_CLOSED_MESSAGE : CLOSED_MESSAGE;
throw new SQLNonTransientConnectionException(message, SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
}
Expand All @@ -116,9 +106,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
handleClose(true);
}
return null;
}

if (method.getDeclaringClass().equals(Connection.class)
} else if (method.getDeclaringClass().equals(Connection.class)
&& STATEMENT_CREATION_METHOD_NAMES.contains(method.getName())) {
Statement pstmt = (Statement) method.invoke(connection, args);
StatementHandler stmtHandler = new StatementHandler(this, pstmt);
Expand All @@ -127,7 +115,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
return stmtHandler.getProxy();
}

// All other methods
return method.invoke(connection, args);
} catch (InvocationTargetException ite) {
Expand Down
1 change: 1 addition & 0 deletions src/main/org/firebirdsql/gds/JaybirdErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public interface JaybirdErrorCodes {
/**
* @deprecated use {@link #jb_stmtInErrorRequireClose}
*/
@SuppressWarnings("java:S1845")
@Deprecated(forRemoval = true, since = "6")
int jb_stmtInErrorRequireCLose = jb_stmtInErrorRequireClose;
int jb_invalidTransactionStateTransition = 337248305;
Expand Down
2 changes: 1 addition & 1 deletion src/main/org/firebirdsql/gds/ng/FbExceptionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public SQLException toFlatSQLException() {
interestingExceptionInfo = info;
}

if (fullExceptionMessage.length() > 0) {
if (!fullExceptionMessage.isEmpty()) {
fullExceptionMessage.append("; ");
}
fullExceptionMessage.append(info.toMessage());
Expand Down
9 changes: 5 additions & 4 deletions src/main/org/firebirdsql/jdbc/FBResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
public class FBResultSet implements ResultSet, FirebirdResultSet, FBObjectListener.FetcherListener {

private static final String UNICODE_STREAM_NOT_SUPPORTED = "Unicode stream not supported";
private static final String TYPE_SQLXML = "SQLXML";

private final FBStatement fbStatement;
private FBFetcher fbFetcher;
Expand Down Expand Up @@ -1802,12 +1803,12 @@ public RowId getRowId(String columnLabel) throws SQLException {

@Override
public SQLXML getSQLXML(int columnIndex) throws SQLException {
throw typeNotSupported("SQLXML");
throw typeNotSupported(TYPE_SQLXML);
}

@Override
public SQLXML getSQLXML(String columnLabel) throws SQLException {
throw typeNotSupported("SQLXML");
throw typeNotSupported(TYPE_SQLXML);
}

/**
Expand Down Expand Up @@ -1893,12 +1894,12 @@ private void rowIdNotUpdatable() throws SQLException {

@Override
public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
throw typeNotSupported("SQLXML");
throw typeNotSupported(TYPE_SQLXML);
}

@Override
public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
throw typeNotSupported("SQLXML");
throw typeNotSupported(TYPE_SQLXML);
}

@Override
Expand Down
21 changes: 6 additions & 15 deletions src/main/org/firebirdsql/jdbc/FBStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,8 @@ public boolean getMoreResults(int mode) throws SQLException {
public void setFetchDirection(int direction) throws SQLException {
checkValidity();
switch (direction) {
case ResultSet.FETCH_FORWARD:
case ResultSet.FETCH_REVERSE:
case ResultSet.FETCH_UNKNOWN:
fetchDirection = direction;
break;
default:
throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_invalidFetchDirection)
case ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, ResultSet.FETCH_UNKNOWN -> fetchDirection = direction;
default -> throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_invalidFetchDirection)
.messageParameter(direction)
.toSQLException();
}
Expand Down Expand Up @@ -1125,7 +1120,7 @@ public final long executeLargeUpdate(String sql) throws SQLException {
public final long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
try (LockCloseable ignored = withLock()) {
if (execute(sql, autoGeneratedKeys)) {
throw new SQLNonTransientException("Update statement returned result set", SQL_STATE_INVALID_STMT_TYPE);
throw updateReturnedResultSet();
}
return getLargeUpdateCountMinZero();
}
Expand All @@ -1135,7 +1130,7 @@ public final long executeLargeUpdate(String sql, int autoGeneratedKeys) throws S
public final long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException {
try (LockCloseable ignored = withLock()) {
if (execute(sql, columnIndexes)) {
throw new SQLNonTransientException("Update statement returned result set", SQL_STATE_INVALID_STMT_TYPE);
throw updateReturnedResultSet();
}
return getLargeUpdateCountMinZero();
}
Expand All @@ -1145,7 +1140,7 @@ public final long executeLargeUpdate(String sql, int[] columnIndexes) throws SQL
public final long executeLargeUpdate(String sql, String[] columnNames) throws SQLException {
try (LockCloseable ignored = withLock()) {
if (execute(sql, columnNames)) {
throw new SQLNonTransientException("Update statement returned result set", SQL_STATE_INVALID_STMT_TYPE);
throw updateReturnedResultSet();
}
return getLargeUpdateCountMinZero();
}
Expand Down Expand Up @@ -1341,10 +1336,7 @@ private StatementResult determineInitialStatementResult(boolean hasResultSet, bo
@Override
public void statementStateChanged(FbStatement sender, StatementState newState, StatementState previousState) {
if (isUnexpectedSender(sender)) return;
switch (newState) {
case PREPARED:
break;
case EXECUTING:
if (newState == StatementState.EXECUTING) {
specialResult.clear();
sqlCountHolder = null;
currentStatementResult = StatementResult.NO_MORE_RESULTS;
Expand All @@ -1354,7 +1346,6 @@ public void statementStateChanged(FbStatement sender, StatementState newState, S
} catch (SQLException e) {
throw new AssertionError("Unexpected SQLException", e);
}
break;
}
}

Expand Down
23 changes: 9 additions & 14 deletions src/main/org/firebirdsql/jdbc/FBStatementFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,42 +148,37 @@ public boolean next() throws SQLException {

@Override
public boolean absolute(int row) throws SQLException {
notScrollable();
return false;
throw notScrollable();
}

@Override
public boolean first() throws SQLException {
notScrollable();
return false;
throw notScrollable();
}

@Override
public boolean last() throws SQLException {
notScrollable();
return false;
throw notScrollable();
}

@Override
public boolean previous() throws SQLException {
notScrollable();
return false;
throw notScrollable();
}

@Override
public boolean relative(int row) throws SQLException {
notScrollable();
return false;
throw notScrollable();
}

@Override
public void beforeFirst() throws SQLException {
notScrollable();
throw notScrollable();
}

@Override
public void afterLast() throws SQLException {
notScrollable();
throw notScrollable();
}

public void fetch() throws SQLException {
Expand Down Expand Up @@ -336,8 +331,8 @@ public int getFetchSize() {
return fetchSize;
}

private void notScrollable() throws SQLException {
throw FbExceptionBuilder.forNonTransientException(JaybirdErrorCodes.jb_operationNotAllowedOnForwardOnly)
private SQLException notScrollable() {
return FbExceptionBuilder.forNonTransientException(JaybirdErrorCodes.jb_operationNotAllowedOnForwardOnly)
.toSQLException();
}

Expand Down
78 changes: 41 additions & 37 deletions src/main/org/firebirdsql/jdbc/metadata/GetTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ private static GetTables createInstance(DbMetadataMediator mediator) {

@Override
MetadataQuery createGetTablesQuery(String tableNamePattern, Set<String> types) {
Clause tableNameClause = new Clause("RDB$RELATION_NAME", tableNamePattern);
List<Clause> clauses = new ArrayList<>(types.size());
StringBuilder queryBuilder = new StringBuilder(2000);
var tableNameClause = new Clause("RDB$RELATION_NAME", tableNamePattern);
var clauses = new ArrayList<Clause>(types.size());
var queryBuilder = new StringBuilder(2000);
String tableNameCondition = tableNameClause.getCondition("\nand ", "");
QUERY_PER_TYPE.entrySet().stream()
.filter(typeAndQuery -> types.contains(typeAndQuery.getKey()))
.map(Map.Entry::getValue)
.forEach(query -> {
if (queryBuilder.length() > 0) {
if (!queryBuilder.isEmpty()) {
queryBuilder.append("\nunion all\n");
}
queryBuilder.append(query).append(tableNameCondition);
Expand Down Expand Up @@ -266,9 +266,9 @@ private static GetTables createInstance(DbMetadataMediator mediator) {

@Override
MetadataQuery createGetTablesQuery(String tableNamePattern, Set<String> types) {
Clause tableNameClause = new Clause("RDB$RELATION_NAME", tableNamePattern);
var tableNameClause = new Clause("RDB$RELATION_NAME", tableNamePattern);

StringBuilder queryBuilder = new StringBuilder(1000).append(TABLE_COLUMNS_2_5);
var queryBuilder = new StringBuilder(1000).append(TABLE_COLUMNS_2_5);
List<String> params;
if (tableNameClause.hasCondition()) {
queryBuilder.append("\nwhere ").append(tableNameClause.getCondition(false));
Expand All @@ -279,37 +279,7 @@ MetadataQuery createGetTablesQuery(String tableNamePattern, Set<String> types) {

if (!types.containsAll(ALL_TYPES_2_5)) {
// Only construct conditions when we don't query for all
StringBuilder typeCondition = new StringBuilder(120);
if (types.contains(SYSTEM_TABLE) && types.contains(TABLE)) {
typeCondition.append("(rdb$relation_type in (0, 2, 3) or " + LEGACY_IS_TABLE + ")");
} else if (types.contains(SYSTEM_TABLE)) {
// We assume that external tables are never system and that virtual tables are always system
typeCondition.append("(rdb$relation_type in (0, 3) or " + LEGACY_IS_TABLE + ") and rdb$system_flag = 1");
} else if (types.contains(TABLE)) {
// We assume that external tables are never system and that virtual tables are always system
typeCondition.append("(rdb$relation_type in (0, 2) or " + LEGACY_IS_TABLE + ") and rdb$system_flag = 0");
}

if (types.contains(VIEW)) {
if (typeCondition.length() > 0) {
typeCondition.append(" or ");
}
// We assume (but don't check) that views are never system
typeCondition.append("(rdb$relation_type = 1 or " + LEGACY_IS_VIEW + ")");
}

if (types.contains(GLOBAL_TEMPORARY)) {
if (typeCondition.length() > 0) {
typeCondition.append(" or ");
}
typeCondition.append("rdb$relation_type in (4, 5)");
}

if (typeCondition.length() == 0) {
// Requested types are unknown, query nothing
typeCondition.append("1 = 0");
}

StringBuilder typeCondition = buildTypeCondition(types);
if (tableNameClause.hasCondition()) {
queryBuilder.append("\nand (").append(typeCondition).append(")");
} else {
Expand All @@ -321,6 +291,40 @@ MetadataQuery createGetTablesQuery(String tableNamePattern, Set<String> types) {
return new MetadataQuery(queryBuilder.toString(), params);
}

private static StringBuilder buildTypeCondition(Set<String> types) {
var typeCondition = new StringBuilder(120);
if (types.contains(SYSTEM_TABLE) && types.contains(TABLE)) {
typeCondition.append("(rdb$relation_type in (0, 2, 3) or " + LEGACY_IS_TABLE + ")");
} else if (types.contains(SYSTEM_TABLE)) {
// We assume that external tables are never system and that virtual tables are always system
typeCondition.append("(rdb$relation_type in (0, 3) or " + LEGACY_IS_TABLE + ") and rdb$system_flag = 1");
} else if (types.contains(TABLE)) {
// We assume that external tables are never system and that virtual tables are always system
typeCondition.append("(rdb$relation_type in (0, 2) or " + LEGACY_IS_TABLE + ") and rdb$system_flag = 0");
}

if (types.contains(VIEW)) {
if (!typeCondition.isEmpty()) {
typeCondition.append(" or ");
}
// We assume (but don't check) that views are never system
typeCondition.append("(rdb$relation_type = 1 or " + LEGACY_IS_VIEW + ")");
}

if (types.contains(GLOBAL_TEMPORARY)) {
if (!typeCondition.isEmpty()) {
typeCondition.append(" or ");
}
typeCondition.append("rdb$relation_type in (4, 5)");
}

if (typeCondition.isEmpty()) {
// Requested types are unknown, query nothing
typeCondition.append("1 = 0");
}
return typeCondition;
}

@Override
Set<String> allTableTypes() {
return ALL_TYPES_2_5;
Expand Down
Loading

0 comments on commit 98614ac

Please sign in to comment.