butler.sql.metadata
Class Table

java.lang.Object
  extended by butler.util.BaseGroovyObjectSupport
      extended by butler.sql.metadata.Table
All Implemented Interfaces:
groovy.lang.GroovyObject
Direct Known Subclasses:
AttributedTable, AttributesTable, CachingTable, GeneratedTable, View, VirtualTable

public class Table
extends butler.util.BaseGroovyObjectSupport

Represents a table in the database. This class should not be serialized.

Author:
Fredrik Bertilsson

Field Summary
protected  java.util.Map _fks
           
 java.lang.String _name
           
static java.lang.String DUMMY
           
 
Constructor Summary
Table(java.lang.String name)
           
Table(java.lang.String name, Schema schema)
           
Table(java.lang.String name, Schema schema, boolean lazy)
           
Table(java.lang.String name, Schema schema, java.lang.String caption)
           
Table(java.lang.String name, Schema schema, java.lang.String caption, boolean lazy)
           
 
Method Summary
 void addForeignKey(ForeignKey fk)
           
 void addIndex(Index index)
          Adds an index to this table.
 Record addRecord()
          Creates a new empty record with the state NEW.
 void addRecordListener(RecordListener listener)
           
 void addRelation(ManyToManyRelation relation)
           
 Table copy()
          Makes a deep copy of this table
 void create()
          Creates the table in the database.
 Column createColumn(java.lang.String name, Datatype datatype, boolean primaryKey, boolean nullable)
           
 Column createColumn(java.lang.String name, int type, int size, int decimals, boolean primaryKey, boolean nullable)
          Creates a column to this table with the given name.
 Column createColumn(java.lang.String name, int type, int size, int decimals, boolean primaryKey, boolean nullable, Datatype datatype)
          Creates a column to this table with the given name.
 Column createColumn(java.lang.String name, java.lang.String datatype, boolean primaryKey, boolean nullable)
           
 Column createColumn(java.lang.String name, java.lang.String caption, int type, int size, int decimals, boolean primaryKey, boolean nullable)
          Creates a column to this table with the given name.
 Column createColumn(java.lang.String name, java.lang.String caption, int type, int size, int decimals, boolean primaryKey, boolean nullable, Datatype datatype)
          Creates a column to this table with the given name.
 DeleteQuery createDeleteQuery()
          Creates a delete query with this table as the main table.
 Query createQuery()
          Creates a query with this table as the main table.
 Record createRecord()
          Creates a new empty record with the state NEW, and firing init event.
 void drop()
          Drops the table in the database.
 boolean equals(java.lang.Object other)
           
 boolean equals(Table other)
           
 boolean exists()
           
 RecordList findAll()
          Returns all records in this table.
 Record findByPK(java.lang.Object value)
          Returns the record with the given (single-column) primary key.
 Record findByPK(butler.util.Tuple pk)
          Returns the record with the given (multi-column) primary key.
 Record findByPK0(butler.util.Tuple pk)
           
 Record findByPKIfExists(java.lang.Object key)
          Returns the record with the given primary key.
 Record findByPKIfExists(butler.util.Tuple key)
          Returns the record with the given primary key.
 Authorizator getAuthorizator()
           
 java.lang.String getCaption()
          Returns the table caption, the name if no caption is given.
 Column getColumn(java.lang.String columnname)
          Returns the column with the given name.
 java.util.List getColumns()
          Returns all columns.
 Database getDatabase()
           
 java.util.List getDataColumns()
          Returns physical columns that are not in the primary key.
 java.util.List getExportedForeignKeys()
           
 ForeignKey getForeignKey(java.lang.String name)
          Returns the foreign key with the given name.
 ForeignKey getForeignKey(Table table)
          Returns the foreign key that connects this table and the given table.
 java.util.Iterator getForeignKeyNames()
          Deprecated. Use getForeignKeys()
 java.util.List getForeignKeys()
          Returns the foreign keys for this table.
 java.util.List getImportedForeignKeys()
           
 Index getIndex(java.lang.String name)
          Returns the index with the given name.
 java.util.List getIndexes()
          Returns all indexes for this table.
 butler.util.LocalizedText getLocalizedCaption()
           
 java.util.List getLogicalColumns()
          Returns logical columns.
 java.lang.String getName()
          Returns the table name.
 java.lang.String getPath()
           
 java.util.List getPhysicalColumns()
           
 java.lang.String getPhysicalName()
           
 java.util.List getPrimaryKeyColumns()
          Returns the primary key columns.
 java.lang.Object getProperty(java.lang.String property)
           
 java.lang.String getQualifiedName(java.sql.DatabaseMetaData metaData)
           
 Query getQuery(java.lang.String name)
           
 java.util.Comparator getRecordComparator()
           
 java.util.Set getRecordListeners()
           
 ManyToManyRelation getRelation(java.lang.String name)
           
 Schema getSchema()
          Returns the schema to which the table belongs.
static Table getTable(org.dom4j.Element element)
           
static Table getTable(java.util.Map tableDictonary, org.dom4j.Element element)
           
 RecordValidator getValidator()
           
 int hashCode()
           
 java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
           
 boolean isDummy()
           
protected  void load()
           
protected  void loadAll()
           
protected  void loadFKs()
           
 Record newRecord()
          Creates a new empty record.
 void registerColumn(Column column)
           
 void registerQuery(Query q)
           
 void setAuthorizator(Authorizator auth)
           
 void setRecordComparator(java.util.Comparator comp)
           
 void setSchema(Schema schema)
          Sets the schema to which to table belongs.
 void setValidator(RecordValidator validator)
           
 void toXML(org.xml.sax.ContentHandler handler)
           
 void unload(java.io.PrintStream stream)
           
 void upgrade()
          Creates the table, adds column, foreign keys and indexes if necessary.
 
Methods inherited from class butler.util.BaseGroovyObjectSupport
getMetaClass, setMetaClass, setProperty
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DUMMY

public static final java.lang.String DUMMY
See Also:
Constant Field Values

_name

public java.lang.String _name

_fks

protected java.util.Map _fks
Constructor Detail

Table

public Table(java.lang.String name)

Table

public Table(java.lang.String name,
             Schema schema)

Table

public Table(java.lang.String name,
             Schema schema,
             java.lang.String caption)

Table

public Table(java.lang.String name,
             Schema schema,
             boolean lazy)

Table

public Table(java.lang.String name,
             Schema schema,
             java.lang.String caption,
             boolean lazy)
Method Detail

getName

public java.lang.String getName()
Returns the table name.


getCaption

public java.lang.String getCaption()
                            throws java.lang.Exception
Returns the table caption, the name if no caption is given.

Throws:
java.lang.Exception

getLocalizedCaption

public butler.util.LocalizedText getLocalizedCaption()
                                              throws java.lang.Exception
Throws:
java.lang.Exception

getSchema

public Schema getSchema()
                 throws java.lang.Exception
Returns the schema to which the table belongs.

Throws:
java.lang.Exception

getDatabase

public Database getDatabase()
                     throws java.lang.Exception
Throws:
java.lang.Exception

setSchema

public void setSchema(Schema schema)
Sets the schema to which to table belongs.


getForeignKey

public ForeignKey getForeignKey(java.lang.String name)
                         throws java.lang.Exception
Returns the foreign key with the given name.

Throws:
java.lang.Exception

addForeignKey

public void addForeignKey(ForeignKey fk)
                   throws java.lang.Exception
Throws:
java.lang.Exception

addRelation

public void addRelation(ManyToManyRelation relation)
                 throws java.lang.Exception
Throws:
java.lang.Exception

getRelation

public ManyToManyRelation getRelation(java.lang.String name)

setValidator

public void setValidator(RecordValidator validator)

getValidator

public RecordValidator getValidator()

setAuthorizator

public void setAuthorizator(Authorizator auth)

getAuthorizator

public Authorizator getAuthorizator()

getForeignKeyNames

public java.util.Iterator getForeignKeyNames()
                                      throws java.lang.Exception
Deprecated. Use getForeignKeys()

Returns the names of all foreign keys for this table.

Throws:
java.lang.Exception

getForeignKeys

public java.util.List getForeignKeys()
                              throws java.lang.Exception
Returns the foreign keys for this table.

Throws:
java.lang.Exception

getImportedForeignKeys

public java.util.List getImportedForeignKeys()
                                      throws java.lang.Exception
Throws:
java.lang.Exception

getExportedForeignKeys

public java.util.List getExportedForeignKeys()
                                      throws java.lang.Exception
Throws:
java.lang.Exception

getColumn

public Column getColumn(java.lang.String columnname)
                 throws java.lang.Exception
Returns the column with the given name.

Throws:
java.lang.Exception

getColumns

public java.util.List getColumns()
                          throws java.lang.Exception
Returns all columns.

Throws:
java.lang.Exception

getPhysicalColumns

public java.util.List getPhysicalColumns()
                                  throws java.lang.Exception
Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           int type,
                           int size,
                           int decimals,
                           boolean primaryKey,
                           boolean nullable)
                    throws java.lang.Exception
Creates a column to this table with the given name. The column is not actually added until the create method is called.

Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           java.lang.String caption,
                           int type,
                           int size,
                           int decimals,
                           boolean primaryKey,
                           boolean nullable)
                    throws java.lang.Exception
Creates a column to this table with the given name. The column is not actually added until the create method is called.

Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           int type,
                           int size,
                           int decimals,
                           boolean primaryKey,
                           boolean nullable,
                           Datatype datatype)
                    throws java.lang.Exception
Creates a column to this table with the given name. The column is not actually added until the create method is called.

Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           java.lang.String caption,
                           int type,
                           int size,
                           int decimals,
                           boolean primaryKey,
                           boolean nullable,
                           Datatype datatype)
                    throws java.lang.Exception
Creates a column to this table with the given name. The column is not actually added until the create method is called.

Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           Datatype datatype,
                           boolean primaryKey,
                           boolean nullable)
                    throws java.lang.Exception
Throws:
java.lang.Exception

createColumn

public Column createColumn(java.lang.String name,
                           java.lang.String datatype,
                           boolean primaryKey,
                           boolean nullable)
                    throws java.lang.Exception
Throws:
java.lang.Exception

registerColumn

public void registerColumn(Column column)
                    throws java.lang.Exception
Throws:
java.lang.Exception

getPrimaryKeyColumns

public java.util.List getPrimaryKeyColumns()
                                    throws java.lang.Exception
Returns the primary key columns.

Throws:
java.lang.Exception

getDataColumns

public java.util.List getDataColumns()
                              throws java.lang.Exception
Returns physical columns that are not in the primary key.

Throws:
java.lang.Exception

getLogicalColumns

public java.util.List getLogicalColumns()
                                 throws java.lang.Exception
Returns logical columns.

Throws:
java.lang.Exception

getIndexes

public java.util.List getIndexes()
                          throws java.lang.Exception
Returns all indexes for this table.

Throws:
java.lang.Exception

getIndex

public Index getIndex(java.lang.String name)
               throws java.lang.Exception
Returns the index with the given name.

Throws:
java.lang.Exception

addIndex

public void addIndex(Index index)
Adds an index to this table.


findByPK

public Record findByPK(java.lang.Object value)
                throws java.lang.Exception
Returns the record with the given (single-column) primary key.

Throws:
RecordNotFoundException
java.lang.Exception

findByPK

public Record findByPK(butler.util.Tuple pk)
                throws java.lang.Exception
Returns the record with the given (multi-column) primary key.

Throws:
RecordNotFoundException
java.lang.Exception

findByPKIfExists

public Record findByPKIfExists(butler.util.Tuple key)
                        throws java.lang.Exception
Returns the record with the given primary key. If no record with that key exists, null is returned.

Throws:
java.lang.Exception

findByPKIfExists

public Record findByPKIfExists(java.lang.Object key)
                        throws java.lang.Exception
Returns the record with the given primary key. If no record with that key exists, null is returned.

Throws:
java.lang.Exception

findByPK0

public Record findByPK0(butler.util.Tuple pk)
                 throws java.lang.Exception
Throws:
java.lang.Exception

findAll

public RecordList findAll()
                   throws java.lang.Exception
Returns all records in this table.

Throws:
java.lang.Exception

createQuery

public Query createQuery()
                  throws java.lang.Exception
Creates a query with this table as the main table.

Throws:
java.lang.Exception

createDeleteQuery

public DeleteQuery createDeleteQuery()
                              throws java.lang.Exception
Creates a delete query with this table as the main table.

Throws:
java.lang.Exception

create

public void create()
            throws java.lang.Exception
Creates the table in the database.

Throws:
java.lang.Exception

upgrade

public void upgrade()
             throws java.lang.Exception
Creates the table, adds column, foreign keys and indexes if necessary.

Throws:
java.lang.Exception

drop

public void drop()
          throws java.lang.Exception
Drops the table in the database.

Throws:
java.lang.Exception

addRecord

public Record addRecord()
                 throws java.lang.Exception
Creates a new empty record with the state NEW.

Throws:
java.lang.Exception

createRecord

public Record createRecord()
                    throws java.lang.Exception
Creates a new empty record with the state NEW, and firing init event.

Throws:
java.lang.Exception

newRecord

public Record newRecord()
                 throws java.lang.Exception
Creates a new empty record.

Throws:
java.lang.Exception

toXML

public void toXML(org.xml.sax.ContentHandler handler)
           throws java.lang.Exception
Throws:
java.lang.Exception

loadAll

protected void loadAll()
                throws java.lang.Exception
Throws:
java.lang.Exception

load

protected void load()
             throws java.lang.Exception
Throws:
java.lang.Exception

loadFKs

protected void loadFKs()
                throws java.lang.Exception
Throws:
java.lang.Exception

getQualifiedName

public java.lang.String getQualifiedName(java.sql.DatabaseMetaData metaData)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

equals

public boolean equals(Table other)

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

unload

public void unload(java.io.PrintStream stream)
            throws java.lang.Exception
Throws:
java.lang.Exception

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

getForeignKey

public ForeignKey getForeignKey(Table table)
                         throws java.lang.Exception
Returns the foreign key that connects this table and the given table. If more that one, or zero, foreign keys exists, an exception is thrown.

Throws:
java.lang.IllegalArgumentException - Thrown if no or multiple foreign keys was found.
java.lang.Exception

addRecordListener

public void addRecordListener(RecordListener listener)

getRecordListeners

public java.util.Set getRecordListeners()

getTable

public static Table getTable(org.dom4j.Element element)
                      throws java.lang.Exception
Throws:
java.lang.Exception

exists

public boolean exists()
               throws java.lang.Exception
Throws:
java.lang.Exception

copy

public Table copy()
           throws java.lang.Exception
Makes a deep copy of this table

Throws:
java.lang.Exception

getRecordComparator

public java.util.Comparator getRecordComparator()

setRecordComparator

public void setRecordComparator(java.util.Comparator comp)

getPath

public java.lang.String getPath()

getTable

public static Table getTable(java.util.Map tableDictonary,
                             org.dom4j.Element element)
                      throws java.lang.Exception
Throws:
java.lang.Exception

registerQuery

public void registerQuery(Query q)

getQuery

public Query getQuery(java.lang.String name)

getProperty

public java.lang.Object getProperty(java.lang.String property)
Specified by:
getProperty in interface groovy.lang.GroovyObject
Overrides:
getProperty in class butler.util.BaseGroovyObjectSupport

invokeMethod

public java.lang.Object invokeMethod(java.lang.String name,
                                     java.lang.Object args)
Specified by:
invokeMethod in interface groovy.lang.GroovyObject
Overrides:
invokeMethod in class butler.util.BaseGroovyObjectSupport

getPhysicalName

public java.lang.String getPhysicalName()

isDummy

public boolean isDummy()