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.impl;
22
23 import org.apache.expreval.expr.var.NamedParameter;
24 import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
25 import org.apache.hadoop.hbase.hbql.client.HBqlException;
26 import org.apache.hadoop.hbase.hbql.client.HPreparedStatement;
27 import org.apache.hadoop.hbase.hbql.client.HRecord;
28 import org.apache.hadoop.hbase.hbql.client.HResultSet;
29 import org.apache.hadoop.hbase.hbql.statement.HBqlStatement;
30 import org.apache.hadoop.hbase.hbql.statement.StatementWithParameters;
31
32 import java.util.List;
33
34 public class HPreparedStatementImpl extends HStatementImpl implements HPreparedStatement {
35
36 private final String sql;
37 private final HBqlStatement statement;
38
39 public HPreparedStatementImpl(final HConnectionImpl hbqlConnection, final String sql) throws HBqlException {
40 super(hbqlConnection);
41 this.sql = sql;
42
43 this.statement = Utils.parseHBqlStatement(sql);
44
45 if ((this.getStatement() instanceof StatementWithParameters)) {
46 final StatementWithParameters paramStmt = (StatementWithParameters)this.getStatement();
47
48
49 paramStmt.validate(this.getHConnectionImpl());
50 }
51 }
52
53 public String getSql() {
54 return this.sql;
55 }
56
57 private HBqlStatement getStatement() {
58 return this.statement;
59 }
60
61 public HResultSet<HRecord> executeQuery() throws HBqlException {
62 return this.executeQuery(this.getStatement(), HRecord.class);
63 }
64
65 public <T> HResultSet<T> executeQuery(final Class clazz) throws HBqlException {
66 return this.executeQuery(this.getStatement(), clazz);
67 }
68
69 public List<HRecord> executeQueryAndFetch() throws HBqlException {
70 return this.executeQueryAndFetch(this.getStatement(), HRecord.class);
71 }
72
73 public <T> List<T> executeQueryAndFetch(final Class clazz) throws HBqlException {
74 return this.executeQueryAndFetch(this.getStatement(), clazz);
75 }
76
77 public ExecutionResults executeUpdate() throws HBqlException {
78 return this.executeUpdate(this.getStatement());
79 }
80
81 public ExecutionResults execute() throws HBqlException {
82 return this.execute(this.getStatement());
83 }
84
85 public int setParameter(final String name, final Object val) throws HBqlException {
86 checkForNullParameterValue(val);
87 final StatementWithParameters paramStmt = getParameterStatement(this.getStatement());
88 return paramStmt.setStatementParameter(name, val);
89 }
90
91 public void setParameter(final int i, final Object val) throws HBqlException {
92 checkForNullParameterValue(val);
93 final StatementWithParameters paramStmt = getParameterStatement(this.getStatement());
94 final NamedParameter param = paramStmt.getNamedParameters().getParameter(i);
95 param.setParameter(val);
96 }
97
98 public void resetParameters() throws HBqlException {
99 final StatementWithParameters paramStmt = getParameterStatement(this.getStatement());
100 paramStmt.resetParameters();
101 }
102
103 public static void checkForNullParameterValue(final Object val) throws HBqlException {
104
105
106 }
107
108 public static StatementWithParameters getParameterStatement(final HBqlStatement statement) throws HBqlException {
109
110 if (!(statement instanceof StatementWithParameters))
111 throw new HBqlException(statement.getClass().getSimpleName() + " statements do not support parameters");
112
113 return (StatementWithParameters)statement;
114 }
115 }