All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.execution.ParameterExtractor Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.execution;

import com.google.common.collect.ImmutableMap;
import io.trino.sql.tree.DefaultTraversalVisitor;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.NodeLocation;
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.Parameter;
import io.trino.sql.tree.Statement;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.ImmutableList.toImmutableList;

public final class ParameterExtractor
{
    private ParameterExtractor() {}

    public static int getParameterCount(Statement statement)
    {
        return extractParameters(statement).size();
    }

    public static List extractParameters(Statement statement)
    {
        ParameterExtractingVisitor parameterExtractingVisitor = new ParameterExtractingVisitor();
        parameterExtractingVisitor.process(statement, null);
        return parameterExtractingVisitor.getParameters().stream()
                .sorted(Comparator.comparing(
                        parameter -> parameter.getLocation().get(),
                        Comparator.comparing(NodeLocation::getLineNumber)
                                .thenComparing(NodeLocation::getColumnNumber)))
                .collect(toImmutableList());
    }

    public static Map, Expression> bindParameters(Statement statement, List values)
    {
        List parametersList = extractParameters(statement);

        ImmutableMap.Builder, Expression> builder = ImmutableMap.builder();
        Iterator iterator = values.iterator();
        for (Parameter parameter : parametersList) {
            builder.put(NodeRef.of(parameter), iterator.next());
        }
        return builder.buildOrThrow();
    }

    private static class ParameterExtractingVisitor
            extends DefaultTraversalVisitor
    {
        private final List parameters = new ArrayList<>();

        public List getParameters()
        {
            return parameters;
        }

        @Override
        public Void visitParameter(Parameter node, Void context)
        {
            parameters.add(node);
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy