@@ -144,16 +144,21 @@ public static String toTable(String alias) |
144 | 144 | return alias == null ? null : alias.substring(0, 1).toUpperCase().concat(alias.substring(1)); |
145 | 145 | } |
146 | 146 |
|
| 147 | + // TODO: Cleanup |
147 | 148 | public static class Builder<T> implements Observer |
148 | 149 | { |
149 | | - private Class<?> retType; |
| 150 | + private Class<T> retType; |
150 | 151 | private boolean isNativeQuery = false; |
151 | 152 | private final Stack<Capture> captures = new Stack<>(); |
152 | 153 | private final Set<String> tableNames = new LinkedHashSet<>(); |
153 | 154 | private final ParameterBinding params = new JpqlBinding(); |
| 155 | + private final List<CapturedStatement> select = new ArrayList<>(); |
| 156 | + private final List<CapturedStatement> groupBy = new ArrayList<>(); |
| 157 | + private final List<CapturedStatement> orderBy = new ArrayList<>(); |
| 158 | + private ORDER_BY order = null; |
154 | 159 | private final List<CapturedStatement> statements = new ArrayList<>(); |
155 | | - private final List<CapturedStatement> customSelect = new ArrayList<>(); |
156 | 160 |
|
| 161 | + @SuppressWarnings("unchecked") |
157 | 162 | Builder(T main, Object... others) |
158 | 163 | { |
159 | 164 | assert proxy2Class.containsKey(main.getClass()) // |
@@ -166,7 +171,7 @@ public static class Builder<T> implements Observer |
166 | 171 | tableNames.add(EntityMatcher.proxy2Class.get(main.getClass()).getSimpleName()); |
167 | 172 | Arrays.asList(others).forEach(o -> tableNames.add(EntityMatcher.proxy2Class.get(o.getClass()).getSimpleName())); |
168 | 173 |
|
169 | | - retType = EntityMatcher.proxy2Class.get(main.getClass()); |
| 174 | + retType = (Class<T>) EntityMatcher.proxy2Class.get(main.getClass()); |
170 | 175 | } |
171 | 176 |
|
172 | 177 | /** |
@@ -180,15 +185,56 @@ public static class Builder<T> implements Observer |
180 | 185 | */ |
181 | 186 | public Builder<T> select(Object... os) |
182 | 187 | { |
183 | | - customSelect.clear(); // doesn't allow incremental selects. |
| 188 | + select.clear(); // doesn't allow incremental selects. |
184 | 189 | for (int i = 0; i < os.length; i++) |
185 | 190 | { |
186 | 191 | final Capture lastCapture = getLastCapture(); |
187 | 192 | assert lastCapture == null : "Builders are not thread safe."; |
188 | 193 |
|
189 | | - if (os[i] instanceof LhsRhsStatement) customSelect.add(new CapturedStatement(lastCapture, null, |
| 194 | + if (os[i] instanceof LhsRhsStatement) select.add(new CapturedStatement(lastCapture, null, |
190 | 195 | (LhsRhsStatement<?>) os[i])); |
191 | | - else customSelect.add(new CapturedStatement(lastCapture, null, null)); |
| 196 | + else select.add(new CapturedStatement(lastCapture, null, null)); |
| 197 | + } |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + public enum ORDER_BY |
| 202 | + { |
| 203 | + ASC, DESC |
| 204 | + }; |
| 205 | + |
| 206 | + public Builder<T> orderBy(Object... os) |
| 207 | + { |
| 208 | + return orderBy(ORDER_BY.ASC, os); |
| 209 | + } |
| 210 | + |
| 211 | + public Builder<T> orderBy(ORDER_BY order, Object... os) |
| 212 | + { |
| 213 | + orderBy.clear(); // doesn't allow incremental orderBy's. |
| 214 | + this.order = order; |
| 215 | + for (int i = 0; i < os.length; i++) |
| 216 | + { |
| 217 | + final Capture lastCapture = getLastCapture(); |
| 218 | + assert lastCapture == null : "Builders are not thread safe."; |
| 219 | + |
| 220 | + if (os[i] instanceof LhsRhsStatement) orderBy.add(new CapturedStatement(lastCapture, null, |
| 221 | + (LhsRhsStatement<?>) os[i])); |
| 222 | + else orderBy.add(new CapturedStatement(lastCapture, null, null)); |
| 223 | + } |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + public Builder<T> groupBy(Object... os) |
| 228 | + { |
| 229 | + groupBy.clear(); // doesn't allow incremental groupBy's. |
| 230 | + for (int i = 0; i < os.length; i++) |
| 231 | + { |
| 232 | + final Capture lastCapture = getLastCapture(); |
| 233 | + assert lastCapture == null : "Builders are not thread safe."; |
| 234 | + |
| 235 | + if (os[i] instanceof LhsRhsStatement) groupBy.add(new CapturedStatement(lastCapture, null, |
| 236 | + (LhsRhsStatement<?>) os[i])); |
| 237 | + else groupBy.add(new CapturedStatement(lastCapture, null, null)); |
192 | 238 | } |
193 | 239 | return this; |
194 | 240 | } |
@@ -269,8 +315,7 @@ private String getColumnName(Field f) |
269 | 315 | final Column c = f.getAnnotation(Column.class); |
270 | 316 | return c == null || c.name().isEmpty() ? f.getName() : c.name(); |
271 | 317 | } |
272 | | - else |
273 | | - return f.getName(); |
| 318 | + else return f.getName(); |
274 | 319 | } |
275 | 320 |
|
276 | 321 | Capture getLastCapture() |
@@ -315,6 +360,9 @@ public String toString() |
315 | 360 |
|
316 | 361 | public PreparedQuery<T> build() |
317 | 362 | { |
| 363 | + if (isNativeQuery) |
| 364 | + return build(retType); |
| 365 | + |
318 | 366 | return new PreparedQuery<T>() |
319 | 367 | { |
320 | 368 | @Override |
@@ -347,16 +395,19 @@ public String toString() |
347 | 395 | }; |
348 | 396 | } |
349 | 397 |
|
| 398 | + @SuppressWarnings("unchecked") |
350 | 399 | public <E> PreparedQuery<E> build(Class<E> clazz) |
351 | 400 | { |
| 401 | + if (clazz.equals(retType) && !isNativeQuery) |
| 402 | + return (PreparedQuery<E>) build(); |
| 403 | + |
352 | 404 | return new PreparedQuery<E>() |
353 | 405 | { |
354 | | - @SuppressWarnings("unchecked") |
355 | 406 | private Function<Object[], E> copyProperties = os -> |
356 | 407 | { |
357 | 408 | try |
358 | 409 | { |
359 | | - assert os.length == customSelect.size() : "Request / response column size mismatch."; |
| 410 | + assert os.length == select.size() : "Request / response column size mismatch."; |
360 | 411 |
|
361 | 412 | // If length == 1, it could be two options: |
362 | 413 | // 1. List<Integer> is = ...build(Integer.class).getMatching(em) |
@@ -377,13 +428,37 @@ else if (clazz.isAssignableFrom(o.getClass())) |
377 | 428 | } |
378 | 429 |
|
379 | 430 | final E e = clazz.newInstance(); |
380 | | - for (int i = 0; i < os.length; i++) |
| 431 | + if (select.isEmpty()) |
381 | 432 | { |
382 | | - final CapturedStatement c = customSelect.get(i); |
383 | | - final Class<?> paramType = c.lhs.method.getReturnType(); |
384 | | - final String setterName = c.lhs.method.getName().replaceFirst("(is|get)", "set"); |
385 | | - final Method setterMethod = clazz.getMethod(setterName, paramType); |
386 | | - setterMethod.invoke(e, os[i]); |
| 433 | + int i = 0; |
| 434 | + // FIXME review all this. |
| 435 | + for (Field f : clazz.getDeclaredFields()) |
| 436 | + { |
| 437 | + if (f.isAnnotationPresent(Transient.class)) |
| 438 | + continue; |
| 439 | + |
| 440 | + if (os[i] == null) |
| 441 | + { |
| 442 | + i++; |
| 443 | + continue; |
| 444 | + } |
| 445 | + |
| 446 | + final int rank = NumberConversion.getRank(f.getType()); |
| 447 | + final Object o = rank >= 0 ? NumberConversion.convert(os[i], rank) : os[i]; |
| 448 | + f.set(e, o); |
| 449 | + i++; |
| 450 | + } |
| 451 | + } |
| 452 | + else |
| 453 | + { |
| 454 | + for (int i = 0; i < os.length; i++) |
| 455 | + { |
| 456 | + final CapturedStatement c = select.get(i); |
| 457 | + final Class<?> paramType = c.lhs.method.getReturnType(); |
| 458 | + final String setterName = c.lhs.method.getName().replaceFirst("(is|get)", "set"); |
| 459 | + final Method setterMethod = clazz.getMethod(setterName, paramType); |
| 460 | + setterMethod.invoke(e, os[i]); |
| 461 | + } |
387 | 462 | } |
388 | 463 | return e; |
389 | 464 | } |
@@ -409,7 +484,6 @@ public E getSingleMatching(EntityManager em) |
409 | 484 | } |
410 | 485 |
|
411 | 486 | @Override |
412 | | - @SuppressWarnings("unchecked") |
413 | 487 | public List<E> getMatching(EntityManager em) |
414 | 488 | { |
415 | 489 | return Lists.transform(createQuery(em, isNativeQuery).getResultList(), copyProperties); |
@@ -444,15 +518,60 @@ public int compare(CapturedStatement o1, CapturedStatement o2) |
444 | 518 |
|
445 | 519 | final Set<String> unaliasedTables = new LinkedHashSet<>(Builder.this.tableNames); |
446 | 520 | final StringBuilder fromClause = new StringBuilder().append(" FROM "); |
447 | | - final String select = composeSelect(fromClause, unaliasedTables); |
448 | | - |
| 521 | + final String select = appendSelectAndFrom(fromClause, unaliasedTables); |
449 | 522 | if (statements.isEmpty()) |
450 | 523 | { |
451 | | - return new StringBuilder(select).append(removeLastComma(fromClause)).toString(); |
| 524 | + return new StringBuilder(select).append(removeLastComma(fromClause)).append(getGroupByClause()).append(getOrderByClause()).toString(); |
452 | 525 | } |
453 | 526 |
|
454 | 527 | final StringBuilder whereClause = new StringBuilder().append(" WHERE "); |
| 528 | + appendFromAndWhere(unaliasedTables, fromClause, whereClause); |
| 529 | + |
| 530 | + final StringBuilder groupByClause = getGroupByClause(); |
| 531 | + final StringBuilder orderByClause = getOrderByClause(); |
| 532 | + |
| 533 | + return new StringBuilder(select).append(fromClause).append(whereClause).append(groupByClause).append(orderByClause) |
| 534 | + .toString(); |
| 535 | + } |
| 536 | + |
| 537 | + private StringBuilder getOrderByClause() |
| 538 | + { |
| 539 | + final StringBuilder orderByClause = new StringBuilder(); |
| 540 | + if (!orderBy.isEmpty()) |
| 541 | + { |
| 542 | + orderByClause.append(" ORDER BY "); |
| 543 | + for (Iterator<CapturedStatement> it = orderBy.iterator(); it.hasNext();) |
| 544 | + { |
| 545 | + final CapturedStatement next = it.next(); |
| 546 | + orderByClause.append(toAlias(getTableName(next.lhs))).append(".").append(getColumnName(next.lhs)); |
| 547 | + if (it.hasNext()) |
| 548 | + orderByClause.append(", "); |
| 549 | + } |
| 550 | + orderByClause.append(" ").append(order.name()); |
| 551 | + } |
| 552 | + return orderByClause; |
| 553 | + } |
| 554 | + |
| 555 | + private StringBuilder getGroupByClause() |
| 556 | + { |
| 557 | + final StringBuilder groupByClause = new StringBuilder(); |
| 558 | + if (!groupBy.isEmpty()) |
| 559 | + { |
| 560 | + groupByClause.append(" GROUP BY "); |
| 561 | + for (Iterator<CapturedStatement> it = groupBy.iterator(); it.hasNext();) |
| 562 | + { |
| 563 | + final CapturedStatement next = it.next(); |
| 564 | + groupByClause.append(toAlias(getTableName(next.lhs))).append(".").append(getColumnName(next.lhs)); |
| 565 | + if (it.hasNext()) |
| 566 | + groupByClause.append(", "); |
| 567 | + } |
| 568 | + } |
| 569 | + return groupByClause; |
| 570 | + } |
455 | 571 |
|
| 572 | + private void appendFromAndWhere(final Set<String> unaliasedTables, final StringBuilder fromClause, |
| 573 | + final StringBuilder whereClause) |
| 574 | + { |
456 | 575 | final Iterator<CapturedStatement> it = statements.iterator(); |
457 | 576 | while (it.hasNext()) |
458 | 577 | { |
@@ -495,7 +614,6 @@ public int compare(CapturedStatement o1, CapturedStatement o2) |
495 | 614 | } |
496 | 615 |
|
497 | 616 | removeLastComma(fromClause); |
498 | | - return new StringBuilder(select).append(fromClause).append(whereClause).toString(); |
499 | 617 | } |
500 | 618 |
|
501 | 619 | // It is actually easier to remove a known last comma, that to handle all unknowns while |
@@ -505,11 +623,21 @@ private StringBuilder removeLastComma(final StringBuilder fromClause) |
505 | 623 | return fromClause.replace(fromClause.length() - 2, fromClause.length(), ""); |
506 | 624 | } |
507 | 625 |
|
508 | | - private String composeSelect(StringBuilder fromClause, Set<String> unaliasedTables) |
| 626 | + private String appendSelectAndFrom(StringBuilder fromClause, Set<String> unaliasedTables) |
509 | 627 | { |
510 | | - return !customSelect.isEmpty() ? createCustomSelect(fromClause, unaliasedTables) |
511 | | - : isNativeQuery ? createNativeSelect(fromClause, unaliasedTables) : "SELECT ".concat(toAlias(retType |
512 | | - .getSimpleName())); |
| 628 | + return !select.isEmpty() ? createCustomSelect(fromClause, unaliasedTables) |
| 629 | + : isNativeQuery ? createNativeSelect(fromClause, unaliasedTables) : createSimpleSelect(fromClause, |
| 630 | + unaliasedTables); |
| 631 | + } |
| 632 | + |
| 633 | + private String createSimpleSelect(StringBuilder fromClause, Set<String> unaliasedTables) |
| 634 | + { |
| 635 | + final String tableName = getTableName(retType); |
| 636 | + final String tableAlias = toAlias(tableName); |
| 637 | + unaliasedTables.remove(tableName); |
| 638 | + |
| 639 | + fromClause.append(tableName).append(" ").append(tableAlias).append(", "); |
| 640 | + return "SELECT ".concat(tableAlias); |
513 | 641 | } |
514 | 642 |
|
515 | 643 | private String createNativeSelect(StringBuilder fromClause, Set<String> unaliasedTables) |
@@ -539,7 +667,7 @@ private String createNativeSelect(StringBuilder fromClause, Set<String> unaliase |
539 | 667 | private String createCustomSelect(StringBuilder fromClause, Set<String> unaliasedTables) |
540 | 668 | { |
541 | 669 | final StringBuilder sb = new StringBuilder("SELECT "); |
542 | | - for (Iterator<CapturedStatement> it = customSelect.iterator(); it.hasNext();) |
| 670 | + for (Iterator<CapturedStatement> it = select.iterator(); it.hasNext();) |
543 | 671 | { |
544 | 672 | final CapturedStatement next = it.next(); |
545 | 673 | final String tableName = getTableName(next.lhs); |
|