New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using actual argument name as bind parameter on a single collection #1856
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
148 changes: 148 additions & 0 deletions
148
src/test/java/org/apache/ibatis/submitted/param_name_resolve/ActualParamNameTest.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,148 @@ | ||
/** | ||
* Copyright 2009-2020 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 | ||
* | ||
* http://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.apache.ibatis.submitted.param_name_resolve; | ||
|
||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.Reader; | ||
import java.sql.Connection; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
class ActualParamNameTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeAll | ||
static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
try (Reader reader = Resources | ||
.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml")) { | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
sqlSessionFactory.getConfiguration().addMapper(Mapper.class); | ||
} | ||
|
||
// populate in-memory database | ||
try (Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection(); | ||
Reader reader = Resources | ||
.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql")) { | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
} | ||
} | ||
|
||
@Test | ||
void testSingleListParameterWhenUseActualParamNameIsTrue() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
// use actual name | ||
{ | ||
long count = mapper.getUserCountUsingList(Arrays.asList(1, 2)); | ||
assertEquals(2, count); | ||
} | ||
// use 'collection' as alias | ||
{ | ||
long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2)); | ||
assertEquals(2, count); | ||
} | ||
// use 'list' as alias | ||
{ | ||
long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2)); | ||
assertEquals(2, count); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testSingleArrayParameterWhenUseActualParamNameIsTrue() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
// use actual name | ||
{ | ||
long count = mapper.getUserCountUsingArray(1, 2); | ||
assertEquals(2, count); | ||
} | ||
// use 'array' as alias | ||
{ | ||
long count = mapper.getUserCountUsingArrayWithAliasArray(1, 2); | ||
assertEquals(2, count); | ||
} | ||
} | ||
} | ||
|
||
interface Mapper { | ||
@Select({ | ||
"<script>", | ||
" select count(*) from users u where u.id in", | ||
" <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>", | ||
" #{item}", | ||
" </foreach>", | ||
"</script>" | ||
}) | ||
Long getUserCountUsingList(List<Integer> ids); | ||
|
||
@Select({ | ||
"<script>", | ||
" select count(*) from users u where u.id in", | ||
" <foreach item='item' index='index' collection='collection' open='(' separator=',' close=')'>", | ||
" #{item}", | ||
" </foreach>", | ||
"</script>" | ||
}) | ||
Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids); | ||
|
||
@Select({ | ||
"<script>", | ||
" select count(*) from users u where u.id in", | ||
" <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>", | ||
" #{item}", | ||
" </foreach>", | ||
"</script>" | ||
}) | ||
Long getUserCountUsingListWithAliasIsList(List<Integer> ids); | ||
|
||
@Select({ | ||
"<script>", | ||
" select count(*) from users u where u.id in", | ||
" <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>", | ||
" #{item}", | ||
" </foreach>", | ||
"</script>" | ||
}) | ||
Long getUserCountUsingArray(Integer... ids); | ||
|
||
@Select({ | ||
"<script>", | ||
" select count(*) from users u where u.id in", | ||
" <foreach item='item' index='index' collection='array' open='(' separator=',' close=')'>", | ||
" #{item}", | ||
" </foreach>", | ||
"</script>" | ||
}) | ||
Long getUserCountUsingArrayWithAliasArray(Integer... ids); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql
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,24 @@ | ||
-- | ||
-- Copyright 2009-2020 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 | ||
-- | ||
-- http://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. | ||
-- | ||
|
||
drop table users if exists; | ||
|
||
create table users ( | ||
id int, | ||
name varchar(20) | ||
); | ||
|
||
insert into users (id, name) values (1, 'User1'), (2, 'User2'), (3, 'User3'); |
36 changes: 36 additions & 0 deletions
36
src/test/java/org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml
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,36 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
|
||
Copyright 2009-2020 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 | ||
|
||
http://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. | ||
|
||
--> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
|
||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC" /> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.hsqldb.jdbcDriver"/> | ||
<property name="url" value="jdbc:hsqldb:mem:param_name_resolve"/> | ||
<property name="username" value="sa"/> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
</configuration> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this variable necessary? No test fails even if I removed it.
If it is necessary for some corner case, there should be a test.
Sorry if I missed something obvious... 😩😪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx! I will confirm my changes again at tomorrow or after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is necessary for keeping the backward-compatibility when
Configuration#useActualParamName
isfalse
. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Thank you for the update!
It would be simpler to store the value of
config.isUseActualParamName()
in a final field. i.e.Or, keep the
Configuration
instance and reference the value directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right! I've applied a your comment via ce14cec.