Revision 1268
tags/org.gvsig.postgresql-2.0.244/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.244</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.jdbc</artifactId> |
|
46 |
<scope>test</scope> |
|
47 |
<type>test-jar</type> |
|
48 |
</dependency> |
|
49 |
|
|
50 |
<dependency> |
|
51 |
<groupId>org.gvsig</groupId> |
|
52 |
<artifactId>org.gvsig.fmap.dal.db.lib</artifactId> |
|
53 |
<scope>compile</scope> |
|
54 |
</dependency> |
|
55 |
|
|
56 |
<dependency> |
|
57 |
<groupId>org.gvsig</groupId> |
|
58 |
<artifactId>org.gvsig.fmap.dal.spi</artifactId> |
|
59 |
<scope>compile</scope> |
|
60 |
</dependency> |
|
61 |
<dependency> |
|
62 |
<groupId>org.gvsig</groupId> |
|
63 |
<artifactId>org.gvsig.metadata.lib.basic.api</artifactId> |
|
64 |
<scope>compile</scope> |
|
65 |
</dependency> |
|
66 |
<dependency> |
|
67 |
<groupId>org.gvsig</groupId> |
|
68 |
<artifactId>org.gvsig.fmap.geometry.api</artifactId> |
|
69 |
<scope>compile</scope> |
|
70 |
</dependency> |
|
71 |
<!-- |
|
72 |
<dependency> |
|
73 |
<groupId>org.gvsig</groupId> |
|
74 |
<artifactId>org.gvsig.fmap.geometry.operation</artifactId> |
|
75 |
<scope>compile</scope> |
|
76 |
</dependency> |
|
77 |
--> |
|
78 |
<dependency> |
|
79 |
<groupId>org.gvsig</groupId> |
|
80 |
<artifactId>org.gvsig.timesupport.lib.api</artifactId> |
|
81 |
<scope>compile</scope> |
|
82 |
</dependency> |
|
83 |
<dependency> |
|
84 |
<groupId>org.gvsig</groupId> |
|
85 |
<artifactId>org.gvsig.utils</artifactId> |
|
86 |
<scope>compile</scope> |
|
87 |
</dependency> |
|
88 |
<dependency> |
|
89 |
<groupId>org.postgresql</groupId> |
|
90 |
<artifactId>postgresql</artifactId> |
|
91 |
<scope>compile</scope> |
|
92 |
</dependency> |
|
93 |
<dependency> |
|
94 |
<groupId>org.apache.commons</groupId> |
|
95 |
<artifactId>commons-dbcp2</artifactId> |
|
96 |
</dependency> |
|
97 |
<dependency> |
|
98 |
<groupId>org.apache.commons</groupId> |
|
99 |
<artifactId>commons-pool2</artifactId> |
|
100 |
</dependency> |
|
101 |
<dependency> |
|
102 |
<groupId>commons-collections</groupId> |
|
103 |
<artifactId>commons-collections</artifactId> |
|
104 |
<scope>compile</scope> |
|
105 |
</dependency> |
|
106 |
|
|
107 |
|
|
108 |
<!-- TESTS --> |
|
109 |
<dependency> |
|
110 |
<groupId>org.gvsig</groupId> |
|
111 |
<artifactId>org.gvsig.tools.lib</artifactId> |
|
112 |
<type>test-jar</type> |
|
113 |
<scope>test</scope> |
|
114 |
</dependency> |
|
115 |
<dependency> |
|
116 |
<groupId>org.gvsig</groupId> |
|
117 |
<artifactId>org.gvsig.compat.se</artifactId> |
|
118 |
<scope>test</scope> |
|
119 |
</dependency> |
|
120 |
<dependency> |
|
121 |
<groupId>org.gvsig</groupId> |
|
122 |
<artifactId>org.gvsig.fmap.dal.impl</artifactId> |
|
123 |
<scope>test</scope> |
|
124 |
</dependency> |
|
125 |
<dependency> |
|
126 |
<groupId>org.gvsig</groupId> |
|
127 |
<artifactId>org.gvsig.fmap.dal.file.csv</artifactId> |
|
128 |
<scope>test</scope> |
|
129 |
</dependency> |
|
130 |
<dependency> |
|
131 |
<groupId>org.gvsig</groupId> |
|
132 |
<artifactId>org.gvsig.tools.util.impl</artifactId> |
|
133 |
<scope>test</scope> |
|
134 |
</dependency> |
|
135 |
<dependency> |
|
136 |
<groupId>org.gvsig</groupId> |
|
137 |
<artifactId>org.gvsig.tools.swing.impl</artifactId> |
|
138 |
<scope>test</scope> |
|
139 |
</dependency> |
|
140 |
<dependency> |
|
141 |
<groupId>org.gvsig</groupId> |
|
142 |
<artifactId>${org.gvsig.fmap.geometry.impl}</artifactId> |
|
143 |
<scope>test</scope> |
|
144 |
</dependency> |
|
145 |
<dependency> |
|
146 |
<groupId>org.gvsig</groupId> |
|
147 |
<artifactId>org.gvsig.expressionevaluator.geometry.lib.impl</artifactId> |
|
148 |
<scope>test</scope> |
|
149 |
</dependency> |
|
150 |
<dependency> |
|
151 |
<groupId>org.gvsig</groupId> |
|
152 |
<artifactId>${org.gvsig.proj.lib.impl}</artifactId> |
|
153 |
<scope>test</scope> |
|
154 |
</dependency> |
|
155 |
<dependency> |
|
156 |
<groupId>org.gvsig</groupId> |
|
157 |
<artifactId>org.gvsig.projection.cresques.impl</artifactId> |
|
158 |
<scope>test</scope> |
|
159 |
</dependency> |
|
160 |
<dependency> |
|
161 |
<groupId>org.gvsig</groupId> |
|
162 |
<artifactId>org.gvsig.timesupport.lib.impl</artifactId> |
|
163 |
<scope>test</scope> |
|
164 |
</dependency> |
|
165 |
<dependency> |
|
166 |
<groupId>org.gvsig</groupId> |
|
167 |
<artifactId>org.gvsig.metadata.lib.basic.impl</artifactId> |
|
168 |
<scope>test</scope> |
|
169 |
</dependency> |
|
170 |
|
|
171 |
|
|
172 |
<dependency> |
|
173 |
<artifactId>junit</artifactId> |
|
174 |
<groupId>junit</groupId> |
|
175 |
<scope>test</scope> |
|
176 |
</dependency> |
|
177 |
</dependencies> |
|
178 |
|
|
179 |
<build> |
|
180 |
<plugins> |
|
181 |
|
|
182 |
<!-- Skip test execution ? --> |
|
183 |
<plugin> |
|
184 |
<groupId>org.apache.maven.plugins</groupId> |
|
185 |
<artifactId>maven-surefire-plugin</artifactId> |
|
186 |
<configuration> |
|
187 |
<skipTests>false</skipTests> |
|
188 |
</configuration> |
|
189 |
</plugin> |
|
190 |
|
|
191 |
<!-- Skip test compilation ? --> |
|
192 |
<plugin> |
|
193 |
<groupId>org.apache.maven.plugins</groupId> |
|
194 |
<artifactId>maven-compiler-plugin</artifactId> |
|
195 |
<executions> |
|
196 |
<execution> |
|
197 |
<id>default-testCompile</id> |
|
198 |
<phase>process-test-sources</phase> |
|
199 |
<goals> |
|
200 |
<goal>testCompile</goal> |
|
201 |
</goals> |
|
202 |
<configuration> |
|
203 |
<skip>false</skip> |
|
204 |
</configuration> |
|
205 |
</execution> |
|
206 |
</executions> |
|
207 |
</plugin> |
|
208 |
|
|
209 |
<!-- Ignore test execution failure ? --> |
|
210 |
<plugin> |
|
211 |
<groupId>org.apache.maven.plugins</groupId> |
|
212 |
<artifactId>maven-surefire-plugin</artifactId> |
|
213 |
<configuration> |
|
214 |
<testFailureIgnore>false</testFailureIgnore> |
|
215 |
</configuration> |
|
216 |
</plugin> |
|
217 |
|
|
218 |
</plugins> |
|
219 |
</build> |
|
220 |
|
|
221 |
</project> |
tags/org.gvsig.postgresql-2.0.244/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.244/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.isBlank(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.244/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.json.Json; |
|
11 |
import org.gvsig.metadata.exceptions.MetadataException; |
|
12 |
import org.gvsig.tools.library.AbstractLibrary; |
|
13 |
import org.gvsig.tools.library.LibraryException; |
|
14 |
|
|
15 |
|
|
16 |
public class PostgreSQLLibrary extends AbstractLibrary { |
|
17 |
|
|
18 |
public static final String NAME = "PostgreSQL"; |
|
19 |
|
|
20 |
@Override |
|
21 |
public void doRegistration() { |
|
22 |
registerAsServiceOf(DALLibrary.class); |
|
23 |
require(JDBCLibrary.class); |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
protected void doInitialize() throws LibraryException { |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
protected void doPostInitialize() throws LibraryException { |
|
32 |
LibraryException ex = null; |
|
33 |
|
|
34 |
DataManagerProviderServices dataman = |
|
35 |
(DataManagerProviderServices) DALLocator.getDataManager(); |
|
36 |
|
|
37 |
try { |
|
38 |
Class.forName(PostgreSQLHelper.POSTGRESQL_JDBC_DRIVER); |
|
39 |
} catch(Throwable th) { |
|
40 |
PostgreSQLHelper.LOGGER.warn("Can't load PostgreSQL JDBC Driver.",th); |
|
41 |
} |
|
42 |
|
|
43 |
DBHelper.registerParametersDefinition( |
|
44 |
NAME + "StoreParameters", |
|
45 |
PostgreSQLStoreParameters.class, |
|
46 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
47 |
); |
|
48 |
DBHelper.registerParametersDefinition( |
|
49 |
NAME + "NewStoreParameters", |
|
50 |
PostgreSQLNewStoreParameters.class, |
|
51 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
52 |
); |
|
53 |
DBHelper.registerParametersDefinition( |
|
54 |
NAME + "ServerExplorerParameters", |
|
55 |
PostgreSQLServerExplorerParameters.class, |
|
56 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
57 |
); |
|
58 |
// DBHelper.registerParametersDefinition( |
|
59 |
// NAME + "ResourceParameters", |
|
60 |
// PostgreSQLResourceParameters.class, |
|
61 |
// dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
62 |
// ); |
|
63 |
try { |
|
64 |
DBHelper.registerMetadataDefinition( |
|
65 |
NAME, |
|
66 |
JDBCStoreProviderBase.class, |
|
67 |
dataman.getResourceAsStream(this, NAME + "Metadata.xml") |
|
68 |
); |
|
69 |
} catch (MetadataException e) { |
|
70 |
ex = new LibraryException(this.getClass(), e); |
|
71 |
} |
|
72 |
|
|
73 |
// ResourceManagerProviderServices resman = (ResourceManagerProviderServices) DALLocator |
|
74 |
// .getResourceManager(); |
|
75 |
// |
|
76 |
// if (!resman.getResourceProviders().contains(NAME)) { |
|
77 |
// resman.register(NAME, |
|
78 |
// "Resource for " + NAME, |
|
79 |
// PostgreSQLResource.class, |
|
80 |
// PostgreSQLResourceParameters.class |
|
81 |
// ); |
|
82 |
// } |
|
83 |
|
|
84 |
if (!dataman.getStoreProviderRegister().exits(NAME)) { |
|
85 |
dataman.registerStoreProviderFactory(new PostgreSQLStoreProviderFactory()); |
|
86 |
} |
|
87 |
|
|
88 |
if (!dataman.getServerExplorerRegister().exits(NAME)) { |
|
89 |
dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory()); |
|
90 |
} |
|
91 |
|
|
92 |
// Por compatibilidad con gvSIG 2.3 registramos otra vez la factoria con |
|
93 |
// el nombre que tenia antes. |
|
94 |
if (!dataman.getServerExplorerRegister().exits("PostgreSQLExplorer")) { |
|
95 |
dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory("PostgreSQLExplorer")); |
|
96 |
} |
|
97 |
Json.registerSerializer(PostgreSQLServerExplorerParameters.class); |
|
98 |
if (ex != null) { |
|
99 |
throw ex; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
} |
tags/org.gvsig.postgresql-2.0.244/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.DatabaseMetaData; |
|
28 |
import java.text.MessageFormat; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.List; |
|
31 |
import org.apache.commons.lang3.StringUtils; |
|
32 |
import org.gvsig.expressionevaluator.Formatter; |
|
33 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
|
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 |
JDBCConnection 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 |
JDBCConnection.closeQuietly(conn); |
|
78 |
} |
|
79 |
} |
|
80 |
return databaseVersion; |
|
81 |
} |
|
82 |
|
|
83 |
public PostgreSQLBuilder(JDBCHelper helper) { |
|
84 |
super(helper); |
|
85 |
this.defaultSchema = "public"; |
|
86 |
this.supportSchemas = true; |
|
87 |
this.allowAutomaticValues = true; |
|
88 |
this.geometrySupportType = this.helper.getGeometrySupportType(); |
|
89 |
this.hasSpatialFunctions = this.helper.hasSpatialFunctions(); |
|
90 |
|
|
91 |
this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null; |
|
92 |
this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null; |
|
93 |
|
|
94 |
this.STMT_UPDATE_TABLE_STATISTICS_table = "ANALYZE {0}"; |
|
95 |
|
|
96 |
// config.set(SQLConfig.ST_GeomFromEWKB, "ST_GeomFromEWKB({0}, {1})"); |
|
97 |
// config.set(SQLConfig.ST_AsEWKB, "ST_AsEWKB(({0}))"); |
|
98 |
// config.set(SQLConfig.ST_ExtentAggregate, "ST_Extent({0})"); |
|
99 |
// config.set(SQLConfig.ST_UnionAggregate, "ST_Union({0})"); |
|
100 |
|
|
101 |
// config.set(SQLConfig.isNull, "( ({0}) ISNULL )"); |
|
102 |
// config.set(SQLConfig.notIsNull, "( ({0}) NOT NULL )"); |
|
103 |
|
|
104 |
} |
|
105 |
|
|
106 |
public class PostgreSQLUpdateTableStatisticsBuilderBase extends UpdateTableStatisticsBuilderBase { |
|
107 |
@Override |
|
108 |
public List<String> toStrings() { |
|
109 |
List<String> sqls = new ArrayList<>(); |
|
110 |
|
|
111 |
if( !StringUtils.isBlank(STMT_UPDATE_TABLE_STATISTICS_table) ) { |
|
112 |
// In postGIS, UpdateLayerStatistics function, don't allow to |
|
113 |
// use the database name in the table name. |
|
114 |
String name = as_identifier(this.table.getName()); |
|
115 |
if( table.has_schema()) { |
|
116 |
name = as_identifier(this.table.getSchema()) + "." + name; |
|
117 |
} |
|
118 |
String sql = MessageFormat.format( |
|
119 |
STMT_UPDATE_TABLE_STATISTICS_table, |
|
120 |
name |
|
121 |
); |
|
122 |
if( !StringUtils.isEmpty(sql) ) { |
|
123 |
sqls.add(sql); |
|
124 |
} |
|
125 |
} |
|
126 |
return sqls; |
|
127 |
} |
|
128 |
} |
|
129 |
protected class PostgreSQLCreateIndexBuilder extends CreateIndexBuilderBase { |
|
130 |
|
|
131 |
@Override |
|
132 |
public List<String> toStrings(Formatter formatter) { |
|
133 |
StringBuilder builder = new StringBuilder(); |
|
134 |
builder.append("CREATE "); |
|
135 |
// if( this.isUnique ) { |
|
136 |
// builder.append("UNIQUE "); |
|
137 |
// } |
|
138 |
builder.append("INDEX "); |
|
139 |
if( this.ifNotExist ) { |
|
140 |
Version version = getDatabaseVersion(); |
|
141 |
if( version.getMajor()>=9 && version.getMinor()>=5 ) { |
|
142 |
builder.append("IF NOT EXISTS "); |
|
143 |
} |
|
144 |
} |
|
145 |
builder.append(as_identifier(this.indexName)); |
|
146 |
builder.append(" ON "); |
|
147 |
builder.append(this.table.toString(formatter)); |
|
148 |
if( this.isSpatial ) { |
|
149 |
builder.append(" USING GIST "); |
|
150 |
} |
|
151 |
builder.append(" ( "); |
|
152 |
boolean is_first_column = true; |
|
153 |
for( String column : this.columns) { |
|
154 |
if( is_first_column ) { |
|
155 |
is_first_column = false; |
|
156 |
} else { |
|
157 |
builder.append(", "); |
|
158 |
} |
|
159 |
builder.append(as_identifier(column)); |
|
160 |
} |
|
161 |
builder.append(" )"); |
|
162 |
|
|
163 |
List<String> sqls = new ArrayList<>(); |
|
164 |
sqls.add(builder.toString()); |
|
165 |
return sqls; |
|
166 |
} |
|
167 |
|
|
168 |
} |
|
169 |
|
|
170 |
protected class PostgreSQLCreateTableBuilder extends CreateTableBuilderBase { |
|
171 |
|
|
172 |
@Override |
|
173 |
public List<String> toStrings(Formatter formatter) { |
|
174 |
// |
|
175 |
// https://www.postgresql.org/docs/9.1/static/sql-createtable.html |
|
176 |
// |
|
177 |
List<String> sqls = new ArrayList<>(); |
|
178 |
StringBuilder builder = new StringBuilder(); |
|
179 |
|
|
180 |
builder.append("CREATE TABLE "); |
|
181 |
builder.append(this.table.toString(formatter)); |
|
182 |
builder.append(" ("); |
|
183 |
boolean first = true; |
|
184 |
for (ColumnDescriptor column : columns) { |
|
185 |
if( column.isGeometry() ) { |
|
186 |
continue; |
|
187 |
} |
|
188 |
if (first) { |
|
189 |
first = false; |
|
190 |
} else { |
|
191 |
builder.append(", "); |
|
192 |
} |
|
193 |
builder.append(as_identifier(column.getName())); |
|
194 |
builder.append(" "); |
|
195 |
if( column.isAutomatic() ) { |
|
196 |
builder.append(" SERIAL"); |
|
197 |
} else { |
|
198 |
builder.append(sqltype( |
|
199 |
column.getType(), |
|
200 |
column.getSize(), |
|
201 |
column.getPrecision(), |
|
202 |
column.getScale(), |
|
203 |
column.getGeometryType(), |
|
204 |
column.getGeometrySubtype() |
|
205 |
)); |
|
206 |
if (column.getDefaultValue() == null) { |
|
207 |
if (column.allowNulls()) { |
|
208 |
builder.append(" DEFAULT NULL"); |
|
209 |
} |
|
210 |
} else { |
|
211 |
builder.append(" DEFAULT '"); |
|
212 |
builder.append(column.getDefaultValue().toString()); |
|
213 |
builder.append("'"); |
|
214 |
} |
|
215 |
if (column.allowNulls()) { |
|
216 |
builder.append(" NULL"); |
|
217 |
} else { |
|
218 |
builder.append(" NOT NULL"); |
|
219 |
} |
|
220 |
} |
|
221 |
if (column.isPrimaryKey()) { |
|
222 |
builder.append(" PRIMARY KEY"); |
|
223 |
} |
|
224 |
} |
|
225 |
builder.append(" )"); |
|
226 |
sqls.add(builder.toString()); |
|
227 |
|
|
228 |
String AddGeometryColumn = "SELECT AddGeometryColumn({0} , {1} , {2}, {3,number,#######} , {4} , {5}, {6})"; |
|
229 |
for (ColumnDescriptor column : columns) { |
|
230 |
if( column.isGeometry() ) { |
|
231 |
String sql = MessageFormat.format( |
|
232 |
AddGeometryColumn, |
|
233 |
as_string(this.table.has_schema()?this.table.getSchema():"public"), |
|
234 |
as_string(this.table.getName()), |
|
235 |
as_string(column.getName()), |
|
236 |
column.getGeometrySRSId(), |
|
237 |
as_string(sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype())), |
|
238 |
as_string(sqlgeometrydimension(column.getGeometryType(), column.getGeometrySubtype())), |
|
239 |
as_string(column.allowNulls()) |
|
240 |
); |
|
241 |
sqls.add(sql); |
|
242 |
} |
|
243 |
} |
|
244 |
return sqls; |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
public class PostgreSQLSelectBuilderBase extends SelectBuilderBase { |
|
249 |
|
|
250 |
@Override |
|
251 |
protected boolean isValid(StringBuilder message) { |
|
252 |
if( message == null ) { |
|
253 |
message = new StringBuilder(); |
|
254 |
} |
|
255 |
if( this.has_offset() && !this.has_order_by() ) { |
|
256 |
// Algunos gestores de BBDD requieren que se especifique un |
|
257 |
// orden para poder usar OFFSET. Como eso parece buena idea para |
|
258 |
// asegurar que siempre tengamos los mismo resultados, lo exijimos |
|
259 |
// siempre. |
|
260 |
message.append("Can't use OFFSET without an ORDER BY."); |
|
261 |
return false; |
|
262 |
} |
|
263 |
return true; |
|
264 |
} |
|
265 |
|
|
266 |
@Override |
|
267 |
public String toString(Formatter formatter) { |
|
268 |
// |
|
269 |
// https://www.postgresql.org/docs/9.1/static/sql-select.html |
|
270 |
// |
|
271 |
StringBuilder builder = new StringBuilder(); |
|
272 |
if( !isValid(builder) ) { |
|
273 |
throw new IllegalStateException(builder.toString()); |
|
274 |
} |
|
275 |
builder.append("SELECT "); |
|
276 |
if( this.distinct ) { |
|
277 |
builder.append("DISTINCT "); |
|
278 |
} |
|
279 |
boolean first = true; |
|
280 |
for (SelectColumnBuilder column : columns) { |
|
281 |
if (first) { |
|
282 |
first = false; |
|
283 |
} else { |
|
284 |
builder.append(", "); |
|
285 |
} |
|
286 |
builder.append(column.toString(formatter)); |
|
287 |
} |
|
288 |
|
|
289 |
if ( this.has_from() ) { |
|
290 |
builder.append(" FROM "); |
|
291 |
builder.append(this.from.toString(formatter)); |
|
292 |
} |
|
293 |
|
|
294 |
if ( this.has_where() ) { |
|
295 |
builder.append(" WHERE "); |
|
296 |
builder.append(this.where.toString(formatter)); |
|
297 |
} |
|
298 |
if( this.has_group_by() ) { |
|
299 |
builder.append(" GROUP BY "); |
|
300 |
builder.append(this.groupColumn.get(0).toString(formatter)); |
|
301 |
for (int i = 1; i < groupColumn.size(); i++) { |
|
302 |
builder.append(", "); |
|
303 |
builder.append(this.groupColumn.get(i).toString(formatter)); |
|
304 |
} |
|
305 |
} |
|
306 |
|
|
307 |
if( this.has_order_by() ) { |
|
308 |
builder.append(" ORDER BY "); |
|
309 |
first = true; |
|
310 |
for (OrderByBuilder item : this.order_by) { |
|
311 |
if (first) { |
|
312 |
first = false; |
|
313 |
} else { |
|
314 |
builder.append(", "); |
|
315 |
} |
|
316 |
builder.append(item.toString(formatter)); |
|
317 |
} |
|
318 |
} |
|
319 |
|
|
320 |
if ( this.has_limit() && this.has_offset() ) { |
|
321 |
builder.append(" OFFSET "); |
|
322 |
builder.append(this.offset); |
|
323 |
builder.append(" FETCH NEXT "); |
|
324 |
builder.append(this.limit); |
|
325 |
builder.append(" ROWS ONLY"); |
|
326 |
|
|
327 |
} else if ( this.has_limit()) { |
|
328 |
builder.append(" LIMIT "); |
|
329 |
builder.append(this.limit); |
|
330 |
|
|
331 |
} else if ( this.has_offset() ) { |
|
332 |
builder.append(" LIMIT ALL OFFSET "); |
|
333 |
builder.append(this.offset); |
|
334 |
} |
|
335 |
return builder.toString(); |
|
336 |
|
|
337 |
} |
|
338 |
} |
|
339 |
|
|
340 |
@Override |
|
341 |
public Formatter formatter() { |
|
342 |
if( this.formatter==null ) { |
|
343 |
this.formatter = new PostgreSQLFormatter(this); |
|
344 |
} |
|
345 |
return this.formatter; |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
public PostgreSQLHelper getHelper() { |
|
350 |
return (PostgreSQLHelper) helper; |
|
351 |
} |
|
352 |
|
|
353 |
@Override |
|
354 |
protected CreateTableBuilder createCreateTableBuilder() { |
|
355 |
return new PostgreSQLCreateTableBuilder(); |
|
356 |
} |
|
357 |
|
|
358 |
@Override |
|
359 |
protected CreateIndexBuilder createCreateIndexBuilder() { |
|
360 |
return new PostgreSQLCreateIndexBuilder(); |
|
361 |
} |
|
362 |
|
|
363 |
@Override |
|
364 |
public SelectBuilder createSelectBuilder() { |
|
365 |
return new PostgreSQLSelectBuilderBase(); |
|
366 |
} |
|
367 |
|
|
368 |
@Override |
|
369 |
protected UpdateTableStatisticsBuilder createUpdateTableStatisticsBuilder() { |
|
370 |
return new PostgreSQLUpdateTableStatisticsBuilderBase(); |
|
371 |
} |
|
372 |
|
|
373 |
@Override |
|
374 |
public int getMaxRecomendedSQLLength() { |
|
375 |
return 10240; |
|
376 |
} |
|
377 |
|
|
378 |
@Override |
|
379 |
public String as_clob(String s) { |
|
380 |
int chunkSize = 1024; |
|
381 |
StringBuilder builder = new StringBuilder(); |
|
382 |
builder.append("('"); |
|
383 |
for (int i = 0; i < s.length(); i += chunkSize) { |
|
384 |
String chunk = s.substring(i, Math.min(s.length(), i + chunkSize)); |
|
385 |
if( i>0 ) { |
|
386 |
builder.append("'::text || '"); |
|
387 |
} |
|
388 |
builder.append(StringUtils.replace(chunk, "'", "''")); |
|
389 |
} |
|
390 |
builder.append("'::text)"); |
|
391 |
return builder.toString(); |
|
392 |
} |
|
393 |
|
|
394 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Getattr.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import org.apache.commons.lang3.StringUtils; |
|
4 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
|
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Constant; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
public class Getattr implements Formatter<Value> { |
|
17 |
|
|
18 |
private final SQLBuilder sqlbuilder; |
|
19 |
private final Formatter<Value> formatter; |
|
20 |
|
|
21 |
public Getattr(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
22 |
this.sqlbuilder = sqlbuilder; |
|
23 |
this.formatter = formatter; |
|
24 |
} |
|
25 |
@Override |
|
26 |
public boolean canApply(Value value) { |
|
27 |
if (value instanceof Function) { |
|
28 |
if( StringUtils.equalsIgnoreCase(FUNCTION_GETATTR, ((Function) value).name()) ) { |
|
29 |
Value obj = ((Function) value).parameters().get(0); |
|
30 |
Value attrname = ((Function) value).parameters().get(1); |
|
31 |
return obj instanceof Variable && attrname instanceof Constant; |
|
32 |
|
|
33 |
} |
|
34 |
} |
|
35 |
return false; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public String format(Value function0) { |
|
40 |
Function function = (Function) function0; |
|
41 |
Variable obj = (Variable) function.parameters().get(0); |
|
42 |
Constant attrname = (Constant) function.parameters().get(1); |
|
43 |
return this.sqlbuilder.as_identifier(obj.name())+"."+this.sqlbuilder.as_identifier((String) attrname.value()); |
|
44 |
} |
|
45 |
|
|
46 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ILike.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
7 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_ILIKE; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
class ILike implements Formatter<Value> { |
|
17 |
|
|
18 |
private final Formatter<Value> formatter; |
|
19 |
private final SQLBuilder builder; |
|
20 |
|
|
21 |
public ILike(SQLBuilder builder, Formatter<Value> formatter) { |
|
22 |
this.builder = builder; |
|
23 |
this.formatter = formatter; |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public boolean canApply(Value value) { |
|
28 |
if (value instanceof Function) { |
|
29 |
return StringUtils.equalsIgnoreCase(OPERATOR_ILIKE, ((Function) value).name()); |
|
30 |
} |
|
31 |
return false; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public String format(Value function) { |
|
36 |
List<Value> parameters = ((Function) function).parameters(); |
|
37 |
String p1 = parameters.get(0).toString(formatter); |
|
38 |
String p2 = parameters.get(1).toString(formatter); |
|
39 |
String r = MessageFormat.format("({0}) ~~* ({1})", p1, p2); |
|
40 |
return r; |
|
41 |
} |
|
42 |
|
|
43 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Decode.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.Objects; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
public class Decode implements Formatter<Value> { |
|
18 |
|
|
19 |
private final SQLBuilder sqlbuilder; |
|
20 |
private final Formatter<Value> formatter; |
|
21 |
|
|
22 |
public Decode(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
23 |
this.sqlbuilder = sqlbuilder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
29 |
if (value instanceof ExpressionBuilder.Function) { |
|
30 |
if( StringUtils.equalsIgnoreCase(FUNCTION_DECODE, ((Function) value).name()) ) { |
|
31 |
List<Value> parameters = ((Function) value).parameters(); |
|
32 |
if( parameters.size()==2) { |
|
33 |
Value p1 = parameters.get(0); |
|
34 |
Value p2 = parameters.get(1); |
|
35 |
if( p1 instanceof ExpressionBuilder.Constant && |
|
36 |
p2 instanceof ExpressionBuilder.Constant && |
|
37 |
((ExpressionBuilder.Constant)p1).value() instanceof String && |
|
38 |
((ExpressionBuilder.Constant)p2).value() instanceof String |
|
39 |
) { |
|
40 |
String p2value = ((ExpressionBuilder.Constant)p2).value().toString(); |
|
41 |
if( StringUtils.equalsIgnoreCase(p2value, "hex") ) { |
|
42 |
return true; |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
return false; |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public String format(Value function) { |
|
54 |
ExpressionBuilder builder = this.sqlbuilder.expression(); |
|
55 |
List<Value> parameters = ((Function) function).parameters(); |
|
56 |
Value p1 = parameters.get(0); |
|
57 |
String p1value = Objects.toString(((ExpressionBuilder.Constant)p1).value(), ""); |
|
58 |
String r = builder.string("\\x"+p1value)+"::bytea"; |
|
59 |
return r; |
|
60 |
} |
|
61 |
|
|
62 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_ExtentAggregate.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import static org.gvsig.expressionevaluator.GeometryExpressionBuilder.FUNCTION_ST_EXTENTAGGREGATE; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
public class ST_ExtentAggregate implements Formatter<Value> { |
|
18 |
|
|
19 |
private final SQLBuilder sqlbuilder; |
|
20 |
private final Formatter<Value> formatter; |
|
21 |
|
|
22 |
public ST_ExtentAggregate(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
23 |
this.sqlbuilder = sqlbuilder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
@Override |
|
27 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
28 |
if (value instanceof ExpressionBuilder.Function) { |
|
29 |
return StringUtils.equalsIgnoreCase(FUNCTION_ST_EXTENTAGGREGATE, ((Function) value).name()); |
|
30 |
} |
|
31 |
return false; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public String format(Value function) { |
|
36 |
List<Value> parameters = ((Function) function).parameters(); |
|
37 |
String p1 = parameters.get(0).toString(formatter); |
|
38 |
String r = MessageFormat.format("ST_Extent({0})", p1); |
|
39 |
return r; |
|
40 |
} |
|
41 |
|
|
42 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Sum.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
|
12 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_QUERY; |
|
13 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
14 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
15 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
16 |
import org.gvsig.timesupport.DataTypes; |
|
17 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
* @author jjdelcerro |
|
21 |
*/ |
|
22 |
class Sum implements Formatter<Value> { |
|
23 |
|
|
24 |
private final Formatter<Value> formatter; |
|
25 |
private final SQLBuilder builder; |
|
26 |
|
|
27 |
public Sum(SQLBuilder builder, Formatter<Value> formatter) { |
|
28 |
this.builder = builder; |
|
29 |
this.formatter = formatter; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public boolean canApply(Value value) { |
|
34 |
if (value instanceof Function) { |
|
35 |
return StringUtils.equalsIgnoreCase(ExpressionBuilder.FUNCTION_SUM, ((Function) value).name()); |
|
36 |
} |
|
37 |
return false; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public String format(Value function) { |
|
42 |
List<Value> parameters = ((Function) function).parameters(); |
|
43 |
Value p1 = parameters.get(0); |
|
44 |
|
|
45 |
if(p1 instanceof ExpressionBuilder.Variable) { |
|
46 |
FeatureType featureType = (FeatureType) p1.getProperty(PROP_FEATURE_TYPE); |
|
47 |
if(featureType != null){ |
|
48 |
ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) p1; |
|
49 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptorFromAll(variable.name()); |
|
50 |
if (attr == null) { |
|
51 |
FeatureQuery query = (FeatureQuery) p1.getProperty(PROP_QUERY); |
|
52 |
if(query != null){ |
|
53 |
attr = query.getExtraColumn().get(variable.name()); |
|
54 |
} |
|
55 |
} |
|
56 |
if(attr != null){ |
|
57 |
if(attr.getDataType().getType() == DataTypes.BOOLEAN){ |
|
58 |
String r = MessageFormat.format("SUM(({0})::INT)", p1.toString(formatter)); |
|
59 |
return r; |
|
60 |
} |
|
61 |
if(attr.getDataType().isNumeric()){ |
|
62 |
String r = MessageFormat.format("SUM(({0}))", p1.toString(formatter)); |
|
63 |
return r; |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
String r = MessageFormat.format("SUM(({0})::DECIMAL)", p1.toString(formatter)); |
|
70 |
return r; |
|
71 |
|
|
72 |
|
|
73 |
} |
|
74 |
|
|
75 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_GeomFromWKB.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
13 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author jjdelcerro |
|
18 |
*/ |
|
19 |
public class ST_GeomFromWKB implements Formatter<Value> { |
|
20 |
|
|
21 |
private final SQLBuilder sqlbuilder; |
|
22 |
private final Formatter<Value> formatter; |
|
23 |
|
|
24 |
public ST_GeomFromWKB(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
25 |
this.sqlbuilder = sqlbuilder; |
|
26 |
this.formatter = formatter; |
|
27 |
} |
|
28 |
@Override |
|
29 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
30 |
if (value instanceof ExpressionBuilder.Function) { |
|
31 |
return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_GEOMFROMWKB, ((Function) value).name()); |
|
32 |
} |
|
33 |
return false; |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public String format(Value function) { |
|
38 |
String r; |
|
39 |
String wkb; |
|
40 |
String srid; |
|
41 |
List<Value> parameters = ((Function) function).parameters(); |
|
42 |
switch(parameters.size()) { |
|
43 |
case 1: |
|
44 |
wkb = parameters.get(0).toString(formatter); |
|
45 |
r = MessageFormat.format("ST_GeomFromWKB({0})", wkb); |
|
46 |
break; |
|
47 |
case 2: |
|
48 |
wkb = parameters.get(0).toString(formatter); |
|
49 |
srid = parameters.get(1).toString(formatter); |
|
50 |
if( StringUtils.equals("0", srid) ) { |
|
51 |
r = MessageFormat.format("ST_GeomFromWKB({0})", wkb); |
|
52 |
} else { |
|
53 |
FeatureType ftype = (FeatureType) function.getProperty(PROP_FEATURE_TYPE); |
|
54 |
if( ftype == null || ftype.getDefaultSRS()!=null ) { |
|
55 |
r = MessageFormat.format("ST_GeomFromWKB({0},{1})", wkb, srid); |
|
56 |
} else { |
|
57 |
r = MessageFormat.format("ST_GeomFromWKB({0})", wkb); |
|
58 |
} |
|
59 |
} |
|
60 |
break; |
|
61 |
default: |
|
62 |
throw new IllegalArgumentException("Wrong number of arguments ("+parameters.size()+") in function ST_GeomFromWKB; requiered 1 or 2 arguments."); |
|
63 |
} |
|
64 |
return r; |
|
65 |
} |
|
66 |
|
|
67 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/IsNull.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.Code.Constant; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_IS; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
class IsNull implements Formatter<Value> { |
|
18 |
|
|
19 |
private final Formatter<Value> formatter; |
|
20 |
private final SQLBuilder builder; |
|
21 |
|
|
22 |
public IsNull(SQLBuilder builder, Formatter<Value> formatter) { |
|
23 |
this.builder = builder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean canApply(Value value) { |
|
29 |
if (value instanceof Function) { |
|
30 |
if( StringUtils.equalsIgnoreCase(OPERATOR_IS, ((Function) value).name()) ) { |
|
31 |
List<Value> parameters = ((Function) value).parameters(); |
|
32 |
Value p2 = parameters.get(1); |
|
33 |
if( p2 instanceof Constant && ((Constant)p2).value()==null ) { |
|
34 |
return true; |
|
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
return false; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public String format(Value function) { |
|
43 |
List<Value> parameters = ((Function) function).parameters(); |
|
44 |
String p1 = parameters.get(0).toString(formatter); |
|
45 |
String r = MessageFormat.format("( ({0}) ISNULL )", p1); |
|
46 |
return r; |
|
47 |
} |
|
48 |
|
|
49 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/JsonValue.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_IFNULL; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
import org.gvsig.tools.dataTypes.DataTypes; |
|
13 |
|
|
14 |
/** |
|
15 |
* |
|
16 |
* @author jjdelcerro |
|
17 |
*/ |
|
18 |
class JsonValue implements Formatter<Value> { |
|
19 |
|
|
20 |
private final Formatter<Value> formatter; |
|
21 |
private final SQLBuilder builder; |
|
22 |
|
|
23 |
public JsonValue(SQLBuilder builder, Formatter<Value> formatter) { |
|
24 |
this.builder = builder; |
|
25 |
this.formatter = formatter; |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public boolean canApply(Value value) { |
|
30 |
if (value instanceof Function) { |
|
31 |
if (value instanceof ExpressionBuilder.Function) { |
|
32 |
boolean canApply = StringUtils.equalsIgnoreCase(ExpressionBuilder.FUNCTION_JSON_VALUE, ((Function) value).name()); |
|
33 |
return canApply; |
|
34 |
} |
|
35 |
} |
|
36 |
return false; |
|
37 |
} |
|
38 |
|
|
39 |
@Override |
|
40 |
public String format(Value function) { |
|
41 |
List<Value> parameters = ((Function) function).parameters(); |
|
42 |
String p1 = parameters.get(0).toString(formatter); |
|
43 |
String p2 = parameters.get(1).toString(formatter); |
|
44 |
String p3 = null; |
|
45 |
if(parameters.size()==3) { |
|
46 |
p3 = parameters.get(2).toString(formatter).replace("'", ""); |
|
47 |
} |
|
48 |
|
|
49 |
String r; |
|
50 |
if (p3 != null) { |
|
51 |
r = MessageFormat.format( |
|
52 |
"jsonb_path_query_first({0}::jsonb, {1})::{2}", |
|
53 |
p1, |
|
54 |
p2, |
|
55 |
p3 |
|
56 |
); |
|
57 |
} else { |
|
58 |
r = MessageFormat.format( |
|
59 |
"jsonb_path_query_first({0}::jsonb, {1})", |
|
60 |
p1, |
|
61 |
p2 |
|
62 |
); |
|
63 |
} |
|
64 |
return r; |
|
65 |
|
|
66 |
} |
|
67 |
|
|
68 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_GeomFromText.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
13 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author jjdelcerro |
|
18 |
*/ |
|
19 |
public class ST_GeomFromText implements Formatter<Value> { |
|
20 |
|
|
21 |
private final SQLBuilder sqlbuilder; |
|
22 |
private final Formatter<Value> formatter; |
|
23 |
|
|
24 |
public ST_GeomFromText(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
25 |
this.sqlbuilder = sqlbuilder; |
|
26 |
this.formatter = formatter; |
|
27 |
} |
|
28 |
@Override |
|
29 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
30 |
if (value instanceof ExpressionBuilder.Function) { |
|
31 |
return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_GEOMFROMTEXT, ((Function) value).name()); |
|
32 |
} |
|
33 |
return false; |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public String format(Value function) { |
|
38 |
String r; |
|
39 |
String wkt; |
|
40 |
String srid; |
|
41 |
List<Value> parameters = ((Function) function).parameters(); |
|
42 |
switch(parameters.size()) { |
|
43 |
case 1: |
|
44 |
wkt = parameters.get(0).toString(formatter); |
|
45 |
r = MessageFormat.format("ST_GeomFromText({0})", wkt); |
|
46 |
break; |
|
47 |
case 2: |
|
48 |
wkt = parameters.get(0).toString(formatter); |
|
49 |
srid = parameters.get(1).toString(formatter); |
|
50 |
if( StringUtils.equals("0", srid) ) { |
|
51 |
r = MessageFormat.format("ST_GeomFromText({0})", wkt); |
|
52 |
} else { |
|
53 |
FeatureType ftype = (FeatureType) function.getProperty(PROP_FEATURE_TYPE); |
|
54 |
if( ftype == null || ftype.getDefaultSRS()!=null ) { |
|
55 |
r = MessageFormat.format("ST_GeomFromText({0},{1})", wkt, srid); |
|
56 |
} else { |
|
57 |
r = MessageFormat.format("ST_GeomFromText({0})", wkt); |
|
58 |
} |
|
59 |
} |
|
60 |
break; |
|
61 |
default: |
|
62 |
throw new IllegalArgumentException("Wrong number of arguments ("+parameters.size()+") in function ST_GeomFromText; requiered 1 or 2 arguments."); |
|
63 |
} |
|
64 |
return r; |
|
65 |
} |
|
66 |
|
|
67 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/IsNotNull.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.Code.Constant; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_IS; |
|
9 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_NOT; |
|
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
11 |
import org.gvsig.expressionevaluator.Formatter; |
|
12 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
13 |
|
|
14 |
/** |
|
15 |
* |
|
16 |
* @author jjdelcerro |
|
17 |
*/ |
|
18 |
class IsNotNull implements Formatter<Value> { |
|
19 |
|
|
20 |
private final Formatter<Value> formatter; |
|
21 |
private final SQLBuilder builder; |
|
22 |
|
|
23 |
public IsNotNull(SQLBuilder builder, Formatter<Value> formatter) { |
|
24 |
this.builder = builder; |
|
25 |
this.formatter = formatter; |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public boolean canApply(Value value_not) { |
|
30 |
if (value_not instanceof Function) { |
|
31 |
// NOT( IS( p1, NULL) ) |
|
32 |
if( StringUtils.equalsIgnoreCase(OPERATOR_NOT, ((Function) value_not).name()) ) { |
|
33 |
List<Value> parameters = ((Function) value_not).parameters(); |
|
34 |
Value value_is = parameters.get(0); |
|
35 |
if( value_is instanceof Function && StringUtils.equalsIgnoreCase(OPERATOR_IS, ((Function)value_is).name()) ) { |
|
36 |
List<Value> parameters_is = ((Function) value_is).parameters(); |
|
37 |
Value is_p2 = parameters_is.get(1); |
|
38 |
if( is_p2 instanceof Constant && ((Constant)is_p2).value()==null ) { |
|
39 |
return true; |
|
40 |
} |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
return false; |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public String format(Value value_not) { |
|
49 |
// NOT( IS( p1, NULL) ) |
|
50 |
|
|
51 |
List<Value> parameters = ((Function) value_not).parameters(); |
|
52 |
Value value_is = parameters.get(0); |
|
53 |
List<Value> parameters_is = ((Function)value_is).parameters(); |
|
54 |
Value is_p1 = parameters_is.get(0); |
|
55 |
String r = MessageFormat.format( |
|
56 |
"( ({0}) IS NOT NULL )", |
|
57 |
is_p1.toString(formatter) |
|
58 |
); |
|
59 |
return r; |
|
60 |
} |
|
61 |
|
|
62 |
} |
tags/org.gvsig.postgresql-2.0.244/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/PostgreSQLFormatter.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
4 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
5 |
import org.gvsig.expressionevaluator.Formatter; |
|
6 |
import org.gvsig.fmap.dal.DALLocator; |
|
7 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
8 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.$Constant; |
|
9 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.$Identifier; |
|
10 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ComputedAttribute; |
|
11 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.Exists; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ToDouble; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ToInteger; |
|
14 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ToLong; |
|
15 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ToString; |
|
16 |
|
|
17 |
/** |
|
18 |
* |
|
19 |
* @author jjdelcerro |
|
20 |
*/ |
|
21 |
public class PostgreSQLFormatter implements Formatter<Value> { |
|
22 |
|
|
23 |
private final SQLBuilder builder; |
|
24 |
private final Formatter<ExpressionBuilder.Value>[] formatters; |
|
25 |
|
|
26 |
@SuppressWarnings("LeakingThisInConstructor") |
|
27 |
public PostgreSQLFormatter(SQLBuilder builder) { |
|
28 |
|
|
29 |
this.builder = builder; |
|
30 |
this.formatters = new Formatter[]{ |
|
31 |
new Decode(this.builder, this), |
|
32 |
new Constant(this.builder, this), |
|
33 |
new ILike(this.builder, this), |
|
34 |
new IfNull(this.builder, this), |
Also available in: Unified diff