Statistics
| Revision:

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 @ 46315

History | View | Annotate | Download (4.18 KB)

1
/**
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
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
25

    
26
import java.io.IOException;
27
import java.sql.Blob;
28
import java.sql.Clob;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31
import java.sql.Statement;
32
import org.apache.commons.io.IOUtils;
33
import org.gvsig.expressionevaluator.ExpressionBuilder;
34
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder;
35
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
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
40
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
41
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
42

    
43
public class RetrieveValueOperation extends AbstractConnectionOperation {
44

    
45
    private final TableReference table;
46
    private final String filter;
47
    private final String order;
48
    private final String fieldname;
49

    
50
    public RetrieveValueOperation(
51
            JDBCHelper helper
52
        ) {
53
        this(helper, null, null, null, null);
54
    }
55

    
56
    public RetrieveValueOperation(
57
            JDBCHelper helper,
58
            TableReference table,
59
            String filter,
60
            String order,
61
            String fieldname
62
        ) {
63
        super(helper);
64
        this.table = table;
65
        this.filter = filter;
66
        this.order = order;
67
        this.fieldname = fieldname;
68
    }
69

    
70
    @Override
71
    public final Object perform(JDBCConnection conn) throws DataException {
72
        return this.retrieveValue(conn);
73
    }
74

    
75
    public String getSQL() {
76
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
77
        ExpressionBuilder expbuilder = sqlbuilder.expression();
78

    
79
        SelectBuilder select = sqlbuilder.select();
80
        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
        String sql = select.toString();
88
        return sql;
89
    }
90

    
91
    public Object retrieveValue(JDBCConnection conn) throws DataException {
92

    
93
        String sql = this.getSQL();
94
        Statement st = null;
95
        ResultSet rs = null;
96
        try {
97
            st = conn.createStatement();
98
            rs = JDBCUtils.executeQuery(st, sql);
99
            if (!rs.next()) {
100
                return null;
101
            }
102
            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

    
114
        } catch (SQLException ex) {
115
            throw new JDBCSQLException(ex,sql);
116
        } catch (IOException ex) {
117
            throw new JDBCSQLException(ex,sql);
118
        } finally {
119
            JDBCUtils.closeQuietly(st);
120
            JDBCUtils.closeQuietly(rs);
121
        }
122
    }
123
       
124
}