Examples of CompiledStatement


Examples of com.j256.ormlite.support.CompiledStatement

    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

Examples of com.j256.ormlite.support.CompiledStatement

    OurEnum val = OurEnum.SECOND;
    LocalEnumString foo = new LocalEnumString();
    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.toString(),
          DataType.ENUM_STRING.getDataPersister()
              .resultToJava(null, results, results.findColumn(ENUM_COLUMN)));
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

Examples of com.j256.ormlite.support.CompiledStatement

    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,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      int colNum = results.findColumn(STRING_COLUMN);
      DataType.DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

Examples of com.j256.ormlite.support.CompiledStatement

      Object sqlArg, String defaultValStr, DataType dataType, String columnName, boolean isValidGeneratedType,
      boolean isAppropriateId, boolean isEscapedValue, boolean isPrimitive, boolean isSelectArgRequired,
      boolean isStreamType, boolean isComparable, boolean isConvertableId) throws Exception {
    DataPersister dataPersister = dataType.getDataPersister();
    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());
      int colNum = results.findColumn(columnName);
      Field field = clazz.getDeclaredField(columnName);
      FieldType fieldType = FieldType.createFieldType(connectionSource, TABLE_NAME, field, clazz);
      Class<?>[] classes = fieldType.getDataPersister().getAssociatedClasses();
      if (classes.length > 0) {
        assertTrue(classes[0].isAssignableFrom(fieldType.getType()));
      }
      assertTrue(fieldType.getDataPersister().isValidForField(field));
      if (javaVal instanceof byte[]) {
        assertTrue(Arrays.equals((byte[]) javaVal,
            (byte[]) dataPersister.resultToJava(fieldType, results, colNum)));
      } else {
        Map<String, Integer> colMap = new HashMap<String, Integer>();
        colMap.put(columnName, colNum);
        Object result = fieldType.resultToJava(results, colMap);
        assertEquals(javaVal, result);
      }
      if (dataType == DataType.STRING_BYTES || dataType == DataType.BYTE_ARRAY
          || dataType == DataType.SERIALIZABLE) {
        try {
          dataPersister.parseDefaultString(fieldType, "");
          fail("parseDefaultString should have thrown for " + dataType);
        } catch (SQLException e) {
          // expected
        }
      } else if (defaultValStr != null) {
        assertEquals(defaultSqlVal, dataPersister.parseDefaultString(fieldType, defaultValStr));
      }
      if (sqlArg == null) {
        // noop
      } else if (sqlArg instanceof byte[]) {
        assertTrue(Arrays.equals((byte[]) sqlArg, (byte[]) dataPersister.javaToSqlArg(fieldType, javaVal)));
      } else {
        assertEquals(sqlArg, dataPersister.javaToSqlArg(fieldType, javaVal));
      }
      assertEquals(isValidGeneratedType, dataPersister.isValidGeneratedType());
      assertEquals(isAppropriateId, dataPersister.isAppropriateId());
      assertEquals(isEscapedValue, dataPersister.isEscapedValue());
      assertEquals(isEscapedValue, dataPersister.isEscapedDefaultValue());
      assertEquals(isPrimitive, dataPersister.isPrimitive());
      assertEquals(isSelectArgRequired, dataPersister.isArgumentHolderRequired());
      assertEquals(isStreamType, dataPersister.isStreamType());
      assertEquals(isComparable, dataPersister.isComparable());
      if (isConvertableId) {
        assertNotNull(dataPersister.convertIdNumber(10));
      } else {
        assertNull(dataPersister.convertIdNumber(10));
      }
      List<T> list = dao.queryForAll();
      assertEquals(1, list.size());
      assertTrue(dao.objectsEqual(foo, list.get(0)));
      // if we have a value then look for it, floats don't find any results because of rounding issues
      if (javaVal != null && dataPersister.isComparable() && dataType != DataType.FLOAT
          && dataType != DataType.FLOAT_OBJ) {
        // test for inline arguments
        list = dao.queryForMatching(foo);
        assertEquals(1, list.size());
        assertTrue(dao.objectsEqual(foo, list.get(0)));
        // test for SelectArg arguments
        list = dao.queryForMatchingArgs(foo);
        assertEquals(1, list.size());
        assertTrue(dao.objectsEqual(foo, list.get(0)));
      }
      if (dataType == DataType.STRING_BYTES || dataType == DataType.BYTE_ARRAY
          || dataType == DataType.SERIALIZABLE) {
        // no converting from string to value
      } else {
        // test string conversion
        String stringVal = results.getString(colNum);
        Object convertedJavaVal = fieldType.convertStringToJavaField(stringVal, 0);
        assertEquals(javaVal, convertedJavaVal);
      }
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

Examples of com.j256.ormlite.support.CompiledStatement

    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,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      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

Examples of com.j256.ormlite.support.CompiledStatement

    Dao<LocalByteArray, Object> dao = createDao(clazz, true);
    LocalByteArray foo = new LocalByteArray();
    foo.byteField = new byte[] { 1, 2, 3, 4, 5 };
    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());
      FieldType fieldType =
          FieldType.createFieldType(connectionSource, TABLE_NAME,
              LocalSerializable.class.getDeclaredField(SERIALIZABLE_COLUMN), LocalSerializable.class);
      DataType.SERIALIZABLE.getDataPersister().resultToJava(fieldType, results, results.findColumn(BYTE_COLUMN));
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      connectionSource.releaseConnection(conn);
    }
  }
View Full Code Here

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],
              DatabaseConnection.DEFAULT_RESULT_FLAGS);

      DatabaseResults results = stmt.runQuery(null);
      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

Examples of com.j256.ormlite.support.CompiledStatement

  }

  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, StatementType.SELECT);
      DatabaseResults results = stmt.runQuery(null);
      int fooC = 0;
      while (results.next()) {
        LocalFoo foo2 = preparedQuery.mapRow(results);
        assertEquals(foos.get(fooC).id, foo2.id);
        fooC++;
View Full Code Here

Examples of com.j256.ormlite.support.CompiledStatement

      sb.append("DELETE FROM ");
    }
    databaseType.appendEscapedEntityName(sb, tableName);
    String statement = sb.toString();
    logger.info("clearing table '{}' with '{}", tableName, statement);
    CompiledStatement compiledStmt = null;
    DatabaseConnection connection = connectionSource.getReadWriteConnection();
    try {
      compiledStmt =
          connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      return compiledStmt.runExecute();
    } finally {
      IOUtils.closeThrowSqlException(compiledStmt, "compiled statement");
      connectionSource.releaseConnection(connection);
    }
  }
View Full Code Here

Examples of com.j256.ormlite.support.CompiledStatement

  private static int doStatements(DatabaseConnection connection, String label, Collection<String> statements,
      boolean ignoreErrors, boolean returnsNegative, boolean expectingZero) throws SQLException {
    int stmtC = 0;
    for (String statement : statements) {
      int rowC = 0;
      CompiledStatement compiledStmt = null;
      try {
        compiledStmt =
            connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes,
                DatabaseConnection.DEFAULT_RESULT_FLAGS);
        rowC = compiledStmt.runExecute();
        logger.info("executed {} table statement changed {} rows: {}", label, rowC, statement);
      } catch (SQLException e) {
        if (ignoreErrors) {
          logger.info("ignoring {} error '{}' for statement: {}", label, e, statement);
        } else {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.