Revision 22040

View differences:

branches/v2_0_0_prep/libraries/libFMap_dataDB/src/org/gvsig/fmap/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlStoreUtils.java
330 330
	}
331 331

  
332 332
	static String getDefaultSchema(Connection conn, String catalog) throws InitializeException {
333
		//TODO
334
		return null;
333
		String sql = "Select current_schema()";
334
		ResultSet rs = null;
335
		Statement st = null;
336
		String schema = null;
337
		try {
338
			st = conn.createStatement();
339
			rs = st.executeQuery(sql);
340
			rs.next();
341
			schema = rs.getString(1);
342
		} catch (java.sql.SQLException e) {
343
			throw new InitializeException(PostgresqlStore.DATASTORE_NAME, e);
344
		} finally {
345
			if (st != null) {
346
				if (rs != null) {
347
					try {
348
						rs.close();
349
					} catch (java.sql.SQLException e) {
350
						throw new InitializeException(
351
								PostgresqlStore.DATASTORE_NAME, e);
352
					}
353
				}
354
				try {
355
					st.close();
356
				} catch (java.sql.SQLException e) {
357
					throw new InitializeException(
358
							PostgresqlStore.DATASTORE_NAME, e);
359
				}
360

  
361
			}
362

  
363
		}
364

  
365
		return schema;
335 366
	}
336 367

  
337 368
}
branches/v2_0_0_prep/libraries/libFMap_dataDB/src/org/gvsig/fmap/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlExplorerParameters.java
1
package org.gvsig.fmap.data.datastores.vectorial.db.jdbc.postgresql;
2

  
3
import org.gvsig.fmap.data.datastores.vectorial.db.jdbc.JDBCExplorerParameter;
4

  
5
public class PostgresqlExplorerParameters extends JDBCExplorerParameter {
6

  
7
	private boolean showInformationDBTables=false;
8

  
9
	public String getUrl() {
10
		return PostgresqlStoreUtils.getJDBCUrl(this.getHost(), this.getDb(),
11
				this.getPort());
12

  
13
	}
14

  
15
	public String getDataExplorerName() {
16
		return PostgresqlExplorer.DATAEXPLORER_NAME;
17
	}
18

  
19
	public boolean isShowInformationDBTables() {
20
		return showInformationDBTables;
21
	}
22

  
23
	public void setShowInformationDBTables(boolean showInformationDBTables) {
24
		this.showInformationDBTables = showInformationDBTables;
25
	}
26

  
27
}
0 28

  
branches/v2_0_0_prep/libraries/libFMap_dataDB/src/org/gvsig/fmap/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlExplorer.java
1
package org.gvsig.fmap.data.datastores.vectorial.db.jdbc.postgresql;
2

  
3
import java.security.KeyException;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.sql.Statement;
8
import java.util.ArrayList;
9

  
10
import org.gvsig.fmap.data.CloseException;
11
import org.gvsig.fmap.data.DataException;
12
import org.gvsig.fmap.data.DataExplorerParameters;
13
import org.gvsig.fmap.data.DataManager;
14
import org.gvsig.fmap.data.DataStoreParameters;
15
import org.gvsig.fmap.data.InitializeException;
16
import org.gvsig.fmap.data.NewDataStoreParameters;
17
import org.gvsig.fmap.data.ReadException;
18
import org.gvsig.fmap.data.ResourceManager;
19
import org.gvsig.fmap.data.datastores.vectorial.db.DBAttributeDescriptor;
20
import org.gvsig.fmap.data.datastores.vectorial.db.DBFeatureType;
21
import org.gvsig.fmap.data.datastores.vectorial.db.jdbc.JDBCExplorer;
22
import org.gvsig.fmap.data.vectorial.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.data.vectorial.FeatureType;
24
import org.gvsig.fmap.data.vectorial.InitializeWriterException;
25
import org.gvsig.fmap.data.vectorial.NewFeatureStoreParameters;
26

  
27
public class PostgresqlExplorer extends JDBCExplorer {
28
	public static String DATAEXPLORER_NAME = "PostgresqlExplorer";
29
	private String defaultSchema;
30

  
31
	private void appendFieldToCreteSQL(DBAttributeDescriptor attr,StringBuffer sql) throws InitializeException{
32
		/**
33
		 * column_name data_type [ DEFAULT default_expr ] [ column_constraint [
34
		 * ... ] ]
35
		 *
36
		 * where column_constraint is:
37
		 *
38
		 * [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY
39
		 * KEY | CHECK (expression) | REFERENCES reftable [ ( refcolumn ) ] [
40
		 * MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON
41
		 * UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY
42
		 * DEFERRED | INITIALLY IMMEDIATE ]
43
		 */
44

  
45
		//name
46
		sql.append(attr.getName());
47
		sql.append(" ");
48

  
49
		//dataType
50
		String type =attr.getDataType();
51
		if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)){
52
			if (attr.getSize() < 1 || attr.getSize() > 255) {
53
				sql.append("text");
54
			} else {
55
				sql.append("varchar(" + attr.getSize() + ")");
56
			}
57
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)){
58
			sql.append("bool");
59
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)){
60
			sql.append("smallint");
61
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)){
62
			sql.append("date");
63
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_TIMESTAMP)){
64
			sql.append("timestamp");
65
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_TIME)){
66
			sql.append("time");
67
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)){
68
			sql.append("double precision");
69
			if (attr.getPrecision() > 0){
70
				sql.append(" "+ attr.getPrecision());
71
			}
72
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)){
73
			sql.append("real");
74
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
75
			sql.append("geometry");
76
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)){
77
			if (attr.isAutoIncrement()){
78
				sql.append("serial");
79
			}else{
80
				sql.append("integer");
81
			}
82
		} else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)){
83
			if (attr.isAutoIncrement()){
84
				sql.append("bigserial");
85
			}else{
86
				sql.append("bigint");
87
			}
88
		} else {
89
			throw new InitializeException(this.getName(),new Exception("Unsuported type "+type));
90
		}
91
		sql.append(" ");
92

  
93
//		//DefeaultValue
94
//		if (attr.getDefaultValue() != null || (attr.getDefaultValue() == null && attr.isAllowNull() )){
95
//			sql.append("DEFAULT ? ");
96
//			sqlParams.add(attr.getDefaultValue());
97
//
98
//		}
99

  
100
		//Null
101
		if (attr.isAllowNull()){
102
			sql.append("NOT NULL ");
103
		} else {
104
			sql.append("NULL ");
105
		}
106

  
107
		// Primery key
108
		if (attr.isPrimaryKey()) {
109
			sql.append("PRIMARY KEY ");
110
		}
111

  
112
	}
113

  
114

  
115
	public DataStoreParameters add(NewFeatureStoreParameters ndsp)
116
			throws InitializeException, InitializeWriterException {
117
		Connection conn;
118
		try {
119
			conn = this.getConnection();
120
		} catch (ReadException e1) {
121
			throw new InitializeException(this.getName(),e1);
122
		}
123

  
124
		try {
125
			conn.setAutoCommit(true);
126
		} catch (SQLException e) {
127
			throw new InitializeException(this.getName(),e);
128
		}
129
		DataStoreParameters params = this.add(ndsp,conn);
130

  
131
		return params;
132
	}
133

  
134

  
135
	protected DataStoreParameters add(NewFeatureStoreParameters ndsp,Connection conn)
136
		throws InitializeException, InitializeWriterException {
137

  
138
		/**
139
		 * CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
140
		 * { column_name data_type [ DEFAULT default_expr ] [ column_constraint
141
		 * [ ... ] ] | table_constraint | LIKE parent_table [ { INCLUDING |
142
		 * EXCLUDING } DEFAULTS ] } [, ... ] ) [ INHERITS ( parent_table [, ...
143
		 * ] ) ] [ WITH OIDS | WITHOUT OIDS ] [ ON COMMIT { PRESERVE ROWS |
144
		 * DELETE ROWS | DROP } ]
145
		 *
146
		 * where column_constraint is:
147
		 *
148
		 * [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY
149
		 * KEY | CHECK (expression) | REFERENCES reftable [ ( refcolumn ) ] [
150
		 * MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON
151
		 * UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY
152
		 * DEFERRED | INITIALLY IMMEDIATE ]
153
		 *
154
		 * and table_constraint is:
155
		 *
156
		 * [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] ) |
157
		 * PRIMARY KEY ( column_name [, ... ] ) | CHECK ( expression ) | FOREIGN
158
		 * KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ...
159
		 * ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE
160
		 * action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [
161
		 * INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
162
		 */
163

  
164
//		PreparedStatement st=null;
165
		Statement st=null;
166
		StringBuffer sql = new StringBuffer();
167

  
168
		if (!ndsp.isValid()){
169
			//TODO Exception
170
			throw new InitializeException(this.getName(),new Exception("Parameters not valid"));
171
		}
172
		PostgresqlStoreParameters param = (PostgresqlStoreParameters) ndsp
173
				.getDataStoreParameters();
174
		DBFeatureType fType = (DBFeatureType)ndsp.getFeatureType();
175

  
176
		sql.append("Create table " + param.tableID() + "(");
177
		DBAttributeDescriptor attr;
178
		FeatureAttributeDescriptor[] fads=(FeatureAttributeDescriptor[])fType.toArray(new FeatureAttributeDescriptor[0]);
179
		for (int j = 0; j < fads.length-1; j++) {
180

  
181
//			appendFieldToCreteSQL(attr, sql,sqlParamas);
182
			appendFieldToCreteSQL((DBAttributeDescriptor)fads[j], sql);
183
			sql.append(",");
184
		}
185
		attr = (DBAttributeDescriptor)fads[fads.length-1];
186
		appendFieldToCreteSQL(attr, sql);
187
		sql.append(")");
188

  
189
		try{
190
//			st = conn.prepareStatement(sql.toString());
191
//			for (i=0;i<sqlParamas.size();i++){
192
//				st.setObject(i+1, sqlParamas.get(i));
193
//			}
194
			st = conn.createStatement();
195
			st.execute(sql.toString());
196
		} catch (SQLException e) {
197
			throw new InitializeException(this.getName(),e);
198
		} finally{
199
			if (st != null){
200
				try {
201
					st.close();
202
				} catch (SQLException e) {
203
					// TODO ???
204
					e.printStackTrace();
205
				}
206
			}
207
		}
208
		return param;
209
	}
210

  
211
	public NewDataStoreParameters createNewDataStoreParameter() {
212
		return new PostgresqlNewStoreParameter(this.newStoreParamters());
213
	}
214

  
215
	public void remove(DataStoreParameters dsp) throws ReadException {
216
		Connection conn = this.getConnection();
217

  
218
		try {
219
			conn.setAutoCommit(true);
220
		} catch (SQLException e) {
221
			throw new InitializeException(this.getName(),e);
222
		}
223
		this.remove(dsp, conn);
224
	}
225

  
226
	protected void remove(DataStoreParameters dsp,Connection conn) throws ReadException {
227
		Statement st=null;
228
		try{
229
			st = conn.createStatement();
230
			st.execute("Drop table "
231
					+ ((PostgresqlStoreParameters) dsp).tableID());
232
		} catch (SQLException e) {
233
			throw new ReadException(this.getName(),e);
234
		} finally{
235
			if (st != null){
236
				try {
237
					st.close();
238
				} catch (SQLException e) {
239
					// TODO ???
240
					e.printStackTrace();
241
				}
242
			}
243
		}
244
	}
245

  
246
	public boolean canCreate() {
247
		return true;
248
	}
249

  
250
	public String getName() {
251
		return DATAEXPLORER_NAME;
252
	}
253

  
254
	public String getDataStoreName(){
255
		return PostgresqlStore.DATASTORE_NAME;
256
	}
257

  
258
	public void init(DataExplorerParameters parameters) throws InitializeException {
259

  
260
		PostgresqlResource tmpResource = new PostgresqlResource(
261
				(PostgresqlExplorerParameters) parameters);
262
		PostgresqlResource theResource;
263
		ResourceManager resMan = ResourceManager.getResourceManager();
264

  
265
		try {
266
			theResource = (PostgresqlResource) resMan.addResource(tmpResource);
267
		} catch (DataException e1) {
268
			throw new InitializeException(this.getName(),e1);
269
		}
270

  
271
		super.init(parameters,theResource);
272
		try {
273
			this.defaultSchema = PostgresqlStoreUtils.getDefaultSchema(this
274
					.getConnection(), this.parameters.getCatalog());
275
		} catch (ReadException e) {
276
			throw new InitializeException(this.getName(),e);
277
		}
278
	}
279

  
280
	private PostgresqlStoreParameters newStoreParamters() {
281
		DataManager manager = DataManager.getManager();
282
		PostgresqlStoreParameters param;
283
		try {
284
			param = (PostgresqlStoreParameters) manager
285
					.createDataStoreParameters(getDataStoreName());
286
		} catch (InitializeException e) {
287
			// FIXME no deber?a de ocurrir pero... que hacemos???
288
			return null;
289
		}
290
		this.parameters.fillStoreParameters(param);
291
		return param;
292
	}
293

  
294
	public DataStoreParameters[] list() throws ReadException {
295
		return this.list(((PostgresqlExplorerParameters)this.parameters).isShowInformationDBTables());
296
	}
297

  
298
	public DataStoreParameters[] list(boolean showInformationDBTables) throws ReadException {
299
		Connection conn = this.getConnection();
300
		StringBuffer sqlBuf = new StringBuffer();
301
		String sql;
302
		sqlBuf
303
				.append("SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, false as ISVIEW");
304
		sqlBuf.append("FROM INFORMATION_SCHEMA.TABLES");
305
		sqlBuf.append(" xxWHERExx ");
306
		sqlBuf.append(" union ");
307
		sqlBuf
308
				.append("SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, true as ISVIEW");
309
		sqlBuf.append("FROM INFORMATION_SCHEMA.VIEWS");
310
		sqlBuf.append(" xxWHERExx ");
311

  
312
		if (showInformationDBTables) {
313
			sql = sqlBuf
314
					.toString()
315
					.replaceAll("xxWHERExx",
316
							"");
317
		} else {
318
			sql = sqlBuf
319
					.toString()
320
					.replaceAll("xxWHERExx",
321
							"WHERE TABLE_SCHEMA NOT IN ('information_schema','pg_catalog')");
322

  
323
		}
324
		ArrayList paramList = new ArrayList();
325
		PostgresqlStoreParameters param = null;
326
		ResultSet rs = null;
327
		Statement st = null;
328
		try{
329
			st = conn.createStatement();
330
			rs = st.executeQuery(sql);
331
			while (rs.next()){
332
				param = this.newStoreParamters();
333
				param.setCatalog(rs.getString(1));
334
				param.setSchema(rs.getString(2));
335
				param.setTableName(rs.getString(3));
336
				param.setFields(new String[] { "*" });
337
				if (rs.getBoolean(4)) {
338
					param.setIsView(param.IS_VIEW);
339
				} else {
340
					param.setIsView(param.NOT_IS_VIEW);
341
				}
342
				paramList.add(param);
343
			}
344

  
345

  
346
		} catch (SQLException e) {
347
			throw new ReadException(this.getName(),e);
348
		} finally {
349
			if (st != null) {
350
				if (rs != null) {
351
					try {
352
						rs.close();
353
					} catch (java.sql.SQLException e) {
354
						throw new InitializeException(
355
								PostgresqlStore.DATASTORE_NAME, e);
356
					}
357
				}
358
				try {
359
					st.close();
360
				} catch (java.sql.SQLException e) {
361
					throw new InitializeException(
362
							PostgresqlStore.DATASTORE_NAME, e);
363
				}
364

  
365
			}
366

  
367
		}
368

  
369

  
370
		return (DataStoreParameters[]) paramList
371
				.toArray(new PostgresqlStoreParameters[0]);
372

  
373
	}
374

  
375
	public FeatureType[] getFeatureTypes(DataStoreParameters dsp) throws ReadException {
376
		return new FeatureType[] { PostgresqlStoreUtils.getFeatureType(this
377
				.getConnection(), (PostgresqlStoreParameters) dsp) };
378
	}
379

  
380
	public String getDefaultSchema(){
381
		return this.defaultSchema;
382
	}
383

  
384
	/* (non-Javadoc)
385
	 * @see org.gvsig.fmap.data.DataExplorer#dispose()
386
	 */
387
	public void dispose() throws DataException {
388
		ResourceManager resMan = ResourceManager.getResourceManager();
389

  
390
    	try {
391
			resMan.remove(this.resource);
392
		} catch (DataException e1) {
393
			throw new CloseException(this.getName(),e1);
394
		} catch (KeyException e) {
395
			throw new CloseException(this.getName(),e);
396
		}
397

  
398
	}
399

  
400
}
0 401

  
branches/v2_0_0_prep/libraries/libFMap_dataDB/src/org/gvsig/fmap/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlNewStoreParameter.java
1
package org.gvsig.fmap.data.datastores.vectorial.db.jdbc.postgresql;
2

  
3
import org.gvsig.fmap.data.DataStoreParameters;
4
import org.gvsig.fmap.data.datastores.vectorial.db.DBFeatureType;
5
import org.gvsig.fmap.data.datastores.vectorial.db.DBNewStoreParameter;
6

  
7
public class PostgresqlNewStoreParameter extends DBNewStoreParameter {
8

  
9
	PostgresqlNewStoreParameter(DataStoreParameters dsp) {
10
		this.init(dsp);
11
	}
12

  
13
	public PostgresqlStoreParameters getPostgresqlParameters() {
14
		return (PostgresqlStoreParameters) this.dsparameters;
15
	}
16

  
17
	public DBFeatureType getDBFeatureType(){
18
		return (DBFeatureType)this.featureType;
19
	}
20

  
21
	public String getDataStoreName() {
22
		return PostgresqlStore.DATASTORE_NAME;
23
	}
24

  
25
	public boolean isValid() {
26
		if (!super.isValid()){
27
			return false;
28
		}
29
		if (!(this.dsparameters instanceof PostgresqlStoreParameters)) {
30
			return false;
31
		}
32
		if (this.getDBFeatureType().getFieldsId().length != 1){
33
			return false;
34
		}
35
		return true;
36
	}
37

  
38

  
39

  
40
}
0 41

  

Also available in: Unified diff