com.perdues.db
Class Schema

java.lang.Object
  |
  +--com.perdues.db.Schema

public class Schema
extends java.lang.Object

This class defines information about one or more databases. A Schema contains information about one or more databases, their tables and fields. Beyond names and types, a Schema object contains information about which fields represent foreign keys and application-level type information about the fields. For example a field type can be a high-level type such as a phone number or an email, not just a varchar or integer.

Schemas work with the Form class to provide very easy and concise database operations: read, insert, update, delete, and very automated validation.

These Schema objects are in principle immutable except that the current database can be changed with the "use" or "useDB" method. This immutability is not enforced, however.

The type information for a column can indicate a Java type or a conceptual type such as "email", stored as a standard database type such as VARCHAR, but validated in a more specific way, and may have one or more specialized presentations as HTML forms.

Validation for application types and certain other operations are determined by DBTypeHandler classes. A Schema has a set of DBTypeHandlers, defined by addTypeHandler.

See Also:
DBTypeHandler

Constructor Summary
Schema(Schema template)
          The primary way to build a Schema is with Schema.configure, but you can build one similar to one that already exists with this constructor.
 
Method Summary
 void addDatabase(XMLTag tag)
          Adds a database, defined by an XMLTag of type "db", to the set of databases described by this Schema.
 void addTypeHandler(java.lang.String type, DBTypeHandler handler)
          Adds a handler for a type.
static Schema configure(java.lang.String resourceName)
          Loads database definitions from a file or archive member, given its Java "resource name".
 java.util.Enumeration databases()
          Enumerate all available databases.
static void defTypeHandler(java.lang.String type, DBTypeHandler handler)
          Defines a global ("default") handler for a type.
 DBInfo get(java.lang.String databaseName)
          Return a Database given its name.
 ColumnInfo getColumn(java.lang.String name)
          Return a ColumnInfo given a qualified column name, from the default Database.
 DBInfo getDB()
          Return the default Database.
 TableInfo getTable(java.lang.String name)
          Get a table from the current database by name or shorthand, or errs if there is none.
 void useDB(java.lang.String databaseName)
          Sets the current database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Schema

public Schema(Schema template)
The primary way to build a Schema is with Schema.configure, but you can build one similar to one that already exists with this constructor.
Method Detail

configure

public static Schema configure(java.lang.String resourceName)
                        throws java.io.IOException
Loads database definitions from a file or archive member, given its Java "resource name". This sets the primary schema according to the information in the resource file. If a description already exists in the primary Schema, this overwrites it. The first database described in the resource file becomes the Schema's current database.

Java resource names are like relative file paths, with "/" always the delimiter between path elements. The paths must be relative to some element of the CLASSPATH, and the first matching file is chosen. For example, if CLASSPATH is /home/myself/classes:/foo/classes, and the the resourceName is "db/config.xml", then the search will first look for a file named /home/myself/classes/db/config.xml, then /foo/classes/db/config.xml. If a CLASSPATH element is an archive file, the relative path indicates an archive member.


getTable

public TableInfo getTable(java.lang.String name)
Get a table from the current database by name or shorthand, or errs if there is none.

getColumn

public ColumnInfo getColumn(java.lang.String name)
Return a ColumnInfo given a qualified column name, from the default Database.

getDB

public DBInfo getDB()
Return the default Database.

useDB

public void useDB(java.lang.String databaseName)
Sets the current database.

databases

public java.util.Enumeration databases()
Enumerate all available databases.

get

public DBInfo get(java.lang.String databaseName)
Return a Database given its name. Throws an IllegalArgumentException if there is none.

addDatabase

public void addDatabase(XMLTag tag)
Adds a database, defined by an XMLTag of type "db", to the set of databases described by this Schema. Replaces any existing database with the same name. If it is the first database added to this schema, it is set as the current database.

addTypeHandler

public void addTypeHandler(java.lang.String type,
                           DBTypeHandler handler)
Adds a handler for a type.

defTypeHandler

public static void defTypeHandler(java.lang.String type,
                                  DBTypeHandler handler)
Defines a global ("default") handler for a type. Overrides an existing default. Type handlers for a schema override it in turn.