Package org.teiid.query.sql.lang

Examples of org.teiid.query.sql.lang.StoredProcedure


            Query query = (Query)command;
            return registerQuery(context, contextStore, query);
        }
        if (command instanceof ProcedureContainer) {
          if (command instanceof StoredProcedure) {
            StoredProcedure proc = (StoredProcedure)command;
            if (CoreConstants.SYSTEM_ADMIN_MODEL.equals(modelName)) {
              TupleSource result = handleSystemProcedures(context, proc);
              if (result != null) {
                return result;
              }
            } else if (proc.getGroup().isGlobalTable()) {
              return handleCachedProcedure(context, proc);
            }
            return null; //it's not a stored procedure we want to handle
          }
         
View Full Code Here


     * org.teiid.query.metadata.QueryMetadataInterface)
     */
    private void findCommandMetadata(Command command, TempMetadataStore discoveredMetadata, QueryMetadataInterface metadata)
    throws QueryMetadataException, QueryResolverException, TeiidComponentException {

        StoredProcedure storedProcedureCommand = (StoredProcedure) command;
       
        StoredProcedureInfo storedProcedureInfo = null;
        try {
          storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(storedProcedureCommand.getProcedureName());
        } catch (QueryMetadataException e) {
          String[] parts = storedProcedureCommand.getProcedureName().split("\\.", 2); //$NON-NLS-1$
        if (parts.length > 1 && parts[0].equalsIgnoreCase(metadata.getVirtualDatabaseName())) {
              try {
                storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(parts[1]);
                storedProcedureCommand.setProcedureName(parts[1]);
              } catch(QueryMetadataException e1) {
              }
          }
        if (storedProcedureInfo == null) {
          throw e;
        }
        }

        storedProcedureCommand.setUpdateCount(storedProcedureInfo.getUpdateCount());
        storedProcedureCommand.setModelID(storedProcedureInfo.getModelID());
        storedProcedureCommand.setProcedureID(storedProcedureInfo.getProcedureID());
        storedProcedureCommand.setProcedureCallableName(storedProcedureInfo.getProcedureCallableName());

        // Get old parameters as they may have expressions set on them - collect
        // those expressions to copy later into the resolved parameters
        List<SPParameter> oldParams = storedProcedureCommand.getParameters();

        boolean namedParameters = storedProcedureCommand.displayNamedParameters();
       
        // If parameter count is zero, then for the purposes of this method treat that
        // as if named parameters were used.  Even though the StoredProcedure was not
        // parsed that way, the user may have entered no parameters with the intention
        // of relying on all default values of all optional parameters.
        if (oldParams.size() == 0 || (oldParams.size() == 1 && storedProcedureCommand.isCalledWithReturn())) {
          storedProcedureCommand.setDisplayNamedParameters(true);
            namedParameters = true;
        }
       
        // Cache original input parameter expressions.  Depending on whether
        // the procedure was parsed with named or unnamed parameters, the keys
        // for this map will either be the String names of the parameters or
        // the Integer indices, as entered in the user query
        Map<Object, Expression> inputExpressions = new HashMap<Object, Expression>();
        int adjustIndex = 0;
        for (SPParameter param : oldParams) {
            if(param.getExpression() == null) {
              if (param.getParameterType() == SPParameter.RESULT_SET) {
                adjustIndex--;  //If this was already resolved, just pretend the result set param doesn't exist
            }
              continue;
            }
            if (namedParameters && param.getParameterType() != SPParameter.RETURN_VALUE) {
                if (inputExpressions.put(param.getName().toUpperCase(), param.getExpression()) != null) {
                  throw new QueryResolverException(QueryPlugin.Util.getString("ExecResolver.duplicate_named_params", param.getName().toUpperCase())); //$NON-NLS-1$
                }
            } else {
                inputExpressions.put(param.getIndex() + adjustIndex, param.getExpression());
            }
        }

        storedProcedureCommand.clearParameters();
        int origInputs = inputExpressions.size();
        /*
         * Take the values set from the stored procedure implementation, and match up with the
         * types of parameter it is from the metadata and then reset the newly joined parameters
         * into the stored procedure command.  If it is a result set get those columns and place
         * them into the stored procedure command as well.
         */
        List<SPParameter> metadataParams = storedProcedureInfo.getParameters();
        List<SPParameter> clonedMetadataParams = new ArrayList<SPParameter>(metadataParams.size());
        int inputParams = 0;
        int outParams = 0;
        boolean hasReturnValue = false;
        for (SPParameter metadataParameter : metadataParams) {
            if( (metadataParameter.getParameterType()==ParameterInfo.IN) ||
                (metadataParameter.getParameterType()==ParameterInfo.INOUT)){

                inputParams++;
            } else if (metadataParameter.getParameterType() == ParameterInfo.OUT) {
              outParams++;
            } else if (metadataParameter.getParameterType() == ParameterInfo.RETURN_VALUE) {
              hasReturnValue = true;
            }
            SPParameter clonedParam = (SPParameter)metadataParameter.clone();
            clonedMetadataParams.add(clonedParam);
            storedProcedureCommand.setParameter(clonedParam);
        }
       
        if (storedProcedureCommand.isCalledWithReturn() && !hasReturnValue) {
          throw new QueryResolverException(QueryPlugin.Util.getString("ExecResolver.return_expected", storedProcedureCommand.getGroup()))//$NON-NLS-1$
        }

        if(!namedParameters && (inputParams > inputExpressions.size())) {
            throw new QueryResolverException("ERR.015.008.0007", QueryPlugin.Util.getString("ERR.015.008.0007", inputParams, origInputs, storedProcedureCommand.getGroup())); //$NON-NLS-1$ //$NON-NLS-2$
        }
       
        // Walk through the resolved parameters and set the expressions from the
        // input parameters
        int exprIndex = 1;
        HashSet<String> expected = new HashSet<String>();
        if (storedProcedureCommand.isCalledWithReturn() && hasReturnValue) {
          for (SPParameter param : clonedMetadataParams) {
            if (param.getParameterType() == SPParameter.RETURN_VALUE) {
              Expression expr = inputExpressions.remove(exprIndex++);
                  param.setExpression(expr);
            }
          }
        }
        for (SPParameter param : clonedMetadataParams) {
            if(param.getParameterType() == SPParameter.RESULT_SET || param.getParameterType() == SPParameter.RETURN_VALUE) {
              continue;
            }
            if (namedParameters) {
                String nameKey = param.getParameterSymbol().getShortCanonicalName();
                Expression expr = inputExpressions.remove(nameKey);
                // With named parameters, have to check on optional params and default values
                if (expr == null && param.getParameterType() != ParameterInfo.OUT) {
                  expr = ResolverUtil.getDefault(param.getParameterSymbol(), metadata);
                  param.setUsingDefault(true);
                  expected.add(nameKey);
                }
                param.setExpression(expr);                   
            } else {
              if(param.getParameterType() == SPParameter.OUT) {
                continue;
              }
                Expression expr = inputExpressions.remove(exprIndex++);
                param.setExpression(expr);
            }
        }
       
        // Check for leftovers, i.e. params entered by user w/ wrong/unknown names
        if (!inputExpressions.isEmpty()) {
          if (namedParameters) {
            throw new QueryResolverException(QueryPlugin.Util.getString("ExecResolver.invalid_named_params", inputExpressions.keySet(), expected)); //$NON-NLS-1$
          }
          throw new QueryResolverException("ERR.015.008.0007", QueryPlugin.Util.getString("ERR.015.008.0007", inputParams, origInputs, storedProcedureCommand.getGroup().toString())); //$NON-NLS-1$ //$NON-NLS-2$
        }
       
        // Create temporary metadata that defines a group based on either the stored proc
        // name or the stored query name - this will be used later during planning
        String procName = storedProcedureCommand.getProcedureName();
        List tempElements = storedProcedureCommand.getProjectedSymbols();
        boolean isVirtual = storedProcedureInfo.getQueryPlan() != null;
        discoveredMetadata.addTempGroup(procName, tempElements, isVirtual);

        // Resolve tempElements against new metadata
        GroupSymbol procGroup = new GroupSymbol(storedProcedureInfo.getProcedureCallableName());
        procGroup.setProcedure(true);
        TempMetadataID tid = discoveredMetadata.getTempGroupID(procName);
        tid.setOriginalMetadataID(storedProcedureCommand.getProcedureID());
        procGroup.setMetadataID(tid);
        storedProcedureCommand.setGroup(procGroup);
    }
View Full Code Here

              command.getType() == Command.TYPE_INSERT
              || command.getType() == Command.TYPE_UPDATE
              || command.getType() == Command.TYPE_DELETE);
          //handle stored procedure calls
          if (command.getType() == Command.TYPE_STORED_PROCEDURE) {
            StoredProcedure sp = (StoredProcedure)command;
            if (sp.isCallableStatement()) {
              Map<ElementSymbol, ElementSymbol> assignments = new LinkedHashMap<ElementSymbol, ElementSymbol>();
              for (SPParameter param : sp.getParameters()) {
                if (param.getParameterType() == SPParameter.RESULT_SET
                    || param.getParameterType() == SPParameter.IN) {
                  continue;
                }
                Expression expr = param.getExpression();
View Full Code Here

  private boolean addNestedProcedure(PlanNode sourceNode,
      ProcedureContainer container, Object metadataId) throws TeiidComponentException,
      QueryMetadataException, TeiidProcessingException {
    if (container instanceof StoredProcedure) {
      StoredProcedure sp = (StoredProcedure)container;
      if (sp.getProcedureID() instanceof Procedure) {
        context.accessedPlanningObject(sp.getProcedureID());
      }
    }
    String cacheString = "transformation/" + container.getClass().getSimpleName().toUpperCase(); //$NON-NLS-1$
    Command c = (Command)metadata.getFromMetadataCache(metadataId, cacheString);
    if (c == null) {
View Full Code Here

      String fullName = metadata.getFullName(group.getMetadataID());
      String queryName = group.getName();
     
      StoredProcedureInfo storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(fullName);

      StoredProcedure storedProcedureCommand = new StoredProcedure();
      storedProcedureCommand.setProcedureRelational(true);
      storedProcedureCommand.setProcedureName(fullName);
     
      List<SPParameter> metadataParams = storedProcedureInfo.getParameters();
     
      Query procQuery = new Query();
      From from = new From();
      from.addClause(new SubqueryFromClause("X", storedProcedureCommand)); //$NON-NLS-1$
      procQuery.setFrom(from);
      Select select = new Select();
      select.addSymbol(new AllInGroupSymbol("X.*")); //$NON-NLS-1$
      procQuery.setSelect(select);
     
      List<String> accessPatternElementNames = new LinkedList<String>();
     
      int paramIndex = 1;
     
      for (SPParameter metadataParameter : metadataParams) {
          SPParameter clonedParam = (SPParameter)metadataParameter.clone();
          if (clonedParam.getParameterType()==ParameterInfo.IN || metadataParameter.getParameterType()==ParameterInfo.INOUT) {
              ElementSymbol paramSymbol = clonedParam.getParameterSymbol();
              Reference ref = new Reference(paramSymbol);
              clonedParam.setExpression(ref);
              clonedParam.setIndex(paramIndex++);
              storedProcedureCommand.setParameter(clonedParam);
             
              String aliasName = paramSymbol.getShortName();
             
              if (metadataParameter.getParameterType()==ParameterInfo.INOUT) {
                  aliasName += "_IN"; //$NON-NLS-1$
              }
             
              SingleElementSymbol newSymbol = new AliasSymbol(aliasName, new ExpressionSymbol(paramSymbol.getShortName(), ref));
             
              select.addSymbol(newSymbol);
              accessPatternElementNames.add(queryName + ElementSymbol.SEPARATOR + aliasName);
          }
      }
     
      QueryResolver.resolveCommand(procQuery, metadata.getMetadata());
     
      List<SingleElementSymbol> projectedSymbols = procQuery.getProjectedSymbols();
     
      HashSet<String> foundNames = new HashSet<String>();
     
      for (SingleElementSymbol ses : projectedSymbols) {
          if (!foundNames.add(ses.getShortCanonicalName())) {
              throw new QueryResolverException(QueryPlugin.Util.getString("SimpleQueryResolver.Proc_Relational_Name_conflict", fullName)); //$NON-NLS-1$                           
          }
      }
     
      TempMetadataID id = metadata.getMetadataStore().getTempGroupID(queryName);

      if (id == null) {
          metadata.getMetadataStore().addTempGroup(queryName, projectedSymbols, true);
         
          id = metadata.getMetadataStore().getTempGroupID(queryName);
          id.setOriginalMetadataID(storedProcedureCommand.getProcedureID());
          if (!accessPatternElementNames.isEmpty()) {
            List<TempMetadataID> accessPatternIds = new LinkedList<TempMetadataID>();
           
            for (String name : accessPatternElementNames) {
                accessPatternIds.add(metadata.getMetadataStore().getTempElementID(name));
View Full Code Here

    case Command.TYPE_UPDATE_PROCEDURE:
      CreateUpdateProcedureCommand cupc = (CreateUpdateProcedureCommand)command;
      if (cupc.isUpdateProcedure()) {
        result = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, context);
      } else {
        StoredProcedure c = (StoredProcedure)cupc.getUserCommand();
        Object pid = cupc.getVirtualGroup().getMetadataID();
        if (c != null) {
          pid = c.getProcedureID();
        }
        String fullName = metadata.getFullName(pid);
        fullName = "procedure cache:" + fullName; //$NON-NLS-1$
        PreparedPlan pp = context.getPlan(fullName);
        if (pp == null) {
          Determinism determinismLevel = context.resetDeterminismLevel();
          CommandContext clone = context.clone();
          ProcessorPlan plan = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, clone);
          //note that this is not a full prepared plan.  It is not usable by user queries.
          if (pid instanceof Procedure) {
            clone.accessedPlanningObject(pid);
          }
          pp = new PreparedPlan();
          pp.setPlan(plan, clone);
          context.putPlan(fullName, pp, context.getDeterminismLevel());
          context.setDeterminismLevel(determinismLevel);
        }
        result = pp.getPlan().clone();
        for (Object id : pp.getAccessInfo().getObjectsAccessed()) {
          context.accessedPlanningObject(id);
        }
      }
          // propagate procedure parameters to the plan to allow runtime type checking
          ProcedureContainer container = (ProcedureContainer)cupc.getUserCommand();
          ProcedurePlan plan = (ProcedurePlan)result;
          if (container != null) {
            LinkedHashMap<ElementSymbol, Expression> params = container.getProcedureParameters();
            if (container instanceof StoredProcedure) {
              plan.setRequiresTransaction(container.getUpdateCount() > 0);
              StoredProcedure sp = (StoredProcedure)container;
              if (sp.returnParameters()) {
                List<ElementSymbol> outParams = new LinkedList<ElementSymbol>();
                for (SPParameter param : sp.getParameters()) {
              if (param.getParameterType() == SPParameter.RETURN_VALUE) {
                outParams.add(param.getParameterSymbol());
              }
            }
                for (SPParameter param : sp.getParameters()) {
              if (param.getParameterType() == SPParameter.INOUT ||
                  param.getParameterType() == SPParameter.OUT) {
                outParams.add(param.getParameterSymbol());
              }
            }
View Full Code Here

        }
          response.setWarnings(responseWarnings);
         
          // If it is stored procedure, set parameters
          if (originalCommand instanceof StoredProcedure) {
            StoredProcedure proc = (StoredProcedure)originalCommand;
            if (proc.returnParameters()) {
              response.setParameters(getParameterInfo(proc));
            }
          }
          /*
           * mark the results sent at this point.
View Full Code Here

        throws QueryMetadataException, QueryResolverException, TeiidComponentException {

        findCommandMetadata(command, metadata.getMetadataStore(), metadata);
       
        //Resolve expressions on input parameters
        StoredProcedure storedProcedureCommand = (StoredProcedure) command;
        GroupContext externalGroups = storedProcedureCommand.getExternalGroupContexts();
        for (SPParameter param : storedProcedureCommand.getParameters()) {
            Expression expr = param.getExpression();
            if(expr == null) {
              continue;
            }
            for (SubqueryContainer<?> container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(expr)) {
                QueryResolver.setChildMetadata(container.getCommand(), command);
               
                QueryResolver.resolveCommand(container.getCommand(), metadata.getMetadata());
            }
            ResolverVisitor.resolveLanguageObject(expr, null, externalGroups, metadata);
            Class<?> paramType = param.getClassType();

            ResolverUtil.setDesiredType(expr, paramType, storedProcedureCommand);
           
            // Compare type of parameter expression against parameter type
            // and add implicit conversion if necessary
            Class<?> exprType = expr.getType();
            if(paramType == null || exprType == null) {
                throw new QueryResolverException("ERR.015.008.0061", QueryPlugin.Util.getString("ERR.015.008.0061", storedProcedureCommand.getProcedureName(), param.getName())); //$NON-NLS-1$ //$NON-NLS-2$
            }
            String tgtType = DataTypeManager.getDataTypeName(paramType);
            String srcType = DataTypeManager.getDataTypeName(exprType);
            Expression result = null;
                           
View Full Code Here

                Command subCommand = cmdStmt.getCommand();
               
                TempMetadataStore discoveredMetadata = resolveEmbeddedCommand(metadata, externalGroups, subCommand);
               
                if (subCommand instanceof StoredProcedure) {
                  StoredProcedure sp = (StoredProcedure)subCommand;
                  for (SPParameter param : sp.getParameters()) {
                  switch (param.getParameterType()) {
                      case ParameterInfo.OUT:
                      case ParameterInfo.RETURN_VALUE:
                        if (!isAssignable(metadata, param)) {
                              throw new QueryResolverException(QueryPlugin.Util.getString("UpdateProcedureResolver.only_variables", param.getExpression())); //$NON-NLS-1$
                        }
                        sp.setCallableStatement(true);
                        break;
                      case ParameterInfo.INOUT:
                        if (!isAssignable(metadata, param)) {
                          continue;
                        }
                        sp.setCallableStatement(true);
                        break;
                      }
          }
                }
               
View Full Code Here

                 "INSERT INTO m.g (a) VALUES (?)"//$NON-NLS-1$
                 insert);                    
    }
       
    @Test public void testStoredQueryWithNoParameter(){
      StoredProcedure storedQuery = new StoredProcedure();
      storedQuery.setProcedureName("proc1"); //$NON-NLS-1$
      helpTest("exec proc1()", "EXEC proc1()", storedQuery); //$NON-NLS-1$ //$NON-NLS-2$
      helpTest("execute proc1()", "EXEC proc1()", storedQuery); //$NON-NLS-1$ //$NON-NLS-2$
    }
View Full Code Here

TOP

Related Classes of org.teiid.query.sql.lang.StoredProcedure

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.