sachinnn99 commented on code in PR #11207:
URL: https://github.com/apache/gravitino/pull/11207#discussion_r3309282362


##########
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.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);
+      Column[] columns = discoverColumns(connection, viewName);
+
+      SQLRepresentation rep =
+          SQLRepresentation.builder()
+              .withDialect(dialectName())
+              .withSql(viewDefinition != null ? viewDefinition : "")

Review Comment:
   Good catch -- in practice `viewDefinition` should never be null since both 
MySQL and PostgreSQL's `information_schema.views.view_definition` is non-null 
for existing views, and `loadViewDefinition` already throws 
`NoSuchViewException` when the row is missing.
   
   I'll replace the silent empty-string fallback with a 
`Preconditions.checkNotNull` so we fail fast on unexpected DB state instead of 
silently producing an empty SQL representation.



-- 
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]

Reply via email to