From 64c8d9236065c15a225fd7880cf5e389333a1a51 Mon Sep 17 00:00:00 2001 From: zhaoxi Date: Fri, 8 Dec 2017 11:03:55 +0800 Subject: [PATCH 1/3] java8 inner interface, do need static --- .../apache/ibatis/scripting/xmltags/ExpressionEvaluator.java | 2 +- .../ibatis/builder/xml/dynamic/ExpressionEvaluatorTest.java | 2 +- .../ibatis/submitted/usesjava8/default_method/Mapper.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/ibatis/scripting/xmltags/ExpressionEvaluator.java b/src/main/java/org/apache/ibatis/scripting/xmltags/ExpressionEvaluator.java index be6d4167f74..44138df9675 100644 --- a/src/main/java/org/apache/ibatis/scripting/xmltags/ExpressionEvaluator.java +++ b/src/main/java/org/apache/ibatis/scripting/xmltags/ExpressionEvaluator.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2017 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. diff --git a/src/test/java/org/apache/ibatis/builder/xml/dynamic/ExpressionEvaluatorTest.java b/src/test/java/org/apache/ibatis/builder/xml/dynamic/ExpressionEvaluatorTest.java index 6df59332b25..9a8525747cb 100644 --- a/src/test/java/org/apache/ibatis/builder/xml/dynamic/ExpressionEvaluatorTest.java +++ b/src/test/java/org/apache/ibatis/builder/xml/dynamic/ExpressionEvaluatorTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2017 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. diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java index da394be3c37..7e3c9156ef7 100644 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java @@ -30,7 +30,7 @@ default User defaultGetUser(Object... args) { return getUserById((Integer) args[0]); } - static interface SubMapper extends Mapper { + interface SubMapper extends Mapper { default User defaultGetUser(Object... args) { return getUserByIdAndName((String) args[0], (Integer) args[1]); } From 1ca310f6a67578d76fee2e17598beedb735bd71e Mon Sep 17 00:00:00 2001 From: zhaoxi1988 Date: Fri, 8 Dec 2017 17:57:06 +0800 Subject: [PATCH 2/3] Remove Blank Lines --- .../java/org/apache/ibatis/parsing/XNode.java | 12 ++ .../scripting/xmltags/XMLScriptBuilder.java | 3 +- .../apache/ibatis/session/Configuration.java | 9 + .../usesjava8/default_method/CreateDB.sql | 24 --- .../default_method/DefaultMethodTest.java | 92 ---------- .../usesjava8/default_method/Mapper.java | 39 ----- .../default_method/PackageMapper.java | 27 --- .../usesjava8/default_method/User.java | 38 ---- .../default_method/mybatis-config.xml | 44 ----- .../usesjava8/keycolumn/CreateDB.sql | 30 ---- .../usesjava8/keycolumn/InsertMapper.java | 32 ---- .../usesjava8/keycolumn/InsertMapper.xml | 28 --- .../usesjava8/keycolumn/InsertTest.java | 132 -------------- .../submitted/usesjava8/keycolumn/Name.java | 40 ----- .../multiple_resultsets/CreateDB.sql | 83 --------- .../usesjava8/multiple_resultsets/Mapper.java | 26 --- .../usesjava8/multiple_resultsets/Mapper.xml | 40 ----- .../MultipleResultTest.java | 135 -------------- .../multiple_resultsets/OrderDetail.java | 56 ------ .../multiple_resultsets/OrderHeader.java | 39 ----- .../usesjava8/postgres_genkeys/CreateDB.sql | 34 ---- .../usesjava8/postgres_genkeys/Mapper.java | 31 ---- .../PostgresGeneratedKeysTest.java | 116 ------------ .../usesjava8/postgres_genkeys/Section.java | 39 ----- .../usesjava8/postgres_genkeys/User.java | 38 ---- .../usesjava8/refcursor/CreateDB.sql | 102 ----------- .../submitted/usesjava8/refcursor/Order.java | 42 ----- .../usesjava8/refcursor/OrderDetail.java | 47 ----- .../usesjava8/refcursor/OrdersMapper.java | 28 --- .../usesjava8/refcursor/OrdersMapper.xml | 57 ------ .../usesjava8/refcursor/RefCursorTest.java | 165 ------------------ .../use_actual_param_name/CreateDB.sql | 25 --- .../use_actual_param_name/Mapper.java | 34 ---- .../use_actual_param_name/Mapper.xml | 41 ----- .../UseActualParamNameTest.java | 106 ----------- .../usesjava8/use_actual_param_name/User.java | 38 ---- .../use_actual_param_name/mybatis-config.xml | 42 ----- 37 files changed, 23 insertions(+), 1891 deletions(-) delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java delete mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml diff --git a/src/main/java/org/apache/ibatis/parsing/XNode.java b/src/main/java/org/apache/ibatis/parsing/XNode.java index 3423dd93380..9c10ae5187f 100644 --- a/src/main/java/org/apache/ibatis/parsing/XNode.java +++ b/src/main/java/org/apache/ibatis/parsing/XNode.java @@ -136,6 +136,18 @@ public String getStringBody(String def) { } } + public String getStringBody(String def, boolean removeBlankLines){ + if(body == null){ + return def; + } else { + if (removeBlankLines) { + return body.replaceAll("(\\n|\\r\\n)", " ").replaceAll("\\t"," "); + } else { + return body; + } + } + } + public Boolean getBooleanBody() { return getBooleanBody(null); } diff --git a/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java b/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java index 8496b8476c6..749a4300727 100644 --- a/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java +++ b/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java @@ -61,12 +61,13 @@ public SqlSource parseScriptNode() { } List parseDynamicTags(XNode node) { + final boolean compactedSQL = configuration.isCompactedSQL(); List contents = new ArrayList(); NodeList children = node.getNode().getChildNodes(); for (int i = 0; i < children.getLength(); i++) { XNode child = node.newXNode(children.item(i)); if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) { - String data = child.getStringBody(""); + String data = child.getStringBody("", compactedSQL); TextSqlNode textSqlNode = new TextSqlNode(data); if (textSqlNode.isDynamic()) { contents.add(textSqlNode); diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index f2a60fe03f7..86c410aa2dd 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -109,6 +109,7 @@ public class Configuration { protected boolean callSettersOnNulls; protected boolean useActualParamName = true; protected boolean returnInstanceForEmptyRow; + protected boolean compactedSQL = false; protected String logPrefix; protected Class logImpl; @@ -259,6 +260,14 @@ public void setReturnInstanceForEmptyRow(boolean returnEmptyInstance) { this.returnInstanceForEmptyRow = returnEmptyInstance; } + public boolean isCompactedSQL() { + return compactedSQL; + } + + public void setCompactedSQL(boolean compactedSQL) { + this.compactedSQL = compactedSQL; + } + public String getDatabaseId() { return databaseId; } diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql deleted file mode 100644 index 2354824ab88..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql +++ /dev/null @@ -1,24 +0,0 @@ --- --- Copyright 2009-2016 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'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java deleted file mode 100644 index 52eada1bc5a..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.default_method; - -import static org.junit.Assert.*; - -import java.io.Reader; -import java.sql.Connection; - -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.apache.ibatis.submitted.usesjava8.default_method.Mapper.SubMapper; -import org.junit.BeforeClass; -import org.junit.Test; - -public class DefaultMethodTest { - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // create an SqlSessionFactory - Reader reader = Resources.getResourceAsReader( - "org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml"); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); - reader.close(); - - // populate in-memory database - SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - reader = Resources.getResourceAsReader( - "org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql"); - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - conn.close(); - reader.close(); - session.close(); - } - - @Test - public void shouldInvokeDefaultMethod() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - User user = mapper.defaultGetUser(1); - assertEquals("User1", user.getName()); - } finally { - sqlSession.close(); - } - } - - @Test - public void shouldInvokeDefaultMethodOfSubclass() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - SubMapper mapper = sqlSession.getMapper(SubMapper.class); - User user = mapper.defaultGetUser("User1", 1); - assertEquals("User1", user.getName()); - } finally { - sqlSession.close(); - } - } - - @Test - public void shouldInvokeDefaultMethodOfPackagePrivateMapper() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - PackageMapper mapper = sqlSession.getMapper(PackageMapper.class); - User user = mapper.defaultGetUser(1); - assertEquals("User1", user.getName()); - } finally { - sqlSession.close(); - } - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java deleted file mode 100644 index 7e3c9156ef7..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.default_method; - -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; - -public interface Mapper { - - @Select("select * from users where id = #{id}") - User getUserById(Integer id); - - @Select("select * from users where id = #{id} and name = #{name}") - User getUserByIdAndName(@Param("name") String name, @Param("id") Integer id); - - default User defaultGetUser(Object... args) { - return getUserById((Integer) args[0]); - } - - interface SubMapper extends Mapper { - default User defaultGetUser(Object... args) { - return getUserByIdAndName((String) args[0], (Integer) args[1]); - } - } - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java deleted file mode 100644 index 7ad681c113e..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.default_method; - -import org.apache.ibatis.annotations.Select; - -interface PackageMapper { - @Select("select * from users where id = #{id}") - User getUserById(Integer id); - - default User defaultGetUser(Object... args) { - return getUserById((Integer) args[0]); - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java deleted file mode 100644 index e38de2200cf..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2009-2016 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.usesjava8.default_method; - -public class User { - - private Integer id; - private String name; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml deleted file mode 100644 index 5f5e7307a4b..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql deleted file mode 100644 index 510559028e5..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql +++ /dev/null @@ -1,30 +0,0 @@ --- --- Copyright 2009-2017 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. --- - -CREATE SCHEMA mbtest; - -CREATE TABLE mbtest.test_identity -( - first_name character varying(30), - last_name character varying(30), - name_id serial NOT NULL, - CONSTRAINT test_identity_pkey PRIMARY KEY (name_id) -) -WITH ( - OIDS=FALSE -); - -ALTER TABLE mbtest.test_identity OWNER TO postgres; diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java deleted file mode 100644 index 443c33ae6d3..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.keycolumn; - -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Options; - -public interface InsertMapper { - - @Insert({ - "insert into mbtest.test_identity", - "(first_name, last_name)", - "values(#{firstName,jdbcType=VARCHAR}, #{lastName,jdbcType=VARCHAR})" - }) - @Options(keyProperty="id", useGeneratedKeys=true, keyColumn="name_id") - int insertNameAnnotated(Name name); - - int insertNameMapped(Name name); -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml deleted file mode 100644 index 85e08a4032f..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - insert into mbtest.test_identity - (first_name, last_name) - values(#{firstName,jdbcType=VARCHAR}, #{lastName,jdbcType=VARCHAR}) - - \ No newline at end of file diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java deleted file mode 100644 index 031129c63ea..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.keycolumn; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.Reader; -import java.nio.file.Paths; -import java.sql.Connection; -import java.util.Collections; -import java.util.List; - -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.executor.BatchResult; -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.ibatis.test.EmbeddedPostgresqlTests; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; -import ru.yandex.qatools.embed.postgresql.util.SocketUtil; - -/** - * @author Jeff Butler - */ -@Category(EmbeddedPostgresqlTests.class) -public class InsertTest { - - private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // Launch PostgreSQL server. Download / unarchive if necessary. - String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "keycolumn", "postgres", "root", Collections.emptyList()); - - Configuration configuration = new Configuration(); - Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( - "org.postgresql.Driver", url, null)); - configuration.setEnvironment(environment); - configuration.addMapper(InsertMapper.class); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); - - try (SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql")) { - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - } - } - - @AfterClass - public static void tearDown() { - postgres.stop(); - } - - @Test - public void testInsertAnnotated() throws Exception { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); - Name name = new Name(); - name.setFirstName("Fred"); - name.setLastName("Flintstone"); - - int rows = mapper.insertNameAnnotated(name); - - assertNotNull(name.getId()); - assertEquals(1, rows); - } - } - - @Test - public void testInsertMapped() throws Exception { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); - Name name = new Name(); - name.setFirstName("Fred"); - name.setLastName("Flintstone"); - - int rows = mapper.insertNameMapped(name); - - assertNotNull(name.getId()); - assertEquals(1, rows); - } - } - - @Test - public void testInsertMappedBatch() throws Exception { - try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { - InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); - Name name = new Name(); - name.setFirstName("Fred"); - name.setLastName("Flintstone"); - mapper.insertNameMapped(name); - Name name2 = new Name(); - name2.setFirstName("Wilma"); - name2.setLastName("Flintstone"); - mapper.insertNameMapped(name2); - List batchResults = sqlSession.flushStatements(); - assertNotNull(name.getId()); - assertNotNull(name2.getId()); - assertEquals(1, batchResults.size()); - } - } - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java deleted file mode 100644 index 1fb0d4a6376..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.keycolumn; - -public class Name { - private Integer id; - private String firstName; - private String lastName; - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - public String getFirstName() { - return firstName; - } - public void setFirstName(String firstName) { - this.firstName = firstName; - } - public String getLastName() { - return lastName; - } - public void setLastName(String lastName) { - this.lastName = lastName; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql deleted file mode 100644 index d5f5bf76a7a..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql +++ /dev/null @@ -1,83 +0,0 @@ --- --- Copyright 2009-2017 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. --- - -CREATE SCHEMA mbtest; - -CREATE TABLE mbtest.order_detail -( - order_id integer NOT NULL, - line_number integer NOT NULL, - quantity integer NOT NULL, - item_description character varying(50) NOT NULL, - CONSTRAINT order_detail_pkey PRIMARY KEY (order_id, line_number) -) -WITH ( - OIDS=FALSE -); - -ALTER TABLE mbtest.order_detail OWNER TO postgres; - -CREATE TABLE mbtest.order_header -( - order_id integer NOT NULL, - cust_name character varying(50) NOT NULL, - CONSTRAINT order_header_pkey PRIMARY KEY (order_id) -) -WITH ( - OIDS=FALSE -); - -ALTER TABLE mbtest.order_header OWNER TO postgres; - -INSERT INTO mbtest.order_header(order_id, cust_name) - VALUES (1, 'Fred'); -INSERT INTO mbtest.order_header(order_id, cust_name) - VALUES (2, 'Barney'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 1, 1, 'Pen'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 2, 3, 'Pencil'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 3, 2, 'Notepad'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 1, 1, 'Compass'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 2, 1, 'Protractor'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 3, 2, 'Pencil'); - --- @DELIMITER | - -CREATE OR REPLACE FUNCTION mbtest.get_order(order_number integer) - RETURNS refcursor AS -$BODY$ -DECLARE - mycurs refcursor; -BEGIN - open mycurs for select a.*, b.* - from mbtest.order_header a join mbtest.order_detail b - on a.order_id = b.order_id - where a.order_id = ORDER_NUMBER; - return mycurs; -END; -$BODY$ - LANGUAGE plpgsql VOLATILE - COST 100| - --- @DELIMITER ; - -ALTER FUNCTION mbtest.get_order(integer) OWNER TO postgres; - diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java deleted file mode 100644 index dce276cae2f..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.multiple_resultsets; - -import java.util.List; - -public interface Mapper { - - List> getUsersAndGroups(); - - List> multiResultsWithUpdate(); - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml deleted file mode 100644 index 8836266d516..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java deleted file mode 100644 index 91047b9bfa8..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.multiple_resultsets; - -import java.io.IOException; -import java.io.Reader; -import java.nio.file.Paths; -import java.sql.Connection; -import java.util.Collections; -import java.util.List; - -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.ibatis.test.EmbeddedPostgresqlTests; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; -import ru.yandex.qatools.embed.postgresql.util.SocketUtil; - -/* - * This class contains tests for multiple results. - * It is based on Jeff's ref cursor tests. - */ -@Category(EmbeddedPostgresqlTests.class) -public class MultipleResultTest { - - private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // Launch PostgreSQL server. Download / unarchive if necessary. - String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "multiple_resultsets", "postgres", "root", Collections.emptyList()); - - Configuration configuration = new Configuration(); - Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( - "org.postgresql.Driver", url, null)); - configuration.setEnvironment(environment); - configuration.setMapUnderscoreToCamelCase(true); - configuration.addMapper(Mapper.class); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); - - try (SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql")) { - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - } - } - - @AfterClass - public static void tearDown() { - postgres.stop(); - } - - @Test - public void shouldGetMultipleResultSetsWithOneStatement() throws IOException { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - Mapper mapper = sqlSession.getMapper(Mapper.class); - List> results = mapper.getUsersAndGroups(); - Assert.assertEquals(2, results.size()); - - Assert.assertEquals(6, results.get(0).size()); - OrderDetail detail = (OrderDetail)results.get(0).get(0); - Assert.assertEquals(1, detail.getOrderId()); - Assert.assertEquals(1, detail.getLineNumber()); - Assert.assertEquals(1, detail.getQuantity()); - Assert.assertEquals("Pen", detail.getItemDescription()); - - Assert.assertEquals(2, results.get(1).size()); - OrderHeader header = (OrderHeader)results.get(1).get(0); - Assert.assertEquals(1, header.getOrderId()); - Assert.assertEquals("Fred", header.getCustName()); - } - } - - @Test - public void shouldSkipNullResultSet() { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - List> results = mapper.multiResultsWithUpdate(); - Assert.assertEquals(2, results.size()); - - Assert.assertEquals(6, results.get(0).size()); - OrderDetail detail = (OrderDetail) results.get(0).get(0); - Assert.assertEquals(1, detail.getOrderId()); - Assert.assertEquals(1, detail.getLineNumber()); - Assert.assertEquals(1, detail.getQuantity()); - Assert.assertEquals("Pen", detail.getItemDescription()); - - Assert.assertEquals(2, results.get(1).size()); - OrderHeader header = (OrderHeader) results.get(1).get(0); - Assert.assertEquals(1, header.getOrderId()); - Assert.assertEquals("Fred", header.getCustName()); - - results = mapper.getUsersAndGroups(); - Assert.assertEquals(7, results.get(0).size()); - detail = (OrderDetail) results.get(0).get(6); - Assert.assertEquals(2, detail.getOrderId()); - Assert.assertEquals(4, detail.getLineNumber()); - Assert.assertEquals(5, detail.getQuantity()); - Assert.assertEquals("Eraser", detail.getItemDescription()); - } finally { - sqlSession.rollback(true); - } - } - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java deleted file mode 100644 index bc6531ea97f..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.multiple_resultsets; - -public class OrderDetail { - - private int orderId; - private int lineNumber; - private int quantity; - private String itemDescription; - - public int getOrderId() { - return orderId; - } - - public void setOrderId(int orderId) { - this.orderId = orderId; - } - - public int getLineNumber() { - return lineNumber; - } - - public void setLineNumber(int lineNumber) { - this.lineNumber = lineNumber; - } - - public int getQuantity() { - return quantity; - } - - public void setQuantity(int quantity) { - this.quantity = quantity; - } - - public String getItemDescription() { - return itemDescription; - } - - public void setItemDescription(String itemDescription) { - this.itemDescription = itemDescription; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java deleted file mode 100644 index f58f5b0a31c..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.multiple_resultsets; - -public class OrderHeader { - - private int orderId; - private String custName; - - public int getOrderId() { - return orderId; - } - - public void setOrderId(int orderId) { - this.orderId = orderId; - } - - public String getCustName() { - return custName; - } - - public void setCustName(String custName) { - this.custName = custName; - } - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql deleted file mode 100644 index 8b36b091b97..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql +++ /dev/null @@ -1,34 +0,0 @@ --- --- Copyright 2009-2017 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. --- - -CREATE SCHEMA mbtest; - -CREATE TABLE mbtest.users ( - user_id serial PRIMARY KEY, - name character varying(30) -); - -INSERT INTO mbtest.users (name) values -('Jimmy'); - - -CREATE TABLE mbtest.sections ( - section_id int PRIMARY KEY, - name character varying(30) -); - -INSERT INTO mbtest.sections (section_id, name) values -(1, 'Section 1'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java deleted file mode 100644 index f43b4c93581..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.postgres_genkeys; - -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Update; - -public interface Mapper { - @Insert("insert into mbtest.sections (section_id, name) values (#{sectionId}, #{name})") - int insertSection(Section section); - - @Update("update mbtest.users set name = #{name} where user_id = #{userId}") - int updateUser(User user); - - @Insert("insert into mbtest.users (name) values (#{name})") - int insertUser(@Param("name") String name); -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java deleted file mode 100644 index 7e3c14b0dfa..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.postgres_genkeys; - -import static org.junit.Assert.*; - -import java.io.Reader; -import java.nio.file.Paths; -import java.sql.Connection; -import java.util.Collections; - -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.ibatis.submitted.usesjava8.keycolumn.InsertMapper; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; -import ru.yandex.qatools.embed.postgresql.util.SocketUtil; - -public class PostgresGeneratedKeysTest { - - private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // Launch PostgreSQL server. Download / unarchive if necessary. - String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "postgres_genkeys", "postgres", "root", Collections.emptyList()); - - Configuration configuration = new Configuration(); - Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( - "org.postgresql.Driver", url, null)); - configuration.setEnvironment(environment); - configuration.setUseGeneratedKeys(true); - configuration.addMapper(Mapper.class); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); - - try (SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql")) { - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - } - } - - @AfterClass - public static void tearDown() { - postgres.stop(); - } - - @Test - public void testInsertIntoTableWithNoSerialColumn() throws Exception { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - Section section = new Section(); - section.setSectionId(2); - section.setName("Section 2"); - int result = mapper.insertSection(section); - assertEquals(1, result); - } finally { - sqlSession.close(); - } - } - - @Test - public void testUpdateTableWithSerial() throws Exception { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - User user = new User(); - user.setUserId(1); - user.setName("Ethan"); - int result = mapper.updateUser(user); - assertEquals(1, result); - } finally { - sqlSession.close(); - } - } - - @Test - public void testUnusedGeneratedKey() throws Exception { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - int result = mapper.insertUser("John"); - assertEquals(1, result); - } finally { - sqlSession.close(); - } - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java deleted file mode 100644 index 235ed30c38b..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.postgres_genkeys; - -public class Section { - private Integer sectionId; - - private String name; - - public Integer getSectionId() { - return sectionId; - } - - public void setSectionId(Integer sectionId) { - this.sectionId = sectionId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java deleted file mode 100644 index 5e3a827812f..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.postgres_genkeys; - -public class User { - private Integer userId; - - private String name; - - public Integer getUserId() { - return userId; - } - - public void setUserId(Integer userId) { - this.userId = userId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql deleted file mode 100644 index 0dfd45501ef..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql +++ /dev/null @@ -1,102 +0,0 @@ --- --- Copyright 2009-2017 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. --- - -CREATE SCHEMA mbtest; - -CREATE TABLE mbtest.order_detail -( - order_id integer NOT NULL, - line_number integer NOT NULL, - quantity integer NOT NULL, - item_description character varying(50) NOT NULL, - CONSTRAINT order_detail_pkey PRIMARY KEY (order_id, line_number) -) -WITH ( - OIDS=FALSE -); - -ALTER TABLE mbtest.order_detail OWNER TO postgres; - -CREATE TABLE mbtest.order_header -( - order_id integer NOT NULL, - cust_name character varying(50) NOT NULL, - CONSTRAINT order_header_pkey PRIMARY KEY (order_id) -) -WITH ( - OIDS=FALSE -); - -ALTER TABLE mbtest.order_header OWNER TO postgres; - -INSERT INTO mbtest.order_header(order_id, cust_name) - VALUES (1, 'Fred'); -INSERT INTO mbtest.order_header(order_id, cust_name) - VALUES (2, 'Barney'); - -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 1, 1, 'Pen'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 2, 3, 'Pencil'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (1, 3, 2, 'Notepad'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 1, 1, 'Compass'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 2, 1, 'Protractor'); -INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) - VALUES (2, 3, 2, 'Pencil'); - --- @DELIMITER | - -CREATE OR REPLACE FUNCTION mbtest.get_order(order_number integer) - RETURNS refcursor AS -$BODY$ -DECLARE - mycurs refcursor; -BEGIN - open mycurs for select a.*, b.* - from mbtest.order_header a join mbtest.order_detail b - on a.order_id = b.order_id - where a.order_id = ORDER_NUMBER; - return mycurs; -END; -$BODY$ - LANGUAGE plpgsql VOLATILE - COST 100 | - - -CREATE OR REPLACE FUNCTION mbtest.get_order_out_params( - order_number integer, - detail_count out integer, - header_curs out refcursor -) AS $BODY$ -DECLARE - order_exists boolean; -BEGIN - select order_id is not null into order_exists from mbtest.order_header where order_id = ORDER_NUMBER; - if order_exists then - open header_curs for select * from mbtest.order_header where order_id = ORDER_NUMBER; - end if; - select count(*) into detail_count from mbtest.order_detail where order_id = ORDER_NUMBER; -END; -$BODY$ - LANGUAGE plpgsql VOLATILE - COST 100 | - --- @DELIMITER ; - -ALTER FUNCTION mbtest.get_order(integer) OWNER TO postgres; diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java deleted file mode 100644 index ce89c47f33b..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.refcursor; - -import java.util.List; - -public class Order { - private Integer orderId; - private String customerName; - private List detailLines; - public Integer getOrderId() { - return orderId; - } - public void setOrderId(Integer orderId) { - this.orderId = orderId; - } - public String getCustomerName() { - return customerName; - } - public void setCustomerName(String customerName) { - this.customerName = customerName; - } - public List getDetailLines() { - return detailLines; - } - public void setDetailLines(List detailLines) { - this.detailLines = detailLines; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java deleted file mode 100644 index 3934aa978e1..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.refcursor; - -public class OrderDetail { - private Integer orderNumber; - private Integer lineNumber; - private Integer quantity; - private String description; - public Integer getLineNumber() { - return lineNumber; - } - public void setLineNumber(Integer lineNumber) { - this.lineNumber = lineNumber; - } - public Integer getQuantity() { - return quantity; - } - public void setQuantity(Integer quantity) { - this.quantity = quantity; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public Integer getOrderNumber() { - return orderNumber; - } - public void setOrderNumber(Integer orderNumber) { - this.orderNumber = orderNumber; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java deleted file mode 100644 index a65d7ed9c4c..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.refcursor; - -import java.util.Map; - -import org.apache.ibatis.session.ResultHandler; - -public interface OrdersMapper { - void getOrder1(Map parameter); - - void getOrder2(Map parameter); - - void getOrder3(Map parameter, ResultHandler resultHandler); -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml deleted file mode 100644 index 01aaa6b1f07..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - - - { #{order,jdbcType=OTHER,mode=OUT,resultMap=OrderResult,javaType=java.sql.ResultSet} = - call mbtest.get_order(#{orderId,jdbcType=INTEGER,mode=IN}) } - - - - { #{order,jdbcType=OTHER,mode=OUT,resultMap=org.apache.ibatis.submitted.usesjava8.refcursor.OrdersMapper.OrderResult,javaType=java.sql.ResultSet} = - call mbtest.get_order(#{orderId,jdbcType=INTEGER,mode=IN}) } - - - - - - - - - - diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java deleted file mode 100644 index c250ea4ffdb..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.refcursor; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.io.Reader; -import java.nio.file.Paths; -import java.sql.Connection; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.ResultContext; -import org.apache.ibatis.session.ResultHandler; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.ibatis.test.EmbeddedPostgresqlTests; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; -import ru.yandex.qatools.embed.postgresql.util.SocketUtil; - -/** - * @author Jeff Butler - */ -@Category(EmbeddedPostgresqlTests.class) -public class RefCursorTest { - - private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // Launch PostgreSQL server. Download / unarchive if necessary. - String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "refcursor", "postgres", "root", Collections.emptyList()); - - Configuration configuration = new Configuration(); - Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( - "org.postgresql.Driver", url, null)); - configuration.setEnvironment(environment); - configuration.addMapper(OrdersMapper.class); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); - - try (SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql")) { - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - } - } - - @AfterClass - public static void tearDown() { - postgres.stop(); - } - - @Test - public void testRefCursor1() throws IOException { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); - Map parameter = new HashMap(); - parameter.put("orderId", 1); - mapper.getOrder1(parameter); - - assertNotNull(parameter.get("order")); - @SuppressWarnings("unchecked") - List orders = (List) parameter.get("order"); - assertEquals(1, orders.size()); - Order order = orders.get(0); - assertEquals(3, order.getDetailLines().size()); - } - } - - @Test - public void testRefCursor2() throws IOException { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); - Map parameter = new HashMap(); - parameter.put("orderId", 1); - mapper.getOrder2(parameter); - - assertNotNull(parameter.get("order")); - @SuppressWarnings("unchecked") - List orders = (List) parameter.get("order"); - assertEquals(1, orders.size()); - Order order = orders.get(0); - assertEquals(3, order.getDetailLines().size()); - } - } - - @Test - public void shouldUseResultHandlerOnOutputParam() throws IOException { - class OrderResultHandler implements ResultHandler { - private List orders = new ArrayList(); - - @Override - public void handleResult(ResultContext resultContext) { - Order order = resultContext.getResultObject(); - order.setCustomerName("Anonymous"); - orders.add(order); - } - - List getResult() { - return orders; - } - } - - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); - OrderResultHandler handler = new OrderResultHandler(); - Map parameter = new HashMap(); - parameter.put("orderId", 1); - mapper.getOrder3(parameter, handler); - - assertNull(parameter.get("order")); - assertEquals(Integer.valueOf(3), parameter.get("detailCount")); - assertEquals("Anonymous", handler.getResult().get(0).getCustomerName()); - } - } - - @Test - public void shouldNullResultSetNotCauseNpe() throws IOException { - try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); - Map parameter = new HashMap(); - parameter.put("orderId", 99); - mapper.getOrder3(parameter, new ResultHandler() { - @Override - public void handleResult(ResultContext resultContext) { - // won't be used - } - }); - assertEquals(Integer.valueOf(0), parameter.get("detailCount")); - } - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql deleted file mode 100644 index d5b2c1ea6e9..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql +++ /dev/null @@ -1,25 +0,0 @@ --- --- Copyright 2009-2016 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'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java deleted file mode 100644 index 374f2ce9aeb..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2009-2016 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.usesjava8.use_actual_param_name; - -import java.util.List; - -import org.apache.ibatis.annotations.Select; - -public interface Mapper { - - @Select("select * from users where id = #{foo}") - User getUserById(Integer id); - - @Select("select * from users where id = #{id} and name = #{name}") - User getUserByIdAndName(Integer id, String name); - - List getUsersByIdList(List ids); - - List getUsersByIdListAndName(List ids, String name); - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml deleted file mode 100644 index eb88c20b136..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java deleted file mode 100644 index 1fd91f523be..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Copyright 2009-2017 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.usesjava8.use_actual_param_name; - -import static org.junit.Assert.*; - -import java.io.Reader; -import java.sql.Connection; -import java.util.Arrays; -import java.util.List; - -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.BeforeClass; -import org.junit.Test; - -public class UseActualParamNameTest { - - private static SqlSessionFactory sqlSessionFactory; - - @BeforeClass - public static void setUp() throws Exception { - // create an SqlSessionFactory - Reader reader = Resources.getResourceAsReader( - "org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml"); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); - reader.close(); - - // populate in-memory database - SqlSession session = sqlSessionFactory.openSession(); - Connection conn = session.getConnection(); - reader = Resources.getResourceAsReader( - "org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql"); - ScriptRunner runner = new ScriptRunner(conn); - runner.setLogWriter(null); - runner.runScript(reader); - conn.close(); - reader.close(); - session.close(); - } - - @Test - public void shouldSingleParamBeReferencedByAnyName() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - User user = mapper.getUserById(1); - assertNotNull(user); - } finally { - sqlSession.close(); - } - } - - @Test - public void shouldMultipleParamsBeReferencedByActualNames() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - User user = mapper.getUserByIdAndName(1, "User1"); - assertNotNull(user); - } finally { - sqlSession.close(); - } - } - - @Test - public void shouldSoleListParamBeReferencedByImplicitName() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - List users = mapper.getUsersByIdList(Arrays.asList(1, 2)); - assertEquals(2, users.size()); - } finally { - sqlSession.close(); - } - } - - @Test - public void shouldListParamBeReferencedByActualNameIfAnotherParamExists() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { - Mapper mapper = sqlSession.getMapper(Mapper.class); - List users = mapper.getUsersByIdListAndName(Arrays.asList(1, 2), null); - assertEquals(2, users.size()); - } finally { - sqlSession.close(); - } - } - -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java deleted file mode 100644 index 16e6c0e7769..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2009-2016 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.usesjava8.use_actual_param_name; - -public class User { - - private Integer id; - private String name; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml deleted file mode 100644 index d81897e9522..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - From 7410ece619dcdef6bba588310bdfa0f179394717 Mon Sep 17 00:00:00 2001 From: zhaoxi1988 Date: Fri, 8 Dec 2017 18:21:24 +0800 Subject: [PATCH 3/3] reset package usejava8 --- .../usesjava8/default_method/CreateDB.sql | 24 +++ .../default_method/DefaultMethodTest.java | 92 ++++++++++ .../usesjava8/default_method/Mapper.java | 39 +++++ .../default_method/PackageMapper.java | 27 +++ .../usesjava8/default_method/User.java | 38 ++++ .../default_method/mybatis-config.xml | 44 +++++ .../usesjava8/keycolumn/CreateDB.sql | 30 ++++ .../usesjava8/keycolumn/InsertMapper.java | 32 ++++ .../usesjava8/keycolumn/InsertMapper.xml | 28 +++ .../usesjava8/keycolumn/InsertTest.java | 132 ++++++++++++++ .../submitted/usesjava8/keycolumn/Name.java | 40 +++++ .../multiple_resultsets/CreateDB.sql | 83 +++++++++ .../usesjava8/multiple_resultsets/Mapper.java | 26 +++ .../usesjava8/multiple_resultsets/Mapper.xml | 40 +++++ .../MultipleResultTest.java | 135 ++++++++++++++ .../multiple_resultsets/OrderDetail.java | 56 ++++++ .../multiple_resultsets/OrderHeader.java | 39 +++++ .../usesjava8/postgres_genkeys/CreateDB.sql | 34 ++++ .../usesjava8/postgres_genkeys/Mapper.java | 31 ++++ .../PostgresGeneratedKeysTest.java | 116 ++++++++++++ .../usesjava8/postgres_genkeys/Section.java | 39 +++++ .../usesjava8/postgres_genkeys/User.java | 38 ++++ .../usesjava8/refcursor/CreateDB.sql | 102 +++++++++++ .../submitted/usesjava8/refcursor/Order.java | 42 +++++ .../usesjava8/refcursor/OrderDetail.java | 47 +++++ .../usesjava8/refcursor/OrdersMapper.java | 28 +++ .../usesjava8/refcursor/OrdersMapper.xml | 57 ++++++ .../usesjava8/refcursor/RefCursorTest.java | 165 ++++++++++++++++++ .../use_actual_param_name/CreateDB.sql | 25 +++ .../use_actual_param_name/Mapper.java | 34 ++++ .../use_actual_param_name/Mapper.xml | 41 +++++ .../UseActualParamNameTest.java | 106 +++++++++++ .../usesjava8/use_actual_param_name/User.java | 38 ++++ .../use_actual_param_name/mybatis-config.xml | 42 +++++ 34 files changed, 1890 insertions(+) create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql new file mode 100644 index 00000000000..2354824ab88 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql @@ -0,0 +1,24 @@ +-- +-- Copyright 2009-2016 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'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java new file mode 100644 index 00000000000..52eada1bc5a --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/DefaultMethodTest.java @@ -0,0 +1,92 @@ +/** + * Copyright 2009-2017 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.usesjava8.default_method; + +import static org.junit.Assert.*; + +import java.io.Reader; +import java.sql.Connection; + +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.apache.ibatis.submitted.usesjava8.default_method.Mapper.SubMapper; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DefaultMethodTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // create an SqlSessionFactory + Reader reader = Resources.getResourceAsReader( + "org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml"); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + reader.close(); + + // populate in-memory database + SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + reader = Resources.getResourceAsReader( + "org/apache/ibatis/submitted/usesjava8/default_method/CreateDB.sql"); + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + conn.close(); + reader.close(); + session.close(); + } + + @Test + public void shouldInvokeDefaultMethod() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + User user = mapper.defaultGetUser(1); + assertEquals("User1", user.getName()); + } finally { + sqlSession.close(); + } + } + + @Test + public void shouldInvokeDefaultMethodOfSubclass() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + SubMapper mapper = sqlSession.getMapper(SubMapper.class); + User user = mapper.defaultGetUser("User1", 1); + assertEquals("User1", user.getName()); + } finally { + sqlSession.close(); + } + } + + @Test + public void shouldInvokeDefaultMethodOfPackagePrivateMapper() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + PackageMapper mapper = sqlSession.getMapper(PackageMapper.class); + User user = mapper.defaultGetUser(1); + assertEquals("User1", user.getName()); + } finally { + sqlSession.close(); + } + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java new file mode 100644 index 00000000000..da394be3c37 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/Mapper.java @@ -0,0 +1,39 @@ +/** + * Copyright 2009-2017 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.usesjava8.default_method; + +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +public interface Mapper { + + @Select("select * from users where id = #{id}") + User getUserById(Integer id); + + @Select("select * from users where id = #{id} and name = #{name}") + User getUserByIdAndName(@Param("name") String name, @Param("id") Integer id); + + default User defaultGetUser(Object... args) { + return getUserById((Integer) args[0]); + } + + static interface SubMapper extends Mapper { + default User defaultGetUser(Object... args) { + return getUserByIdAndName((String) args[0], (Integer) args[1]); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java new file mode 100644 index 00000000000..7ad681c113e --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/PackageMapper.java @@ -0,0 +1,27 @@ +/** + * Copyright 2009-2017 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.usesjava8.default_method; + +import org.apache.ibatis.annotations.Select; + +interface PackageMapper { + @Select("select * from users where id = #{id}") + User getUserById(Integer id); + + default User defaultGetUser(Object... args) { + return getUserById((Integer) args[0]); + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java new file mode 100644 index 00000000000..e38de2200cf --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/User.java @@ -0,0 +1,38 @@ +/** + * Copyright 2009-2016 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.usesjava8.default_method; + +public class User { + + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml new file mode 100644 index 00000000000..5f5e7307a4b --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/default_method/mybatis-config.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql new file mode 100644 index 00000000000..510559028e5 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql @@ -0,0 +1,30 @@ +-- +-- Copyright 2009-2017 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. +-- + +CREATE SCHEMA mbtest; + +CREATE TABLE mbtest.test_identity +( + first_name character varying(30), + last_name character varying(30), + name_id serial NOT NULL, + CONSTRAINT test_identity_pkey PRIMARY KEY (name_id) +) +WITH ( + OIDS=FALSE +); + +ALTER TABLE mbtest.test_identity OWNER TO postgres; diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java new file mode 100644 index 00000000000..443c33ae6d3 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.java @@ -0,0 +1,32 @@ +/** + * Copyright 2009-2017 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.usesjava8.keycolumn; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Options; + +public interface InsertMapper { + + @Insert({ + "insert into mbtest.test_identity", + "(first_name, last_name)", + "values(#{firstName,jdbcType=VARCHAR}, #{lastName,jdbcType=VARCHAR})" + }) + @Options(keyProperty="id", useGeneratedKeys=true, keyColumn="name_id") + int insertNameAnnotated(Name name); + + int insertNameMapped(Name name); +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml new file mode 100644 index 00000000000..85e08a4032f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertMapper.xml @@ -0,0 +1,28 @@ + + + + + + insert into mbtest.test_identity + (first_name, last_name) + values(#{firstName,jdbcType=VARCHAR}, #{lastName,jdbcType=VARCHAR}) + + \ No newline at end of file diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java new file mode 100644 index 00000000000..031129c63ea --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/InsertTest.java @@ -0,0 +1,132 @@ +/** + * Copyright 2009-2017 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.usesjava8.keycolumn; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.Reader; +import java.nio.file.Paths; +import java.sql.Connection; +import java.util.Collections; +import java.util.List; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.executor.BatchResult; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.test.EmbeddedPostgresqlTests; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; +import ru.yandex.qatools.embed.postgresql.util.SocketUtil; + +/** + * @author Jeff Butler + */ +@Category(EmbeddedPostgresqlTests.class) +public class InsertTest { + + private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // Launch PostgreSQL server. Download / unarchive if necessary. + String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "keycolumn", "postgres", "root", Collections.emptyList()); + + Configuration configuration = new Configuration(); + Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( + "org.postgresql.Driver", url, null)); + configuration.setEnvironment(environment); + configuration.addMapper(InsertMapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); + + try (SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/keycolumn/CreateDB.sql")) { + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + } + } + + @AfterClass + public static void tearDown() { + postgres.stop(); + } + + @Test + public void testInsertAnnotated() throws Exception { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); + Name name = new Name(); + name.setFirstName("Fred"); + name.setLastName("Flintstone"); + + int rows = mapper.insertNameAnnotated(name); + + assertNotNull(name.getId()); + assertEquals(1, rows); + } + } + + @Test + public void testInsertMapped() throws Exception { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); + Name name = new Name(); + name.setFirstName("Fred"); + name.setLastName("Flintstone"); + + int rows = mapper.insertNameMapped(name); + + assertNotNull(name.getId()); + assertEquals(1, rows); + } + } + + @Test + public void testInsertMappedBatch() throws Exception { + try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { + InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); + Name name = new Name(); + name.setFirstName("Fred"); + name.setLastName("Flintstone"); + mapper.insertNameMapped(name); + Name name2 = new Name(); + name2.setFirstName("Wilma"); + name2.setLastName("Flintstone"); + mapper.insertNameMapped(name2); + List batchResults = sqlSession.flushStatements(); + assertNotNull(name.getId()); + assertNotNull(name2.getId()); + assertEquals(1, batchResults.size()); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java new file mode 100644 index 00000000000..1fb0d4a6376 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/keycolumn/Name.java @@ -0,0 +1,40 @@ +/** + * Copyright 2009-2017 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.usesjava8.keycolumn; + +public class Name { + private Integer id; + private String firstName; + private String lastName; + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql new file mode 100644 index 00000000000..d5f5bf76a7a --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql @@ -0,0 +1,83 @@ +-- +-- Copyright 2009-2017 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. +-- + +CREATE SCHEMA mbtest; + +CREATE TABLE mbtest.order_detail +( + order_id integer NOT NULL, + line_number integer NOT NULL, + quantity integer NOT NULL, + item_description character varying(50) NOT NULL, + CONSTRAINT order_detail_pkey PRIMARY KEY (order_id, line_number) +) +WITH ( + OIDS=FALSE +); + +ALTER TABLE mbtest.order_detail OWNER TO postgres; + +CREATE TABLE mbtest.order_header +( + order_id integer NOT NULL, + cust_name character varying(50) NOT NULL, + CONSTRAINT order_header_pkey PRIMARY KEY (order_id) +) +WITH ( + OIDS=FALSE +); + +ALTER TABLE mbtest.order_header OWNER TO postgres; + +INSERT INTO mbtest.order_header(order_id, cust_name) + VALUES (1, 'Fred'); +INSERT INTO mbtest.order_header(order_id, cust_name) + VALUES (2, 'Barney'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 1, 1, 'Pen'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 2, 3, 'Pencil'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 3, 2, 'Notepad'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 1, 1, 'Compass'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 2, 1, 'Protractor'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 3, 2, 'Pencil'); + +-- @DELIMITER | + +CREATE OR REPLACE FUNCTION mbtest.get_order(order_number integer) + RETURNS refcursor AS +$BODY$ +DECLARE + mycurs refcursor; +BEGIN + open mycurs for select a.*, b.* + from mbtest.order_header a join mbtest.order_detail b + on a.order_id = b.order_id + where a.order_id = ORDER_NUMBER; + return mycurs; +END; +$BODY$ + LANGUAGE plpgsql VOLATILE + COST 100| + +-- @DELIMITER ; + +ALTER FUNCTION mbtest.get_order(integer) OWNER TO postgres; + diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java new file mode 100644 index 00000000000..dce276cae2f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.java @@ -0,0 +1,26 @@ +/** + * Copyright 2009-2017 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.usesjava8.multiple_resultsets; + +import java.util.List; + +public interface Mapper { + + List> getUsersAndGroups(); + + List> multiResultsWithUpdate(); + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml new file mode 100644 index 00000000000..8836266d516 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/Mapper.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java new file mode 100644 index 00000000000..91047b9bfa8 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/MultipleResultTest.java @@ -0,0 +1,135 @@ +/** + * Copyright 2009-2017 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.usesjava8.multiple_resultsets; + +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Paths; +import java.sql.Connection; +import java.util.Collections; +import java.util.List; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.test.EmbeddedPostgresqlTests; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; +import ru.yandex.qatools.embed.postgresql.util.SocketUtil; + +/* + * This class contains tests for multiple results. + * It is based on Jeff's ref cursor tests. + */ +@Category(EmbeddedPostgresqlTests.class) +public class MultipleResultTest { + + private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // Launch PostgreSQL server. Download / unarchive if necessary. + String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "multiple_resultsets", "postgres", "root", Collections.emptyList()); + + Configuration configuration = new Configuration(); + Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( + "org.postgresql.Driver", url, null)); + configuration.setEnvironment(environment); + configuration.setMapUnderscoreToCamelCase(true); + configuration.addMapper(Mapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); + + try (SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/multiple_resultsets/CreateDB.sql")) { + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + } + } + + @AfterClass + public static void tearDown() { + postgres.stop(); + } + + @Test + public void shouldGetMultipleResultSetsWithOneStatement() throws IOException { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + List> results = mapper.getUsersAndGroups(); + Assert.assertEquals(2, results.size()); + + Assert.assertEquals(6, results.get(0).size()); + OrderDetail detail = (OrderDetail)results.get(0).get(0); + Assert.assertEquals(1, detail.getOrderId()); + Assert.assertEquals(1, detail.getLineNumber()); + Assert.assertEquals(1, detail.getQuantity()); + Assert.assertEquals("Pen", detail.getItemDescription()); + + Assert.assertEquals(2, results.get(1).size()); + OrderHeader header = (OrderHeader)results.get(1).get(0); + Assert.assertEquals(1, header.getOrderId()); + Assert.assertEquals("Fred", header.getCustName()); + } + } + + @Test + public void shouldSkipNullResultSet() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + List> results = mapper.multiResultsWithUpdate(); + Assert.assertEquals(2, results.size()); + + Assert.assertEquals(6, results.get(0).size()); + OrderDetail detail = (OrderDetail) results.get(0).get(0); + Assert.assertEquals(1, detail.getOrderId()); + Assert.assertEquals(1, detail.getLineNumber()); + Assert.assertEquals(1, detail.getQuantity()); + Assert.assertEquals("Pen", detail.getItemDescription()); + + Assert.assertEquals(2, results.get(1).size()); + OrderHeader header = (OrderHeader) results.get(1).get(0); + Assert.assertEquals(1, header.getOrderId()); + Assert.assertEquals("Fred", header.getCustName()); + + results = mapper.getUsersAndGroups(); + Assert.assertEquals(7, results.get(0).size()); + detail = (OrderDetail) results.get(0).get(6); + Assert.assertEquals(2, detail.getOrderId()); + Assert.assertEquals(4, detail.getLineNumber()); + Assert.assertEquals(5, detail.getQuantity()); + Assert.assertEquals("Eraser", detail.getItemDescription()); + } finally { + sqlSession.rollback(true); + } + } + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java new file mode 100644 index 00000000000..bc6531ea97f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderDetail.java @@ -0,0 +1,56 @@ +/** + * Copyright 2009-2017 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.usesjava8.multiple_resultsets; + +public class OrderDetail { + + private int orderId; + private int lineNumber; + private int quantity; + private String itemDescription; + + public int getOrderId() { + return orderId; + } + + public void setOrderId(int orderId) { + this.orderId = orderId; + } + + public int getLineNumber() { + return lineNumber; + } + + public void setLineNumber(int lineNumber) { + this.lineNumber = lineNumber; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getItemDescription() { + return itemDescription; + } + + public void setItemDescription(String itemDescription) { + this.itemDescription = itemDescription; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java new file mode 100644 index 00000000000..f58f5b0a31c --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/multiple_resultsets/OrderHeader.java @@ -0,0 +1,39 @@ +/** + * Copyright 2009-2017 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.usesjava8.multiple_resultsets; + +public class OrderHeader { + + private int orderId; + private String custName; + + public int getOrderId() { + return orderId; + } + + public void setOrderId(int orderId) { + this.orderId = orderId; + } + + public String getCustName() { + return custName; + } + + public void setCustName(String custName) { + this.custName = custName; + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql new file mode 100644 index 00000000000..8b36b091b97 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql @@ -0,0 +1,34 @@ +-- +-- Copyright 2009-2017 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. +-- + +CREATE SCHEMA mbtest; + +CREATE TABLE mbtest.users ( + user_id serial PRIMARY KEY, + name character varying(30) +); + +INSERT INTO mbtest.users (name) values +('Jimmy'); + + +CREATE TABLE mbtest.sections ( + section_id int PRIMARY KEY, + name character varying(30) +); + +INSERT INTO mbtest.sections (section_id, name) values +(1, 'Section 1'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java new file mode 100644 index 00000000000..f43b4c93581 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Update; + +public interface Mapper { + @Insert("insert into mbtest.sections (section_id, name) values (#{sectionId}, #{name})") + int insertSection(Section section); + + @Update("update mbtest.users set name = #{name} where user_id = #{userId}") + int updateUser(User user); + + @Insert("insert into mbtest.users (name) values (#{name})") + int insertUser(@Param("name") String name); +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java new file mode 100644 index 00000000000..7e3c14b0dfa --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java @@ -0,0 +1,116 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +import static org.junit.Assert.*; + +import java.io.Reader; +import java.nio.file.Paths; +import java.sql.Connection; +import java.util.Collections; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.submitted.usesjava8.keycolumn.InsertMapper; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; +import ru.yandex.qatools.embed.postgresql.util.SocketUtil; + +public class PostgresGeneratedKeysTest { + + private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // Launch PostgreSQL server. Download / unarchive if necessary. + String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "postgres_genkeys", "postgres", "root", Collections.emptyList()); + + Configuration configuration = new Configuration(); + Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( + "org.postgresql.Driver", url, null)); + configuration.setEnvironment(environment); + configuration.setUseGeneratedKeys(true); + configuration.addMapper(Mapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); + + try (SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql")) { + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + } + } + + @AfterClass + public static void tearDown() { + postgres.stop(); + } + + @Test + public void testInsertIntoTableWithNoSerialColumn() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Section section = new Section(); + section.setSectionId(2); + section.setName("Section 2"); + int result = mapper.insertSection(section); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } + + @Test + public void testUpdateTableWithSerial() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + User user = new User(); + user.setUserId(1); + user.setName("Ethan"); + int result = mapper.updateUser(user); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } + + @Test + public void testUnusedGeneratedKey() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + int result = mapper.insertUser("John"); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java new file mode 100644 index 00000000000..235ed30c38b --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java @@ -0,0 +1,39 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +public class Section { + private Integer sectionId; + + private String name; + + public Integer getSectionId() { + return sectionId; + } + + public void setSectionId(Integer sectionId) { + this.sectionId = sectionId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java new file mode 100644 index 00000000000..5e3a827812f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java @@ -0,0 +1,38 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +public class User { + private Integer userId; + + private String name; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql new file mode 100644 index 00000000000..0dfd45501ef --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql @@ -0,0 +1,102 @@ +-- +-- Copyright 2009-2017 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. +-- + +CREATE SCHEMA mbtest; + +CREATE TABLE mbtest.order_detail +( + order_id integer NOT NULL, + line_number integer NOT NULL, + quantity integer NOT NULL, + item_description character varying(50) NOT NULL, + CONSTRAINT order_detail_pkey PRIMARY KEY (order_id, line_number) +) +WITH ( + OIDS=FALSE +); + +ALTER TABLE mbtest.order_detail OWNER TO postgres; + +CREATE TABLE mbtest.order_header +( + order_id integer NOT NULL, + cust_name character varying(50) NOT NULL, + CONSTRAINT order_header_pkey PRIMARY KEY (order_id) +) +WITH ( + OIDS=FALSE +); + +ALTER TABLE mbtest.order_header OWNER TO postgres; + +INSERT INTO mbtest.order_header(order_id, cust_name) + VALUES (1, 'Fred'); +INSERT INTO mbtest.order_header(order_id, cust_name) + VALUES (2, 'Barney'); + +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 1, 1, 'Pen'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 2, 3, 'Pencil'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (1, 3, 2, 'Notepad'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 1, 1, 'Compass'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 2, 1, 'Protractor'); +INSERT INTO mbtest.order_detail(order_id, line_number, quantity, item_description) + VALUES (2, 3, 2, 'Pencil'); + +-- @DELIMITER | + +CREATE OR REPLACE FUNCTION mbtest.get_order(order_number integer) + RETURNS refcursor AS +$BODY$ +DECLARE + mycurs refcursor; +BEGIN + open mycurs for select a.*, b.* + from mbtest.order_header a join mbtest.order_detail b + on a.order_id = b.order_id + where a.order_id = ORDER_NUMBER; + return mycurs; +END; +$BODY$ + LANGUAGE plpgsql VOLATILE + COST 100 | + + +CREATE OR REPLACE FUNCTION mbtest.get_order_out_params( + order_number integer, + detail_count out integer, + header_curs out refcursor +) AS $BODY$ +DECLARE + order_exists boolean; +BEGIN + select order_id is not null into order_exists from mbtest.order_header where order_id = ORDER_NUMBER; + if order_exists then + open header_curs for select * from mbtest.order_header where order_id = ORDER_NUMBER; + end if; + select count(*) into detail_count from mbtest.order_detail where order_id = ORDER_NUMBER; +END; +$BODY$ + LANGUAGE plpgsql VOLATILE + COST 100 | + +-- @DELIMITER ; + +ALTER FUNCTION mbtest.get_order(integer) OWNER TO postgres; diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java new file mode 100644 index 00000000000..ce89c47f33b --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/Order.java @@ -0,0 +1,42 @@ +/** + * Copyright 2009-2017 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.usesjava8.refcursor; + +import java.util.List; + +public class Order { + private Integer orderId; + private String customerName; + private List detailLines; + public Integer getOrderId() { + return orderId; + } + public void setOrderId(Integer orderId) { + this.orderId = orderId; + } + public String getCustomerName() { + return customerName; + } + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + public List getDetailLines() { + return detailLines; + } + public void setDetailLines(List detailLines) { + this.detailLines = detailLines; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java new file mode 100644 index 00000000000..3934aa978e1 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrderDetail.java @@ -0,0 +1,47 @@ +/** + * Copyright 2009-2017 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.usesjava8.refcursor; + +public class OrderDetail { + private Integer orderNumber; + private Integer lineNumber; + private Integer quantity; + private String description; + public Integer getLineNumber() { + return lineNumber; + } + public void setLineNumber(Integer lineNumber) { + this.lineNumber = lineNumber; + } + public Integer getQuantity() { + return quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public Integer getOrderNumber() { + return orderNumber; + } + public void setOrderNumber(Integer orderNumber) { + this.orderNumber = orderNumber; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java new file mode 100644 index 00000000000..a65d7ed9c4c --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.java @@ -0,0 +1,28 @@ +/** + * Copyright 2009-2017 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.usesjava8.refcursor; + +import java.util.Map; + +import org.apache.ibatis.session.ResultHandler; + +public interface OrdersMapper { + void getOrder1(Map parameter); + + void getOrder2(Map parameter); + + void getOrder3(Map parameter, ResultHandler resultHandler); +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml new file mode 100644 index 00000000000..01aaa6b1f07 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/OrdersMapper.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + { #{order,jdbcType=OTHER,mode=OUT,resultMap=OrderResult,javaType=java.sql.ResultSet} = + call mbtest.get_order(#{orderId,jdbcType=INTEGER,mode=IN}) } + + + + { #{order,jdbcType=OTHER,mode=OUT,resultMap=org.apache.ibatis.submitted.usesjava8.refcursor.OrdersMapper.OrderResult,javaType=java.sql.ResultSet} = + call mbtest.get_order(#{orderId,jdbcType=INTEGER,mode=IN}) } + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java new file mode 100644 index 00000000000..c250ea4ffdb --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/refcursor/RefCursorTest.java @@ -0,0 +1,165 @@ +/** + * Copyright 2009-2017 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.usesjava8.refcursor; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Paths; +import java.sql.Connection; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.test.EmbeddedPostgresqlTests; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; +import ru.yandex.qatools.embed.postgresql.util.SocketUtil; + +/** + * @author Jeff Butler + */ +@Category(EmbeddedPostgresqlTests.class) +public class RefCursorTest { + + private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // Launch PostgreSQL server. Download / unarchive if necessary. + String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "refcursor", "postgres", "root", Collections.emptyList()); + + Configuration configuration = new Configuration(); + Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( + "org.postgresql.Driver", url, null)); + configuration.setEnvironment(environment); + configuration.addMapper(OrdersMapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); + + try (SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/refcursor/CreateDB.sql")) { + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + } + } + + @AfterClass + public static void tearDown() { + postgres.stop(); + } + + @Test + public void testRefCursor1() throws IOException { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); + Map parameter = new HashMap(); + parameter.put("orderId", 1); + mapper.getOrder1(parameter); + + assertNotNull(parameter.get("order")); + @SuppressWarnings("unchecked") + List orders = (List) parameter.get("order"); + assertEquals(1, orders.size()); + Order order = orders.get(0); + assertEquals(3, order.getDetailLines().size()); + } + } + + @Test + public void testRefCursor2() throws IOException { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); + Map parameter = new HashMap(); + parameter.put("orderId", 1); + mapper.getOrder2(parameter); + + assertNotNull(parameter.get("order")); + @SuppressWarnings("unchecked") + List orders = (List) parameter.get("order"); + assertEquals(1, orders.size()); + Order order = orders.get(0); + assertEquals(3, order.getDetailLines().size()); + } + } + + @Test + public void shouldUseResultHandlerOnOutputParam() throws IOException { + class OrderResultHandler implements ResultHandler { + private List orders = new ArrayList(); + + @Override + public void handleResult(ResultContext resultContext) { + Order order = resultContext.getResultObject(); + order.setCustomerName("Anonymous"); + orders.add(order); + } + + List getResult() { + return orders; + } + } + + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); + OrderResultHandler handler = new OrderResultHandler(); + Map parameter = new HashMap(); + parameter.put("orderId", 1); + mapper.getOrder3(parameter, handler); + + assertNull(parameter.get("order")); + assertEquals(Integer.valueOf(3), parameter.get("detailCount")); + assertEquals("Anonymous", handler.getResult().get(0).getCustomerName()); + } + } + + @Test + public void shouldNullResultSetNotCauseNpe() throws IOException { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class); + Map parameter = new HashMap(); + parameter.put("orderId", 99); + mapper.getOrder3(parameter, new ResultHandler() { + @Override + public void handleResult(ResultContext resultContext) { + // won't be used + } + }); + assertEquals(Integer.valueOf(0), parameter.get("detailCount")); + } + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql new file mode 100644 index 00000000000..d5b2c1ea6e9 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql @@ -0,0 +1,25 @@ +-- +-- Copyright 2009-2016 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'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java new file mode 100644 index 00000000000..374f2ce9aeb --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.java @@ -0,0 +1,34 @@ +/** + * Copyright 2009-2016 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.usesjava8.use_actual_param_name; + +import java.util.List; + +import org.apache.ibatis.annotations.Select; + +public interface Mapper { + + @Select("select * from users where id = #{foo}") + User getUserById(Integer id); + + @Select("select * from users where id = #{id} and name = #{name}") + User getUserByIdAndName(Integer id, String name); + + List getUsersByIdList(List ids); + + List getUsersByIdListAndName(List ids, String name); + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml new file mode 100644 index 00000000000..eb88c20b136 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/Mapper.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java new file mode 100644 index 00000000000..1fd91f523be --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/UseActualParamNameTest.java @@ -0,0 +1,106 @@ +/** + * Copyright 2009-2017 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.usesjava8.use_actual_param_name; + +import static org.junit.Assert.*; + +import java.io.Reader; +import java.sql.Connection; +import java.util.Arrays; +import java.util.List; + +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.BeforeClass; +import org.junit.Test; + +public class UseActualParamNameTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // create an SqlSessionFactory + Reader reader = Resources.getResourceAsReader( + "org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml"); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + reader.close(); + + // populate in-memory database + SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + reader = Resources.getResourceAsReader( + "org/apache/ibatis/submitted/usesjava8/use_actual_param_name/CreateDB.sql"); + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + conn.close(); + reader.close(); + session.close(); + } + + @Test + public void shouldSingleParamBeReferencedByAnyName() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + User user = mapper.getUserById(1); + assertNotNull(user); + } finally { + sqlSession.close(); + } + } + + @Test + public void shouldMultipleParamsBeReferencedByActualNames() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + User user = mapper.getUserByIdAndName(1, "User1"); + assertNotNull(user); + } finally { + sqlSession.close(); + } + } + + @Test + public void shouldSoleListParamBeReferencedByImplicitName() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + List users = mapper.getUsersByIdList(Arrays.asList(1, 2)); + assertEquals(2, users.size()); + } finally { + sqlSession.close(); + } + } + + @Test + public void shouldListParamBeReferencedByActualNameIfAnotherParamExists() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + List users = mapper.getUsersByIdListAndName(Arrays.asList(1, 2), null); + assertEquals(2, users.size()); + } finally { + sqlSession.close(); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java new file mode 100644 index 00000000000..16e6c0e7769 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/User.java @@ -0,0 +1,38 @@ +/** + * Copyright 2009-2016 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.usesjava8.use_actual_param_name; + +public class User { + + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml new file mode 100644 index 00000000000..d81897e9522 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/use_actual_param_name/mybatis-config.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + +