@Test void getSqlSortSortWithoutUniqueAttribute() { Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("idAttr").getMock(); when(idAttr.getIdentifier()).thenReturn("idAttrId"); when(idAttr.getDataType()).thenReturn(STRING); when(idAttr.isUnique()).thenReturn(true); Attribute attr = when(mock(Attribute.class).getName()).thenReturn("attr").getMock(); when(attr.getIdentifier()).thenReturn("attrId"); when(attr.getDataType()).thenReturn(STRING); EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("entity").getMock(); when(entityType.getId()).thenReturn("entityTypeId"); when(entityType.getAtomicAttributes()).thenReturn(newArrayList(idAttr, attr)); when(entityType.getIdAttribute()).thenReturn(idAttr); when(entityType.getAttribute("idAttr")).thenReturn(idAttr); when(entityType.getAttribute("attr")).thenReturn(attr); @SuppressWarnings("unchecked") Query<Entity> q = mock(Query.class); Sort sort = new Sort().on("attr"); when(q.getSort()).thenReturn(sort); assertEquals("ORDER BY \"attr\" ASC, \"idAttr\" ASC", getSqlSort(entityType, q)); }
static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) { StringBuilder sortSql = new StringBuilder(); Sort sort; if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) { LOG.debug("Query with sort without unique attribute detected: {}", q); sort = new Sort(q.getSort()); sort.on(entityType.getIdAttribute().getName()); } else if (q.getSort() == null) { LOG.debug("Query without sort detected: {}", q); sort = new Sort(entityType.getIdAttribute().getName()); } else { sort = q.getSort(); } for (Sort.Order o : sort) { Attribute attr = entityType.getAttribute(o.getAttr()); sortSql.append(", ").append(getColumnName(attr)); if (o.getDirection().equals(Sort.Direction.DESC)) { sortSql.append(" DESC"); } else { sortSql.append(" ASC"); } } if (sortSql.length() > 0) { sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2)); } return sortSql.toString(); }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) { StringBuilder sortSql = new StringBuilder(); Sort sort; if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) { LOG.debug("Query with sort without unique attribute detected: {}", q); sort = new Sort(q.getSort()); sort.on(entityType.getIdAttribute().getName()); } else if (q.getSort() == null) { LOG.debug("Query without sort detected: {}", q); sort = new Sort(entityType.getIdAttribute().getName()); } else { sort = q.getSort(); } for (Sort.Order o : sort) { Attribute attr = entityType.getAttribute(o.getAttr()); sortSql.append(", ").append(getColumnName(attr)); if (o.getDirection().equals(Sort.Direction.DESC)) { sortSql.append(" DESC"); } else { sortSql.append(" ASC"); } } if (sortSql.length() > 0) { sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2)); } return sortSql.toString(); } }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) { StringBuilder sortSql = new StringBuilder(); Sort sort; if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) { LOG.debug("Query with sort without unique attribute detected: {}", q); sort = new Sort(q.getSort()); sort.on(entityType.getIdAttribute().getName()); } else if (q.getSort() == null) { LOG.debug("Query without sort detected: {}", q); sort = new Sort(entityType.getIdAttribute().getName()); } else { sort = q.getSort(); } for (Sort.Order o : sort) { Attribute attr = entityType.getAttribute(o.getAttr()); sortSql.append(", ").append(getColumnName(attr)); if (o.getDirection().equals(Sort.Direction.DESC)) { sortSql.append(" DESC"); } else { sortSql.append(" ASC"); } } if (sortSql.length() > 0) { sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2)); } return sortSql.toString(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) { StringBuilder sortSql = new StringBuilder(); Sort sort; if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) { LOG.debug("Query with sort without unique attribute detected: {}", q); sort = new Sort(q.getSort()); sort.on(entityType.getIdAttribute().getName()); } else if (q.getSort() == null) { LOG.debug("Query without sort detected: {}", q); sort = new Sort(entityType.getIdAttribute().getName()); } else { sort = q.getSort(); } for (Sort.Order o : sort) { Attribute attr = entityType.getAttribute(o.getAttr()); sortSql.append(", ").append(getColumnName(attr)); if (o.getDirection().equals(Sort.Direction.DESC)) { sortSql.append(" DESC"); } else { sortSql.append(" ASC"); } } if (sortSql.length() > 0) { sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2)); } return sortSql.toString(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) { StringBuilder sortSql = new StringBuilder(); Sort sort; if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) { LOG.debug("Query with sort without unique attribute detected: {}", q); sort = new Sort(q.getSort()); sort.on(entityType.getIdAttribute().getName()); } else if (q.getSort() == null) { LOG.debug("Query without sort detected: {}", q); sort = new Sort(entityType.getIdAttribute().getName()); } else { sort = q.getSort(); } for (Sort.Order o : sort) { Attribute attr = entityType.getAttribute(o.getAttr()); sortSql.append(", ").append(getColumnName(attr)); if (o.getDirection().equals(Sort.Direction.DESC)) { sortSql.append(" DESC"); } else { sortSql.append(" ASC"); } } if (sortSql.length() > 0) { sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2)); } return sortSql.toString(); } private  PostgreSqlQueryGenerator();   }