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

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

    
33
public class ExecuteOperation extends AbstractConnectionOperation {
34

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

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

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

    
107

    
108
}