@Test void getSqlSelectXref() { Attribute attr = when(mock(Attribute.class).getName()).thenReturn("attr").getMock(); when(attr.getIdentifier()).thenReturn("attrId"); when(attr.getDataType()).thenReturn(XREF); when(attr.isInversedBy()).thenReturn(false); 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); 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("attr")).thenReturn(attr); when(entityType.getAttribute("idAttr")).thenReturn(idAttr); @SuppressWarnings("unchecked") Query<Entity> q = mock(Query.class); List<Object> parameters = Lists.newArrayList(); assertEquals( "SELECT this.\"idAttr\", this.\"attr\" FROM \"entityTypeId#c34894ba\" AS this ORDER BY \"idAttr\" ASC", getSqlSelect(entityType, q, parameters, true)); assertEquals(emptyList(), parameters); }
static <E extends Entity> String getSqlSelect( EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) { final StringBuilder select = new StringBuilder("SELECT "); if (isDistinctSelectRequired(entityType, q)) { select.append("DISTINCT "); } final StringBuilder group = new StringBuilder(); final AtomicInteger count = new AtomicInteger(); final Attribute idAttribute = entityType.getIdAttribute(); getPersistedAttributes(entityType) .forEach( attr -> { if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) { if (count.get() > 0) { select.append(", "); } if (isPersistedInOtherTable(attr)) { if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) { if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) { Attribute refIdAttr = attr.getRefEntity().getIdAttribute(); String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr); Sort orderBy = attr.getOrderBy(); if (orderBy == null) { orderBy = new Sort(refIdAttr.getName()); } mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr); select.append(mrefSelect); } else { String mrefSelect = MessageFormat.format( "(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute)); select.append(mrefSelect); } } else { select.append("NULL AS ").append(getColumnName(attr)); } } else { select.append("this.").append(getColumnName(attr)); if (group.length() > 0) { group.append(", this.").append(getColumnName(attr)); } else { group.append("this.").append(getColumnName(attr)); } } count.incrementAndGet(); } }); StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q)); String where = getSqlWhere(entityType, q, parameters, new AtomicInteger()); if (where.length() > 0) { result.append(" WHERE ").append(where); } result.append(' ').append(getSqlSort(entityType, q)); if (q.getPageSize() > 0) { result.append(" LIMIT ").append(q.getPageSize()); } if (q.getOffset() > 0) { result.append(" OFFSET ").append(q.getOffset()); } return result.toString().trim(); }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSelect( EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) { final StringBuilder select = new StringBuilder("SELECT "); if (isDistinctSelectRequired(entityType, q)) { select.append("DISTINCT "); } final StringBuilder group = new StringBuilder(); final AtomicInteger count = new AtomicInteger(); final Attribute idAttribute = entityType.getIdAttribute(); getPersistedAttributes(entityType) .forEach( attr -> { if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) { if (count.get() > 0) { select.append(", "); } if (isPersistedInOtherTable(attr)) { if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) { if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) { Attribute refIdAttr = attr.getRefEntity().getIdAttribute(); String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr); Sort orderBy = attr.getOrderBy(); if (orderBy == null) { orderBy = new Sort(refIdAttr.getName()); } mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr); select.append(mrefSelect); } else { String mrefSelect = MessageFormat.format( "(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute)); select.append(mrefSelect); } } else { select.append("NULL AS ").append(getColumnName(attr)); } } else { select.append("this.").append(getColumnName(attr)); if (group.length() > 0) { group.append(", this.").append(getColumnName(attr)); } else { group.append("this.").append(getColumnName(attr)); } } count.incrementAndGet(); } }); StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q)); String where = getSqlWhere(entityType, q, parameters, new AtomicInteger()); if (where.length() > 0) { result.append(" WHERE ").append(where); } result.append(' ').append(getSqlSort(entityType, q)); if (q.getPageSize() > 0) { result.append(" LIMIT ").append(q.getPageSize()); } if (q.getOffset() > 0) { result.append(" OFFSET ").append(q.getOffset()); } return result.toString().trim(); } }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSelect( EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) { final StringBuilder select = new StringBuilder("SELECT "); if (isDistinctSelectRequired(entityType, q)) { select.append("DISTINCT "); } final StringBuilder group = new StringBuilder(); final AtomicInteger count = new AtomicInteger(); final Attribute idAttribute = entityType.getIdAttribute(); getPersistedAttributes(entityType) .forEach( attr -> { if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) { if (count.get() > 0) { select.append(", "); } if (isPersistedInOtherTable(attr)) { if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) { if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) { Attribute refIdAttr = attr.getRefEntity().getIdAttribute(); String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr); Sort orderBy = attr.getOrderBy(); if (orderBy == null) { orderBy = new Sort(refIdAttr.getName()); } mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr); select.append(mrefSelect); } else { String mrefSelect = MessageFormat.format( "(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute)); select.append(mrefSelect); } } else { select.append("NULL AS ").append(getColumnName(attr)); } } else { select.append("this.").append(getColumnName(attr)); if (group.length() > 0) { group.append(", this.").append(getColumnName(attr)); } else { group.append("this.").append(getColumnName(attr)); } } count.incrementAndGet(); } }); StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q)); String where = getSqlWhere(entityType, q, parameters, new AtomicInteger()); if (where.length() > 0) { result.append(" WHERE ").append(where); } result.append(' ').append(getSqlSort(entityType, q)); if (q.getPageSize() > 0) { result.append(" LIMIT ").append(q.getPageSize()); } if (q.getOffset() > 0) { result.append(" OFFSET ").append(q.getOffset()); } return result.toString().trim(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSelect( EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) { final StringBuilder select = new StringBuilder("SELECT "); if (isDistinctSelectRequired(entityType, q)) { select.append("DISTINCT "); } final StringBuilder group = new StringBuilder(); final AtomicInteger count = new AtomicInteger(); final Attribute idAttribute = entityType.getIdAttribute(); getPersistedAttributes(entityType) .forEach( attr -> { if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) { if (count.get() > 0) { select.append(", "); } if (isPersistedInOtherTable(attr)) { if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) { if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) { Attribute refIdAttr = attr.getRefEntity().getIdAttribute(); String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr); Sort orderBy = attr.getOrderBy(); if (orderBy == null) { orderBy = new Sort(refIdAttr.getName()); } mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr); select.append(mrefSelect); } else { String mrefSelect = MessageFormat.format( "(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute)); select.append(mrefSelect); } } else { select.append("NULL AS ").append(getColumnName(attr)); } } else { select.append("this.").append(getColumnName(attr)); if (group.length() > 0) { group.append(", this.").append(getColumnName(attr)); } else { group.append("this.").append(getColumnName(attr)); } } count.incrementAndGet(); } }); StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q)); String where = getSqlWhere(entityType, q, parameters, new AtomicInteger()); if (where.length() > 0) { result.append(" WHERE ").append(where); } result.append(' ').append(getSqlSort(entityType, q)); if (q.getPageSize() > 0) { result.append(" LIMIT ").append(q.getPageSize()); } if (q.getOffset() > 0) { result.append(" OFFSET ").append(q.getOffset()); } return result.toString().trim(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { static <E extends Entity> String getSqlSelect( EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) { final StringBuilder select = new StringBuilder("SELECT "); if (isDistinctSelectRequired(entityType, q)) { select.append("DISTINCT "); } final StringBuilder group = new StringBuilder(); final AtomicInteger count = new AtomicInteger(); final Attribute idAttribute = entityType.getIdAttribute(); getPersistedAttributes(entityType) .forEach( attr -> { if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) { if (count.get() > 0) { select.append(", "); } if (isPersistedInOtherTable(attr)) { if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) { if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) { Attribute refIdAttr = attr.getRefEntity().getIdAttribute(); String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr); Sort orderBy = attr.getOrderBy(); if (orderBy == null) { orderBy = new Sort(refIdAttr.getName()); } mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr); select.append(mrefSelect); } else { String mrefSelect = MessageFormat.format( "(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute)); select.append(mrefSelect); } } else { select.append("NULL AS ").append(getColumnName(attr)); } } else { select.append("this.").append(getColumnName(attr)); if (group.length() > 0) { group.append(", this.").append(getColumnName(attr)); } else { group.append("this.").append(getColumnName(attr)); } } count.incrementAndGet(); } }); StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q)); String where = getSqlWhere(entityType, q, parameters, new AtomicInteger()); if (where.length() > 0) { result.append(" WHERE ").append(where); } result.append(' ').append(getSqlSort(entityType, q)); if (q.getPageSize() > 0) { result.append(" LIMIT ").append(q.getPageSize()); } if (q.getOffset() > 0) { result.append(" OFFSET ").append(q.getOffset()); } return result.toString().trim(); } private  PostgreSqlQueryGenerator();   }