<commit_before>/*--
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package solidbase.core.plugins;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import solidbase.core.Command;
import solidbase.core.CommandListener;
import solidbase.core.CommandProcessor;
import solidbase.core.Database;


/**
 * This plugin will print the results from the SELECT to the console.
 *
 * <blockquote><pre>
 * PRINT SELECT 'Inserted ' || COUNT(*) || ' records in ATABLE.'
 * FROM ATABLE
 * GO
 * </pre></blockquote>
 *
 * @author <NAME>
 * @since May 2010
 */
public class PrintSelect implements CommandListener
{
	static private final Pattern printSelectPattern = Pattern.compile( "PRINT\\s+(SELECT\\s+.+)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE );

	//@Override
	public boolean execute( CommandProcessor processor, Command command ) throws SQLException
	{
		if( command.isTransient() )
			return false;

		Matcher matcher = printSelectPattern.matcher( command.getCommand() );
		if( !matcher.matches() )
			return false;

		String sql = matcher.group( 1 );

		Database database = processor.getCurrentDatabase();
		Connection connection = database.getConnection();
		Statement statement = processor.createStatement( connection );
		try
		{
			ResultSet result = statement.executeQuery( sql );
			while( result.next() )
			{
				Object object = result.getObject( 1 );
				processor.getCallBack().print( object.toString() );
			}
		}
		finally
		{
			if( statement != null )
				statement.close();
			// TODO This is not right, we should check autocommit
			connection.commit();
		}

		return true;
	}

	//@Override
	public void terminate()
	{
		// Nothing to clean up
	}
}
<commit_msg>The PRINT SELECT plugins now also prints CLOBs and LONGVARCHARs correctly.<commit_after>/*--
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package solidbase.core.plugins;

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

import solidbase.core.Command;
import solidbase.core.CommandListener;
import solidbase.core.CommandProcessor;
import solidbase.core.Database;


/**
 * This plugin will print the results from the SELECT to the console.
 *
 * <blockquote><pre>
 * PRINT SELECT 'Inserted ' || COUNT(*) || ' records in ATABLE.'
 * FROM ATABLE
 * GO
 * </pre></blockquote>
 *
 * @author <NAME>
 * @since May 2010
 */
public class PrintSelect implements CommandListener
{
	static private final Pattern printSelectPattern = Pattern.compile( "PRINT\\s+(SELECT\\s+.+)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE );

	//@Override
	public boolean execute( CommandProcessor processor, Command command ) throws SQLException
	{
		if( command.isTransient() )
			return false;

		Matcher matcher = printSelectPattern.matcher( command.getCommand() );
		if( !matcher.matches() )
			return false;

		String sql = matcher.group( 1 );

		Database database = processor.getCurrentDatabase();
		Connection connection = database.getConnection();
		Statement statement = processor.createStatement( connection );
		try
		{
			ResultSet result = statement.executeQuery( sql );
			ResultSetMetaData metaData = result.getMetaData();
			int type = metaData.getColumnType( 1 );
			if( type == Types.CLOB || type == Types.NCLOB || type == Types.LONGVARCHAR || type == Types.LONGNVARCHAR )
			{
				StringBuilder buffer = new StringBuilder();
				char[] buf = new char[ 4096 ];
				while( result.next() )
				{
					Reader in = result.getCharacterStream( 1 );
					buffer.setLength( 0 );
					try
					{
						// TODO Can we do this in a streaming way? Maybe add streaming capability indicator to the output.
						for( int read = in.read( buf ); read >= 0; read = in.read( buf ) )
							buffer.append( buf, 0, read );
					}
					catch( IOException e )
					{
						throw new SQLException( e );
					}
					processor.getCallBack().print( buffer.toString() );
				}
			}
			else
			{
				while( result.next() )
					processor.getCallBack().print( result.getObject( 1 ).toString() );
			}
		}
		finally
		{
			if( statement != null )
				statement.close();
			// TODO This is not right, we should check autocommit
			connection.commit();
		}

		return true;
	}

	//@Override
	public void terminate()
	{
		// Nothing to clean up
	}
}

{'fraction_non_alphanumeric': 0.10607621009268794, 'fraction_numerical': 0.006350841057329214, 'mean_word_length': 3.0776766969909026, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5649360', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1608, 'n_words': 533}