1 package org.apache.ojb.tools.mapping.reversedb; 2 3 17 18 import java.util.Iterator ; 19 import javax.swing.tree.TreeNode ; 20 21 26 public class DBMeta implements MetadataNodeInterface, TreeNode , org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel 27 { 28 private boolean enabled = true; 29 30 private java.sql.DatabaseMetaData dbMeta; 31 32 private java.util.HashMap hmCatalogs = new java.util.HashMap (); 34 private String catalogTerm; 35 private String catalogSeparator; 36 private boolean isCatalogAtStart; 37 private boolean supportsCatalogsInDataManipulation; 38 private boolean supportsCatalogsInIndexDefinitions; 39 private boolean supportsCatalogsInPrivilegeDefinitions; 40 private boolean supportsCatalogsInProcedureCalls; 41 private boolean supportsCatalogsInTableDefinitions; 42 private String schemaTerm; 43 44 private String catalogPattern; 45 private String schemaPattern; 46 47 private String databaseProductName = null; 48 private String databaseProductVersion = null; 49 50 51 52 public DBMeta (String pCatalogPattern, String pSchemaPattern, java.sql.DatabaseMetaData pDbMeta) throws java.sql.SQLException 53 { 54 super(); 55 this.dbMeta = pDbMeta; 56 this.catalogPattern = pCatalogPattern; 57 this.schemaPattern = pSchemaPattern; 58 System.err.println("Using " + dbMeta.getDriverName() + " " + dbMeta.getDriverVersion()); 59 } 60 61 public DBMeta (java.sql.DatabaseMetaData pDbMeta) throws java.sql.SQLException 62 { 63 this(null, null, pDbMeta); 64 } 65 66 public String getSchemaPattern() 67 { 68 return this.schemaPattern; 69 } 70 71 public boolean isEnabled() 72 { 73 return this.enabled; 74 } 75 76 public void setEnabled(boolean b) 77 { 78 this.enabled = b; 79 } 80 81 public String getDatabaseProductVersion() 82 { 83 return this.databaseProductVersion; 84 } 85 86 public String getDatabaseProductName() 87 { 88 return this.databaseProductName; 89 } 90 91 public boolean getSupportsCatalogsInIndexDefinitions() 92 { 93 return this.supportsCatalogsInIndexDefinitions; 94 } 95 96 public boolean getSupportsCatalogsInDataManipulation() 97 { 98 return this.supportsCatalogsInDataManipulation; 99 } 100 101 public boolean getSupportsCatalogsInPrivilegeDefinitions() 102 { 103 return this.supportsCatalogsInPrivilegeDefinitions; 104 } 105 106 public boolean getSupportsCatalogsInProcedureCalls() 107 { 108 return this.supportsCatalogsInProcedureCalls; 109 } 110 111 public boolean getSupportsCatalogsInTableDefinitions() 112 { 113 return this.supportsCatalogsInTableDefinitions; 114 } 115 116 public String getCatalogTerm() 117 { 118 return this.catalogTerm; 119 } 120 121 public String getSchemaTerm() 122 { 123 return this.schemaTerm; 124 } 125 126 public String getCatalogSeparator() 127 { 128 return this.catalogSeparator; 129 } 130 131 public boolean getIsCatalogAtStart() 132 { 133 return this.isCatalogAtStart; 134 } 135 136 public DBCatalog getCatalog(String catalogName) 137 { 138 return (DBCatalog)this.hmCatalogs.get(catalogName); 139 } 140 141 public void read() 142 throws java.sql.SQLException 143 { 144 this.databaseProductName = dbMeta.getDatabaseProductName (); 145 this.databaseProductVersion = dbMeta.getDatabaseProductVersion (); 146 catalogTerm = dbMeta.getCatalogTerm(); 147 catalogSeparator = dbMeta.getCatalogSeparator(); 148 isCatalogAtStart = dbMeta.isCatalogAtStart(); 149 supportsCatalogsInDataManipulation = dbMeta.supportsCatalogsInDataManipulation(); 150 supportsCatalogsInIndexDefinitions = dbMeta.supportsCatalogsInIndexDefinitions(); 151 supportsCatalogsInPrivilegeDefinitions = dbMeta.supportsCatalogsInPrivilegeDefinitions(); 152 supportsCatalogsInProcedureCalls = dbMeta.supportsCatalogsInProcedureCalls(); 153 supportsCatalogsInTableDefinitions = dbMeta.supportsCatalogsInTableDefinitions(); 154 schemaTerm = dbMeta.getSchemaTerm(); 155 156 java.sql.ResultSet rs = dbMeta.getCatalogs(); 157 int count = 0; 158 while(rs.next()) 159 { 160 count++; 161 String strCatalogName = rs.getString("TABLE_CAT"); 162 try 163 { 164 if (new org.apache.regexp.RE(this.catalogPattern).match(strCatalogName)) 165 { 166 DBCatalog aDBCatalog = new DBCatalog(dbMeta, this, strCatalogName); 167 this.hmCatalogs.put(strCatalogName, aDBCatalog); 168 } 169 } 170 catch (org.apache.regexp.RESyntaxException ex) 171 { 172 ex.printStackTrace(); 174 } 175 } 176 rs.close(); 177 if (count==0) 178 { 179 DBCatalog aDBCatalog = new DBCatalog(dbMeta, this, null); 180 this.hmCatalogs.put(null, aDBCatalog); 181 } 182 183 Iterator it = hmCatalogs.values().iterator(); 185 while (it.hasNext()) ((DBCatalog)it.next()).read(); 186 } 187 188 public void generateReferences() 189 throws java.sql.SQLException 190 { 191 Iterator it = this.hmCatalogs.values().iterator(); 192 while (it.hasNext()) 193 { 194 ((DBCatalog)it.next()).generateReferences(); 195 } 196 } 197 198 public void debug() 199 { 200 201 } 202 203 public java.util.Enumeration children () 204 { 205 return java.util.Collections.enumeration(this.hmCatalogs.values()); 206 } 207 208 public boolean getAllowsChildren () 209 { 210 return true; 211 } 212 213 public TreeNode getChildAt(int param) 214 { 215 return (TreeNode )this.hmCatalogs.values().toArray()[param]; 216 } 217 218 public int getChildCount () 219 { 220 return this.hmCatalogs.size(); 221 } 222 223 public int getIndex(TreeNode treeNode) 224 { 225 return new java.util.Vector (hmCatalogs.values()).indexOf(treeNode); 226 } 227 228 public TreeNode getParent() 229 { 230 return null; 231 } 232 233 public boolean isLeaf () 234 { 235 return false; 236 } 237 238 public String toString() 239 { 240 return "DBMeta"; 241 } 242 243 public Class getPropertySheetClass () 244 { 245 return org.apache.ojb.tools.mapping.reversedb.gui.DBMetaPropertySheet.class; 246 } 247 248 public String getXML() 249 { 250 java.io.StringWriter sw = new java.io.StringWriter (); 251 writeXML(new java.io.PrintWriter (sw)); 252 return sw.getBuffer().toString(); 253 } 254 255 public void writeXML(java.io.PrintWriter pw) 256 { 257 258 259 pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 260 pw.println("<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\">" ); 261 pw.println("<descriptor-repository version=\"0.9.9\">" ); 262 pw.println(" <jdbc-connection-descriptor" ); 263 pw.println(" jcd-alias=\"default\"" ); 264 pw.println(" default-connection=\"true\"" ); 265 pw.println(" platform=\"XXXX\"" ); 266 pw.println(" jdbc-level=\"1.0\""); 267 pw.println(" driver=\"XXX\"" ); 268 pw.println(" protocol=\"XXX\"" ); 269 pw.println(" subprotocol=\"XXX\""); 270 pw.println(" dbalias=\"XXX\"" ); 271 pw.println(" username=\"XXX\""); 272 pw.println(" password=\"XXX\">"); 273 pw.println(" </jdbc-connection-descriptor>"); 274 275 Iterator i = this.hmCatalogs.values().iterator(); 276 while (i.hasNext()) 277 { 278 ((DBCatalog)i.next()).writeXML(pw); 279 } 280 pw.println("</descriptor-repository>"); 281 } 282 283 public void generateJava (java.io.File aFile, String strHeader, String strFooter) throws java.io.IOException , java.io.FileNotFoundException 284 { 285 Iterator it = this.hmCatalogs.values().iterator(); 286 while (it.hasNext()) ((DBCatalog)it.next()).generateJava(aFile, strHeader, strFooter); 287 } 288 289 public void setPackage (String packageName) 290 { 291 Iterator it = this.hmCatalogs.values().iterator(); 292 while (it.hasNext()) ((DBCatalog)it.next()).setPackage(packageName); 293 } 294 295 public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) 296 { 297 Iterator it = this.hmCatalogs.values().iterator(); 298 while (it.hasNext()) ((DBCatalog)it.next()).disableClassesWithRegex(aRegexp); 299 } 300 301 302 } 303 304 305 371 | Popular Tags |