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