Revision 780

View differences:

tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLNewStoreParameters.java
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
package org.gvsig.postgresql.dal;
23

  
24
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
25
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
26

  
27
public class PostgreSQLNewStoreParameters 
28
    extends 
29
        JDBCNewStoreParameters 
30
    implements 
31
        PostgreSQLConnectionParameters 
32
    {
33

  
34
    private final PostgreSQLConnectionParametersHelper helper;
35
    
36
    public PostgreSQLNewStoreParameters() {
37
        super(
38
            PostgreSQLLibrary.NAME + "NewStoreParameters", 
39
            PostgreSQLLibrary.NAME
40
        );
41
        this.helper = new PostgreSQLConnectionParametersHelper(this);
42
    }
43

  
44
    @Override
45
    public String getUrl() {
46
        return this.helper.getUrl();
47
    }
48
    
49
    @Override
50
    public void validate() throws ValidateDataParametersException {
51
        this.helper.validate();
52
    }
53
        
54
    @Override
55
    public boolean getUseSSL() {
56
        return this.helper.getUseSSL();
57
    }
58

  
59
    @Override
60
    public int getMaxIdle() {
61
        return this.helper.getMaxIdle();
62
    }
63

  
64
    public void setUseSSL(boolean v) {
65
        this.helper.setUseSSL(v);
66
    }
67

  
68
    @Override
69
    public int getNetworkTimeout() {
70
        return this.helper.getNetworkTimeout();
71
    }
72

  
73
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLServerExplorerParameters.java
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
 */
25
package org.gvsig.postgresql.dal;
26

  
27
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
28
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
29

  
30
             
31
public class PostgreSQLServerExplorerParameters extends
32
        JDBCServerExplorerParameters 
33
    implements
34
        PostgreSQLConnectionParameters
35
    {
36
    
37
    private final PostgreSQLConnectionParametersHelper helper;
38

  
39
    public PostgreSQLServerExplorerParameters() {
40
        super(
41
                PostgreSQLLibrary.NAME + "ServerExplorerParameters",
42
                PostgreSQLLibrary.NAME
43
        );
44
        this.helper = new PostgreSQLConnectionParametersHelper(this);
45
    }
46

  
47
    @Override
48
    public String getUrl() {
49
        return this.helper.getUrl();
50
    }
51
    
52
    @Override
53
    public void validate() throws ValidateDataParametersException {
54
        // Esto seria para convertir los parametros de gvSIG 2.3 a 2.4.
55
//        if( !StringUtils.equalsIgnoreCase(PostgreSQLLibrary.NAME, (CharSequence) getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME)) ) {
56
//            setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, PostgreSQLLibrary.NAME);
57
//        }
58
        this.helper.validate();
59
        super.validate();        
60
    }
61

  
62
    @Override
63
    public boolean getUseSSL() {
64
        return this.helper.getUseSSL();
65
    }
66

  
67
    @Override
68
    public int getMaxIdle() {
69
        return this.helper.getMaxIdle();
70
    }
71

  
72
    public void setUseSSL(boolean v) {
73
        this.helper.setUseSSL(v);
74
    }
75

  
76
    @Override
77
    public int getNetworkTimeout() {
78
        return this.helper.getNetworkTimeout();
79
    }
80
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLHelper.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import java.sql.Connection;
5
import java.sql.SQLException;
6
import java.util.logging.Level;
7
import org.apache.commons.dbcp2.BasicDataSource;
8
import org.apache.commons.lang3.BooleanUtils;
9
import org.apache.commons.lang3.StringUtils;
10
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType;
11
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
12
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
13
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
14
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
15
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
16
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
17
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
18
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
19
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
20
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase;
21
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverDumb;
22
import org.gvsig.postgresql.dal.operations.PostgreSQLOperationsFactory;
23
import org.gvsig.tools.dispose.Disposable;
24
import org.gvsig.tools.exception.BaseException;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

  
28
public class PostgreSQLHelper extends JDBCHelperBase {
29

  
30
    /* friend */ static final Logger LOGGER = LoggerFactory.getLogger(PostgreSQLHelper.class);
31

  
32
    public static final String POSTGRESQL_JDBC_DRIVER = "org.postgresql.Driver";
33
    
34
    public static String getConnectionURL(PostgreSQLConnectionParameters params) {
35
        return getConnectionURL(
36
            params.getHost(),
37
            params.getPort(),
38
            params.getDBName()
39
        );
40
    }
41
    
42
    public static String getConnectionURL(String host, Integer port, String db) {
43
        if( StringUtils.isEmpty(host) ) {
44
            throw new IllegalArgumentException("Parameter 'host' can't be null.");
45
        }
46
        String connectionURL = "jdbc:postgresql://" + host;
47
        if (port != null) {
48
            connectionURL = connectionURL + ":" + port;
49
        }
50
        connectionURL = connectionURL + "/" + db;
51
        LOGGER.debug("connectionURL: {}", connectionURL);
52
        return connectionURL;
53
    }
54

  
55
    private static class ConnectionProvider implements Disposable {
56

  
57
        private static boolean needRegisterDriver = true;
58

  
59
        private BasicDataSource dataSource = null;
60

  
61
        private PostgreSQLConnectionParameters connectionParameters;
62

  
63
        public ConnectionProvider(PostgreSQLConnectionParameters connectionParameters) {
64
            this.connectionParameters = connectionParameters;
65
        }
66

  
67
        public Connection getConnection() throws SQLException {
68
            if (this.dataSource == null) {
69
                this.dataSource = this.createDataSource();               
70
            }
71
            if( LOGGER.isDebugEnabled() ) {
72
                LOGGER.debug("getConnection:\n" + getStatusInformation());
73
            }
74
            Connection conn;
75
            try {
76
                conn = this.dataSource.getConnection();
77
            } catch(Throwable ex) {
78
                LOGGER.debug("Error getting connection from pool.",ex);
79
                throw ex;
80
            }
81
            try {
82
                conn.setNetworkTimeout(null, this.connectionParameters.getNetworkTimeout());
83
            } catch(Throwable ex) {
84
                LOGGER.warn("Error setting the network timeout.",ex);
85
            }
86
            if( LOGGER.isDebugEnabled() ) {
87
                LOGGER.debug("Created connection: {}\n  NumActive: {}\n  NumIdle: {}",
88
                    new Object[] {
89
                        conn.hashCode(), 
90
                        this.dataSource.getNumActive(),
91
                        this.dataSource.getNumIdle()
92
                    }
93
                );
94
            }
95
            return conn;
96
        }
97
        
98
        public void closeConnection(Connection connection) {
99
            if( connection != null ) {
100
                int connectionId = connection.hashCode();
101
                try {
102
                    connection.close();
103
                } catch(Throwable ex) {
104
                    LOGGER.warn("Can't close connection.", ex);
105
                }
106
                if( LOGGER.isDebugEnabled() ) {
107
                    Boolean isClosed;
108
                    try {
109
                        isClosed = connection.isClosed();
110
                    } catch(Throwable th) {
111
                        isClosed = null;
112
                    }
113
                    LOGGER.debug("Closed connection: {}\n  isClosed: {}\n  NumActive: {}\n  NumIdle: {}",
114
                        new Object[] {
115
                            connectionId, 
116
                            isClosed,
117
                            this.dataSource.getNumActive(),
118
                            this.dataSource.getNumIdle()
119
                        }
120
                    );
121
                }
122
           } else if( LOGGER.isDebugEnabled() ) {
123
               LOGGER.debug("Close connection: null");
124
           }
125
        }
126
        
127
        public String getStatusInformation() {
128
            StringBuilder builder = new StringBuilder();
129
            builder.append("BasicDataSource pool status:\n");
130
            builder.append("  Connection URL: '").append(this.dataSource.getUrl()).append("'\n");
131
            if( this.dataSource.getInitialSize()>0 ) {
132
                builder.append("  InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n");
133
            }
134
            if( this.dataSource.isPoolPreparedStatements() ) {
135
                builder.append("  PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n");
136
                builder.append("  MaxOpenPreparedStatements: ").append(this.dataSource.getMaxOpenPreparedStatements()).append(" (The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit)\n");
137
            }
138
            builder.append("  MaxTotal: ").append(this.dataSource.getMaxTotal()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n");
139
            builder.append("  MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n");
140
            builder.append("  NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n");
141
            builder.append("  NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n");
142
            return builder.toString();
143
        }
144

  
145
        private BasicDataSource createDataSource() throws SQLException {
146
            if (!this.isRegistered()) {
147
                this.registerDriver();
148
            }
149
            PostgreSQLConnectionParameters params = connectionParameters;
150

  
151
            BasicDataSource ds = new BasicDataSource();
152
            ds.setMaxIdle(params.getMaxIdle());
153
            ds.setDriverClassName(params.getJDBCDriverClassName());
154
            if( params.getUseSSL() ) {
155
                String s = BooleanUtils.toStringTrueFalse(params.getUseSSL());
156
                ds.addConnectionProperty("ssl", s );
157
            }
158
            if( !StringUtils.isEmpty(params.getUser()) ) {
159
                ds.setUsername(params.getUser());
160
            }
161
            if( !StringUtils.isEmpty(params.getPassword()) ) {
162
                ds.setPassword(params.getPassword());
163
            }
164
            ds.setUrl(params.getUrl());
165
            
166
            ds.setMinEvictableIdleTimeMillis(20*1000);
167
            ds.setTimeBetweenEvictionRunsMillis(20*1000);
168
            ds.setMinIdle(0);
169
            
170
            ds.setMaxWaitMillis(60L * 1000);
171
            return ds;
172
        }
173

  
174
        private boolean isRegistered() {
175
            return needRegisterDriver;
176
        }
177

  
178
        public void registerDriver() throws SQLException {
179
            String className = this.connectionParameters.getJDBCDriverClassName();
180
            if (className == null) {
181
                return;
182
            }
183
            try {
184
                Class theClass = Class.forName(className);
185
                if (theClass == null) {
186
                    throw new JDBCDriverClassNotFoundException(PostgreSQLLibrary.NAME, className);
187
                }
188
            } catch (Exception e) {
189
                throw new SQLException("Can't register JDBC driver '" + className + "'.", e);
190
            }
191
            needRegisterDriver = false;
192
        }
193

  
194
        @Override
195
        public void dispose() {
196
            if( this.dataSource!=null ) {
197
                try {
198
                    this.dataSource.close();
199
                } catch (SQLException ex) {
200
                    LOGGER.warn("Can't close BasicDataSource", ex);
201
                }
202
                this.dataSource = null;
203
            }
204
            this.connectionParameters = null;
205
        }
206

  
207
    }
208

  
209
    private ConnectionProvider connectionProvider = null;
210
   
211
    public PostgreSQLHelper() {
212
        super(null);
213
        this.srssolver = new SRSSolverDumb(this);
214
    }
215

  
216
    public PostgreSQLHelper(JDBCConnectionParameters connectionParameters) {
217
        super(connectionParameters);
218
        this.srssolver = new SRSSolverBase(this);
219
    }
220

  
221
    @Override
222
    protected void doDispose() throws BaseException {
223
        if( this.connectionProvider!=null ) {
224
            this.connectionProvider.dispose();
225
            this.connectionProvider = null;
226
        }
227
        super.doDispose(); 
228
    }
229

  
230
    @Override
231
    public Connection getConnection() throws AccessResourceException {
232
        try {
233
            if (this.connectionProvider == null) {
234
              if( this.getConnectionParameters()==null ) {
235
                return null;
236
              }
237
              this.connectionProvider = new ConnectionProvider(this.getConnectionParameters());
238
            }
239
            return this.connectionProvider.getConnection();
240
        } catch (SQLException ex) {
241
            throw new AccessResourceException(PostgreSQLLibrary.NAME, ex);
242
        }
243
    }
244

  
245
    @Override
246
    public void closeConnection(Connection connection) {
247
      if( connection!=null ) { // In test ???
248
         this.connectionProvider.closeConnection(connection);
249
      }
250
    }
251
    
252
    @Override
253
    public PostgreSQLConnectionParameters getConnectionParameters() {
254
        return (PostgreSQLConnectionParameters) super.getConnectionParameters();
255
    }
256
    
257
    @Override
258
    public String getConnectionURL() {
259
        return getConnectionURL(this.getConnectionParameters());
260
    }
261

  
262
    @Override
263
    protected String getResourceType() {
264
        return PostgreSQLLibrary.NAME;
265
    }
266

  
267
    @Override
268
    public String getProviderName() {
269
        return PostgreSQLLibrary.NAME;
270
    }
271

  
272
    @Override
273
    public JDBCSQLBuilderBase createSQLBuilder() {
274
        return new PostgreSQLBuilder(this);
275
    }
276
    
277
    @Override
278
    public OperationsFactory getOperations() {
279
        if (this.operationsFactory == null) {
280
            this.operationsFactory = new PostgreSQLOperationsFactory(this);
281
        }
282
        return operationsFactory;
283
    }
284

  
285
    @Override
286
    public GeometrySupportType getGeometrySupportType() {
287
        return GeometrySupportType.WKB;
288
    }
289

  
290
    @Override
291
    public boolean hasSpatialFunctions() {
292
        return true;
293
    }
294

  
295
    @Override
296
    public boolean canWriteGeometry(int geometryType, int geometrySubtype) {
297
        return true;
298
    }
299

  
300
    @Override
301
    public String getQuoteForIdentifiers() {
302
        return "\"";
303
    }
304

  
305
    @Override
306
    public boolean allowAutomaticValues() {
307
        return true;
308
    }
309

  
310
    @Override
311
    public boolean supportOffsetInSelect() {
312
        return true;
313
    }
314

  
315
    @Override
316
    public String getQuoteForStrings() {
317
        return "'";
318
    }
319
    
320
    @Override
321
    public String getSourceId(JDBCStoreParameters parameters) {
322
        return parameters.getDBName() + "." + 
323
               parameters.getSchema()+ "." + 
324
               parameters.getTable();
325
    }
326

  
327
    @Override
328
    public JDBCNewStoreParameters createNewStoreParameters() {
329
        return new PostgreSQLNewStoreParameters();
330
    }
331

  
332
    @Override
333
    public JDBCStoreParameters createOpenStoreParameters() {
334
        return new PostgreSQLStoreParameters();
335
    }
336

  
337
    @Override
338
    public JDBCServerExplorerParameters createServerExplorerParameters() {
339
        return new PostgreSQLServerExplorerParameters();
340
    }
341
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLServerExplorerFactory.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.fmap.dal.DataServerExplorerParameters;
6
import org.gvsig.fmap.dal.exception.InitializeException;
7
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
8
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
9
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
10
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
13
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCServerExplorerFactory;
14

  
15

  
16
public class PostgreSQLServerExplorerFactory extends JDBCServerExplorerFactory {
17

  
18
    private static final String NAME = PostgreSQLLibrary.NAME;
19
    
20
    public PostgreSQLServerExplorerFactory() {
21
        super(
22
            NAME,
23
            "PostgreSQL Server"
24
        );
25
    }
26

  
27
    public PostgreSQLServerExplorerFactory(String name) {
28
        // Cuando se instancia la factoria con un "name" que no es el de por
29
        // defecto, es para declarar "alias" para el ServerExplorer, normalmente
30
        // para mantener compatibilidad con versiones anteriores. 
31
        // Marcaremos la factoria como "hidden" para que no aparezca
32
        // en el interface de usuario.
33
        super(
34
            name,
35
            "PostgreSQL Server (for compatibility)",
36
            true
37
        );
38
    }
39

  
40
    @Override
41
    public JDBCServerExplorer create(
42
            DataServerExplorerParameters parameters, 
43
            DataServerExplorerProviderServices providerServices
44
        ) throws InitializeException {
45
        // Esto seria para convertir los parametros de gvSIG 2.3 a 2.4.
46
//        if( !StringUtils.equalsIgnoreCase(NAME, (CharSequence) parameters.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME)) ) {
47
//            parameters.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, NAME);
48
//        }
49
        JDBCHelper helper = new PostgreSQLHelper((JDBCConnectionParameters) parameters);
50
        JDBCServerExplorer server = helper.createServerExplorer(
51
                (JDBCServerExplorerParameters) parameters, 
52
                providerServices
53
        );
54
        return server;
55
    }
56
        
57

  
58
    @Override
59
    public JDBCServerExplorerParameters createParameters() {
60
        JDBCServerExplorerParameters params = new PostgreSQLServerExplorerParameters();
61
        return params;    
62
    }
63
    
64
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLStoreProviderFactory.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.gvsig.fmap.dal.DataParameters;
5
import org.gvsig.fmap.dal.exception.InitializeException;
6
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
7
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
8
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
9
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
10
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider;
11
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCStoreProviderFactory;
12

  
13

  
14
public class PostgreSQLStoreProviderFactory extends JDBCStoreProviderFactory {
15
    
16
    public PostgreSQLStoreProviderFactory() {
17
        super(
18
                PostgreSQLLibrary.NAME, 
19
                "PostgreSQL store"
20
        );
21
    }
22

  
23
    @Override
24
    public JDBCStoreProvider createProvider(
25
            DataParameters parameters,
26
            DataStoreProviderServices providerServices
27
    ) throws InitializeException {
28
        JDBCHelper helper = new PostgreSQLHelper((JDBCConnectionParameters) parameters);
29
        JDBCStoreProvider provider = helper.createProvider(
30
                (JDBCStoreParameters) parameters, 
31
                providerServices
32
        );
33
        return provider;
34
    }
35

  
36
    @Override
37
    public JDBCStoreParameters createParameters() {
38
        JDBCStoreParameters params = new PostgreSQLStoreParameters();
39
        return params;
40
    }
41
    
42
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/operations/PostgreSQLFetchFeatureTypeOperation.java
1

  
2
package org.gvsig.postgresql.dal.operations;
3

  
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.ResultSetMetaData;
7
import java.sql.SQLException;
8
import java.sql.Statement;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
import org.apache.commons.lang3.StringUtils;
13
import org.cresques.cts.IProjection;
14
import org.gvsig.expressionevaluator.ExpressionBuilder;
15
import org.gvsig.fmap.dal.DataTypes;
16
import org.gvsig.fmap.dal.exception.DataException;
17
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.EditableFeatureType;
19
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
20
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
21
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
22
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
23
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver;
24
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
25
import org.gvsig.fmap.geom.Geometry;
26
import org.gvsig.fmap.geom.GeometryLocator;
27
import org.gvsig.fmap.geom.GeometryManager;
28
import org.gvsig.fmap.geom.type.GeometryType;
29
import org.gvsig.postgresql.dal.PostgreSQLBuilder;
30

  
31
@SuppressWarnings("UseSpecificCatch")
32
public class PostgreSQLFetchFeatureTypeOperation extends FetchFeatureTypeOperation {
33

  
34
    private static Map<String,GeometryType>databaseGeometryTypes = null;
35
    
36
    public PostgreSQLFetchFeatureTypeOperation(
37
            JDBCHelper helper
38
        ) {
39
        super(helper);
40
    }
41

  
42
    private GeometryType getGT(
43
            GeometryManager manager, 
44
            int type, 
45
            int subtype
46
        ) {
47
        try {
48
            return manager.getGeometryType(type, subtype);
49
        } catch (Exception ex) {
50
            return null;
51
        }
52
    }
53
    
54
    public PostgreSQLFetchFeatureTypeOperation(
55
            JDBCHelper helper,
56
            EditableFeatureType featureType,
57
            TableReference table,
58
            List<String> primaryKeys,
59
            String defaultGeometryColumn,
60
            IProjection crs
61
        ) {
62
        super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs);
63
    }            
64

  
65
    @Override
66
    public void fetch(Connection conn) throws DataException {
67
        super.fetch(conn);
68
    }
69

  
70
    @Override
71
    protected int getDataTypeFromMetadata(
72
            ResultSetMetaData rsMetadata,
73
            int colIndex
74
        ) throws SQLException {
75

  
76
        return super.getDataTypeFromMetadata(rsMetadata, colIndex);
77
    }
78
    
79
    @Override
80
    public String getSQLToRetrievePrimaryKeysFromInformationSchema() throws SQLException {
81
        PostgreSQLBuilder sqlbuilder = (PostgreSQLBuilder) this.createSQLBuilder();
82
        ExpressionBuilder expbuilder = sqlbuilder.expression();
83
        
84
        String column_COLUMN_NAME = "column_name";
85
        String column_CONSTRAINT_TYPE = "constraint_type";
86
        
87
        if( sqlbuilder.getDatabaseVersion().getMajor()<10 ) {
88
            column_COLUMN_NAME = "COLUMN_NAME";
89
            column_CONSTRAINT_TYPE = "CONSTRAINT_TYPE";
90
        }
91
        sqlbuilder.select().column().name(column_COLUMN_NAME);
92
        sqlbuilder.select().column().name(column_CONSTRAINT_TYPE);
93
        sqlbuilder.select().from().custom(
94
                "INFORMATION_SCHEMA.table_constraints t_cons "
95
                + "inner join INFORMATION_SCHEMA.key_column_usage c on "
96
                + "c.constraint_catalog = t_cons.constraint_catalog and "
97
                + "c.table_schema = t_cons.table_schema and "
98
                + "c.table_name = t_cons.table_name and "
99
                + "c.constraint_name = t_cons.constraint_name "
100
        );
101
        sqlbuilder.select().where().set(
102
                expbuilder.like(
103
                        expbuilder.custom("c.TABLE_NAME"), 
104
                        expbuilder.constant(table.getTable())
105
                )
106
        );
107
        if (table.hasSchema()) {
108
            sqlbuilder.select().where().and(
109
                    expbuilder.like(
110
                            expbuilder.custom("c.TABLE_SCHEMA"),
111
                            expbuilder.constant(table.getSchema())
112
                    )
113
            );
114
        }
115
//        if (catalog != null) {
116
//            sqlbuilder.select().where().and(
117
//                    expbuilder.like(
118
//                            expbuilder.custom("c.CONSTRAINT_CATALOG"),
119
//                            expbuilder.constant(catalog)
120
//                    )
121
//            );
122
//        }
123
        sqlbuilder.select().where().and(
124
                expbuilder.eq(
125
                        expbuilder.column(column_CONSTRAINT_TYPE),
126
                        expbuilder.constant("PRIMARY KEY")
127
                )
128
        );
129
        return sqlbuilder.toString();
130
    }
131
        
132
    @Override
133
    protected void fetchGeometryTypeAndSRS(
134
            EditableFeatureAttributeDescriptor attr,
135
            ResultSetMetaData rsMetadata,
136
            int colIndex
137
        ) {
138
        if( attr.getType()!=DataTypes.GEOMETRY ) {
139
            return;
140
        }
141
        try {
142
            JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
143
            ExpressionBuilder expbuilder = sqlbuilder.expression();
144
            
145
            sqlbuilder.select().column().name("f_table_catalog");
146
            sqlbuilder.select().column().name("f_table_schema");
147
            sqlbuilder.select().column().name("f_table_name");
148
            sqlbuilder.select().column().name("f_geometry_column");
149
            sqlbuilder.select().column().name("coord_dimension");
150
            sqlbuilder.select().column().name("srid");
151
            sqlbuilder.select().column().name("type");
152
            sqlbuilder.select().where().set(
153
                    expbuilder.eq(
154
                            expbuilder.column("f_table_name"),
155
                            expbuilder.constant(this.getTable().getTable())
156
                    )
157
            );                
158
            sqlbuilder.select().where().and(
159
                    expbuilder.eq(
160
                            expbuilder.column("f_geometry_column"),
161
                            expbuilder.constant(attr.getName())
162
                    )
163
            );         
164
            sqlbuilder.select().from().table().name("geometry_columns");
165
            Statement st = null;
166
            ResultSet rs = null;
167
            
168
            Integer srsid = null;
169
            String geometryTypeName = null;
170
            try {
171
                st = this.getConnection().createStatement();
172
                rs = JDBCUtils.executeQuery(st, sqlbuilder.toString());
173
                if (rs.next()) {
174
                    srsid = rs.getInt("srid");
175
                    geometryTypeName = rs.getString("type");
176
                }
177
            } finally {
178
                JDBCUtils.closeQuietly(rs);
179
                JDBCUtils.closeQuietly(st);
180
            }
181
            if( !StringUtils.isEmpty(geometryTypeName) ) {
182
                GeometryType gt = getGeometryTypeFromDatabaseTypeName(geometryTypeName);
183
                attr.setGeometryType(gt);
184
            }
185
            if( srsid!=null ) {
186
                SRSSolver srssolver = this.helper.getSRSSolver();
187
                attr.setSRS(srssolver.getProjection(this.getConnection(),srsid));
188
            }
189
        } catch (Exception ex) {
190
            LOGGER.debug("Can't get geometry type and srs from column '"+attr.getName()+"'.",ex);
191
        }
192
    }
193

  
194
    private GeometryType getGeometryTypeFromDatabaseTypeName(String typeName) {
195
        if( databaseGeometryTypes==null ) {
196
            GeometryManager manager = GeometryLocator.getGeometryManager();
197
            databaseGeometryTypes = new HashMap<>();
198
            databaseGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
199
            databaseGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
200
            databaseGeometryTypes.put("POINTM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2DM));
201
            databaseGeometryTypes.put("POINTZM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3DM));
202
            
203
            databaseGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
204
            databaseGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
205
            databaseGeometryTypes.put("LINESTRINGM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2DM));
206
            databaseGeometryTypes.put("LINESTRINGZM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3DM));
207
            
208
            databaseGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
209
            databaseGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
210
            databaseGeometryTypes.put("POLYGONM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2DM));
211
            databaseGeometryTypes.put("POLYGONZM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3DM));
212

  
213
            databaseGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
214
            databaseGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
215
            databaseGeometryTypes.put("MULTIPOINTM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2DM));
216
            databaseGeometryTypes.put("MULTIPOINTZM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3DM));
217

  
218
            databaseGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
219
            databaseGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
220
            databaseGeometryTypes.put("MULTILINESTRINGM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2DM));
221
            databaseGeometryTypes.put("MULTILINESTRINGZM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3DM));
222

  
223
            databaseGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
224
            databaseGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
225
            databaseGeometryTypes.put("MULTIPOLYGONM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2DM));
226
            databaseGeometryTypes.put("MULTIPOLYGONZM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3DM));
227

  
228
            databaseGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
229
            databaseGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
230
            databaseGeometryTypes.put("GEOMETRYM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2DM));
231
            databaseGeometryTypes.put("GEOMETRYZM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3DM));
232
        }
233
        return databaseGeometryTypes.get(typeName);
234
    }
235
    
236
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/operations/PostgreSQLOperationsFactory.java
1

  
2
package org.gvsig.postgresql.dal.operations;
3

  
4
import java.util.List;
5
import org.cresques.cts.IProjection;
6
import org.gvsig.fmap.dal.feature.EditableFeatureType;
7
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
8
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
9
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase;
10

  
11

  
12
public class PostgreSQLOperationsFactory extends OperationsFactoryBase {
13
    
14
    public PostgreSQLOperationsFactory(JDBCHelper helper) {
15
        super(helper);
16
    }
17

  
18
    @Override
19
    public FetchFeatureTypeOperation createFetchFeatureType(EditableFeatureType type, TableReference table, List<String> primaryKeys, String defaultGeometryField, IProjection crs) {
20
        return new PostgreSQLFetchFeatureTypeOperation(
21
                helper, type, table, primaryKeys, 
22
                defaultGeometryField, crs
23
        );
24
    }  
25
    
26
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLStoreParameters.java
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
package org.gvsig.postgresql.dal;
23

  
24
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
25
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
26

  
27
public class PostgreSQLStoreParameters extends JDBCStoreParameters implements PostgreSQLConnectionParameters {
28

  
29
    private final PostgreSQLConnectionParametersHelper helper;
30
    
31
    public PostgreSQLStoreParameters() {
32
        super(
33
                PostgreSQLLibrary.NAME + "StoreParameters",
34
                PostgreSQLLibrary.NAME
35
        );
36
        this.helper = new PostgreSQLConnectionParametersHelper(this);
37
    }
38

  
39
    @Override
40
    public String getUrl() {
41
        return this.helper.getUrl();
42
    }
43
    
44
    @Override
45
    public void validate() throws ValidateDataParametersException {
46
        this.helper.validate();
47
        super.validate();
48
    }
49

  
50
    @Override
51
    public boolean getUseSSL() {
52
        return this.helper.getUseSSL();
53
    }
54

  
55
    @Override
56
    public int getMaxIdle() {
57
        return this.helper.getMaxIdle();
58
    }
59

  
60
    public void setUseSSL(boolean v) {
61
        this.helper.setUseSSL(v);
62
    }
63

  
64
    @Override
65
    public int getNetworkTimeout() {
66
        return this.helper.getNetworkTimeout();
67
    }
68
    
69
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLConnectionParameters.java
1
package org.gvsig.postgresql.dal;
2

  
3
import org.gvsig.fmap.dal.resource.db.DBParameters;
4
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
5

  
6
public interface PostgreSQLConnectionParameters extends JDBCConnectionParameters, DBParameters {
7

  
8
    public static final String USESSL_PARAMTER_NAME = "UseSSL";
9
    public static final String MAXIDLE_PARAMTER_NAME = "maxIdle";
10
    public static final String NETWORK_TIMEOUT = "networkTimeout";
11

  
12
    public boolean getUseSSL();
13

  
14
    public int getMaxIdle();
15

  
16
    public int getNetworkTimeout();
17
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLConnectionParametersHelper.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.fmap.dal.DataParameters;
6
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
7
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
8

  
9

  
10
public class PostgreSQLConnectionParametersHelper {
11

  
12
    private final JDBCConnectionParameters parameters;
13
    
14
    public PostgreSQLConnectionParametersHelper(JDBCConnectionParameters parameters) {
15
        this.parameters = parameters;
16
    }
17

  
18
    public String getUrl() {
19
        String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME);
20
        if( StringUtils.isEmpty(url) ) {
21
            url = PostgreSQLHelper.getConnectionURL((PostgreSQLConnectionParameters) this.parameters);
22
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME,url);
23
        }
24
        return url;
25
    }
26
    
27
    public void validate() throws ValidateDataParametersException {
28
        if (this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null) {
29
            this.setDynValue(
30
                JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME,
31
                PostgreSQLHelper.POSTGRESQL_JDBC_DRIVER
32
            );
33
        }
34
        if( this.getDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME)==null ) {
35
            this.setDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME, 5432);
36
        }
37
		if ( StringUtils.isEmpty((CharSequence) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME))) {
38
            String url = PostgreSQLHelper.getConnectionURL(
39
                parameters.getHost(),
40
                parameters.getPort(),
41
                parameters.getDBName()
42
            );
43
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
44
		}
45
    }
46

  
47
    private Object getDynValue(String name) {
48
        return ((DataParameters)this.parameters).getDynValue(name);
49
    }
50
    
51
    private void setDynValue(String name, Object value) {
52
        ((DataParameters)this.parameters).setDynValue(name,value);
53
    }
54
    
55
    public int getMaxIdle() {
56
        return (int) this.getDynValue(PostgreSQLConnectionParameters.MAXIDLE_PARAMTER_NAME);
57
    }
58

  
59
    public boolean getUseSSL() {
60
        return (boolean) this.getDynValue(PostgreSQLConnectionParameters.USESSL_PARAMTER_NAME);
61
    }
62

  
63
    public void setUseSSL(boolean v) {
64
        this.setDynValue(PostgreSQLConnectionParameters.USESSL_PARAMTER_NAME, v);
65
    }
66

  
67
    public int getNetworkTimeout() {
68
        return (int) this.getDynValue(PostgreSQLConnectionParameters.NETWORK_TIMEOUT);
69
    }
70

  
71
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLLibrary.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.gvsig.fmap.dal.DALLibrary;
5
import org.gvsig.fmap.dal.DALLocator;
6
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
7
import org.gvsig.fmap.dal.store.db.DBHelper;
8
import org.gvsig.fmap.dal.store.jdbc2.JDBCLibrary;
9
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCStoreProviderBase;
10
import org.gvsig.metadata.exceptions.MetadataException;
11
import org.gvsig.tools.library.AbstractLibrary;
12
import org.gvsig.tools.library.LibraryException;
13

  
14

  
15
public class PostgreSQLLibrary extends AbstractLibrary {
16

  
17
    public static final String NAME = "PostgreSQL";
18

  
19
    @Override
20
    public void doRegistration() {
21
        registerAsServiceOf(DALLibrary.class);
22
        require(JDBCLibrary.class);
23
    }
24

  
25
    @Override
26
    protected void doInitialize() throws LibraryException {
27
    }
28

  
29
    @Override
30
    protected void doPostInitialize() throws LibraryException {
31
        LibraryException ex = null;
32

  
33
        DataManagerProviderServices dataman = 
34
                (DataManagerProviderServices) DALLocator.getDataManager();
35

  
36
        try {
37
            Class.forName(PostgreSQLHelper.POSTGRESQL_JDBC_DRIVER);
38
        } catch(Throwable th) {
39
            PostgreSQLHelper.LOGGER.warn("Can't load PostgreSQL JDBC Driver.",th);
40
        }
41
        
42
        DBHelper.registerParametersDefinition(
43
                NAME + "StoreParameters",
44
                PostgreSQLStoreParameters.class,
45
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
46
        );
47
        DBHelper.registerParametersDefinition(
48
                NAME + "NewStoreParameters",
49
                PostgreSQLNewStoreParameters.class,
50
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
51
        );
52
        DBHelper.registerParametersDefinition(
53
                NAME + "ServerExplorerParameters",
54
                PostgreSQLServerExplorerParameters.class,
55
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
56
        );
57
//        DBHelper.registerParametersDefinition(
58
//                NAME + "ResourceParameters",
59
//                PostgreSQLResourceParameters.class,
60
//                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
61
//        );
62
        try {
63
            DBHelper.registerMetadataDefinition(
64
                NAME,
65
                JDBCStoreProviderBase.class,
66
                dataman.getResourceAsStream(this, NAME + "Metadata.xml")
67
            );
68
        } catch (MetadataException e) {
69
            ex = new LibraryException(this.getClass(), e);
70
        }
71

  
72
//        ResourceManagerProviderServices resman = (ResourceManagerProviderServices) DALLocator
73
//                .getResourceManager();
74
//
75
//        if (!resman.getResourceProviders().contains(NAME)) {
76
//            resman.register(NAME,
77
//                "Resource for " + NAME,
78
//                PostgreSQLResource.class,
79
//                PostgreSQLResourceParameters.class
80
//            );
81
//        }
82

  
83
        if (!dataman.getStoreProviderRegister().exits(NAME)) {
84
            dataman.registerStoreProviderFactory(new PostgreSQLStoreProviderFactory());
85
        }
86

  
87
        if (!dataman.getServerExplorerRegister().exits(NAME)) {
88
            dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory());
89
        }
90

  
91
        // Por compatibilidad con gvSIG 2.3 registramos otra vez la factoria con
92
        // el nombre que tenia antes.
93
        if (!dataman.getServerExplorerRegister().exits("PostgreSQLExplorer")) {
94
            dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory("PostgreSQLExplorer"));
95
        }
96
        
97
        if (ex != null) {
98
            throw ex;
99
        }
100
    }
101

  
102
}
tags/org.gvsig.postgresql-2.0.155/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLBuilder.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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

  
25
package org.gvsig.postgresql.dal;
26

  
27
import java.sql.Connection;
28
import java.sql.DatabaseMetaData;
29
import java.text.MessageFormat;
30
import java.util.ArrayList;
31
import java.util.List;
32
import org.apache.commons.lang3.StringUtils;
33
import org.gvsig.expressionevaluator.Formatter;
34
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
35
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
36
import org.gvsig.postgresql.dal.expressionbuilderformatter.PostgreSQLFormatter;
37

  
38
@SuppressWarnings("UseSpecificCatch")
39
public class PostgreSQLBuilder extends JDBCSQLBuilderBase {
40

  
41
    protected Formatter formatter = null;
42
    
43
    public static class Version {
44

  
45
        private final int major;
46
        private final int minor;
47
        
48
        public Version(int major, int minor) {
49
            this.major = major;
50
            this.minor = minor;
51
        }
52

  
53
        public int getMajor() {
54
            return major;
55
        }
56

  
57
        public int getMinor() {
58
            return minor;
59
        }
60
        
61
    }
62
    private Version databaseVersion = null;
63
    
64
    public Version getDatabaseVersion() {
65
        if( databaseVersion == null ) {
66
            Connection conn = null;
67
            try {
68
                conn = this.getHelper().getConnection();
69
                DatabaseMetaData metadata = conn.getMetaData();
70
                databaseVersion = new Version(
71
                    metadata.getDatabaseMajorVersion(),
72
                    metadata.getDatabaseMinorVersion()
73
                );
74
            } catch (Exception ex) {
75
                databaseVersion = new Version(0,0);
76
            } finally {
77
                this.getHelper().closeConnectionQuietly(conn);
78
            }
79
        }
80
        return databaseVersion;
81
    }
82
    
83
    public PostgreSQLBuilder(JDBCHelper helper) {
84
        super(helper);
85
        this.defaultSchema = "public";
86
        this.supportSchemas = true;
87
        this.allowAutomaticValues = true;
88
        this.geometrySupportType = this.helper.getGeometrySupportType();
89
        this.hasSpatialFunctions = this.helper.hasSpatialFunctions();
90

  
91
        this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null;
92
        this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null;
93

  
94
        this.STMT_UPDATE_TABLE_STATISTICS_table = "ANALYZE {0}";
95

  
96
//        config.set(SQLConfig.ST_GeomFromEWKB, "ST_GeomFromEWKB({0}, {1})");
97
//        config.set(SQLConfig.ST_AsEWKB, "ST_AsEWKB(({0}))");        
98
//        config.set(SQLConfig.ST_ExtentAggregate, "ST_Extent({0})");        
99
//        config.set(SQLConfig.ST_UnionAggregate, "ST_Union({0})");
100
        
101
//        config.set(SQLConfig.isNull, "( ({0}) ISNULL )");
102
//        config.set(SQLConfig.notIsNull, "( ({0}) NOT NULL )");
103

  
104
    }
105
    
106
    public class PostgreSQLUpdateTableStatisticsBuilderBase extends UpdateTableStatisticsBuilderBase {
107
        @Override
108
        public List<String> toStrings() {
109
            List<String> sqls = new ArrayList<>();
110
            
111
            if( !StringUtils.isBlank(STMT_UPDATE_TABLE_STATISTICS_table) ) {
112
                // In postGIS, UpdateLayerStatistics function, don't allow to 
113
                // use the database name in the table name.
114
                String name = as_identifier(this.table.getName());
115
                if( table.has_schema()) {
116
                    name = as_identifier(this.table.getSchema()) + "." + name;
117
                }
118
                String sql = MessageFormat.format(
119
                        STMT_UPDATE_TABLE_STATISTICS_table,
120
                        name
121
                    );
122
                if( !StringUtils.isEmpty(sql) ) {
123
                    sqls.add(sql);
124
                }
125
            }
126
            return sqls;
127
        }        
128
    }
129
    protected class PostgreSQLCreateIndexBuilder extends CreateIndexBuilderBase {
130

  
131
        @Override
132
        public List<String> toStrings(Formatter formatter) {
133
            StringBuilder builder = new StringBuilder();
134
            builder.append("CREATE ");
135
//            if( this.isUnique ) {
136
//                builder.append("UNIQUE ");
137
//            }
138
            builder.append("INDEX ");
139
            if( this.ifNotExist ) {
140
                Version version = getDatabaseVersion();
141
                if( version.getMajor()>=9 && version.getMinor()>=5 ) {
142
                    builder.append("IF NOT EXISTS ");
143
                }
144
            }
145
            builder.append(as_identifier(this.indexName));
146
            builder.append(" ON ");
147
            builder.append(this.table.toString(formatter));
148
            if( this.isSpatial ) {
149
                builder.append(" USING GIST ");
150
            }
151
            builder.append(" ( ");
152
            boolean is_first_column = true;
153
            for( String column : this.columns) {
154
                if( is_first_column ) {
155
                    is_first_column = false;
156
                } else {
157
                    builder.append(", ");
158
                }
159
                builder.append(as_identifier(column));
160
            }
161
            builder.append(" )");
162
            
163
            List<String> sqls = new ArrayList<>();
164
            sqls.add(builder.toString());
165
            return sqls;
166
        }
167
        
168
    }
169
    
170
    protected class PostgreSQLCreateTableBuilder extends CreateTableBuilderBase {
171

  
172
        @Override
173
       public List<String> toStrings(Formatter formatter) {
174
           // 
175
           // https://www.postgresql.org/docs/9.1/static/sql-createtable.html
176
           //
177
            List<String> sqls = new ArrayList<>();
178
            StringBuilder builder = new StringBuilder();
179

  
180
            builder.append("CREATE TABLE ");
181
            builder.append(this.table.toString(formatter));
182
            builder.append(" (");
183
            boolean first = true;
184
            for (ColumnDescriptor column : columns) {
185
                if( column.isGeometry() ) {
186
                    continue;
187
                }
188
                if (first) {
189
                    first = false;
190
                } else {
191
                    builder.append(", ");
192
                }
193
                builder.append(as_identifier(column.getName()));
194
                builder.append(" ");
195
                if( column.isAutomatic() ) {
196
                    builder.append(" SERIAL");
197
                } else {
198
                    builder.append(sqltype(
199
                            column.getType(), 
200
                            column.getSize(),
201
                            column.getPrecision(), 
202
                            column.getScale(), 
203
                            column.getGeometryType(),
204
                            column.getGeometrySubtype()
205
                    ));
206
                    if (column.getDefaultValue() == null) {
207
                        if (column.allowNulls()) {
208
                            builder.append(" DEFAULT NULL");
209
                        }
210
                    } else {
211
                        builder.append(" DEFAULT '");
212
                        builder.append(column.getDefaultValue().toString());
213
                        builder.append("'");
214
                    }
215
                    if (column.allowNulls()) {
216
                        builder.append(" NULL");
217
                    } else {
218
                        builder.append(" NOT NULL");
219
                    }
220
                }
221
                if (column.isPrimaryKey()) {
222
                    builder.append(" PRIMARY KEY");
223
                }
224
            }
225
            builder.append(" )");
226
            sqls.add(builder.toString());
227

  
228
            String AddGeometryColumn = "SELECT AddGeometryColumn({0} , {1} , {2}, {3,number,#######} , {4} , {5}, {6})";
229
            for (ColumnDescriptor column : columns) {
230
                if( column.isGeometry() ) {
231
                    String sql = MessageFormat.format(
232
                        AddGeometryColumn,
233
                        as_string(this.table.has_schema()?this.table.getSchema():"public"),
234
                        as_string(this.table.getName()),
235
                        as_string(column.getName()),
236
                        column.getGeometrySRSId(),
237
                        as_string(sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype())),
238
                        as_string(sqlgeometrydimension(column.getGeometryType(), column.getGeometrySubtype())),
239
                        as_string(column.allowNulls())
240
                    );
241
                    sqls.add(sql);
242
                }
243
            }
244
            return sqls;
245
        }
246
    }
247

  
248
    public class PostgreSQLSelectBuilderBase extends SelectBuilderBase {
249
        
250
        @Override
251
        protected boolean isValid(StringBuilder message) {
252
            if( message == null ) {
253
                message = new StringBuilder();
254
            }
255
            if( this.has_offset() && !this.has_order_by() ) {
256
                // Algunos gestores de BBDD requieren que se especifique un
257
                // orden para poder usar OFFSET. Como eso parece buena idea para
258
                // asegurar que siempre tengamos los mismo resultados, lo exijimos
259
                // siempre.
260
                message.append("Can't use OFFSET without an ORDER BY.");
261
                return false;
262
            }
263
            return true;
264
        }        
265
        
266
        @Override
267
        public String toString(Formatter formatter) {
268
            //
269
            // https://www.postgresql.org/docs/9.1/static/sql-select.html
270
            //
271
            StringBuilder builder = new StringBuilder();
272
            if( !isValid(builder) ) {
273
                throw new IllegalStateException(builder.toString());
274
            }
275
            builder.append("SELECT ");
276
            if( this.distinct ) {
277
                builder.append("DISTINCT ");
278
            }
279
            boolean first = true;
280
            for (SelectColumnBuilder column : columns) {
281
                if (first) {
282
                    first = false;
283
                } else {
284
                    builder.append(", ");
285
                }
286
                builder.append(column.toString(formatter));
287
            }
288

  
289
            if ( this.has_from() ) {
290
                builder.append(" FROM ");
291
                builder.append(this.from.toString(formatter));
292
            }
293
            if( this.has_group_by() ) {
294
                builder.append(" GROUP BY ");
295
                builder.append(this.groupColumn.get(0).toString(formatter));
296
                for (int i = 1; i < groupColumn.size(); i++) {
297
                    builder.append(", ");
298
                    builder.append(this.groupColumn.get(i).toString(formatter));
299
                }
300
            }            
301
            if ( this.has_where() ) {
302
                builder.append(" WHERE ");
303
                builder.append(this.where.toString(formatter));
304
            }
305
            
306
            if( this.has_order_by() ) {
307
                builder.append(" ORDER BY ");
308
                first = true;
309
                for (OrderByBuilder item : this.order_by) {
310
                    if (first) {
311
                        first = false;
312
                    } else {
313
                        builder.append(", ");
314
                    }
315
                    builder.append(item.toString(formatter));
316
                }   
317
            }
318
            
319
            if ( this.has_limit() && this.has_offset() ) {
320
                builder.append(" OFFSET ");
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff