Revision 10627 trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/engine/data/DataSourceFactory.java

View differences:

DataSourceFactory.java
2 2

  
3 3
import java.io.ByteArrayInputStream;
4 4
import java.io.File;
5
import java.io.IOException;
6 5
import java.rmi.server.UID;
7 6
import java.sql.Connection;
8 7
import java.sql.SQLException;
......
16 15
import com.hardcode.driverManager.DriverLoadException;
17 16
import com.hardcode.driverManager.DriverManager;
18 17
import com.hardcode.driverManager.WriterManager;
18
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
19
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
19 20
import com.hardcode.gdbms.engine.data.db.DBDataSource;
20 21
import com.hardcode.gdbms.engine.data.db.DBDataSourceFactory;
21 22
import com.hardcode.gdbms.engine.data.db.DBQuerySourceInfo;
......
102 103
	private File tempDir = new File(".");
103 104

  
104 105
	private WriterManager wm;
105
	
106

  
106 107
	private Hashtable driversNamesAliases = new Hashtable();
107 108

  
108 109
	/**
......
112 113
	 */
113 114
	private String getUID() {
114 115
		UID uid = new UID();
115
		
116

  
116 117
		String name = "gdbms" + uid.toString().replace(':','_').replace('-','_');
117 118
		return name;
118 119
	}
119
	
120

  
120 121
	/**
121 122
	 * Removes all associations between names and data sources of any layer.
122 123
	 */
......
130 131
	 *
131 132
	 * @param ds
132 133
	 *            Name of the data source to remove
133
	 *
134
	 * @throws DriverException
135
	 *             If ds is a database data source it's associated view must be
136
	 *             deleted. Thrown if could not be deleted.
134
	 * @throws WriteDriverException TODO
137 135
	 * @throws RuntimeException
138 136
	 *             If there is no data source registered with that name
139 137
	 */
140
	public void remove(DataSource ds) throws DriverException {
138
	public void remove(DataSource ds) throws WriteDriverException {
141 139
		String name = ds.getName();
142 140

  
143 141
		if (tableSource.remove(name) == null) {
......
153 151
	 *
154 152
	 * @param ds
155 153
	 *            DataSource whose view will be deleted
156
	 *
157
	 * @throws DriverException
158
	 *             If the view could not be deleted
154
	 * @throws ReadDriverException TODO
159 155
	 */
160
	private void clearView(DBDataSource ds) throws DriverException {
156
	private void clearView(DBDataSource ds) throws ReadDriverException {
161 157
		DBTableSourceInfo dbInfo = (DBTableSourceInfo) ds.getSourceInfo();
162 158
		String sql = "DROP VIEW " + dbInfo.tableName;
163 159
		ds.execute(sql);
......
165 161

  
166 162
	/**
167 163
	 * Removes the views created at query delegation
168
	 *
169
	 * @throws DriverException
170
	 *             If any view could not be removed
164
	 * @throws ReadDriverException TODO
171 165
	 */
172
	public void clearViews() throws DriverException {
166
	public void clearViews() throws ReadDriverException {
173 167
		Iterator i = sourceInfoServerViewInfo.values().iterator();
174 168

  
175 169
		while (i.hasNext()) {
......
393 387
		tableSource.put(name, info);
394 388
		nameTable.put(name, tableName);
395 389
	}
396
	
397
	
390

  
391

  
398 392
	/**
399 393
	 * A?ade un origen de datos de base de datos al sistema
400 394
	 *
......
419 413
		tableSource.put(name, info);
420 414
		nameTable.put(name, tableName);
421 415
	}
422
	
423
	
424
	
425 416

  
417

  
418

  
419

  
426 420
	/**
427 421
	 * Fills the info struct with the values passed in the parameters
428 422
	 *
......
494 488
		info.connection= conection;
495 489
		info.tableName = tableName;
496 490
		info.driverName = driverInfo;
497
	}	
498
	
499
	
491
	}
500 492

  
501
	
502
	
503
	
504
	
505
	
493

  
494

  
495

  
496

  
497

  
498

  
499

  
506 500
	/**
507 501
	 * Adds a spatial database data source
508 502
	 *
......
536 530
		nameTable.put(name, tableName);
537 531
	}
538 532

  
539
	
533

  
540 534
	/**
541 535
	 * Adds a spatial database data source
542 536
	 *
......
557 551
		info.geometryField = geometryFieldName;
558 552
		tableSource.put(name, info);
559 553
		nameTable.put(name, tableName);
560
	}	
561
	
562
	
563
	
564
	
565
	
566
	
567
	
568
	
554
	}
555

  
556

  
557

  
558

  
559

  
560

  
561

  
562

  
569 563
	/**
570 564
	 * Adds a spatial database data source
571 565
	 *
......
598 592
		return ret;
599 593
	}
600 594

  
601
	
595

  
602 596
	/**
603 597
	 * Adds a spatial database data source
604 598
	 *
605 599
	 * @param connection
606
	 *            
600
	 *
607 601
	 * @param tableName
608 602
	 *            table name
609 603
	 * @param geometryFieldName
......
621 615

  
622 616
		return ret;
623 617
	}
624
	
625
	
626
	
627
	
628
	
618

  
619

  
620

  
621

  
622

  
629 623
	/**
630 624
	 * A?ade un origen de datos de base de datos al sistema
631 625
	 *
......
661 655
		return name;
662 656
	}
663 657

  
664
	
665
	
658

  
659

  
666 660
	/**
667 661
	 * A?ade un origen de datos de base de datos al sistema
668 662
	 *
......
686 680

  
687 681
		return name;
688 682
	}
689
	
690
	
691
	
692
	
693
	
694
	
695
	
696
	
697
	
698
	
699
	
700
	
701
	
702
	
703
	
704
	
705
	
706
	
707
	
708
	
709
	
683

  
684

  
685

  
686

  
687

  
688

  
689

  
690

  
691

  
692

  
693

  
694

  
695

  
696

  
697

  
698

  
699

  
700

  
701

  
702

  
703

  
710 704
	/**
711 705
	 * A?ade un origen de datos de base de datos al sistema
712 706
	 *
......
750 744
		tableSource.put(name, info);
751 745
	}
752 746

  
753
	
747

  
754 748
	/**
755 749
	 * A?ade un origen de datos de base de datos al sistema
756 750
	 *
......
758 752
	 *            Nombre de la tabla con el que se har? referencia en las
759 753
	 *            instrucciones
760 754
	 * @param connection
761
	 *            Conexion de JDBC a la base de datos ya abierta (el 
755
	 *            Conexion de JDBC a la base de datos ya abierta (el
762 756
	 *            DataSource la usara, pero no la abrira/cerrara)
763 757
	 * @param sql
764 758
	 *            Instrucci?n SQL que define los datos de la tabla
......
782 776
		info.driverName = driverInfo;
783 777
		tableSource.put(name, info);
784 778
	}
785
	
786
	
787
	
788
	
789
	
790
	
791
	
779

  
780

  
781

  
782

  
783

  
784

  
785

  
792 786
	/**
793 787
	 * A?ade un origen de datos de base de datos al sistema
794 788
	 *
......
823 817

  
824 818
		return ret;
825 819
	}
826
	
820

  
827 821
	/**
828 822
	 * A?ade un origen de datos de base de datos al sistema
829 823
	 *
......
847 841

  
848 842
		return ret;
849 843
	}
850
	
851 844

  
845

  
852 846
	/**
853 847
	 * Cambia el nombre de una fuente de datos. Las consultas SQL que se
854 848
	 * ejecuten con el nombre anterior fallar?n
......
912 906
	 *            Nombre de la fuente de datos
913 907
	 *
914 908
	 * @return DataSource que accede a dicha fuente
915
	 *
916 909
	 * @throws DriverLoadException
917 910
	 * @throws NoSuchTableException
918
	 * @throws DriverException
911
	 * @throws ReadDriverException TODO
919 912
	 */
920 913
	public DataSource createRandomDataSource(String tableName)
921
			throws DriverLoadException, NoSuchTableException, DriverException {
914
			throws DriverLoadException, NoSuchTableException, ReadDriverException {
922 915
		return createRandomDataSource(tableName, tableName, MANUAL_OPENING);
923 916
	}
924 917

  
......
936 929
	 *            manually
937 930
	 *
938 931
	 * @return DataSource que accede a dicha fuente
939
	 *
940 932
	 * @throws DriverLoadException
941 933
	 * @throws NoSuchTableException
942
	 * @throws DriverException
934
	 * @throws ReadDriverException TODO
943 935
	 */
944 936
	public DataSource createRandomDataSource(String tableName, int mode)
945
			throws DriverLoadException, NoSuchTableException, DriverException {
937
			throws DriverLoadException, NoSuchTableException, ReadDriverException {
946 938
		return createRandomDataSource(tableName, tableName, mode);
947 939
	}
948 940

  
......
960 952
	 * @return DataSource que accede a dicha fuente de datos si la fuente de
961 953
	 *         datos es alfanum?rica o SpatialDataSource si la fuente de datos
962 954
	 *         es espacial
963
	 *
964 955
	 * @throws NoSuchTableException
965 956
	 *             Si no hay una fuente de datos registrada con ese nombre
966 957
	 * @throws DriverLoadException
967 958
	 *             Si hay alg?n error con el sistema de carga de drivers
968
	 * @throws DriverException
969
	 *             If the created DataSource have to access some data this
970
	 *             access fails
959
	 * @throws ReadDriverException TODO
971 960
	 * @throws RuntimeException
972 961
	 *             bug
973 962
	 */
974 963
	public DataSource createRandomDataSource(String tableName, String tableAlias)
975
			throws NoSuchTableException, DriverLoadException, DriverException {
964
			throws NoSuchTableException, DriverLoadException, ReadDriverException {
976 965
		return createRandomDataSource(tableName, tableAlias, MANUAL_OPENING);
977 966
	}
978 967

  
......
992 981
	 * @return DataSource que accede a dicha fuente de datos si la fuente de
993 982
	 *         datos es alfanum?rica o SpatialDataSource si la fuente de datos
994 983
	 *         es espacial
995
	 *
996 984
	 * @throws NoSuchTableException
997 985
	 *             Si no hay una fuente de datos registrada con ese nombre
998 986
	 * @throws DriverLoadException
999 987
	 *             Si hay alg?n error con el sistema de carga de drivers
1000
	 * @throws DriverException
1001
	 *             If the created DataSource have to access some data this
1002
	 *             access fails
988
	 * @throws ReadDriverException TODO
1003 989
	 * @throws RuntimeException
1004 990
	 *             bug
1005 991
	 */
1006 992
	public DataSource createRandomDataSource(String tableName,
1007 993
			String tableAlias, int mode) throws NoSuchTableException,
1008
			DriverLoadException, DriverException {
994
			DriverLoadException, ReadDriverException {
1009 995
		Object o = tableSource.get(tableName);
1010 996

  
1011 997
		if (o == null) {
......
1033 1019

  
1034 1020
			if (info instanceof FileCreationSourceInfo) {
1035 1021
				FileCreationSourceInfo creationInfo = (FileCreationSourceInfo) info;
1036
				try {
1037 1022
					if (!new File(creationInfo.file).exists()) {
1038 1023
						((FileDriver) d).createSource(creationInfo.file,
1039 1024
								creationInfo.fieldNames,
1040 1025
								creationInfo.fieldTypes);
1041 1026
					}
1042
				} catch (IOException e) {
1043
					throw new DriverException(e);
1044
				}
1045 1027
			}
1046 1028

  
1047 1029
			FileDataSource adapter;
......
1143 1125
			} catch (NoSuchTableException e) {
1144 1126
				throw new RuntimeException(
1145 1127
						"La informaci?n guardada no es consistente", e);
1146
			} catch (DriverException e) {
1128
			} catch (ReadDriverException e) {
1147 1129
				throw new RuntimeException(e);
1148 1130
			}
1149 1131
		} else {
......
1157 1139
			} catch (ParseException e) {
1158 1140
				throw new RuntimeException(
1159 1141
						"La informaci?n guardada no es consistente", e);
1160
			} catch (DriverException e) {
1161
				throw new RuntimeException(
1162
						"La informaci?n guardada no es consistente", e);
1163 1142
			} catch (SemanticException e) {
1164 1143
				throw new RuntimeException(
1165 1144
						"La informaci?n guardada no es consistente", e);
1166
			} catch (IOException e) {
1145
			} catch (EvaluationException e) {
1167 1146
				throw new RuntimeException(
1168 1147
						"La informaci?n guardada no es consistente", e);
1169
			} catch (EvaluationException e) {
1148
			} catch (ReadDriverException e) {
1170 1149
				throw new RuntimeException(
1171 1150
						"La informaci?n guardada no es consistente", e);
1172 1151
			}
......
1212 1191
	 *            opening mode
1213 1192
	 *
1214 1193
	 * @return DataSource que accede a los datos resultado de ejecutar la select
1215
	 *
1216 1194
	 * @throws DriverLoadException
1217
	 * @throws DriverException
1218
	 *             Si fallo la lectura de la fuente de datos por parte del
1219
	 *             driver
1220 1195
	 * @throws SemanticException
1221 1196
	 *             Si la instrucci?n tiene errores sem?nticos
1222
	 * @throws IOException
1223
	 *             Si se produce un error accediendo a las estructuras de datos
1224
	 *             internas
1225 1197
	 * @throws EvaluationException
1226 1198
	 *             If there's an error evaluating any expression
1199
	 * @throws ReadDriverException TODO
1227 1200
	 */
1228 1201
	public DataSource createRandomDataSource(SelectAdapter instr, int mode)
1229
			throws DriverLoadException, DriverException, SemanticException,
1230
			IOException, EvaluationException {
1202
			throws DriverLoadException, SemanticException,
1203
			EvaluationException, ReadDriverException {
1231 1204
		return getModedDataSource(getDataSource(instr), mode);
1232 1205
	}
1233 1206

  
......
1241 1214
	 *            The SQL query defining the view
1242 1215
	 *
1243 1216
	 * @return Name of the view
1244
	 *
1217
	 * @throws ReadDriverException TODO
1245 1218
	 * @throws DriverException
1246 1219
	 *             If the view cannot be created
1247 1220
	 */
1248 1221
	private String getView(DBDataSource dbds, String sql)
1249
			throws DriverException {
1222
			throws ReadDriverException {
1250 1223
		ServerViewInfo svi = (ServerViewInfo) sourceInfoServerViewInfo.get(dbds
1251 1224
				.getSourceInfo());
1252 1225

  
......
1282 1255
	 *            data source info
1283 1256
	 *
1284 1257
	 * @return DataSource
1285
	 *
1286
	 * @throws DriverException
1287
	 *             If cannot create the view
1258
	 * @throws ReadDriverException TODO
1288 1259
	 */
1289 1260
	private DBDataSource getDataSourceByQuery(String sql,
1290 1261
			AlphanumericDBDriver driver, DBTableSourceInfo dbInfo)
1291
			throws DriverException {
1262
			throws ReadDriverException {
1292 1263
		// Create the adapter
1293 1264
		DBDataSource adapter = DBDataSourceFactory.newDataSourceInstance();
1294 1265

  
......
1322 1293
	 *            Instrucci?n select origen del datasource
1323 1294
	 *
1324 1295
	 * @return DataSource que accede a los datos resultado de ejecutar la select
1325
	 *
1326
	 * @throws DriverException
1327 1296
	 * @throws SemanticException
1328
	 * @throws IOException
1329 1297
	 * @throws EvaluationException
1298
	 * @throws ReadDriverException TODO
1330 1299
	 * @throws RuntimeException
1331 1300
	 *             bug
1332 1301
	 */
1333 1302
	private DataSource getDataSource(SelectAdapter instr)
1334
			throws DriverException, SemanticException, IOException,
1335
			EvaluationException {
1303
			throws SemanticException, EvaluationException, ReadDriverException {
1336 1304
		DataSource[] tables = instr.getTables();
1337 1305

  
1338 1306
		// Estrategia de delegaci?n de la instrucci?n en el sgbd original de la
......
1355 1323
				throw new RuntimeException(e);
1356 1324
			} catch (DriverLoadException e) {
1357 1325
				throw new RuntimeException(e);
1358
			} catch (DriverException e) {
1359
				throw new RuntimeException(e);
1360 1326
			}
1361 1327
		}
1362 1328

  
......
1381 1347
	 *            DataSources involved in the instruction
1382 1348
	 *
1383 1349
	 * @return The translated sql query
1384
	 *
1385
	 * @throws DriverException
1386
	 *             If driver access fails
1387 1350
	 * @throws SemanticException
1388 1351
	 *             If the instruction is not semantically correct
1352
	 * @throws ReadDriverException TODO
1389 1353
	 */
1390 1354
	private String translateInstruction(Adapter instr, DataSource[] tables)
1391
			throws DriverException, SemanticException {
1355
			throws SemanticException, ReadDriverException {
1392 1356
		HashMap instrNameDBName = new HashMap();
1393 1357

  
1394 1358
		translateFromTables(instr, instrNameDBName);
......
1406 1370
	 *            tables involved in the search
1407 1371
	 *
1408 1372
	 * @return table name
1409
	 *
1410
	 * @throws DriverException
1411
	 *             If driver access fails
1373
	 * @throws ReadDriverException TODO
1412 1374
	 * @throws SemanticException
1413
	 *             If the instruction is not semantically correct
1414 1375
	 */
1415 1376
	private String guessTableName(String fieldName, DataSource[] tables)
1416
			throws DriverException, SemanticException {
1377
			throws ReadDriverException, SemanticException {
1417 1378
		int tableIndex = -1;
1418 1379

  
1419 1380
		for (int i = 0; i < tables.length; i++) {
......
1449 1410
	 *            as the values.
1450 1411
	 * @param tables
1451 1412
	 *            tables involved in the instruction
1452
	 *
1453
	 * @throws DriverException
1454
	 *             If driver access fails
1455 1413
	 * @throws SemanticException
1456 1414
	 *             If the instruction is not semantically correct
1415
	 * @throws ReadDriverException TODO
1457 1416
	 */
1458 1417
	private void translateColRefs(Adapter adapter, HashMap instrNameDBName,
1459
			DataSource[] tables) throws DriverException, SemanticException {
1418
			DataSource[] tables) throws SemanticException, ReadDriverException {
1460 1419
		if (adapter instanceof ColRefAdapter) {
1461 1420
			ColRefAdapter tra = (ColRefAdapter) adapter;
1462 1421
			SimpleNode s = tra.getEntity();
......
1522 1481
	 *            opening mode
1523 1482
	 *
1524 1483
	 * @return DataSource
1525
	 *
1526
	 * @throws DriverException
1527
	 *             Si fallo la lectura de la fuente de datos por parte del
1528
	 *             driver
1529
	 * @throws IOException
1530
	 *             Si se produce un error de entrada/salida
1531 1484
	 * @throws SemanticException
1532 1485
	 *             Si la instrucci?n tiene errores sem?nticos
1533 1486
	 * @throws EvaluationException
......
1535 1488
	 * @throws ParseException
1536 1489
	 *             If there is a select statement embeeded in the union
1537 1490
	 *             statement and its parse fails
1491
	 * @throws ReadDriverException TODO
1538 1492
	 */
1539 1493
	public DataSource createRandomDataSource(UnionAdapter instr, int mode)
1540
			throws DriverException, IOException, SemanticException,
1541
			EvaluationException, ParseException {
1494
			throws SemanticException,
1495
			EvaluationException, ParseException, ReadDriverException {
1542 1496
		return getModedDataSource(getDataSource(instr), mode);
1543 1497
	}
1544 1498

  
......
1549 1503
	 *            instrucci?n de union
1550 1504
	 *
1551 1505
	 * @return DataSource
1552
	 *
1553
	 * @throws DriverException
1554
	 *             Si fallo la lectura de la fuente de datos por parte del
1555
	 *             driver
1556
	 * @throws IOException
1557
	 *             Si se produce un error de entrada/salida
1558 1506
	 * @throws SemanticException
1559 1507
	 *             Si la instrucci?n tiene errores sem?nticos
1560 1508
	 * @throws ParseException
......
1562 1510
	 *             statement and its parse fails
1563 1511
	 * @throws EvaluationException
1564 1512
	 *             If there's any problem during expresion evaluation
1513
	 * @throws ReadDriverException TODO
1565 1514
	 */
1566 1515
	private DataSource getDataSource(UnionAdapter instr)
1567
			throws DriverException, IOException, SemanticException,
1568
			ParseException, EvaluationException {
1516
			throws SemanticException,
1517
			ParseException, EvaluationException, ReadDriverException {
1569 1518
		try {
1570 1519
			Strategy strategy = StrategyManager.getStrategy(instr);
1571 1520

  
......
1577 1526

  
1578 1527
			return ret;
1579 1528
		} catch (DriverLoadException e) {
1580
			throw new DriverException(e);
1529
			throw new ReadDriverException("DataSourceFactory",e);
1581 1530
		}
1582 1531

  
1583 1532
	}
......
1633 1582
	 *            opening mode
1634 1583
	 *
1635 1584
	 * @return DataSource con el resultado
1636
	 *
1637 1585
	 * @throws ParseException
1638 1586
	 *             Si se produce un error de parse de la instrucci?n
1639 1587
	 * @throws DriverLoadException
1640 1588
	 *             Si no se pueden cargar los drivers
1641
	 * @throws DriverException
1642
	 *             Si se produce un error accediendo a los drivers
1643 1589
	 * @throws SemanticException
1644 1590
	 *             Si la instrucci?n tiene alg?n error sem?ntico
1645
	 * @throws IOException
1646
	 *             Si se produce un error accediendo a las estructuras de datos
1647
	 *             internas usadas para los c?lculos
1648 1591
	 * @throws EvaluationException
1649 1592
	 *             If there's an error evaluating any expression
1593
	 * @throws ReadDriverException TODO
1650 1594
	 */
1651 1595
	public DataSource executeSQL(String sql, int mode) throws ParseException,
1652
			DriverLoadException, DriverException, SemanticException,
1653
			IOException, EvaluationException {
1596
			DriverLoadException, SemanticException,
1597
			EvaluationException, ReadDriverException {
1654 1598
		ByteArrayInputStream bytes = new ByteArrayInputStream(sql.getBytes());
1655 1599
		SQLEngine parser = new SQLEngine(bytes);
1656 1600

  
......
1786 1730

  
1787 1731
	/**
1788 1732
	 * Frees all resources used during execution
1789
	 *
1790 1733
	 * @throws SQLException
1791 1734
	 *             If cannot free internal resources
1792
	 * @throws DriverException
1793
	 *             If drivers cannot free remote resources
1794
	 */	
1795
	public void finalizeThis()  throws SQLException, DriverException {
1796
		
1735
	 * @throws ReadDriverException TODO
1736
	 */
1737
	public void finalizeThis()  throws SQLException, ReadDriverException {
1738

  
1797 1739
		try {
1798 1740
			clearViews();
1799 1741
		} finally {
......
1809 1751
			st.close();
1810 1752
			c.close();
1811 1753
		}
1812
		
1754

  
1813 1755
	}
1814 1756

  
1815 1757
	/**
......
1861 1803
	 * Registra alias de nombres de los drivers
1862 1804
	 * por si ha sido necesario modificar el nombre
1863 1805
	 * de alguno, y se necesita compatibilidad
1864
	 * 
1806
	 *
1865 1807
	 */
1866 1808
	private void fillDriversNamesAliases() {
1867
		
1809

  
1868 1810
	}
1869
	
1811

  
1870 1812
	private Driver getDriver(String name) {
1871 1813
		if (this.driversNamesAliases.containsKey(name)) {
1872 1814
			name = (String)this.driversNamesAliases.get(name);
1873 1815
		}
1874 1816
		return this.dm.getDriver(name);
1875
		
1817

  
1876 1818
	}
1877 1819

  
1878 1820
	/**
......
1897 1839
			viewName = name;
1898 1840
		}
1899 1841
	}
1900
	
1842

  
1901 1843
}

Also available in: Unified diff