<commit_before>package com.impossibl.postgres.jdbc;

import static com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException;
import static com.impossibl.postgres.jdbc.ErrorUtils.makeSQLWarningChain;
import static com.impossibl.postgres.jdbc.Exceptions.INVALID_COMMAND_FOR_GENERATED_KEYS;
import static com.impossibl.postgres.jdbc.Exceptions.NOT_IMPLEMENTED;
import static com.impossibl.postgres.jdbc.Exceptions.NOT_SUPPORTED;
import static com.impossibl.postgres.jdbc.SQLTextUtils.appendReturningClause;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getBeginText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getCommitText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getGetSessionIsolationLevelText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getGetSessionReadabilityText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getIsolationLevel;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getProtocolSQLText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getReleaseSavepointText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getRollbackText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getRollbackToText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSavepointText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSessionIsolationLevelText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSessionReadabilityText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.isTrue;
import static com.impossibl.postgres.protocol.TransactionStatus.Active;
import static com.impossibl.postgres.protocol.TransactionStatus.Idle;
import static java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;
import static java.sql.Statement.RETURN_GENERATED_KEYS;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableMap;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.SocketAddress;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import com.impossibl.postgres.protocol.Command;
import com.impossibl.postgres.protocol.PrepareCommand;
import com.impossibl.postgres.system.BasicContext;
import com.impossibl.postgres.system.NoticeException;
import com.impossibl.postgres.types.Type;



class PGConnection extends BasicContext implements Connection {

	
	
	long statementId = 0l;
	long portalId = 0l;
	int savepointId;
	private int holdability;
	boolean autoCommit = true;
	boolean readOnly = false;
	int networkTimeout;
	SQLWarning warningChain;
	List<WeakReference<PGStatement>> activeStatements;

	
	
	PGConnection(SocketAddress address, Properties settings, Map<String, Class<?>> targetTypeMap) throws IOException {
		super(address, settings, targetTypeMap);
		activeStatements = new ArrayList<>();
	}
	
	protected void finalize() throws SQLException {
		close();
	}

	/**
	 * Ensure the connection is not closed
	 * 
	 * @throws SQLException
	 * 					If the connection is closed
	 */
	void checkClosed() throws SQLException {

		if(isClosed())
			throw new SQLException("connection closed");
	}

	/**
	 * Ensures the connection is currently in manual-commit mode
	 * 
	 * @throws SQLException
	 * 					If the connection is not in manual-commit mode
	 */
	void checkManualCommit() throws SQLException {

		if(autoCommit != false)
			throw new SQLException("must not be in auto-commit mode");
	}

	/**
	 * Ensures the connection is currently in auto-commit mode
	 * 
	 * @throws SQLException
	 * 					IF the connection is not in auto-commit mode
	 */
	void checkAutoCommit() throws SQLException {

		if(autoCommit != false)
			throw new SQLException("must be in auto-commit mode");
	}

	/**
	 * Generates and returns the next unique statement name for this connection
	 * 
	 * @return New unique statement name
	 */
	String getNextStatementName() {
		return String.format("%016X", ++statementId);
	}

	/**
	 * Generates and returns the next unique portal name for this connection
	 * 
	 * @return New unique portal name
	 */
	String getNextPortalName() {
		return String.format("%016X", ++portalId);
	}

	/**
	 * Called by statements to notify the connection of their closure
	 * 
	 * @param statement
	 */
	void handleStatementClosure(PGStatement statement) {

		Iterator<WeakReference<PGStatement>> stmtRefIter = activeStatements.iterator();
		while(stmtRefIter.hasNext()) {
			
			PGStatement stmt = stmtRefIter.next().get();
			if(stmt == null || stmt == statement) {
				
				stmtRefIter.remove();
			}
			
		}
		
	}

	/**
	 * Closes all active statements for this connection
	 * 
	 * @throws SQLException
	 */
	void closeStatements() throws SQLException {

		for(WeakReference<PGStatement> stmtRef : activeStatements) {

			if(stmtRef.get() != null)
				stmtRef.get().internalClose();
		}
	}

	/**
	 * Executes the given command and throws a SQLException if an error was
	 * encountered and returns a chain of SQLWarnings if any were generated.
	 * 
	 * @param cmd
	 *          Command to execute
	 * @return Chain of SQLWarning objects if any were encountered
	 * @throws SQLException
	 *           If an error was encountered
	 */
	SQLWarning execute(Command cmd) throws SQLException {

		try {

			protocol.execute(cmd);

			if(cmd.getError() != null) {
<commit_msg>Fix potential NullPtr by only referencing Reference.get() once<commit_after>package com.impossibl.postgres.jdbc;

import static com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException;
import static com.impossibl.postgres.jdbc.ErrorUtils.makeSQLWarningChain;
import static com.impossibl.postgres.jdbc.Exceptions.INVALID_COMMAND_FOR_GENERATED_KEYS;
import static com.impossibl.postgres.jdbc.Exceptions.NOT_IMPLEMENTED;
import static com.impossibl.postgres.jdbc.Exceptions.NOT_SUPPORTED;
import static com.impossibl.postgres.jdbc.SQLTextUtils.appendReturningClause;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getBeginText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getCommitText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getGetSessionIsolationLevelText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getGetSessionReadabilityText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getIsolationLevel;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getProtocolSQLText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getReleaseSavepointText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getRollbackText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getRollbackToText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSavepointText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSessionIsolationLevelText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.getSetSessionReadabilityText;
import static com.impossibl.postgres.jdbc.SQLTextUtils.isTrue;
import static com.impossibl.postgres.protocol.TransactionStatus.Active;
import static com.impossibl.postgres.protocol.TransactionStatus.Idle;
import static java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;
import static java.sql.Statement.RETURN_GENERATED_KEYS;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableMap;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.SocketAddress;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import com.impossibl.postgres.protocol.Command;
import com.impossibl.postgres.protocol.PrepareCommand;
import com.impossibl.postgres.system.BasicContext;
import com.impossibl.postgres.system.NoticeException;
import com.impossibl.postgres.types.Type;



class PGConnection extends BasicContext implements Connection {

	
	
	long statementId = 0l;
	long portalId = 0l;
	int savepointId;
	private int holdability;
	boolean autoCommit = true;
	boolean readOnly = false;
	int networkTimeout;
	SQLWarning warningChain;
	List<WeakReference<PGStatement>> activeStatements;

	
	
	PGConnection(SocketAddress address, Properties settings, Map<String, Class<?>> targetTypeMap) throws IOException {
		super(address, settings, targetTypeMap);
		activeStatements = new ArrayList<>();
	}
	
	protected void finalize() throws SQLException {
		close();
	}

	/**
	 * Ensure the connection is not closed
	 * 
	 * @throws SQLException
	 * 					If the connection is closed
	 */
	void checkClosed() throws SQLException {

		if(isClosed())
			throw new SQLException("connection closed");
	}

	/**
	 * Ensures the connection is currently in manual-commit mode
	 * 
	 * @throws SQLException
	 * 					If the connection is not in manual-commit mode
	 */
	void checkManualCommit() throws SQLException {

		if(autoCommit != false)
			throw new SQLException("must not be in auto-commit mode");
	}

	/**
	 * Ensures the connection is currently in auto-commit mode
	 * 
	 * @throws SQLException
	 * 					IF the connection is not in auto-commit mode
	 */
	void checkAutoCommit() throws SQLException {

		if(autoCommit != false)
			throw new SQLException("must be in auto-commit mode");
	}

	/**
	 * Generates and returns the next unique statement name for this connection
	 * 
	 * @return New unique statement name
	 */
	String getNextStatementName() {
		return String.format("%016X", ++statementId);
	}

	/**
	 * Generates and returns the next unique portal name for this connection
	 * 
	 * @return New unique portal name
	 */
	String getNextPortalName() {
		return String.format("%016X", ++portalId);
	}

	/**
	 * Called by statements to notify the connection of their closure
	 * 
	 * @param statement
	 */
	void handleStatementClosure(PGStatement statement) {

		Iterator<WeakReference<PGStatement>> stmtRefIter = activeStatements.iterator();
		while(stmtRefIter.hasNext()) {
			
			PGStatement stmt = stmtRefIter.next().get();
			if(stmt == null || stmt == statement) {
				
				stmtRefIter.remove();
			}
			
		}
		
	}

	/**
	 * Closes all active statements for this connection
	 * 
	 * @throws SQLException
	 */
	void closeStatements() throws SQLException {

		for(WeakReference<PGStatement> stmtRef : activeStatements) {

			PGStatement statement = stmtRef.get();
			if(statement != null)
				statement.internalClose();
		}
	}

	/**
	 * Executes the given command and throws a SQLException if an error was
	 * encountered and returns a chain of SQLWarnings if any were generated.
	 * 
	 * @param cmd
	 *          Command to execute
	 * @return Chain of SQLWarning objects if any were encountered
	 * @throws SQLException
	 *           If an error was encountered
	 */
	SQLWarning execute(Command cmd) throws SQLException {

		try {

			protocol.execute(cmd);

			if(cmd.getError() != null) {

{'fraction_non_alphanumeric': 0.09196555471950506, 'fraction_numerical': 0.0013376807959200735, 'mean_word_length': 4.826595226497808, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1789853', 'n_tokens_mistral': 3868, 'n_tokens_neox': 3546, 'n_words': 852}