singhpk234 commented on code in PR #1287:
URL: https://github.com/apache/polaris/pull/1287#discussion_r2047986634


##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcCrudQueryGenerator.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.polaris.extension.persistence.relational.jdbc;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import 
org.apache.polaris.extension.persistence.relational.jdbc.models.ModelGrantRecord;
+import 
org.apache.polaris.extension.persistence.relational.jdbc.models.ModelPrincipalAuthenticationData;
+
+public class JdbcCrudQueryGenerator {
+
+  private static final Pattern CAMEL_CASE_PATTERN =
+      Pattern.compile("(?<=[a-z0-9])[A-Z]|(?<=[A-Z])[A-Z](?=[a-z])");
+
+  public static String generateSelectQuery(
+      Class<?> entityClass, String filter, Integer limit, Integer offset, 
String orderBy) {
+    String tableName = getTableName(entityClass);
+    List<String> fields = new ArrayList<>();
+
+    for (Field field : entityClass.getDeclaredFields()) {
+      fields.add(camelToSnake(field.getName()));
+    }
+
+    String columns = String.join(", ", fields);
+    StringBuilder query =
+        new StringBuilder("SELECT ").append(columns).append(" FROM 
").append(tableName);
+    if (filter != null && !filter.isEmpty()) {
+      query.append(" WHERE ").append(String.join(" AND ", filter));
+    }
+    return query.toString();
+  }
+
+  public static String generateSelectQuery(
+      Class<?> entityClass,
+      Map<String, Object> whereClause,
+      Integer limit,
+      Integer offset,
+      String orderBy) {
+    String tableName = getTableName(entityClass);
+    List<String> fields = new ArrayList<>();
+
+    for (Field field : entityClass.getDeclaredFields()) {
+      fields.add(camelToSnake(field.getName()));
+    }
+
+    String columns = String.join(", ", fields);
+    StringBuilder query =
+        new StringBuilder("SELECT ").append(columns).append(" FROM 
").append(tableName);
+
+    if (whereClause != null && !whereClause.isEmpty()) {
+      query.append(generateWhereClause(whereClause));
+    }
+
+    if (orderBy != null && !orderBy.isEmpty()) {
+      query.append(" ORDER BY ").append(orderBy);
+    }
+
+    if (limit != null) {
+      query.append(" LIMIT ").append(limit);
+    }
+
+    if (offset != null && limit != null) { // Offset only makes sense with 
limit.
+      query.append(" OFFSET ").append(offset);
+    }
+
+    return query.toString();
+  }
+
+  public static String generateInsertQuery(Object object, Class<?> 
entityClass) {
+    String tableName = getTableName(entityClass);
+    if (object == null || tableName.isEmpty()) {
+      return null; // Or throw an exception
+    }
+
+    Class<?> objectClass = object.getClass();
+    Field[] fields = objectClass.getDeclaredFields();
+    List<String> columnNames = new ArrayList<>();
+    List<String> values = new ArrayList<>();
+
+    for (Field field : fields) {
+      field.setAccessible(true); // Allow access to private fields
+      try {
+        Object value = field.get(object);
+        if (value != null) { // Only include non-null fields
+          columnNames.add(camelToSnake(field.getName()));

Review Comment:
   I see, My thought process was, having an approach like reflection helps to 
not keep this mapping which essentially will be like all the members of the 
class, as our current approach is we essentially ask to get all the projected 
columns (its select *), which in this case is all the member.
   
    please let me know if you feel strongly about this considering above.



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