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.graph.query.parse;
25  
26  import net.jcip.annotations.Immutable;
27  import org.modeshape.common.text.Position;
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.ObjectUtil;
30  import org.modeshape.graph.query.model.SelectorName;
31  
32  /**
33   * A representation of a column as expressed in the SQL query. Note that the selector name may not have been explicitly specified.
34   */
35  @Immutable
36  class ColumnExpression {
37  
38      private final SelectorName selectorName;
39      private final String propertyName;
40      private final String columnName;
41      private final Position position;
42  
43      /**
44       * A column with the given name representing the named property on the node identified by the selector.
45       * 
46       * @param selectorName the selector name; may be null if no selector was explicitly used in the query
47       * @param propertyName the name of the property
48       * @param columnName the name of the column
49       * @param position the position of the column in the query
50       */
51      ColumnExpression( SelectorName selectorName,
52                        String propertyName,
53                        String columnName,
54                        Position position ) {
55          CheckArg.isNotNull(propertyName, "propertyName");
56          CheckArg.isNotNull(columnName, "columnName");
57          CheckArg.isNotNull(position, "position");
58          this.selectorName = selectorName;
59          this.propertyName = propertyName;
60          this.columnName = columnName;
61          this.position = position;
62      }
63  
64      /**
65       * Get the column's position in the query.
66       * 
67       * @return the column's position; never null
68       */
69      public Position getPosition() {
70          return position;
71      }
72  
73      /**
74       * @return the name of the selector; may be null if no selector was explicitly used in the query
75       */
76      public final SelectorName getSelectorName() {
77          return selectorName;
78      }
79  
80      /**
81       * @return propertyName
82       */
83      public final String getPropertyName() {
84          return propertyName;
85      }
86  
87      /**
88       * @return columnName
89       */
90      public final String getColumnName() {
91          return columnName;
92      }
93  
94      /**
95       * {@inheritDoc}
96       * 
97       * @see java.lang.Object#equals(java.lang.Object)
98       */
99      @Override
100     public boolean equals( Object obj ) {
101         if (obj == this) return true;
102         if (obj instanceof ColumnExpression) {
103             ColumnExpression that = (ColumnExpression)obj;
104             if (!ObjectUtil.isEqualWithNulls(this.selectorName, that.selectorName)) return false;
105             if (!ObjectUtil.isEqualWithNulls(this.propertyName, that.propertyName)) return false;
106             if (!ObjectUtil.isEqualWithNulls(this.columnName, that.columnName)) return false;
107             return true;
108         }
109         return false;
110     }
111 
112     /**
113      * {@inheritDoc}
114      * 
115      * @see java.lang.Object#toString()
116      */
117     @Override
118     public String toString() {
119         StringBuilder sb = new StringBuilder();
120         if (selectorName != null) {
121             sb.append(selectorName.name());
122             sb.append('.');
123         }
124         sb.append(propertyName);
125         if (columnName != null) {
126             sb.append(" AS ").append(columnName);
127         }
128         return sb.toString();
129     }
130 }