Exemple #1
0
 private void appendDefault(BaseColumn column) {
   if (column.getDefaultValue() != null) {
     append(SPACE).append(DEFAULT).append(SPACE);
     if (BaseColumn.EXPRESSION_DEFAULT.equalsIgnoreCase(
         column.getProperty(BaseColumn.DEFAULT_HANDLING, false))) {
       append(column.getDefaultValue());
     } else {
       append(TICK)
           .append(StringUtil.replaceAll(column.getDefaultValue(), TICK, TICK + TICK))
           .append(TICK);
     }
   }
 }
Exemple #2
0
 @Override
 public String toString() {
   if (this.prefixMap != null) {
     StringBuilder sb = new StringBuilder();
     for (Map.Entry<String, String> entry : this.prefixMap.entrySet()) {
       sb.append("SET NAMESPACE '")
           .append(StringUtil.replaceAll(entry.getKey(), "'", "''"))
           .append('\'') // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
           .append(" AS ")
           .append(SQLStringVisitor.escapeSinglePart(entry.getValue()))
           .append(";\n"); // $NON-NLS-1$  //$NON-NLS-2$
     }
     return sb.append("\n").toString() + buffer.toString(); // $NON-NLS-1$
   }
   return buffer.toString();
 }
  @Override
  public void execute() throws TranslatorException {
    String worksheet = null;
    Integer limit = null;
    Integer offset = null;
    String toQuery = query;

    List<String> parts = StringUtil.tokenize(query, ';');
    for (String var : parts) {
      int index = var.indexOf('=');
      if (index == -1) {
        continue;
      }
      String key = var.substring(0, index).trim();
      String value = var.substring(index + 1).trim();

      if (key.equalsIgnoreCase(WORKSHEET)) {
        worksheet = value;
      } else if (key.equalsIgnoreCase(QUERY)) {
        StringBuilder buffer = new StringBuilder();
        SQLStringVisitor.parseNativeQueryParts(
            value,
            arguments,
            buffer,
            new SQLStringVisitor.Substitutor() {

              @Override
              public void substitute(Argument arg, StringBuilder builder, int index) {
                Literal argumentValue = arg.getArgumentValue();
                SpreadsheetSQLVisitor visitor = new SpreadsheetSQLVisitor();
                visitor.visit(argumentValue);
                builder.append(visitor.getTranslatedSQL());
              }
            });
        toQuery = buffer.toString();
      } else if (key.equalsIgnoreCase(LIMIT)) {
        limit = Integer.parseInt(value);
      } else if (key.equalsIgnoreCase(OFFEST)) {
        offset = Integer.parseInt(value);
      }
    }

    this.rowIterator =
        this.connection
            .executeQuery(worksheet, toQuery, offset, limit, executionContext.getBatchSize())
            .iterator();
  }
Exemple #4
0
 /** @return Returns the version. */
 public String getVersion() {
   if (this.version != null) {
     // normalize to allow for more increments
     StringBuilder builder = new StringBuilder();
     List<String> parts = StringUtil.split(this.version, "."); // $NON-NLS-1$
     for (int i = 0; i < parts.size(); i++) {
       if (i > 0) {
         builder.append('.');
       }
       String part = parts.get(i);
       if (part.length() < 2 && Character.isDigit(part.charAt(0))) {
         builder.append('0');
       }
       builder.append(part);
     }
     return builder.toString();
   }
   return this.version;
 }
 @Override
 public Collection getGroupsForPartialName(String partialGroupName)
     throws TeiidComponentException, QueryMetadataException {
   Collection groups = super.getGroupsForPartialName(partialGroupName);
   ArrayList<String> allGroups = new ArrayList<String>(groups);
   for (Map.Entry<String, TempMetadataID> entry : tempStore.getData().entrySet()) {
     String name = entry.getKey();
     if (StringUtil.endsWithIgnoreCase(name, partialGroupName)
         // don't want to match tables by anything less than the full name,
         // since this should be a temp or a global temp and in the latter case there's a real
         // metadata entry
         // alternatively we could check to see if the name is already in the result list
         && (name.length() == partialGroupName.length()
             || (entry.getValue().getMetadataType() != Type.TEMP
                 && name.length() > partialGroupName.length()
                 && name.charAt(name.length() - partialGroupName.length() - 1) == '.'))) {
       allGroups.add(name);
     }
   }
   return allGroups;
 }
 public void visit(Literal obj) {
   if (!isUpdate()) {
     super.visit(obj);
     return;
   }
   if (obj.getValue() == null) {
     buffer.append(NULL);
     return;
   }
   Class<?> type = obj.getType();
   if (Number.class.isAssignableFrom(type)) {
     buffer.append(obj.toString());
     return;
   } else if (obj.getType().equals(DataTypeManager.DefaultDataClasses.DATE)) {
     buffer.append(obj.getValue().toString());
     return;
   } else {
     buffer.append("\""); // $NON-NLS-1$
     buffer.append(
         StringUtil.replace(obj.getValue().toString(), "\"", "\"\"")); // $NON-NLS-1$ //$NON-NLS-2$
     buffer.append("\""); // $NON-NLS-1$
     return;
   }
 }
Exemple #7
0
  private TupleSource handleSystemProcedures(final CommandContext context, StoredProcedure proc)
      throws TeiidComponentException, QueryMetadataException, QueryProcessingException,
          QueryResolverException, QueryValidatorException, TeiidProcessingException,
          ExpressionEvaluationException {
    final QueryMetadataInterface metadata = context.getMetadata();
    if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEW)) {
      Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      String matViewName = metadata.getFullName(groupID);
      String matTableName = metadata.getFullName(matTableId);
      LogManager.logDetail(
          LogConstants.CTX_MATVIEWS, "processing refreshmatview for", matViewName); // $NON-NLS-1$
      boolean invalidate =
          Boolean.TRUE.equals(((Constant) proc.getParameter(3).getExpression()).getValue());
      boolean needsLoading =
          globalStore.needsLoading(matTableName, globalStore.getAddress(), true, true, invalidate);
      if (!needsLoading) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
      }
      GroupSymbol matTable = new GroupSymbol(matTableName);
      matTable.setMetadataID(matTableId);
      return loadGlobalTable(context, matTable, matTableName, globalStore);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROW)) {
      final Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      Object pk = metadata.getPrimaryKey(groupID);
      String matViewName = metadata.getFullName(groupID);
      if (pk == null) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30230,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
      }
      List<?> ids = metadata.getElementIDsInKey(pk);
      Constant key = (Constant) proc.getParameter(3).getExpression();
      Object initialValue = key.getValue();
      SPParameter keyOther = proc.getParameter(4);
      Object[] otherCols = null;
      int length = 1;
      if (keyOther != null) {
        otherCols = ((ArrayImpl) ((Constant) keyOther.getExpression()).getValue()).getValues();
        if (otherCols != null) {
          length += otherCols.length;
        }
      }
      if (ids.size() != length) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30231,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30231, matViewName, ids.size(), length));
      }
      final String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
      MatTableInfo info = globalStore.getMatTableInfo(matTableName);
      if (!info.isValid()) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
      }
      TempTable tempTable = globalStore.getTempTable(matTableName);
      if (!tempTable.isUpdatable()) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30232,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30232, matViewName));
      }
      Iterator<?> iter = ids.iterator();
      final Object[] params = new Object[length];
      StringBuilder criteria = new StringBuilder();
      for (int i = 0; i < length; i++) {
        Object id = iter.next();
        String targetTypeName = metadata.getElementType(id);
        Object value = i == 0 ? initialValue : otherCols[i - 1];
        value =
            DataTypeManager.transformValue(value, DataTypeManager.getDataTypeClass(targetTypeName));
        params[i] = value;
        if (i != 0) {
          criteria.append(" AND "); // $NON-NLS-1$
        }
        criteria.append(metadata.getFullName(id)).append(" = ?"); // $NON-NLS-1$
      }
      LogManager.logInfo(
          LogConstants.CTX_MATVIEWS,
          QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30012, matViewName, Arrays.toString(params)));

      String queryString =
          Reserved.SELECT
              + " * "
              + Reserved.FROM
              + ' '
              + matViewName
              + ' '
              + Reserved.WHERE
              + ' '
              + //$NON-NLS-1$
              criteria.toString()
              + ' '
              + Reserved.OPTION
              + ' '
              + Reserved.NOCACHE;
      final QueryProcessor qp =
          context
              .getQueryProcessorFactory()
              .createQueryProcessor(queryString, matViewName.toUpperCase(), context, params);
      final TupleSource ts = new BatchCollector.BatchProducerTupleSource(qp);
      return new ProxyTupleSource() {

        @Override
        protected TupleSource createTupleSource()
            throws TeiidComponentException, TeiidProcessingException {
          List<?> tuple = ts.nextTuple();
          boolean delete = false;
          if (tuple == null) {
            delete = true;
            tuple = Arrays.asList(params);
          } else {
            tuple = new ArrayList<Object>(tuple); // ensure the list is serializable
          }
          List<?> result = globalStore.updateMatViewRow(matTableName, tuple, delete);
          if (eventDistributor != null) {
            eventDistributor.updateMatViewRow(
                context.getVdbName(),
                context.getVdbVersion(),
                metadata.getName(metadata.getModelID(groupID)),
                metadata.getName(groupID),
                tuple,
                delete);
          }
          return CollectionTupleSource.createUpdateCountTupleSource(result != null ? 1 : 0);
        }

        @Override
        public void closeSource() {
          super.closeSource();
          qp.closeProcessing();
        }
      };
    }
    return null;
  }
  private TupleSource handleSystemProcedures(final CommandContext context, StoredProcedure proc)
      throws TeiidComponentException, QueryMetadataException, QueryProcessingException,
          QueryResolverException, QueryValidatorException, TeiidProcessingException,
          ExpressionEvaluationException {
    final QueryMetadataInterface metadata = context.getMetadata();
    if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEW)) {
      Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      String matViewName = metadata.getFullName(groupID);
      String matTableName = metadata.getFullName(matTableId);
      LogManager.logDetail(
          LogConstants.CTX_MATVIEWS, "processing refreshmatview for", matViewName); // $NON-NLS-1$
      boolean invalidate =
          Boolean.TRUE.equals(((Constant) proc.getParameter(3).getExpression()).getValue());
      boolean needsLoading =
          globalStore.needsLoading(matTableName, globalStore.getAddress(), true, true, invalidate);
      if (!needsLoading) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
      }
      GroupSymbol matTable = new GroupSymbol(matTableName);
      matTable.setMetadataID(matTableId);
      return loadGlobalTable(context, matTable, matTableName, globalStore);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROWS)) {
      final Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      Object pk = metadata.getPrimaryKey(groupID);
      String matViewName = metadata.getFullName(groupID);
      if (pk == null) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30230,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
      }
      List<?> ids = metadata.getElementIDsInKey(pk);
      Object[][] params =
          (Object[][])
              ((ArrayImpl) ((Constant) proc.getParameter(3).getExpression()).getValue())
                  .getValues();
      return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROW)) {
      final Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      Object pk = metadata.getPrimaryKey(groupID);
      final String matViewName = metadata.getFullName(groupID);
      if (pk == null) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30230,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
      }
      List<?> ids = metadata.getElementIDsInKey(pk);
      Constant key = (Constant) proc.getParameter(3).getExpression();
      Object initialValue = key.getValue();
      SPParameter keyOther = proc.getParameter(4);
      Object[] param = null;
      if (keyOther != null) {
        Object[] otherCols =
            ((ArrayImpl) ((Constant) keyOther.getExpression()).getValue()).getValues();
        if (otherCols != null) {
          param = new Object[1 + otherCols.length];
          param[0] = initialValue;
          for (int i = 0; i < otherCols.length; i++) {
            param[i + 1] = otherCols[i];
          }
        }
      }
      if (param == null) {
        param = new Object[] {initialValue};
      }

      Object[][] params = new Object[][] {param};

      return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    }
    return null;
  }