Пример #1
0
 @Test
 public void testGetBytes() throws Exception {
   List<Query.Type> types =
       Arrays.asList(
           Query.Type.TEXT,
           Query.Type.BLOB,
           Query.Type.VARCHAR,
           Query.Type.VARBINARY,
           Query.Type.CHAR,
           Query.Type.BINARY,
           Query.Type.BIT);
   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("hello world".length())
                         .addLengths(-1) // SQL NULL
                         .setValues(ByteString.copyFromUtf8("hello world")))
                 .build())) {
       Row row = cursor.next();
       Assert.assertNotNull(row);
       Assert.assertArrayEquals("hello world".getBytes("UTF-8"), row.getBytes("col1"));
       Assert.assertFalse(row.wasNull());
       Assert.assertEquals(null, row.getBytes("null"));
       Assert.assertTrue(row.wasNull());
     }
   }
 }
Пример #2
0
 @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());
     }
   }
 }
Пример #3
0
 @Test
 public void testGetBigDecimal() throws Exception {
   List<Query.Type> types = Arrays.asList(Query.Type.DECIMAL);
   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("1234.56789".length())
                         .addLengths(-1) // SQL NULL
                         .setValues(ByteString.copyFromUtf8("1234.56789")))
                 .build())) {
       Row row = cursor.next();
       Assert.assertNotNull(row);
       Assert.assertEquals(
           new BigDecimal(BigInteger.valueOf(123456789), 5), row.getBigDecimal("col1"));
       Assert.assertFalse(row.wasNull());
       Assert.assertEquals(null, row.getBigDecimal("null"));
       Assert.assertTrue(row.wasNull());
     }
   }
 }
Пример #4
0
 @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());
     }
   }
 }
Пример #5
0
 @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());
     }
   }
 }
Пример #6
0
 @Test
 public void testFindColumn() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(Field.newBuilder().setName("col0").build())
               .addFields(Field.newBuilder().setName("col1").build())
               .addFields(Field.newBuilder().setName("col2").build())
               .build())) {
     Assert.assertEquals(0, cursor.findColumn("col0"));
     Assert.assertEquals(1, cursor.findColumn("col1"));
     Assert.assertEquals(2, cursor.findColumn("col2"));
   }
 }
Пример #7
0
 @Test
 public void testFindColumn() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(Field.newBuilder().setName("col1").build())
               .addFields(Field.newBuilder().setName("COL2").build()) // case-insensitive
               .addFields(Field.newBuilder().setName("col1").build()) // duplicate
               .addFields(Field.newBuilder().setName("col4").build()) // skip duplicate
               .build())) {
     Assert.assertEquals(1, cursor.findColumn("col1")); // should return first col1
     Assert.assertEquals(2, cursor.findColumn("Col2")); // should be case-insensitive
     Assert.assertEquals(4, cursor.findColumn("col4")); // index should skip over duplicate
   }
 }
Пример #8
0
 @Test
 public void testGetBytes() throws Exception {
   List<Field.Type> types =
       Arrays.asList(
           Field.Type.TYPE_VARCHAR,
           Field.Type.TYPE_BIT,
           Field.Type.TYPE_TINY_BLOB,
           Field.Type.TYPE_MEDIUM_BLOB,
           Field.Type.TYPE_LONG_BLOB,
           Field.Type.TYPE_BLOB,
           Field.Type.TYPE_VAR_STRING,
           Field.Type.TYPE_STRING,
           Field.Type.TYPE_GEOMETRY);
   for (Field.Type type : types) {
     try (Cursor cursor =
         new SimpleCursor(
             QueryResult.newBuilder()
                 .addFields(Field.newBuilder().setName("col0").setType(type).build())
                 .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("hello world")))
                 .build())) {
       cursor.next();
       Assert.assertArrayEquals("hello world".getBytes("UTF-8"), cursor.getBytes("col0"));
     }
   }
 }
Пример #9
0
 @Test
 public void testNull() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(Field.newBuilder().setName("col0").setType(Field.Type.TYPE_NULL).build())
               .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("1234")))
               .build())) {
     cursor.next();
     Assert.assertEquals(null, cursor.getObject("col0"));
   }
 }
Пример #10
0
 @Test
 public void testGetULong() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(Field.newBuilder().setName("col1").setType(Query.Type.UINT64).build())
               .addFields(Field.newBuilder().setName("null").setType(Query.Type.UINT64).build())
               .addRows(
                   Query.Row.newBuilder()
                       .addLengths("18446744073709551615".length())
                       .addLengths(-1) // SQL NULL
                       .setValues(ByteString.copyFromUtf8("18446744073709551615")))
               .build())) {
     Row row = cursor.next();
     Assert.assertNotNull(row);
     Assert.assertEquals(UnsignedLong.fromLongBits(-1), row.getULong("col1"));
     Assert.assertFalse(row.wasNull());
     Assert.assertEquals(null, row.getULong("null"));
     Assert.assertTrue(row.wasNull());
   }
 }
Пример #11
0
  @Test
  public void testGetDateTime() throws Exception {
    List<Field.Type> types = Arrays.asList(Field.Type.TYPE_DATETIME, Field.Type.TYPE_TIMESTAMP);
    for (Field.Type type : types) {
      try (Cursor cursor =
          new SimpleCursor(
              QueryResult.newBuilder()
                  .addFields(Field.newBuilder().setName("col0").setType(type).build())
                  .addRows(
                      Row.newBuilder().addValues(ByteString.copyFromUtf8("2008-01-02 14:15:16")))
                  .build())) {
        cursor.next();
        Assert.assertEquals(new DateTime(2008, 1, 2, 14, 15, 16), cursor.getDateTime("col0"));
      }
    }

    types = Arrays.asList(Field.Type.TYPE_DATE, Field.Type.TYPE_NEWDATE);
    for (Field.Type type : types) {
      try (Cursor cursor =
          new SimpleCursor(
              QueryResult.newBuilder()
                  .addFields(Field.newBuilder().setName("col0").setType(type).build())
                  .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("2008-01-02")))
                  .build())) {
        cursor.next();
        Assert.assertEquals(new DateTime(2008, 1, 2, 0, 0, 0), cursor.getDateTime("col0"));
      }
    }

    try (Cursor cursor =
        new SimpleCursor(
            QueryResult.newBuilder()
                .addFields(Field.newBuilder().setName("col0").setType(Field.Type.TYPE_TIME).build())
                .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("12:34:56")))
                .build())) {
      cursor.next();
      Assert.assertEquals(new DateTime(1970, 1, 1, 12, 34, 56), cursor.getDateTime("col0"));
    }
  }
Пример #12
0
 @Test
 public void testGetFloat() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(
                   Field.newBuilder().setName("col0").setType(Field.Type.TYPE_FLOAT).build())
               .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("2.5")))
               .build())) {
     cursor.next();
     Assert.assertEquals(2.5f, cursor.getFloat("col0"), 0.01f);
   }
 }
Пример #13
0
 @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());
   }
 }
Пример #14
0
 @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());
   }
 }
Пример #15
0
 @Test
 public void testGetLong() throws Exception {
   List<Field.Type> types = Arrays.asList(Field.Type.TYPE_LONG, Field.Type.TYPE_LONGLONG);
   for (Field.Type type : types) {
     try (Cursor cursor =
         new SimpleCursor(
             QueryResult.newBuilder()
                 .addFields(Field.newBuilder().setName("col0").setType(type).build())
                 .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("12345")))
                 .build())) {
       cursor.next();
       Assert.assertEquals(12345L, cursor.getLong("col0"));
     }
   }
 }
Пример #16
0
 @Test
 public void testGetString() throws Exception {
   List<Query.Type> types = Arrays.asList(Query.Type.ENUM, Query.Type.SET);
   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("val123".length())
                         .addLengths(-1) // SQL NULL
                         .setValues(ByteString.copyFromUtf8("val123")))
                 .build())) {
       Row row = cursor.next();
       Assert.assertNotNull(row);
       Assert.assertEquals("val123", row.getString("col1"));
       Assert.assertFalse(row.wasNull());
       Assert.assertEquals(null, row.getString("null"));
       Assert.assertTrue(row.wasNull());
     }
   }
 }
Пример #17
0
 @Test
 public void testGetBigDecimal() throws Exception {
   List<Field.Type> types = Arrays.asList(Field.Type.TYPE_DECIMAL, Field.Type.TYPE_NEWDECIMAL);
   for (Field.Type type : types) {
     try (Cursor cursor =
         new SimpleCursor(
             QueryResult.newBuilder()
                 .addFields(Field.newBuilder().setName("col0").setType(type).build())
                 .addRows(Row.newBuilder().addValues(ByteString.copyFromUtf8("1234.56789")))
                 .build())) {
       cursor.next();
       Assert.assertEquals(
           new BigDecimal(BigInteger.valueOf(123456789), 5), cursor.getBigDecimal("col0"));
     }
   }
 }
Пример #18
0
 @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());
   }
 }
Пример #19
0
 @Test
 public void testGetULong() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(
                   Field.newBuilder()
                       .setName("col0")
                       .setType(Field.Type.TYPE_LONGLONG)
                       .setFlags(Field.Flag.VT_UNSIGNED_FLAG_VALUE)
                       .build())
               .addRows(
                   Row.newBuilder().addValues(ByteString.copyFromUtf8("18446744073709551615")))
               .build())) {
     cursor.next();
     Assert.assertEquals(UnsignedLong.fromLongBits(-1), cursor.getULong("col0"));
   }
 }