Ejemplo n.º 1
0
 @Override
 public CountResponse executeCount(Query query, List<SQLParam> parameters) {
   ConnectionImpl connection = null;
   try {
     String sql = query.toString();
     LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$
     connection = getConnection();
     final PreparedStatementImpl stmt = connection.prepareStatement(sql);
     if (!parameters.isEmpty()) {
       for (int i = 0; i < parameters.size(); i++) {
         stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType);
       }
     }
     ResultSet rs = stmt.executeQuery();
     rs.next();
     int count = rs.getInt(1);
     rs.close();
     stmt.close();
     return Responses.count(count);
   } catch (Exception e) {
     throw new ServerErrorException(e.getMessage(), e);
   } finally {
     try {
       if (connection != null) {
         connection.close();
       }
     } catch (SQLException e) {
     }
   }
 }
Ejemplo n.º 2
0
  @Override
  public UpdateResponse executeUpdate(Command query, List<SQLParam> parameters) {
    ConnectionImpl connection = null;
    try {
      String sql = query.toString();
      LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$
      connection = getConnection();
      final PreparedStatementImpl stmt =
          connection.prepareStatement(
              sql,
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY,
              ResultSet.HOLD_CURSORS_OVER_COMMIT,
              Statement.RETURN_GENERATED_KEYS);
      if (!parameters.isEmpty()) {
        for (int i = 0; i < parameters.size(); i++) {
          stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType);
        }
      }
      final int count = stmt.executeUpdate();
      final Map<String, Object> keys = getGeneratedKeys(stmt.getGeneratedKeys());
      stmt.close();
      return new UpdateResponse() {
        @Override
        public Map<String, Object> getGeneratedKeys() {
          return keys;
        }

        @Override
        public int getUpdateCount() {
          return count;
        }
      };
    } catch (Exception e) {
      throw new ServerErrorException(e.getMessage(), e);
    } finally {
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (SQLException e) {
      }
    }
  }