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


##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcCrudQueryGenerator.java:
##########
@@ -0,0 +1,307 @@
+/*
+ * 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.core.entity.PolarisEntityCore;
+import org.apache.polaris.core.entity.PolarisEntityId;
+import 
org.apache.polaris.extension.persistence.relational.jdbc.models.ModelEntity;
+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 generateDeleteQueryForEntityGrantRecords(
+      PolarisEntityCore entity, String realmId) {
+    // generate where clause
+    StringBuilder granteeCondition = new StringBuilder("(grantee_id, 
grantee_catalog_id) IN (");
+    granteeCondition
+        .append("(")
+        .append(entity.getId())
+        .append(", ")
+        .append(entity.getCatalogId())
+        .append(")");
+    granteeCondition.append(",");
+    // extra , removed
+    granteeCondition.deleteCharAt(granteeCondition.length() - 1);
+    granteeCondition.append(")");
+
+    StringBuilder securableCondition =
+        new StringBuilder("(securable_catalog_id, securable_id) IN (");
+
+    String in = "(" + entity.getCatalogId() + ", " + entity.getId() + ")";
+    securableCondition.append(in);
+    securableCondition.append(",");
+
+    // extra , removed
+    securableCondition.deleteCharAt(securableCondition.length() - 1);
+    securableCondition.append(")");
+
+    String whereClause =
+        " WHERE ("
+            + granteeCondition
+            + " OR "
+            + securableCondition
+            + ") AND realm_id = '"
+            + realmId
+            + "'";
+    return JdbcCrudQueryGenerator.generateDeleteQuery(ModelGrantRecord.class, 
whereClause);
+  }
+
+  public static String generateSelectQueryForMultipleEntities(
+      String realmId, List<PolarisEntityId> entityIds) {
+    StringBuilder condition = new StringBuilder("(catalog_id, id) IN (");
+    for (PolarisEntityId entityId : entityIds) {
+      String in = "(" + entityId.getCatalogId() + ", " + entityId.getId() + 
")";
+      condition.append(in);
+      condition.append(",");
+    }
+    // extra , removed
+    condition.deleteCharAt(condition.length() - 1);
+    condition.append(")");
+    condition.append(" AND realm_id = '").append(realmId).append("'");
+    return JdbcCrudQueryGenerator.generateSelectQuery(
+        ModelEntity.class, entityIds.isEmpty() ? "" : 
String.valueOf(condition), null, null, null);
+  }
+
+  public static String generateInsertQuery(Object object, String realmId) {
+    if (object == null) {
+      return null;
+    }
+
+    String tableName = getTableName(object.getClass());
+
+    Class<?> objectClass = object.getClass();
+    Field[] fields = objectClass.getDeclaredFields();
+    List<String> columnNames = new ArrayList<>();
+    List<String> values = new ArrayList<>();
+    columnNames.add("realm_id");
+    values.add("'" + realmId + "'");
+
+    for (Field field : fields) {
+      field.setAccessible(true); // Allow access to private fields
+      try {
+        Object value = field.get(object);

Review Comment:
   > then you can directly call these methods and don't have to reflect each 
fields here, which might be bette
   
   I was mostly coming from the fact, the each member needs to selected or 
specified in the insert op, so effectively the toMap being referred here would 
just be all the members of the model, to avoid that and to dynamically get the 
model members I choose the reflection implementation ! IMHO  given the query 
pattern we should good.



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