1 5 package org.h2.message; 6 7 import java.io.FileWriter ; 8 import java.io.IOException ; 9 import java.io.PrintWriter ; 10 import java.math.BigDecimal ; 11 import java.sql.SQLException ; 12 13 import org.h2.engine.Constants; 14 import org.h2.util.StringUtils; 15 16 21 public class TraceObject { 22 public static final int CALLABLE_STATEMENT = 0, CONNECTION = 1, DATABASE_META_DATA = 2, 23 PREPARED_STATEMENT = 3, RESULT_SET = 4, RESULT_SET_META_DATA = 5, 24 SAVEPOINT = 6, SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10, 25 PARAMETER_META_DATA = 11; 26 public static final int DATASOURCE = 12, XA_DATASOURCE = 13, XID = 14; 27 28 private static int LAST = XID + 1; 29 private Trace trace; 30 private static final int[] ID = new int[LAST]; 31 private static final String [] PREFIX = { 32 "call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta", 33 "ds", "xads", "xid" 34 }; 35 private int type, id; 36 37 protected void setTrace(Trace trace, int type, int id) { 38 this.trace = trace; 39 this.type = type; 40 this.id = id; 41 } 42 43 protected int getTraceId() { 44 return id; 45 } 46 47 50 public String toString() { 51 return PREFIX[type] + id ; 52 } 53 54 protected int getNextId(int type) { 55 return ID[type]++; 56 } 57 58 protected boolean debug() { 59 return trace.debug(); 60 } 61 62 protected boolean info() { 63 return trace.info(); 64 } 65 66 protected Trace getTrace() { 67 return trace; 68 } 69 70 protected void debugCodeAssign(String className, int type, int id) { 71 if(!trace.debug()) { 72 return; 73 } 74 trace.debugCode(className + " " + toString() + " = "); 75 } 76 77 protected void infoCodeAssign(String className, int type, int id) { 78 if(!trace.info()) { 79 return; 80 } 81 trace.infoCode(className + " " + toString() + " = "); 82 } 83 84 protected void debugCodeCall(String text) { 85 if(!trace.debug()) { 86 return; 87 } 88 trace.debugCode(toString() + "." + text + "();"); 89 } 90 91 protected void debugCodeCall(String text, long param) { 92 if(!trace.debug()) { 93 return; 94 } 95 trace.debugCode(toString() + "." + text + "("+param+");"); 96 } 97 98 protected void debugCodeCall(String text, String param) { 99 if(!trace.debug()) { 100 return; 101 } 102 trace.debugCode(toString() + "." + text + "("+quote(param)+");"); 103 } 104 105 protected void debugCode(String text) { 106 if(!trace.debug()) { 107 return; 108 } 109 trace.debugCode(toString() + "." + text); 110 } 111 112 protected String quote(String s) { 113 return StringUtils.quoteJavaString(s); 114 } 115 116 protected String quoteTime(java.sql.Time x) { 117 if(x == null) { 118 return "null"; 119 } 120 return "Time.valueOf(\"" + x.toString() + "\")"; 121 } 122 123 protected String quoteTimestamp(java.sql.Timestamp x) { 124 if(x == null) { 125 return "null"; 126 } 127 return "Timestamp.valueOf(\"" + x.toString() + "\")"; 128 } 129 130 protected String quoteDate(java.sql.Date x) { 131 if(x == null) { 132 return "null"; 133 } 134 return "Date.valueOf(\"" + x.toString() + "\")"; 135 } 136 137 protected String quoteBigDecimal(BigDecimal x) { 138 if(x == null) { 139 return "null"; 140 } 141 return "new BigDecimal(\"" + x.toString() + "\")"; 142 } 143 144 protected String quoteBytes(byte[] x) { 145 if(x == null) { 146 return "null"; 147 } 148 return "new byte[" + x.length + "]"; 149 } 150 151 protected String quoteArray(String [] s) { 152 return StringUtils.quoteJavaStringArray(s); 153 } 154 155 protected String quoteIntArray(int[] s) { 156 return StringUtils.quoteJavaIntArray(s); 157 } 158 159 protected SQLException logAndConvert(Throwable e) { 160 if(Constants.LOG_ALL_ERRORS) { 161 synchronized(this.getClass()) { 162 try { 164 FileWriter writer = new FileWriter (Constants.LOG_ALL_ERRORS_FILE, true); 165 PrintWriter p = new PrintWriter (writer); 166 e.printStackTrace(p); 167 p.close(); 168 writer.close(); 169 } catch(IOException e2) { 170 e2.printStackTrace(); 171 } 172 } 173 } 174 if(trace == null) { 175 TraceSystem.traceThrowable(e); 176 } else { 177 if(e instanceof SQLException ) { 178 trace.error("SQLException", e); 179 return (SQLException )e; 180 } else { 181 trace.error("Uncaught Exception", e); 182 } 183 } 184 return Message.convert(e); 185 } 186 187 } 188 | Popular Tags |