Browse Source
The JDBC API that retrieves a proedure or a function allows to specify patterns for the schema and the procedure name. So far, we've called this API with the value as is, which does not work if either contains a wildcard characters that need to be escaped. This commit updates GenericCallMetadataProvider to escape, if necessary, the schema or procedure name using the search string escape from the database metadata. Closes gh-22725pull/31679/head
3 changed files with 107 additions and 15 deletions
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.jdbc.core.metadata; |
||||
|
||||
import java.sql.DatabaseMetaData; |
||||
import java.sql.SQLException; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
||||
import static org.mockito.BDDMockito.given; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.verify; |
||||
|
||||
/** |
||||
* Tests for {@link GenericCallMetaDataProvider}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
class GenericCallMetaDataProviderTests { |
||||
|
||||
private final DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class); |
||||
|
||||
@Test |
||||
void procedureNameWithPatternIsEscape() throws SQLException { |
||||
given(this.databaseMetaData.getSearchStringEscape()).willReturn("@"); |
||||
GenericCallMetaDataProvider provider = new GenericCallMetaDataProvider(this.databaseMetaData); |
||||
given(this.databaseMetaData.getProcedures(null, null, "MY@_PROCEDURE")) |
||||
.willThrow(new IllegalStateException("Expected")); |
||||
assertThatIllegalStateException().isThrownBy(() -> provider.initializeWithProcedureColumnMetaData( |
||||
this.databaseMetaData, null, null, "my_procedure")); |
||||
verify(this.databaseMetaData).getProcedures(null, null, "MY@_PROCEDURE"); |
||||
} |
||||
|
||||
@Test |
||||
void schemaNameWithPatternIsEscape() throws SQLException { |
||||
given(this.databaseMetaData.getSearchStringEscape()).willReturn("@"); |
||||
GenericCallMetaDataProvider provider = new GenericCallMetaDataProvider(this.databaseMetaData); |
||||
given(this.databaseMetaData.getProcedures(null, "MY@_SCHEMA", "TEST")) |
||||
.willThrow(new IllegalStateException("Expected")); |
||||
assertThatIllegalStateException().isThrownBy(() -> provider.initializeWithProcedureColumnMetaData( |
||||
this.databaseMetaData, null, "my_schema", "test")); |
||||
verify(this.databaseMetaData).getProcedures(null, "MY@_SCHEMA", "TEST"); |
||||
} |
||||
|
||||
@Test |
||||
void nameIsNotEscapedIfEscapeCharacterIsNotAvailable() throws SQLException { |
||||
given(this.databaseMetaData.getSearchStringEscape()).willReturn(null); |
||||
GenericCallMetaDataProvider provider = new GenericCallMetaDataProvider(this.databaseMetaData); |
||||
given(this.databaseMetaData.getProcedures(null, "MY_SCHEMA", "MY_TEST")) |
||||
.willThrow(new IllegalStateException("Expected")); |
||||
assertThatIllegalStateException().isThrownBy(() -> provider.initializeWithProcedureColumnMetaData( |
||||
this.databaseMetaData, null, "my_schema", "my_test")); |
||||
verify(this.databaseMetaData).getProcedures(null, "MY_SCHEMA", "MY_TEST"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue