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.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / H2ServerExplorer.java @ 41719
History | View | Annotate | Download (8.57 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
/**
|
28 |
*
|
29 |
*/
|
30 |
package org.gvsig.fmap.dal.store.h2; |
31 |
|
32 |
import java.sql.Connection; |
33 |
import java.sql.SQLException; |
34 |
import java.sql.Statement; |
35 |
import java.util.ArrayList; |
36 |
import java.util.Iterator; |
37 |
import java.util.List; |
38 |
import java.util.logging.Level; |
39 |
|
40 |
import org.gvsig.fmap.dal.DataStoreParameters; |
41 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.exception.InitializeException; |
44 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
45 |
import org.gvsig.fmap.dal.exception.RemoveException; |
46 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
47 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
48 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
49 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
50 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
51 |
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction; |
52 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
53 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
54 |
import org.slf4j.Logger; |
55 |
import org.slf4j.LoggerFactory; |
56 |
|
57 |
/**
|
58 |
* @author jmvivo
|
59 |
*
|
60 |
*/
|
61 |
public class H2ServerExplorer extends JDBCServerExplorer { |
62 |
|
63 |
final static private Logger logger = LoggerFactory |
64 |
.getLogger(H2ServerExplorer.class); |
65 |
|
66 |
public static final String NAME = "H2SQLExplorer"; |
67 |
|
68 |
public H2ServerExplorer(
|
69 |
H2ServerExplorerParameters parameters, |
70 |
DataServerExplorerProviderServices services) |
71 |
throws InitializeException {
|
72 |
super(parameters, services);
|
73 |
} |
74 |
|
75 |
private H2ServerExplorerParameters getH2SQLParameters() {
|
76 |
return (H2ServerExplorerParameters) getParameters();
|
77 |
} |
78 |
|
79 |
protected JDBCHelper createHelper() throws InitializeException { |
80 |
return new H2Helper(this, getH2SQLParameters()); |
81 |
} |
82 |
|
83 |
public String getStoreName() { |
84 |
return H2StoreProvider.NAME;
|
85 |
} |
86 |
|
87 |
public String getProviderName() { |
88 |
return NAME;
|
89 |
} |
90 |
|
91 |
protected JDBCStoreParameters createStoreParams()
|
92 |
throws InitializeException, ProviderNotRegisteredException {
|
93 |
H2StoreParameters orgParams = (H2StoreParameters) super
|
94 |
.createStoreParams(); |
95 |
|
96 |
orgParams.setUseSSL(getH2SQLParameters().getUseSSL()); |
97 |
|
98 |
return orgParams;
|
99 |
} |
100 |
|
101 |
// ****************************
|
102 |
public boolean canAdd() { |
103 |
return true; |
104 |
} |
105 |
|
106 |
public DataStoreParameters getOpenParameters() throws DataException { |
107 |
H2ServerExplorerParameters parameters = getH2SQLParameters(); |
108 |
H2StoreParameters params = new H2StoreParameters();
|
109 |
params.setHost(parameters.getHost()); |
110 |
params.setPort(parameters.getPort()); |
111 |
params.setDBName(parameters.getDBName()); |
112 |
params.setUser(parameters.getUser()); |
113 |
params.setPassword(parameters.getPassword()); |
114 |
params.setCatalog(parameters.getCatalog()); |
115 |
params.setSchema(parameters.getSchema()); |
116 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
117 |
params.setUrl(parameters.getUrl()); |
118 |
return params;
|
119 |
} |
120 |
|
121 |
protected void checkIsMine(DataStoreParameters dsp) { |
122 |
if (!(dsp instanceof H2ConnectionParameters)) { |
123 |
// FIXME Excpetion ???
|
124 |
throw new IllegalArgumentException( |
125 |
"not instance of H2SQLConnectionParameters");
|
126 |
} |
127 |
super.checkIsMine(dsp);
|
128 |
|
129 |
H2ConnectionParameters pgp = (H2ConnectionParameters) dsp; |
130 |
if (pgp.getUseSSL().booleanValue() != getH2SQLParameters()
|
131 |
.getUseSSL()) { |
132 |
throw new IllegalArgumentException("worng explorer: Host"); |
133 |
} |
134 |
} |
135 |
|
136 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
137 |
final H2StoreParameters pgParams = (H2StoreParameters) dsp;
|
138 |
|
139 |
TransactionalAction action = new TransactionalAction() {
|
140 |
public boolean continueTransactionAllowed() { |
141 |
return false; |
142 |
} |
143 |
|
144 |
public Object action(Connection conn) throws DataException { |
145 |
|
146 |
Statement st;
|
147 |
try {
|
148 |
st = conn.createStatement(); |
149 |
} catch (SQLException e) { |
150 |
throw new JDBCSQLException(e); |
151 |
} |
152 |
|
153 |
String sqlDrop = "Drop table " |
154 |
+ pgParams.tableID(); |
155 |
|
156 |
StringBuilder strb = new StringBuilder(); |
157 |
strb.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
|
158 |
if (pgParams.getSchema() == null || pgParams.getSchema().length() == 0) { |
159 |
strb.append("current_schema() ");
|
160 |
} else {
|
161 |
strb.append('\'');
|
162 |
strb.append(pgParams.getSchema()); |
163 |
strb.append("' ");
|
164 |
} |
165 |
strb.append("and f_table_name = '");
|
166 |
strb.append(pgParams.getTable()); |
167 |
strb.append('\'');
|
168 |
|
169 |
String sqlDeleteFromGeometry_column = strb.toString();
|
170 |
try {
|
171 |
try {
|
172 |
JDBCHelper.execute(st, sqlDrop); |
173 |
} catch (SQLException e) { |
174 |
throw new JDBCExecuteSQLException(sqlDrop, e); |
175 |
} |
176 |
|
177 |
try {
|
178 |
JDBCHelper.execute(st, sqlDeleteFromGeometry_column); |
179 |
} catch (SQLException e) { |
180 |
throw new JDBCExecuteSQLException( |
181 |
sqlDeleteFromGeometry_column, e); |
182 |
} |
183 |
|
184 |
} finally {
|
185 |
try {
|
186 |
st.close(); |
187 |
} catch (SQLException e) { |
188 |
}; |
189 |
} |
190 |
return null; |
191 |
} |
192 |
}; |
193 |
try {
|
194 |
this.helper.doConnectionAction(action);
|
195 |
} catch (Exception e) { |
196 |
throw new RemoveException(this.getProviderName(), e); |
197 |
} |
198 |
} |
199 |
|
200 |
public NewDataStoreParameters getAddParameters() throws DataException { |
201 |
H2ServerExplorerParameters parameters = getH2SQLParameters(); |
202 |
H2NewStoreParameters params = new H2NewStoreParameters();
|
203 |
params.setHost(parameters.getHost()); |
204 |
params.setPort(parameters.getPort()); |
205 |
params.setDBName(parameters.getDBName()); |
206 |
params.setUser(parameters.getUser()); |
207 |
params.setPassword(parameters.getPassword()); |
208 |
params.setCatalog(parameters.getCatalog()); |
209 |
params.setSchema(parameters.getSchema()); |
210 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
211 |
params.setUrl(parameters.getUrl()); |
212 |
params.setUseSSL(parameters.getUseSSL()); |
213 |
|
214 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
215 |
.createNewFeatureType()); |
216 |
|
217 |
return params;
|
218 |
} |
219 |
|
220 |
public boolean hasGeometrySupport() { |
221 |
return true; |
222 |
} |
223 |
|
224 |
protected H2Helper getPgHelper() {
|
225 |
return (H2Helper) getHelper();
|
226 |
} |
227 |
|
228 |
@Override
|
229 |
public List getDataStoreProviderNames() { |
230 |
List x = new ArrayList(1); |
231 |
x.add(H2StoreProvider.NAME); |
232 |
return x;
|
233 |
} |
234 |
|
235 |
public void updateTableStatistics(String tableName) throws JDBCExecuteSQLException { |
236 |
String sql=""; |
237 |
try {
|
238 |
|
239 |
if( tableName.startsWith("\"") ) { |
240 |
sql = "VACUUM ANALYZE " + tableName ;
|
241 |
} else {
|
242 |
sql = "VACUUM ANALYZE \"" + tableName + "\""; |
243 |
} |
244 |
Connection conn = this.getHelper().getConnection(); |
245 |
Statement st = conn.createStatement();
|
246 |
JDBCHelper.execute(st, sql); |
247 |
} catch (Exception ex) { |
248 |
throw new JDBCExecuteSQLException(sql, ex); |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
} |