Debian Codegraph @

src/main/org/h2/message/TraceObject.java

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