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

com.facebook.presto.jdbc.internal.spi.ConnectorTableMetadata Maven / Gradle / Ivy

There is a newer version: 0.289
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 com.facebook.presto.jdbc.internal.spi;

import com.facebook.presto.jdbc.internal.spi.constraints.TableConstraint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

public class ConnectorTableMetadata
{
    private final SchemaTableName table;
    private final Optional comment;
    private final List columns;
    private final Map properties;
    private final List> tableConstraints;

    public ConnectorTableMetadata(SchemaTableName table, List columns)
    {
        this(table, columns, emptyMap());
    }

    public ConnectorTableMetadata(SchemaTableName table, List columns, Map properties)
    {
        this(table, columns, properties, Optional.empty(), emptyList());
    }

    public ConnectorTableMetadata(SchemaTableName table, List columns, Map properties, Optional comment)
    {
        this(table, columns, properties, comment, emptyList());
    }

    public ConnectorTableMetadata(SchemaTableName table, List columns, Map properties, Optional comment, List> tableConstraints)
    {
        requireNonNull(table, "table is null");
        requireNonNull(columns, "columns is null");
        requireNonNull(comment, "comment is null");
        requireNonNull(tableConstraints, "tableConstraints is null");

        this.table = table;
        this.columns = Collections.unmodifiableList(new ArrayList<>(columns));
        this.properties = Collections.unmodifiableMap(new LinkedHashMap<>(properties));
        this.comment = comment;
        this.tableConstraints = Collections.unmodifiableList(new ArrayList<>(tableConstraints));
    }

    //rebase a list ot table constraints of column reference type T on column reference type R
    public static  List> rebaseTableConstraints(List> tableConstraints, Map assignments)
    {
        List> mappedTableConstraints = new ArrayList<>();
        tableConstraints.stream().forEach(tableConstraint ->
        {
            Optional> mappedConstraint = tableConstraint.rebaseConstraint(assignments);
            if (mappedConstraint.isPresent()) {
                mappedTableConstraints.add(mappedConstraint.get());
            }
        });
        return mappedTableConstraints;
    }

    public SchemaTableName getTable()
    {
        return table;
    }

    public List getColumns()
    {
        return columns;
    }

    public Map getProperties()
    {
        return properties;
    }

    public Optional getComment()
    {
        return comment;
    }

    public List> getTableConstraints()
    {
        return tableConstraints;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder("ConnectorTableMetadata{");
        sb.append("table=").append(table);
        sb.append(", columns=").append(columns);
        sb.append(", tableConstraints=").append(tableConstraints);
        sb.append(", properties=").append(properties);
        comment.ifPresent(value -> sb.append(", comment='").append(value).append("'"));
        sb.append('}');
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy