File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\message\TraceObject.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.message;
8
9 import java.math.BigDecimal;
10 import java.sql.SQLException;
11 import java.util.Map;
12 import org.h2.util.StringUtils;
13
14 /**
15 * The base class for objects that can print trace information about themselves.
16 */
17 public class TraceObject {
18
19 /**
20 * The trace type id for callable statements.
21 */
22 protected static final int CALLABLE_STATEMENT = 0;
23
24 /**
25 * The trace type id for connections.
26 */
27 protected static final int CONNECTION = 1;
28
29 /**
30 * The trace type id for database meta data objects.
31 */
32 protected static final int DATABASE_META_DATA = 2;
33
34 /**
35 * The trace type id for prepared statements.
36 */
37 protected static final int PREPARED_STATEMENT = 3;
38
39 /**
40 * The trace type id for result sets.
41 */
42 protected static final int RESULT_SET = 4;
43
44 /**
45 * The trace type id for result set meta data objects.
46 */
47 protected static final int RESULT_SET_META_DATA = 5;
48
49 /**
50 * The trace type id for savepoint objects.
51 */
52 protected static final int SAVEPOINT = 6;
53
54 /**
55 * The trace type id for sql exceptions.
56 */
57 protected static final int SQL_EXCEPTION = 7;
58
59 /**
60 * The trace type id for statements.
61 */
62 protected static final int STATEMENT = 8;
63
64 /**
65 * The trace type id for blobs.
66 */
67 protected static final int BLOB = 9;
68
69 /**
70 * The trace type id for clobs.
71 */
72 protected static final int CLOB = 10;
73
74 /**
75 * The trace type id for parameter meta data objects.
76 */
77 protected static final int PARAMETER_META_DATA = 11;
78
79 /**
80 * The trace type id for data sources.
81 */
82 protected static final int DATA_SOURCE = 12;
83
84 /**
85 * The trace type id for XA data sources.
86 */
87 protected static final int XA_DATA_SOURCE = 13;
88
89 /**
90 * The trace type id for XA resources.
91 */
92 protected static final int XA_RESOURCE = 14;
93
94 /**
95 * The trace type id for transaction ids.
96 */
97 protected static final int XID = 15;
98
99 /**
100 * The trace type id for array objects.
101 */
102 protected static final int ARRAY = 16;
103
104 private static final int LAST = ARRAY + 1;
105 private static final int[] ID = new int[LAST];
106 private static final String[] PREFIX = {
107 "call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta",
108 "ds", "xads", "xares", "xid", "ar"
109 };
110
111 /**
112 * The trace module used by this object.
113 */
114 protected Trace trace;
115
116 private int traceType;
117 private int id;
118
119 /**
120 * Set the options to use when writing trace message.
121 *
122 * @param trace the trace object
123 * @param type the trace object type
124 * @param id the trace object id
125 */
126 protected void setTrace(Trace trace, int type, int id) {
127 this.trace = trace;
128 this.traceType = type;
129 this.id = id;
130 }
131
132 /**
133 * Get the trace object.
134 *
135 * @return the trace object
136 */
137 protected Trace getTrace() {
138 return trace;
139 }
140
141 /**
142 * INTERNAL
143 */
144 public int getTraceId() {
145 return id;
146 }
147
148 /**
149 * INTERNAL
150 */
151 public String getTraceObjectName() {
152 return PREFIX[traceType] + id;
153 }
154
155 /**
156 * Get the next trace object id for this object type.
157 *
158 * @param type the object type
159 * @return the new trace object id
160 */
161 protected static int getNextId(int type) {
162 return ID[type]++;
163 }
164
165 /**
166 * Check if the debug trace level is enabled.
167 *
168 * @return true if it is
169 */
170 protected boolean isDebugEnabled() {
171 return trace.isDebugEnabled();
172 }
173
174 /**
175 * Check if info trace level is enabled.
176 *
177 * @return true if it is
178 */
179 protected boolean isInfoEnabled() {
180 return trace.isInfoEnabled();
181 }
182
183 /**
184 * Write trace information as an assignment in the form
185 * className prefixId = objectName.value.
186 *
187 * @param className the class name of the result
188 * @param newType the prefix type
189 * @param newId the trace object id of the created object
190 * @param value the value to assign this new object to
191 */
192 protected void debugCodeAssign(String className, int newType, int newId, String value) {
193 if (trace.isDebugEnabled()) {
194 trace.debugCode(className + " " + PREFIX[newType] + newId + " = " + getTraceObjectName() + "." + value + ";");
195 }
196 }
197
198 /**
199 * Write trace information as a method call in the form
200 * objectName.methodName().
201 *
202 * @param methodName the method name
203 */
204 protected void debugCodeCall(String methodName) {
205 if (trace.isDebugEnabled()) {
206 trace.debugCode(getTraceObjectName() + "." + methodName + "();");
207 }
208 }
209
210 /**
211 * Write trace information as a method call in the form
212 * objectName.methodName(param) where the parameter is formatted as a long
213 * value.
214 *
215 * @param methodName the method name
216 * @param param one single long parameter
217 */
218 protected void debugCodeCall(String methodName, long param) {
219 if (trace.isDebugEnabled()) {
220 trace.debugCode(getTraceObjectName() + "." + methodName + "(" + param + ");");
221 }
222 }
223
224 /**
225 * Write trace information as a method call in the form
226 * objectName.methodName(param) where the parameter is formatted as a Java
227 * string.
228 *
229 * @param methodName the method name
230 * @param param one single string parameter
231 */
232 protected void debugCodeCall(String methodName, String param) {
233 if (trace.isDebugEnabled()) {
234 trace.debugCode(getTraceObjectName() + "." + methodName + "(" + quote(param) + ");");
235 }
236 }
237
238 /**
239 * Write trace information in the form objectName.text.
240 *
241 * @param text the trace text
242 */
243 protected void debugCode(String text) {
244 if (trace.isDebugEnabled()) {
245 trace.debugCode(getTraceObjectName() + "." + text);
246 }
247 }
248
249 /**
250 * Format a string as a Java string literal.
251 *
252 * @param s the string to convert
253 * @return the Java string literal
254 */
255 protected static String quote(String s) {
256 return StringUtils.quoteJavaString(s);
257 }
258
259 /**
260 * Format a time to the Java source code that represents this object.
261 *
262 * @param x the time to convert
263 * @return the Java source code
264 */
265 protected static String quoteTime(java.sql.Time x) {
266 if (x == null) {
267 return "null";
268 }
269 return "Time.valueOf(\"" + x.toString() + "\")";
270 }
271
272 /**
273 * Format a timestamp to the Java source code that represents this object.
274 *
275 * @param x the timestamp to convert
276 * @return the Java source code
277 */
278 protected static String quoteTimestamp(java.sql.Timestamp x) {
279 if (x == null) {
280 return "null";
281 }
282 return "Timestamp.valueOf(\"" + x.toString() + "\")";
283 }
284
285 /**
286 * Format a date to the Java source code that represents this object.
287 *
288 * @param x the date to convert
289 * @return the Java source code
290 */
291 protected static String quoteDate(java.sql.Date x) {
292 if (x == null) {
293 return "null";
294 }
295 return "Date.valueOf(\"" + x.toString() + "\")";
296 }
297
298 /**
299 * Format a big decimal to the Java source code that represents this object.
300 *
301 * @param x the big decimal to convert
302 * @return the Java source code
303 */
304 protected static String quoteBigDecimal(BigDecimal x) {
305 if (x == null) {
306 return "null";
307 }
308 return "new BigDecimal(\"" + x.toString() + "\")";
309 }
310
311 /**
312 * Format a byte array to the Java source code that represents this object.
313 *
314 * @param x the byte array to convert
315 * @return the Java source code
316 */
317 protected static String quoteBytes(byte[] x) {
318 if (x == null) {
319 return "null";
320 }
321 return "org.h2.util.StringUtils.convertHexToBytes(\"" + StringUtils.convertBytesToHex(x) + "\")";
322 }
323
324 /**
325 * Format a string array to the Java source code that represents this
326 * object.
327 *
328 * @param s the string array to convert
329 * @return the Java source code
330 */
331 protected static String quoteArray(String[] s) {
332 return StringUtils.quoteJavaStringArray(s);
333 }
334
335 /**
336 * Format an int array to the Java source code that represents this object.
337 *
338 * @param s the int array to convert
339 * @return the Java source code
340 */
341 protected static String quoteIntArray(int[] s) {
342 return StringUtils.quoteJavaIntArray(s);
343 }
344
345 /**
346 * Format a map to the Java source code that represents this object.
347 *
348 * @param map the map to convert
349 * @return the Java source code
350 */
351 protected static String quoteMap(Map<String, Class<?>> map) {
352 if (map == null) {
353 return "null";
354 }
355 if (map.size() == 0) {
356 return "new Map()";
357 }
358 return "new Map() /* " + map.toString() + " */";
359 }
360
361 /**
362 * Log an exception and convert it to a SQL exception if required.
363 *
364 * @param ex the exception
365 * @return the SQL exception object
366 */
367 protected SQLException logAndConvert(Exception ex) {
368 SQLException e = DbException.toSQLException(ex);
369 if (trace == null) {
370 TraceSystem.traceThrowable(e);
371 } else {
372 int errorCode = e.getErrorCode();
373 if (errorCode >= 23000 && errorCode < 24000) {
374 trace.info(e, "exception");
375 } else {
376 trace.error(e, "exception");
377 }
378 }
379 return e;
380 }
381
382 /**
383 * Get and throw a SQL exception meaning this feature is not supported.
384 *
385 * @param message the message
386 * @return never returns normally
387 * @throws SQLException the exception
388 */
389 protected SQLException unsupported(String message) throws SQLException {
390 try {
391 throw DbException.getUnsupportedException(message);
392 } catch (Exception e) {
393 throw logAndConvert(e);
394 }
395 }
396
397 }
398