1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.expression; |
7 | |
8 | import java.util.ArrayList; |
9 | import org.h2.engine.Database; |
10 | import org.h2.util.New; |
11 | import org.h2.util.ValueHashMap; |
12 | import org.h2.value.Value; |
13 | import org.h2.value.ValueNull; |
14 | |
15 | /** |
16 | * Data stored while calculating a GROUP_CONCAT aggregate. |
17 | */ |
18 | class AggregateDataGroupConcat extends AggregateData { |
19 | private ArrayList<Value> list; |
20 | private ValueHashMap<AggregateDataGroupConcat> distinctValues; |
21 | |
22 | @Override |
23 | void add(Database database, int dataType, boolean distinct, Value v) { |
24 | if (v == ValueNull.INSTANCE) { |
25 | return; |
26 | } |
27 | if (distinct) { |
28 | if (distinctValues == null) { |
29 | distinctValues = ValueHashMap.newInstance(); |
30 | } |
31 | distinctValues.put(v, this); |
32 | return; |
33 | } |
34 | if (list == null) { |
35 | list = New.arrayList(); |
36 | } |
37 | list.add(v); |
38 | } |
39 | |
40 | @Override |
41 | Value getValue(Database database, int dataType, boolean distinct) { |
42 | if (distinct) { |
43 | groupDistinct(database, dataType); |
44 | } |
45 | return null; |
46 | } |
47 | |
48 | ArrayList<Value> getList() { |
49 | return list; |
50 | } |
51 | |
52 | private void groupDistinct(Database database, int dataType) { |
53 | if (distinctValues == null) { |
54 | return; |
55 | } |
56 | for (Value v : distinctValues.keys()) { |
57 | add(database, dataType, false, v); |
58 | } |
59 | } |
60 | } |