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.jdbc.impl;
22  
23  import org.apache.expreval.expr.var.NamedParameter;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.impl.HPreparedStatementImpl;
26  import org.apache.hadoop.hbase.hbql.impl.Utils;
27  import org.apache.hadoop.hbase.hbql.statement.HBqlStatement;
28  import org.apache.hadoop.hbase.hbql.statement.StatementWithParameters;
29  
30  import java.io.InputStream;
31  import java.io.Reader;
32  import java.math.BigDecimal;
33  import java.net.URL;
34  import java.sql.Array;
35  import java.sql.Blob;
36  import java.sql.Clob;
37  import java.sql.Date;
38  import java.sql.NClob;
39  import java.sql.ParameterMetaData;
40  import java.sql.PreparedStatement;
41  import java.sql.Ref;
42  import java.sql.ResultSet;
43  import java.sql.ResultSetMetaData;
44  import java.sql.RowId;
45  import java.sql.SQLException;
46  import java.sql.SQLFeatureNotSupportedException;
47  import java.sql.SQLXML;
48  import java.sql.Time;
49  import java.sql.Timestamp;
50  import java.util.Calendar;
51  
52  public class PreparedStatementImpl extends StatementImpl implements PreparedStatement {
53  
54      private final HBqlStatement statement;
55  
56      public PreparedStatementImpl(final ConnectionImpl connectionImpl, final String sql) throws HBqlException {
57          super(connectionImpl);
58  
59          this.statement = Utils.parseHBqlStatement(sql);
60  
61          if ((this.getStatement() instanceof StatementWithParameters)) {
62              final StatementWithParameters paramStmt = (StatementWithParameters)this.getStatement();
63              // Need to call this here to enable setParameters
64              paramStmt.validate(this.getHConnectionImpl());
65          }
66      }
67  
68      private HBqlStatement getStatement() {
69          return this.statement;
70      }
71  
72      public ResultSet executeQuery() throws SQLException {
73          return this.executeQuery(this.getStatement());
74      }
75  
76      public int executeUpdate() throws SQLException {
77          return this.executeUpdate(this.getStatement());
78      }
79  
80      public boolean execute() throws SQLException {
81          return this.execute(this.getStatement());
82      }
83  
84      public void close() throws SQLException {
85          this.getConnectionImpl().fireStatementClosed(this);
86      }
87  
88      public ResultSetMetaData getMetaData() throws SQLException {
89          return null;
90      }
91  
92      public ParameterMetaData getParameterMetaData() throws SQLException {
93          return null;
94      }
95  
96      private void setParameter(final int i, final Object val) throws HBqlException {
97          HPreparedStatementImpl.checkForNullParameterValue(val);
98          final StatementWithParameters paramStmt = HPreparedStatementImpl.getParameterStatement(this.getStatement());
99          final NamedParameter param = paramStmt.getNamedParameters().getParameter(i);
100         param.setParameter(val);
101     }
102 
103     public void setNull(final int i, final int i1) throws HBqlException {
104         this.setParameter(i, i1);
105     }
106 
107     public void setBoolean(final int i, final boolean b) throws HBqlException {
108         this.setParameter(i, b);
109     }
110 
111     public void setByte(final int i, final byte b) throws HBqlException {
112         this.setParameter(i, b);
113     }
114 
115     public void setShort(final int i, final short i2) throws HBqlException {
116         this.setParameter(i, i2);
117     }
118 
119     public void setInt(final int i, final int i1) throws HBqlException {
120         this.setParameter(i, i1);
121     }
122 
123     public void setLong(final int i, final long l) throws HBqlException {
124         this.setParameter(i, l);
125     }
126 
127     public void setFloat(final int i, final float v) throws HBqlException {
128         this.setParameter(i, v);
129     }
130 
131     public void setDouble(final int i, final double v) throws HBqlException {
132         this.setParameter(i, v);
133     }
134 
135     public void setBigDecimal(final int i, final BigDecimal bigDecimal) throws SQLException {
136         throw new SQLFeatureNotSupportedException();
137     }
138 
139     public void setString(final int i, final String s) throws HBqlException {
140         this.setParameter(i, s);
141     }
142 
143     public void setBytes(final int i, final byte[] bytes) throws HBqlException {
144         this.setParameter(i, bytes);
145     }
146 
147     public void setDate(final int i, final Date date) throws HBqlException {
148         this.setParameter(i, date);
149     }
150 
151     public void setTime(final int i, final Time time) throws SQLException {
152         throw new SQLFeatureNotSupportedException();
153     }
154 
155     public void setTimestamp(final int i, final Timestamp timestamp) throws SQLException {
156         throw new SQLFeatureNotSupportedException();
157     }
158 
159     public void setObject(final int i, final Object o) throws HBqlException {
160         this.setParameter(i, o);
161     }
162 
163     public void setAsciiStream(final int i, final InputStream inputStream, final int i1) throws SQLException {
164         throw new SQLFeatureNotSupportedException();
165     }
166 
167     public void setUnicodeStream(final int i, final InputStream inputStream, final int i1) throws SQLException {
168         throw new SQLFeatureNotSupportedException();
169     }
170 
171     public void setBinaryStream(final int i, final InputStream inputStream, final int i1) throws SQLException {
172         throw new SQLFeatureNotSupportedException();
173     }
174 
175     public void clearParameters() throws HBqlException {
176         if (!(this.getStatement() instanceof StatementWithParameters)) {
177             throw new HBqlException(this.getStatement().getClass().getSimpleName()
178                                     + " statements do not support parameters");
179         }
180 
181         final StatementWithParameters paramStmt = (StatementWithParameters)this.getStatement();
182         paramStmt.resetParameters();
183     }
184 
185     public void setObject(final int i, final Object o, final int i1) throws SQLException {
186         throw new SQLFeatureNotSupportedException();
187     }
188 
189     public void addBatch() throws SQLException {
190         throw new SQLFeatureNotSupportedException();
191     }
192 
193     public void setCharacterStream(final int i, final Reader reader, final int i1) throws SQLException {
194         throw new SQLFeatureNotSupportedException();
195     }
196 
197     public void setRef(final int i, final Ref ref) throws SQLException {
198         throw new SQLFeatureNotSupportedException();
199     }
200 
201     public void setBlob(final int i, final Blob blob) throws SQLException {
202         throw new SQLFeatureNotSupportedException();
203     }
204 
205     public void setClob(final int i, final Clob clob) throws SQLException {
206         throw new SQLFeatureNotSupportedException();
207     }
208 
209     public void setArray(final int i, final Array array) throws SQLException {
210 
211     }
212 
213     public void setDate(final int i, final Date date, final Calendar calendar) throws SQLException {
214         throw new SQLFeatureNotSupportedException();
215     }
216 
217     public void setTime(final int i, final Time time, final Calendar calendar) throws SQLException {
218         throw new SQLFeatureNotSupportedException();
219     }
220 
221     public void setTimestamp(final int i, final Timestamp timestamp, final Calendar calendar) throws SQLException {
222         throw new SQLFeatureNotSupportedException();
223     }
224 
225     public void setNull(final int i, final int i1, final String s) throws SQLException {
226         throw new SQLFeatureNotSupportedException();
227     }
228 
229     public void setURL(final int i, final URL url) throws SQLException {
230         throw new SQLFeatureNotSupportedException();
231     }
232 
233     public void setRowId(final int i, final RowId rowId) throws SQLException {
234         throw new SQLFeatureNotSupportedException();
235     }
236 
237     public void setNString(final int i, final String s) throws SQLException {
238         throw new SQLFeatureNotSupportedException();
239     }
240 
241     public void setNCharacterStream(final int i, final Reader reader, final long l) throws SQLException {
242         throw new SQLFeatureNotSupportedException();
243     }
244 
245     public void setNClob(final int i, final NClob nClob) throws SQLException {
246         throw new SQLFeatureNotSupportedException();
247     }
248 
249     public void setClob(final int i, final Reader reader, final long l) throws SQLException {
250         throw new SQLFeatureNotSupportedException();
251     }
252 
253     public void setBlob(final int i, final InputStream inputStream, final long l) throws SQLException {
254         throw new SQLFeatureNotSupportedException();
255     }
256 
257     public void setNClob(final int i, final Reader reader, final long l) throws SQLException {
258         throw new SQLFeatureNotSupportedException();
259     }
260 
261     public void setSQLXML(final int i, final SQLXML sqlxml) throws SQLException {
262         throw new SQLFeatureNotSupportedException();
263     }
264 
265     public void setObject(final int i, final Object o, final int i1, final int i2) throws SQLException {
266         throw new SQLFeatureNotSupportedException();
267     }
268 
269     public void setAsciiStream(final int i, final InputStream inputStream, final long l) throws SQLException {
270         throw new SQLFeatureNotSupportedException();
271     }
272 
273     public void setBinaryStream(final int i, final InputStream inputStream, final long l) throws SQLException {
274         throw new SQLFeatureNotSupportedException();
275     }
276 
277     public void setCharacterStream(final int i, final Reader reader, final long l) throws SQLException {
278         throw new SQLFeatureNotSupportedException();
279     }
280 
281     public void setAsciiStream(final int i, final InputStream inputStream) throws SQLException {
282         throw new SQLFeatureNotSupportedException();
283     }
284 
285     public void setBinaryStream(final int i, final InputStream inputStream) throws SQLException {
286         throw new SQLFeatureNotSupportedException();
287     }
288 
289     public void setCharacterStream(final int i, final Reader reader) throws SQLException {
290         throw new SQLFeatureNotSupportedException();
291     }
292 
293     public void setNCharacterStream(final int i, final Reader reader) throws SQLException {
294         throw new SQLFeatureNotSupportedException();
295     }
296 
297     public void setClob(final int i, final Reader reader) throws SQLException {
298         throw new SQLFeatureNotSupportedException();
299     }
300 
301     public void setBlob(final int i, final InputStream inputStream) throws SQLException {
302         throw new SQLFeatureNotSupportedException();
303     }
304 
305     public void setNClob(final int i, final Reader reader) throws SQLException {
306         throw new SQLFeatureNotSupportedException();
307     }
308 }