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

io.trino.metadata.ViewDefinition 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.metadata;

import io.trino.spi.connector.CatalogSchemaName;
import io.trino.spi.connector.ConnectorViewDefinition;
import io.trino.spi.security.Identity;

import java.util.List;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;

public class ViewDefinition
{
    private final String originalSql;
    private final Optional catalog;
    private final Optional schema;
    private final List columns;
    private final Optional comment;
    private final Optional runAsIdentity;
    private final List path;

    public ViewDefinition(
            String originalSql,
            Optional catalog,
            Optional schema,
            List columns,
            Optional comment,
            Optional runAsIdentity,
            List path)
    {
        this.originalSql = requireNonNull(originalSql, "originalSql is null");
        this.catalog = requireNonNull(catalog, "catalog is null");
        this.schema = requireNonNull(schema, "schema is null");
        this.columns = List.copyOf(requireNonNull(columns, "columns is null"));
        this.comment = requireNonNull(comment, "comment is null");
        this.runAsIdentity = requireNonNull(runAsIdentity, "runAsIdentity is null");
        this.path = requireNonNull(path, "path is null");
        checkArgument(schema.isEmpty() || catalog.isPresent(), "catalog must be present if schema is present");
        checkArgument(!columns.isEmpty(), "columns list is empty");
    }

    public String getOriginalSql()
    {
        return originalSql;
    }

    public Optional getCatalog()
    {
        return catalog;
    }

    public Optional getSchema()
    {
        return schema;
    }

    public List getColumns()
    {
        return columns;
    }

    public Optional getComment()
    {
        return comment;
    }

    public boolean isRunAsInvoker()
    {
        return runAsIdentity.isEmpty();
    }

    public Optional getRunAsIdentity()
    {
        return runAsIdentity;
    }

    public List getPath()
    {
        return path;
    }

    public ConnectorViewDefinition toConnectorViewDefinition()
    {
        return new ConnectorViewDefinition(
                originalSql,
                catalog,
                schema,
                columns.stream()
                        .map(column -> new ConnectorViewDefinition.ViewColumn(column.getName(), column.getType(), column.getComment()))
                        .collect(toImmutableList()),
                comment,
                runAsIdentity.map(Identity::getUser),
                runAsIdentity.isEmpty(),
                path);
    }

    @Override
    public String toString()
    {
        return toStringHelper(this).omitNullValues()
                .add("originalSql", originalSql)
                .add("catalog", catalog.orElse(null))
                .add("schema", schema.orElse(null))
                .add("columns", columns)
                .add("comment", comment.orElse(null))
                .add("runAsIdentity", runAsIdentity.orElse(null))
                .add("path", path)
                .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy