root / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresqlbin / PostgresqlBinStoreUtils.java @ 20908
History | View | Annotate | Download (6.95 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresqlbin; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.DriverManager; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.ResultSetMetaData; |
7 |
import java.sql.Statement; |
8 |
|
9 |
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor; |
10 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
11 |
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.JDBCDriverNotFoundException; |
12 |
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException; |
13 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStore; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreParameters; |
15 |
import org.gvsig.data.exception.DataException; |
16 |
import org.gvsig.data.exception.InitializeException; |
17 |
import org.gvsig.data.exception.ReadException; |
18 |
import org.gvsig.data.vectorial.AttributeDescriptor; |
19 |
import org.gvsig.data.vectorial.FeatureType; |
20 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
21 |
import org.gvsig.data.vectorial.IsNotAttributeSettingException; |
22 |
|
23 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
24 |
import com.vividsolutions.jts.io.WKBWriter; |
25 |
|
26 |
public class PostgresqlBinStoreUtils { |
27 |
|
28 |
private static WKBParser2 wkbParser = new WKBParser2(); |
29 |
private static WKBWriter wkbWriter = new WKBWriter(); |
30 |
|
31 |
private static int cursorCount=0; |
32 |
private static String baseCursorName=null; |
33 |
|
34 |
static Connection getConnection(String dbUrl, String dbUser, String dbPass) throws InitializeException { |
35 |
//TODO: Aqu? habria que implementar la llamada
|
36 |
// al Resource Manager para comprobar si ya hay
|
37 |
// una connexion a la BD
|
38 |
String connID = getConnectionResourceID(dbUrl, dbUser);
|
39 |
|
40 |
Connection conn = null; |
41 |
// IResource res = ResourceManager.getResourceManager().getResource(connID);
|
42 |
|
43 |
|
44 |
|
45 |
try {
|
46 |
Class.forName("org.postgresql.Driver"); |
47 |
} catch (ClassNotFoundException e) { |
48 |
throw new JDBCDriverNotFoundException("org.postgresql.Driver",e); |
49 |
} |
50 |
try {
|
51 |
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
|
52 |
conn.setAutoCommit(false);
|
53 |
|
54 |
} catch (java.sql.SQLException e1) {
|
55 |
throw new InitializeException(PostgresqlStore.DATASTORE_NAME,e1); |
56 |
} |
57 |
//TODO: Registrar en el Resource manager
|
58 |
// ResourceManager.getResourceManager().addResource(res);
|
59 |
|
60 |
return conn;
|
61 |
} |
62 |
|
63 |
static String getConnectionResourceID(String dbUrl,String dbUser){ |
64 |
return PostgresqlStore.CONNECTION_STRING+";"+dbUrl+";"+dbUser; |
65 |
|
66 |
} |
67 |
|
68 |
|
69 |
static String addLimitsToSQL(String aSql,int fetchSize,int page){ |
70 |
return aSql+ " limit " + fetchSize + " offset " + (fetchSize*page); |
71 |
} |
72 |
|
73 |
static String createCursorName() { |
74 |
if (baseCursorName == null){ |
75 |
baseCursorName = "gv_"+ System.currentTimeMillis(); |
76 |
} |
77 |
cursorCount++; |
78 |
return baseCursorName+"_"+cursorCount; |
79 |
} |
80 |
|
81 |
|
82 |
|
83 |
static DBFeatureType getFeatureType(Connection connection, PostgresqlStoreParameters params) throws ReadException{ |
84 |
DBFeatureType featureType = new DBFeatureType();
|
85 |
|
86 |
|
87 |
loadFieldsToFeatureType(connection, params, featureType); |
88 |
|
89 |
|
90 |
try {
|
91 |
featureType.setFieldsId(params.getFieldsId()); |
92 |
} catch (DataException e) {
|
93 |
throw new ReadException(PostgresqlBinStore.DATASTORE_NAME,e); |
94 |
} |
95 |
|
96 |
if (params.getDefaultGeometryField() != null && params.getDefaultGeometryField() != ""){ |
97 |
if (featureType.getFieldIndex(params.getDefaultGeometryField())< 0){ |
98 |
// FIXME: crear una nueva excepcion??
|
99 |
throw new InitializeException( |
100 |
PostgresqlStore.DATASTORE_NAME, |
101 |
new Exception("Geometry Field '"+ params.getDefaultGeometryField() +"' not Found")); |
102 |
|
103 |
} |
104 |
|
105 |
featureType.setDefaultGeometry(params.getDefaultGeometryField()); |
106 |
} |
107 |
|
108 |
return featureType;
|
109 |
|
110 |
} |
111 |
|
112 |
private static void loadFieldsToFeatureType(Connection conn,PostgresqlStoreParameters params,DBFeatureType featureType) throws ReadException{ |
113 |
String sql=""; |
114 |
String columns=params.getFieldsString();
|
115 |
|
116 |
if (params.getSqlSoure() != null){ |
117 |
sql = params.getSqlSoure(); |
118 |
} else {
|
119 |
sql = "Select "+columns+" from " + params.tableID() + " limit 1;"; |
120 |
} |
121 |
|
122 |
try {
|
123 |
|
124 |
Statement stAux = conn.createStatement();
|
125 |
ResultSet rs = stAux.executeQuery(sql);
|
126 |
ResultSetMetaData rsMetadata = rs.getMetaData();
|
127 |
|
128 |
int i;
|
129 |
|
130 |
featureType.setTableID(params.tableID()); |
131 |
AttributeDescriptor attr; |
132 |
for (i=1;i<=rsMetadata.getColumnCount();i++){ |
133 |
attr = getAttributeFromJDBC(featureType,conn,rsMetadata,i); |
134 |
featureType.add(attr); |
135 |
// attr.setOrdinal(i-1);
|
136 |
} |
137 |
rs.close(); |
138 |
} catch (java.sql.SQLException e) {
|
139 |
// TODO Auto-generated catch block
|
140 |
throw new SQLException(sql,"getFeatureType",e); |
141 |
} |
142 |
|
143 |
} |
144 |
|
145 |
private static AttributeDescriptor getAttributeFromJDBC(FeatureType fType, Connection conn,ResultSetMetaData rsMetadata,int colIndex) throws SQLException{ |
146 |
DBAttributeDescriptor column= (DBAttributeDescriptor) fType.createAttributeDescriptor(); |
147 |
try {
|
148 |
column.setName(rsMetadata.getColumnName(colIndex)); |
149 |
column.setSqlType(rsMetadata.getColumnType(colIndex)); |
150 |
column.setAllowNull(rsMetadata.isNullable(colIndex) == ResultSetMetaData.columnNullable);
|
151 |
column.setAutoIncrement(rsMetadata.isAutoIncrement(colIndex)); |
152 |
column.setReadOnly(rsMetadata.isReadOnly(colIndex)); |
153 |
|
154 |
switch (rsMetadata.getColumnType(colIndex)) {
|
155 |
case java.sql.Types.INTEGER:
|
156 |
column.setType(IFeatureAttributeDescriptor.TYPE_INT); |
157 |
break;
|
158 |
case java.sql.Types.BIGINT:
|
159 |
column.setType(IFeatureAttributeDescriptor.TYPE_LONG); |
160 |
break;
|
161 |
case java.sql.Types.REAL:
|
162 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
163 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
164 |
break;
|
165 |
case java.sql.Types.DOUBLE:
|
166 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
167 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
168 |
break;
|
169 |
case java.sql.Types.CHAR:
|
170 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
171 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
172 |
break;
|
173 |
case java.sql.Types.VARCHAR:
|
174 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
175 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
176 |
break;
|
177 |
case java.sql.Types.FLOAT:
|
178 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
179 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
180 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
181 |
break;
|
182 |
case java.sql.Types.DECIMAL:
|
183 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
184 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
185 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
186 |
break;
|
187 |
case java.sql.Types.DATE:
|
188 |
column.setType(IFeatureAttributeDescriptor.TYPE_DATE); |
189 |
break;
|
190 |
case java.sql.Types.BOOLEAN:
|
191 |
column.setType(IFeatureAttributeDescriptor.TYPE_BOOLEAN); |
192 |
break;
|
193 |
default:
|
194 |
column.setType(IFeatureAttributeDescriptor.TYPE_OBJECT); |
195 |
break;
|
196 |
} |
197 |
} catch (java.sql.SQLException e){
|
198 |
throw new SQLException("","load attribute definition",e); |
199 |
} catch (IsNotAttributeSettingException e) {
|
200 |
e.printStackTrace(); |
201 |
} |
202 |
|
203 |
return column;
|
204 |
|
205 |
} |
206 |
|
207 |
static String getJDBCUrl(String host, String db, String port) { |
208 |
String url;
|
209 |
url = "jdbc:postgresql://"+host+":" + port +"/"+db; |
210 |
|
211 |
return url;
|
212 |
} |
213 |
|
214 |
} |
215 |
|