svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / operations / RetrieveValueOperation.java @ 46505
History | View | Annotate | Download (4.18 KB)
1 | 45065 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 | |||
26 | 46277 | jjdelcerro | import java.io.IOException; |
27 | import java.sql.Blob; |
||
28 | import java.sql.Clob; |
||
29 | 43020 | jjdelcerro | import java.sql.ResultSet; |
30 | import java.sql.SQLException; |
||
31 | import java.sql.Statement; |
||
32 | 46277 | jjdelcerro | import org.apache.commons.io.IOUtils; |
33 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
34 | 44727 | jjdelcerro | import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
35 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
36 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
37 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
38 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
39 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
40 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
41 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
42 | 43020 | jjdelcerro | |
43 | 46277 | jjdelcerro | public class RetrieveValueOperation extends AbstractConnectionOperation { |
44 | 43020 | jjdelcerro | |
45 | 44058 | jjdelcerro | private final TableReference table; |
46 | 46277 | jjdelcerro | private final String filter; |
47 | private final String order; |
||
48 | private final String fieldname; |
||
49 | 43020 | jjdelcerro | |
50 | 46277 | jjdelcerro | public RetrieveValueOperation(
|
51 | 43020 | jjdelcerro | JDBCHelper helper |
52 | ) { |
||
53 | 44376 | jjdelcerro | this(helper, null, null, null, null); |
54 | 43020 | jjdelcerro | } |
55 | |||
56 | 46277 | jjdelcerro | public RetrieveValueOperation(
|
57 | 43020 | jjdelcerro | JDBCHelper helper, |
58 | 44058 | jjdelcerro | TableReference table, |
59 | 46277 | jjdelcerro | String filter,
|
60 | String order,
|
||
61 | String fieldname
|
||
62 | 43020 | jjdelcerro | ) { |
63 | super(helper);
|
||
64 | 44058 | jjdelcerro | this.table = table;
|
65 | 46277 | jjdelcerro | this.filter = filter;
|
66 | this.order = order;
|
||
67 | this.fieldname = fieldname;
|
||
68 | 43020 | jjdelcerro | } |
69 | |||
70 | @Override
|
||
71 | 46315 | jjdelcerro | public final Object perform(JDBCConnection conn) throws DataException { |
72 | 46277 | jjdelcerro | return this.retrieveValue(conn); |
73 | 43020 | jjdelcerro | } |
74 | |||
75 | 44678 | jjdelcerro | public String getSQL() { |
76 | 43020 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
77 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
78 | 43020 | jjdelcerro | |
79 | 44727 | jjdelcerro | SelectBuilder select = sqlbuilder.select(); |
80 | 46277 | jjdelcerro | select.column().name(this.fieldname);
|
81 | select.where().set(expbuilder.toValue(this.filter));
|
||
82 | // FIXME: Falta por ver de poner el order.
|
||
83 | select.from().table() |
||
84 | .database(this.table.getDatabase())
|
||
85 | .schema(this.table.getSchema())
|
||
86 | .name(this.table.getTable());
|
||
87 | 44727 | jjdelcerro | String sql = select.toString();
|
88 | 44678 | jjdelcerro | return sql;
|
89 | } |
||
90 | 45155 | omartinez | |
91 | 46315 | jjdelcerro | public Object retrieveValue(JDBCConnection conn) throws DataException { |
92 | 43020 | jjdelcerro | |
93 | 44678 | jjdelcerro | String sql = this.getSQL(); |
94 | 43020 | jjdelcerro | Statement st = null; |
95 | ResultSet rs = null; |
||
96 | try {
|
||
97 | st = conn.createStatement(); |
||
98 | rs = JDBCUtils.executeQuery(st, sql); |
||
99 | if (!rs.next()) {
|
||
100 | 46277 | jjdelcerro | return null; |
101 | 43020 | jjdelcerro | } |
102 | 46277 | jjdelcerro | Object value = rs.getObject(1); |
103 | if (value instanceof Blob) { |
||
104 | Blob blob = (Blob) value; |
||
105 | value = blob.getBytes(1, (int) blob.length()); |
||
106 | blob.free(); |
||
107 | } else if (value instanceof Clob) { |
||
108 | Clob clob = (Clob) value; |
||
109 | value = new String(IOUtils.toCharArray(clob.getCharacterStream())); |
||
110 | clob.free(); |
||
111 | } |
||
112 | return value;
|
||
113 | 43020 | jjdelcerro | |
114 | } catch (SQLException ex) { |
||
115 | 46148 | jjdelcerro | throw new JDBCSQLException(ex,sql); |
116 | 46277 | jjdelcerro | } catch (IOException ex) { |
117 | throw new JDBCSQLException(ex,sql); |
||
118 | 43020 | jjdelcerro | } finally {
|
119 | JDBCUtils.closeQuietly(st); |
||
120 | JDBCUtils.closeQuietly(rs); |
||
121 | } |
||
122 | } |
||
123 | 46010 | jjdelcerro | |
124 | 43020 | jjdelcerro | } |