1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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 }