📄 cmssetupdb.java
字号:
* Returns an optional warning message if needed, <code>null</code> if not.<p>
*
* @param db the selected database key
*
* @return html warning, or <code>null</code> if no warning
*/
public String checkVariables(String db) {
StringBuffer html = new StringBuffer(512);
if (m_con == null) {
return null; // prior error, trying to get a connection
}
SQLException exception = null;
if (db.equals("mysql")) { // just for 4.0, > is not needed, < is not supported.
String statement = "SELECT @@max_allowed_packet;";
Statement stmt = null;
ResultSet rs = null;
long map = 0;
try {
stmt = m_con.createStatement();
rs = stmt.executeQuery(statement);
if (rs.next()) {
map = rs.getLong(1);
}
} catch (SQLException e) {
exception = e;
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// ignore
}
}
}
if (exception == null) {
if (map > 0) {
html.append("MySQL system variable <code>'max_allowed_packet'</code> is set to ");
html.append(map);
html.append(" Bytes.<p>\n");
}
html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n");
if (map < 15 * 1024 * 1024) {
m_errors.addElement("<b>Your <code>'max_allowed_packet'</code> variable is set to less than 16Mb ("
+ map
+ ").</b>\n"
+ "The recommended value for running OpenCms is 16Mb."
+ "Please change your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).\n");
}
}
}
if (exception != null || db.equals("mysql_3")) {
html.append("<i>OpenCms was not able to detect the value of your <code>'max_allowed_packet'</code> variable.</i><p>\n");
html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n");
html.append("<b>The recommended value for running OpenCms is 16Mb, please set it in your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).</b>\n");
if (exception != null) {
html.append(CmsException.getStackTraceAsString(exception));
}
}
if (html.length() == 0) {
return null;
}
return html.toString();
}
/**
* Calls an update script.<p>
*
* @param updateScript the update script code
* @param replacers the replacers to use in the script code
*/
public void updateDatabase(String updateScript, Map replacers) {
StringReader reader = new StringReader(updateScript);
executeSql(reader, replacers, true);
}
/**
* Calls an update script.<p>
*
* @param updateScript the update script code
* @param replacers the replacers to use in the script code
* @param abortOnError indicates if the script is aborted if an error occurs
*/
public void updateDatabase(String updateScript, Map replacers, boolean abortOnError) {
StringReader reader = new StringReader(updateScript);
executeSql(reader, replacers, abortOnError);
}
/**
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
try {
closeConnection();
} catch (Throwable t) {
// ignore
}
super.finalize();
}
/**
* Internal method to parse and execute a setup script.<p>
*
* @param inputReader an input stream reader on the setup script
* @param replacers the replacements to perform in the script
* @param abortOnError if a error occurs this flag indicates if to continue or to abort
*/
private void executeSql(Reader inputReader, Map replacers, boolean abortOnError) {
String statement = "";
LineNumberReader reader = null;
String line = null;
// parse the setup script
try {
reader = new LineNumberReader(inputReader);
line = null;
while (true) {
line = reader.readLine();
if (line == null) {
break;
}
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
// comment! Skip rest of the line
if (currentToken.startsWith("#")) {
break;
}
// not to be executed
if (currentToken.startsWith("prompt")) {
break;
}
// add token to query
statement += " " + currentToken;
// query complete (terminated by ';')
if (currentToken.endsWith(";")) {
// cut of ';' at the end
statement = statement.substring(0, (statement.length() - 1));
// normal statement, execute it
try {
if (replacers != null) {
statement = replaceTokens(statement, replacers);
executeStatement(statement);
} else {
executeStatement(statement);
}
} catch (SQLException e) {
if (!abortOnError) {
if (m_errorLogging) {
m_errors.addElement("Error executing SQL statement: " + statement);
m_errors.addElement(CmsException.getStackTraceAsString(e));
}
} else {
throw e;
}
}
// reset
statement = "";
}
}
statement += " \n";
}
} catch (SQLException e) {
if (m_errorLogging) {
m_errors.addElement("Error executing SQL statement: " + statement);
m_errors.addElement(CmsException.getStackTraceAsString(e));
}
} catch (Exception e) {
if (m_errorLogging) {
m_errors.addElement("Error parsing database setup SQL script in line: " + line);
m_errors.addElement(CmsException.getStackTraceAsString(e));
}
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
// noop
}
}
}
/**
* Internal method to parse and execute a setup script.<p>
*
* @param databaseKey the database variant of the script
* @param sqlScript the name of the script
* @param replacers the replacements to perform in the script
* @param abortOnError if a error occurs this flag indicates if to continue or to abort
*/
private void executeSql(String databaseKey, String sqlScript, Map replacers, boolean abortOnError) {
String filename = null;
InputStreamReader reader = null;
try {
filename = m_basePath
+ "setup"
+ File.separator
+ "database"
+ File.separator
+ databaseKey
+ File.separator
+ sqlScript;
executeSql(new FileReader(filename), replacers, abortOnError);
} catch (FileNotFoundException e) {
if (m_errorLogging) {
m_errors.addElement("Database setup SQL script not found: " + filename);
m_errors.addElement(CmsException.getStackTraceAsString(e));
}
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
// noop
}
}
}
/**
* Creates and executes a database statment from a String.<p>
*
* @param statement the database statement
*
* @throws SQLException if something goes wrong
*/
private void executeStatement(String statement) throws SQLException {
Statement stmt = null;
try {
stmt = m_con.createStatement();
stmt.execute(statement);
} finally {
if (stmt != null) {
stmt.close();
}
}
}
/**
* Replaces tokens "${xxx}" in a specified SQL query.<p>
*
* @param sql a SQL query
* @param replacers a Map with values keyed by "${xxx}" tokens
* @return the SQl query with all "${xxx}" tokens replaced
*/
private String replaceTokens(String sql, Map replacers) {
Iterator keys = replacers.keySet().iterator();
while (keys.hasNext()) {
String key = (String)keys.next();
String value = (String)replacers.get(key);
sql = CmsStringUtil.substitute(sql, key, value);
}
return sql;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -