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