From 8061ef093988e62150a7b2fa136e21848b26c3e2 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 13 Feb 2001 00:25:14 +0000 Subject: [PATCH] Created base class for JORA-based database repository implementations that takes care of establishing a JORA session based on JDBC configuration properties and provides a few other useful patterns. git-svn-id: https://samskivert.googlecode.com/svn/trunk@38 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/jdbc/Makefile | 4 +- .../com/samskivert/jdbc/MySQLRepository.java | 41 ++++++++ .../java/com/samskivert/jdbc/Repository.java | 93 +++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 projects/samskivert/src/java/com/samskivert/jdbc/MySQLRepository.java create mode 100644 projects/samskivert/src/java/com/samskivert/jdbc/Repository.java diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/Makefile b/projects/samskivert/src/java/com/samskivert/jdbc/Makefile index f87205ae..d81e29bf 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/Makefile +++ b/projects/samskivert/src/java/com/samskivert/jdbc/Makefile @@ -1,9 +1,11 @@ # -# $Id: Makefile,v 1.1 2000/12/06 00:27:30 mdb Exp $ +# $Id: Makefile,v 1.2 2001/02/13 00:25:14 mdb Exp $ ROOT = ../../.. SRCS = \ JDBCUtil.java \ + MySQLRepository.java \ + Repository.java \ include $(ROOT)/build/Makefile.java diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/MySQLRepository.java b/projects/samskivert/src/java/com/samskivert/jdbc/MySQLRepository.java new file mode 100644 index 00000000..b27ef135 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/jdbc/MySQLRepository.java @@ -0,0 +1,41 @@ +// +// $Id: MySQLRepository.java,v 1.1 2001/02/13 00:25:14 mdb Exp $ + +package com.samskivert.jdbc; + +import java.sql.*; +import java.util.Properties; + +/** + * The MySQL repository class provides functionality useful to repository + * implementations that make use of MySQL as their underlying database. + */ +public abstract class MySQLRepository extends Repository +{ + /** + * Constructs a MySQL repository implementation with the supplied + * configuration properties. + */ + public MySQLRepository (Properties props) + throws SQLException + { + super(props); + } + + /** + * @return the most recent ID generated by an insert into an + * AUTO_INCREMENT table. + */ + protected int lastInsertedId () + throws SQLException + { + // we have to do this by hand. alas all is not roses. + Statement stmt = _session.connection.createStatement(); + ResultSet rs = stmt.executeQuery("select LAST_INSERT_ID()"); + if (rs.next()) { + return rs.getInt(1); + } else { + return -1; + } + } +} diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java b/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java new file mode 100644 index 00000000..6afb79af --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java @@ -0,0 +1,93 @@ +// +// $Id: Repository.java,v 1.1 2001/02/13 00:25:14 mdb Exp $ + +package com.samskivert.jdbc; + +import java.sql.*; +import java.util.List; +import java.util.Properties; + +import com.samskivert.util.*; +import jora.*; + +/** + * The repository class provides basic functionality upon which to build + * an interface to a repository of information stored in a database (a + * table or set of tables). + * + *

These services are based on the JORA Java/RDBMS interoperability + * package. + */ +public abstract class Repository +{ + /** + * Creates the repository and opens music database. A properties + * object should be supplied with the following fields: + * + *

+     * driver=[jdbc driver class]
+     * url=[jdbc driver url]
+     * username=[jdbc username]
+     * password=[jdbc password]
+     * 
+ * + * @param props a properties object containing the configuration + * parameters for the repository. + */ + public Repository (Properties props) + throws SQLException + { + // create our JORA session + String dclass = + requireProp(props, "driver", "No driver class specified."); + _session = new Session(dclass); + + // connect the session to the database + String url = + requireProp(props, "url", "No driver url specified."); + String username = + requireProp(props, "username", "No driver username specified."); + String password = + requireProp(props, "password", "No driver password specified."); + _session.open(url, username, password); + + // set auto-commit to false + _session.connection.setAutoCommit(false); + + // create our table objects + createTables(); + } + + /** + * After the database session is begun, this function will be called + * to give the repository implementation the opportunity to create its + * table objects. As this is done during the initialization of the + * repository, the implementation has a chance to fail the entire + * repository creation process if the necessary tables do not exist. + */ + protected abstract void createTables () throws SQLException; + + protected static String requireProp (Properties props, + String name, String errmsg) + throws SQLException + { + String value = props.getProperty(name); + if (StringUtil.blank(value)) { + throw new SQLException(errmsg); + } + return value; + } + + /** + * A repository should be shutdown before the calling code disposes of + * it. This allows the repository to cleanly terminate the underlying + * database services. + */ + public void shutdown () + throws SQLException + { + _session.close(); + } + + protected Session _session; +}