mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2025-02-16 07:00:16 -05:00
Fix bug with generated SQL for getGeneratedKey, add unit test for it
This commit is contained in:
parent
f9dff338da
commit
f24ba9ba0b
@ -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(", ");
|
||||
|
@ -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<FieldPerson> getFieldPeopleNotIn(List<Long> 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;
|
||||
}
|
||||
|
@ -403,4 +403,14 @@ public class QueryMapperQmDao implements QmDao {
|
||||
public List<FieldPerson> getFieldPeopleNotIn(final List<Long> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user