KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > sql > SqlUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

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 JavaDoc;
30 import java.text.DateFormat JavaDoc;
31 import java.text.SimpleDateFormat JavaDoc;
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     // protected by SqlUtils.class' lock
40
final static DateFormat JavaDoc tsdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.SSSS");
41
42     public final static String JavaDoc DRIVER_MANAGER_USER_PROPERTY = "user";
43     public final static String JavaDoc DRIVER_MANAGER_PASSWORD_PROPERTY = "password";
44
45     public static String JavaDoc escapeBadSqlPatternChars(String JavaDoc s)
46     {
47     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc escapeAsTimestamp( Date JavaDoc date )
59     { return "{ts '" + tsdf.format( date ) + "'}"; }
60
61     public static SQLException toSQLException(Throwable JavaDoc t)
62     { return toSQLException(null, t ); }
63
64     public static SQLException toSQLException(String JavaDoc msg, Throwable JavaDoc t)
65     { return toSQLException(msg, null, t);}
66
67     public static SQLException toSQLException(String JavaDoc msg, String JavaDoc sqlState, Throwable JavaDoc 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 JavaDoc tmp = new StringBuffer JavaDoc(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             //t.printStackTrace();
98
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