Screw having .sql files, let's just define the schema in code where everything

else lives anyway.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1791 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-02-20 22:41:16 +00:00
parent 2624e2d9e1
commit 58cbc0e496
+16 -40
View File
@@ -209,50 +209,26 @@ public class JDBCUtil
} }
/** /**
* Loads in the specified schema configuration file. The file data is * Used to programatically create a database table. Does nothing if the
* loaded as a resource from the classpath. This is used for the * table already exists.
* 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) public static void createTableIfMissing (
Connection conn, String table, String[] definition)
throws SQLException throws SQLException
{ {
InputStream schemaIn = if (tableExists(conn, table)) {
JDBCUtil.class.getClassLoader().getResourceAsStream(path); return;
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(); Statement stmt = conn.createStatement();
try { try {
stmt.executeUpdate(schema); stmt.executeUpdate("create table " + table + "(" +
StringUtil.join(definition, ",") + ")");
} finally { } finally {
close(stmt); close(stmt);
} }
Log.info("Database table '" + table + "' created.");
} }
/** /**
@@ -423,8 +399,8 @@ public class JDBCUtil
close(stmt); close(stmt);
} }
Log.info("Database column '" + cname + "' added to table '" + table + Log.info("Database column '" + cname + "' added to table '" +
"'."); table + "'.");
} }
/** /**
@@ -468,7 +444,7 @@ public class JDBCUtil
stmt = conn.prepareStatement(update); stmt = conn.prepareStatement(update);
if (stmt.executeUpdate() == 1) { if (stmt.executeUpdate() == 1) {
Log.info("Database index '" + cname + "' removed from " + Log.info("Database index '" + cname + "' removed from " +
"table '" + table + "'."); "table '" + table + "'.");
} }
} finally { } finally {
close(stmt); close(stmt);
@@ -488,7 +464,7 @@ public class JDBCUtil
stmt = conn.prepareStatement(update); stmt = conn.prepareStatement(update);
if (stmt.executeUpdate() == 1) { if (stmt.executeUpdate() == 1) {
Log.info("Database index '" + iname + "' removed from " + Log.info("Database index '" + iname + "' removed from " +
"table '" + table + "'."); "table '" + table + "'.");
} }
} finally { } finally {
close(stmt); close(stmt);
@@ -542,7 +518,7 @@ public class JDBCUtil
close(stmt); close(stmt);
} }
Log.info("Database index '" + idx_name + "' added to table '" + table + Log.info("Database index '" + idx_name + "' added to table '" +
"'"); table + "'");
} }
} }