예제 #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) {
     }
   }
 }
예제 #2
0
  @Override
  public VDBMetaData getVDB() {
    ConnectionImpl connection = null;
    if (this.vdb == null) {
      try {
        connection = getConnection();
        LocalServerConnection lsc = (LocalServerConnection) connection.getServerConnection();
        VDBMetaData vdb = lsc.getWorkContext().getVDB();
        if (vdb == null) {
          throw new NotFoundException(
              ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16001, this.vdbName, this.vdbVersion));
        }
        this.vdb = vdb;
      } catch (SQLException e) {
        throw new ServerErrorException(e.getMessage(), e);
      } finally {
        if (connection != null) {
          try {
            connection.close();
          } catch (SQLException e) {

          }
        }
      }
    }
    return this.vdb;
  }
예제 #3
0
 public void abort(Executor executor) throws SQLException {
   if (closed) {
     return;
   }
   // TODO: ensure that threads are released.  In theory they will be since close effectively
   // cancels current executions
   close();
 }
예제 #4
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) {
      }
    }
  }
예제 #5
0
  @Override
  public EntityList executeSQL(
      Query query,
      List<SQLParam> parameters,
      EdmEntitySet entitySet,
      LinkedHashMap<String, Boolean> projectedColumns,
      QueryInfo queryInfo) {
    ConnectionImpl connection = null;
    try {
      boolean cache = queryInfo != null && this.batchSize > 0;
      if (cache) {
        CacheHint hint = new CacheHint();
        hint.setTtl(this.cacheTime);
        hint.setScope(CacheDirective.Scope.USER);
        hint.setMinRows(Long.valueOf(this.batchSize));
        query.setCacheHint(hint);
      }

      boolean getCount = false;
      if (queryInfo != null) {
        getCount = queryInfo.inlineCount == InlineCount.ALLPAGES;
        if (!getCount && (queryInfo.top != null || queryInfo.skip != null)) {
          if (queryInfo.top != null && queryInfo.skip != null) {
            query.setLimit(new Limit(new Constant(queryInfo.skip), new Constant(queryInfo.top)));
          } else if (queryInfo.top != null) {
            query.setLimit(new Limit(new Constant(0), new Constant(queryInfo.top)));
          }
        }
      }

      connection = getConnection();
      String sessionId = connection.getServerConnection().getLogonResult().getSessionID();

      String skipToken = null;
      if (queryInfo != null && queryInfo.skipToken != null) {
        skipToken = queryInfo.skipToken;
        if (cache) {
          int idx = queryInfo.skipToken.indexOf(DELIMITER);
          sessionId = queryInfo.skipToken.substring(0, idx);
          skipToken = queryInfo.skipToken.substring(idx + 2);
        }
      }
      String sql = query.toString();
      if (cache) {
        sql += " /* " + sessionId + " */"; // $NON-NLS-1$ //$NON-NLS-2$
      }
      LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$

      final PreparedStatement stmt =
          connection.prepareStatement(
              sql,
              cache ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY);
      if (parameters != null && !parameters.isEmpty()) {
        for (int i = 0; i < parameters.size(); i++) {
          stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType);
        }
      }

      final ResultSet rs = stmt.executeQuery();

      if (projectedColumns == null) {
        projectedColumns = new LinkedHashMap<String, Boolean>();
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
          projectedColumns.put(rs.getMetaData().getColumnLabel(i + 1), Boolean.TRUE);
        }
      }

      EntityList result = new EntityList(invalidCharacterReplacement);

      HashMap<String, EdmProperty> propertyTypes = new HashMap<String, EdmProperty>();

      EdmEntityType entityType = entitySet.getType();
      Iterator<EdmProperty> propIter = entityType.getProperties().iterator();
      while (propIter.hasNext()) {
        EdmProperty prop = propIter.next();
        propertyTypes.put(prop.getName(), prop);
      }

      // skip to the initial position
      int count = 0;
      int skipSize = 0;
      // skip based upon the skip value
      if (getCount && queryInfo.skip != null) {
        skipSize = queryInfo.skip;
      }
      // skip based upon the skipToken
      if (skipToken != null) {
        skipSize += Integer.parseInt(skipToken);
      }
      if (skipSize > 0) {
        count += skip(cache, rs, skipSize);
      }

      // determine the number of records to return
      int size = batchSize;
      int top = Integer.MAX_VALUE;
      if (getCount && queryInfo.top != null) {
        top = queryInfo.top;
        size = top;
        if (batchSize > 0) {
          size = Math.min(batchSize, size);
        }
      } else if (size < 1) {
        size = Integer.MAX_VALUE;
      }

      // build the results
      for (int i = 0; i < size; i++) {
        if (!rs.next()) {
          break;
        }
        count++;
        result.addEntity(rs, propertyTypes, projectedColumns, entitySet);
      }

      // set the count
      if (getCount) {
        if (!cache) {
          while (rs.next()) {
            count++;
          }
        } else {
          rs.last();
          count = rs.getRow();
        }
      }
      result.setCount(count);

      // set the skipToken if needed
      if (cache && result.size() == this.batchSize) {
        int end = skipSize + result.size();
        if (getCount) {
          if (end < Math.min(top, count)) {
            result.setNextToken(nextToken(cache, sessionId, end));
          }
        } else if (rs.next()) {
          result.setNextToken(nextToken(cache, sessionId, end));
          // will force the entry to cache or is effectively a no-op when already cached
          rs.last();
        }
      }
      return result;
    } catch (Exception e) {
      throw new ServerErrorException(e.getMessage(), e);
    } finally {
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        }
      }
    }
  }
예제 #6
0
  @Override
  public BaseResponse executeCall(String sql, List<SQLParam> parameters, EdmType returnType) {
    ConnectionImpl connection = null;
    try {
      LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$
      connection = getConnection();
      final CallableStatementImpl stmt = connection.prepareCall(sql);

      int i = 1;
      if (returnType != null && returnType.isSimple()) {
        stmt.registerOutParameter(
            i++,
            JDBCSQLTypeInfo.getSQLType(
                ODataTypeManager.teiidType(returnType.getFullyQualifiedTypeName())));
      }

      if (!parameters.isEmpty()) {
        for (SQLParam param : parameters) {
          stmt.setObject(i++, param.value, param.sqlType);
        }
      }

      boolean results = stmt.execute();
      if (results) {
        final ResultSet rs = stmt.getResultSet();
        OCollection.Builder resultRows =
            OCollections.newBuilder(
                (EdmComplexType) ((EdmCollectionType) returnType).getItemType());
        while (rs.next()) {
          int idx = 1;
          List<OProperty<?>> row = new ArrayList<OProperty<?>>();
          Iterator<EdmProperty> props =
              ((EdmComplexType) ((EdmCollectionType) returnType).getItemType())
                  .getProperties()
                  .iterator();
          while (props.hasNext()) {
            EdmProperty prop = props.next();
            row.add(
                buildPropery(
                    prop.getName(),
                    prop.getType(),
                    rs.getObject(idx++),
                    invalidCharacterReplacement));
          }
          OComplexObject erow =
              OComplexObjects.create(
                  (EdmComplexType) ((EdmCollectionType) returnType).getItemType(), row);
          resultRows.add(erow);
        }
        String collectionName = returnType.getFullyQualifiedTypeName();
        collectionName = collectionName.replace("(", "_"); // $NON-NLS-1$ //$NON-NLS-2$
        collectionName = collectionName.replace(")", "_"); // $NON-NLS-1$ //$NON-NLS-2$
        return Responses.collection(resultRows.build(), null, null, null, collectionName);
      }

      if (returnType != null) {
        Object result = stmt.getObject(1);
        OProperty prop =
            buildPropery("return", returnType, result, invalidCharacterReplacement); // $NON-NLS-1$
        return Responses.property(prop);
      }
      return null;
    } catch (Exception e) {
      throw new ServerErrorException(e.getMessage(), e);
    } finally {
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        }
      }
    }
  }