MySQLTextResultSetRowPacket implements MySQLPacket { @Override public void write(final MySQLPacketPayload payload) { for (Object each : data) { if (null == each) { payload.writeInt1(NULL); } else { if (each instanceof byte[]) { payload.writeBytesLenenc((byte[]) each); } else if ((each instanceof Timestamp) && (0 == ((Timestamp) each).getNanos())) { payload.writeStringLenenc(each.toString().split("\\.")[0]); } else if (each instanceof BigDecimal) { payload.writeStringLenenc(((BigDecimal) each).toPlainString()); } else if (each instanceof Boolean) { payload.writeBytesLenenc((Boolean) each ? new byte[]{1} : new byte[]{0}); } else { payload.writeStringLenenc(each.toString()); } } } } MySQLTextResultSetRowPacket(final MySQLPacketPayload payload, final int columnCount); @Override void write(final MySQLPacketPayload payload);  }
@Test public void assertWrite() { long now = System.currentTimeMillis(); Timestamp timestamp = new Timestamp(now); MySQLTextResultSetRowPacket actual = new MySQLTextResultSetRowPacket(1, Arrays.asList(null, "value", BigDecimal.ONE, new byte[] {}, timestamp)); actual.write(payload); verify(payload).writeInt1(0xfb); verify(payload).writeStringLenenc("value"); verify(payload).writeStringLenenc("1"); if (0 == timestamp.getNanos()) { verify(payload).writeStringLenenc(timestamp.toString().split("\\.")[0]); } else { verify(payload).writeStringLenenc(timestamp.toString()); } }
@Test public void assertTimestampWithoutNanos() { long now = System.currentTimeMillis() / 1000 * 1000; Timestamp timestamp = new Timestamp(now); MySQLTextResultSetRowPacket actual = new MySQLTextResultSetRowPacket(1, Arrays.asList(null, "value", BigDecimal.ONE, new byte[]{}, timestamp)); actual.write(payload); verify(payload).writeInt1(0xfb); verify(payload).writeStringLenenc("value"); verify(payload).writeStringLenenc("1"); verify(payload).writeStringLenenc(timestamp.toString().split("\\.")[0]); }