diff --git a/src/java/com/samskivert/jdbc/JDBCUtil.java b/src/java/com/samskivert/jdbc/JDBCUtil.java index 346112c0..97812221 100644 --- a/src/java/com/samskivert/jdbc/JDBCUtil.java +++ b/src/java/com/samskivert/jdbc/JDBCUtil.java @@ -20,8 +20,15 @@ package com.samskivert.jdbc; +import java.io.InputStream; import java.io.UnsupportedEncodingException; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.commons.io.IOUtils; import com.samskivert.Log; import com.samskivert.io.PersistenceException; @@ -201,6 +208,53 @@ public class JDBCUtil return StringUtil.replace(jigger(text), "'", "\\'"); } + /** + * Loads in the specified schema configuration file. The file data is + * loaded as a resource from the classpath. This is used for the + * autocreation of repository tables. For example: + * + *
+ * if (!JDBCUtil.tableExists(conn, "MONKEYS")) {
+ * Log.info("Creating monkeys repository schema...");
+ * JDBCUtil.loadSchema(conn, "src/sql/monkeys.sql");
+ * }
+ *
+ */
+ public static void loadSchema (Connection conn, String path)
+ throws SQLException
+ {
+ InputStream schemaIn =
+ JDBCUtil.class.getClassLoader().getResourceAsStream(path);
+ if (schemaIn == null) {
+ throw new SQLException("Unable to load '" + path +
+ "' using class loader.");
+ }
+ loadSchema(conn, schemaIn);
+ }
+
+ /**
+ * A version of {@link #loadSchema(Connection,String)} that allows the
+ * caller to obtain the schema data.
+ */
+ public static void loadSchema (Connection conn, InputStream schemaIn)
+ throws SQLException
+ {
+ String schema;
+ try {
+ schema = IOUtils.toString(schemaIn);
+ } catch (Exception e) {
+ String errmsg = "Error reading schema [src=" + schemaIn + "].";
+ throw (SQLException)new SQLException(errmsg).initCause(e);
+ }
+
+ Statement stmt = conn.createStatement();
+ try {
+ stmt.executeUpdate(schema);
+ } finally {
+ close(stmt);
+ }
+ }
+
/**
* Returns true if the table with the specified name exists, false if
* it does not. Note: the table name is case sensitive.