Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.DatabaseResults


      try {
        compiledStmt =
            connection.compileStatement(query, StatementType.SELECT, noFieldTypes,
                DatabaseConnection.DEFAULT_RESULT_FLAGS);
        // we don't care about an object cache here
        DatabaseResults results = compiledStmt.runQuery(null);
        int rowC = 0;
        // count the results
        for (boolean isThereMore = results.first(); isThereMore; isThereMore = results.next()) {
          rowC++;
        }
        logger.info("executing create table after-query got {} results: {}", rowC, query);
      } catch (SQLException e) {
        // we do this to make sure that the statement is in the exception
View Full Code Here


    assertEquals(1, dao.create(foo));

    CloseableIterator<Foo> iterator =
        dao.iterator(dao.queryBuilder().where().eq(Foo.VAL_COLUMN_NAME, foo.val).prepare());
    try {
      DatabaseResults results = iterator.getRawResults();
      assertTrue(results.first());
      assertTrue(results.getColumnCount() >= 4);
      int valIndex = results.findColumn(Foo.VAL_COLUMN_NAME);
      assertEquals(foo.val, results.getInt(valIndex));
      assertFalse(results.next());
    } finally {
      iterator.closeQuietly();
    }
  }
View Full Code Here

    PreparedStatement stmt =
        connection.prepareStatement(statement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    if (args != null) {
      statementSetArgs(stmt, args, argFieldTypes);
    }
    DatabaseResults results = new H2DatabaseResults(stmt.executeQuery(), objectCache);
    if (!results.next()) {
      // no results at all
      IOUtils.closeThrowSqlException(results, "results");
      return null;
    }
    T first = rowMapper.mapRow(results);
    if (results.next()) {
      return MORE_THAN_ONE;
    } else {
      return first;
    }
  }
View Full Code Here

    QueryBuilder<Foo, Object> qb = dao.queryBuilder();
    qb.selectColumns(Foo.ID_COLUMN_NAME, Foo.VAL_COLUMN_NAME);
    CloseableIterator<Foo> iterator = dao.iterator();
    try {
      DatabaseResults results = iterator.getRawResults();
      for (int count = 0; results.next(); count++) {
        Foo foo = dao.mapSelectStarRow(results);
        switch (count) {
          case 0 :
            assertEquals(foo1.id, foo.id);
            assertEquals(foo1.val, foo.val);
View Full Code Here

   * Return the first object that matches the {@link PreparedStmt} or null if none.
   */
  public T queryForFirst(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt, ObjectCache objectCache)
      throws SQLException {
    CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT);
    DatabaseResults results = null;
    try {
      results = compiledStatement.runQuery(objectCache);
      if (results.first()) {
        logger.debug("query-for-first of '{}' returned at least 1 result", preparedStmt.getStatement());
        return preparedStmt.mapRow(results);
      } else {
        logger.debug("query-for-first of '{}' returned at 0 results", preparedStmt.getStatement());
        return null;
View Full Code Here

  /**
   * Return a long value from a prepared query.
   */
  public long queryForLong(DatabaseConnection databaseConnection, PreparedStmt<T> preparedStmt) throws SQLException {
    CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT_LONG);
    DatabaseResults results = null;
    try {
      results = compiledStatement.runQuery(null);
      if (results.first()) {
        return results.getLong(0);
      } else {
        throw new SQLException("No result found in queryForLong: " + preparedStmt.getStatement());
      }
    } finally {
      IOUtils.closeThrowSqlException(results, "results");
View Full Code Here

    if (arguments.length > 0) {
      // need to do the (Object) cast to force args to be a single object
      logger.trace("query arguments: {}", (Object) arguments);
    }
    CompiledStatement compiledStatement = null;
    DatabaseResults results = null;
    try {
      compiledStatement =
          databaseConnection.compileStatement(query, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      assignStatementArguments(compiledStatement, arguments);
      results = compiledStatement.runQuery(null);
      if (results.first()) {
        return results.getLong(0);
      } else {
        throw new SQLException("No result found in queryForLong: " + query);
      }
    } finally {
      IOUtils.closeThrowSqlException(results, "results");
View Full Code Here

    PreparedQuery<Foo> prepared = dao.queryBuilder().prepare();
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement compiled = null;
    try {
      compiled = prepared.compile(conn, StatementType.SELECT);
      DatabaseResults results = compiled.runQuery(null);
      assertTrue(results.next());
      Foo result = dao.mapSelectStarRow(results);
      assertEquals(foo.id, result.id);
      GenericRowMapper<Foo> mapper = dao.getSelectStarRowMapper();
      result = mapper.mapRow(results);
      assertEquals(foo.id, result.id);
View Full Code Here

  @Test(expected = RuntimeException.class)
  public void testMapSelectStarRowThrow() throws Exception {
    @SuppressWarnings("unchecked")
    Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
    RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
    DatabaseResults results = createMock(DatabaseResults.class);
    expect(dao.mapSelectStarRow(results)).andThrow(new SQLException("Testing catch"));
    replay(dao);
    rtDao.mapSelectStarRow(results);
    verify(dao);
  }
View Full Code Here

  private void testStatement(ConnectionSource connectionSource, DatabaseType databaseType, String statement,
      String queryAfter, int rowN, boolean throwExecute, Callable<Integer> callable) throws Exception {
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    CompiledStatement stmt = createMock(CompiledStatement.class);
    DatabaseResults results = null;
    final AtomicInteger rowC = new AtomicInteger(1);
    if (throwExecute) {
      expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt())).andThrow(
          new SQLException("you asked us to!!"));
    } else {
      expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt())).andReturn(
          stmt);
      expect(stmt.runExecute()).andReturn(rowN);
      stmt.close();
      if (queryAfter != null) {
        expect(
            conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class),
                anyInt())).andReturn(stmt);
        results = createMock(DatabaseResults.class);
        expect(results.first()).andReturn(false);
        expect(stmt.runQuery(null)).andReturn(results);
        stmt.close();
        replay(results);
        rowC.incrementAndGet();
      }
View Full Code Here

TOP

Related Classes of com.j256.ormlite.support.DatabaseResults

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.