@Test void getSqlFrom() { Package eric = createPackage("eu_bbmri_eric"); Attribute collectionsIdAttribute = createIdAttribute("collectionsId"); EntityType collectionsEntity = createMockEntityWithIdAttribute( "eu_bbmri_eric_collections", collectionsIdAttribute, "collectionsId"); Attribute typeIdAttribute = createIdAttribute("typeId"); EntityType typeEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_type", typeIdAttribute, "typeId"); Attribute categoryIdAttribute = createIdAttribute("categoryId"); EntityType categoryEntity = createMockEntityWithIdAttribute( "eu_bbmri_eric_category", categoryIdAttribute, "categoryId"); Attribute typeAttribute = createMrefAttribute("type", typeEntity); Attribute categoryAttribute = createMrefAttribute("category", categoryEntity); when(collectionsEntity.getPackage()).thenReturn(eric); when(collectionsEntity.getAttribute("type")).thenReturn(typeAttribute); when(collectionsEntity.getAttribute("category")).thenReturn(categoryAttribute); when(collectionsEntity.getAtomicAttributes()) .thenReturn(asList(collectionsIdAttribute, typeAttribute, categoryAttribute)); QueryImpl<Entity> q = new QueryImpl<>(); List<Object> parameters = Lists.newArrayList(); String sqlSelect = PostgreSqlQueryGenerator.getSqlSelect(collectionsEntity, q, parameters, true); assertEquals( "SELECT this.\"collectionsId\", (SELECT array_agg(DISTINCT ARRAY[\"type\".\"order\"::TEXT,\"type\".\"type\"::TEXT]) FROM \"eu_bbmri_eric_collecti#4dc023e6_type\" AS \"type\" WHERE this.\"collectionsId\" = \"type\".\"collectionsId\") AS \"type\", (SELECT array_agg(DISTINCT ARRAY[\"category\".\"order\"::TEXT,\"category\".\"category\"::TEXT]) FROM \"eu_bbmri_eric_collecti#4dc023e6_category\" AS \"category\" WHERE this.\"collectionsId\" = \"category\".\"collectionsId\") AS \"category\" FROM \"eu_bbmri_eric_collections#4dc023e6\" AS this ORDER BY \"collectionsId\" ASC", sqlSelect); }
private static <E extends Entity> String getSqlFrom(EntityType entityType, Query<E> q) { List<Attribute> mrefAttrsInQuery = getJoinQueryAttrs(entityType, q); StringBuilder from = new StringBuilder(" FROM ").append(getTableName(entityType)).append(" AS this"); Attribute idAttribute = entityType.getIdAttribute(); for (int i = 0; i < mrefAttrsInQuery.size(); i++) { Attribute mrefAttr = mrefAttrsInQuery.get(i); if (mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy()) { from.append(" LEFT JOIN ") .append(getTableName(mrefAttr.getRefEntity())) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(mrefAttr.getMappedBy())) .append(')'); } else { from.append(" LEFT JOIN ") .append(getJunctionTableName(entityType, mrefAttr)) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(idAttribute)) .append(')'); } } return from.toString(); }
PostgreSqlQueryGenerator { private static <E extends Entity> String getSqlFrom(EntityType entityType, Query<E> q) { List<Attribute> mrefAttrsInQuery = getJoinQueryAttrs(entityType, q); StringBuilder from = new StringBuilder(" FROM ").append(getTableName(entityType)).append(" AS this"); Attribute idAttribute = entityType.getIdAttribute(); for (int i = 0; i < mrefAttrsInQuery.size(); i++) { Attribute mrefAttr = mrefAttrsInQuery.get(i); if (mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy()) { from.append(" LEFT JOIN ") .append(getTableName(mrefAttr.getRefEntity())) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(mrefAttr.getMappedBy())) .append(')'); } else { from.append(" LEFT JOIN ") .append(getJunctionTableName(entityType, mrefAttr)) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(idAttribute)) .append(')'); } } return from.toString(); } }
PostgreSqlQueryGenerator { private static <E extends Entity> String getSqlFrom(EntityType entityType, Query<E> q) { List<Attribute> mrefAttrsInQuery = getJoinQueryAttrs(entityType, q); StringBuilder from = new StringBuilder(" FROM ").append(getTableName(entityType)).append(" AS this"); Attribute idAttribute = entityType.getIdAttribute(); for (int i = 0; i < mrefAttrsInQuery.size(); i++) { Attribute mrefAttr = mrefAttrsInQuery.get(i); if (mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy()) { from.append(" LEFT JOIN ") .append(getTableName(mrefAttr.getRefEntity())) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(mrefAttr.getMappedBy())) .append(')'); } else { from.append(" LEFT JOIN ") .append(getJunctionTableName(entityType, mrefAttr)) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(idAttribute)) .append(')'); } } return from.toString(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { private static <E extends Entity> String getSqlFrom(EntityType entityType, Query<E> q) { List<Attribute> mrefAttrsInQuery = getJoinQueryAttrs(entityType, q); StringBuilder from = new StringBuilder(" FROM ").append(getTableName(entityType)).append(" AS this"); Attribute idAttribute = entityType.getIdAttribute(); for (int i = 0; i < mrefAttrsInQuery.size(); i++) { Attribute mrefAttr = mrefAttrsInQuery.get(i); if (mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy()) { from.append(" LEFT JOIN ") .append(getTableName(mrefAttr.getRefEntity())) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(mrefAttr.getMappedBy())) .append(')'); } else { from.append(" LEFT JOIN ") .append(getJunctionTableName(entityType, mrefAttr)) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(idAttribute)) .append(')'); } } return from.toString(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { private static <E extends Entity> String getSqlFrom(EntityType entityType, Query<E> q) { List<Attribute> mrefAttrsInQuery = getJoinQueryAttrs(entityType, q); StringBuilder from = new StringBuilder(" FROM ").append(getTableName(entityType)).append(" AS this"); Attribute idAttribute = entityType.getIdAttribute(); for (int i = 0; i < mrefAttrsInQuery.size(); i++) { Attribute mrefAttr = mrefAttrsInQuery.get(i); if (mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy()) { from.append(" LEFT JOIN ") .append(getTableName(mrefAttr.getRefEntity())) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(mrefAttr.getMappedBy())) .append(')'); } else { from.append(" LEFT JOIN ") .append(getJunctionTableName(entityType, mrefAttr)) .append(" AS ") .append(getFilterColumnName(mrefAttr, i + 1)) .append(" ON (this.") .append(getColumnName(idAttribute)) .append(" = ") .append(getFilterColumnName(mrefAttr, i + 1)) .append('.') .append(getColumnName(idAttribute)) .append(')'); } } return from.toString(); } private  PostgreSqlQueryGenerator();   }