001: /*
002: * DbmsOutput.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.oracle;
013:
014: import java.sql.CallableStatement;
015: import java.sql.Connection;
016: import java.sql.SQLException;
017: import java.sql.Types;
018:
019: import workbench.log.LogMgr;
020:
021: public class DbmsOutput {
022: private Connection conn;
023: private boolean enabled = false;
024: private long lastSize;
025:
026: public DbmsOutput(Connection aConn) throws SQLException {
027: this .conn = aConn;
028: }
029:
030: public void enable(long size) throws SQLException {
031: if (this .enabled && size == this .lastSize)
032: return;
033: CallableStatement enableStatement = conn
034: .prepareCall("begin dbms_output.enable(:1); end;");
035: enableStatement.setLong(1, size);
036: enableStatement.executeUpdate();
037: enableStatement.close();
038: this .enabled = true;
039: this .lastSize = size;
040: LogMgr.logDebug("DbmsOutput.enable()",
041: "Support for DBMS_OUTPUT package enabled (max size="
042: + size + ")");
043: }
044:
045: public void enable() throws SQLException {
046: this .enable(-1);
047: }
048:
049: /*
050: * disable simply executes the dbms_output.disable
051: */
052: public void disable() throws SQLException {
053: CallableStatement disableStatement = conn
054: .prepareCall("begin dbms_output.disable; end;");
055: disableStatement.executeUpdate();
056: disableStatement.close();
057: this .enabled = false;
058: }
059:
060: /*
061: * getResult() does most of the work. It loops over
062: * all of the dbms_output data.
063: */
064: public String getResult() throws SQLException {
065: if (!this .enabled)
066: return "";
067: CallableStatement showOutputStatement = conn
068: .prepareCall("declare "
069: + " l_line varchar2(255); "
070: + " l_done number; "
071: + " l_buffer long; "
072: + "begin "
073: + " loop "
074: + " exit when length(l_buffer)+255 > :maxbytes OR l_done = 1; "
075: + " dbms_output.get_line( l_line, l_done ); "
076: + " l_buffer := l_buffer || l_line || chr(10); "
077: + " end loop; " + " :done := l_done; "
078: + " :buffer := l_buffer; " + "end;");
079:
080: StringBuilder result = new StringBuilder(1024);
081: try {
082: showOutputStatement.registerOutParameter(2, Types.INTEGER);
083: showOutputStatement.registerOutParameter(3, Types.VARCHAR);
084: for (;;) {
085: showOutputStatement.setInt(1, 32000);
086: showOutputStatement.executeUpdate();
087: result.append(showOutputStatement.getString(3).trim());
088: if (showOutputStatement.getInt(2) == 1)
089: break;
090: }
091: } finally {
092: try {
093: showOutputStatement.close();
094: } catch (Throwable th) {
095: }
096: }
097: return result.toString().trim();
098: }
099:
100: public void close() {
101: try {
102: this .disable();
103: } catch (Throwable th) {
104: }
105: }
106:
107: protected void finalize() {
108: this.close();
109: }
110: }
|