1 22 23 24 package com.mchange.v2.sql; 25 26 import java.sql.*; 27 import com.mchange.v2.log.*; 28 29 import java.util.Date ; 30 import java.text.DateFormat ; 31 import java.text.SimpleDateFormat ; 32 import com.mchange.lang.ThrowableUtils; 33 import com.mchange.v2.lang.VersionUtils; 34 35 public final class SqlUtils 36 { 37 final static MLogger logger = MLog.getLogger( SqlUtils.class ); 38 39 final static DateFormat tsdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSSS"); 41 42 public final static String DRIVER_MANAGER_USER_PROPERTY = "user"; 43 public final static String DRIVER_MANAGER_PASSWORD_PROPERTY = "password"; 44 45 public static String escapeBadSqlPatternChars(String s) 46 { 47 StringBuffer sb = new StringBuffer (s); 48 for (int i = 0, len = sb.length(); i < len; ++i) 49 if (sb.charAt(i) == '\'') 50 { 51 sb.insert(i, '\''); 52 ++len; 53 i+=2; 54 } 55 return sb.toString(); 56 } 57 58 public synchronized static String escapeAsTimestamp( Date date ) 59 { return "{ts '" + tsdf.format( date ) + "'}"; } 60 61 public static SQLException toSQLException(Throwable t) 62 { return toSQLException(null, t ); } 63 64 public static SQLException toSQLException(String msg, Throwable t) 65 { return toSQLException(msg, null, t);} 66 67 public static SQLException toSQLException(String msg, String sqlState, Throwable t) 68 { 69 if (t instanceof SQLException) 70 { 71 if (Debug.DEBUG && 72 Debug.TRACE == Debug.TRACE_MAX && 73 logger.isLoggable( MLevel.FINER )) 74 { 75 SQLException s = (SQLException) t; 76 StringBuffer tmp = new StringBuffer (255); 77 tmp.append("Attempted to convert SQLException to SQLException. Leaving it alone."); 78 tmp.append(" [SQLState: "); 79 tmp.append( s.getSQLState() ); 80 tmp.append("; errorCode: " ); 81 tmp.append( s.getErrorCode() ); 82 tmp.append(']'); 83 if (msg != null) 84 tmp.append(" Ignoring suggested message: '" + msg + "'."); 85 logger.log( MLevel.FINER, tmp.toString(), t ); 86 87 SQLException s2 = s; 88 while ((s2 = s2.getNextException()) != null) 89 logger.log( MLevel.FINER, "Nested SQLException or SQLWarning: ", s2 ); 90 } 91 return (SQLException) t; 92 } 93 else 94 { 95 if (Debug.DEBUG) 96 { 97 if ( logger.isLoggable( MLevel.FINE ) ) 99 logger.log( MLevel.FINE, "Converting Throwable to SQLException...", t ); 100 } 101 102 if (msg == null) 103 msg = "An SQLException was provoked by the following failure: " + t.toString(); 104 if ( VersionUtils.isAtLeastJavaVersion14() ) 105 { 106 SQLException out = new SQLException(msg); 107 out.initCause( t ); 108 return out; 109 } 110 else 111 return new SQLException( msg + System.getProperty( "line.separator" ) + 112 "[Cause: " + ThrowableUtils.extractStackTrace(t) + ']', sqlState); 113 } 114 } 115 116 private SqlUtils() 117 {} 118 } 119 | Popular Tags |