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 / ExecuteOperation.java @ 47606

History | View | Annotate | Download (3.45 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.sql.ResultSet;
27
import java.sql.Statement;
28
import org.apache.commons.lang3.StringUtils;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
31
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
32
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
33

    
34
public class ExecuteOperation extends AbstractConnectionOperation {
35

    
36
    private final String sql;
37
    
38
    public ExecuteOperation(
39
            JDBCHelper helper,
40
            String sql
41
        ) {
42
        super(helper);
43
        this.sql = sql;
44
    }
45

    
46
    @Override
47
    public final Object perform(JDBCConnection conn) throws DataException {
48
        return execute(sql);
49
    }
50

    
51
    public Object execute(String sql) {
52
        Statement st = null;
53
        ResultSet rs = null;
54
        try {
55
            st = this.getConnection().createStatement(sql);
56
            String mode;
57
            try {
58
                String sqltrim = sql.trim();
59
                mode = StringUtils.left(sqltrim, sqltrim.indexOf(' ')).toLowerCase();
60
            } catch (Exception e) {
61
                mode = "unknown";
62
            }
63
            
64
            switch(mode){
65
                case "select":
66
                    rs = JDBCUtils.executeQuery(st, sql);
67
                    break;
68
                case "update":
69
                    JDBCUtils.executeUpdate(st, sql);
70
                    break;
71
                default:
72
                    JDBCUtils.execute(st, sql);
73
                    break;
74
            }
75
            if(rs == null) {
76
                JDBCUtils.closeQuietly(st);
77
                return null;
78
            }
79
            if(rs.getMetaData().getColumnCount() > 1) {
80
                return rs;
81
            }
82
            if(!rs.next()){
83
                rs.close();
84
                JDBCUtils.closeQuietly(st);
85
                return null;
86
            }
87
            if(rs.isFirst() && rs.isLast()) {
88
                Object res = rs.getObject(1);
89
                rs.close();
90
                JDBCUtils.closeQuietly(st);
91
                return res;
92
            }
93
            rs.first();
94
            return rs;
95
        } catch (Exception ex) {
96
            JDBCUtils.closeQuietly(rs);
97
            JDBCUtils.closeQuietly(st);
98
            throw new RuntimeException("Can't execute query ["+sql+"].",ex);
99
        }
100
    }
101
    
102
    protected void closeConnection(Object result) throws Exception {
103
        if (!(result instanceof ResultSet)) {
104
            this.getConnection().close();
105
            conn = null;
106
        }
107
    }
108

    
109

    
110
}