Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.CompiledStatement


  public void testUpdateThrow() throws Exception {
    TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(connectionSource, null, Foo.class);
    DatabaseConnection connection = createMock(DatabaseConnection.class);
    @SuppressWarnings("unchecked")
    PreparedUpdate<Foo> update = createMock(PreparedUpdate.class);
    CompiledStatement compiledStmt = createMock(CompiledStatement.class);
    expect(update.compile(connection, StatementType.UPDATE)).andReturn(compiledStmt);
    expect(compiledStmt.runUpdate()).andThrow(new SQLException("expected"));
    compiledStmt.close();
    StatementExecutor<Foo, String> statementExec =
        new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
    replay(connection, compiledStmt, update);
    try {
      statementExec.update(connection, update);
View Full Code Here


  public void testDeleteThrow() throws Exception {
    TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(connectionSource, null, Foo.class);
    DatabaseConnection connection = createMock(DatabaseConnection.class);
    @SuppressWarnings("unchecked")
    PreparedDelete<Foo> delete = createMock(PreparedDelete.class);
    CompiledStatement compiledStmt = createMock(CompiledStatement.class);
    expect(delete.compile(connection, StatementType.DELETE)).andReturn(compiledStmt);
    expect(compiledStmt.runUpdate()).andThrow(new SQLException("expected"));
    compiledStmt.close();
    StatementExecutor<Foo, String> statementExec =
        new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
    replay(connection, compiledStmt, delete);
    try {
      statementExec.delete(connection, delete);
View Full Code Here

  public void testIndex() throws Exception {
    final ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    final CompiledStatement stmt = createMock(CompiledStatement.class);
    expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt())).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
            assertEquals(4, args.length);
            if (stmtC == 0) {
              assertEquals("CREATE TABLE `index` (`stuff` VARCHAR(255) ) ", args[0]);
            } else if (stmtC == 1) {
              assertEquals("CREATE INDEX `index_stuff_idx` ON `index` ( `stuff` )", args[0]);
            } else if (stmtC == 2) {
              assertEquals("DROP INDEX `index_stuff_idx`", args[0]);
            } else if (stmtC == 3) {
              assertEquals("DROP TABLE `index` ", args[0]);
            } else {
              fail("Should only be called 4 times");
            }
            stmtC++;
            assertEquals(StatementType.EXECUTE, args[1]);
            assertEquals(0, ((FieldType[]) args[2]).length);
            return stmt;
          }
        })
        .anyTimes();
    expect(stmt.runExecute()).andReturn(0).anyTimes();
    connectionSource.releaseConnection(conn);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    connectionSource.releaseConnection(conn);
    expectLastCall().anyTimes();
    stmt.close();
    expectLastCall().anyTimes();
    replay(connectionSource, conn, stmt);
    TableUtils.createTable(connectionSource, Index.class);
    TableUtils.dropTable(connectionSource, Index.class, true);
    verify(connectionSource, conn, stmt);
View Full Code Here

  public void testComboIndex() throws Exception {
    final ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    final CompiledStatement stmt = createMock(CompiledStatement.class);
    expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt())).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
            assertEquals(4, args.length);
            if (stmtC == 0) {
              assertEquals("CREATE TABLE `comboindex` (`stuff` VARCHAR(255) , `junk` BIGINT ) ", args[0]);
            } else if (stmtC == 1) {
              assertEquals("CREATE INDEX `" + ComboIndex.INDEX_NAME
                  + "` ON `comboindex` ( `stuff`, `junk` )", args[0]);
            } else if (stmtC == 2) {
              assertEquals("DROP INDEX `" + ComboIndex.INDEX_NAME + "`", args[0]);
            } else if (stmtC == 3) {
              assertEquals("DROP TABLE `comboindex` ", args[0]);
            } else {
              fail("Should only be called 4 times");
            }
            stmtC++;
            assertEquals(StatementType.EXECUTE, args[1]);
            assertEquals(0, ((FieldType[]) args[2]).length);
            return stmt;
          }
        })
        .anyTimes();
    expect(stmt.runExecute()).andReturn(0).anyTimes();
    connectionSource.releaseConnection(conn);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    connectionSource.releaseConnection(conn);
    expectLastCall().anyTimes();
    stmt.close();
    expectLastCall().anyTimes();
    replay(connectionSource, conn, stmt);
    TableUtils.createTable(connectionSource, ComboIndex.class);
    TableUtils.dropTable(connectionSource, ComboIndex.class, false);
    verify(connectionSource, conn, stmt);
View Full Code Here

  public void testUniqueIndex() throws Exception {
    final ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    final CompiledStatement stmt = createMock(CompiledStatement.class);
    expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class), anyInt())).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
            assertEquals(4, args.length);
            if (stmtC == 0) {
              assertEquals("CREATE TABLE `uniqueindex` (`stuff` VARCHAR(255) ) ", args[0]);
            } else if (stmtC == 1) {
              assertEquals("CREATE UNIQUE INDEX `uniqueindex_stuff_idx` ON `uniqueindex` ( `stuff` )",
                  args[0]);
            } else if (stmtC == 2) {
              assertEquals("DROP INDEX `uniqueindex_stuff_idx`", args[0]);
            } else if (stmtC == 3) {
              assertEquals("DROP TABLE `uniqueindex` ", args[0]);
            } else {
              fail("Should only be called 4 times");
            }
            stmtC++;
            assertEquals(StatementType.EXECUTE, args[1]);
            assertEquals(0, ((FieldType[]) args[2]).length);
            return stmt;
          }
        }).anyTimes();
    expect(stmt.runExecute()).andReturn(0).anyTimes();
    connectionSource.releaseConnection(conn);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    connectionSource.releaseConnection(conn);
    expectLastCall().anyTimes();
    stmt.close();
    expectLastCall().anyTimes();
    replay(connectionSource, conn, stmt);
    TableUtils.createTable(connectionSource, UniqueIndex.class);
    TableUtils.dropTable(connectionSource, UniqueIndex.class, false);
    verify(connectionSource, conn, stmt);
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();
      }
    }
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
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());
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());
      }
View Full Code Here

    logger.debug("executing raw query for long: {}", query);
    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);
      }
View Full Code Here

   * Create and return an {@link SelectIterator} for the class using a prepared statement.
   */
  public SelectIterator<T, ID> buildIterator(BaseDaoImpl<T, ID> classDao, ConnectionSource connectionSource,
      PreparedStmt<T> preparedStmt, ObjectCache objectCache, int resultFlags) throws SQLException {
    DatabaseConnection connection = connectionSource.getReadOnlyConnection();
    CompiledStatement compiledStatement = null;
    try {
      compiledStatement = preparedStmt.compile(connection, StatementType.SELECT, resultFlags);
      SelectIterator<T, ID> iterator =
          new SelectIterator<T, ID>(tableInfo.getDataClass(), classDao, preparedStmt, connectionSource,
              connection, compiledStatement, preparedStmt.getStatement(), objectCache);
View Full Code Here

TOP

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

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.