Added loadSchema().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1790 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-02-20 21:58:50 +00:00
parent 95f9f9405b
commit 2624e2d9e1
+55 -1
View File
@@ -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:
*
* <pre>
* if (!JDBCUtil.tableExists(conn, "MONKEYS")) {
* Log.info("Creating monkeys repository schema...");
* JDBCUtil.loadSchema(conn, "src/sql/monkeys.sql");
* }
* </pre>
*/
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. <em>Note:</em> the table name is case sensitive.