Revision 40017
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/org/gvsig/fmap/dal/store/jdbc/JDBCTestWrite.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 |
package org.gvsig.fmap.dal.store.jdbc; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
31 |
import org.gvsig.fmap.dal.exception.DataException; |
|
32 |
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore; |
|
33 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
34 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
37 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
38 |
|
|
39 |
import com.mysql.jdbc.Driver; |
|
40 |
|
|
41 |
|
|
42 |
public class JDBCTestWrite extends BaseTestEditableFeatureStore { |
|
43 |
private JDBCNewStoreParameters newParams; |
|
44 |
private JDBCServerExplorer myExplorer; |
|
45 |
private int newId = 50000; |
|
46 |
|
|
47 |
public void fillPrimaryKeyInserFeature(EditableFeature feature) { |
|
48 |
feature.set("id", newId); |
|
49 |
newId++; |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
protected void doSetUp() throws Exception { |
|
54 |
// Nothing to do |
|
55 |
} |
|
56 |
|
|
57 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
58 |
throws DataException { |
|
59 |
JDBCStoreParameters parameters = null; |
|
60 |
parameters = (JDBCStoreParameters) dataManager |
|
61 |
.createStoreParameters(JDBCStoreProvider.NAME); |
|
62 |
|
|
63 |
parameters.setHost("localhost"); |
|
64 |
parameters.setPort(3306); |
|
65 |
parameters.setUser("test"); |
|
66 |
parameters.setPassword("test"); |
|
67 |
parameters.setDBName("gis"); |
|
68 |
parameters.setTable("alfanum"); |
|
69 |
parameters.setUrl("jdbc:mysql://" + parameters.getHost() + ":" |
|
70 |
+ parameters.getPort() + "/" + parameters.getDBName()); |
|
71 |
parameters.setJDBCDriverClassName(Driver.class.getName()); |
|
72 |
|
|
73 |
return parameters; |
|
74 |
} |
|
75 |
|
|
76 |
/* |
|
77 |
* (non-Javadoc) |
|
78 |
* |
|
79 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer() |
|
80 |
*/ |
|
81 |
@Override |
|
82 |
public boolean hasExplorer() { |
|
83 |
// TODO Auto-generated method stub |
|
84 |
return true; |
|
85 |
} |
|
86 |
|
|
87 |
/* |
|
88 |
* (non-Javadoc) |
|
89 |
* |
|
90 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources() |
|
91 |
*/ |
|
92 |
@Override |
|
93 |
public boolean usesResources() { |
|
94 |
// TODO Auto-generated method stub |
|
95 |
return true; |
|
96 |
} |
|
97 |
|
|
98 |
/* |
|
99 |
public void testImportDB() throws Exception { |
|
100 |
JDBCNewStoreParameters newParasm = (JDBCNewStoreParameters) getDefaultNewDataStoreParameters(); |
|
101 |
JDBCServerExplorerParameters seParams = (JDBCServerExplorerParameters) dataManager |
|
102 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
|
103 |
|
|
104 |
assertTrue(myExplorer.add(newParasm, true)); |
|
105 |
|
|
106 |
FeatureStore store = (FeatureStore) dataManager |
|
107 |
.createStore(getDefaultDataStoreParameters()); |
|
108 |
|
|
109 |
FeatureStore newstore = (FeatureStore) dataManager |
|
110 |
.createStore(newParasm); |
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
newstore.edit(FeatureStore.MODE_APPEND); |
|
115 |
FeatureSet set = store.getFeatureSet(); |
|
116 |
DisposableIterator iter = set.iterator(); |
|
117 |
Feature org; |
|
118 |
EditableFeature trg; |
|
119 |
Iterator attrIter; |
|
120 |
FeatureAttributeDescriptor attr; |
|
121 |
while (iter.hasNext()) { |
|
122 |
org = (Feature) iter.next(); |
|
123 |
trg = newstore.createNewFeature(false); |
|
124 |
attrIter = trg.getType().iterator(); |
|
125 |
while (attrIter.hasNext()) { |
|
126 |
attr = (FeatureAttributeDescriptor) attrIter.next(); |
|
127 |
trg.set(attr.getName(), org.get(attr.getName())); |
|
128 |
} |
|
129 |
newstore.insert(trg); |
|
130 |
} |
|
131 |
newstore.finishEditing(); |
|
132 |
|
|
133 |
iter.dispose(); |
|
134 |
set.dispose(); |
|
135 |
store.dispose(); |
|
136 |
newstore.dispose(); |
|
137 |
|
|
138 |
|
|
139 |
} |
|
140 |
*/ |
|
141 |
|
|
142 |
@Override |
|
143 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters() |
|
144 |
throws Exception { |
|
145 |
|
|
146 |
|
|
147 |
JDBCStoreParameters params = (JDBCStoreParameters) getDefaultDataStoreParameters(); |
|
148 |
|
|
149 |
if (this.myExplorer == null) { |
|
150 |
|
|
151 |
JDBCServerExplorerParameters seParameters = null; |
|
152 |
seParameters = (JDBCServerExplorerParameters) dataManager |
|
153 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
|
154 |
|
|
155 |
seParameters.setHost("localhost"); |
|
156 |
seParameters.setPort(3306); |
|
157 |
seParameters.setUser("test"); |
|
158 |
seParameters.setPassword("test"); |
|
159 |
seParameters.setDBName("gis"); |
|
160 |
|
|
161 |
seParameters.setUrl("jdbc:mysql://" + seParameters.getHost() + ":" |
|
162 |
+ seParameters.getPort() + "/" + seParameters.getDBName()); |
|
163 |
seParameters.setJDBCDriverClassName(Driver.class.getName()); |
|
164 |
|
|
165 |
|
|
166 |
myExplorer = (JDBCServerExplorer) dataManager |
|
167 |
.createServerExplorer(seParameters); |
|
168 |
} |
|
169 |
if (this.newParams == null) { |
|
170 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
|
171 |
|
|
172 |
newParams = (JDBCNewStoreParameters) myExplorer |
|
173 |
.getAddParameters(); |
|
174 |
|
|
175 |
|
|
176 |
newParams.setTable(params.getTable() + "_test"); |
|
177 |
FeatureType ftOrg = store.getDefaultFeatureType(); |
|
178 |
EditableFeatureType ftTrg = ftOrg.getEditable(); |
|
179 |
|
|
180 |
// EditableFeatureType ftTrg = (EditableFeatureType) newParams |
|
181 |
// .getDefaultFeatureType(); |
|
182 |
newParams.setDefaultFeatureType(ftTrg); |
|
183 |
|
|
184 |
// FeatureAttributeDescriptor org; |
|
185 |
// EditableFeatureAttributeDescriptor trg; |
|
186 |
// Iterator iter = ftOrg.iterator(); |
|
187 |
// while (iter.hasNext()) { |
|
188 |
// org = (FeatureAttributeDescriptor) iter.next(); |
|
189 |
// trg = ftTrg.add(org.getName(), org.getDataType()); |
|
190 |
// trg.setAllowNull(org.allowNull()); |
|
191 |
// trg.setDefaultValue(org.getDefaultValue()); |
|
192 |
// trg.setGeometrySubType(org.getGeometrySubType()); |
|
193 |
// trg.setGeometryType(org.getGeometryType()); |
|
194 |
// trg.setIsAutomatic(org.isAutomatic()); |
|
195 |
// trg.setIsPrimaryKey(org.isPrimaryKey()); |
|
196 |
// trg.setIsReadOnly(org.isReadOnly()); |
|
197 |
// trg.setMaximumOccurrences(org.getMaximumOccurrences()); |
|
198 |
// trg.setMinimumOccurrences(org.getMinimumOccurrences()); |
|
199 |
// trg.setPrecision(org.getPrecision()); |
|
200 |
// trg.setSize(org.getSize()); |
|
201 |
// trg.setSRS(org.getSRS()); |
|
202 |
// trg.setAdditionalInfo("SQLType", org |
|
203 |
// .getAdditionalInfo("SQLType")); |
|
204 |
// trg.setAdditionalInfo("SQLTypeName", org |
|
205 |
// .getAdditionalInfo("SQLTypeName")); |
|
206 |
// |
|
207 |
// } |
|
208 |
// ftTrg.setDefaultGeometryAttributeName(ftOrg |
|
209 |
// .getDefaultGeometryAttributeName()); |
|
210 |
// ftTrg.setHasOID(ftOrg.hasOID()); |
|
211 |
|
|
212 |
store.dispose(); |
|
213 |
} |
|
214 |
|
|
215 |
return this.newParams; |
|
216 |
} |
|
217 |
|
|
218 |
@Override |
|
219 |
public boolean resourcesNotifyChanges() { |
|
220 |
// TODO Auto-generated method stub |
|
221 |
return false; |
|
222 |
} |
|
223 |
|
|
224 |
} |
|
0 | 225 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/org/gvsig/fmap/dal/store/jdbc/JDBCTest.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.jdbc; |
|
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 |
|
|
37 |
import com.mysql.jdbc.Driver; |
|
38 |
|
|
39 |
/** |
|
40 |
* @author jmvivo |
|
41 |
* |
|
42 |
*/ |
|
43 |
public class JDBCTest extends BaseTestFeatureStore { |
|
44 |
|
|
45 |
@Override |
|
46 |
protected void doSetUp() throws Exception { |
|
47 |
// Nothing to do |
|
48 |
} |
|
49 |
|
|
50 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
51 |
throws DataException { |
|
52 |
JDBCStoreParameters parameters = null; |
|
53 |
parameters = (JDBCStoreParameters) dataManager |
|
54 |
.createStoreParameters(JDBCStoreProvider.NAME); |
|
55 |
|
|
56 |
|
|
57 |
parameters.setHost("localhost"); |
|
58 |
parameters.setPort(3306); |
|
59 |
parameters.setUser("test"); |
|
60 |
parameters.setPassword("test"); |
|
61 |
parameters.setDBName("gis"); |
|
62 |
parameters.setTable("alfanum"); |
|
63 |
parameters.setUrl("jdbc:mysql://" + parameters.getHost() + ":" |
|
64 |
+ parameters.getPort() + "/" + parameters.getDBName()); |
|
65 |
parameters.setJDBCDriverClassName(Driver.class.getName()); |
|
66 |
|
|
67 |
return parameters; |
|
68 |
} |
|
69 |
|
|
70 |
/* (non-Javadoc) |
|
71 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer() |
|
72 |
*/ |
|
73 |
@Override |
|
74 |
public boolean hasExplorer() { |
|
75 |
// TODO Auto-generated method stub |
|
76 |
return true; |
|
77 |
} |
|
78 |
|
|
79 |
/* (non-Javadoc) |
|
80 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources() |
|
81 |
*/ |
|
82 |
@Override |
|
83 |
public boolean usesResources() { |
|
84 |
// TODO Auto-generated method stub |
|
85 |
return true; |
|
86 |
} |
|
87 |
|
|
88 |
} |
|
0 | 89 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/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 |
} |
|
0 | 241 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/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 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 IVER T.I {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.postgresql; |
|
29 |
|
|
30 |
import java.sql.Connection; |
|
31 |
import java.sql.DriverManager; |
|
32 |
import java.sql.PreparedStatement; |
|
33 |
import java.sql.ResultSet; |
|
34 |
import java.sql.SQLException; |
|
35 |
import java.sql.Statement; |
|
36 |
import java.text.MessageFormat; |
|
37 |
|
|
38 |
import org.gvsig.fmap.geom.Geometry; |
|
39 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
40 |
import org.gvsig.fmap.geom.GeometryManager; |
|
41 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
|
42 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext; |
|
43 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
|
44 |
import org.gvsig.fmap.geom.operation.towkt.ToWKT; |
|
45 |
import org.gvsig.fmap.geom.primitive.impl.Point2D; |
|
46 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
|
47 |
|
|
48 |
public class TestReadAndWriteGeom extends AbstractLibraryAutoInitTestCase { |
|
49 |
private static final int FEATURES_TO_INSERT = 12000; |
|
50 |
private static final String TABLE_NAME_INSERT = "testReadadnwritegeom_testcase"; |
|
51 |
private static final String FIELD_NAME_INSERT = "geom"; |
|
52 |
private Connection conn; |
|
53 |
private GeometryManager geoManager; |
|
54 |
private static String SQL_FOR_READ_TEST = "Select {0} from medio_ejes"; |
|
55 |
|
|
56 |
@Override |
|
57 |
protected void doSetUp() throws Exception { |
|
58 |
geoManager = GeometryLocator.getGeometryManager(); |
|
59 |
|
|
60 |
Class klass = Class.forName(PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME); |
|
61 |
if (klass == null) { |
|
62 |
throw new Exception("Driver not found: " |
|
63 |
+ PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME); |
|
64 |
} |
|
65 |
|
|
66 |
// inicializamos operaciones de geometrias de las que dependemos |
|
67 |
int code = FromWKB.CODE; |
|
68 |
code = ToWKB.CODE; |
|
69 |
code = ToWKT.CODE; |
|
70 |
conn = DriverManager.getConnection(PostgreSQLLibrary.getJdbcUrl( |
|
71 |
"localhost", 5432, "gis"), "postgres", "postgres"); |
|
72 |
} |
|
73 |
|
|
74 |
public void testInsertWKB() throws Exception { |
|
75 |
PreparedStatement pst = null; |
|
76 |
try { |
|
77 |
crearTablaTest(); |
|
78 |
Point2D geom = (Point2D) geoManager.createPoint(0, 0, |
|
79 |
Geometry.SUBTYPES.GEOM2D); |
|
80 |
pst = conn.prepareStatement( |
|
81 |
"Insert into " |
|
82 |
+ TABLE_NAME_INSERT.toLowerCase() + |
|
83 |
" (" |
|
84 |
+ FIELD_NAME_INSERT + ") Values (GeomFromWKB(?))"); |
|
85 |
|
|
86 |
|
|
87 |
int i; |
|
88 |
for (i = 1; i <= FEATURES_TO_INSERT; i++) { |
|
89 |
pst.clearParameters(); |
|
90 |
geom.setX(i); |
|
91 |
geom.setY(i); |
|
92 |
pst.setBytes(1, |
|
93 |
(byte[]) geom.invokeOperation( |
|
94 |
ToWKB.CODE, |
|
95 |
null) |
|
96 |
); |
|
97 |
|
|
98 |
pst.executeUpdate(); |
|
99 |
} |
|
100 |
|
|
101 |
System.out |
|
102 |
.println("TestReadAndWriteGeom.testInsertPostgis() Inserteds= i"); |
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
} finally { |
|
108 |
if (pst != null) { |
|
109 |
try {pst.close();} catch (SQLException e) {e.printStackTrace();} |
|
110 |
} |
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 |
|
|
115 |
} |
|
116 |
|
|
117 |
private void crearTablaTest() throws SQLException { |
|
118 |
execute("DROP TABLE IF EXISTS " + TABLE_NAME_INSERT.toLowerCase()); |
|
119 |
execute("Delete from geometry_columns where f_table_name = '" |
|
120 |
+ TABLE_NAME_INSERT.toLowerCase() + "'"); |
|
121 |
execute("CREATE TABLE " + TABLE_NAME_INSERT.toLowerCase() |
|
122 |
+ " (id serial PRIMARY KEY)"); |
|
123 |
execute("Select AddGeometryColumn('" + TABLE_NAME_INSERT.toLowerCase() |
|
124 |
+ "','" |
|
125 |
+ FIELD_NAME_INSERT + "',-1,'GEOMETRY',2)"); |
|
126 |
} |
|
127 |
|
|
128 |
private void execute(String sql) throws SQLException { |
|
129 |
Statement st = null; |
|
130 |
try { |
|
131 |
st = conn.createStatement(); |
|
132 |
st.execute(sql); |
|
133 |
} finally { |
|
134 |
if (st != null) { |
|
135 |
try {st.close();} catch (SQLException e) {e.printStackTrace();} |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
} |
|
140 |
|
|
141 |
public String getSQLForRead(String geoColumn) { |
|
142 |
return MessageFormat.format(SQL_FOR_READ_TEST, geoColumn); |
|
143 |
|
|
144 |
} |
|
145 |
|
|
146 |
public void testReadBinary() throws Exception { |
|
147 |
// st.execute("declare " + getTableName() + myCursorId + |
|
148 |
// "_wkb_cursor binary scroll cursor with hold for " + sqlAux); |
|
149 |
// rs = st.executeQuery("fetch forward " + FETCH_SIZE+ " in " + |
|
150 |
// getTableName() + myCursorId + "_wkb_cursor"); |
|
151 |
|
|
152 |
Statement st = null; |
|
153 |
ResultSet rs = null; |
|
154 |
long count = 0; |
|
155 |
Geometry geom; |
|
156 |
Geometry nullGeom = geoManager.createNullGeometry(Geometry.SUBTYPES.GEOM2D); |
|
157 |
|
|
158 |
try { |
|
159 |
st = conn.createStatement(); |
|
160 |
String cursorName = "myCursor___xxx"; |
|
161 |
st.execute("declare " + cursorName |
|
162 |
+ "_wkb_cursor binary scroll cursor with hold for " |
|
163 |
+ getSQLForRead("the_geom")); |
|
164 |
rs = st.executeQuery("fetch forward all in " + cursorName |
|
165 |
+ "_wkb_cursor"); |
|
166 |
byte[] buff; |
|
167 |
FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext(); |
|
168 |
while (rs.next()) { |
|
169 |
count++; |
|
170 |
buff = rs.getBytes(1); |
|
171 |
// if (buff != null) { |
|
172 |
// opContext.setData(buff); |
|
173 |
// geom = (Geometry) nullGeom.invokeOperation(FromWKB.CODE, |
|
174 |
// opContext); |
|
175 |
// assertNotNull(geom); |
|
176 |
// } |
|
177 |
} |
|
178 |
|
|
179 |
System.out |
|
180 |
.println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
181 |
+ count); |
|
182 |
|
|
183 |
} finally { |
|
184 |
if (st != null) { |
|
185 |
try { |
|
186 |
st.close(); |
|
187 |
} catch (SQLException e) { |
|
188 |
e.printStackTrace(); |
|
189 |
} |
|
190 |
} |
|
191 |
if (rs != null) { |
|
192 |
try { |
|
193 |
rs.close(); |
|
194 |
} catch (SQLException e) { |
|
195 |
e.printStackTrace(); |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
|
|
200 |
} |
|
201 |
|
|
202 |
public void testReadWKB() throws Exception { |
|
203 |
Statement st = null; |
|
204 |
ResultSet rs = null; |
|
205 |
long count = 0; |
|
206 |
Geometry geom; |
|
207 |
Geometry nullGeom = geoManager |
|
208 |
.createNullGeometry(Geometry.SUBTYPES.GEOM2D); |
|
209 |
byte[] buff; |
|
210 |
FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext(); |
|
211 |
try { |
|
212 |
st = conn.createStatement(); |
|
213 |
rs = st.executeQuery(getSQLForRead("asBinary(the_geom)")); |
|
214 |
while (rs.next()) { |
|
215 |
count++; |
|
216 |
buff = rs.getBytes(1); |
|
217 |
// if (buff != null) { |
|
218 |
// opContext.setData(buff); |
|
219 |
// geom = (Geometry) nullGeom.invokeOperation(FromWKB.CODE, |
|
220 |
// opContext); |
|
221 |
// assertNotNull(geom); |
|
222 |
// } |
|
223 |
} |
|
224 |
|
|
225 |
System.out |
|
226 |
.println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
227 |
+ count); |
|
228 |
|
|
229 |
|
|
230 |
} finally { |
|
231 |
if (st != null) { |
|
232 |
try { |
|
233 |
st.close(); |
|
234 |
} catch (SQLException e) { |
|
235 |
e.printStackTrace(); |
|
236 |
} |
|
237 |
} |
|
238 |
if (rs != null) { |
|
239 |
try { |
|
240 |
rs.close(); |
|
241 |
} catch (SQLException e) { |
|
242 |
e.printStackTrace(); |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
// public void testReadPostgis() throws Exception { |
|
249 |
// Statement st = null; |
|
250 |
// ResultSet rs = null; |
|
251 |
// long count = 0; |
|
252 |
// Geometry geom; |
|
253 |
// PGgeometry pgGeom; |
|
254 |
// PostGIS2Geometry converter = new PostGIS2Geometry(); |
|
255 |
// try { |
|
256 |
// st = conn.createStatement(); |
|
257 |
// rs = st.executeQuery(getSQLForRead("the_geom")); |
|
258 |
// while (rs.next()) { |
|
259 |
// count++; |
|
260 |
// pgGeom = (PGgeometry) rs.getObject(1); |
|
261 |
// if (pgGeom != null) { |
|
262 |
// geom = converter.getGeometry(pgGeom); |
|
263 |
// assertNotNull(geom); |
|
264 |
// } |
|
265 |
// } |
|
266 |
// |
|
267 |
// |
|
268 |
// System.out |
|
269 |
// .println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
270 |
// + count); |
|
271 |
// |
|
272 |
// |
|
273 |
// } finally { |
|
274 |
// if (st != null) { |
|
275 |
// try { |
|
276 |
// st.close(); |
|
277 |
// } catch (SQLException e) { |
|
278 |
// e.printStackTrace(); |
|
279 |
// } |
|
280 |
// } |
|
281 |
// if (rs != null) { |
|
282 |
// try { |
|
283 |
// rs.close(); |
|
284 |
// } catch (SQLException e) { |
|
285 |
// e.printStackTrace(); |
|
286 |
// } |
|
287 |
// } |
|
288 |
// } |
|
289 |
// } |
|
290 |
|
|
291 |
|
|
292 |
protected void tearDown() throws Exception { |
|
293 |
super.tearDown(); |
|
294 |
conn.close(); |
|
295 |
} |
|
296 |
|
|
297 |
} |
|
0 | 298 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/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 |
} |
|
0 | 176 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src-test/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/libraries/org.gvsig.fmap.dal.db/2.0/src-test/org/gvsig/fmap/dal/store/mysql/MySQLTestWrite.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 |
package org.gvsig.fmap.dal.store.mysql; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
31 |
import org.gvsig.fmap.dal.exception.DataException; |
|
32 |
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore; |
|
33 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
36 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
37 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
38 |
|
|
39 |
|
|
40 |
public class MySQLTestWrite extends BaseTestEditableFeatureStore { |
|
41 |
private MySQLNewStoreParameters newParams; |
|
42 |
private MySQLServerExplorer myExplorer; |
|
43 |
|
|
44 |
@Override |
|
45 |
protected void doSetUp() throws Exception { |
|
46 |
super.doSetUp(); |
|
47 |
} |
|
48 |
|
|
49 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
50 |
throws DataException { |
|
51 |
MySQLStoreParameters parameters = null; |
|
52 |
parameters = (MySQLStoreParameters) dataManager |
|
53 |
.createStoreParameters(MySQLStoreProvider.NAME); |
|
54 |
|
|
55 |
parameters.setHost("localhost"); |
|
56 |
parameters.setPort(3306); |
|
57 |
parameters.setUser("test"); |
|
58 |
parameters.setPassword("test"); |
|
59 |
parameters.setDBName("gis"); |
|
60 |
// parameters.setTable("alfanum_autoid"); |
|
61 |
parameters.setTable("comunic_lin_300k"); |
|
62 |
|
|
63 |
return parameters; |
|
64 |
} |
|
65 |
|
|
66 |
/* |
|
67 |
* (non-Javadoc) |
|
68 |
* |
|
69 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer() |
|
70 |
*/ |
|
71 |
@Override |
|
72 |
public boolean hasExplorer() { |
|
73 |
// TODO Auto-generated method stub |
|
74 |
return true; |
|
75 |
} |
|
76 |
|
|
77 |
/* |
|
78 |
* (non-Javadoc) |
|
79 |
* |
|
80 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources() |
|
81 |
*/ |
|
82 |
@Override |
|
83 |
public boolean usesResources() { |
|
84 |
// TODO Auto-generated method stub |
|
85 |
return true; |
|
86 |
} |
|
87 |
|
|
88 |
/* |
|
89 |
public void testImportDB() throws Exception { |
|
90 |
JDBCNewStoreParameters newParasm = (JDBCNewStoreParameters) getDefaultNewDataStoreParameters(); |
|
91 |
JDBCServerExplorerParameters seParams = (JDBCServerExplorerParameters) dataManager |
|
92 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
|
93 |
|
|
94 |
assertTrue(myExplorer.add(newParasm, true)); |
|
95 |
|
|
96 |
FeatureStore store = (FeatureStore) dataManager |
|
97 |
.createStore(getDefaultDataStoreParameters()); |
|
98 |
|
|
99 |
FeatureStore newstore = (FeatureStore) dataManager |
|
100 |
.createStore(newParasm); |
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
newstore.edit(FeatureStore.MODE_APPEND); |
|
105 |
FeatureSet set = store.getFeatureSet(); |
|
106 |
DisposableIterator iter = set.iterator(); |
|
107 |
Feature org; |
|
108 |
EditableFeature trg; |
|
109 |
Iterator attrIter; |
|
110 |
FeatureAttributeDescriptor attr; |
|
111 |
while (iter.hasNext()) { |
|
112 |
org = (Feature) iter.next(); |
|
113 |
trg = newstore.createNewFeature(false); |
|
114 |
attrIter = trg.getType().iterator(); |
|
115 |
while (attrIter.hasNext()) { |
|
116 |
attr = (FeatureAttributeDescriptor) attrIter.next(); |
|
117 |
trg.set(attr.getName(), org.get(attr.getName())); |
|
118 |
} |
|
119 |
newstore.insert(trg); |
|
120 |
} |
|
121 |
newstore.finishEditing(); |
|
122 |
|
|
123 |
iter.dispose(); |
|
124 |
set.dispose(); |
|
125 |
store.dispose(); |
|
126 |
newstore.dispose(); |
|
127 |
|
|
128 |
|
|
129 |
} |
|
130 |
*/ |
|
131 |
|
|
132 |
@Override |
|
133 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters() |
|
134 |
throws Exception { |
|
135 |
|
|
136 |
|
|
137 |
JDBCStoreParameters params = (JDBCStoreParameters) getDefaultDataStoreParameters(); |
|
138 |
|
|
139 |
if (this.myExplorer == null) { |
|
140 |
|
|
141 |
MySQLServerExplorerParameters seParameters = null; |
|
142 |
seParameters = (MySQLServerExplorerParameters) dataManager |
|
143 |
.createServerExplorerParameters(MySQLServerExplorer.NAME); |
|
144 |
|
|
145 |
seParameters.setHost("localhost"); |
|
146 |
seParameters.setPort(3306); |
|
147 |
seParameters.setUser("test"); |
|
148 |
seParameters.setPassword("test"); |
|
149 |
seParameters.setDBName("gis"); |
|
150 |
|
|
151 |
|
|
152 |
myExplorer = (MySQLServerExplorer) dataManager |
|
153 |
.createServerExplorer(seParameters); |
|
154 |
} |
|
155 |
if (this.newParams == null) { |
|
156 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
|
157 |
|
|
158 |
newParams = (MySQLNewStoreParameters) myExplorer |
|
159 |
.getAddParameters(); |
|
160 |
|
|
161 |
|
|
162 |
newParams.setTable(params.getTable() + "_test"); |
|
163 |
FeatureType ftOrg = store.getDefaultFeatureType(); |
|
164 |
EditableFeatureType ftTrg = ftOrg.getEditable(); |
|
165 |
|
|
166 |
// EditableFeatureType ftTrg = (EditableFeatureType) newParams |
|
167 |
// .getDefaultFeatureType(); |
|
168 |
newParams.setDefaultFeatureType(ftTrg); |
|
169 |
|
|
170 |
// FeatureAttributeDescriptor org; |
|
171 |
// EditableFeatureAttributeDescriptor trg; |
|
172 |
// Iterator iter = ftOrg.iterator(); |
|
173 |
// while (iter.hasNext()) { |
|
174 |
// org = (FeatureAttributeDescriptor) iter.next(); |
|
175 |
// trg = ftTrg.add(org.getName(), org.getDataType()); |
|
176 |
// trg.setAllowNull(org.allowNull()); |
|
177 |
// trg.setDefaultValue(org.getDefaultValue()); |
|
178 |
// trg.setGeometrySubType(org.getGeometrySubType()); |
|
179 |
// trg.setGeometryType(org.getGeometryType()); |
|
180 |
// trg.setIsAutomatic(org.isAutomatic()); |
|
181 |
// trg.setIsPrimaryKey(org.isPrimaryKey()); |
|
182 |
// trg.setIsReadOnly(org.isReadOnly()); |
|
183 |
// trg.setMaximumOccurrences(org.getMaximumOccurrences()); |
|
184 |
// trg.setMinimumOccurrences(org.getMinimumOccurrences()); |
|
185 |
// trg.setPrecision(org.getPrecision()); |
|
186 |
// trg.setSize(org.getSize()); |
|
187 |
// trg.setSRS(org.getSRS()); |
|
188 |
// trg.setAdditionalInfo("SQLType", org |
|
189 |
// .getAdditionalInfo("SQLType")); |
|
190 |
// trg.setAdditionalInfo("SQLTypeName", org |
|
191 |
// .getAdditionalInfo("SQLTypeName")); |
|
192 |
// |
|
193 |
// } |
|
194 |
// ftTrg.setDefaultGeometryAttributeName(ftOrg |
|
195 |
// .getDefaultGeometryAttributeName()); |
|
196 |
// ftTrg.setHasOID(ftOrg.hasOID()); |
|
197 |
|
|
198 |
store.dispose(); |
|
199 |
} |
|
200 |
|
|
201 |
return this.newParams; |
|
202 |
} |
|
203 |
|
|
204 |
@Override |
|
205 |
public boolean resourcesNotifyChanges() { |
|
206 |
// TODO Auto-generated method stub |
|
207 |
return false; |
|
208 |
} |
|
209 |
|
|
210 |
} |
|
0 | 211 |
tags/libraries/org.gvsig.fmap.dal.db/2.0/src/org/gvsig/fmap/dal/resource/db/DBParameters.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 |
package org.gvsig.fmap.dal.resource.db; |
|
29 |
|
|
30 |
/** |
|
31 |
* Interface for describe parameters of a Data Base |
|
32 |
* |
|
33 |
* |
|
34 |
* @author jmvivo |
|
35 |
* |
|
36 |
*/ |
|
37 |
public interface DBParameters { |
|
38 |
|
|
39 |
/** |
|
40 |
* Parameter name for <code>host</code> |
|
41 |
*/ |
|
42 |
public static final String HOST_PARAMTER_NAME = "host"; |
|
43 |
|
|
44 |
/** |
|
45 |
* Parameter name for <code>port</code> |
|
46 |
*/ |
|
47 |
public static final String PORT_PARAMTER_NAME = "port"; |
|
48 |
|
|
49 |
/** |
|
50 |
* Parameter name for <code>data base name</code> |
|
51 |
*/ |
|
52 |
public static final String DBNAME_PARAMTER_NAME = "dbname"; |
|
53 |
|
|
54 |
/** |
|
55 |
* Parameter name for <code>user</code> |
|
56 |
*/ |
|
57 |
public static final String USER_PARAMTER_NAME = "dbuser"; |
|
58 |
|
|
59 |
/** |
|
60 |
* Parameter name for <code>password</code> |
|
61 |
*/ |
|
62 |
public static final String PASSWORD_PARAMTER_NAME = "password"; |
|
63 |
|
|
64 |
/** |
|
65 |
* Return the value of <code>host</code> parameter |
|
66 |
* |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public String getHost(); |
|
70 |
|
|
71 |
/** |
|
72 |
* Return the value of <code>port</code> parameter |
|
73 |
* |
|
74 |
* @return |
|
75 |
*/ |
|
76 |
public Integer getPort(); |
|
77 |
|
|
78 |
/** |
|
79 |
* Return the value of <code>data base name</code> parameter |
|
80 |
* |
|
81 |
* @return |
|
82 |
*/ |
|
83 |
public String getDBName(); |
|
84 |
|
|
85 |
/** |
|
86 |
* Return the value of <code>password</code> parameter |
|
87 |
* |
|
88 |
* @return |
|
89 |
*/ |
|
90 |
public String getPassword(); |
|
91 |
|
|
92 |
/** |
|
93 |
* Return the value of <code>user</code> parameter |
|
94 |
* |
Also available in: Unified diff