1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.internal

File AbstractQueryFilter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
18
2
1
120
43
4
0.22
9
2
2

Classes

Class Line # Actions
AbstractQueryFilter 34 18 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.query.internal;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.apache.commons.collections.ListUtils;
26    import org.xwiki.query.QueryFilter;
27   
28    /**
29    * Abstract {@link QueryFilter} providing methods to parse Query statements.
30    *
31    * @version $Id: b45d560e90867a9dfa2b81779e50041dd5766c15 $
32    * @since 4.1M1
33    */
 
34    public abstract class AbstractQueryFilter implements QueryFilter
35    {
36    /**
37    * Usual identifier of the document full name column in our queries.
38    */
39    protected static final String FULLNAME_COLUMN = "doc.fullName";
40   
41    /**
42    * Distinct document full name column in our queries.
43    */
44    protected static final String DISTINCT_FULLNAME_COLUMN = "distinct doc.fullName";
45   
46    /**
47    * Character used to separate columns in select, order by and group by clauses.
48    */
49    protected static final String COLUMN_SEPARATOR = ",";
50   
51    /**
52    * Select clause keyword.
53    */
54    private static final String SELECT = "select ";
55   
56    /**
57    * From clause keyword.
58    */
59    private static final String FROM = " from ";
60   
61    /**
62    * Order by clause keyword.
63    */
64    private static final String ORDER_BY = " order by ";
65   
66    /**
67    * Group by clause keyword.
68    */
69    private static final String GROUP_BY = " group by ";
70   
71    /**
72    * Get the select columns of a given statement.
73    *
74    * @param statement the statement to get the select clause for.
75    * @return the select clause of the given statement.
76    */
 
77  1096 toggle protected List<String> getSelectColumns(String statement)
78    {
79  1096 List<String> columns = new ArrayList<String>();
80  1096 String select = statement.substring(SELECT.length(), statement.indexOf(FROM));
81  1096 for (String column : select.split(COLUMN_SEPARATOR)) {
82  1127 columns.add(column.trim());
83    }
84   
85  1096 return columns;
86    }
87   
88    /**
89    * Get the list of columns present in the order by clause. This method is required because HSQLDB only
90    * support SELECT DISTINCT SQL statements where the columns present in the order by clause are also present in the
91    * select clause.
92    *
93    * @param statement the statement to evaluate.
94    * @return the list of columns to return in the select clause as a string starting with ", " if there are columns or
95    * an empty string otherwise. The returned columns are extracted from the order by clause.
96    */
 
97  266 toggle protected List<String> getOrderByColumns(String statement)
98    {
99  266 List<String> columns = new ArrayList<String>();
100  266 int oidx = statement.indexOf(ORDER_BY);
101   
102  266 if (oidx > -1) {
103  209 String fragment = statement.substring(oidx + ORDER_BY.length());
104  209 int gidx = fragment.indexOf(GROUP_BY);
105  209 if (gidx > -1) {
106  2 fragment = fragment.substring(0, gidx);
107    }
108  209 fragment = fragment.replaceAll(" desc", "");
109  209 fragment = fragment.replaceAll(" asc", "");
110   
111  209 for (String column : fragment.split(COLUMN_SEPARATOR)) {
112  214 columns.add(column.trim());
113    }
114   
115  209 return columns;
116    }
117   
118  57 return ListUtils.EMPTY_LIST;
119    }
120    }