From f24ba9ba0b2fc4d3161664f90eb920140ede727b Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Mon, 4 Feb 2019 23:23:37 -0500 Subject: [PATCH] Fix bug with generated SQL for getGeneratedKey, add unit test for it --- .../jdbc/codegen/JdbcMapperProcessor.java | 5 ++-- .../moparisthebest/jdbc/codegen/QmDao.java | 8 ++++++- .../jdbc/codegen/QueryMapperQmDao.java | 10 ++++++++ .../moparisthebest/jdbc/QueryMapperTest.java | 23 ++++++++++--------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java index a4cd986..f6e8f2e 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java @@ -501,8 +501,9 @@ public class JdbcMapperProcessor extends AbstractProcessor { setArray(w, databaseType, arrayNumberTypeName, arrayStringTypeName, param); w.write("\t\t\tps = "); final boolean isGeneratedKeyLong = !parsedSQl.isSelect() && (returnType.equals("long") || returnType.equals("java.lang.Long")); - final boolean cachePreparedStatements = sql.cachePreparedStatement().combine(defaultCachePreparedStatements) && !bindInList; - if (cachePreparedStatements && !isGeneratedKeyLong) { // make isGeneratedKeyLong work with cachePreparedStatements + // todo: make isGeneratedKeyLong work with cachePreparedStatements + final boolean cachePreparedStatements = sql.cachePreparedStatement().combine(defaultCachePreparedStatements) && !bindInList && !isGeneratedKeyLong; + if (cachePreparedStatements) { w.write("this.prepareStatement("); w.write(Integer.toString(cachedPreparedStatements)); w.write(", "); diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java index 2b05cc2..acfac2d 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java @@ -14,7 +14,7 @@ import java.time.*; //IFJAVA8_END @JdbcMapper.Mapper( - cachePreparedStatements = JdbcMapper.OptionalBool.FALSE + cachePreparedStatements = JdbcMapper.OptionalBool.TRUE , allowReflection = JdbcMapper.OptionalBool.TRUE ) public interface QmDao extends JdbcMapper { @@ -258,4 +258,10 @@ public interface QmDao extends JdbcMapper { @SQL("SELECT person_no, first_name, last_name, birth_date from person WHERE {person_no NOT IN personNos} ORDER BY person_no") List getFieldPeopleNotIn(List personNos) throws SQLException; + + @SQL("INSERT INTO a_thaoeu_table (a_thaoeu_table_no, a_thaoeu_table_val) VALUES (a_thaoeu_table_seq.nextval, {value})") + long insertGetGeneratedKeyOracle(long value) throws SQLException; + + @SQL("INSERT INTO a_thaoeu_table (a_thaoeu_table_val) VALUES ({value})") + long insertGetGeneratedKey(long value) throws SQLException; } diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java index 93775b6..41abe14 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java @@ -403,4 +403,14 @@ public class QueryMapperQmDao implements QmDao { public List getFieldPeopleNotIn(final List personNos) throws SQLException { return qm.toList("SELECT * from person WHERE " + inListReplace + " ORDER BY person_no", FieldPerson.class, qm.notInList("person_no", personNos)); } + + @Override + public long insertGetGeneratedKeyOracle(final long value) throws SQLException { + return qm.insertGetGeneratedKey("INSERT INTO a_thaoeu_table (a_thaoeu_table_no, a_thaoeu_table_val) VALUES (a_thaoeu_table_seq.nextval, ?)", value); + } + + @Override + public long insertGetGeneratedKey(long value) throws SQLException { + return qm.insertGetGeneratedKey("INSERT INTO a_thaoeu_table (a_thaoeu_table_val) VALUES (?)", value); + } } diff --git a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java index aa03ad9..4360fa9 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java @@ -464,10 +464,9 @@ public class QueryMapperTest { @Test public void testGetGeneratedKeysSingleLong() throws SQLException { - if(!(qm instanceof QueryMapperQmDao)) - return; - final QueryMapper qm = ((QueryMapperQmDao)this.qm).getQm(); + QueryMapper qm = null; try { + qm = new QueryMapper(this.qm.getConnection()); // auto increment stuff for getGeneratedKeys, how obnoxious are these subtle differences... if (isWrapperFor(qm.getConnection(), classForName("org.sqlite.SQLiteConnection"))) { qm.executeUpdate("CREATE TABLE a_thaoeu_table(\n" + @@ -501,7 +500,7 @@ public class QueryMapperTest { "CACHE 10"); // so different we have to do test here for (long expected = 1; expected < 5; ++expected) { - final long autoTableNo = qm.insertGetGeneratedKey("INSERT INTO a_thaoeu_table (a_thaoeu_table_no, a_thaoeu_table_val) VALUES (a_thaoeu_table_seq.nextval, ?)", expected * 2); + final long autoTableNo = this.qm.insertGetGeneratedKeyOracle(expected * 2); assertEquals(expected, autoTableNo); } return; @@ -510,16 +509,18 @@ public class QueryMapperTest { } for (long expected = 1; expected < 5; ++expected) { - final long autoTableNo = qm.insertGetGeneratedKey("INSERT INTO a_thaoeu_table (a_thaoeu_table_val) VALUES (?)", expected * 2); + final long autoTableNo = this.qm.insertGetGeneratedKey(expected * 2); assertEquals(expected, autoTableNo); } } finally { - try { - qm.executeUpdate("DROP TABLE a_thaoeu_table"); - qm.executeUpdate("DROP SEQUENCE a_thaoeu_table_seq"); - } catch(Exception e) { - // ignore - } + if(qm != null) + try { + qm.executeUpdate("DROP TABLE a_thaoeu_table"); + qm.executeUpdate("DROP SEQUENCE a_thaoeu_table_seq"); + } catch(Exception e) { + // ignore + } + tryClose(qm); } }