-
Notifications
You must be signed in to change notification settings - Fork 3
/
OQLQueryImpl.java
2902 lines (2651 loc) · 118 KB
/
OQLQueryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* IBM Corporation - bug fixes for instanceof, big changes for tables
*******************************************************************************/
package org.eclipse.mat.parser.internal.oql;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.collect.ArrayLong;
import org.eclipse.mat.collect.IteratorInt;
import org.eclipse.mat.collect.IteratorLong;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.parser.internal.Messages;
import org.eclipse.mat.parser.internal.oql.compiler.CompilerImpl;
import org.eclipse.mat.parser.internal.oql.compiler.EvaluationContext;
import org.eclipse.mat.parser.internal.oql.compiler.Expression;
import org.eclipse.mat.parser.internal.oql.compiler.Query;
import org.eclipse.mat.parser.internal.oql.compiler.Query.FromClause;
import org.eclipse.mat.parser.internal.oql.compiler.Query.SelectClause;
import org.eclipse.mat.parser.internal.oql.compiler.Query.SelectItem;
import org.eclipse.mat.parser.internal.oql.parser.OQLParser;
import org.eclipse.mat.parser.internal.oql.parser.ParseException;
import org.eclipse.mat.parser.internal.oql.parser.TokenMgrError;
import org.eclipse.mat.query.Column;
import org.eclipse.mat.query.ContextProvider;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IContextObjectSet;
import org.eclipse.mat.query.IResultTable;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.query.IStructuredResult;
import org.eclipse.mat.query.ResultMetaData;
import org.eclipse.mat.snapshot.IOQLQuery;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.OQL;
import org.eclipse.mat.snapshot.OQLParseException;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.ObjectReference;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.PatternUtil;
import org.eclipse.mat.util.SilentProgressListener;
import org.eclipse.mat.util.SimpleMonitor;
import org.eclipse.mat.util.VoidProgressListener;
public class OQLQueryImpl implements IOQLQuery
{
Query query;
EvaluationContext ctx;
// //////////////////////////////////////////////////////////////
// result set implementations
// //////////////////////////////////////////////////////////////
private interface CustomTableResultSet extends IOQLQuery.Result, IResultTable, List<AbstractCustomTableResultSet.RowMap>
{}
/**
* Find the distinct rows in the table.
* @param table
* @param listener
* @return an array of indexes of unique rows
*/
private static ArrayInt distinctList(CustomTableResultSet table, IProgressListener listener)
{
LinkedHashSet<Object> hs = new LinkedHashSet<Object>();
ArrayInt newrows = new ArrayInt();
int i = 0;
for (Object row : table)
{
if (hs.add(row))
{
newrows.add(i);
}
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
++i;
}
hs.clear();
return newrows;
}
/**
* A table holding the result of a query.
*
*/
private static abstract class AbstractCustomTableResultSet extends AbstractList<AbstractCustomTableResultSet.RowMap> implements CustomTableResultSet
{
/**
* Holds a row from a sub-select with columns ready for a select.
*/
private static class RowMap extends AbstractMap<String,Object>
{
private static final Object NULL_VALUE = new Object();
private final class ColEntry implements Entry<String, Object>
{
int col2;
Object value;
private ColEntry(int col)
{
this.col2 = col;
}
@Override
public String getKey()
{
return isr.getColumns()[col2].getLabel();
}
@Override
public Object getValue()
{
Object o = value;
if (o == NULL_VALUE)
return null;
else if (o != null)
return o;
Object row;
if (irtb != null)
row = irtb.getRow(index);
else if (irtr != null)
row = irtr.getElements().get(index);
else
return null;
o = isr.getColumnValue(row, col2);
if (o == null)
value = NULL_VALUE;
else
value = o;
return o;
}
@Override
public Object setValue(Object value)
{
throw new UnsupportedOperationException();
}
public int hashCode()
{
return Objects.hash(getKey(), getValue());
}
public boolean equals(Object o)
{
if ((o instanceof Entry<?,?>))
{
Entry<?,?>ox = (Entry<?,?>)o;
return Objects.equals(getKey(), ox.getKey()) &&
Objects.equals(getValue(), ox.getValue());
}
{
return false;
}
}
}
IStructuredResult isr;
IResultTable irtb;
IResultTree irtr;
int index;
int subindex;
public RowMap(IStructuredResult irt, int index)
{
this(irt, index, -1);
}
public RowMap(IStructuredResult irt, int index, int subindex)
{
this.isr = irt;
if (isr instanceof IResultTable)
this.irtb = (IResultTable)irt;
if (isr instanceof IResultTree)
this.irtr = (IResultTree)irt;
this.index = index;
this.subindex = subindex;
}
@Override
public int size()
{
return isr.getColumns().length;
}
@Override
public Object get(Object colname)
{
for (int col = 0; col < isr.getColumns().length; ++col)
{
if (isr.getColumns()[col].getLabel().equals(colname))
{
Object row;
if (irtb != null)
row = irtb.getRow(index);
else if (irtr != null)
row = irtr.getElements().get(index);
else
return null;
return isr.getColumnValue(row, col);
}
}
return null;
}
public int getObjectId()
{
IContextObject cx = isr.getContext(index);
if (cx != null)
return cx.getObjectId();
return -1;
}
@Override
public Set<Entry<String, Object>> entrySet()
{
AbstractSet<Entry<String, Object>> set = new AbstractSet<Entry<String, Object>>() {
@Override
public Iterator<Entry<String, Object>> iterator()
{
return new Iterator<Entry<String, Object>>() {
int col;
@Override
public boolean hasNext()
{
return col < size();
}
@Override
public Entry<String, Object> next()
{
if (col >= size())
throw new NoSuchElementException(Integer.toString(col));
return new ColEntry(col++);
}
};
}
@Override
public int size()
{
return isr.getColumns().length;
}
};
return set;
}
}
public AbstractCustomTableResultSet(OQLQueryImpl source)
{
this.source = source;
}
/**
* Find the object ID of an IObject or a row backed by an IObject
* @param o
* @return
*/
static int getObjectId(Object o)
{
try
{
if (o instanceof IObject)
return ((IObject)o).getObjectId();
//else if (o instanceof RowMap)
// return ((RowMap)o).getObjectId();
}
catch (RuntimeException e)
{
if (e.getCause() instanceof SnapshotException)
return -1;
throw e;
}
return -1;
}
protected OQLQueryImpl source;
protected Column[] columns;
@Override
public RowMap get(int index)
{
// Always return a map not a single item for consistency.
// Except if the alias is "" as this used for CompareTablesQuery.
if (getColumns().length == 1 && getColumns()[0].getLabel().isEmpty())
{
Object ret = getColumnValue(getRow(index), 0);
// Squash?
if (ret instanceof RowMap)
return (RowMap)ret;
}
// Delay resolving columns until needed
return new RowMap(this, index);
}
@Override
public int size()
{
return getRowCount();
}
/**
* Get more details for a table.
* Allows extra menu options for columns holding objects.
* @param rt
* @param columns
* @param source
* @return
*/
protected ResultMetaData getResultMetaData(OQLQueryImpl source)
{
Query query = source.query;
Column columns[] = getColumns();
int objectCount = source.ctx.getSnapshot().getSnapshotInfo().getNumberOfObjects();
ResultMetaData.Builder builder = new ResultMetaData.Builder();
int prov = 0;
for (int ii = 0; ii < columns.length; ++ii)
{
// Find an example object for each column
Object sample = getRowCount() > 0 ? getColumnValue(getRow(0), ii) : null;
// See if it is one or more objects from the dump
if (getObjectId(sample) != -1 || sample instanceof IObject ||
sample instanceof Iterable<?> && ((Iterable<?>)sample).iterator().hasNext() && getObjectId(((Iterable<?>)sample).iterator().next()) != -1 ||
sample instanceof int[])
{
// Also add the underlying row
if (prov == 0 && getContext(getRow(0)) != null)
{
String label;
FromClause fc = query.getFromClause();
String alias = fc.getAlias();
if (alias != null)
label = alias;
else
label = fc.toString();
// Distinguish the select for all the columns
label = "SELECT ... " + label; //$NON-NLS-1$
// Use a null label to provide a default context without a sub-menu
builder.addContext(new ContextProvider(label) {
@Override
public IContextObject getContext(Object row)
{
return AbstractCustomTableResultSet.this.getContext(row);
}
});
++prov;
};
int columnIndex = ii;
builder.addContext(new ContextProvider(columns[ii].getLabel()) {
@Override
public IContextObject getContext(Object row)
{
Object o = getColumnValue(row, columnIndex);
int objectId = getObjectId(o);
boolean goodContext = false;
if (objectId == -1 && !(o instanceof Iterable<?> || o instanceof int[]))
{
IContextObject cx = AbstractCustomTableResultSet.this.getContext(row);
if (cx != null)
{
int selectId = cx.getObjectId();
if (selectId != -1)
goodContext = true;
else if (cx instanceof IContextObjectSet)
{
if (((IContextObjectSet)cx).getOQL() != null)
goodContext = true;
}
}
}
if (objectId != -1 || o instanceof IObject || goodContext)
{
return new IContextObjectSet() {
@Override
public int getObjectId()
{
return objectId;
}
@Override
public int[] getObjectIds()
{
if (objectId != -1)
return new int[] {objectId};
else
return new int[0];
}
@Override
public String getOQL()
{
String alias = query.getFromClause().getAlias();
String alias2;
if (alias == null)
alias2 = ""; //$NON-NLS-1$
else
alias2 = " "+alias; //$NON-NLS-1$
Query.SelectItem column = query.getSelectClause().getSelectList().get(columnIndex);
IContextObject cx = AbstractCustomTableResultSet.this.getContext(row);
if (cx != null)
{
int selectId = cx.getObjectId();
if (selectId != -1)
return "SELECT " + column.toString() + " FROM OBJECTS " + selectId + alias2; //$NON-NLS-1$ //$NON-NLS-2$
else if (cx instanceof IContextObjectSet)
{
IContextObjectSet cs = (IContextObjectSet)cx;
String oqlsource = cs.getOQL();
if (oqlsource != null)
{
try
{
OQLQueryImpl q2 = new OQLQueryImpl(oqlsource);
q2.query.getSelectClause().setSelectList(Collections.emptyList());
//String oqlsource2 = q2.query.toString();
//return "SELECT "+column.toString()+" FROM OBJECTS (" + oqlsource2+") "+alias2; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return "SELECT "+column.toString()+" FROM " + q2.query.getFromClause(); //$NON-NLS-1$//$NON-NLS-2$
}
catch (OQLParseException e)
{
}
}
}
}
if (objectId != -1)
return OQL.forObjectId(objectId);
else
return OQLforSubject("*", o, ""); //$NON-NLS-1$ //$NON-NLS-2$
}
};
}
else if (o instanceof Iterable<?> || o instanceof int[])
{
return new IContextObjectSet() {
@Override
public int getObjectId()
{
IContextObject cx = AbstractCustomTableResultSet.this.getContext(row);
if (cx != null)
return cx.getObjectId();
else
return -1;
}
@Override
public int[] getObjectIds()
{
if (o instanceof int[])
{
for (int ix : (int[])o)
{
if (ix < 0 || ix >= objectCount)
return new int[0];
}
return (int[])o;
}
ArrayInt ai = new ArrayInt();
if (o instanceof Iterable<?>)
{
Iterable<?>l = (Iterable<?>)o;
for (Object o : l)
{
int objectId = AbstractCustomTableResultSet.getObjectId(o);
if (objectId != -1)
{
ai.add(objectId);
}
}
}
return ai.toArray();
}
@Override
public String getOQL()
{
String alias = query.getFromClause().getAlias();
String alias2;
if (alias == null)
alias2 = ""; //$NON-NLS-1$
else
alias2 = " "+alias; //$NON-NLS-1$
Query.SelectItem column = query.getSelectClause().getSelectList().get(columnIndex);
int selectId = getObjectId();
if (selectId != -1)
return "SELECT "+column.toString()+" FROM OBJECTS " + selectId + alias2; //$NON-NLS-1$ //$NON-NLS-2$
else
{
IContextObject cx = AbstractCustomTableResultSet.this.getContext(row);
if (cx instanceof IContextObjectSet)
{
IContextObjectSet cs = (IContextObjectSet)cx;
String oqlsource = cs.getOQL();
if (oqlsource != null)
{
try
{
OQLQueryImpl q2 = new OQLQueryImpl(oqlsource);
q2.query.getSelectClause().setSelectList(Collections.emptyList());
String oqlsource2 = q2.query.toString();
return "SELECT " + column.toString() + " FROM OBJECTS (" + oqlsource2 + ") " + alias2; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
catch (OQLParseException e)
{
}
}
}
}
return OQL.forObjectIds(getObjectIds());
}
};
}
return null;
}
});
++prov;
}
}
return builder.build();
}
/**
* Simple toString() which does not evaluate all the rows and columns,
* which otherwise a subclass of AbstractList would do.
*/
public String toString()
{
int ncols = getColumns() != null ? getColumns().length : 0;
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getSimpleName()).append('@').append(Integer.toHexString(System.identityHashCode(this)));
sb.append('[').append(getRowCount()).append(']');
sb.append('[').append(ncols).append(']');
sb.append('[');
final int maxRows = 2;
for (int i = 0; i < Math.min(maxRows, getRowCount()); ++i)
{
sb.append(get(i).toString());
if (i + 1 < getRowCount())
sb.append(',');
}
if (getRowCount() > maxRows)
sb.append("..."); //$NON-NLS-1$
sb.append(']');
return sb.toString();
}
}
/**
* Result from a select with select list where the from clause returned a list or array of non-heap objects.
* Each row is backed by a non-heap object.
*/
private static class ObjectResultSet extends AbstractCustomTableResultSet
{
private static final Object NULL_VALUE = new Object();
private static class ValueHolder
{
Object subject;
Object[] values;
public ValueHolder(Object subject, Object[] values)
{
this.subject = subject;
this.values = values;
}
}
Object[] objects;
ObjectResultSet(OQLQueryImpl source, Collection<Object> objects) throws SnapshotException
{
this(source, objects.toArray());
}
ObjectResultSet(OQLQueryImpl source, Object[] objects) throws SnapshotException
{
super(source);
this.objects = objects;
List<SelectItem> selectList = source.query.getSelectClause().getSelectList();
columns = new Column[selectList.size()];
try
{
for (int ii = 0; ii < columns.length; ii++)
columns[ii] = buildColumn(selectList.get(ii), getRowCount() > 0 ? getColumnValue(getRow(0), ii) : null);
}
catch (RuntimeException e)
{
throw SnapshotException.rethrow(e);
}
}
public ResultMetaData getResultMetaData()
{
return getResultMetaData(source);
}
public Column[] getColumns()
{
return columns;
}
public int getRowCount()
{
return objects.length;
}
public Object getColumnValue(Object row, int columnIndex)
{
int index = (Integer) row;
if (!(objects[index] instanceof ValueHolder))
resolve(index);
ValueHolder holder = ((ValueHolder) objects[index]);
if (holder.values[columnIndex] == null)
{
try
{
source.ctx.setSubject(holder.subject);
// Don't track progress here for reading the cell
IProgressListener old = source.ctx.getProgressListener();
source.ctx.setProgressListener(new SilentProgressListener(old));
Query.SelectItem column = source.query.getSelectClause().getSelectList().get(columnIndex);
Object v = column.getExpression().compute(source.ctx);
source.ctx.setProgressListener(old);
holder.values[columnIndex] = v == null ? NULL_VALUE : v;
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
}
return holder.values[columnIndex] == NULL_VALUE ? null : holder.values[columnIndex];
}
public IContextObjectSet getContext(Object row)
{
final int index = (Integer) row;
if (!(objects[index] instanceof ValueHolder))
resolve(index);
Object subject = ((ValueHolder) objects[index]).subject;
int objectId = getObjectId(subject);
if (objectId != -1 || subject instanceof IObject || subject == null || subject instanceof Character || subject instanceof Integer
|| subject instanceof Long || subject instanceof Float || subject instanceof Double
|| subject instanceof Boolean || subject instanceof String || subject instanceof AbstractCustomTableResultSet.RowMap)
{
if (objectId == -1)
{
if (OQLforSubject("*", subject, "") == null) //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
return new IContextObjectSet()
{
public int getObjectId()
{
return objectId;
}
public int[] getObjectIds()
{
if (getObjectId() != -1)
return new int[] {getObjectId()};
else
return new int[0];
}
public String getOQL()
{
String alias = source.query.getFromClause().getAlias();
String alias2;
if (alias == null)
alias2 = ""; //$NON-NLS-1$
else
alias2 = " "+alias; //$NON-NLS-1$
SelectClause sc = source.query.getSelectClause();
if (sc.isRetainedSet())
{
// Remove asRetainedSet() as we just have a single object here
SelectClause sc2 = new SelectClause();
sc2.setAsObjects(sc.isAsObjects());
sc2.setSelectList(sc.getSelectList());
sc = sc2;
}
String from;
if (getObjectId() != -1)
from = String.valueOf(getObjectId());
else
{
return OQLforSubject(sc.toString(), subject, alias2);
}
return "SELECT "+sc.toString() +" FROM OBJECTS " + from + alias2; //$NON-NLS-1$ //$NON-NLS-2$
}
};
}
else
{
return null;
}
}
public Object getRow(int index)
{
return index;
}
public String getOQLQuery()
{
return source.toString();
}
private void resolve(int index)
{
ValueHolder answer = new ValueHolder(objects[index], new Object[columns.length]);
objects[index] = answer;
}
}
/**
* Result from a select with select list where the from clause returned an array of object IDs.
* Each row is backed by a heap object, so will appear in the inspector view.
*/
private static class ResultSet extends AbstractCustomTableResultSet
{
private static final Object NULL_VALUE = new Object();
private static class ValueHolder
{
Object[] values;
public ValueHolder(Object[] values)
{
this.values = values;
}
}
int[] objectIds;
ValueHolder[] objects;
public ResultSet(OQLQueryImpl source, int[] objectIds) throws SnapshotException
{
super(source);
this.objectIds = objectIds;
this.objects = new ValueHolder[objectIds.length];
List<SelectItem> selectList = source.query.getSelectClause().getSelectList();
columns = new Column[selectList.size()];
try
{
for (int ii = 0; ii < columns.length; ii++)
columns[ii] = buildColumn(selectList.get(ii), getRowCount() > 0 ? getColumnValue(getRow(0), ii) : null);
}
catch (RuntimeException e)
{
throw SnapshotException.rethrow(e);
}
}
public ResultMetaData getResultMetaData()
{
return getResultMetaData(source);
}
public int getRowCount()
{
return objectIds.length;
}
public Object getColumnValue(Object row, int columnIndex)
{
int index = (Integer) row;
if (objects[index] == null)
objects[index] = new ValueHolder(new Object[columns.length]);
if (objects[index].values[columnIndex] == null)
{
// each column value is calculated separately, because I do not
// want sorting to resolve all values in all rows
// NULL_VALUE is used to keep track of column values which have
// been calculated but returned a null value
try
{
IObject object = source.ctx.getSnapshot().getObject(objectIds[index]);
source.ctx.setSubject(object);
// Don't track progress here for reading the cell
IProgressListener old = source.ctx.getProgressListener();
source.ctx.setProgressListener(new SilentProgressListener(old));
List<SelectItem> selectList = source.query.getSelectClause().getSelectList();
Object value = selectList.get(columnIndex).getExpression().compute(source.ctx);
source.ctx.setProgressListener(old);
objects[index].values[columnIndex] = value != null ? value : NULL_VALUE;
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
}
Object value = objects[index].values[columnIndex];
return value == NULL_VALUE ? null : value;
}
public IContextObjectSet getContext(final Object row)
{
return new IContextObjectSet()
{
public int getObjectId()
{
return objectIds[(Integer) row];
}
public int[] getObjectIds()
{
return new int[] {getObjectId()};
}
public String getOQL()
{
String alias = source.query.getFromClause().getAlias();
String alias2;
if (alias == null)
alias2 = ""; //$NON-NLS-1$
else
alias2 = " "+alias; //$NON-NLS-1$
SelectClause sc = source.query.getSelectClause();
if (sc.isRetainedSet())
{
// Remove asRetainedSet() as we just have a single object here
SelectClause sc2 = new SelectClause();
sc2.setAsObjects(sc.isAsObjects());
sc2.setSelectList(sc.getSelectList());
sc = sc2;
}
return "SELECT " + sc.toString() + " FROM OBJECTS " + getObjectId() + alias2; //$NON-NLS-1$ //$NON-NLS-2$
}
};
}
public Object getRow(int index)
{
return index;
}
public Column[] getColumns()
{
return columns;
}
public String getOQLQuery()
{
return source.query.toString();
}
}
private static Column buildColumn(SelectItem column, Object columnValue)
{
String name = column.getName();
if (name == null)
name = column.getExpression().toString();
Class<?> type = columnValue != null ? columnValue.getClass() : Object.class;
return new Column(name, type).noTotals();
}
public static String OQLLiteral(Object subject)
{
// OQL literals (not Byte or Short)
if (subject instanceof Character)
return "'" + subject + "'"; //$NON-NLS-1$ //$NON-NLS-2$
else if (subject instanceof String)
return "\"" + subject + "\""; //$NON-NLS-1$ //$NON-NLS-2$
else if (subject instanceof Boolean || subject instanceof Integer)
return String.valueOf(subject);
else if (subject instanceof Float)
{
float f = (float)subject;
// OQL doesn't have literal NaN or infinity
if (Float.isFinite(f))
return String.valueOf(f);
else if (f == Float.POSITIVE_INFINITY)
return "1E99F"; //$NON-NLS-1$
else if (f == Float.NEGATIVE_INFINITY)
return "-1E99F"; //$NON-NLS-1$
else
// Actually evaluates to a Double, but hard to get OQL Float NaN
return "0.0F / 0.0F"; //$NON-NLS-1$
}
else if (subject instanceof Double)
{
double d = (double)subject;
// OQL doesn't have literal NaN or infinity
if (Double.isFinite(d))
{
// OQL doesn't parse double constants
// Evaluate as an expression of float constants
float f1 = (float)d;
double d2 = (d - f1);
if (d2 == 0)
return String.valueOf(f1) + "F + 0.0"; //$NON-NLS-1$
float f2 = (float)d2;
// Promote to a double as OQL add will do this
double d3 = d - ((double)f1 + f2);
if (d3 == 0)
return String.valueOf(f1) + "F + " + String.valueOf(f2) + "F"; //$NON-NLS-1$ //$NON-NLS-2$
float f3 = (float)d3;
double d4 = d - ((double)f1 + f2 / 1.0 + f3);
if (d4 == 0)
return String.valueOf(f1) + "F + " + String.valueOf(f2)+ "F + " + String.valueOf(f3) + "F"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return String.valueOf(d)+"D"; //$NON-NLS-1$
}
else if (d == Double.POSITIVE_INFINITY)
return "1.0D / 0.0D"; //$NON-NLS-1$
else if (d == Double.NEGATIVE_INFINITY)
return "-1.0D / 0.0D"; //$NON-NLS-1$
else
return "0.0D / 0.0D"; //$NON-NLS-1$
}
else if (subject == null)
// OQL null?
return "null"; //$NON-NLS-1$
else if (subject instanceof Long)
return Long.toString((Long) subject) + "L"; //$NON-NLS-1$
else if (subject instanceof IObject)
{
IObject obj = (IObject) subject;
try
{
return "${snapshot}.getObject(" + Integer.toString(obj.getObjectId()) + ")"; //$NON-NLS-1$//$NON-NLS-2$
}
catch (RuntimeException e)
{
if (e.getCause() instanceof SnapshotException)
{
return "eval((SELECT * FROM OBJECTS 0x" + Long.toHexString(obj.getObjectAddress()) + "))[0]"; //$NON-NLS-1$//$NON-NLS-2$
}
throw e;
}
}
else
return null;
}
public static String OQLforSubject(String select, Object subject, String alias)
{
String from;
String literal;
if (subject instanceof IObject)
from = "0x" + Long.toHexString(((IObject)subject).getObjectAddress()); //$NON-NLS-1$
else if ((literal = OQLLiteral(subject)) != null)
{
// OQL literals (not Byte or Short)
from = "(" + literal + ")"; //$NON-NLS-1$ //$NON-NLS-2$
}
else if (subject instanceof AbstractCustomTableResultSet.RowMap)
{
AbstractCustomTableResultSet.RowMap rm = ( AbstractCustomTableResultSet.RowMap)subject;
StringBuilder buf = new StringBuilder();