Revision 10272 branches/v10/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/DefaultDBDriver.java
DefaultDBDriver.java | ||
---|---|---|
51 | 51 |
import java.sql.SQLException; |
52 | 52 |
import java.sql.Statement; |
53 | 53 |
import java.sql.Types; |
54 |
import java.util.ArrayList; |
|
55 | 54 |
import java.util.Hashtable; |
56 |
import java.util.TreeMap; |
|
57 | 55 |
|
58 | 56 |
import org.apache.log4j.Logger; |
59 | 57 |
|
... | ... | |
63 | 61 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
64 | 62 |
import com.iver.cit.gvsig.fmap.DriverException; |
65 | 63 |
import com.iver.cit.gvsig.fmap.DriverJdbcNotFoundExceptionType; |
66 |
import com.iver.cit.gvsig.fmap.MapContext; |
|
67 | 64 |
import com.iver.cit.gvsig.fmap.Messages; |
68 | 65 |
import com.iver.cit.gvsig.fmap.SqlDriveExceptionType; |
69 | 66 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
70 | 67 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
71 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.ConnectionWithParams; |
|
72 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.SingleJDBCConnectionManager; |
|
73 | 68 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
74 | 69 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
75 | 70 |
import com.iver.utiles.XMLEntity; |
... | ... | |
82 | 77 |
*/ |
83 | 78 |
public abstract class DefaultDBDriver implements VectorialJDBCDriver, ObjectDriver { |
84 | 79 |
private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName()); |
85 |
protected static Hashtable poolPassw = new Hashtable();
|
|
80 |
private static Hashtable poolPassw = new Hashtable();
|
|
86 | 81 |
|
87 | 82 |
protected Connection conn; |
88 | 83 |
// protected String tableName; |
89 | 84 |
// protected String whereClause; |
90 | 85 |
// protected String fields; |
91 | 86 |
// protected String sqlOrig; |
92 |
protected DBLayerDefinition lyrDef = null;
|
|
87 |
private DBLayerDefinition lyrDef = null;
|
|
93 | 88 |
protected ResultSet rs; |
94 | 89 |
protected boolean bCursorActivo = false; |
95 | 90 |
protected Statement st; |
96 | 91 |
protected int numReg=-1; |
97 | 92 |
|
98 |
protected Rectangle2D fullExtent = null;
|
|
93 |
private Rectangle2D fullExtent = null;
|
|
99 | 94 |
|
100 | 95 |
// protected String strFID_FieldName; |
101 | 96 |
// protected String idFID_FieldName; |
... | ... | |
105 | 100 |
|
106 | 101 |
protected ResultSetMetaData metaData = null; |
107 | 102 |
protected Rectangle2D workingArea; |
108 |
protected String driverClass;
|
|
109 |
protected String userName;
|
|
110 |
protected String dbUrl;
|
|
111 |
protected String className;
|
|
112 |
protected String catalogName;
|
|
113 |
protected String tableName;
|
|
114 |
protected String[] fields;
|
|
115 |
protected String FIDfield;
|
|
116 |
protected String geometryField;
|
|
117 |
protected String whereClause;
|
|
118 |
protected String strSRID;
|
|
103 |
private String driverClass;
|
|
104 |
private String userName;
|
|
105 |
private String dbUrl;
|
|
106 |
private String className;
|
|
107 |
private String catalogName;
|
|
108 |
private String tableName;
|
|
109 |
private String[] fields;
|
|
110 |
private String FIDfield;
|
|
111 |
private String geometryField;
|
|
112 |
private String whereClause;
|
|
113 |
private String strSRID;
|
|
119 | 114 |
//private double flatness; |
120 |
|
|
121 |
protected String host, port, dbName, connName; |
|
122 |
|
|
123 | 115 |
|
124 | 116 |
abstract public void setData(Connection conn, DBLayerDefinition lyrDef); |
125 | 117 |
|
... | ... | |
297 | 289 |
} catch (SQLException e) { |
298 | 290 |
throw new com.hardcode.gdbms.engine.data.driver.DriverException(e); |
299 | 291 |
} |
300 |
return Types.OTHER; |
|
301 |
// throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: " + str); |
|
292 |
throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: " + str); |
|
302 | 293 |
} |
303 | 294 |
/** |
304 | 295 |
* Obtiene el valor que se encuentra en la fila y columna indicada |
... | ... | |
405 | 396 |
return -1; |
406 | 397 |
} |
407 | 398 |
|
408 |
public void close() { |
|
399 |
public void close() |
|
400 |
{ |
|
409 | 401 |
} |
410 | 402 |
|
411 | 403 |
/** |
... | ... | |
469 | 461 |
{ |
470 | 462 |
|
471 | 463 |
className = xml.getStringProperty("className"); |
472 |
|
|
464 |
dbUrl = xml.getStringProperty("dbURL"); |
|
473 | 465 |
catalogName = xml.getStringProperty("catalog"); |
474 | 466 |
userName =xml.getStringProperty("username"); |
475 | 467 |
driverClass =xml.getStringProperty("driverclass"); |
... | ... | |
479 | 471 |
geometryField = xml.getStringProperty("THE_GEOM"); |
480 | 472 |
whereClause = xml.getStringProperty("whereclause"); |
481 | 473 |
strSRID = xml.getStringProperty("SRID"); |
482 |
|
|
483 |
if (xml.contains("host")) |
|
484 |
{ |
|
485 |
host = xml.getStringProperty("host"); |
|
486 |
port = xml.getStringProperty("port"); |
|
487 |
dbName = xml.getStringProperty("dbName"); |
|
488 |
connName = xml.getStringProperty("connName"); |
|
489 |
} |
|
490 |
else |
|
491 |
{ |
|
492 |
// Por compatibilidad con versiones anteriores |
|
493 |
dbUrl = xml.getStringProperty("dbURL"); |
|
494 |
extractParamsFromDbUrl(dbUrl); |
|
495 |
|
|
496 |
} |
|
497 | 474 |
if (xml.contains("minXworkArea")) |
498 | 475 |
{ |
499 | 476 |
double x = xml.getDoubleProperty("minXworkArea"); |
... | ... | |
520 | 497 |
|
521 | 498 |
} |
522 | 499 |
|
523 |
private void extractParamsFromDbUrl(String dbUrl2) { |
|
524 |
//jdbc:postgres://localhost:5431/latin1 |
|
525 |
int iDbName = dbUrl2.lastIndexOf('/'); |
|
526 |
dbName = dbUrl2.substring(iDbName+1); |
|
527 |
int iLast2points = dbUrl2.lastIndexOf(':'); |
|
528 |
port = dbUrl2.substring(iLast2points+1, iDbName); |
|
529 |
int iHost = dbUrl2.indexOf("//"); |
|
530 |
host = dbUrl2.substring(iHost + 2, iLast2points); |
|
531 |
connName = dbUrl2; |
|
532 |
} |
|
533 |
|
|
534 |
public void load() throws DriverException { |
|
500 |
public void load() throws DriverException{ |
|
535 | 501 |
try { |
536 | 502 |
Class.forName(driverClass); |
537 |
|
|
538 |
String _drvName = getName(); |
|
539 | 503 |
|
540 |
String keyPool = _drvName.toLowerCase() + "_" + host.toLowerCase() |
|
541 |
+ "_" + port + "_" + dbName.toLowerCase() |
|
542 |
+ "_" + userName.toLowerCase(); |
|
543 |
|
|
504 |
String keyPool = dbUrl + "_" + userName; |
|
544 | 505 |
Connection newConn = null; |
545 | 506 |
String clave = null; |
546 |
ConnectionWithParams cwp = null; |
|
547 |
|
|
548 |
if (poolPassw.containsKey(keyPool)) { |
|
549 |
|
|
507 |
if (!poolPassw.containsKey(keyPool)) |
|
508 |
{ |
|
509 |
JPasswordDlg dlg = new JPasswordDlg(); |
|
510 |
String strMessage = Messages.getString("conectar_jdbc"); |
|
511 |
String strPassword = Messages.getString("password"); |
|
512 |
dlg.setMessage(strMessage + " " + dbUrl + ". " + strPassword + "?"); |
|
513 |
dlg.show(); |
|
514 |
clave = dlg.getPassword(); |
|
515 |
if (clave == null) |
|
516 |
return; |
|
517 |
poolPassw.put(keyPool, clave); |
|
518 |
} |
|
519 |
else |
|
520 |
{ |
|
550 | 521 |
clave = (String) poolPassw.get(keyPool); |
551 |
cwp = SingleJDBCConnectionManager.instance().getConnection( |
|
552 |
_drvName, userName, clave, connName, |
|
553 |
host, port, dbName, true); |
|
554 |
|
|
555 |
} else { |
|
556 |
|
|
557 |
cwp = SingleJDBCConnectionManager.instance().getConnection( |
|
558 |
_drvName, userName, null, connName, |
|
559 |
host, port, dbName, false); |
|
560 |
|
|
561 |
if (cwp.isConnected()) { |
|
562 |
|
|
563 |
poolPassw.put(keyPool, cwp.getPw()); |
|
564 |
|
|
565 |
} else { |
|
566 |
|
|
567 |
JPasswordDlg dlg = new JPasswordDlg(); |
|
568 |
String strMessage = Messages.getString("conectar_jdbc"); |
|
569 |
String strPassword = Messages.getString("password"); |
|
570 |
dlg.setMessage(strMessage |
|
571 |
+ " [" |
|
572 |
+ _drvName + ", " |
|
573 |
+ host + ", " |
|
574 |
+ port + ", " |
|
575 |
+ dbName + ", " |
|
576 |
+ userName + "]. " |
|
577 |
+ strPassword |
|
578 |
+ "?"); |
|
579 |
dlg.show(); |
|
580 |
clave = dlg.getPassword(); |
|
581 |
if (clave == null) |
|
582 |
return; |
|
583 |
poolPassw.put(keyPool, clave); |
|
584 |
|
|
585 |
cwp.connect(clave); |
|
586 |
} |
|
587 | 522 |
} |
588 |
|
|
589 |
newConn = cwp.getConnection(); |
|
523 |
newConn = DriverManager.getConnection(dbUrl, userName, clave); |
|
590 | 524 |
newConn.setAutoCommit(false); |
591 | 525 |
|
592 | 526 |
DBLayerDefinition lyrDef = new DBLayerDefinition(); |
... | ... | |
606 | 540 |
lyrDef = getLyrDef(); |
607 | 541 |
} |
608 | 542 |
|
543 |
|
|
544 |
|
|
609 | 545 |
setData(newConn, lyrDef); |
610 | 546 |
} catch (ClassNotFoundException e) { |
611 | 547 |
logger.debug(e); |
... | ... | |
615 | 551 |
type.setLayerName(this.getTableName()); |
616 | 552 |
throw new DriverException("Driver JDBC no encontrado", e, type); |
617 | 553 |
} catch (SQLException e) { |
618 |
|
|
619 |
throw new DriverException(e.getMessage()); |
|
620 |
|
|
621 |
} |
|
554 |
logger.debug(e); |
|
555 |
SqlDriveExceptionType type = |
|
556 |
new SqlDriveExceptionType(); |
|
557 |
type.setSql("No disponible"); |
|
558 |
throw new DriverException(e, type); |
|
559 |
} |
|
622 | 560 |
} |
623 | 561 |
/* (non-Javadoc) |
624 | 562 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getXMLEntity() |
... | ... | |
629 | 567 |
xml.putProperty("className",this.getClass().getName()); |
630 | 568 |
try { |
631 | 569 |
DatabaseMetaData metadata = getConnection().getMetaData(); |
570 |
xml.putProperty("dbURL", metadata.getURL()); |
|
632 | 571 |
xml.putProperty("catalog", getLyrDef().getCatalogName()); |
633 |
|
|
634 | 572 |
// TODO: NO DEBEMOS GUARDAR EL NOMBRE DE USUARIO Y CONTRASE?A |
635 | 573 |
// AQUI. Hay que utilizar un pool de conexiones |
636 | 574 |
// y pedir al usuario que conecte a la base de datos |
... | ... | |
652 | 590 |
xml.putProperty("THE_GEOM", lyrDef.getFieldGeometry()); |
653 | 591 |
xml.putProperty("whereclause", getWhereClause()); |
654 | 592 |
xml.putProperty("SRID", lyrDef.getSRID_EPSG()); |
655 |
|
|
656 |
ConnectionWithParams cwp = |
|
657 |
SingleJDBCConnectionManager.instance().findConnection(getConnection()); |
|
658 |
|
|
659 |
xml.putProperty("host", cwp.getHost()); |
|
660 |
xml.putProperty("port", cwp.getPort()); |
|
661 |
xml.putProperty("dbName", cwp.getDb()); |
|
662 |
xml.putProperty("connName", cwp.getName()); |
|
663 |
|
|
664 | 593 |
if (getWorkingArea() != null) |
665 | 594 |
{ |
666 | 595 |
xml.putProperty("minXworkArea", getWorkingArea().getMinX()); |
... | ... | |
789 | 718 |
} |
790 | 719 |
|
791 | 720 |
/** |
792 |
* Gets available table names. Should be overwritten by subclasses if its |
|
793 |
* not compatible or if it can be refined |
|
794 |
* |
|
795 |
* @param conn connection object |
|
796 |
* @param catalog catalog name |
|
797 |
* @return array of strings with available table names |
|
798 |
* @throws SQLException |
|
799 |
*/ |
|
800 |
public String[] getTableNames(Connection conn, String catalog) throws SQLException { |
|
801 |
|
|
802 |
DatabaseMetaData dbmd = conn.getMetaData(); |
|
803 |
String[] types = {"TABLE", "VIEW"}; |
|
804 |
ResultSet rs = dbmd.getTables(catalog, null, null, types); |
|
805 |
TreeMap ret = new TreeMap(); |
|
806 |
while (rs.next()){ |
|
807 |
ret.put(rs.getString("TABLE_NAME"), rs.getString("TABLE_NAME")); |
|
808 |
} |
|
809 |
rs.close(); |
|
810 |
return (String[]) ret.keySet().toArray(new String[0]); |
|
811 |
} |
|
812 |
|
|
813 |
|
|
814 |
/** |
|
815 |
* Gets all field names of a given table |
|
816 |
* @param conn connection object |
|
817 |
* @param table_name table name |
|
818 |
* @return all field names of the given table |
|
819 |
* @throws SQLException |
|
820 |
*/ |
|
821 |
public String[] getAllFields(Connection conn, String table_name) throws SQLException { |
|
822 |
|
|
823 |
Statement st = conn.createStatement(); |
|
824 |
ResultSet rs = st.executeQuery("select * from " + table_name + " LIMIT 1"); |
|
825 |
ResultSetMetaData rsmd = rs.getMetaData(); |
|
826 |
String[] ret = new String[rsmd.getColumnCount()]; |
|
827 |
|
|
828 |
for (int i = 0; i < ret.length; i++) { |
|
829 |
ret[i] = rsmd.getColumnName(i+1); |
|
830 |
} |
|
831 |
rs.close(); st.close(); |
|
832 |
return ret; |
|
833 |
} |
|
834 |
|
|
835 |
/** |
|
836 |
* Gets all field type names of a given table |
|
837 |
* @param conn connection object |
|
838 |
* @param table_name table name |
|
839 |
* @return all field type names of the given table |
|
840 |
* @throws SQLException |
|
841 |
*/ |
|
842 |
public String[] getAllFieldTypeNames(Connection conn, String table_name) throws SQLException { |
|
843 |
|
|
844 |
Statement st = conn.createStatement(); |
|
845 |
ResultSet rs = st.executeQuery("select * from " + table_name + " LIMIT 1"); |
|
846 |
ResultSetMetaData rsmd = rs.getMetaData(); |
|
847 |
String[] ret = new String[rsmd.getColumnCount()]; |
|
848 |
|
|
849 |
for (int i = 0; i < ret.length; i++) { |
|
850 |
ret[i] = rsmd.getColumnTypeName(i+1); |
|
851 |
} |
|
852 |
rs.close(); st.close(); |
|
853 |
return ret; |
|
854 |
} |
|
855 |
|
|
856 |
/** |
|
857 |
* Gets the table's possible id fields. By default, all fields can be id. |
|
858 |
* It should be overwritten by subclasses. |
|
859 |
* |
|
860 |
* @param conn conenction object |
|
861 |
* @param table_name table name |
|
862 |
* @return the table's possible id fields |
|
863 |
* @throws SQLException |
|
864 |
*/ |
|
865 |
public String[] getIdFieldsCandidates(Connection conn, String table_name) throws SQLException { |
|
866 |
return getAllFields(conn, table_name); |
|
867 |
} |
|
868 |
|
|
869 |
/** |
|
870 |
* Gets the table's possible geometry fields. By default, all fields can be geometry |
|
871 |
* fields. It should be overwritten by subclasses. |
|
872 |
* |
|
873 |
* @param conn conenction object |
|
874 |
* @param table_name table name |
|
875 |
* @return the table's possible geometry fields |
|
876 |
* @throws SQLException |
|
877 |
*/ |
|
878 |
public String[] getGeometryFieldsCandidates(Connection conn, String table_name) throws SQLException { |
|
879 |
return getAllFields(conn, table_name); |
|
880 |
} |
|
881 |
|
|
882 |
/** |
|
883 |
* Tells if it's an empty table (with no records) |
|
884 |
* @param conn conenction object |
|
885 |
* @param tableName rtable name |
|
886 |
* @return whether it's an empty table (with no records) or not |
|
887 |
*/ |
|
888 |
public boolean isEmptyTable(Connection conn, String tableName) { |
|
889 |
|
|
890 |
boolean res = true; |
|
891 |
|
|
892 |
try { |
|
893 |
Statement st = conn.createStatement(); |
|
894 |
ResultSet rs = null; |
|
895 |
rs = st.executeQuery("select * from " + tableName + " LIMIT 1"); |
|
896 |
res = !rs.next(); |
|
897 |
rs.close(); st.close(); |
|
898 |
} catch (Exception ex) { |
|
899 |
res = true; |
|
900 |
} |
|
901 |
return res; |
|
902 |
} |
|
903 |
|
|
904 |
|
|
905 |
|
|
906 |
/** |
|
907 |
* Utility method to allow the driver to do something with the geometry field. |
|
908 |
* By default does nothing. |
|
909 |
* |
|
910 |
* @param flds user-selected fields |
|
911 |
* @param geomField geometry field |
|
912 |
* @return new user-selected fields |
|
913 |
*/ |
|
914 |
public String[] manageGeometryField(String[] flds, String geomField) { |
|
915 |
return flds; |
|
916 |
} |
|
917 |
|
|
918 |
/** |
|
919 | 721 |
* Empty method called when the layer is going to be removed from the view. |
920 | 722 |
* Subclasses can overwrite it if needed. |
921 | 723 |
* |
... | ... | |
923 | 725 |
public void remove() { |
924 | 726 |
|
925 | 727 |
} |
926 |
|
|
927 | 728 |
|
928 |
|
|
729 |
|
|
929 | 730 |
} |
Also available in: Unified diff