View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.expreval.expr.var;
22  
23  import org.apache.expreval.client.InternalErrorException;
24  import org.apache.expreval.client.NullColumnValueException;
25  import org.apache.expreval.client.ResultMissingColumnException;
26  import org.apache.expreval.expr.MultipleExpressionContext;
27  import org.apache.expreval.expr.node.GenericValue;
28  import org.apache.hadoop.hbase.hbql.client.HBqlException;
29  import org.apache.hadoop.hbase.hbql.impl.ColumnNotAllowedException;
30  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
31  import org.apache.hadoop.hbase.hbql.impl.InvalidColumnException;
32  import org.apache.hadoop.hbase.hbql.mapping.ColumnAttrib;
33  
34  public class DelegateColumn extends GenericColumn<GenericValue> {
35  
36      private GenericColumn<? extends GenericValue> typedColumn = null;
37      private final String variableName;
38      private boolean variableDefinedInMapping = false;
39  
40      public DelegateColumn(final String variableName) {
41          super(null);
42          this.variableName = variableName;
43      }
44  
45      public GenericColumn<? extends GenericValue> getTypedColumn() {
46          return this.typedColumn;
47      }
48  
49      private void setTypedColumn(final GenericColumn<? extends GenericValue> typedColumn) {
50          this.typedColumn = typedColumn;
51      }
52  
53      public String getVariableName() {
54          return this.variableName;
55      }
56  
57      public Object getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
58                                                                                     ResultMissingColumnException,
59                                                                                     NullColumnValueException {
60  
61          if (!this.isVariableDefinedInMapping())
62              throw new InvalidColumnException(this.getVariableName());
63  
64          return this.getTypedColumn().getValue(conn, object);
65      }
66  
67      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
68                                                         final boolean allowCollections) throws HBqlException {
69          if (!this.isVariableDefinedInMapping())
70              throw new InvalidColumnException(this.getVariableName());
71  
72          return this.getTypedColumn().validateTypes(parentExpr, allowCollections);
73      }
74  
75      private boolean isVariableDefinedInMapping() {
76          return this.variableDefinedInMapping;
77      }
78  
79      public void setExpressionContext(final MultipleExpressionContext context) throws HBqlException {
80  
81          if (!context.allowColumns())
82              throw new ColumnNotAllowedException("Column " + this.getVariableName()
83                                                  + " not allowed in: " + context.asString());
84  
85          if (context.getMapping() == null)
86              throw new InternalErrorException("Null mapping for: " + context.asString());
87  
88          // See if referenced var is in mapping
89          final String variableName = this.getVariableName();
90          final ColumnAttrib attrib = context.getResultAccessor().getColumnAttribByName(variableName);
91  
92          this.variableDefinedInMapping = (attrib != null);
93  
94          if (this.isVariableDefinedInMapping()) {
95  
96              switch (attrib.getFieldType()) {
97  
98                  case KeyType:
99                      this.setTypedColumn(new KeyColumn(attrib));
100                     break;
101 
102                 case StringType:
103                     this.setTypedColumn(new StringColumn(attrib));
104                     break;
105 
106                 case BooleanType:
107                     this.setTypedColumn(new BooleanColumn(attrib));
108                     break;
109 
110                 case ByteType:
111                     this.setTypedColumn(new ByteColumn(attrib));
112                     break;
113 
114                 case CharType:
115                     this.setTypedColumn(new CharColumn(attrib));
116                     break;
117 
118                 case ShortType:
119                     this.setTypedColumn(new ShortColumn(attrib));
120                     break;
121 
122                 case IntegerType:
123                     this.setTypedColumn(new IntegerColumn(attrib));
124                     break;
125 
126                 case LongType:
127                     this.setTypedColumn(new LongColumn(attrib));
128                     break;
129 
130                 case FloatType:
131                     this.setTypedColumn(new FloatColumn(attrib));
132                     break;
133 
134                 case DoubleType:
135                     this.setTypedColumn(new DoubleColumn(attrib));
136                     break;
137 
138                 case DateType:
139                     this.setTypedColumn(new DateColumn(attrib));
140                     break;
141 
142                 case ObjectType:
143                     this.setTypedColumn(new ObjectColumn(attrib));
144                     break;
145 
146                 default:
147                     throw new HBqlException("Invalid type: " + attrib.getFieldType().name());
148             }
149 
150             this.getTypedColumn().setExpressionContext(context);
151         }
152     }
153 }