Revision 908

View differences:

tags/org.gvsig.postgresql-2.0.179/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4

  
5
  <modelVersion>4.0.0</modelVersion>
6
  <artifactId>org.gvsig.postgresql</artifactId>
7
  <version>2.0.179</version>
8
  <packaging>pom</packaging>
9
  <name>${project.artifactId}</name>
10
  <description>PostgreSQL support fort DAL and gvSIG</description>
11
  <parent>
12
      <groupId>org.gvsig</groupId>
13
      <artifactId>org.gvsig.desktop</artifactId>
14
      <version>2.0.340</version>
15
  </parent>
16

  
17
  <url>https://devel.gvsig.org/redmine/projects/gvsig-postgresql</url>
18
  <scm>
19
      <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-postgresql/tags/org.gvsig.postgresql-2.0.179</connection>
20
      <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-postgresql/tags/org.gvsig.postgresql-2.0.179</developerConnection>
21
      <url>https://devel.gvsig.org/redmine/projects/gvsig-postgresql/repository/show/tags/org.gvsig.postgresql-2.0.179</url>
22
  </scm>
23

  
24
    <repositories>
25
        <repository>
26
            <id>gvsig-public-http-repository</id>
27
            <name>gvSIG maven public HTTP repository</name>
28
            <url>http://devel.gvsig.org/m2repo/j2se</url>
29
            <releases>
30
                <enabled>true</enabled>
31
                <updatePolicy>daily</updatePolicy>
32
                <checksumPolicy>warn</checksumPolicy>
33
            </releases>
34
            <snapshots>
35
                <enabled>true</enabled>
36
                <updatePolicy>daily</updatePolicy>
37
                <checksumPolicy>warn</checksumPolicy>
38
            </snapshots>
39
        </repository>
40
    </repositories>
41
  
42
  
43
	<build>
44
		<plugins>
45
			<plugin>
46
				<groupId>org.apache.maven.plugins</groupId>
47
				<artifactId>maven-release-plugin</artifactId>
48
				<configuration>
49
					<tagBase>https://devel.gvsig.org/svn/gvsig-postgresql/tags/</tagBase>
50
					<goals>deploy</goals>
51
				</configuration>
52
			</plugin>
53
		</plugins>
54
	</build>
55

  
56

  
57
  <dependencyManagement>
58
      <dependencies>
59
          <dependency>
60
            <groupId>org.gvsig</groupId>
61
            <artifactId>org.gvsig.postgresql.provider</artifactId>
62
            <version>2.0.179</version>
63
          </dependency>
64
          <dependency>
65
            <groupId>org.gvsig</groupId>
66
            <artifactId>org.gvsig.postgresql.app.mainplugin</artifactId>
67
            <version>2.0.179</version>
68
          </dependency>
69
        <dependency>
70
            <groupId>org.postgresql</groupId>
71
            <artifactId>postgresql</artifactId>
72
            <version>42.2.6</version>
73
        </dependency>
74
        <dependency>
75
          <groupId>org.apache.commons</groupId>
76
          <artifactId>commons-dbcp2</artifactId>
77
          <version>2.7.0</version>
78
        </dependency>   
79
        <dependency>
80
            <groupId>org.apache.commons</groupId>
81
            <artifactId>commons-pool2</artifactId>
82
            <version>2.7.0</version>
83
        </dependency>
84
      </dependencies>
85
  </dependencyManagement>
86
 
87
  <modules>
88
    <module>org.gvsig.postgresql.app</module>
89
    <module>org.gvsig.postgresql.provider</module>
90
  </modules>
91

  
92
</project>
tags/org.gvsig.postgresql-2.0.179/org.gvsig.postgresql.provider/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4

  
5
  <modelVersion>4.0.0</modelVersion>
6
  <artifactId>org.gvsig.postgresql.provider</artifactId>
7
  <packaging>jar</packaging>
8
  <name>${project.artifactId}</name>
9
  <description>DAL provider for PostgeSQL files</description>
10
  <parent>
11
    <groupId>org.gvsig</groupId>
12
    <artifactId>org.gvsig.postgresql</artifactId>
13
    <version>2.0.179</version>
14
  </parent>
15
  <dependencies>
16
    <dependency>
17
      <groupId>com.googlecode.log4jdbc</groupId>
18
      <artifactId>log4jdbc</artifactId>
19
      <scope>compile</scope>
20
    </dependency>
21
    <dependency>
22
      <groupId>org.gvsig</groupId>
23
      <artifactId>org.gvsig.tools.lib</artifactId>
24
      <scope>compile</scope>
25
    </dependency>
26
    <dependency>
27
      <groupId>org.gvsig</groupId>
28
      <artifactId>org.gvsig.projection.api</artifactId>
29
      <scope>compile</scope>
30
    </dependency>
31
    <dependency>
32
      <groupId>org.gvsig</groupId>
33
      <artifactId>org.gvsig.fmap.dal.api</artifactId>
34
      <scope>compile</scope>
35
    </dependency>
36
    
37
    <dependency>
38
      <groupId>org.gvsig</groupId>
39
      <artifactId>org.gvsig.fmap.dal.db.jdbc</artifactId>
40
      <scope>compile</scope>
41
    </dependency>
42

  
43
    <dependency>
44
      <groupId>org.gvsig</groupId>
45
      <artifactId>org.gvsig.fmap.dal.db.lib</artifactId>
46
      <scope>compile</scope>
47
    </dependency>
48
    
49
    <dependency>
50
      <groupId>org.gvsig</groupId>
51
      <artifactId>org.gvsig.fmap.dal.spi</artifactId>
52
      <scope>compile</scope>
53
    </dependency>
54
    <dependency>
55
      <groupId>org.gvsig</groupId>
56
      <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
57
      <scope>compile</scope>
58
    </dependency>
59
    <dependency>
60
      <groupId>org.gvsig</groupId>
61
      <artifactId>org.gvsig.fmap.geometry.api</artifactId>
62
      <scope>compile</scope>
63
    </dependency>
64
    <!--
65
    <dependency>
66
        <groupId>org.gvsig</groupId>
67
        <artifactId>org.gvsig.fmap.geometry.operation</artifactId>
68
        <scope>compile</scope>
69
    </dependency>
70
    -->
71
    <dependency>
72
      <groupId>org.gvsig</groupId>
73
      <artifactId>org.gvsig.timesupport.lib.api</artifactId>
74
      <scope>compile</scope>
75
    </dependency>  
76
    <dependency>
77
      <groupId>org.gvsig</groupId>
78
      <artifactId>org.gvsig.utils</artifactId>
79
      <scope>compile</scope>
80
    </dependency>
81
    <dependency>
82
      <groupId>org.postgresql</groupId>
83
      <artifactId>postgresql</artifactId>
84
      <scope>compile</scope>
85
    </dependency>
86
    <dependency>
87
        <groupId>org.apache.commons</groupId>
88
        <artifactId>commons-dbcp2</artifactId>
89
    </dependency>
90
    <dependency>
91
        <groupId>org.apache.commons</groupId>
92
        <artifactId>commons-pool2</artifactId>
93
    </dependency>
94
    <dependency>
95
      <groupId>commons-collections</groupId>
96
      <artifactId>commons-collections</artifactId>
97
      <scope>compile</scope>
98
    </dependency>
99

  
100

  
101
    <!-- TESTS -->
102
    <dependency>
103
      <groupId>org.gvsig</groupId>
104
      <artifactId>org.gvsig.tools.lib</artifactId>
105
      <type>test-jar</type>
106
      <scope>test</scope>
107
    </dependency>
108
    <dependency>
109
      <groupId>org.gvsig</groupId>
110
      <artifactId>org.gvsig.compat.se</artifactId>
111
      <scope>test</scope>
112
    </dependency>
113
    <dependency>
114
      <groupId>org.gvsig</groupId>
115
      <artifactId>org.gvsig.fmap.dal.impl</artifactId>
116
      <scope>test</scope>
117
    </dependency>
118
    <dependency>
119
      <groupId>org.gvsig</groupId>
120
      <artifactId>org.gvsig.fmap.dal.file.csv</artifactId>
121
      <scope>test</scope>
122
    </dependency>
123
    <dependency>
124
      <groupId>org.gvsig</groupId>
125
      <artifactId>org.gvsig.tools.util.impl</artifactId>
126
      <scope>test</scope>
127
    </dependency>
128
    <dependency>
129
      <groupId>org.gvsig</groupId>
130
      <artifactId>org.gvsig.tools.swing.impl</artifactId>
131
      <scope>test</scope>
132
    </dependency>
133
    <dependency>
134
      <groupId>org.gvsig</groupId>
135
      <artifactId>${org.gvsig.fmap.geometry.impl}</artifactId>
136
      <scope>test</scope>
137
    </dependency>    
138
    <dependency>
139
      <groupId>org.gvsig</groupId>
140
      <artifactId>org.gvsig.expressionevaluator.geometry.lib.impl</artifactId>
141
      <scope>test</scope>
142
    </dependency>    
143
    <dependency>
144
      <groupId>org.gvsig</groupId>
145
      <artifactId>${org.gvsig.proj.lib.impl}</artifactId>
146
      <scope>test</scope>
147
    </dependency>
148
    <dependency>
149
      <groupId>org.gvsig</groupId>
150
      <artifactId>org.gvsig.projection.cresques.impl</artifactId>
151
      <scope>test</scope>
152
    </dependency>
153
    <dependency>
154
      <groupId>org.gvsig</groupId>
155
      <artifactId>org.gvsig.timesupport.lib.impl</artifactId>
156
      <scope>test</scope>
157
    </dependency>
158
    <dependency>
159
      <groupId>org.gvsig</groupId>
160
      <artifactId>org.gvsig.metadata.lib.basic.impl</artifactId>
161
      <scope>test</scope>
162
    </dependency>      
163
    
164
    
165
    <dependency>
166
      <artifactId>junit</artifactId>
167
      <groupId>junit</groupId>
168
      <scope>test</scope>
169
    </dependency>
170
  </dependencies>
171

  
172
  <build>
173
    <plugins>
174

  
175
      <!-- Skip test execution ? -->
176
      <plugin>
177
        <groupId>org.apache.maven.plugins</groupId>
178
        <artifactId>maven-surefire-plugin</artifactId>
179
        <configuration>
180
          <skipTests>false</skipTests>
181
        </configuration>
182
      </plugin>
183
        
184
      <!-- Skip test compilation ? -->
185
      <plugin>
186
        <groupId>org.apache.maven.plugins</groupId>
187
        <artifactId>maven-compiler-plugin</artifactId>
188
        <executions>
189
          <execution>
190
            <id>default-testCompile</id>
191
            <phase>process-test-sources</phase>
192
            <goals>
193
              <goal>testCompile</goal>
194
            </goals>
195
            <configuration>
196
              <skip>false</skip>
197
            </configuration>
198
          </execution>
199
        </executions>
200
      </plugin>
201

  
202
      <!-- Ignore test execution failure ? -->
203
      <plugin>
204
        <groupId>org.apache.maven.plugins</groupId>
205
        <artifactId>maven-surefire-plugin</artifactId>
206
        <configuration>
207
          <testFailureIgnore>true</testFailureIgnore>
208
        </configuration>
209
      </plugin>
210

  
211
    </plugins>
212
  </build>
213

  
214
</project>
tags/org.gvsig.postgresql-2.0.179/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLHelper.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import java.io.IOException;
5
import java.sql.Connection;
6
import java.sql.SQLException;
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.JDBCUtils;
18
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
19
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider;
20
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
21
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
22
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase;
23
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverDumb;
24
import org.gvsig.postgresql.dal.operations.PostgreSQLOperationsFactory;
25
import org.gvsig.tools.dispose.Disposable;
26
import org.gvsig.tools.exception.BaseException;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

  
30
public class PostgreSQLHelper extends JDBCHelperBase {
31

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

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

  
57
    private static class PostgreSQLConnectionProvider implements Disposable, ConnectionProvider {
58

  
59
        private static boolean needRegisterDriver = true;
60

  
61
        private BasicDataSource dataSource = null;
62

  
63
        private PostgreSQLConnectionParameters connectionParameters;
64

  
65
        public PostgreSQLConnectionProvider(PostgreSQLConnectionParameters connectionParameters) {
66
            this.connectionParameters = connectionParameters;
67
        }
68

  
69
        public Connection getConnection() throws SQLException {
70
            if (this.dataSource == null) {
71
                this.dataSource = this.createDataSource();               
72
            }
73
            if( LOGGER.isDebugEnabled() ) {
74
                LOGGER.debug("getConnection:\n" + getStatusInformation());
75
            }
76
            Connection conn;
77
            try {
78
                conn = this.dataSource.getConnection();
79
            } catch(Throwable ex) {
80
                LOGGER.debug("Error getting connection from pool.",ex);
81
                throw ex;
82
            }
83
            try {
84
                conn.setNetworkTimeout(null, this.connectionParameters.getNetworkTimeout());
85
            } catch(Throwable ex) {
86
                LOGGER.warn("Error setting the network timeout.",ex);
87
            }
88
            if( LOGGER.isDebugEnabled() ) {
89
                LOGGER.debug("Created connection: {}\n  NumActive: {}\n  NumIdle: {}",
90
                    new Object[] {
91
                        conn.hashCode(), 
92
                        this.dataSource.getNumActive(),
93
                        this.dataSource.getNumIdle()
94
                    }
95
                );
96
            }
97
            return conn;
98
        }
99
        
100
        public String getStatusInformation() {
101
            StringBuilder builder = new StringBuilder();
102
            builder.append("BasicDataSource pool status:\n");
103
            builder.append("  Connection URL: '").append(this.dataSource.getUrl()).append("'\n");
104
            if( this.dataSource.getInitialSize()>0 ) {
105
                builder.append("  InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n");
106
            }
107
            if( this.dataSource.isPoolPreparedStatements() ) {
108
                builder.append("  PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n");
109
                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");
110
            }
111
            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");
112
            builder.append("  MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n");
113
            builder.append("  NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n");
114
            builder.append("  NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n");
115
            return builder.toString();
116
        }
117

  
118
        private BasicDataSource createDataSource() throws SQLException {
119
            if (!this.isRegistered()) {
120
                this.registerDriver();
121
            }
122
            PostgreSQLConnectionParameters params = connectionParameters;
123

  
124
            BasicDataSource ds = new BasicDataSource();
125
            ds.setMaxIdle(params.getMaxIdle());
126
            ds.setDriverClassName(params.getJDBCDriverClassName());
127
            if( params.getUseSSL() ) {
128
                String s = BooleanUtils.toStringTrueFalse(params.getUseSSL());
129
                ds.addConnectionProperty("ssl", s );
130
            }
131
            if( !StringUtils.isEmpty(params.getUser()) ) {
132
                ds.setUsername(params.getUser());
133
            }
134
            if( !StringUtils.isEmpty(params.getPassword()) ) {
135
                ds.setPassword(params.getPassword());
136
            }
137
            ds.setUrl(params.getUrl());
138
            
139
            ds.setMinEvictableIdleTimeMillis(20*1000);
140
            ds.setTimeBetweenEvictionRunsMillis(20*1000);
141
            ds.setMinIdle(0);
142
            
143
            ds.setMaxWaitMillis(60L * 1000);
144
            return ds;
145
        }
146

  
147
        private boolean isRegistered() {
148
            return needRegisterDriver;
149
        }
150

  
151
        public void registerDriver() throws SQLException {
152
            String className = this.connectionParameters.getJDBCDriverClassName();
153
            if (className == null) {
154
                return;
155
            }
156
            try {
157
                Class theClass = Class.forName(className);
158
                if (theClass == null) {
159
                    throw new JDBCDriverClassNotFoundException(PostgreSQLLibrary.NAME, className);
160
                }
161
            } catch (Exception e) {
162
                throw new SQLException("Can't register JDBC driver '" + className + "'.", e);
163
            }
164
            needRegisterDriver = false;
165
        }
166

  
167
        @Override
168
        public void dispose() {
169
            if( this.dataSource!=null ) {
170
                try {
171
                    this.dataSource.close();
172
                } catch (SQLException ex) {
173
                    LOGGER.warn("Can't close BasicDataSource", ex);
174
                }
175
                this.dataSource = null;
176
            }
177
            this.connectionParameters = null;
178
        }
179

  
180
        @Override
181
        public String getStatus() {
182
            StringBuilder builder = new StringBuilder();
183
            builder.append("Pool: ");
184
            builder.append(JDBCUtils.getHexId(dataSource));
185
            builder.append(" Actives: ");
186
            builder.append(dataSource.getNumActive());
187
            builder.append("/");
188
            builder.append(dataSource.getMaxTotal()); //.getMaxActive());
189
            builder.append(" idle: ");
190
            builder.append(dataSource.getNumIdle());
191
            builder.append("/");
192
            builder.append(dataSource.getMinIdle());
193
            builder.append(":");
194
            builder.append(dataSource.getMaxIdle());
195
            return builder.toString();
196
        }
197

  
198
    }
199

  
200
    private ConnectionProvider connectionProvider = null;
201
   
202
    /**
203
     * Constructor for use only for testing purposes.
204
     * 
205
     * @param connectionParameters
206
     * @param connectionProvider
207
     */
208
    public PostgreSQLHelper(PostgreSQLConnectionParameters connectionParameters, ConnectionProvider connectionProvider) { 
209
        super(connectionParameters);
210
        this.srssolver = new SRSSolverDumb(this);
211
        this.connectionProvider = connectionProvider;
212
    }
213
  
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 PostgreSQLConnectionProvider(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
            LOGGER.debug("[" + JDBCUtils.getConnId(connection) + "] closeConnection " + this.connectionProvider.getStatus());
249
        }
250
        super.closeConnection(connection);
251
    }
252
    
253
    @Override
254
    public PostgreSQLConnectionParameters getConnectionParameters() {
255
        return (PostgreSQLConnectionParameters) super.getConnectionParameters();
256
    }
257
    
258
    @Override
259
    public String getConnectionURL() {
260
        return getConnectionURL(this.getConnectionParameters());
261
    }
262

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

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

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

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

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

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

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

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

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

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

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

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

  
338
    @Override
339
    public JDBCServerExplorerParameters createServerExplorerParameters() {
340
        return new PostgreSQLServerExplorerParameters();
341
    }
342
}
tags/org.gvsig.postgresql-2.0.179/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.179/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLStoreProviderFactory.java
1
package org.gvsig.postgresql.dal;
2

  
3
import java.util.Arrays;
4
import java.util.Collections;
5
import java.util.HashSet;
6
import java.util.Set;
7
import java.util.function.Function;
8
import org.gvsig.fmap.dal.DataParameters;
9
import static org.gvsig.fmap.dal.DataStoreProviderFactory.NO;
10
import static org.gvsig.fmap.dal.DataStoreProviderFactory.UNKNOWN;
11
import static org.gvsig.fmap.dal.DataStoreProviderFactory.YES;
12
import org.gvsig.fmap.dal.exception.InitializeException;
13
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
14
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
15
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
16
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
17
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider;
18
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCStoreProviderFactory;
19

  
20
public class PostgreSQLStoreProviderFactory extends JDBCStoreProviderFactory {
21

  
22
    private static final Set<String> ALLOWED_FUNCTIONS = new HashSet<>(
23
            Arrays.asList(new String[]{
24
        "COUNT",
25
        "SUM",
26
        "MIN",
27
        "MAX"
28
    }
29
            )
30
    );
31
    private static final Set<String> DENIED_FUNCTIONS = Collections.EMPTY_SET;
32

  
33
    public PostgreSQLStoreProviderFactory() {
34
        super(
35
                PostgreSQLLibrary.NAME,
36
                "PostgreSQL store"
37
        );
38
    }
39

  
40
    @Override
41
    public JDBCStoreProvider createProvider(
42
            DataParameters parameters,
43
            DataStoreProviderServices providerServices
44
    ) throws InitializeException {
45
        JDBCHelper helper = new PostgreSQLHelper((JDBCConnectionParameters) parameters);
46
        JDBCStoreProvider provider = helper.createProvider(
47
                (JDBCStoreParameters) parameters,
48
                providerServices
49
        );
50
        return provider;
51
    }
52

  
53
    @Override
54
    public JDBCStoreParameters createParameters() {
55
        JDBCStoreParameters params = new PostgreSQLStoreParameters();
56
        return params;
57
    }
58

  
59
    @Override
60
    public Function<String, Integer> allowFunction() {
61
        return (Function<String, Integer>) (String name) -> {
62
            if (ALLOWED_FUNCTIONS.contains(name)) {
63
                return YES;
64
            }
65
            if (DENIED_FUNCTIONS.contains(name)) {
66
                return NO;
67
            }
68
            return UNKNOWN;
69
        };
70
    }
71

  
72
    @Override
73
    public int allowGroupBy() {
74
        return YES;
75
    }
76

  
77
    @Override
78
    public int hasVectorialSupport() {
79
        return YES;
80
    }
81

  
82
    @Override
83
    public int hasRasterSupport() {
84
        return NO;
85
    }
86

  
87
    @Override
88
    public int hasTabularSupport() {
89
        return YES;
90
    }
91

  
92
    @Override
93
    public int hasSQLSupport() {
94
        return YES;
95
    }
96
}
tags/org.gvsig.postgresql-2.0.179/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
            int geometryType,
62
            int geometrySubtype
63
    ) {
64
        super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs, geometryType, geometrySubtype);
65
    }
66
    
67

  
68
    @Override
69
    public void fetch(Connection conn) throws DataException {
70
        super.fetch(conn);
71
    }
72

  
73
    @Override
74
    protected int getDataTypeFromMetadata(
75
            ResultSetMetaData rsMetadata,
76
            int colIndex
77
        ) throws SQLException {
78

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

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

  
216
            databaseGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
217
            databaseGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
218
            databaseGeometryTypes.put("MULTIPOINTM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2DM));
219
            databaseGeometryTypes.put("MULTIPOINTZM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3DM));
220

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

  
226
            databaseGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
227
            databaseGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
228
            databaseGeometryTypes.put("MULTIPOLYGONM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2DM));
229
            databaseGeometryTypes.put("MULTIPOLYGONZM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3DM));
230

  
231
            databaseGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
232
            databaseGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
233
            databaseGeometryTypes.put("GEOMETRYM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2DM));
234
            databaseGeometryTypes.put("GEOMETRYZM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3DM));
235
        }
236
        return databaseGeometryTypes.get(typeName);
237
    }
238
    
239
}
tags/org.gvsig.postgresql-2.0.179/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, -1, -1
23
        );
24
    }  
25

  
26
    @Override
27
    public FetchFeatureTypeOperation createFetchFeatureType(EditableFeatureType type, TableReference table, List<String> primaryKeys, String defaultGeometryField, IProjection crs, int geometryType, int geometrySubtype) {
28
        return new PostgreSQLFetchFeatureTypeOperation(
29
                helper, type, table, primaryKeys, 
30
                defaultGeometryField, crs, geometryType, geometrySubtype
31
        );
32
    }  
33

  
34
}
tags/org.gvsig.postgresql-2.0.179/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.179/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.179/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.179/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.179/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 )");
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff