📄 rdbms.java
字号:
/** * Creates a unique index on the specified column in the specified table. * * @param table The table name. * @param column The column name. **/ public void createUniqueIndex(String table, String column) throws SQLException { createIndex(table, new String[] {column}, true); } /** * Creates an index on the specified columns in the specified table. * * @param table The table name. * @param columns The column names * @param unique Flag indicating whether the index should be a unique index. **/ public void createIndex(String table, String[] columns, boolean unique) throws SQLException { if (columns.length == 0) { throw new IllegalArgumentException( "columns must contain at least one column name"); } StringBuffer query = new StringBuffer(64); query.append("CREATE "); if (unique) { query.append("UNIQUE "); } query.append("INDEX ").append(getIndexName(table, columns)); query.append(" ON ").append(table); query.append(" (").append(columns[0]); for (int i = 1; i < columns.length; i++) { query.append(", ").append(columns[i]); } query.append(")"); executeUpdate(query.toString()); } /** * Drops the index on the specified column name from the specified table. * * @param table The table name. * @param column The column name. **/ public void dropIndex(String table, String column) throws SQLException { dropIndex(table, new String[] {column}); } /** * Drops the index on the specified columns from the specified table. * * @param table The table name. * @param columns The column names. **/ public void dropIndex(String table, String[] columns) throws SQLException { executeUpdate("DROP INDEX " + getIndexName(table, columns)); } /** * Creates an index name based on the name of the column and table that * it's supposed to index. **/ public String getIndexName(String table, String column) { return getIndexName(table, new String[] {column}); } /** * Creates an index name based on the name of the columns and table that * it's supposed to index. **/ public String getIndexName(String table, String[] columns) { StringBuffer name = new StringBuffer(32); name.append(table).append("_").append(columns[0]); for (int i = 1; i < columns.length; i++) { name.append("_").append(columns[i]); } name.append("_idx"); return name.toString(); }/*----------------------------------------+| Methods for tables |+----------------------------------------*/ /** * Checks whether a table with the specified name exists. **/ public boolean tableExists(String tableName) throws SQLException { boolean tableExists = false; Connection con = getConnection(); try { Statement st = con.createStatement(); try { ResultSet rs = st.executeQuery("select count(*) from " + tableName); tableExists = rs.next(); rs.close(); } catch (SQLException e) { // ignore, assume this means that the table doesn't exist } finally { st.close(); } } finally { con.close(); } return tableExists; } /** * Optimizes a table. The actual action taken depends on the database. **/ public void optimizeTable(String tableName, int modifiedRowsCount) throws SQLException { //ThreadLog.trace("optimizeTable(\"" + tableName + "\", " + modifiedRowsCount + "\")"); if (modifiedRowsCount <= 0) { return; } // Get row count Integer i = (Integer)_tableRowCounts.get(tableName); int rowCount = (i == null) ? _getRowCount(tableName) : i.intValue(); // Get modified row count i = (Integer)_tableModRowCounts.get(tableName); int modRowCount = (i == null) ? 0 : i.intValue(); // Update modified row count modRowCount += modifiedRowsCount; //ThreadLog.trace("rowCount=" + rowCount + "; modRowCount=" + modRowCount); if (modRowCount > 10000 || // More than 10000 rows changed rowCount / modRowCount <= 2) // More than 50% of the rows changed { optimizeTable(tableName); rowCount = _getRowCount(tableName); modRowCount = 0; } // Update statistics _tableRowCounts.put(tableName, new Integer(rowCount)); _tableModRowCounts.put(tableName, new Integer(modRowCount)); //ThreadLog.trace("optimizeTable() done"); } protected int _getRowCount(String tableName) throws SQLException { Connection con = getConnection(); try { Statement st = con.createStatement(); try { ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM " + tableName); rs.next(); int result = rs.getInt(1); rs.close(); return result; } finally { st.close(); } } finally { con.close(); } } /** * Optimizes a table. The actual action taken depends on the database. **/ public void optimizeTable(String tableName) throws SQLException { // There is no default for this in SQL92. } /** * Clears a table. **/ public final void clearTable(String tableName) throws SQLException { _clearTable(tableName); _tableRowCounts.put(tableName, new Integer(0)); _tableModRowCounts.put(tableName, new Integer(0)); } protected void _clearTable(String tableName) throws SQLException { executeUpdate("DELETE FROM " + tableName); } /** * Drops a table. **/ public final void dropTable(String tableName) throws SQLException { _dropTable(tableName); _tableRowCounts.remove(tableName); _tableModRowCounts.remove(tableName); } protected void _dropTable(String tableName) throws SQLException { executeUpdate("DROP TABLE " + tableName); } /** * Renames a table. **/ public final void renameTable(String currentTableName, String newTableName) throws SQLException { _renameTable(currentTableName, newTableName); Integer rowCount = (Integer)_tableRowCounts.remove(currentTableName); if (rowCount != null) { _tableRowCounts.put(newTableName, rowCount); } Integer modRowCount = (Integer)_tableModRowCounts.remove(currentTableName); if (modRowCount != null) { _tableModRowCounts.put(newTableName, modRowCount); } } protected void _renameTable(String currentTableName, String newTableName) throws SQLException { executeUpdate("ALTER TABLE " + currentTableName + " RENAME TO " + newTableName); } public void renameTableColumn(String tableName, String currentColumnName, String newColumnName, String columnSignature) throws SQLException { executeUpdate("ALTER TABLE " + tableName + " RENAME COLUMN " + currentColumnName + " TO " + newColumnName); } /** * Copies rows from one table to another. * @return the number of rows that were copied. **/ public int copyRows(String sourceTable, String targetTable) throws SQLException { return executeUpdate( "INSERT INTO " + targetTable + " SELECT * FROM " + sourceTable); } /** * Copies distinct rows from one table to another (duplicates are suppressed). * @return the number of rows that were copied. **/ public int copyDistinctRows(String sourceTable, String targetTable) throws SQLException { return executeUpdate( "INSERT INTO " + targetTable + " SELECT DISTINCT * FROM " + sourceTable); }/*----------------------------------------+| Other methods |+----------------------------------------*/ /** * Converts a boolean value to a string representation that can be used * in a query for this RDBMS. **/ public String convertBoolean(boolean b) { return b ? TRUE : FALSE; } /** * Escapes any special characters in the specifed string such that it can * be used in a query for this RDBMS. * * @return The original string with escape codes for any special characters. **/ public String escapeString(String s) { String result = StringUtil.gsub("\\", "\\\\", s); result = StringUtil.gsub("'", "\\'", result); return result; } /** * Should return <tt>true</tt> if the database converts empty string to * NULL. Default return value is <tt>false</tt>. **/ public boolean emptyStringIsNull() { return false; } /** * Indicates whether the database supports LIKE "..." ESCAPE '\' constructions. **/ public boolean supportsLikeEscapeClause() { return _supportsLikeEscapeClause; } /** * Returns the character string that can be used to escape special * characters in patterns. The default character is <tt>\\</tt>, e.g. * the pattern <tt>\\_%</tt> matches strings with an underscore as * first character. **/ public String getSearchStringEscape() { return _searchStringEscape; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -