@Test public void testGetNonprimitiveTypeNullAsOwnType() throws Exception { final ResultSet rs = statement.executeQuery( "SELECT CAST( NULL AS VARCHAR ) FROM INFORMATION_SCHEMA.CATALOGS" ); assertTrue( rs.next() ); assertThat( "getString(...) for NULL", rs.getString( 1 ), nullValue() ); assertThat( "wasNull", rs.wasNull(), equalTo( true ) ); }
@Test public void testGetBooleanGetsFalse() throws Exception { ResultSet rs = statement.executeQuery( "SELECT FALSE FROM INFORMATION_SCHEMA.CATALOGS" ); rs.next(); assertThat( "getBoolean(...) for FALSE", rs.getBoolean( 1 ), equalTo( false ) ); assertThat( "wasNull", rs.wasNull(), equalTo( false ) ); }
out.print(rs.getString(columnIndex)); } else if (type == Types.DATE) { Date date = rs.getDate(columnIndex); if (rs.wasNull()) { out.print("null"); } else { if (rs.wasNull()) { out.print("null"); } else { if (rs.wasNull()) { out.print("null"); } else { if (rs.wasNull()) { out.print("null"); } else { if (rs.wasNull()) { out.print("null"); } else { int value = rs.getInt(columnIndex); if (rs.wasNull()) { out.print("null"); } else { long value = rs.getLong(columnIndex);
@Test public void shouldThrowOnPropertyTypeWithoutRegisteredMapper() throws Exception { mockColumns("longField", "valueTypeField"); when(resultSet.getLong(1)).thenReturn(123L); when(resultSet.getObject(2)).thenReturn(new Object()); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldUseRegisteredMapperForUnknownPropertyType() throws Exception { handle.registerColumnMapper(new ValueTypeMapper()); mockColumns("longField", "valueTypeField"); when(resultSet.getLong(1)).thenReturn(123L); when(resultSet.getString(2)).thenReturn("foo"); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); Long expected = 123L; assertThat(sampleBean.getLongField()).isEqualTo(expected); assertThat(sampleBean.getValueTypeField()).isEqualTo(ValueType.valueOf("foo")); }
@Test public void shouldThrowOnProtectedSetter() throws Exception { mockColumns("protectedStringField"); String expected = "string"; when(resultSet.getString(1)).thenReturn(expected); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldThrowOnPackagePrivateSetter() throws Exception { mockColumns("packagePrivateIntField"); when(resultSet.getInt(1)).thenReturn(200); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
System.out.print("\n"); for(Column c : row.getColumns()) { String avg = String.format("%.2f", rs.getDouble(4 + i*cc)); if (rs.wasNull()) avg = "null"; String stddev = String.format("%.2f", rs.getDouble(7 + i*cc)); if (rs.wasNull()) stddev = "null"; String p05 = String.format("%.2f", rs.getDouble(8 + i*cc)); if (rs.wasNull()) p05 = "null"; String p25 = String.format("%.2f", rs.getDouble(9 + i*cc)); if (rs.wasNull()) p25 = "null"; String p50 = String.format("%.2f", rs.getDouble(10 + i*cc)); if (rs.wasNull()) p50 = "null"; String p75 = String.format("%.2f", rs.getDouble(11 + i*cc)); if (rs.wasNull()) p75 = "null"; String p95 = String.format("%.2f", rs.getDouble(12 + i*cc)); if (rs.wasNull()) p95 = "null"; System.out.print(String.format(fmt, c.getName(), c.getType(), cntRows, rs.getString(2 + i*cc), rs.getString(3 + i*cc), avg, rs.getString(5 + i*cc), rs.getString(6 + i*cc), stddev, p05, p25, p50, p75, p95)); i++;
@Override public LocalTime get(final ResultSet rs, final int index, final int dbSqlType) throws SQLException { if (dbSqlType == Types.VARCHAR) { String string = rs.getString(index); if (string == null) { return null; } return LocalTime.parse(string); } long time = rs.getLong(index); if (time == 0 && rs.wasNull()) { return null; } return LocalTime.ofSecondOfDay(time); } }
/** * Load an ndarray blob given an id * * @param id the id to load * @return the blob */ @Override public Blob loadForID(String id) throws SQLException { Connection c = dataSource.getConnection(); PreparedStatement preparedStatement = c.prepareStatement(loadStatement()); preparedStatement.setString(1, id); ResultSet r = preparedStatement.executeQuery(); if (r.wasNull() || !r.next()) { return null; } else { Blob first = r.getBlob(2); return first; } }
@Test public void testGetPrimitiveTypeNullAsOwnType() throws Exception { final ResultSet rs = statement.executeQuery( "SELECT CAST( NULL AS INTEGER ) FROM INFORMATION_SCHEMA.CATALOGS" ); assertTrue( rs.next() ); assertThat( "getInt(...) for NULL", rs.getInt( 1 ), equalTo( 0 ) ); assertThat( "wasNull", rs.wasNull(), equalTo( true ) ); }
@Test public void testGetBooleanGetsNull() throws Exception { ResultSet rs = statement.executeQuery( "SELECT CAST( NULL AS BOOLEAN ) FROM INFORMATION_SCHEMA.CATALOGS" ); rs.next(); assertThat( "getBoolean(...) for BOOLEAN NULL", rs.getBoolean( 1 ), equalTo( false ) ); assertThat( "wasNull", rs.wasNull(), equalTo( true ) ); }
protected List<RecoveryInfo> getAllRecoveryInfos(Connection c) throws SQLException { PreparedStatement s = c.prepareStatement("SELECT * from `positions` where client_id = ? order by last_heartbeat_read DESC"); s.setString(1, clientID); ResultSet rs = s.executeQuery(); ArrayList<RecoveryInfo> recoveries = new ArrayList<>(); while ( rs.next() ) { Long server_id = rs.getLong("server_id"); String gtid = gtidMode ? rs.getString("gtid_set") : null; Position position = new Position( BinlogPosition.at(gtid, rs.getLong("binlog_position"), rs.getString("binlog_file") ), rs.getLong("last_heartbeat_read")); if ( rs.wasNull() ) { LOGGER.warn("master recovery is ignoring position with NULL heartbeat"); } else { recoveries.add(new RecoveryInfo(position, server_id, clientID)); } } return recoveries; }
@Test public void shouldThrowOnPropertyTypeWithoutRegisteredMapper() throws Exception { mockColumns("longField", "valueTypeField"); when(resultSet.getLong(1)).thenReturn(123L); when(resultSet.getObject(2)).thenReturn(new Object()); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldUseRegisteredMapperForUnknownPropertyType() throws Exception { handle.registerColumnMapper(new ValueTypeMapper()); mockColumns("longField", "valueTypeField"); Long expected = 123L; when(resultSet.getLong(1)).thenReturn(expected); when(resultSet.getString(2)).thenReturn("foo"); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(expected); assertThat(sampleBean.getValueTypeField()).isEqualTo(ValueType.valueOf("foo")); }