Revision 45437 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
ExecuteOperation.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.sql.Connection; |
27 | 27 |
import java.sql.ResultSet; |
28 |
import java.sql.SQLException; |
|
28 | 29 |
import java.sql.Statement; |
29 |
import org.apache.commons.lang3.StringUtils; |
|
30 | 30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
|
31 | 32 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
32 | 33 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
34 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.AbstractConnectionOperation.LOGGER; |
|
33 | 35 |
|
34 | 36 |
public class ExecuteOperation extends AbstractConnectionOperation { |
35 | 37 |
|
... | ... | |
50 | 52 |
|
51 | 53 |
public Object execute(String sql) { |
52 | 54 |
Statement st = null; |
55 |
ResultSet rs = null; |
|
53 | 56 |
try { |
54 | 57 |
st = this.getConnection().createStatement(); |
55 |
ResultSet rs = null; |
|
56 | 58 |
String mode = "select"; |
57 | 59 |
try { |
58 | 60 |
mode = sql.trim().substring(0, sql.trim().indexOf(' ')).toLowerCase(); |
... | ... | |
71 | 73 |
JDBCUtils.execute(st, sql); |
72 | 74 |
break; |
73 | 75 |
} |
74 |
if(rs != null) { |
|
75 |
if(rs.next()){ |
|
76 |
if(rs.getMetaData().getColumnCount() == 1 && rs.isFirst() && rs.isLast()) { |
|
77 |
Object res = rs.getObject(1); |
|
78 |
rs.close(); |
|
79 |
return res; |
|
80 |
} |
|
81 |
return rs; |
|
82 |
} |
|
83 |
rs.close(); |
|
76 |
if(rs == null) { |
|
77 |
JDBCUtils.closeQuietly(st); |
|
78 |
return null; |
|
84 | 79 |
} |
85 |
return null; |
|
80 |
if(rs.getMetaData().getColumnCount() > 0) { |
|
81 |
return rs; |
|
82 |
} |
|
83 |
if(!rs.next()){ |
|
84 |
rs.close(); |
|
85 |
JDBCUtils.closeQuietly(st); |
|
86 |
return null; |
|
87 |
} |
|
88 |
if(rs.isFirst() && rs.isLast()) { |
|
89 |
Object res = rs.getObject(1); |
|
90 |
rs.close(); |
|
91 |
JDBCUtils.closeQuietly(st); |
|
92 |
return res; |
|
93 |
} |
|
94 |
rs.first(); |
|
95 |
return rs; |
|
86 | 96 |
} catch (Exception ex) { |
87 |
throw new RuntimeException("Can't execute query ["+sql+"].",ex); |
|
88 |
|
|
89 |
} finally { |
|
97 |
JDBCUtils.closeQuietly(rs); |
|
90 | 98 |
JDBCUtils.closeQuietly(st); |
99 |
throw new RuntimeException("Can't execute query ["+sql+"].",ex); |
|
91 | 100 |
} |
92 | 101 |
} |
93 | 102 |
|
94 |
// |
|
95 |
// |
|
96 |
// public void execute( |
|
97 |
// Connection conn, |
|
98 |
// String sql |
|
99 |
// ) { |
|
100 |
// Statement st = null; |
|
101 |
// try { |
|
102 |
// st = conn.createStatement(); |
|
103 |
// JDBCUtils.execute(st, sql); |
|
104 |
// } catch (SQLException ex) { |
|
105 |
// throw new RuntimeException("Can't execute query ["+sql+"].",ex); |
|
106 |
// |
|
107 |
// } finally { |
|
108 |
// JDBCUtils.closeQuietly(st); |
|
109 |
// } |
|
110 |
// |
|
111 |
// } |
|
103 |
protected void closeConnection(Object result) throws Exception { |
|
104 |
if (!(result instanceof ResultSet)) { |
|
105 |
helper.closeConnection(this.getConnection()); |
|
106 |
conn = null; |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
|
|
112 | 111 |
} |
Also available in: Unified diff