View Javadoc

1   /*
2    * Copyright (c) 2011.  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.hadoop.hbase.hbql.statement.args;
22  
23  import org.apache.expreval.expr.node.GenericValue;
24  import org.apache.expreval.expr.var.NamedParameter;
25  import org.apache.hadoop.hbase.hbql.client.HBqlException;
26  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
27  import org.apache.hadoop.hbase.hbql.statement.select.SelectExpressionContext;
28  import org.apache.hadoop.hbase.hbql.util.Lists;
29  
30  import java.util.List;
31  
32  public class SingleRowInsertSource extends InsertValueSource {
33  
34      private final List<SelectExpressionContext> valueList       = Lists.newArrayList();
35      private       boolean                       calledForValues = false;
36  
37      public SingleRowInsertSource(final List<GenericValue> valueList) {
38          for (final GenericValue val : valueList)
39              this.getValueList().add(SelectExpressionContext.newExpression(val, null));
40      }
41  
42      private List<SelectExpressionContext> getValueList() {
43          return this.valueList;
44      }
45  
46      public List<NamedParameter> getParameterList() {
47  
48          final List<NamedParameter> parameterList = Lists.newArrayList();
49  
50          for (final SelectExpressionContext expr : this.getValueList())
51              parameterList.addAll(expr.getParameterList());
52  
53          return parameterList;
54      }
55  
56      public int setInsertSourceParameter(final String name, final Object val) throws HBqlException {
57  
58          int cnt = 0;
59  
60          for (final SelectExpressionContext expr : this.getValueList())
61              cnt += expr.setParameter(name, val);
62  
63          return cnt;
64      }
65  
66      public void validate() throws HBqlException {
67  
68          final HConnectionImpl conn = this.getInsertStatement().getConnection();
69  
70          for (final SelectExpressionContext element : this.getValueList()) {
71              element.validate(this.getInsertStatement().getMappingContext(), conn);
72  
73              // Make sure values do not have column references
74              if (element.hasAColumnReference())
75                  throw new HBqlException("Column reference " + element.asString() + " not valid in " + this.asString());
76          }
77      }
78  
79      public void execute() {
80          // No op
81      }
82  
83      public void reset() {
84          this.calledForValues = false;
85          for (final SelectExpressionContext expr : this.getValueList())
86              expr.reset();
87      }
88  
89      public String asString() {
90  
91          final StringBuilder sbuf = new StringBuilder();
92  
93          sbuf.append("VALUES (");
94  
95          boolean firstTime = true;
96          for (final SelectExpressionContext val : this.getValueList()) {
97              if (!firstTime)
98                  sbuf.append(", ");
99              firstTime = false;
100 
101             sbuf.append(val.asString());
102         }
103 
104         sbuf.append(")");
105 
106         return sbuf.toString();
107     }
108 
109     public boolean isDefaultValue(final int i) throws HBqlException {
110         return this.getValueList().get(i).isDefaultKeyword();
111     }
112 
113     public Object getValue(final HConnectionImpl conn, final int i) throws HBqlException {
114         return this.getValueList().get(i).evaluateConstant(0, false);
115     }
116 
117     public List<Class<? extends GenericValue>> getValuesTypeList() throws HBqlException {
118         final List<Class<? extends GenericValue>> typeList = Lists.newArrayList();
119         for (final SelectExpressionContext element : this.getValueList()) {
120             final Class<? extends GenericValue> type = element.getExpressionType();
121             typeList.add(type);
122         }
123         return typeList;
124     }
125 
126     public boolean hasValues() {
127         this.calledForValues = !this.calledForValues;
128         return this.calledForValues;
129     }
130 }