📄 sqlerror.java
字号:
MysqlErrorNumbers.ER_WRONG_VALUE_FOR_VAR), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WRONG_TYPE_FOR_VAR), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_CANT_USE_OPTION_HERE), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_NOT_SUPPORTED_YET), "42000"); mysqlToSql99State.put(new Integer(MysqlErrorNumbers.ER_WRONG_FK_DEF), "42000"); mysqlToSql99State.put(new Integer(MysqlErrorNumbers.ER_OPERAND_COLUMNS), "21000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_SUBQUERY_NO_1_ROW), "21000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_ILLEGAL_REFERENCE), "42S22"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_DERIVED_MUST_HAVE_ALIAS), "42000"); mysqlToSql99State.put(new Integer(MysqlErrorNumbers.ER_SELECT_REDUCED), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_TABLENAME_NOT_ALLOWED_HERE), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_NOT_SUPPORTED_AUTH_MODE), "08004"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_SPATIAL_CANT_HAVE_NULL), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_COLLATION_CHARSET_MISMATCH), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WARN_TOO_FEW_RECORDS), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WARN_TOO_MANY_RECORDS), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WARN_NULL_TO_NOTNULL), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WARN_DATA_OUT_OF_RANGE), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WARN_DATA_TRUNCATED), "01000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WRONG_NAME_FOR_INDEX), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WRONG_NAME_FOR_CATALOG), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_UNKNOWN_STORAGE_ENGINE), "42000"); } static String get(String stateCode) { return (String) sqlStateMessages.get(stateCode); } /** * Map MySQL error codes to X/Open or SQL-92 error codes * * @param errno the MySQL error code * * @return the corresponding X/Open or SQL-92 error code */ static String mysqlToSqlState(int errno, boolean useSql92States) { if (useSql92States) { return mysqlToSql99(errno); } return mysqlToXOpen(errno); } private static String mysqlToXOpen(int errno) { Integer err = new Integer(errno); if (mysqlToSqlState.containsKey(err)) { return (String) mysqlToSqlState.get(err); } return SQL_STATE_GENERAL_ERROR; } private static String mysqlToSql99(int errno) { Integer err = new Integer(errno); if (mysqlToSql99State.containsKey(err)) { return (String) mysqlToSql99State.get(err); } return "HY000"; } /** * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances. * * If 'forTruncationOnly' is true, only looks for truncation warnings, * and actually throws DataTruncation as an exception. * * @param connection the connection to use for getting warnings. * * @return the SQLWarning chain (or null if no warnings) * * @throws SQLException if the warnings could not be retrieved */ static SQLWarning convertShowWarningsToSQLWarnings(Connection connection) throws SQLException { return convertShowWarningsToSQLWarnings(connection, 0, false); } /** * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances. * * If 'forTruncationOnly' is true, only looks for truncation warnings, * and actually throws DataTruncation as an exception. * * @param connection the connection to use for getting warnings. * @param warningCountIfKnown the warning count (if known), otherwise set it to 0. * @param forTruncationOnly if this method should only scan for data truncation warnings * * @return the SQLWarning chain (or null if no warnings) * * @throws SQLException if the warnings could not be retrieved, or if data truncation is * being scanned for and truncations were found. */ static SQLWarning convertShowWarningsToSQLWarnings(Connection connection, int warningCountIfKnown, boolean forTruncationOnly) throws SQLException { java.sql.Statement stmt = null; java.sql.ResultSet warnRs = null; SQLWarning currentWarning = null; try { if (warningCountIfKnown < 100) { stmt = connection.createStatement(); if (stmt.getMaxRows() != 0) { stmt.setMaxRows(0); } } else { // stream large warning counts stmt = connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE); } /* +---------+------+---------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------+ | Warning | 1265 | Data truncated for column 'field1' at row 1 | +---------+------+---------------------------------------------+ */ warnRs = stmt.executeQuery("SHOW WARNINGS"); //$NON-NLS-1$ while (warnRs.next()) { int code = warnRs.getInt("Code"); //$NON-NLS-1$ if (forTruncationOnly) { if (code == 1265) { DataTruncation newTruncation = new MysqlDataTruncation(warnRs.getString( "Message"), 0, false, false, 0, 0); //$NON-NLS-1$ if (currentWarning == null) { currentWarning = newTruncation; } else { currentWarning.setNextWarning(newTruncation); currentWarning = newTruncation; } } } else { String level = warnRs.getString("Level"); //$NON-NLS-1$ String message = warnRs.getString("Message"); //$NON-NLS-1$ SQLWarning newWarning = new SQLWarning(message, SQLError.mysqlToSqlState(code, connection.getUseSqlStateCodes()), code); if (currentWarning == null) { currentWarning = newWarning; } else { currentWarning.setNextWarning(newWarning); currentWarning = newWarning; } } } if (forTruncationOnly && (currentWarning != null)) { throw currentWarning; } return currentWarning; } finally { SQLException reThrow = null; if (warnRs != null) { try { warnRs.close(); } catch (SQLException sqlEx) { reThrow = sqlEx; } } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { // ideally, we'd use chained exceptions here, // but we still support JDK-1.2.x with this driver // which doesn't have them.... reThrow = sqlEx; } } if (reThrow != null) { throw reThrow; } } } public static void dumpSqlStatesMappingsAsXml() throws Exception { TreeMap allErrorNumbers = new TreeMap(); Map mysqlErrorNumbersToNames = new HashMap(); Integer errorNumber = null; // // First create a list of all 'known' error numbers that // are mapped. // for (Iterator mysqlErrorNumbers = mysqlToSql99State.keySet().iterator(); mysqlErrorNumbers.hasNext();) { errorNumber = (Integer) mysqlErrorNumbers.next(); allErrorNumbers.put(errorNumber, errorNumber); } for (Iterator mysqlErrorNumbers = mysqlToSqlState.keySet().iterator(); mysqlErrorNumbers.hasNext();) { errorNumber = (Integer) mysqlErrorNumbers.next(); allErrorNumbers.put(errorNumber, errorNumber); } // // Now create a list of the actual MySQL error numbers we know about // java.lang.reflect.Field[] possibleFields = MysqlErrorNumbers.class.getDeclaredFields(); for (int i = 0; i < possibleFields.length; i++) { String fieldName = possibleFields[i].getName(); if (fieldName.startsWith("ER_")) { mysqlErrorNumbersToNames.put(possibleFields[i].get(null), fieldName); } } System.out.println("<ErrorMappings>"); for (Iterator allErrorNumbersIter = allErrorNumbers.keySet().iterator(); allErrorNumbersIter.hasNext();) { errorNumber = (Integer) allErrorNumbersIter.next(); String sql92State = mysqlToSql99(errorNumber.intValue()); String oldSqlState = mysqlToXOpen(errorNumber.intValue()); System.out.println(" <ErrorMapping mysqlErrorNumber=\"" + errorNumber + "\" mysqlErrorName=\"" + mysqlErrorNumbersToNames.get(errorNumber) + "\" legacySqlState=\"" + ((oldSqlState == null) ? "" : oldSqlState) + "\" sql92SqlState=\"" + ((sql92State == null) ? "" : sql92State) + "\"/>"); } System.out.println("</ErrorMappings>"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -