package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleConnection;
import org.utplsql.api.Version;
import org.utplsql.api.exception.InvalidVersionException;
import org.utplsql.api.reporter.Reporter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OutputBufferProvider {

    /** Returns an OutputBuffer compatible with the given databaseVersion
     * If we are at 3.1.0 or greater, returns an OutputBuffer based upon the information whether the Reporter has Output or not
     *
     * @param databaseVersion
     * @param reporter
     * @param conn
     * @return
     * @throws SQLException
     */
    public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Reporter reporter, Connection conn ) throws SQLException {
       OracleConnection oraConn = conn.unwrap(OracleConnection.class);

       try {
           if (databaseVersion.isGreaterOrEqualThan(new Version("3.1.0"))) {
               if ( hasOutput(reporter, oraConn) ) {
                   return new DefaultOutputBuffer(reporter);
               }
               else {
                   return new NonOutputBuffer(reporter);
               }
           }
       }
       catch ( InvalidVersionException ignored ) { }

       // If we couldn't find an appropriate OutputBuffer, return the Pre310-Compatibility-Buffer
       return new CompatibilityOutputBufferPre310(reporter);
    }

    private static boolean hasOutput( Reporter reporter, OracleConnection oraConn ) throws SQLException {

        String sql = "select is_output_reporter from table(ut_runner.get_reporters_list) where regexp_like(reporter_object_name, ?)";
        try ( PreparedStatement stmt = oraConn.prepareStatement(sql)) {
            stmt.setString(1, "[a-zA-Z0-9_]*\\.?" + reporter.getTypeName());

            try ( ResultSet rs = stmt.executeQuery() ) {
                if ( rs.next() ) {
                    String isReporterResult = rs.getString(1);

                    if ( isReporterResult == null )
                        throw new IllegalArgumentException("The given type " + reporter.getTypeName() + " is not a valid Reporter!");
                    else
                        return isReporterResult.equalsIgnoreCase("Y");
                }
                else
                    throw new SQLException("Could not check Reporter validity");
            }
        }
    }

    private OutputBufferProvider() {
    }
}

@@ -1618,16 +1618,38 @@
 reporter
+ %22 +%0A                %22
  from ta
@@ -1685,26 +1685,75 @@
 ist)
- where regexp_like
+%22 +%0A                %22 where ? = substr(reporter_object_name, length
 (rep
@@ -1773,11 +1773,13 @@
 name
-, ?
+)-?+1
 )%22;%0A
@@ -1874,39 +1874,106 @@
 .set
-String(1, %22%5Ba-zA-Z0-9_%5D*%5C%5C.?%22 +
+QueryTimeout(3);%0A            stmt.setString(1, reporter.getTypeName());%0A            stmt.setInt(2,
  rep
@@ -1991,16 +1991,25 @@
 peName()
+.length()
 );%0A%0A    

Change hasOutput-Query to not use Regexp. Added Statement Timeout