<commit_before>/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

import org.postgresql.jdbc2.AbstractJdbc2Connection;

/**
 * Utility class for JDBC tests
 */
public class TestUtil
{
    /*
     * Returns the Test database JDBC URL
     */
    public static String getURL()
    {
        String protocolVersion = "";
        if (getProtocolVersion() != 0) {
            protocolVersion = "&protocolVersion=" + getProtocolVersion();
        }

        String binaryTransfer = "";
        if (getBinaryTransfer() != null && !getBinaryTransfer().equals("")) {
            binaryTransfer = "&binaryTransfer=" + getBinaryTransfer();
        }

		String receiveBufferSize = "";
		if (getReceiveBufferSize() != -1 ){
			receiveBufferSize = "&receiveBufferSize="+getReceiveBufferSize();
		}
		
		String sendBufferSize = "";
		if (getSendBufferSize() != -1 ){
			sendBufferSize = "&sendBufferSize="+getSendBufferSize();
		}
		
        return "jdbc:postgresql://"
                                + getServer() + ":" 
                                + getPort() + "/" 
                                + getDatabase() 
                                + "?prepareThreshold=" + getPrepareThreshold()
                                + "&loglevel=" + getLogLevel()
                                + protocolVersion
                                + binaryTransfer
								+ receiveBufferSize
								+ sendBufferSize;
    }

    /*
     * Returns the Test server
     */
    public static String getServer()
    {
        return System.getProperty("server", "localhost");
    }

    /*
     * Returns the Test port
     */
    public static int getPort()
    {
        return Integer.parseInt(System.getProperty("port", System.getProperty("def_pgport")));
    }

    /*
     * Returns the server side prepared statement threshold.
     */
    public static int getPrepareThreshold()
    {
        return Integer.parseInt(System.getProperty("preparethreshold", "5"));
    }

    public static int getProtocolVersion()
    {
        return Integer.parseInt(System.getProperty("protocolVersion", "0"));
    }

    /*
     * Returns the Test database
     */
    public static String getDatabase()
    {
        return System.getProperty("database");
    }

    /*
     * Returns the Postgresql username
     */
    public static String getUser()
    {
        return System.getProperty("username");
    }

    /*
     * Returns the user's password
     */
    public static String getPassword()
    {
        return System.getProperty("password");
    }

    /*
     * Returns the log level to use
     */
    public static int getLogLevel()
    {
        return Integer.parseInt(System.getProperty("loglevel", "0"));
    }

    // BEGIN_PGXC
    /*
     * Returns whether the tests are being run for XC
     */
    public static boolean isPGXC()
    {
        return System.getProperty("PGXC").equalsIgnoreCase("true");
    }
    // END_PGXC

    /*
     * Returns the binary transfer mode to use
     */
    public static String getBinaryTransfer()
    {
        return System.getProperty("binaryTransfer");
    }
	
	public static int getSendBufferSize()
	{
		return Integer.parseInt(System.getProperty("sendBufferSize", "-1"));
	}
	
	public static int getReceiveBufferSize()
	{
		return Integer.parseInt(System.getProperty("receiveBufferSize","-1"));
	}
	
    private static boolean initialized = false;
    public static void initDriver() throws Exception
    {
        synchronized (TestUtil.class) {
            if (initialized)
                return;

            Properties p = new Properties();
            try {
              p.load(new FileInputStream("build.properties"));
              p.load(new FileInputStream("build.local.properties"));
            } catch (IOException ex) {
              // ignore
            }
            p.putAll(System.getProperties());
            System.getProperties().putAll(p);

            if (getLogLevel() > 0) { 
                // Ant's junit task likes to buffer stdout/stderr and tends to run out of memory.
                // So we put debugging output to a file instead.
                java.io.Writer output = new java.io.FileWriter("postgresql-jdbc-tests.debug.txt", true);
                java.sql.DriverManager.setLogWriter(new java.io.PrintWriter(output,true));
            }
            
            org.postgresql.Driver.setLogLevel(getLogLevel()); // Also loads and registers driver.
            initialized = true;
        }
    }        

    /*
     * Helper - opens a connection.
     */
    public static java.sql.Connection openDB() throws Exception
    {
        return openDB(new Properties());
    }

    /*
     * Helper - opens a connection with the allowance for passing
     * additional parameters, like "compatible".
     */
    public static java.sql.Connection openDB(Properties props) throws Exception
    {
        initDriver();

        props.setProperty("user", getUser());
        props.setProperty("password", <PASSWORD>());

        return DriverManager.getConnection(getURL(), props);
    }

    /*
     * Helper - closes an open connection.
     */
    public static void closeDB(Connection con) throws SQLException
    {
        if (con != null)
            con.close();
    }

    /*
     * Helper - creates a test schema for use by a test
     */
    public static void createSchema(Connection con,
                                    String schema) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the schema
            dropSchema(con, schema);

            // Now create the schema
            String sql = "CREATE SCHEMA " + schema;

            st.executeUpdate(sql);
        }
        finally
        {
            st.close();
        }
    }

    /*
     * Helper - drops a schema
     */
    public static void dropSchema(Connection con, String schema) throws SQLException
    {
        Statement stmt = con.createStatement();
        try
        {
            String sql = "DROP SCHEMA " + schema;
            if (haveMinimumServerVersion(con, "7.3"))
            {
                sql += " CASCADE ";
            }
            stmt.executeUpdate(sql);
        }
        catch (SQLException ex)
        {
            // Since every create schema issues a drop schema
            // it's easy to get a schema doesn't exist error.
            // we want to ignore these, but if we're in a
            // transaction then we've got trouble
            if (!con.getAutoCommit())
                throw ex;
        }
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns) throws SQLException
    {
        // by default we don't request oids.
        createTable(con,table,columns,false);
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns,
                                   boolean withOids) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // Now create the table
            String sql = "CREATE TABLE " + table + " (" + columns + ") ";

            // Starting with 8.0 oids may be turned off by default.
            // Some tests need them, so they flag that here.
            if (withOids && haveMinimumServerVersion(con,"8.0")) {
                sql += " WITH OIDS";
            }
            st.executeUpdate(sql);
        }
        finally
        {
            st.close();
        }
    }

    /**
     * Helper creates a temporary table
     * @param con Connection
     * @param table String
     * @param columns String
     * @throws SQLException
     */

    public static void createTempTable( Connection con,
                                        String table,
                                        String columns) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // Now create the table
            st.executeUpdate("create temp table " + table + " (" + columns + ")");
        }
        finally
        {
            st.close();
        }
    }

    /**
     * Helper creates an enum type
     * @param con Connection
     * @param name String
     * @param values String
     * @throws SQLException
     */

    public static void createEnumType( Connection con,
                                       String name,
                                       String values) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            dropType(con, name);


            // Now create the table
            st.executeUpdate("create type " + name + " as enum (" + values + ")");
        }<commit_msg>2-Support for creating temp tables<commit_after>/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

import org.postgresql.jdbc2.AbstractJdbc2Connection;

/**
 * Utility class for JDBC tests
 */
public class TestUtil
{
    /*
     * Returns the Test database JDBC URL
     */
    public static String getURL()
    {
        String protocolVersion = "";
        if (getProtocolVersion() != 0) {
            protocolVersion = "&protocolVersion=" + getProtocolVersion();
        }

        String binaryTransfer = "";
        if (getBinaryTransfer() != null && !getBinaryTransfer().equals("")) {
            binaryTransfer = "&binaryTransfer=" + getBinaryTransfer();
        }

		String receiveBufferSize = "";
		if (getReceiveBufferSize() != -1 ){
			receiveBufferSize = "&receiveBufferSize="+getReceiveBufferSize();
		}
		
		String sendBufferSize = "";
		if (getSendBufferSize() != -1 ){
			sendBufferSize = "&sendBufferSize="+getSendBufferSize();
		}
		
        return "jdbc:postgresql://"
                                + getServer() + ":" 
                                + getPort() + "/" 
                                + getDatabase() 
                                + "?prepareThreshold=" + getPrepareThreshold()
                                + "&loglevel=" + getLogLevel()
                                + protocolVersion
                                + binaryTransfer
								+ receiveBufferSize
								+ sendBufferSize;
    }

    /*
     * Returns the Test server
     */
    public static String getServer()
    {
        return System.getProperty("server", "localhost");
    }

    /*
     * Returns the Test port
     */
    public static int getPort()
    {
        return Integer.parseInt(System.getProperty("port", System.getProperty("def_pgport")));
    }

    /*
     * Returns the server side prepared statement threshold.
     */
    public static int getPrepareThreshold()
    {
        return Integer.parseInt(System.getProperty("preparethreshold", "5"));
    }

    public static int getProtocolVersion()
    {
        return Integer.parseInt(System.getProperty("protocolVersion", "0"));
    }

    /*
     * Returns the Test database
     */
    public static String getDatabase()
    {
        return System.getProperty("database");
    }

    /*
     * Returns the Postgresql username
     */
    public static String getUser()
    {
        return System.getProperty("username");
    }

    /*
     * Returns the user's password
     */
    public static String getPassword()
    {
        return System.getProperty("password");
    }

    /*
     * Returns the log level to use
     */
    public static int getLogLevel()
    {
        return Integer.parseInt(System.getProperty("loglevel", "0"));
    }

    // BEGIN_PGXC
    /*
     * Returns whether the tests are being run for XC
     */
    public static boolean isPGXC()
    {
        return System.getProperty("PGXC").equalsIgnoreCase("true");
    }
    // END_PGXC

    /*
     * Returns the binary transfer mode to use
     */
    public static String getBinaryTransfer()
    {
        return System.getProperty("binaryTransfer");
    }
	
	public static int getSendBufferSize()
	{
		return Integer.parseInt(System.getProperty("sendBufferSize", "-1"));
	}
	
	public static int getReceiveBufferSize()
	{
		return Integer.parseInt(System.getProperty("receiveBufferSize","-1"));
	}
	
    private static boolean initialized = false;
    public static void initDriver() throws Exception
    {
        synchronized (TestUtil.class) {
            if (initialized)
                return;

            Properties p = new Properties();
            try {
              p.load(new FileInputStream("build.properties"));
              p.load(new FileInputStream("build.local.properties"));
            } catch (IOException ex) {
              // ignore
            }
            p.putAll(System.getProperties());
            System.getProperties().putAll(p);

            if (getLogLevel() > 0) { 
                // Ant's junit task likes to buffer stdout/stderr and tends to run out of memory.
                // So we put debugging output to a file instead.
                java.io.Writer output = new java.io.FileWriter("postgresql-jdbc-tests.debug.txt", true);
                java.sql.DriverManager.setLogWriter(new java.io.PrintWriter(output,true));
            }
            
            org.postgresql.Driver.setLogLevel(getLogLevel()); // Also loads and registers driver.
            initialized = true;
        }
    }        

    /*
     * Helper - opens a connection.
     */
    public static java.sql.Connection openDB() throws Exception
    {
        return openDB(new Properties());
    }

    /*
     * Helper - opens a connection with the allowance for passing
     * additional parameters, like "compatible".
     */
    public static java.sql.Connection openDB(Properties props) throws Exception
    {
        initDriver();

        props.setProperty("user", getUser());
        props.setProperty("password", getPassword());

        return DriverManager.getConnection(getURL(), props);
    }

    /*
     * Helper - closes an open connection.
     */
    public static void closeDB(Connection con) throws SQLException
    {
        if (con != null)
            con.close();
    }

    /*
     * Helper - creates a test schema for use by a test
     */
    public static void createSchema(Connection con,
                                    String schema) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the schema
            dropSchema(con, schema);

            // Now create the schema
            String sql = "CREATE SCHEMA " + schema;

            st.executeUpdate(sql);
        }
        finally
        {
            st.close();
        }
    }

    /*
     * Helper - drops a schema
     */
    public static void dropSchema(Connection con, String schema) throws SQLException
    {
        Statement stmt = con.createStatement();
        try
        {
            String sql = "DROP SCHEMA " + schema;
            if (haveMinimumServerVersion(con, "7.3"))
            {
                sql += " CASCADE ";
            }
            stmt.executeUpdate(sql);
        }
        catch (SQLException ex)
        {
            // Since every create schema issues a drop schema
            // it's easy to get a schema doesn't exist error.
            // we want to ignore these, but if we're in a
            // transaction then we've got trouble
            if (!con.getAutoCommit())
                throw ex;
        }
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns) throws SQLException
    {
        // by default we don't request oids.
        createTable(con,table,columns,false);
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns,
                                   boolean withOids) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // Now create the table
            String sql = "CREATE TABLE " + table + " (" + columns + ") ";

            // Starting with 8.0 oids may be turned off by default.
            // Some tests need them, so they flag that here.
            if (withOids && haveMinimumServerVersion(con,"8.0")) {
                sql += " WITH OIDS";
            }
            st.executeUpdate(sql);
        }
        finally
        {
            st.close();
        }
    }

    /**
     * Helper creates a temporary table
     * @param con Connection
     * @param table String
     * @param columns String
     * @throws SQLException
     */

    public static void createTempTable( Connection con,
                                        String table,
                                        String columns) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // BEGIN_PGXC
            // Enforce use of COMMIT instead of 2PC for temporary objects
            st.execute("SET enforce_two_phase_commit TO false");
            // END_PGXC

            // Now create the table
            st.executeUpdate("create temp table " + table + " (" + columns + ")");
        }
        finally
        {
            st.close();
        }
    }

    /**
     * Helper creates an enum type
     * @param con Connection
     * @param name String
     * @param values String
     * @throws SQLException
     */

    public static void createEnumType( Connection con,
                                       String name,
                                       String values) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            dropType(con, name);


            // Now create the table
            st.executeUpdate("create type " + name + " as enum (" + values + ")");
        }
{'fraction_non_alphanumeric': 0.11275948836234011, 'fraction_numerical': 0.002725938351855735, 'mean_word_length': 1.5157589344586575, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '344050', 'n_tokens_mistral': 4855, 'n_tokens_neox': 4643, 'n_words': 1298}