Skip to content

Commit

Permalink
fix #23847 (#24340)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbgaspar authored Nov 25, 2023
1 parent 19806c4 commit 226a6fd
Showing 1 changed file with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ package <%= packageName %>.repository;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.convert.R2dbcConverter;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.r2dbc.core.StatementMapper;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.query.UpdateMapper;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.SelectBuilder.SelectFromAndJoin;
import org.springframework.data.relational.core.sql.SelectBuilder.SelectFromAndJoinCondition;
import org.springframework.data.relational.core.sql.SelectBuilder.SelectOrdered;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.Table;
import org.springframework.data.relational.core.sql.render.SqlRenderer;
import org.springframework.r2dbc.core.Parameter;
Expand Down Expand Up @@ -240,11 +235,57 @@ public class EntityManager {
for (Sort.Order order : sortToUse) {

String propertyName = order.getProperty();
OrderByField orderByField = OrderByField.from(table.column(propertyName).as(EntityManager.ALIAS_PREFIX + propertyName));
OrderByField orderByField = !propertyName.contains(".")
? OrderByField.from(table.column(propertyName).as(EntityManager.ALIAS_PREFIX + propertyName))
: createOrderByField(propertyName);

fields.add(order.isAscending() ? orderByField.asc() : orderByField.desc());
}

return fields;
}

/**
* Creates an OrderByField instance for sorting a query by the specified property.
*
* @param propertyName The full property name in the format "tableName.columnName".
* @return An OrderByField instance for sorting by the specified property.
*/
private static OrderByField createOrderByField(String propertyName) {
// Split the propertyName into table name and column name
String[] parts = propertyName.split("\\.");
String tableName = parts[0];
String columnName = parts[1];

// Create and return an OrderByField instance
return OrderByField.from(
// Create a column with the given name and alias it with the table name and column name
Column.aliased(
columnName,
// Create a table alias with the same name as the table
Table.aliased(camelCaseToSnakeCase(tableName), tableName),
// Use a composite alias of "tableName_columnName"
String.format("%s_%s", tableName, columnName)
)
);
}

/**
* Converts a camel case string to snake case.
*
* @param input The camel case string to be converted to snake case.
* @return The input string converted to snake case.
*/
public static String camelCaseToSnakeCase(String input) {
// Regular Expression
String regex = "([a-z])([A-Z]+)";

// Replacement string
String replacement = "$1_$2";

// Replace the given regex
// with replacement string
// and convert it to lower case.
return input.replaceAll(regex, replacement).toLowerCase();
}
}

0 comments on commit 226a6fd

Please sign in to comment.