Revision 131
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/test/java/org/gvsig/fmap/dal/store/postgresql/DatabaseMetadataChecks.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package org.gvsig.fmap.dal.store.postgresql; |
|
5 |
|
|
6 |
import java.sql.Connection; |
|
7 |
import java.sql.DatabaseMetaData; |
|
8 |
import java.sql.DriverManager; |
|
9 |
import java.sql.ResultSet; |
|
10 |
import java.sql.ResultSetMetaData; |
|
11 |
import java.sql.SQLException; |
|
12 |
|
|
13 |
/** |
|
14 |
* @author cordin |
|
15 |
* |
|
16 |
*/ |
|
17 |
public class DatabaseMetadataChecks { |
|
18 |
|
|
19 |
// Columns as specified by the DatabaseMetadata.getTables javadoc |
|
20 |
private static final String[] COLUMNS = new String[] { |
|
21 |
// Metadata resultset column names |
|
22 |
"TABLE_CAT", // String => table catalog (may be null) |
|
23 |
"TABLE_SCHEM", // String => table schema (may be null) |
|
24 |
"TABLE_NAME", // String => table name |
|
25 |
"TABLE_TYPE", // String => table type. Typical types are "TABLE", |
|
26 |
// "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", |
|
27 |
// "LOCAL TEMPORARY", "ALIAS", "SYNONYM". |
|
28 |
"REMARKS", // String => explanatory comment on the table |
|
29 |
"TYPE_CAT", // String => the types catalog (may be null) |
|
30 |
"TYPE_SCHEM", // String => the types schema (may be null) |
|
31 |
"TYPE_NAME", // String => type name (may be null) |
|
32 |
"SELF_REFERENCING_COL_NAME", // String => name of the designated |
|
33 |
// "identifier" column of a typed |
|
34 |
// table (may be null) |
|
35 |
"REF_GENERATION", // String => specifies how values in |
|
36 |
// SELF_REFERENCING_COL_NAME are created. Values |
|
37 |
// are "SYSTEM", "USER", "DERIVED". (may be |
|
38 |
// null) |
|
39 |
}; |
|
40 |
|
|
41 |
public void showTables() throws Exception { |
|
42 |
|
|
43 |
Connection conn = DriverManager.getConnection( |
|
44 |
"jdbc:postgresql://localhost/gvsigDB", "gvsig", "gvsig"); |
|
45 |
|
|
46 |
try { |
|
47 |
// Statement st = conn.createStatement(); |
|
48 |
// String sql = "select tablename from pg_tables"; |
|
49 |
// ResultSet result = st.executeQuery(sql); |
|
50 |
|
|
51 |
DatabaseMetaData metadata = conn.getMetaData(); |
|
52 |
ResultSet result = metadata.getTables(null, null, null, |
|
53 |
new String[] { "TABLE", "VIEW" }); |
|
54 |
ResultSetMetaData resultMD = result.getMetaData(); |
|
55 |
String[] columns = new String[resultMD.getColumnCount()]; |
|
56 |
for (int i = 0; i < columns.length; i++) { |
|
57 |
columns[i] = resultMD.getColumnName(i + 1); |
|
58 |
} |
|
59 |
|
|
60 |
for (int i = 0; i < columns.length; i++) { |
|
61 |
System.out.print(columns[i]); |
|
62 |
System.out.print(";"); |
|
63 |
} |
|
64 |
System.out.println(); |
|
65 |
while (result.next()) { |
|
66 |
|
|
67 |
for (int i = 0; i < columns.length; i++) { |
|
68 |
System.out.print(result.getString(columns[i])); |
|
69 |
System.out.print(";"); |
|
70 |
} |
|
71 |
System.out.println(); |
|
72 |
} |
|
73 |
|
|
74 |
} finally { |
|
75 |
try { |
|
76 |
conn.close(); |
|
77 |
} catch (SQLException e) { |
|
78 |
e.printStackTrace(); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* @param args |
|
86 |
*/ |
|
87 |
public static void main(String[] args) { |
|
88 |
try { |
|
89 |
new DatabaseMetadataChecks().showTables(); |
|
90 |
} catch (Exception e) { |
|
91 |
e.printStackTrace(); |
|
92 |
System.exit(-1); |
|
93 |
} |
|
94 |
System.exit(0); |
|
95 |
} |
|
96 |
|
|
97 |
} |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/test/java/org/gvsig/fmap/dal/store/postgresql/TestPostgreSQLWrite.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 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 IVER T.I {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
/** |
|
29 |
* |
|
30 |
*/ |
|
31 |
package org.gvsig.fmap.dal.store.postgresql; |
|
32 |
|
|
33 |
import java.util.Iterator; |
|
34 |
|
|
35 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
36 |
import org.gvsig.fmap.dal.exception.DataException; |
|
37 |
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore; |
|
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
41 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
43 |
import org.gvsig.fmap.dal.feature.FeatureStoreTransform; |
|
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
45 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
46 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
|
47 |
import org.gvsig.fmap.geom.Geometry; |
|
48 |
import org.gvsig.fmap.geom.operation.towkt.ToWKT; |
|
49 |
import org.gvsig.tools.evaluator.Evaluator; |
|
50 |
import org.gvsig.tools.evaluator.EvaluatorData; |
|
51 |
import org.gvsig.tools.evaluator.EvaluatorException; |
|
52 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
/** |
|
57 |
* @author jmvivo |
|
58 |
* |
|
59 |
*/ |
|
60 |
// public class TestPostgreSQL extends BaseTestEditableFeatureStore { |
|
61 |
public class TestPostgreSQLWrite extends BaseTestEditableFeatureStore { |
|
62 |
|
|
63 |
|
|
64 |
private JDBCServerExplorer myExplorer; |
|
65 |
private PostgreSQLNewStoreParameters newParams; |
|
66 |
|
|
67 |
@Override |
|
68 |
protected void doSetUp() throws Exception { |
|
69 |
// Initialize wkt geom operation |
|
70 |
int wktCode = ToWKT.CODE; |
|
71 |
} |
|
72 |
|
|
73 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
74 |
throws DataException { |
|
75 |
PostgreSQLStoreParameters pgParameters = null; |
|
76 |
pgParameters = (PostgreSQLStoreParameters) dataManager |
|
77 |
.createStoreParameters(PostgreSQLStoreProvider.NAME); |
|
78 |
|
|
79 |
pgParameters.setHost("localhost"); |
|
80 |
pgParameters.setUser("postgres"); |
|
81 |
pgParameters.setPassword("postgres"); |
|
82 |
pgParameters.setDBName("gis"); |
|
83 |
pgParameters.setTable("muni10000_peq"); |
|
84 |
|
|
85 |
return pgParameters; |
|
86 |
} |
|
87 |
|
|
88 |
public boolean hasExplorer() { |
|
89 |
return true; |
|
90 |
} |
|
91 |
|
|
92 |
public void testLoadMetadata() throws Exception { |
|
93 |
DataStoreParameters params = this.getDefaultDataStoreParameters(); |
|
94 |
|
|
95 |
FeatureStore store = null; |
|
96 |
store = (FeatureStore) dataManager.createStore(params); |
|
97 |
FeatureType fType = store.getDefaultFeatureType(); |
|
98 |
FeatureAttributeDescriptor geomAttr; |
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
if (fType.getDefaultGeometryAttributeIndex() >= 0) { |
|
103 |
assertNotNull(store.getEnvelope()); |
|
104 |
geomAttr = fType.getAttributeDescriptor(fType |
|
105 |
.getDefaultGeometryAttributeIndex()); |
|
106 |
assertTrue(geomAttr.getGeometryType() == Geometry.TYPES.MULTISURFACE); |
|
107 |
assertTrue(geomAttr.getGeometrySubType() == Geometry.SUBTYPES.GEOM2D); |
|
108 |
assertNotNull(store.getDynValue("CRS")); |
|
109 |
|
|
110 |
} |
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 |
public void testCloserConnection() throws Exception { |
|
115 |
|
|
116 |
DataStoreParameters params = this.getDefaultDataStoreParameters(); |
|
117 |
|
|
118 |
FeatureStore store = null; |
|
119 |
store = (FeatureStore) dataManager.createStore(params); |
|
120 |
|
|
121 |
FeatureQuery query = store.createFeatureQuery(); |
|
122 |
|
|
123 |
query.getOrder().add("gid", true); |
|
124 |
|
|
125 |
query.setFilter(new Evaluator() { |
|
126 |
|
|
127 |
public Object evaluate(EvaluatorData data) |
|
128 |
throws EvaluatorException { |
|
129 |
// TODO Auto-generated method stub |
|
130 |
return Boolean.TRUE; |
|
131 |
} |
|
132 |
|
|
133 |
public String getSQL() { |
|
134 |
return "true = true"; |
|
135 |
} |
|
136 |
|
|
137 |
public String getDescription() { |
|
138 |
// TODO Auto-generated method stub |
|
139 |
return null; |
|
140 |
} |
|
141 |
|
|
142 |
public String getName() { |
|
143 |
return "AlwaysTrue"; |
|
144 |
} |
|
145 |
|
|
146 |
public EvaluatorFieldsInfo getFieldsInfo() { |
|
147 |
// TODO Auto-generated method stub |
|
148 |
return null; |
|
149 |
} |
|
150 |
|
|
151 |
}); |
|
152 |
|
|
153 |
FeatureStoreTransform transform = new StringsToLowerTransform(); |
|
154 |
transform.setFeatureStore(store); |
|
155 |
|
|
156 |
store.getTransforms().add(transform); |
|
157 |
|
|
158 |
transform = new StringsToLowerTransform(); |
|
159 |
transform.setFeatureStore(store); |
|
160 |
|
|
161 |
store.getTransforms().add(transform); |
|
162 |
|
|
163 |
transform = new StringsToLowerTransform(); |
|
164 |
transform.setFeatureStore(store); |
|
165 |
|
|
166 |
store.getTransforms().add(transform); |
|
167 |
|
|
168 |
for (int i = 0; i < 30; i++) { |
|
169 |
// this.fullStoreIteratorTest(store); |
|
170 |
|
|
171 |
this.testIterationFastAndStandart(store, query); |
|
172 |
} |
|
173 |
|
|
174 |
|
|
175 |
store.dispose(); |
|
176 |
|
|
177 |
} |
|
178 |
|
|
179 |
public boolean usesResources() { |
|
180 |
return true; |
|
181 |
} |
|
182 |
|
|
183 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters() |
|
184 |
throws Exception { |
|
185 |
PostgreSQLStoreParameters parameters = (PostgreSQLStoreParameters) this |
|
186 |
.getDefaultDataStoreParameters(); |
|
187 |
|
|
188 |
FeatureStore store = null; |
|
189 |
|
|
190 |
if (this.myExplorer == null) { |
|
191 |
store = (FeatureStore) dataManager.createStore(parameters); |
|
192 |
myExplorer = (JDBCServerExplorer) store.getExplorer(); |
|
193 |
} |
|
194 |
if (this.newParams == null) { |
|
195 |
if (store == null){ |
|
196 |
store = (FeatureStore) dataManager.createStore(parameters); |
|
197 |
} |
|
198 |
|
|
199 |
newParams = (PostgreSQLNewStoreParameters) myExplorer |
|
200 |
.getAddParameters(); |
|
201 |
|
|
202 |
newParams.setTable(parameters.getTable() + "_test"); |
|
203 |
FeatureType ftOrg = store.getDefaultFeatureType(); |
|
204 |
EditableFeatureType ftTrg = (EditableFeatureType) newParams |
|
205 |
.getDefaultFeatureType(); |
|
206 |
FeatureAttributeDescriptor org; |
|
207 |
EditableFeatureAttributeDescriptor trg; |
|
208 |
Iterator iter = ftOrg.iterator(); |
|
209 |
while (iter.hasNext()) { |
|
210 |
org = (FeatureAttributeDescriptor) iter.next(); |
|
211 |
trg = ftTrg.add(org.getName(), org.getType()); |
|
212 |
trg.setAllowNull(org.allowNull()); |
|
213 |
trg.setDefaultValue(org.getDefaultValue()); |
|
214 |
trg.setGeometrySubType(org.getGeometrySubType()); |
|
215 |
trg.setGeometryType(org.getGeometryType()); |
|
216 |
trg.setIsAutomatic(org.isAutomatic()); |
|
217 |
trg.setIsPrimaryKey(org.isPrimaryKey()); |
|
218 |
trg.setIsReadOnly(org.isReadOnly()); |
|
219 |
trg.setMaximumOccurrences(org.getMaximumOccurrences()); |
|
220 |
trg.setMinimumOccurrences(org.getMinimumOccurrences()); |
|
221 |
trg.setPrecision(org.getPrecision()); |
|
222 |
trg.setSize(org.getSize()); |
|
223 |
trg.setSRS(org.getSRS()); |
|
224 |
} |
|
225 |
ftTrg.setDefaultGeometryAttributeName(ftOrg |
|
226 |
.getDefaultGeometryAttributeName()); |
|
227 |
ftTrg.setHasOID(ftOrg.hasOID()); |
|
228 |
|
|
229 |
} |
|
230 |
if (store != null){ |
|
231 |
store.dispose(); |
|
232 |
} |
|
233 |
|
|
234 |
return this.newParams; |
|
235 |
} |
|
236 |
|
|
237 |
public boolean resourcesNotifyChanges() { |
|
238 |
return false; |
|
239 |
} |
|
240 |
} |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/test/java/org/gvsig/fmap/dal/store/postgresql/TestReadAndWriteGeom.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.fmap.dal.store.postgresql; |
|
26 |
|
|
27 |
import java.sql.Connection; |
|
28 |
import java.sql.DriverManager; |
|
29 |
import java.sql.PreparedStatement; |
|
30 |
import java.sql.ResultSet; |
|
31 |
import java.sql.SQLException; |
|
32 |
import java.sql.Statement; |
|
33 |
import java.text.MessageFormat; |
|
34 |
|
|
35 |
import org.gvsig.fmap.geom.Geometry; |
|
36 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
37 |
import org.gvsig.fmap.geom.GeometryManager; |
|
38 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
|
39 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext; |
|
40 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
|
41 |
import org.gvsig.fmap.geom.operation.towkt.ToWKT; |
|
42 |
import org.gvsig.fmap.geom.primitive.Point; |
|
43 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
|
44 |
|
|
45 |
public class TestReadAndWriteGeom extends AbstractLibraryAutoInitTestCase { |
|
46 |
private static final int FEATURES_TO_INSERT = 12000; |
|
47 |
private static final String TABLE_NAME_INSERT = "testReadadnwritegeom_testcase"; |
|
48 |
private static final String FIELD_NAME_INSERT = "geom"; |
|
49 |
private Connection conn; |
|
50 |
private GeometryManager geoManager; |
|
51 |
private static String SQL_FOR_READ_TEST = "Select {0} from medio_ejes"; |
|
52 |
|
|
53 |
@Override |
|
54 |
protected void doSetUp() throws Exception { |
|
55 |
geoManager = GeometryLocator.getGeometryManager(); |
|
56 |
|
|
57 |
Class klass = Class.forName(PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME); |
|
58 |
if (klass == null) { |
|
59 |
throw new Exception("Driver not found: " |
|
60 |
+ PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME); |
|
61 |
} |
|
62 |
|
|
63 |
// inicializamos operaciones de geometrias de las que dependemos |
|
64 |
int code = FromWKB.CODE; |
|
65 |
code = ToWKB.CODE; |
|
66 |
code = ToWKT.CODE; |
|
67 |
conn = DriverManager.getConnection(PostgreSQLLibrary.getJdbcUrl( |
|
68 |
"localhost", 5432, "gis"), "postgres", "postgres"); |
|
69 |
} |
|
70 |
|
|
71 |
public void testInsertWKB() throws Exception { |
|
72 |
PreparedStatement pst = null; |
|
73 |
try { |
|
74 |
crearTablaTest(); |
|
75 |
Point geom = geoManager.createPoint(0, 0, |
|
76 |
Geometry.SUBTYPES.GEOM2D); |
|
77 |
pst = conn.prepareStatement( |
|
78 |
"Insert into " |
|
79 |
+ TABLE_NAME_INSERT.toLowerCase() + |
|
80 |
" (" |
|
81 |
+ FIELD_NAME_INSERT + ") Values (GeomFromWKB(?))"); |
|
82 |
|
|
83 |
|
|
84 |
int i; |
|
85 |
for (i = 1; i <= FEATURES_TO_INSERT; i++) { |
|
86 |
pst.clearParameters(); |
|
87 |
geom.setX(i); |
|
88 |
geom.setY(i); |
|
89 |
pst.setBytes(1, |
|
90 |
(byte[]) geom.invokeOperation( |
|
91 |
ToWKB.CODE, |
|
92 |
null) |
|
93 |
); |
|
94 |
|
|
95 |
pst.executeUpdate(); |
|
96 |
} |
|
97 |
|
|
98 |
System.out |
|
99 |
.println("TestReadAndWriteGeom.testInsertPostgis() Inserteds= i"); |
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
} finally { |
|
105 |
if (pst != null) { |
|
106 |
try {pst.close();} catch (SQLException e) {e.printStackTrace();} |
|
107 |
} |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 |
private void crearTablaTest() throws SQLException { |
|
115 |
execute("DROP TABLE IF EXISTS " + TABLE_NAME_INSERT.toLowerCase()); |
|
116 |
execute("Delete from geometry_columns where f_table_name = '" |
|
117 |
+ TABLE_NAME_INSERT.toLowerCase() + "'"); |
|
118 |
execute("CREATE TABLE " + TABLE_NAME_INSERT.toLowerCase() |
|
119 |
+ " (id serial PRIMARY KEY)"); |
|
120 |
execute("Select AddGeometryColumn('" + TABLE_NAME_INSERT.toLowerCase() |
|
121 |
+ "','" |
|
122 |
+ FIELD_NAME_INSERT + "',-1,'GEOMETRY',2)"); |
|
123 |
} |
|
124 |
|
|
125 |
private void execute(String sql) throws SQLException { |
|
126 |
Statement st = null; |
|
127 |
try { |
|
128 |
st = conn.createStatement(); |
|
129 |
st.execute(sql); |
|
130 |
} finally { |
|
131 |
if (st != null) { |
|
132 |
try {st.close();} catch (SQLException e) {e.printStackTrace();} |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
} |
|
137 |
|
|
138 |
public String getSQLForRead(String geoColumn) { |
|
139 |
return MessageFormat.format(SQL_FOR_READ_TEST, geoColumn); |
|
140 |
|
|
141 |
} |
|
142 |
|
|
143 |
public void testReadBinary() throws Exception { |
|
144 |
// st.execute("declare " + getTableName() + myCursorId + |
|
145 |
// "_wkb_cursor binary scroll cursor with hold for " + sqlAux); |
|
146 |
// rs = st.executeQuery("fetch forward " + FETCH_SIZE+ " in " + |
|
147 |
// getTableName() + myCursorId + "_wkb_cursor"); |
|
148 |
|
|
149 |
Statement st = null; |
|
150 |
ResultSet rs = null; |
|
151 |
long count = 0; |
|
152 |
Geometry geom; |
|
153 |
|
|
154 |
try { |
|
155 |
st = conn.createStatement(); |
|
156 |
String cursorName = "myCursor___xxx"; |
|
157 |
st.execute("declare " + cursorName |
|
158 |
+ "_wkb_cursor binary scroll cursor with hold for " |
|
159 |
+ getSQLForRead("the_geom")); |
|
160 |
rs = st.executeQuery("fetch forward all in " + cursorName |
|
161 |
+ "_wkb_cursor"); |
|
162 |
byte[] buff; |
|
163 |
FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext(); |
|
164 |
while (rs.next()) { |
|
165 |
count++; |
|
166 |
buff = rs.getBytes(1); |
|
167 |
} |
|
168 |
|
|
169 |
System.out |
|
170 |
.println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
171 |
+ count); |
|
172 |
|
|
173 |
} finally { |
|
174 |
if (st != null) { |
|
175 |
try { |
|
176 |
st.close(); |
|
177 |
} catch (SQLException e) { |
|
178 |
e.printStackTrace(); |
|
179 |
} |
|
180 |
} |
|
181 |
if (rs != null) { |
|
182 |
try { |
|
183 |
rs.close(); |
|
184 |
} catch (SQLException e) { |
|
185 |
e.printStackTrace(); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
} |
|
191 |
|
|
192 |
public void testReadWKB() throws Exception { |
|
193 |
Statement st = null; |
|
194 |
ResultSet rs = null; |
|
195 |
long count = 0; |
|
196 |
Geometry geom; |
|
197 |
|
|
198 |
byte[] buff; |
|
199 |
FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext(); |
|
200 |
try { |
|
201 |
st = conn.createStatement(); |
|
202 |
rs = st.executeQuery(getSQLForRead("asBinary(the_geom)")); |
|
203 |
while (rs.next()) { |
|
204 |
count++; |
|
205 |
buff = rs.getBytes(1); |
|
206 |
} |
|
207 |
|
|
208 |
System.out |
|
209 |
.println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
210 |
+ count); |
|
211 |
|
|
212 |
|
|
213 |
} finally { |
|
214 |
if (st != null) { |
|
215 |
try { |
|
216 |
st.close(); |
|
217 |
} catch (SQLException e) { |
|
218 |
e.printStackTrace(); |
|
219 |
} |
|
220 |
} |
|
221 |
if (rs != null) { |
|
222 |
try { |
|
223 |
rs.close(); |
|
224 |
} catch (SQLException e) { |
|
225 |
e.printStackTrace(); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
// public void testReadPostgis() throws Exception { |
|
232 |
// Statement st = null; |
|
233 |
// ResultSet rs = null; |
|
234 |
// long count = 0; |
|
235 |
// Geometry geom; |
|
236 |
// PGgeometry pgGeom; |
|
237 |
// PostGIS2Geometry converter = new PostGIS2Geometry(); |
|
238 |
// try { |
|
239 |
// st = conn.createStatement(); |
|
240 |
// rs = st.executeQuery(getSQLForRead("the_geom")); |
|
241 |
// while (rs.next()) { |
|
242 |
// count++; |
|
243 |
// pgGeom = (PGgeometry) rs.getObject(1); |
|
244 |
// if (pgGeom != null) { |
|
245 |
// geom = converter.getGeometry(pgGeom); |
|
246 |
// assertNotNull(geom); |
|
247 |
// } |
|
248 |
// } |
|
249 |
// |
|
250 |
// |
|
251 |
// System.out |
|
252 |
// .println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
253 |
// + count); |
|
254 |
// |
|
255 |
// |
|
256 |
// } finally { |
|
257 |
// if (st != null) { |
|
258 |
// try { |
|
259 |
// st.close(); |
|
260 |
// } catch (SQLException e) { |
|
261 |
// e.printStackTrace(); |
|
262 |
// } |
|
263 |
// } |
|
264 |
// if (rs != null) { |
|
265 |
// try { |
|
266 |
// rs.close(); |
|
267 |
// } catch (SQLException e) { |
|
268 |
// e.printStackTrace(); |
|
269 |
// } |
|
270 |
// } |
|
271 |
// } |
|
272 |
// } |
|
273 |
|
|
274 |
|
|
275 |
protected void tearDown() throws Exception { |
|
276 |
super.tearDown(); |
|
277 |
conn.close(); |
|
278 |
} |
|
279 |
|
|
280 |
} |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/test/java/org/gvsig/fmap/dal/store/postgresql/TestPostgreSQL.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 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 IVER T.I {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
/** |
|
29 |
* |
|
30 |
*/ |
|
31 |
package org.gvsig.fmap.dal.store.postgresql; |
|
32 |
|
|
33 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
34 |
import org.gvsig.fmap.dal.exception.DataException; |
|
35 |
import org.gvsig.fmap.dal.feature.BaseTestFeatureStore; |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
39 |
import org.gvsig.fmap.dal.feature.FeatureStoreTransform; |
|
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
41 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
42 |
import org.gvsig.fmap.geom.Geometry; |
|
43 |
import org.gvsig.tools.evaluator.Evaluator; |
|
44 |
import org.gvsig.tools.evaluator.EvaluatorData; |
|
45 |
import org.gvsig.tools.evaluator.EvaluatorException; |
|
46 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
|
47 |
|
|
48 |
/** |
|
49 |
* @author jmvivo |
|
50 |
* |
|
51 |
*/ |
|
52 |
// public class TestPostgreSQL extends BaseTestEditableFeatureStore { |
|
53 |
public class TestPostgreSQL extends BaseTestFeatureStore { |
|
54 |
|
|
55 |
@Override |
|
56 |
protected void doSetUp() throws Exception { |
|
57 |
super.doSetUp(); |
|
58 |
} |
|
59 |
|
|
60 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
61 |
throws DataException { |
|
62 |
PostgreSQLStoreParameters pgParameters = null; |
|
63 |
pgParameters = (PostgreSQLStoreParameters) dataManager |
|
64 |
.createStoreParameters(PostgreSQLStoreProvider.NAME); |
|
65 |
|
|
66 |
pgParameters.setHost("localhost"); |
|
67 |
pgParameters.setUser("postgres"); |
|
68 |
pgParameters.setPassword("postgres"); |
|
69 |
pgParameters.setDBName("gis"); |
|
70 |
pgParameters.setTable("muni10000_peq"); |
|
71 |
|
|
72 |
return pgParameters; |
|
73 |
} |
|
74 |
|
|
75 |
public boolean hasExplorer() { |
|
76 |
return true; |
|
77 |
} |
|
78 |
|
|
79 |
public void testLoadMetadata() throws Exception { |
|
80 |
DataStoreParameters params = this.getDefaultDataStoreParameters(); |
|
81 |
|
|
82 |
FeatureStore store = null; |
|
83 |
store = (FeatureStore) dataManager.createStore(params); |
|
84 |
FeatureType fType = store.getDefaultFeatureType(); |
|
85 |
FeatureAttributeDescriptor geomAttr; |
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
if (fType.getDefaultGeometryAttributeIndex() >= 0) { |
|
90 |
assertNotNull(store.getEnvelope()); |
|
91 |
geomAttr = fType.getAttributeDescriptor(fType |
|
92 |
.getDefaultGeometryAttributeIndex()); |
|
93 |
assertTrue(geomAttr.getGeometryType() == Geometry.TYPES.MULTISURFACE); |
|
94 |
assertTrue(geomAttr.getGeometrySubType() == Geometry.SUBTYPES.GEOM2D); |
|
95 |
assertNotNull(store.getDynValue("CRS")); |
|
96 |
|
|
97 |
} |
|
98 |
|
|
99 |
} |
|
100 |
|
|
101 |
public void testCloserConnection() throws Exception { |
|
102 |
|
|
103 |
DataStoreParameters params = this.getDefaultDataStoreParameters(); |
|
104 |
|
|
105 |
FeatureStore store = null; |
|
106 |
store = (FeatureStore) dataManager.createStore(params); |
|
107 |
|
|
108 |
FeatureQuery query = store.createFeatureQuery(); |
|
109 |
|
|
110 |
query.getOrder().add("gid", true); |
|
111 |
|
|
112 |
query.setFilter(new Evaluator() { |
|
113 |
|
|
114 |
public Object evaluate(EvaluatorData data) |
|
115 |
throws EvaluatorException { |
|
116 |
// TODO Auto-generated method stub |
|
117 |
return Boolean.TRUE; |
|
118 |
} |
|
119 |
|
|
120 |
public String getSQL() { |
|
121 |
return "true = true"; |
|
122 |
} |
|
123 |
|
|
124 |
public String getDescription() { |
|
125 |
// TODO Auto-generated method stub |
|
126 |
return null; |
|
127 |
} |
|
128 |
|
|
129 |
public String getName() { |
|
130 |
return "AlwaysTrue"; |
|
131 |
} |
|
132 |
|
|
133 |
public EvaluatorFieldsInfo getFieldsInfo() { |
|
134 |
// TODO Auto-generated method stub |
|
135 |
return null; |
|
136 |
} |
|
137 |
|
|
138 |
}); |
|
139 |
|
|
140 |
FeatureStoreTransform transform = new StringsToLowerTransform(); |
|
141 |
transform.setFeatureStore(store); |
|
142 |
|
|
143 |
store.getTransforms().add(transform); |
|
144 |
|
|
145 |
transform = new StringsToLowerTransform(); |
|
146 |
transform.setFeatureStore(store); |
|
147 |
|
|
148 |
store.getTransforms().add(transform); |
|
149 |
|
|
150 |
transform = new StringsToLowerTransform(); |
|
151 |
transform.setFeatureStore(store); |
|
152 |
|
|
153 |
store.getTransforms().add(transform); |
|
154 |
|
|
155 |
for (int i = 0; i < 30; i++) { |
|
156 |
// this.fullStoreIteratorTest(store); |
|
157 |
|
|
158 |
this.testIterationFastAndStandart(store, query); |
|
159 |
} |
|
160 |
|
|
161 |
|
|
162 |
store.dispose(); |
|
163 |
|
|
164 |
} |
|
165 |
|
|
166 |
public boolean usesResources() { |
|
167 |
return true; |
|
168 |
} |
|
169 |
|
|
170 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters() |
|
171 |
throws Exception { |
|
172 |
// TODO Auto-generated method stub |
|
173 |
return null; |
|
174 |
} |
|
175 |
} |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/main/resources/org/gvsig/fmap/dal/store/postgresql/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="JDBC" 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.31/org.gvsig.postgresql.provider/src/main/resources/org/gvsig/fmap/dal/store/postgresql/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>JDBCResourceParameters</class> |
|
8 |
</extends> |
|
9 |
<fields> |
|
10 |
<field name="JDBCDriverClass" type="string" mandatory="true" |
|
11 |
defaultValue="org.postgresql.Driver" group="Advanced"> |
|
12 |
<description>JDBC Driver class</description> |
|
13 |
</field> |
|
14 |
<field name="port" type="integer" mandatory="false" |
|
15 |
defaultValue="5432" group="Connection"> |
|
16 |
<description></description> |
|
17 |
</field> |
|
18 |
<field name="UseSSL" type="boolean" mandatory="false" |
|
19 |
defaultValue="false" group="Basic"> |
|
20 |
<description>Use SSL connetion</description> |
|
21 |
</field> |
|
22 |
</fields> |
|
23 |
</class> |
|
24 |
|
|
25 |
<class name="PostgreSQLStoreParameters"> |
|
26 |
<extends> |
|
27 |
<class>JDBCStoreParameters</class> |
|
28 |
<class>PostgreSQLResourceParameters</class> |
|
29 |
</extends> |
|
30 |
<fields/> |
|
31 |
</class> |
|
32 |
|
|
33 |
<class name="PostgreSQLNewStoreParameters"> |
|
34 |
<extends> |
|
35 |
<class>JDBCNewStoreParameters</class> |
|
36 |
<class>PostgreSQLResourceParameters</class> |
|
37 |
</extends> |
|
38 |
<fields/> |
|
39 |
</class> |
|
40 |
|
|
41 |
|
|
42 |
<class name="PostgreSQLServerExplorerParameters"> |
|
43 |
<extends> |
|
44 |
<class>PostgreSQLResourceParameters</class> |
|
45 |
<class>JDBCServerExplorerParameters</class> |
|
46 |
</extends> |
|
47 |
<fields/> |
|
48 |
</class> |
|
49 |
|
|
50 |
|
|
51 |
</classes> |
|
52 |
</definitions> |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.fmap.dal.store.postgresql.PostgreSQLLibrary |
tags/org.gvsig.postgresql-2.0.31/org.gvsig.postgresql.provider/src/main/java/org/gvsig/fmap/dal/store/postgresql/PostgreSQLHelper.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 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 IVER T.I {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
/** |
|
29 |
* |
|
30 |
*/ |
|
31 |
package org.gvsig.fmap.dal.store.postgresql; |
|
32 |
|
|
33 |
import java.sql.Connection; |
|
34 |
import java.sql.PreparedStatement; |
|
35 |
import java.sql.ResultSet; |
|
36 |
import java.sql.ResultSetMetaData; |
|
37 |
import java.sql.SQLException; |
|
38 |
import java.sql.Statement; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Comparator; |
|
41 |
import java.util.Iterator; |
|
42 |
import java.util.List; |
|
43 |
import java.util.Map; |
|
44 |
import java.util.Properties; |
|
45 |
import java.util.TreeMap; |
|
46 |
import java.util.TreeSet; |
|
47 |
|
|
48 |
import org.cresques.cts.IProjection; |
|
49 |
import org.postgresql.PGResultSetMetaData; |
|
50 |
import org.slf4j.Logger; |
|
51 |
import org.slf4j.LoggerFactory; |
|
52 |
|
|
53 |
import org.gvsig.fmap.crs.CRSFactory; |
|
54 |
import org.gvsig.fmap.dal.DALLocator; |
|
55 |
import org.gvsig.fmap.dal.DataTypes; |
|
56 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
|
57 |
import org.gvsig.fmap.dal.exception.DataException; |
|
58 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
59 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
60 |
import org.gvsig.fmap.dal.exception.WriteException; |
|
61 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
62 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
63 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
64 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
65 |
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
|
66 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
|
67 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
68 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
|
69 |
import org.gvsig.fmap.dal.store.jdbc.ConnectionAction; |
|
70 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
|
71 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelperUser; |
|
72 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
73 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
74 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
|
75 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
|
76 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
|
77 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
|
78 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
|
79 |
import org.gvsig.fmap.geom.Geometry; |
|
80 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
81 |
import org.gvsig.tools.ToolsLocator; |
|
82 |
import org.gvsig.tools.exception.BaseException; |
|
83 |
|
|
84 |
/** |
|
85 |
* @author jmvivo |
|
86 |
* |
|
87 |
*/ |
|
88 |
public class PostgreSQLHelper extends JDBCHelper { |
|
89 |
|
|
90 |
private static Logger logger = LoggerFactory |
|
91 |
.getLogger(PostgreSQLHelper.class); |
|
92 |
|
|
93 |
private Map pgSR2SRSID = new TreeMap(); |
|
94 |
private Map srsID2pgSR = new TreeMap(); |
|
95 |
|
|
96 |
private static Properties beforePostgis13 = null; |
|
97 |
private int[] postGISVersion = { 0,0,0 }; |
|
98 |
private boolean versionSet = false; |
|
99 |
|
|
100 |
PostgreSQLHelper(JDBCHelperUser consumer, |
|
101 |
PostgreSQLConnectionParameters params) |
|
102 |
throws InitializeException { |
|
103 |
|
|
104 |
super(consumer, params); |
|
105 |
} |
|
106 |
|
|
107 |
protected void initializeResource() throws InitializeException { |
|
108 |
ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator |
|
109 |
.getResourceManager(); |
|
110 |
PostgreSQLResource resource = (PostgreSQLResource) manager |
|
111 |
.createAddResource( |
|
112 |
PostgreSQLResource.NAME, new Object[] { |
|
113 |
params.getUrl(), params.getHost(), |
|
114 |
params.getPort(), params.getDBName(), params.getUser(), |
|
115 |
params.getPassword(), |
|
116 |
params.getJDBCDriverClassName(), |
|
117 |
((PostgreSQLConnectionParameters) params).getUseSSL() }); |
|
118 |
this.setResource(resource); |
|
119 |
} |
|
120 |
|
|
121 |
|
|
122 |
protected String getDefaultSchema(Connection conn) |
|
123 |
throws JDBCException { |
|
124 |
if (defaultSchema == null) { |
|
125 |
String sql = "Select current_schema()"; |
|
126 |
ResultSet rs = null; |
|
127 |
Statement st = null; |
|
128 |
String schema = null; |
|
129 |
try { |
|
130 |
st = conn.createStatement(); |
|
131 |
try { |
|
132 |
rs = JDBCHelper.executeQuery(st, sql); |
|
133 |
} catch (java.sql.SQLException e) { |
|
134 |
throw new JDBCExecuteSQLException(sql, e); |
|
135 |
} |
|
136 |
rs.next(); |
|
137 |
schema = rs.getString(1); |
|
138 |
} catch (java.sql.SQLException e) { |
|
139 |
throw new JDBCSQLException(e); |
|
140 |
} finally { |
|
141 |
try {rs.close();} catch (Exception e) {logger.error("Exception clossing resulset", e);}; |
|
142 |
try {st.close();} catch (Exception e) {logger.error("Exception clossing statement", e);}; |
|
143 |
rs = null; |
|
144 |
st = null; |
|
145 |
} |
|
146 |
defaultSchema = schema; |
|
147 |
} |
|
148 |
|
|
149 |
return defaultSchema; |
|
150 |
} |
|
151 |
|
|
152 |
public Envelope getFullEnvelopeOfField( |
|
153 |
JDBCStoreParameters storeParams, |
|
154 |
String geometryAttrName, Envelope limit) |
|
155 |
throws DataException { |
|
156 |
|
|
157 |
StringBuilder strb = new StringBuilder(); |
|
158 |
strb.append("Select " + getFunctionName("ST_AsBinary") + "(" |
|
159 |
+ getFunctionName("ST_Extent") + "("); |
|
160 |
strb.append(escapeFieldName(geometryAttrName)); |
|
161 |
strb.append(")) from "); |
|
162 |
|
|
163 |
if (storeParams.getSQL() != null |
|
164 |
&& storeParams.getSQL().trim().length() > 0) { |
|
165 |
strb.append('('); |
|
166 |
strb.append(storeParams.getSQL()); |
|
167 |
strb.append(") as _subquery_alias_ "); |
|
168 |
} else { |
|
169 |
strb.append(storeParams.tableID()); |
|
170 |
} |
|
171 |
|
|
172 |
if (limit != null || (storeParams.getBaseFilter() != null |
|
173 |
&& storeParams.getBaseFilter().trim().length() > 0)) { |
|
174 |
strb.append(" where "); |
|
175 |
|
|
176 |
if (limit != null) { |
|
177 |
strb.append(" ( " + getFunctionName("ST_Intersects") + "(" |
|
178 |
+ getFunctionName("ST_GeomFromText") + "('"); |
|
179 |
String workAreaWkt = null; |
|
180 |
try { |
|
181 |
workAreaWkt = limit.getGeometry().convertToWKT(); |
|
182 |
} catch (Exception e) { |
|
183 |
throw new CreateGeometryException(e); |
|
184 |
} |
|
185 |
strb.append(workAreaWkt); |
|
186 |
strb.append("', "); |
|
187 |
|
|
188 |
IProjection proj = storeParams.getCRS(); |
|
189 |
int sridInt = this.getProviderSRID(proj); |
|
190 |
if (sridInt == -1) { |
|
191 |
throw new CreateGeometryException( |
|
192 |
new Exception("CRS is null or unknown.")); |
|
193 |
} else { |
|
194 |
strb.append(Integer.toString(sridInt)); |
|
195 |
} |
|
196 |
strb.append("), " + getFunctionName("ST_Envelope") + "("); |
|
197 |
strb.append(escapeFieldName(geometryAttrName)); |
|
198 |
strb.append(")) ) "); |
|
199 |
|
|
200 |
} |
|
201 |
if (storeParams.getBaseFilter() != null && storeParams.getBaseFilter().trim().length() > 0) { |
|
202 |
if (limit != null) { |
|
203 |
strb.append(" and "); |
|
204 |
} |
|
205 |
strb.append(" ( "); |
|
206 |
strb.append(storeParams.getBaseFilter()); |
|
207 |
strb.append(" ) "); |
|
208 |
} |
|
209 |
|
|
210 |
} |
|
211 |
|
|
212 |
final String sql = strb.toString(); |
|
213 |
|
|
214 |
this.open(); |
|
215 |
|
|
216 |
return (Envelope) getResource().execute(new ResourceAction() { |
|
217 |
public String toString() { |
|
218 |
return "getEnvelope"; |
|
219 |
} |
|
220 |
|
|
221 |
public Object run() throws Exception { |
|
222 |
ResultSet rs = null; |
|
223 |
Statement st = null; |
|
224 |
Connection conn = null; |
|
225 |
Envelope fullEnvelope = null; |
|
226 |
|
|
227 |
Envelope emptyEnv |
|
228 |
= geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D); |
|
229 |
|
|
230 |
try { |
|
231 |
|
|
232 |
conn = getConnection(); |
|
233 |
st = conn.createStatement(); |
|
234 |
try { |
|
235 |
rs = JDBCHelper.executeQuery(st, sql); |
|
236 |
} catch (java.sql.SQLException e) { |
|
237 |
throw new JDBCExecuteSQLException(sql, e); |
|
238 |
} |
|
239 |
if (!rs.next()) { |
|
240 |
return emptyEnv; |
|
241 |
} |
|
242 |
|
|
243 |
byte[] data = rs.getBytes(1); |
|
244 |
if (data == null) { |
|
245 |
return emptyEnv; |
|
246 |
} |
|
247 |
|
|
248 |
Geometry geom = geomManager.createFrom(data); |
|
249 |
|
|
250 |
fullEnvelope = geom.getEnvelope(); |
|
251 |
|
|
252 |
return fullEnvelope; |
|
253 |
} catch (java.sql.SQLException e) { |
|
254 |
throw new JDBCSQLException(e); |
|
255 |
} catch (BaseException e) { |
|
256 |
throw new ReadException(user.getProviderName(), e); |
|
257 |
} finally { |
|
258 |
try { |
|
259 |
rs.close(); |
|
260 |
} catch (Exception e) { |
|
261 |
} |
|
262 |
try { |
|
263 |
st.close(); |
|
264 |
} catch (Exception e) { |
|
265 |
} |
|
266 |
try { |
|
267 |
conn.close(); |
|
268 |
} catch (Exception e) { |
|
269 |
} |
|
270 |
rs = null; |
|
271 |
st = null; |
|
272 |
conn = null; |
|
273 |
} |
|
274 |
} |
|
275 |
}); |
|
276 |
} |
|
277 |
|
|
278 |
@Override |
|
279 |
protected boolean supportsGeometry() { |
|
280 |
return true; |
|
281 |
} |
|
282 |
|
|
283 |
/** |
|
284 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType |
|
285 |
* information stored in the table GEOMETRY_COLUMNS |
|
286 |
* |
|
287 |
* @param conn |
|
288 |
* @param rsMetadata |
|
289 |
* @param featureType |
|
290 |
* @throws ReadException |
|
291 |
*/ |
|
292 |
protected void loadSRS_and_shapeType(Connection conn, |
|
293 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType, |
|
294 |
String baseSchema, String baseTable) |
|
295 |
throws JDBCException { |
|
296 |
|
|
297 |
Statement st = null; |
|
298 |
ResultSet rs = null; |
|
299 |
try { |
|
300 |
// Sacamos la lista de los attributos geometricos |
|
301 |
EditableFeatureAttributeDescriptor attr; |
|
302 |
List geoAttrs = new ArrayList(); |
|
303 |
|
|
304 |
Iterator iter = featureType.iterator(); |
|
305 |
while (iter.hasNext()) { |
|
306 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
307 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
308 |
geoAttrs.add(attr); |
|
309 |
} |
|
310 |
} |
|
311 |
if (geoAttrs.size() < 1) { |
|
312 |
return; |
|
313 |
} |
|
314 |
|
|
315 |
|
|
316 |
// preparamos un set con las lista de tablas de origen |
|
317 |
// de los campos |
|
318 |
class TableId { |
|
319 |
public String schema=null; |
|
320 |
public String table=null; |
|
321 |
public String field = null; |
|
322 |
|
|
323 |
public void appendToSQL(StringBuilder strb) { |
|
324 |
if (schema == null || schema.length() == 0) { |
|
325 |
strb |
|
326 |
.append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '"); |
|
327 |
} else { |
|
328 |
strb.append("( F_TABLE_SCHEMA = '"); |
|
329 |
strb.append(schema); |
|
330 |
strb.append("' AND F_TABLE_NAME = '"); |
|
331 |
} |
|
332 |
strb.append(table); |
|
333 |
strb.append("' AND F_GEOMETRY_COLUMN = '"); |
|
334 |
strb.append(field); |
|
335 |
strb.append("' )"); |
|
336 |
} |
|
337 |
|
|
338 |
} |
|
339 |
Comparator cmp = new Comparator(){ |
|
340 |
public int compare(Object arg0, Object arg1) { |
|
341 |
TableId a0 = (TableId) arg0; |
|
342 |
TableId a1 = (TableId) arg1; |
|
343 |
|
|
344 |
int aux = a0.field.compareTo(a1.field); |
|
345 |
if (aux != 0) { |
|
346 |
return aux; |
|
347 |
} |
|
348 |
|
|
349 |
aux = a0.table.compareTo(a1.table); |
|
350 |
if (aux != 0) { |
|
351 |
return aux; |
|
352 |
} |
|
353 |
|
|
354 |
if (a0.schema == null) { |
|
355 |
if (a1.schema == null) { |
|
356 |
aux = 0; |
|
357 |
} else { |
|
358 |
aux = -1; |
|
359 |
} |
|
360 |
} else { |
|
361 |
if (a1.schema == null) { |
|
362 |
aux = -1; |
|
363 |
} else { |
|
364 |
aux = a0.schema.compareTo(a1.schema); |
|
365 |
} |
|
366 |
} |
|
367 |
return aux; |
|
368 |
} |
|
369 |
}; |
|
370 |
TreeSet set = new TreeSet(cmp); |
|
371 |
TableId tableId; |
|
372 |
iter = geoAttrs.iterator(); |
|
373 |
int rsIndex; |
|
374 |
while (iter.hasNext()) { |
|
375 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
376 |
tableId = new TableId(); |
|
377 |
rsIndex = attr.getIndex() + 1; |
|
378 |
|
|
379 |
if (baseSchema == null && baseTable == null) { |
|
380 |
if (rsMetadata instanceof PGResultSetMetaData) { |
|
381 |
tableId.schema = ((PGResultSetMetaData) rsMetadata) |
|
382 |
.getBaseSchemaName(rsIndex); |
|
383 |
tableId.table = ((PGResultSetMetaData) rsMetadata) |
|
384 |
.getBaseTableName(rsIndex); |
|
385 |
tableId.field = ((PGResultSetMetaData) rsMetadata) |
|
386 |
.getBaseColumnName(rsIndex); |
|
387 |
|
|
388 |
} else { |
|
389 |
tableId.schema = rsMetadata.getSchemaName(rsIndex); |
|
390 |
tableId.table = rsMetadata.getTableName(rsIndex); |
|
391 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
392 |
} |
|
393 |
} else { |
|
394 |
tableId.schema = baseSchema; |
|
395 |
tableId.table = baseTable; |
|
396 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
397 |
} |
|
398 |
if (tableId.table == null || tableId.table.length() == 0) { |
|
399 |
// Si no tiene tabla origen (viene de algun calculo por ej.) |
|
400 |
// lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS |
|
401 |
continue; |
|
402 |
} |
|
403 |
set.add(tableId); |
|
404 |
} |
|
405 |
|
|
406 |
if (set.size() == 0) { |
|
407 |
return; |
|
408 |
} |
|
409 |
|
|
410 |
// Preparamos una sql para que nos saque el resultado |
|
411 |
StringBuilder strb = new StringBuilder(); |
|
412 |
strb.append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID "); |
|
413 |
strb.append("from geometry_columns left join spatial_ref_sys on "); |
|
414 |
strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE "); |
|
415 |
iter = set.iterator(); |
|
416 |
for (int i=0;i<set.size()-1;i++) { |
|
417 |
tableId = (TableId) iter.next(); |
|
418 |
tableId.appendToSQL(strb); |
|
419 |
strb.append(" OR "); |
|
420 |
} |
|
421 |
tableId = (TableId) iter.next(); |
|
422 |
tableId.appendToSQL(strb); |
|
423 |
String sql = strb.toString(); |
|
424 |
|
|
425 |
|
|
426 |
st = conn.createStatement(); |
|
427 |
try { |
|
428 |
rs = JDBCHelper.executeQuery(st,sql); |
|
429 |
} catch (SQLException e) { |
|
430 |
throw new JDBCExecuteSQLException(sql, e); |
|
431 |
} |
|
432 |
String srsID; |
|
433 |
int pgSrid; |
|
434 |
int geometryType; |
|
435 |
int geometrySubtype; |
|
436 |
String geomTypeStr; |
|
437 |
int dimensions; |
|
438 |
IProjection srs; |
|
439 |
|
|
440 |
while (rs.next()){ |
|
441 |
srsID = rs.getString("SRSID"); |
|
442 |
pgSrid = rs.getInt("SRID"); |
|
443 |
geomTypeStr = rs.getString("TYPE").toUpperCase(); |
|
444 |
geometryType = Geometry.TYPES.GEOMETRY; |
|
445 |
if (geomTypeStr.startsWith("POINT")) { |
|
446 |
geometryType = Geometry.TYPES.POINT; |
|
447 |
} else if (geomTypeStr.startsWith("LINESTRING")) { |
|
448 |
geometryType = Geometry.TYPES.CURVE; |
|
449 |
} else if (geomTypeStr.startsWith("POLYGON")) { |
|
450 |
geometryType = Geometry.TYPES.SURFACE; |
|
451 |
} else if (geomTypeStr.startsWith("MULTIPOINT")) { |
|
452 |
geometryType = Geometry.TYPES.MULTIPOINT; |
|
453 |
} else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
|
454 |
geometryType = Geometry.TYPES.MULTICURVE; |
|
455 |
} else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
|
456 |
geometryType = Geometry.TYPES.MULTISURFACE; |
|
457 |
} |
|
458 |
dimensions = rs.getInt("coord_dimension"); |
|
459 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
|
460 |
if (dimensions > 2) { |
|
461 |
if (dimensions == 3) { |
|
462 |
if (geomTypeStr.endsWith("M")) { |
|
463 |
geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
|
464 |
} else { |
|
465 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
|
466 |
} |
|
467 |
|
|
468 |
} else { |
|
469 |
geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
|
470 |
} |
|
471 |
} |
|
472 |
addToPgSRToSRSID(pgSrid, srsID); |
|
473 |
|
|
474 |
|
|
475 |
iter = geoAttrs.iterator(); |
|
476 |
while (iter.hasNext()) { |
|
477 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
478 |
rsIndex = attr.getIndex() + 1; |
|
479 |
if (!rsMetadata.getColumnName(rsIndex).equals( |
|
480 |
rs.getString("f_geometry_column"))) { |
|
481 |
continue; |
|
482 |
} |
|
483 |
|
|
484 |
if (baseSchema == null && baseTable == null) { |
|
485 |
|
|
486 |
if (rsMetadata instanceof PGResultSetMetaData) { |
|
487 |
if (!((PGResultSetMetaData) rsMetadata) |
|
488 |
.getBaseTableName(rsIndex).equals( |
|
489 |
rs.getString("f_table_name"))) { |
|
490 |
continue; |
|
491 |
} |
|
492 |
String curSchema = rs.getString("f_table_schema"); |
|
493 |
String metaSchema = ((PGResultSetMetaData) rsMetadata) |
|
494 |
.getBaseSchemaName(rsIndex); |
|
495 |
if (!metaSchema.equals(curSchema)) { |
|
496 |
if (metaSchema.length() == 0 |
|
497 |
&& metaSchema == getDefaultSchema(conn)) { |
|
498 |
} else { |
|
499 |
continue; |
|
500 |
} |
|
501 |
} |
|
502 |
|
|
503 |
} else { |
|
504 |
|
|
505 |
if (!rsMetadata.getTableName(rsIndex).equals( |
|
506 |
rs.getString("f_table_name"))) { |
|
507 |
continue; |
|
508 |
} |
|
509 |
String curSchema = rs.getString("f_table_schema"); |
|
510 |
String metaSchema = rsMetadata |
|
511 |
.getSchemaName(rsIndex); |
|
512 |
if (!metaSchema.equals(curSchema)) { |
|
513 |
if (metaSchema.length() == 0 |
|
514 |
&& metaSchema == getDefaultSchema(conn)) { |
|
515 |
} else { |
|
516 |
continue; |
|
517 |
} |
|
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
attr.setGeometryType(geometryType); |
|
522 |
attr.setGeometrySubType(geometrySubtype); |
|
523 |
if (srsID != null && srsID.length() > 0) { |
|
524 |
attr.setSRS(CRSFactory.getCRS(srsID)); |
|
525 |
} |
|
526 |
iter.remove(); |
|
527 |
} |
|
528 |
iter = geoAttrs.iterator(); |
|
529 |
while (iter.hasNext()) { |
|
530 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
531 |
attr.setSRS(null); |
|
532 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
|
533 |
|
|
534 |
} |
|
535 |
} |
|
536 |
|
|
537 |
} catch (java.sql.SQLException e) { |
|
538 |
throw new JDBCSQLException(e); |
|
539 |
} finally { |
|
540 |
try {rs.close();} catch (Exception e) { }; |
|
541 |
try {st.close();} catch (Exception e) { }; |
|
542 |
} |
|
543 |
|
|
544 |
} |
|
545 |
|
|
546 |
|
|
547 |
public String getSqlColumnTypeDescription(FeatureAttributeDescriptor attr) { |
|
548 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
549 |
return "geometry"; |
|
550 |
} |
|
551 |
return super.getSqlColumnTypeDescription(attr); |
|
552 |
} |
|
553 |
|
|
554 |
|
|
555 |
public int getPostgisGeomDimensions(int geometrySubType) { |
|
556 |
switch (geometrySubType) { |
|
557 |
case Geometry.SUBTYPES.GEOM2D: |
|
558 |
return 2; |
|
559 |
case Geometry.SUBTYPES.GEOM2DM: |
|
560 |
case Geometry.SUBTYPES.GEOM3D: |
|
561 |
return 3; |
|
562 |
|
|
563 |
case Geometry.SUBTYPES.GEOM3DM: |
|
564 |
return 4; |
|
565 |
default: |
|
566 |
throw new UnsupportedDataTypeException( |
|
567 |
ToolsLocator.getDataTypesManager().getTypeName(DataTypes.GEOMETRY), |
|
568 |
DataTypes.GEOMETRY); |
|
569 |
} |
|
570 |
} |
|
571 |
|
|
572 |
public String getPostgisGeomType(int geometryType, int geometrySubType) { |
|
573 |
String pgGeomType; |
|
574 |
switch (geometryType) { |
|
575 |
case Geometry.TYPES.GEOMETRY: |
|
576 |
pgGeomType = "GEOMETRY"; |
|
577 |
break; |
|
578 |
case Geometry.TYPES.POINT: |
|
579 |
pgGeomType = "POINT"; |
|
580 |
break; |
|
581 |
case Geometry.TYPES.CURVE: |
|
582 |
pgGeomType = "LINESTRING"; |
|
583 |
break; |
|
584 |
case Geometry.TYPES.SURFACE: |
|
585 |
pgGeomType = "POLYGON"; |
|
586 |
break; |
|
587 |
case Geometry.TYPES.MULTIPOINT: |
|
588 |
pgGeomType = "MULTIPOINT"; |
|
589 |
break; |
|
590 |
case Geometry.TYPES.MULTICURVE: |
|
591 |
pgGeomType = "MULTILINESTRING"; |
|
592 |
break; |
|
593 |
case Geometry.TYPES.MULTISURFACE: |
|
594 |
pgGeomType = "MULTIPOLYGON"; |
|
595 |
break; |
|
596 |
default: |
|
597 |
logger.warn("Can't determine PostGIS geometry type, use GEOMETRY."); |
|
598 |
pgGeomType = "GEOMETRY"; |
|
599 |
} |
|
600 |
if (geometrySubType == Geometry.SUBTYPES.GEOM2DM |
Also available in: Unified diff