Statistics
| Revision:

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 22373 jmvivo
package org.gvsig.fmap.data.feature.db.jdbc.postgresqlbin;
2 19959 jmvivo
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 24491 jmvivo
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 22373 jmvivo
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 19959 jmvivo
23
public class PostgresqlBinStoreUtils {
24
25
        private static int cursorCount=0;
26
        private static String baseCursorName=null;
27
28 19965 jmvivo
        static Connection getConnection(String dbUrl, String dbUser, String dbPass) throws InitializeException {
29 19959 jmvivo
                //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 19965 jmvivo
        static String createCursorName() {
68 19959 jmvivo
                if (baseCursorName == null){
69
                        baseCursorName = "gv_"+ System.currentTimeMillis();
70
                }
71
                cursorCount++;
72
                return baseCursorName+"_"+cursorCount;
73
        }
74
75
76
77 19965 jmvivo
        static DBFeatureType getFeatureType(Connection connection, PostgresqlStoreParameters params) throws ReadException{
78 19959 jmvivo
                DBFeatureType featureType = new DBFeatureType();
79
80
81
                loadFieldsToFeatureType(connection, params, featureType);
82
83
84 20849 jmvivo
                try {
85
                        featureType.setFieldsId(params.getFieldsId());
86
                } catch (DataException e) {
87
                        throw new ReadException(PostgresqlBinStore.DATASTORE_NAME,e);
88
                }
89 19959 jmvivo
90 22141 jmvivo
                if (params.getDefaultGeometryField() != null && params.getDefaultGeometryField() != ""){
91 23754 jjdelcerro
                        if (featureType.getIndex(params.getDefaultGeometryField())< 0){
92 19959 jmvivo
                                // FIXME: crear una nueva excepcion??
93
                                throw new InitializeException(
94
                                                PostgresqlStore.DATASTORE_NAME,
95 22141 jmvivo
                                                new Exception("Geometry Field '"+ params.getDefaultGeometryField() +"' not Found"));
96 19959 jmvivo
97
                        }
98
99 22141 jmvivo
                        featureType.setDefaultGeometry(params.getDefaultGeometryField());
100 19959 jmvivo
                }
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 20605 vcaballero
                        AttributeDescriptor attr;
126 19959 jmvivo
                        for (i=1;i<=rsMetadata.getColumnCount();i++){
127 20908 jmvivo
                                attr = getAttributeFromJDBC(featureType,conn,rsMetadata,i);
128 19959 jmvivo
                                featureType.add(attr);
129 20413 vcaballero
//                                attr.setOrdinal(i-1);
130 19959 jmvivo
                        }
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 21045 jmvivo
        private static AttributeDescriptor getAttributeFromJDBC(DefaultFeatureType fType, Connection conn,ResultSetMetaData rsMetadata,int colIndex) throws SQLException{
140 20908 jmvivo
                DBAttributeDescriptor column= (DBAttributeDescriptor) fType.createAttributeDescriptor();
141 19959 jmvivo
                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 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.INT);
151 19959 jmvivo
                                break;
152
                        case java.sql.Types.BIGINT:
153 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.LONG);
154 19959 jmvivo
                                break;
155
                        case java.sql.Types.REAL:
156 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.DOUBLE);
157 19959 jmvivo
                                column.setPrecision(rsMetadata.getPrecision(colIndex));
158
                                break;
159
                        case java.sql.Types.DOUBLE:
160 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.DOUBLE);
161 19959 jmvivo
                                column.setPrecision(rsMetadata.getPrecision(colIndex));
162
                                break;
163
                        case java.sql.Types.CHAR:
164 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.STRING);
165 19959 jmvivo
                                column.setSize(rsMetadata.getColumnDisplaySize(colIndex));
166
                                break;
167
                        case java.sql.Types.VARCHAR:
168 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.STRING);
169 19959 jmvivo
                                column.setSize(rsMetadata.getColumnDisplaySize(colIndex));
170
                                break;
171
                        case java.sql.Types.FLOAT:
172 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.FLOAT);
173 19959 jmvivo
                                column.setSize(rsMetadata.getColumnDisplaySize(colIndex));
174
                                column.setPrecision(rsMetadata.getPrecision(colIndex));
175
                                break;
176
                        case java.sql.Types.DECIMAL:
177 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.FLOAT);
178 19959 jmvivo
                                column.setSize(rsMetadata.getColumnDisplaySize(colIndex));
179
                                column.setPrecision(rsMetadata.getPrecision(colIndex));
180
                                break;
181
                        case java.sql.Types.DATE:
182 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.DATE);
183 19959 jmvivo
                                break;
184
                        case java.sql.Types.BOOLEAN:
185 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.BOOLEAN);
186 19959 jmvivo
                                break;
187
                        default:
188 23754 jjdelcerro
                                column.setType(FeatureAttributeDescriptor.OBJECT);
189 19959 jmvivo
                                break;
190
                        }
191
                } catch (java.sql.SQLException e){
192
                        throw new SQLException("","load attribute definition",e);
193 20420 vcaballero
                } catch (IsNotAttributeSettingException e) {
194
                        e.printStackTrace();
195 19959 jmvivo
                }
196
197
                return column;
198
199
        }
200
201 19965 jmvivo
        static String getJDBCUrl(String host, String db, String port) {
202 19959 jmvivo
                String url;
203
                url = "jdbc:postgresql://"+host+":" + port +"/"+db;
204
205
                return url;
206
        }
207
208
}