|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shardingsphere.infra.database.presto.metadata.data.loader; |
| 19 | + |
| 20 | +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; |
| 21 | +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; |
| 22 | +import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; |
| 23 | +import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; |
| 24 | +import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; |
| 25 | +import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry; |
| 26 | + |
| 27 | +import java.sql.Connection; |
| 28 | +import java.sql.PreparedStatement; |
| 29 | +import java.sql.ResultSet; |
| 30 | +import java.sql.SQLException; |
| 31 | +import java.sql.Types; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.LinkedList; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +/** |
| 40 | + * Meta data loader for Presto. |
| 41 | + * As of the Memory Connector of prestodb/presto 0.290, the table `INFORMATION_SCHEMA.INDEXES` does not exist, |
| 42 | + * and `INFORMATION_SCHEMA.COLUMNS` does not have a column `IS_VISIBLE`. |
| 43 | + * The current implementation does not record the table's index, primary keys, generated info, or column visibility. |
| 44 | + */ |
| 45 | +public final class PrestoMetaDataLoader implements DialectMetaDataLoader { |
| 46 | + |
| 47 | + @Override |
| 48 | + public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException { |
| 49 | + Collection<TableMetaData> tableMetaDataList = new LinkedList<>(); |
| 50 | + try (Connection connection = material.getDataSource().getConnection()) { |
| 51 | + Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames()); |
| 52 | + for (Map.Entry<String, Collection<ColumnMetaData>> entry : columnMetaDataMap.entrySet()) { |
| 53 | + tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), Collections.emptyList(), Collections.emptyList())); |
| 54 | + } |
| 55 | + } |
| 56 | + return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList)); |
| 57 | + } |
| 58 | + |
| 59 | + @SuppressWarnings("SqlSourceToSinkFlow") |
| 60 | + private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables) throws SQLException { |
| 61 | + Map<String, Collection<ColumnMetaData>> result = new HashMap<>(); |
| 62 | + try (PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) { |
| 63 | + preparedStatement.setString(1, connection.getCatalog()); |
| 64 | + try (ResultSet resultSet = preparedStatement.executeQuery()) { |
| 65 | + while (resultSet.next()) { |
| 66 | + String tableName = resultSet.getString("TABLE_NAME"); |
| 67 | + ColumnMetaData columnMetaData = loadColumnMetaData(resultSet); |
| 68 | + if (!result.containsKey(tableName)) { |
| 69 | + result.put(tableName, new LinkedList<>()); |
| 70 | + } |
| 71 | + result.get(tableName).add(columnMetaData); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + private String getTableMetaDataSQL(final Collection<String> tables) { |
| 79 | + if (tables.isEmpty()) { |
| 80 | + return "SELECT TABLE_CATALOG,\n" |
| 81 | + + " TABLE_NAME,\n" |
| 82 | + + " COLUMN_NAME,\n" |
| 83 | + + " DATA_TYPE,\n" |
| 84 | + + " ORDINAL_POSITION,\n" |
| 85 | + + " IS_NULLABLE\n" |
| 86 | + + "FROM INFORMATION_SCHEMA.COLUMNS\n" |
| 87 | + + "WHERE TABLE_CATALOG = ?\n" |
| 88 | + + "ORDER BY ORDINAL_POSITION"; |
| 89 | + } |
| 90 | + String collect = tables.stream().map(each -> String.format("'%s'", each).toUpperCase()).collect(Collectors.joining(",")); |
| 91 | + return String.format("SELECT TABLE_CATALOG,\n" |
| 92 | + + " TABLE_NAME,\n" |
| 93 | + + " COLUMN_NAME,\n" |
| 94 | + + " DATA_TYPE,\n" |
| 95 | + + " ORDINAL_POSITION,\n" |
| 96 | + + " IS_NULLABLE\n" |
| 97 | + + "FROM INFORMATION_SCHEMA.COLUMNS\n" |
| 98 | + + "WHERE TABLE_CATALOG = ?\n" |
| 99 | + + " AND UPPER(TABLE_NAME) IN (%s)\n" |
| 100 | + + "ORDER BY ORDINAL_POSITION", collect); |
| 101 | + } |
| 102 | + |
| 103 | + private ColumnMetaData loadColumnMetaData(final ResultSet resultSet) throws SQLException { |
| 104 | + String columnName = resultSet.getString("COLUMN_NAME"); |
| 105 | + String dataType = resultSet.getString("DATA_TYPE"); |
| 106 | + boolean isNullable = "YES".equals(resultSet.getString("IS_NULLABLE")); |
| 107 | + return new ColumnMetaData(columnName, DataTypeRegistry.getDataType(getDatabaseType(), dataType).orElse(Types.OTHER), Boolean.FALSE, Boolean.FALSE, false, Boolean.TRUE, false, isNullable); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getDatabaseType() { |
| 112 | + return "Presto"; |
| 113 | + } |
| 114 | +} |
0 commit comments