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.client.HRecord;
27  import org.apache.hadoop.hbase.hbql.client.HResultSet;
28  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
29  import org.apache.hadoop.hbase.hbql.impl.InvalidTypeException;
30  import org.apache.hadoop.hbase.hbql.impl.Query;
31  import org.apache.hadoop.hbase.hbql.statement.SelectStatement;
32  import org.apache.hadoop.hbase.hbql.statement.select.SelectElement;
33  import org.apache.hadoop.hbase.hbql.statement.select.SelectExpressionContext;
34  import org.apache.hadoop.hbase.hbql.util.Lists;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  public class SelectValuesInsertSource extends InsertValueSource {
40  
41      private final SelectStatement selectStatement;
42      private Iterator<HRecord> resultsIterator = null;
43      private HRecord           currentRecord   = null;
44  
45  
46      public SelectValuesInsertSource(final SelectStatement selectStatement) {
47          this.selectStatement = selectStatement;
48      }
49  
50      public SelectStatement getSelectStatement() {
51          return this.selectStatement;
52      }
53  
54      public List<NamedParameter> getParameterList() {
55          return this.getSelectStatement().getNamedParameters().getParameterList();
56      }
57  
58      public int setInsertSourceParameter(final String name, final Object val) throws HBqlException {
59          //HPreparedStatementImpl.checkForNullParameterValue(val);
60          return this.getSelectStatement().setStatementParameter(name, val);
61      }
62  
63      public void validate() throws HBqlException {
64  
65          for (final SelectElement element : this.getSelectStatement().getSelectElementList()) {
66              if (element.isAFamilySelect())
67                  throw new InvalidTypeException("Family select items are not valid in INSERT statement");
68          }
69  
70          this.getSelectStatement().validate(this.getInsertStatement().getConnection());
71      }
72  
73      private Iterator<HRecord> getResultsIterator() {
74          return this.resultsIterator;
75      }
76  
77      private void setResultsIterator(final Iterator<HRecord> resultsIterator) {
78          this.resultsIterator = resultsIterator;
79      }
80  
81      private HRecord getCurrentRecord() {
82          return this.currentRecord;
83      }
84  
85      private void setCurrentRecord(final HRecord currentRecord) {
86          this.currentRecord = currentRecord;
87      }
88  
89      public void execute() throws HBqlException {
90          final Query<HRecord> query = Query.newQuery(this.getInsertStatement().getConnection(),
91                                                      this.getSelectStatement(),
92                                                      HRecord.class);
93          final HResultSet<HRecord> results = query.newResultSet(false);
94          this.setResultsIterator(results.iterator());
95      }
96  
97      public List<Class<? extends GenericValue>> getValuesTypeList() throws HBqlException {
98          final List<Class<? extends GenericValue>> typeList = Lists.newArrayList();
99          for (final SelectElement element : this.getSelectStatement().getSelectElementList()) {
100             if (element instanceof SelectExpressionContext) {
101                 final SelectExpressionContext expr = (SelectExpressionContext)element;
102                 final Class<? extends GenericValue> type = expr.getExpressionType();
103                 typeList.add(type);
104             }
105         }
106         return typeList;
107     }
108 
109     public void reset() {
110         this.getSelectStatement().resetParameters();
111     }
112 
113     public String asString() {
114         return this.getSelectStatement().asString();
115     }
116 
117     public Object getValue(final HConnectionImpl conn, final int i) throws HBqlException {
118         final SelectElement element = this.getSelectStatement().getSelectElementList().get(i);
119         final String name = element.getElementName();
120         return this.getCurrentRecord().getCurrentValue(name);
121     }
122 
123     public boolean isDefaultValue(final int i) throws HBqlException {
124         return false;
125     }
126 
127     public boolean hasValues() {
128         if (this.getResultsIterator().hasNext()) {
129             this.setCurrentRecord(this.getResultsIterator().next());
130             return true;
131         }
132         else {
133             return false;
134         }
135     }
136 }