1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.jcr.query.qom;
25
26 import java.util.List;
27 import javax.jcr.query.qom.Column;
28 import javax.jcr.query.qom.Ordering;
29 import org.modeshape.graph.query.model.SetQuery;
30 import org.modeshape.jcr.api.query.qom.Limit;
31 import org.modeshape.jcr.api.query.qom.QueryObjectModelConstants;
32
33 /**
34 *
35 */
36 public class JcrSetQuery extends SetQuery implements org.modeshape.jcr.api.query.qom.SetQuery, JcrQueryCommand {
37
38 private static final long serialVersionUID = 1L;
39
40 /**
41 * Create a set query involving the supplied left- and right-hand-side queries.
42 *
43 * @param left the left-hand-side query being combined
44 * @param operation the set operation
45 * @param right the right-hand-side query being combined
46 * @param all true if all of the results should be included
47 * @throws IllegalArgumentException if the left-hand-side query, right-hand-side query, or operation are null
48 */
49 public JcrSetQuery( JcrQueryCommand left,
50 Operation operation,
51 JcrQueryCommand right,
52 boolean all ) {
53 super(left, operation, right, all);
54 }
55
56 /**
57 * Create a set query involving the supplied left- and right-hand-side queries.
58 *
59 * @param left the left-hand-side query being combined
60 * @param operation the set operation
61 * @param right the right-hand-side query being combined
62 * @param all true if all of the results should be included
63 * @param orderings the specification of the order of the result rows, or null if the results need not be ordered
64 * @param limit the limit for the result rows, or null if there are no limits
65 * @throws IllegalArgumentException if the left-hand-side query, right-hand-side query, or operation are null
66 */
67 public JcrSetQuery( JcrQueryCommand left,
68 Operation operation,
69 JcrQueryCommand right,
70 boolean all,
71 List<? extends JcrOrdering> orderings,
72 JcrLimit limit ) {
73 super(left, operation, right, all, orderings, limit != null ? limit : JcrLimit.NONE);
74 }
75
76 /**
77 * {@inheritDoc}
78 *
79 * @see org.modeshape.graph.query.model.SetQuery#left()
80 */
81 @Override
82 public JcrQueryCommand left() {
83 return (JcrQueryCommand)super.left();
84 }
85
86 /**
87 * {@inheritDoc}
88 *
89 * @see org.modeshape.graph.query.model.Query#columns()
90 */
91 @SuppressWarnings( "unchecked" )
92 @Override
93 public List<? extends JcrColumn> columns() {
94 return (List<? extends JcrColumn>)super.columns();
95 }
96
97 /**
98 * {@inheritDoc}
99 *
100 * @see org.modeshape.graph.query.model.Query#orderings()
101 */
102 @SuppressWarnings( "unchecked" )
103 @Override
104 public List<? extends JcrOrdering> orderings() {
105 return (List<? extends JcrOrdering>)super.orderings();
106 }
107
108 /**
109 * {@inheritDoc}
110 *
111 * @see org.modeshape.graph.query.model.SetQuery#right()
112 */
113 @Override
114 public JcrQueryCommand right() {
115 return (JcrQueryCommand)super.right();
116 }
117
118 /**
119 * {@inheritDoc}
120 *
121 * @see org.modeshape.graph.query.model.SetQuery#limits()
122 */
123 @Override
124 public JcrLimit limits() {
125 return (JcrLimit)super.limits();
126 }
127
128 /**
129 * {@inheritDoc}
130 *
131 * @see org.modeshape.jcr.api.query.qom.QueryCommand#getLimits()
132 */
133 @Override
134 public Limit getLimits() {
135 return limits();
136 }
137
138 /**
139 * {@inheritDoc}
140 *
141 * @see org.modeshape.jcr.api.query.qom.SetQuery#getLeft()
142 */
143 @Override
144 public JcrQueryCommand getLeft() {
145 return left();
146 }
147
148 /**
149 * {@inheritDoc}
150 *
151 * @see org.modeshape.jcr.api.query.qom.SetQuery#getRight()
152 */
153 @Override
154 public JcrQueryCommand getRight() {
155 return right();
156 }
157
158 /**
159 * {@inheritDoc}
160 *
161 * @see org.modeshape.jcr.api.query.qom.SetQuery#getOperation()
162 */
163 @Override
164 public String getOperation() {
165 switch (operation()) {
166 case UNION:
167 return QueryObjectModelConstants.JCR_SET_TYPE_UNION;
168 case INTERSECT:
169 return QueryObjectModelConstants.JCR_SET_TYPE_INTERSECT;
170 case EXCEPT:
171 return QueryObjectModelConstants.JCR_SET_TYPE_EXCEPT;
172 }
173 assert false;
174 return null;
175 }
176
177 /**
178 * {@inheritDoc}
179 *
180 * @see org.modeshape.jcr.api.query.qom.QueryCommand#getColumns()
181 */
182 @Override
183 public Column[] getColumns() {
184 List<? extends JcrColumn> columns = columns();
185 return columns.toArray(new Column[columns.size()]);
186 }
187
188 /**
189 * {@inheritDoc}
190 *
191 * @see org.modeshape.jcr.api.query.qom.QueryCommand#getOrderings()
192 */
193 @Override
194 public Ordering[] getOrderings() {
195 List<? extends JcrOrdering> orderings = orderings();
196 return orderings.toArray(new Ordering[orderings.size()]);
197 }
198
199 /**
200 * {@inheritDoc}
201 *
202 * @see org.modeshape.graph.query.model.SetQuery#withLimit(int)
203 */
204 @Override
205 public JcrSetQuery withLimit( int rowLimit ) {
206 if (limits().rowLimit() == rowLimit) return this; // nothing to change
207 return new JcrSetQuery(left(), operation(), right(), isAll(), orderings(), limits().withRowLimit(rowLimit));
208 }
209
210 /**
211 * {@inheritDoc}
212 *
213 * @see org.modeshape.graph.query.model.SetQuery#withOffset(int)
214 */
215 @Override
216 public JcrSetQuery withOffset( int offset ) {
217 if (limits().offset() == offset) return this; // nothing to change
218 return new JcrSetQuery(left(), operation(), right(), isAll(), orderings(), limits().withOffset(offset));
219 }
220 }