@Test public void testGetInt() throws Exception { List<Query.Type> types = Arrays.asList( Query.Type.INT8, Query.Type.UINT8, Query.Type.INT16, Query.Type.UINT16, Query.Type.INT24, Query.Type.UINT24, Query.Type.INT32); for (Query.Type type : types) { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(type).build()) .addFields(Field.newBuilder().setName("null").setType(type).build()) .addRows( Query.Row.newBuilder() .addLengths("12345".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("12345"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(12345, row.getInt("col1")); Assert.assertFalse(row.wasNull()); Assert.assertEquals(0, row.getInt("null")); Assert.assertTrue(row.wasNull()); Assert.assertEquals(null, row.getObject("null", Integer.class)); Assert.assertTrue(row.wasNull()); } } }
@Test public void testGetTimestamp() throws Exception { List<Query.Type> types = Arrays.asList(Query.Type.DATETIME, Query.Type.TIMESTAMP); for (Query.Type type : types) { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(type).build()) .addFields(Field.newBuilder().setName("null").setType(type).build()) .addRows( Query.Row.newBuilder() .addLengths("2008-01-02 14:15:16.123456".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("2008-01-02 14:15:16.123456"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(Timestamp.valueOf("2008-01-02 14:15:16.123456"), row.getObject("col1")); Assert.assertEquals( Timestamp.valueOf("2008-01-02 14:15:16.123456"), row.getTimestamp("col1")); Timestamp ts = new Timestamp(1199283316000L); ts.setNanos(123456000); Assert.assertEquals(ts, row.getTimestamp("col1", GMT)); Assert.assertFalse(row.wasNull()); Assert.assertEquals(null, row.getTimestamp("null")); Assert.assertTrue(row.wasNull()); } } }
@Test public void testGetDate() throws Exception { List<Query.Type> types = Arrays.asList(Query.Type.DATE); for (Query.Type type : types) { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(type).build()) .addFields(Field.newBuilder().setName("null").setType(type).build()) .addRows( Query.Row.newBuilder() .addLengths("2008-01-02".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("2008-01-02"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(Date.valueOf("2008-01-02"), row.getObject("col1")); Assert.assertEquals(Date.valueOf("2008-01-02"), row.getDate("col1")); Assert.assertEquals(new Date(1199232000000L), row.getDate("col1", GMT)); Assert.assertFalse(row.wasNull()); Assert.assertEquals(null, row.getDate("null")); Assert.assertTrue(row.wasNull()); } } }
@Test public void testNull() throws Exception { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("null").setType(Query.Type.NULL_TYPE).build()) .addRows( Query.Row.newBuilder().addLengths(-1) // SQL NULL ) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(null, row.getObject("null")); Assert.assertTrue(row.wasNull()); } }
@Test public void testGetShort() throws Exception { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(Query.Type.YEAR).build()) .addFields(Field.newBuilder().setName("null").setType(Query.Type.YEAR).build()) .addRows( Query.Row.newBuilder() .addLengths("1234".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("1234"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(1234, row.getShort("col1")); Assert.assertFalse(row.wasNull()); Assert.assertEquals(0, row.getShort("null")); Assert.assertTrue(row.wasNull()); Assert.assertEquals(null, row.getObject("null", Short.class)); Assert.assertTrue(row.wasNull()); } }
@Test public void testGetTime() throws Exception { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(Query.Type.TIME).build()) .addFields(Field.newBuilder().setName("null").setType(Query.Type.TIME).build()) .addRows( Query.Row.newBuilder() .addLengths("12:34:56".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("12:34:56"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(Time.valueOf("12:34:56"), row.getObject("col1")); Assert.assertEquals(Time.valueOf("12:34:56"), row.getTime("col1")); Assert.assertEquals(new Time(45296000L), row.getTime("col1", GMT)); Assert.assertFalse(row.wasNull()); Assert.assertEquals(null, row.getTime("null")); Assert.assertTrue(row.wasNull()); } }