|
| 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.databases.oceanbase.catalog; |
| 20 | + |
| 21 | +import org.apache.flink.connector.jdbc.catalog.AbstractJdbcCatalog; |
| 22 | +import org.apache.flink.connector.jdbc.dialect.JdbcDialectTypeMapper; |
| 23 | +import org.apache.flink.table.catalog.ObjectPath; |
| 24 | +import org.apache.flink.table.catalog.UniqueConstraint; |
| 25 | +import org.apache.flink.table.catalog.exceptions.CatalogException; |
| 26 | +import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException; |
| 27 | +import org.apache.flink.table.types.DataType; |
| 28 | +import org.apache.flink.util.Preconditions; |
| 29 | + |
| 30 | +import org.apache.commons.lang3.StringUtils; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import java.sql.DatabaseMetaData; |
| 35 | +import java.sql.ResultSetMetaData; |
| 36 | +import java.sql.SQLException; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.Properties; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.COMPATIBLE_MODE; |
| 45 | + |
| 46 | +/** Catalog for OceanBase. */ |
| 47 | +public class OceanBaseCatalog extends AbstractJdbcCatalog { |
| 48 | + |
| 49 | + private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCatalog.class); |
| 50 | + |
| 51 | + private static final Set<String> builtinDatabases = |
| 52 | + new HashSet<String>() { |
| 53 | + { |
| 54 | + add("__public"); |
| 55 | + add("information_schema"); |
| 56 | + add("mysql"); |
| 57 | + add("oceanbase"); |
| 58 | + add("LBACSYS"); |
| 59 | + add("ORAAUDITOR"); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + private final String compatibleMode; |
| 64 | + private final JdbcDialectTypeMapper dialectTypeMapper; |
| 65 | + |
| 66 | + public OceanBaseCatalog( |
| 67 | + ClassLoader userClassLoader, |
| 68 | + String catalogName, |
| 69 | + String compatibleMode, |
| 70 | + String defaultDatabase, |
| 71 | + String baseUrl, |
| 72 | + Properties connectionProperties) { |
| 73 | + super(userClassLoader, catalogName, defaultDatabase, baseUrl, connectionProperties); |
| 74 | + this.compatibleMode = compatibleMode; |
| 75 | + this.dialectTypeMapper = new OceanBaseTypeMapper(compatibleMode); |
| 76 | + } |
| 77 | + |
| 78 | + private boolean isMySQLMode() { |
| 79 | + return "mysql".equalsIgnoreCase(compatibleMode); |
| 80 | + } |
| 81 | + |
| 82 | + private String getConnUrl() { |
| 83 | + return isMySQLMode() ? baseUrl : defaultUrl; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public List<String> listDatabases() throws CatalogException { |
| 88 | + if (isMySQLMode()) { |
| 89 | + return extractColumnValuesBySQL( |
| 90 | + getConnUrl(), |
| 91 | + "SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`", |
| 92 | + 1, |
| 93 | + dbName -> !builtinDatabases.contains(dbName)); |
| 94 | + } |
| 95 | + return extractColumnValuesBySQL( |
| 96 | + getConnUrl(), |
| 97 | + "SELECT USERNAME FROM ALL_USERS", |
| 98 | + 1, |
| 99 | + schema -> !builtinDatabases.contains(schema)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public List<String> listTables(String databaseName) |
| 104 | + throws DatabaseNotExistException, CatalogException { |
| 105 | + Preconditions.checkState( |
| 106 | + StringUtils.isNotBlank(databaseName), "Database name must not be blank."); |
| 107 | + if (!databaseExists(databaseName)) { |
| 108 | + throw new DatabaseNotExistException(getName(), databaseName); |
| 109 | + } |
| 110 | + String sql = |
| 111 | + isMySQLMode() |
| 112 | + ? "SELECT TABLE_NAME FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = ?" |
| 113 | + : "SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = ?"; |
| 114 | + return extractColumnValuesBySQL(getConnUrl(), sql, 1, null, databaseName); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean tableExists(ObjectPath tablePath) throws CatalogException { |
| 119 | + return isMySQLMode() |
| 120 | + ? !extractColumnValuesBySQL( |
| 121 | + getConnUrl(), |
| 122 | + "SELECT TABLE_NAME FROM information_schema.`TABLES` " |
| 123 | + + "WHERE TABLE_SCHEMA = ? and TABLE_NAME = ?", |
| 124 | + 1, |
| 125 | + null, |
| 126 | + tablePath.getDatabaseName(), |
| 127 | + tablePath.getObjectName()) |
| 128 | + .isEmpty() |
| 129 | + : !extractColumnValuesBySQL( |
| 130 | + getConnUrl(), |
| 131 | + "SELECT TABLE_NAME FROM ALL_TABLES " |
| 132 | + + "WHERE OWNER = ? and TABLE_NAME = ?", |
| 133 | + 1, |
| 134 | + null, |
| 135 | + tablePath.getDatabaseName(), |
| 136 | + tablePath.getObjectName()) |
| 137 | + .isEmpty(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected Optional<UniqueConstraint> getPrimaryKey( |
| 142 | + DatabaseMetaData metaData, String database, String schema, String table) |
| 143 | + throws SQLException { |
| 144 | + if (isMySQLMode()) { |
| 145 | + return super.getPrimaryKey(metaData, database, null, table); |
| 146 | + } else { |
| 147 | + return super.getPrimaryKey(metaData, null, database, table); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + protected Map<String, String> getOptions(ObjectPath tablePath) { |
| 153 | + Map<String, String> options = super.getOptions(tablePath); |
| 154 | + options.put(COMPATIBLE_MODE.key(), compatibleMode); |
| 155 | + return options; |
| 156 | + } |
| 157 | + |
| 158 | + /** Converts OceanBase type to Flink {@link DataType}. */ |
| 159 | + @Override |
| 160 | + protected DataType fromJDBCType(ObjectPath tablePath, ResultSetMetaData metadata, int colIndex) |
| 161 | + throws SQLException { |
| 162 | + return dialectTypeMapper.mapping(tablePath, metadata, colIndex); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + protected String getTableName(ObjectPath tablePath) { |
| 167 | + return tablePath.getObjectName(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + protected String getSchemaName(ObjectPath tablePath) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + protected String getSchemaTableName(ObjectPath tablePath) { |
| 177 | + return tablePath.getObjectName(); |
| 178 | + } |
| 179 | +} |
0 commit comments