svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgesql / PostgreSQLUtils.java @ 27753
History | View | Annotate | Download (6.81 KB)
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.postgesql; |
29 |
|
30 |
import java.sql.Connection; |
31 |
import java.sql.ResultSet; |
32 |
import java.sql.ResultSetMetaData; |
33 |
import java.sql.Statement; |
34 |
import java.util.Arrays; |
35 |
import java.util.List; |
36 |
|
37 |
import org.gvsig.fmap.dal.DataTypes; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.exception.ReadException; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 |
|
43 |
public class PostgreSQLUtils { |
44 |
private static PostgreSQLUtils instance = null; |
45 |
|
46 |
static PostgreSQLUtils getInstance() {
|
47 |
if (instance == null) { |
48 |
instance = new PostgreSQLUtils();
|
49 |
} |
50 |
return instance;
|
51 |
|
52 |
} |
53 |
|
54 |
PostgreSQLResourceParameters getResourceParameters( |
55 |
PostgreSQLStoreParameters stParams) { |
56 |
PostgreSQLResourceParameters result = new PostgreSQLResourceParameters(
|
57 |
stParams.getHost(), |
58 |
stParams.getPort(), |
59 |
stParams.getDBName(), |
60 |
stParams.getUser(), |
61 |
stParams.getPassword(), |
62 |
stParams.getJDBCDriverClassName(), |
63 |
stParams.getUseSSL()); |
64 |
|
65 |
return result;
|
66 |
} |
67 |
|
68 |
void loadFeatureType(Connection conn, |
69 |
EditableFeatureType featureType, |
70 |
PostgreSQLStoreParameters params, String sql)
|
71 |
throws DataException {
|
72 |
|
73 |
loadFeatureType(conn, featureType, sql, params.getPkFields(), params |
74 |
.getDefaultGeometry()); |
75 |
|
76 |
} |
77 |
|
78 |
private void loadFeatureType(Connection conn, |
79 |
EditableFeatureType featureType, String sql, String[] pks, |
80 |
String defGeomName)
|
81 |
throws DataException {
|
82 |
|
83 |
try {
|
84 |
|
85 |
Statement stAux = conn.createStatement();
|
86 |
stAux.setFetchSize(1);
|
87 |
ResultSet rs = stAux.executeQuery(sql);
|
88 |
ResultSetMetaData rsMetadata = rs.getMetaData();
|
89 |
|
90 |
List pksList = Arrays.asList(pks); |
91 |
|
92 |
int i;
|
93 |
|
94 |
EditableFeatureAttributeDescriptor attr; |
95 |
for (i = 1; i <= rsMetadata.getColumnCount(); i++) { |
96 |
attr = getAttributeFromJDBC(featureType, conn, rsMetadata, i); |
97 |
if (pksList.contains(attr.getName())) {
|
98 |
attr.setIsPrimaryKey(true);
|
99 |
} |
100 |
if (attr.getName().equals(defGeomName)
|
101 |
&& attr.getDataType() == DataTypes.GEOMETRY) { |
102 |
featureType.setDefaultGeometryAttributeName(defGeomName); |
103 |
} |
104 |
} |
105 |
rs.close(); |
106 |
stAux.close(); |
107 |
|
108 |
|
109 |
|
110 |
} catch (java.sql.SQLException e) {
|
111 |
throw new ReadException("", e); // FIXME exception |
112 |
} finally {
|
113 |
|
114 |
} |
115 |
|
116 |
} |
117 |
|
118 |
private EditableFeatureAttributeDescriptor getAttributeFromJDBC(
|
119 |
EditableFeatureType fType, Connection conn,
|
120 |
ResultSetMetaData rsMetadata, int colIndex) throws ReadException { |
121 |
EditableFeatureAttributeDescriptor column; |
122 |
try {
|
123 |
|
124 |
switch (rsMetadata.getColumnType(colIndex)) {
|
125 |
case java.sql.Types.INTEGER:
|
126 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
127 |
DataTypes.INT); |
128 |
break;
|
129 |
case java.sql.Types.BIGINT:
|
130 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
131 |
DataTypes.LONG); |
132 |
break;
|
133 |
case java.sql.Types.REAL:
|
134 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
135 |
DataTypes.DOUBLE); |
136 |
break;
|
137 |
case java.sql.Types.DOUBLE:
|
138 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
139 |
DataTypes.DOUBLE); |
140 |
break;
|
141 |
case java.sql.Types.CHAR:
|
142 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
143 |
DataTypes.STRING); |
144 |
break;
|
145 |
case java.sql.Types.VARCHAR:
|
146 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
147 |
DataTypes.STRING); |
148 |
break;
|
149 |
case java.sql.Types.FLOAT:
|
150 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
151 |
DataTypes.FLOAT); |
152 |
break;
|
153 |
case java.sql.Types.DECIMAL:
|
154 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
155 |
DataTypes.FLOAT); |
156 |
break;
|
157 |
case java.sql.Types.DATE:
|
158 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
159 |
DataTypes.DATE); |
160 |
break;
|
161 |
case java.sql.Types.TIME:
|
162 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
163 |
DataTypes.TIME); |
164 |
break;
|
165 |
case java.sql.Types.TIMESTAMP:
|
166 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
167 |
DataTypes.TIMESTAMP); |
168 |
break;
|
169 |
case java.sql.Types.BOOLEAN:
|
170 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
171 |
DataTypes.BOOLEAN); |
172 |
break;
|
173 |
case java.sql.Types.OTHER:
|
174 |
if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
|
175 |
"geometry")) {
|
176 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
177 |
DataTypes.GEOMETRY); |
178 |
break;
|
179 |
} |
180 |
// No hacemos break para que se quede en default
|
181 |
|
182 |
default:
|
183 |
column = fType.add(rsMetadata.getColumnName(colIndex), |
184 |
DataTypes.OBJECT); |
185 |
break;
|
186 |
} |
187 |
// column.setCaseSensitive(rsMetadata.isCaseSensitive(colIndex));
|
188 |
// column.setSqlType(rsMetadata.getColumnType(colIndex));
|
189 |
column |
190 |
.setAllowNull(rsMetadata.isNullable(colIndex) == ResultSetMetaData.columnNullable);
|
191 |
column.setIsAutomatic(rsMetadata.isAutoIncrement(colIndex)); |
192 |
column.setIsReadOnly(rsMetadata.isReadOnly(colIndex)); |
193 |
// column.setWritable(rsMetadata.isWritable(colIndex));
|
194 |
// column.setClassName(rsMetadata.getColumnClassName(colIndex));
|
195 |
// column.setCatalogName(rsMetadata.getCatalogName(colIndex));
|
196 |
// column.setDefinitelyWritable(rsMetadata
|
197 |
// .isDefinitelyWritable(colIndex));
|
198 |
// column.setLabel(rsMetadata.getColumnLabel(colIndex));
|
199 |
// column.setSchemaName(rsMetadata.getSchemaName(colIndex));
|
200 |
// column.setTableName(rsMetadata.getTableName(colIndex));
|
201 |
// column.setCatalogName(rsMetadata.getCatalogName(colIndex));
|
202 |
// column.setSqlTypeName();
|
203 |
// column.setSearchable(rsMetadata.isSearchable(colIndex));
|
204 |
// column.setSigned(rsMetadata.isSigned(colIndex));
|
205 |
// column.setCurrency(rsMetadata.isCurrency(colIndex));
|
206 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
207 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
208 |
|
209 |
} catch (java.sql.SQLException e) {
|
210 |
throw new ReadException("", e); // FIXME |
211 |
} |
212 |
|
213 |
return column;
|
214 |
|
215 |
} |
216 |
|
217 |
public String getJdbcUrl(String host, Integer port, String db) { |
218 |
String url;
|
219 |
String sport = ""; |
220 |
if (port != null) { |
221 |
sport = ":" + port;
|
222 |
} |
223 |
url = "jdbc:postgresql://" + host + sport + "/" + db; |
224 |
|
225 |
return url;
|
226 |
} |
227 |
|
228 |
|
229 |
} |