Revision 20014

View differences:

trunk/libraries/libDataSourceDBBaseDrivers/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Test.java
236 236

  
237 237
		assertEquals(9, fc.size());
238 238

  
239
		fc.dispose();
240

  
239 241
		assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
240 242

  
241 243
		try {
......
343 345

  
344 346
		assertEquals(9, fc.size());
345 347

  
348
		fc.dispose();
349

  
346 350
		try {
347 351
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null);
348 352
		} catch (ReadException e1) {
......
361 365
		}
362 366
		assertEquals("Iteration error",2,count);
363 367

  
368
		fc.dispose();
364 369

  
370

  
365 371
		try {
366 372
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC");
367 373
		} catch (ReadException e1) {
......
383 389
		}
384 390
		assertEquals("Iteration error",9,count);
385 391

  
392
		fc.dispose();
386 393

  
394

  
387 395
		try {
388 396
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC");
389 397
		} catch (ReadException e1) {
......
406 414
		}
407 415
		assertEquals("Iteration error",9,count);
408 416

  
417
		fc.dispose();
409 418

  
419

  
420

  
410 421
		try {
411 422
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE");
412 423
		} catch (ReadException e1) {
......
432 443
			count++;
433 444
		}
434 445
		assertEquals("Iteration error",2,count);
446
		fc.dispose();
435 447

  
436 448

  
437 449

  
......
457 469
			count++;
458 470
		}
459 471
		assertEquals("Iteration error",9,count);
472
		fc.dispose();
460 473

  
461 474

  
462 475

  
......
483 496
		}
484 497

  
485 498
		assertEquals(10, fc.size());
499
		fc.dispose();
486 500

  
487 501
		try {
488 502
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null);
......
501 515
			count++;
502 516
		}
503 517
		assertEquals("Iteration error",3,count);
518
		fc.dispose();
504 519

  
505 520

  
506 521
		try {
......
523 538
			count++;
524 539
		}
525 540
		assertEquals("Iteration error",10,count);
541
		fc.dispose();
526 542

  
527 543

  
528 544
		try {
......
546 562
			count++;
547 563
		}
548 564
		assertEquals("Iteration error",10,count);
565
		fc.dispose();
549 566

  
550 567

  
551 568
		try {
......
573 590
			count++;
574 591
		}
575 592
		assertEquals("Iteration error",3,count);
593
		fc.dispose();
576 594

  
577 595

  
578 596

  
......
598 616
			count++;
599 617
		}
600 618
		assertEquals("Iteration error",10,count);
619
		fc.dispose();
601 620

  
602 621

  
603 622
		fs.cancelEditing();
......
650 669
			count++;
651 670
		}
652 671
		assertEquals("Iteration error",10,count);
672
		fc.dispose();
653 673

  
654 674

  
655 675

  
......
701 721
			fail();
702 722
		}
703 723
		assertEquals(1, fc.size());
724
		fc.dispose();
704 725

  
705 726

  
706 727
		//Eliminacion
......
720 741
		}
721 742
		assertEquals(1, fc.size());
722 743
		fs.delete((IFeature)fc.iterator().next());
744
		fc.dispose();
723 745
		try {
724 746
			fs.finishEditing();
725 747
		} catch (WriteException e) {
......
737 759
			fail();
738 760
		}
739 761
		assertEquals(9, fc.size());
762
		fc.dispose();
740 763

  
741 764

  
742 765

  
743

  
744 766
		try {
745 767
			fs.close();
746 768
		} catch (CloseException e) {
trunk/libraries/libDataSourceDBBaseDrivers/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/postgresqlTest.java
7 7
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCTest;
8 8
import org.gvsig.data.exception.CloseException;
9 9
import org.gvsig.data.exception.InitializeException;
10
import org.gvsig.data.exception.OpenException;
10 11
import org.gvsig.data.exception.ReadException;
12
import org.gvsig.data.exception.WriteException;
13
import org.gvsig.data.vectorial.Feature;
14
import org.gvsig.data.vectorial.IFeature;
15
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
11 16
import org.gvsig.data.vectorial.IFeatureCollection;
12 17
import org.gvsig.data.vectorial.IFeatureStore;
13 18

  
......
115 120

  
116 121
	}
117 122

  
123
	public void test__sqlMode(){
124
		Register.selfRegister();
125
		DataManager manager = DataManager.getManager();
126

  
127

  
128
		PostgresqlStoreParameters dparam =
129
			(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
130

  
131
		dparam.setHost(SERVER_IP);
132
		dparam.setPort(SERVER_PORT);
133
		dparam.setUser(SERVER_USER);
134
		dparam.setPassw(SERVER_PASWD);
135
		dparam.setSchema(SERVER_SCHEMA);
136
		dparam.setDb(SERVER_DBNAME);
137
//		dparam.setTableName("prueba");
138
		dparam.setSqlSoure("Select * from "+ SERVER_SCHEMA+".prueba");
139
		dparam.setFieldId(new String[] {"gid"});
140
		dparam.setFields(new String[] {"*"});
141
		dparam.setDefaultGeometryField("the_geom");
142

  
143
		storeTest(dparam, null, null, false);
144

  
145
		Exception exc = null;
146

  
147
		IFeatureStore fs=null;
148
		try {
149
			fs = (IFeatureStore)manager.createDataStore(dparam);
150
		} catch (InitializeException e) {
151
			e.printStackTrace();
152
			fail("Exception:" + e);
153
		}
154
		assertNotNull("Can't create Feature Store", fs);
155

  
156
		try {
157
			fs.open();
158
		} catch (OpenException e2) {
159
			e2.printStackTrace();
160
			fail();
161
		}
162

  
163

  
164
		Iterator it;
165
		IFeatureCollection fc =null;
166

  
167
		try {
168
			fc = (IFeatureCollection)fs.getDataCollection();
169
		} catch (ReadException e1) {
170
			e1.printStackTrace();
171
			fail();
172
		}
173

  
174
		assertEquals(9, fc.size());
175

  
176
		fc.dispose();
177

  
178
		assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
179

  
180
		try {
181
			fs.startEditing();
182
		} catch (ReadException e1) {
183
			exc=e1;
184
		}
185
		assertNotNull("Edition allowed in sqlSource mode",exc);
186

  
187
		exc=null;
188
		try {
189
			fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like 'B%'",null);
190
		} catch (ReadException e1) {
191
			exc=e1;
192
		}
193
		assertNotNull("Filter allowed in sqlSource mode",exc);
194

  
195
		exc=null;
196
		try {
197
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE");
198
		} catch (ReadException e1) {
199
			exc=e1;
200
		}
201
		assertNotNull("Order allowed in sqlSource mode",exc);
202

  
203

  
204
		try {
205
			fs.close();
206
		} catch (CloseException e) {
207
			e.printStackTrace();
208
			fail("Exception:" + e);
209
		}
210

  
211
	}
212

  
213
	public void test2(){
214
		Register.selfRegister();
215
		DataManager manager = DataManager.getManager();
216

  
217

  
218
		PostgresqlStoreParameters dparam =
219
			(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
220

  
221
		dparam.setHost(SERVER_IP);
222
		dparam.setPort(SERVER_PORT);
223
		dparam.setUser(SERVER_USER);
224
		dparam.setPassw(SERVER_PASWD);
225
		dparam.setSchema(SERVER_SCHEMA);
226
		dparam.setDb(SERVER_DBNAME);
227
		dparam.setTableName("prueba");
228
		dparam.setFieldId(new String[] {"gid"});
229
		dparam.setFields(new String[] {"*"});
230
		dparam.setDefaultGeometryField("the_geom");
231

  
232
//		storeTest(dparam, null, null, true);
233
		storeTest(dparam, null, null, false);
234

  
235
		IFeatureStore fs=null;
236
		try {
237
			fs = (IFeatureStore)manager.createDataStore(dparam);
238
		} catch (InitializeException e) {
239
			e.printStackTrace();
240
			fail("Exception:" + e);
241
		}
242
		assertNotNull("Can't create Feature Store", fs);
243

  
244
		try {
245
			fs.open();
246
		} catch (OpenException e2) {
247
			e2.printStackTrace();
248
			fail();
249
		}
250

  
251
		Iterator it;
252
		IFeatureCollection fc =null;
253
		Comparable v1,v2;
254
		IFeature feature,pfeature;
255
		long count;
256

  
257

  
258
		try {
259
			fc = (IFeatureCollection)fs.getDataCollection();
260
		} catch (ReadException e1) {
261
			e1.printStackTrace();
262
			fail();
263
		}
264

  
265
		assertEquals(9, fc.size());
266

  
267
		fc.dispose();
268

  
269
		try {
270
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null);
271
		} catch (ReadException e1) {
272
			e1.printStackTrace();
273
			fail();
274
		}
275

  
276
		assertEquals(2, fc.size());
277

  
278
		it = fc.iterator();
279
		count=0;
280
		while (it.hasNext()){
281
			feature = (IFeature)it.next();
282
			assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
283
			count++;
284
		}
285
		assertEquals("Iteration error",2,count);
286

  
287
		fc.dispose();
288

  
289

  
290
		try {
291
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC");
292
		} catch (ReadException e1) {
293
			e1.printStackTrace();
294
			fail();
295
		}
296
		assertEquals(9, fc.size());
297
		it = fc.iterator();
298
		count=0;
299
		pfeature = (IFeature)it.next();
300
		count++;
301
		while (it.hasNext()){
302
			feature = (IFeature)it.next();
303
			v1 = (Comparable)pfeature.get("nombre");
304
			v2 = (Comparable)feature.get("nombre");
305
			pfeature=feature;
306
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
307
			count++;
308
		}
309
		assertEquals("Iteration error",9,count);
310

  
311
		fc.dispose();
312

  
313

  
314
		try {
315
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC");
316
		} catch (ReadException e1) {
317
			e1.printStackTrace();
318
			fail();
319
		}
320
		assertEquals(9, fc.size());
321
		it = fc.iterator();
322

  
323
		count=0;
324
		pfeature = (IFeature)it.next();
325
		count++;
326
		while (it.hasNext()){
327
			feature = (IFeature)it.next();
328
			v1 = (Comparable)pfeature.get("nombre");
329
			v2 = (Comparable)feature.get("nombre");
330
			pfeature=feature;
331
			assertTrue("Short error", (v1.compareTo(v1) >= 0));
332
			count++;
333
		}
334
		assertEquals("Iteration error",9,count);
335

  
336
		fc.dispose();
337

  
338

  
339

  
340
		try {
341
			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","NOMBRE");
342
		} catch (ReadException e1) {
343
			e1.printStackTrace();
344
			fail();
345
		}
346

  
347
		assertEquals(2, fc.size());
348

  
349
		it = fc.iterator();
350

  
351
		count=0;
352
		pfeature = (IFeature)it.next();
353
		assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
354
		count++;
355
		while (it.hasNext()){
356
			feature = (IFeature)it.next();
357
			assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
358
			v1 = (Comparable)pfeature.get("nombre");
359
			v2 = (Comparable)feature.get("nombre");
360
			pfeature=feature;
361
			assertTrue("Short error", (v1.compareTo(v1) <= 0));
362
			count++;
363
		}
364
		assertEquals("Iteration error",2,count);
365
		fc.dispose();
366

  
367

  
368

  
369
		try {
370
			fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(nombre) Desc");
371
		} catch (ReadException e1) {
372
			e1.printStackTrace();
373
			fail();
374
		}
375
		assertEquals(9, fc.size());
376
		it = fc.iterator();
377
		count=0;
378
		pfeature = (IFeature)it.next();
379
		System.out.println(pfeature.getString("nombre"));
380
		count++;
381
		while (it.hasNext()){
382
			feature = (IFeature)it.next();
383
			v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
384
			v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
385
			pfeature=feature;
386
			assertTrue("Short error", (v1.compareTo(v2) >= 0));
387
			System.out.println(pfeature.getString("nombre"));
388
			count++;
389
		}
390
		assertEquals("Iteration error",9,count);
391
		fc.dispose();
392

  
393

  
394

  
395
		/// CON  EDICION
396
//		try {
397
//			fs.startEditing();
398
//		} catch (ReadException e1) {
399
//			e1.printStackTrace();
400
//			fail();
401
//		}
402
//
403
//
404
//		IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType());
405
//		newFeature.set("nombre","BuRjaSOT");
406
//		newFeature.set("TIPO","MUNICIPIO");
407
//		fs.insert(newFeature);
408
//
409
//
410
//		try {
411
//			fc = (IFeatureCollection)fs.getDataCollection();
412
//		} catch (ReadException e1) {
413
//			e1.printStackTrace();
414
//			fail();
415
//		}
416
//
417
//		assertEquals(10, fc.size());
418
//		fc.dispose();
419
//
420
//		try {
421
//			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null);
422
//		} catch (ReadException e1) {
423
//			e1.printStackTrace();
424
//			fail();
425
//		}
426
//
427
//		assertEquals(3, fc.size());
428
//
429
//		it = fc.iterator();
430
//		count=0;
431
//		while (it.hasNext()){
432
//			feature = (IFeature)it.next();
433
//			assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
434
//			count++;
435
//		}
436
//		assertEquals("Iteration error",3,count);
437
//		fc.dispose();
438
//
439
//
440
//		try {
441
//			fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC");
442
//		} catch (ReadException e1) {
443
//			e1.printStackTrace();
444
//			fail();
445
//		}
446
//		assertEquals(10, fc.size());
447
//		it = fc.iterator();
448
//		count=0;
449
//		pfeature = (IFeature)it.next();
450
//		count++;
451
//		while (it.hasNext()){
452
//			feature = (IFeature)it.next();
453
//			v1 = (Comparable)pfeature.get("nombre");
454
//			v2 = (Comparable)feature.get("nombre");
455
//			pfeature=feature;
456
//			assertTrue("Short error", (v1.compareTo(v1) <= 0));
457
//			count++;
458
//		}
459
//		assertEquals("Iteration error",10,count);
460
//		fc.dispose();
461
//
462
//
463
//		try {
464
//			fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC");
465
//		} catch (ReadException e1) {
466
//			e1.printStackTrace();
467
//			fail();
468
//		}
469
//		assertEquals(10, fc.size());
470
//		it = fc.iterator();
471
//
472
//		count=0;
473
//		pfeature = (IFeature)it.next();
474
//		count++;
475
//		while (it.hasNext()){
476
//			feature = (IFeature)it.next();
477
//			v1 = (Comparable)pfeature.get("nombre");
478
//			v2 = (Comparable)feature.get("nombre");
479
//			pfeature=feature;
480
//			assertTrue("Short error", (v1.compareTo(v1) >= 0));
481
//			count++;
482
//		}
483
//		assertEquals("Iteration error",10,count);
484
//		fc.dispose();
485
//
486
//
487
//		try {
488
//			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","NOMBRE");
489
//		} catch (ReadException e1) {
490
//			e1.printStackTrace();
491
//			fail();
492
//		}
493
//
494
//		assertEquals(3, fc.size());
495
//
496
//		it = fc.iterator();
497
//
498
//		count=0;
499
//		pfeature = (IFeature)it.next();
500
//		assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
501
//		count++;
502
//		while (it.hasNext()){
503
//			feature = (IFeature)it.next();
504
//			assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
505
//			v1 = (Comparable)pfeature.get("nombre");
506
//			v2 = (Comparable)feature.get("nombre");
507
//			pfeature=feature;
508
//			assertTrue("Short error", (v1.compareTo(v1) <= 0));
509
//			count++;
510
//		}
511
//		assertEquals("Iteration error",3,count);
512
//		fc.dispose();
513
//
514
//
515
//
516
//		try {
517
//			fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc");
518
//		} catch (ReadException e1) {
519
//			e1.printStackTrace();
520
//			fail();
521
//		}
522
//		assertEquals(10, fc.size());
523
//		it = fc.iterator();
524
//		count=0;
525
//		pfeature = (IFeature)it.next();
526
//		System.out.println(pfeature.getString("nombre"));
527
//		count++;
528
//		while (it.hasNext()){
529
//			feature = (IFeature)it.next();
530
//			v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
531
//			v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
532
//			pfeature=feature;
533
//			assertTrue("Short error", (v1.compareTo(v2) >= 0));
534
//			System.out.println(pfeature.getString("nombre"));
535
//			count++;
536
//		}
537
//		assertEquals("Iteration error",10,count);
538
//		fc.dispose();
539
//
540
//
541
//		fs.cancelEditing();
542
//
543
//
544
//		//Insertar un elemento
545
//		try{
546
//			fs.startEditing();
547
//		} catch (ReadException e1) {
548
//			e1.printStackTrace();
549
//			fail();
550
//		}
551
//
552
//		feature = fs.createDefaultFeature(true);
553
//		feature.set("id", 90000);
554
//		feature.set("nombre","BurJASOT");
555
//		feature.set("tipo", "OTRO");
556
//		fs.insert(feature);
557
//
558
//		try {
559
//			fs.finishEditing();
560
//		} catch (WriteException e) {
561
//			e.printStackTrace();
562
//			fail("Exception: "+e);
563
//		} catch (ReadException e) {
564
//			e.printStackTrace();
565
//			fail("Exception: "+e);
566
//		}
567
//
568
//
569
//		try {
570
//			fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(nombre) Desc");
571
//		} catch (ReadException e1) {
572
//			e1.printStackTrace();
573
//			fail();
574
//		}
575
//		assertEquals(10, fc.size());
576
//		it = fc.iterator();
577
//		count=0;
578
//		pfeature = (IFeature)it.next();
579
//		System.out.println(pfeature.getString("nombre"));
580
//		count++;
581
//		while (it.hasNext()){
582
//			feature = (IFeature)it.next();
583
//			v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
584
//			v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
585
//			pfeature=feature;
586
//			assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0));
587
//			System.out.println(pfeature.getString("nombre"));
588
//			count++;
589
//		}
590
//		assertEquals("Iteration error",10,count);
591
//		fc.dispose();
592
//
593
//
594
//
595
//		//Actualizacion
596
//		try{
597
//			fs.startEditing();
598
//		} catch (ReadException e1) {
599
//			e1.printStackTrace();
600
//			fail();
601
//		}
602
//
603
//
604
//		try {
605
//			fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) = 'burjasot'",null);
606
//		} catch (ReadException e1) {
607
//			e1.printStackTrace();
608
//			fail();
609
//		}
610
//		assertEquals(1, fc.size());
611
//		Feature old =(Feature)fc.iterator().next();
612
//		feature = fs.createDefaultFeature(false);
613
//		it = fs.getDefaultFeatureType().iterator();
614
//		IFeatureAttributeDescriptor attr;
615
//		while(it.hasNext()){
616
//			 attr = (IFeatureAttributeDescriptor)it.next();
617
////			 if (attr.getDataType() == IFeatureAttributeDescriptor.TYPE_GEOMETRY){
618
////				 feature.set(attr.getName(), old.get(attr.getName()));
619
////			 } else {
620
//				 feature.set(attr.getName(), old.get(attr.getName()));
621
////			 }
622
//		}
623
//		feature.set("NOMBRE", feature.getString("nombre")+"__KK__");
624
//
625
//		fs.update(feature, old);
626
//		try {
627
//			fs.finishEditing();
628
//		} catch (WriteException e) {
629
//			e.printStackTrace();
630
//			fail("Exception: "+e);
631
//		} catch (ReadException e) {
632
//			e.printStackTrace();
633
//			fail("Exception: "+e);
634
//		}
635
//
636
//		try {
637
//			fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null);
638
//		} catch (ReadException e1) {
639
//			e1.printStackTrace();
640
//			fail();
641
//		}
642
//		assertEquals(1, fc.size());
643
//		fc.dispose();
644
//
645
//
646
//		//Eliminacion
647
//		try{
648
//			fs.startEditing();
649
//		} catch (ReadException e1) {
650
//			e1.printStackTrace();
651
//			fail();
652
//		}
653
//
654
//
655
//		try {
656
//			fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null);
657
//		} catch (ReadException e1) {
658
//			e1.printStackTrace();
659
//			fail();
660
//		}
661
//		assertEquals(1, fc.size());
662
//		fs.delete((IFeature)fc.iterator().next());
663
//		fc.dispose();
664
//		try {
665
//			fs.finishEditing();
666
//		} catch (WriteException e) {
667
//			e.printStackTrace();
668
//			fail("Exception: "+e);
669
//		} catch (ReadException e) {
670
//			e.printStackTrace();
671
//			fail("Exception: "+e);
672
//		}
673
//
674
//		try {
675
//			fc = (IFeatureCollection)fs.getDataCollection();
676
//		} catch (ReadException e1) {
677
//			e1.printStackTrace();
678
//			fail();
679
//		}
680
//		assertEquals(9, fc.size());
681
//		fc.dispose();
682

  
683

  
684

  
685
		try {
686
			fs.close();
687
		} catch (CloseException e) {
688
			e.printStackTrace();
689
			fail("Exception:" + e);
690
		}
691

  
692

  
693

  
694
	}
695

  
696

  
118 697
}
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlFeatureCollection.java
89 89
	}
90 90

  
91 91
	public int size() {
92
		checkModified();
92 93
		try {
93 94
			if (this.numReg < 0){
94 95
				ResultSet r=null;
......
124 125
	}
125 126

  
126 127
	public Iterator iterator() {
127

  
128
		PostgresqlIterator dbfIter;
128
		checkModified();
129
		PostgresqlIterator dbIter;
129 130
		try {
130
			dbfIter=new PostgresqlIterator((PostgresqlStore)this.store,this.featureType,this.sql,this.fetchSize);
131
			dbIter=new PostgresqlIterator((PostgresqlStore)this.store,this.featureType,this.sql,this.fetchSize);
131 132
		} catch (ReadException e) {
132 133
			throw new RuntimeException(e);
133 134
		}
134 135

  
135
		return dbfIter;
136
		return dbIter;
136 137
	}
137 138

  
138 139
	protected class PostgresqlIterator implements Iterator{
......
181 182
		}
182 183

  
183 184
		public boolean hasNext(){
185
			checkModified();
184 186
			try {
185 187
				if (rs.isLast()){
186 188
					if (this.index == this.fetchSize){
......
207 209
		}
208 210

  
209 211
		public Object next() {
212
			checkModified();
210 213
			if (!hasNext())
211 214
				throw new NoSuchElementException();
212 215
			return nextFeature();
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlFeatureCollectionEditingFiltered.java
1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql;
2

  
3

  
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.Statement;
7
import java.util.ConcurrentModificationException;
8
import java.util.Iterator;
9
import java.util.NoSuchElementException;
10

  
11
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
12
import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
14
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
15
import org.gvsig.data.exception.ReadException;
16
import org.gvsig.data.vectorial.AbstractFeatureCollection;
17
import org.gvsig.data.vectorial.IFeature;
18
import org.gvsig.data.vectorial.IFeatureType;
19
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
20
import org.gvsig.data.vectorial.filter.FeatureFilterParser;
21
import org.gvsig.exceptions.BaseException;
22

  
23
public class PostgresqlFeatureCollectionEditingFiltered extends AbstractJDBCDataFeatureCollection {
24
	protected DBFeatureType featureType;
25
	protected String totalFilter;
26
	protected String filter;
27
	protected PostgresqlStore store;
28
	private int numReg=-1;
29
	private String sql;
30
	private String sqlCount;
31
	private String totalOrder;
32
	private int fetchSize=5000;
33
	private FeatureManager featureManager;
34
	private FeatureFilterParser parser;
35

  
36

  
37
	PostgresqlFeatureCollectionEditingFiltered(FeatureManager fm,PostgresqlStore store,IFeatureType type,String filter) {
38
		this.featureManager=fm;
39
		this.store=store;
40
		this.featureType=(DBFeatureType)type;
41

  
42
		this.filter = filter;
43

  
44
		this.calculateWhere();
45
		this.totalOrder = store.getBaseOrder();
46

  
47
		this.sql = this.store.getSqlSelectPart();
48
		this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID();
49
		if (!isStringEmpty(this.totalFilter)){
50
			this.sql= this.sql + " Where " + this.totalFilter;
51
			this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
52
		}
53
		if (!isStringEmpty(this.totalOrder)){
54
			this.sql= this.sql + " Order by " + this.totalOrder;
55
		}
56

  
57
		if (this.filter!=null)
58
			parser = new FeatureFilterParser(filter,this.featureType);
59

  
60
	}
61

  
62
	private void calculateWhere(){
63
		if (isStringEmpty(this.store.getBaseWhereClause())){
64
			this.totalFilter = this.filter;
65
		} else {
66
			this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
67
		}
68
	}
69

  
70
	private ResultSet getNewResulset(String aSql) throws ReadException{
71
		this.store.open();
72

  
73
		Connection con = this.store.getCurrentConnection();
74

  
75
		try {
76
			Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
77
			return st.executeQuery(aSql);
78

  
79
		} catch (java.sql.SQLException e) {
80
			throw new SQLException(aSql,this.store.getName(),e);
81
		}
82

  
83
	}
84

  
85
	public int size() {
86
		checkModified();
87
		if (this.numReg < 0){
88
			this.numReg =0;
89
			try{
90
				Iterator iter = this.iterator();
91
				while (true){
92
					iter.next();
93
					this.numReg++;
94
				}
95
			} catch (NoSuchElementException e){
96
				//Normal condition exit
97
			}
98
		}
99
		return this.numReg;
100

  
101
	}
102

  
103

  
104
	public Iterator iterator() {
105
		checkModified();
106
		try{
107
			PostgresIterator dbIter=new PostgresIterator(this.store,this.featureType,this.sql,this.fetchSize,this.featureManager);
108
			return dbIter;
109
		} catch (BaseException e){
110
			throw new RuntimeException(e);
111
		}
112
	}
113

  
114
	protected class PostgresIterator implements Iterator{
115
		private Iterator dbIter;
116
		private Iterator mIter;
117
		public PostgresIterator(JDBCStore store,DBFeatureType featureType,String sql, int fetchSize,FeatureManager featureManager) throws ReadException{
118
			this.dbIter = new IteratorDB(store,featureType,sql,fetchSize,featureManager);
119
			this.mIter = new IteratorMemory(featureType,featureManager);
120
		}
121
		public boolean hasNext() {
122
			return dbIter.hasNext() || mIter.hasNext();
123
		}
124
		public Object next() {
125
			if (dbIter.hasNext()){
126
				return dbIter.next();
127
			}
128
			return mIter.next();
129
		}
130
		public void remove() {
131
			throw new UnsupportedOperationException();
132
		}
133

  
134

  
135

  
136
	}
137

  
138
	protected class IteratorDB implements Iterator{
139
		private boolean nextChecked=false;
140
		private IFeature feature;
141
		private ResultSet rs;
142
		private JDBCStore store;
143
		private DBFeatureType featureType;
144
		private boolean rsEOF=false;
145
		private FeatureManager featureManager;
146
		private int fetchSize;
147
		private String sql;
148
		private int page;
149
		private int index;
150

  
151

  
152
		public IteratorDB(JDBCStore store, DBFeatureType featureType,String sql,int fetchSize, FeatureManager featureManager) throws ReadException{
153
			this.store = store;
154
			this.featureType = featureType;
155
			this.featureManager= featureManager;
156
			this.fetchSize = fetchSize;
157
			this.sql = sql;
158
			this.page = 0;
159
			this.index = 0;
160
			this.createResulset();
161

  
162
		}
163

  
164
		public boolean hasNext(){
165
			checkModified();
166

  
167

  
168
			if (nextChecked){
169
				return this.feature != null;
170
			}
171
			IFeature feature=null;
172
			nextChecked=true;
173
			while (true){
174
				if (!rsEOF){
175
					try {
176
						if (rs.isLast()){
177
							if (this.index == this.fetchSize){
178
								this.index = 0;
179
								this.page++;
180
								this.createResulset();
181
								if (rs.isLast()){
182
									rsEOF = true;
183
									rs.close();
184
									continue;
185
								}
186
							} else{
187
								rsEOF = true;
188
								rs.close();
189
								continue;
190
							}
191
						} else {
192
							feature=nextFeature();
193
							if(featureManager.isDeleted(feature))
194
								continue;
195
							this.feature=feature;
196
							return true;
197

  
198
						}
199
					} catch (java.sql.SQLException e) {
200
						throw new RuntimeException(
201
								new ReadException(this.store.getName(),e)
202
						);
203
					} catch (ReadException e) {
204
						throw new RuntimeException(e);
205
					}
206

  
207
				}else {
208
					return false;
209

  
210
				}
211

  
212

  
213
			}
214
		}
215

  
216
		public Object next() {
217
			checkModified();
218
			if (!nextChecked){
219
				hasNext();
220
			}
221
			if (this.feature == null)
222
				throw new NoSuchElementException();
223
			nextChecked=false;
224
			IFeature feature = this.feature;
225
			this.feature = null;
226
			return feature;
227
		}
228

  
229
		private IFeature nextFeature() {
230
			IFeature feature=null;
231
			try {
232
				if(rs.next()){
233
					feature=PostgresqlStoreUtils.createFeature(this.store, this.rs, featureType);
234
				}
235
			} catch (java.sql.SQLException e) {
236
				throw new RuntimeException(
237
						new ReadException(this.store.getName(),e)
238
					);
239
			} catch (ReadException e) {
240
				throw new RuntimeException(e);
241
			}
242
			return feature;
243
		}
244

  
245
		public void remove() {
246
			throw new UnsupportedOperationException();
247
		}
248

  
249

  
250
		protected void createResulset() throws ReadException{
251

  
252
			this.store.open();
253

  
254
			String mSql = PostgresqlStoreUtils.addLimitsToSQL(
255
					this.sql,
256
					this.fetchSize,
257
					this.page);
258
			Connection conn = ((PostgresqlStore)this.store).getCurrentConnection();
259
			try {
260
				if (this.rs != null){
261
					this.rs.close();
262
				}
263

  
264
				Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
265
				this.rs = st.executeQuery(mSql);
266

  
267
			} catch (java.sql.SQLException e) {
268
				throw new SQLException(mSql,this.store.getName(),e);
269
			}
270

  
271

  
272
		}
273

  
274
	}
275

  
276

  
277
	protected class IteratorMemory implements Iterator{
278
		protected long position=0;
279
		private boolean nextChecked=false;
280
		private IFeature feature;
281
		private FeatureManager featureManager;
282

  
283
		public IteratorMemory(DBFeatureType featureType,FeatureManager featureManager){
284
			this.featureManager= featureManager;
285
			position=0;
286
		}
287

  
288
		public boolean hasNext(){
289
			checkModified();
290

  
291

  
292
			if (nextChecked){
293
				return this.feature != null;
294
			}
295
			IFeature feature=null;
296
			nextChecked=true;
297
			while (true){
298
				if (position<featureManager.getNum()){
299
					feature=featureManager.getFeature((int)position);
300
					position++;
301

  
302
				}else{
303
					this.feature = null;
304
					return false;
305
				}
306

  
307
				if(featureManager.isDeleted(feature))
308
					continue;
309

  
310
				if (filter == null) {
311
					this.feature=feature;
312
					return true;
313

  
314
				} else {
315
					try {
316
						if (parser.match(feature)){
317
							this.feature=feature;
318
							return true;
319
						}else{
320
							continue;
321
						}
322
					} catch (Exception e) {
323
						throw new RuntimeException(e);
324
					}
325
				}
326
			}
327
		}
328

  
329
		public Object next() {
330
			checkModified();
331
			if (!nextChecked){
332
				hasNext();
333
			}
334
			if (this.feature == null)
335
				throw new NoSuchElementException();
336
			nextChecked=false;
337
			IFeature feature = this.feature;
338
			this.feature = null;
339
			return feature;
340
		}
341

  
342
		public void remove() {
343
			throw new UnsupportedOperationException();
344
		}
345
	}
346

  
347

  
348
	public void dispose() {
349
		this.store.deleteObserver(this);
350
		this.store = null;
351
		this.featureManager = null;
352
		this.featureType = null;
353
		this.parser= null;
354

  
355
	}
356

  
357
}
0 358

  
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlFeatureCollectionEditing.java
1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql;
2

  
3
import java.sql.Connection;
4
import java.sql.ResultSet;
5
import java.sql.Statement;
6
import java.util.ConcurrentModificationException;
7
import java.util.Iterator;
8
import java.util.NoSuchElementException;
9

  
10
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
11
import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection;
12
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
14
import org.gvsig.data.exception.ReadException;
15
import org.gvsig.data.vectorial.AbstractFeatureCollection;
16
import org.gvsig.data.vectorial.IFeature;
17
import org.gvsig.data.vectorial.IFeatureType;
18
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
19
import org.gvsig.exceptions.BaseException;
20

  
21
public class PostgresqlFeatureCollectionEditing extends AbstractJDBCDataFeatureCollection {
22
	protected DBFeatureType featureType;
23
	protected String totalFilter;
24
	protected PostgresqlStore store;
25
	private int numReg=-1;
26
	private String sql;
27
	private String sqlCount;
28
	private String totalOrder;
29
	private int fetchSize=5000;
30
	private FeatureManager featureManager;
31

  
32

  
33
	PostgresqlFeatureCollectionEditing(FeatureManager fm,PostgresqlStore store,IFeatureType type) {
34
		this.featureManager=fm;
35
		this.store=store;
36
		this.featureType=(DBFeatureType)type;
37

  
38
		this.totalFilter =store.getBaseWhereClause();
39
		this.totalOrder = store.getBaseOrder();
40

  
41
		this.sql = this.store.getSqlSelectPart();
42
		this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID();
43
		if (!isStringEmpty(this.totalFilter)){
44
			this.sql= this.sql + " Where " + this.totalFilter;
45
			this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
46
		}
47
		if (!isStringEmpty(this.totalOrder)){
48
			this.sql= this.sql + " Order by " + this.totalOrder;
49
		}
50

  
51
	}
52

  
53
	private ResultSet getNewResulset(String aSql) throws ReadException{
54
		this.store.open();
55

  
56
		Connection conn = this.store.getCurrentConnection();
57

  
58
		try {
59
			Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
60
			return st.executeQuery(aSql);
61

  
62
		} catch (java.sql.SQLException e) {
63
			throw new SQLException(aSql,this.store.getName(),e);
64
		}
65

  
66
	}
67

  
68
	public int size() {
69
		checkModified();
70
		try {
71
			if (this.numReg < 0){
72
				ResultSet r=null;
73
				r = this.getNewResulset(this.sqlCount);
74
				try {
75
					r.next();
76
					numReg = r.getInt(1);
77
				} catch (java.sql.SQLException e) {
78
					throw new ReadException(this.store.getName(),e);
79
				} finally{
80
					try {
81
						r.close();
82
					} catch (java.sql.SQLException e) {
83
						throw new ReadException(this.store.getName(),e);
84
					}
85
				}
86
				numReg = numReg+featureManager.getNum();
87
			}
88
			return numReg;
89
		} catch (BaseException e){
90
			throw new RuntimeException(e);
91
		}
92

  
93
	}
94

  
95

  
96
	public Iterator iterator() {
97
		checkModified();
98
		try{
99
			ResultSet r=null;
100
			r = this.getNewResulset(this.sql);
101
			PostgresIterator dbIter=new PostgresIterator(this.store,this.featureType,this.sql,this.fetchSize);
102
			return dbIter;
103
		} catch (BaseException e){
104
			throw new RuntimeException(e);
105
		}
106
	}
107

  
108
	protected class PostgresIterator implements Iterator{
109
		private ResultSet rs;
110
		private boolean nextChecked=false;
111
		private IFeature feature;
112
		private JDBCStore store;
113
		private DBFeatureType featureType;
114
		private String sql;
115
		private int fetchSize;
116
		private int page;
117
		private int index;
118
		private long position;
119
		private boolean rsEOF=false;
120

  
121
		public PostgresIterator(JDBCStore store, DBFeatureType featureType,String sql,int fetchSize)  throws ReadException{
122
			this.sql = sql;
123
			this.store = store;
124
			this.featureType = featureType;
125
			this.fetchSize= fetchSize;
126
			this.page = 0;
127
			this.index = 0;
128
			this.createResulset();
129
		}
130

  
131
		public boolean hasNext(){
132
			checkModified();
133

  
134
			IFeature feature=null;
135
			if (nextChecked){
136
				return feature != null;
137
			}
138
			nextChecked=true;
139
			while (true){
140
				if (!rsEOF){
141
					try {
142
						if (rs.isLast()){
143
							if (this.index == this.fetchSize){
144
								this.index = 0;
145
								this.page++;
146
								this.createResulset();
147
								if (rs.isLast()){
148
									rsEOF = true;
149
									rs.close();
150
									continue;
151
								}
152
							} else{
153
								rsEOF = true;
154
								rs.close();
155
								continue;
156
							}
157
						} else {
158
							feature=nextFeature();
159
						}
160
					} catch (java.sql.SQLException e) {
161
						throw new RuntimeException(
162
								new ReadException(this.store.getName(),e)
163
						);
164
					} catch (ReadException e) {
165
						throw new RuntimeException(e);
166
					}
167

  
168

  
169
				}else {
170
					if (position<featureManager.getNum()){
171
						feature=featureManager.getFeature((int)position);
172
					}else{
173
						this.feature = null;
174
						return false;
175
					}
176
				}
177

  
178

  
179

  
180
				if(featureManager.isDeleted(feature))
181
					continue;
182
				position++;
183
				this.feature=feature;
184
				return true;
185

  
186
			}
187
		}
188

  
189
		public Object next() {
190
			checkModified();
191
			if (!nextChecked){
192
				hasNext();
193
			}
194
			if (feature == null)
195
				throw new NoSuchElementException();
196
			nextChecked=false;
197
			return feature;
198
		}
199

  
200
		private IFeature nextFeature() {
201
			IFeature feature=null;
202
			try {
203
				if(rs.next()){
204
					feature=PostgresqlStoreUtils.createFeature(this.store, this.rs, featureType);
205
				}
206
			} catch (java.sql.SQLException e) {
207
				throw new RuntimeException(
208
						new ReadException(this.store.getName(),e)
209
					);
210
			} catch (ReadException e) {
211
				throw new RuntimeException(e);
212
			}
213
			return feature;
214
		}
215

  
216
		protected void createResulset() throws ReadException{
217

  
218
			this.store.open();
219

  
220
			String mSql = PostgresqlStoreUtils.addLimitsToSQL(
221
					this.sql,
222
					this.fetchSize,
223
					this.page);
224
			Connection conn = ((PostgresqlStore)this.store).getCurrentConnection();
225
			try {
226
				if (this.rs != null){
227
					this.rs.close();
228
//					this.rs.getStatement().close();
229
				}
230

  
231
				Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
232
//				System.out.println(mSql);
233
				this.rs = st.executeQuery(mSql);
234

  
235
			} catch (java.sql.SQLException e) {
236
				throw new SQLException(mSql,this.store.getName(),e);
237
			}
238

  
239

  
240
		}
241

  
242

  
243
		public void remove() {
244
			throw new UnsupportedOperationException();
245
		}
246

  
247
	}
248

  
249
	public void dispose() {
250
		this.store.deleteObserver(this);
251
		this.store = null;
252
		this.featureManager = null;
253
		this.featureType = null;
254
	}
255

  
256
}
0 257

  
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlStore.java
119 119
			if ((order != null && order != "")){
120 120
				coll=new PostgresqlFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
121 121
			} else{
122
//				if (filter == null || filter == ""){
123
//					coll=new H2FeatureCollectionEditing(featureManager,this,type);
124
//				} else {
125
//					coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
126
//				}
122
				if (filter == null || filter == ""){
123
					coll=new PostgresqlFeatureCollectionEditing(featureManager,this,type);
124
				} else {
125
					coll=new PostgresqlFeatureCollectionEditingFiltered(featureManager,this,type,filter);
126
				}
127 127
			}
128 128

  
129 129

  
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeatureCollectionEditing.java
68 68
	}
69 69

  
70 70
	public int size() {
71
		checkModified();
71 72
		try {
72 73
			if (this.numReg < 0){
73 74
				ResultSet r=null;
......
95 96

  
96 97

  
97 98
	public Iterator iterator() {
99
		checkModified();
98 100
		try{
99 101
			ResultSet r=null;
100 102
			r = this.getNewResulset(this.sql);
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeatureCollectionEditingFiltered.java
83 83

  
84 84
	public int size() {
85 85
		checkModified();
86
		try {
87
			if (this.numReg < 0){
88
				ResultSet r=null;
89
				r = this.getNewResulset(this.sqlCount);
90
				try {
91
					r.next();
92
					numReg = r.getInt(1);
93
				} catch (java.sql.SQLException e) {
94
					throw new ReadException(this.store.getName(),e);
95
				} finally{
96
					try {
97
						r.close();
98
					} catch (java.sql.SQLException e) {
99
						throw new ReadException(this.store.getName(),e);
100
					}
86
		if (this.numReg < 0){
87
			this.numReg=0;
88
			try{
89
				Iterator iter =this.iterator();
90
				while (true){
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff