Пример #1
0
  /**
   * Adds {@literal order by} clause to the JPQL query.
   *
   * @param query
   * @param sort
   * @param alias
   * @return
   */
  public static String applySorting(String query, Sort sort, String alias) {

    Assert.hasText(query);

    if (null == sort || !sort.iterator().hasNext()) {
      return query;
    }

    StringBuilder builder = new StringBuilder(query);

    if (!ORDER_BY.matcher(query).matches()) {
      builder.append(" order by ");
    } else {
      builder.append(", ");
    }

    Set<String> aliases = getOuterJoinAliases(query);

    for (Order order : sort) {
      builder.append(getOrderClause(aliases, alias, order)).append(", ");
    }

    builder.delete(builder.length() - 2, builder.length());

    return builder.toString();
  }
Пример #2
0
  /**
   * Creates a where-clause referencing the given entities and appends it to the given query string.
   * Binds the given entities to the query.
   *
   * @param <T>
   * @param queryString
   * @param entities
   * @param entityManager
   * @return
   */
  public static <T> Query applyAndBind(
      String queryString, Iterable<T> entities, EntityManager entityManager) {

    Assert.notNull(queryString);
    Assert.notNull(entities);
    Assert.notNull(entityManager);

    Iterator<T> iterator = entities.iterator();

    if (!iterator.hasNext()) {
      return entityManager.createQuery(queryString);
    }

    String alias = detectAlias(queryString);
    StringBuilder builder = new StringBuilder(queryString);
    builder.append(" where");

    int i = 0;

    while (iterator.hasNext()) {

      iterator.next();

      builder.append(String.format(" %s = ?%d", alias, ++i));

      if (iterator.hasNext()) {
        builder.append(" or");
      }
    }

    Query query = entityManager.createQuery(builder.toString());

    iterator = entities.iterator();
    i = 0;

    while (iterator.hasNext()) {
      query.setParameter(++i, iterator.next());
    }

    return query;
  }
Пример #3
0
  /**
   * Returns the query string to execute an exists query for the given id attributes.
   *
   * @param entityName the name of the entity to create the query for, must not be {@literal null}.
   * @param countQueryPlaceHolder the placeholder for the count clause, must not be {@literal null}.
   * @param idAttributes the id attributes for the entity, must not be {@literal null}.
   * @return
   */
  public static String getExistsQueryString(
      String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes) {

    StringBuilder sb =
        new StringBuilder(String.format(COUNT_QUERY_STRING, countQueryPlaceHolder, entityName));
    sb.append(" WHERE ");

    for (String idAttribute : idAttributes) {
      sb.append(String.format(EQUALS_CONDITION_STRING, "x", idAttribute, idAttribute));
      sb.append(" AND ");
    }

    sb.append("1 = 1");
    return sb.toString();
  }
Пример #4
0
  static {
    StringBuilder builder = new StringBuilder();
    builder.append("(?<=from)"); // from as starting delimiter
    builder.append("(?: )+"); // at least one space separating
    builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
    builder.append("(?: as)*"); // exclude possible "as" keyword
    builder.append("(?: )+"); // at least one space separating
    builder.append("(\\w*)"); // the actual alias

    ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE);

    builder = new StringBuilder();
    builder.append("(select\\s+((distinct )?(.+?)?)\\s+)?(from\\s+");
    builder.append(IDENTIFIER);
    builder.append("(?:\\s+as)?\\s+)");
    builder.append(IDENTIFIER_GROUP);
    builder.append("(.*)");

    COUNT_MATCH = compile(builder.toString(), CASE_INSENSITIVE);

    Map<PersistentAttributeType, Class<? extends Annotation>> persistentAttributeTypes =
        new HashMap<PersistentAttributeType, Class<? extends Annotation>>();
    persistentAttributeTypes.put(ONE_TO_ONE, OneToOne.class);
    persistentAttributeTypes.put(ONE_TO_MANY, null);
    persistentAttributeTypes.put(MANY_TO_ONE, ManyToOne.class);
    persistentAttributeTypes.put(MANY_TO_MANY, null);

    ASSOCIATION_TYPES = Collections.unmodifiableMap(persistentAttributeTypes);
  }