📄 database.java
字号:
* Return an array of all tables * * @return array of all tables */ public Table[] getTables() { int size = tableList.size(); Table[] tbls = new Table[size]; for (int i = 0; i < size; i++) { tbls[i] = (Table) tableList.get(i); } return tbls; } /** * Return the table with the specified name. * * @param name table name * @return A Table object. If it does not exist it returns null */ public Table getTable(String name) { return (Table) tablesByName.get(name); } /** * Return the table with the specified javaName. * * @param javaName name of the java object representing the table * @return A Table object. If it does not exist it returns null */ public Table getTableByJavaName(String javaName) { return (Table) tablesByJavaName.get(javaName); } /** * An utility method to add a new table from an xml attribute. * * @param attrib the xml attributes * @return the created Table */ public Table addTable(Attributes attrib) { Table tbl = new Table(); tbl.setDatabase(this); tbl.loadFromXML(attrib, this.getDefaultIdMethod()); addTable(tbl); return tbl; } /** * Add a table to the list and sets the Database property to this Database * * @param tbl the table to add */ public void addTable(Table tbl) { tbl.setDatabase(this); tableList.add(tbl); tablesByName.put(tbl.getName(), tbl); tablesByJavaName.put(tbl.getJavaName(), tbl); tbl.setPackage(getPackage()); } /** * Set the parent of the database * * @param parent the parent */ public void setAppData(AppData parent) { dbParent = parent; } /** * Get the parent of the table * * @return the parent */ public AppData getAppData() { return dbParent; } protected String getDatabaseType() { return databaseType; } public void setDatabaseType(String databaseType) { this.databaseType = databaseType; } /** * Returns the value of the named property from this database's * <code>db.props</code> file. * * @param name The name of the property to retrieve the value of. * @return The value of the specified property. * @exception EngineException Couldn't access properties. */ protected String getProperty(String name) throws EngineException { Properties p = getAppData().getIdiosyncrasies(databaseType); return (p == null ? null : p.getProperty(name)); } /** * Determines if this database will be using the * <code>IDMethod.ID_BROKER</code> to create ids for torque OM * objects. * @return true if there is at least one table in this database that * uses the <code>IDMethod.ID_BROKER</code> method of generating * ids. returns false otherwise. */ public boolean requiresIdTable() { Table table[] = getTables(); for (int i = 0; i < table.length; i++) { if (table[i].getIdMethod().equals(IDMethod.ID_BROKER)) { return true; } } return false; } public void doFinalInitialization() throws EngineException { Table[] tables = getTables(); for (int i = 0; i < tables.length; i++) { Table currTable = tables[i]; // check schema integrity // if idMethod="autoincrement", make sure a column is // specified as autoIncrement="true" // FIXME: Handle idMethod="native" via DB adapter. if (currTable.getIdMethod().equals("autoincrement")) { Column[] columns = currTable.getColumns(); boolean foundOne = false; for (int j = 0; j < columns.length && !foundOne; j++) { foundOne = columns[j].isAutoIncrement(); } if (!foundOne) { String errorMessage = "Table '" + currTable.getName() + "' is marked as autoincrement, but it does not " + "have a column which declared as the one to " + "auto increment (i.e. autoIncrement=\"true\")\n"; throw new EngineException("Error in XML schema: " + errorMessage); } } currTable.doFinalInitialization(); // setup reverse fk relations ForeignKey[] fks = currTable.getForeignKeys(); for (int j = 0; j < fks.length; j++) { ForeignKey currFK = fks[j]; Table foreignTable = getTable(currFK.getForeignTableName()); if (foreignTable == null) { throw new EngineException("Attempt to set foreign" + " key to nonexistent table, " + currFK.getForeignTableName()); } else { List referrers = foreignTable.getReferrers(); if ((referrers == null || !referrers.contains(currFK))) { foreignTable.addReferrer(currFK); } // local column references Iterator localColumnNames = currFK.getLocalColumns().iterator(); while (localColumnNames.hasNext()) { Column local = currTable .getColumn((String) localColumnNames.next()); // give notice of a schema inconsistency. // note we do not prevent the npe as there is nothing // that we can do, if it is to occur. if (local == null) { throw new EngineException("Attempt to define foreign" + " key with nonexistent column in table, " + currTable.getName()); } else { //check for foreign pk's if (local.isPrimaryKey()) { currTable.setContainsForeignPK(true); } } } // foreign column references Iterator foreignColumnNames = currFK.getForeignColumns().iterator(); while (foreignColumnNames.hasNext()) { String foreignColumnName = (String) foreignColumnNames.next(); Column foreign = foreignTable.getColumn(foreignColumnName); // if the foreign column does not exist, we may have an // external reference or a misspelling if (foreign == null) { throw new EngineException("Attempt to set foreign" + " key to nonexistent column: table=" + currTable.getName() + ", foreign column=" + foreignColumnName); } else { foreign.addReferrer(currFK); } } } } } } /** * Creats a string representation of this Database. * The representation is given in xml format. * * @return string representation in xml */ public String toString() { StringBuffer result = new StringBuffer(); result.append("<database name=\"").append(getName()).append('"') .append(" package=\"").append(getPackage()).append('"') .append(" defaultIdMethod=\"").append(getDefaultIdMethod()) .append('"') .append(" baseClass=\"").append(getBaseClass()).append('"') .append(" basePeer=\"").append(getBasePeer()).append('"') .append(">\n"); for (Iterator i = tableList.iterator(); i.hasNext();) { result.append(i.next()); } result.append("</database>"); return result.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -