-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'); |
31 changes: 31 additions & 0 deletions
31
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |
116 changes: 116 additions & 0 deletions
116
...ava/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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("target/postgres")), "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(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |