Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.CompiledStatement


    MappedPreparedStmt<LocalFoo, Integer> rowMapper =
        new MappedPreparedStmt<LocalFoo, Integer>(tableInfo, null, new FieldType[0], tableInfo.getFieldTypes(),
            new ArgumentHolder[0], null, StatementType.SELECT);

    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, new FieldType[0]);

      DatabaseResults results = stmt.runQuery();
      while (results.next()) {
        LocalFoo foo2 = rowMapper.mapRow(results);
        assertEquals(foo1.id, foo2.id);
      }
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here


  }

  private void checkResults(List<LocalFoo> foos, MappedPreparedStmt<LocalFoo, Integer> preparedQuery, int expectedNum)
      throws SQLException {
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt = preparedQuery.compile(conn);
      DatabaseResults results = stmt.runQuery();
      int fooC = 0;
      while (results.next()) {
        LocalFoo foo2 = preparedQuery.mapRow(results);
        assertEquals(foos.get(fooC).id, foo2.id);
        fooC++;
      }
      assertEquals(expectedNum, fooC);
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

    Dao<LocalString, Object> dao = createDao(clazz, true);
    LocalString foo = new LocalString();
    foo.string = "not a date format";
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes);
      DatabaseResults results = stmt.runQuery();
      assertTrue(results.next());
      int colNum = results.findColumn(STRING_COLUMN);
      DataType.JAVA_DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

    Dao<LocalSerializable, Object> dao = createDao(clazz, true);
    LocalSerializable foo = new LocalSerializable();
    foo.serializable = null;
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes);
      DatabaseResults results = stmt.runQuery();
      assertTrue(results.next());
      FieldType fieldType =
          FieldType.createFieldType(connectionSource, TABLE_NAME,
              clazz.getDeclaredField(SERIALIZABLE_COLUMN), clazz);
      assertNull(DataType.SERIALIZABLE.getDataPersister().resultToJava(fieldType, results,
          results.findColumn(SERIALIZABLE_COLUMN)));
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

  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)).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)).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))).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() throws Throwable {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
            assertEquals(3, 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 {
              fail("Should only be called twice");
            }
            stmtC++;
            assertEquals(StatementType.EXECUTE, args[1]);
            assertEquals(0, ((FieldType[]) args[2]).length);
            return stmt;
          }
        }).anyTimes();
    expect(stmt.runUpdate()).andReturn(0).anyTimes();
    connectionSource.releaseConnection(conn);
    expectLastCall().anyTimes();
    stmt.close();
    expectLastCall().anyTimes();
    replay(connectionSource, conn, stmt);
    TableUtils.createTable(connectionSource, Index.class);
    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))).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() throws Throwable {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
            assertEquals(3, 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 {
              fail("Should only be called twice");
            }
            stmtC++;
            assertEquals(StatementType.EXECUTE, args[1]);
            assertEquals(0, ((FieldType[]) args[2]).length);
            return stmt;
          }
        })
        .anyTimes();
    expect(stmt.runUpdate()).andReturn(0).anyTimes();
    connectionSource.releaseConnection(conn);
    expectLastCall().anyTimes();
    stmt.close();
    expectLastCall().anyTimes();
    replay(connectionSource, conn, stmt);
    TableUtils.createTable(connectionSource, ComboIndex.class);
    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))).andThrow(
          new SQLException("you asked us to!!"));
    } else {
      expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andReturn(
          stmt);
      expect(stmt.runUpdate()).andReturn(rowN);
      stmt.close();
      if (queryAfter != null) {
        expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andReturn(
            stmt);
        results = createMock(DatabaseResults.class);
        expect(results.next()).andReturn(false);
        expect(stmt.runQuery()).andReturn(results);
        stmt.close();
        replay(results);
        rowC.incrementAndGet();
      }
    }
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
View Full Code Here

    OurEnum val = OurEnum.SECOND;
    LocalEnumInt foo = new LocalEnumInt();
    foo.ourEnum = val;
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      assertEquals(
          val.ordinal(),
          DataType.ENUM_INTEGER.getDataPersister().resultToJava(null, results,
              results.findColumn(ENUM_COLUMN)));
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
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.