View Javadoc

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;
25  
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.LinkedList;
29  import java.util.List;
30  import javax.jcr.ItemNotFoundException;
31  import javax.jcr.Node;
32  import javax.jcr.PropertyType;
33  import javax.jcr.RepositoryException;
34  import javax.jcr.Value;
35  import javax.jcr.query.Row;
36  import javax.jcr.query.RowIterator;
37  import org.modeshape.graph.Location;
38  import org.modeshape.graph.property.Path;
39  import org.modeshape.graph.query.QueryResults;
40  import org.modeshape.graph.query.validate.Schemata;
41  
42  /**
43   * 
44   */
45  public class JcrSqlQueryResult extends JcrQueryResult {
46  
47      public static final String JCR_SCORE_COLUMN_NAME = "jcr:score";
48      public static final String JCR_PATH_COLUMN_NAME = "jcr:path";
49      /* The TypeFactory.getTypeName() always returns an uppercased type */
50      public static final String JCR_SCORE_COLUMN_TYPE = PropertyType.nameFromValue(PropertyType.DOUBLE).toUpperCase();
51      public static final String JCR_PATH_COLUMN_TYPE = PropertyType.nameFromValue(PropertyType.STRING).toUpperCase();
52  
53      private final List<String> columnNames;
54      private final List<String> columnTypes;
55  
56      public JcrSqlQueryResult( JcrQueryContext context,
57                                String query,
58                                QueryResults graphResults,
59                                Schemata schemata ) {
60          super(context, query, graphResults, schemata);
61          List<String> columnNames = new LinkedList<String>(graphResults.getColumns().getColumnNames());
62          List<String> columnTypes = new LinkedList<String>(graphResults.getColumns().getColumnTypes());
63          if (!columnNames.contains(JCR_SCORE_COLUMN_NAME)) {
64              columnNames.add(0, JCR_SCORE_COLUMN_NAME);
65              columnTypes.add(0, JCR_SCORE_COLUMN_TYPE);
66          }
67          if (!columnNames.contains(JCR_PATH_COLUMN_NAME)) {
68              columnNames.add(0, JCR_PATH_COLUMN_NAME);
69              columnTypes.add(0, JCR_PATH_COLUMN_TYPE);
70          }
71          this.columnNames = Collections.unmodifiableList(columnNames);
72          this.columnTypes = Collections.unmodifiableList(columnTypes);
73      }
74  
75      /**
76       * {@inheritDoc}
77       * 
78       * @see JcrQueryResult#getColumnNameList()
79       */
80      @Override
81      public List<String> getColumnNameList() {
82          return columnNames;
83      }
84  
85      /**
86       * {@inheritDoc}
87       * 
88       * @see org.modeshape.jcr.query.JcrQueryResult#getColumnTypeList()
89       */
90      @Override
91      public java.util.List<String> getColumnTypeList() {
92          return columnTypes;
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see JcrQueryResult#getRows()
99       */
100     @Override
101     public RowIterator getRows() {
102         final int numRows = results.getRowCount();
103         final List<Object[]> tuples = results.getTuples();
104         return new JcrSqlQueryResultRowIterator(context, queryStatement, results, tuples.iterator(), numRows);
105     }
106 
107     protected static class JcrSqlQueryResultRowIterator extends SingleSelectorQueryResultRowIterator {
108 
109         protected JcrSqlQueryResultRowIterator( JcrQueryContext context,
110                                                 String query,
111                                                 QueryResults results,
112                                                 Iterator<Object[]> tuples,
113                                                 long numRows ) {
114             super(context, query, results, tuples, numRows);
115         }
116 
117         @Override
118         protected Row createRow( Node node,
119                                  Object[] tuple ) {
120             return new JcrSqlQueryResultRow(this, node, tuple);
121         }
122 
123         protected Value jcrPath( Path path ) {
124             return context.createValue(PropertyType.PATH, path);
125         }
126 
127         protected Value jcrScore( Float score ) {
128             return context.createValue(PropertyType.DOUBLE, score);
129         }
130     }
131 
132     protected static class JcrSqlQueryResultRow extends SingleSelectorQueryResultRow {
133         protected JcrSqlQueryResultRow( SingleSelectorQueryResultRowIterator iterator,
134                                         Node node,
135                                         Object[] tuple ) {
136             super(iterator, node, tuple);
137         }
138 
139         /**
140          * {@inheritDoc}
141          * 
142          * @see javax.jcr.query.Row#getValue(java.lang.String)
143          */
144         @Override
145         public Value getValue( String columnName ) throws ItemNotFoundException, RepositoryException {
146             if (JCR_PATH_COLUMN_NAME.equals(columnName)) {
147                 Location location = (Location)tuple[iterator.locationIndex];
148                 return ((JcrSqlQueryResultRowIterator)iterator).jcrPath(location.getPath());
149             }
150             if (JCR_SCORE_COLUMN_NAME.equals(columnName)) {
151                 Float score = (Float)tuple[iterator.scoreIndex];
152                 return ((JcrSqlQueryResultRowIterator)iterator).jcrScore(score);
153             }
154             return super.getValue(columnName);
155         }
156     }
157 }