📄 schemamanager.java
字号:
Log.info(LocaleUtils.getLocalizedString("upgrade.database.missing_schema",
Arrays.asList(schemaKey)));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.missing_schema",
Arrays.asList(schemaKey)));
// Resource will be like "/database/wildfire_hsqldb.sql"
String resourceName = schemaKey + "_" +
DbConnectionManager.getDatabaseType() + ".sql";
InputStream resource = resourceLoader.loadResource(resourceName);
if (resource == null) {
return false;
}
try {
executeSQLScript(con, resource);
}
catch (Exception e) {
Log.error(e);
return false;
}
finally {
try {
resource.close();
}
catch (Exception e) {
// Ignore.
}
}
Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.success"));
return true;
}
// Must have a version of the schema that needs to be upgraded.
else {
// The database is an old version that needs to be upgraded.
Log.info(LocaleUtils.getLocalizedString("upgrade.database.old_schema",
Arrays.asList(currentVersion, schemaKey, requiredVersion)));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.old_schema",
Arrays.asList(currentVersion, schemaKey, requiredVersion)));
// If the database type is unknown, we don't know how to upgrade it.
if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.unknown) {
Log.info(LocaleUtils.getLocalizedString("upgrade.database.unknown_db"));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.unknown_db"));
return false;
}
// Upgrade scripts for interbase are not maintained.
else if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.interbase) {
Log.info(LocaleUtils.getLocalizedString("upgrade.database.interbase_db"));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.interbase_db"));
return false;
}
// Run all upgrade scripts until we're up to the latest schema.
for (int i = currentVersion + 1; i <= requiredVersion; i++) {
InputStream resource = getUpgradeResource(resourceLoader, i, schemaKey);
if (resource == null) {
continue;
}
try {
executeSQLScript(con, resource);
}
catch (Exception e) {
Log.error(e);
return false;
}
finally {
try {
resource.close();
}
catch (Exception e) {
// Ignore.
}
}
}
Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.success"));
return true;
}
}
private InputStream getUpgradeResource(ResourceLoader resourceLoader, int upgradeVersion,
String schemaKey)
{
InputStream resource = null;
if ("wildfire".equals(schemaKey)) {
// Resource will be like "/database/upgrade/6/wildfire_hsqldb.sql"
String path = JiveGlobals.getHomeDirectory() + File.separator + "resources" +
File.separator + "database" + File.separator + "upgrade" + File.separator +
upgradeVersion;
String filename = schemaKey + "_" + DbConnectionManager.getDatabaseType() + ".sql";
File file = new File(path, filename);
try {
resource = new FileInputStream(file);
}
catch (FileNotFoundException e) {
// If the resource is null, the specific upgrade number is not available.
}
}
else {
String resourceName = "upgrade/" + upgradeVersion + "/" + schemaKey + "_" +
DbConnectionManager.getDatabaseType() + ".sql";
resource = resourceLoader.loadResource(resourceName);
}
return resource;
}
/**
* Executes a SQL script.
*
* @param con database connection.
* @param resource an input stream for the script to execute.
* @throws IOException if an IOException occurs.
* @throws SQLException if an SQLException occurs.
*/
private static void executeSQLScript(Connection con, InputStream resource) throws IOException,
SQLException
{
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(resource));
boolean done = false;
while (!done) {
StringBuilder command = new StringBuilder();
while (true) {
String line = in.readLine();
if (line == null) {
done = true;
break;
}
// Ignore comments and blank lines.
if (isSQLCommandPart(line)) {
command.append(" ").append(line);
}
if (line.trim().endsWith(";")) {
break;
}
}
// Send command to database.
if (!done && !command.toString().equals("")) {
// Remove last semicolon when using Oracle to prevent "invalid character error"
if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType
.oracle) {
command.deleteCharAt(command.length() - 1);
}
Statement stmt = con.createStatement();
stmt.execute(command.toString());
stmt.close();
}
}
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
Log.error(e);
}
}
}
}
private static abstract class ResourceLoader {
public abstract InputStream loadResource(String resourceName);
}
/**
* Returns true if a line from a SQL schema is a valid command part.
*
* @param line the line of the schema.
* @return true if a valid command part.
*/
private static boolean isSQLCommandPart(String line) {
line = line.trim();
if (line.equals("")) {
return false;
}
// Check to see if the line is a comment. Valid comment types:
// "//" is HSQLDB
// "--" is DB2 and Postgres
// "#" is MySQL
// "REM" is Oracle
// "/*" is SQLServer
return !(line.startsWith("//") || line.startsWith("--") || line.startsWith("#") ||
line.startsWith("REM") || line.startsWith("/*") || line.startsWith("*"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -