<commit_before>// Copyright (c) 2003  <NAME> <<EMAIL>>
//
// $Id: SQLRunner.java,v 1.3 2003/07/22 07:44:02 dustin Exp $

package net.spy.db;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

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

import net.spy.SpyObject;

/**
 * Run a SQL script from an InputStream.
 *
 * <p>
 *  SQL scripts run by this class are slightly special over normal SQL
 *  scripts in that they require a particular structure.
 * </p>
 * <p>
 *  Lines beginning with a SQL comment (--) are logged at info level.
 *  Queries are executed when a semicolon is encountered on a line all by
 *  itself.  Empty lines are ignored.  Everything else is concatenated to
 *  form the next query to execute.  Queries that return a result set will
 *  throw a SQLException.
 * </p>
 */
public class SQLRunner extends SpyObject {

	private Connection conn=null;
	private int timeout=0;

	/**
	 * Get an instance of SQLRunner.
	 */
	public SQLRunner(Connection conn) {
		super();
		this.conn=conn;
	}

	/** 
	 * Set the query timeout (in seconds).
	 */
	public void setTimeout(int timeout) {
		this.timeout=timeout;
	}

	/** 
	 * Run the given script as a transaction.
	 * 
	 * @param is the stream containing the script
	 * @throws SQLException if there's a SQL problem with this script
	 * @throws IOException if there's a problem reading the script
	 */
	public void runScript(InputStream is) throws SQLException, IOException {
		runScript(is, false, false);
	}

	/** 
	 * Run the given script.
	 * 
	 * @param is the stream containing the script
	 * @param autocommit if true, commit after each statement
	 * @param errok if true, ignore SQL exceptions on each statement
	 * @throws SQLException if there's a SQL problem executing the script
	 * @throws IOException if there's a problem reading the script
	 */
	public void runScript(InputStream is, boolean autocommit, boolean errok)
		throws SQLException, IOException {

		// Get a LineNumberReader from this stream so we can process it one
		// line at a time
		LineNumberReader lr=new LineNumberReader(new InputStreamReader(is));

		boolean successful=false;
		try {
			// Set the autocommit setting
			conn.setAutoCommit(autocommit);

			// Execute the script
			executeScript(lr, errok);

			// We're finished, commit
			if(!autocommit) {
				conn.commit();
			}
			successful=true;
		} finally {

			// Close the reader
			lr.close();

			// If we weren't successful, but we did at least get a
			// connection, clean it up.
			if(conn!=null) {
				// Stuff to do when we weren't successful
				if(!successful) {
					try {
						conn.rollback();
					} catch(SQLException e) {
						getLogger().warn("Error rolling back", e);
					}
				}
				// Reset the autocommit if it was set to false
				if(!autocommit) {
					try {
						conn.setAutoCommit(true);
					} catch(SQLException e) {
						getLogger().warn("Error resetting autocommit");
					}
				}
			} // successful check
		} // finally block
	}

	private void executeScript(LineNumberReader lr, boolean errok)
		throws SQLException, IOException {

		String curline=null;
		StringBuffer query=new StringBuffer(1024);

		while( (curline=lr.readLine()) != null) {
			curline=curline.trim();

			if(curline.equals(";")) {
				// Execute the current query

				Statement st=conn.createStatement();
				st.setQueryTimeout(timeout);
				int updated=0;
				long starttime=System.currentTimeMillis();
				try {
					updated=st.executeUpdate(query.toString());
				} catch(SQLException e) {
					if(errok) {
						// log the exception
						getLogger().warn("Ignorning problem executing "
							+ query, e);
					} else {
						throw e;
					}
				}
				long stoptime=System.currentTimeMillis();
				st.close();
				st=null;
				String rows=" rows";
				if(updated == 1) {
					rows=" row";
				}
				getLogger().info("Updated " + updated + rows
					+ " in " + (stoptime-starttime) + "ms");

				// Clear out the string buffer
				query.delete(0, query.length() + 1);
				// This is an assertion, but I want to remain < 1.4
				// compatible
				if(query.length() != 0) {
					throw new Error("Assertion failed:  query.length() == "
						+ query.length());
				}<commit_msg>Kamran didn't like the log messages saying ``updated.'' Keywords: cscvs:MAIN-244<commit_after>// Copyright (c) 2003  <NAME> <<EMAIL>>
//
// $Id: SQLRunner.java,v 1.4 2003/07/23 03:56:59 dustin Exp $

package net.spy.db;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

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

import net.spy.SpyObject;

/**
 * Run a SQL script from an InputStream.
 *
 * <p>
 *  SQL scripts run by this class are slightly special over normal SQL
 *  scripts in that they require a particular structure.
 * </p>
 * <p>
 *  Lines beginning with a SQL comment (--) are logged at info level.
 *  Queries are executed when a semicolon is encountered on a line all by
 *  itself.  Empty lines are ignored.  Everything else is concatenated to
 *  form the next query to execute.  Queries that return a result set will
 *  throw a SQLException.
 * </p>
 */
public class SQLRunner extends SpyObject {

	private Connection conn=null;
	private int timeout=0;

	/**
	 * Get an instance of SQLRunner.
	 */
	public SQLRunner(Connection conn) {
		super();
		this.conn=conn;
	}

	/** 
	 * Set the query timeout (in seconds).
	 */
	public void setTimeout(int timeout) {
		this.timeout=timeout;
	}

	/** 
	 * Run the given script as a transaction.
	 * 
	 * @param is the stream containing the script
	 * @throws SQLException if there's a SQL problem with this script
	 * @throws IOException if there's a problem reading the script
	 */
	public void runScript(InputStream is) throws SQLException, IOException {
		runScript(is, false, false);
	}

	/** 
	 * Run the given script.
	 * 
	 * @param is the stream containing the script
	 * @param autocommit if true, commit after each statement
	 * @param errok if true, ignore SQL exceptions on each statement
	 * @throws SQLException if there's a SQL problem executing the script
	 * @throws IOException if there's a problem reading the script
	 */
	public void runScript(InputStream is, boolean autocommit, boolean errok)
		throws SQLException, IOException {

		// Get a LineNumberReader from this stream so we can process it one
		// line at a time
		LineNumberReader lr=new LineNumberReader(new InputStreamReader(is));

		boolean successful=false;
		try {
			// Set the autocommit setting
			conn.setAutoCommit(autocommit);

			// Execute the script
			executeScript(lr, errok);

			// We're finished, commit
			if(!autocommit) {
				conn.commit();
			}
			successful=true;
		} finally {

			// Close the reader
			lr.close();

			// If we weren't successful, but we did at least get a
			// connection, clean it up.
			if(conn!=null) {
				// Stuff to do when we weren't successful
				if(!successful) {
					try {
						conn.rollback();
					} catch(SQLException e) {
						getLogger().warn("Error rolling back", e);
					}
				}
				// Reset the autocommit if it was set to false
				if(!autocommit) {
					try {
						conn.setAutoCommit(true);
					} catch(SQLException e) {
						getLogger().warn("Error resetting autocommit");
					}
				}
			} // successful check
		} // finally block
	}

	private void executeScript(LineNumberReader lr, boolean errok)
		throws SQLException, IOException {

		String curline=null;
		StringBuffer query=new StringBuffer(1024);

		while( (curline=lr.readLine()) != null) {
			curline=curline.trim();

			if(curline.equals(";")) {
				// Execute the current query

				Statement st=conn.createStatement();
				st.setQueryTimeout(timeout);
				int affected=0;
				long starttime=System.currentTimeMillis();
				try {
					affected=st.executeUpdate(query.toString());
				} catch(SQLException e) {
					if(errok) {
						// log the exception
						getLogger().warn("Ignorning problem executing "
							+ query, e);
					} else {
						throw e;
					}
				}
				long stoptime=System.currentTimeMillis();
				st.close();
				st=null;
				String rows=" rows";
				if(affected == 1) {
					rows=" row";
				}
				getLogger().info("Affected " + affected + rows
					+ " in " + (stoptime-starttime) + "ms");

				// Clear out the string buffer
				query.delete(0, query.length() + 1);
				// This is an assertion, but I want to remain < 1.4
				// compatible
				if(query.length() != 0) {
					throw new Error("Assertion failed:  query.length() == "
						+ query.length());
				}
{'fraction_non_alphanumeric': 0.11135271229114214, 'fraction_numerical': 0.007667658503089952, 'mean_word_length': 2.9940585009140768, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '7627312', 'n_tokens_mistral': 3189, 'n_tokens_neox': 2597, 'n_words': 879}