001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.sql;
024:
025: import java.sql.*;
026: import com.mchange.v2.log.*;
027:
028: import java.util.Date;
029: import java.text.DateFormat;
030: import java.text.SimpleDateFormat;
031: import com.mchange.lang.ThrowableUtils;
032: import com.mchange.v2.lang.VersionUtils;
033:
034: public final class SqlUtils {
035: final static MLogger logger = MLog.getLogger(SqlUtils.class);
036:
037: // protected by SqlUtils.class' lock
038: final static DateFormat tsdf = new SimpleDateFormat(
039: "yyyy-MM-dd HH:mm:ss.SSSS");
040:
041: public final static String DRIVER_MANAGER_USER_PROPERTY = "user";
042: public final static String DRIVER_MANAGER_PASSWORD_PROPERTY = "password";
043:
044: public static String escapeBadSqlPatternChars(String s) {
045: StringBuffer sb = new StringBuffer(s);
046: for (int i = 0, len = sb.length(); i < len; ++i)
047: if (sb.charAt(i) == '\'') {
048: sb.insert(i, '\'');
049: ++len;
050: i += 2;
051: }
052: return sb.toString();
053: }
054:
055: public synchronized static String escapeAsTimestamp(Date date) {
056: return "{ts '" + tsdf.format(date) + "'}";
057: }
058:
059: public static SQLException toSQLException(Throwable t) {
060: return toSQLException(null, t);
061: }
062:
063: public static SQLException toSQLException(String msg, Throwable t) {
064: return toSQLException(msg, null, t);
065: }
066:
067: public static SQLException toSQLException(String msg,
068: String sqlState, Throwable t) {
069: if (t instanceof SQLException) {
070: if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX
071: && logger.isLoggable(MLevel.FINER)) {
072: SQLException s = (SQLException) t;
073: StringBuffer tmp = new StringBuffer(255);
074: tmp
075: .append("Attempted to convert SQLException to SQLException. Leaving it alone.");
076: tmp.append(" [SQLState: ");
077: tmp.append(s.getSQLState());
078: tmp.append("; errorCode: ");
079: tmp.append(s.getErrorCode());
080: tmp.append(']');
081: if (msg != null)
082: tmp.append(" Ignoring suggested message: '" + msg
083: + "'.");
084: logger.log(MLevel.FINER, tmp.toString(), t);
085:
086: SQLException s2 = s;
087: while ((s2 = s2.getNextException()) != null)
088: logger.log(MLevel.FINER,
089: "Nested SQLException or SQLWarning: ", s2);
090: }
091: return (SQLException) t;
092: } else {
093: if (Debug.DEBUG) {
094: //t.printStackTrace();
095: if (logger.isLoggable(MLevel.FINE))
096: logger.log(MLevel.FINE,
097: "Converting Throwable to SQLException...",
098: t);
099: }
100:
101: if (msg == null)
102: msg = "An SQLException was provoked by the following failure: "
103: + t.toString();
104: if (VersionUtils.isAtLeastJavaVersion14()) {
105: SQLException out = new SQLException(msg);
106: out.initCause(t);
107: return out;
108: } else
109: return new SQLException(msg
110: + System.getProperty("line.separator")
111: + "[Cause: "
112: + ThrowableUtils.extractStackTrace(t) + ']',
113: sqlState);
114: }
115: }
116:
117: private SqlUtils() {
118: }
119: }
|