1 19 20 package org.netbeans.modules.db.explorer.infos; 21 22 import java.sql.DatabaseMetaData ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Vector ; 32 import org.netbeans.api.db.explorer.DatabaseException; 33 import org.netbeans.lib.ddl.impl.DriverSpecification; 34 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 35 import org.openide.ErrorManager; 36 37 public class TableListNodeInfo extends DatabaseNodeInfo implements TableOwnerOperations { 38 static final long serialVersionUID =-6156362126513404875L; 39 40 protected void initChildren(Vector children) throws DatabaseException { 41 try { 42 String [] types = new String [] {"TABLE"}; List recycleBinTables; 44 45 DriverSpecification drvSpec = getDriverSpecification(); 46 47 DatabaseMetaData dmd = drvSpec.getMetaData(); 49 if ("Oracle".equals(dmd.getDatabaseProductName()) && dmd.getDatabaseMajorVersion() >= 10) { recycleBinTables = getOracleRecycleBinTables(dmd); 51 } else { 52 recycleBinTables = Collections.EMPTY_LIST; 53 } 54 55 drvSpec.getTables("%", types); 56 ResultSet rs = drvSpec.getResultSet(); 57 if (rs != null) { 58 HashMap rset = new HashMap (); 59 DatabaseNodeInfo info; 60 while (rs.next()) { 61 rset = drvSpec.getRow(); 62 info = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.TABLE, rset); 63 if (info != null) { 64 if (!recycleBinTables.contains(info.getName())) { 65 info.put(DatabaseNode.TABLE, info.getName()); 66 children.add(info); 67 } 68 } else 69 throw new Exception (bundle().getString("EXC_UnableToCreateNodeInformationForTable")); rset.clear(); 71 } 72 rs.close(); 73 } 74 } catch (Exception e) { 75 DatabaseException dbe = new DatabaseException(e.getMessage()); 76 dbe.initCause(e); 77 throw dbe; 78 } 79 } 80 81 84 public void addTable(String tname) throws DatabaseException { 85 try { 86 String [] types = new String [] {"TABLE", "BASE"}; 88 DriverSpecification drvSpec = getDriverSpecification(); 89 drvSpec.getTables(tname, types); 90 ResultSet rs = drvSpec.getResultSet(); 91 if (rs != null) { 92 HashMap rset = new HashMap (); 93 rs.next(); 94 rset = drvSpec.getRow(); 95 if (rset == null) 96 throw new NullPointerException (); 97 DatabaseNodeInfo info = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.TABLE, rset); 98 rset.clear(); 99 rs.close(); 100 101 if (info != null) 102 info.put(DatabaseNode.TABLE, info.getName()); 103 else 104 throw new Exception (bundle().getString("EXC_UnableToCreateNodeInformationForTable")); refreshChildren(); 107 } 108 } catch (Exception e) { 109 DatabaseException dbe = new DatabaseException(e.getMessage()); 110 dbe.initCause(e); 111 throw dbe; 112 } 113 } 114 115 118 public TableNodeInfo getChildrenTableInfo(TableNodeInfo info) { 119 String scode = info.getCode(); 120 String sname = info.getName(); 121 122 try { 123 Enumeration enu = getChildren().elements(); 124 while (enu.hasMoreElements()) { 125 TableNodeInfo elem = (TableNodeInfo)enu.nextElement(); 126 if (elem.getCode().equals(scode) && elem.getName().equals(sname)) 127 return elem; 128 } 129 } catch (Exception e) { 130 } 132 133 return null; 134 } 135 136 private List getOracleRecycleBinTables(DatabaseMetaData dmd) { 137 List result = new ArrayList (); 138 try { 139 Statement stmt = dmd.getConnection().createStatement(); 140 try { 141 ResultSet rs = stmt.executeQuery("SELECT OBJECT_NAME FROM RECYCLEBIN WHERE TYPE = 'TABLE'"); try { 143 while (rs.next()) { 144 result.add(rs.getString("OBJECT_NAME")); } 146 } finally { 147 rs.close(); 148 } 149 } finally { 150 stmt.close(); 151 } 152 } catch (SQLException e) { 153 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 155 result = Collections.EMPTY_LIST; 156 } 157 return result; 158 } 159 160 175 } 176 | Popular Tags |