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