|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.connector.jdbc.dameng.database.catalog; |
| 20 | + |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.apache.flink.annotation.Internal; |
| 23 | +import org.apache.flink.annotation.VisibleForTesting; |
| 24 | +import org.apache.flink.connector.jdbc.core.database.catalog.AbstractJdbcCatalog; |
| 25 | +import org.apache.flink.connector.jdbc.core.database.catalog.JdbcCatalogTypeMapper; |
| 26 | +import org.apache.flink.table.catalog.ObjectPath; |
| 27 | +import org.apache.flink.table.catalog.exceptions.CatalogException; |
| 28 | +import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException; |
| 29 | +import org.apache.flink.table.types.DataType; |
| 30 | +import org.apache.flink.util.Preconditions; |
| 31 | +import org.apache.flink.util.TemporaryClassLoaderContext; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +import java.sql.Connection; |
| 36 | +import java.sql.DriverManager; |
| 37 | +import java.sql.ResultSetMetaData; |
| 38 | +import java.sql.SQLException; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Properties; |
| 42 | +import java.util.Set; |
| 43 | +import java.util.regex.Matcher; |
| 44 | +import java.util.regex.Pattern; |
| 45 | + |
| 46 | +import static org.apache.flink.connector.jdbc.JdbcConnectionOptions.getBriefAuthProperties; |
| 47 | + |
| 48 | +/** Catalog for DaMeng. */ |
| 49 | +@Internal |
| 50 | +public class DaMengCatalog |
| 51 | + extends AbstractJdbcCatalog { |
| 52 | + |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(DaMengCatalog.class); |
| 54 | + |
| 55 | + private final JdbcCatalogTypeMapper dialectTypeMapper; |
| 56 | + |
| 57 | + // DaMeng system schemas that shouldn't be exposed to users |
| 58 | + private static final Set<String> builtinDatabases = |
| 59 | + new HashSet<String>() { |
| 60 | + { |
| 61 | + add("SYS"); |
| 62 | + add("SYSDBA"); |
| 63 | + add("SYSAUDITOR"); |
| 64 | + add("INFORMATION_SCHEMA"); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + @VisibleForTesting |
| 69 | + public DaMengCatalog( |
| 70 | + ClassLoader userClassLoader, |
| 71 | + String catalogName, |
| 72 | + String defaultDatabase, |
| 73 | + String username, |
| 74 | + String pwd, |
| 75 | + String baseUrl) { |
| 76 | + this( |
| 77 | + userClassLoader, |
| 78 | + catalogName, |
| 79 | + defaultDatabase, |
| 80 | + baseUrl, |
| 81 | + getBriefAuthProperties(username, pwd)); |
| 82 | + } |
| 83 | + |
| 84 | + public DaMengCatalog( |
| 85 | + ClassLoader userClassLoader, |
| 86 | + String catalogName, |
| 87 | + String defaultDatabase, |
| 88 | + String baseUrl, |
| 89 | + Properties connectionProperties) { |
| 90 | + super(userClassLoader, catalogName, defaultDatabase, baseUrl, connectionProperties); |
| 91 | + |
| 92 | + String driverVersion = |
| 93 | + Preconditions.checkNotNull(getDriverVersion(), "Driver version must not be null."); |
| 94 | + String databaseVersion = |
| 95 | + Preconditions.checkNotNull( |
| 96 | + getDatabaseVersion(), "Database version must not be null."); |
| 97 | + LOG.info("Driver version: {}, database version: {}", driverVersion, databaseVersion); |
| 98 | + this.dialectTypeMapper = new DaMengTypeMapper(databaseVersion, driverVersion); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public List<String> listDatabases() throws CatalogException { |
| 103 | + return extractColumnValuesBySQL( |
| 104 | + defaultUrl, |
| 105 | + "SELECT SCHEMA_NAME FROM ALL_SCHEMAS", |
| 106 | + 1, |
| 107 | + dbName -> !builtinDatabases.contains(dbName.toUpperCase())); |
| 108 | + } |
| 109 | + |
| 110 | + // ------ tables ------ |
| 111 | + |
| 112 | + @Override |
| 113 | + public List<String> listTables(String databaseName) |
| 114 | + throws DatabaseNotExistException, CatalogException { |
| 115 | + Preconditions.checkState( |
| 116 | + StringUtils.isNotBlank(databaseName), "Database name must not be blank."); |
| 117 | + if (!databaseExists(databaseName)) { |
| 118 | + throw new DatabaseNotExistException(getName(), databaseName); |
| 119 | + } |
| 120 | + |
| 121 | + return extractColumnValuesBySQL( |
| 122 | + getDatabaseUrl(databaseName), |
| 123 | + "SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = ?", |
| 124 | + 1, |
| 125 | + null, |
| 126 | + databaseName.toUpperCase()); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean tableExists(ObjectPath tablePath) throws CatalogException { |
| 131 | + return !extractColumnValuesBySQL( |
| 132 | + baseUrl, |
| 133 | + "SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = ? AND TABLE_NAME = ?", |
| 134 | + 1, |
| 135 | + null, |
| 136 | + tablePath.getDatabaseName().toUpperCase(), |
| 137 | + tablePath.getObjectName().toUpperCase()) |
| 138 | + .isEmpty(); |
| 139 | + } |
| 140 | + |
| 141 | + private String getDatabaseVersion() { |
| 142 | + try (TemporaryClassLoaderContext ignored = |
| 143 | + TemporaryClassLoaderContext.of(userClassLoader)) { |
| 144 | + try (Connection conn = DriverManager.getConnection(defaultUrl, connectionProperties)) { |
| 145 | + return conn.getMetaData().getDatabaseProductVersion(); |
| 146 | + } catch (Exception e) { |
| 147 | + throw new CatalogException( |
| 148 | + String.format("Failed in getting DaMeng version by %s.", defaultUrl), e); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private String getDriverVersion() { |
| 154 | + try (TemporaryClassLoaderContext ignored = |
| 155 | + TemporaryClassLoaderContext.of(userClassLoader)) { |
| 156 | + try (Connection conn = DriverManager.getConnection(defaultUrl, connectionProperties)) { |
| 157 | + String driverVersion = conn.getMetaData().getDriverVersion(); |
| 158 | + Pattern regexp = Pattern.compile("\\d+?\\.\\d+?\\.\\d+"); |
| 159 | + Matcher matcher = regexp.matcher(driverVersion); |
| 160 | + return matcher.find() ? matcher.group(0) : null; |
| 161 | + } catch (Exception e) { |
| 162 | + throw new CatalogException( |
| 163 | + String.format("Failed in getting DaMeng driver version by %s.", defaultUrl), |
| 164 | + e); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** Converts DaMeng type to Flink {@link DataType}. */ |
| 170 | + @Override |
| 171 | + protected DataType fromJDBCType(ObjectPath tablePath, ResultSetMetaData metadata, int colIndex) |
| 172 | + throws SQLException { |
| 173 | + return dialectTypeMapper.mapping(tablePath, metadata, colIndex); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + protected String getTableName(ObjectPath tablePath) { |
| 178 | + return tablePath.getObjectName().toUpperCase(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + protected String getSchemaName(ObjectPath tablePath) { |
| 183 | + return tablePath.getDatabaseName().toUpperCase(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + protected String getSchemaTableName(ObjectPath tablePath) { |
| 188 | + return tablePath.getObjectName().toUpperCase(); |
| 189 | + } |
| 190 | +} |
0 commit comments