Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLServerExplorer.java @ 28967

History | View | Annotate | Download (6.87 KB)

1 27906 jmvivo
/* 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
 */
31 27919 jmvivo
package org.gvsig.fmap.dal.store.postgresql;
32 27906 jmvivo
33
import java.sql.Connection;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36
37
import org.gvsig.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.dal.NewDataStoreParameters;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.InitializeException;
41 28909 jmvivo
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
42 27906 jmvivo
import org.gvsig.fmap.dal.exception.RemoveException;
43 28909 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
44 27906 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
45 28909 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
47 28784 jmvivo
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
48 28785 jmvivo
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
49
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
50 27906 jmvivo
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52
53
/**
54
 * @author jmvivo
55
 *
56
 */
57 28784 jmvivo
public class PostgreSQLServerExplorer extends JDBCServerExplorer {
58 27906 jmvivo
        final static private Logger logger = LoggerFactory
59
                        .getLogger(PostgreSQLServerExplorer.class);
60
61 27953 jmvivo
        public static final String NAME = "PostgreSQLExplorer";
62 27906 jmvivo
63 28909 jmvivo
        private PostgreSQLServerExplorerParameters pgParameters;
64 27906 jmvivo
65
66
        public PostgreSQLServerExplorer(
67
                        PostgreSQLServerExplorerParameters parameters)
68
                        throws InitializeException {
69
                super(parameters);
70 28909 jmvivo
        }
71 27906 jmvivo
72 28909 jmvivo
        protected void init(JDBCServerExplorerParameters parameters)
73
                        throws InitializeException {
74
                pgParameters = (PostgreSQLServerExplorerParameters) parameters;
75
                super.init(parameters);
76 27906 jmvivo
        }
77
78 28909 jmvivo
        protected JDBCHelper createHelper() throws InitializeException {
79
                return new PostgreSQLHelper(this, pgParameters);
80
        }
81 27906 jmvivo
82 28909 jmvivo
83 27906 jmvivo
        protected String getStoreName() {
84
                return PostgreSQLStoreProvider.NAME;
85
        }
86
87
        public String getName() {
88
                return NAME;
89
        }
90
91 28909 jmvivo
        protected JDBCStoreParameters createStoreParams()
92
                        throws InitializeException, ProviderNotRegisteredException {
93
                PostgreSQLStoreParameters orgParams = (PostgreSQLStoreParameters) super
94
                                .createStoreParams();
95 27906 jmvivo
96 28909 jmvivo
                orgParams.setUseSSL(pgParameters.getUseSSL());
97 27908 jmvivo
98 28909 jmvivo
                return orgParams;
99 27906 jmvivo
        }
100
101 27908 jmvivo
102
        // ****************************
103
104
105
        public boolean canAdd() {
106 28909 jmvivo
                return true;
107 27906 jmvivo
        }
108
109 28909 jmvivo
        protected void checkIsMine(DataStoreParameters dsp) {
110 28257 jmvivo
                if (!(dsp instanceof PostgreSQLStoreParameters)) {
111
                        // FIXME Excpetion ???
112
                        throw new IllegalArgumentException(
113 28909 jmvivo
                                        "not instance of PostgreSQLStoreParameters");
114 28257 jmvivo
                }
115 28909 jmvivo
                super.checkIsMine(dsp);
116
117 28257 jmvivo
                PostgreSQLStoreParameters pgp = (PostgreSQLStoreParameters) dsp;
118 28909 jmvivo
                if (pgp.getUseSSL().booleanValue() != pgParameters.getUseSSL()) {
119 28257 jmvivo
                        throw new IllegalArgumentException("worng explorer: Host");
120
                }
121
        }
122
123 27906 jmvivo
        public void remove(DataStoreParameters dsp) throws RemoveException {
124 28257 jmvivo
                final PostgreSQLStoreParameters pgParams =(PostgreSQLStoreParameters) dsp;
125 27906 jmvivo
126 28257 jmvivo
                TransactionalAction action = new TransactionalAction() {
127
                        public boolean continueTransactionAllowed() {
128
                                return false;
129
                        }
130
                        public Object action(Connection conn) throws DataException {
131
132
133
                                Statement st;
134
                                try{
135
                                        st = conn.createStatement();
136
                                } catch (SQLException e) {
137
                                        throw new JDBCSQLException(e);
138
                                }
139
140
                                String sqlDrop = "Drop table "
141
                                        + pgParams.tableID();
142
143
                                StringBuilder strb = new StringBuilder();
144
                                strb.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
145
                                if (pgParams.getSchema() == null || pgParams.getSchema().length() ==  0) {
146
                                        strb.append("current_schema() ");
147
                                } else {
148
                                        strb.append('\'');
149
                                        strb.append(pgParams.getSchema());
150
                                        strb.append("' ");
151
                                }
152
                                strb.append("and f_table_name = '");
153
                                strb.append(pgParams.getTable());
154
                                strb.append('\'');
155
156
                                String sqlDeleteFromGeometry_column = strb.toString();
157
                                try{
158
                                        try{
159
                                                st.execute(sqlDrop);
160
                                        } catch (SQLException e) {
161
                                                throw new JDBCExecuteSQLException(sqlDrop, e);
162
                                        }
163
164
                                        try {
165
                                                st.execute(sqlDeleteFromGeometry_column);
166
                                        } catch (SQLException e) {
167
                                                throw new JDBCExecuteSQLException(
168
                                                                sqlDeleteFromGeometry_column, e);
169
                                        }
170
171
                                } finally{
172
                                        try{ st.close(); } catch (SQLException e) {};
173
                                }
174
                                return null;
175
                        }
176
                };
177
                try {
178
                        this.helper.doConnectionAction(action);
179
                } catch (Exception e) {
180
                        throw new RemoveException(this.getName(), e);
181
                }
182 27906 jmvivo
        }
183
184
        public NewDataStoreParameters getAddParameters() throws DataException {
185 28257 jmvivo
                PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
186
                params.setHost(this.parameters.getHost());
187
                params.setPort(this.parameters.getPort());
188
                params.setDBName(this.parameters.getDBName());
189
                params.setUser(this.parameters.getUser());
190
                params.setPassword(this.parameters.getPassword());
191
                params.setCatalog(this.parameters.getCatalog());
192
                params.setSchema(this.parameters.getSchema());
193
                params.setJDBCDriverClassName(this.parameters.getJDBCDriverClassName());
194 28909 jmvivo
                params.setUrl(this.parameters.getUrl());
195
                params.setUseSSL(((PostgreSQLServerExplorerParameters) this.parameters)
196
                                .getUseSSL());
197 28257 jmvivo
198
199
                params.setDefaultFeatureType(this.getServerExplorerProviderServices()
200
                                .createNewFeatureType());
201
202
203
                return params;
204 27906 jmvivo
        }
205
206
207
208
        // ***********************
209
        // ***********************
210
211
212 28784 jmvivo
        public boolean hasGeometrySupport() {
213
                return true;
214
        }
215 27982 jmvivo
216 28909 jmvivo
        protected PostgreSQLHelper getPgHelper() {
217
                return (PostgreSQLHelper) getHelper();
218
        }
219 27982 jmvivo
220 28784 jmvivo
221 28917 jmvivo
        protected String getSQLForList(int mode, boolean showInformationDBTables) {
222
                StringBuffer sqlBuf = new StringBuffer();
223
                sqlBuf
224
                                .append("SELECT null as TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, false as ISVIEW ");
225
                sqlBuf.append(" FROM INFORMATION_SCHEMA.TABLES ");
226
                sqlBuf.append(" xxWHERExx ");
227
                sqlBuf.append(" union ");
228
                sqlBuf
229
                                .append("SELECT null as TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, true as ISVIEW ");
230
                sqlBuf.append(" FROM INFORMATION_SCHEMA.VIEWS ");
231
                sqlBuf.append(" xxWHERExx ");
232
233
                if (showInformationDBTables) {
234
                        return sqlBuf.toString().replaceAll("xxWHERExx", "");
235
                } else {
236
                        return sqlBuf
237
                                        .toString()
238
                                        .replaceAll("xxWHERExx",
239
                                                        "WHERE TABLE_SCHEMA NOT IN ('information_schema','pg_catalog')");
240
241
                }
242
243
244
        }
245 27906 jmvivo
}