1 19 20 package org.netbeans.modules.db.sql.loader; 21 22 import java.io.IOException ; 23 import java.text.NumberFormat ; 24 import org.netbeans.modules.db.sql.execute.SQLExecutionLogger; 25 import org.netbeans.modules.db.sql.execute.SQLExecutionResult; 26 import org.netbeans.modules.db.sql.execute.StatementInfo; 27 import org.openide.ErrorManager; 28 import org.openide.cookies.LineCookie; 29 import org.openide.text.Line; 30 import org.openide.util.NbBundle; 31 import org.openide.windows.IOProvider; 32 import org.openide.windows.InputOutput; 33 import org.openide.windows.OutputEvent; 34 import org.openide.windows.OutputListener; 35 import org.openide.windows.OutputWriter; 36 37 41 public class SQLExecutionLoggerImpl implements SQLExecutionLogger { 42 43 private final LineCookie lineCookie; 44 private final InputOutput inputOutput; 45 46 private boolean inputOutputSelected = false; 47 private int errorCount; 48 49 public SQLExecutionLoggerImpl(String displayName, LineCookie lineCookie) { 50 this.lineCookie = lineCookie; 51 52 String ioName = NbBundle.getMessage(SQLEditorSupport.class, "LBL_SQLFileExecution", displayName); 53 inputOutput = IOProvider.getDefault().getIO(ioName, true); 54 } 55 56 public void log(SQLExecutionResult result) { 57 if (result.getException() != null) { 58 logException(result); 59 } else { 60 logSuccess(result); 61 } 62 } 63 64 public void finish(long executionTime) { 65 OutputWriter writer = inputOutput.getOut(); 66 writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutionFinished", 67 String.valueOf(millisecondsToSeconds(executionTime)), 68 String.valueOf(errorCount))); 69 writer.println(""); } 71 72 public void cancel() { 73 OutputWriter writer = inputOutput.getErr(); 74 writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutionCancelled")); 75 writer.println(""); } 77 78 public void close() { 79 inputOutput.closeInputOutput(); 80 } 81 82 private void logException(SQLExecutionResult result) { 83 errorCount++; 84 85 if (!inputOutputSelected) { 86 inputOutputSelected = true; 87 inputOutput.select(); 88 } 89 90 OutputWriter writer = inputOutput.getErr(); 91 92 writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ErrorCodeStateMessage", 93 String.valueOf(result.getException().getErrorCode()), 94 result.getException().getSQLState(), 95 result.getException().getMessage())); 96 printLineColumn(writer, result.getStatementInfo(), true); 97 writer.println(""); } 99 100 private void logSuccess(SQLExecutionResult result) { 101 OutputWriter writer = inputOutput.getOut(); 102 103 String executionTimeStr = millisecondsToSeconds(result.getExecutionTime()); 104 String successLine = null; 105 if (result.getRowCount() >= 0) { 106 successLine = NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutedSuccessfullyTimeRows", 107 String.valueOf(executionTimeStr), 108 String.valueOf(result.getRowCount())); 109 } else { 110 successLine = NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutedSuccessfullyTime", 111 String.valueOf(executionTimeStr)); 112 } 113 writer.println(successLine); 114 printLineColumn(writer, result.getStatementInfo(), false); 115 writer.println(""); } 117 118 private void printLineColumn(OutputWriter writer, StatementInfo statementInfo, boolean hyperlink) { 119 String lineColumn = NbBundle.getMessage(SQLEditorSupport.class, "LBL_LineColumn", 120 String.valueOf(statementInfo.getStartLine() + 1), 121 String.valueOf(statementInfo.getStartColumn() + 1)); 122 try { 123 if (hyperlink) { 124 writer.println(lineColumn, new Hyperlink(statementInfo.getStartLine(), statementInfo.getStartColumn())); 125 } else { 126 writer.println(lineColumn); 127 } 128 } catch (IOException e) { 129 ErrorManager.getDefault().notify(e); 130 } 131 } 132 133 private String millisecondsToSeconds(long ms) { 134 NumberFormat fmt = NumberFormat.getInstance(); 135 fmt.setMaximumFractionDigits(3); 136 return fmt.format(ms / 1000.0); 137 } 138 139 public void logResultSetException(Exception e) { 140 inputOutput.select(); 141 OutputWriter writer = inputOutput.getErr(); 142 143 writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ResultSetErrorDetailed", 144 e.getMessage())); 145 } 146 147 150 private final class Hyperlink implements OutputListener { 151 152 private final int line; 153 private final int column; 154 155 public Hyperlink(int line, int column) { 156 this.line = line; 157 this.column = column; 158 } 159 160 public void outputLineSelected(OutputEvent ev) { 161 goToLine(false); 162 } 163 164 public void outputLineCleared(OutputEvent ev) { 165 } 166 167 public void outputLineAction(OutputEvent ev) { 168 goToLine(true); 169 } 170 171 private void goToLine(boolean focus) { 172 Line l = lineCookie.getLineSet().getOriginal(line); 173 if (!l.isDeleted()) { 174 l.show(focus ? Line.SHOW_GOTO : Line.SHOW_TRY_SHOW, column); 175 } 176 } 177 } 178 } 179 | Popular Tags |