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.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              // Need to call this here to enable setParameters
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         //if (val == null)
105         //    throw new HBqlException("Parameter value cannot be NULL");
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 }