Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.CompiledStatement


  /**
   * Delete rows that match the prepared statement.
   */
  public int delete(DatabaseConnection databaseConnection, PreparedDelete<T> preparedDelete) throws SQLException {
    CompiledStatement stmt = preparedDelete.compile(databaseConnection);
    try {
      return stmt.runUpdate();
    } finally {
      if (stmt != null) {
        stmt.close();
      }
    }
  }
View Full Code Here


    this.limit = limit;
    this.type = type;
  }

  public CompiledStatement compile(DatabaseConnection databaseConnection) throws SQLException {
    CompiledStatement stmt = databaseConnection.compileStatement(statement, type, argFieldTypes);
    boolean ok = false;
    try {
      if (limit != null) {
        // we use this if SQL statement LIMITs are not supported by this database type
        stmt.setMaxRows(limit);
      }
      // set any arguments if we are logging our object
      Object[] argValues = null;
      if (logger.isLevelEnabled(Level.TRACE) && argHolders.length > 0) {
        argValues = new Object[argHolders.length];
      }
      for (int i = 0; i < argHolders.length; i++) {
        Object argValue = argHolders[i].getSqlArgValue();
        if (argValue == null) {
          stmt.setNull(i, argFieldTypes[i].getSqlType());
        } else {
          stmt.setObject(i, argValue, argFieldTypes[i].getSqlType());
        }
        if (argValues != null) {
          argValues[i] = argValue;
        }
      }
      logger.debug("prepared statement '{}' with {} args", statement, argHolders.length);
      if (argValues != null) {
        // need to do the (Object) cast to force args to be a single object
        logger.trace("prepared statement arguments: {}", (Object) argValues);
      }
      ok = true;
      return stmt;
    } finally {
      if (!ok) {
        stmt.close();
      }
    }
  }
View Full Code Here

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

  private static int doStatements(DatabaseConnection connection, String label, Collection<String> statements,
      boolean ignoreErrors, 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);
        rowC = compiledStmt.runUpdate();
        logger.info("executed {} table statement changed {} rows: {}", label, rowC, statement);
      } catch (SQLException e) {
        if (ignoreErrors) {
          logger.info("ignoring {} error '{}' for statement: {}", label, e.getMessage(), statement);
        } else {
          throw SqlExceptionUtil.create("SQL statement failed: " + statement, e);
        }
      } finally {
        if (compiledStmt != null) {
          compiledStmt.close();
        }
      }
      // sanity check
      if (rowC < 0) {
        throw new SQLException("SQL statement " + statement + " updated " + rowC
View Full Code Here

  private static int doCreateTestQueries(DatabaseConnection connection, DatabaseType databaseType,
      List<String> queriesAfter) throws SQLException {
    int stmtC = 0;
    // now execute any test queries which test the newly created table
    for (String query : queriesAfter) {
      CompiledStatement compiledStmt = null;
      try {
        compiledStmt = connection.compileStatement(query, StatementType.EXECUTE, noFieldTypes);
        DatabaseResults results = compiledStmt.runQuery();
        int rowC = 0;
        // count the results
        while (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
        throw SqlExceptionUtil.create("executing create table after-query failed: " + query, e);
      } finally {
        // result set is closed by the statement being closed
        if (compiledStmt != null) {
          compiledStmt.close();
        }
      }
      stmtC++;
    }
    return stmtC;
View Full Code Here

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

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

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

      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);
      DatabaseResults results = stmt.runQuery();
      assertTrue(results.next());
      int colNum = results.findColumn(columnName);
      FieldType fieldType =
          FieldType.createFieldType(connectionSource, TABLE_NAME, clazz.getDeclaredField(columnName), clazz);
      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.isSelectArgRequired());
      assertEquals(isStreamType, dataPersister.isStreamType());
      assertEquals(isComparable, dataPersister.isComparable());
      if (isConvertableId) {
        assertNotNull(dataPersister.convertIdNumber(10));
      } else {
        assertNull(dataPersister.convertIdNumber(10));
      }
      dataTypeSet.add(dataType);
    } 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.DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
    } 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.