@Override
  public final R fetchAny() {
    Cursor<R> c = fetchLazy();

    try {
      return c.fetchOne();
    } finally {
      c.close();
    }
  }
Exemple #2
0
    @Test
    public void testPlainSQLWithQueryParts() throws Exception {
        // Mix {keywords} with {numbered placeholders}
        String sql = "{select} {0}, a.{1} {from} {2} a {where} {3} = {4}";
        QueryPart[] parts = {
            val("a"), name(TAuthor_LAST_NAME().getName()), name(TAuthor().getName()), name(TAuthor_ID().getName()), inline(1)
        };

        Record author = create()
                .select(val("a"), TAuthor_LAST_NAME())
                .from(TAuthor())
                .where(TAuthor_ID().equal(1))
                .fetchOne();

        Record record = create().fetchOne(sql, parts);
        Result<Record> result = create().fetch(sql, parts);
        Cursor<Record> cursor = create().fetchLazy(sql, parts);

        assertEquals(author, record);
        assertEquals(author, result.get(0));
        assertEquals(author, cursor.fetchOne());
    }
  @Override
  protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
    Connection connection = ctx.getConnection();
    boolean autoCommit = false;

    // [#706] Postgres requires two separate queries running in the same
    // transaction to be executed when fetching refcursor types
    if (ctx.getDialect() == SQLDialect.POSTGRES && isSelectingRefCursor()) {
      autoCommit = connection.getAutoCommit();

      if (autoCommit) {
        if (log.isDebugEnabled()) log.debug("Unsetting auto-commit", false);

        connection.setAutoCommit(false);
      }
    }

    try {
      listener.executeStart(ctx);

      // JTDS doesn't seem to implement PreparedStatement.execute()
      // correctly, at least not for sp_help
      if (ctx.getDialect() == ASE) {
        ctx.resultSet(ctx.statement().executeQuery());
      }

      // [#1232] Avoid executeQuery() in order to handle queries that may
      // not return a ResultSet, e.g. SQLite's pragma foreign_key_list(table)
      else if (ctx.statement().execute()) {
        ctx.resultSet(ctx.statement().getResultSet());
      }

      listener.executeEnd(ctx);

      // Fetch a single result set
      if (!many) {
        if (ctx.resultSet() != null) {
          FieldList fields = new FieldList(getFields(ctx.resultSet().getMetaData()));
          cursor = new CursorImpl<R>(ctx, listener, fields, getRecordType());

          if (!lazy) {
            result = cursor.fetch();
            cursor = null;
          }
        } else {
          result = new ResultImpl<R>(ctx, new FieldList());
        }
      }

      // Fetch several result sets
      else {
        results = new ArrayList<Result<Record>>();
        boolean anyResults = false;

        while (ctx.resultSet() != null) {
          anyResults = true;

          FieldProvider fields = new MetaDataFieldProvider(ctx, ctx.resultSet().getMetaData());
          Cursor<Record> c = new CursorImpl<Record>(ctx, listener, fields, true);
          results.add(c.fetch());

          if (ctx.statement().getMoreResults()) {
            ctx.resultSet(ctx.statement().getResultSet());
          } else {
            ctx.resultSet(null);
          }
        }

        // Call this only when there was at least one ResultSet.
        // Otherwise, this call is not supported by ojdbc...
        if (anyResults) {
          ctx.statement().getMoreResults(Statement.CLOSE_ALL_RESULTS);
        }

        ctx.statement().close();
      }
    } finally {
      if (autoCommit) {
        if (log.isDebugEnabled()) log.debug("Resetting auto-commit", autoCommit);

        connection.setAutoCommit(autoCommit);
      }
    }

    return result != null ? result.size() : 0;
  }
Exemple #4
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());
    }
    @Test
    public void testExecuteListenerFetchLazyTest() throws Exception {
        Executor create = create();
        create.getExecuteListeners().add(new FetchLazyListener());
        FetchLazyListener.reset();

        create.selectFrom(TAuthor()).fetch();
        assertEquals(1, FetchLazyListener.countStart);
        assertEquals(1, FetchLazyListener.countRenderStart);
        assertEquals(1, FetchLazyListener.countRenderEnd);
        assertEquals(1, FetchLazyListener.countPrepareStart);
        assertEquals(1, FetchLazyListener.countPrepareEnd);
        assertEquals(1, FetchLazyListener.countBindStart);
        assertEquals(1, FetchLazyListener.countBindEnd);
        assertEquals(1, FetchLazyListener.countExecuteStart);
        assertEquals(1, FetchLazyListener.countExecuteEnd);
        assertEquals(1, FetchLazyListener.countFetchStart);
        assertEquals(1, FetchLazyListener.countResultStart);
        assertEquals(2, FetchLazyListener.countRecordStart);
        assertEquals(2, FetchLazyListener.countRecordEnd);
        assertEquals(1, FetchLazyListener.countResultEnd);
        assertEquals(1, FetchLazyListener.countFetchEnd);
        assertEquals(1, FetchLazyListener.countEnd);
        assertEquals(0, FetchLazyListener.countException);

        // [#1868] fetchLazy should behave almost the same as fetch
        FetchLazyListener.reset();
        Cursor<A> cursor = create.selectFrom(TAuthor()).fetchLazy();
        assertEquals(1, FetchLazyListener.countStart);
        assertEquals(1, FetchLazyListener.countRenderStart);
        assertEquals(1, FetchLazyListener.countRenderEnd);
        assertEquals(1, FetchLazyListener.countPrepareStart);
        assertEquals(1, FetchLazyListener.countPrepareEnd);
        assertEquals(1, FetchLazyListener.countBindStart);
        assertEquals(1, FetchLazyListener.countBindEnd);
        assertEquals(1, FetchLazyListener.countExecuteStart);
        assertEquals(1, FetchLazyListener.countExecuteEnd);
        assertEquals(0, FetchLazyListener.countFetchStart);
        assertEquals(0, FetchLazyListener.countResultStart);
        assertEquals(0, FetchLazyListener.countRecordStart);
        assertEquals(0, FetchLazyListener.countRecordEnd);
        assertEquals(0, FetchLazyListener.countResultEnd);
        assertEquals(0, FetchLazyListener.countFetchEnd);
        assertEquals(0, FetchLazyListener.countEnd);
        assertEquals(0, FetchLazyListener.countException);

        cursor.fetchOne();
        assertEquals(1, FetchLazyListener.countStart);
        assertEquals(1, FetchLazyListener.countRenderStart);
        assertEquals(1, FetchLazyListener.countRenderEnd);
        assertEquals(1, FetchLazyListener.countPrepareStart);
        assertEquals(1, FetchLazyListener.countPrepareEnd);
        assertEquals(1, FetchLazyListener.countBindStart);
        assertEquals(1, FetchLazyListener.countBindEnd);
        assertEquals(1, FetchLazyListener.countExecuteStart);
        assertEquals(1, FetchLazyListener.countExecuteEnd);
        assertEquals(1, FetchLazyListener.countFetchStart);
        assertEquals(1, FetchLazyListener.countResultStart);
        assertEquals(1, FetchLazyListener.countRecordStart);
        assertEquals(1, FetchLazyListener.countRecordEnd);
        assertEquals(1, FetchLazyListener.countResultEnd);
        assertEquals(0, FetchLazyListener.countFetchEnd);
        assertEquals(0, FetchLazyListener.countEnd);
        assertEquals(0, FetchLazyListener.countException);

        cursor.fetchOne();
        assertEquals(1, FetchLazyListener.countStart);
        assertEquals(1, FetchLazyListener.countRenderStart);
        assertEquals(1, FetchLazyListener.countRenderEnd);
        assertEquals(1, FetchLazyListener.countPrepareStart);
        assertEquals(1, FetchLazyListener.countPrepareEnd);
        assertEquals(1, FetchLazyListener.countBindStart);
        assertEquals(1, FetchLazyListener.countBindEnd);
        assertEquals(1, FetchLazyListener.countExecuteStart);
        assertEquals(1, FetchLazyListener.countExecuteEnd);
        assertEquals(1, FetchLazyListener.countFetchStart);
        assertEquals(2, FetchLazyListener.countResultStart);
        assertEquals(2, FetchLazyListener.countRecordStart);
        assertEquals(2, FetchLazyListener.countRecordEnd);
        assertEquals(2, FetchLazyListener.countResultEnd);
        assertEquals(0, FetchLazyListener.countFetchEnd);
        assertEquals(0, FetchLazyListener.countEnd);
        assertEquals(0, FetchLazyListener.countException);

        cursor.fetchOne();
        assertEquals(1, FetchLazyListener.countStart);
        assertEquals(1, FetchLazyListener.countRenderStart);
        assertEquals(1, FetchLazyListener.countRenderEnd);
        assertEquals(1, FetchLazyListener.countPrepareStart);
        assertEquals(1, FetchLazyListener.countPrepareEnd);
        assertEquals(1, FetchLazyListener.countBindStart);
        assertEquals(1, FetchLazyListener.countBindEnd);
        assertEquals(1, FetchLazyListener.countExecuteStart);
        assertEquals(1, FetchLazyListener.countExecuteEnd);
        assertEquals(1, FetchLazyListener.countFetchStart);
        assertEquals(3, FetchLazyListener.countResultStart);
        assertEquals(2, FetchLazyListener.countRecordStart);
        assertEquals(2, FetchLazyListener.countRecordEnd);
        assertEquals(3, FetchLazyListener.countResultEnd);
        assertEquals(1, FetchLazyListener.countFetchEnd);
        assertEquals(1, FetchLazyListener.countEnd);
        assertEquals(0, FetchLazyListener.countException);
    }