singhpk234 commented on code in PR #1287: URL: https://github.com/apache/polaris/pull/1287#discussion_r2053578097
########## 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: I **_dont_** agree with argument being made here around reflection. > since there are many draw backs with reflection, includes performance, readability and security etc How do we think JOOQ, which is suggested as an alternative to this approach below is implementing these things under the hood ? please ref detailed code pointers for ref: [1] Method Extractors : https://github.com/jOOQ/jOOQ/blob/main/jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java#L128 [2] CamelCase interpretations to get the Getters / Setters: https://github.com/jOOQ/jOOQ/blob/main/jOOQ/src/main/java/org/jooq/impl/Tools.java#L4545 Never the less we do have reflection being used in our code base up and down, if this is some concern or is being recommended as best practice without evaluating why this was done in the first place, I would recommend having it added in the guide lines of the code base and then every reflection change being throughly voted upon. -- 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: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org