📄 table.java
字号:
package org.apache.torque.engine.database.model;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.torque.engine.EngineException;import org.xml.sax.Attributes;/** * Data about a table used in an application. * * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a> * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a> * @author <a href="mailto:jmcnally@collab.net>John McNally</a> * @author <a href="mailto:dlr@collab.net>Daniel Rall</a> * @author <a href="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a> * @version $Id: Table.java,v 1.3 2003/06/26 22:09:41 dlr Exp $ */public class Table implements IDMethod{ /** Logging class from commons.logging */ private static Log log = LogFactory.getLog(Table.class); //private AttributeListImpl attributes; private List columnList; private List foreignKeys; private List indices; private List unices; private List idMethodParameters; private String name; private String description; private String javaName; private String idMethod; private String javaNamingMethod; private Database tableParent; private List referrers; private List foreignTableNames; private boolean containsForeignPK; private Column inheritanceColumn; private boolean skipSql; private boolean abstractValue; private String alias; private String enterface; private String pkg; private String baseClass; private String basePeer; private Hashtable columnsByName; private Hashtable columnsByJavaName; private boolean needsTransactionInPostgres; private boolean heavyIndexing; private boolean forReferenceOnly; /** * Default Constructor */ public Table() { this(null); } /** * Constructs a table object with a name * * @param name table name */ public Table(String name) { this.name = name; columnList = new ArrayList(); foreignKeys = new ArrayList(5); indices = new ArrayList(5); unices = new ArrayList(5); columnsByName = new Hashtable(); columnsByJavaName = new Hashtable(); } /** * Load the table object from an xml tag. * * @param attrib xml attributes * @param defaultIdMethod defined at db level */ public void loadFromXML(Attributes attrib, String defaultIdMethod) { name = attrib.getValue("name"); javaName = attrib.getValue("javaName"); idMethod = attrib.getValue("idMethod"); // retrieves the method for converting from specified name to // a java name. javaNamingMethod = attrib.getValue("javaNamingMethod"); if (javaNamingMethod == null) { javaNamingMethod = getDatabase().getDefaultJavaNamingMethod(); } if ("null".equals(idMethod)) { idMethod = defaultIdMethod; } if ("autoincrement".equals(idMethod) || "sequence".equals(idMethod)) { log.warn("The value '" + idMethod + "' for Torque's " + "table.idMethod attribute has been deprecated in favor " + "of '" + NATIVE + "'. Please adjust your " + "Torque XML schema accordingly."); idMethod = NATIVE; } skipSql = "true".equals(attrib.getValue("skipSql")); // pkg = attrib.getValue("package"); abstractValue = "true".equals(attrib.getValue("abstract")); baseClass = attrib.getValue("baseClass"); basePeer = attrib.getValue("basePeer"); alias = attrib.getValue("alias"); heavyIndexing = "true".equals(attrib.getValue("heavyIndexing")) || (!"false".equals(attrib.getValue("heavyIndexing")) && getDatabase().isHeavyIndexing()); description = attrib.getValue("description"); enterface = attrib.getValue("interface"); } /** * <p>A hook for the SAX XML parser to call when this table has * been fully loaded from the XML, and all nested elements have * been processed.</p> * * <p>Performs heavy indexing and naming of elements which weren't * provided with a name.</p> */ public void doFinalInitialization() { // Heavy indexing must wait until after all columns composing // a table's primary key have been parsed. if (heavyIndexing) { doHeavyIndexing(); } // Name any indices which are missing a name using the // appropriate algorithm. doNaming(); } /** * <p>Adds extra indices for multi-part primary key columns.</p> * * <p>For databases like MySQL, values in a where clause must * match key part order from the left to right. So, in the key * definition <code>PRIMARY KEY (FOO_ID, BAR_ID)</code>, * <code>FOO_ID</code> <i>must</i> be the first element used in * the <code>where</code> clause of the SQL query used against * this table for the primary key index to be used. This feature * could cause problems under MySQL with heavily indexed tables, * as MySQL currently only supports 16 indices per table (i.e. it * might cause too many indices to be created).</p> * * <p>See <a href="http://www.mysql.com/doc/E/X/EXPLAIN.html">the * manual</a> for a better description of why heavy indexing is * useful for quickly searchable database tables.</p> */ private void doHeavyIndexing() { if (log.isDebugEnabled()) { log.debug("doHeavyIndex() called on table " + name); } List pk = getPrimaryKey(); int size = pk.size(); try { // We start at an offset of 1 because the entire column // list is generally implicitly indexed by the fact that // it's a primary key. for (int i = 1; i < size; i++) { addIndex(new Index(this, pk.subList(i, size))); } } catch (EngineException e) { log.error(e, e); } } /** * Names composing objects which haven't yet been named. This * currently consists of foreign-key and index entities. */ private void doNaming() { int i; int size; String name; // Assure names are unique across all databases. try { for (i = 0, size = foreignKeys.size(); i < size; i++) { ForeignKey fk = (ForeignKey) foreignKeys.get(i); name = fk.getName(); if (StringUtils.isEmpty(name)) { name = acquireConstraintName("FK", i + 1); fk.setName(name); } } for (i = 0, size = indices.size(); i < size; i++) { Index index = (Index) indices.get(i); name = index.getName(); if (StringUtils.isEmpty(name)) { name = acquireConstraintName("I", i + 1); index.setName(name); } } // NOTE: Most RDBMSes can apparently name unique column // constraints/indices themselves (using MySQL and Oracle // as test cases), so we'll assume that we needn't add an // entry to the system name list for these. } catch (EngineException nameAlreadyInUse) { log.error(nameAlreadyInUse, nameAlreadyInUse); } } /** * Macro to a constraint name. * * @param nameType constraint type * @param nbr unique number for this constraint type * @return unique name for constraint * @throws EngineException */ private final String acquireConstraintName(String nameType, int nbr) throws EngineException { List inputs = new ArrayList(4); inputs.add(getDatabase()); inputs.add(getName()); inputs.add(nameType); inputs.add(new Integer(nbr)); return NameFactory.generateName(NameFactory.CONSTRAINT_GENERATOR, inputs); } /** * Gets the value of base class for classes produced from this table. * * @return The base class for classes produced from this table. */ public String getBaseClass() { if (isAlias() && baseClass == null) { return alias; } else if (baseClass == null) { return getDatabase().getBaseClass(); } else { return baseClass; } } /** * Set the value of baseClass. * @param v Value to assign to baseClass. */ public void setBaseClass(String v) { this.baseClass = v; } /** * Get the value of basePeer. * @return value of basePeer. */ public String getBasePeer() { if (isAlias() && basePeer == null) { return alias + "Peer"; } else if (basePeer == null) { return getDatabase().getBasePeer(); } else { return basePeer; } } /** * Set the value of basePeer. * @param v Value to assign to basePeer. */ public void setBasePeer(String v) { this.basePeer = v; } /** * A utility function to create a new column from attrib and add it to this * table. * * @param attrib xml attributes for the column to add * @return the added column */ public Column addColumn(Attributes attrib) { Column col = new Column(); col.setTable(this); col.loadFromXML(attrib); addColumn(col); return col; } /** * Adds a new column to the column list and set the * parent table of the column to the current table * * @param col the column to add */ public void addColumn(Column col) { col.setTable (this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -