2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
20 package cx.fbn.nevernote.sql.driver;
\r
22 import java.io.ByteArrayInputStream;
\r
23 import java.sql.Blob;
\r
24 import java.sql.Connection;
\r
25 import java.sql.PreparedStatement;
\r
26 import java.sql.ResultSet;
\r
27 import java.sql.SQLException;
\r
28 import java.sql.Statement;
\r
29 import java.util.HashMap;
\r
31 public class NSqlQuery {
\r
33 private final Connection connection;
\r
34 private String lastError;
\r
35 private ResultSet resultSet;
\r
36 private PreparedStatement preparedStatement;
\r
37 private final HashMap<String, Integer> positionMap;
\r
38 private ByteArrayInputStream fis;
\r
41 public NSqlQuery(Connection c) {
\r
43 positionMap = new HashMap<String, Integer>();
\r
47 public boolean next() {
\r
50 if (resultSet == null) {
\r
51 lastError = "Result set is null";
\r
55 return resultSet.next();
\r
56 } catch (SQLException e) {
\r
57 e.printStackTrace();
\r
58 lastError = e.getMessage();
\r
64 public boolean exec(String sql) {
\r
66 boolean retVal = false;
\r
70 st = connection.createStatement();
\r
71 retVal = st.execute(sql);
\r
72 resultSet = st.getResultSet();
\r
73 } catch (SQLException e) {
\r
74 e.printStackTrace();
\r
75 lastError = e.getMessage();
\r
80 public boolean exec() {
\r
84 if (preparedStatement == null) {
\r
85 lastError = "No SQL statement prepared";
\r
90 preparedStatement.execute();
\r
91 resultSet = preparedStatement.getResultSet();
\r
92 } catch (SQLException e) {
\r
93 e.printStackTrace();
\r
94 lastError = e.getMessage();
\r
103 public String lastError() {
\r
104 if (lastError == null)
\r
110 public Object valueObject(int position) {
\r
112 if (resultSet == null) {
\r
113 lastError = "ResultSet is null";
\r
118 return resultSet.getObject(position+1);
\r
119 } catch (SQLException e) {
\r
120 e.printStackTrace();
\r
121 lastError = e.getMessage();
\r
127 public String valueString(int position) {
\r
129 if (resultSet == null) {
\r
130 lastError = "ResultSet is null";
\r
135 return resultSet.getString(position+1);
\r
136 } catch (SQLException e) {
\r
137 e.printStackTrace();
\r
138 lastError = e.getMessage();
\r
143 public boolean valueBoolean(int position, boolean unknown) {
\r
145 return resultSet.getBoolean(position+1);
\r
146 } catch (SQLException e) {
\r
147 e.printStackTrace();
\r
152 public long valueLong(int position) {
\r
154 return resultSet.getLong(position+1);
\r
155 } catch (SQLException e) {
\r
156 e.printStackTrace();
\r
161 public int valueInteger(int position) {
\r
163 return resultSet.getInt(position+1);
\r
164 } catch (SQLException e) {
\r
165 e.printStackTrace();
\r
170 public void bindValue(String field, String value) {
\r
171 Integer position = positionMap.get(field.toLowerCase());
\r
173 if (preparedStatement == null) {
\r
174 lastError = "No prepared statement exists";
\r
177 if (position != null && position > 0) {
\r
179 preparedStatement.setString(position, value);
\r
180 } catch (SQLException e) {
\r
181 e.printStackTrace();
\r
182 lastError = e.getMessage();
\r
188 public void bindValue(String field, boolean value) {
\r
189 Integer position = positionMap.get(field.toLowerCase());
\r
191 if (preparedStatement == null) {
\r
192 lastError = "No prepared statement exists";
\r
195 if (position > 0) {
\r
197 preparedStatement.setBoolean(position, value);
\r
198 } catch (SQLException e) {
\r
199 e.printStackTrace();
\r
200 lastError = e.getMessage();
\r
208 public void bindValue(String field, int value) {
\r
209 Integer position = positionMap.get(field.toLowerCase());
\r
211 if (preparedStatement == null) {
\r
212 lastError = "No prepared statement exists";
\r
215 if (position > 0) {
\r
217 preparedStatement.setInt(position, value);
\r
218 } catch (SQLException e) {
\r
219 e.printStackTrace();
\r
220 lastError = e.getMessage();
\r
228 public void bindValue(String field, double value) {
\r
229 Integer position = positionMap.get(field.toLowerCase());
\r
231 if (preparedStatement == null) {
\r
232 lastError = "No prepared statement exists";
\r
235 if (position > 0) {
\r
237 preparedStatement.setDouble(position, value);
\r
238 } catch (SQLException e) {
\r
239 e.printStackTrace();
\r
240 lastError = e.getMessage();
\r
248 public void bindValue(String field, byte[] value) {
\r
249 Integer position = positionMap.get(field.toLowerCase());
\r
251 if (preparedStatement == null) {
\r
252 lastError = "No prepared statement exists";
\r
255 if (position > 0) {
\r
257 preparedStatement.setBytes(position, value);
\r
258 } catch (SQLException e) {
\r
259 e.printStackTrace();
\r
260 lastError = e.getMessage();
\r
268 public boolean prepare(String statement) {
\r
269 positionMap.clear();
\r
270 preparedStatement = null;
\r
274 for (int i=statement.indexOf(":"); i>0; i=statement.indexOf(":",i)) {
\r
275 int endField = statement.indexOf(" ",i+1);
\r
276 int nextComma = statement.indexOf(",",i+1);
\r
277 int nextBracket = statement.indexOf(")",i+1);
\r
279 if (nextComma > 0 && nextComma < endField)
\r
280 endField = nextComma;
\r
281 if (endField == -1)
\r
282 endField = nextBracket;
\r
283 if (nextBracket > 0 && nextBracket < endField)
\r
284 endField = nextBracket;
\r
286 String fieldName = null;
\r
287 if (endField > 0) {
\r
288 fieldName = statement.substring(i,endField);
\r
291 fieldName = statement.substring(i);
\r
292 endField = statement.length();
\r
294 statement = statement.substring(0,i)+"?" +statement.substring(endField);
\r
295 positionMap.put(fieldName.toLowerCase(), new Integer(position));
\r
301 preparedStatement = connection.prepareStatement(statement);
\r
302 } catch (SQLException e) {
\r
303 e.printStackTrace();
\r
304 lastError = e.getMessage();
\r
313 public void bindBlob(String field, byte[] value) {
\r
314 Integer position = positionMap.get(field.toLowerCase());
\r
316 if (preparedStatement == null) {
\r
317 lastError = "No prepared statement exists";
\r
320 if (position != null && position > 0) {
\r
322 fis = new ByteArrayInputStream(value);
\r
323 preparedStatement.setBinaryStream(position, fis);
\r
324 } catch (SQLException e) {
\r
325 e.printStackTrace();
\r
326 lastError = e.getMessage();
\r
333 public byte[] getBlob(int position) {
\r
336 dataBinary = resultSet.getBlob(position+1);
\r
338 if (dataBinary == null)
\r
340 b = dataBinary.getBytes(1, (int) dataBinary.length());
\r
342 } catch (SQLException e) {
\r
343 e.printStackTrace();
\r
344 lastError = e.getMessage();
\r