1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion.db; 21 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.util.LinkedList ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 import org.netbeans.modules.dbschema.DBException; 28 import org.netbeans.modules.dbschema.DBIdentifier; 29 import org.netbeans.modules.dbschema.SchemaElement; 30 import org.netbeans.modules.dbschema.TableElement; 31 import org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider; 32 import org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl; 33 import org.openide.ErrorManager; 34 35 39 public class Schema { 40 41 private DBMetaDataProvider provider; 42 private Catalog catalog; 43 private String name; 44 private Set tableNames; 45 46 private ConnectionProvider cp; 47 private SchemaElementImpl schemaElementImpl; 48 private SchemaElement schemaElement; 49 50 52 Schema(DBMetaDataProvider provider, Catalog catalog, String name) { 53 this.provider = provider; 54 this.catalog = catalog; 55 this.name = name; 56 } 57 58 public String getName() { 59 return name; 60 } 61 62 public synchronized String [] getTableNames() throws SQLException { 63 if (tableNames == null) { 64 tableNames = getTableNamesByType("TABLE"); } 66 67 return (String [])tableNames.toArray(new String [tableNames.size()]); 68 } 69 70 public TableElement getTable(String tableName) throws SQLException { 71 SchemaElement schemaElement; 72 synchronized (this) { 73 schemaElement = this.schemaElement; 74 } 75 if (schemaElement == null) { 76 cp = new ConnectionProvider(provider.getConnection(), provider.getDriverClass()); 77 cp.setSchema(name); 78 schemaElementImpl = new SchemaElementImpl(cp); 79 try { 80 schemaElementImpl.setName(DBIdentifier.create("foo")); } catch (DBException e) { 82 ErrorManager.getDefault().notify(e); 83 } 84 schemaElement = new SchemaElement(schemaElementImpl); 85 synchronized (this) { 86 this.schemaElement = schemaElement; 87 } 88 } 89 90 DBIdentifier tableId = DBIdentifier.create(tableName); 91 TableElement tableElement = schemaElement.getTable(tableId); 92 if (tableElement == null) { 93 LinkedList tableList = new LinkedList (); 94 tableList.add(tableName); 95 LinkedList viewList = new LinkedList (); 96 schemaElementImpl.initTables(cp, tableList, viewList, false); 97 98 tableElement = schemaElement.getTable(tableId); 99 } 100 101 return tableElement; 102 } 103 104 public synchronized void refresh() { 105 schemaElement = null; 106 tableNames = null; 107 } 108 109 private Set getTableNamesByType(String type) throws SQLException { 110 Set result = new TreeSet (); 111 112 ResultSet rs = provider.getMetaData().getTables(catalog.getName(), name, "%", new String [] { type }); try { 114 while (rs.next()) { 115 String tableName = rs.getString("TABLE_NAME"); result.add(tableName); 117 } 118 } finally { 119 rs.close(); 120 } 121 122 return result; 123 } 124 125 public String toString() { 126 return "Schema[catalog=" + catalog + ",name='" + name + "']"; } 128 } 129 | Popular Tags |