|
26 | 26 | import com.orientechnologies.orient.core.command.OCommandContext; |
27 | 27 | import com.orientechnologies.orient.core.db.record.ODatabaseRecord; |
28 | 28 | import com.orientechnologies.orient.core.db.record.OIdentifiable; |
29 | | -import com.orientechnologies.orient.core.id.ORID; |
30 | 29 | import com.orientechnologies.orient.core.id.ORecordId; |
31 | 30 | import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper; |
32 | 31 | import com.orientechnologies.orient.core.sql.filter.OSQLFilter; |
@@ -61,6 +60,40 @@ public class OSQLEngine { |
61 | 60 | private static OQueryOperator[] SORTED_OPERATORS = null; |
62 | 61 | private static ClassLoader orientClassLoader = OSQLEngine.class.getClassLoader(); |
63 | 62 |
|
| 63 | + /** |
| 64 | + * internal use only, to sort operators. |
| 65 | + */ |
| 66 | + private static final class Pair { |
| 67 | + |
| 68 | + final OQueryOperator before; |
| 69 | + final OQueryOperator after; |
| 70 | + |
| 71 | + public Pair(final OQueryOperator before, final OQueryOperator after) { |
| 72 | + this.before = before; |
| 73 | + this.after = after; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(final Object obj) { |
| 78 | + if (obj instanceof Pair) { |
| 79 | + final Pair that = (Pair) obj; |
| 80 | + return before == that.before && after == that.after; |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode() { |
| 87 | + return System.identityHashCode(before) + 31 * System.identityHashCode(after); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String toString() { |
| 92 | + return before + " > " + after; |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
64 | 97 | protected OSQLEngine() { |
65 | 98 | } |
66 | 99 |
|
@@ -263,6 +296,20 @@ public static OCollate getCollate(final String name) { |
263 | 296 | return null; |
264 | 297 | } |
265 | 298 |
|
| 299 | + public static OSQLMethod getMethod(String iMethodName) { |
| 300 | + iMethodName = iMethodName.toLowerCase(Locale.ENGLISH); |
| 301 | + |
| 302 | + final Iterator<OSQLMethodFactory> ite = getMethodFactories(); |
| 303 | + while (ite.hasNext()) { |
| 304 | + final OSQLMethodFactory factory = ite.next(); |
| 305 | + if (factory.hasMethod(iMethodName)) { |
| 306 | + return factory.createMethod(iMethodName); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + return null; |
| 311 | + } |
| 312 | + |
266 | 313 | public synchronized OQueryOperator[] getRecordOperators() { |
267 | 314 | if (SORTED_OPERATORS != null) { |
268 | 315 | return SORTED_OPERATORS; |
@@ -396,75 +443,48 @@ public OSQLTarget parseTarget(final String iText, final OCommandContext iContext |
396 | 443 | return new OSQLTarget(iText, iContext, iFilterKeyword); |
397 | 444 | } |
398 | 445 |
|
399 | | - public Set<ORID> parseRIDTarget(final ODatabaseRecord database, final String iTarget) { |
400 | | - final Set<ORID> ids; |
| 446 | + public Set<OIdentifiable> parseRIDTarget(final ODatabaseRecord database, String iTarget, final OCommandContext iContext) { |
| 447 | + final Set<OIdentifiable> ids; |
401 | 448 | if (iTarget.startsWith("(")) { |
402 | 449 | // SUB-QUERY |
403 | | - final List<OIdentifiable> result = database.query(new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1))); |
| 450 | + final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1)); |
| 451 | + query.setContext(iContext); |
| 452 | + |
| 453 | + final List<OIdentifiable> result = database.query(query); |
404 | 454 | if (result == null || result.isEmpty()) |
405 | 455 | ids = Collections.emptySet(); |
406 | 456 | else { |
407 | | - ids = new HashSet<ORID>((int) (result.size() * 1.3)); |
| 457 | + ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3)); |
408 | 458 | for (OIdentifiable aResult : result) |
409 | 459 | ids.add(aResult.getIdentity()); |
410 | 460 | } |
411 | 461 | } else if (iTarget.startsWith("[")) { |
412 | 462 | // COLLECTION OF RIDS |
413 | 463 | final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(","); |
414 | | - ids = new HashSet<ORID>((int) (idsAsStrings.length * 1.3)); |
415 | | - for (String idsAsString : idsAsStrings) |
416 | | - ids.add(new ORecordId(idsAsString)); |
417 | | - } else |
418 | | - // SINGLE RID |
419 | | - ids = Collections.<ORID> singleton(new ORecordId(iTarget)); |
420 | | - return ids; |
421 | | - } |
422 | | - |
423 | | - public static OSQLMethod getMethod(String iMethodName) { |
424 | | - iMethodName = iMethodName.toLowerCase(Locale.ENGLISH); |
425 | | - |
426 | | - final Iterator<OSQLMethodFactory> ite = getMethodFactories(); |
427 | | - while (ite.hasNext()) { |
428 | | - final OSQLMethodFactory factory = ite.next(); |
429 | | - if (factory.hasMethod(iMethodName)) { |
430 | | - return factory.createMethod(iMethodName); |
431 | | - } |
432 | | - } |
433 | | - |
434 | | - return null; |
435 | | - } |
436 | | - |
437 | | - /** |
438 | | - * internal use only, to sort operators. |
439 | | - */ |
440 | | - private static final class Pair { |
441 | | - |
442 | | - final OQueryOperator before; |
443 | | - final OQueryOperator after; |
444 | | - |
445 | | - public Pair(final OQueryOperator before, final OQueryOperator after) { |
446 | | - this.before = before; |
447 | | - this.after = after; |
448 | | - } |
449 | | - |
450 | | - @Override |
451 | | - public boolean equals(final Object obj) { |
452 | | - if (obj instanceof Pair) { |
453 | | - final Pair that = (Pair) obj; |
454 | | - return before == that.before && after == that.after; |
| 464 | + ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3)); |
| 465 | + for (String idsAsString : idsAsStrings) { |
| 466 | + if (idsAsString.startsWith("$")) { |
| 467 | + Object r = iContext.getVariable(idsAsString); |
| 468 | + if (r instanceof OIdentifiable) |
| 469 | + ids.add((OIdentifiable) r); |
| 470 | + else |
| 471 | + OMultiValue.add(ids, r); |
| 472 | + } else |
| 473 | + ids.add(new ORecordId(idsAsString)); |
455 | 474 | } |
456 | | - return false; |
457 | | - } |
| 475 | + } else { |
| 476 | + // SINGLE RID |
| 477 | + if (iTarget.startsWith("$")) { |
| 478 | + Object r = iContext.getVariable(iTarget); |
| 479 | + if (r instanceof OIdentifiable) |
| 480 | + ids = Collections.<OIdentifiable> singleton((OIdentifiable) r); |
| 481 | + else |
| 482 | + ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r); |
458 | 483 |
|
459 | | - @Override |
460 | | - public int hashCode() { |
461 | | - return System.identityHashCode(before) + 31 * System.identityHashCode(after); |
462 | | - } |
| 484 | + } else |
| 485 | + ids = Collections.<OIdentifiable> singleton(new ORecordId(iTarget)); |
463 | 486 |
|
464 | | - @Override |
465 | | - public String toString() { |
466 | | - return before + " > " + after; |
467 | 487 | } |
468 | | - |
| 488 | + return ids; |
469 | 489 | } |
470 | 490 | } |
0 commit comments