001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.message;
007:
008: import java.io.PrintWriter;
009: import java.io.Writer;
010: import java.math.BigDecimal;
011: import java.sql.SQLException;
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import org.h2.constant.SysProperties;
016: import org.h2.expression.ParameterInterface;
017: import org.h2.util.ByteUtils;
018: import org.h2.util.FileUtils;
019: import org.h2.util.ObjectArray;
020: import org.h2.util.StringUtils;
021:
022: /**
023: * The base class for objects that can print trace information about themselves.
024: */
025: public class TraceObject {
026: public static final int CALLABLE_STATEMENT = 0, CONNECTION = 1,
027: DATABASE_META_DATA = 2, PREPARED_STATEMENT = 3,
028: RESULT_SET = 4, RESULT_SET_META_DATA = 5, SAVEPOINT = 6,
029: SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10,
030: PARAMETER_META_DATA = 11;
031: public static final int DATA_SOURCE = 12, XA_DATA_SOURCE = 13,
032: XID = 14, ARRAY = 15;
033:
034: private static final int LAST = ARRAY + 1;
035: private Trace trace;
036: private static final int[] ID = new int[LAST];
037: private static final String[] PREFIX = { "call", "conn", "dbMeta",
038: "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob",
039: "pMeta", "ds", "xads", "xid", "ar" };
040: private int type, id;
041:
042: protected void setTrace(Trace trace, int type, int id) {
043: this .trace = trace;
044: this .type = type;
045: this .id = id;
046: }
047:
048: protected int getTraceId() {
049: return id;
050: }
051:
052: /**
053: * INTERNAL
054: */
055: public String getTraceObjectName() {
056: return PREFIX[type] + id;
057: }
058:
059: protected int getNextId(int type) {
060: return ID[type]++;
061: }
062:
063: protected boolean debug() {
064: return trace.debug();
065: }
066:
067: protected boolean info() {
068: return trace.info();
069: }
070:
071: protected Trace getTrace() {
072: return trace;
073: }
074:
075: protected void debugCodeAssign(String className, int type, int id,
076: String value) {
077: if (trace.debug()) {
078: trace.debugCode(className + " " + PREFIX[type] + id + " = "
079: + getTraceObjectName() + "." + value + ";");
080: }
081: }
082:
083: protected void debugCodeCall(String text) {
084: if (trace.debug()) {
085: trace.debugCode(getTraceObjectName() + "." + text + "();");
086: }
087: }
088:
089: protected void debugCodeCall(String text, long param) {
090: if (trace.debug()) {
091: trace.debugCode(getTraceObjectName() + "." + text + "("
092: + param + ");");
093: }
094: }
095:
096: protected void debugCodeCall(String text, String param) {
097: if (trace.debug()) {
098: trace.debugCode(getTraceObjectName() + "." + text + "("
099: + quote(param) + ");");
100: }
101: }
102:
103: protected void debugCode(String text) {
104: if (trace.debug()) {
105: trace.debugCode(getTraceObjectName() + "." + text);
106: }
107: }
108:
109: protected String quote(String s) {
110: return StringUtils.quoteJavaString(s);
111: }
112:
113: protected String quoteTime(java.sql.Time x) {
114: if (x == null) {
115: return "null";
116: }
117: return "Time.valueOf(\"" + x.toString() + "\")";
118: }
119:
120: protected String quoteTimestamp(java.sql.Timestamp x) {
121: if (x == null) {
122: return "null";
123: }
124: return "Timestamp.valueOf(\"" + x.toString() + "\")";
125: }
126:
127: protected String quoteDate(java.sql.Date x) {
128: if (x == null) {
129: return "null";
130: }
131: return "Date.valueOf(\"" + x.toString() + "\")";
132: }
133:
134: protected String quoteBigDecimal(BigDecimal x) {
135: if (x == null) {
136: return "null";
137: }
138: return "new BigDecimal(\"" + x.toString() + "\")";
139: }
140:
141: protected String quoteBytes(byte[] x) {
142: if (x == null) {
143: return "null";
144: }
145: return "org.h2.util.ByteUtils.convertStringToBytes(\""
146: + ByteUtils.convertBytesToString(x) + "\")";
147: }
148:
149: protected String quoteArray(String[] s) {
150: return StringUtils.quoteJavaStringArray(s);
151: }
152:
153: protected String quoteIntArray(int[] s) {
154: return StringUtils.quoteJavaIntArray(s);
155: }
156:
157: protected String quoteMap(Map map) {
158: if (map == null) {
159: return "null";
160: }
161: if (map.size() == 0) {
162: return "new Map()";
163: }
164: StringBuffer buff = new StringBuffer("new Map() /* ");
165: try {
166: // Map<String, Class>
167: for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
168: Map.Entry entry = (Map.Entry) it.next();
169: String key = (String) entry.getKey();
170: buff.append(key);
171: buff.append(':');
172: Class clazz = (Class) entry.getValue();
173: buff.append(clazz.getName());
174: }
175: } catch (Exception e) {
176: buff.append(e.toString() + ": " + map.toString());
177: }
178: buff.append("*/");
179: return buff.toString();
180: }
181:
182: protected SQLException logAndConvert(Throwable e) {
183: if (SysProperties.LOG_ALL_ERRORS) {
184: synchronized (TraceObject.class) {
185: // e.printStackTrace();
186: try {
187: Writer writer = FileUtils.openFileWriter(
188: SysProperties.LOG_ALL_ERRORS_FILE, true);
189: PrintWriter p = new PrintWriter(writer);
190: e.printStackTrace(p);
191: p.close();
192: writer.close();
193: } catch (Exception e2) {
194: e2.printStackTrace();
195: }
196: }
197: }
198: if (trace == null) {
199: TraceSystem.traceThrowable(e);
200: } else {
201: if (e instanceof SQLException) {
202: trace.error("SQLException", e);
203: return (SQLException) e;
204: } else {
205: trace.error("Uncaught Exception", e);
206: }
207: }
208: return Message.convert(e);
209: }
210:
211: /**
212: * INTERNAL
213: */
214: public static String toString(String sql, ObjectArray params) {
215: StringBuffer buff = new StringBuffer(sql);
216: if (params != null && params.size() > 0) {
217: buff.append(" {");
218: for (int i = 0; i < params.size(); i++) {
219: try {
220: ParameterInterface p = (ParameterInterface) params
221: .get(i);
222: String s = p.getParamValue().getSQL();
223: if (i > 0) {
224: buff.append(", ");
225: }
226: buff.append(i + 1);
227: buff.append(": ");
228: buff.append(s);
229: } catch (SQLException e) {
230: buff.append("/* ");
231: buff.append(i + 1);
232: buff.append(": ");
233: buff.append(e.toString());
234: buff.append("*/ ");
235: }
236: }
237: buff.append("};");
238: }
239: return buff.toString();
240:
241: }
242:
243: }
|