mchades commented on code in PR #11207: URL: https://github.com/apache/gravitino/pull/11207#discussion_r3310132224
########## catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/JdbcViewOperations.java: ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.gravitino.catalog.jdbc.operation; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import javax.sql.DataSource; +import org.apache.gravitino.catalog.jdbc.JdbcColumn; +import org.apache.gravitino.catalog.jdbc.JdbcView; +import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter; +import org.apache.gravitino.catalog.jdbc.converter.JdbcTypeConverter; +import org.apache.gravitino.exceptions.NoSuchViewException; +import org.apache.gravitino.rel.Column; +import org.apache.gravitino.rel.SQLRepresentation; + +/** Abstract base class for database-specific JDBC view operations. */ +public abstract class JdbcViewOperations { + + protected DataSource dataSource; + protected JdbcExceptionConverter exceptionMapper; + protected JdbcTypeConverter typeConverter; + + /** + * Initializes the view operations with the given data source and converters. + * + * @param dataSource The JDBC data source. + * @param exceptionMapper The exception converter. + * @param typeConverter The type converter. + * @param conf The configuration map. + */ + public void initialize( + DataSource dataSource, + JdbcExceptionConverter exceptionMapper, + JdbcTypeConverter typeConverter, + Map<String, String> conf) { + this.dataSource = dataSource; + this.exceptionMapper = exceptionMapper; + this.typeConverter = typeConverter; + } + + /** + * Lists all view names in the given database/schema. + * + * @param databaseName The database or schema name. + * @return A list of view names. + */ + public List<String> listViews(String databaseName) { + try (Connection connection = getConnection(databaseName)) { + String sql = generateListViewsSql(); + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + bindListViewsParameters(stmt, databaseName); + try (ResultSet rs = stmt.executeQuery()) { + List<String> views = new ArrayList<>(); + while (rs.next()) { + views.add(rs.getString(1)); + } + return views; + } + } + } catch (SQLException e) { + throw exceptionMapper.toGravitinoException(e); + } + } + + /** + * Loads view metadata from the database. + * + * @param databaseName The database or schema name. + * @param viewName The view name. + * @return The loaded view. + * @throws NoSuchViewException If the view does not exist. + */ + public JdbcView load(String databaseName, String viewName) throws NoSuchViewException { + try (Connection connection = getConnection(databaseName)) { + String viewDefinition = loadViewDefinition(connection, databaseName, viewName); + Preconditions.checkNotNull( + viewDefinition, "View definition is null for view %s in %s", viewName, databaseName); + Column[] columns = discoverColumns(connection, viewName); + + SQLRepresentation rep = + SQLRepresentation.builder().withDialect(dialectName()).withSql(viewDefinition).build(); + + return JdbcView.builder() + .withName(viewName) + .withColumns(columns) + .withRepresentations(new SQLRepresentation[] {rep}) + .withProperties(ImmutableMap.of()) Review Comment: JdbcView has a comment field, but the load path never sets it. Please map backend view comment into JdbcView.comment() when available; otherwise comment round-trip is lost and future identifier-in-comment wiring cannot work. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
