-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape schema and function name patterns if necessary
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-22725
- Loading branch information
Showing
3 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...rc/test/java/org/springframework/jdbc/core/metadata/GenericCallMetaDataProviderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters