예제 #1
0
    @Test
    public void testPlainSQLResultQuery() throws Exception {
        // [#1749] TODO Firebird renders CAST(? as VARCHAR(...)) bind values with sizes
        // pre-calculated. Hence the param needs to have some min length...
        String sql = create().select(param("p", "abc").as("p")).getSQL(false);
        ResultQuery<Record> q = create().resultQuery(sql, "10");

        Result<Record> fetch1 = q.fetch();
        assertEquals(1, fetch1.size());
        assertEquals(1, fetch1.fieldsRow().size());
        assertEquals("p", fetch1.field(0).getName());
        assertEquals("p", fetch1.field("p").getName());
        assertEquals("10", fetch1.getValue(0, 0));
        assertEquals("10", fetch1.getValue(0, "p"));
        assertEquals("10", fetch1.getValue(0, fetch1.field("p")));

        List<?> fetch2 = q.fetch("p");
        assertEquals(1, fetch2.size());
        assertEquals("10", fetch2.get(0));

        List<Long> fetch3 = q.fetch(0, Long.class);
        assertEquals(1, fetch3.size());
        assertEquals(10L, (long) fetch3.get(0));

        Record fetch4 = q.fetchAny();
        assertEquals(1, fetch4.fieldsRow().size());
        assertEquals("p", fetch4.field(0).getName());
        assertEquals("p", fetch4.field("p").getName());
        assertEquals("10", fetch4.getValue(0));
        assertEquals("10", fetch4.getValue("p"));
        assertEquals("10", fetch4.getValue(fetch4.field("p")));

        // [#1722] Check the actual returned type of arrays, also
        Object[] fetch5 = q.fetchArray("p");
        assertEquals(1, fetch5.length);
        assertEquals("10", fetch5[0]);
        assertTrue(fetch5 instanceof String[]);

        Object[] fetch6 = q.fetchArray(0);
        assertEquals(1, fetch6.length);
        assertEquals("10", fetch6[0]);
        assertTrue(fetch6 instanceof String[]);

        Long[] fetch7 = q.fetchArray(0, Long.class);
        assertEquals(1, fetch7.length);
        assertEquals(10L, (long) fetch7[0]);

        List<TestPlainSQLResultQuery> fetch8 = q.fetchInto(TestPlainSQLResultQuery.class);
        assertEquals(1, fetch8.size());
        assertEquals(10, fetch8.get(0).p);

        final Integer[] count = new Integer[] { 0 };
        q.fetchInto(new RecordHandler<Record>() {
            @Override
            public void next(Record record) {
                assertEquals(1, record.fieldsRow().size());
                assertEquals("10", record.getValue(0));
                count[0] += 1;
            }
        });

        assertEquals(1, (int) count[0]);

        FutureResult<Record> fetch9 = q.fetchLater();
        Thread.sleep(50);
        assertTrue(fetch9.isDone());
        assertEquals(1, fetch9.get().size());
        assertEquals("10", fetch9.get().getValue(0, 0));

        Cursor<Record> fetch10 = q.fetchLazy();
        assertFalse(fetch10.isClosed());
        assertTrue(fetch10.hasNext());
        assertEquals(1, fetch10.fieldsRow().size());
        assertEquals("p", fetch10.field(0).getName());
        assertEquals("10", fetch10.fetchOne().getValue(0));
        assertFalse(fetch10.isClosed());
        assertFalse(fetch10.hasNext());
        assertTrue(fetch10.isClosed());

        assertEquals(fetch1.get(0), q.fetchOne());
    }
예제 #2
0
  @Override
  public int compareTo(Record that) {
    // Note: keep this implementation in-sync with AbstractStore.equals()!

    if (that == null) {
      throw new NullPointerException();
    }
    if (size() != that.size()) {
      throw new ClassCastException(
          String.format(
              "Trying to compare incomparable records (wrong degree):\n%s\n%s", this, that));
    }

    Class<?>[] thisTypes = this.fieldsRow().types();
    Class<?>[] thatTypes = that.fieldsRow().types();

    if (!asList(thisTypes).equals(asList(thatTypes))) {
      throw new ClassCastException(
          String.format(
              "Trying to compare incomparable records (type mismatch):\n%s\n%s", this, that));
    }

    for (int i = 0; i < size(); i++) {
      final Object thisValue = getValue(i);
      final Object thatValue = that.getValue(i);

      // [#1850] Only return -1/+1 early. In all other cases,
      // continue checking the remaining fields
      if (thisValue == null && thatValue == null) {
        continue;
      }

      // Order column values in a SQL NULLS LAST manner
      else if (thisValue == null) {
        return 1;
      } else if (thatValue == null) {
        return -1;
      }

      // [#985] Compare arrays too.
      else if (thisValue.getClass().isArray() && thatValue.getClass().isArray()) {

        // Might be byte[]
        if (thisValue.getClass() == byte[].class) {
          int compare = compare((byte[]) thisValue, (byte[]) thatValue);

          if (compare != 0) {
            return compare;
          }
        }

        // Other primitive types are not expected
        else if (!thisValue.getClass().getComponentType().isPrimitive()) {
          int compare = compare((Object[]) thisValue, (Object[]) thatValue);

          if (compare != 0) {
            return compare;
          }
        } else {
          throw new ClassCastException(
              String.format("Unsupported data type in natural ordering: %s", thisValue.getClass()));
        }
      } else {
        int compare = ((Comparable) thisValue).compareTo(thatValue);

        if (compare != 0) {
          return compare;
        }
      }
    }

    // If we got through the above loop, the two records are equal
    return 0;
  }