net.sourceforge.mayfly.Database Class Reference

List of all members.

Detailed Description

A Database contains a set of tables, but can be managed as easily as any other object.

Here's an example of what your test might look like:

 private Database database;
 public void setUp() throws Exception {
     database = new Database();
 }

 public void tearDown() throws Exception {
     // Nulling variables can hurt performance in general, but 
     // might be helpful here because JUnit keeps test case
     // objects around until the end of a test run.
     database = null;
 }

 public void testData() throws Exception {
     connection = database.openConnection();
     . . .
     connection.close();
 }
 

In the above example, we first call openConnection() and then perform all operations via that connection. There are also a variety of methods to operate on the database directly (for example, execute(String). These are basically convenience methods; most/all of these operations can also be done via connections. These methods take per-connection settings (for example the current schema as set by SET SCHEMA, or the auto-commit flag) from a default connection; that is they are shared with other calls which use the default connection, but not with connections opened explicitly (for example via openConnection()).


Public Member Functions

 Database ()
 Create an empty database (one with no tables).
 Database (DataStore store)
 Create a database starting with the contents of a net.sourceforge.mayfly.datastore.DataStore, which you'd normally get from the dataStore() method of another Database object.
int execute (String sql) throws SQLException
 Execute an SQL command which does not return results.
void executeScript (Reader script) throws MayflyException
 Execute a series of SQL commands separated by semicolons.
ResultSet query (String sql) throws SQLException
 Execute an SQL command which returns results.
Set tables ()
 List your tables, as names.
Set schemas ()
 Return the schema names for this Database.
List columnNames (String tableName) throws SQLException
 Return the column names in the given table.
int rowCount (String tableName) throws SQLException
 Number of rows in given table.
Connection openConnection () throws SQLException
 Open a JDBC connection.
DataStore dataStore ()
 Take a snapshot of this database.


Member Function Documentation

List net.sourceforge.mayfly.Database.columnNames String  tableName  )  throws SQLException
 

Return the column names in the given table.

If a future version of Mayfly implements this functionality in java.sql.DatabaseMetaData, this method may go away or become some kind of convenience method.

DataStore net.sourceforge.mayfly.Database.dataStore  ) 
 

Take a snapshot of this database.

Specifically, return the data store, which is an immutable object containing all the data, and table definitions, for this database. Because the data store is immutable, one might store it in a constant and use it from multiple tests. Here's an example:

    static final DataStore standardSetup = makeData();

    private static DataStore makeData() {
        try {
            Database original = new Database();
            original.execute("create table foo (a integer)");
            original.execute("insert into foo(a) values(6)");
            return original.dataStore();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    Database database;
    public void setUp() {
        database = new Database(standardSetup);
    }
    

See also:
JdbcDriver#create(DataStore)

int net.sourceforge.mayfly.Database.execute String  sql  )  throws SQLException
 

Execute an SQL command which does not return results.

This is similar to the JDBC java.sql.Statement.executeUpdate(java.lang.String) but is more convenient if you have a Database instance around.

Returns:
Number of rows changed.

void net.sourceforge.mayfly.Database.executeScript Reader  script  )  throws MayflyException
 

Execute a series of SQL commands separated by semicolons.

This method closes the reader when done.

Connection net.sourceforge.mayfly.Database.openConnection  )  throws SQLException
 

Open a JDBC connection.

This is similar to the JDBC java.sql.DriverManager.getConnection(java.lang.String) but is based on this Database, rather than the static Database used in the JDBC case.

ResultSet net.sourceforge.mayfly.Database.query String  sql  )  throws SQLException
 

Execute an SQL command which returns results.

This is similar to the JDBC java.sql.Statement.executeQuery(java.lang.String) but is more convenient if you have a Database instance around.

int net.sourceforge.mayfly.Database.rowCount String  tableName  )  throws SQLException
 

Number of rows in given table.

This is a convenience method. Your production code will almost surely be counting rows (if it needs to at all) via java.sql.ResultSet (or the SQL COUNT expression). But this method may be convenient in tests.

Set net.sourceforge.mayfly.Database.schemas  ) 
 

Return the schema names for this Database.

This returned list only includes schemas which you have explicitly created. The anonymous schema is not included in the returned list, but will always exist.

If a future version of Mayfly implements this functionality in java.sql.DatabaseMetaData, this method is likely to remain, as being more convenient than DatabaseMetaData.

Set net.sourceforge.mayfly.Database.tables  ) 
 

List your tables, as names.

The returned list only includes tables which you have explicitly created; there are no tables here which are for Mayfly's own use. If you have more than one schema, only tables in the current schema (as set by SET SCHEMA) are returned.

If a future version of Mayfly implements this functionality in java.sql.DatabaseMetaData, this method is likely to remain, as being more convenient than DatabaseMetaData.


The documentation for this class was generated from the following file:
For more information on Mayfly see mayfly.sourceforge.net.