Revision 42704
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
|
|
3 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
4 |
|
|
5 |
<modelVersion>4.0.0</modelVersion> |
|
6 |
<artifactId>org.gvsig.fmap.dal.db</artifactId> |
|
7 |
<name>${project.artifactId}</name> |
|
8 |
<packaging>pom</packaging> |
|
9 |
<parent> |
|
10 |
<groupId>org.gvsig</groupId> |
|
11 |
<artifactId>org.gvsig.fmap.dal</artifactId> |
|
12 |
<version>2.0.127</version> |
|
13 |
</parent> |
|
14 |
|
|
15 |
<modules> |
|
16 |
<module>org.gvsig.fmap.dal.db.lib</module> |
|
17 |
<module>org.gvsig.fmap.dal.db.jdbc</module> |
|
18 |
<module>org.gvsig.fmap.dal.db.h2</module> |
|
19 |
</modules> |
|
20 |
<description>This library has the implementation of the provider of te Data Access Library for accesing to database resources, like JDBC, PostgreSQL or H2.</description> |
|
21 |
</project> |
|
0 | 22 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
|
|
3 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
4 |
|
|
5 |
<modelVersion>4.0.0</modelVersion> |
|
6 |
<artifactId>org.gvsig.fmap.dal.db.jdbc</artifactId> |
|
7 |
<name>${project.artifactId}</name> |
|
8 |
<parent> |
|
9 |
<groupId>org.gvsig</groupId> |
|
10 |
<artifactId>org.gvsig.fmap.dal.db</artifactId> |
|
11 |
<version>2.0.127</version> |
|
12 |
</parent> |
|
13 |
|
|
14 |
<dependencies> |
|
15 |
<dependency> |
|
16 |
<groupId>org.gvsig</groupId> |
|
17 |
<artifactId>org.gvsig.tools.lib</artifactId> |
|
18 |
<scope>compile</scope> |
|
19 |
</dependency> |
|
20 |
<dependency> |
|
21 |
<groupId>org.gvsig</groupId> |
|
22 |
<artifactId>org.gvsig.fmap.dal.api</artifactId> |
|
23 |
<scope>compile</scope> |
|
24 |
</dependency> |
|
25 |
<dependency> |
|
26 |
<groupId>org.gvsig</groupId> |
|
27 |
<artifactId>org.gvsig.fmap.dal.spi</artifactId> |
|
28 |
<scope>compile</scope> |
|
29 |
</dependency> |
|
30 |
<dependency> |
|
31 |
<groupId>org.gvsig</groupId> |
|
32 |
<artifactId>org.gvsig.fmap.dal.db.lib</artifactId> |
|
33 |
<scope>compile</scope> |
|
34 |
</dependency> |
|
35 |
<dependency> |
|
36 |
<groupId>commons-dbcp</groupId> |
|
37 |
<artifactId>commons-dbcp</artifactId> |
|
38 |
<scope>compile</scope> |
|
39 |
</dependency> |
|
40 |
|
|
41 |
<!-- Tests --> |
|
42 |
|
|
43 |
<dependency> |
|
44 |
<groupId>org.gvsig</groupId> |
|
45 |
<artifactId>org.gvsig.fmap.dal.impl</artifactId> |
|
46 |
<type>test-jar</type> |
|
47 |
<scope>test</scope> |
|
48 |
</dependency> |
|
49 |
|
|
50 |
<dependency> |
|
51 |
<groupId>mysql</groupId> |
|
52 |
<artifactId>mysql-connector-java</artifactId> |
|
53 |
<scope>test</scope> |
|
54 |
</dependency> |
|
55 |
|
|
56 |
</dependencies> |
|
57 |
|
|
58 |
|
|
59 |
<build> |
|
60 |
<plugins> |
|
61 |
<plugin> |
|
62 |
<groupId>org.apache.maven.plugins</groupId> |
|
63 |
<artifactId>maven-surefire-plugin</artifactId> |
|
64 |
<configuration> |
|
65 |
<skipTests>true</skipTests> |
|
66 |
</configuration> |
|
67 |
</plugin> |
|
68 |
|
|
69 |
<plugin> |
|
70 |
<groupId>org.apache.maven.plugins</groupId> |
|
71 |
<artifactId>maven-compiler-plugin</artifactId> |
|
72 |
<executions> |
|
73 |
<execution> |
|
74 |
<id>default-testCompile</id> |
|
75 |
<phase>process-test-sources</phase> |
|
76 |
<goals> |
|
77 |
<goal>testCompile</goal> |
|
78 |
</goals> |
|
79 |
<configuration> |
|
80 |
<skip>true</skip> |
|
81 |
</configuration> |
|
82 |
</execution> |
|
83 |
</executions> |
|
84 |
</plugin> |
|
85 |
</plugins> |
|
86 |
</build> |
|
87 |
|
|
88 |
</project> |
|
0 | 89 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/test/java/org/gvsig/fmap/dal/store/jdbc/JDBCTestWrite.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
|
|
25 |
package org.gvsig.fmap.dal.store.jdbc; |
|
26 |
|
|
27 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
|
29 |
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore; |
|
30 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
31 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
34 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
35 |
|
|
36 |
import com.mysql.jdbc.Driver; |
|
37 |
|
|
38 |
|
|
39 |
public class JDBCTestWrite extends BaseTestEditableFeatureStore { |
|
40 |
private JDBCNewStoreParameters newParams; |
|
41 |
private JDBCServerExplorer myExplorer; |
|
42 |
private int newId = 50000; |
|
43 |
|
|
44 |
public void fillPrimaryKeyInserFeature(EditableFeature feature) { |
|
45 |
feature.set("id", newId); |
|
46 |
newId++; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
protected void doSetUp() throws Exception { |
|
51 |
// Nothing to do |
|
52 |
} |
|
53 |
|
|
54 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
55 |
throws DataException { |
|
56 |
JDBCStoreParameters parameters = null; |
|
57 |
parameters = (JDBCStoreParameters) dataManager |
|
58 |
.createStoreParameters(JDBCStoreProvider.NAME); |
|
59 |
|
|
60 |
parameters.setHost("localhost"); |
|
61 |
parameters.setPort(3306); |
|
62 |
parameters.setUser("test"); |
|
63 |
parameters.setPassword("test"); |
|
64 |
parameters.setDBName("gis"); |
|
65 |
parameters.setTable("alfanum"); |
|
66 |
parameters.setUrl("jdbc:mysql://" + parameters.getHost() + ":" |
|
67 |
+ parameters.getPort() + "/" + parameters.getDBName()); |
|
68 |
parameters.setJDBCDriverClassName(Driver.class.getName()); |
|
69 |
|
|
70 |
return parameters; |
|
71 |
} |
|
72 |
|
|
73 |
/* |
|
74 |
* (non-Javadoc) |
|
75 |
* |
|
76 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer() |
|
77 |
*/ |
|
78 |
@Override |
|
79 |
public boolean hasExplorer() { |
|
80 |
// TODO Auto-generated method stub |
|
81 |
return true; |
|
82 |
} |
|
83 |
|
|
84 |
/* |
|
85 |
* (non-Javadoc) |
|
86 |
* |
|
87 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources() |
|
88 |
*/ |
|
89 |
@Override |
|
90 |
public boolean usesResources() { |
|
91 |
// TODO Auto-generated method stub |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
/* |
|
96 |
public void testImportDB() throws Exception { |
|
97 |
JDBCNewStoreParameters newParasm = (JDBCNewStoreParameters) getDefaultNewDataStoreParameters(); |
|
98 |
JDBCServerExplorerParameters seParams = (JDBCServerExplorerParameters) dataManager |
|
99 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
|
100 |
|
|
101 |
assertTrue(myExplorer.add(newParasm, true)); |
|
102 |
|
|
103 |
FeatureStore store = (FeatureStore) dataManager |
|
104 |
.createStore(getDefaultDataStoreParameters()); |
|
105 |
|
|
106 |
FeatureStore newstore = (FeatureStore) dataManager |
|
107 |
.createStore(newParasm); |
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
newstore.edit(FeatureStore.MODE_APPEND); |
|
112 |
FeatureSet set = store.getFeatureSet(); |
|
113 |
DisposableIterator iter = set.iterator(); |
|
114 |
Feature org; |
|
115 |
EditableFeature trg; |
|
116 |
Iterator attrIter; |
|
117 |
FeatureAttributeDescriptor attr; |
|
118 |
while (iter.hasNext()) { |
|
119 |
org = (Feature) iter.next(); |
|
120 |
trg = newstore.createNewFeature(false); |
|
121 |
attrIter = trg.getType().iterator(); |
|
122 |
while (attrIter.hasNext()) { |
|
123 |
attr = (FeatureAttributeDescriptor) attrIter.next(); |
|
124 |
trg.set(attr.getName(), org.get(attr.getName())); |
|
125 |
} |
|
126 |
newstore.insert(trg); |
|
127 |
} |
|
128 |
newstore.finishEditing(); |
|
129 |
|
|
130 |
iter.dispose(); |
|
131 |
set.dispose(); |
|
132 |
store.dispose(); |
|
133 |
newstore.dispose(); |
|
134 |
|
|
135 |
|
|
136 |
} |
|
137 |
*/ |
|
138 |
|
|
139 |
@Override |
|
140 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters() |
|
141 |
throws Exception { |
|
142 |
|
|
143 |
|
|
144 |
JDBCStoreParameters params = (JDBCStoreParameters) getDefaultDataStoreParameters(); |
|
145 |
|
|
146 |
if (this.myExplorer == null) { |
|
147 |
|
|
148 |
JDBCServerExplorerParameters seParameters = null; |
|
149 |
seParameters = (JDBCServerExplorerParameters) dataManager |
|
150 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
|
151 |
|
|
152 |
seParameters.setHost("localhost"); |
|
153 |
seParameters.setPort(3306); |
|
154 |
seParameters.setUser("test"); |
|
155 |
seParameters.setPassword("test"); |
|
156 |
seParameters.setDBName("gis"); |
|
157 |
|
|
158 |
seParameters.setUrl("jdbc:mysql://" + seParameters.getHost() + ":" |
|
159 |
+ seParameters.getPort() + "/" + seParameters.getDBName()); |
|
160 |
seParameters.setJDBCDriverClassName(Driver.class.getName()); |
|
161 |
|
|
162 |
|
|
163 |
myExplorer = (JDBCServerExplorer) dataManager |
|
164 |
.createServerExplorer(seParameters); |
|
165 |
} |
|
166 |
if (this.newParams == null) { |
|
167 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
|
168 |
|
|
169 |
newParams = (JDBCNewStoreParameters) myExplorer |
|
170 |
.getAddParameters(); |
|
171 |
|
|
172 |
|
|
173 |
// newParams.setTable(params.getTable() + "_test"); |
|
174 |
FeatureType ftOrg = store.getDefaultFeatureType(); |
|
175 |
EditableFeatureType ftTrg = ftOrg.getEditable(); |
|
176 |
|
|
177 |
// EditableFeatureType ftTrg = (EditableFeatureType) newParams |
|
178 |
// .getDefaultFeatureType(); |
|
179 |
newParams.setDefaultFeatureType(ftTrg); |
|
180 |
|
|
181 |
// FeatureAttributeDescriptor org; |
|
182 |
// EditableFeatureAttributeDescriptor trg; |
|
183 |
// Iterator iter = ftOrg.iterator(); |
|
184 |
// while (iter.hasNext()) { |
|
185 |
// org = (FeatureAttributeDescriptor) iter.next(); |
|
186 |
// trg = ftTrg.add(org.getName(), org.getDataType()); |
|
187 |
// trg.setAllowNull(org.allowNull()); |
|
188 |
// trg.setDefaultValue(org.getDefaultValue()); |
|
189 |
// trg.setGeometrySubType(org.getGeometrySubType()); |
|
190 |
// trg.setGeometryType(org.getGeometryType()); |
|
191 |
// trg.setIsAutomatic(org.isAutomatic()); |
|
192 |
// trg.setIsPrimaryKey(org.isPrimaryKey()); |
|
193 |
// trg.setIsReadOnly(org.isReadOnly()); |
|
194 |
// trg.setMaximumOccurrences(org.getMaximumOccurrences()); |
|
195 |
// trg.setMinimumOccurrences(org.getMinimumOccurrences()); |
|
196 |
// trg.setPrecision(org.getPrecision()); |
|
197 |
// trg.setSize(org.getSize()); |
|
198 |
// trg.setSRS(org.getSRS()); |
|
199 |
// trg.setAdditionalInfo("SQLType", org |
|
200 |
// .getAdditionalInfo("SQLType")); |
|
201 |
// trg.setAdditionalInfo("SQLTypeName", org |
|
202 |
// .getAdditionalInfo("SQLTypeName")); |
|
203 |
// |
|
204 |
// } |
|
205 |
// ftTrg.setDefaultGeometryAttributeName(ftOrg |
|
206 |
// .getDefaultGeometryAttributeName()); |
|
207 |
// ftTrg.setHasOID(ftOrg.hasOID()); |
|
208 |
|
|
209 |
store.dispose(); |
|
210 |
} |
|
211 |
|
|
212 |
return this.newParams; |
|
213 |
} |
|
214 |
|
|
215 |
@Override |
|
216 |
public boolean resourcesNotifyChanges() { |
|
217 |
// TODO Auto-generated method stub |
|
218 |
return false; |
|
219 |
} |
|
220 |
|
|
221 |
} |
|
0 | 222 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/test/java/org/gvsig/fmap/dal/store/jdbc/JDBCTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.store.jdbc; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
27 |
import org.gvsig.fmap.dal.exception.DataException; |
|
28 |
import org.gvsig.fmap.dal.feature.BaseTestFeatureStore; |
|
29 |
|
|
30 |
import com.mysql.jdbc.Driver; |
|
31 |
|
|
32 |
/** |
|
33 |
* @author jmvivo |
|
34 |
* |
|
35 |
*/ |
|
36 |
public class JDBCTest extends BaseTestFeatureStore { |
|
37 |
|
|
38 |
@Override |
|
39 |
protected void doSetUp() throws Exception { |
|
40 |
// Nothing to do |
|
41 |
} |
|
42 |
|
|
43 |
public DataStoreParameters getDefaultDataStoreParameters() |
|
44 |
throws DataException { |
|
45 |
JDBCStoreParameters parameters = null; |
|
46 |
parameters = (JDBCStoreParameters) dataManager |
|
47 |
.createStoreParameters(JDBCStoreProvider.NAME); |
|
48 |
|
|
49 |
|
|
50 |
parameters.setHost("localhost"); |
|
51 |
parameters.setPort(3306); |
|
52 |
parameters.setUser("test"); |
|
53 |
parameters.setPassword("test"); |
|
54 |
parameters.setDBName("gis"); |
|
55 |
parameters.setTable("alfanum"); |
|
56 |
parameters.setUrl("jdbc:mysql://" + parameters.getHost() + ":" |
|
57 |
+ parameters.getPort() + "/" + parameters.getDBName()); |
|
58 |
parameters.setJDBCDriverClassName(Driver.class.getName()); |
|
59 |
|
|
60 |
return parameters; |
|
61 |
} |
|
62 |
|
|
63 |
/* (non-Javadoc) |
|
64 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer() |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
public boolean hasExplorer() { |
|
68 |
// TODO Auto-generated method stub |
|
69 |
return true; |
|
70 |
} |
|
71 |
|
|
72 |
/* (non-Javadoc) |
|
73 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources() |
|
74 |
*/ |
|
75 |
@Override |
|
76 |
public boolean usesResources() { |
|
77 |
// TODO Auto-generated method stub |
|
78 |
return true; |
|
79 |
} |
|
80 |
|
|
81 |
} |
|
0 | 82 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.fmap.dal.store.jdbc.JDBCLibrary |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/resources/org/gvsig/fmap/dal/store/jdbc/JDBCMetadata.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<!-- |
|
3 |
|
|
4 |
gvSIG. Desktop Geographic Information System. |
|
5 |
|
|
6 |
Copyright (C) 2007-2013 gvSIG Association. |
|
7 |
|
|
8 |
This program is free software; you can redistribute it and/or |
|
9 |
modify it under the terms of the GNU General Public License |
|
10 |
as published by the Free Software Foundation; either version 3 |
|
11 |
of the License, or (at your option) any later version. |
|
12 |
|
|
13 |
This program is distributed in the hope that it will be useful, |
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
GNU General Public License for more details. |
|
17 |
|
|
18 |
You should have received a copy of the GNU General Public License |
|
19 |
along with this program; if not, write to the Free Software |
|
20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
21 |
MA 02110-1301, USA. |
|
22 |
|
|
23 |
For any additional information, do not hesitate to contact us |
|
24 |
at info AT gvsig.com, or visit our website www.gvsig.com. |
|
25 |
|
|
26 |
--> |
|
27 |
<definitions> |
|
28 |
<version>1.0.0</version> |
|
29 |
<classes> |
|
30 |
<class name="JDBC" namespace="Metadata"> |
|
31 |
<extends> |
|
32 |
<class name="SpatialProvider" namespace="Metadata"/> |
|
33 |
</extends> |
|
34 |
<description>Metadata of a JDBC store</description> |
|
35 |
<fields> |
|
36 |
</fields> |
|
37 |
</class> |
|
38 |
|
|
39 |
</classes> |
|
40 |
</definitions> |
|
0 | 41 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/resources/org/gvsig/fmap/dal/store/jdbc/JDBCParameters.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<!-- |
|
3 |
|
|
4 |
gvSIG. Desktop Geographic Information System. |
|
5 |
|
|
6 |
Copyright (C) 2007-2013 gvSIG Association. |
|
7 |
|
|
8 |
This program is free software; you can redistribute it and/or |
|
9 |
modify it under the terms of the GNU General Public License |
|
10 |
as published by the Free Software Foundation; either version 3 |
|
11 |
of the License, or (at your option) any later version. |
|
12 |
|
|
13 |
This program is distributed in the hope that it will be useful, |
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
GNU General Public License for more details. |
|
17 |
|
|
18 |
You should have received a copy of the GNU General Public License |
|
19 |
along with this program; if not, write to the Free Software |
|
20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
21 |
MA 02110-1301, USA. |
|
22 |
|
|
23 |
For any additional information, do not hesitate to contact us |
|
24 |
at info AT gvsig.com, or visit our website www.gvsig.com. |
|
25 |
|
|
26 |
--> |
|
27 |
<definitions> |
|
28 |
<version>1.0.0</version> |
|
29 |
<classes> |
|
30 |
<class name="JDBCResourceParameters"> |
|
31 |
<description></description> |
|
32 |
<extends> |
|
33 |
<class>DBResourceParameters</class> |
|
34 |
</extends> |
|
35 |
<fields> |
|
36 |
<field name="JDBCDriverClass" type="string" mandatory="true" group="Advanced"> |
|
37 |
<description>JDBC Driver class</description> |
|
38 |
</field> |
|
39 |
<field name="URL" type="string" mandatory="true" group="Connection"> |
|
40 |
<description>JDBC url connection</description> |
|
41 |
</field> |
|
42 |
<field name="Catalog" type="string" mandatory="false" group="Basic"> |
|
43 |
<description>Database catalog</description> |
|
44 |
</field> |
|
45 |
<field name="Schema" type="string" mandatory="false" group="Basic"> |
|
46 |
<description>Database schema</description> |
|
47 |
</field> |
|
48 |
</fields> |
|
49 |
</class> |
|
50 |
|
|
51 |
<class name="JDBCStoreParameters"> |
|
52 |
<extends> |
|
53 |
<class>JDBCResourceParameters</class> |
|
54 |
<class>DBStoreParameters</class> |
|
55 |
</extends> |
|
56 |
<tags> |
|
57 |
<value name="defaultGroup" value="Basic"/> |
|
58 |
</tags> |
|
59 |
<fields> |
|
60 |
</fields> |
|
61 |
</class> |
|
62 |
|
|
63 |
<class name="JDBCNewStoreParameters"> |
|
64 |
<description></description> |
|
65 |
<extends> |
|
66 |
<class>JDBCResourceParameters</class> |
|
67 |
<class>DBNewStoreParameters</class> |
|
68 |
</extends> |
|
69 |
<tags> |
|
70 |
<value name="defaultGroup" value="Basic"/> |
|
71 |
</tags> |
|
72 |
<fields> |
|
73 |
<field name="selectRole" label="Roles for select" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
74 |
<description>Roles asigned to have select privilegies</description> |
|
75 |
</field> |
|
76 |
<field name="insertRole" label="Roles for insert" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
77 |
<description>Roles asigned to have insert privilegies</description> |
|
78 |
</field> |
|
79 |
<field name="updateRole" label="Roles for update" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
80 |
<description>Roles asigned to have update privilegies</description> |
|
81 |
</field> |
|
82 |
<field name="deleteRole" label="Roles for delete" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
83 |
<description>Roles asigned to have delete privilegies</description> |
|
84 |
</field> |
|
85 |
<field name="truncateRole" label="Roles for truncate" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
86 |
<description>Roles asigned to have truncate privilegies</description> |
|
87 |
</field> |
|
88 |
<field name="referenceRole" label="Roles for reference" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
89 |
<description>Roles asigned to have reference privilegies</description> |
|
90 |
</field> |
|
91 |
<field name="triggerRole" label="Roles for trigger" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
92 |
<description>Roles asigned to have trigger privilegies</description> |
|
93 |
</field> |
|
94 |
<field name="allRole" label="Roles for all" type="String" mandatory="false" defaultValue="" group="Permissions"> |
|
95 |
<description>Roles asigned to have all privilegies</description> |
|
96 |
</field> |
|
97 |
<field name="postCreatingStatement" label="Post-creating statement" type="String" mandatory="false" defaultValue="" group="Advanced"> |
|
98 |
<description>Roles asigned to have all privilegies</description> |
|
99 |
</field> |
|
100 |
|
|
101 |
</fields> |
|
102 |
</class> |
|
103 |
|
|
104 |
<class name="JDBCServerExplorerParameters"> |
|
105 |
<extends> |
|
106 |
<class>JDBCResourceParameters</class> |
|
107 |
<class>DBServerExplorerParameters</class> |
|
108 |
</extends> |
|
109 |
<tags> |
|
110 |
<value name="defaultGroup" value="Basic"/> |
|
111 |
</tags> |
|
112 |
<fields> |
|
113 |
<field name="ShowInformationDBTables" type="boolean" |
|
114 |
mandatory="false" defaultValue="false" group="Basic"> |
|
115 |
<description>Show information tables</description> |
|
116 |
</field> |
|
117 |
</fields> |
|
118 |
</class> |
|
119 |
|
|
120 |
|
|
121 |
</classes> |
|
122 |
</definitions> |
|
0 | 123 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCSetProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.store.jdbc; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Arrays; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.List; |
|
30 |
import java.util.NoSuchElementException; |
|
31 |
|
|
32 |
import org.gvsig.fmap.dal.DataTypes; |
|
33 |
import org.gvsig.fmap.dal.exception.DataException; |
|
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
|
39 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider; |
|
40 |
import org.gvsig.tools.evaluator.Evaluator; |
|
41 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
|
42 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
|
43 |
import org.gvsig.tools.exception.BaseException; |
|
44 |
import org.slf4j.Logger; |
|
45 |
import org.slf4j.LoggerFactory; |
|
46 |
|
|
47 |
/** |
|
48 |
* @author jmvivo |
|
49 |
* |
|
50 |
*/ |
|
51 |
public class JDBCSetProvider extends AbstractFeatureSetProvider { |
|
52 |
|
|
53 |
final static private Logger logger = LoggerFactory |
|
54 |
.getLogger(JDBCSetProvider.class); |
|
55 |
|
|
56 |
protected String filter; |
|
57 |
protected String order; |
|
58 |
protected Long size = null; |
|
59 |
protected Boolean isEmpty = null; |
|
60 |
|
|
61 |
protected List resultSetIDReferenced; |
|
62 |
|
|
63 |
private JDBCHelper helper = null; |
|
64 |
|
|
65 |
private int defaultFetchSize = 1000; |
|
66 |
|
|
67 |
private long limit = -1l; |
|
68 |
|
|
69 |
private boolean directSQLMode; |
|
70 |
|
|
71 |
public JDBCSetProvider(JDBCStoreProvider store, FeatureQuery query, |
|
72 |
FeatureType featureType) throws DataException { |
|
73 |
super(store, query, featureType); |
|
74 |
this.helper = store.getHelper(); |
|
75 |
this.resultSetIDReferenced = new ArrayList(); |
|
76 |
this.directSQLMode = store.isDirectSQLMode(); |
|
77 |
|
|
78 |
if (query.hasFilter() && this.canFilter()) { |
|
79 |
setFilter(query.getFilter()); |
|
80 |
} else { |
|
81 |
setFilter(null); |
|
82 |
} |
|
83 |
|
|
84 |
if (query.hasOrder() && canOrder()) { |
|
85 |
setOrder(query.getOrder()); |
|
86 |
} else { |
|
87 |
setOrder(null); |
|
88 |
} |
|
89 |
|
|
90 |
if (query != null) { |
|
91 |
limit = query.getLimit(); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* @return the defaultFetchSize |
|
97 |
*/ |
|
98 |
public int getDefaultFetchSize() { |
|
99 |
return defaultFetchSize; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* @param defaultFetchSize |
|
104 |
* the defaultFetchSize to set |
|
105 |
*/ |
|
106 |
public void setDefaultFetchSize(int defaultFetchSize) { |
|
107 |
this.defaultFetchSize = defaultFetchSize; |
|
108 |
} |
|
109 |
|
|
110 |
protected String getSqlForEvaluator(Evaluator filter) { |
|
111 |
if (filter == null) { |
|
112 |
return null; |
|
113 |
} |
|
114 |
EvaluatorFieldsInfo info = filter.getFieldsInfo(); |
|
115 |
String filterString = filter.getSQL(); |
|
116 |
if (info == null) { |
|
117 |
return filterString; |
|
118 |
} |
|
119 |
String[] filterNames = info.getFieldNames(); |
|
120 |
String[] finalNames = new String[filterNames.length]; |
|
121 |
EvaluatorFieldValue[] fValues; |
|
122 |
|
|
123 |
List values = new ArrayList(); |
|
124 |
|
|
125 |
FeatureAttributeDescriptor attr; |
|
126 |
for (int i = 0; i < filterNames.length; i++) { |
|
127 |
attr = getFeatureType().getAttributeDescriptor(filterNames[i]); |
|
128 |
if (attr == null) { |
|
129 |
finalNames[i] = filterNames[i]; |
|
130 |
continue; |
|
131 |
} |
|
132 |
finalNames[i] = getEscapedFieldName(attr.getName()); |
|
133 |
|
|
134 |
} |
|
135 |
|
|
136 |
for (int i = 0; i < filterNames.length; i++) { |
|
137 |
if (!filterNames[i].equals(finalNames[i])) { |
|
138 |
filterString = filterString.replaceAll( |
|
139 |
"\\b" + filterNames[i] + "\\b", |
|
140 |
finalNames[i]); |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
return filterString; |
|
145 |
} |
|
146 |
|
|
147 |
|
|
148 |
protected String getEscapedFieldName(String fieldName) { |
|
149 |
if (helper == null) { |
|
150 |
helper = getJDBCStoreProvider().getHelper(); |
|
151 |
} |
|
152 |
return helper.escapeFieldName(fieldName); |
|
153 |
} |
|
154 |
|
|
155 |
|
|
156 |
protected void setOrder(FeatureQueryOrder order) { |
|
157 |
if (order == null || order.size() == 0) { |
|
158 |
this.order = null; |
|
159 |
return; |
|
160 |
} |
|
161 |
|
|
162 |
StringBuilder buffer = new StringBuilder(); |
|
163 |
Iterator iter = order.iterator(); |
|
164 |
FeatureQueryOrderMember menber; |
|
165 |
while (true) { |
|
166 |
menber = (FeatureQueryOrderMember) iter.next(); |
|
167 |
if (menber.hasEvaluator()) { |
|
168 |
buffer.append(getSqlForEvaluator(menber.getEvaluator())); |
|
169 |
} else { |
|
170 |
buffer.append(getEscapedFieldName(menber.getAttributeName())); |
|
171 |
} |
|
172 |
if (menber.getAscending()) { |
|
173 |
buffer.append(" ASC"); |
|
174 |
} else { |
|
175 |
buffer.append(" DESC"); |
|
176 |
} |
|
177 |
if (iter.hasNext()) { |
|
178 |
buffer.append(", "); |
|
179 |
} else { |
|
180 |
buffer.append(' '); |
|
181 |
break; |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
this.order = buffer.toString(); |
|
186 |
} |
|
187 |
|
|
188 |
protected void setFilter(Evaluator filter) { |
|
189 |
this.filter = getSqlForEvaluator(filter); |
|
190 |
} |
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
/* (non-Javadoc) |
|
195 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canFilter() |
|
196 |
*/ |
|
197 |
public boolean canFilter() { |
|
198 |
Evaluator filter = getQuery().getFilter(); |
|
199 |
if (directSQLMode){ |
|
200 |
return false; |
|
201 |
} |
|
202 |
if (filter != null) { |
|
203 |
if (filter.getSQL() == null || filter.getSQL().length() == 0) { |
|
204 |
return false; |
|
205 |
} else { |
|
206 |
// TODO Check Geom fields if |
|
207 |
EvaluatorFieldsInfo fInfo = filter.getFieldsInfo(); |
|
208 |
if (fInfo == null || fInfo.getFieldNames() == null) { |
|
209 |
return true; |
|
210 |
} |
|
211 |
Iterator names = Arrays.asList(fInfo.getFieldNames()) |
|
212 |
.iterator(); |
|
213 |
String name; |
|
214 |
int type; |
|
215 |
while (names.hasNext()) { |
|
216 |
name = (String) names.next(); |
|
217 |
type = |
|
218 |
this.getFeatureType() |
|
219 |
.getAttributeDescriptor(name) |
|
220 |
.getType(); |
|
221 |
if (type == DataTypes.GEOMETRY |
|
222 |
&& !this.helper.supportsGeometry()) { |
|
223 |
return false; |
|
224 |
} |
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
} |
|
229 |
|
|
230 |
return true; |
|
231 |
} |
|
232 |
|
|
233 |
} else{ |
|
234 |
return false; |
|
235 |
} |
|
236 |
} |
|
237 |
|
|
238 |
/* (non-Javadoc) |
|
239 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canIterateFromIndex() |
|
240 |
*/ |
|
241 |
public boolean canIterateFromIndex() { |
|
242 |
return helper.supportOffset(); |
|
243 |
} |
|
244 |
|
|
245 |
/* (non-Javadoc) |
|
246 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canOrder() |
|
247 |
*/ |
|
248 |
public boolean canOrder() { |
|
249 |
if (directSQLMode){ |
|
250 |
return false; |
|
251 |
} |
|
252 |
// TODO Check Geom fields if postgis not are available |
|
253 |
FeatureQuery query = getQuery(); |
|
254 |
if (query.hasOrder()) { |
|
255 |
Iterator iter = query.getOrder().iterator(); |
|
256 |
FeatureQueryOrderMember menber; |
|
257 |
String sql; |
|
258 |
while (iter.hasNext()){ |
|
259 |
menber = (FeatureQueryOrderMember) iter.next(); |
|
260 |
if (menber.hasEvaluator()){ |
|
261 |
sql = menber.getEvaluator().getSQL(); |
|
262 |
if (sql == null || sql.length() == 0) { |
|
263 |
return false; |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
} |
|
268 |
return true; |
|
269 |
} |
|
270 |
|
|
271 |
private JDBCStoreProvider getJDBCStoreProvider() { |
|
272 |
return (JDBCStoreProvider) getStore(); |
|
273 |
} |
|
274 |
|
|
275 |
@Override |
|
276 |
protected void doDispose() throws BaseException { |
|
277 |
if (resultSetIDReferenced != null) { |
|
278 |
Iterator iter = resultSetIDReferenced.iterator(); |
|
279 |
Integer resID; |
|
280 |
while (iter.hasNext()) { |
|
281 |
resID = (Integer) iter.next(); |
|
282 |
if (resID != null) { |
|
283 |
logger.warn( |
|
284 |
"ResultSet (ID {}) not closed on dispose, will close", |
|
285 |
resID); |
|
286 |
try { |
|
287 |
getJDBCStoreProvider().closeResulset(resID.intValue()); |
|
288 |
} catch (DataException e) { |
|
289 |
logger.error("Close resulset Exception", e); |
|
290 |
} |
|
291 |
} |
|
292 |
iter.remove(); |
|
293 |
} |
|
294 |
} |
|
295 |
resultSetIDReferenced = null; |
|
296 |
filter = null; |
|
297 |
order = null; |
|
298 |
size = null; |
|
299 |
isEmpty = null; |
|
300 |
} |
|
301 |
|
|
302 |
protected String getSQL(long fromIndex) throws DataException { |
|
303 |
return getJDBCStoreProvider().compoundSelect(getFeatureType(), filter, |
|
304 |
order, limit, fromIndex); |
|
305 |
|
|
306 |
} |
|
307 |
|
|
308 |
/* (non-Javadoc) |
|
309 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#getSize() |
|
310 |
*/ |
|
311 |
public long getSize() throws DataException { |
|
312 |
if (size == null) { |
|
313 |
size = new Long(getJDBCStoreProvider().getCount(filter)); |
|
314 |
} |
|
315 |
return size.longValue(); |
|
316 |
} |
|
317 |
|
|
318 |
/* (non-Javadoc) |
|
319 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#isEmpty() |
|
320 |
*/ |
|
321 |
public boolean isEmpty() throws DataException { |
|
322 |
JDBCStoreProvider store = getJDBCStoreProvider(); |
|
323 |
if (isEmpty == null) { |
|
324 |
if (size == null) { |
|
325 |
String sql = |
|
326 |
store.compoundSelect(getFeatureType(), filter, null, 1, |
|
327 |
0); |
|
328 |
int rsID = store.createResultSet(sql, getFetchSize()); |
|
329 |
isEmpty = new Boolean(!store.resulsetNext(rsID)); |
|
330 |
store.closeResulset(rsID); |
|
331 |
} else { |
|
332 |
isEmpty = new Boolean(size.longValue() < 1); |
|
333 |
} |
|
334 |
} |
|
335 |
return isEmpty.booleanValue(); |
|
336 |
} |
|
337 |
|
|
338 |
protected int getFetchSize() { |
|
339 |
long pageSize = -1; |
|
340 |
if (getQuery() != null) { |
|
341 |
pageSize = getQuery().getPageSize(); |
|
342 |
pageSize = |
|
343 |
pageSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : pageSize; |
|
344 |
} |
|
345 |
return (pageSize > 0 ? (int) pageSize : defaultFetchSize); |
|
346 |
} |
|
347 |
|
|
348 |
protected JDBCIterator createFastIterator(long index) throws DataException { |
|
349 |
if (isEmpty != null && isEmpty.booleanValue()) { |
|
350 |
return new EmptyJDBCIterator(getJDBCStoreProvider()); |
|
351 |
} |
|
352 |
int rsID = |
|
353 |
getJDBCStoreProvider().createResultSet(getSQL(index), |
|
354 |
getFetchSize()); |
|
355 |
return createDefaultFastIterator(rsID); |
|
356 |
} |
|
357 |
|
|
358 |
protected JDBCIterator createDefaultFastIterator(int resultSetID) |
|
359 |
throws DataException { |
|
360 |
return new JDBCFastIterator(getJDBCStoreProvider(), this, |
|
361 |
getFeatureType(), resultSetID); |
|
362 |
} |
|
363 |
|
|
364 |
protected JDBCIterator createIterator(long index) throws DataException { |
|
365 |
if (isEmpty != null && isEmpty.booleanValue()) { |
|
366 |
return new EmptyJDBCIterator(getJDBCStoreProvider()); |
|
367 |
} |
|
368 |
int rsID = |
|
369 |
getJDBCStoreProvider().createResultSet(getSQL(index), |
|
370 |
getFetchSize()); |
|
371 |
return createDefaultIterator(rsID); |
|
372 |
} |
|
373 |
|
|
374 |
protected JDBCIterator createDefaultIterator(int resultSetID) |
|
375 |
throws DataException { |
|
376 |
return new JDBCIterator(getJDBCStoreProvider(), this, getFeatureType(), |
|
377 |
resultSetID); |
|
378 |
} |
|
379 |
|
|
380 |
public void addResulsetReference(int resulsetID) { |
|
381 |
this.resultSetIDReferenced.add(new Integer(resulsetID)); |
|
382 |
} |
|
383 |
|
|
384 |
public void removeResulsetReference(int resulsetID) { |
|
385 |
this.resultSetIDReferenced.remove(new Integer(resulsetID)); |
|
386 |
} |
|
387 |
|
|
388 |
private class EmptyJDBCIterator extends JDBCIterator { |
|
389 |
|
|
390 |
protected EmptyJDBCIterator(JDBCStoreProvider store) throws DataException { |
|
391 |
super(store, null, null, -1); |
|
392 |
} |
|
393 |
|
|
394 |
@Override |
|
395 |
protected boolean internalHasNext() { |
|
396 |
return false; |
|
397 |
} |
|
398 |
|
|
399 |
@Override |
|
400 |
protected Object internalNext() { |
|
401 |
throw new NoSuchElementException(); |
|
402 |
} |
|
403 |
|
|
404 |
@Override |
|
405 |
protected void doDispose() throws BaseException { |
|
406 |
// nothing to do |
|
407 |
} |
|
408 |
|
|
409 |
} |
|
410 |
|
|
411 |
} |
|
0 | 412 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCFastIterator.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.store.jdbc; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.exception.DataException; |
|
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
28 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
29 |
import org.gvsig.tools.exception.BaseException; |
|
30 |
|
|
31 |
/** |
|
32 |
* @author jmvivo |
|
33 |
* |
|
34 |
*/ |
|
35 |
public class JDBCFastIterator extends JDBCIterator { |
|
36 |
|
|
37 |
protected FeatureProvider featureProvider; |
|
38 |
|
|
39 |
protected JDBCFastIterator(JDBCStoreProvider store, JDBCSetProvider set, |
|
40 |
FeatureType featureType, int resultsetID) throws DataException { |
|
41 |
super(store, set, featureType, resultsetID); |
|
42 |
this.featureProvider = super.createFeatureProvider(); |
|
43 |
} |
|
44 |
|
|
45 |
protected FeatureProvider createFeatureProvider() throws DataException { |
|
46 |
return this.featureProvider; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
protected void doDispose() throws BaseException { |
|
51 |
super.doDispose(); |
|
52 |
featureProvider = null; |
|
53 |
} |
|
54 |
|
|
55 |
} |
|
0 | 56 |
tags/org.gvsig.desktop-2.0.127/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCStoreProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.store.jdbc; |
|
25 |
|
|
26 |
import java.security.InvalidParameterException; |
|
27 |
import java.sql.Connection; |
|
28 |
import java.sql.PreparedStatement; |
|
29 |
import java.sql.ResultSet; |
|
30 |
import java.sql.SQLException; |
|
31 |
import java.sql.Statement; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Collections; |
|
34 |
import java.util.Iterator; |
|
35 |
import java.util.List; |
|
36 |
|
|
37 |
import org.cresques.cts.IProjection; |
|
38 |
import org.gvsig.fmap.dal.DALLocator; |
|
39 |
import org.gvsig.fmap.dal.DataManager; |
|
40 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
41 |
import org.gvsig.fmap.dal.DataStore; |
|
42 |
import org.gvsig.fmap.dal.DataStoreNotification; |
|
43 |
import org.gvsig.fmap.dal.DataTypes; |
|
44 |
import org.gvsig.fmap.dal.exception.CloseException; |
|
45 |
import org.gvsig.fmap.dal.exception.DataException; |
|
46 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
47 |
import org.gvsig.fmap.dal.exception.OpenException; |
|
48 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
49 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
51 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
52 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
53 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
54 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
|
55 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
56 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
57 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
|
58 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
59 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
|
60 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
|
61 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
62 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
|
63 |
import org.gvsig.fmap.dal.store.db.FeatureTypeHelper; |
|
64 |
import org.gvsig.fmap.dal.store.jdbc.exception.InvalidResultSetIdException; |
|
65 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
|
66 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
|
67 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
|
68 |
import org.gvsig.fmap.geom.Geometry; |
|
69 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
70 |
import org.gvsig.fmap.geom.GeometryManager; |
|
71 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
72 |
import org.gvsig.tools.dynobject.DynObject; |
|
73 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
|
74 |
import org.gvsig.tools.exception.BaseException; |
|
75 |
import org.slf4j.Logger; |
|
76 |
import org.slf4j.LoggerFactory; |
|
77 |
|
|
78 |
|
|
79 |
/** |
|
80 |
* @author jmvivo |
|
81 |
* |
|
82 |
*/ |
|
83 |
public class JDBCStoreProvider extends AbstractFeatureStoreProvider |
|
84 |
implements JDBCHelperUser { |
|
85 |
|
|
86 |
final static private Logger logger = LoggerFactory |
|
87 |
.getLogger(JDBCStoreProvider.class); |
|
88 |
|
|
89 |
private List<ResultSetInfo> resulsetList; |
|
90 |
|
|
91 |
public static String NAME = "JDBC"; |
|
92 |
public static String DESCRIPTION = "JDBC source"; |
|
93 |
|
|
94 |
public static final String METADATA_DEFINITION_NAME = NAME; |
|
95 |
|
|
96 |
private long mlsecondsToZombie = 1000 * 60 * 10; // 10 Min |
|
97 |
|
|
98 |
protected JDBCHelper helper; |
|
99 |
|
|
100 |
protected boolean directSQLMode; |
|
101 |
|
|
102 |
private Long totalCount = null; |
|
103 |
private GeometryManager geomManager = null; |
|
104 |
|
|
105 |
public JDBCStoreProvider(JDBCStoreParameters params, |
|
106 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
107 |
this( |
|
108 |
params, |
|
109 |
storeServices, |
|
110 |
DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
|
111 |
); |
|
112 |
} |
|
113 |
|
|
114 |
protected JDBCStoreProvider(JDBCStoreParameters params, |
|
115 |
DataStoreProviderServices storeServices, DynObject metadata) |
|
116 |
throws InitializeException { |
|
117 |
super(params, storeServices, metadata); |
|
118 |
geomManager = GeometryLocator.getGeometryManager(); |
|
119 |
|
|
120 |
resulsetList = new ArrayList<ResultSetInfo>(10); |
|
121 |
|
|
122 |
helper = createHelper(); |
|
123 |
if (params.getSQL() != null && (params.getSQL()).trim().length() > 0) { |
|
124 |
directSQLMode = true; |
|
125 |
} |
|
126 |
|
|
127 |
this.initFeatureType(); |
|
128 |
} |
|
129 |
|
|
130 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
|
131 |
try { |
|
132 |
if( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) { |
|
133 |
Envelope env = this.getEnvelope(); |
|
134 |
if( env != null ) { |
|
135 |
return env; |
|
136 |
} |
|
137 |
} else if( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) { |
|
138 |
IProjection proj; |
|
139 |
proj = this.getFeatureStore().getDefaultFeatureType().getDefaultSRS(); |
|
140 |
if( proj != null ) { |
|
141 |
return proj; |
|
142 |
} |
|
143 |
} |
|
144 |
} catch (DataException e) { |
|
145 |
throw new RuntimeException(e); |
|
146 |
} |
|
147 |
return super.getDynValue(name); |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
protected JDBCStoreParameters getJDBCParameters() { |
|
152 |
return (JDBCStoreParameters) this.getParameters(); |
|
153 |
} |
|
154 |
|
|
155 |
|
|
156 |
/** |
|
157 |
* Load data form a resulset.<br> |
|
158 |
* |
|
159 |
* <strong>Note:</strong><br> |
|
160 |
* this method have to perform <code>resouceBegin</code> at the begining and |
|
161 |
* <code>resourceEnd</code> at the end of execution. |
|
162 |
* |
|
163 |
* |
|
164 |
* @param data |
|
165 |
* @param resulsetID |
|
166 |
* |
|
167 |
* @return |
|
168 |
* @throws DataException |
|
169 |
*/ |
|
170 |
public void loadFeatureProvider(final FeatureProvider data, final int resultsetID) |
|
171 |
throws DataException { |
|
172 |
getResource().execute(new ResourceAction() { |
|
173 |
public Object run() throws Exception { |
|
174 |
ResultSet rs = getResultSet(resultsetID); |
|
175 |
FeatureAttributeDescriptor attr; |
|
176 |
Iterator<FeatureAttributeDescriptor> iter = FeatureTypeHelper.iterator(data.getType()); |
|
177 |
while (iter.hasNext()) { |
|
178 |
attr = iter.next(); |
|
179 |
loadFeatureProviderValue(data, rs, attr); |
|
180 |
} |
|
181 |
return null; |
|
182 |
} |
|
183 |
public String toString() { |
|
184 |
return "loadFeature"; |
|
185 |
} |
|
186 |
|
|
187 |
}); |
|
188 |
} |
|
189 |
|
|
190 |
protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs, |
|
191 |
FeatureAttributeDescriptor attr) throws DataException { |
|
192 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
193 |
byte[] buffer; |
|
194 |
try { |
|
195 |
buffer = rs.getBytes(attr.getIndex() + 1); |
|
196 |
if (buffer == null) { |
|
197 |
data.set(attr.getIndex(), null); |
|
198 |
} else { |
|
199 |
data.set(attr.getIndex(), this.helper.getGeometry(buffer)); |
|
200 |
} |
|
201 |
} catch (SQLException e) { |
|
202 |
throw new JDBCSQLException(e); |
|
203 |
} catch (BaseException e) { |
|
204 |
throw new ReadException(getProviderName(), e); |
|
205 |
} |
|
206 |
|
|
207 |
} else { |
|
208 |
try { |
|
209 |
data.set(attr.getIndex(), rs.getObject(attr.getIndex() + 1)); |
|
210 |
} catch (SQLException e) { |
|
211 |
throw new JDBCSQLException(e); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
public long getTimeToResulSetZombie() { |
|
217 |
return mlsecondsToZombie; |
|
218 |
} |
|
219 |
|
|
220 |
public void setTimeToResulSetZombie(long mlSeconds) { |
|
221 |
mlsecondsToZombie = mlSeconds; |
|
222 |
} |
|
223 |
|
|
224 |
private class ResultSetInfo{ |
|
225 |
private ResultSet resultSet = null; |
|
226 |
private long lastUse = 0; |
|
227 |
|
|
228 |
public ResultSetInfo(ResultSet resulSet) { |
|
229 |
this.resultSet = resulSet; |
|
230 |
used(); |
|
231 |
} |
|
232 |
|
|
233 |
private void used() { |
|
234 |
lastUse = System.currentTimeMillis(); |
|
235 |
} |
|
236 |
|
|
237 |
|
|
238 |
public ResultSet get() { |
|
239 |
used(); |
|
240 |
return resultSet; |
|
241 |
} |
|
242 |
|
|
243 |
public boolean isZombie() { |
|
244 |
return System.currentTimeMillis() - lastUse > mlsecondsToZombie; |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
public final int createResultSet(String sql, int fetchSize) |
|
249 |
throws DataException { |
|
250 |
return createResultSet(sql, null, fetchSize); |
|
251 |
} |
|
252 |
|
|
253 |
public final int createResultSet(final String sql, final Object[] values, |
|
254 |
final int fetchSize) |
|
255 |
throws DataException { |
|
256 |
synchronized (this) { |
|
257 |
checksResulsets(); |
|
258 |
return ((Integer) getResource().execute(new ResourceAction() { |
|
259 |
public Object run() throws Exception { |
|
260 |
ResultSetInfo newRs = |
|
261 |
new ResultSetInfo(createNewResultSet(sql, values, |
|
262 |
fetchSize)); |
|
263 |
int newId = getNewId(); |
|
264 |
if (newId < 0) { |
|
265 |
newId = resulsetList.size(); |
|
266 |
resulsetList.add(newRs); |
|
267 |
} else { |
|
268 |
resulsetList.set(newId, newRs); |
|
269 |
} |
|
270 |
logger.debug("Created resultset id: {} (total open: {})", |
|
271 |
newId, getResultsetOpenCount()); |
|
272 |
|
|
273 |
return Integer.valueOf(newId); |
|
274 |
} |
|
275 |
})).intValue(); |
|
276 |
} |
|
277 |
} |
|
278 |
|
|
279 |
private int getNewId() { |
|
280 |
int newId; |
|
281 |
if (resulsetList.size() < 1) { |
|
282 |
return -1; |
|
283 |
} |
|
284 |
for (newId = 0; newId < resulsetList.size(); newId++) { |
|
285 |
if (resulsetList.get(newId) == null) { |
|
286 |
return newId; |
|
287 |
} |
|
288 |
} |
|
289 |
return -1; |
|
290 |
} |
|
291 |
|
|
292 |
protected final void forceCloseAllResultSet() |
|
293 |
throws ResourceExecuteException, |
|
294 |
JDBCException { |
|
295 |
synchronized (this) { |
|
296 |
// FIXME: Esto no deberia funcionar. |
|
297 |
Iterator iter = resulsetList.iterator(); |
|
298 |
Integer rsID = null; |
|
299 |
while (iter.hasNext()) { |
|
300 |
rsID = (Integer) iter.next(); |
|
301 |
if (rsID != null) { |
|
302 |
try { |
|
303 |
forceCloseResultSet(rsID.intValue()); |
|
304 |
} catch (InvalidResultSetIdException e) { |
|
305 |
continue; |
|
306 |
} |
|
307 |
} |
|
308 |
iter.remove(); |
|
309 |
} |
|
310 |
|
|
311 |
} |
|
312 |
} |
|
313 |
|
|
314 |
protected final void forceCloseResultSet(int rsID) |
|
315 |
throws ResourceExecuteException, JDBCException, |
|
316 |
InvalidResultSetIdException { |
|
317 |
logger.warn("Close forced of resultSet ({})", rsID); |
|
318 |
closeResulset(rsID); |
|
319 |
} |
|
320 |
|
|
321 |
protected final ResultSet getResultSet(int resultsetID) |
|
322 |
throws InvalidResultSetIdException { |
|
323 |
if (resultsetID >= resulsetList.size()) { |
|
324 |
throw new InvalidResultSetIdException(resultsetID); |
|
325 |
} |
|
326 |
ResultSetInfo rsInfo = resulsetList.get(resultsetID); |
|
327 |
if (rsInfo == null) { |
|
328 |
throw new InvalidResultSetIdException(resultsetID); |
|
329 |
} |
|
330 |
return rsInfo.get(); |
|
331 |
|
|
332 |
} |
|
333 |
|
|
334 |
private ResultSet dropResultSet(int resultsetID) |
|
335 |
throws InvalidResultSetIdException { |
|
336 |
if (resultsetID >= resulsetList.size()) { |
|
337 |
throw new InvalidResultSetIdException(resultsetID); |
|
338 |
} |
|
339 |
ResultSetInfo rsInfo = (ResultSetInfo) resulsetList.get(resultsetID); |
|
340 |
if (rsInfo == null) { |
|
341 |
throw new InvalidResultSetIdException(resultsetID); |
|
342 |
} |
|
343 |
if (resultsetID == resulsetList.size() - 1) { |
|
344 |
resulsetList.remove(resultsetID); |
|
345 |
} else { |
|
346 |
resulsetList.set(resultsetID, null); |
|
347 |
} |
|
348 |
return rsInfo.get(); |
|
349 |
} |
|
350 |
|
|
351 |
|
|
352 |
public final boolean resulsetNext(final int resultsetID) |
|
353 |
throws JDBCException, |
|
354 |
InvalidResultSetIdException, ResourceExecuteException { |
|
355 |
return ((Boolean) getResource().execute(new ResourceAction() { |
|
356 |
public Object run() throws Exception { |
|
357 |
boolean bool = getResultSet(resultsetID).next(); |
|
358 |
return Boolean.valueOf(bool); |
|
359 |
} |
|
360 |
})).booleanValue(); |
|
361 |
} |
|
362 |
|
Also available in: Unified diff