ca.sqlpower.architect
Class SQLTable

java.lang.Object
  extended by ca.sqlpower.architect.SQLObject
      extended by ca.sqlpower.architect.SQLTable
All Implemented Interfaces:
java.io.Serializable

public class SQLTable
extends SQLObject

See Also:
Serialized Form

Nested Class Summary
static class SQLTable.Folder<T extends SQLObject>
          The Folder class is a SQLObject that holds a SQLTable's child folders (columns and relationships).
 
Field Summary
protected  SQLTable.Folder<SQLColumn> columnsFolder
          A List of SQLColumn objects which make up all the columns of this table.
protected  SQLTable.Folder<SQLRelationship> exportedKeysFolder
          A List of SQLRelationship objects describing keys that this table exports.
protected  SQLTable.Folder<SQLRelationship> importedKeysFolder
          A container for SQLRelationship objects describing keys that this table imports.
protected  SQLObject parent
           
protected  java.lang.String physicalPrimaryKeyName
           
protected  java.lang.String remarks
           
 
Fields inherited from class ca.sqlpower.architect.SQLObject
children, magicDisableCount, populated, undoEventListeners
 
Constructor Summary
SQLTable()
          Creates a new SQLTable with no children, no parent, and all properties set to their defaults.
SQLTable(SQLDatabase parent, boolean startPopulated)
          Creates a new SQLTable with parent as its parent and a null schema and catalog.
SQLTable(SQLObject parent, java.lang.String name, java.lang.String remarks, java.lang.String objectType, boolean startPopulated)
           
 
Method Summary
protected  void addChildImpl(int index, SQLObject child)
          Connects up the columnsFolder, exportedKeysFolder, importedKeysFolder, and indicesFolder pointers to the children at indices 0, 1, 2, and 3 respectively.
 void addColumn(int pos, SQLColumn col)
           
 void addColumn(SQLColumn col)
           
 void addIndex(SQLIndex idx)
          Adds the given SQLIndex object to this table's index folder.
 boolean allowsChildren()
          Returns true (tables have several folders as children).
 void changeColumnIndex(int oldIdx, int newIdx, boolean putInPK)
          Moves the column at index oldIdx to index newIdx.
 SQLCatalog getCatalog()
           
 java.lang.String getCatalogName()
           
 java.lang.Class<? extends SQLObject> getChildType()
           
 SQLColumn getColumn(int idx)
           
 SQLColumn getColumnByName(java.lang.String colName)
          Populates this table then searches for the named column in a case-insensitive manner.
 SQLColumn getColumnByName(java.lang.String colName, boolean populate, boolean caseSensitive)
          Searches for the named column.
 SQLColumn getColumnByName(java.lang.String colName, boolean populate, boolean caseSensitive, java.sql.DatabaseMetaData dbmd)
           
 SQLColumn getColumnByName(java.lang.String colName, java.sql.DatabaseMetaData dbmd)
           
 int getColumnIndex(SQLColumn col)
           
 java.util.List<SQLColumn> getColumns()
          Gets the value of columns
 SQLTable.Folder<SQLColumn> getColumnsFolder()
           
static SQLTable getDerivedInstance(SQLTable source, SQLDatabase parent)
          Creates a new SQLTable under the given parent database.
 SQLRelationship getExportedKeyByName(java.lang.String name)
          Gets the value of exportedKeys by name
 SQLRelationship getExportedKeyByName(java.lang.String name, boolean populate)
          Gets the value of exportedKeys by name
 java.util.List<SQLRelationship> getExportedKeys()
          Gets the value of exportedKeys
 java.util.List<SQLRelationship> getExportedKeys(java.sql.DatabaseMetaData dbmd)
           
 SQLTable.Folder<SQLRelationship> getExportedKeysFolder()
           
 java.util.List<SQLRelationship> getImportedKeys()
          Gets the value of importedKeys
 SQLTable.Folder<SQLRelationship> getImportedKeysFolder()
           
 SQLIndex getIndexByName(java.lang.String name)
          Gets the value of index by name
 SQLIndex getIndexByName(java.lang.String name, boolean populate)
          Gets the value of index by name
 java.util.List<SQLIndex> getIndices()
          Returns an unmodifiable list of all the indices of this table, in the same order they appear in the indices folder.
 SQLTable.Folder<SQLIndex> getIndicesFolder()
           
 java.lang.String getObjectType()
          Gets the type of table this object represents (TABLE or VIEW).
 SQLObject getParent()
          Returns the parent of this SQLObject or null if it is a root object such as SQLDatabase.
 SQLDatabase getParentDatabase()
          Walks up the SQLObject containment hierarchy and returns the first SQLDatabase object encountered.
 java.lang.String getPhysicalPrimaryKeyName()
          Gets the value of physicalPrimaryKeyName
 int getPkSize()
          Counts the number of columns in the primary key of this table.
 SQLIndex getPrimaryKeyIndex()
          Returns the primary key for this table, or null if none exists.
 java.lang.String getPrimaryKeyName()
          Gets the name of this table's Primary Key index if it has one, otherwise returns null.
 java.lang.String getRemarks()
          Gets the value of remarks
 SQLSchema getSchema()
           
 java.lang.String getSchemaName()
           
 java.lang.String getShortDisplayName()
          The table's name.
 java.util.List<SQLIndex> getUniqueIndices()
          Gets a list of unique indices
 void inherit(int pos, SQLColumn sourceCol, boolean addToPK)
           
 void inherit(int pos, SQLTable source)
          Inserts all the columns of the given source table into this table at position pos.
 void inherit(SQLTable source)
          Adds all the columns of the given source table to the end of this table's column list.
 void initFolders(boolean populated)
          If you create a table from scratch using the no-args constructor, you should call this to create the standard set of Folder objects under this table.
 boolean isColumnsPopulated()
          Returns true if this table's columns folder says it's populated.
 boolean isIndicesPopulated()
          Returns true if this table's indices folder says it's populated.
 boolean isPopulated()
          Tells if this object has already been filled with children, or if that operation is still pending.
 boolean isRelationshipsPopulated()
          Returns true if this table's imported keys folder and exported keys folders both say they're populated.
 java.util.List<SQLRelationship> keysOfColumn(SQLColumn col)
           
 void normalizePrimaryKey()
          Renumbers the PrimaryKeySeq values of all columns in this table, then rebuilds this table's Primary Key Index so it correctly reflects the new PrimaryKeySeq column properties.
 void populate()
          Since SQLTable is just a container for Folders, there is no special populate step.
 void removeColumn(int index)
          Calls removeColumn(SQLColumn) with the appropriate argument.
 void removeColumn(SQLColumn col)
          Removes the given column if it is in this table.
 void removeExportedKey(SQLRelationship r)
          Convenience method for getExportedKeys.removeChild(r).
 void removeImportedKey(SQLRelationship r)
          Convenience method for getImportedKeys.removeChild(r).
 void setName(java.lang.String argName)
          Sets the table name, and also modifies the primary key name if it was previously null or set to the default of "oldTableName_pk".
 void setObjectType(java.lang.String argObjectType)
          Sets the type of table this object represents (TABLE or VIEW).
protected  void setParent(SQLObject newParent)
          Parents call this on their children to update parent pointers during addChild and removeChild requests.
 void setPhysicalPrimaryKeyName(java.lang.String argPhysicalPrimaryKeyName)
          Sets the value of physicalPrimaryKeyName
 void setRemarks(java.lang.String argRemarks)
          Sets the value of remarks
 java.lang.String toString()
           
 
Methods inherited from class ca.sqlpower.architect.SQLObject
addChild, addChild, addSQLObjectListener, addSQLObjectPreEventListener, addUndoEventListener, endCompoundEdit, fireDbChildInserted, fireDbChildPreRemove, fireDbChildRemoved, fireDbChildrenInserted, fireDbChildrenPreRemove, fireDbChildrenRemoved, fireDbObjectChanged, fireDbStructureChanged, getChild, getChildByName, getChildByNameIgnoreCase, getChildCount, getChildren, getIndexOfChildByName, getName, getPhysicalName, getSQLObjectListeners, getSQLObjectPreEventListeners, getUndoEventListeners, isMagicEnabled, removeChild, removeChild, removeImpl, removeSQLObjectListener, removeSQLObjectPreEventListener, removeUndoEventListener, setMagicEnabled, setPhysicalName, setPopulated, startCompoundEdit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parent

protected SQLObject parent

remarks

protected java.lang.String remarks

physicalPrimaryKeyName

protected java.lang.String physicalPrimaryKeyName

columnsFolder

protected SQLTable.Folder<SQLColumn> columnsFolder
A List of SQLColumn objects which make up all the columns of this table.


exportedKeysFolder

protected SQLTable.Folder<SQLRelationship> exportedKeysFolder
A List of SQLRelationship objects describing keys that this table exports. This SQLTable is the "pkTable" in its exported keys.


importedKeysFolder

protected SQLTable.Folder<SQLRelationship> importedKeysFolder
A container for SQLRelationship objects describing keys that this table imports. This SQLTable is the "fkTable" in its imported keys.

Constructor Detail

SQLTable

public SQLTable(SQLObject parent,
                java.lang.String name,
                java.lang.String remarks,
                java.lang.String objectType,
                boolean startPopulated)
         throws ArchitectException
Throws:
ArchitectException

SQLTable

public SQLTable(SQLDatabase parent,
                boolean startPopulated)
         throws ArchitectException
Creates a new SQLTable with parent as its parent and a null schema and catalog. The table will contain the four default folders: "Columns" "Exported Keys" "Imported Keys" and "Indices".

Parameters:
startPopulated - The initial setting of this table's folders' populated flags. If this is set to false, the table will attempt to lazy-load the child folders. Otherwise, this table will not try to load its children from a database connection.
Throws:
ArchitectException

SQLTable

public SQLTable()
Creates a new SQLTable with no children, no parent, and all properties set to their defaults. Note this should never Initialize the folders.

This is mainly for code that needs to reconstruct a SQLTable from outside configuration info, such as the SwingUIProject.load() method. If you want to make SQLTable objects from scratch, consider using one of the other constructors, which initialise the state more thoroughly.

Method Detail

initFolders

public void initFolders(boolean populated)
                 throws ArchitectException
If you create a table from scratch using the no-args constructor, you should call this to create the standard set of Folder objects under this table. The regular constructor does it automatically.

Parameters:
populated - The initial value to give the folders' populated status. When loading from a file, this should be true; if lazy loading from a database, it should be false.
Throws:
ArchitectException

getDerivedInstance

public static SQLTable getDerivedInstance(SQLTable source,
                                          SQLDatabase parent)
                                   throws ArchitectException
Creates a new SQLTable under the given parent database. The new table will have all the same properties as the given source table.

Parameters:
source - The table to copy
parent - The database to insert the new table into
Returns:
The new table
Throws:
ArchitectException - if there are populate problems on source or parent Or if the parent has children of type other than SQLTable.

removeImportedKey

public void removeImportedKey(SQLRelationship r)
Convenience method for getImportedKeys.removeChild(r).


removeExportedKey

public void removeExportedKey(SQLRelationship r)
Convenience method for getExportedKeys.removeChild(r).


getPkSize

public int getPkSize()
Counts the number of columns in the primary key of this table.


inherit

public void inherit(SQLTable source)
             throws ArchitectException
Adds all the columns of the given source table to the end of this table's column list.

Throws:
ArchitectException

inherit

public void inherit(int pos,
                    SQLTable source)
             throws ArchitectException
Inserts all the columns of the given source table into this table at position pos.

If this table currently has no columns, then the source's primary key will remain intact (and this table will become an identical copy of source). If not, and if the insertion position <= this.pkSize(), then all source columns will be added to this table's primary key. Otherwise, no source columns will be added to this table's primary key.

Throws:
ArchitectException

inherit

public void inherit(int pos,
                    SQLColumn sourceCol,
                    boolean addToPK)
             throws ArchitectException
Throws:
ArchitectException

getColumn

public SQLColumn getColumn(int idx)
                    throws ArchitectException
Throws:
ArchitectException

getColumnByName

public SQLColumn getColumnByName(java.lang.String colName)
                          throws ArchitectException
Populates this table then searches for the named column in a case-insensitive manner.

Throws:
ArchitectException

getColumnByName

public SQLColumn getColumnByName(java.lang.String colName,
                                 java.sql.DatabaseMetaData dbmd)
                          throws ArchitectException
Throws:
ArchitectException

getColumnByName

public SQLColumn getColumnByName(java.lang.String colName,
                                 boolean populate,
                                 boolean caseSensitive)
                          throws ArchitectException
Searches for the named column.

Parameters:
populate - If true, this table will retrieve its column list from the database; otherwise it just searches the current list.
Throws:
ArchitectException

getColumnByName

public SQLColumn getColumnByName(java.lang.String colName,
                                 boolean populate,
                                 boolean caseSensitive,
                                 java.sql.DatabaseMetaData dbmd)
                          throws ArchitectException
Throws:
ArchitectException

getColumnIndex

public int getColumnIndex(SQLColumn col)
                   throws ArchitectException
Throws:
ArchitectException

addColumn

public void addColumn(SQLColumn col)
               throws ArchitectException
Throws:
ArchitectException

addColumn

public void addColumn(int pos,
                      SQLColumn col)
               throws ArchitectException
Throws:
ArchitectException

addIndex

public void addIndex(SQLIndex idx)
              throws ArchitectException
Adds the given SQLIndex object to this table's index folder.

Throws:
ArchitectException

addChildImpl

protected void addChildImpl(int index,
                            SQLObject child)
                     throws ArchitectException
Connects up the columnsFolder, exportedKeysFolder, importedKeysFolder, and indicesFolder pointers to the children at indices 0, 1, 2, and 3 respectively.

Overrides:
addChildImpl in class SQLObject
Parameters:
index - The index that the new child will have
child - The new child to add (must be same type as all other children)
Throws:
ArchitectException - If you try to add a child of a different type than the existing children.

removeColumn

public void removeColumn(int index)
                  throws ArchitectException
Calls removeColumn(SQLColumn) with the appropriate argument.

Throws:
LockedColumnException - If the column is "owned" by a relationship, and cannot be safely removed.
ArchitectException

removeColumn

public void removeColumn(SQLColumn col)
                  throws ArchitectException
Removes the given column if it is in this table. If you want to change a column's, index, use the #changeColumnIndex(int,int) method because it does not throw LockedColumnException.

FIXME: This should be implemented by decreasing the column's reference count. (addColumn already does increase reference count when appropriate) Then, everything that manipulates reference counts directly can just use regular addColumn and removeColumn and magic will take care of the correct behaviour!

Throws:
LockedColumnException - If the column is "owned" by a relationship, and cannot be safely removed.
ArchitectException

changeColumnIndex

public void changeColumnIndex(int oldIdx,
                              int newIdx,
                              boolean putInPK)
                       throws ArchitectException
Moves the column at index oldIdx to index newIdx. This may cause the moved column to become part of the primary key (or to be removed from the primary key).

Parameters:
oldIdx - the present index of the column.
newIdx - the index that the column will have when this method returns.
Throws:
ArchitectException

normalizePrimaryKey

public void normalizePrimaryKey()
                         throws ArchitectException
Renumbers the PrimaryKeySeq values of all columns in this table, then rebuilds this table's Primary Key Index so it correctly reflects the new PrimaryKeySeq column properties.

The process happens in three phases:

  1. Columns are assigned PrimaryKeySeq values starting with 0 and increasing by 1 with each successive column. This phase ends when a column with a null PrimaryKeySeq is encountered (of course, this could be the very first column).
  2. The remaining columns all have their PrimaryKeySeq set to null.
  3. This table's PrimaryKeyIndex is rebuilt from scratch, created if it did not previously exist, or deleted if there are no primary key columns left after phases 1 and 2. The Primary Key index will agree with the new set of columns that have a non-null PrimaryKeySeq.

Assumptions:

Throws:
ArchitectException

keysOfColumn

public java.util.List<SQLRelationship> keysOfColumn(SQLColumn col)
                                             throws ArchitectException
Throws:
ArchitectException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getParent

public SQLObject getParent()
Description copied from class: SQLObject
Returns the parent of this SQLObject or null if it is a root object such as SQLDatabase.

Specified by:
getParent in class SQLObject

setParent

protected void setParent(SQLObject newParent)
Description copied from class: SQLObject
Parents call this on their children to update parent pointers during addChild and removeChild requests.

Specified by:
setParent in class SQLObject

getShortDisplayName

public java.lang.String getShortDisplayName()
The table's name.

Specified by:
getShortDisplayName in class SQLObject

populate

public void populate()
              throws ArchitectException
Since SQLTable is just a container for Folders, there is no special populate step. The various populate operations (columns, keys, indices) are triggered by visiting the individual folders.

Specified by:
populate in class SQLObject
Throws:
ArchitectException

isPopulated

public boolean isPopulated()
Description copied from class: SQLObject
Tells if this object has already been filled with children, or if that operation is still pending.

Overrides:
isPopulated in class SQLObject

allowsChildren

public boolean allowsChildren()
Returns true (tables have several folders as children).

Specified by:
allowsChildren in class SQLObject

getChildType

public java.lang.Class<? extends SQLObject> getChildType()
Specified by:
getChildType in class SQLObject

getParentDatabase

public SQLDatabase getParentDatabase()
Walks up the SQLObject containment hierarchy and returns the first SQLDatabase object encountered. If this SQLTable has no SQLDatabase ancestors, the return value is null.

Returns:
the value of parentDatabase

getCatalogName

public java.lang.String getCatalogName()
Returns:
An empty string if the catalog for this table is null; otherwise, getCatalog().getCatalogName().

getCatalog

public SQLCatalog getCatalog()

getSchemaName

public java.lang.String getSchemaName()
Returns:
An empty string if the schema for this table is null; otherwise, schema.getSchemaName().

getSchema

public SQLSchema getSchema()

getColumnsFolder

public SQLTable.Folder<SQLColumn> getColumnsFolder()

getImportedKeysFolder

public SQLTable.Folder<SQLRelationship> getImportedKeysFolder()

getExportedKeysFolder

public SQLTable.Folder<SQLRelationship> getExportedKeysFolder()

getIndicesFolder

public SQLTable.Folder<SQLIndex> getIndicesFolder()

setName

public void setName(java.lang.String argName)
Sets the table name, and also modifies the primary key name if it was previously null or set to the default of "oldTableName_pk". Additionally, if any of this table's columns' sequence names have been explicitly set, the old table name within those sequence names will be replaced by the new table name.

Overrides:
setName in class SQLObject
Parameters:
argName - The new table name. NULL is not allowed.

getRemarks

public java.lang.String getRemarks()
Gets the value of remarks

Returns:
the value of remarks

setRemarks

public void setRemarks(java.lang.String argRemarks)
Sets the value of remarks

Parameters:
argRemarks - Value to assign to this.remarks

getColumns

public java.util.List<SQLColumn> getColumns()
                                     throws ArchitectException
Gets the value of columns

Returns:
the value of columns
Throws:
ArchitectException

getImportedKeys

public java.util.List<SQLRelationship> getImportedKeys()
                                                throws ArchitectException
Gets the value of importedKeys

Returns:
the value of importedKeys
Throws:
ArchitectException

getExportedKeys

public java.util.List<SQLRelationship> getExportedKeys()
                                                throws ArchitectException
Gets the value of exportedKeys

Returns:
the value of exportedKeys
Throws:
ArchitectException

getExportedKeys

public java.util.List<SQLRelationship> getExportedKeys(java.sql.DatabaseMetaData dbmd)
                                                throws ArchitectException
Throws:
ArchitectException

getExportedKeyByName

public SQLRelationship getExportedKeyByName(java.lang.String name)
                                     throws ArchitectException
Gets the value of exportedKeys by name

Returns:
the value of exportedKeys
Throws:
ArchitectException

getExportedKeyByName

public SQLRelationship getExportedKeyByName(java.lang.String name,
                                            boolean populate)
                                     throws ArchitectException
Gets the value of exportedKeys by name

Returns:
the value of exportedKeys
Throws:
ArchitectException

getUniqueIndices

public java.util.List<SQLIndex> getUniqueIndices()
                                          throws ArchitectException
Gets a list of unique indices

Throws:
ArchitectException

getIndexByName

public SQLIndex getIndexByName(java.lang.String name)
                        throws ArchitectException
Gets the value of index by name

Returns:
the value of index
Throws:
ArchitectException

getIndexByName

public SQLIndex getIndexByName(java.lang.String name,
                               boolean populate)
                        throws ArchitectException
Gets the value of index by name

Returns:
the value of index
Throws:
ArchitectException

isColumnsPopulated

public boolean isColumnsPopulated()
Returns true if this table's columns folder says it's populated.


isRelationshipsPopulated

public boolean isRelationshipsPopulated()
Returns true if this table's imported keys folder and exported keys folders both say they're populated.


isIndicesPopulated

public boolean isIndicesPopulated()
Returns true if this table's indices folder says it's populated.


getPrimaryKeyName

public java.lang.String getPrimaryKeyName()
                                   throws ArchitectException
Gets the name of this table's Primary Key index if it has one, otherwise returns null.

Throws:
ArchitectException

getPhysicalPrimaryKeyName

public java.lang.String getPhysicalPrimaryKeyName()
                                           throws ArchitectException
Gets the value of physicalPrimaryKeyName

Returns:
the value of physicalPrimaryKeyName
Throws:
ArchitectException

setPhysicalPrimaryKeyName

public void setPhysicalPrimaryKeyName(java.lang.String argPhysicalPrimaryKeyName)
Sets the value of physicalPrimaryKeyName

Parameters:
argPhysicalPrimaryKeyName - Value to assign to this.physicalPrimaryKeyName

getObjectType

public java.lang.String getObjectType()
Gets the type of table this object represents (TABLE or VIEW).

Returns:
the value of objectType

setObjectType

public void setObjectType(java.lang.String argObjectType)
Sets the type of table this object represents (TABLE or VIEW).

Parameters:
argObjectType - Value to assign to this.objectType

getPrimaryKeyIndex

public SQLIndex getPrimaryKeyIndex()
                            throws ArchitectException
Returns the primary key for this table, or null if none exists.

Throws:
ArchitectException

getIndices

public java.util.List<SQLIndex> getIndices()
                                    throws ArchitectException
Returns an unmodifiable list of all the indices of this table, in the same order they appear in the indices folder. If this table has no indices, the returned list will be empty (never null).

Throws:
ArchitectException - If there is a problem populating the indices folder


Copyright © 2003-2007 SQL Power Group Inc. www.sqlpower.ca