Revision 508
tags/org.gvsig.postgresql-2.0.103/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.103</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.241</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.103</connection> |
|
20 |
<developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-postgresql/tags/org.gvsig.postgresql-2.0.103</developerConnection> |
|
21 |
<url>https://devel.gvsig.org/redmine/projects/gvsig-postgresql/repository/show/tags/org.gvsig.postgresql-2.0.103</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.103</version> |
|
63 |
</dependency> |
|
64 |
<dependency> |
|
65 |
<groupId>org.gvsig</groupId> |
|
66 |
<artifactId>org.gvsig.postgresql.app.mainplugin</artifactId> |
|
67 |
<version>2.0.103</version> |
|
68 |
</dependency> |
|
69 |
</dependencies> |
|
70 |
</dependencyManagement> |
|
71 |
|
|
72 |
<dependencies> |
|
73 |
<dependency> |
|
74 |
<groupId>org.slf4j</groupId> |
|
75 |
<artifactId>slf4j-api</artifactId> |
|
76 |
<scope>compile</scope> |
|
77 |
</dependency> |
|
78 |
</dependencies> |
|
79 |
|
|
80 |
|
|
81 |
<modules> |
|
82 |
<module>org.gvsig.postgresql.app</module> |
|
83 |
<module>org.gvsig.postgresql.provider</module> |
|
84 |
</modules> |
|
85 |
|
|
86 |
|
|
87 |
</project> |
tags/org.gvsig.postgresql-2.0.103/org.gvsig.postgresql.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.postgresql.dal.PostgreSQLLibrary |
tags/org.gvsig.postgresql-2.0.103/org.gvsig.postgresql.provider/src/main/resources/org/gvsig/postgresql/dal/PostgreSQLMetadata.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="PostgreSQL" namespace="Metadata"> |
|
6 |
<extends> |
|
7 |
<class name="JDBC2" namespace="Metadata"/> |
|
8 |
</extends> |
|
9 |
<description>Metadata of a PostgreSQL store</description> |
|
10 |
<fields> |
|
11 |
</fields> |
|
12 |
</class> |
|
13 |
|
|
14 |
</classes> |
|
15 |
</definitions> |
tags/org.gvsig.postgresql-2.0.103/org.gvsig.postgresql.provider/src/main/resources/org/gvsig/postgresql/dal/PostgreSQLParameters.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="PostgreSQLResourceParameters"> |
|
6 |
<extends> |
|
7 |
<class>JDBC2ResourceParameters</class> |
|
8 |
</extends> |
|
9 |
<fields> |
|
10 |
<field name="JDBCDriverClass" type="string" mandatory="true" |
|
11 |
defaultValue="org.postgresql.Driver" group="Advanced"> |
|
12 |
<description>JDBC Driver class</description> |
|
13 |
</field> |
|
14 |
<field name="port" type="integer" mandatory="false" |
|
15 |
defaultValue="5432" group="Connection"> |
|
16 |
<description></description> |
|
17 |
</field> |
|
18 |
<field name="host" type="string" mandatory="false" |
|
19 |
defaultValue="127.0.0.1" group="Connection"> |
|
20 |
<description></description> |
|
21 |
</field> |
|
22 |
<field name="UseSSL" type="boolean" mandatory="false" |
|
23 |
defaultValue="false" group="Basic"> |
|
24 |
<description>Use SSL connetion</description> |
|
25 |
</field> |
|
26 |
<field name="maxIdle" type="integer" mandatory="false" |
|
27 |
defaultValue="8" group="Advanced"> |
|
28 |
<description>The maximum number of connections that can remail idle in the pool.</description> |
|
29 |
</field> |
|
30 |
</fields> |
|
31 |
</class> |
|
32 |
|
|
33 |
<class name="PostgreSQLStoreParameters"> |
|
34 |
<extends> |
|
35 |
<class>JDBC2StoreParameters</class> |
|
36 |
<class>PostgreSQLResourceParameters</class> |
|
37 |
</extends> |
|
38 |
<fields/> |
|
39 |
</class> |
|
40 |
|
|
41 |
<class name="PostgreSQLNewStoreParameters"> |
|
42 |
<extends> |
|
43 |
<class>JDBC2NewStoreParameters</class> |
|
44 |
<class>PostgreSQLResourceParameters</class> |
|
45 |
</extends> |
|
46 |
<fields/> |
|
47 |
</class> |
|
48 |
|
|
49 |
|
|
50 |
<class name="PostgreSQLServerExplorerParameters"> |
|
51 |
<extends> |
|
52 |
<class>PostgreSQLResourceParameters</class> |
|
53 |
<class>JDBC2ServerExplorerParameters</class> |
|
54 |
</extends> |
|
55 |
<fields/> |
|
56 |
</class> |
|
57 |
|
|
58 |
|
|
59 |
</classes> |
|
60 |
</definitions> |
tags/org.gvsig.postgresql-2.0.103/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLConnectionParameters.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.postgresql.dal; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.resource.db.DBParameters; |
|
5 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
6 |
|
|
7 |
public interface PostgreSQLConnectionParameters extends JDBCConnectionParameters, DBParameters { |
|
8 |
public static final String USESSL_PARAMTER_NAME = "UseSSL"; |
|
9 |
public static final String MAXIDLE_PARAMTER_NAME = "maxIdle"; |
|
10 |
|
|
11 |
public boolean getUseSSL(); |
|
12 |
|
|
13 |
public int getMaxIdle(); |
|
14 |
} |
tags/org.gvsig.postgresql-2.0.103/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 |
} |
tags/org.gvsig.postgresql-2.0.103/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.103/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.fmap.dal.store.jdbc2.JDBCHelper; |
|
34 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
35 |
import org.gvsig.tools.packageutils.Version; |
|
36 |
import org.gvsig.tools.packageutils.impl.DefaultVersion; |
|
37 |
|
|
38 |
public class PostgreSQLBuilder extends JDBCSQLBuilderBase { |
|
39 |
|
|
40 |
public static class Version { |
|
41 |
|
|
42 |
private final int major; |
|
43 |
private final int minor; |
|
44 |
|
|
45 |
public Version(int major, int minor) { |
|
46 |
this.major = major; |
|
47 |
this.minor = minor; |
|
48 |
} |
|
49 |
|
|
50 |
public int getMajor() { |
|
51 |
return major; |
|
52 |
} |
|
53 |
|
|
54 |
public int getMinor() { |
|
55 |
return minor; |
|
56 |
} |
|
57 |
|
|
58 |
} |
|
59 |
private Version databaseVersion = null; |
|
60 |
|
|
61 |
public Version getDatabaseVersion() { |
|
62 |
if( databaseVersion == null ) { |
|
63 |
Connection conn; |
|
64 |
try { |
|
65 |
conn = this.getHelper().getConnection(); |
|
66 |
DatabaseMetaData metadata = conn.getMetaData(); |
|
67 |
databaseVersion = new Version( |
|
68 |
metadata.getDatabaseMajorVersion(), |
|
69 |
metadata.getDatabaseMinorVersion() |
|
70 |
); |
|
71 |
} catch (Exception ex) { |
|
72 |
databaseVersion = new Version(0,0); |
|
73 |
} |
|
74 |
} |
|
75 |
return databaseVersion; |
|
76 |
} |
|
77 |
|
|
78 |
public PostgreSQLBuilder(JDBCHelper helper) { |
|
79 |
super(helper); |
|
80 |
|
|
81 |
config.set(SQLConfig.default_schema, "public"); |
|
82 |
config.set(SQLConfig.allowAutomaticValues, true); |
|
83 |
config.set(SQLConfig.geometry_type_support, this.helper.getGeometrySupportType()); |
|
84 |
config.set(SQLConfig.has_spatial_functions, this.helper.hasSpatialFunctions()); |
|
85 |
config.set(SQLConfig.constant_true, "true"); |
|
86 |
config.set(SQLConfig.constant_false, "false"); |
|
87 |
|
|
88 |
config.remove_functionality(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table); |
|
89 |
config.remove_functionality(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_table); |
|
90 |
|
|
91 |
config.set(SQLConfig.UPDATE_TABLE_STATISTICS_table,"ANALYZE {0}"); |
|
92 |
|
|
93 |
config.set(SQLConfig.ST_GeomFromEWKB, "ST_GeomFromEWKB({0}, {1})"); |
|
94 |
config.set(SQLConfig.ST_AsEWKB, "ST_AsEWKB(({0}))"); |
|
95 |
config.set(SQLConfig.ST_ExtentAggregate, "ST_Extent({0})"); |
|
96 |
config.set(SQLConfig.ST_UnionAggregate, "ST_Union({0})"); |
|
97 |
|
|
98 |
config.set(SQLConfig.lcase, "lower({0})"); |
|
99 |
config.set(SQLConfig.ucase, "upper({0})"); |
|
100 |
config.set(SQLConfig.operator_ILIKE, "({0}) ILIKE ({1})"); |
|
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( config.has_functionality(SQLConfig.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 = identifier(this.table.getName()); |
|
115 |
if( table.has_schema()) { |
|
116 |
name = identifier(this.table.getSchema()) + "." + name; |
|
117 |
} |
|
118 |
String sql = MessageFormat.format( |
|
119 |
config.getString(SQLConfig.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() { |
|
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(identifier(this.indexName)); |
|
146 |
builder.append(" ON "); |
|
147 |
builder.append(this.table.toString()); |
|
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(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() { |
|
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()); |
|
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(identifier(column.getName())); |
|
194 |
builder.append(" "); |
|
195 |
if( column.isAutomatic() ) { |
|
196 |
builder.append(" SERIAL"); |
|
197 |
} else { |
|
198 |
builder.append(sqltype(column.getType(), column.getPrecision(), column.getSize())); |
|
199 |
if (column.getDefaultValue() == null) { |
|
200 |
if (column.allowNulls()) { |
|
201 |
builder.append(" DEFAULT NULL"); |
|
202 |
} |
|
203 |
} else { |
|
204 |
builder.append(" DEFAULT '"); |
|
205 |
builder.append(column.getDefaultValue().toString()); |
|
206 |
builder.append("'"); |
|
207 |
} |
|
208 |
if (column.allowNulls()) { |
|
209 |
builder.append(" NULL"); |
|
210 |
} else { |
|
211 |
builder.append(" NOT NULL"); |
|
212 |
} |
|
213 |
} |
|
214 |
if (column.isPrimaryKey()) { |
|
215 |
builder.append(" PRIMARY KEY"); |
|
216 |
} |
|
217 |
} |
|
218 |
builder.append(" )"); |
|
219 |
sqls.add(builder.toString()); |
|
220 |
|
|
221 |
String AddGeometryColumn = "SELECT AddGeometryColumn({0} , {1} , {2}, {3,number,#######} , {4} , {5}, {6})"; |
|
222 |
for (ColumnDescriptor column : columns) { |
|
223 |
if( column.isGeometry() ) { |
|
224 |
String sql = MessageFormat.format( |
|
225 |
AddGeometryColumn, |
|
226 |
constant(this.table.getSchema()), |
|
227 |
constant(this.table.getName()), |
|
228 |
constant(column.getName()), |
|
229 |
column.getGeometrySRSId(), |
|
230 |
constant(sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype())), |
|
231 |
constant(sqlgeometrydimension(column.getGeometryType(), column.getGeometrySubtype())), |
|
232 |
constant(column.allowNulls()) |
|
233 |
); |
|
234 |
sqls.add(sql); |
|
235 |
} |
|
236 |
} |
|
237 |
return sqls; |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
public class PostgreSQLSelectBuilderBase extends SelectBuilderBase { |
|
242 |
|
|
243 |
@Override |
|
244 |
protected boolean isValid(StringBuilder message) { |
|
245 |
if( message == null ) { |
|
246 |
message = new StringBuilder(); |
|
247 |
} |
|
248 |
if( this.has_offset() && !this.has_order_by() ) { |
|
249 |
// Algunos gestores de BBDD requieren que se especifique un |
|
250 |
// orden para poder usar OFFSET. Como eso parece buena idea para |
|
251 |
// asegurar que siempre tengamos los mismo resultados, lo exijimos |
|
252 |
// siempre. |
|
253 |
message.append("Can't use OFFSET without an ORDER BY."); |
|
254 |
return false; |
|
255 |
} |
|
256 |
return true; |
|
257 |
} |
|
258 |
|
|
259 |
@Override |
|
260 |
public String toString() { |
|
261 |
// |
|
262 |
// https://www.postgresql.org/docs/9.1/static/sql-select.html |
|
263 |
// |
|
264 |
StringBuilder builder = new StringBuilder(); |
|
265 |
if( !isValid(builder) ) { |
|
266 |
throw new IllegalStateException(builder.toString()); |
|
267 |
} |
|
268 |
builder.append("SELECT "); |
|
269 |
if( this.distinct ) { |
|
270 |
builder.append("DISTINCT "); |
|
271 |
} |
|
272 |
boolean first = true; |
|
273 |
for (SelectColumnBuilder column : columns) { |
|
274 |
if (first) { |
|
275 |
first = false; |
|
276 |
} else { |
|
277 |
builder.append(", "); |
|
278 |
} |
|
279 |
builder.append(column.toString()); |
|
280 |
} |
|
281 |
|
|
282 |
if ( this.has_from() ) { |
|
283 |
builder.append(" FROM "); |
|
284 |
builder.append(this.from.toString()); |
|
285 |
} |
|
286 |
if ( this.has_where() ) { |
|
287 |
builder.append(" WHERE "); |
|
288 |
builder.append(this.where.toString()); |
|
289 |
} |
|
290 |
|
|
291 |
if( this.has_order_by() ) { |
|
292 |
builder.append(" ORDER BY "); |
|
293 |
first = true; |
|
294 |
for (OrderByBuilder item : this.order_by) { |
|
295 |
if (first) { |
|
296 |
first = false; |
|
297 |
} else { |
|
298 |
builder.append(", "); |
|
299 |
} |
|
300 |
builder.append(item.toString()); |
|
301 |
} |
|
302 |
} |
|
303 |
|
|
304 |
if ( this.has_limit() && this.has_offset() ) { |
|
305 |
builder.append(" OFFSET "); |
|
306 |
builder.append(this.offset); |
|
307 |
builder.append(" FETCH NEXT "); |
|
308 |
builder.append(this.limit); |
|
309 |
builder.append(" ROWS ONLY"); |
|
310 |
|
|
311 |
} else if ( this.has_limit()) { |
|
312 |
builder.append(" LIMIT "); |
|
313 |
builder.append(this.limit); |
|
314 |
|
|
315 |
} else if ( this.has_offset() ) { |
|
316 |
builder.append(" LIMIT ALL OFFSET "); |
|
317 |
builder.append(this.offset); |
|
318 |
} |
|
319 |
return builder.toString(); |
|
320 |
|
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
@Override |
|
325 |
public String bytearray(byte[] data) { |
|
326 |
return "decode('"+bytearray_hex(data)+"','hex')"; |
|
327 |
} |
|
328 |
|
|
329 |
public PostgreSQLHelper getHelper() { |
|
330 |
return (PostgreSQLHelper) helper; |
|
331 |
} |
|
332 |
|
|
333 |
@Override |
|
334 |
protected CreateTableBuilder createCreateTableBuilder() { |
|
335 |
return new PostgreSQLCreateTableBuilder(); |
|
336 |
} |
|
337 |
|
|
338 |
@Override |
|
339 |
protected CreateIndexBuilder createCreateIndexBuilder() { |
|
340 |
return new PostgreSQLCreateIndexBuilder(); |
|
341 |
} |
|
342 |
|
|
343 |
@Override |
|
344 |
protected SelectBuilder createSelectBuilder() { |
|
345 |
return new PostgreSQLSelectBuilderBase(); |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
protected UpdateTableStatisticsBuilder createUpdateTableStatisticsBuilder() { |
|
350 |
return new PostgreSQLUpdateTableStatisticsBuilderBase(); |
|
351 |
} |
|
352 |
|
|
353 |
} |
tags/org.gvsig.postgresql-2.0.103/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 |
} |
tags/org.gvsig.postgresql-2.0.103/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.apache.commons.lang3.StringUtils; |
|
28 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
29 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
30 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
31 |
|
|
32 |
|
|
33 |
public class PostgreSQLServerExplorerParameters extends |
|
34 |
JDBCServerExplorerParameters |
|
35 |
implements |
|
36 |
PostgreSQLConnectionParameters |
|
37 |
{ |
|
38 |
|
|
39 |
private final PostgreSQLConnectionParametersHelper helper; |
|
40 |
|
|
41 |
public PostgreSQLServerExplorerParameters() { |
|
42 |
super( |
|
43 |
PostgreSQLLibrary.NAME + "ServerExplorerParameters", |
|
44 |
PostgreSQLLibrary.NAME |
|
45 |
); |
|
46 |
this.helper = new PostgreSQLConnectionParametersHelper(this); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public String getUrl() { |
|
51 |
return this.helper.getUrl(); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public void validate() throws ValidateDataParametersException { |
|
56 |
// Esto seria para convertir los parametros de gvSIG 2.3 a 2.4. |
|
57 |
// if( !StringUtils.equalsIgnoreCase(PostgreSQLLibrary.NAME, (CharSequence) getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME)) ) { |
|
58 |
// setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, PostgreSQLLibrary.NAME); |
|
59 |
// } |
|
60 |
this.helper.validate(); |
|
61 |
super.validate(); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public boolean getUseSSL() { |
|
66 |
return this.helper.getUseSSL(); |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public int getMaxIdle() { |
|
71 |
return this.helper.getMaxIdle(); |
|
72 |
} |
|
73 |
|
|
74 |
public void setUseSSL(boolean v) { |
|
75 |
this.helper.setUseSSL(v); |
|
76 |
} |
|
77 |
} |
tags/org.gvsig.postgresql-2.0.103/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 org.apache.commons.dbcp.BasicDataSource; |
|
7 |
import org.apache.commons.lang3.BooleanUtils; |
|
8 |
import org.apache.commons.lang3.StringUtils; |
|
9 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
10 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
11 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
14 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
15 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
|
16 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
|
17 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
|
18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
19 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase; |
|
20 |
import org.gvsig.postgresql.dal.operations.PostgreSQLOperationsFactory; |
|
21 |
import org.slf4j.Logger; |
|
22 |
import org.slf4j.LoggerFactory; |
|
23 |
|
|
24 |
public class PostgreSQLHelper extends JDBCHelperBase { |
|
25 |
|
|
26 |
static final Logger logger = LoggerFactory.getLogger(PostgreSQLHelper.class); |
|
27 |
|
|
28 |
public static final String POSTGRESQL_JDBC_DRIVER = "org.postgresql.Driver"; |
|
29 |
|
|
30 |
public static String getConnectionURL(PostgreSQLConnectionParameters params) { |
|
31 |
return getConnectionURL( |
|
32 |
params.getHost(), |
|
33 |
params.getPort(), |
|
34 |
params.getDBName() |
|
35 |
); |
|
36 |
} |
|
37 |
|
|
38 |
public static String getConnectionURL(String host, Integer port, String db) { |
|
39 |
if( StringUtils.isEmpty(host) ) { |
|
40 |
throw new IllegalArgumentException("Parameter 'host' can't be null."); |
|
41 |
} |
|
42 |
String connectionURL = "jdbc:postgresql://" + host; |
|
43 |
if (port != null) { |
|
44 |
connectionURL = connectionURL + ":" + port; |
|
45 |
} |
|
46 |
connectionURL = connectionURL + "/" + db; |
|
47 |
logger.debug("connectionURL: {}", connectionURL); |
|
48 |
return connectionURL; |
|
49 |
} |
|
50 |
|
|
51 |
private static class ConnectionProvider { |
|
52 |
|
|
53 |
private static boolean needRegisterDriver = true; |
|
54 |
|
|
55 |
private BasicDataSource dataSource = null; |
|
56 |
|
|
57 |
private final PostgreSQLConnectionParameters connectionParameters; |
|
58 |
|
|
59 |
public ConnectionProvider(PostgreSQLConnectionParameters connectionParameters) { |
|
60 |
this.connectionParameters = connectionParameters; |
|
61 |
} |
|
62 |
|
|
63 |
public Connection getConnection() throws SQLException { |
|
64 |
if (this.dataSource == null) { |
|
65 |
this.dataSource = this.createDataSource(); |
|
66 |
} |
|
67 |
if( logger.isDebugEnabled() ) { |
|
68 |
logger.debug("getConnection:\n" + getStatusInformation()); |
|
69 |
} |
|
70 |
Connection conn; |
|
71 |
try { |
|
72 |
conn = this.dataSource.getConnection(); |
|
73 |
} catch(Throwable ex) { |
|
74 |
logger.debug("Error getting connection from pool.",ex); |
|
75 |
throw ex; |
|
76 |
} |
|
77 |
if( logger.isDebugEnabled() ) { |
|
78 |
logger.debug("Created connection: {}\n NumActive: {}\n NumIdle: {}", |
|
79 |
new Object[] { |
|
80 |
conn.hashCode(), |
|
81 |
this.dataSource.getNumActive(), |
|
82 |
this.dataSource.getNumIdle() |
|
83 |
} |
|
84 |
); |
|
85 |
} |
|
86 |
return conn; |
|
87 |
} |
|
88 |
|
|
89 |
public void closeConnection(Connection connection) { |
|
90 |
if( connection != null ) { |
|
91 |
int connectionId = connection.hashCode(); |
|
92 |
try { |
|
93 |
connection.close(); |
|
94 |
} catch(Throwable ex) { |
|
95 |
logger.warn("Can't close connection.", ex); |
|
96 |
} |
|
97 |
if( logger.isDebugEnabled() ) { |
|
98 |
Boolean isClosed; |
|
99 |
try { |
|
100 |
isClosed = connection.isClosed(); |
|
101 |
} catch(Throwable th) { |
|
102 |
isClosed = null; |
|
103 |
} |
|
104 |
logger.debug("Closed connection: {}\n isClosed: {}\n NumActive: {}\n NumIdle: {}", |
|
105 |
new Object[] { |
|
106 |
connectionId, |
|
107 |
isClosed, |
|
108 |
this.dataSource.getNumActive(), |
|
109 |
this.dataSource.getNumIdle() |
|
110 |
} |
|
111 |
); |
|
112 |
} |
|
113 |
} else if( logger.isDebugEnabled() ) { |
|
114 |
logger.debug("Close connection: null"); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
public String getStatusInformation() { |
|
119 |
StringBuilder builder = new StringBuilder(); |
|
120 |
builder.append("BasicDataSource pool status:\n"); |
|
121 |
builder.append(" Connection URL: '").append(this.dataSource.getUrl()).append("'\n"); |
|
122 |
if( this.dataSource.getInitialSize()>0 ) { |
|
123 |
builder.append(" InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n"); |
|
124 |
} |
|
125 |
if( this.dataSource.isPoolPreparedStatements() ) { |
|
126 |
builder.append(" PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n"); |
|
127 |
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"); |
|
128 |
} |
|
129 |
builder.append(" MaxActive: ").append(this.dataSource.getMaxActive()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n"); |
|
130 |
builder.append(" MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n"); |
|
131 |
builder.append(" NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n"); |
|
132 |
builder.append(" NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n"); |
|
133 |
return builder.toString(); |
|
134 |
} |
|
135 |
|
|
136 |
private BasicDataSource createDataSource() throws SQLException { |
|
137 |
if (!this.isRegistered()) { |
|
138 |
this.registerDriver(); |
|
139 |
} |
|
140 |
PostgreSQLConnectionParameters params = connectionParameters; |
|
141 |
|
|
142 |
BasicDataSource ds = new BasicDataSource(); |
|
143 |
ds.setMaxIdle(params.getMaxIdle()); |
|
144 |
ds.setDriverClassName(params.getJDBCDriverClassName()); |
|
145 |
if( params.getUseSSL() ) { |
|
146 |
String s = BooleanUtils.toStringTrueFalse(params.getUseSSL()); |
|
147 |
ds.addConnectionProperty("ssl", s ); |
|
148 |
} |
|
149 |
if( !StringUtils.isEmpty(params.getUser()) ) { |
|
150 |
ds.setUsername(params.getUser()); |
|
151 |
} |
|
152 |
if( !StringUtils.isEmpty(params.getPassword()) ) { |
|
153 |
ds.setPassword(params.getPassword()); |
|
154 |
} |
|
155 |
ds.setUrl(params.getUrl()); |
|
156 |
|
|
157 |
ds.setMaxWait(60L * 1000); |
|
158 |
return ds; |
|
159 |
} |
|
160 |
|
|
161 |
private boolean isRegistered() { |
|
162 |
return needRegisterDriver; |
|
163 |
} |
|
164 |
|
|
165 |
public void registerDriver() throws SQLException { |
|
166 |
String className = this.connectionParameters.getJDBCDriverClassName(); |
|
167 |
if (className == null) { |
|
168 |
return; |
|
169 |
} |
|
170 |
try { |
|
171 |
Class theClass = Class.forName(className); |
|
172 |
if (theClass == null) { |
|
173 |
throw new JDBCDriverClassNotFoundException(PostgreSQLLibrary.NAME, className); |
|
174 |
} |
|
175 |
} catch (Exception e) { |
|
176 |
throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
|
177 |
} |
|
178 |
needRegisterDriver = false; |
|
179 |
} |
|
180 |
|
|
181 |
} |
|
182 |
|
|
183 |
private ConnectionProvider connectionProvider = null; |
|
184 |
|
|
185 |
public PostgreSQLHelper(JDBCConnectionParameters connectionParameters) { |
|
186 |
super(connectionParameters); |
|
187 |
this.srssolver = new SRSSolverBase(this); |
|
188 |
} |
|
189 |
|
|
190 |
@Override |
|
191 |
public Connection getConnection() throws AccessResourceException { |
|
192 |
try { |
|
193 |
if (this.connectionProvider == null) { |
|
194 |
this.connectionProvider = new ConnectionProvider(this.getConnectionParameters()); |
|
195 |
} |
|
196 |
return this.connectionProvider.getConnection(); |
|
197 |
} catch (SQLException ex) { |
|
198 |
throw new AccessResourceException(PostgreSQLLibrary.NAME, ex); |
|
199 |
} |
|
200 |
} |
|
201 |
|
|
202 |
@Override |
|
203 |
public void closeConnection(Connection connection) { |
|
204 |
this.connectionProvider.closeConnection(connection); |
|
205 |
} |
|
206 |
|
|
207 |
@Override |
|
208 |
public PostgreSQLConnectionParameters getConnectionParameters() { |
|
209 |
return (PostgreSQLConnectionParameters) super.getConnectionParameters(); |
|
210 |
} |
|
211 |
|
|
212 |
@Override |
|
213 |
public String getConnectionURL() { |
|
214 |
return getConnectionURL(this.getConnectionParameters()); |
|
215 |
} |
|
216 |
|
|
217 |
@Override |
|
218 |
protected String getResourceType() { |
|
219 |
return PostgreSQLLibrary.NAME; |
|
220 |
} |
|
221 |
|
|
222 |
@Override |
|
223 |
public String getProviderName() { |
|
224 |
return PostgreSQLLibrary.NAME; |
|
225 |
} |
|
226 |
|
|
227 |
@Override |
|
228 |
public JDBCSQLBuilderBase createSQLBuilder() { |
|
229 |
return new PostgreSQLBuilder(this); |
|
230 |
} |
|
231 |
|
|
232 |
@Override |
|
233 |
public OperationsFactory getOperations() { |
|
234 |
if (this.operationsFactory == null) { |
|
235 |
this.operationsFactory = new PostgreSQLOperationsFactory(this); |
|
236 |
} |
|
237 |
return operationsFactory; |
|
238 |
} |
|
239 |
|
|
240 |
@Override |
|
241 |
public SQLBuilder.GeometrySupportType getGeometrySupportType() { |
|
242 |
return SQLBuilder.GeometrySupportType.WKB; |
|
243 |
} |
|
244 |
|
|
245 |
@Override |
|
246 |
public boolean hasSpatialFunctions() { |
|
247 |
return true; |
|
248 |
} |
|
249 |
|
|
250 |
@Override |
|
251 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
|
252 |
return true; |
|
253 |
} |
|
254 |
|
|
255 |
@Override |
|
256 |
public String getQuoteForIdentifiers() { |
|
257 |
return "\""; |
|
258 |
} |
|
259 |
|
|
260 |
@Override |
|
261 |
public boolean allowAutomaticValues() { |
|
262 |
return true; |
|
263 |
} |
|
264 |
|
|
265 |
@Override |
|
266 |
public boolean supportOffsetInSelect() { |
|
267 |
return true; |
|
268 |
} |
|
269 |
|
|
270 |
@Override |
|
271 |
public String getQuoteForStrings() { |
|
272 |
return "'"; |
|
273 |
} |
|
274 |
|
|
275 |
@Override |
|
276 |
public String getSourceId(JDBCStoreParameters parameters) { |
|
277 |
return parameters.getDBName() + "." + |
|
278 |
parameters.getSchema()+ "." + |
|
279 |
parameters.getTable(); |
|
280 |
} |
|
281 |
|
|
282 |
@Override |
|
283 |
public JDBCNewStoreParameters createNewStoreParameters() { |
|
284 |
return new PostgreSQLNewStoreParameters(); |
|
285 |
} |
|
286 |
|
|
287 |
@Override |
|
288 |
public JDBCStoreParameters createOpenStoreParameters() { |
|
289 |
return new PostgreSQLStoreParameters(); |
|
290 |
} |
|
291 |
|
|
292 |
@Override |
|
293 |
public JDBCServerExplorerParameters createServerExplorerParameters() { |
|
294 |
return new PostgreSQLServerExplorerParameters(); |
|
295 |
} |
|
296 |
} |
tags/org.gvsig.postgresql-2.0.103/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.103/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.103/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.fmap.dal.DataTypes; |
|
15 |
import org.gvsig.fmap.dal.exception.DataException; |
|
16 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
18 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
20 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
|
21 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
22 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
|
23 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
|
24 |
import org.gvsig.fmap.geom.Geometry; |
|
25 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
26 |
import org.gvsig.fmap.geom.GeometryManager; |
|
27 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
28 |
import org.gvsig.postgresql.dal.PostgreSQLBuilder; |
|
29 |
|
|
30 |
@SuppressWarnings("UseSpecificCatch") |
|
31 |
public class PostgreSQLFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
|
32 |
|
|
33 |
private static Map<String,GeometryType>databaseGeometryTypes = null; |
|
34 |
|
|
35 |
public PostgreSQLFetchFeatureTypeOperation( |
|
36 |
JDBCHelper helper |
|
37 |
) { |
|
38 |
super(helper); |
|
39 |
} |
|
40 |
|
|
41 |
private GeometryType getGT( |
|
42 |
GeometryManager manager, |
|
43 |
int type, |
|
44 |
int subtype |
|
45 |
) { |
|
46 |
try { |
|
47 |
return manager.getGeometryType(type, subtype); |
|
48 |
} catch (Exception ex) { |
|
49 |
return null; |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
public PostgreSQLFetchFeatureTypeOperation( |
|
54 |
JDBCHelper helper, |
|
55 |
EditableFeatureType featureType, |
|
56 |
TableReference table, |
|
57 |
List<String> primaryKeys, |
|
58 |
String defaultGeometryColumn, |
|
59 |
IProjection crs |
|
60 |
) { |
|
61 |
super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException { |
|
66 |
super.fetch(featureType, conn, table, pks, defaultGeometryColumn, crs); |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
protected int getDataTypeFromMetadata( |
|
71 |
ResultSetMetaData rsMetadata, |
|
72 |
int colIndex |
|
73 |
) throws SQLException { |
|
74 |
|
|
75 |
return super.getDataTypeFromMetadata(rsMetadata, colIndex); |
|
76 |
} |
|
77 |
protected String getSQLToRetrievePrimaryKeysFromInformationSchema( |
|
78 |
String catalog, |
|
79 |
String schema, |
|
80 |
String table |
|
81 |
) throws SQLException { |
|
82 |
PostgreSQLBuilder sqlbuilder = (PostgreSQLBuilder) this.createSQLBuilder(); |
|
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 |
sqlbuilder.like( |
|
103 |
sqlbuilder.custom("c.TABLE_NAME"), |
|
104 |
sqlbuilder.constant(table) |
|
105 |
) |
|
106 |
); |
|
107 |
if (schema != null) { |
|
108 |
sqlbuilder.select().where().and( |
|
109 |
sqlbuilder.like( |
|
110 |
sqlbuilder.custom("c.TABLE_SCHEMA"), |
|
111 |
sqlbuilder.constant(schema) |
|
112 |
) |
|
113 |
); |
|
114 |
} |
|
115 |
if (catalog != null) { |
|
116 |
sqlbuilder.select().where().and( |
|
117 |
sqlbuilder.like( |
|
118 |
sqlbuilder.custom("c.CONSTRAINT_CATALOG"), |
|
119 |
sqlbuilder.constant(catalog) |
|
120 |
) |
|
121 |
); |
|
122 |
} |
|
123 |
sqlbuilder.select().where().and( |
|
124 |
sqlbuilder.eq( |
|
125 |
sqlbuilder.column(column_CONSTRAINT_TYPE), |
|
126 |
sqlbuilder.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 |
sqlbuilder.select().column().name("f_table_catalog"); |
|
144 |
sqlbuilder.select().column().name("f_table_schema"); |
|
145 |
sqlbuilder.select().column().name("f_table_name"); |
|
146 |
sqlbuilder.select().column().name("f_geometry_column"); |
|
147 |
sqlbuilder.select().column().name("coord_dimension"); |
|
148 |
sqlbuilder.select().column().name("srid"); |
|
149 |
sqlbuilder.select().column().name("type"); |
|
150 |
sqlbuilder.select().where().set( |
|
151 |
sqlbuilder.eq( |
|
152 |
sqlbuilder.column("f_table_name"), |
|
153 |
sqlbuilder.constant(this.getTable().getTable()) |
|
154 |
) |
|
155 |
); |
|
156 |
sqlbuilder.select().where().and( |
|
157 |
sqlbuilder.eq( |
|
158 |
sqlbuilder.column("f_geometry_column"), |
|
159 |
sqlbuilder.constant(attr.getName()) |
|
160 |
) |
|
161 |
); |
|
162 |
sqlbuilder.select().from().table().name("geometry_columns"); |
|
163 |
Statement st = null; |
|
164 |
ResultSet rs = null; |
|
165 |
|
|
166 |
Integer srsid = null; |
|
167 |
String geometryTypeName = null; |
|
168 |
try { |
|
169 |
st = this.getConnection().createStatement(); |
|
170 |
rs = JDBCUtils.executeQuery(st, sqlbuilder.toString()); |
|
171 |
if (rs.next()) { |
|
172 |
srsid = rs.getInt("srid"); |
|
173 |
geometryTypeName = rs.getString("type"); |
|
174 |
} |
|
175 |
} finally { |
|
176 |
JDBCUtils.closeQuietly(rs); |
|
177 |
JDBCUtils.closeQuietly(st); |
|
178 |
} |
|
179 |
if( !StringUtils.isEmpty(geometryTypeName) ) { |
|
180 |
GeometryType gt = getGeometryTypeFromDatabaseTypeName(geometryTypeName); |
|
181 |
attr.setGeometryType(gt); |
|
182 |
} |
|
183 |
if( srsid!=null ) { |
|
184 |
SRSSolver srssolver = this.helper.getSRSSolver(); |
Also available in: Unified diff