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 27 public class DBSchema implements MetadataNodeInterface, TreeNode , org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel 28 { 29 private java.sql.DatabaseMetaData dbMeta; 30 private DBCatalog aDBCatalog; 31 32 private boolean enabled = true; 33 34 private java.util.TreeMap tmTables = new java.util.TreeMap (); 35 36 private String m_strSchemaName; 37 38 39 40 public DBSchema( java.sql.DatabaseMetaData pdbMeta, DBCatalog paDBCatalog, 41 String pstrSchemaName) 42 { 43 aDBCatalog = paDBCatalog; 44 dbMeta = pdbMeta; 45 m_strSchemaName = pstrSchemaName; 46 } 47 48 public boolean isEnabled() 49 { 50 return this.enabled; 51 } 52 53 public void setEnabled(boolean b) 54 { 55 this.enabled = b; 56 } 57 58 59 public DBCatalog getDBCatalog() 60 { 61 return this.aDBCatalog; 62 } 63 64 public String getSchemaName() 65 { 66 return this.m_strSchemaName; 67 } 68 69 public boolean isTreeEnabled() 70 { 71 return this.getDBCatalog().isTreeEnabled() && this.isEnabled(); 72 } 73 74 75 public void read() 76 throws java.sql.SQLException 77 { 78 java.sql.ResultSet rs = dbMeta.getTables(this.getDBCatalog().getCatalogName(), this.getSchemaName(), "%", null); 79 while (rs.next()) 80 { 81 String strTableCat = rs.getString("TABLE_CAT"); 82 String strSchemaName = rs.getString("TABLE_SCHEM"); 83 String strTableName = rs.getString("TABLE_NAME"); 84 String strTableType = rs.getString("TABLE_TYPE"); 85 if ( 88 (strTableCat!=null && strTableCat.equalsIgnoreCase(this.getDBCatalog().getCatalogName()) || strTableCat==this.getDBCatalog().getCatalogName()) 89 && 90 (strSchemaName!=null && strSchemaName.equals(this.getSchemaName()) || strSchemaName==this.getSchemaName()) 91 ) 92 this.addTable(strTableName, strTableType); 93 94 } 95 rs.close(); 96 97 rs = dbMeta.getColumns(this.getDBCatalog().getCatalogName(), this.getSchemaName(), "%", "%"); 98 while (rs.next()) 99 { 100 String strSchemaName = rs.getString("TABLE_SCHEM"); 101 String strTableName = rs.getString("TABLE_NAME"); 102 String strColumnName = rs.getString("COLUMN_NAME"); 103 int iDataType = rs.getInt("DATA_TYPE"); 104 String strTypeName = rs.getString("TYPE_NAME"); 105 int iColumnSize = rs.getInt("COLUMN_SIZE"); 106 int iNullable = rs.getInt("NULLABLE"); 107 this.addColumn(strTableName, strColumnName, 108 iDataType, strTypeName, iColumnSize, iNullable); 109 } 110 rs.close(); 111 } 112 113 public void addTable(String strTableName, String strTableType) 114 throws java.sql.SQLException 115 { 116 DBTable aDBTable = new DBTable(dbMeta, this, strTableName); 117 this.tmTables.put(strTableName, aDBTable); 118 aDBTable.read(); 119 } 120 121 public void addColumn(String strTableName, String strColumnName, 122 int iDataType, String strTypeName, int iColumnSize, int iNullable) 123 { 124 DBTable aDBTable= this.getTable(strTableName); 125 if (aDBTable != null) 126 { 127 aDBTable.addColumn( strColumnName, 128 iDataType, strTypeName, iColumnSize, iNullable); 129 } 130 } 131 132 public void addPrimaryKeyColumn(String strTableName, String strColumnName) 133 { 134 DBTable aDBTable = this.getTable(strTableName); 135 if (aDBTable != null) 136 { 137 aDBTable.addPrimaryKeyColumn(strColumnName); 138 } 139 } 140 141 142 public DBTable getTable(String strTableName) 143 { 144 return (DBTable)tmTables.get(strTableName); 145 } 146 147 public void generateReferences() 148 throws java.sql.SQLException 149 { 150 Iterator it = tmTables.values().iterator(); 151 while (it.hasNext()) 152 { 153 ((DBTable)it.next()).generateReferences(); 154 } 155 } 156 157 public java.util.Enumeration children () 158 { 159 return java.util.Collections.enumeration(this.tmTables.values()); 160 } 161 162 public boolean getAllowsChildren () 163 { 164 return true; 165 } 166 167 public TreeNode getChildAt(int param) 168 { 169 TreeNode tn = (TreeNode )tmTables.values().toArray()[param]; 170 return tn; 171 } 172 173 public int getChildCount () 174 { 175 return this.tmTables.size(); 176 } 177 178 public int getIndex(TreeNode treeNode) 179 { 180 int indexOf = new java.util.Vector (tmTables.values()).indexOf(treeNode); 181 return indexOf; 182 } 183 184 public TreeNode getParent() 185 { 186 return this.aDBCatalog; 187 } 188 189 public boolean isLeaf () 190 { 191 return false; 192 } 193 194 public String toString() 195 { 196 if (m_strSchemaName == null || m_strSchemaName.trim().length() == 0) 197 return "<empty schema>"; 198 else return this.m_strSchemaName; 199 } 200 201 public Class getPropertySheetClass () 202 { 203 return org.apache.ojb.tools.mapping.reversedb.gui.DBSchemaPropertySheet.class; 204 } 205 206 public String getXML() 207 { 208 java.io.StringWriter swr = new java.io.StringWriter (); 209 writeXML(new java.io.PrintWriter (swr)); 210 return swr.getBuffer().toString(); 211 } 212 213 public void writeXML(java.io.PrintWriter pw) 214 { 215 Iterator i = this.tmTables.values().iterator(); 216 while (i.hasNext()) 217 { 218 ((DBTable)i.next()).writeXML(pw); 219 } 220 } 221 222 public void generateJava (java.io.File aFile, String strHeader, String strFooter) throws java.io.IOException , java.io.FileNotFoundException 223 { 224 Iterator i = this.tmTables.values().iterator(); 225 while (i.hasNext()) ((DBTable)i.next()).generateJava(aFile, strHeader, strFooter); 226 } 227 228 public void setPackage (String packageName) 229 { 230 Iterator i = this.tmTables.values().iterator(); 231 while (i.hasNext()) ((DBTable)i.next()).setPackage(packageName); 232 } 233 234 public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) 235 { 236 Iterator it = this.tmTables.values().iterator(); 237 while (it.hasNext()) ((DBTable)it.next()).disableClassesWithRegex(aRegexp); 238 } 239 240 241 } 242 243 324 | Popular Tags |