Revision 892
tags/org.gvsig.postgresql-2.0.176/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.176/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.176/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 |
<description>_PostgreSQL_loading_parameters</description> |
|
10 |
<fields> |
|
11 |
<field name="JDBCDriverClass" type="string" mandatory="true" |
|
12 |
defaultValue="org.postgresql.Driver" group="Advanced"> |
|
13 |
<description>JDBC Driver class</description> |
|
14 |
</field> |
|
15 |
<field name="port" type="integer" mandatory="false" |
|
16 |
defaultValue="5432" group="Connection"> |
|
17 |
<description></description> |
|
18 |
</field> |
|
19 |
<field name="host" type="string" mandatory="false" |
|
20 |
defaultValue="127.0.0.1" group="Connection"> |
|
21 |
<description></description> |
|
22 |
</field> |
|
23 |
<field name="UseSSL" type="boolean" mandatory="false" |
|
24 |
defaultValue="false" group="Basic"> |
|
25 |
<description>Use SSL connetion</description> |
|
26 |
</field> |
|
27 |
<field name="maxIdle" type="integer" mandatory="false" |
|
28 |
defaultValue="8" group="Advanced"> |
|
29 |
<description>The maximum number of connections that can remail idle in the pool.</description> |
|
30 |
</field> |
|
31 |
<field name="networkTimeout" type="integer" mandatory="false" |
|
32 |
defaultValue="30000" group="Advanced"> |
|
33 |
<description>The maximum period a connection or objects created from the connection will wait for the database to reply to any one request.</description> |
|
34 |
</field> |
|
35 |
</fields> |
|
36 |
</class> |
|
37 |
|
|
38 |
<class name="PostgreSQLStoreParameters"> |
|
39 |
<extends> |
|
40 |
<class>JDBC2StoreParameters</class> |
|
41 |
<class>PostgreSQLResourceParameters</class> |
|
42 |
</extends> |
|
43 |
<fields/> |
|
44 |
</class> |
|
45 |
|
|
46 |
<class name="PostgreSQLNewStoreParameters"> |
|
47 |
<extends> |
|
48 |
<class>JDBC2NewStoreParameters</class> |
|
49 |
<class>PostgreSQLResourceParameters</class> |
|
50 |
</extends> |
|
51 |
<fields/> |
|
52 |
</class> |
|
53 |
|
|
54 |
|
|
55 |
<class name="PostgreSQLServerExplorerParameters"> |
|
56 |
<extends> |
|
57 |
<class>PostgreSQLResourceParameters</class> |
|
58 |
<class>JDBC2ServerExplorerParameters</class> |
|
59 |
</extends> |
|
60 |
<fields/> |
|
61 |
</class> |
|
62 |
|
|
63 |
|
|
64 |
</classes> |
|
65 |
</definitions> |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLBuilder.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
|
|
25 |
package org.gvsig.postgresql.dal; |
|
26 |
|
|
27 |
import java.sql.Connection; |
|
28 |
import java.sql.DatabaseMetaData; |
|
29 |
import java.text.MessageFormat; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.List; |
|
32 |
import org.apache.commons.lang3.StringUtils; |
|
33 |
import org.gvsig.expressionevaluator.Formatter; |
|
34 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
35 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
36 |
import org.gvsig.postgresql.dal.expressionbuilderformatter.PostgreSQLFormatter; |
|
37 |
|
|
38 |
@SuppressWarnings("UseSpecificCatch") |
|
39 |
public class PostgreSQLBuilder extends JDBCSQLBuilderBase { |
|
40 |
|
|
41 |
protected Formatter formatter = null; |
|
42 |
|
|
43 |
public static class Version { |
|
44 |
|
|
45 |
private final int major; |
|
46 |
private final int minor; |
|
47 |
|
|
48 |
public Version(int major, int minor) { |
|
49 |
this.major = major; |
|
50 |
this.minor = minor; |
|
51 |
} |
|
52 |
|
|
53 |
public int getMajor() { |
|
54 |
return major; |
|
55 |
} |
|
56 |
|
|
57 |
public int getMinor() { |
|
58 |
return minor; |
|
59 |
} |
|
60 |
|
|
61 |
} |
|
62 |
private Version databaseVersion = null; |
|
63 |
|
|
64 |
public Version getDatabaseVersion() { |
|
65 |
if( databaseVersion == null ) { |
|
66 |
Connection conn = null; |
|
67 |
try { |
|
68 |
conn = this.getHelper().getConnection(); |
|
69 |
DatabaseMetaData metadata = conn.getMetaData(); |
|
70 |
databaseVersion = new Version( |
|
71 |
metadata.getDatabaseMajorVersion(), |
|
72 |
metadata.getDatabaseMinorVersion() |
|
73 |
); |
|
74 |
} catch (Exception ex) { |
|
75 |
databaseVersion = new Version(0,0); |
|
76 |
} finally { |
|
77 |
this.getHelper().closeConnectionQuietly(conn); |
|
78 |
} |
|
79 |
} |
|
80 |
return databaseVersion; |
|
81 |
} |
|
82 |
|
|
83 |
public PostgreSQLBuilder(JDBCHelper helper) { |
|
84 |
super(helper); |
|
85 |
this.defaultSchema = "public"; |
|
86 |
this.supportSchemas = true; |
|
87 |
this.allowAutomaticValues = true; |
|
88 |
this.geometrySupportType = this.helper.getGeometrySupportType(); |
|
89 |
this.hasSpatialFunctions = this.helper.hasSpatialFunctions(); |
|
90 |
|
|
91 |
this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null; |
|
92 |
this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null; |
|
93 |
|
|
94 |
this.STMT_UPDATE_TABLE_STATISTICS_table = "ANALYZE {0}"; |
|
95 |
|
|
96 |
// config.set(SQLConfig.ST_GeomFromEWKB, "ST_GeomFromEWKB({0}, {1})"); |
|
97 |
// config.set(SQLConfig.ST_AsEWKB, "ST_AsEWKB(({0}))"); |
|
98 |
// config.set(SQLConfig.ST_ExtentAggregate, "ST_Extent({0})"); |
|
99 |
// config.set(SQLConfig.ST_UnionAggregate, "ST_Union({0})"); |
|
100 |
|
|
101 |
// config.set(SQLConfig.isNull, "( ({0}) ISNULL )"); |
|
102 |
// config.set(SQLConfig.notIsNull, "( ({0}) NOT NULL )"); |
|
103 |
|
|
104 |
} |
|
105 |
|
|
106 |
public class PostgreSQLUpdateTableStatisticsBuilderBase extends UpdateTableStatisticsBuilderBase { |
|
107 |
@Override |
|
108 |
public List<String> toStrings() { |
|
109 |
List<String> sqls = new ArrayList<>(); |
|
110 |
|
|
111 |
if( !StringUtils.isBlank(STMT_UPDATE_TABLE_STATISTICS_table) ) { |
|
112 |
// In postGIS, UpdateLayerStatistics function, don't allow to |
|
113 |
// use the database name in the table name. |
|
114 |
String name = as_identifier(this.table.getName()); |
|
115 |
if( table.has_schema()) { |
|
116 |
name = as_identifier(this.table.getSchema()) + "." + name; |
|
117 |
} |
|
118 |
String sql = MessageFormat.format( |
|
119 |
STMT_UPDATE_TABLE_STATISTICS_table, |
|
120 |
name |
|
121 |
); |
|
122 |
if( !StringUtils.isEmpty(sql) ) { |
|
123 |
sqls.add(sql); |
|
124 |
} |
|
125 |
} |
|
126 |
return sqls; |
|
127 |
} |
|
128 |
} |
|
129 |
protected class PostgreSQLCreateIndexBuilder extends CreateIndexBuilderBase { |
|
130 |
|
|
131 |
@Override |
|
132 |
public List<String> toStrings(Formatter formatter) { |
|
133 |
StringBuilder builder = new StringBuilder(); |
|
134 |
builder.append("CREATE "); |
|
135 |
// if( this.isUnique ) { |
|
136 |
// builder.append("UNIQUE "); |
|
137 |
// } |
|
138 |
builder.append("INDEX "); |
|
139 |
if( this.ifNotExist ) { |
|
140 |
Version version = getDatabaseVersion(); |
|
141 |
if( version.getMajor()>=9 && version.getMinor()>=5 ) { |
|
142 |
builder.append("IF NOT EXISTS "); |
|
143 |
} |
|
144 |
} |
|
145 |
builder.append(as_identifier(this.indexName)); |
|
146 |
builder.append(" ON "); |
|
147 |
builder.append(this.table.toString(formatter)); |
|
148 |
if( this.isSpatial ) { |
|
149 |
builder.append(" USING GIST "); |
|
150 |
} |
|
151 |
builder.append(" ( "); |
|
152 |
boolean is_first_column = true; |
|
153 |
for( String column : this.columns) { |
|
154 |
if( is_first_column ) { |
|
155 |
is_first_column = false; |
|
156 |
} else { |
|
157 |
builder.append(", "); |
|
158 |
} |
|
159 |
builder.append(as_identifier(column)); |
|
160 |
} |
|
161 |
builder.append(" )"); |
|
162 |
|
|
163 |
List<String> sqls = new ArrayList<>(); |
|
164 |
sqls.add(builder.toString()); |
|
165 |
return sqls; |
|
166 |
} |
|
167 |
|
|
168 |
} |
|
169 |
|
|
170 |
protected class PostgreSQLCreateTableBuilder extends CreateTableBuilderBase { |
|
171 |
|
|
172 |
@Override |
|
173 |
public List<String> toStrings(Formatter formatter) { |
|
174 |
// |
|
175 |
// https://www.postgresql.org/docs/9.1/static/sql-createtable.html |
|
176 |
// |
|
177 |
List<String> sqls = new ArrayList<>(); |
|
178 |
StringBuilder builder = new StringBuilder(); |
|
179 |
|
|
180 |
builder.append("CREATE TABLE "); |
|
181 |
builder.append(this.table.toString(formatter)); |
|
182 |
builder.append(" ("); |
|
183 |
boolean first = true; |
|
184 |
for (ColumnDescriptor column : columns) { |
|
185 |
if( column.isGeometry() ) { |
|
186 |
continue; |
|
187 |
} |
|
188 |
if (first) { |
|
189 |
first = false; |
|
190 |
} else { |
|
191 |
builder.append(", "); |
|
192 |
} |
|
193 |
builder.append(as_identifier(column.getName())); |
|
194 |
builder.append(" "); |
|
195 |
if( column.isAutomatic() ) { |
|
196 |
builder.append(" SERIAL"); |
|
197 |
} else { |
|
198 |
builder.append(sqltype( |
|
199 |
column.getType(), |
|
200 |
column.getSize(), |
|
201 |
column.getPrecision(), |
|
202 |
column.getScale(), |
|
203 |
column.getGeometryType(), |
|
204 |
column.getGeometrySubtype() |
|
205 |
)); |
|
206 |
if (column.getDefaultValue() == null) { |
|
207 |
if (column.allowNulls()) { |
|
208 |
builder.append(" DEFAULT NULL"); |
|
209 |
} |
|
210 |
} else { |
|
211 |
builder.append(" DEFAULT '"); |
|
212 |
builder.append(column.getDefaultValue().toString()); |
|
213 |
builder.append("'"); |
|
214 |
} |
|
215 |
if (column.allowNulls()) { |
|
216 |
builder.append(" NULL"); |
|
217 |
} else { |
|
218 |
builder.append(" NOT NULL"); |
|
219 |
} |
|
220 |
} |
|
221 |
if (column.isPrimaryKey()) { |
|
222 |
builder.append(" PRIMARY KEY"); |
|
223 |
} |
|
224 |
} |
|
225 |
builder.append(" )"); |
|
226 |
sqls.add(builder.toString()); |
|
227 |
|
|
228 |
String AddGeometryColumn = "SELECT AddGeometryColumn({0} , {1} , {2}, {3,number,#######} , {4} , {5}, {6})"; |
|
229 |
for (ColumnDescriptor column : columns) { |
|
230 |
if( column.isGeometry() ) { |
|
231 |
String sql = MessageFormat.format( |
|
232 |
AddGeometryColumn, |
|
233 |
as_string(this.table.has_schema()?this.table.getSchema():"public"), |
|
234 |
as_string(this.table.getName()), |
|
235 |
as_string(column.getName()), |
|
236 |
column.getGeometrySRSId(), |
|
237 |
as_string(sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype())), |
|
238 |
as_string(sqlgeometrydimension(column.getGeometryType(), column.getGeometrySubtype())), |
|
239 |
as_string(column.allowNulls()) |
|
240 |
); |
|
241 |
sqls.add(sql); |
|
242 |
} |
|
243 |
} |
|
244 |
return sqls; |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
public class PostgreSQLSelectBuilderBase extends SelectBuilderBase { |
|
249 |
|
|
250 |
@Override |
|
251 |
protected boolean isValid(StringBuilder message) { |
|
252 |
if( message == null ) { |
|
253 |
message = new StringBuilder(); |
|
254 |
} |
|
255 |
if( this.has_offset() && !this.has_order_by() ) { |
|
256 |
// Algunos gestores de BBDD requieren que se especifique un |
|
257 |
// orden para poder usar OFFSET. Como eso parece buena idea para |
|
258 |
// asegurar que siempre tengamos los mismo resultados, lo exijimos |
|
259 |
// siempre. |
|
260 |
message.append("Can't use OFFSET without an ORDER BY."); |
|
261 |
return false; |
|
262 |
} |
|
263 |
return true; |
|
264 |
} |
|
265 |
|
|
266 |
@Override |
|
267 |
public String toString(Formatter formatter) { |
|
268 |
// |
|
269 |
// https://www.postgresql.org/docs/9.1/static/sql-select.html |
|
270 |
// |
|
271 |
StringBuilder builder = new StringBuilder(); |
|
272 |
if( !isValid(builder) ) { |
|
273 |
throw new IllegalStateException(builder.toString()); |
|
274 |
} |
|
275 |
builder.append("SELECT "); |
|
276 |
if( this.distinct ) { |
|
277 |
builder.append("DISTINCT "); |
|
278 |
} |
|
279 |
boolean first = true; |
|
280 |
for (SelectColumnBuilder column : columns) { |
|
281 |
if (first) { |
|
282 |
first = false; |
|
283 |
} else { |
|
284 |
builder.append(", "); |
|
285 |
} |
|
286 |
builder.append(column.toString(formatter)); |
|
287 |
} |
|
288 |
|
|
289 |
if ( this.has_from() ) { |
|
290 |
builder.append(" FROM "); |
|
291 |
builder.append(this.from.toString(formatter)); |
|
292 |
} |
|
293 |
|
|
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 |
protected 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 |
protected SelectBuilder createSelectBuilder() { |
|
365 |
return new PostgreSQLSelectBuilderBase(); |
|
366 |
} |
|
367 |
|
|
368 |
@Override |
|
369 |
protected UpdateTableStatisticsBuilder createUpdateTableStatisticsBuilder() { |
|
370 |
return new PostgreSQLUpdateTableStatisticsBuilderBase(); |
|
371 |
} |
|
372 |
|
|
373 |
} |
tags/org.gvsig.postgresql-2.0.176/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.176/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.176/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.176/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.176/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.176/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.176/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.176/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.Exists; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author jjdelcerro |
|
13 |
*/ |
|
14 |
public class PostgreSQLFormatter implements Formatter<Value> { |
|
15 |
|
|
16 |
private final SQLBuilder builder; |
|
17 |
private final Formatter<ExpressionBuilder.Value>[] formatters; |
|
18 |
|
|
19 |
@SuppressWarnings("LeakingThisInConstructor") |
|
20 |
public PostgreSQLFormatter(SQLBuilder builder) { |
|
21 |
|
|
22 |
this.builder = builder; |
|
23 |
this.formatters = new Formatter[]{ |
|
24 |
new Decode(this.builder, this), |
|
25 |
new Constant(this.builder, this), |
|
26 |
new ILike(this.builder, this), |
|
27 |
new IfNull(this.builder, this), |
|
28 |
new IsNull(this.builder, this), |
|
29 |
new IsNotNull(this.builder, this), |
|
30 |
new ST_ExtentAggregate(this.builder, this), |
|
31 |
new ST_GeomFromWKB(this.builder, this), |
|
32 |
new ST_GeomFromText(this.builder, this), |
|
33 |
new Getattr(this.builder, this), |
|
34 |
// new Iff(this), |
|
35 |
new Exists(this), |
|
36 |
DALLocator.getDataManager().createDALExpressionBuilder().formatter(this), |
|
37 |
}; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public boolean canApply(Value value) { |
|
42 |
for (Formatter<Value> formatter : formatters) { |
|
43 |
if (formatter.canApply(value)) { |
|
44 |
return true; |
|
45 |
} |
|
46 |
} |
|
47 |
return false; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public String format(Value value) { |
|
52 |
for (Formatter<Value> formatter : formatters) { |
|
53 |
if (formatter.canApply(value)) { |
|
54 |
return formatter.format(value); |
|
55 |
} |
|
56 |
} |
|
57 |
return value.toString(this); |
|
58 |
} |
|
59 |
|
|
60 |
} |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Constant.java | ||
---|---|---|
1 |
package org.gvsig.postgresql.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
6 |
import org.gvsig.expressionevaluator.Formatter; |
|
7 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
8 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
9 |
import org.gvsig.fmap.geom.Geometry; |
|
10 |
import org.gvsig.fmap.geom.GeometryUtils; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
public class Constant implements Formatter<Value> { |
|
17 |
|
|
18 |
private static final String FORMAT_ST_GEOMFROMWKB = "ST_GeomFromWKB(({0}), ({1}))"; |
|
19 |
private static final String FORMAT_ST_GEOMFROMEWKB = "ST_GeomFromEWKB(({0}), ({1}))"; |
|
20 |
private static final String FORMAT_ST_GEOMFROMTEXT = "ST_GeomFromText(({0}), ({1}))"; |
|
21 |
|
|
22 |
private final SQLBuilder sqlbuilder; |
|
23 |
private final Formatter<Value> formatter; |
|
24 |
|
|
25 |
public Constant(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
26 |
this.sqlbuilder = sqlbuilder; |
|
27 |
this.formatter = formatter; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
32 |
if (value instanceof ExpressionBuilder.Constant) { |
|
33 |
Object x = ((ExpressionBuilder.Constant)value).value(); |
|
34 |
if( x instanceof byte[] ) { |
|
35 |
return true; |
|
36 |
} |
|
37 |
if( x instanceof Geometry ) { |
|
38 |
return true; |
|
39 |
} |
|
40 |
} |
|
41 |
return false; |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public String format(Value constant) { |
|
46 |
Object x = ((ExpressionBuilder.Constant)constant).value(); |
|
47 |
GeometryExpressionBuilder builder = this.sqlbuilder.expression(); |
|
48 |
if( x instanceof byte[] ) { |
|
49 |
return builder.string("\\x"+builder.bytearray_hex((byte[]) x))+"::bytea"; |
|
50 |
} |
|
51 |
if( x instanceof Geometry ) { |
|
52 |
Geometry geometry = (Geometry) x; |
|
53 |
switch (builder.geometry_support_type()) { |
|
54 |
case EWKB: |
|
55 |
return MessageFormat.format( |
|
56 |
FORMAT_ST_GEOMFROMEWKB, |
|
57 |
builder.string("\\x"+builder.bytearray_hex(GeometryUtils.toEWKB(geometry)))+"::bytea", |
|
58 |
String.valueOf(builder.srs_id(geometry.getProjection())) |
|
59 |
); |
|
60 |
case WKB: |
|
61 |
return MessageFormat.format( |
|
62 |
FORMAT_ST_GEOMFROMWKB, |
|
63 |
builder.string("\\x"+builder.bytearray_hex(GeometryUtils.toWKB(geometry)))+"::bytea", |
|
64 |
String.valueOf(builder.srs_id(geometry.getProjection())) |
|
65 |
); |
|
66 |
case WKT: |
|
67 |
default: |
|
68 |
return MessageFormat.format( |
|
69 |
FORMAT_ST_GEOMFROMTEXT, |
|
70 |
builder.string(GeometryUtils.toWKT(geometry)), |
|
71 |
String.valueOf(builder.srs_id(geometry.getProjection())) |
|
72 |
); |
|
73 |
} |
|
74 |
|
|
75 |
} |
|
76 |
return null; |
|
77 |
} |
|
78 |
|
|
79 |
} |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/IfNull.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 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
class IfNull implements Formatter<Value> { |
|
18 |
|
|
19 |
private final Formatter<Value> formatter; |
|
20 |
private final SQLBuilder builder; |
|
21 |
|
|
22 |
public IfNull(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 (value instanceof ExpressionBuilder.Function) { |
|
31 |
return StringUtils.equalsIgnoreCase(FUNCTION_IFNULL, ((Function) value).name()); |
|
32 |
} |
|
33 |
} |
|
34 |
return false; |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public String format(Value function) { |
|
39 |
List<Value> parameters = ((Function) function).parameters(); |
|
40 |
String p1 = parameters.get(0).toString(formatter); |
|
41 |
String p2 = parameters.get(1).toString(formatter); |
|
42 |
String p3 = parameters.get(2).toString(formatter); |
|
43 |
String r = MessageFormat.format( |
|
44 |
"CASE WHEN ({0}) IS NULL THEN ({1}) ELSE ({2}) END", |
|
45 |
p1, |
|
46 |
p3, |
|
47 |
p2 |
|
48 |
); |
|
49 |
return r; |
|
50 |
|
|
51 |
} |
|
52 |
|
|
53 |
} |
tags/org.gvsig.postgresql-2.0.176/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.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/FakePostgreSQLConnectionParameters.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.postgresql.dal; |
|
7 |
|
|
8 |
import org.gvsig.fmap.dal.store.jdbc2.spi.FakeConnectionParameters; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author fdiaz |
|
13 |
*/ |
|
14 |
public class FakePostgreSQLConnectionParameters extends FakeConnectionParameters implements PostgreSQLConnectionParameters { |
|
15 |
|
|
16 |
@Override |
|
17 |
public boolean getUseSSL() { |
|
18 |
return false; |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public int getMaxIdle() { |
|
23 |
return 8; |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public int getNetworkTimeout() { |
|
28 |
return 30000; |
|
29 |
} |
|
30 |
|
|
31 |
} |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLNewStoreParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.postgresql.dal; |
|
23 |
|
|
24 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
25 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
26 |
|
|
27 |
public class PostgreSQLNewStoreParameters |
|
28 |
extends |
|
29 |
JDBCNewStoreParameters |
|
30 |
implements |
|
31 |
PostgreSQLConnectionParameters |
|
32 |
{ |
|
33 |
|
|
34 |
private final PostgreSQLConnectionParametersHelper helper; |
|
35 |
|
|
36 |
public PostgreSQLNewStoreParameters() { |
|
37 |
super( |
|
38 |
PostgreSQLLibrary.NAME + "NewStoreParameters", |
|
39 |
PostgreSQLLibrary.NAME |
|
40 |
); |
|
41 |
this.helper = new PostgreSQLConnectionParametersHelper(this); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public String getUrl() { |
|
46 |
return this.helper.getUrl(); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public void validate() throws ValidateDataParametersException { |
|
51 |
this.helper.validate(); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public boolean getUseSSL() { |
|
56 |
return this.helper.getUseSSL(); |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public int getMaxIdle() { |
|
61 |
return this.helper.getMaxIdle(); |
|
62 |
} |
|
63 |
|
|
64 |
public void setUseSSL(boolean v) { |
|
65 |
this.helper.setUseSSL(v); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public int getNetworkTimeout() { |
|
70 |
return this.helper.getNetworkTimeout(); |
|
71 |
} |
|
72 |
|
|
73 |
} |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLServerExplorerParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
/** |
|
23 |
* |
|
24 |
*/ |
|
25 |
package org.gvsig.postgresql.dal; |
|
26 |
|
|
27 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
28 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
29 |
|
|
30 |
|
|
31 |
public class PostgreSQLServerExplorerParameters extends |
|
32 |
JDBCServerExplorerParameters |
|
33 |
implements |
|
34 |
PostgreSQLConnectionParameters |
|
35 |
{ |
|
36 |
|
|
37 |
private final PostgreSQLConnectionParametersHelper helper; |
|
38 |
|
|
39 |
public PostgreSQLServerExplorerParameters() { |
|
40 |
super( |
|
41 |
PostgreSQLLibrary.NAME + "ServerExplorerParameters", |
|
42 |
PostgreSQLLibrary.NAME |
|
43 |
); |
|
44 |
this.helper = new PostgreSQLConnectionParametersHelper(this); |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public String getUrl() { |
|
49 |
return this.helper.getUrl(); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void validate() throws ValidateDataParametersException { |
|
54 |
// Esto seria para convertir los parametros de gvSIG 2.3 a 2.4. |
|
55 |
// if( !StringUtils.equalsIgnoreCase(PostgreSQLLibrary.NAME, (CharSequence) getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME)) ) { |
|
56 |
// setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, PostgreSQLLibrary.NAME); |
|
57 |
// } |
|
58 |
this.helper.validate(); |
|
59 |
super.validate(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public boolean getUseSSL() { |
|
64 |
return this.helper.getUseSSL(); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public int getMaxIdle() { |
|
69 |
return this.helper.getMaxIdle(); |
|
70 |
} |
|
71 |
|
|
72 |
public void setUseSSL(boolean v) { |
|
73 |
this.helper.setUseSSL(v); |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public int getNetworkTimeout() { |
|
78 |
return this.helper.getNetworkTimeout(); |
|
79 |
} |
|
80 |
} |
tags/org.gvsig.postgresql-2.0.176/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLHelper.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.postgresql.dal; |
|
3 |
|
|
4 |
import java.io.IOException; |
|
5 |
import java.sql.Connection; |
|
6 |
import java.sql.SQLException; |
|
7 |
import org.apache.commons.dbcp2.BasicDataSource; |
|
8 |
import org.apache.commons.lang3.BooleanUtils; |
|
9 |
import org.apache.commons.lang3.StringUtils; |
|
10 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
|
11 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
14 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
15 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
16 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
|
17 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
18 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
|
19 |
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider; |
|
20 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
|
21 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
22 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase; |
|
23 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverDumb; |
|
24 |
import org.gvsig.postgresql.dal.operations.PostgreSQLOperationsFactory; |
|
25 |
import org.gvsig.tools.dispose.Disposable; |
|
26 |
import org.gvsig.tools.exception.BaseException; |
|
27 |
import org.slf4j.Logger; |
|
28 |
import org.slf4j.LoggerFactory; |
|
29 |
|
|
30 |
public class PostgreSQLHelper extends JDBCHelperBase { |
|
31 |
|
|
32 |
/* friend */ static final Logger LOGGER = LoggerFactory.getLogger(PostgreSQLHelper.class); |
|
33 |
|
|
34 |
public static final String POSTGRESQL_JDBC_DRIVER = "org.postgresql.Driver"; |
|
35 |
|
|
36 |
public static String getConnectionURL(PostgreSQLConnectionParameters params) { |
|
37 |
return getConnectionURL( |
|
38 |
params.getHost(), |
|
39 |
params.getPort(), |
|
40 |
params.getDBName() |
|
41 |
); |
|
42 |
} |
|
43 |
|
|
44 |
public static String getConnectionURL(String host, Integer port, String db) { |
|
45 |
if( StringUtils.isEmpty(host) ) { |
|
46 |
throw new IllegalArgumentException("Parameter 'host' can't be null."); |
|
47 |
} |
|
48 |
String connectionURL = "jdbc:postgresql://" + host; |
|
49 |
if (port != null) { |
|
50 |
connectionURL = connectionURL + ":" + port; |
|
51 |
} |
|
52 |
connectionURL = connectionURL + "/" + db; |
|
53 |
LOGGER.debug("connectionURL: {}", connectionURL); |
|
54 |
return connectionURL; |
|
55 |
} |
|
56 |
|
|
57 |
private static class PostgreSQLConnectionProvider implements Disposable, ConnectionProvider { |
|
58 |
|
|
59 |
private static boolean needRegisterDriver = true; |
|
60 |
|
|
61 |
private BasicDataSource dataSource = null; |
|
62 |
|
|
63 |
private PostgreSQLConnectionParameters connectionParameters; |
|
64 |
|
|
65 |
public PostgreSQLConnectionProvider(PostgreSQLConnectionParameters connectionParameters) { |
|
66 |
this.connectionParameters = connectionParameters; |
|
67 |
} |
|
68 |
|
|
69 |
public Connection getConnection() throws SQLException { |
|
70 |
if (this.dataSource == null) { |
|
71 |
this.dataSource = this.createDataSource(); |
|
72 |
} |
|
73 |
if( LOGGER.isDebugEnabled() ) { |
|
74 |
LOGGER.debug("getConnection:\n" + getStatusInformation()); |
|
75 |
} |
|
76 |
Connection conn; |
|
77 |
try { |
|
78 |
conn = this.dataSource.getConnection(); |
|
79 |
} catch(Throwable ex) { |
|
80 |
LOGGER.debug("Error getting connection from pool.",ex); |
|
81 |
throw ex; |
|
82 |
} |
|
83 |
try { |
|
84 |
conn.setNetworkTimeout(null, this.connectionParameters.getNetworkTimeout()); |
|
85 |
} catch(Throwable ex) { |
|
86 |
LOGGER.warn("Error setting the network timeout.",ex); |
|
87 |
} |
|
88 |
if( LOGGER.isDebugEnabled() ) { |
|
89 |
LOGGER.debug("Created connection: {}\n NumActive: {}\n NumIdle: {}", |
|
90 |
new Object[] { |
|
91 |
conn.hashCode(), |
|
92 |
this.dataSource.getNumActive(), |
|
93 |
this.dataSource.getNumIdle() |
|
94 |
} |
|
95 |
); |
|
96 |
} |
|
97 |
return conn; |
|
98 |
} |
|
99 |
|
|
100 |
public String getStatusInformation() { |
|
101 |
StringBuilder builder = new StringBuilder(); |
|
102 |
builder.append("BasicDataSource pool status:\n"); |
|
103 |
builder.append(" Connection URL: '").append(this.dataSource.getUrl()).append("'\n"); |
|
104 |
if( this.dataSource.getInitialSize()>0 ) { |
|
105 |
builder.append(" InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n"); |
|
106 |
} |
|
107 |
if( this.dataSource.isPoolPreparedStatements() ) { |
|
108 |
builder.append(" PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n"); |
|
109 |
builder.append(" MaxOpenPreparedStatements: ").append(this.dataSource.getMaxOpenPreparedStatements()).append(" (The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit)\n"); |
|
110 |
} |
|
111 |
builder.append(" MaxTotal: ").append(this.dataSource.getMaxTotal()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n"); |
|
112 |
builder.append(" MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n"); |
|
113 |
builder.append(" NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n"); |
|
114 |
builder.append(" NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n"); |
|
115 |
return builder.toString(); |
|
116 |
} |
|
117 |
|
|
118 |
private BasicDataSource createDataSource() throws SQLException { |
|
119 |
if (!this.isRegistered()) { |
|
120 |
this.registerDriver(); |
|
121 |
} |
|
122 |
PostgreSQLConnectionParameters params = connectionParameters; |
|
123 |
|
|
124 |
BasicDataSource ds = new BasicDataSource(); |
|
125 |
ds.setMaxIdle(params.getMaxIdle()); |
|
126 |
ds.setDriverClassName(params.getJDBCDriverClassName()); |
|
127 |
if( params.getUseSSL() ) { |
|
128 |
String s = BooleanUtils.toStringTrueFalse(params.getUseSSL()); |
|
129 |
ds.addConnectionProperty("ssl", s ); |
|
130 |
} |
|
131 |
if( !StringUtils.isEmpty(params.getUser()) ) { |
|
132 |
ds.setUsername(params.getUser()); |
|
133 |
} |
|
134 |
if( !StringUtils.isEmpty(params.getPassword()) ) { |
|
135 |
ds.setPassword(params.getPassword()); |
|
136 |
} |
|
137 |
ds.setUrl(params.getUrl()); |
Also available in: Unified diff