<commit_before>/*
	Title:  SQL
	Author: <NAME>
	Date:   11/05/2014
	
	This class is merely a collection of useful static methods that support code recycling. Specifically, this
	class offers methods and classes which provide a means to execute and process SQL queries.
*/

package api.util.networking;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;

public final class SQL
{
	public static class Script
	{
		private Connection	connection			= null;
		private String		delimiter			= ";";
		private boolean		isDebugging			= false;
		private boolean		isFullLineDelimiter	= false;
		
		public Script(final Connection connection, final boolean isDebugging)
		{
			this.setConnection(connection);
		}
		
		public Script(final Connection connection, final boolean isDebugging, final Reader reader) throws IOException, SQLException
		{
			this(connection, isDebugging);
			
			try
			{
				this.execute(reader);
			}
			catch (final IOException exception)
			{
				throw exception;
			}
			catch (final SQLException exception)
			{
				throw exception;
			}
		}
		
		private void execute(final Reader reader) throws IOException, SQLException
		{
			StringBuffer query = null;
			
			try
			{
				LineNumberReader	lineReader	= new LineNumberReader(reader);
				String				line		= null;
				
				while ((line = lineReader.readLine()) != null)
				{
					if (query == null)
					{
						query = new StringBuffer();
					}
					
					//String trimmedLine = line.trim();
					String trimmedLine = line.trim().replaceAll(";$", Matcher.quoteReplacement("; \\"));
					
					if (trimmedLine.startsWith("--"))
					{
						if (this.isDebugging())
						{
							System.out.println(trimmedLine);
						}
					}
					else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//"))
					{
						// Do nothing.
					}
					else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--"))
					{
						// Do nothing.
					}
					else if (!this.isFullLineDelimiter() && trimmedLine.endsWith(this.getDelimiter())
						|| this.isFullLineDelimiter() && trimmedLine.equals(this.getDelimiter()))
					{
						query.append(line.substring(0, line.lastIndexOf(this.getDelimiter())));
						query.append(" ");
						
						ResultSet results = SQL.executeQuery(this.getConnection(), query.toString());
						
						if (this.isDebugging() && results != null)
						{
							System.out.println(query);
							ResultSetMetaData meta = results.getMetaData();
							int cols = meta.getColumnCount();
							
							for (int i = 1; i <= cols; i++)
							{
								String name = meta.getColumnLabel(i);
								System.out.print(name + "\t");
							}
							
							System.out.println("");
							
							while (results.next())
							{
								for (int i = 1; i <= cols; i++)
								{
									String value = results.getString(i);
									System.out.print(value + "\t");
								}
								
								System.out.println("");
							}
						}
						
						query = null;
					}
					else
					{
						query.append(line);
						query.append(" ");
					}
				}
				if (this.getConnection().getAutoCommit())
				{
					this.getConnection().commit();
				}
			}
			catch (final SQLException exception)
			{
				exception.fillInStackTrace();
				throw exception;
			}
			catch (final IOException exception)
			{
				exception.fillInStackTrace();
				throw exception;
			}
			finally
			{
				this.getConnection().rollback();
			}
		}
		
		public final Connection getConnection()
		{
			return this.connection;
		}
		
		public final String getDelimiter()
		{
			return this.delimiter;
		}
		
		public final boolean isDebugging()
		{
			return this.isDebugging;
		}
		
		public final boolean isFullLineDelimiter()
		{
			return this.isFullLineDelimiter;
		}
		
		protected final void setConnection(final Connection connection)
		{
			this.connection = connection;
		}
		
		protected final void setDebugging(final boolean isDebugging)
		{
			this.isDebugging = isDebugging;
		}
		
		protected final void setDelimiter(final String delimiter)
		{
			this.delimiter = delimiter;
		}
		
		protected final void setFullLineDelimiter(final boolean isFullLineDelimiter)
		{
			this.isFullLineDelimiter = isFullLineDelimiter;
		}
	}
	
	public static Connection createConnection(final String server, final String username, final String password)
		throws SQLException
	{
		Connection connection = null;
		
		/*
		*	NOTE: You must include the Oracle JDBC driver JAR on the build path in order to get the following code to work!!!  
		*/
		
		DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		connection = DriverManager.getConnection("jdbc:oracle:thin:@" + server, username, password);
		
		return connection;
	}
	
	public static ResultSet executeQuery(final Connection connection, final String query)
		throws SQLException
	{
		Statement statement = connection.createStatement();
		ResultSet results	= null;
		
		try
		{
			if (statement.execute(query))
			{
				results = statement.getResultSet();
			}
			
			if (connection.getAutoCommit())
			{
				connection.commit();
			}
		}
		catch (final SQLException exception)
		{
			exception.fillInStackTrace();
			throw exception;
		}
		finally
		{
			if (statement != null)
			{
				try
				{
					statement.close();
				}
				catch (final Exception exception)
				{
					// Ignore to workaround a bug in Jakarta DBCP.
				}
			}
			
			connection.rollback();
		}
		
		Thread.yield();
		return results;
	}
}<commit_msg>Fixed bug in SQL, expanded code comments.<commit_after>/*
	Title:  SQL
	Author: <NAME>
	Date:   11/05/2014
	
	This class is merely a collection of useful static methods that support code recycling. Specifically, this
	class offers methods and classes which provide a means to execute and process SQL queries.
*/

package api.util.networking;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;

public final class SQL
{
	public static class Script
	{
		private Connection	connection			= null;
		private String		delimiter			= ";";
		private boolean		isDebugging			= false;
		private boolean		isFullLineDelimiter	= false;
		
		public Script(final Connection connection, final boolean isDebugging)
		{
			this.setConnection(connection);
			this.setDebugging(isDebugging);
		}
		
		public Script(final Connection connection, final boolean isDebugging, final Reader reader) throws IOException, SQLException
		{
			this(connection, isDebugging);
			
			try
			{
				this.execute(reader);
			}
			catch (final IOException exception)
			{
				throw exception;
			}
			catch (final SQLException exception)
			{
				throw exception;
			}
		}
		
		private void execute(final Reader reader) throws IOException, SQLException
		{
			StringBuffer query = null;
			
			try
			{
				LineNumberReader	lineReader	= new LineNumberReader(reader);
				String				line		= null;
				
				while ((line = lineReader.readLine()) != null)
				{
					if (query == null)
					{
						query = new StringBuffer();
					}
					
					/*
					*	String trimmedLine = line.trim();
					*	Leads to [http://stackoverflow.com/questions/3499483/error-unterminated-quoted-string-at-or-near]
					*/
					
					String trimmedLine = line.trim().replaceAll(";$", Matcher.quoteReplacement("; \\"));
					
					if (trimmedLine.startsWith("--"))
					{
						if (this.isDebugging())
						{
							System.out.println(trimmedLine);
						}
					}
					else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//"))
					{
						// Do nothing.
					}
					else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--"))
					{
						// Do nothing.
					}
					else if (!this.isFullLineDelimiter() && trimmedLine.endsWith(this.getDelimiter())
						|| this.isFullLineDelimiter() && trimmedLine.equals(this.getDelimiter()))
					{
						query.append(line.substring(0, line.lastIndexOf(this.getDelimiter())));
						query.append(" ");
						
						ResultSet results = SQL.executeQuery(this.getConnection(), query.toString());
						
						if (this.isDebugging() && results != null)
						{
							System.out.println(query);
							ResultSetMetaData meta = results.getMetaData();
							int cols = meta.getColumnCount();
							
							for (int i = 1; i <= cols; i++)
							{
								String colName = meta.getColumnLabel(i);
								System.out.print(colName + "\t");
							}
							
							System.out.println("");
							
							while (results.next())
							{
								for (int i = 1; i <= cols; i++)
								{
									String colValue = results.getString(i);
									System.out.print(colValue + "\t");
								}
								
								System.out.println("");
							}
						}
						
						query = null;
					}
					else
					{
						query.append(line);
						query.append(" ");
					}
				}
				if (this.getConnection().getAutoCommit())
				{
					this.getConnection().commit();
				}
			}
			catch (final SQLException exception)
			{
				exception.fillInStackTrace();
				throw exception;
			}
			catch (final IOException exception)
			{
				exception.fillInStackTrace();
				throw exception;
			}
			finally
			{
				this.getConnection().rollback();
			}
		}
		
		public final Connection getConnection()
		{
			return this.connection;
		}
		
		public final String getDelimiter()
		{
			return this.delimiter;
		}
		
		public final boolean isDebugging()
		{
			return this.isDebugging;
		}
		
		public final boolean isFullLineDelimiter()
		{
			return this.isFullLineDelimiter;
		}
		
		protected final void setConnection(final Connection connection)
		{
			this.connection = connection;
		}
		
		protected final void setDebugging(final boolean isDebugging)
		{
			this.isDebugging = isDebugging;
		}
		
		protected final void setDelimiter(final String delimiter)
		{
			this.delimiter = delimiter;
		}
		
		protected final void setFullLineDelimiter(final boolean isFullLineDelimiter)
		{
			this.isFullLineDelimiter = isFullLineDelimiter;
		}
	}
	
	public static Connection createConnection(final String server, final String username, final String password)
		throws SQLException
	{
		Connection connection = null;
		
		/*
		*	NOTE: You must include the Oracle JDBC driver JAR on the build path in order to get the following code to work!!!
		*	http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
		*/
		
		DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		connection = DriverManager.getConnection("jdbc:oracle:thin:@" + server, username, password);
		
		return connection;
	}
	
	public static ResultSet executeQuery(final Connection connection, final String query)
		throws SQLException
	{
		Statement statement = connection.createStatement();
		ResultSet results	= null;
		
		try
		{
			if (statement.execute(query))
			{
				results = statement.getResultSet();
			}
			
			if (connection.getAutoCommit())
			{
				connection.commit();
			}
		}
		catch (final SQLException exception)
		{
			exception.fillInStackTrace();
			throw exception;
		}
		finally
		{
			if (statement != null)
			{
				try
				{
					statement.close();
				}
				catch (final Exception exception)
				{
					// Ignore to workaround a bug in Jakarta DBCP.
				}
			}
			
			connection.rollback();
		}
		
		Thread.yield();
		return results;
	}
}
{'fraction_non_alphanumeric': 0.1089032258064516, 'fraction_numerical': 0.0033548387096774194, 'mean_word_length': 2.6628859483301826, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '787951', 'n_tokens_mistral': 4744, 'n_tokens_neox': 3408, 'n_words': 829}