diff --git a/DatabaseConnector.mpr b/DatabaseConnector.mpr index 4053c96..2b5c037 100644 Binary files a/DatabaseConnector.mpr and b/DatabaseConnector.mpr differ diff --git a/javasource/databaseconnector/impl/PreparedStatementCreatorImpl.java b/javasource/databaseconnector/impl/PreparedStatementCreatorImpl.java index bcd7e16..7b60087 100644 --- a/javasource/databaseconnector/impl/PreparedStatementCreatorImpl.java +++ b/javasource/databaseconnector/impl/PreparedStatementCreatorImpl.java @@ -5,10 +5,7 @@ import databaseconnector.interfaces.PreparedStatementCreator; import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Timestamp; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -37,26 +34,40 @@ public PreparedStatement create(IStringTemplate sql, Connection connection) thro private void addPreparedStatementParameters(List queryParameters, PreparedStatement preparedStatement) throws SQLException, IllegalArgumentException { for (int i = 0; i < queryParameters.size(); i++) { ITemplateParameter parameter = queryParameters.get(i); + Object parameterValue = parameter.getValue(); switch (parameter.getParameterType()) { case INTEGER: - preparedStatement.setLong(i + 1, (long) parameter.getValue()); + if (parameterValue == null) + preparedStatement.setNull(i + 1, Types.BIGINT); + else + preparedStatement.setLong(i + 1, (long) parameter.getValue()); break; case STRING: - preparedStatement.setString(i + 1, (String) parameter.getValue()); + if (parameterValue == null) + preparedStatement.setNull(i + 1, Types.VARCHAR); + else + preparedStatement.setString(i + 1, (String) parameter.getValue()); break; case BOOLEAN: - preparedStatement.setBoolean(i + 1, (Boolean) parameter.getValue()); + if (parameterValue == null) + preparedStatement.setNull(i + 1, Types.BOOLEAN); + else + preparedStatement.setBoolean(i + 1, (Boolean) parameter.getValue()); break; case DECIMAL: - preparedStatement.setBigDecimal(i + 1, (BigDecimal) parameter.getValue()); + if (parameterValue == null) + preparedStatement.setNull(i + 1, Types.DECIMAL); + else + preparedStatement.setBigDecimal(i + 1, (BigDecimal) parameter.getValue()); break; case DATETIME: - java.util.Date date = ((java.util.Date) parameter.getValue()); - if (date == null) - preparedStatement.setTimestamp(i + 1, null); - else + if (parameterValue == null) + preparedStatement.setNull(i + 1, Types.TIMESTAMP); + else { + java.util.Date date = ((java.util.Date) parameter.getValue()); preparedStatement.setTimestamp(i + 1, new Timestamp(date.getTime())); + } break; default: throw new IllegalArgumentException("Invalid parameter type: " + parameter.getParameterType()); diff --git a/javasource/databaseconnectortest/test/JdbcConnectorTest.java b/javasource/databaseconnectortest/test/JdbcConnectorTest.java index f01d7de..29efd4d 100644 --- a/javasource/databaseconnectortest/test/JdbcConnectorTest.java +++ b/javasource/databaseconnectortest/test/JdbcConnectorTest.java @@ -78,7 +78,8 @@ private SimpleEntry entry(String name, IMe return new SimpleEntry<>(name, type); } - @Test public void testStatementCreationException() throws SQLException { + @Test(expected = SQLException.class) + public void testStatementCreationException() throws SQLException { Exception testException = new SQLException("Test Exception Text"); when(connectionManager.getConnection(anyString(), anyString(), anyString())).thenReturn(connection); @@ -87,10 +88,11 @@ private SimpleEntry entry(String name, IMe try { jdbcConnector.executeQuery(jdbcUrl, userName, password, mockIMetaObject(), sqlQuery, context); fail("An exception should occur!"); - } catch(SQLException sqlException) {} - - verify(connection).close(); - verify(preparedStatement, never()).close(); + } + finally { + verify(connection).close(); + verify(preparedStatement, never()).close(); + } } @Test public void testObjectInstantiatorException() throws SQLException { diff --git a/javasource/databaseconnectortest/test/PreparedStatementCreatorTest.java b/javasource/databaseconnectortest/test/PreparedStatementCreatorTest.java index 3599197..b9e69ea 100644 --- a/javasource/databaseconnectortest/test/PreparedStatementCreatorTest.java +++ b/javasource/databaseconnectortest/test/PreparedStatementCreatorTest.java @@ -4,7 +4,6 @@ import com.mendix.systemwideinterfaces.javaactions.parameters.IStringTemplate; import com.mendix.systemwideinterfaces.javaactions.parameters.ITemplateParameter; import com.mendix.systemwideinterfaces.javaactions.parameters.TemplateParameterType; -import com.sap.db.jdbcext.wrapper.PreparedStatement; import databaseconnector.impl.PreparedStatementCreatorImpl; import org.junit.Before; import org.junit.Rule; @@ -14,9 +13,7 @@ import org.mockito.junit.MockitoRule; import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Timestamp; +import java.sql.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -71,8 +68,11 @@ public void testParameterTypes() throws SQLException { builder.addParameter(true, TemplateParameterType.BOOLEAN); builder.addParameter("Hello", TemplateParameterType.STRING); builder.addParameter(new BigDecimal(45.5), TemplateParameterType.DECIMAL); - builder.addParameter(null, TemplateParameterType.DATETIME); builder.addParameter(null, TemplateParameterType.STRING); + builder.addParameter(null, TemplateParameterType.DATETIME); + builder.addParameter(null, TemplateParameterType.DECIMAL); + builder.addParameter(null, TemplateParameterType.BOOLEAN); + builder.addParameter(null, TemplateParameterType.INTEGER); sut.create(builder.build(), connection); @@ -81,8 +81,11 @@ public void testParameterTypes() throws SQLException { verify(preparedStatement).setBoolean(3, true); verify(preparedStatement).setString(4, "Hello"); verify(preparedStatement).setBigDecimal(5, new BigDecimal(45.5)); - verify(preparedStatement).setTimestamp(6, null); - verify(preparedStatement).setString(7, null); + verify(preparedStatement).setNull(6, Types.VARCHAR); + verify(preparedStatement).setNull(7, Types.TIMESTAMP); + verify(preparedStatement).setNull(8, Types.DECIMAL); + verify(preparedStatement).setNull(9, Types.BOOLEAN); + verify(preparedStatement).setNull(10, Types.BIGINT); } @Test