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.client.InternalErrorException;
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.HConnection;
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.impl.HConnectionImpl;
30  import org.apache.hadoop.hbase.hbql.impl.Query;
31  import org.apache.hadoop.hbase.hbql.impl.Utils;
32  import org.apache.hadoop.hbase.hbql.statement.ConnectionStatement;
33  import org.apache.hadoop.hbase.hbql.statement.HBqlStatement;
34  import org.apache.hadoop.hbase.hbql.statement.NonConnectionStatement;
35  import org.apache.hadoop.hbase.hbql.statement.SelectStatement;
36  
37  import java.sql.Connection;
38  import java.sql.ResultSet;
39  import java.sql.SQLException;
40  import java.sql.SQLFeatureNotSupportedException;
41  import java.sql.SQLWarning;
42  import java.sql.Statement;
43  
44  public class StatementImpl implements Statement {
45  
46      private final ConnectionImpl connectionImpl;
47  
48      private ResultSet resultSet = null;
49  
50      public StatementImpl(final ConnectionImpl connectionImpl) {
51          this.connectionImpl = connectionImpl;
52      }
53  
54      protected ConnectionImpl getConnectionImpl() {
55          return this.connectionImpl;
56      }
57  
58      protected HConnectionImpl getHConnectionImpl() {
59          return (HConnectionImpl)this.getConnectionImpl().getHConnection();
60      }
61  
62      public HConnection getHConnection() {
63          return this.getHConnectionImpl();
64      }
65  
66      public Connection getConnection() {
67          return this.getConnectionImpl();
68      }
69  
70      public int executeUpdate(final HBqlStatement statement) throws SQLException {
71  
72          if (Utils.isSelectStatement(statement)) {
73              throw new HBqlException("executeUpdate() requires a non-SELECT statement");
74          }
75          else if (Utils.isDMLStatement(statement)) {
76              final ConnectionStatement stmt = ((ConnectionStatement)statement);
77              final ExecutionResults results = stmt.evaluatePredicateAndExecute(this.getHConnectionImpl());
78              return results.getCount();
79          }
80          else if (Utils.isConnectionStatemet(statement)) {
81              final ConnectionStatement stmt = ((ConnectionStatement)statement);
82              stmt.evaluatePredicateAndExecute(this.getHConnectionImpl());
83              return 0;
84          }
85          else if (Utils.isNonConectionStatemet(statement)) {
86              final NonConnectionStatement stmt = ((NonConnectionStatement)statement);
87              stmt.execute();
88              return 0;
89          }
90          else {
91              throw new InternalErrorException("Bad state with " + statement.getClass().getSimpleName());
92          }
93      }
94  
95      protected ResultSet executeQuery(final HBqlStatement stmt) throws SQLException {
96  
97          if (!Utils.isSelectStatement(stmt))
98              throw new HBqlException("executeQuery() requires a SELECT statement");
99  
100         final Query<HRecord> query = Query.newQuery(this.getHConnectionImpl(), (SelectStatement)stmt, HRecord.class);
101         final HResultSet<HRecord> hResultSet = query.newResultSet(false);
102         final ResultSet resultSet = new ResultSetImpl(this, query, hResultSet);
103         this.setResultSet(resultSet);
104         return this.getResultSet();
105     }
106 
107     protected boolean execute(final HBqlStatement statement) throws SQLException {
108         if (Utils.isSelectStatement(statement)) {
109             this.executeQuery(statement);
110             return true;
111         }
112         else {
113             this.executeUpdate(statement);
114             return false;
115         }
116     }
117 
118     public boolean execute(final String sql) throws SQLException {
119         return this.execute(Utils.parseHBqlStatement(sql));
120     }
121 
122     public ResultSet executeQuery(final String sql) throws SQLException {
123         return this.executeQuery(Utils.parseHBqlStatement(sql));
124     }
125 
126     public int executeUpdate(final String sql) throws SQLException {
127         return this.executeUpdate(Utils.parseHBqlStatement(sql));
128     }
129 
130     public void close() throws SQLException {
131     }
132 
133     public int getMaxFieldSize() throws SQLException {
134         return 0;
135     }
136 
137     public void setMaxFieldSize(final int i) throws SQLException {
138 
139     }
140 
141     public int getMaxRows() throws SQLException {
142         return 0;
143     }
144 
145     public void setMaxRows(final int i) throws SQLException {
146 
147     }
148 
149     public void setEscapeProcessing(final boolean b) throws SQLException {
150 
151     }
152 
153     public int getQueryTimeout() throws SQLException {
154         return 0;
155     }
156 
157     public void setQueryTimeout(final int i) throws SQLException {
158 
159     }
160 
161     public void cancel() throws SQLException {
162 
163     }
164 
165     public SQLWarning getWarnings() throws SQLException {
166         return null;
167     }
168 
169     public void clearWarnings() throws SQLException {
170 
171     }
172 
173     public void setCursorName(final String s) throws SQLException {
174 
175     }
176 
177     protected void setResultSet(final ResultSet resultSet) {
178         this.resultSet = resultSet;
179     }
180 
181     public ResultSet getResultSet() {
182         return this.resultSet;
183     }
184 
185     public int getUpdateCount() throws SQLException {
186         return 0;
187     }
188 
189     public boolean getMoreResults() throws SQLException {
190         return false;
191     }
192 
193     public void setFetchDirection(final int i) throws SQLException {
194 
195     }
196 
197     public int getFetchDirection() throws SQLException {
198         return 0;
199     }
200 
201     public void setFetchSize(final int i) throws SQLException {
202 
203     }
204 
205     public int getFetchSize() throws SQLException {
206         return 0;
207     }
208 
209     public int getResultSetConcurrency() throws SQLException {
210         return 0;
211     }
212 
213     public int getResultSetType() throws SQLException {
214         return 0;
215     }
216 
217     public void addBatch(final String s) throws SQLException {
218 
219     }
220 
221     public void clearBatch() throws SQLException {
222 
223     }
224 
225     public int[] executeBatch() throws SQLException {
226         return new int[0];
227     }
228 
229     public boolean getMoreResults(final int i) throws SQLException {
230         return false;
231     }
232 
233     public ResultSet getGeneratedKeys() throws SQLException {
234         return null;
235     }
236 
237     public int executeUpdate(final String s, final int i) throws SQLException {
238         throw new SQLFeatureNotSupportedException();
239     }
240 
241     public int executeUpdate(final String s, final int[] ints) throws SQLException {
242         throw new SQLFeatureNotSupportedException();
243     }
244 
245     public int executeUpdate(final String s, final String[] strings) throws SQLException {
246         throw new SQLFeatureNotSupportedException();
247     }
248 
249     public boolean execute(final String s, final int i) throws SQLException {
250         throw new SQLFeatureNotSupportedException();
251     }
252 
253     public boolean execute(final String s, final int[] ints) throws SQLException {
254         throw new SQLFeatureNotSupportedException();
255     }
256 
257     public boolean execute(final String s, final String[] strings) throws SQLException {
258         throw new SQLFeatureNotSupportedException();
259     }
260 
261     public int getResultSetHoldability() throws SQLException {
262         throw new SQLFeatureNotSupportedException();
263     }
264 
265     public boolean isClosed() throws SQLException {
266         return false;
267     }
268 
269     public void setPoolable(final boolean b) throws SQLException {
270         throw new SQLFeatureNotSupportedException();
271     }
272 
273     public boolean isPoolable() throws SQLException {
274         return false;
275     }
276 
277     public <T> T unwrap(final Class<T> tClass) throws SQLException {
278         return null;
279     }
280 
281     public boolean isWrapperFor(final Class<?> aClass) throws SQLException {
282         return false;
283     }
284 }