jimm.datavision.source.sql
Class Database

java.lang.Object
  extended by jimm.datavision.source.DataSource
      extended by jimm.datavision.source.sql.Database
All Implemented Interfaces:
Writeable
Direct Known Subclasses:
SubreportDatabase

public class Database
extends DataSource

A database knows about the tables in a database.

Author:
Jim Menard, jimm@io.com

Field Summary
protected  java.sql.Connection conn
           
protected  boolean connectionOwnedByMe
           
protected  java.lang.String connInfo
           
protected static java.lang.String[] DB_OBJECT_TYPES
           
protected  java.lang.String driverClassName
           
protected  boolean hasPassword
           
protected  java.lang.String name
           
protected  java.lang.String quoteString
           
protected  java.lang.String schemaName
           
protected  boolean storesLowerCaseIdentifiers
           
protected  boolean storesUpperCaseIdentifiers
           
protected  java.util.HashMap tableCacheMap
           
protected  java.util.TreeMap tables
           
protected  java.lang.String username
           
 
Fields inherited from class jimm.datavision.source.DataSource
metadataURL, query, report
 
Constructor Summary
  Database(java.sql.Connection conn, Report report)
           
  Database(java.lang.String driverClassName, java.lang.String connInfo, Report report, java.lang.String name, java.lang.String user)
          Constructor.
  Database(java.lang.String driverClassName, java.lang.String connInfo, Report report, java.lang.String name, java.lang.String user, java.lang.String password)
          Constructor.
protected Database(java.lang.String driverClassName, java.lang.String connInfo, Report report, java.lang.String name, java.lang.String user, java.lang.String password, boolean givenPassword)
          Constructor.
 
Method Summary
 boolean areRecordsSelectable()
          Used to enable/disable the "Select Records" menu item.
 boolean areRecordsSortable()
          Used to enable/disable the "Sort By" menu item.
 boolean canGroupRecords()
          Used to enable/disable the "Group By" menu item.
 boolean canJoinTables()
          Used to enable/disable the "Table Linker" menu item.
 java.util.Iterator columns()
          Returns an iterator over all columns.
protected  void doWriteXML(XMLWriter out)
          Writes this database and all its tables as an XML tag.
 DataCursor execute()
           
 Column findColumn(java.lang.Object id)
          Given an id (a column name), returns the column that has that id.
protected  Table findTable(java.lang.String tableName)
          Given a table name, find the table.
protected  Table findTableWithId(java.lang.String id)
          Finds table with given id string.
 java.sql.Connection getConnection()
          Returns a connection to the database.
 java.lang.String getConnectionInfo()
          Returns the connection info string.
 java.lang.String getDriverClassName()
          Returns the driver class name.
 java.lang.String getName()
          Returns the database name.
 java.lang.String getPassword()
          Returns the password.
 java.lang.String getUserName()
          Returns the user name.
 void initializeConnection()
          Initializes the connection to the database.
 boolean isConnectionEditable()
          Used to enable/disable the "Connection" menu item.
 boolean isSQLGenerated()
          Used to enable/disable the "SQL Query Text" menu item.
protected  void loadAllTables()
          Loads information about all tables in the database.
protected  void loadTablesUsingSchemaNameAndTypes(java.sql.DatabaseMetaData dbmd, java.lang.String schema, java.lang.String[] objectTypes)
          Loads our list of tables using a database meta data object and a schema name.
 java.lang.String quoteString()
           
 void reset(java.lang.String driverClassName, java.lang.String connInfo, java.lang.String dbName, java.lang.String username, java.lang.String password)
          Reset key instance variables, closes current connection, and "reloads" all table information (compares new info with existing info and complains if any existing info is not in the new info).
protected  void setConnectionInfo(java.lang.String newConnectionInfo)
          Sets the connection info string.
protected  void setDriverClassName(java.lang.String newDriverClassName)
          Sets the driver class name.
protected  void setName(java.lang.String newName)
          Sets the name.
 void setPassword(java.lang.String newPassword)
          Sets the password.
 void setUserName(java.lang.String newUserName)
          Sets the user name.
 boolean storesLowerCaseIdentifiers()
           
 boolean storesUpperCaseIdentifiers()
           
 java.util.Iterator tables()
          Returns an iterator over all tables, or null if the data source does not have tables (for example, a character-separated file data source).
 java.util.Iterator tablesUsedInReport()
          Returns an iterator over all tables actually used in the report, or null if the data source does not have tables (for example, a character-separated file data source).
 
Methods inherited from class jimm.datavision.source.DataSource
addColumn, alreadyUsedSourceFile, canRunReports, columnsInTablesUsedInReport, containsReferenceTo, getQuery, getReport, getSourceFile, indexOfSelectable, needsSourceFile, readMetadataFrom, reloadColumns, removeSort, reuseSourceFile, setSourceFile, usesSourceFile, writeXML
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DB_OBJECT_TYPES

protected static final java.lang.String[] DB_OBJECT_TYPES

driverClassName

protected java.lang.String driverClassName

connInfo

protected java.lang.String connInfo

name

protected java.lang.String name

username

protected java.lang.String username

tables

protected java.util.TreeMap tables

tableCacheMap

protected java.util.HashMap tableCacheMap

schemaName

protected java.lang.String schemaName

conn

protected java.sql.Connection conn

hasPassword

protected boolean hasPassword

connectionOwnedByMe

protected boolean connectionOwnedByMe

storesLowerCaseIdentifiers

protected boolean storesLowerCaseIdentifiers

storesUpperCaseIdentifiers

protected boolean storesUpperCaseIdentifiers

quoteString

protected java.lang.String quoteString
Constructor Detail

Database

public Database(java.sql.Connection conn,
                Report report)
         throws java.sql.SQLException
Throws:
java.sql.SQLException

Database

public Database(java.lang.String driverClassName,
                java.lang.String connInfo,
                Report report,
                java.lang.String name,
                java.lang.String user)
         throws java.sql.SQLException,
                java.lang.ClassNotFoundException,
                java.lang.InstantiationException,
                java.lang.IllegalAccessException,
                UserCancellationException
Constructor.

Parameters:
driverClassName - database driver class name
connInfo - database connection info string
report - the report using this database
name - the database name
user - the user name to use when logging in to the database
Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
UserCancellationException

Database

public Database(java.lang.String driverClassName,
                java.lang.String connInfo,
                Report report,
                java.lang.String name,
                java.lang.String user,
                java.lang.String password)
         throws java.sql.SQLException,
                java.lang.ClassNotFoundException,
                java.lang.InstantiationException,
                java.lang.IllegalAccessException,
                UserCancellationException
Constructor.

Parameters:
driverClassName - database driver class name
connInfo - database connection info string
report - the report using this database
name - the database name
user - the user name to use when logging in to the database
password - the database password
Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
UserCancellationException

Database

protected Database(java.lang.String driverClassName,
                   java.lang.String connInfo,
                   Report report,
                   java.lang.String name,
                   java.lang.String user,
                   java.lang.String password,
                   boolean givenPassword)
            throws java.sql.SQLException,
                   java.lang.ClassNotFoundException,
                   java.lang.InstantiationException,
                   java.lang.IllegalAccessException,
                   UserCancellationException
Constructor.

Parameters:
driverClassName - database driver class name
connInfo - database connection info string
report - the report using this database
name - the database name
user - the user name to use when logging in to the database
password - the database password
givenPassword - if true, the password was passed in to some other constructor
Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
UserCancellationException
Method Detail

canJoinTables

public boolean canJoinTables()
Description copied from class: DataSource
Used to enable/disable the "Table Linker" menu item.

Specified by:
canJoinTables in class DataSource
Returns:
true if the "Table Linker" menu item should be enabled.

isSQLGenerated

public boolean isSQLGenerated()
Description copied from class: DataSource
Used to enable/disable the "SQL Query Text" menu item.

Specified by:
isSQLGenerated in class DataSource
Returns:
true if the "SQL Query Text" menu item should be enabled.

isConnectionEditable

public boolean isConnectionEditable()
Description copied from class: DataSource
Used to enable/disable the "Connection" menu item.

Specified by:
isConnectionEditable in class DataSource
Returns:
true if the "Connection" menu item should be enabled.

areRecordsSelectable

public boolean areRecordsSelectable()
Description copied from class: DataSource
Used to enable/disable the "Select Records" menu item.

Specified by:
areRecordsSelectable in class DataSource
Returns:
true if the "Select Records" menu item should be enabled.

areRecordsSortable

public boolean areRecordsSortable()
Description copied from class: DataSource
Used to enable/disable the "Sort By" menu item.

Specified by:
areRecordsSortable in class DataSource
Returns:
true if the "Sort By" menu item should be enabled.

canGroupRecords

public boolean canGroupRecords()
Description copied from class: DataSource
Used to enable/disable the "Group By" menu item.

Specified by:
canGroupRecords in class DataSource
Returns:
true if the "Group By" menu item should be enabled.

findColumn

public Column findColumn(java.lang.Object id)
Given an id (a column name), returns the column that has that id. If no column with the specified id exists, returns null. Uses Table.findColumn.

Specified by:
findColumn in class DataSource
Parameters:
id - a column id
Returns:
a column, or null if no column with the specified id exists
See Also:
Table.findColumn(java.lang.Object)

findTable

protected Table findTable(java.lang.String tableName)
Given a table name, find the table. The table name may have a schema name or not. We look for the table first using tableName as-is, then we use this database's schema name, then we try no schema name at all.

Parameters:
tableName - a table name, perhaps including a schema name.

findTableWithId

protected Table findTableWithId(java.lang.String id)
Finds table with given id string. Returns null if it is not found. If the report says that database names are not case-sensitive, then we do a case-insensitive comparison.

Parameters:
id - a table id; if names are not case-sensitive then id will be lower-case when it is passed in

tables

public java.util.Iterator tables()
Description copied from class: DataSource
Returns an iterator over all tables, or null if the data source does not have tables (for example, a character-separated file data source).

Specified by:
tables in class DataSource
Returns:
a possibly null iterator over all tables

tablesUsedInReport

public java.util.Iterator tablesUsedInReport()
Description copied from class: DataSource
Returns an iterator over all tables actually used in the report, or null if the data source does not have tables (for example, a character-separated file data source).

Specified by:
tablesUsedInReport in class DataSource
Returns:
a possibly null iterator over all tables used in the report

columns

public java.util.Iterator columns()
Description copied from class: DataSource
Returns an iterator over all columns.

Specified by:
columns in class DataSource
Returns:
an iterator over all columns

execute

public DataCursor execute()
                   throws java.sql.SQLException
Specified by:
execute in class DataSource
Throws:
java.sql.SQLException

storesLowerCaseIdentifiers

public boolean storesLowerCaseIdentifiers()

storesUpperCaseIdentifiers

public boolean storesUpperCaseIdentifiers()

quoteString

public java.lang.String quoteString()

initializeConnection

public void initializeConnection()
                          throws java.lang.ClassNotFoundException,
                                 java.lang.InstantiationException,
                                 java.lang.IllegalAccessException,
                                 UserCancellationException
Initializes the connection to the database.

Throws:
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
UserCancellationException

getConnection

public java.sql.Connection getConnection()
Returns a connection to the database.

Returns:
a connection to the database

reset

public void reset(java.lang.String driverClassName,
                  java.lang.String connInfo,
                  java.lang.String dbName,
                  java.lang.String username,
                  java.lang.String password)
           throws java.sql.SQLException,
                  java.lang.ClassNotFoundException,
                  java.lang.InstantiationException,
                  java.lang.IllegalAccessException,
                  UserCancellationException
Reset key instance variables, closes current connection, and "reloads" all table information (compares new info with existing info and complains if any existing info is not in the new info).

Note: if the connection we currently have was created by this object, we close it. If the connection was handed to us, we do not close the connection.

Parameters:
driverClassName - database driver class name
connInfo - database connection info string
dbName - database name
username - the user name to use when logging in to the database
Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
UserCancellationException

loadAllTables

protected void loadAllTables()
                      throws java.sql.SQLException
Loads information about all tables in the database. If no tables are found when using the database schema name, try again with a null schema name.

Throws:
java.sql.SQLException

loadTablesUsingSchemaNameAndTypes

protected void loadTablesUsingSchemaNameAndTypes(java.sql.DatabaseMetaData dbmd,
                                                 java.lang.String schema,
                                                 java.lang.String[] objectTypes)
                                          throws java.sql.SQLException
Loads our list of tables using a database meta data object and a schema name. The schema name may be null.

Parameters:
dbmd - the database meta data object
objectTypes - a list of database object types
schema - the schema name; may be null
Throws:
java.sql.SQLException

getDriverClassName

public java.lang.String getDriverClassName()
Returns the driver class name.

Returns:
the driver class name

setDriverClassName

protected void setDriverClassName(java.lang.String newDriverClassName)
Sets the driver class name.

Parameters:
newDriverClassName - the driver class name

getConnectionInfo

public java.lang.String getConnectionInfo()
Returns the connection info string.

Returns:
the connection info string

setConnectionInfo

protected void setConnectionInfo(java.lang.String newConnectionInfo)
Sets the connection info string.

Parameters:
newConnectionInfo - the connection info string

getName

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

Returns:
the database name

setName

protected void setName(java.lang.String newName)
Sets the name.

Parameters:
newName - the new name

getUserName

public java.lang.String getUserName()
Returns the user name.

Returns:
the user name

setUserName

public void setUserName(java.lang.String newUserName)
Sets the user name.

Parameters:
newUserName - the new user name

getPassword

public java.lang.String getPassword()
Returns the password.

Returns:
the password

setPassword

public void setPassword(java.lang.String newPassword)
Sets the password.

Parameters:
newPassword - the new password

doWriteXML

protected void doWriteXML(XMLWriter out)
Writes this database and all its tables as an XML tag.

Specified by:
doWriteXML in class DataSource
Parameters:
out - a writer that knows how to write XML