Search in sources :

Example 26 with StoredProcedure

use of org.teiid.query.sql.lang.StoredProcedure in project teiid by teiid.

the class AccessNode method multiSourceModify.

private static RelationalNode multiSourceModify(AccessNode accessNode, Expression ex, QueryMetadataInterface metadata, List<String> sourceNames) throws TeiidComponentException, TeiidProcessingException {
    List<AccessNode> accessNodes = new ArrayList<AccessNode>();
    boolean hasOutParams = RelationalNodeUtil.hasOutputParams(accessNode.getCommand());
    if (!Constant.NULL_CONSTANT.equals(ex)) {
        for (String sourceName : sourceNames) {
            Command command = accessNode.getCommand();
            // Modify the command to pull the instance column and evaluate the criteria
            if (!(command instanceof Insert || command instanceof StoredProcedure)) {
                command = (Command) command.clone();
                MultiSourceElementReplacementVisitor.visit(sourceName, metadata, command);
                if (!RelationalNodeUtil.shouldExecute(command, false, true)) {
                    continue;
                }
            }
            // Create a new cloned version of the access node and set it's model name to be the bindingUUID
            AccessNode instanceNode = (AccessNode) accessNode.clone();
            instanceNode.setMultiSource(false);
            instanceNode.setCommand(command);
            accessNodes.add(instanceNode);
            if (accessNodes.size() > 1 && command instanceof Insert) {
                // $NON-NLS-1$
                throw new AssertionError("Multi-source insert must target a single source.  Should have been caught in validation");
            }
            instanceNode.setConnectorBindingId(sourceName);
        }
    }
    if (hasOutParams && accessNodes.size() != 1) {
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30561, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30561, accessNode.getCommand()));
    }
    switch(accessNodes.size()) {
        case 0:
            {
                if (RelationalNodeUtil.isUpdate(accessNode.getCommand())) {
                    // should return a 0 update count
                    ProjectNode pnode = new ProjectNode(accessNode.getID());
                    pnode.setSelectSymbols(Arrays.asList(new Constant(0)));
                    return pnode;
                }
                // Replace existing access node with a NullNode
                NullNode nullNode = new NullNode(accessNode.getID());
                return nullNode;
            }
        case 1:
            {
                // Replace existing access node with new access node (simplified command)
                return accessNodes.get(0);
            }
        default:
            {
                UnionAllNode unionNode = new UnionAllNode(accessNode.getID());
                unionNode.setElements(accessNode.getElements());
                for (AccessNode newNode : accessNodes) {
                    unionNode.addChild(newNode);
                }
                RelationalNode parent = unionNode;
                // More than 1 access node - replace with a union
                if (RelationalNodeUtil.isUpdate(accessNode.getCommand())) {
                    GroupingNode groupNode = new GroupingNode(accessNode.getID());
                    AggregateSymbol sumCount = new AggregateSymbol(NonReserved.SUM, false, accessNode.getElements().get(0));
                    groupNode.setElements(Arrays.asList(sumCount));
                    groupNode.addChild(unionNode);
                    ProjectNode projectNode = new ProjectNode(accessNode.getID());
                    Expression intSum = ResolverUtil.getConversion(sumCount, DataTypeManager.getDataTypeName(sumCount.getType()), DataTypeManager.DefaultDataTypes.INTEGER, false, metadata.getFunctionLibrary());
                    List<Expression> outputElements = Arrays.asList(intSum);
                    projectNode.setElements(outputElements);
                    projectNode.setSelectSymbols(outputElements);
                    projectNode.addChild(groupNode);
                    parent = projectNode;
                }
                return parent;
            }
    }
}
Also used : AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Constant(org.teiid.query.sql.symbol.Constant) Insert(org.teiid.query.sql.lang.Insert) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) QueryCommand(org.teiid.query.sql.lang.QueryCommand) Command(org.teiid.query.sql.lang.Command) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand) Expression(org.teiid.query.sql.symbol.Expression) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 27 with StoredProcedure

use of org.teiid.query.sql.lang.StoredProcedure in project teiid by teiid.

the class UpdateProcedureResolver method resolveStatement.

private void resolveStatement(CreateProcedureCommand command, Statement statement, GroupContext externalGroups, GroupSymbol variables, TempMetadataAdapter metadata) throws QueryResolverException, QueryMetadataException, TeiidComponentException {
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving statement", statement });
    switch(statement.getType()) {
        case Statement.TYPE_IF:
            IfStatement ifStmt = (IfStatement) statement;
            Criteria ifCrit = ifStmt.getCondition();
            for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ifCrit)) {
                resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
            }
            ResolverVisitor.resolveLanguageObject(ifCrit, null, externalGroups, metadata);
            resolveBlock(command, ifStmt.getIfBlock(), externalGroups, metadata);
            if (ifStmt.hasElseBlock()) {
                resolveBlock(command, ifStmt.getElseBlock(), externalGroups, metadata);
            }
            break;
        case Statement.TYPE_COMMAND:
            CommandStatement cmdStmt = (CommandStatement) statement;
            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 (param.getExpression() != null) {
                                if (!isAssignable(metadata, param)) {
                                    throw new QueryResolverException(QueryPlugin.Event.TEIID30121, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30121, param.getExpression()));
                                }
                                sp.setCallableStatement(true);
                            }
                            break;
                        case ParameterInfo.INOUT:
                            if (!isAssignable(metadata, param)) {
                                continue;
                            }
                            sp.setCallableStatement(true);
                            break;
                    }
                }
            }
            if (discoveredMetadata != null) {
                metadata.getMetadataStore().getData().putAll(discoveredMetadata.getData());
            }
            // dynamic commands need to be updated as to their implicitly expected projected symbols
            if (subCommand instanceof DynamicCommand) {
                DynamicCommand dynCommand = (DynamicCommand) subCommand;
                if (dynCommand.getIntoGroup() == null && !dynCommand.isAsClauseSet()) {
                    if ((command.getResultSetColumns() != null && command.getResultSetColumns().isEmpty()) || !cmdStmt.isReturnable() || command.getResultSetColumns() == null) {
                        // we're not interested in the resultset
                        dynCommand.setAsColumns(Collections.EMPTY_LIST);
                    } else {
                        // should match the procedure
                        dynCommand.setAsColumns(command.getResultSetColumns());
                    }
                }
            }
            if (command.getResultSetColumns() == null && cmdStmt.isReturnable() && subCommand.returnsResultSet() && subCommand.getResultSetColumns() != null && !subCommand.getResultSetColumns().isEmpty()) {
                command.setResultSetColumns(subCommand.getResultSetColumns());
                if (command.getProjectedSymbols().isEmpty()) {
                    command.setProjectedSymbols(subCommand.getResultSetColumns());
                }
            }
            break;
        case Statement.TYPE_ERROR:
        case Statement.TYPE_ASSIGNMENT:
        case Statement.TYPE_DECLARE:
        case Statement.TYPE_RETURN:
            ExpressionStatement exprStmt = (ExpressionStatement) statement;
            // first resolve the value.  this ensures the value cannot use the variable being defined
            if (exprStmt.getExpression() != null) {
                Expression expr = exprStmt.getExpression();
                for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(expr)) {
                    resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
                }
                ResolverVisitor.resolveLanguageObject(expr, null, externalGroups, metadata);
            }
            // second resolve the variable
            switch(statement.getType()) {
                case Statement.TYPE_DECLARE:
                    collectDeclareVariable((DeclareStatement) statement, variables, metadata, externalGroups);
                    break;
                case Statement.TYPE_ASSIGNMENT:
                    AssignmentStatement assStmt = (AssignmentStatement) statement;
                    ResolverVisitor.resolveLanguageObject(assStmt.getVariable(), null, externalGroups, metadata);
                    if (!metadata.elementSupports(assStmt.getVariable().getMetadataID(), SupportConstants.Element.UPDATE)) {
                        throw new QueryResolverException(QueryPlugin.Event.TEIID30121, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30121, assStmt.getVariable()));
                    }
                    // don't allow variable assignments to be external
                    assStmt.getVariable().setIsExternalReference(false);
                    break;
                case Statement.TYPE_RETURN:
                    ReturnStatement rs = (ReturnStatement) statement;
                    if (rs.getExpression() != null) {
                        if (command.getReturnVariable() == null) {
                            throw new QueryResolverException(QueryPlugin.Event.TEIID31125, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31125, rs));
                        }
                        rs.setVariable(command.getReturnVariable().clone());
                    }
                    // else - we don't currently require the use of return for backwards compatibility
                    break;
            }
            // third ensure the type matches
            if (exprStmt.getExpression() != null) {
                Class<?> varType = exprStmt.getExpectedType();
                Class<?> exprType = exprStmt.getExpression().getType();
                if (exprType == null) {
                    throw new QueryResolverException(QueryPlugin.Event.TEIID30123, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30123));
                }
                String varTypeName = DataTypeManager.getDataTypeName(varType);
                exprStmt.setExpression(ResolverUtil.convertExpression(exprStmt.getExpression(), varTypeName, metadata));
                if (statement.getType() == Statement.TYPE_ERROR) {
                    ResolverVisitor.checkException(exprStmt.getExpression());
                }
            }
            break;
        case Statement.TYPE_WHILE:
            WhileStatement whileStmt = (WhileStatement) statement;
            Criteria whileCrit = whileStmt.getCondition();
            for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(whileCrit)) {
                resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
            }
            ResolverVisitor.resolveLanguageObject(whileCrit, null, externalGroups, metadata);
            resolveBlock(command, whileStmt.getBlock(), externalGroups, metadata);
            break;
        case Statement.TYPE_LOOP:
            LoopStatement loopStmt = (LoopStatement) statement;
            String groupName = loopStmt.getCursorName();
            isValidGroup(metadata, groupName);
            Command cmd = loopStmt.getCommand();
            resolveEmbeddedCommand(metadata, externalGroups, cmd);
            List<Expression> symbols = cmd.getProjectedSymbols();
            // add the loop cursor group into its own context
            TempMetadataStore store = metadata.getMetadataStore().clone();
            metadata = new TempMetadataAdapter(metadata.getMetadata(), store);
            externalGroups = new GroupContext(externalGroups, null);
            ProcedureContainerResolver.addScalarGroup(groupName, store, externalGroups, symbols, false);
            resolveBlock(command, loopStmt.getBlock(), externalGroups, metadata);
            break;
        case Statement.TYPE_COMPOUND:
            resolveBlock(command, (Block) statement, externalGroups, metadata);
            break;
    }
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) SubqueryContainer(org.teiid.query.sql.lang.SubqueryContainer) SPParameter(org.teiid.query.sql.lang.SPParameter) Criteria(org.teiid.query.sql.lang.Criteria) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) Command(org.teiid.query.sql.lang.Command) Expression(org.teiid.query.sql.symbol.Expression) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext)

Example 28 with StoredProcedure

use of org.teiid.query.sql.lang.StoredProcedure in project teiid by teiid.

the class TestCallableStatementParsing method helpTest.

private StoredProcedure helpTest(String call, boolean returnValue) throws QueryParserException {
    StoredProcedure sp = (StoredProcedure) QueryParser.getQueryParser().parseCommand(call);
    assertTrue(sp.isCallableStatement());
    assertEquals(returnValue, sp.returnsScalarValue());
    // $NON-NLS-1$
    assertEquals("procedure_name", sp.getProcedureName());
    return sp;
}
Also used : StoredProcedure(org.teiid.query.sql.lang.StoredProcedure)

Example 29 with StoredProcedure

use of org.teiid.query.sql.lang.StoredProcedure in project teiid by teiid.

the class TestCallableStatementParsing method helpTestGetExec.

private void helpTestGetExec(String call, boolean returnValue) throws QueryParserException {
    StoredProcedure sp = helpTest(call, returnValue);
    // $NON-NLS-1$
    assertEquals((returnValue ? "? = " : "") + "EXEC procedure_name(?, ?, ?)", sp.toString());
}
Also used : StoredProcedure(org.teiid.query.sql.lang.StoredProcedure)

Example 30 with StoredProcedure

use of org.teiid.query.sql.lang.StoredProcedure in project teiid by teiid.

the class TestProcedureResolving method testOptionalParams1.

@Test
public void testOptionalParams1() throws Exception {
    String ddl = "create foreign procedure proc (x integer, y string NOT NULL, z integer);\n";
    TransformationMetadata tm = createMetadata(ddl);
    // $NON-NLS-1$
    String sql = "call proc (1, 'a')";
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals(new Constant("a", DataTypeManager.DefaultDataClasses.STRING), sp.getParameter(2).getExpression());
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Constant(org.teiid.query.sql.symbol.Constant) Test(org.junit.Test)

Aggregations

StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)34 Test (org.junit.Test)10 SPParameter (org.teiid.query.sql.lang.SPParameter)10 Command (org.teiid.query.sql.lang.Command)9 Constant (org.teiid.query.sql.symbol.Constant)9 Expression (org.teiid.query.sql.symbol.Expression)9 ArrayList (java.util.ArrayList)8 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)8 List (java.util.List)7 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)7 QueryCommand (org.teiid.query.sql.lang.QueryCommand)5 Reference (org.teiid.query.sql.symbol.Reference)5 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)4 TeiidProcessingException (org.teiid.core.TeiidProcessingException)4 Call (org.teiid.language.Call)4 Insert (org.teiid.query.sql.lang.Insert)4 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3